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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
d35011a9fa05ac4798b278823cb0e174690ba0cf | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/ring_theory/witt_vector/basic.lean | dcb9ba16ff9897fc21d8899bd562a6198bb68938 | [
"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 | 11,412 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Robert Y. Lewis
-/
import data.mv_polynomial.counit
import data.mv_polynomial.invertible
import ring_theory.witt_vector.defs
/-!
# Witt vectors
This file verifies that the ring operations on `witt_vector p R`
satisfy the axioms of a commutative ring.
## Main definitions
* `witt_vector.map`: lifts a ring homomorphism `R →+* S` to a ring homomorphism `𝕎 R →+* 𝕎 S`.
* `witt_vector.ghost_component n x`: evaluates the `n`th Witt polynomial
on the first `n` coefficients of `x`, producing a value in `R`.
This is a ring homomorphism.
* `witt_vector.ghost_map`: a ring homomorphism `𝕎 R →+* (ℕ → R)`, obtained by packaging
all the ghost components together.
If `p` is invertible in `R`, then the ghost map is an equivalence,
which we use to define the ring operations on `𝕎 R`.
* `witt_vector.comm_ring`: the ring structure induced by the ghost components.
## Notation
We use notation `𝕎 R`, entered `\bbW`, for the Witt vectors over `R`.
## Implementation details
As we prove that the ghost components respect the ring operations, we face a number of repetitive
proofs. To avoid duplicating code we factor these proofs into a custom tactic, only slightly more
powerful than a tactic macro. This tactic is not particularly useful outside of its applications
in this file.
## References
* [Hazewinkel, *Witt Vectors*][Haze09]
* [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21]
-/
noncomputable theory
open mv_polynomial function
open_locale big_operators
variables {p : ℕ} {R S T : Type*} [hp : fact p.prime] [comm_ring R] [comm_ring S] [comm_ring T]
variables {α : Type*} {β : Type*}
local notation `𝕎` := witt_vector p -- type as `\bbW`
open_locale witt
namespace witt_vector
/-- `f : α → β` induces a map from `𝕎 α` to `𝕎 β` by applying `f` componentwise.
If `f` is a ring homomorphism, then so is `f`, see `witt_vector.map f`. -/
def map_fun (f : α → β) : 𝕎 α → 𝕎 β :=
λ x, mk _ (f ∘ x.coeff)
namespace map_fun
lemma injective (f : α → β) (hf : injective f) : injective (map_fun f : 𝕎 α → 𝕎 β) :=
λ x y h, ext $ λ n, hf (congr_arg (λ x, coeff x n) h : _)
lemma surjective (f : α → β) (hf : surjective f) : surjective (map_fun f : 𝕎 α → 𝕎 β) :=
λ x, ⟨mk _ (λ n, classical.some $ hf $ x.coeff n),
by { ext n, dsimp [map_fun], rw classical.some_spec (hf (x.coeff n)) }⟩
variables (f : R →+* S) (x y : 𝕎 R)
/-- Auxiliary tactic for showing that `map_fun` respects the ring operations. -/
meta def map_fun_tac : tactic unit :=
`[ext n,
show f (aeval _ _) = aeval _ _,
rw map_aeval,
apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl,
ext ⟨i, k⟩,
fin_cases i; refl]
include hp
/- We do not tag these lemmas as `@[simp]` because they will be bundled in `map` later on. -/
lemma zero : map_fun f (0 : 𝕎 R) = 0 := by map_fun_tac
lemma one : map_fun f (1 : 𝕎 R) = 1 := by map_fun_tac
lemma add : map_fun f (x + y) = map_fun f x + map_fun f y := by map_fun_tac
lemma sub : map_fun f (x - y) = map_fun f x - map_fun f y := by map_fun_tac
lemma mul : map_fun f (x * y) = map_fun f x * map_fun f y := by map_fun_tac
lemma neg : map_fun f (-x) = -map_fun f x := by map_fun_tac
lemma nsmul (n : ℕ) : map_fun f (n • x) = n • map_fun f x := by map_fun_tac
lemma zsmul (z : ℤ) : map_fun f (z • x) = z • map_fun f x := by map_fun_tac
lemma pow (n : ℕ) : map_fun f (x^ n) = map_fun f x ^ n := by map_fun_tac
lemma nat_cast (n : ℕ) : map_fun f (n : 𝕎 R) = n :=
show map_fun f n.unary_cast = coe n,
by induction n; simp [*, nat.unary_cast, add, one, zero]; refl
lemma int_cast (n : ℤ) : map_fun f (n : 𝕎 R) = n :=
show map_fun f n.cast_def = coe n,
by cases n; simp [*, int.cast_def, add, one, neg, zero, nat_cast]; refl
end map_fun
end witt_vector
section tactic
setup_tactic_parser
open tactic
/-- An auxiliary tactic for proving that `ghost_fun` respects the ring operations. -/
meta def tactic.interactive.ghost_fun_tac (φ fn : parse parser.pexpr) : tactic unit := do
fn ← to_expr ```(%%fn : fin _ → ℕ → R),
`(fin %%k → _ → _) ← infer_type fn,
`[ext n],
`[dunfold
witt_vector.has_zero witt_zero
witt_vector.has_one witt_one
witt_vector.has_neg witt_neg
witt_vector.has_mul witt_mul
witt_vector.has_sub witt_sub
witt_vector.has_add witt_add
witt_vector.has_nat_scalar witt_nsmul
witt_vector.has_int_scalar witt_zsmul
witt_vector.has_nat_pow witt_pow
],
to_expr ```(congr_fun (congr_arg (@peval R _ %%k) (witt_structure_int_prop p %%φ n)) %%fn) >>=
note `this none,
`[simpa [ghost_fun, aeval_rename, aeval_bind₁, (∘), uncurry, peval, eval] using this]
end tactic
namespace witt_vector
/-- Evaluates the `n`th Witt polynomial on the first `n` coefficients of `x`,
producing a value in `R`.
This function will be bundled as the ring homomorphism `witt_vector.ghost_map`
once the ring structure is available,
but we rely on it to set up the ring structure in the first place. -/
private def ghost_fun : 𝕎 R → (ℕ → R) := λ x n, aeval x.coeff (W_ ℤ n)
section ghost_fun
include hp
/- The following lemmas are not `@[simp]` because they will be bundled in `ghost_map` later on. -/
variables (x y : 𝕎 R)
omit hp
local attribute [simp]
lemma matrix_vec_empty_coeff {R} (i j) :
@coeff p R (matrix.vec_empty i) j = (matrix.vec_empty i : ℕ → R) j :=
by rcases i with ⟨_ | _ | _ | _ | i_val, ⟨⟩⟩
include hp
private lemma ghost_fun_zero : ghost_fun (0 : 𝕎 R) = 0 := by ghost_fun_tac 0 ![]
private lemma ghost_fun_one : ghost_fun (1 : 𝕎 R) = 1 := by ghost_fun_tac 1 ![]
private lemma ghost_fun_add : ghost_fun (x + y) = ghost_fun x + ghost_fun y :=
by ghost_fun_tac (X 0 + X 1) ![x.coeff, y.coeff]
private lemma ghost_fun_nat_cast (i : ℕ) : ghost_fun (i : 𝕎 R) = i :=
show ghost_fun i.unary_cast = _,
by induction i; simp [*, nat.unary_cast, ghost_fun_zero, ghost_fun_one, ghost_fun_add, -pi.coe_nat]
private lemma ghost_fun_sub : ghost_fun (x - y) = ghost_fun x - ghost_fun y :=
by ghost_fun_tac (X 0 - X 1) ![x.coeff, y.coeff]
private lemma ghost_fun_mul : ghost_fun (x * y) = ghost_fun x * ghost_fun y :=
by ghost_fun_tac (X 0 * X 1) ![x.coeff, y.coeff]
private lemma ghost_fun_neg : ghost_fun (-x) = - ghost_fun x :=
by ghost_fun_tac (-X 0) ![x.coeff]
private lemma ghost_fun_int_cast (i : ℤ) : ghost_fun (i : 𝕎 R) = i :=
show ghost_fun i.cast_def = _,
by cases i; simp [*, int.cast_def, ghost_fun_nat_cast, ghost_fun_neg, -pi.coe_nat, -pi.coe_int]
private lemma ghost_fun_nsmul (m : ℕ) : ghost_fun (m • x) = m • ghost_fun x :=
by ghost_fun_tac (m • X 0) ![x.coeff]
private lemma ghost_fun_zsmul (m : ℤ) : ghost_fun (m • x) = m • ghost_fun x :=
by ghost_fun_tac (m • X 0) ![x.coeff]
private lemma ghost_fun_pow (m : ℕ) : ghost_fun (x ^ m) = ghost_fun x ^ m :=
by ghost_fun_tac (X 0 ^ m) ![x.coeff]
end ghost_fun
variables (p) (R)
/-- The bijection between `𝕎 R` and `ℕ → R`, under the assumption that `p` is invertible in `R`.
In `witt_vector.ghost_equiv` we upgrade this to an isomorphism of rings. -/
private def ghost_equiv' [invertible (p : R)] : 𝕎 R ≃ (ℕ → R) :=
{ to_fun := ghost_fun,
inv_fun := λ x, mk p $ λ n, aeval x (X_in_terms_of_W p R n),
left_inv :=
begin
intro x,
ext n,
have := bind₁_witt_polynomial_X_in_terms_of_W p R n,
apply_fun (aeval x.coeff) at this,
simpa only [aeval_bind₁, aeval_X, ghost_fun, aeval_witt_polynomial]
end,
right_inv :=
begin
intro x,
ext n,
have := bind₁_X_in_terms_of_W_witt_polynomial p R n,
apply_fun (aeval x) at this,
simpa only [aeval_bind₁, aeval_X, ghost_fun, aeval_witt_polynomial]
end }
include hp
local attribute [instance]
private def comm_ring_aux₁ : comm_ring (𝕎 (mv_polynomial R ℚ)) :=
by letI : comm_ring (mv_polynomial R ℚ) := mv_polynomial.comm_ring; exact
(ghost_equiv' p (mv_polynomial R ℚ)).injective.comm_ring (ghost_fun)
ghost_fun_zero ghost_fun_one ghost_fun_add ghost_fun_mul ghost_fun_neg ghost_fun_sub
ghost_fun_nsmul ghost_fun_zsmul ghost_fun_pow ghost_fun_nat_cast ghost_fun_int_cast
local attribute [instance]
private def comm_ring_aux₂ : comm_ring (𝕎 (mv_polynomial R ℤ)) :=
(map_fun.injective _ $ map_injective (int.cast_ring_hom ℚ) int.cast_injective).comm_ring _
(map_fun.zero _) (map_fun.one _) (map_fun.add _) (map_fun.mul _) (map_fun.neg _) (map_fun.sub _)
(map_fun.nsmul _) (map_fun.zsmul _) (map_fun.pow _) (map_fun.nat_cast _) (map_fun.int_cast _)
attribute [reducible] comm_ring_aux₂
/-- The commutative ring structure on `𝕎 R`. -/
instance : comm_ring (𝕎 R) :=
(map_fun.surjective _ $ counit_surjective _).comm_ring (map_fun $ mv_polynomial.counit _)
(map_fun.zero _) (map_fun.one _) (map_fun.add _) (map_fun.mul _) (map_fun.neg _) (map_fun.sub _)
(map_fun.nsmul _) (map_fun.zsmul _) (map_fun.pow _) (map_fun.nat_cast _) (map_fun.int_cast _)
variables {p R}
/-- `witt_vector.map f` is the ring homomorphism `𝕎 R →+* 𝕎 S` naturally induced
by a ring homomorphism `f : R →+* S`. It acts coefficientwise. -/
noncomputable! def map (f : R →+* S) : 𝕎 R →+* 𝕎 S :=
{ to_fun := map_fun f,
map_zero' := map_fun.zero f,
map_one' := map_fun.one f,
map_add' := map_fun.add f,
map_mul' := map_fun.mul f }
lemma map_injective (f : R →+* S) (hf : injective f) : injective (map f : 𝕎 R → 𝕎 S) :=
map_fun.injective f hf
lemma map_surjective (f : R →+* S) (hf : surjective f) : surjective (map f : 𝕎 R → 𝕎 S) :=
map_fun.surjective f hf
@[simp] lemma map_coeff (f : R →+* S) (x : 𝕎 R) (n : ℕ) :
(map f x).coeff n = f (x.coeff n) := rfl
/-- `witt_vector.ghost_map` is a ring homomorphism that maps each Witt vector
to the sequence of its ghost components. -/
def ghost_map : 𝕎 R →+* ℕ → R :=
{ to_fun := ghost_fun,
map_zero' := ghost_fun_zero,
map_one' := ghost_fun_one,
map_add' := ghost_fun_add,
map_mul' := ghost_fun_mul }
/-- Evaluates the `n`th Witt polynomial on the first `n` coefficients of `x`,
producing a value in `R`. -/
def ghost_component (n : ℕ) : 𝕎 R →+* R := (pi.eval_ring_hom _ n).comp ghost_map
lemma ghost_component_apply (n : ℕ) (x : 𝕎 R) : ghost_component n x = aeval x.coeff (W_ ℤ n) := rfl
@[simp] lemma ghost_map_apply (x : 𝕎 R) (n : ℕ) : ghost_map x n = ghost_component n x := rfl
section invertible
variables (p R) [invertible (p : R)]
/-- `witt_vector.ghost_map` is a ring isomorphism when `p` is invertible in `R`. -/
def ghost_equiv : 𝕎 R ≃+* (ℕ → R) :=
{ .. (ghost_map : 𝕎 R →+* (ℕ → R)), .. (ghost_equiv' p R) }
@[simp] lemma ghost_equiv_coe : (ghost_equiv p R : 𝕎 R →+* (ℕ → R)) = ghost_map := rfl
lemma ghost_map.bijective_of_invertible : function.bijective (ghost_map : 𝕎 R → ℕ → R) :=
(ghost_equiv p R).bijective
end invertible
/-- `witt_vector.coeff x 0` as a `ring_hom` -/
@[simps]
noncomputable! def constant_coeff : 𝕎 R →+* R :=
{ to_fun := λ x, x.coeff 0,
map_zero' := by simp,
map_one' := by simp,
map_add' := add_coeff_zero,
map_mul' := mul_coeff_zero }
instance [nontrivial R] : nontrivial (𝕎 R) :=
constant_coeff.domain_nontrivial
end witt_vector
|
af523bd90dd97da1d12a7e7568307eb6c6452b6b | d1a52c3f208fa42c41df8278c3d280f075eb020c | /src/Lean/Compiler/InitAttr.lean | 4e341a8353c26b77d88a439a6c1f4160473da9cb | [
"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 | 4,911 | 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
import Lean.Attributes
namespace Lean
private def getIOTypeArg : Expr → Option Expr
| Expr.app (Expr.const `IO _ _) arg _ => some arg
| _ => none
private def isUnitType : Expr → Bool
| Expr.const `Unit _ _ => true
| _ => false
private def isIOUnit (type : Expr) : Bool :=
match getIOTypeArg type with
| some type => isUnitType type
| _ => false
/-- Run the initializer for `decl` and store its value for global access. Should only be used while importing. -/
@[extern "lean_run_init"]
unsafe constant runInit (env : @& Environment) (opts : @& Options) (decl initDecl : @& Name) : IO Unit
unsafe def registerInitAttrUnsafe (attrName : Name) (runAfterImport : Bool) : IO (ParametricAttribute Name) :=
registerParametricAttribute {
name := attrName,
descr := "initialization procedure for global references",
getParam := fun declName stx => do
let decl ← getConstInfo declName
match (← Attribute.Builtin.getIdent? stx) with
| some initFnName =>
let initFnName ← resolveGlobalConstNoOverload initFnName
let initDecl ← getConstInfo initFnName
match getIOTypeArg initDecl.type with
| none => throwError "initialization function '{initFnName}' must have type of the form `IO <type>`"
| some initTypeArg =>
if decl.type == initTypeArg then pure initFnName
else throwError "initialization function '{initFnName}' type mismatch"
| none =>
if isIOUnit decl.type then pure Name.anonymous
else throwError "initialization function must have type `IO Unit`"
afterImport := fun entries => do
let ctx ← read
if runAfterImport && (← isInitializerExecutionEnabled) then
for modEntries in entries do
for (decl, initDecl) in modEntries do
if initDecl.isAnonymous then
let initFn ← IO.ofExcept <| ctx.env.evalConst (IO Unit) ctx.opts decl
initFn
else
runInit ctx.env ctx.opts decl initDecl
}
@[implementedBy registerInitAttrUnsafe]
constant registerInitAttr (attrName : Name) (runAfterImport : Bool) : IO (ParametricAttribute Name)
builtin_initialize regularInitAttr : ParametricAttribute Name ← registerInitAttr `init true
builtin_initialize builtinInitAttr : ParametricAttribute Name ← registerInitAttr `builtinInit false
def getInitFnNameForCore? (env : Environment) (attr : ParametricAttribute Name) (fn : Name) : Option Name :=
match attr.getParam env fn with
| some Name.anonymous => none
| some n => some n
| _ => none
@[export lean_get_builtin_init_fn_name_for]
def getBuiltinInitFnNameFor? (env : Environment) (fn : Name) : Option Name :=
getInitFnNameForCore? env builtinInitAttr fn
@[export lean_get_regular_init_fn_name_for]
def getRegularInitFnNameFor? (env : Environment) (fn : Name) : Option Name :=
getInitFnNameForCore? env regularInitAttr fn
@[export lean_get_init_fn_name_for]
def getInitFnNameFor? (env : Environment) (fn : Name) : Option Name :=
getBuiltinInitFnNameFor? env fn <|> getRegularInitFnNameFor? env fn
def isIOUnitInitFnCore (env : Environment) (attr : ParametricAttribute Name) (fn : Name) : Bool :=
match attr.getParam env fn with
| some Name.anonymous => true
| _ => false
@[export lean_is_io_unit_regular_init_fn]
def isIOUnitRegularInitFn (env : Environment) (fn : Name) : Bool :=
isIOUnitInitFnCore env regularInitAttr fn
@[export lean_is_io_unit_builtin_init_fn]
def isIOUnitBuiltinInitFn (env : Environment) (fn : Name) : Bool :=
isIOUnitInitFnCore env builtinInitAttr fn
def isIOUnitInitFn (env : Environment) (fn : Name) : Bool :=
isIOUnitBuiltinInitFn env fn || isIOUnitRegularInitFn env fn
def hasInitAttr (env : Environment) (fn : Name) : Bool :=
(getInitFnNameFor? env fn).isSome
def setBuiltinInitAttr (env : Environment) (declName : Name) (initFnName : Name := Name.anonymous) : Except String Environment :=
builtinInitAttr.setParam env declName initFnName
def declareBuiltin (forDecl : Name) (value : Expr) : CoreM Unit := do
let name := `_regBuiltin ++ forDecl
let type := mkApp (mkConst `IO) (mkConst `Unit)
let decl := Declaration.defnDecl { name, levelParams := [], type, value, hints := ReducibilityHints.opaque,
safety := DefinitionSafety.safe }
match (← getEnv).addAndCompile {} decl with
-- TODO: pretty print error
| Except.error e => do
let msg ← (e.toMessageData {}).toString
throwError "failed to emit registration code for builtin '{forDecl}': {msg}"
| Except.ok env => IO.ofExcept (setBuiltinInitAttr env name) >>= setEnv
end Lean
|
95c9b626eb82352479ad9970ac46c9d6854e9f62 | b0d97c09d47e3b0a68128c64cad26ab132d23108 | /src/compcont/ex3.lean | 34aaf1113826c0008682f33f86d1239f71129a0f | [] | no_license | jmvlangen/RIP-seminar | 2ad32c1c6fceb59ac7ae3b2251baf08f4c0c0caa | ed6771404dd4bcec298de2dfdc89d5e9cfd331bb | refs/heads/master | 1,585,865,817,237 | 1,546,865,823,000 | 1,546,865,823,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,292 | lean | import analysis.topology.topological_space
import analysis.topology.continuity
import analysis.topology.topological_structures
import ex2
open tactic expr function has_add
variables {X : Type*} {Y : Type*} {Z : Type*} {W : Type*}
{f f₁ f₂ : X → Y} {g : Y → Z} {h : Z → W}
variables [topological_space X] [topological_space Y]
[topological_space Z] [topological_space W]
example [add_monoid X] [topological_add_monoid X]:
(λ x : X, x + x) = (uncurry add) ∘ (λ x : X, (x, x)) :=
by reflexivity
#check @has_add.add .
-- Some preparation is necessary, has_add.add has 4 (in words four) arguments
-- first count the non-implicit (default) args, then find them
meta def get_app_num_default_args_aux : expr → nat
| (pi _ binder_info.default _ bbody) := 1 + (get_app_num_default_args_aux bbody)
| (pi _ _ _ bbody) := get_app_num_default_args_aux bbody
| _ := 0
meta def get_app_num_default_args : expr → tactic nat :=
assume e,
do type ← infer_type e, -- the type of a function is a pi type, we inspect the binder_info recursively
return $ get_app_num_default_args_aux type
meta def ith_default_arg_index_aux : expr → nat → tactic nat
| (expr.pi _ binder_info.default _ _) 0 := return 0
| (expr.pi _ binder_info.default _ bbody) (i + 1) := do idx ← ith_default_arg_index_aux bbody i, return $ idx + 1
| (expr.pi _ _ _ bbody) i := do idx ← ith_default_arg_index_aux bbody i, return $ idx + 1
| e i := do fail $ "can't find " ++ to_string i ++ "th default argument index"
meta def ith_default_arg_index (e : expr) (i : nat) : tactic nat :=
do type ← infer_type e,
index ← ith_default_arg_index_aux type i,
return index
meta def get_codomain (e : expr) : tactic expr :=
do t ← infer_type e,
match t with
| `(%%domain → %%codomain) := return codomain
| _ := fail $ "Can't determine codomain, '" ++ to_string t ++ "' is not a function type."
end
--now we can 'uncurry' and move λ inside
/- Push a given lambda expression inside in the goal:
-
- Takes an expression of the form 'λ x, f _[x]',
- creates a hypothesis 'λ x, f _[x] = f ∘ (λ x, _[x])',
- and rewrites the main goal using this hypothesis.
-
- Tries to uncurry if there is a curried function with two arguments.
-
- Fails if it 'doesn't simplify'.
-/
meta def push_lam_inside2 (e : expr): tactic unit :=
--do trace $ to_raw_fmt e,
do lam name binfo bdomain bbody ← return e
<|> (fail "The given expression must be a λ."),
if ¬ is_app bbody
then fail $ "The outer most part of the lambda expression '" ++ (to_string e) ++
"' is not a function application."
else do
-- we use fancier versions instead of simple pattern matching
-- because there are many curried arguments now
fn ← return $ get_app_fn bbody,
arg ← return $ app_arg bbody,
num_default_args ← get_app_num_default_args fn,
-- uncurry if necessary, then push λ inside
match num_default_args with
| 2 := do -- there are two default arguments, uncurry and push lambda inside
-- get first argument and argument type
arg0_idx ← ith_default_arg_index fn 0,
arg0 ← return $ ith_arg bbody arg0_idx,
arg0_type ← get_codomain (expr.lam name binfo bdomain arg0),
-- get second argument and argument type
arg1_idx ← ith_default_arg_index fn 1,
arg1 ← return $ ith_arg bbody arg1_idx,
arg1_type ← get_codomain (expr.lam name binfo bdomain arg1),
-- create the inner expression, i.e. λ x, (arg0[x], arg1[x])
inner_body_app ← to_expr ``(@prod.mk %%arg0_type %%arg1_type),
inner ← return $ expr.lam name binfo bdomain
(expr.app (expr.app inner_body_app arg0) arg1),
-- create the outer expression, i.e. (uncurry f)
expr.const fn_name _ ← return fn,
outer ← return $ @expr.const ff fn_name [],
-- create term 'original = outer ∘ inner', need to hint types here
-- i.e. need to use @comp instead of $∘
can_uncurry ← to_expr ``(%%e = @comp %%bdomain (%%arg0_type × %%arg1_type) _
(uncurry %%outer) %%inner),
-- create hypothesis and prove it by reflexivity
h_can_uncurry ← assert "can_uncurry" can_uncurry,
reflexivity <|> rotate 1,
-- rewrite target
rewrite_target h_can_uncurry,
to_expr ``(function.uncurry_def) >>= rewrite_target,
-- fail if the outer or inner part is the same as the original expression
`(continuous $ %%outer ∘ %%inner) ← target,
(success_if_fail $ unify e inner) <|> fail "inner not changed!",
(success_if_fail $ unify e outer) <|> fail "outer not changed!",
-- done!
return ()
| 1 := do -- same as before
-- create hypothesis
can_push_inside ← to_expr ``(%%e = %%fn ∘ %%(expr.lam name binfo bdomain arg)),
-- prove hypothesis
h_can_push_inside ← assert "can_push_λ_inside" can_push_inside,
reflexivity <|> fail "Can't prove that λ can be pushed inside by reflexivity.",
-- rewrite target
rewrite_target h_can_push_inside,
return ()
| _ := fail "uncurrying more than two arguements is not implemented"
end
example [add_monoid X] [topological_add_monoid X] : (continuous (λ x : X, x + x)) :=
begin
(do `(continuous %%e) ← target,
push_lam_inside2 e,
return ()),
contcomp2,
exact topological_add_monoid.continuous_add X,
sorry
end
example [add_monoid X] [topological_add_monoid X] : (continuous (λ x : X, (x, x))) :=
by sorry
-- Same as contcomp2, bu uses push_lam_inside2
meta def contcomp3 : tactic unit :=
do `(continuous %%e) ← target <|> fail "goal has to be of the form 'continuous _'",
-- if the goal is to show that the expression e is continuous, check if it is an assumption
assumption <|>
-- if e is not continuous by assumption, check if it is of the form 'f ∘ g' for some f and g
match e with
| `(%%outer ∘ %%inner) :=
do
-- assert that the outer function is continuous, if not, rotate the goal away
outer_continuous ← to_expr ``(continuous %%outer),
outer_is_continuous ← assert "h_outer_cont" outer_continuous,
contcomp <|> rotate 1,
-- assert that the inner function is continuous, if not, rotate the goal away
inner_continuous ← to_expr ``(continuous %%inner),
inner_is_continuous ← assert "h_inner_cont" inner_continuous,
contcomp <|> rotate 1,
-- knowing that inner and outer function are continuous, apply the theorem continuous.comp
-- which shows that the composite is continuous
cont_comp ← to_expr ``(continuous.comp %%inner_is_continuous %%outer_is_continuous),
exact cont_comp
| `(λ _, _) :=
-- if we can move the lambda inside, to that and apply contcomp2 recursively
do push_lam_inside2 e <|> fail "can't move lambda inside",
contcomp3
| _ := fail "neither composite nor lambda expression"
end
example [add_monoid X] [topological_add_monoid X] : (continuous (λ x : X, x + x)) :=
begin
contcomp3,
exact topological_add_monoid.continuous_add X,
sorry
end
example [add_monoid X] [topological_add_monoid X] : (continuous (λ x : X, (x, x))) :=
by sorry
|
39afcfa0265f5de16f0ca689f9fee419b2646cb4 | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/group_theory/order_of_element.lean | 1f4e42afc83eecc65f1570d588c56a798e06cdff | [
"Apache-2.0"
] | permissive | hjvromen/lewis | 40b035973df7c77ebf927afab7878c76d05ff758 | 105b675f73630f028ad5d890897a51b3c1146fb0 | refs/heads/master | 1,677,944,636,343 | 1,676,555,301,000 | 1,676,555,301,000 | 327,553,599 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,884 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import algebra.big_operators.order
import group_theory.coset
import data.nat.totient
import data.int.gcd
import data.set.finite
open function
open_locale big_operators
variables {α : Type*} {s : set α} {a a₁ a₂ b c: α}
-- TODO mem_range_iff_mem_finset_range_of_mod_eq should be moved elsewhere.
namespace finset
open finset
lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ}
(hn : 0 < n) (h : ∀i, f (i % n) = f i) :
a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) :=
suffices (∃i, f (i % n) = a) ↔ ∃i, i < n ∧ f ↑i = a, by simpa [h],
have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn,
iff.intro
(assume ⟨i, hi⟩,
have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'),
⟨int.to_nat (i % n),
by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩)
(assume ⟨i, hi, ha⟩,
⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩)
end finset
lemma conj_injective [group α] {x : α} : function.injective (λ (g : α), x * g * x⁻¹) :=
λ a b h, by simpa [mul_left_inj, mul_right_inj] using h
lemma mem_normalizer_fintype [group α] {s : set α} [fintype s] {x : α}
(h : ∀ n, n ∈ s → x * n * x⁻¹ ∈ s) : x ∈ subgroup.set_normalizer s :=
by haveI := classical.prop_decidable;
haveI := set.fintype_image s (λ n, x * n * x⁻¹); exact
λ n, ⟨h n, λ h₁,
have heq : (λ n, x * n * x⁻¹) '' s = s := set.eq_of_subset_of_card_le
(λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective s conj_injective),
have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' s := heq.symm ▸ h₁,
let ⟨y, hy⟩ := this in conj_injective hy.2 ▸ hy.1⟩
section order_of
variable [group α]
open quotient_group set
instance fintype_bot : fintype (⊥ : subgroup α) := ⟨{1},
by {rintro ⟨x, ⟨hx⟩⟩, exact finset.mem_singleton_self _}⟩
@[simp] lemma card_trivial :
fintype.card (⊥ : subgroup α) = 1 :=
fintype.card_eq_one_iff.2
⟨⟨(1 : α), set.mem_singleton 1⟩, λ ⟨y, hy⟩, subtype.eq $ subgroup.mem_bot.1 hy⟩
variables [fintype α] [dec : decidable_eq α]
lemma card_eq_card_quotient_mul_card_subgroup (s : subgroup α) [fintype s]
[decidable_pred (λ a, a ∈ s)] : fintype.card α = fintype.card (quotient s) * fintype.card s :=
by rw ← fintype.card_prod;
exact fintype.card_congr (subgroup.group_equiv_quotient_times_subgroup)
lemma card_subgroup_dvd_card (s : subgroup α) [fintype s] :
fintype.card s ∣ fintype.card α :=
by haveI := classical.prop_decidable; simp [card_eq_card_quotient_mul_card_subgroup s]
lemma card_quotient_dvd_card (s : subgroup α) [decidable_pred (λ a, a ∈ s)] [fintype s] :
fintype.card (quotient s) ∣ fintype.card α :=
by simp [card_eq_card_quotient_mul_card_subgroup s]
lemma exists_gpow_eq_one (a : α) : ∃i≠0, a ^ (i:ℤ) = 1 :=
have ¬ injective (λi:ℤ, a ^ i),
from not_injective_infinite_fintype _,
let ⟨i, j, a_eq, ne⟩ := show ∃(i j : ℤ), a ^ i = a ^ j ∧ i ≠ j,
by rw [injective] at this; simpa [not_forall] in
have a ^ (i - j) = 1,
by simp [sub_eq_add_neg, gpow_add, gpow_neg, a_eq],
⟨i - j, sub_ne_zero.mpr ne, this⟩
lemma exists_pow_eq_one (a : α) : ∃i > 0, a ^ i = 1 :=
let ⟨i, hi, eq⟩ := exists_gpow_eq_one a in
begin
cases i,
{ exact ⟨i, nat.pos_of_ne_zero (by simp [int.of_nat_eq_coe, *] at *), eq⟩ },
{ exact ⟨i + 1, dec_trivial, inv_eq_one.1 eq⟩ }
end
include dec
/-- `order_of a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `a ^ n = 1` -/
def order_of (a : α) : ℕ := nat.find (exists_pow_eq_one a)
lemma pow_order_of_eq_one (a : α) : a ^ order_of a = 1 :=
let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₂
lemma order_of_pos (a : α) : 0 < order_of a :=
let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₁
private lemma pow_injective_aux {n m : ℕ} (a : α) (h : n ≤ m)
(hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m :=
decidable.by_contradiction $ assume ne : n ≠ m,
have h₁ : m - n > 0, from nat.pos_of_ne_zero (by simp [nat.sub_eq_iff_eq_add h, ne.symm]),
have h₂ : a ^ (m - n) = 1, by simp [pow_sub _ h, eq],
have le : order_of a ≤ m - n, from nat.find_min' (exists_pow_eq_one a) ⟨h₁, h₂⟩,
have lt : m - n < order_of a,
from (nat.sub_lt_left_iff_lt_add h).mpr $ nat.lt_add_left _ _ _ hm,
lt_irrefl _ (lt_of_le_of_lt le lt)
lemma pow_injective_of_lt_order_of {n m : ℕ} (a : α)
(hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m :=
(le_total n m).elim
(assume h, pow_injective_aux a h hn hm eq)
(assume h, (pow_injective_aux a h hm hn eq.symm).symm)
lemma order_of_le_card_univ : order_of a ≤ fintype.card α :=
finset.card_le_of_inj_on ((^) a)
(assume n _, fintype.complete _)
(assume i j, pow_injective_of_lt_order_of a)
lemma pow_eq_mod_order_of {n : ℕ} : a ^ n = a ^ (n % order_of a) :=
calc a ^ n = a ^ (n % order_of a + order_of a * (n / order_of a)) :
by rw [nat.mod_add_div]
... = a ^ (n % order_of a) :
by simp [pow_add, pow_mul, pow_order_of_eq_one]
lemma gpow_eq_mod_order_of {i : ℤ} : a ^ i = a ^ (i % order_of a) :=
calc a ^ i = a ^ (i % order_of a + order_of a * (i / order_of a)) :
by rw [int.mod_add_div]
... = a ^ (i % order_of a) :
by simp [gpow_add, gpow_mul, pow_order_of_eq_one]
lemma mem_gpowers_iff_mem_range_order_of {a a' : α} :
a' ∈ subgroup.gpowers a ↔ a' ∈ (finset.range (order_of a)).image ((^) a : ℕ → α) :=
finset.mem_range_iff_mem_finset_range_of_mod_eq
(order_of_pos a)
(assume i, gpow_eq_mod_order_of.symm)
instance decidable_gpowers : decidable_pred (subgroup.gpowers a : set α) :=
assume a', decidable_of_iff'
(a' ∈ (finset.range (order_of a)).image ((^) a))
mem_gpowers_iff_mem_range_order_of
lemma order_of_dvd_of_pow_eq_one {n : ℕ} (h : a ^ n = 1) : order_of a ∣ n :=
by_contradiction
(λ h₁, nat.find_min _ (show n % order_of a < order_of a,
from nat.mod_lt _ (order_of_pos _))
⟨nat.pos_of_ne_zero (mt nat.dvd_of_mod_eq_zero h₁), by rwa ← pow_eq_mod_order_of⟩)
lemma order_of_dvd_iff_pow_eq_one {n : ℕ} : order_of a ∣ n ↔ a ^ n = 1 :=
⟨λ h, by rw [pow_eq_mod_order_of, nat.mod_eq_zero_of_dvd h, pow_zero], order_of_dvd_of_pow_eq_one⟩
lemma order_of_le_of_pow_eq_one {n : ℕ} (hn : 0 < n) (h : a ^ n = 1) : order_of a ≤ n :=
nat.find_min' (exists_pow_eq_one a) ⟨hn, h⟩
lemma sum_card_order_of_eq_card_pow_eq_one {n : ℕ} (hn : 0 < n) :
∑ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (λ a : α, order_of a = m)).card
= (finset.univ.filter (λ a : α, a ^ n = 1)).card :=
calc ∑ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (λ a : α, order_of a = m)).card
= _ : (finset.card_bind (by { intros, apply finset.disjoint_filter.2, cc })).symm
... = _ : congr_arg finset.card (finset.ext (begin
assume a,
suffices : order_of a ≤ n ∧ order_of a ∣ n ↔ a ^ n = 1,
{ simpa [nat.lt_succ_iff], },
exact ⟨λ h, let ⟨m, hm⟩ := h.2 in by rw [hm, pow_mul, pow_order_of_eq_one, one_pow],
λ h, ⟨order_of_le_of_pow_eq_one hn h, order_of_dvd_of_pow_eq_one h⟩⟩
end))
section
local attribute [instance] set_fintype
lemma order_eq_card_gpowers : order_of a = fintype.card (subgroup.gpowers a : set α) :=
begin
refine (finset.card_eq_of_bijective _ _ _ _).symm,
{ exact λn hn, ⟨gpow a n, ⟨n, rfl⟩⟩ },
{ exact assume ⟨_, i, rfl⟩ _,
have pos: (0:int) < order_of a,
from int.coe_nat_lt.mpr $ order_of_pos a,
have 0 ≤ i % (order_of a),
from int.mod_nonneg _ $ ne_of_gt pos,
⟨int.to_nat (i % order_of a),
by rw [← int.coe_nat_lt, int.to_nat_of_nonneg this];
exact ⟨int.mod_lt_of_pos _ pos, subtype.eq gpow_eq_mod_order_of.symm⟩⟩ },
{ intros, exact finset.mem_univ _ },
{ exact assume i j hi hj eq, pow_injective_of_lt_order_of a hi hj $ by simpa using eq }
end
@[simp] lemma order_of_one : order_of (1 : α) = 1 :=
by rw [order_eq_card_gpowers, fintype.card_eq_one_iff];
exact ⟨⟨1, 0, rfl⟩, λ ⟨a, i, ha⟩, by simp [ha.symm]⟩
@[simp] lemma order_of_eq_one_iff : order_of a = 1 ↔ a = 1 :=
⟨λ h, by conv { to_lhs, rw [← pow_one a, ← h, pow_order_of_eq_one] }, λ h, by simp [h]⟩
lemma order_of_eq_prime {p : ℕ} [hp : fact p.prime]
(hg : a^p = 1) (hg1 : a ≠ 1) : order_of a = p :=
(hp.2 _ (order_of_dvd_of_pow_eq_one hg)).resolve_left (mt order_of_eq_one_iff.1 hg1)
section classical
open_locale classical
open quotient_group subgroup
/- TODO: use cardinal theory, introduce `card : set α → ℕ`, or setup decidability for cosets -/
lemma order_of_dvd_card_univ : order_of a ∣ fintype.card α :=
have ft_prod : fintype (quotient (gpowers a) × (gpowers a)),
from fintype.of_equiv α group_equiv_quotient_times_subgroup,
have ft_s : fintype (gpowers a),
from @fintype.fintype_prod_right _ _ _ ft_prod _,
have ft_cosets : fintype (quotient (gpowers a)),
from @fintype.fintype_prod_left _ _ _ ft_prod ⟨⟨1, (gpowers a).one_mem⟩⟩,
have eq₁ : fintype.card α = @fintype.card _ ft_cosets * @fintype.card _ ft_s,
from calc fintype.card α = @fintype.card _ ft_prod :
@fintype.card_congr _ _ _ ft_prod group_equiv_quotient_times_subgroup
... = @fintype.card _ (@prod.fintype _ _ ft_cosets ft_s) :
congr_arg (@fintype.card _) $ subsingleton.elim _ _
... = @fintype.card _ ft_cosets * @fintype.card _ ft_s :
@fintype.card_prod _ _ ft_cosets ft_s,
have eq₂ : order_of a = @fintype.card _ ft_s,
from calc order_of a = _ : order_eq_card_gpowers
... = _ : congr_arg (@fintype.card _) $ subsingleton.elim _ _,
dvd.intro (@fintype.card (quotient (subgroup.gpowers a)) ft_cosets) $
by rw [eq₁, eq₂, mul_comm]
omit dec
@[simp] lemma pow_card_eq_one (a : α) : a ^ fintype.card α = 1 :=
let ⟨m, hm⟩ := @order_of_dvd_card_univ _ a _ _ _ in
by simp [hm, pow_mul, pow_order_of_eq_one]
lemma mem_powers_iff_mem_gpowers {a x : α} : x ∈ submonoid.powers a ↔ x ∈ gpowers a :=
⟨λ ⟨n, hn⟩, ⟨n, by simp * at *⟩,
λ ⟨i, hi⟩, ⟨(i % order_of a).nat_abs,
by rwa [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _
(int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of]⟩⟩
lemma powers_eq_gpowers (a : α) : (submonoid.powers a : set α) = gpowers a :=
set.ext $ λ x, mem_powers_iff_mem_gpowers
end classical
open nat subgroup
lemma order_of_pow (a : α) (n : ℕ) : order_of (a ^ n) = order_of a / gcd (order_of a) n :=
dvd_antisymm
(order_of_dvd_of_pow_eq_one
(by rw [← pow_mul, ← nat.mul_div_assoc _ (gcd_dvd_left _ _), mul_comm,
nat.mul_div_assoc _ (gcd_dvd_right _ _), pow_mul, pow_order_of_eq_one, one_pow]))
(have gcd_pos : 0 < gcd (order_of a) n, from gcd_pos_of_pos_left n (order_of_pos a),
have hdvd : order_of a ∣ n * order_of (a ^ n),
from order_of_dvd_of_pow_eq_one (by rw [pow_mul, pow_order_of_eq_one]),
coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd gcd_pos)
(dvd_of_mul_dvd_mul_right gcd_pos
(by rwa [nat.div_mul_cancel (gcd_dvd_left _ _), mul_assoc,
nat.div_mul_cancel (gcd_dvd_right _ _), mul_comm])))
lemma image_range_order_of (a : α) :
finset.image (λ i, a ^ i) (finset.range (order_of a)) = (gpowers a : set α).to_finset :=
by { ext x, rw [set.mem_to_finset, mem_coe, mem_gpowers_iff_mem_range_order_of] }
omit dec
open_locale classical
lemma pow_gcd_card_eq_one_iff {n : ℕ} {a : α} :
a ^ n = 1 ↔ a ^ (gcd n (fintype.card α)) = 1 :=
⟨λ h, pow_gcd_eq_one _ h $ pow_card_eq_one _,
λ h, let ⟨m, hm⟩ := gcd_dvd_left n (fintype.card α) in
by rw [hm, pow_mul, h, one_pow]⟩
end
end order_of
section cyclic
local attribute [instance] set_fintype
open subgroup
/-- A group is called *cyclic* if it is generated by a single element. -/
class is_cyclic (α : Type*) [group α] : Prop :=
(exists_generator [] : ∃ g : α, ∀ x, x ∈ gpowers g)
/-- A cyclic group is always commutative. This is not an `instance` because often we have a better
proof of `comm_group`. -/
def is_cyclic.comm_group [hg : group α] [is_cyclic α] : comm_group α :=
{ mul_comm := λ x y, show x * y = y * x,
from let ⟨g, hg⟩ := is_cyclic.exists_generator α in
let ⟨n, hn⟩ := hg x in let ⟨m, hm⟩ := hg y in
hm ▸ hn ▸ gpow_mul_comm _ _ _,
..hg }
lemma is_cyclic_of_order_of_eq_card [group α] [decidable_eq α] [fintype α]
(x : α) (hx : order_of x = fintype.card α) : is_cyclic α :=
⟨⟨x, set.eq_univ_iff_forall.1 $ set.eq_of_subset_of_card_le
(set.subset_univ _)
(by {rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_gpowers], refl})⟩⟩
lemma is_cyclic_of_prime_card [group α] [fintype α] {p : ℕ} [hp : fact p.prime]
(h : fintype.card α = p) : is_cyclic α :=
⟨begin
obtain ⟨g, hg⟩ : ∃ g : α, g ≠ 1,
from fintype.exists_ne_of_one_lt_card (by { rw h, exact nat.prime.one_lt hp }) 1,
have : fintype.card (subgroup.gpowers g) ∣ p,
{ rw ←h,
apply card_subgroup_dvd_card },
rw nat.dvd_prime hp at this,
cases this,
{ rw fintype.card_eq_one_iff at this,
cases this with t ht,
suffices : g = 1,
{ contradiction },
have hgt := ht ⟨g, by { change g ∈ subgroup.gpowers g, exact subgroup.mem_gpowers g }⟩,
rw [←ht 1] at hgt,
change (⟨_, _⟩ : subgroup.gpowers g) = ⟨_, _⟩ at hgt,
simpa using hgt },
{ use g,
intro x,
rw [←h] at this,
rw subgroup.eq_top_of_card_eq _ this,
exact subgroup.mem_top _ }
end⟩
lemma order_of_eq_card_of_forall_mem_gpowers [group α] [decidable_eq α] [fintype α]
{g : α} (hx : ∀ x, x ∈ gpowers g) : order_of g = fintype.card α :=
by {rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_gpowers],
simp [hx], apply fintype.card_of_finset', simp, intro x, exact hx x}
instance bot.is_cyclic [group α] : is_cyclic (⊥ : subgroup α) :=
⟨⟨1, λ x, ⟨0, subtype.eq $ eq.symm (subgroup.mem_bot.1 x.2)⟩⟩⟩
instance subgroup.is_cyclic [group α] [is_cyclic α] (H : subgroup α) : is_cyclic H :=
by haveI := classical.prop_decidable; exact
let ⟨g, hg⟩ := is_cyclic.exists_generator α in
if hx : ∃ (x : α), x ∈ H ∧ x ≠ (1 : α) then
let ⟨x, hx₁, hx₂⟩ := hx in
let ⟨k, hk⟩ := hg x in
have hex : ∃ n : ℕ, 0 < n ∧ g ^ n ∈ H,
from ⟨k.nat_abs, nat.pos_of_ne_zero
(λ h, hx₂ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, gpow_zero]),
match k, hk with
| (k : ℕ), hk := by rw [int.nat_abs_of_nat, ← gpow_coe_nat, hk]; exact hx₁
| -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat,
← subgroup.inv_mem_iff H]; simp * at *
end⟩,
⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩,
λ ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in
have hk₁ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ gpowers (g ^ nat.find hex),
from ⟨k / nat.find hex, eq.symm $ gpow_mul _ _ _⟩,
have hk₂ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ H,
by rw gpow_mul; exact H.gpow_mem (nat.find_spec hex).2 _,
have hk₃ : g ^ (k % nat.find hex) ∈ H,
from (subgroup.mul_mem_cancel_right H hk₂).1 $
by rw [← gpow_add, int.mod_add_div, hk]; exact hx,
have hk₄ : k % nat.find hex = (k % nat.find hex).nat_abs,
by rw int.nat_abs_of_nonneg (int.mod_nonneg _
(int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)),
have hk₅ : g ^ (k % nat.find hex ).nat_abs ∈ H,
by rwa [← gpow_coe_nat, ← hk₄],
have hk₆ : (k % (nat.find hex : ℤ)).nat_abs = 0,
from by_contradiction (λ h,
nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hk₄];
exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1))
⟨nat.pos_of_ne_zero h, hk₅⟩),
⟨k / (nat.find hex : ℤ), subtype.ext_iff_val.2 begin
suffices : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) = x,
{ simpa [gpow_mul] },
rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk]
end⟩⟩⟩
else
have H = (⊥ : subgroup α), from subgroup.ext $ λ x, ⟨λ h, by simp at *; tauto,
λ h, by rw [subgroup.mem_bot.1 h]; exact H.one_mem⟩,
by clear _let_match; substI this; apply_instance
open finset nat
lemma is_cyclic.card_pow_eq_one_le [group α] [decidable_eq α] [fintype α] [is_cyclic α] {n : ℕ}
(hn0 : 0 < n) : (univ.filter (λ a : α, a ^ n = 1)).card ≤ n :=
let ⟨g, hg⟩ := is_cyclic.exists_generator α in
calc (univ.filter (λ a : α, a ^ n = 1)).card
≤ ((gpowers (g ^ (fintype.card α / (gcd n (fintype.card α))))) : set α).to_finset.card :
card_le_of_subset (λ x hx, let ⟨m, hm⟩ := show x ∈ submonoid.powers g,
from mem_powers_iff_mem_gpowers.2 $ hg x in
set.mem_to_finset.2 ⟨(m / (fintype.card α / (gcd n (fintype.card α))) : ℕ),
have hgmn : g ^ (m * gcd n (fintype.card α)) = 1,
by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2,
begin
rw [gpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm],
refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card α) hn0) _,
conv {to_lhs, rw [nat.div_mul_cancel (gcd_dvd_right _ _), ← order_of_eq_card_of_forall_mem_gpowers hg]},
exact order_of_dvd_of_pow_eq_one hgmn
end⟩)
... ≤ n :
let ⟨m, hm⟩ := gcd_dvd_right n (fintype.card α) in
have hm0 : 0 < m, from nat.pos_of_ne_zero
(λ hm0, (by rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm; exact hm 1)),
begin
rw [← fintype.card_of_finset' _ (λ _, set.mem_to_finset), ← order_eq_card_gpowers,
order_of_pow, order_of_eq_card_of_forall_mem_gpowers hg],
rw [hm] {occs := occurrences.pos [2,3]},
rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left,
hm, nat.mul_div_cancel _ hm0],
exact le_of_dvd hn0 (gcd_dvd_left _ _)
end
lemma is_cyclic.exists_monoid_generator (α : Type*) [group α] [fintype α] [is_cyclic α] :
∃ x : α, ∀ y : α, y ∈ submonoid.powers x :=
by { simp only [mem_powers_iff_mem_gpowers], exact is_cyclic.exists_generator α }
section
variables [group α] [decidable_eq α] [fintype α]
lemma is_cyclic.image_range_order_of (ha : ∀ x : α, x ∈ gpowers a) :
finset.image (λ i, a ^ i) (range (order_of a)) = univ :=
begin
simp_rw [←subgroup.mem_coe] at ha,
simp only [image_range_order_of, set.eq_univ_iff_forall.mpr ha],
convert set.to_finset_univ
end
lemma is_cyclic.image_range_card (ha : ∀ x : α, x ∈ gpowers a) :
finset.image (λ i, a ^ i) (range (fintype.card α)) = univ :=
by rw [← order_of_eq_card_of_forall_mem_gpowers ha, is_cyclic.image_range_order_of ha]
end
section totient
variables [group α] [decidable_eq α] [fintype α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n)
include hn
lemma card_pow_eq_one_eq_order_of_aux (a : α) :
(finset.univ.filter (λ b : α, b ^ order_of a = 1)).card = order_of a :=
le_antisymm
(hn _ (order_of_pos _))
(calc order_of a = @fintype.card (gpowers a) (id _) : order_eq_card_gpowers
... ≤ @fintype.card (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α)
(fintype.of_finset _ (λ _, iff.rfl)) :
@fintype.card_le_of_injective (gpowers a) (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α)
(id _) (id _) (λ b, ⟨b.1, mem_filter.2 ⟨mem_univ _,
let ⟨i, hi⟩ := b.2 in
by rw [← hi, ← gpow_coe_nat, ← gpow_mul, mul_comm, gpow_mul, gpow_coe_nat,
pow_order_of_eq_one, one_gpow]⟩⟩) (λ _ _ h, subtype.eq (subtype.mk.inj h))
... = (univ.filter (λ b : α, b ^ order_of a = 1)).card : fintype.card_of_finset _ _)
open_locale nat -- use φ for nat.totient
private lemma card_order_of_eq_totient_aux₁ :
∀ {d : ℕ}, d ∣ fintype.card α → 0 < (univ.filter (λ a : α, order_of a = d)).card →
(univ.filter (λ a : α, order_of a = d)).card = φ d
| 0 := λ hd hd0,
let ⟨a, ha⟩ := card_pos.1 hd0 in absurd (mem_filter.1 ha).2 $ ne_of_gt $ order_of_pos a
| (d+1) := λ hd hd0,
let ⟨a, ha⟩ := card_pos.1 hd0 in
have ha : order_of a = d.succ, from (mem_filter.1 ha).2,
have h : ∑ m in (range d.succ).filter (∣ d.succ),
(univ.filter (λ a : α, order_of a = m)).card =
∑ m in (range d.succ).filter (∣ d.succ), φ m, from
finset.sum_congr rfl
(λ m hm, have hmd : m < d.succ, from mem_range.1 (mem_filter.1 hm).1,
have hm : m ∣ d.succ, from (mem_filter.1 hm).2,
card_order_of_eq_totient_aux₁ (dvd.trans hm hd) (finset.card_pos.2
⟨a ^ (d.succ / m), mem_filter.2 ⟨mem_univ _,
by rw [order_of_pow, ha, gcd_eq_right (div_dvd_of_dvd hm),
nat.div_div_self hm (succ_pos _)]⟩⟩)),
have hinsert : insert d.succ ((range d.succ).filter (∣ d.succ))
= (range d.succ.succ).filter (∣ d.succ),
from (finset.ext $ λ x, ⟨λ h, (mem_insert.1 h).elim (λ h, by simp [h, range_succ])
(by clear _let_match; simp [range_succ]; tauto), by clear _let_match; simp [range_succ] {contextual := tt}; tauto⟩),
have hinsert₁ : d.succ ∉ (range d.succ).filter (∣ d.succ),
by simp [mem_range, zero_le_one, le_succ],
(add_left_inj (∑ m in (range d.succ).filter (∣ d.succ),
(univ.filter (λ a : α, order_of a = m)).card)).1
(calc _ = ∑ m in insert d.succ (filter (∣ d.succ) (range d.succ)),
(univ.filter (λ a : α, order_of a = m)).card :
eq.symm (finset.sum_insert (by simp [mem_range, zero_le_one, le_succ]))
... = ∑ m in (range d.succ.succ).filter (∣ d.succ),
(univ.filter (λ a : α, order_of a = m)).card :
sum_congr hinsert (λ _ _, rfl)
... = (univ.filter (λ a : α, a ^ d.succ = 1)).card :
sum_card_order_of_eq_card_pow_eq_one (succ_pos d)
... = ∑ m in (range d.succ.succ).filter (∣ d.succ), φ m :
ha ▸ (card_pow_eq_one_eq_order_of_aux hn a).symm ▸ (sum_totient _).symm
... = _ : by rw [h, ← sum_insert hinsert₁];
exact finset.sum_congr hinsert.symm (λ _ _, rfl))
lemma card_order_of_eq_totient_aux₂ {d : ℕ} (hd : d ∣ fintype.card α) :
(univ.filter (λ a : α, order_of a = d)).card = φ d :=
by_contradiction $ λ h,
have h0 : (univ.filter (λ a : α , order_of a = d)).card = 0 :=
not_not.1 (mt pos_iff_ne_zero.2 (mt (card_order_of_eq_totient_aux₁ hn hd) h)),
let c := fintype.card α in
have hc0 : 0 < c, from fintype.card_pos_iff.2 ⟨1⟩,
lt_irrefl c $
calc c = (univ.filter (λ a : α, a ^ c = 1)).card :
congr_arg card $ by simp [finset.ext_iff, c]
... = ∑ m in (range c.succ).filter (∣ c),
(univ.filter (λ a : α, order_of a = m)).card :
(sum_card_order_of_eq_card_pow_eq_one hc0).symm
... = ∑ m in ((range c.succ).filter (∣ c)).erase d,
(univ.filter (λ a : α, order_of a = m)).card :
eq.symm (sum_subset (erase_subset _ _) (λ m hm₁ hm₂,
have m = d, by simp at *; cc,
by simp [*, finset.ext_iff] at *; exact h0))
... ≤ ∑ m in ((range c.succ).filter (∣ c)).erase d, φ m :
sum_le_sum (λ m hm,
have hmc : m ∣ c, by simp at hm; tauto,
(imp_iff_not_or.1 (card_order_of_eq_totient_aux₁ hn hmc)).elim
(λ h, by simp [nat.le_zero_iff.1 (le_of_not_gt h), nat.zero_le])
(λ h, by rw h))
... < φ d + ∑ m in ((range c.succ).filter (∣ c)).erase d, φ m :
lt_add_of_pos_left _ (totient_pos (nat.pos_of_ne_zero
(λ h, pos_iff_ne_zero.1 hc0 (eq_zero_of_zero_dvd $ h ▸ hd))))
... = ∑ m in insert d (((range c.succ).filter (∣ c)).erase d), φ m : eq.symm (sum_insert (by simp))
... = ∑ m in (range c.succ).filter (∣ c), φ m : finset.sum_congr
(finset.insert_erase (mem_filter.2 ⟨mem_range.2 (lt_succ_of_le (le_of_dvd hc0 hd)), hd⟩)) (λ _ _, rfl)
... = c : sum_totient _
lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic α :=
have (univ.filter (λ a : α, order_of a = fintype.card α)).nonempty,
from (card_pos.1 $
by rw [card_order_of_eq_totient_aux₂ hn (dvd_refl _)];
exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)),
let ⟨x, hx⟩ := this in
is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2
end totient
lemma is_cyclic.card_order_of_eq_totient [group α] [is_cyclic α] [decidable_eq α] [fintype α]
{d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d :=
card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd
end cyclic
|
5bdcffafd09a3d17a2c032ae48526e380d263603 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/linear_algebra/eigenspace/basic.lean | 8de6594e40b3b5e00e8ff4c8d8d7e0605e6ac9e1 | [
"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 | 21,913 | lean | /-
Copyright (c) 2020 Alexander Bentkamp. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Alexander Bentkamp
-/
import algebra.algebra.spectrum
import linear_algebra.general_linear_group
import linear_algebra.finite_dimensional
/-!
# Eigenvectors and eigenvalues
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines eigenspaces, eigenvalues, and eigenvalues, as well as their generalized
counterparts. We follow Axler's approach [axler2015] because it allows us to derive many properties
without choosing a basis and without using matrices.
An eigenspace of a linear map `f` for a scalar `μ` is the kernel of the map `(f - μ • id)`. The
nonzero elements of an eigenspace are eigenvectors `x`. They have the property `f x = μ • x`. If
there are eigenvectors for a scalar `μ`, the scalar `μ` is called an eigenvalue.
There is no consensus in the literature whether `0` is an eigenvector. Our definition of
`has_eigenvector` permits only nonzero vectors. For an eigenvector `x` that may also be `0`, we
write `x ∈ f.eigenspace μ`.
A generalized eigenspace of a linear map `f` for a natural number `k` and a scalar `μ` is the kernel
of the map `(f - μ • id) ^ k`. The nonzero elements of a generalized eigenspace are generalized
eigenvectors `x`. If there are generalized eigenvectors for a natural number `k` and a scalar `μ`,
the scalar `μ` is called a generalized eigenvalue.
The fact that the eigenvalues are the roots of the minimal polynomial is proved in
`linear_algebra.eigenspace.minpoly`.
The existence of eigenvalues over an algebraically closed field
(and the fact that the generalized eigenspaces then span) is deferred to
`linear_algebra.eigenspace.is_alg_closed`.
## References
* [Sheldon Axler, *Linear Algebra Done Right*][axler2015]
* https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors
## Tags
eigenspace, eigenvector, eigenvalue, eigen
-/
universes u v w
namespace module
namespace End
open finite_dimensional
variables {K R : Type v} {V M : Type w}
[comm_ring R] [add_comm_group M] [module R M] [field K] [add_comm_group V] [module K V]
/-- The submodule `eigenspace f μ` for a linear map `f` and a scalar `μ` consists of all vectors `x`
such that `f x = μ • x`. (Def 5.36 of [axler2015])-/
def eigenspace (f : End R M) (μ : R) : submodule R M :=
(f - algebra_map R (End R M) μ).ker
@[simp] lemma eigenspace_zero (f : End R M) : f.eigenspace 0 = f.ker :=
by simp [eigenspace]
/-- A nonzero element of an eigenspace is an eigenvector. (Def 5.7 of [axler2015]) -/
def has_eigenvector (f : End R M) (μ : R) (x : M) : Prop :=
x ∈ eigenspace f μ ∧ x ≠ 0
/-- A scalar `μ` is an eigenvalue for a linear map `f` if there are nonzero vectors `x`
such that `f x = μ • x`. (Def 5.5 of [axler2015]) -/
def has_eigenvalue (f : End R M) (a : R) : Prop :=
eigenspace f a ≠ ⊥
/-- The eigenvalues of the endomorphism `f`, as a subtype of `R`. -/
def eigenvalues (f : End R M) : Type* := {μ : R // f.has_eigenvalue μ}
instance (f : End R M) : has_coe f.eigenvalues R := coe_subtype
lemma has_eigenvalue_of_has_eigenvector {f : End R M} {μ : R} {x : M} (h : has_eigenvector f μ x) :
has_eigenvalue f μ :=
begin
rw [has_eigenvalue, submodule.ne_bot_iff],
use x, exact h,
end
lemma mem_eigenspace_iff {f : End R M} {μ : R} {x : M} : x ∈ eigenspace f μ ↔ f x = μ • x :=
by rw [eigenspace, linear_map.mem_ker, linear_map.sub_apply, algebra_map_End_apply,
sub_eq_zero]
lemma has_eigenvector.apply_eq_smul {f : End R M} {μ : R} {x : M} (hx : f.has_eigenvector μ x) :
f x = μ • x :=
mem_eigenspace_iff.mp hx.1
lemma has_eigenvalue.exists_has_eigenvector {f : End R M} {μ : R} (hμ : f.has_eigenvalue μ) :
∃ v, f.has_eigenvector μ v :=
submodule.exists_mem_ne_zero_of_ne_bot hμ
lemma mem_spectrum_of_has_eigenvalue {f : End R M} {μ : R} (hμ : has_eigenvalue f μ) :
μ ∈ spectrum R f :=
begin
refine spectrum.mem_iff.mpr (λ h_unit, _),
set f' := linear_map.general_linear_group.to_linear_equiv h_unit.unit,
rcases hμ.exists_has_eigenvector with ⟨v, hv⟩,
refine hv.2 ((linear_map.ker_eq_bot'.mp f'.ker) v (_ : μ • v - f v = 0)),
rw [hv.apply_eq_smul, sub_self]
end
lemma has_eigenvalue_iff_mem_spectrum [finite_dimensional K V] {f : End K V} {μ : K} :
f.has_eigenvalue μ ↔ μ ∈ spectrum K f :=
iff.intro mem_spectrum_of_has_eigenvalue
(λ h, by rwa [spectrum.mem_iff, is_unit.sub_iff, linear_map.is_unit_iff_ker_eq_bot] at h)
lemma eigenspace_div (f : End K V) (a b : K) (hb : b ≠ 0) :
eigenspace f (a / b) = (b • f - algebra_map K (End K V) a).ker :=
calc
eigenspace f (a / b) = eigenspace f (b⁻¹ * a) : by { rw [div_eq_mul_inv, mul_comm] }
... = (f - (b⁻¹ * a) • linear_map.id).ker : rfl
... = (f - b⁻¹ • a • linear_map.id).ker : by rw smul_smul
... = (f - b⁻¹ • algebra_map K (End K V) a).ker : rfl
... = (b • (f - b⁻¹ • algebra_map K (End K V) a)).ker : by rw linear_map.ker_smul _ b hb
... = (b • f - algebra_map K (End K V) a).ker : by rw [smul_sub, smul_inv_smul₀ hb]
/-- The eigenspaces of a linear operator form an independent family of subspaces of `V`. That is,
any eigenspace has trivial intersection with the span of all the other eigenspaces. -/
lemma eigenspaces_independent (f : End K V) : complete_lattice.independent f.eigenspace :=
begin
classical,
-- Define an operation from `Π₀ μ : K, f.eigenspace μ`, the vector space of of finitely-supported
-- choices of an eigenvector from each eigenspace, to `V`, by sending a collection to its sum.
let S : @linear_map K K _ _ (ring_hom.id K) (Π₀ μ : K, f.eigenspace μ) V
(@dfinsupp.add_comm_monoid K (λ μ, f.eigenspace μ) _) _
(@dfinsupp.module K _ (λ μ, f.eigenspace μ) _ _ _) _ :=
@dfinsupp.lsum K K ℕ _ V _ _ _ _ _ _ _ _ _
(λ μ, (f.eigenspace μ).subtype),
-- We need to show that if a finitely-supported collection `l` of representatives of the
-- eigenspaces has sum `0`, then it itself is zero.
suffices : ∀ l : Π₀ μ, f.eigenspace μ, S l = 0 → l = 0,
{ rw complete_lattice.independent_iff_dfinsupp_lsum_injective,
change function.injective S,
rw ← @linear_map.ker_eq_bot K K (Π₀ μ, (f.eigenspace μ)) V _ _
(@dfinsupp.add_comm_group K (λ μ, f.eigenspace μ) _),
rw eq_bot_iff,
exact this },
intros l hl,
-- We apply induction on the finite set of eigenvalues from which `l` selects a nonzero
-- eigenvector, i.e. on the support of `l`.
induction h_l_support : l.support using finset.induction with μ₀ l_support' hμ₀ ih generalizing l,
-- If the support is empty, all coefficients are zero and we are done.
{ exact dfinsupp.support_eq_empty.1 h_l_support },
-- Now assume that the support of `l` contains at least one eigenvalue `μ₀`. We define a new
-- collection of representatives `l'` to apply the induction hypothesis on later. The collection
-- of representatives `l'` is derived from `l` by multiplying the coefficient of the eigenvector
-- with eigenvalue `μ` by `μ - μ₀`.
{ let l' := dfinsupp.map_range.linear_map
(λ μ, (μ - μ₀) • @linear_map.id K (f.eigenspace μ) _ _ _) l,
-- The support of `l'` is the support of `l` without `μ₀`.
have h_l_support' : l'.support = l_support',
{ rw [← finset.erase_insert hμ₀, ← h_l_support],
ext a,
have : ¬(a = μ₀ ∨ l a = 0) ↔ ¬a = μ₀ ∧ ¬l a = 0 := not_or_distrib,
simp only [l', dfinsupp.map_range.linear_map_apply, dfinsupp.map_range_apply,
dfinsupp.mem_support_iff, finset.mem_erase, id.def, linear_map.id_coe,
linear_map.smul_apply, ne.def, smul_eq_zero, sub_eq_zero, this] },
-- The entries of `l'` add up to `0`.
have total_l' : S l' = 0,
{ let g := f - algebra_map K (End K V) μ₀,
let a : Π₀ μ : K, V := dfinsupp.map_range.linear_map (λ μ, (f.eigenspace μ).subtype) l,
calc S l'
= dfinsupp.lsum ℕ (λ μ, (f.eigenspace μ).subtype.comp ((μ - μ₀) • linear_map.id)) l : _
... = dfinsupp.lsum ℕ (λ μ, g.comp (f.eigenspace μ).subtype) l : _
... = dfinsupp.lsum ℕ (λ μ, g) a : _
... = g (dfinsupp.lsum ℕ (λ μ, (linear_map.id : V →ₗ[K] V)) a) : _
... = g (S l) : _
... = 0 : by rw [hl, g.map_zero],
{ exact dfinsupp.sum_map_range_index.linear_map },
{ congr,
ext μ v,
simp only [g, eq_self_iff_true, function.comp_app, id.def, linear_map.coe_comp,
linear_map.id_coe, linear_map.smul_apply, linear_map.sub_apply,
module.algebra_map_End_apply, sub_left_inj, sub_smul, submodule.coe_smul_of_tower,
submodule.coe_sub, submodule.subtype_apply, mem_eigenspace_iff.1 v.prop], },
{ rw dfinsupp.sum_map_range_index.linear_map },
{ simp only [dfinsupp.sum_add_hom_apply, linear_map.id_coe, linear_map.map_dfinsupp_sum,
id.def, linear_map.to_add_monoid_hom_coe, dfinsupp.lsum_apply_apply], },
{ congr,
simp only [S, a, dfinsupp.sum_map_range_index.linear_map, linear_map.id_comp] } },
-- Therefore, by the induction hypothesis, all entries of `l'` are zero.
have l'_eq_0 := ih l' total_l' h_l_support',
-- By the definition of `l'`, this means that `(μ - μ₀) • l μ = 0` for all `μ`.
have h_smul_eq_0 : ∀ μ, (μ - μ₀) • l μ = 0,
{ intro μ,
calc (μ - μ₀) • l μ = l' μ : by simp only [l', linear_map.id_coe, id.def,
linear_map.smul_apply, dfinsupp.map_range_apply, dfinsupp.map_range.linear_map_apply]
... = 0 : by { rw [l'_eq_0], refl } },
-- Thus, the eigenspace-representatives in `l` for all `μ ≠ μ₀` are `0`.
have h_lμ_eq_0 : ∀ μ : K, μ ≠ μ₀ → l μ = 0,
{ intros μ hμ,
apply or_iff_not_imp_left.1 (smul_eq_zero.1 (h_smul_eq_0 μ)),
rwa [sub_eq_zero] },
-- So if we sum over all these representatives, we obtain `0`.
have h_sum_l_support'_eq_0 : finset.sum l_support' (λ μ, (l μ : V)) = 0,
{ rw ←finset.sum_const_zero,
apply finset.sum_congr rfl,
intros μ hμ,
rw [submodule.coe_eq_zero, h_lμ_eq_0],
rintro rfl,
exact hμ₀ hμ },
-- The only potentially nonzero eigenspace-representative in `l` is the one corresponding to
-- `μ₀`. But since the overall sum is `0` by assumption, this representative must also be `0`.
have : l μ₀ = 0,
{ simp only [S, dfinsupp.lsum_apply_apply, dfinsupp.sum_add_hom_apply,
linear_map.to_add_monoid_hom_coe, dfinsupp.sum, h_l_support, submodule.subtype_apply,
submodule.coe_eq_zero, finset.sum_insert hμ₀, h_sum_l_support'_eq_0, add_zero] at hl,
exact hl },
-- Thus, all coefficients in `l` are `0`.
show l = 0,
{ ext μ,
by_cases h_cases : μ = μ₀,
{ rwa [h_cases, set_like.coe_eq_coe, dfinsupp.coe_zero, pi.zero_apply] },
exact congr_arg (coe : _ → V) (h_lμ_eq_0 μ h_cases) }}
end
/-- Eigenvectors corresponding to distinct eigenvalues of a linear operator are linearly
independent. (Lemma 5.10 of [axler2015])
We use the eigenvalues as indexing set to ensure that there is only one eigenvector for each
eigenvalue in the image of `xs`. -/
lemma eigenvectors_linear_independent (f : End K V) (μs : set K) (xs : μs → V)
(h_eigenvec : ∀ μ : μs, f.has_eigenvector μ (xs μ)) :
linear_independent K xs :=
complete_lattice.independent.linear_independent _
(f.eigenspaces_independent.comp subtype.coe_injective)
(λ μ, (h_eigenvec μ).1) (λ μ, (h_eigenvec μ).2)
/-- The generalized eigenspace for a linear map `f`, a scalar `μ`, and an exponent `k ∈ ℕ` is the
kernel of `(f - μ • id) ^ k`. (Def 8.10 of [axler2015]). Furthermore, a generalized eigenspace for
some exponent `k` is contained in the generalized eigenspace for exponents larger than `k`. -/
def generalized_eigenspace (f : End R M) (μ : R) : ℕ →o submodule R M :=
{ to_fun := λ k, ((f - algebra_map R (End R M) μ) ^ k).ker,
monotone' := λ k m hm,
begin
simp only [← pow_sub_mul_pow _ hm],
exact linear_map.ker_le_ker_comp
((f - algebra_map R (End R M) μ) ^ k) ((f - algebra_map R (End R M) μ) ^ (m - k)),
end }
@[simp] lemma mem_generalized_eigenspace (f : End R M) (μ : R) (k : ℕ) (m : M) :
m ∈ f.generalized_eigenspace μ k ↔ ((f - μ • 1)^k) m = 0 :=
iff.rfl
@[simp] lemma generalized_eigenspace_zero (f : End R M) (k : ℕ) :
f.generalized_eigenspace 0 k = (f^k).ker :=
by simp [module.End.generalized_eigenspace]
/-- A nonzero element of a generalized eigenspace is a generalized eigenvector.
(Def 8.9 of [axler2015])-/
def has_generalized_eigenvector (f : End R M) (μ : R) (k : ℕ) (x : M) : Prop :=
x ≠ 0 ∧ x ∈ generalized_eigenspace f μ k
/-- A scalar `μ` is a generalized eigenvalue for a linear map `f` and an exponent `k ∈ ℕ` if there
are generalized eigenvectors for `f`, `k`, and `μ`. -/
def has_generalized_eigenvalue (f : End R M) (μ : R) (k : ℕ) : Prop :=
generalized_eigenspace f μ k ≠ ⊥
/-- The generalized eigenrange for a linear map `f`, a scalar `μ`, and an exponent `k ∈ ℕ` is the
range of `(f - μ • id) ^ k`. -/
def generalized_eigenrange (f : End R M) (μ : R) (k : ℕ) : submodule R M :=
((f - algebra_map R (End R M) μ) ^ k).range
/-- The exponent of a generalized eigenvalue is never 0. -/
lemma exp_ne_zero_of_has_generalized_eigenvalue {f : End R M} {μ : R} {k : ℕ}
(h : f.has_generalized_eigenvalue μ k) : k ≠ 0 :=
begin
rintro rfl,
exact h linear_map.ker_id
end
/-- The union of the kernels of `(f - μ • id) ^ k` over all `k`. -/
def maximal_generalized_eigenspace (f : End R M) (μ : R) : submodule R M :=
⨆ k, f.generalized_eigenspace μ k
lemma generalized_eigenspace_le_maximal (f : End R M) (μ : R) (k : ℕ) :
f.generalized_eigenspace μ k ≤ f.maximal_generalized_eigenspace μ :=
le_supr _ _
@[simp] lemma mem_maximal_generalized_eigenspace (f : End R M) (μ : R) (m : M) :
m ∈ f.maximal_generalized_eigenspace μ ↔ ∃ (k : ℕ), ((f - μ • 1)^k) m = 0 :=
by simp only [maximal_generalized_eigenspace, ← mem_generalized_eigenspace,
submodule.mem_supr_of_chain]
/-- If there exists a natural number `k` such that the kernel of `(f - μ • id) ^ k` is the
maximal generalized eigenspace, then this value is the least such `k`. If not, this value is not
meaningful. -/
noncomputable def maximal_generalized_eigenspace_index (f : End R M) (μ : R) :=
monotonic_sequence_limit_index (f.generalized_eigenspace μ)
/-- For an endomorphism of a Noetherian module, the maximal eigenspace is always of the form kernel
`(f - μ • id) ^ k` for some `k`. -/
lemma maximal_generalized_eigenspace_eq [h : is_noetherian R M] (f : End R M) (μ : R) :
maximal_generalized_eigenspace f μ =
f.generalized_eigenspace μ (maximal_generalized_eigenspace_index f μ) :=
begin
rw is_noetherian_iff_well_founded at h,
exact (well_founded.supr_eq_monotonic_sequence_limit h (f.generalized_eigenspace μ) : _),
end
/-- A generalized eigenvalue for some exponent `k` is also
a generalized eigenvalue for exponents larger than `k`. -/
lemma has_generalized_eigenvalue_of_has_generalized_eigenvalue_of_le
{f : End R M} {μ : R} {k : ℕ} {m : ℕ} (hm : k ≤ m) (hk : f.has_generalized_eigenvalue μ k) :
f.has_generalized_eigenvalue μ m :=
begin
unfold has_generalized_eigenvalue at *,
contrapose! hk,
rw [←le_bot_iff, ←hk],
exact (f.generalized_eigenspace μ).monotone hm,
end
/-- The eigenspace is a subspace of the generalized eigenspace. -/
lemma eigenspace_le_generalized_eigenspace {f : End R M} {μ : R} {k : ℕ} (hk : 0 < k) :
f.eigenspace μ ≤ f.generalized_eigenspace μ k :=
(f.generalized_eigenspace μ).monotone (nat.succ_le_of_lt hk)
/-- All eigenvalues are generalized eigenvalues. -/
lemma has_generalized_eigenvalue_of_has_eigenvalue
{f : End R M} {μ : R} {k : ℕ} (hk : 0 < k) (hμ : f.has_eigenvalue μ) :
f.has_generalized_eigenvalue μ k :=
begin
apply has_generalized_eigenvalue_of_has_generalized_eigenvalue_of_le hk,
rw [has_generalized_eigenvalue, generalized_eigenspace, order_hom.coe_fun_mk, pow_one],
exact hμ,
end
/-- All generalized eigenvalues are eigenvalues. -/
lemma has_eigenvalue_of_has_generalized_eigenvalue
{f : End R M} {μ : R} {k : ℕ} (hμ : f.has_generalized_eigenvalue μ k) :
f.has_eigenvalue μ :=
begin
intros contra, apply hμ,
erw linear_map.ker_eq_bot at ⊢ contra, rw linear_map.coe_pow,
exact function.injective.iterate contra k,
end
/-- Generalized eigenvalues are actually just eigenvalues. -/
@[simp] lemma has_generalized_eigenvalue_iff_has_eigenvalue
{f : End R M} {μ : R} {k : ℕ} (hk : 0 < k) :
f.has_generalized_eigenvalue μ k ↔ f.has_eigenvalue μ :=
⟨has_eigenvalue_of_has_generalized_eigenvalue, has_generalized_eigenvalue_of_has_eigenvalue hk⟩
/-- Every generalized eigenvector is a generalized eigenvector for exponent `finrank K V`.
(Lemma 8.11 of [axler2015]) -/
lemma generalized_eigenspace_le_generalized_eigenspace_finrank
[finite_dimensional K V] (f : End K V) (μ : K) (k : ℕ) :
f.generalized_eigenspace μ k ≤ f.generalized_eigenspace μ (finrank K V) :=
ker_pow_le_ker_pow_finrank _ _
/-- Generalized eigenspaces for exponents at least `finrank K V` are equal to each other. -/
lemma generalized_eigenspace_eq_generalized_eigenspace_finrank_of_le [finite_dimensional K V]
(f : End K V) (μ : K) {k : ℕ} (hk : finrank K V ≤ k) :
f.generalized_eigenspace μ k = f.generalized_eigenspace μ (finrank K V) :=
ker_pow_eq_ker_pow_finrank_of_le hk
/-- If `f` maps a subspace `p` into itself, then the generalized eigenspace of the restriction
of `f` to `p` is the part of the generalized eigenspace of `f` that lies in `p`. -/
lemma generalized_eigenspace_restrict
(f : End R M) (p : submodule R M) (k : ℕ) (μ : R) (hfp : ∀ (x : M), x ∈ p → f x ∈ p) :
generalized_eigenspace (linear_map.restrict f hfp) μ k =
submodule.comap p.subtype (f.generalized_eigenspace μ k) :=
begin
simp only [generalized_eigenspace, order_hom.coe_fun_mk, ← linear_map.ker_comp],
induction k with k ih,
{ rw [pow_zero, pow_zero, linear_map.one_eq_id],
apply (submodule.ker_subtype _).symm },
{ erw [pow_succ', pow_succ', linear_map.ker_comp, linear_map.ker_comp, ih,
← linear_map.ker_comp, linear_map.comp_assoc] },
end
/-- If `p` is an invariant submodule of an endomorphism `f`, then the `μ`-eigenspace of the
restriction of `f` to `p` is a submodule of the `μ`-eigenspace of `f`. -/
lemma eigenspace_restrict_le_eigenspace (f : End R M) {p : submodule R M}
(hfp : ∀ x ∈ p, f x ∈ p) (μ : R) :
(eigenspace (f.restrict hfp) μ).map p.subtype ≤ f.eigenspace μ :=
begin
rintros a ⟨x, hx, rfl⟩,
simp only [set_like.mem_coe, mem_eigenspace_iff, linear_map.restrict_apply] at hx ⊢,
exact congr_arg coe hx
end
/-- Generalized eigenrange and generalized eigenspace for exponent `finrank K V` are disjoint. -/
lemma generalized_eigenvec_disjoint_range_ker [finite_dimensional K V] (f : End K V) (μ : K) :
disjoint (f.generalized_eigenrange μ (finrank K V)) (f.generalized_eigenspace μ (finrank K V)) :=
begin
have h := calc
submodule.comap ((f - algebra_map _ _ μ) ^ finrank K V)
(f.generalized_eigenspace μ (finrank K V))
= ((f - algebra_map _ _ μ) ^ finrank K V *
(f - algebra_map K (End K V) μ) ^ finrank K V).ker :
by { simpa only [generalized_eigenspace, order_hom.coe_fun_mk, ← linear_map.ker_comp] }
... = f.generalized_eigenspace μ (finrank K V + finrank K V) :
by { rw ←pow_add, refl }
... = f.generalized_eigenspace μ (finrank K V) :
by { rw generalized_eigenspace_eq_generalized_eigenspace_finrank_of_le, linarith },
rw [disjoint_iff_inf_le, generalized_eigenrange, linear_map.range_eq_map,
submodule.map_inf_eq_map_inf_comap, top_inf_eq, h],
apply submodule.map_comap_le
end
/-- If an invariant subspace `p` of an endomorphism `f` is disjoint from the `μ`-eigenspace of `f`,
then the restriction of `f` to `p` has trivial `μ`-eigenspace. -/
lemma eigenspace_restrict_eq_bot {f : End R M} {p : submodule R M}
(hfp : ∀ x ∈ p, f x ∈ p) {μ : R} (hμp : disjoint (f.eigenspace μ) p) :
eigenspace (f.restrict hfp) μ = ⊥ :=
begin
rw eq_bot_iff,
intros x hx,
simpa using hμp.le_bot ⟨eigenspace_restrict_le_eigenspace f hfp μ ⟨x, hx, rfl⟩, x.prop⟩,
end
/-- The generalized eigenspace of an eigenvalue has positive dimension for positive exponents. -/
lemma pos_finrank_generalized_eigenspace_of_has_eigenvalue [finite_dimensional K V]
{f : End K V} {k : ℕ} {μ : K} (hx : f.has_eigenvalue μ) (hk : 0 < k):
0 < finrank K (f.generalized_eigenspace μ k) :=
calc
0 = finrank K (⊥ : submodule K V) : by rw finrank_bot
... < finrank K (f.eigenspace μ) : submodule.finrank_lt_finrank_of_lt (bot_lt_iff_ne_bot.2 hx)
... ≤ finrank K (f.generalized_eigenspace μ k) :
submodule.finrank_mono ((f.generalized_eigenspace μ).monotone (nat.succ_le_of_lt hk))
/-- A linear map maps a generalized eigenrange into itself. -/
lemma map_generalized_eigenrange_le {f : End K V} {μ : K} {n : ℕ} :
submodule.map f (f.generalized_eigenrange μ n) ≤ f.generalized_eigenrange μ n :=
calc submodule.map f (f.generalized_eigenrange μ n)
= (f * ((f - algebra_map _ _ μ) ^ n)).range : (linear_map.range_comp _ _).symm
... = (((f - algebra_map _ _ μ) ^ n) * f).range : by rw algebra.mul_sub_algebra_map_pow_commutes
... = submodule.map ((f - algebra_map _ _ μ) ^ n) f.range : linear_map.range_comp _ _
... ≤ f.generalized_eigenrange μ n : linear_map.map_le_range
end End
end module
|
a38ac60be2c4d900da923569bfd6ce61bd861c97 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/algebra/subalgebra/basic.lean | f0ef0af5aec095b0a5c20ed6be16d1e0e39ce822 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 47,401 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Yury Kudryashov
-/
import algebra.algebra.basic
import data.set.Union_lift
import linear_algebra.finsupp
import ring_theory.ideal.operations
/-!
# Subalgebras over Commutative Semiring
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we define `subalgebra`s and the usual operations on them (`map`, `comap`).
More lemmas about `adjoin` can be found in `ring_theory.adjoin`.
-/
universes u u' v w w'
open_locale big_operators
set_option old_structure_cmd true
/-- A subalgebra is a sub(semi)ring that includes the range of `algebra_map`. -/
structure subalgebra (R : Type u) (A : Type v)
[comm_semiring R] [semiring A] [algebra R A] extends subsemiring A : Type v :=
(algebra_map_mem' : ∀ r, algebra_map R A r ∈ carrier)
(zero_mem' := (algebra_map R A).map_zero ▸ algebra_map_mem' 0)
(one_mem' := (algebra_map R A).map_one ▸ algebra_map_mem' 1)
/-- Reinterpret a `subalgebra` as a `subsemiring`. -/
add_decl_doc subalgebra.to_subsemiring
namespace subalgebra
variables {R' : Type u'} {R : Type u} {A : Type v} {B : Type w} {C : Type w'}
variables [comm_semiring R]
variables [semiring A] [algebra R A] [semiring B] [algebra R B] [semiring C] [algebra R C]
include R
instance : set_like (subalgebra R A) A :=
{ coe := subalgebra.carrier,
coe_injective' := λ p q h, by cases p; cases q; congr' }
instance : subsemiring_class (subalgebra R A) A :=
{ add_mem := add_mem',
mul_mem := mul_mem',
one_mem := one_mem',
zero_mem := zero_mem' }
@[simp]
lemma mem_carrier {s : subalgebra R A} {x : A} : x ∈ s.carrier ↔ x ∈ s := iff.rfl
@[ext] theorem ext {S T : subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h
@[simp] lemma mem_to_subsemiring {S : subalgebra R A} {x} : x ∈ S.to_subsemiring ↔ x ∈ S := iff.rfl
@[simp] lemma coe_to_subsemiring (S : subalgebra R A) : (↑S.to_subsemiring : set A) = S := rfl
theorem to_subsemiring_injective :
function.injective (to_subsemiring : subalgebra R A → subsemiring A) :=
λ S T h, ext $ λ x, by rw [← mem_to_subsemiring, ← mem_to_subsemiring, h]
theorem to_subsemiring_inj {S U : subalgebra R A} : S.to_subsemiring = U.to_subsemiring ↔ S = U :=
to_subsemiring_injective.eq_iff
/-- Copy of a subalgebra with a new `carrier` equal to the old one. Useful to fix definitional
equalities. -/
protected def copy (S : subalgebra R A) (s : set A) (hs : s = ↑S) : subalgebra R A :=
{ carrier := s,
add_mem' := λ _ _, hs.symm ▸ S.add_mem',
mul_mem' := λ _ _, hs.symm ▸ S.mul_mem',
algebra_map_mem' := hs.symm ▸ S.algebra_map_mem' }
@[simp] lemma coe_copy (S : subalgebra R A) (s : set A) (hs : s = ↑S) :
(S.copy s hs : set A) = s := rfl
lemma copy_eq (S : subalgebra R A) (s : set A) (hs : s = ↑S) : S.copy s hs = S :=
set_like.coe_injective hs
variables (S : subalgebra R A)
theorem algebra_map_mem (r : R) : algebra_map R A r ∈ S :=
S.algebra_map_mem' r
theorem srange_le : (algebra_map R A).srange ≤ S.to_subsemiring :=
λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r
theorem range_subset : set.range (algebra_map R A) ⊆ S :=
λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r
theorem range_le : set.range (algebra_map R A) ≤ S :=
S.range_subset
theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r • x ∈ S :=
(algebra.smul_def r x).symm ▸ mul_mem (S.algebra_map_mem r) hx
instance : smul_mem_class (subalgebra R A) R A :=
{ smul_mem := λ S r x hx, smul_mem S hx r }
protected theorem one_mem : (1 : A) ∈ S := one_mem S
protected theorem mul_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x * y ∈ S := mul_mem hx hy
protected theorem pow_mem {x : A} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := pow_mem hx n
protected theorem zero_mem : (0 : A) ∈ S := zero_mem S
protected theorem add_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x + y ∈ S := add_mem hx hy
protected theorem nsmul_mem {x : A} (hx : x ∈ S) (n : ℕ) : n • x ∈ S := nsmul_mem hx n
protected theorem coe_nat_mem (n : ℕ) : (n : A) ∈ S := coe_nat_mem S n
protected theorem list_prod_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.prod ∈ S := list_prod_mem h
protected theorem list_sum_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.sum ∈ S := list_sum_mem h
protected theorem multiset_sum_mem {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.sum ∈ S :=
multiset_sum_mem m h
protected theorem sum_mem {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) :
∑ x in t, f x ∈ S :=
sum_mem h
protected theorem multiset_prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A]
[algebra R A] (S : subalgebra R A) {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.prod ∈ S :=
multiset_prod_mem m h
protected theorem prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A]
[algebra R A] (S : subalgebra R A) {ι : Type w} {t : finset ι} {f : ι → A}
(h : ∀ x ∈ t, f x ∈ S) : ∏ x in t, f x ∈ S :=
prod_mem h
instance {R A : Type*} [comm_ring R] [ring A] [algebra R A] : subring_class (subalgebra R A) A :=
{ neg_mem := λ S x hx, neg_one_smul R x ▸ S.smul_mem hx _,
.. subalgebra.subsemiring_class }
protected theorem neg_mem {R : Type u} {A : Type v} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : -x ∈ S :=
neg_mem hx
protected theorem sub_mem {R : Type u} {A : Type v} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S :=
sub_mem hx hy
protected theorem zsmul_mem {R : Type u} {A : Type v} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) (n : ℤ) : n • x ∈ S :=
zsmul_mem hx n
protected theorem coe_int_mem {R : Type u} {A : Type v} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) (n : ℤ) : (n : A) ∈ S :=
coe_int_mem S n
/-- The projection from a subalgebra of `A` to an additive submonoid of `A`. -/
def to_add_submonoid {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A]
(S : subalgebra R A) : add_submonoid A :=
S.to_subsemiring.to_add_submonoid
/-- The projection from a subalgebra of `A` to a submonoid of `A`. -/
def to_submonoid {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A]
(S : subalgebra R A) : submonoid A :=
S.to_subsemiring.to_submonoid
/-- A subalgebra over a ring is also a `subring`. -/
def to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) :
subring A :=
{ neg_mem' := λ _, S.neg_mem,
.. S.to_subsemiring }
@[simp] lemma mem_to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A]
{S : subalgebra R A} {x} : x ∈ S.to_subring ↔ x ∈ S := iff.rfl
@[simp] lemma coe_to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A]
(S : subalgebra R A) : (↑S.to_subring : set A) = S := rfl
theorem to_subring_injective {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] :
function.injective (to_subring : subalgebra R A → subring A) :=
λ S T h, ext $ λ x, by rw [← mem_to_subring, ← mem_to_subring, h]
theorem to_subring_inj {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A]
{S U : subalgebra R A} : S.to_subring = U.to_subring ↔ S = U :=
to_subring_injective.eq_iff
instance : inhabited S := ⟨(0 : S.to_subsemiring)⟩
section
/-! `subalgebra`s inherit structure from their `subsemiring` / `semiring` coercions. -/
instance to_semiring {R A}
[comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) :
semiring S := S.to_subsemiring.to_semiring
instance to_comm_semiring {R A}
[comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) :
comm_semiring S := S.to_subsemiring.to_comm_semiring
instance to_ring {R A}
[comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) :
ring S := S.to_subring.to_ring
instance to_comm_ring {R A}
[comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) :
comm_ring S := S.to_subring.to_comm_ring
instance to_ordered_semiring {R A}
[comm_semiring R] [ordered_semiring A] [algebra R A] (S : subalgebra R A) :
ordered_semiring S := S.to_subsemiring.to_ordered_semiring
instance to_strict_ordered_semiring {R A}
[comm_semiring R] [strict_ordered_semiring A] [algebra R A] (S : subalgebra R A) :
strict_ordered_semiring S := S.to_subsemiring.to_strict_ordered_semiring
instance to_ordered_comm_semiring {R A}
[comm_semiring R] [ordered_comm_semiring A] [algebra R A] (S : subalgebra R A) :
ordered_comm_semiring S := S.to_subsemiring.to_ordered_comm_semiring
instance to_strict_ordered_comm_semiring {R A}
[comm_semiring R] [strict_ordered_comm_semiring A] [algebra R A] (S : subalgebra R A) :
strict_ordered_comm_semiring S := S.to_subsemiring.to_strict_ordered_comm_semiring
instance to_ordered_ring {R A}
[comm_ring R] [ordered_ring A] [algebra R A] (S : subalgebra R A) :
ordered_ring S := S.to_subring.to_ordered_ring
instance to_ordered_comm_ring {R A}
[comm_ring R] [ordered_comm_ring A] [algebra R A] (S : subalgebra R A) :
ordered_comm_ring S := S.to_subring.to_ordered_comm_ring
instance to_linear_ordered_semiring {R A}
[comm_semiring R] [linear_ordered_semiring A] [algebra R A] (S : subalgebra R A) :
linear_ordered_semiring S := S.to_subsemiring.to_linear_ordered_semiring
instance to_linear_ordered_comm_semiring {R A}
[comm_semiring R] [linear_ordered_comm_semiring A] [algebra R A] (S : subalgebra R A) :
linear_ordered_comm_semiring S := S.to_subsemiring.to_linear_ordered_comm_semiring
instance to_linear_ordered_ring {R A}
[comm_ring R] [linear_ordered_ring A] [algebra R A] (S : subalgebra R A) :
linear_ordered_ring S := S.to_subring.to_linear_ordered_ring
instance to_linear_ordered_comm_ring {R A}
[comm_ring R] [linear_ordered_comm_ring A] [algebra R A] (S : subalgebra R A) :
linear_ordered_comm_ring S := S.to_subring.to_linear_ordered_comm_ring
end
/-- The forgetful map from `subalgebra` to `submodule` as an `order_embedding` -/
def to_submodule : subalgebra R A ↪o submodule R A :=
{ to_embedding :=
{ to_fun := λ S,
{ carrier := S,
zero_mem' := (0:S).2,
add_mem' := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2,
smul_mem' := λ c x hx, (algebra.smul_def c x).symm ▸
(⟨algebra_map R A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 },
inj' := λ S T h, ext $ by apply set_like.ext_iff.1 h },
map_rel_iff' := λ S T, set_like.coe_subset_coe.symm.trans set_like.coe_subset_coe }
/- TODO: bundle other forgetful maps between algebraic substructures, e.g.
`to_subsemiring` and `to_subring` in this file. -/
@[simp] lemma mem_to_submodule {x} : x ∈ S.to_submodule ↔ x ∈ S := iff.rfl
@[simp] lemma coe_to_submodule (S : subalgebra R A) : (↑S.to_submodule : set A) = S := rfl
section
/-! `subalgebra`s inherit structure from their `submodule` coercions. -/
instance module' [semiring R'] [has_smul R' R] [module R' A] [is_scalar_tower R' R A] :
module R' S :=
S.to_submodule.module'
instance : module R S := S.module'
instance [semiring R'] [has_smul R' R] [module R' A] [is_scalar_tower R' R A] :
is_scalar_tower R' R S :=
S.to_submodule.is_scalar_tower
instance algebra' [comm_semiring R'] [has_smul R' R] [algebra R' A]
[is_scalar_tower R' R A] : algebra R' S :=
{ commutes' := λ c x, subtype.eq $ algebra.commutes _ _,
smul_def' := λ c x, subtype.eq $ algebra.smul_def _ _,
.. (algebra_map R' A).cod_restrict S $ λ x, begin
rw [algebra.algebra_map_eq_smul_one, ←smul_one_smul R x (1 : A),
←algebra.algebra_map_eq_smul_one],
exact algebra_map_mem S _,
end }
instance : algebra R S := S.algebra'
end
instance no_zero_smul_divisors_bot [no_zero_smul_divisors R A] : no_zero_smul_divisors R S :=
⟨λ c x h,
have c = 0 ∨ (x : A) = 0,
from eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg coe h),
this.imp_right (@subtype.ext_iff _ _ x 0).mpr⟩
protected lemma coe_add (x y : S) : (↑(x + y) : A) = ↑x + ↑y := rfl
protected lemma coe_mul (x y : S) : (↑(x * y) : A) = ↑x * ↑y := rfl
protected lemma coe_zero : ((0 : S) : A) = 0 := rfl
protected lemma coe_one : ((1 : S) : A) = 1 := rfl
protected lemma coe_neg {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A]
{S : subalgebra R A} (x : S) : (↑(-x) : A) = -↑x := rfl
protected lemma coe_sub {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A]
{S : subalgebra R A} (x y : S) : (↑(x - y) : A) = ↑x - ↑y := rfl
@[simp, norm_cast] lemma coe_smul [semiring R'] [has_smul R' R] [module R' A]
[is_scalar_tower R' R A] (r : R') (x : S) : (↑(r • x) : A) = r • ↑x := rfl
@[simp, norm_cast] lemma coe_algebra_map [comm_semiring R'] [has_smul R' R] [algebra R' A]
[is_scalar_tower R' R A] (r : R') :
↑(algebra_map R' S r) = algebra_map R' A r := rfl
protected lemma coe_pow (x : S) (n : ℕ) : (↑(x^n) : A) = (↑x)^n := submonoid_class.coe_pow x n
protected lemma coe_eq_zero {x : S} : (x : A) = 0 ↔ x = 0 := zero_mem_class.coe_eq_zero
protected lemma coe_eq_one {x : S} : (x : A) = 1 ↔ x = 1 := one_mem_class.coe_eq_one
-- todo: standardize on the names these morphisms
-- compare with submodule.subtype
/-- Embedding of a subalgebra into the algebra. -/
def val : S →ₐ[R] A :=
by refine_struct { to_fun := (coe : S → A) }; intros; refl
@[simp] lemma coe_val : (S.val : S → A) = coe := rfl
lemma val_apply (x : S) : S.val x = (x : A) := rfl
@[simp] lemma to_subsemiring_subtype : S.to_subsemiring.subtype = (S.val : S →+* A) :=
rfl
@[simp] lemma to_subring_subtype {R A : Type*} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) : S.to_subring.subtype = (S.val : S →+* A) :=
rfl
/-- Linear equivalence between `S : submodule R A` and `S`. Though these types are equal,
we define it as a `linear_equiv` to avoid type equalities. -/
def to_submodule_equiv (S : subalgebra R A) : S.to_submodule ≃ₗ[R] S :=
linear_equiv.of_eq _ _ rfl
/-- Transport a subalgebra via an algebra homomorphism. -/
def map (f : A →ₐ[R] B) (S : subalgebra R A) : subalgebra R B :=
{ algebra_map_mem' := λ r, f.commutes r ▸ set.mem_image_of_mem _ (S.algebra_map_mem r),
.. S.to_subsemiring.map (f : A →+* B) }
lemma map_mono {S₁ S₂ : subalgebra R A} {f : A →ₐ[R] B} :
S₁ ≤ S₂ → S₁.map f ≤ S₂.map f :=
set.image_subset f
lemma map_injective {f : A →ₐ[R] B} (hf : function.injective f) :
function.injective (map f) :=
λ S₁ S₂ ih, ext $ set.ext_iff.1 $ set.image_injective.2 hf $ set.ext $ set_like.ext_iff.mp ih
@[simp] lemma map_id (S : subalgebra R A) : S.map (alg_hom.id R A) = S :=
set_like.coe_injective $ set.image_id _
lemma map_map (S : subalgebra R A) (g : B →ₐ[R] C) (f : A →ₐ[R] B) :
(S.map f).map g = S.map (g.comp f) :=
set_like.coe_injective $ set.image_image _ _ _
lemma mem_map {S : subalgebra R A} {f : A →ₐ[R] B} {y : B} :
y ∈ map f S ↔ ∃ x ∈ S, f x = y :=
subsemiring.mem_map
lemma map_to_submodule {S : subalgebra R A} {f : A →ₐ[R] B} :
(S.map f).to_submodule = S.to_submodule.map f.to_linear_map :=
set_like.coe_injective rfl
lemma map_to_subsemiring {S : subalgebra R A} {f : A →ₐ[R] B} :
(S.map f).to_subsemiring = S.to_subsemiring.map f.to_ring_hom :=
set_like.coe_injective rfl
@[simp] lemma coe_map (S : subalgebra R A) (f : A →ₐ[R] B) :
(S.map f : set B) = f '' S :=
rfl
/-- Preimage of a subalgebra under an algebra homomorphism. -/
def comap (f : A →ₐ[R] B) (S : subalgebra R B) : subalgebra R A :=
{ algebra_map_mem' := λ r, show f (algebra_map R A r) ∈ S,
from (f.commutes r).symm ▸ S.algebra_map_mem r,
.. S.to_subsemiring.comap (f : A →+* B) }
theorem map_le {S : subalgebra R A} {f : A →ₐ[R] B} {U : subalgebra R B} :
map f S ≤ U ↔ S ≤ comap f U :=
set.image_subset_iff
lemma gc_map_comap (f : A →ₐ[R] B) : galois_connection (map f) (comap f) :=
λ S U, map_le
@[simp] lemma mem_comap (S : subalgebra R B) (f : A →ₐ[R] B) (x : A) :
x ∈ S.comap f ↔ f x ∈ S :=
iff.rfl
@[simp, norm_cast] lemma coe_comap (S : subalgebra R B) (f : A →ₐ[R] B) :
(S.comap f : set A) = f ⁻¹' (S : set B) :=
rfl
instance no_zero_divisors {R A : Type*} [comm_semiring R] [semiring A] [no_zero_divisors A]
[algebra R A] (S : subalgebra R A) : no_zero_divisors S :=
S.to_subsemiring.no_zero_divisors
instance is_domain {R A : Type*} [comm_ring R] [ring A] [is_domain A] [algebra R A]
(S : subalgebra R A) : is_domain S :=
subring.is_domain S.to_subring
end subalgebra
namespace submodule
variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A]
variables (p : submodule R A)
/-- A submodule containing `1` and closed under multiplication is a subalgebra. -/
def to_subalgebra (p : submodule R A) (h_one : (1 : A) ∈ p)
(h_mul : ∀ x y, x ∈ p → y ∈ p → x * y ∈ p) : subalgebra R A :=
{ mul_mem' := h_mul,
algebra_map_mem' := λ r, begin
rw algebra.algebra_map_eq_smul_one,
exact p.smul_mem _ h_one,
end,
..p}
@[simp] lemma mem_to_subalgebra {p : submodule R A} {h_one h_mul} {x} :
x ∈ p.to_subalgebra h_one h_mul ↔ x ∈ p := iff.rfl
@[simp] lemma coe_to_subalgebra (p : submodule R A) (h_one h_mul) :
(p.to_subalgebra h_one h_mul : set A) = p := rfl
@[simp] lemma to_subalgebra_mk (s : set A) (h0 hadd hsmul h1 hmul) :
(submodule.mk s hadd h0 hsmul : submodule R A).to_subalgebra h1 hmul =
subalgebra.mk s @hmul h1 @hadd h0
(λ r, by { rw algebra.algebra_map_eq_smul_one, exact hsmul r h1 }) := rfl
@[simp] lemma to_subalgebra_to_submodule (p : submodule R A) (h_one h_mul) :
(p.to_subalgebra h_one h_mul).to_submodule = p :=
set_like.coe_injective rfl
@[simp] lemma _root_.subalgebra.to_submodule_to_subalgebra (S : subalgebra R A) :
S.to_submodule.to_subalgebra S.one_mem (λ _ _, S.mul_mem) = S :=
set_like.coe_injective rfl
end submodule
namespace alg_hom
variables {R' : Type u'} {R : Type u} {A : Type v} {B : Type w} {C : Type w'}
variables [comm_semiring R]
variables [semiring A] [algebra R A] [semiring B] [algebra R B] [semiring C] [algebra R C]
variables (φ : A →ₐ[R] B)
/-- Range of an `alg_hom` as a subalgebra. -/
protected def range (φ : A →ₐ[R] B) : subalgebra R B :=
{ algebra_map_mem' := λ r, ⟨algebra_map R A r, φ.commutes r⟩,
.. φ.to_ring_hom.srange }
@[simp] lemma mem_range (φ : A →ₐ[R] B) {y : B} :
y ∈ φ.range ↔ ∃ x, φ x = y := ring_hom.mem_srange
theorem mem_range_self (φ : A →ₐ[R] B) (x : A) : φ x ∈ φ.range := φ.mem_range.2 ⟨x, rfl⟩
@[simp] lemma coe_range (φ : A →ₐ[R] B) : (φ.range : set B) = set.range φ :=
by { ext, rw [set_like.mem_coe, mem_range], refl }
theorem range_comp (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).range = f.range.map g :=
set_like.coe_injective (set.range_comp g f)
theorem range_comp_le_range (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).range ≤ g.range :=
set_like.coe_mono (set.range_comp_subset_range f g)
/-- Restrict the codomain of an algebra homomorphism. -/
def cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : A →ₐ[R] S :=
{ commutes' := λ r, subtype.eq $ f.commutes r,
.. ring_hom.cod_restrict (f : A →+* B) S hf }
@[simp] lemma val_comp_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) :
S.val.comp (f.cod_restrict S hf) = f :=
alg_hom.ext $ λ _, rfl
@[simp] lemma coe_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) (x : A) :
↑(f.cod_restrict S hf x) = f x := rfl
theorem injective_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) :
function.injective (f.cod_restrict S hf) ↔ function.injective f :=
⟨λ H x y hxy, H $ subtype.eq hxy, λ H x y hxy, H (congr_arg subtype.val hxy : _)⟩
/-- Restrict the codomain of a alg_hom `f` to `f.range`.
This is the bundled version of `set.range_factorization`. -/
@[reducible] def range_restrict (f : A →ₐ[R] B) : A →ₐ[R] f.range :=
f.cod_restrict f.range f.mem_range_self
/-- The equalizer of two R-algebra homomorphisms -/
def equalizer (ϕ ψ : A →ₐ[R] B) : subalgebra R A :=
{ carrier := {a | ϕ a = ψ a},
add_mem' := λ x y (hx : ϕ x = ψ x) (hy : ϕ y = ψ y),
by rw [set.mem_set_of_eq, ϕ.map_add, ψ.map_add, hx, hy],
mul_mem' := λ x y (hx : ϕ x = ψ x) (hy : ϕ y = ψ y),
by rw [set.mem_set_of_eq, ϕ.map_mul, ψ.map_mul, hx, hy],
algebra_map_mem' := λ x,
by rw [set.mem_set_of_eq, alg_hom.commutes, alg_hom.commutes] }
@[simp] lemma mem_equalizer (ϕ ψ : A →ₐ[R] B) (x : A) :
x ∈ ϕ.equalizer ψ ↔ ϕ x = ψ x := iff.rfl
/-- The range of a morphism of algebras is a fintype, if the domain is a fintype.
Note that this instance can cause a diamond with `subtype.fintype` if `B` is also a fintype. -/
instance fintype_range [fintype A] [decidable_eq B] (φ : A →ₐ[R] B) : fintype φ.range :=
set.fintype_range φ
end alg_hom
namespace alg_equiv
variables {R : Type u} {A : Type v} {B : Type w}
variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
/-- Restrict an algebra homomorphism with a left inverse to an algebra isomorphism to its range.
This is a computable alternative to `alg_equiv.of_injective`. -/
def of_left_inverse
{g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) :
A ≃ₐ[R] f.range :=
{ to_fun := f.range_restrict,
inv_fun := g ∘ f.range.val,
left_inv := h,
right_inv := λ x, subtype.ext $
let ⟨x', hx'⟩ := f.mem_range.mp x.prop in
show f (g x) = x, by rw [←hx', h x'],
..f.range_restrict }
@[simp] lemma of_left_inverse_apply
{g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : A) :
↑(of_left_inverse h x) = f x := rfl
@[simp] lemma of_left_inverse_symm_apply
{g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : f.range) :
(of_left_inverse h).symm x = g x := rfl
/-- Restrict an injective algebra homomorphism to an algebra isomorphism -/
noncomputable def of_injective (f : A →ₐ[R] B) (hf : function.injective f) :
A ≃ₐ[R] f.range :=
of_left_inverse (classical.some_spec hf.has_left_inverse)
@[simp] lemma of_injective_apply (f : A →ₐ[R] B) (hf : function.injective f) (x : A) :
↑(of_injective f hf x) = f x := rfl
/-- Restrict an algebra homomorphism between fields to an algebra isomorphism -/
noncomputable def of_injective_field {E F : Type*} [division_ring E] [semiring F]
[nontrivial F] [algebra R E] [algebra R F] (f : E →ₐ[R] F) : E ≃ₐ[R] f.range :=
of_injective f f.to_ring_hom.injective
/-- Given an equivalence `e : A ≃ₐ[R] B` of `R`-algebras and a subalgebra `S` of `A`,
`subalgebra_map` is the induced equivalence between `S` and `S.map e` -/
@[simps] def subalgebra_map (e : A ≃ₐ[R] B) (S : subalgebra R A) :
S ≃ₐ[R] (S.map e.to_alg_hom) :=
{ commutes' := λ r, by { ext, simp },
..e.to_ring_equiv.subsemiring_map S.to_subsemiring }
end alg_equiv
namespace algebra
variables (R : Type u) {A : Type v} {B : Type w}
variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B]
/-- The minimal subalgebra that includes `s`. -/
def adjoin (s : set A) : subalgebra R A :=
{ algebra_map_mem' := λ r, subsemiring.subset_closure $ or.inl ⟨r, rfl⟩,
.. subsemiring.closure (set.range (algebra_map R A) ∪ s) }
variables {R}
protected lemma gc : galois_connection (adjoin R : set A → subalgebra R A) coe :=
λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) subsemiring.subset_closure) H,
λ H, show subsemiring.closure (set.range (algebra_map R A) ∪ s) ≤ S.to_subsemiring,
from subsemiring.closure_le.2 $ set.union_subset S.range_subset H⟩
/-- Galois insertion between `adjoin` and `coe`. -/
protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe :=
{ choice := λ s hs, (adjoin R s).copy s $ le_antisymm (algebra.gc.le_u_l s) hs,
gc := algebra.gc,
le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_rfl,
choice_eq := λ _ _, subalgebra.copy_eq _ _ _ }
instance : complete_lattice (subalgebra R A) :=
galois_insertion.lift_complete_lattice algebra.gi
@[simp]
lemma coe_top : (↑(⊤ : subalgebra R A) : set A) = set.univ := rfl
@[simp] lemma mem_top {x : A} : x ∈ (⊤ : subalgebra R A) :=
set.mem_univ x
@[simp] lemma top_to_submodule : (⊤ : subalgebra R A).to_submodule = ⊤ := rfl
@[simp] lemma top_to_subsemiring : (⊤ : subalgebra R A).to_subsemiring = ⊤ := rfl
@[simp] lemma top_to_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] :
(⊤ : subalgebra R A).to_subring = ⊤ := rfl
@[simp] lemma to_submodule_eq_top {S : subalgebra R A} : S.to_submodule = ⊤ ↔ S = ⊤ :=
subalgebra.to_submodule.injective.eq_iff' top_to_submodule
@[simp] lemma to_subsemiring_eq_top {S : subalgebra R A} : S.to_subsemiring = ⊤ ↔ S = ⊤ :=
subalgebra.to_subsemiring_injective.eq_iff' top_to_subsemiring
@[simp] lemma to_subring_eq_top {R A : Type*} [comm_ring R] [ring A] [algebra R A]
{S : subalgebra R A} : S.to_subring = ⊤ ↔ S = ⊤ :=
subalgebra.to_subring_injective.eq_iff' top_to_subring
lemma mem_sup_left {S T : subalgebra R A} : ∀ {x : A}, x ∈ S → x ∈ S ⊔ T :=
show S ≤ S ⊔ T, from le_sup_left
lemma mem_sup_right {S T : subalgebra R A} : ∀ {x : A}, x ∈ T → x ∈ S ⊔ T :=
show T ≤ S ⊔ T, from le_sup_right
lemma mul_mem_sup {S T : subalgebra R A} {x y : A} (hx : x ∈ S) (hy : y ∈ T) :
x * y ∈ S ⊔ T :=
(S ⊔ T).mul_mem (mem_sup_left hx) (mem_sup_right hy)
lemma map_sup (f : A →ₐ[R] B) (S T : subalgebra R A) : (S ⊔ T).map f = S.map f ⊔ T.map f :=
(subalgebra.gc_map_comap f).l_sup
@[simp, norm_cast]
lemma coe_inf (S T : subalgebra R A) : (↑(S ⊓ T) : set A) = S ∩ T := rfl
@[simp]
lemma mem_inf {S T : subalgebra R A} {x : A} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := iff.rfl
@[simp] lemma inf_to_submodule (S T : subalgebra R A) :
(S ⊓ T).to_submodule = S.to_submodule ⊓ T.to_submodule := rfl
@[simp] lemma inf_to_subsemiring (S T : subalgebra R A) :
(S ⊓ T).to_subsemiring = S.to_subsemiring ⊓ T.to_subsemiring := rfl
@[simp, norm_cast]
lemma coe_Inf (S : set (subalgebra R A)) : (↑(Inf S) : set A) = ⋂ s ∈ S, ↑s := Inf_image
lemma mem_Inf {S : set (subalgebra R A)} {x : A} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p :=
by simp only [← set_like.mem_coe, coe_Inf, set.mem_Inter₂]
@[simp] lemma Inf_to_submodule (S : set (subalgebra R A)) :
(Inf S).to_submodule = Inf (subalgebra.to_submodule '' S) :=
set_like.coe_injective $ by simp
@[simp] lemma Inf_to_subsemiring (S : set (subalgebra R A)) :
(Inf S).to_subsemiring = Inf (subalgebra.to_subsemiring '' S) :=
set_like.coe_injective $ by simp
@[simp, norm_cast]
lemma coe_infi {ι : Sort*} {S : ι → subalgebra R A} : (↑(⨅ i, S i) : set A) = ⋂ i, S i :=
by simp [infi]
lemma mem_infi {ι : Sort*} {S : ι → subalgebra R A} {x : A} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i :=
by simp only [infi, mem_Inf, set.forall_range_iff]
@[simp] lemma infi_to_submodule {ι : Sort*} (S : ι → subalgebra R A) :
(⨅ i, S i).to_submodule = ⨅ i, (S i).to_submodule :=
set_like.coe_injective $ by simp
instance : inhabited (subalgebra R A) := ⟨⊥⟩
theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map R A) :=
suffices (of_id R A).range = (⊥ : subalgebra R A),
by { rw [← this, ←set_like.mem_coe, alg_hom.coe_range], refl },
le_bot_iff.mp (λ x hx, subalgebra.range_le _ ((of_id R A).coe_range ▸ hx))
theorem to_submodule_bot : (⊥ : subalgebra R A).to_submodule = R ∙ 1 :=
by { ext x, simp [mem_bot, -set.singleton_one, submodule.mem_span_singleton, algebra.smul_def] }
@[simp] theorem coe_bot : ((⊥ : subalgebra R A) : set A) = set.range (algebra_map R A) :=
by simp [set.ext_iff, algebra.mem_bot]
theorem eq_top_iff {S : subalgebra R A} :
S = ⊤ ↔ ∀ x : A, x ∈ S :=
⟨λ h x, by rw h; exact mem_top, λ h, by ext x; exact ⟨λ _, mem_top, λ _, h x⟩⟩
lemma range_top_iff_surjective (f : A →ₐ[R] B) :
f.range = (⊤ : subalgebra R B) ↔ function.surjective f :=
algebra.eq_top_iff
@[simp] theorem range_id : (alg_hom.id R A).range = ⊤ :=
set_like.coe_injective set.range_id
@[simp] theorem map_top (f : A →ₐ[R] B) : (⊤ : subalgebra R A).map f = f.range :=
set_like.coe_injective set.image_univ
@[simp] theorem map_bot (f : A →ₐ[R] B) : (⊥ : subalgebra R A).map f = ⊥ :=
set_like.coe_injective $
by simp only [← set.range_comp, (∘), algebra.coe_bot, subalgebra.coe_map, f.commutes]
@[simp] theorem comap_top (f : A →ₐ[R] B) : (⊤ : subalgebra R B).comap f = ⊤ :=
eq_top_iff.2 $ λ x, mem_top
/-- `alg_hom` to `⊤ : subalgebra R A`. -/
def to_top : A →ₐ[R] (⊤ : subalgebra R A) :=
(alg_hom.id R A).cod_restrict ⊤ (λ _, mem_top)
theorem surjective_algebra_map_iff :
function.surjective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ :=
⟨λ h, eq_bot_iff.2 $ λ y _, let ⟨x, hx⟩ := h y in hx ▸ subalgebra.algebra_map_mem _ _,
λ h y, algebra.mem_bot.1 $ eq_bot_iff.1 h (algebra.mem_top : y ∈ _)⟩
theorem bijective_algebra_map_iff {R A : Type*} [field R] [semiring A] [nontrivial A]
[algebra R A] :
function.bijective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ :=
⟨λ h, surjective_algebra_map_iff.1 h.2,
λ h, ⟨(algebra_map R A).injective, surjective_algebra_map_iff.2 h⟩⟩
/-- The bottom subalgebra is isomorphic to the base ring. -/
noncomputable def bot_equiv_of_injective (h : function.injective (algebra_map R A)) :
(⊥ : subalgebra R A) ≃ₐ[R] R :=
alg_equiv.symm $ alg_equiv.of_bijective (algebra.of_id R _)
⟨λ x y hxy, h (congr_arg subtype.val hxy : _),
λ ⟨y, hy⟩, let ⟨x, hx⟩ := algebra.mem_bot.1 hy in ⟨x, subtype.eq hx⟩⟩
/-- The bottom subalgebra is isomorphic to the field. -/
@[simps symm_apply]
noncomputable def bot_equiv (F R : Type*) [field F] [semiring R] [nontrivial R] [algebra F R] :
(⊥ : subalgebra F R) ≃ₐ[F] F :=
bot_equiv_of_injective (ring_hom.injective _)
end algebra
namespace subalgebra
open algebra
variables {R : Type u} {A : Type v} {B : Type w}
variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B]
variables (S : subalgebra R A)
/-- The top subalgebra is isomorphic to the algebra.
This is the algebra version of `submodule.top_equiv`. -/
@[simps] def top_equiv : (⊤ : subalgebra R A) ≃ₐ[R] A :=
alg_equiv.of_alg_hom (subalgebra.val ⊤) to_top rfl $ alg_hom.ext $ λ _, subtype.ext rfl
instance subsingleton_of_subsingleton [subsingleton A] : subsingleton (subalgebra R A) :=
⟨λ B C, ext (λ x, by { simp only [subsingleton.elim x 0, zero_mem B, zero_mem C] })⟩
instance _root_.alg_hom.subsingleton [subsingleton (subalgebra R A)] : subsingleton (A →ₐ[R] B) :=
⟨λ f g, alg_hom.ext $ λ a,
have a ∈ (⊥ : subalgebra R A) := subsingleton.elim (⊤ : subalgebra R A) ⊥ ▸ mem_top,
let ⟨x, hx⟩ := set.mem_range.mp (mem_bot.mp this) in
hx ▸ (f.commutes _).trans (g.commutes _).symm⟩
instance _root_.alg_equiv.subsingleton_left [subsingleton (subalgebra R A)] :
subsingleton (A ≃ₐ[R] B) :=
⟨λ f g, alg_equiv.ext (λ x, alg_hom.ext_iff.mp (subsingleton.elim f.to_alg_hom g.to_alg_hom) x)⟩
instance _root_.alg_equiv.subsingleton_right [subsingleton (subalgebra R B)] :
subsingleton (A ≃ₐ[R] B) :=
⟨λ f g, by rw [← f.symm_symm, subsingleton.elim f.symm g.symm, g.symm_symm]⟩
lemma range_val : S.val.range = S :=
ext $ set.ext_iff.1 $ S.val.coe_range.trans subtype.range_val
instance : unique (subalgebra R R) :=
{ uniq :=
begin
intro S,
refine le_antisymm (λ r hr, _) bot_le,
simp only [set.mem_range, mem_bot, id.map_eq_self, exists_apply_eq_apply, default],
end
.. algebra.subalgebra.inhabited }
/-- The map `S → T` when `S` is a subalgebra contained in the subalgebra `T`.
This is the subalgebra version of `submodule.of_le`, or `subring.inclusion` -/
def inclusion {S T : subalgebra R A} (h : S ≤ T) : S →ₐ[R] T :=
{ to_fun := set.inclusion h,
map_one' := rfl,
map_add' := λ _ _, rfl,
map_mul' := λ _ _, rfl,
map_zero' := rfl,
commutes' := λ _, rfl }
lemma inclusion_injective {S T : subalgebra R A} (h : S ≤ T) :
function.injective (inclusion h) :=
λ _ _, subtype.ext ∘ subtype.mk.inj
@[simp] lemma inclusion_self {S : subalgebra R A}:
inclusion (le_refl S) = alg_hom.id R S :=
alg_hom.ext $ λ x, subtype.ext rfl
@[simp] lemma inclusion_mk {S T : subalgebra R A} (h : S ≤ T) (x : A) (hx : x ∈ S) :
inclusion h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl
lemma inclusion_right {S T : subalgebra R A} (h : S ≤ T) (x : T)
(m : (x : A) ∈ S) : inclusion h ⟨x, m⟩ = x := subtype.ext rfl
@[simp] lemma inclusion_inclusion {S T U : subalgebra R A} (hst : S ≤ T) (htu : T ≤ U)
(x : S) : inclusion htu (inclusion hst x) = inclusion (le_trans hst htu) x :=
subtype.ext rfl
@[simp] lemma coe_inclusion {S T : subalgebra R A} (h : S ≤ T) (s : S) :
(inclusion h s : A) = s := rfl
/-- Two subalgebras that are equal are also equivalent as algebras.
This is the `subalgebra` version of `linear_equiv.of_eq` and `equiv.set.of_eq`. -/
@[simps apply]
def equiv_of_eq (S T : subalgebra R A) (h : S = T) : S ≃ₐ[R] T :=
{ to_fun := λ x, ⟨x, h ▸ x.2⟩,
inv_fun := λ x, ⟨x, h.symm ▸ x.2⟩,
map_mul' := λ _ _, rfl,
commutes' := λ _, rfl,
.. linear_equiv.of_eq _ _ (congr_arg to_submodule h) }
@[simp] lemma equiv_of_eq_symm (S T : subalgebra R A) (h : S = T) :
(equiv_of_eq S T h).symm = equiv_of_eq T S h.symm :=
rfl
@[simp] lemma equiv_of_eq_rfl (S : subalgebra R A) :
equiv_of_eq S S rfl = alg_equiv.refl :=
by { ext, refl }
@[simp] lemma equiv_of_eq_trans (S T U : subalgebra R A) (hST : S = T) (hTU : T = U) :
(equiv_of_eq S T hST).trans (equiv_of_eq T U hTU) = equiv_of_eq S U (trans hST hTU) :=
rfl
section prod
variables (S₁ : subalgebra R B)
/-- The product of two subalgebras is a subalgebra. -/
def prod : subalgebra R (A × B) :=
{ carrier := S ×ˢ S₁,
algebra_map_mem' := λ r, ⟨algebra_map_mem _ _, algebra_map_mem _ _⟩,
.. S.to_subsemiring.prod S₁.to_subsemiring }
@[simp] lemma coe_prod : (prod S S₁ : set (A × B)) = S ×ˢ S₁ := rfl
lemma prod_to_submodule :
(S.prod S₁).to_submodule = S.to_submodule.prod S₁.to_submodule := rfl
@[simp] lemma mem_prod {S : subalgebra R A} {S₁ : subalgebra R B} {x : A × B} :
x ∈ prod S S₁ ↔ x.1 ∈ S ∧ x.2 ∈ S₁ := set.mem_prod
@[simp] lemma prod_top : (prod ⊤ ⊤ : subalgebra R (A × B)) = ⊤ :=
by ext; simp
lemma prod_mono {S T : subalgebra R A} {S₁ T₁ : subalgebra R B} :
S ≤ T → S₁ ≤ T₁ → prod S S₁ ≤ prod T T₁ := set.prod_mono
@[simp] lemma prod_inf_prod {S T : subalgebra R A} {S₁ T₁ : subalgebra R B} :
S.prod S₁ ⊓ T.prod T₁ = (S ⊓ T).prod (S₁ ⊓ T₁) :=
set_like.coe_injective set.prod_inter_prod
end prod
section supr_lift
variables {ι : Type*}
lemma coe_supr_of_directed [nonempty ι] {S : ι → subalgebra R A}
(dir : directed (≤) S) : ↑(supr S) = ⋃ i, (S i : set A) :=
let K : subalgebra R A :=
{ carrier := ⋃ i, (S i),
mul_mem' := λ x y hx hy,
let ⟨i, hi⟩ := set.mem_Union.1 hx in
let ⟨j, hj⟩ := set.mem_Union.1 hy in
let ⟨k, hik, hjk⟩ := dir i j in
set.mem_Union.2 ⟨k, subalgebra.mul_mem (S k) (hik hi) (hjk hj)⟩ ,
add_mem' := λ x y hx hy,
let ⟨i, hi⟩ := set.mem_Union.1 hx in
let ⟨j, hj⟩ := set.mem_Union.1 hy in
let ⟨k, hik, hjk⟩ := dir i j in
set.mem_Union.2 ⟨k, subalgebra.add_mem (S k) (hik hi) (hjk hj)⟩,
algebra_map_mem' := λ r, let i := @nonempty.some ι infer_instance in
set.mem_Union.2 ⟨i, subalgebra.algebra_map_mem _ _⟩ } in
have supr S = K,
from le_antisymm (supr_le (λ i, set.subset_Union (λ i, ↑(S i)) i))
(set_like.coe_subset_coe.1
(set.Union_subset (λ i, set_like.coe_subset_coe.2 (le_supr _ _)))),
this.symm ▸ rfl
/-- Define an algebra homomorphism on a directed supremum of subalgebras by defining
it on each subalgebra, and proving that it agrees on the intersection of subalgebras. -/
noncomputable def supr_lift [nonempty ι]
(K : ι → subalgebra R A)
(dir : directed (≤) K)
(f : Π i, K i →ₐ[R] B)
(hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h))
(T : subalgebra R A) (hT : T = supr K) :
↥T →ₐ[R] B :=
by subst hT; exact
{ to_fun := set.Union_lift (λ i, ↑(K i)) (λ i x, f i x)
(λ i j x hxi hxj,
let ⟨k, hik, hjk⟩ := dir i j in
begin
rw [hf i k hik, hf j k hjk],
refl
end) ↑(supr K)
(by rw coe_supr_of_directed dir; refl),
map_one' := set.Union_lift_const _ (λ _, 1) (λ _, rfl) _ (by simp),
map_zero' := set.Union_lift_const _ (λ _, 0) (λ _, rfl) _ (by simp),
map_mul' := set.Union_lift_binary (coe_supr_of_directed dir) dir _
(λ _, (*)) (λ _ _ _, rfl) _ (by simp),
map_add' := set.Union_lift_binary (coe_supr_of_directed dir) dir _
(λ _, (+)) (λ _ _ _, rfl) _ (by simp),
commutes' := λ r, set.Union_lift_const _ (λ _, algebra_map _ _ r)
(λ _, rfl) _ (λ i, by erw [alg_hom.commutes (f i)]) }
variables [nonempty ι] {K : ι → subalgebra R A} {dir : directed (≤) K}
{f : Π i, K i →ₐ[R] B}
{hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)}
{T : subalgebra R A} {hT : T = supr K}
@[simp] lemma supr_lift_inclusion {i : ι} (x : K i) (h : K i ≤ T) :
supr_lift K dir f hf T hT (inclusion h x) = f i x :=
by subst T; exact set.Union_lift_inclusion _ _
@[simp] lemma supr_lift_comp_inclusion {i : ι} (h : K i ≤ T) :
(supr_lift K dir f hf T hT).comp (inclusion h) = f i :=
by ext; simp
@[simp] lemma supr_lift_mk {i : ι} (x : K i) (hx : (x : A) ∈ T) :
supr_lift K dir f hf T hT ⟨x, hx⟩ = f i x :=
by subst hT; exact set.Union_lift_mk x hx
lemma supr_lift_of_mem {i : ι} (x : T) (hx : (x : A) ∈ K i) :
supr_lift K dir f hf T hT x = f i ⟨x, hx⟩ :=
by subst hT; exact set.Union_lift_of_mem x hx
end supr_lift
/-! ## Actions by `subalgebra`s
These are just copies of the definitions about `subsemiring` starting from
`subring.mul_action`.
-/
section actions
variables {α β : Type*}
/-- The action by a subalgebra is the action by the underlying algebra. -/
instance [has_smul A α] (S : subalgebra R A) : has_smul S α := S.to_subsemiring.has_smul
lemma smul_def [has_smul A α] {S : subalgebra R A} (g : S) (m : α) : g • m = (g : A) • m := rfl
instance smul_comm_class_left
[has_smul A β] [has_smul α β] [smul_comm_class A α β] (S : subalgebra R A) :
smul_comm_class S α β :=
S.to_subsemiring.smul_comm_class_left
instance smul_comm_class_right
[has_smul α β] [has_smul A β] [smul_comm_class α A β] (S : subalgebra R A) :
smul_comm_class α S β :=
S.to_subsemiring.smul_comm_class_right
/-- Note that this provides `is_scalar_tower S R R` which is needed by `smul_mul_assoc`. -/
instance is_scalar_tower_left
[has_smul α β] [has_smul A α] [has_smul A β] [is_scalar_tower A α β] (S : subalgebra R A) :
is_scalar_tower S α β :=
S.to_subsemiring.is_scalar_tower
instance is_scalar_tower_mid {R S T : Type*} [comm_semiring R] [semiring S] [add_comm_monoid T]
[algebra R S] [module R T] [module S T] [is_scalar_tower R S T] (S' : subalgebra R S) :
is_scalar_tower R S' T :=
⟨λ x y z, (smul_assoc _ (y : S) _ : _)⟩
instance [has_smul A α] [has_faithful_smul A α] (S : subalgebra R A) :
has_faithful_smul S α :=
S.to_subsemiring.has_faithful_smul
/-- The action by a subalgebra is the action by the underlying algebra. -/
instance [mul_action A α] (S : subalgebra R A) : mul_action S α :=
S.to_subsemiring.mul_action
/-- The action by a subalgebra is the action by the underlying algebra. -/
instance [add_monoid α] [distrib_mul_action A α] (S : subalgebra R A) : distrib_mul_action S α :=
S.to_subsemiring.distrib_mul_action
/-- The action by a subalgebra is the action by the underlying algebra. -/
instance [has_zero α] [smul_with_zero A α] (S : subalgebra R A) : smul_with_zero S α :=
S.to_subsemiring.smul_with_zero
/-- The action by a subalgebra is the action by the underlying algebra. -/
instance [has_zero α] [mul_action_with_zero A α] (S : subalgebra R A) : mul_action_with_zero S α :=
S.to_subsemiring.mul_action_with_zero
/-- The action by a subalgebra is the action by the underlying algebra. -/
instance module_left [add_comm_monoid α] [module A α] (S : subalgebra R A) : module S α :=
S.to_subsemiring.module
/-- The action by a subalgebra is the action by the underlying algebra. -/
instance to_algebra {R A : Type*} [comm_semiring R] [comm_semiring A] [semiring α]
[algebra R A] [algebra A α] (S : subalgebra R A) : algebra S α :=
algebra.of_subsemiring S.to_subsemiring
lemma algebra_map_eq {R A : Type*} [comm_semiring R] [comm_semiring A] [semiring α]
[algebra R A] [algebra A α] (S : subalgebra R A) :
algebra_map S α = (algebra_map A α).comp S.val := rfl
@[simp] lemma srange_algebra_map {R A : Type*} [comm_semiring R] [comm_semiring A]
[algebra R A] (S : subalgebra R A) :
(algebra_map S A).srange = S.to_subsemiring :=
by rw [algebra_map_eq, algebra.id.map_eq_id, ring_hom.id_comp, ← to_subsemiring_subtype,
subsemiring.srange_subtype]
@[simp] lemma range_algebra_map {R A : Type*} [comm_ring R] [comm_ring A]
[algebra R A] (S : subalgebra R A) :
(algebra_map S A).range = S.to_subring :=
by rw [algebra_map_eq, algebra.id.map_eq_id, ring_hom.id_comp, ← to_subring_subtype,
subring.range_subtype]
instance no_zero_smul_divisors_top [no_zero_divisors A] (S : subalgebra R A) :
no_zero_smul_divisors S A :=
⟨λ c x h,
have (c : A) = 0 ∨ x = 0,
from eq_zero_or_eq_zero_of_mul_eq_zero h,
this.imp_left (@subtype.ext_iff _ _ c 0).mpr⟩
end actions
section center
lemma _root_.set.algebra_map_mem_center (r : R) : algebra_map R A r ∈ set.center A :=
by simp [algebra.commutes, set.mem_center_iff]
variables (R A)
/-- The center of an algebra is the set of elements which commute with every element. They form a
subalgebra. -/
def center : subalgebra R A :=
{ algebra_map_mem' := set.algebra_map_mem_center,
.. subsemiring.center A }
lemma coe_center : (center R A : set A) = set.center A := rfl
@[simp] lemma center_to_subsemiring :
(center R A).to_subsemiring = subsemiring.center A :=
rfl
@[simp] lemma center_to_subring (R A : Type*) [comm_ring R] [ring A] [algebra R A] :
(center R A).to_subring = subring.center A :=
rfl
@[simp] lemma center_eq_top (A : Type*) [comm_semiring A] [algebra R A] : center R A = ⊤ :=
set_like.coe_injective (set.center_eq_univ A)
variables {R A}
instance : comm_semiring (center R A) := subsemiring.center.comm_semiring
instance {A : Type*} [ring A] [algebra R A] : comm_ring (center R A) := subring.center.comm_ring
lemma mem_center_iff {a : A} : a ∈ center R A ↔ ∀ (b : A), b*a = a*b := iff.rfl
end center
section centralizer
@[simp]
lemma _root_.set.algebra_map_mem_centralizer
{s : set A} (r : R) : algebra_map R A r ∈ s.centralizer :=
λ a h, (algebra.commutes _ _).symm
variables (R)
/-- The centralizer of a set as a subalgebra. -/
def centralizer (s : set A) : subalgebra R A :=
{ algebra_map_mem' := set.algebra_map_mem_centralizer,
..subsemiring.centralizer s, }
@[simp, norm_cast]
lemma coe_centralizer (s : set A) : (centralizer R s : set A) = s.centralizer := rfl
lemma mem_centralizer_iff {s : set A} {z : A} :
z ∈ centralizer R s ↔ ∀ g ∈ s, g * z = z * g :=
iff.rfl
lemma center_le_centralizer (s) : center R A ≤ centralizer R s := s.center_subset_centralizer
lemma centralizer_le (s t : set A) (h : s ⊆ t) :
centralizer R t ≤ centralizer R s :=
set.centralizer_subset h
@[simp] lemma centralizer_eq_top_iff_subset {s : set A} : centralizer R s = ⊤ ↔ s ⊆ center R A :=
set_like.ext'_iff.trans set.centralizer_eq_top_iff_subset
@[simp]
lemma centralizer_univ : centralizer R set.univ = center R A :=
set_like.ext' (set.centralizer_univ A)
end centralizer
/-- Suppose we are given `∑ i, lᵢ * sᵢ = 1` in `S`, and `S'` a subalgebra of `S` that contains
`lᵢ` and `sᵢ`. To check that an `x : S` falls in `S'`, we only need to show that
`r ^ n • x ∈ M'` for some `n` for each `r : s`. -/
lemma mem_of_finset_sum_eq_one_of_pow_smul_mem {S : Type*} [comm_ring S]
[algebra R S] (S' : subalgebra R S) {ι : Type*} (ι' : finset ι) (s : ι → S) (l : ι → S)
(e : ∑ i in ι', l i * s i = 1)
(hs : ∀ i, s i ∈ S') (hl : ∀ i, l i ∈ S') (x : S)
(H : ∀ i, ∃ (n : ℕ), (s i ^ n : S) • x ∈ S') : x ∈ S' :=
begin
classical,
suffices : x ∈ (algebra.of_id S' S).range.to_submodule,
{ obtain ⟨x, rfl⟩ := this, exact x.2 },
choose n hn using H,
let s' : ι → S' := λ x, ⟨s x, hs x⟩,
have : ideal.span (s' '' ι')= ⊤,
{ rw [ideal.eq_top_iff_one, ideal.span, finsupp.mem_span_iff_total],
refine ⟨(finsupp.of_support_finite (λ i : ι', (⟨l i, hl i⟩ : S')) (set.to_finite _))
.map_domain $ λ i, ⟨s' i, i, i.2, rfl⟩, S'.to_submodule.injective_subtype _⟩,
rw [finsupp.total_map_domain, finsupp.total_apply, finsupp.sum_fintype,
map_sum, submodule.subtype_apply, subalgebra.coe_one],
{ exact finset.sum_attach.trans e },
{ exact λ _, zero_smul _ _ } },
let N := ι'.sup n,
have hs' := ideal.span_pow_eq_top _ this N,
apply (algebra.of_id S' S).range.to_submodule.mem_of_span_top_of_smul_mem _ hs',
rintros ⟨_, _, ⟨i, hi, rfl⟩, rfl⟩,
change s i ^ N • x ∈ _,
rw [← tsub_add_cancel_of_le (show n i ≤ N, from finset.le_sup hi), pow_add, mul_smul],
refine submodule.smul_mem _ (⟨_, pow_mem (hs i) _⟩ : S') _,
exact ⟨⟨_, hn i⟩, rfl⟩,
end
lemma mem_of_span_eq_top_of_smul_pow_mem {S : Type*} [comm_ring S] [algebra R S]
(S' : subalgebra R S) (s : set S) (l : s →₀ S) (hs : finsupp.total s S S coe l = 1)
(hs' : s ⊆ S') (hl : ∀ i, l i ∈ S') (x : S)
(H : ∀ r : s, ∃ (n : ℕ), (r ^ n : S) • x ∈ S') : x ∈ S' :=
mem_of_finset_sum_eq_one_of_pow_smul_mem S' l.support coe l hs (λ x, hs' x.2) hl x H
end subalgebra
section nat
variables {R : Type*} [semiring R]
/-- A subsemiring is a `ℕ`-subalgebra. -/
def subalgebra_of_subsemiring (S : subsemiring R) : subalgebra ℕ R :=
{ algebra_map_mem' := λ i, coe_nat_mem S i,
.. S }
@[simp] lemma mem_subalgebra_of_subsemiring {x : R} {S : subsemiring R} :
x ∈ subalgebra_of_subsemiring S ↔ x ∈ S :=
iff.rfl
end nat
section int
variables {R : Type*} [ring R]
/-- A subring is a `ℤ`-subalgebra. -/
def subalgebra_of_subring (S : subring R) : subalgebra ℤ R :=
{ algebra_map_mem' := λ i, int.induction_on i (by simpa using S.zero_mem)
(λ i ih, by simpa using S.add_mem ih S.one_mem)
(λ i ih, show ((-i - 1 : ℤ) : R) ∈ S, by { rw [int.cast_sub, int.cast_one],
exact S.sub_mem ih S.one_mem }),
.. S }
variables {S : Type*} [semiring S]
@[simp] lemma mem_subalgebra_of_subring {x : R} {S : subring R} :
x ∈ subalgebra_of_subring S ↔ x ∈ S :=
iff.rfl
end int
|
8f086c5085db9f9b12a9cd79c021d3588e13a5ff | c777c32c8e484e195053731103c5e52af26a25d1 | /src/probability/ident_distrib.lean | 75775ebd5c98aa07d8b61fbe892fa21428291654 | [
"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 | 15,566 | 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.variance
import measure_theory.function.uniform_integrable
/-!
# Identically distributed random variables
Two random variables defined on two (possibly different) probability spaces but taking value in
the same space are *identically distributed* if their distributions (i.e., the image probability
measures on the target space) coincide. We define this concept and establish its basic properties
in this file.
## Main definitions and results
* `ident_distrib f g μ ν` registers that the image of `μ` under `f` coincides with the image of `ν`
under `g` (and that `f` and `g` are almost everywhere measurable, as otherwise the image measures
don't make sense). The measures can be kept implicit as in `ident_distrib f g` if the spaces
are registered as measure spaces.
* `ident_distrib.comp`: being identically distributed is stable under composition with measurable
maps.
There are two main kind of lemmas, under the assumption that `f` and `g` are identically
distributed: lemmas saying that two quantities computed for `f` and `g` are the same, and lemmas
saying that if `f` has some property then `g` also has it. The first kind is registered as
`ident_distrib.foo_eq`, the second one as `ident_distrib.foo_snd` (in the latter case, to deduce
a property of `f` from one of `g`, use `h.symm.foo_snd` where `h : ident_distrib f g μ ν`). For
instance:
* `ident_distrib.measure_mem_eq`: if `f` and `g` are identically distributed, then the probabilities
that they belong to a given measurable set are the same.
* `ident_distrib.integral_eq`: if `f` and `g` are identically distributed, then their integrals
are the same.
* `ident_distrib.variance_eq`: if `f` and `g` are identically distributed, then their variances
are the same.
* `ident_distrib.ae_strongly_measurable_snd`: if `f` and `g` are identically distributed and `f`
is almost everywhere strongly measurable, then so is `g`.
* `ident_distrib.mem_ℒp_snd`: if `f` and `g` are identically distributed and `f`
belongs to `ℒp`, then so does `g`.
We also register several dot notation shortcuts for convenience.
For instance, if `h : ident_distrib f g μ ν`, then `h.sq` states that `f^2` and `g^2` are
identically distributed, and `h.norm` states that `‖f‖` and `‖g‖` are identically distributed, and
so on.
-/
open measure_theory filter finset
noncomputable theory
open_locale topology big_operators measure_theory ennreal nnreal
variables {α β γ δ : Type*} [measurable_space α] [measurable_space β]
[measurable_space γ] [measurable_space δ]
namespace probability_theory
/-- Two functions defined on two (possibly different) measure spaces are identically distributed if
their image measures coincide. This only makes sense when the functions are ae measurable
(as otherwise the image measures are not defined), so we require this as well in the definition. -/
structure ident_distrib
(f : α → γ) (g : β → γ) (μ : measure α . volume_tac) (ν : measure β . volume_tac) : Prop :=
(ae_measurable_fst : ae_measurable f μ)
(ae_measurable_snd : ae_measurable g ν)
(map_eq : measure.map f μ = measure.map g ν)
namespace ident_distrib
open topological_space
variables {μ : measure α} {ν : measure β} {f : α → γ} {g : β → γ}
protected lemma refl (hf : ae_measurable f μ) :
ident_distrib f f μ μ :=
{ ae_measurable_fst := hf,
ae_measurable_snd := hf,
map_eq := rfl }
protected lemma symm (h : ident_distrib f g μ ν) : ident_distrib g f ν μ :=
{ ae_measurable_fst := h.ae_measurable_snd,
ae_measurable_snd := h.ae_measurable_fst,
map_eq := h.map_eq.symm }
protected lemma trans {ρ : measure δ} {h : δ → γ}
(h₁ : ident_distrib f g μ ν) (h₂ : ident_distrib g h ν ρ) : ident_distrib f h μ ρ :=
{ ae_measurable_fst := h₁.ae_measurable_fst,
ae_measurable_snd := h₂.ae_measurable_snd,
map_eq := h₁.map_eq.trans h₂.map_eq }
protected lemma comp_of_ae_measurable {u : γ → δ} (h : ident_distrib f g μ ν)
(hu : ae_measurable u (measure.map f μ)) :
ident_distrib (u ∘ f) (u ∘ g) μ ν :=
{ ae_measurable_fst := hu.comp_ae_measurable h.ae_measurable_fst,
ae_measurable_snd :=
by { rw h.map_eq at hu, exact hu.comp_ae_measurable h.ae_measurable_snd },
map_eq :=
begin
rw [← ae_measurable.map_map_of_ae_measurable hu h.ae_measurable_fst,
← ae_measurable.map_map_of_ae_measurable _ h.ae_measurable_snd, h.map_eq],
rwa ← h.map_eq,
end }
protected lemma comp {u : γ → δ} (h : ident_distrib f g μ ν) (hu : measurable u) :
ident_distrib (u ∘ f) (u ∘ g) μ ν :=
h.comp_of_ae_measurable hu.ae_measurable
protected lemma of_ae_eq {g : α → γ} (hf : ae_measurable f μ) (heq : f =ᵐ[μ] g) :
ident_distrib f g μ μ :=
{ ae_measurable_fst := hf,
ae_measurable_snd := hf.congr heq,
map_eq := measure.map_congr heq }
lemma measure_mem_eq (h : ident_distrib f g μ ν) {s : set γ} (hs : measurable_set s) :
μ (f ⁻¹' s) = ν (g ⁻¹' s) :=
by rw [← measure.map_apply_of_ae_measurable h.ae_measurable_fst hs,
← measure.map_apply_of_ae_measurable h.ae_measurable_snd hs, h.map_eq]
alias measure_mem_eq ← measure_preimage_eq
lemma ae_snd (h : ident_distrib f g μ ν) {p : γ → Prop}
(pmeas : measurable_set {x | p x}) (hp : ∀ᵐ x ∂μ, p (f x)) :
∀ᵐ x ∂ν, p (g x) :=
begin
apply (ae_map_iff h.ae_measurable_snd pmeas).1,
rw ← h.map_eq,
exact (ae_map_iff h.ae_measurable_fst pmeas).2 hp,
end
lemma ae_mem_snd (h : ident_distrib f g μ ν) {t : set γ}
(tmeas : measurable_set t) (ht : ∀ᵐ x ∂μ, f x ∈ t) :
∀ᵐ x ∂ν, g x ∈ t :=
h.ae_snd tmeas ht
/-- In a second countable topology, the first function in an identically distributed pair is a.e.
strongly measurable. So is the second function, but use `h.symm.ae_strongly_measurable_fst` as
`h.ae_strongly_measurable_snd` has a different meaning.-/
lemma ae_strongly_measurable_fst [topological_space γ]
[metrizable_space γ] [opens_measurable_space γ] [second_countable_topology γ]
(h : ident_distrib f g μ ν) :
ae_strongly_measurable f μ :=
h.ae_measurable_fst.ae_strongly_measurable
/-- If `f` and `g` are identically distributed and `f` is a.e. strongly measurable, so is `g`. -/
lemma ae_strongly_measurable_snd [topological_space γ] [metrizable_space γ] [borel_space γ]
(h : ident_distrib f g μ ν) (hf : ae_strongly_measurable f μ) :
ae_strongly_measurable g ν :=
begin
refine ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨h.ae_measurable_snd, _⟩,
rcases (ae_strongly_measurable_iff_ae_measurable_separable.1 hf).2 with ⟨t, t_sep, ht⟩,
refine ⟨closure t, t_sep.closure, _⟩,
apply h.ae_mem_snd is_closed_closure.measurable_set,
filter_upwards [ht] with x hx using subset_closure hx,
end
lemma ae_strongly_measurable_iff [topological_space γ] [metrizable_space γ] [borel_space γ]
(h : ident_distrib f g μ ν) :
ae_strongly_measurable f μ ↔ ae_strongly_measurable g ν :=
⟨λ hf, h.ae_strongly_measurable_snd hf, λ hg, h.symm.ae_strongly_measurable_snd hg⟩
lemma ess_sup_eq [conditionally_complete_linear_order γ] [topological_space γ]
[opens_measurable_space γ] [order_closed_topology γ] (h : ident_distrib f g μ ν) :
ess_sup f μ = ess_sup g ν :=
begin
have I : ∀ a, μ {x : α | a < f x} = ν {x : β | a < g x} :=
λ a, h.measure_mem_eq measurable_set_Ioi,
simp_rw [ess_sup_eq_Inf, I],
end
lemma lintegral_eq {f : α → ℝ≥0∞} {g : β → ℝ≥0∞} (h : ident_distrib f g μ ν) :
∫⁻ x, f x ∂μ = ∫⁻ x, g x ∂ν :=
begin
change ∫⁻ x, id (f x) ∂μ = ∫⁻ x, id (g x) ∂ν,
rw [← lintegral_map' ae_measurable_id h.ae_measurable_fst,
← lintegral_map' ae_measurable_id h.ae_measurable_snd, h.map_eq],
end
lemma integral_eq [normed_add_comm_group γ] [normed_space ℝ γ] [complete_space γ] [borel_space γ]
(h : ident_distrib f g μ ν) : ∫ x, f x ∂μ = ∫ x, g x ∂ν :=
begin
by_cases hf : ae_strongly_measurable f μ,
{ have A : ae_strongly_measurable id (measure.map f μ),
{ rw ae_strongly_measurable_iff_ae_measurable_separable,
rcases (ae_strongly_measurable_iff_ae_measurable_separable.1 hf).2 with ⟨t, t_sep, ht⟩,
refine ⟨ae_measurable_id, ⟨closure t, t_sep.closure, _⟩⟩,
rw ae_map_iff h.ae_measurable_fst,
{ filter_upwards [ht] with x hx using subset_closure hx },
{ exact is_closed_closure.measurable_set } },
change ∫ x, id (f x) ∂μ = ∫ x, id (g x) ∂ν,
rw [← integral_map h.ae_measurable_fst A],
rw h.map_eq at A,
rw [← integral_map h.ae_measurable_snd A, h.map_eq] },
{ rw integral_non_ae_strongly_measurable hf,
rw h.ae_strongly_measurable_iff at hf,
rw integral_non_ae_strongly_measurable hf }
end
lemma snorm_eq [normed_add_comm_group γ] [opens_measurable_space γ] (h : ident_distrib f g μ ν)
(p : ℝ≥0∞) :
snorm f p μ = snorm g p ν :=
begin
by_cases h0 : p = 0,
{ simp [h0], },
by_cases h_top : p = ∞,
{ simp only [h_top, snorm, snorm_ess_sup, ennreal.top_ne_zero, eq_self_iff_true, if_true,
if_false],
apply ess_sup_eq,
exact h.comp (measurable_coe_nnreal_ennreal.comp measurable_nnnorm) },
simp only [snorm_eq_snorm' h0 h_top, snorm', one_div],
congr' 1,
apply lintegral_eq,
exact h.comp
(measurable.pow_const (measurable_coe_nnreal_ennreal.comp measurable_nnnorm) p.to_real),
end
lemma mem_ℒp_snd [normed_add_comm_group γ] [borel_space γ]
{p : ℝ≥0∞} (h : ident_distrib f g μ ν) (hf : mem_ℒp f p μ) :
mem_ℒp g p ν :=
begin
refine ⟨h.ae_strongly_measurable_snd hf.ae_strongly_measurable, _⟩,
rw ← h.snorm_eq,
exact hf.2
end
lemma mem_ℒp_iff [normed_add_comm_group γ] [borel_space γ] {p : ℝ≥0∞} (h : ident_distrib f g μ ν) :
mem_ℒp f p μ ↔ mem_ℒp g p ν :=
⟨λ hf, h.mem_ℒp_snd hf, λ hg, h.symm.mem_ℒp_snd hg⟩
lemma integrable_snd [normed_add_comm_group γ] [borel_space γ] (h : ident_distrib f g μ ν)
(hf : integrable f μ) : integrable g ν :=
begin
rw ← mem_ℒp_one_iff_integrable at hf ⊢,
exact h.mem_ℒp_snd hf
end
lemma integrable_iff [normed_add_comm_group γ] [borel_space γ] (h : ident_distrib f g μ ν) :
integrable f μ ↔ integrable g ν :=
⟨λ hf, h.integrable_snd hf, λ hg, h.symm.integrable_snd hg⟩
protected lemma norm [normed_add_comm_group γ] [borel_space γ] (h : ident_distrib f g μ ν) :
ident_distrib (λ x, ‖f x‖) (λ x, ‖g x‖) μ ν :=
h.comp measurable_norm
protected lemma nnnorm [normed_add_comm_group γ] [borel_space γ] (h : ident_distrib f g μ ν) :
ident_distrib (λ x, ‖f x‖₊) (λ x, ‖g x‖₊) μ ν :=
h.comp measurable_nnnorm
protected lemma pow [has_pow γ ℕ] [has_measurable_pow γ ℕ] (h : ident_distrib f g μ ν) {n : ℕ} :
ident_distrib (λ x, (f x) ^ n) (λ x, (g x) ^ n) μ ν :=
h.comp (measurable_id.pow_const n)
protected lemma sq [has_pow γ ℕ] [has_measurable_pow γ ℕ] (h : ident_distrib f g μ ν) :
ident_distrib (λ x, (f x) ^ 2) (λ x, (g x) ^ 2) μ ν :=
h.comp (measurable_id.pow_const 2)
protected lemma coe_nnreal_ennreal {f : α → ℝ≥0} {g : β → ℝ≥0} (h : ident_distrib f g μ ν) :
ident_distrib (λ x, (f x : ℝ≥0∞)) (λ x, (g x : ℝ≥0∞)) μ ν :=
h.comp measurable_coe_nnreal_ennreal
@[to_additive]
lemma mul_const [has_mul γ] [has_measurable_mul γ] (h : ident_distrib f g μ ν) (c : γ) :
ident_distrib (λ x, f x * c) (λ x, g x * c) μ ν :=
h.comp (measurable_mul_const c)
@[to_additive]
lemma const_mul [has_mul γ] [has_measurable_mul γ] (h : ident_distrib f g μ ν) (c : γ) :
ident_distrib (λ x, c * f x) (λ x, c * g x) μ ν :=
h.comp (measurable_const_mul c)
@[to_additive]
lemma div_const [has_div γ] [has_measurable_div γ] (h : ident_distrib f g μ ν) (c : γ) :
ident_distrib (λ x, f x / c) (λ x, g x / c) μ ν :=
h.comp (has_measurable_div.measurable_div_const c)
@[to_additive]
lemma const_div [has_div γ] [has_measurable_div γ] (h : ident_distrib f g μ ν) (c : γ) :
ident_distrib (λ x, c / f x) (λ x, c / g x) μ ν :=
h.comp (has_measurable_div.measurable_const_div c)
lemma evariance_eq {f : α → ℝ} {g : β → ℝ} (h : ident_distrib f g μ ν) :
evariance f μ = evariance g ν :=
begin
convert (h.sub_const (∫ x, f x ∂μ)).nnnorm.coe_nnreal_ennreal.sq.lintegral_eq,
rw h.integral_eq,
refl
end
lemma variance_eq {f : α → ℝ} {g : β → ℝ} (h : ident_distrib f g μ ν) :
variance f μ = variance g ν :=
by { rw [variance, h.evariance_eq], refl, }
end ident_distrib
section uniform_integrable
open topological_space
variables {E : Type*} [measurable_space E] [normed_add_comm_group E] [borel_space E]
[second_countable_topology E] {μ : measure α} [is_finite_measure μ]
/-- This lemma is superceded by `mem_ℒp.uniform_integrable_of_ident_distrib` which only require
`ae_strongly_measurable`. -/
lemma mem_ℒp.uniform_integrable_of_ident_distrib_aux {ι : Type*} {f : ι → α → E}
{j : ι} {p : ℝ≥0∞} (hp : 1 ≤ p) (hp' : p ≠ ∞)
(hℒp : mem_ℒp (f j) p μ) (hfmeas : ∀ i, strongly_measurable (f i))
(hf : ∀ i, ident_distrib (f i) (f j) μ μ) :
uniform_integrable f p μ :=
begin
refine uniform_integrable_of' hp hp' hfmeas (λ ε hε, _),
by_cases hι : nonempty ι,
swap, { exact ⟨0, λ i, false.elim (hι $ nonempty.intro i)⟩ },
obtain ⟨C, hC₁, hC₂⟩ := hℒp.snorm_indicator_norm_ge_pos_le μ (hfmeas _) hε,
have hmeas : ∀ i, measurable_set {x | (⟨C, hC₁.le⟩ : ℝ≥0) ≤ ‖f i x‖₊} :=
λ i, measurable_set_le measurable_const (hfmeas _).measurable.nnnorm,
refine ⟨⟨C, hC₁.le⟩, λ i, le_trans (le_of_eq _) hC₂⟩,
have : {x : α | (⟨C, hC₁.le⟩ : ℝ≥0) ≤ ‖f i x‖₊}.indicator (f i) =
(λ x : E, if (⟨C, hC₁.le⟩ : ℝ≥0) ≤ ‖x‖₊ then x else 0) ∘ (f i),
{ ext x,
simp only [set.indicator, set.mem_set_of_eq] },
simp_rw [coe_nnnorm, this],
rw [← snorm_map_measure _ (hf i).ae_measurable_fst, (hf i).map_eq,
snorm_map_measure _ (hf j).ae_measurable_fst],
{ refl },
all_goals { exact ae_strongly_measurable_id.indicator
(measurable_set_le measurable_const measurable_nnnorm) },
end
/-- A sequence of identically distributed Lᵖ functions is p-uniformly integrable. -/
lemma mem_ℒp.uniform_integrable_of_ident_distrib {ι : Type*} {f : ι → α → E}
{j : ι} {p : ℝ≥0∞} (hp : 1 ≤ p) (hp' : p ≠ ∞)
(hℒp : mem_ℒp (f j) p μ) (hf : ∀ i, ident_distrib (f i) (f j) μ μ) :
uniform_integrable f p μ :=
begin
have hfmeas : ∀ i, ae_strongly_measurable (f i) μ :=
λ i, (hf i).ae_strongly_measurable_iff.2 hℒp.1,
set g : ι → α → E := λ i, (hfmeas i).some,
have hgmeas : ∀ i, strongly_measurable (g i) := λ i, (Exists.some_spec $ hfmeas i).1,
have hgeq : ∀ i, g i =ᵐ[μ] f i := λ i, (Exists.some_spec $ hfmeas i).2.symm,
have hgℒp : mem_ℒp (g j) p μ := hℒp.ae_eq (hgeq j).symm,
exact uniform_integrable.ae_eq (mem_ℒp.uniform_integrable_of_ident_distrib_aux hp hp'
hgℒp hgmeas $
λ i, (ident_distrib.of_ae_eq (hgmeas i).ae_measurable (hgeq i)).trans ((hf i).trans
$ ident_distrib.of_ae_eq (hfmeas j).ae_measurable (hgeq j).symm)) hgeq,
end
end uniform_integrable
end probability_theory
|
aeaec10cbd93ec2e61369bc1743c497eed472683 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/lean/run/typeclass_easy.lean | 93472317fadb83661ccf956ee69f9587da80babf | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 191 | lean | new_frontend
set_option ppOld false
#synth HasToString (Nat × (Nat × Bool))
#synth HasAdd Nat
#synth HasCoe Bool Prop
#synth Decidable (True ∧ 1 = 1)
#synth DecidableEq (Nat × Nat)
|
e604ca00592910d3a70c4f32cec6fcc4d7a822d7 | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /library/init/data/to_string.lean | 98cff7368b89a7064fd4fb6547dc0235f11ba740 | [
"Apache-2.0"
] | permissive | bre7k30/lean | de893411bcfa7b3c5572e61b9e1c52951b310aa4 | 5a924699d076dab1bd5af23a8f910b433e598d7a | refs/heads/master | 1,610,900,145,817 | 1,488,006,845,000 | 1,488,006,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,584 | 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.data.string.basic init.data.bool.basic init.data.subtype.basic
import init.data.unsigned init.data.prod init.data.sum.basic init.data.nat.div
open sum subtype nat
universes u v
class has_to_string (α : Type u) :=
(to_string : α → string)
def to_string {α : Type u} [has_to_string α] : α → string :=
has_to_string.to_string
instance : has_to_string bool :=
⟨λ b, cond b "tt" "ff"⟩
instance {p : Prop} : has_to_string (decidable p) :=
-- Remark: type class inference will not consider local instance `b` in the new elaborator
⟨λ b : decidable p, @ite p b _ "tt" "ff"⟩
protected def list.to_string_aux {α : Type u} [has_to_string α] : bool → list α → string
| b [] := ""
| tt (x::xs) := to_string x ++ list.to_string_aux ff xs
| ff (x::xs) := ", " ++ to_string x ++ list.to_string_aux ff xs
protected def list.to_string {α : Type u} [has_to_string α] : list α → string
| [] := "[]"
| (x::xs) := "[" ++ list.to_string_aux tt (x::xs) ++ "]"
instance {α : Type u} [has_to_string α] : has_to_string (list α) :=
⟨list.to_string⟩
instance : has_to_string unit :=
⟨λ u, "star"⟩
instance {α : Type u} [has_to_string α] : has_to_string (option α) :=
⟨λ o, match o with | none := "none" | (some a) := "(some " ++ to_string a ++ ")" end⟩
instance {α : Type u} {β : Type v} [has_to_string α] [has_to_string β] : has_to_string (α ⊕ β) :=
⟨λ s, match s with | (inl a) := "(inl " ++ to_string a ++ ")" | (inr b) := "(inr " ++ to_string b ++ ")" end⟩
instance {α : Type u} {β : Type v} [has_to_string α] [has_to_string β] : has_to_string (α × β) :=
⟨λ p, "(" ++ to_string p.1 ++ ", " ++ to_string p.2 ++ ")"⟩
instance {α : Type u} {β : α → Type v} [has_to_string α] [s : ∀ x, has_to_string (β x)] : has_to_string (sigma β) :=
⟨λ p, "⟨" ++ to_string p.1 ++ ", " ++ to_string p.2 ++ "⟩"⟩
instance {α : Type u} {p : α → Prop} [has_to_string α] : has_to_string (subtype p) :=
⟨λ s, to_string (elt_of s)⟩
/- Remark: the code generator replaces this definition with one that display natural numbers in decimal notation -/
protected def nat.to_string : nat → string
| 0 := "zero"
| (succ a) := "(succ " ++ nat.to_string a ++ ")"
instance : has_to_string nat :=
⟨nat.to_string⟩
def hex_digit_to_string (n : nat) : string :=
if n ≤ 9 then to_string n
else if n = 10 then "a"
else if n = 11 then "b"
else if n = 12 then "c"
else if n = 13 then "d"
else if n = 14 then "e"
else "f"
def char_to_hex (c : char) : string :=
let n := char.to_nat c,
d2 := div n 16,
d1 := n % 16
in hex_digit_to_string d2 ++ hex_digit_to_string d1
def char.quote_core (c : char) : string :=
if c = #"\n" then "\\n"
else if c = #"\t" then "\\t"
else if c = #"\\" then "\\\\"
else if c = #"\"" then "\\\""
else if char.to_nat c <= 31 then "\\x" ++ char_to_hex c
else [c]
instance : has_to_string char :=
⟨λ c, "#\"" ++ char.quote_core c ++ "\""⟩
def string.quote_aux : string → string
| [] := ""
| (x::xs) := string.quote_aux xs ++ char.quote_core x
def string.quote : string → string
| [] := "\"\""
| (x::xs) := "\"" ++ string.quote_aux (x::xs) ++ "\""
instance : has_to_string string :=
⟨string.quote⟩
instance (n : nat) : has_to_string (fin n) :=
⟨λ f, to_string (fin.val f)⟩
instance : has_to_string unsigned :=
⟨λ n, to_string (fin.val n)⟩
|
da14a979085fca2c2147a400be4ed33a3b3ff566 | 08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4 | /src/Init/Data/String/Basic.lean | 2b2c42e739f4eb85dae374afef1b0f88cf5d207a | [
"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",
"Apache-2.0",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | gebner/lean4 | d51c4922640a52a6f7426536ea669ef18a1d9af5 | 8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f | refs/heads/master | 1,685,732,780,391 | 1,672,962,627,000 | 1,673,459,398,000 | 373,307,283 | 0 | 0 | Apache-2.0 | 1,691,316,730,000 | 1,622,669,271,000 | Lean | UTF-8 | Lean | false | false | 19,662 | 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.Data.List.Basic
import Init.Data.Char.Basic
import Init.Data.Option.Basic
universe u
def List.asString (s : List Char) : String :=
⟨s⟩
namespace String
instance : OfNat String.Pos (nat_lit 0) where
ofNat := {}
instance : LT String :=
⟨fun s₁ s₂ => s₁.data < s₂.data⟩
@[extern "lean_string_dec_lt"]
instance decLt (s₁ s₂ : @& String) : Decidable (s₁ < s₂) :=
List.hasDecidableLt s₁.data s₂.data
@[extern "lean_string_length"]
def length : (@& String) → Nat
| ⟨s⟩ => s.length
/-- The internal implementation uses dynamic arrays and will perform destructive updates
if the String is not shared. -/
@[extern "lean_string_push"]
def push : String → Char → String
| ⟨s⟩, c => ⟨s ++ [c]⟩
/-- The internal implementation uses dynamic arrays and will perform destructive updates
if the String is not shared. -/
@[extern "lean_string_append"]
def append : String → (@& String) → String
| ⟨a⟩, ⟨b⟩ => ⟨a ++ b⟩
/-- O(n) in the runtime, where n is the length of the String -/
def toList (s : String) : List Char :=
s.data
private def utf8GetAux : List Char → Pos → Pos → Char
| [], _, _ => default
| c::cs, i, p => if i = p then c else utf8GetAux cs (i + c) p
/--
Return character at position `p`. If `p` is not a valid position
returns `(default : Char)`.
See `utf8GetAux` for the reference implementation.
-/
@[extern "lean_string_utf8_get"]
def get (s : @& String) (p : @& Pos) : Char :=
match s with
| ⟨s⟩ => utf8GetAux s 0 p
private def utf8GetAux? : List Char → Pos → Pos → Option Char
| [], _, _ => none
| c::cs, i, p => if i = p then c else utf8GetAux cs (i + c) p
@[extern "lean_string_utf8_get_opt"]
def get? : (@& String) → (@& Pos) → Option Char
| ⟨s⟩, p => utf8GetAux? s 0 p
/--
Similar to `get`, but produces a panic error message if `p` is not a valid `String.Pos`.
-/
@[extern "lean_string_utf8_get_bang"]
def get! (s : @& String) (p : @& Pos) : Char :=
match s with
| ⟨s⟩ => utf8GetAux s 0 p
private def utf8SetAux (c' : Char) : List Char → Pos → Pos → List Char
| [], _, _ => []
| c::cs, i, p =>
if i = p then (c'::cs) else c::(utf8SetAux c' cs (i + c) p)
@[extern "lean_string_utf8_set"]
def set : String → (@& Pos) → Char → String
| ⟨s⟩, i, c => ⟨utf8SetAux c s 0 i⟩
def modify (s : String) (i : Pos) (f : Char → Char) : String :=
s.set i <| f <| s.get i
@[extern "lean_string_utf8_next"]
def next (s : @& String) (p : @& Pos) : Pos :=
let c := get s p
p + c
private def utf8PrevAux : List Char → Pos → Pos → Pos
| [], _, _ => 0
| c::cs, i, p =>
let i' := i + c
if i' = p then i else utf8PrevAux cs i' p
@[extern "lean_string_utf8_prev"]
def prev : (@& String) → (@& Pos) → Pos
| ⟨s⟩, p => if p = 0 then 0 else utf8PrevAux s 0 p
def front (s : String) : Char :=
get s 0
def back (s : String) : Char :=
get s (prev s s.endPos)
@[extern "lean_string_utf8_at_end"]
def atEnd : (@& String) → (@& Pos) → Bool
| s, p => p.byteIdx ≥ utf8ByteSize s
/--
Similar to `get` but runtime does not perform bounds check.
-/
@[extern "lean_string_utf8_get_fast"]
def get' (s : @& String) (p : @& Pos) (h : ¬ s.atEnd p) : Char :=
match s with
| ⟨s⟩ => utf8GetAux s 0 p
@[extern "lean_string_utf8_next_fast"]
def next' (s : @& String) (p : @& Pos) (h : ¬ s.atEnd p) : Pos :=
let c := get s p
p + c
/- TODO: remove `partial` keywords after we restore the tactic
framework and wellfounded recursion support -/
partial def posOfAux (s : String) (c : Char) (stopPos : Pos) (pos : Pos) : Pos :=
if pos >= stopPos then pos
else if s.get pos == c then pos
else posOfAux s c stopPos (s.next pos)
@[inline] def posOf (s : String) (c : Char) : Pos :=
posOfAux s c s.endPos 0
partial def revPosOfAux (s : String) (c : Char) (pos : Pos) : Option Pos :=
if s.get pos == c then some pos
else if pos == 0 then none
else revPosOfAux s c (s.prev pos)
def revPosOf (s : String) (c : Char) : Option Pos :=
if s.endPos == 0 then none
else revPosOfAux s c (s.prev s.endPos)
partial def findAux (s : String) (p : Char → Bool) (stopPos : Pos) (pos : Pos) : Pos :=
if pos >= stopPos then pos
else if p (s.get pos) then pos
else findAux s p stopPos (s.next pos)
@[inline] def find (s : String) (p : Char → Bool) : Pos :=
findAux s p s.endPos 0
partial def revFindAux (s : String) (p : Char → Bool) (pos : Pos) : Option Pos :=
if p (s.get pos) then some pos
else if pos == 0 then none
else revFindAux s p (s.prev pos)
def revFind (s : String) (p : Char → Bool) : Option Pos :=
if s.endPos == 0 then none
else revFindAux s p (s.prev s.endPos)
abbrev Pos.min (p₁ p₂ : Pos) : Pos :=
{ byteIdx := p₁.byteIdx.min p₂.byteIdx }
/-- Returns the first position where the two strings differ. -/
partial def firstDiffPos (a b : String) : Pos :=
let stopPos := a.endPos.min b.endPos
let rec loop (i : Pos) : Pos :=
if i >= stopPos || a.get i != b.get i then i
else loop (a.next i)
loop 0
@[extern "lean_string_utf8_extract"]
def extract : (@& String) → (@& Pos) → (@& Pos) → String
| ⟨s⟩, b, e => if b.byteIdx ≥ e.byteIdx then ⟨[]⟩ else ⟨go₁ s 0 b e⟩
where
go₁ : List Char → Pos → Pos → Pos → List Char
| [], _, _, _ => []
| s@(c::cs), i, b, e => if i = b then go₂ s i e else go₁ cs (i + c) b e
go₂ : List Char → Pos → Pos → List Char
| [], _, _ => []
| c::cs, i, e => if i = e then [] else c :: go₂ cs (i + c) e
@[specialize] partial def splitAux (s : String) (p : Char → Bool) (b : Pos) (i : Pos) (r : List String) : List String :=
if s.atEnd i then
let r := (s.extract b i)::r
r.reverse
else if p (s.get i) then
let i := s.next i
splitAux s p i i (s.extract b { byteIdx := i.byteIdx - 1 } :: r)
else
splitAux s p b (s.next i) r
@[specialize] def split (s : String) (p : Char → Bool) : List String :=
splitAux s p 0 0 []
partial def splitOnAux (s sep : String) (b : Pos) (i : Pos) (j : Pos) (r : List String) : List String :=
if s.atEnd i then
let r := if sep.atEnd j then ""::(s.extract b (i - j))::r else (s.extract b i)::r
r.reverse
else if s.get i == sep.get j then
let i := s.next i
let j := sep.next j
if sep.atEnd j then
splitOnAux s sep i i 0 (s.extract b (i - j)::r)
else
splitOnAux s sep b i j r
else
splitOnAux s sep b (s.next i) 0 r
def splitOn (s : String) (sep : String := " ") : List String :=
if sep == "" then [s] else splitOnAux s sep 0 0 0 []
instance : Inhabited String := ⟨""⟩
instance : Append String := ⟨String.append⟩
def str : String → Char → String := push
def pushn (s : String) (c : Char) (n : Nat) : String :=
n.repeat (fun s => s.push c) s
def isEmpty (s : String) : Bool :=
s.endPos == 0
def join (l : List String) : String :=
l.foldl (fun r s => r ++ s) ""
def singleton (c : Char) : String :=
"".push c
def intercalate (s : String) : List String → String
| [] => ""
| a :: as => go a s as
where go (acc : String) (s : String) : List String → String
| a :: as => go (acc ++ s ++ a) s as
| [] => acc
/-- Iterator for `String`. That is, a `String` and a position in that string. -/
structure Iterator where
s : String
i : Pos
deriving DecidableEq
def mkIterator (s : String) : Iterator :=
⟨s, 0⟩
abbrev iter := mkIterator
instance : SizeOf String.Iterator where
sizeOf i := i.1.utf8ByteSize - i.2.byteIdx
theorem Iterator.sizeOf_eq (i : String.Iterator) : sizeOf i = i.1.utf8ByteSize - i.2.byteIdx :=
rfl
namespace Iterator
def toString : Iterator → String
| ⟨s, _⟩ => s
def remainingBytes : Iterator → Nat
| ⟨s, i⟩ => s.endPos.byteIdx - i.byteIdx
def pos : Iterator → Pos
| ⟨_, i⟩ => i
def curr : Iterator → Char
| ⟨s, i⟩ => get s i
def next : Iterator → Iterator
| ⟨s, i⟩ => ⟨s, s.next i⟩
def prev : Iterator → Iterator
| ⟨s, i⟩ => ⟨s, s.prev i⟩
def atEnd : Iterator → Bool
| ⟨s, i⟩ => i.byteIdx ≥ s.endPos.byteIdx
def hasNext : Iterator → Bool
| ⟨s, i⟩ => i.byteIdx < s.endPos.byteIdx
def hasPrev : Iterator → Bool
| ⟨_, i⟩ => i.byteIdx > 0
def setCurr : Iterator → Char → Iterator
| ⟨s, i⟩, c => ⟨s.set i c, i⟩
def toEnd : Iterator → Iterator
| ⟨s, _⟩ => ⟨s, s.endPos⟩
def extract : Iterator → Iterator → String
| ⟨s₁, b⟩, ⟨s₂, e⟩ =>
if s₁ ≠ s₂ || b > e then ""
else s₁.extract b e
def forward : Iterator → Nat → Iterator
| it, 0 => it
| it, n+1 => forward it.next n
def remainingToString : Iterator → String
| ⟨s, i⟩ => s.extract i s.endPos
def nextn : Iterator → Nat → Iterator
| it, 0 => it
| it, i+1 => nextn it.next i
def prevn : Iterator → Nat → Iterator
| it, 0 => it
| it, i+1 => prevn it.prev i
end Iterator
partial def offsetOfPosAux (s : String) (pos : Pos) (i : Pos) (offset : Nat) : Nat :=
if i >= pos || s.atEnd i then
offset
else
offsetOfPosAux s pos (s.next i) (offset+1)
def offsetOfPos (s : String) (pos : Pos) : Nat :=
offsetOfPosAux s pos 0 0
@[specialize] partial def foldlAux {α : Type u} (f : α → Char → α) (s : String) (stopPos : Pos) (i : Pos) (a : α) : α :=
let rec loop (i : Pos) (a : α) :=
if i >= stopPos then a
else loop (s.next i) (f a (s.get i))
loop i a
@[inline] def foldl {α : Type u} (f : α → Char → α) (init : α) (s : String) : α :=
foldlAux f s s.endPos 0 init
@[specialize] partial def foldrAux {α : Type u} (f : Char → α → α) (a : α) (s : String) (stopPos : Pos) (i : Pos) : α :=
let rec loop (i : Pos) :=
if i >= stopPos then a
else f (s.get i) (loop (s.next i))
loop i
@[inline] def foldr {α : Type u} (f : Char → α → α) (init : α) (s : String) : α :=
foldrAux f init s s.endPos 0
@[specialize] partial def anyAux (s : String) (stopPos : Pos) (p : Char → Bool) (i : Pos) : Bool :=
let rec loop (i : Pos) :=
if i >= stopPos then false
else if p (s.get i) then true
else loop (s.next i)
loop i
@[inline] def any (s : String) (p : Char → Bool) : Bool :=
anyAux s s.endPos p 0
@[inline] def all (s : String) (p : Char → Bool) : Bool :=
!s.any (fun c => !p c)
def contains (s : String) (c : Char) : Bool :=
s.any (fun a => a == c)
@[specialize] partial def mapAux (f : Char → Char) (i : Pos) (s : String) : String :=
if s.atEnd i then s
else
let c := f (s.get i)
let s := s.set i c
mapAux f (s.next i) s
@[inline] def map (f : Char → Char) (s : String) : String :=
mapAux f 0 s
def isNat (s : String) : Bool :=
!s.isEmpty && s.all (·.isDigit)
def toNat? (s : String) : Option Nat :=
if s.isNat then
some <| s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0
else
none
/--
Return `true` iff the substring of byte size `sz` starting at position `off1` in `s1` is equal to that starting at `off2` in `s2.`.
False if either substring of that byte size does not exist. -/
partial def substrEq (s1 : String) (off1 : String.Pos) (s2 : String) (off2 : String.Pos) (sz : Nat) : Bool :=
off1.byteIdx + sz ≤ s1.endPos.byteIdx && off2.byteIdx + sz ≤ s2.endPos.byteIdx && loop off1 off2 { byteIdx := off1.byteIdx + sz }
where
loop (off1 off2 stop1 : Pos) :=
if off1.byteIdx >= stop1.byteIdx then
true
else
let c₁ := s1.get off1
let c₂ := s2.get off2
c₁ == c₂ && loop (off1 + c₁) (off2 + c₂) stop1
/-- Return true iff `p` is a prefix of `s` -/
def isPrefixOf (p : String) (s : String) : Bool :=
substrEq p 0 s 0 p.endPos.byteIdx
/-- Replace all occurrences of `pattern` in `s` with `replacment`. -/
partial def replace (s pattern replacement : String) : String :=
loop "" 0 0
where
loop (acc : String) (accStop pos : String.Pos) :=
if pos.byteIdx + pattern.endPos.byteIdx > s.endPos.byteIdx then
acc ++ s.extract accStop s.endPos
else if s.substrEq pos pattern 0 pattern.endPos.byteIdx then
loop (acc ++ s.extract accStop pos ++ replacement) (pos + pattern) (pos + pattern)
else
loop acc accStop (s.next pos)
end String
namespace Substring
@[inline] def isEmpty (ss : Substring) : Bool :=
ss.bsize == 0
@[inline] def toString : Substring → String
| ⟨s, b, e⟩ => s.extract b e
@[inline] def toIterator : Substring → String.Iterator
| ⟨s, b, _⟩ => ⟨s, b⟩
/-- Return the codepoint at the given offset into the substring. -/
@[inline] def get : Substring → String.Pos → Char
| ⟨s, b, _⟩, p => s.get (b+p)
/-- Given an offset of a codepoint into the substring,
return the offset there of the next codepoint. -/
@[inline] def next : Substring → String.Pos → String.Pos
| ⟨s, b, e⟩, p =>
let absP := b+p
if absP = e then p else { byteIdx := (s.next absP).byteIdx - b.byteIdx }
/-- Given an offset of a codepoint into the substring,
return the offset there of the previous codepoint. -/
@[inline] def prev : Substring → String.Pos → String.Pos
| ⟨s, b, _⟩, p =>
let absP := b+p
if absP = b then p else { byteIdx := (s.prev absP).byteIdx - b.byteIdx }
def nextn : Substring → Nat → String.Pos → String.Pos
| _, 0, p => p
| ss, i+1, p => ss.nextn i (ss.next p)
def prevn : Substring → Nat → String.Pos → String.Pos
| _, 0, p => p
| ss, i+1, p => ss.prevn i (ss.prev p)
@[inline] def front (s : Substring) : Char :=
s.get 0
/-- Return the offset into `s` of the first occurence of `c` in `s`,
or `s.bsize` if `c` doesn't occur. -/
@[inline] def posOf (s : Substring) (c : Char) : String.Pos :=
match s with
| ⟨s, b, e⟩ => { byteIdx := (String.posOfAux s c e b).byteIdx - b.byteIdx }
@[inline] def drop : Substring → Nat → Substring
| ss@⟨s, b, e⟩, n => ⟨s, b + ss.nextn n 0, e⟩
@[inline] def dropRight : Substring → Nat → Substring
| ss@⟨s, b, _⟩, n => ⟨s, b, b + ss.prevn n ⟨ss.bsize⟩⟩
@[inline] def take : Substring → Nat → Substring
| ss@⟨s, b, _⟩, n => ⟨s, b, b + ss.nextn n 0⟩
@[inline] def takeRight : Substring → Nat → Substring
| ss@⟨s, b, e⟩, n => ⟨s, b + ss.prevn n ⟨ss.bsize⟩, e⟩
@[inline] def atEnd : Substring → String.Pos → Bool
| ⟨_, b, e⟩, p => b + p == e
@[inline] def extract : Substring → String.Pos → String.Pos → Substring
| ⟨s, b, e⟩, b', e' => if b' ≥ e' then ⟨"", 0, 0⟩ else ⟨s, e.min (b+b'), e.min (b+e')⟩
partial def splitOn (s : Substring) (sep : String := " ") : List Substring :=
if sep == "" then
[s]
else
let rec loop (b i j : String.Pos) (r : List Substring) : List Substring :=
if i.byteIdx == s.bsize then
let r := if sep.atEnd j then
"".toSubstring :: s.extract b (i-j) :: r
else
s.extract b i :: r
r.reverse
else if s.get i == sep.get j then
let i := s.next i
let j := sep.next j
if sep.atEnd j then
loop i i 0 (s.extract b (i-j) :: r)
else
loop b i j r
else
loop b (s.next i) 0 r
loop 0 0 0 []
@[inline] def foldl {α : Type u} (f : α → Char → α) (init : α) (s : Substring) : α :=
match s with
| ⟨s, b, e⟩ => String.foldlAux f s e b init
@[inline] def foldr {α : Type u} (f : Char → α → α) (init : α) (s : Substring) : α :=
match s with
| ⟨s, b, e⟩ => String.foldrAux f init s e b
@[inline] def any (s : Substring) (p : Char → Bool) : Bool :=
match s with
| ⟨s, b, e⟩ => String.anyAux s e p b
@[inline] def all (s : Substring) (p : Char → Bool) : Bool :=
!s.any (fun c => !p c)
def contains (s : Substring) (c : Char) : Bool :=
s.any (fun a => a == c)
@[specialize] private partial def takeWhileAux (s : String) (stopPos : String.Pos) (p : Char → Bool) (i : String.Pos) : String.Pos :=
if i >= stopPos then i
else if p (s.get i) then takeWhileAux s stopPos p (s.next i)
else i
@[inline] def takeWhile : Substring → (Char → Bool) → Substring
| ⟨s, b, e⟩, p =>
let e := takeWhileAux s e p b;
⟨s, b, e⟩
@[inline] def dropWhile : Substring → (Char → Bool) → Substring
| ⟨s, b, e⟩, p =>
let b := takeWhileAux s e p b;
⟨s, b, e⟩
@[specialize] private partial def takeRightWhileAux (s : String) (begPos : String.Pos) (p : Char → Bool) (i : String.Pos) : String.Pos :=
if i == begPos then i
else
let i' := s.prev i
let c := s.get i'
if !p c then i
else takeRightWhileAux s begPos p i'
@[inline] def takeRightWhile : Substring → (Char → Bool) → Substring
| ⟨s, b, e⟩, p =>
let b := takeRightWhileAux s b p e
⟨s, b, e⟩
@[inline] def dropRightWhile : Substring → (Char → Bool) → Substring
| ⟨s, b, e⟩, p =>
let e := takeRightWhileAux s b p e
⟨s, b, e⟩
@[inline] def trimLeft (s : Substring) : Substring :=
s.dropWhile Char.isWhitespace
@[inline] def trimRight (s : Substring) : Substring :=
s.dropRightWhile Char.isWhitespace
@[inline] def trim : Substring → Substring
| ⟨s, b, e⟩ =>
let b := takeWhileAux s e Char.isWhitespace b
let e := takeRightWhileAux s b Char.isWhitespace e
⟨s, b, e⟩
def isNat (s : Substring) : Bool :=
s.all fun c => c.isDigit
def toNat? (s : Substring) : Option Nat :=
if s.isNat then
some <| s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0
else
none
def beq (ss1 ss2 : Substring) : Bool :=
ss1.bsize == ss2.bsize && ss1.str.substrEq ss1.startPos ss2.str ss2.startPos ss1.bsize
instance hasBeq : BEq Substring := ⟨beq⟩
end Substring
namespace String
def drop (s : String) (n : Nat) : String :=
(s.toSubstring.drop n).toString
def dropRight (s : String) (n : Nat) : String :=
(s.toSubstring.dropRight n).toString
def take (s : String) (n : Nat) : String :=
(s.toSubstring.take n).toString
def takeRight (s : String) (n : Nat) : String :=
(s.toSubstring.takeRight n).toString
def takeWhile (s : String) (p : Char → Bool) : String :=
(s.toSubstring.takeWhile p).toString
def dropWhile (s : String) (p : Char → Bool) : String :=
(s.toSubstring.dropWhile p).toString
def takeRightWhile (s : String) (p : Char → Bool) : String :=
(s.toSubstring.takeRightWhile p).toString
def dropRightWhile (s : String) (p : Char → Bool) : String :=
(s.toSubstring.dropRightWhile p).toString
def startsWith (s pre : String) : Bool :=
s.toSubstring.take pre.length == pre.toSubstring
def endsWith (s post : String) : Bool :=
s.toSubstring.takeRight post.length == post.toSubstring
def trimRight (s : String) : String :=
s.toSubstring.trimRight.toString
def trimLeft (s : String) : String :=
s.toSubstring.trimLeft.toString
def trim (s : String) : String :=
s.toSubstring.trim.toString
@[inline] def nextWhile (s : String) (p : Char → Bool) (i : String.Pos) : String.Pos :=
Substring.takeWhileAux s s.endPos p i
@[inline] def nextUntil (s : String) (p : Char → Bool) (i : String.Pos) : String.Pos :=
nextWhile s (fun c => !p c) i
def toUpper (s : String) : String :=
s.map Char.toUpper
def toLower (s : String) : String :=
s.map Char.toLower
def capitalize (s : String) :=
s.set 0 <| s.get 0 |>.toUpper
def decapitalize (s : String) :=
s.set 0 <| s.get 0 |>.toLower
end String
protected def Char.toString (c : Char) : String :=
String.singleton c
|
0c3a2a97c2b42f3f8e50672294461371db29b50f | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/real/ereal.lean | 4318cd8faa61d8b7626c007777579131d80d9f3a | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 3,264 | lean | /-
Copyright (c) 2019 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard
-/
import data.real.basic
/-!
# The extended reals [-∞, ∞].
This file defines `ereal`, the real numbers together with a top and bottom element,
referred to as ⊤ and ⊥. It is implemented as `with_top (with_bot ℝ)`
Addition and multiplication are problematic in the presence of ±∞, but
negation has a natural definition and satisfies the usual properties.
An addition is derived, but `ereal` is not even a monoid (there is no identity).
`ereal` is a `complete_lattice`; this is now deduced by type class inference from
the fact that `with_top (with_bot L)` is a complete lattice if `L` is
a conditionally complete lattice.
## Tags
real, ereal, complete lattice
## TODO
abs : ereal → ennreal
In Isabelle they define + - * and / (making junk choices for things like -∞ + ∞)
and then prove whatever bits of the ordered ring/field axioms still hold. They
also do some limits stuff (liminf/limsup etc).
See https://isabelle.in.tum.de/dist/library/HOL/HOL-Library/Extended_Real.html
-/
/-- ereal : The type `[-∞, ∞]` -/
@[derive [linear_order, order_bot, order_top,
has_Sup, has_Inf, complete_lattice, has_add]]
def ereal := with_top (with_bot ℝ)
namespace ereal
instance : has_coe ℝ ereal := ⟨some ∘ some⟩
@[simp, elim_cast] protected lemma coe_real_le {x y : ℝ} : (x : ereal) ≤ (y : ereal) ↔ x ≤ y :=
by { unfold_coes, norm_num }
@[simp, elim_cast] protected lemma coe_real_lt {x y : ℝ} : (x : ereal) < (y : ereal) ↔ x < y :=
by { unfold_coes, norm_num }
@[simp, elim_cast] protected lemma coe_real_inj' {x y : ℝ} : (x : ereal) = (y : ereal) ↔ x = y :=
by { unfold_coes, simp [option.some_inj] }
/- neg -/
/-- negation on ereal -/
protected def neg : ereal → ereal
| ⊥ := ⊤
| ⊤ := ⊥
| (x : ℝ) := (-x : ℝ)
instance : has_neg ereal := ⟨ereal.neg⟩
@[move_cast] protected lemma neg_def (x : ℝ) : ((-x : ℝ) : ereal) = -x := rfl
/-- - -a = a on ereal -/
protected theorem neg_neg : ∀ (a : ereal), - (- a) = a
| ⊥ := rfl
| ⊤ := rfl
| (a : ℝ) := by { norm_cast, simp [neg_neg a] }
theorem neg_inj (a b : ereal) (h : -a = -b) : a = b := by rw [←ereal.neg_neg a, h, ereal.neg_neg b]
/-- Even though ereal is not an additive group, -a = b ↔ -b = a still holds -/
theorem neg_eq_iff_neg_eq {a b : ereal} : -a = b ↔ -b = a :=
⟨by {intro h, rw ←h, exact ereal.neg_neg a},
by {intro h, rw ←h, exact ereal.neg_neg b}⟩
/-- if -a ≤ b then -b ≤ a on ereal -/
protected theorem neg_le_of_neg_le : ∀ {a b : ereal} (h : -a ≤ b), -b ≤ a
| ⊥ ⊥ h := h
| ⊥ (some b) h := by cases (top_le_iff.1 h)
| ⊤ l h := le_top
| (a : ℝ) ⊥ h := by cases (le_bot_iff.1 h)
| l ⊤ h := bot_le
| (a : ℝ) (b : ℝ) h := by { norm_cast at h ⊢, exact _root_.neg_le_of_neg_le h }
/-- -a ≤ b ↔ -b ≤ a on ereal-/
protected theorem neg_le {a b : ereal} : -a ≤ b ↔ -b ≤ a :=
⟨ereal.neg_le_of_neg_le, ereal.neg_le_of_neg_le⟩
/-- a ≤ -b → b ≤ -a on ereal -/
theorem le_neg_of_le_neg {a b : ereal} (h : a ≤ -b) : b ≤ -a :=
by rwa [←ereal.neg_neg b, ereal.neg_le, ereal.neg_neg]
end ereal
|
dd76f310ad8a25a35094f738a072a18e34528b32 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/tactic/omega/term.lean | 8afb7e044ec5489db364192144e5af6110c72b7e | [
"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 | 2,684 | lean | /- Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Seul Baek
Normalized linear integer arithmetic terms. -/
import tactic.omega.coeffs
namespace omega
/-- Shadow syntax of normalized terms. The first element
represents the constant term and the list represents
the coefficients. -/
@[derive inhabited]
def term : Type := int × list int
namespace term
/-- Evaluate a term using the valuation v. -/
@[simp] def val (v : nat → int) : term → int
| (b,as) := b + coeffs.val v as
@[simp] def neg : term → term
| (b,as) := (-b, list.func.neg as)
@[simp] def add : term → term → term
| (c1,cfs1) (c2,cfs2) := (c1+c2, list.func.add cfs1 cfs2)
@[simp] def sub : term → term → term
| (c1,cfs1) (c2,cfs2) := (c1 - c2, list.func.sub cfs1 cfs2)
@[simp] def mul (i : int) : term → term
| (b,as) := (i * b, as.map ((*) i))
@[simp] def div (i : int) : term → term
| (b,as) := (b/i, as.map (λ x, x / i))
lemma val_neg {v : nat → int} {t : term} :
(neg t).val v = -(t.val v) :=
begin
cases t with b as,
simp only [val, neg_add, neg, val, coeffs.val_neg]
end
@[simp] lemma val_sub {v : nat → int} {t1 t2 : term} :
(sub t1 t2).val v = t1.val v - t2.val v :=
begin
cases t1, cases t2,
simp only [add_assoc, coeffs.val_sub, neg_add_rev,
val, sub, add_comm, add_left_comm, sub_eq_add_neg]
end
@[simp] lemma val_add {v : nat → int} {t1 t2 : term} :
(add t1 t2).val v = t1.val v + t2.val v :=
begin
cases t1, cases t2,
simp only [coeffs.val_add, add,
val, add_comm, add_left_comm]
end
@[simp] lemma val_mul {v : nat → int} {i : int} {t : term} :
val v (mul i t) = i * (val v t) :=
begin
cases t,
simp only [mul, mul_add, add_mul, list.length_map,
coeffs.val, coeffs.val_between_map_mul, val, list.map]
end
lemma val_div {v : nat → int} {i b : int} {as : list int} :
i ∣ b → (∀ x ∈ as, i ∣ x) → (div i (b,as)).val v = (val v (b,as)) / i :=
begin
intros h1 h2, simp only [val, div, list.map],
rw [int.add_div_of_dvd h1 (coeffs.dvd_val h2)],
apply fun_mono_2 rfl,
rw ← coeffs.val_map_div h2
end
/-- Fresh de Brujin index not used by any variable ocurring in the term -/
def fresh_index (t : term) : nat := t.snd.length
def to_string (t : term) : string :=
t.2.enum.foldr (λ ⟨i, n⟩ r,
to_string n ++ " * x" ++ to_string i ++ " + " ++ r) (to_string t.1)
instance : has_to_string term := ⟨to_string⟩
end term
/-- Fresh de Brujin index not used by any variable ocurring in the list of terms -/
def terms.fresh_index : list term → nat
| [] := 0
| (t::ts) := max t.fresh_index (terms.fresh_index ts)
end omega
|
6f95b99c8c91d3ee9a74ceb273a0cd13b7747bf5 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/group_theory/noncomm_pi_coprod.lean | fdac562bc65491c1b179c77487f6baef89f15682 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 9,708 | lean | /-
Copyright (c) 2022 Joachim Breitner. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joachim Breitner
-/
import group_theory.order_of_element
import data.finset.noncomm_prod
import data.fintype.card
/-!
# Canonical homomorphism from a finite family of monoids
This file defines the construction of the canonical homomorphism from a family of monoids.
Given a family of morphisms `ϕ i : N i →* M` for each `i : ι` where elements in the
images of different morphisms commute, we obtain a canonical morphism
`monoid_hom.noncomm_pi_coprod : (Π i, N i) →* M` that coincides with `ϕ`
## Main definitions
* `monoid_hom.noncomm_pi_coprod : (Π i, N i) →* M` is the main homomorphism
* `subgroup.noncomm_pi_coprod : (Π i, H i) →* G` is the specialization to `H i : subgroup G`
and the subgroup embedding.
## Main theorems
* `monoid_hom.noncomm_pi_coprod` coincides with `ϕ i` when restricted to `N i`
* `monoid_hom.noncomm_pi_coprod_mrange`: The range of `monoid_hom.noncomm_pi_coprod` is
`⨆ (i : ι), (ϕ i).mrange`
* `monoid_hom.noncomm_pi_coprod_range`: The range of `monoid_hom.noncomm_pi_coprod` is
`⨆ (i : ι), (ϕ i).range`
* `subgroup.noncomm_pi_coprod_range`: The range of `subgroup.noncomm_pi_coprod` is `⨆ (i : ι), H i`.
* `monoid_hom.injective_noncomm_pi_coprod_of_independent`: in the case of groups, `pi_hom.hom` is
injective if the `ϕ` are injective and the ranges of the `ϕ` are independent.
* `monoid_hom.independent_range_of_coprime_order`: If the `N i` have coprime orders, then the ranges
of the `ϕ` are independent.
* `subgroup.independent_of_coprime_order`: If commuting normal subgroups `H i` have coprime orders,
they are independent.
-/
open_locale big_operators
section family_of_monoids
variables {M : Type*} [monoid M]
-- We have a family of monoids
-- The fintype assumption is not always used, but declared here, to keep things in order
variables {ι : Type*} [hdec : decidable_eq ι] [fintype ι]
variables {N : ι → Type*} [∀ i, monoid (N i)]
-- And morphisms ϕ into G
variables (ϕ : Π (i : ι), N i →* M)
-- We assume that the elements of different morphism commute
variables (hcomm : ∀ (i j : ι), i ≠ j → ∀ (x : N i) (y : N j), commute (ϕ i x) (ϕ j y))
include hcomm
-- We use `f` and `g` to denote elements of `Π (i : ι), N i`
variables (f g : Π (i : ι), N i)
namespace monoid_hom
/-- The canonical homomorphism from a family of monoids. -/
@[to_additive "The canonical homomorphism from a family of additive monoids.
See also `linear_map.lsum` for a linear version without the commutativity assumption."]
def noncomm_pi_coprod : (Π (i : ι), N i) →* M :=
{ to_fun := λ f, finset.univ.noncomm_prod (λ i, ϕ i (f i)) $
by { rintros i - j -, by_cases h : i = j, { subst h }, { exact hcomm _ _ h _ _ } },
map_one' := by {apply (finset.noncomm_prod_eq_pow_card _ _ _ _ _).trans (one_pow _), simp},
map_mul' := λ f g,
begin
classical,
convert @finset.noncomm_prod_mul_distrib _ _ _ _ (λ i, ϕ i (f i)) (λ i, ϕ i (g i)) _ _ _,
{ ext i, exact map_mul (ϕ i) (f i) (g i), },
{ rintros i - j - h, exact hcomm _ _ h _ _ },
end }
variable {hcomm}
include hdec
@[simp, to_additive]
lemma noncomm_pi_coprod_mul_single (i : ι) (y : N i):
noncomm_pi_coprod ϕ hcomm (pi.mul_single i y) = ϕ i y :=
begin
change finset.univ.noncomm_prod (λ j, ϕ j (pi.mul_single i y j)) _ = ϕ i y,
simp only [←finset.insert_erase (finset.mem_univ i)] {single_pass := tt},
rw finset.noncomm_prod_insert_of_not_mem _ _ _ _ (finset.not_mem_erase i _),
rw pi.mul_single_eq_same,
rw finset.noncomm_prod_eq_pow_card,
{ rw one_pow, exact mul_one _ },
{ intros j hj, simp only [finset.mem_erase] at hj, simp [hj], },
end
omit hcomm
/-- The universal property of `noncomm_pi_coprod` -/
@[to_additive "The universal property of `noncomm_pi_coprod`"]
def noncomm_pi_coprod_equiv :
{ϕ : Π i, N i →* M // pairwise (λ i j, ∀ x y, commute (ϕ i x) (ϕ j y)) }
≃ ((Π i, N i) →* M) :=
{ to_fun := λ ϕ, noncomm_pi_coprod ϕ.1 ϕ.2,
inv_fun := λ f,
⟨ λ i, f.comp (monoid_hom.single N i),
λ i j hij x y, commute.map (pi.mul_single_commute i j hij x y) f ⟩,
left_inv := λ ϕ, by { ext, simp, },
right_inv := λ f, pi_ext (λ i x, by simp) }
omit hdec
include hcomm
@[to_additive]
lemma noncomm_pi_coprod_mrange : (noncomm_pi_coprod ϕ hcomm).mrange = ⨆ i : ι, (ϕ i).mrange :=
begin
classical,
apply le_antisymm,
{ rintro x ⟨f, rfl⟩,
refine submonoid.noncomm_prod_mem _ _ _ _ _,
intros i hi,
apply submonoid.mem_Sup_of_mem, { use i },
simp, },
{ refine supr_le _,
rintro i x ⟨y, rfl⟩,
refine ⟨pi.mul_single i y, noncomm_pi_coprod_mul_single _ _ _⟩, },
end
end monoid_hom
end family_of_monoids
section family_of_groups
variables {G : Type*} [group G]
variables {ι : Type*} [hdec : decidable_eq ι] [hfin : fintype ι]
variables {H : ι → Type*} [∀ i, group (H i)]
variables (ϕ : Π (i : ι), H i →* G)
variables {hcomm : ∀ (i j : ι), i ≠ j → ∀ (x : H i) (y : H j), commute (ϕ i x) (ϕ j y)}
include hcomm
-- We use `f` and `g` to denote elements of `Π (i : ι), H i`
variables (f g : Π (i : ι), H i)
include hfin
namespace monoid_hom
-- The subgroup version of `noncomm_pi_coprod_mrange`
@[to_additive]
lemma noncomm_pi_coprod_range : (noncomm_pi_coprod ϕ hcomm).range = ⨆ i : ι, (ϕ i).range :=
begin
classical,
apply le_antisymm,
{ rintro x ⟨f, rfl⟩,
refine subgroup.noncomm_prod_mem _ _ _,
intros i hi,
apply subgroup.mem_Sup_of_mem, { use i },
simp, },
{ refine supr_le _,
rintro i x ⟨y, rfl⟩,
refine ⟨pi.mul_single i y, noncomm_pi_coprod_mul_single _ _ _⟩, },
end
@[to_additive]
lemma injective_noncomm_pi_coprod_of_independent
(hind : complete_lattice.independent (λ i, (ϕ i).range))
(hinj : ∀ i, function.injective (ϕ i)) :
function.injective (noncomm_pi_coprod ϕ hcomm):=
begin
classical,
apply (monoid_hom.ker_eq_bot_iff _).mp,
apply eq_bot_iff.mpr,
intros f heq1,
change finset.univ.noncomm_prod (λ i, ϕ i (f i)) _ = 1 at heq1,
change f = 1,
have : ∀ i, i ∈ finset.univ → ϕ i (f i) = 1 :=
subgroup.eq_one_of_noncomm_prod_eq_one_of_independent _ _ _ _ hind (by simp) heq1,
ext i,
apply hinj,
simp [this i (finset.mem_univ i)],
end
variable (hcomm)
omit hfin
@[to_additive]
lemma independent_range_of_coprime_order [finite ι] [Π i, fintype (H i)]
(hcoprime : ∀ i j, i ≠ j → nat.coprime (fintype.card (H i)) (fintype.card (H j))) :
complete_lattice.independent (λ i, (ϕ i).range) :=
begin
casesI nonempty_fintype ι,
classical,
rintros i f ⟨hxi, hxp⟩, dsimp at hxi hxp,
rw [supr_subtype', ← noncomm_pi_coprod_range] at hxp,
rotate, { intros _ _ hj, apply hcomm, exact hj ∘ subtype.ext },
cases hxp with g hgf, cases hxi with g' hg'f,
have hxi : order_of f ∣ fintype.card (H i),
{ rw ← hg'f, exact (order_of_map_dvd _ _).trans order_of_dvd_card_univ },
have hxp : order_of f ∣ ∏ j : {j // j ≠ i}, fintype.card (H j),
{ rw [← hgf, ← fintype.card_pi], exact (order_of_map_dvd _ _).trans order_of_dvd_card_univ },
change f = 1, rw [← pow_one f, ← order_of_dvd_iff_pow_eq_one],
convert ← nat.dvd_gcd hxp hxi, rw ← nat.coprime_iff_gcd_eq_one,
apply nat.coprime_prod_left, intros j _, apply hcoprime, exact j.2,
end
end monoid_hom
end family_of_groups
namespace subgroup
-- We have an family of subgroups
variables {G : Type*} [group G]
variables {ι : Type*} [hdec : decidable_eq ι] [hfin : fintype ι] {H : ι → subgroup G}
-- Elements of `Π (i : ι), H i` are called `f` and `g` here
variables (f g : Π (i : ι), H i)
section commuting_subgroups
-- We assume that the elements of different subgroups commute
variables (hcomm : ∀ (i j : ι), i ≠ j → ∀ (x y : G), x ∈ H i → y ∈ H j → commute x y)
include hcomm
@[to_additive]
lemma commute_subtype_of_commute (i j : ι) (hne : i ≠ j) :
∀ (x : H i) (y : H j), commute ((H i).subtype x) ((H j).subtype y) :=
by { rintros ⟨x, hx⟩ ⟨y, hy⟩, exact hcomm i j hne x y hx hy }
include hfin
/-- The canonical homomorphism from a family of subgroups where elements from different subgroups
commute -/
@[to_additive "The canonical homomorphism from a family of additive subgroups where elements from
different subgroups commute"]
def noncomm_pi_coprod : (Π (i : ι), H i) →* G :=
monoid_hom.noncomm_pi_coprod (λ i, (H i).subtype) (commute_subtype_of_commute hcomm)
variable {hcomm}
include hdec
@[simp, to_additive]
lemma noncomm_pi_coprod_mul_single (i : ι) (y : H i) :
noncomm_pi_coprod hcomm (pi.mul_single i y) = y :=
by apply monoid_hom.noncomm_pi_coprod_mul_single
omit hdec
@[to_additive]
lemma noncomm_pi_coprod_range : (noncomm_pi_coprod hcomm).range = ⨆ i : ι, H i :=
by simp [noncomm_pi_coprod, monoid_hom.noncomm_pi_coprod_range]
@[to_additive]
lemma injective_noncomm_pi_coprod_of_independent (hind : complete_lattice.independent H) :
function.injective (noncomm_pi_coprod hcomm) :=
begin
apply monoid_hom.injective_noncomm_pi_coprod_of_independent,
{ simpa using hind },
{ intro i, exact subtype.coe_injective }
end
variable (hcomm)
omit hfin
@[to_additive]
lemma independent_of_coprime_order [finite ι] [∀ i, fintype (H i)]
(hcoprime : ∀ i j, i ≠ j → nat.coprime (fintype.card (H i)) (fintype.card (H j))) :
complete_lattice.independent H :=
by simpa using monoid_hom.independent_range_of_coprime_order (λ i, (H i).subtype)
(commute_subtype_of_commute hcomm) hcoprime
end commuting_subgroups
end subgroup
|
9c368e96d58b55f8ce7023829230ef5b9acfbcff | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/1882.lean | 24801bfd6c1aa9cae3297befc5088978069e9176 | [
"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 | 144 | lean | theorem test (p : Nat × Nat) : (match p with | (a, b) => a = b) → (p.1 = p.2) := by
intro (h : (match p with | (a, b) => a = b))
exact h
|
bc641a2c9a65fe1610918b049e800cbd273b2ae4 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebraic_geometry/stalks.lean | 445e913282bee20e55c7f87d52d8e69c23058561 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 3,194 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebraic_geometry.presheafed_space
import topology.sheaves.stalks
/-!
# Stalks for presheaved spaces
This file lifts constructions of stalks and pushforwards of stalks to work with
the category of presheafed spaces.
-/
noncomputable theory
universes v u v' u'
open category_theory
open category_theory.limits category_theory.category category_theory.functor
open algebraic_geometry
open topological_space
open opposite
variables {C : Type u} [category.{v} C] [has_colimits C]
local attribute [tidy] tactic.op_induction'
open Top.presheaf
namespace algebraic_geometry.PresheafedSpace
/--
The stalk at `x` of a `PresheafedSpace`.
-/
def stalk (X : PresheafedSpace C) (x : X) : C := X.presheaf.stalk x
/--
A morphism of presheafed spaces induces a morphism of stalks.
-/
def stalk_map {X Y : PresheafedSpace C} (α : X ⟶ Y) (x : X) : Y.stalk (α.base x) ⟶ X.stalk x :=
(stalk_functor C (α.base x)).map (α.c) ≫ X.presheaf.stalk_pushforward C α.base x
@[simp, elementwise, reassoc]
lemma stalk_map_germ {X Y : PresheafedSpace C} (α : X ⟶ Y) (U : opens Y.carrier)
(x : (opens.map α.base).obj U) :
Y.presheaf.germ ⟨α.base x, x.2⟩ ≫ stalk_map α ↑x = α.c.app (op U) ≫ X.presheaf.germ x :=
by rw [stalk_map, stalk_functor_map_germ_assoc, stalk_pushforward_germ]
section restrict
-- PROJECT: restriction preserves stalks.
-- We'll want to define cofinal functors, show precomposing with a cofinal functor preserves
-- colimits, and (easily) verify that "open neighbourhoods of x within U" is cofinal in "open
-- neighbourhoods of x".
/-
def restrict_stalk_iso {U : Top} (X : PresheafedSpace C)
(f : U ⟶ (X : Top.{v})) (h : open_embedding f) (x : U) :
(X.restrict f h).stalk x ≅ X.stalk (f x) :=
begin
dsimp only [stalk, Top.presheaf.stalk, stalk_functor],
dsimp [colim],
sorry
end
-- TODO `restrict_stalk_iso` is compatible with `germ`.
-/
end restrict
namespace stalk_map
@[simp] lemma id (X : PresheafedSpace C) (x : X) : stalk_map (𝟙 X) x = 𝟙 (X.stalk x) :=
begin
dsimp [stalk_map],
simp only [stalk_pushforward.id],
rw [←map_comp],
convert (stalk_functor C x).map_id X.presheaf,
tidy,
end
-- TODO understand why this proof is still gross (i.e. requires using `erw`)
@[simp] lemma comp {X Y Z : PresheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (x : X) :
stalk_map (α ≫ β) x =
(stalk_map β (α.base x) : Z.stalk (β.base (α.base x)) ⟶ Y.stalk (α.base x)) ≫
(stalk_map α x : Y.stalk (α.base x) ⟶ X.stalk x) :=
begin
dsimp [stalk_map, stalk_functor, stalk_pushforward],
ext U,
op_induction U,
cases U,
simp only [colimit.ι_map_assoc, colimit.ι_pre_assoc, colimit.ι_pre,
whisker_left_app, whisker_right_app,
assoc, id_comp, map_id, map_comp],
dsimp,
simp only [map_id, assoc, pushforward.comp_inv_app],
-- FIXME Why doesn't simp do this:
erw [category_theory.functor.map_id],
erw [category_theory.functor.map_id],
erw [id_comp, id_comp, id_comp],
end
end stalk_map
end algebraic_geometry.PresheafedSpace
|
505d3fbf50d846f4e5bc9ccda8b2a2e8fe44db2d | 6dc0c8ce7a76229dd81e73ed4474f15f88a9e294 | /tests/lean/run/closure1.lean | c8d6f3ab888ce333f16dc9fa4887de4915f42c08 | [
"Apache-2.0"
] | permissive | williamdemeo/lean4 | 72161c58fe65c3ad955d6a3050bb7d37c04c0d54 | 6d00fcf1d6d873e195f9220c668ef9c58e9c4a35 | refs/heads/master | 1,678,305,356,877 | 1,614,708,995,000 | 1,614,708,995,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,471 | 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
|
5a95df9c7c75a3efa93234955dfd635fa783bb60 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/algebra/uniform_group_auto.lean | cff344f7809a6ae3637f2b6f833060dbaa30ee2c | [] | 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 | 12,031 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Johannes Hölzl
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.uniform_space.uniform_embedding
import Mathlib.topology.uniform_space.complete_separated
import Mathlib.topology.algebra.group
import Mathlib.tactic.abel
import Mathlib.PostPort
universes u_3 l u_1 u_2 u u_4 u_5
namespace Mathlib
/-!
# Uniform structure on topological groups
* `topological_add_group.to_uniform_space` and `topological_add_group_is_uniform` can be used to
construct a canonical uniformity for a topological add group.
* extension of ℤ-bilinear maps to complete groups (useful for ring completions)
* `add_group_with_zero_nhd`: construct the topological structure from a group with a neighbourhood
around zero. Then with `topological_add_group.to_uniform_space` one can derive a `uniform_space`.
-/
/-- A uniform (additive) group is a group in which the addition and negation are
uniformly continuous. -/
class uniform_add_group (α : Type u_3) [uniform_space α] [add_group α] where
uniform_continuous_sub : uniform_continuous fun (p : α × α) => prod.fst p - prod.snd p
theorem uniform_add_group.mk' {α : Type u_1} [uniform_space α] [add_group α]
(h₁ : uniform_continuous fun (p : α × α) => prod.fst p + prod.snd p)
(h₂ : uniform_continuous fun (p : α) => -p) : uniform_add_group α :=
sorry
theorem uniform_continuous_sub {α : Type u_1} [uniform_space α] [add_group α]
[uniform_add_group α] : uniform_continuous fun (p : α × α) => prod.fst p - prod.snd p :=
uniform_add_group.uniform_continuous_sub
theorem uniform_continuous.sub {α : Type u_1} {β : Type u_2} [uniform_space α] [add_group α]
[uniform_add_group α] [uniform_space β] {f : β → α} {g : β → α} (hf : uniform_continuous f)
(hg : uniform_continuous g) : uniform_continuous fun (x : β) => f x - g x :=
uniform_continuous.comp uniform_continuous_sub (uniform_continuous.prod_mk hf hg)
theorem uniform_continuous.neg {α : Type u_1} {β : Type u_2} [uniform_space α] [add_group α]
[uniform_add_group α] [uniform_space β] {f : β → α} (hf : uniform_continuous f) :
uniform_continuous fun (x : β) => -f x :=
sorry
theorem uniform_continuous_neg {α : Type u_1} [uniform_space α] [add_group α]
[uniform_add_group α] : uniform_continuous fun (x : α) => -x :=
uniform_continuous.neg uniform_continuous_id
theorem uniform_continuous.add {α : Type u_1} {β : Type u_2} [uniform_space α] [add_group α]
[uniform_add_group α] [uniform_space β] {f : β → α} {g : β → α} (hf : uniform_continuous f)
(hg : uniform_continuous g) : uniform_continuous fun (x : β) => f x + g x :=
sorry
theorem uniform_continuous_add {α : Type u_1} [uniform_space α] [add_group α]
[uniform_add_group α] : uniform_continuous fun (p : α × α) => prod.fst p + prod.snd p :=
uniform_continuous.add uniform_continuous_fst uniform_continuous_snd
protected instance uniform_add_group.to_topological_add_group {α : Type u_1} [uniform_space α]
[add_group α] [uniform_add_group α] : topological_add_group α :=
topological_add_group.mk (uniform_continuous.continuous uniform_continuous_neg)
protected instance prod.uniform_add_group {α : Type u_1} {β : Type u_2} [uniform_space α]
[add_group α] [uniform_add_group α] [uniform_space β] [add_group β] [uniform_add_group β] :
uniform_add_group (α × β) :=
uniform_add_group.mk
(uniform_continuous.prod_mk
(uniform_continuous.sub
(uniform_continuous.comp uniform_continuous_fst uniform_continuous_fst)
(uniform_continuous.comp uniform_continuous_fst uniform_continuous_snd))
(uniform_continuous.sub
(uniform_continuous.comp uniform_continuous_snd uniform_continuous_fst)
(uniform_continuous.comp uniform_continuous_snd uniform_continuous_snd)))
theorem uniformity_translate {α : Type u_1} [uniform_space α] [add_group α] [uniform_add_group α]
(a : α) :
filter.map (fun (x : α × α) => (prod.fst x + a, prod.snd x + a)) (uniformity α) =
uniformity α :=
sorry
theorem uniform_embedding_translate {α : Type u_1} [uniform_space α] [add_group α]
[uniform_add_group α] (a : α) : uniform_embedding fun (x : α) => x + a :=
sorry
theorem uniformity_eq_comap_nhds_zero (α : Type u_1) [uniform_space α] [add_group α]
[uniform_add_group α] :
uniformity α = filter.comap (fun (x : α × α) => prod.snd x - prod.fst x) (nhds 0) :=
sorry
theorem group_separation_rel {α : Type u_1} [uniform_space α] [add_group α] [uniform_add_group α]
(x : α) (y : α) : (x, y) ∈ Mathlib.separation_rel α ↔ x - y ∈ closure (singleton 0) :=
sorry
theorem uniform_continuous_of_tendsto_zero {α : Type u_1} {β : Type u_2} [uniform_space α]
[add_group α] [uniform_add_group α] [uniform_space β] [add_group β] [uniform_add_group β]
{f : α → β} [is_add_group_hom f] (h : filter.tendsto f (nhds 0) (nhds 0)) :
uniform_continuous f :=
sorry
theorem uniform_continuous_of_continuous {α : Type u_1} {β : Type u_2} [uniform_space α]
[add_group α] [uniform_add_group α] [uniform_space β] [add_group β] [uniform_add_group β]
{f : α → β} [is_add_group_hom f] (h : continuous f) : uniform_continuous f :=
sorry
/-- The right uniformity on a topological group. -/
def topological_add_group.to_uniform_space (G : Type u) [add_comm_group G] [topological_space G]
[topological_add_group G] : uniform_space G :=
uniform_space.mk
(uniform_space.core.mk (filter.comap (fun (p : G × G) => prod.snd p - prod.fst p) (nhds 0))
sorry sorry sorry)
sorry
theorem uniformity_eq_comap_nhds_zero' (G : Type u) [add_comm_group G] [topological_space G]
[topological_add_group G] :
uniformity G = filter.comap (fun (p : G × G) => prod.snd p - prod.fst p) (nhds 0) :=
rfl
theorem topological_add_group_is_uniform {G : Type u} [add_comm_group G] [topological_space G]
[topological_add_group G] : uniform_add_group G :=
sorry
theorem to_uniform_space_eq {G : Type u} [u : uniform_space G] [add_comm_group G]
[uniform_add_group G] : topological_add_group.to_uniform_space G = u :=
sorry
namespace add_comm_group
/- TODO: when modules are changed to have more explicit base ring, then change replace `is_Z_bilin`
by using `is_bilinear_map ℤ` from `tensor_product`. -/
/-- `ℤ`-bilinearity for maps between additive commutative groups. -/
class is_Z_bilin {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group α] [add_comm_group β]
[add_comm_group γ] (f : α × β → γ)
where
add_left : ∀ (a a' : α) (b : β), f (a + a', b) = f (a, b) + f (a', b)
add_right : ∀ (a : α) (b b' : β), f (a, b + b') = f (a, b) + f (a, b')
theorem is_Z_bilin.comp_hom {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4}
[add_comm_group α] [add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f]
{g : γ → δ} [add_comm_group δ] [is_add_group_hom g] : is_Z_bilin (g ∘ f) :=
sorry
protected instance is_Z_bilin.comp_swap {α : Type u_1} {β : Type u_2} {γ : Type u_3}
[add_comm_group α] [add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f] :
is_Z_bilin (f ∘ prod.swap) :=
is_Z_bilin.mk (fun (a a' : β) (b : α) => is_Z_bilin.add_right f b a a')
fun (a : β) (b b' : α) => is_Z_bilin.add_left f b b' a
theorem is_Z_bilin.zero_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group α]
[add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f] (b : β) : f (0, b) = 0 :=
sorry
theorem is_Z_bilin.zero_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group α]
[add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f] (a : α) : f (a, 0) = 0 :=
is_Z_bilin.zero_left (f ∘ prod.swap)
theorem is_Z_bilin.zero {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group α]
[add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f] : f (0, 0) = 0 :=
is_Z_bilin.zero_left f 0
theorem is_Z_bilin.neg_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group α]
[add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f] (a : α) (b : β) :
f (-a, b) = -f (a, b) :=
sorry
theorem is_Z_bilin.neg_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group α]
[add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f] (a : α) (b : β) :
f (a, -b) = -f (a, b) :=
is_Z_bilin.neg_left (f ∘ prod.swap) b a
theorem is_Z_bilin.sub_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group α]
[add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f] (a : α) (a' : α) (b : β) :
f (a - a', b) = f (a, b) - f (a', b) :=
sorry
theorem is_Z_bilin.sub_right {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group α]
[add_comm_group β] [add_comm_group γ] (f : α × β → γ) [is_Z_bilin f] (a : α) (b : β) (b' : β) :
f (a, b - b') = f (a, b) - f (a, b') :=
is_Z_bilin.sub_left (f ∘ prod.swap) b b' a
end add_comm_group
-- α, β and G are abelian topological groups, G is a uniform space
theorem is_Z_bilin.tendsto_zero_left {α : Type u_1} {β : Type u_2} [topological_space α]
[add_comm_group α] [topological_space β] [add_comm_group β] {G : Type u_5} [uniform_space G]
[add_comm_group G] {ψ : α × β → G} (hψ : continuous ψ) [ψbilin : add_comm_group.is_Z_bilin ψ]
(x₁ : α) : filter.tendsto ψ (nhds (x₁, 0)) (nhds 0) :=
sorry
theorem is_Z_bilin.tendsto_zero_right {α : Type u_1} {β : Type u_2} [topological_space α]
[add_comm_group α] [topological_space β] [add_comm_group β] {G : Type u_5} [uniform_space G]
[add_comm_group G] {ψ : α × β → G} (hψ : continuous ψ) [ψbilin : add_comm_group.is_Z_bilin ψ]
(y₁ : β) : filter.tendsto ψ (nhds (0, y₁)) (nhds 0) :=
eq.mp
(Eq._oldrec (Eq.refl (filter.tendsto ψ (nhds (0, y₁)) (nhds (ψ (0, y₁)))))
(add_comm_group.is_Z_bilin.zero_left ψ y₁))
(continuous.tendsto hψ (0, y₁))
-- β is a dense subgroup of α, inclusion is denoted by e
theorem tendsto_sub_comap_self {α : Type u_1} {β : Type u_2} [topological_space α]
[add_comm_group α] [topological_add_group α] [topological_space β] [add_comm_group β]
{e : β → α} [is_add_group_hom e] (de : dense_inducing e) (x₀ : α) :
filter.tendsto (fun (t : β × β) => prod.snd t - prod.fst t)
(filter.comap (fun (p : β × β) => (e (prod.fst p), e (prod.snd p))) (nhds (x₀, x₀)))
(nhds 0) :=
sorry
namespace dense_inducing
-- β is a dense subgroup of α, inclusion is denoted by e
-- δ is a dense subgroup of γ, inclusion is denoted by f
/-- Bourbaki GT III.6.5 Theorem I:
ℤ-bilinear continuous maps from dense images into a complete Hausdorff group extend by continuity.
Note: Bourbaki assumes that α and β are also complete Hausdorff, but this is not necessary. -/
theorem extend_Z_bilin {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {G : Type u_5}
[topological_space α] [add_comm_group α] [topological_add_group α] [topological_space β]
[add_comm_group β] [topological_add_group β] [topological_space γ] [add_comm_group γ]
[topological_add_group γ] [topological_space δ] [add_comm_group δ] [topological_add_group δ]
[uniform_space G] [add_comm_group G] [uniform_add_group G] [separated_space G]
[complete_space G] {e : β → α} [is_add_group_hom e] (de : dense_inducing e) {f : δ → γ}
[is_add_group_hom f] (df : dense_inducing f) {φ : β × δ → G} (hφ : continuous φ)
[bilin : add_comm_group.is_Z_bilin φ] : continuous (extend (dense_inducing.prod de df) φ) :=
sorry
end Mathlib |
9b198041bc208df32c2aaa1e0b4e6ef21fdacb56 | 0845ae2ca02071debcfd4ac24be871236c01784f | /library/init/lean/toexpr.lean | 10d385194a5f9eb9412f3f64fa9731a64f627b3b | [
"Apache-2.0"
] | permissive | GaloisInc/lean4 | 74c267eb0e900bfaa23df8de86039483ecbd60b7 | 228ddd5fdcd98dd4e9c009f425284e86917938aa | refs/heads/master | 1,643,131,356,301 | 1,562,715,572,000 | 1,562,715,572,000 | 192,390,898 | 0 | 0 | null | 1,560,792,750,000 | 1,560,792,749,000 | null | UTF-8 | Lean | false | false | 836 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.lean.expr
universe u
namespace Lean
class ToExpr (α : Type u) :=
(toExpr : α → Expr)
export ToExpr (toExpr)
instance exprToExpr : ToExpr Expr := ⟨id⟩
instance natToExpr : ToExpr Nat := ⟨fun n => Expr.lit (Literal.natVal n)⟩
instance strToExpr : ToExpr String := ⟨fun s => Expr.lit (Literal.strVal s)⟩
def nameToExprAux : Name → Expr
| Name.anonymous := mkConst `Lean.Name.anonymous
| (Name.mkString p s) := mkBinCApp `Lean.Name.mkString (nameToExprAux p) (toExpr s)
| (Name.mkNumeral p n) := mkBinCApp `Lean.Name.mkNumeral (nameToExprAux p) (toExpr n)
instance nameToExpr : ToExpr Name := ⟨nameToExprAux⟩
end Lean
|
c223edcab7608ac2233435428b33ede9203436f3 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/category_theory/structured_arrow.lean | 4ee2f8d7dba5eecc559df21fa7ea48f4957857c0 | [
"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 | 13,254 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz, Scott Morrison
-/
import category_theory.comma
import category_theory.punit
import category_theory.limits.shapes.terminal
/-!
# The category of "structured arrows"
For `T : C ⥤ D`, a `T`-structured arrow with source `S : D`
is just a morphism `S ⟶ T.obj Y`, for some `Y : D`.
These form a category with morphisms `g : Y ⟶ Y'` making the obvious diagram commute.
We prove that `𝟙 (T.obj Y)` is the initial object in `T`-structured objects with source `T.obj Y`.
-/
namespace category_theory
-- morphism levels before object levels. See note [category_theory universes].
universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D]
/--
The category of `T`-structured arrows with domain `S : D` (here `T : C ⥤ D`),
has as its objects `D`-morphisms of the form `S ⟶ T Y`, for some `Y : C`,
and morphisms `C`-morphisms `Y ⟶ Y'` making the obvious triangle commute.
-/
@[derive category, nolint has_inhabited_instance]
def structured_arrow (S : D) (T : C ⥤ D) := comma (functor.from_punit S) T
namespace structured_arrow
/-- The obvious projection functor from structured arrows. -/
@[simps]
def proj (S : D) (T : C ⥤ D) : structured_arrow S T ⥤ C := comma.snd _ _
variables {S S' S'' : D} {Y Y' : C} {T : C ⥤ D}
/-- Construct a structured arrow from a morphism. -/
def mk (f : S ⟶ T.obj Y) : structured_arrow S T := ⟨⟨⟩, Y, f⟩
@[simp] lemma mk_left (f : S ⟶ T.obj Y) : (mk f).left = punit.star := rfl
@[simp] lemma mk_right (f : S ⟶ T.obj Y) : (mk f).right = Y := rfl
@[simp] lemma mk_hom_eq_self (f : S ⟶ T.obj Y) : (mk f).hom = f := rfl
@[simp, reassoc] lemma w {A B : structured_arrow S T} (f : A ⟶ B) : A.hom ≫ T.map f.right = B.hom :=
by { have := f.w; tidy }
lemma eq_mk (f : structured_arrow S T) : f = mk f.hom :=
by { cases f, congr, ext, }
/--
To construct a morphism of structured arrows,
we need a morphism of the objects underlying the target,
and to check that the triangle commutes.
-/
@[simps]
def hom_mk {f f' : structured_arrow S T} (g : f.right ⟶ f'.right) (w : f.hom ≫ T.map g = f'.hom) :
f ⟶ f' :=
{ left := eq_to_hom (by ext),
right := g,
w' := by { dsimp, simpa using w.symm, }, }
/--
Given a structured arrow `X ⟶ F(U)`, and an arrow `U ⟶ Y`, we can construct a morphism of
structured arrow given by `(X ⟶ F(U)) ⟶ (X ⟶ F(U) ⟶ F(Y))`.
-/
def hom_mk' {F : C ⥤ D} {X : D} {Y : C}
(U : structured_arrow X F) (f : U.right ⟶ Y) :
U ⟶ mk (U.hom ≫ F.map f) := { right := f }
/--
To construct an isomorphism of structured arrows,
we need an isomorphism of the objects underlying the target,
and to check that the triangle commutes.
-/
@[simps]
def iso_mk {f f' : structured_arrow S T} (g : f.right ≅ f'.right)
(w : f.hom ≫ T.map g.hom = f'.hom) : f ≅ f' :=
comma.iso_mk (eq_to_iso (by ext)) g (by simpa using w.symm)
/--
A morphism between source objects `S ⟶ S'`
contravariantly induces a functor between structured arrows,
`structured_arrow S' T ⥤ structured_arrow S T`.
Ideally this would be described as a 2-functor from `D`
(promoted to a 2-category with equations as 2-morphisms)
to `Cat`.
-/
@[simps]
def map (f : S ⟶ S') : structured_arrow S' T ⥤ structured_arrow S T :=
comma.map_left _ ((functor.const _).map f)
@[simp] lemma map_mk {f : S' ⟶ T.obj Y} (g : S ⟶ S') :
(map g).obj (mk f) = mk (g ≫ f) := rfl
@[simp] lemma map_id {f : structured_arrow S T} : (map (𝟙 S)).obj f = f :=
by { rw eq_mk f, simp, }
@[simp] lemma map_comp {f : S ⟶ S'} {f' : S' ⟶ S''} {h : structured_arrow S'' T} :
(map (f ≫ f')).obj h = (map f).obj ((map f').obj h) :=
by { rw eq_mk h, simp, }
instance proj_reflects_iso : reflects_isomorphisms (proj S T) :=
{ reflects := λ Y Z f t, by exactI
⟨⟨structured_arrow.hom_mk (inv ((proj S T).map f)) (by simp), by tidy⟩⟩ }
open category_theory.limits
/-- The identity structured arrow is initial. -/
def mk_id_initial [full T] [faithful T] : is_initial (mk (𝟙 (T.obj Y))) :=
{ desc := λ c, hom_mk (T.preimage c.X.hom) (by { dsimp, simp, }),
uniq' := λ c m _, begin
ext,
apply T.map_injective,
simpa only [hom_mk_right, T.image_preimage, ←w m] using (category.id_comp _).symm,
end }
variables {A : Type u₃} [category.{v₃} A] {B : Type u₄} [category.{v₄} B]
/-- The functor `(S, F ⋙ G) ⥤ (S, G)`. -/
@[simps]
def pre (S : D) (F : B ⥤ C) (G : C ⥤ D) : structured_arrow S (F ⋙ G) ⥤ structured_arrow S G :=
comma.pre_right _ F G
/-- The functor `(S, F) ⥤ (G(S), F ⋙ G)`. -/
@[simps] def post (S : C) (F : B ⥤ C) (G : C ⥤ D) :
structured_arrow S F ⥤ structured_arrow (G.obj S) (F ⋙ G) :=
{ obj := λ X, { right := X.right, hom := G.map X.hom },
map := λ X Y f, { right := f.right, w' :=
by { simp [functor.comp_map, ←G.map_comp, ← f.w] } } }
end structured_arrow
/--
The category of `S`-costructured arrows with target `T : D` (here `S : C ⥤ D`),
has as its objects `D`-morphisms of the form `S Y ⟶ T`, for some `Y : C`,
and morphisms `C`-morphisms `Y ⟶ Y'` making the obvious triangle commute.
-/
@[derive category, nolint has_inhabited_instance]
def costructured_arrow (S : C ⥤ D) (T : D) := comma S (functor.from_punit T)
namespace costructured_arrow
/-- The obvious projection functor from costructured arrows. -/
@[simps]
def proj (S : C ⥤ D) (T : D) : costructured_arrow S T ⥤ C := comma.fst _ _
variables {T T' T'' : D} {Y Y' : C} {S : C ⥤ D}
/-- Construct a costructured arrow from a morphism. -/
def mk (f : S.obj Y ⟶ T) : costructured_arrow S T := ⟨Y, ⟨⟩, f⟩
@[simp] lemma mk_left (f : S.obj Y ⟶ T) : (mk f).left = Y := rfl
@[simp] lemma mk_right (f : S.obj Y ⟶ T) : (mk f).right = punit.star := rfl
@[simp] lemma mk_hom_eq_self (f : S.obj Y ⟶ T) : (mk f).hom = f := rfl
@[simp, reassoc] lemma w {A B : costructured_arrow S T} (f : A ⟶ B) :
S.map f.left ≫ B.hom = A.hom :=
by tidy
lemma eq_mk (f : costructured_arrow S T) : f = mk f.hom :=
by { cases f, congr, ext, }
/--
To construct a morphism of costructured arrows,
we need a morphism of the objects underlying the source,
and to check that the triangle commutes.
-/
@[simps]
def hom_mk {f f' : costructured_arrow S T} (g : f.left ⟶ f'.left) (w : S.map g ≫ f'.hom = f.hom) :
f ⟶ f' :=
{ left := g,
right := eq_to_hom (by ext),
w' := by simpa using w, }
/--
To construct an isomorphism of costructured arrows,
we need an isomorphism of the objects underlying the source,
and to check that the triangle commutes.
-/
@[simps]
def iso_mk {f f' : costructured_arrow S T} (g : f.left ≅ f'.left)
(w : S.map g.hom ≫ f'.hom = f.hom) : f ≅ f' :=
comma.iso_mk g (eq_to_iso (by ext)) (by simpa using w)
/--
A morphism between target objects `T ⟶ T'`
covariantly induces a functor between costructured arrows,
`costructured_arrow S T ⥤ costructured_arrow S T'`.
Ideally this would be described as a 2-functor from `D`
(promoted to a 2-category with equations as 2-morphisms)
to `Cat`.
-/
@[simps]
def map (f : T ⟶ T') : costructured_arrow S T ⥤ costructured_arrow S T' :=
comma.map_right _ ((functor.const _).map f)
@[simp] lemma map_mk {f : S.obj Y ⟶ T} (g : T ⟶ T') :
(map g).obj (mk f) = mk (f ≫ g) := rfl
@[simp] lemma map_id {f : costructured_arrow S T} : (map (𝟙 T)).obj f = f :=
by { rw eq_mk f, simp, }
@[simp] lemma map_comp {f : T ⟶ T'} {f' : T' ⟶ T''} {h : costructured_arrow S T} :
(map (f ≫ f')).obj h = (map f').obj ((map f).obj h) :=
by { rw eq_mk h, simp, }
instance proj_reflects_iso : reflects_isomorphisms (proj S T) :=
{ reflects := λ Y Z f t, by exactI
⟨⟨costructured_arrow.hom_mk (inv ((proj S T).map f)) (by simp), by tidy⟩⟩ }
open category_theory.limits
/-- The identity costructured arrow is terminal. -/
def mk_id_terminal [full S] [faithful S] : is_terminal (mk (𝟙 (S.obj Y))) :=
{ lift := λ c, hom_mk (S.preimage c.X.hom) (by { dsimp, simp, }),
uniq' := begin
rintros c m -,
ext,
apply S.map_injective,
simpa only [hom_mk_left, S.image_preimage, ←w m] using (category.comp_id _).symm,
end }
variables {A : Type u₃} [category.{v₃} A] {B : Type u₄} [category.{v₄} B]
/-- The functor `(F ⋙ G, S) ⥤ (G, S)`. -/
@[simps]
def pre (F : B ⥤ C) (G : C ⥤ D) (S : D) : costructured_arrow (F ⋙ G) S ⥤ costructured_arrow G S :=
comma.pre_left F G _
/-- The functor `(F, S) ⥤ (F ⋙ G, G(S))`. -/
@[simps] def post (F : B ⥤ C) (G : C ⥤ D) (S : C) :
costructured_arrow F S ⥤ costructured_arrow (F ⋙ G) (G.obj S) :=
{ obj := λ X, { left := X.left, hom := G.map X.hom },
map := λ X Y f, { left := f.left, w' :=
by { simp [functor.comp_map, ←G.map_comp, ← f.w] } } }
end costructured_arrow
open opposite
namespace structured_arrow
/--
For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the
category of structured arrows `d ⟶ F.obj c` to the category of costructured arrows
`F.op.obj c ⟶ (op d)`.
-/
@[simps]
def to_costructured_arrow (F : C ⥤ D) (d : D) :
(structured_arrow d F)ᵒᵖ ⥤ costructured_arrow F.op (op d) :=
{ obj := λ X, @costructured_arrow.mk _ _ _ _ _ (op X.unop.right) F.op X.unop.hom.op,
map := λ X Y f, costructured_arrow.hom_mk (f.unop.right.op)
begin
dsimp,
rw [← op_comp, ← f.unop.w, functor.const.obj_map],
erw category.id_comp,
end }
/--
For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the
category of structured arrows `op d ⟶ F.op.obj c` to the category of costructured arrows
`F.obj c ⟶ d`.
-/
@[simps]
def to_costructured_arrow' (F : C ⥤ D) (d : D) :
(structured_arrow (op d) F.op)ᵒᵖ ⥤ costructured_arrow F d :=
{ obj := λ X, @costructured_arrow.mk _ _ _ _ _ (unop X.unop.right) F X.unop.hom.unop,
map := λ X Y f, costructured_arrow.hom_mk f.unop.right.unop
begin
dsimp,
rw [← quiver.hom.unop_op (F.map (quiver.hom.unop f.unop.right)), ← unop_comp, ← F.op_map,
← f.unop.w, functor.const.obj_map],
erw category.id_comp,
end }
end structured_arrow
namespace costructured_arrow
/--
For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the
category of costructured arrows `F.obj c ⟶ d` to the category of structured arrows
`op d ⟶ F.op.obj c`.
-/
@[simps]
def to_structured_arrow (F : C ⥤ D) (d : D) :
(costructured_arrow F d)ᵒᵖ ⥤ structured_arrow (op d) F.op :=
{ obj := λ X, @structured_arrow.mk _ _ _ _ _ (op X.unop.left) F.op X.unop.hom.op,
map := λ X Y f, structured_arrow.hom_mk f.unop.left.op
begin
dsimp,
rw [← op_comp, f.unop.w, functor.const.obj_map],
erw category.comp_id,
end }
/--
For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the
category of costructured arrows `F.op.obj c ⟶ op d` to the category of structured arrows
`d ⟶ F.obj c`.
-/
@[simps]
def to_structured_arrow' (F : C ⥤ D) (d : D) :
(costructured_arrow F.op (op d))ᵒᵖ ⥤ structured_arrow d F :=
{ obj := λ X, @structured_arrow.mk _ _ _ _ _ (unop X.unop.left) F X.unop.hom.unop,
map := λ X Y f, structured_arrow.hom_mk (f.unop.left.unop)
begin
dsimp,
rw [← quiver.hom.unop_op (F.map f.unop.left.unop), ← unop_comp, ← F.op_map,
f.unop.w, functor.const.obj_map],
erw category.comp_id,
end }
end costructured_arrow
/--
For a functor `F : C ⥤ D` and an object `d : D`, the category of structured arrows `d ⟶ F.obj c`
is contravariantly equivalent to the category of costructured arrows `F.op.obj c ⟶ op d`.
-/
def structured_arrow_op_equivalence (F : C ⥤ D) (d : D) :
(structured_arrow d F)ᵒᵖ ≌ costructured_arrow F.op (op d) :=
equivalence.mk (structured_arrow.to_costructured_arrow F d)
(costructured_arrow.to_structured_arrow' F d).right_op
(nat_iso.of_components (λ X, (@structured_arrow.iso_mk _ _ _ _ _ _
(structured_arrow.mk (unop X).hom) (unop X) (iso.refl _) (by tidy)).op)
(λ X Y f, quiver.hom.unop_inj $ begin ext, dsimp, simp end))
(nat_iso.of_components (λ X, @costructured_arrow.iso_mk _ _ _ _ _ _
(costructured_arrow.mk X.hom) X (iso.refl _) (by tidy))
(λ X Y f, begin ext, dsimp, simp end))
/--
For a functor `F : C ⥤ D` and an object `d : D`, the category of costructured arrows
`F.obj c ⟶ d` is contravariantly equivalent to the category of structured arrows
`op d ⟶ F.op.obj c`.
-/
def costructured_arrow_op_equivalence (F : C ⥤ D) (d : D) :
(costructured_arrow F d)ᵒᵖ ≌ structured_arrow (op d) F.op :=
equivalence.mk (costructured_arrow.to_structured_arrow F d)
(structured_arrow.to_costructured_arrow' F d).right_op
(nat_iso.of_components (λ X, (@costructured_arrow.iso_mk _ _ _ _ _ _
(costructured_arrow.mk (unop X).hom) (unop X) (iso.refl _) (by tidy)).op)
(λ X Y f, quiver.hom.unop_inj $ begin ext, dsimp, simp end))
(nat_iso.of_components (λ X, @structured_arrow.iso_mk _ _ _ _ _ _
(structured_arrow.mk X.hom) X (iso.refl _) (by tidy))
(λ X Y f, begin ext, dsimp, simp end))
end category_theory
|
3aa4cff90d2d285a8fc9544924d50f48c197235f | 9b9a16fa2cb737daee6b2785474678b6fa91d6d4 | /src/algebra/big_operators.lean | 3199fb7153c60d85dd41446128b26c4132cf03fb | [
"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 | 29,917 | 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
Some big operators for lists and finite sets.
-/
import data.list.basic data.list.perm data.finset
import algebra.group algebra.ordered_group algebra.group_power
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
theorem directed.finset_le {r : α → α → Prop} [is_trans α r]
{ι} (hι : nonempty ι) {f : ι → α} (D : directed r f) (s : finset ι) :
∃ z, ∀ i ∈ s, r (f i) (f z) :=
show ∃ z, ∀ i ∈ s.1, r (f i) (f z), from
multiset.induction_on s.1 (let ⟨z⟩ := hι in ⟨z, λ _, false.elim⟩) $
λ i s ⟨j, H⟩, let ⟨k, h₁, h₂⟩ := D i j in
⟨k, λ a h, or.cases_on (multiset.mem_cons.1 h)
(λ h, h.symm ▸ h₁)
(λ h, trans (H _ h) h₂)⟩
namespace finset
variables {s s₁ s₂ : finset α} {a : α} {f g : α → β}
/-- `prod s f` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/
@[to_additive finset.sum]
protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod
attribute [to_additive finset.sum.equations._eqn_1] finset.prod.equations._eqn_1
@[to_additive finset.sum_eq_fold]
theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = s.fold (*) 1 f := rfl
section comm_monoid
variables [comm_monoid β]
@[simp, to_additive finset.sum_empty]
lemma prod_empty {α : Type u} {f : α → β} : (∅:finset α).prod f = 1 := rfl
@[simp, to_additive finset.sum_insert]
lemma prod_insert [decidable_eq α] : a ∉ s → (insert a s).prod f = f a * s.prod f := fold_insert
@[simp, to_additive finset.sum_singleton]
lemma prod_singleton : (singleton a).prod f = f a :=
eq.trans fold_singleton $ mul_one _
@[simp] lemma prod_const_one : s.prod (λx, (1 : β)) = 1 :=
by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow]
@[simp] lemma sum_const_zero {β} {s : finset α} [add_comm_monoid β] : s.sum (λx, (0 : β)) = 0 :=
@prod_const_one _ (multiplicative β) _ _
attribute [to_additive finset.sum_const_zero] prod_const_one
@[simp, to_additive finset.sum_image]
lemma prod_image [decidable_eq α] {s : finset γ} {g : γ → α} :
(∀x∈s, ∀y∈s, g x = g y → x = y) → (s.image g).prod f = s.prod (λx, f (g x)) :=
fold_image
@[congr, to_additive finset.sum_congr]
lemma prod_congr (h : s₁ = s₂) : (∀x∈s₂, f x = g x) → s₁.prod f = s₂.prod g :=
by rw [h]; exact fold_congr
attribute [congr] finset.sum_congr
@[to_additive finset.sum_union_inter]
lemma prod_union_inter [decidable_eq α] : (s₁ ∪ s₂).prod f * (s₁ ∩ s₂).prod f = s₁.prod f * s₂.prod f :=
fold_union_inter
@[to_additive finset.sum_union]
lemma prod_union [decidable_eq α] (h : s₁ ∩ s₂ = ∅) : (s₁ ∪ s₂).prod f = s₁.prod f * s₂.prod f :=
by rw [←prod_union_inter, h]; exact (mul_one _).symm
@[to_additive finset.sum_sdiff]
lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) : (s₂ \ s₁).prod f * s₁.prod f = s₂.prod f :=
by rw [←prod_union (sdiff_inter_self _ _), sdiff_union_of_subset h]
@[to_additive finset.sum_bind]
lemma prod_bind [decidable_eq α] {s : finset γ} {t : γ → finset α} :
(∀x∈s, ∀y∈s, x ≠ y → t x ∩ t y = ∅) → (s.bind t).prod f = s.prod (λx, (t x).prod f) :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s (λ _, by simp only [bind_empty, prod_empty])
(assume x s hxs ih hd,
have hd' : ∀x∈s, ∀y∈s, x ≠ y → t x ∩ t y = ∅,
from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy),
have t x ∩ finset.bind s t = ∅,
from eq_empty_of_forall_not_mem $ assume a,
by rw [mem_inter, mem_bind];
rintro ⟨h₁, y, hys, hy₂⟩;
exact eq_empty_iff_forall_not_mem.1
(hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hys) (assume h, hxs (h.symm ▸ hys)))
_ (mem_inter_of_mem h₁ hy₂),
by simp only [bind_insert, prod_insert hxs, prod_union this, ih hd'])
@[to_additive finset.sum_product]
lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} :
(s.product t).prod f = s.prod (λx, t.prod $ λy, f (x, y)) :=
begin
haveI := classical.dec_eq α, haveI := classical.dec_eq γ,
rw [product_eq_bind, prod_bind (λ x hx y hy h, eq_empty_of_forall_not_mem _)],
{ congr, funext, exact prod_image (λ _ _ _ _ H, (prod.mk.inj H).2) },
simp only [mem_inter, mem_image], rintro ⟨_, _⟩ ⟨⟨_, _, _⟩, ⟨_, _, _⟩⟩, apply h, cc
end
@[to_additive finset.sum_sigma]
lemma prod_sigma {σ : α → Type*}
{s : finset α} {t : Πa, finset (σ a)} {f : sigma σ → β} :
(s.sigma t).prod f = s.prod (λa, (t a).prod $ λs, f ⟨a, s⟩) :=
by haveI := classical.dec_eq α; haveI := (λ a, classical.dec_eq (σ a)); exact
calc (s.sigma t).prod f =
(s.bind (λa, (t a).image (λs, sigma.mk a s))).prod f : by rw sigma_eq_bind
... = s.prod (λa, ((t a).image (λs, sigma.mk a s)).prod f) :
prod_bind $ assume a₁ ha a₂ ha₂ h, eq_empty_of_forall_not_mem $
by simp only [mem_inter, mem_image];
rintro ⟨_, _⟩ ⟨⟨_, _, _⟩, ⟨_, _, _⟩⟩; apply h; cc
... = (s.prod $ λa, (t a).prod $ λs, f ⟨a, s⟩) :
prod_congr rfl $ λ _ _, prod_image $ λ _ _ _ _ _, by cc
@[to_additive finset.sum_image']
lemma prod_image' [decidable_eq α] {s : finset γ} {g : γ → α} (h : γ → β)
(eq : ∀c∈s, f (g c) = (s.filter (λc', g c' = g c)).prod h) :
(s.image g).prod f = s.prod h :=
begin
letI := classical.dec_eq γ,
rw [← image_bind_filter_eq s g] {occs := occurrences.pos [2]},
rw [finset.prod_bind],
{ refine finset.prod_congr rfl (assume a ha, _),
rcases finset.mem_image.1 ha with ⟨b, hb, rfl⟩,
exact eq b hb },
assume a₀ _ a₁ _ ne,
refine disjoint_iff_inter_eq_empty.1 (disjoint_iff_ne.2 _),
assume c₀ h₀ c₁ h₁,
rcases mem_filter.1 h₀ with ⟨h₀, rfl⟩,
rcases mem_filter.1 h₁ with ⟨h₁, rfl⟩,
exact mt (congr_arg g) ne
end
@[to_additive finset.sum_add_distrib]
lemma prod_mul_distrib : s.prod (λx, f x * g x) = s.prod f * s.prod g :=
eq.trans (by rw one_mul; refl) fold_op_distrib
@[to_additive finset.sum_comm]
lemma prod_comm [decidable_eq γ] {s : finset γ} {t : finset α} {f : γ → α → β} :
s.prod (λx, t.prod $ f x) = t.prod (λy, s.prod $ λx, f x y) :=
finset.induction_on s (by simp only [prod_empty, prod_const_one]) $
λ _ _ H ih, by simp only [prod_insert H, prod_mul_distrib, ih]
lemma prod_hom [comm_monoid γ] (g : β → γ) [is_monoid_hom g] : s.prod (λx, g (f x)) = g (s.prod f) :=
eq.trans (by rw is_monoid_hom.map_one g; refl) (fold_hom (λ _ _, is_monoid_hom.map_mul g))
@[to_additive finset.sum_hom_rel]
lemma prod_hom_rel [comm_monoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : finset α}
(h₁ : r 1 1) (h₂ : ∀a b c, r b c → r (f a * b) (g a * c)) : r (s.prod f) (s.prod g) :=
begin
letI := classical.dec_eq α,
refine finset.induction_on s h₁ (assume a s has ih, _),
rw [prod_insert has, prod_insert has],
exact h₂ a (s.prod f) (s.prod g) ih,
end
@[to_additive finset.sum_subset]
lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → f x = 1) : s₁.prod f = s₂.prod f :=
by haveI := classical.dec_eq α; exact
have (s₂ \ s₁).prod f = (s₂ \ s₁).prod (λx, 1),
from prod_congr rfl $ by simpa only [mem_sdiff, and_imp],
by rw [←prod_sdiff h]; simp only [this, prod_const_one, one_mul]
@[to_additive sum_filter]
lemma prod_filter (p : α → Prop) [decidable_pred p] (f : α → β) :
(s.filter p).prod f = s.prod (λa, if p a then f a else 1) :=
calc (s.filter p).prod f = (s.filter p).prod (λa, if p a then f a else 1) :
prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2])
... = s.prod (λa, if p a then f a else 1) :
begin
refine prod_subset (filter_subset s) (assume x hs h, _),
rw [mem_filter, not_and] at h,
exact if_neg (h hs)
end
@[to_additive finset.sum_eq_single]
lemma prod_eq_single {s : finset α} {f : α → β} (a : α)
(h₀ : ∀b∈s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : s.prod f = f a :=
by haveI := classical.dec_eq α;
from classical.by_cases
(assume : a ∈ s,
calc s.prod f = ({a} : finset α).prod f :
begin
refine (prod_subset _ _).symm,
{ intros _ H, rwa mem_singleton.1 H },
{ simpa only [mem_singleton] }
end
... = f a : prod_singleton)
(assume : a ∉ s,
(prod_congr rfl $ λ b hb, h₀ b hb $ by rintro rfl; cc).trans $
prod_const_one.trans (h₁ this).symm)
@[to_additive finset.sum_attach]
lemma prod_attach {f : α → β} : s.attach.prod (λx, f x.val) = s.prod f :=
by haveI := classical.dec_eq α; exact
calc s.attach.prod (λx, f x.val) = ((s.attach).image subtype.val).prod f :
by rw [prod_image]; exact assume x _ y _, subtype.eq
... = _ : by rw [attach_image_val]
@[to_additive finset.sum_bij]
lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β}
(i : Πa∈s, γ) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha))
(i_inj : ∀a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀b∈t, ∃a ha, b = i a ha) :
s.prod f = t.prod g :=
by haveI := classical.prop_decidable; exact
calc s.prod f = s.attach.prod (λx, f x.val) : prod_attach.symm
... = s.attach.prod (λx, g (i x.1 x.2)) : prod_congr rfl $ assume x hx, h _ _
... = (s.attach.image $ λx:{x // x ∈ s}, i x.1 x.2).prod g :
(prod_image $ assume (a₁:{x // x ∈ s}) _ a₂ _ eq, subtype.eq $ i_inj a₁.1 a₂.1 a₁.2 a₂.2 eq).symm
... = t.prod g :
prod_subset
(by simp only [subset_iff, mem_image, mem_attach]; rintro _ ⟨⟨_, _⟩, _, rfl⟩; solve_by_elim)
(assume b hb hb1, false.elim $ hb1 $ by rcases i_surj b hb with ⟨a, ha, rfl⟩;
exact mem_image.2 ⟨⟨_, _⟩, mem_attach _ _, rfl⟩)
@[to_additive finset.sum_bij_ne_zero]
lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β}
(i : Πa∈s, f a ≠ 1 → γ) (hi₁ : ∀a h₁ h₂, i a h₁ h₂ ∈ t)
(hi₂ : ∀a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂)
(hi₃ : ∀b∈t, g b ≠ 1 → ∃a h₁ h₂, b = i a h₁ h₂)
(h : ∀a h₁ h₂, f a = g (i a h₁ h₂)) :
s.prod f = t.prod g :=
by haveI := classical.prop_decidable; exact
calc s.prod f = (s.filter $ λx, f x ≠ 1).prod f :
(prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro).symm
... = (t.filter $ λx, g x ≠ 1).prod g :
prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2)
(assume a ha, (mem_filter.mp ha).elim $ λh₁ h₂, mem_filter.mpr
⟨hi₁ a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩)
(assume a ha, (mem_filter.mp ha).elim $ h a)
(assume a₁ a₂ ha₁ ha₂,
(mem_filter.mp ha₁).elim $ λha₁₁ ha₁₂, (mem_filter.mp ha₂).elim $ λha₂₁ ha₂₂, hi₂ a₁ a₂ _ _ _ _)
(assume b hb, (mem_filter.mp hb).elim $ λh₁ h₂,
let ⟨a, ha₁, ha₂, eq⟩ := hi₃ b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩)
... = t.prod g :
(prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro)
@[to_additive finset.exists_ne_zero_of_sum_ne_zero]
lemma exists_ne_one_of_prod_ne_one : s.prod f ≠ 1 → ∃a∈s, f a ≠ 1 :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (λ H, (H rfl).elim) (assume a s has ih h,
classical.by_cases
(assume ha : f a = 1,
let ⟨a, ha, hfa⟩ := ih (by rwa [prod_insert has, ha, one_mul] at h) in
⟨a, mem_insert_of_mem ha, hfa⟩)
(assume hna : f a ≠ 1,
⟨a, mem_insert_self _ _, hna⟩))
@[to_additive finset.sum_range_succ]
lemma prod_range_succ (f : ℕ → β) (n : ℕ) :
(range (nat.succ n)).prod f = f n * (range n).prod f :=
by rw [range_succ, prod_insert not_mem_range_self]
lemma prod_range_succ' (f : ℕ → β) :
∀ n : ℕ, (range (nat.succ n)).prod f = (range n).prod (f ∘ nat.succ) * f 0
| 0 := (prod_range_succ _ _).trans $ mul_comm _ _
| (n + 1) := by rw [prod_range_succ (λ m, f (nat.succ m)), mul_assoc, ← prod_range_succ'];
exact prod_range_succ _ _
@[simp] lemma prod_const (b : β) : s.prod (λ a, b) = b ^ s.card :=
by haveI := classical.dec_eq α; exact
finset.induction_on s rfl (λ a s has ih,
by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih])
lemma prod_pow (s : finset α) (n : ℕ) (f : α → β) :
s.prod (λ x, f x ^ n) = s.prod f ^ n :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (by simp [_root_.mul_pow] {contextual := tt})
lemma prod_nat_pow (s : finset α) (n : ℕ) (f : α → ℕ) :
s.prod (λ x, f x ^ n) = s.prod f ^ n :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (by simp [nat.mul_pow] {contextual := tt})
@[to_additive finset.sum_involution]
lemma prod_involution {s : finset α} {f : α → β} :
∀ (g : Π a ∈ s, α)
(h₁ : ∀ a ha, f a * f (g a ha) = 1)
(h₂ : ∀ a ha, f a ≠ 1 → g a ha ≠ a)
(h₃ : ∀ a ha, g a ha ∈ s)
(h₄ : ∀ a ha, g (g a ha) (h₃ a ha) = a),
s.prod f = 1 :=
by haveI := classical.dec_eq α;
haveI := classical.dec_eq β; exact
finset.strong_induction_on s
(λ s ih g h₁ h₂ h₃ h₄,
if hs : s = ∅ then hs.symm ▸ rfl
else let ⟨x, hx⟩ := exists_mem_of_ne_empty hs in
have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s,
from λ y hy, (mem_of_mem_erase (mem_of_mem_erase hy)),
have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y,
from λ x hx y hy h, by rw [← h₄ x hx, ← h₄ y hy]; simp [h],
have ih': (erase (erase s x) (g x hx)).prod f = (1 : β) :=
ih ((s.erase x).erase (g x hx))
⟨subset.trans (erase_subset _ _) (erase_subset _ _),
λ h, not_mem_erase (g x hx) (s.erase x) (h (h₃ x hx))⟩
(λ y hy, g y (hmem y hy))
(λ y hy, h₁ y (hmem y hy))
(λ y hy, h₂ y (hmem y hy))
(λ y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy,
mem_erase.2 ⟨λ (h : g y _ = x),
have y = g x hx, from h₄ y (hmem y hy) ▸ by simp [h],
by simpa [this] using hy, h₃ y (hmem y hy)⟩⟩)
(λ y hy, h₄ y (hmem y hy)),
if hx1 : f x = 1
then ih' ▸ eq.symm (prod_subset hmem
(λ y hy hy₁,
have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto,
this.elim (λ h, h.symm ▸ hx1)
(λ h, h₁ x hx ▸ h ▸ hx1.symm ▸ (one_mul _).symm)))
else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _),
← insert_erase (mem_erase.2 ⟨h₂ x hx hx1, h₃ x hx⟩),
prod_insert (not_mem_erase _ _), ih', mul_one, h₁ x hx])
@[to_additive finset.sum_eq_zero]
lemma prod_eq_one [comm_monoid β] {f : α → β} {s : finset α} (h : ∀x∈s, f x = 1) : s.prod f = 1 :=
calc s.prod f = s.prod (λx, 1) : finset.prod_congr rfl h
... = 1 : finset.prod_const_one
end comm_monoid
lemma sum_hom [add_comm_monoid β] [add_comm_monoid γ] (g : β → γ) [is_add_monoid_hom g] :
s.sum (λx, g (f x)) = g (s.sum f) :=
eq.trans (by rw is_add_monoid_hom.map_zero g; refl) (fold_hom (λ _ _, is_add_monoid_hom.map_add g))
attribute [to_additive finset.sum_hom] prod_hom
lemma sum_smul [add_comm_monoid β] (s : finset α) (n : ℕ) (f : α → β) :
s.sum (λ x, add_monoid.smul n (f x)) = add_monoid.smul n (s.sum f) :=
@prod_pow _ (multiplicative β) _ _ _ _
attribute [to_additive finset.sum_smul] prod_pow
@[simp] lemma sum_const [add_comm_monoid β] (b : β) :
s.sum (λ a, b) = add_monoid.smul s.card b :=
@prod_const _ (multiplicative β) _ _ _
attribute [to_additive finset.sum_const] prod_const
lemma sum_range_succ' [add_comm_monoid β] (f : ℕ → β) :
∀ n : ℕ, (range (nat.succ n)).sum f = (range n).sum (f ∘ nat.succ) + f 0 :=
@prod_range_succ' (multiplicative β) _ _
attribute [to_additive finset.sum_range_succ'] prod_range_succ'
lemma sum_nat_cast [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) :
↑(s.sum f) = s.sum (λa, f a : α → β) :=
(sum_hom _).symm
section comm_group
variables [comm_group β]
@[simp, to_additive finset.sum_neg_distrib]
lemma prod_inv_distrib : s.prod (λx, (f x)⁻¹) = (s.prod f)⁻¹ :=
prod_hom has_inv.inv
end comm_group
@[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) :
card (s.sigma t) = s.sum (λ a, card (t a)) :=
multiset.card_sigma _ _
lemma card_bind [decidable_eq β] {s : finset α} {t : α → finset β}
(h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → t x ∩ t y = ∅) :
(s.bind t).card = s.sum (λ u, card (t u)) :=
calc (s.bind t).card = (s.bind t).sum (λ _, 1) : by simp
... = s.sum (λ a, (t a).sum (λ _, 1)) : finset.sum_bind h
... = s.sum (λ u, card (t u)) : by simp
lemma card_bind_le [decidable_eq β] {s : finset α} {t : α → finset β} :
(s.bind t).card ≤ s.sum (λ a, (t a).card) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp)
(λ a s has ih,
calc ((insert a s).bind t).card ≤ (t a).card + (s.bind t).card :
by rw bind_insert; exact finset.card_union_le _ _
... ≤ (insert a s).sum (λ a, card (t a)) :
by rw sum_insert has; exact add_le_add_left ih _)
lemma gsmul_sum [add_comm_group β] {f : α → β} {s : finset α} (z : ℤ) :
gsmul z (s.sum f) = s.sum (λa, gsmul z (f a)) :=
(finset.sum_hom (gsmul z)).symm
end finset
namespace finset
variables {s s₁ s₂ : finset α} {f g : α → β} {b : β} {a : α}
@[simp] lemma sum_sub_distrib [add_comm_group β] : s.sum (λx, f x - g x) = s.sum f - s.sum g :=
sum_add_distrib.trans $ congr_arg _ sum_neg_distrib
section ordered_cancel_comm_monoid
variables [decidable_eq α] [ordered_cancel_comm_monoid β]
lemma sum_le_sum : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g :=
finset.induction_on s (λ _, le_refl _) $ assume a s ha ih h,
have f a + s.sum f ≤ g a + s.sum g,
from add_le_add (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx),
by simpa only [sum_insert ha]
lemma zero_le_sum (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by rw [sum_const_zero]) (sum_le_sum h)
lemma sum_le_zero (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum h) (by rw [sum_const_zero])
end ordered_cancel_comm_monoid
section semiring
variables [semiring β]
lemma sum_mul : s.sum f * b = s.sum (λx, f x * b) :=
(sum_hom (λx, x * b)).symm
lemma mul_sum : b * s.sum f = s.sum (λx, b * f x) :=
(sum_hom (λx, b * x)).symm
end semiring
section comm_semiring
variables [decidable_eq α] [comm_semiring β]
lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : s.prod f = 0 :=
calc s.prod f = (insert a (erase s a)).prod f : by rw insert_erase ha
... = 0 : by rw [prod_insert (not_mem_erase _ _), h, zero_mul]
lemma prod_sum {δ : α → Type*} [∀a, decidable_eq (δ a)]
{s : finset α} {t : Πa, finset (δ a)} {f : Πa, δ a → β} :
s.prod (λa, (t a).sum (λb, f a b)) =
(s.pi t).sum (λp, s.attach.prod (λx, f x.1 (p x.1 x.2))) :=
begin
induction s using finset.induction with a s ha ih,
{ rw [pi_empty, sum_singleton], refl },
{ have h₁ : ∀x ∈ t a, ∀y ∈ t a, ∀h : x ≠ y,
image (pi.cons s a x) (pi s t) ∩ image (pi.cons s a y) (pi s t) = ∅,
{ assume x hx y hy h,
apply eq_empty_of_forall_not_mem,
simp only [mem_inter, mem_image],
rintro p₁ ⟨⟨p₂, hp, eq⟩, ⟨p₃, hp₃, rfl⟩⟩,
have : pi.cons s a x p₂ a (mem_insert_self _ _) = pi.cons s a y p₃ a (mem_insert_self _ _),
{ rw [eq] },
rw [pi.cons_same, pi.cons_same] at this,
exact h this },
rw [prod_insert ha, pi_insert ha, ih, sum_mul, sum_bind h₁],
refine sum_congr rfl (λ b _, _),
have h₂ : ∀p₁∈pi s t, ∀p₂∈pi s t, pi.cons s a b p₁ = pi.cons s a b p₂ → p₁ = p₂, from
assume p₁ h₁ p₂ h₂ eq, injective_pi_cons ha eq,
rw [sum_image h₂, mul_sum],
refine sum_congr rfl (λ g _, _),
rw [attach_insert, prod_insert, prod_image],
{ simp only [pi.cons_same],
congr', ext ⟨v, hv⟩, congr',
exact (pi.cons_ne (by rintro rfl; exact ha hv)).symm },
{ exact λ _ _ _ _, subtype.eq ∘ subtype.mk.inj },
{ simp only [mem_image], rintro ⟨⟨_, hm⟩, _, rfl⟩, exact ha hm } }
end
end comm_semiring
section integral_domain /- add integral_semi_domain to support nat and ennreal -/
variables [decidable_eq α] [integral_domain β]
lemma prod_eq_zero_iff : s.prod f = 0 ↔ (∃a∈s, f a = 0) :=
finset.induction_on s ⟨not.elim one_ne_zero, λ ⟨_, H, _⟩, H.elim⟩ $ λ a s ha ih,
by rw [prod_insert ha, mul_eq_zero_iff_eq_zero_or_eq_zero,
bex_def, exists_mem_insert, ih, ← bex_def]
end integral_domain
section ordered_comm_monoid
variables [decidable_eq α] [ordered_comm_monoid β]
lemma sum_le_sum' : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g :=
finset.induction_on s (λ _, le_refl _) $ assume a s ha ih h,
have f a + s.sum f ≤ g a + s.sum g,
from add_le_add' (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx),
by simpa only [sum_insert ha]
lemma zero_le_sum' (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by rw [sum_const_zero]) (sum_le_sum' h)
lemma sum_le_zero' (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum' h) (by rw [sum_const_zero])
lemma sum_le_sum_of_subset_of_nonneg
(h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → 0 ≤ f x) : s₁.sum f ≤ s₂.sum f :=
calc s₁.sum f ≤ (s₂ \ s₁).sum f + s₁.sum f :
le_add_of_nonneg_left' $ zero_le_sum' $ by simpa only [mem_sdiff, and_imp]
... = (s₂ \ s₁ ∪ s₁).sum f : (sum_union (sdiff_inter_self _ _)).symm
... = s₂.sum f : by rw [sdiff_union_of_subset h]
lemma sum_eq_zero_iff_of_nonneg : (∀x∈s, 0 ≤ f x) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) :=
finset.induction_on s (λ _, ⟨λ _ _, false.elim, λ _, rfl⟩) $ λ a s ha ih H,
have ∀ x ∈ s, 0 ≤ f x, from λ _, H _ ∘ mem_insert_of_mem,
by rw [sum_insert ha,
add_eq_zero_iff' (H _ $ mem_insert_self _ _) (zero_le_sum' this),
forall_mem_insert, ih this]
lemma single_le_sum (hf : ∀x∈s, 0 ≤ f x) {a} (h : a ∈ s) : f a ≤ s.sum f :=
have (singleton a).sum f ≤ s.sum f,
from sum_le_sum_of_subset_of_nonneg
(λ x e, (mem_singleton.1 e).symm ▸ h) (λ x h _, hf x h),
by rwa sum_singleton at this
end ordered_comm_monoid
section canonically_ordered_monoid
variables [decidable_eq α] [canonically_ordered_monoid β] [@decidable_rel β (≤)]
lemma sum_le_sum_of_subset (h : s₁ ⊆ s₂) : s₁.sum f ≤ s₂.sum f :=
sum_le_sum_of_subset_of_nonneg h $ assume x h₁ h₂, zero_le _
lemma sum_le_sum_of_ne_zero (h : ∀x∈s₁, f x ≠ 0 → x ∈ s₂) : s₁.sum f ≤ s₂.sum f :=
calc s₁.sum f = (s₁.filter (λx, f x = 0)).sum f + (s₁.filter (λx, f x ≠ 0)).sum f :
by rw [←sum_union, filter_union_filter_neg_eq]; apply filter_inter_filter_neg_eq
... ≤ s₂.sum f : add_le_of_nonpos_of_le'
(sum_le_zero' $ by simp only [mem_filter, and_imp]; exact λ _ _, le_of_eq)
(sum_le_sum_of_subset $ by simpa only [subset_iff, mem_filter, and_imp])
end canonically_ordered_monoid
section discrete_linear_ordered_field
variables [discrete_linear_ordered_field α] [decidable_eq β]
lemma abs_sum_le_sum_abs {f : β → α} {s : finset β} : abs (s.sum f) ≤ s.sum (λa, abs (f a)) :=
finset.induction_on s (le_of_eq abs_zero) $
assume a s has ih,
calc abs (sum (insert a s) f) ≤ abs (f a) + abs (sum s f) :
by rw sum_insert has; exact abs_add_le_abs_add_abs _ _
... ≤ abs (f a) + s.sum (λa, abs (f a)) : add_le_add (le_refl _) ih
... ≤ sum (insert a s) (λ (a : β), abs (f a)) : by rw sum_insert has
end discrete_linear_ordered_field
@[simp] lemma card_pi [decidable_eq α] {δ : α → Type*}
(s : finset α) (t : Π a, finset (δ a)) :
(s.pi t).card = s.prod (λ a, card (t a)) :=
multiset.card_pi _ _
@[simp] lemma prod_range_id_eq_fact (n : ℕ) : ((range n.succ).erase 0).prod (λ x, x) = nat.fact n :=
calc ((range n.succ).erase 0).prod (λ x, x) = (range n).prod nat.succ :
eq.symm (prod_bij (λ x _, nat.succ x)
(λ a h₁, mem_erase.2 ⟨nat.succ_ne_zero _, mem_range.2 $ nat.succ_lt_succ $ by simpa using h₁⟩)
(by simp) (λ _ _ _ _, nat.succ_inj)
(λ b h,
have b.pred.succ = b, from nat.succ_pred_eq_of_pos $
by simp [nat.pos_iff_ne_zero] at *; tauto,
⟨nat.pred b, mem_range.2 $ nat.lt_of_succ_lt_succ (by simp [*] at *), this.symm⟩))
... = nat.fact n : by induction n; simp [*, range_succ]
end finset
section geom_sum
open finset
theorem geom_sum_mul_add [semiring α] (x : α) :
∀ (n : ℕ), ((range n).sum (λ i, (x+1)^i)) * x + 1 = (x+1)^n
| 0 := by simp
| (n+1) := calc (range (n + 1)).sum (λi, (x + 1) ^ i) * x + 1 =
(x + 1)^n * x + (((range n).sum (λ i, (x+1)^i)) * x + 1) :
by simp [range_add_one, add_mul]
... = (x + 1)^n * x + (x + 1)^n :
by rw geom_sum_mul_add n
... = (x + 1) ^ (n + 1) :
by simp [pow_add, mul_add]
theorem geom_sum_mul [ring α] (x : α) (n : ℕ) :
((range n).sum (λ i, x^i)) * (x-1) = x^n-1 :=
have _ := geom_sum_mul_add (x-1) n,
by rw [sub_add_cancel] at this; rw [← this, add_sub_cancel]
theorem geom_sum [division_ring α] {x : α} (h : x ≠ 1) (n : ℕ) :
(range n).sum (λ i, x^i) = (x^n-1)/(x-1) :=
have x - 1 ≠ 0, by simp [*, -sub_eq_add_neg, sub_eq_iff_eq_add] at *,
by rw [← geom_sum_mul, mul_div_cancel _ this]
lemma geom_sum_inv [division_ring α] {x : α} (hx1 : x ≠ 1) (hx0 : x ≠ 0) (n : ℕ) :
(range n).sum (λ m, x⁻¹ ^ m) = (x - 1)⁻¹ * (x - x⁻¹ ^ n * x) :=
have h₁ : x⁻¹ ≠ 1, by rwa [inv_eq_one_div, ne.def, div_eq_iff_mul_eq hx0, one_mul],
have h₂ : x⁻¹ - 1 ≠ 0, from mt sub_eq_zero.1 h₁,
have h₃ : x - 1 ≠ 0, from mt sub_eq_zero.1 hx1,
have h₄ : x * x⁻¹ ^ n = x⁻¹ ^ n * x,
from nat.cases_on n (by simp)
(λ _, by conv { to_rhs, rw [pow_succ', mul_assoc, inv_mul_cancel hx0, mul_one] };
rw [pow_succ, ← mul_assoc, mul_inv_cancel hx0, one_mul]),
by rw [geom_sum h₁, div_eq_iff_mul_eq h₂, ← domain.mul_left_inj h₃,
← mul_assoc, ← mul_assoc, mul_inv_cancel h₃];
simp [mul_add, add_mul, mul_inv_cancel hx0, mul_assoc, h₄]
end geom_sum
section group
open list
variables [group α] [group β]
@[to_additive is_add_group_hom.sum]
theorem is_group_hom.prod (f : α → β) [is_group_hom f] (l : list α) :
f (prod l) = prod (map f l) :=
by induction l; simp only [*, is_group_hom.mul f, is_group_hom.one f, prod_nil, prod_cons, map]
theorem is_group_anti_hom.prod (f : α → β) [is_group_anti_hom f] (l : list α) :
f (prod l) = prod (map f (reverse l)) :=
by induction l with hd tl ih; [exact is_group_anti_hom.one f,
simp only [prod_cons, is_group_anti_hom.mul f, ih, reverse_cons, map_append, prod_append, map_singleton, prod_cons, prod_nil, mul_one]]
theorem inv_prod : ∀ l : list α, (prod l)⁻¹ = prod (map (λ x, x⁻¹) (reverse l)) :=
λ l, @is_group_anti_hom.prod _ _ _ _ _ inv_is_group_anti_hom l -- TODO there is probably a cleaner proof of this
end group
section comm_group
variables [comm_group α] [comm_group β] (f : α → β) [is_group_hom f]
@[to_additive is_add_group_hom.multiset_sum]
lemma is_group_hom.multiset_prod (m : multiset α) : f m.prod = (m.map f).prod :=
quotient.induction_on m $ assume l, by simp [is_group_hom.prod f l]
@[to_additive is_add_group_hom.finset_sum]
lemma is_group_hom.finset_prod (g : γ → α) (s : finset γ) : f (s.prod g) = s.prod (f ∘ g) :=
show f (s.val.map g).prod = (s.val.map (f ∘ g)).prod, by rw [is_group_hom.multiset_prod f]; simp
end comm_group
@[to_additive is_add_group_hom_finset_sum]
lemma is_group_hom_finset_prod {α β γ} [group α] [comm_group β] (s : finset γ)
(f : γ → α → β) [∀c, is_group_hom (f c)] : is_group_hom (λa, s.prod (λc, f c a)) :=
⟨assume a b, by simp only [λc, is_group_hom.mul (f c), finset.prod_mul_distrib]⟩
attribute [instance] is_group_hom_finset_prod is_add_group_hom_finset_sum
namespace multiset
variables [decidable_eq α]
@[simp] lemma to_finset_sum_count_eq (s : multiset α) :
s.to_finset.sum (λa, s.count a) = s.card :=
multiset.induction_on s rfl
(assume a s ih,
calc (to_finset (a :: s)).sum (λx, count x (a :: s)) =
(to_finset (a :: s)).sum (λx, (if x = a then 1 else 0) + count x s) :
finset.sum_congr rfl $ λ _ _, by split_ifs;
[simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]]
... = card (a :: s) :
begin
by_cases a ∈ s.to_finset,
{ have : (to_finset s).sum (λx, ite (x = a) 1 0) = (finset.singleton a).sum (λx, ite (x = a) 1 0),
{ apply (finset.sum_subset _ _).symm,
{ intros _ H, rwa mem_singleton.1 H },
{ exact λ _ _ H, if_neg (mt finset.mem_singleton.2 H) } },
rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this, finset.sum_singleton, if_pos rfl, add_comm, card_cons] },
{ have ha : a ∉ s, by rwa mem_to_finset at h,
have : (to_finset s).sum (λx, ite (x = a) 1 0) = (to_finset s).sum (λx, 0), from
finset.sum_congr rfl (λ x hx, if_neg $ by rintro rfl; cc),
rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this, finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] }
end)
end multiset
|
9984903c76e01e3aa23b48c61014c7fa1a3a350a | 2c2aac4ae1d23cd0d1f9bf0f8c04b05427d38103 | /src/tactic/suggest.lean | a74e701842784f24dce0332edc6067ed59f4eb8b | [
"Apache-2.0"
] | permissive | egsgaard/mathlib | 3f733823fbc8d34f8e104bf6f5d2d55f150c774e | 8e8037f5ce6f1633dba7f8b284a92a6dc2941abc | refs/heads/master | 1,650,473,269,445 | 1,586,936,514,000 | 1,586,936,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,585 | 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
/-- compute the head symbol of an expression, but normalise `>` to `<` and `≥` to `≤` -/
-- We may want to tweak this further?
meta def head_symbol : expr → name
| (expr.pi _ _ _ t) := head_symbol t
| (expr.app f _) := head_symbol f
| (expr.const n _) :=
-- TODO this is a hack; if you suspect more cases here would help, please report them
match n with
| `gt := `has_lt.lt
| `ge := `has_le.le
| _ := n
end
| _ := `_
/--
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"
/--
When we are determining if a given declaration is potentially relevant for the current goal,
we compute `unfold_head_symbol` on the head symbol of the declaration, producing a list of names.
We consider the declaration potentially relevant if the head symbol of the goal appears in this
list.
-/
-- This is a hack.
meta def unfold_head_symbol : name → list name
| `false := [`not, `false]
| n := [n]
/-- Determine if, and in which way, a given expression matches the specified head symbol. -/
meta def match_head_symbol (hs : name) : expr → option head_symbol_match
| (expr.pi _ _ _ t) := match_head_symbol t
| `(%%a ↔ %%b) := if `iff = hs then some ex else
match (match_head_symbol a, match_head_symbol b) with
| (some ex, some ex) :=
some both
| (some ex, _) := some mpr
| (_, some ex) := some mp
| _ := none
end
| (expr.app f _) := match_head_symbol f
| (expr.const n _) := if list.mem hs (unfold_head_symbol n) then some ex else none
| _ := if hs = `_ then some ex else none
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) (d : declaration) : option decl_data :=
let n := d.to_name in
if ¬ d.is_trusted ∨ n.is_internal then
none
else
(λ m, ⟨d, n, m, n.length⟩) <$> match_head_symbol hs d.type
/-- Retrieve all library definitions with a given head symbol. -/
meta def library_defs (hs : name) : tactic (list decl_data) :=
do env ← get_env,
return $ env.decl_filter_map (process_declaration hs)
/--
Apply the lemma `e`, then attempt to close all goals using
`solve_by_elim { discharger := discharger }`, failing if `close_goals = tt`
and there are any goals remaining.
-/
-- Implementation note: as this is used by both `library_search` and `suggest`,
-- we first run `solve_by_elim` separately on a subset of the goals,
-- whether or not `close_goals` is set,
-- and then if `close_goals = tt`, require that `solve_by_elim { all_goals := tt }` succeeds
-- on the remaining goals.
meta def apply_and_solve (close_goals : bool) (discharger : tactic unit) (e : expr) : tactic unit :=
apply e >>
-- 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 { discharger := discharger })) >>
-- Phase 2
(done <|>
-- 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, discharger := discharger } <|>
-- and fail unless `close_goals = ff`
guard ¬ close_goals)
/--
Apply the declaration `d` (or the forward and backward implications separately, if it is an `iff`),
and then attempt to solve the goal using `apply_and_solve`.
-/
meta def apply_declaration (close_goals : bool) (discharger : tactic unit) (d : decl_data) :
tactic unit :=
let tac := apply_and_solve close_goals discharger 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 :=
(do l ← iff_mp_core e t, tac l) <|>
(do l ← iff_mpr_core e t, tac l)
end
/--
Replace any metavariables in the expression with underscores, in preparation for printing
`refine ...` statements.
-/
meta def replace_mvars (e : expr) : expr :=
e.replace (λ e' _, if e'.is_mvar then some (unchecked_cast pexpr.mk_placeholder) else none)
/--
Construct a `refine ...` or `exact ...` string which would construct `g`.
-/
meta def tactic_statement (g : expr) : tactic string :=
do g ← instantiate_mvars g,
g ← head_beta g,
r ← pp (replace_mvars g),
if g.has_meta_var
then return (sformat!"Try this: refine {r}")
else return (sformat!"Try this: exact {r}")
/--
Assuming that a goal `g` has been (partially) solved in the tactic_state `l`,
this function prepares a string of the form `exact ...` or `refine ...` (possibly with underscores)
that will reproduce that result.
-/
meta def message (g : expr) (l : tactic_state) : tactic string :=
do s ← read,
write l,
r ← tactic_statement g,
write s,
return r
/-- 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 suggest
declare_trace suggest -- Trace a list of all relevant lemmas
-- 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' (discharger : tactic unit := done) :
tactic (mllist tactic application) :=
do g :: _ ← get_goals,
hyps ← local_context,
-- Make sure that `solve_by_elim` doesn't just solve the goal immediately:
(lock_tactic_state (do
focus1 $ solve_by_elim { discharger := discharger },
s ← read,
m ← tactic_statement g,
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 ← library_defs (head_symbol t),
-- 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)),
-- Try applying each lemma against the goal,
-- then record the number of remaining goals, and number of local hypotheses used.
return $ (mllist.of_list defs).mfilter_map
-- (This tactic block is only executed when we evaluate the mllist,
-- so we need to do the `focus1` here.)
(λ d, lock_tactic_state $ focus1 $ do
apply_declaration ff discharger d,
ng ← num_goals,
g ← instantiate_mvars g,
state ← read,
m ← message g state,
return
{ application .
state := state,
decl := d.d,
script := m,
num_goals := ng,
hyps_used := hyps.countp(λ h, h.occurs g) }))
/--
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 `refine ...` or `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 (discharger : tactic unit := done) : mllist tactic application :=
(mllist.monad_lift (suggest_core' discharger)).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) (discharger : tactic unit := done) :
tactic (list application) :=
do let results := suggest_core discharger,
-- 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 `exact ...` or `refine ...`, which make
progress on the current goal using a declaration from the library.
-/
meta def suggest_scripts (limit : option ℕ := none) (discharger : tactic unit := done) :
tactic (list string) :=
do L ← suggest limit discharger,
return $ L.map application.script
/--
Returns a string of the form `exact ...`, which closes the current goal.
-/
meta def library_search (discharger : tactic unit := done) : tactic string :=
(suggest_core discharger).mfirst (λ a, do guard (a.num_goals = 0), write a.state, return a.script)
namespace interactive
open tactic
open interactive
open lean.parser
local postfix `?`:9001 := optional
declare_trace silence_suggest -- Turn off `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.
-/
meta def suggest (n : parse (with_desc "n" small_nat)?) : tactic unit :=
do L ← tactic.suggest_scripts (n.get_or_else 50),
when (¬ is_trace_enabled_for `silence_suggest)
(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"] }
declare_trace silence_library_search -- Turn off `exact ...` trace message for `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`.
-/
meta def library_search : tactic unit :=
tactic.library_search tactic.done >>=
if is_trace_enabled_for `silence_library_search then
(λ _, skip)
else
trace
/--
`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 'exact ' prefix on this string?
return [((script.mk_iterator.remove 6).to_string, "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
|
19d7cc10b057fdec2fba79411959911689bcc9f7 | 4fa161becb8ce7378a709f5992a594764699e268 | /docs/tutorial/category_theory/intro.lean | 397005211dd0988e0688968409868424da6248dd | [
"Apache-2.0"
] | permissive | laughinggas/mathlib | e4aa4565ae34e46e834434284cb26bd9d67bc373 | 86dcd5cda7a5017c8b3c8876c89a510a19d49aad | refs/heads/master | 1,669,496,232,688 | 1,592,831,995,000 | 1,592,831,995,000 | 274,155,979 | 0 | 0 | Apache-2.0 | 1,592,835,190,000 | 1,592,835,189,000 | null | UTF-8 | Lean | false | false | 11,629 | lean | import category_theory.functor_category -- this transitively imports
-- category_theory.category
-- category_theory.functor
-- category_theory.natural_transformation
/-!
# An introduction to category theory in Lean
This is an introduction to the basic usage of category theory (in the mathematical sense) in Lean.
We cover how the basic theory of categories, functors and natural transformations is set up in Lean.
Most of the below is not hard to read off from the files `category_theory/category.lean`,
`category_theory/functor.lean` and `category_theory/natural_transformation.lean`.
First a word of warning. In `mathlib`, in the `/src` directory, there is a subdirectory called
`category`. This is *not* where categories, in the sense of mathematics, are defined; it's for use
by computer scientists. The directory we will be concerned with here is the `category_theory`
subdirectory.
## Overview
A category is a collection of objects, and a collection of morphisms (also known as arrows) between
the objects. The objects and morphisms have some extra structure and satisfy some axioms -- see the
[definition on Wikipedia](https://en.wikipedia.org/wiki/Category_%28mathematics%29#Definition) for
details.
One important thing to note is that a morphism in an abstract category may not be an actual function
between two types. In particular, there is new notation `⟶` , typed as `\h` or `\hom` in VS Code,
for a morphism. Nevertheless, in most of the "concrete" categories like `Top` and `Ab`, it is still
possible to write `f x` when `x : X` and `f : X ⟶ Y` is a morphism, as there is an automatic
coercion from morphisms to functions. (If the coercion doesn't fire automatically, sometimes it is
necessary to write `(f : X → Y) x`.)
In some fonts the `⟶` morphism arrow can be virtually indistinguishable from the standard function
arrow `→` . You may want to install the [Deja Vu Sans Mono](https://dejavu-fonts.github.io/) and put
that at the beginning of the `Font Family` setting in VSCode, to get a nice readable font with
excellent unicode coverage.
Another point of confusion can be universe issues. Following Lean's conventions for universe
polymorphism, the objects of a category might live in one universe `u` and the morphisms in another
universe `v`. Note that in many categories showing up in "set-theoretic mathematics", the morphisms
between two objects often form a set, but the objects themselves may or may not form a set. In Lean
this corresponds to the two possibilities `u=v` and `u=v+1`, known as `small_category` and
`large_category` respectively. In order to avoid proving the same statements for both small and
large categories, we usually stick to the general polymorphic situation with `u` and `v` independent
universes, and we do this below.
## Getting started with categories
The structure of a category on a type `C` in Lean is done using typeclasses; terms of `C` then
correspond to objects in the category. The convention in the category theory library is to use
universes prefixed with `u` (e.g. `u`, `u₁`, `u₂`) for the objects, and universes prefixed with `v`
for morphisms. Thus we have `C : Type u`, and if `X : C` and `Y : C` then morphisms `X ⟶ Y : Type v`
(note the non-standard arrow).
We set this up as follows:
-/
open category_theory
section category
universes v u -- the order matters (see below)
variables (C : Type u) [𝒞 : category.{v} C]
include 𝒞
variables {W X Y Z : C}
variables (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z)
/-!
This says "let `C` be a category, let `W`, `X`, `Y`, `Z` be objects of `C`, and let `f : W ⟶ X`, `g
: X ⟶ Y` and `h : Y ⟶ Z` be morphisms in `C` (with the specified source and targets)".
Note two unusual things. Firstly, the typeclass `category C` is explicitly named as `𝒞` (in
contrast to group theory, where one would just write `[group G]` rather than `[h : group G]`).
Secondly, we have to explicitly tell Lean the universe where the morphisms live (by writing
`category.{v} C`), because Lean cannot guess from knowing `C` alone.
The order in which universes are introduced at the top of the file matters: we put the universes for
morphisms first (typically `v`, `v₁` and so on), and then universes for objects (typically `u`, `u₁`
and so on). This ensures that in any new definition we make the universe variables for morphisms
come first, so that they can be explicitly specified while still allowing the universe levels of the
objects to be inferred automatically.
The reason that the typeclass is given an explicit name `𝒞` (typeset `\McC`) is that one often has
to write `include 𝒞` in code to ensure that Lean includes the typeclass in theorems and
definitions. (Lean is not willing to guess the universe level of morphisms, so sometimes won't
automatically include the `[category.{v} C]` variable.) One can use `omit 𝒞` again (or appropriate
scoping constructs) to make sure it isn't included in declarations where it isn't needed.
## Basic notation
In categories one has morphisms between objects, such as the identity morphism from an object to
itself. One can compose morphisms, and there are standard facts about the composition of a morphism
with the identity morphism, and the fact that morphism composition is associative. In Lean all of
this looks like the following:
-/
-- The identity morphism from `X` to `X` (remember that this is the `\h` arrow):
example : X ⟶ X := 𝟙 X -- type `𝟙` as `\bb1`
-- Function composition `h ∘ g`, a morphism from `X` to `Z`:
example : X ⟶ Z := g ≫ h
/-
Note in particular the order! The "maps on the right" convention was chosen; `g ≫ h` means "`g` then
`h`". Type `≫` with `\gg` in VS Code. Here are the theorems which ensure that we have a category.
-/
open category_theory.category
example : 𝟙 X ≫ g = g := id_comp g
example : g ≫ 𝟙 Y = g := comp_id g
example : (f ≫ g) ≫ h = f ≫ (g ≫ h) := assoc f g h
example : (f ≫ g) ≫ h = f ≫ g ≫ h := assoc f g h -- note \gg is right associative
-- All four examples above can also be proved with `simp`.
-- Monomorphisms and epimorphisms are predicates on morphisms and are implemented as typeclasses.
variables (f' : W ⟶ X) (h' : Y ⟶ Z)
example [mono g] : f ≫ g = f' ≫ g → f = f' := mono.right_cancellation f f'
example [epi g] : g ≫ h = g ≫ h' → h = h' := epi.left_cancellation h h'
end category -- end of section
/-!
## Getting started with functors
A functor is a map between categories. It is implemented as a structure. The notation for a functor
from `C` to `D` is `C ⥤ D`. Type `\func` in VS Code for the symbol. Here we demonstrate how to
evaluate functors on objects and on morphisms, how to show functors preserve the identity morphism
and composition of morphisms, how to compose functors, and show the notation `𝟭` for the identity
functor.
-/
section functor
-- recall we put morphism universes (`vᵢ`) before object universes (`uᵢ`)
universes v₁ v₂ v₃ u₁ u₂ u₃
variables (C : Type u₁) [𝒞 : category.{v₁} C]
variables (D : Type u₂) [𝒟 : category.{v₂} D]
variables (E : Type u₃) [ℰ : category.{v₃} E]
include 𝒞 𝒟 ℰ
variables {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
-- functors
variables (F : C ⥤ D) (G : D ⥤ E)
example : D := F.obj X -- functor F on objects
example : F.obj Y ⟶ F.obj Z := F.map g -- functor F on morphisms
-- A functor sends identity objects to identity objects
example : F.map (𝟙 X) = 𝟙 (F.obj X) := F.map_id X
-- and preserves compositions
example : F.map (f ≫ g) = (F.map f) ≫ (F.map g) := F.map_comp f g
-- The identity functor is `𝟭`, currently apparently untypesettable in Lean!
example : C ⥤ C := 𝟭 C
-- The identity functor is (definitionally) the identity on objects and morphisms:
example : (𝟭 C).obj X = X := category_theory.functor.id_obj X
example : (𝟭 C).map f = f := category_theory.functor.id_map f
-- Composition of functors; note order:
example : C ⥤ E := F ⋙ G -- typeset with `\ggg`
-- Composition of the identity either way does nothing:
example : F ⋙ 𝟭 D = F := F.comp_id
example : 𝟭 C ⋙ F = F := F.id_comp
-- Composition of functors definitionally does the right thing on objects and morphisms:
example : (F ⋙ G).obj X = G.obj (F.obj X) := F.comp_obj G X -- or rfl
example : (F ⋙ G).map f = G.map (F.map f) := rfl -- or F.comp_map G X Y f
end functor -- end of section
/-!
One can also check that associativity of composition of functors is definitionally true,
although we've observed that relying on this can result in slow proofs. (One should
rather use the natural isomorphisms provided in `src/category_theory/whiskering.lean`.)
## Getting started with natural transformations
A natural transformation is a morphism between functors. If `F` and `G` are functors from `C` to `D`
then a natural transformation is a map `F X ⟶ G X` for each object `X : C` plus the theorem that if
`f : X ⟶ Y` is a morphism then the two routes from `F X` to `G Y` are the same. One might imagine
that this is now another layer of notation, but fortunately the `category_theory.functor_category`
import gives the type of functors from `C` to `D` a category structure, which means that we can just
use morphism notation for natural transformations.
-/
section nat_trans
universes v₁ v₂ u₁ u₂
variables {C : Type u₁} [𝒞 : category.{v₁} C] {D : Type u₂} [𝒟 : category.{v₂} D]
include 𝒞 𝒟
variables (X Y : C)
variable (f : X ⟶ Y)
variables (F G H : C ⥤ D)
variables (α : F ⟶ G) (β : G ⟶ H) -- natural transformations (note it's the usual `\hom` arrow here)
-- Composition of natural transformations is just composition of morphisms:
example : F ⟶ H := α ≫ β
-- Applying natural transformation to an object:
example (X : C) : F.obj X ⟶ G.obj X := α.app X
/- The diagram coming from g and α
F(f)
F X ---> F Y
| |
|α(X) |α(Y)
v v
G X ---> G Y
G(f)
commutes.
-/
example : F.map f ≫ α.app Y = (α.app X) ≫ G.map f := α.naturality f
end nat_trans -- section
/-!
## Debugging universe problems
Unfortunately, dealing with universe polymorphism is an intrinsic problem in the category theory
library.
A very common problem is Lean complaining that it can't find an instance of `category X`, when you
can see right there in the hypotheses a `category X`! What's going on? Nearly always this is because
the universe level of the morphisms has not been specified explicitly, so in fact Lean is looking
for a `category.{? u} X` instance, while it has available a `category.{v u} X` instance. (The object
universe level is unambiguous, because this can be inferred from `X`.) You can determine if this is
a problem by using `set_option pp.universes true`. The reason this causes a problem is that Lean 3
is not willing to specialise a universe metavariable in order to solve a typeclass search.
Typically, you solve this problem by working out how to tell Lean which universe you want the
morphisms to live in, usually by adding a `.{v}` to the end of some identifier. As an example, in
```
instance coe_to_Top : has_coe (PresheafedSpace.{v} C) Top :=
{ coe := λ X, X.to_Top }
```
(taken from `src/algebraic_geometry/presheafed_space.lean`), if you remove the `.{v}` you get a
typeclass resolution error.
-/
/-!
## What next?
There are several lean files in the [category theory docs directory of
mathlib](https://github.com/leanprover-community/mathlib/tree/master/docs/tutorial/category_theory)
which give further examples of using the category theory library in Lean.
-/
|
1cfbb7604d91c93fd171e97529fe723c6e6ec56f | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/sum_integral_comparisons.lean | 10380facc6359df0d091fbefd5f748a5057380ef | [
"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,151 | lean | /-
Copyright (c) 2022 Kevin H. Wilson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin H. Wilson
-/
import measure_theory.integral.interval_integral
import data.set.function
import analysis.special_functions.integrals
/-!
# Comparing sums and integrals
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
## Summary
It is often the case that error terms in analysis can be computed by comparing
an infinite sum to the improper integral of an antitone function. This file will eventually enable
that.
At the moment it contains four lemmas in this direction: `antitone_on.integral_le_sum`,
`antitone_on.sum_le_integral` and versions for monotone functions, which can all be paired
with a `filter.tendsto` to estimate some errors.
`TODO`: Add more lemmas to the API to directly address limiting issues
## Main Results
* `antitone_on.integral_le_sum`: The integral of an antitone function is at most the sum of its
values at integer steps aligning with the left-hand side of the interval
* `antitone_on.sum_le_integral`: The sum of an antitone function along integer steps aligning with
the right-hand side of the interval is at most the integral of the function along that interval
* `monotone_on.integral_le_sum`: The integral of a monotone function is at most the sum of its
values at integer steps aligning with the right-hand side of the interval
* `monotone_on.sum_le_integral`: The sum of a monotone function along integer steps aligning with
the left-hand side of the interval is at most the integral of the function along that interval
## Tags
analysis, comparison, asymptotics
-/
open set measure_theory.measure_space
open_locale big_operators
variables {x₀ : ℝ} {a b : ℕ} {f : ℝ → ℝ}
lemma antitone_on.integral_le_sum (hf : antitone_on f (Icc x₀ (x₀ + a))) :
∫ x in x₀..(x₀ + a), f x ≤ ∑ i in finset.range a, f (x₀ + i) :=
begin
have hint : ∀ (k : ℕ), k < a → interval_integrable f volume (x₀+k) (x₀ + (k + 1 : ℕ)),
{ assume k hk,
refine (hf.mono _).interval_integrable,
rw uIcc_of_le,
{ apply Icc_subset_Icc,
{ simp only [le_add_iff_nonneg_right, nat.cast_nonneg] },
{ simp only [add_le_add_iff_left, nat.cast_le, nat.succ_le_of_lt hk] } },
{ simp only [add_le_add_iff_left, nat.cast_le, nat.le_succ] } },
calc
∫ x in x₀..(x₀ + a), f x = ∑ i in finset.range a, ∫ x in (x₀+i)..(x₀+(i+1 : ℕ)), f x :
begin
convert (interval_integral.sum_integral_adjacent_intervals hint).symm,
simp only [nat.cast_zero, add_zero],
end
... ≤ ∑ i in finset.range a, ∫ x in (x₀+i)..(x₀+(i+1 : ℕ)), f (x₀ + i) :
begin
apply finset.sum_le_sum (λ i hi, _),
have ia : i < a := finset.mem_range.1 hi,
refine interval_integral.integral_mono_on (by simp) (hint _ ia) (by simp) (λ x hx, _),
apply hf _ _ hx.1,
{ simp only [ia.le, mem_Icc, le_add_iff_nonneg_right, nat.cast_nonneg, add_le_add_iff_left,
nat.cast_le, and_self] },
{ refine mem_Icc.2 ⟨le_trans (by simp) hx.1, le_trans hx.2 _⟩,
simp only [add_le_add_iff_left, nat.cast_le, nat.succ_le_of_lt ia] },
end
... = ∑ i in finset.range a, f (x₀ + i) : by simp
end
lemma antitone_on.integral_le_sum_Ico (hab : a ≤ b) (hf : antitone_on f (set.Icc a b)) :
∫ x in a..b, f x ≤ ∑ x in finset.Ico a b, f x :=
begin
rw [(nat.sub_add_cancel hab).symm, nat.cast_add],
conv { congr, congr, skip, skip, rw add_comm, skip, skip, congr, congr, rw ←zero_add a, },
rw [← finset.sum_Ico_add, nat.Ico_zero_eq_range],
conv { to_rhs, congr, skip, funext, rw nat.cast_add, },
apply antitone_on.integral_le_sum,
simp only [hf, hab, nat.cast_sub, add_sub_cancel'_right],
end
lemma antitone_on.sum_le_integral (hf : antitone_on f (Icc x₀ (x₀ + a))) :
∑ i in finset.range a, f (x₀ + (i + 1 : ℕ)) ≤ ∫ x in x₀..(x₀ + a), f x :=
begin
have hint : ∀ (k : ℕ), k < a → interval_integrable f volume (x₀+k) (x₀ + (k + 1 : ℕ)),
{ assume k hk,
refine (hf.mono _).interval_integrable,
rw uIcc_of_le,
{ apply Icc_subset_Icc,
{ simp only [le_add_iff_nonneg_right, nat.cast_nonneg] },
{ simp only [add_le_add_iff_left, nat.cast_le, nat.succ_le_of_lt hk] } },
{ simp only [add_le_add_iff_left, nat.cast_le, nat.le_succ] } },
calc ∑ i in finset.range a, f (x₀ + (i + 1 : ℕ))
= ∑ i in finset.range a, ∫ x in (x₀+i)..(x₀+(i+1:ℕ)), f (x₀ + (i + 1 : ℕ)) : by simp
... ≤ ∑ i in finset.range a, ∫ x in (x₀+i)..(x₀+(i+1:ℕ)), f x :
begin
apply finset.sum_le_sum (λ i hi, _),
have ia : i + 1 ≤ a := finset.mem_range.1 hi,
refine interval_integral.integral_mono_on (by simp) (by simp) (hint _ ia) (λ x hx, _),
apply hf _ _ hx.2,
{ refine mem_Icc.2 ⟨le_trans ((le_add_iff_nonneg_right _).2 (nat.cast_nonneg _)) hx.1,
le_trans hx.2 _⟩,
simp only [nat.cast_le, add_le_add_iff_left, ia] },
{ refine mem_Icc.2 ⟨(le_add_iff_nonneg_right _).2 (nat.cast_nonneg _), _⟩,
simp only [add_le_add_iff_left, nat.cast_le, ia] },
end
... = ∫ x in x₀..(x₀ + a), f x :
begin
convert interval_integral.sum_integral_adjacent_intervals hint,
simp only [nat.cast_zero, add_zero],
end
end
lemma antitone_on.sum_le_integral_Ico (hab : a ≤ b) (hf : antitone_on f (set.Icc a b)) :
∑ i in finset.Ico a b, f (i + 1 : ℕ) ≤ ∫ x in a..b, f x :=
begin
rw [(nat.sub_add_cancel hab).symm, nat.cast_add],
conv { congr, congr, congr, rw ← zero_add a, skip, skip, skip, rw add_comm, },
rw [← finset.sum_Ico_add, nat.Ico_zero_eq_range],
conv { to_lhs, congr, congr, skip, funext, rw [add_assoc, nat.cast_add], },
apply antitone_on.sum_le_integral,
simp only [hf, hab, nat.cast_sub, add_sub_cancel'_right],
end
lemma monotone_on.sum_le_integral (hf : monotone_on f (Icc x₀ (x₀ + a))) :
∑ i in finset.range a, f (x₀ + i) ≤ ∫ x in x₀..(x₀ + a), f x :=
begin
rw [← neg_le_neg_iff, ← finset.sum_neg_distrib, ← interval_integral.integral_neg],
exact hf.neg.integral_le_sum,
end
lemma monotone_on.sum_le_integral_Ico (hab : a ≤ b) (hf : monotone_on f (set.Icc a b)) :
∑ x in finset.Ico a b, f x ≤ ∫ x in a..b, f x :=
begin
rw [← neg_le_neg_iff, ← finset.sum_neg_distrib, ← interval_integral.integral_neg],
exact hf.neg.integral_le_sum_Ico hab,
end
lemma monotone_on.integral_le_sum (hf : monotone_on f (Icc x₀ (x₀ + a))) :
∫ x in x₀..(x₀ + a), f x ≤ ∑ i in finset.range a, f (x₀ + (i + 1 : ℕ)) :=
begin
rw [← neg_le_neg_iff, ← finset.sum_neg_distrib, ← interval_integral.integral_neg],
exact hf.neg.sum_le_integral,
end
lemma monotone_on.integral_le_sum_Ico (hab : a ≤ b) (hf : monotone_on f (set.Icc a b)) :
∫ x in a..b, f x ≤ ∑ i in finset.Ico a b, f (i + 1 : ℕ) :=
begin
rw [← neg_le_neg_iff, ← finset.sum_neg_distrib, ← interval_integral.integral_neg],
exact hf.neg.sum_le_integral_Ico hab,
end
|
de232680866fcc7f66a1f405a2a468ff0888d940 | ec5a7ae10c533e1b1f4b0bc7713e91ecf829a3eb | /ijcar16/examples/cc22.lean | 621dc8b1affd9aa7ea541710a146eefe0c8047af | [
"MIT"
] | permissive | leanprover/leanprover.github.io | cf248934af7c7e9aeff17cf8df3c12c5e7e73f1a | 071a20d2e059a2c3733e004c681d3949cac3c07a | refs/heads/master | 1,692,621,047,417 | 1,691,396,994,000 | 1,691,396,994,000 | 19,366,263 | 18 | 27 | MIT | 1,693,989,071,000 | 1,399,006,345,000 | Lean | UTF-8 | Lean | false | false | 743 | lean | /-
Example/test file for the congruence closure procedure described in the paper:
"Congruence Closure for Intensional Type Theory"
Daniel Selsam and Leonardo de Moura
The tactic `by blast` has been configured in this file to use just
the congruence closure procedure using the command
set_option blast.strategy "cc"
-/
set_option blast.strategy "cc"
example (C : nat → Type) (f : Π n, C n → C n) (n m : nat) (c : C n) (d : C m) :
f n == f m → c == d → n == m → f n c == f m d :=
by blast
example (f : nat → nat → nat) (a b c d : nat) :
c = d → f a = f b → f a c = f b d :=
by blast
example (f : nat → nat → nat) (a b c d : nat) :
c == d → f a == f b → f a c == f b d :=
by blast
|
d40a1ba2e855f514be1ceee81fc261384397a58e | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/number_theory/padics/padic_integers.lean | 0209c07555d39903321439dce2befe2e13b51e41 | [
"Apache-2.0"
] | permissive | dexmagic/mathlib | ff48eefc56e2412429b31d4fddd41a976eb287ce | 7a5d15a955a92a90e1d398b2281916b9c41270b2 | refs/heads/master | 1,693,481,322,046 | 1,633,360,193,000 | 1,633,360,193,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,102 | 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, Mario Carneiro, Johan Commelin
-/
import data.int.modeq
import data.zmod.basic
import number_theory.padics.padic_numbers
import ring_theory.discrete_valuation_ring
import topology.metric_space.cau_seq_filter
/-!
# p-adic integers
This file defines the p-adic integers `ℤ_p` as the subtype of `ℚ_p` with norm `≤ 1`.
We show that `ℤ_p`
* is complete
* is nonarchimedean
* is a normed ring
* is a local ring
* is a discrete valuation ring
The relation between `ℤ_[p]` and `zmod p` is established in another file.
## Important definitions
* `padic_int` : the type of p-adic numbers
## Notation
We introduce the notation `ℤ_[p]` for the p-adic integers.
## Implementation notes
Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically
by taking `[fact (nat.prime p)] as a type class argument.
Coercions into `ℤ_p` are set up to work with the `norm_cast` tactic.
## References
* [F. Q. Gouêva, *p-adic numbers*][gouvea1997]
* [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]
* <https://en.wikipedia.org/wiki/P-adic_number>
## Tags
p-adic, p adic, padic, p-adic integer
-/
open padic metric local_ring
noncomputable theory
open_locale classical
/-- The p-adic integers ℤ_p are the p-adic numbers with norm ≤ 1. -/
def padic_int (p : ℕ) [fact p.prime] := {x : ℚ_[p] // ∥x∥ ≤ 1}
notation `ℤ_[`p`]` := padic_int p
namespace padic_int
/-! ### Ring structure and coercion to `ℚ_[p]` -/
variables {p : ℕ} [fact p.prime]
instance : has_coe ℤ_[p] ℚ_[p] := ⟨subtype.val⟩
lemma ext {x y : ℤ_[p]} : (x : ℚ_[p]) = y → x = y := subtype.ext_iff_val.2
/-- Addition on ℤ_p is inherited from ℚ_p. -/
instance : has_add ℤ_[p] :=
⟨λ ⟨x, hx⟩ ⟨y, hy⟩, ⟨x+y,
le_trans (padic_norm_e.nonarchimedean _ _) (max_le_iff.2 ⟨hx,hy⟩)⟩⟩
/-- Multiplication on ℤ_p is inherited from ℚ_p. -/
instance : has_mul ℤ_[p] :=
⟨λ ⟨x, hx⟩ ⟨y, hy⟩, ⟨x*y,
begin rw padic_norm_e.mul, apply mul_le_one; {assumption <|> apply norm_nonneg} end⟩⟩
/-- Negation on ℤ_p is inherited from ℚ_p. -/
instance : has_neg ℤ_[p] :=
⟨λ ⟨x, hx⟩, ⟨-x, by simpa⟩⟩
/-- Subtraction on ℤ_p is inherited from ℚ_p. -/
instance : has_sub ℤ_[p] :=
⟨λ ⟨x, hx⟩ ⟨y, hy⟩, ⟨x - y,
by { rw sub_eq_add_neg, rw ← norm_neg at hy,
exact le_trans (padic_norm_e.nonarchimedean _ _) (max_le_iff.2 ⟨hx, hy⟩) }⟩⟩
/-- Zero on ℤ_p is inherited from ℚ_p. -/
instance : has_zero ℤ_[p] :=
⟨⟨0, by norm_num⟩⟩
instance : inhabited ℤ_[p] := ⟨0⟩
/-- One on ℤ_p is inherited from ℚ_p. -/
instance : has_one ℤ_[p] :=
⟨⟨1, by norm_num⟩⟩
@[simp] lemma mk_zero {h} : (⟨0, h⟩ : ℤ_[p]) = (0 : ℤ_[p]) := rfl
@[simp] lemma val_eq_coe (z : ℤ_[p]) : z.val = z := rfl
@[simp, norm_cast] lemma coe_add : ∀ (z1 z2 : ℤ_[p]), ((z1 + z2 : ℤ_[p]) : ℚ_[p]) = z1 + z2
| ⟨_, _⟩ ⟨_, _⟩ := rfl
@[simp, norm_cast] lemma coe_mul : ∀ (z1 z2 : ℤ_[p]), ((z1 * z2 : ℤ_[p]) : ℚ_[p]) = z1 * z2
| ⟨_, _⟩ ⟨_, _⟩ := rfl
@[simp, norm_cast] lemma coe_neg : ∀ (z1 : ℤ_[p]), ((-z1 : ℤ_[p]) : ℚ_[p]) = -z1
| ⟨_, _⟩ := rfl
@[simp, norm_cast] lemma coe_sub : ∀ (z1 z2 : ℤ_[p]), ((z1 - z2 : ℤ_[p]) : ℚ_[p]) = z1 - z2
| ⟨_, _⟩ ⟨_, _⟩ := rfl
@[simp, norm_cast] lemma coe_one : ((1 : ℤ_[p]) : ℚ_[p]) = 1 := rfl
@[simp, norm_cast] lemma coe_coe : ∀ n : ℕ, ((n : ℤ_[p]) : ℚ_[p]) = n
| 0 := rfl
| (k+1) := by simp [coe_coe]
@[simp, norm_cast] lemma coe_coe_int : ∀ (z : ℤ), ((z : ℤ_[p]) : ℚ_[p]) = z
| (int.of_nat n) := by simp
| -[1+n] := by simp
@[simp, norm_cast] lemma coe_zero : ((0 : ℤ_[p]) : ℚ_[p]) = 0 := rfl
instance : ring ℤ_[p] :=
by refine_struct
{ add := (+),
mul := (*),
neg := has_neg.neg,
zero := (0 : ℤ_[p]),
one := 1,
sub := has_sub.sub,
npow := @npow_rec _ ⟨1⟩ ⟨(*)⟩,
nsmul := @nsmul_rec _ ⟨0⟩ ⟨(+)⟩,
gsmul := @gsmul_rec _ ⟨0⟩ ⟨(+)⟩ ⟨has_neg.neg⟩ };
intros; try { refl }; ext; simp; ring
/-- The coercion from ℤ[p] to ℚ[p] as a ring homomorphism. -/
def coe.ring_hom : ℤ_[p] →+* ℚ_[p] :=
{ to_fun := (coe : ℤ_[p] → ℚ_[p]),
map_zero' := rfl,
map_one' := rfl,
map_mul' := coe_mul,
map_add' := coe_add }
@[simp, norm_cast] lemma coe_pow (x : ℤ_[p]) (n : ℕ) : (↑(x^n) : ℚ_[p]) = (↑x : ℚ_[p])^n :=
coe.ring_hom.map_pow x n
@[simp] lemma mk_coe : ∀ (k : ℤ_[p]), (⟨k, k.2⟩ : ℤ_[p]) = k
| ⟨_, _⟩ := rfl
/-- The inverse of a p-adic integer with norm equal to 1 is also a p-adic integer. Otherwise, the
inverse is defined to be 0. -/
def inv : ℤ_[p] → ℤ_[p]
| ⟨k, _⟩ := if h : ∥k∥ = 1 then ⟨1/k, by simp [h]⟩ else 0
instance : char_zero ℤ_[p] :=
{ cast_injective :=
λ m n h, nat.cast_injective $
show (m:ℚ_[p]) = n, by { rw subtype.ext_iff at h, norm_cast at h, exact h } }
@[simp, norm_cast] lemma coe_int_eq (z1 z2 : ℤ) : (z1 : ℤ_[p]) = z2 ↔ z1 = z2 :=
suffices (z1 : ℚ_[p]) = z2 ↔ z1 = z2, from iff.trans (by norm_cast) this,
by norm_cast
/--
A sequence of integers that is Cauchy with respect to the `p`-adic norm
converges to a `p`-adic integer.
-/
def of_int_seq (seq : ℕ → ℤ) (h : is_cau_seq (padic_norm p) (λ n, seq n)) : ℤ_[p] :=
⟨⟦⟨_, h⟩⟧,
show ↑(padic_seq.norm _) ≤ (1 : ℝ), begin
rw padic_seq.norm,
split_ifs with hne; norm_cast,
{ exact zero_le_one },
{ apply padic_norm.of_int }
end ⟩
end padic_int
namespace padic_int
/-!
### Instances
We now show that `ℤ_[p]` is a
* complete metric space
* normed ring
* integral domain
-/
variables (p : ℕ) [fact p.prime]
instance : metric_space ℤ_[p] := subtype.metric_space
instance complete_space : complete_space ℤ_[p] :=
have is_closed {x : ℚ_[p] | ∥x∥ ≤ 1}, from is_closed_le continuous_norm continuous_const,
this.complete_space_coe
instance : has_norm ℤ_[p] := ⟨λ z, ∥(z : ℚ_[p])∥⟩
variables {p}
protected lemma mul_comm : ∀ z1 z2 : ℤ_[p], z1*z2 = z2*z1
| ⟨q1, h1⟩ ⟨q2, h2⟩ := show (⟨q1*q2, _⟩ : ℤ_[p]) = ⟨q2*q1, _⟩, by simp [_root_.mul_comm]
protected lemma zero_ne_one : (0 : ℤ_[p]) ≠ 1 :=
show (⟨(0 : ℚ_[p]), _⟩ : ℤ_[p]) ≠ ⟨(1 : ℚ_[p]), _⟩, from mt subtype.ext_iff_val.1 zero_ne_one
protected lemma eq_zero_or_eq_zero_of_mul_eq_zero :
∀ (a b : ℤ_[p]), a * b = 0 → a = 0 ∨ b = 0
| ⟨a, ha⟩ ⟨b, hb⟩ := λ h : (⟨a * b, _⟩ : ℤ_[p]) = ⟨0, _⟩,
have a * b = 0, from subtype.ext_iff_val.1 h,
(mul_eq_zero.1 this).elim
(λ h1, or.inl (by simp [h1]; refl))
(λ h2, or.inr (by simp [h2]; refl))
lemma norm_def {z : ℤ_[p]} : ∥z∥ = ∥(z : ℚ_[p])∥ := rfl
variables (p)
instance : normed_comm_ring ℤ_[p] :=
{ dist_eq := λ ⟨_, _⟩ ⟨_, _⟩, rfl,
norm_mul := λ ⟨_, _⟩ ⟨_, _⟩, norm_mul_le _ _,
mul_comm := padic_int.mul_comm }
instance : norm_one_class ℤ_[p] := ⟨norm_def.trans norm_one⟩
instance is_absolute_value : is_absolute_value (λ z : ℤ_[p], ∥z∥) :=
{ abv_nonneg := norm_nonneg,
abv_eq_zero := λ ⟨_, _⟩, by simp [norm_eq_zero],
abv_add := λ ⟨_,_⟩ ⟨_, _⟩, norm_add_le _ _,
abv_mul := λ _ _, by simp only [norm_def, padic_norm_e.mul, padic_int.coe_mul]}
variables {p}
instance : integral_domain ℤ_[p] :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ x y, padic_int.eq_zero_or_eq_zero_of_mul_eq_zero x y,
exists_pair_ne := ⟨0, 1, padic_int.zero_ne_one⟩,
.. padic_int.normed_comm_ring p }
end padic_int
namespace padic_int
/-! ### Norm -/
variables {p : ℕ} [fact p.prime]
lemma norm_le_one : ∀ z : ℤ_[p], ∥z∥ ≤ 1
| ⟨_, h⟩ := h
@[simp] lemma norm_mul (z1 z2 : ℤ_[p]) : ∥z1 * z2∥ = ∥z1∥ * ∥z2∥ :=
by simp [norm_def]
@[simp] lemma norm_pow (z : ℤ_[p]) : ∀ n : ℕ, ∥z^n∥ = ∥z∥^n
| 0 := by simp
| (k+1) := by { rw [pow_succ, pow_succ, norm_mul], congr, apply norm_pow }
theorem nonarchimedean : ∀ (q r : ℤ_[p]), ∥q + r∥ ≤ max (∥q∥) (∥r∥)
| ⟨_, _⟩ ⟨_, _⟩ := padic_norm_e.nonarchimedean _ _
theorem norm_add_eq_max_of_ne : ∀ {q r : ℤ_[p]}, ∥q∥ ≠ ∥r∥ → ∥q+r∥ = max (∥q∥) (∥r∥)
| ⟨_, _⟩ ⟨_, _⟩ := padic_norm_e.add_eq_max_of_ne
lemma norm_eq_of_norm_add_lt_right {z1 z2 : ℤ_[p]}
(h : ∥z1 + z2∥ < ∥z2∥) : ∥z1∥ = ∥z2∥ :=
by_contradiction $ λ hne,
not_lt_of_ge (by rw norm_add_eq_max_of_ne hne; apply le_max_right) h
lemma norm_eq_of_norm_add_lt_left {z1 z2 : ℤ_[p]}
(h : ∥z1 + z2∥ < ∥z1∥) : ∥z1∥ = ∥z2∥ :=
by_contradiction $ λ hne,
not_lt_of_ge (by rw norm_add_eq_max_of_ne hne; apply le_max_left) h
@[simp] lemma padic_norm_e_of_padic_int (z : ℤ_[p]) : ∥(↑z : ℚ_[p])∥ = ∥z∥ :=
by simp [norm_def]
lemma norm_int_cast_eq_padic_norm (z : ℤ) : ∥(z : ℤ_[p])∥ = ∥(z : ℚ_[p])∥ :=
by simp [norm_def]
@[simp] lemma norm_eq_padic_norm {q : ℚ_[p]} (hq : ∥q∥ ≤ 1) :
@norm ℤ_[p] _ ⟨q, hq⟩ = ∥q∥ := rfl
@[simp] lemma norm_p : ∥(p : ℤ_[p])∥ = p⁻¹ :=
show ∥((p : ℤ_[p]) : ℚ_[p])∥ = p⁻¹, by exact_mod_cast padic_norm_e.norm_p
@[simp] lemma norm_p_pow (n : ℕ) : ∥(p : ℤ_[p])^n∥ = p^(-n:ℤ) :=
show ∥((p^n : ℤ_[p]) : ℚ_[p])∥ = p^(-n:ℤ),
by { convert padic_norm_e.norm_p_pow n, simp, }
private def cau_seq_to_rat_cau_seq (f : cau_seq ℤ_[p] norm) :
cau_seq ℚ_[p] (λ a, ∥a∥) :=
⟨ λ n, f n,
λ _ hε, by simpa [norm, norm_def] using f.cauchy hε ⟩
variables (p)
instance complete : cau_seq.is_complete ℤ_[p] norm :=
⟨ λ f,
have hqn : ∥cau_seq.lim (cau_seq_to_rat_cau_seq f)∥ ≤ 1,
from padic_norm_e_lim_le zero_lt_one (λ _, norm_le_one _),
⟨ ⟨_, hqn⟩,
λ ε, by simpa [norm, norm_def] using cau_seq.equiv_lim (cau_seq_to_rat_cau_seq f) ε⟩⟩
end padic_int
namespace padic_int
variables (p : ℕ) [hp_prime : fact p.prime]
include hp_prime
lemma exists_pow_neg_lt {ε : ℝ} (hε : 0 < ε) :
∃ (k : ℕ), ↑p ^ -((k : ℕ) : ℤ) < ε :=
begin
obtain ⟨k, hk⟩ := exists_nat_gt ε⁻¹,
use k,
rw ← inv_lt_inv hε (_root_.fpow_pos_of_pos _ _),
{ rw [fpow_neg, inv_inv₀, gpow_coe_nat],
apply lt_of_lt_of_le hk,
norm_cast,
apply le_of_lt,
convert nat.lt_pow_self _ _ using 1,
exact hp_prime.1.one_lt },
{ exact_mod_cast hp_prime.1.pos }
end
lemma exists_pow_neg_lt_rat {ε : ℚ} (hε : 0 < ε) :
∃ (k : ℕ), ↑p ^ -((k : ℕ) : ℤ) < ε :=
begin
obtain ⟨k, hk⟩ := @exists_pow_neg_lt p _ ε (by exact_mod_cast hε),
use k,
rw (show (p : ℝ) = (p : ℚ), by simp) at hk,
exact_mod_cast hk
end
variable {p}
lemma norm_int_lt_one_iff_dvd (k : ℤ) : ∥(k : ℤ_[p])∥ < 1 ↔ ↑p ∣ k :=
suffices ∥(k : ℚ_[p])∥ < 1 ↔ ↑p ∣ k, by rwa norm_int_cast_eq_padic_norm,
padic_norm_e.norm_int_lt_one_iff_dvd k
lemma norm_int_le_pow_iff_dvd {k : ℤ} {n : ℕ} : ∥(k : ℤ_[p])∥ ≤ ((↑p)^(-n : ℤ)) ↔ ↑p^n ∣ k :=
suffices ∥(k : ℚ_[p])∥ ≤ ((↑p)^(-n : ℤ)) ↔ ↑(p^n) ∣ k, by simpa [norm_int_cast_eq_padic_norm],
padic_norm_e.norm_int_le_pow_iff_dvd _ _
/-! ### Valuation on `ℤ_[p]` -/
/-- `padic_int.valuation` lifts the p-adic valuation on `ℚ` to `ℤ_[p]`. -/
def valuation (x : ℤ_[p]) := padic.valuation (x : ℚ_[p])
lemma norm_eq_pow_val {x : ℤ_[p]} (hx : x ≠ 0) :
∥x∥ = p^(-x.valuation) :=
begin
convert padic.norm_eq_pow_val _,
contrapose! hx,
exact subtype.val_injective hx
end
@[simp] lemma valuation_zero : valuation (0 : ℤ_[p]) = 0 :=
padic.valuation_zero
@[simp] lemma valuation_one : valuation (1 : ℤ_[p]) = 0 :=
padic.valuation_one
@[simp] lemma valuation_p : valuation (p : ℤ_[p]) = 1 :=
by simp [valuation, -cast_eq_of_rat_of_nat]
lemma valuation_nonneg (x : ℤ_[p]) : 0 ≤ x.valuation :=
begin
by_cases hx : x = 0,
{ simp [hx] },
have h : (1 : ℝ) < p := by exact_mod_cast hp_prime.1.one_lt,
rw [← neg_nonpos, ← (fpow_strict_mono h).le_iff_le],
show (p : ℝ) ^ -valuation x ≤ p ^ 0,
rw [← norm_eq_pow_val hx],
simpa using x.property,
end
@[simp] lemma valuation_p_pow_mul (n : ℕ) (c : ℤ_[p]) (hc : c ≠ 0) :
(↑p ^ n * c).valuation = n + c.valuation :=
begin
have : ∥↑p ^ n * c∥ = ∥(p ^ n : ℤ_[p])∥ * ∥c∥,
{ exact norm_mul _ _ },
have aux : ↑p ^ n * c ≠ 0,
{ contrapose! hc, rw mul_eq_zero at hc, cases hc,
{ refine (hp_prime.1.ne_zero _).elim,
exact_mod_cast (pow_eq_zero hc) },
{ exact hc } },
rwa [norm_eq_pow_val aux, norm_p_pow, norm_eq_pow_val hc,
← fpow_add, ← neg_add, fpow_inj, neg_inj] at this,
{ exact_mod_cast hp_prime.1.pos },
{ exact_mod_cast hp_prime.1.ne_one },
{ exact_mod_cast hp_prime.1.ne_zero },
end
section units
/-! ### Units of `ℤ_[p]` -/
local attribute [reducible] padic_int
lemma mul_inv : ∀ {z : ℤ_[p]}, ∥z∥ = 1 → z * z.inv = 1
| ⟨k, _⟩ h :=
begin
have hk : k ≠ 0, from λ h', @zero_ne_one ℚ_[p] _ _ (by simpa [h'] using h),
unfold padic_int.inv, split_ifs,
{ change (⟨k * (1/k), _⟩ : ℤ_[p]) = 1,
simp [hk], refl },
{ apply subtype.ext_iff_val.2, simp [mul_inv_cancel hk] }
end
lemma inv_mul {z : ℤ_[p]} (hz : ∥z∥ = 1) : z.inv * z = 1 :=
by rw [mul_comm, mul_inv hz]
lemma is_unit_iff {z : ℤ_[p]} : is_unit z ↔ ∥z∥ = 1 :=
⟨λ h, begin
rcases is_unit_iff_dvd_one.1 h with ⟨w, eq⟩,
refine le_antisymm (norm_le_one _) _,
have := mul_le_mul_of_nonneg_left (norm_le_one w) (norm_nonneg z),
rwa [mul_one, ← norm_mul, ← eq, norm_one] at this
end, λ h, ⟨⟨z, z.inv, mul_inv h, inv_mul h⟩, rfl⟩⟩
lemma norm_lt_one_add {z1 z2 : ℤ_[p]} (hz1 : ∥z1∥ < 1) (hz2 : ∥z2∥ < 1) : ∥z1 + z2∥ < 1 :=
lt_of_le_of_lt (nonarchimedean _ _) (max_lt hz1 hz2)
lemma norm_lt_one_mul {z1 z2 : ℤ_[p]} (hz2 : ∥z2∥ < 1) : ∥z1 * z2∥ < 1 :=
calc ∥z1 * z2∥ = ∥z1∥ * ∥z2∥ : by simp
... < 1 : mul_lt_one_of_nonneg_of_lt_one_right (norm_le_one _) (norm_nonneg _) hz2
@[simp] lemma mem_nonunits {z : ℤ_[p]} : z ∈ nonunits ℤ_[p] ↔ ∥z∥ < 1 :=
by rw lt_iff_le_and_ne; simp [norm_le_one z, nonunits, is_unit_iff]
/-- A `p`-adic number `u` with `∥u∥ = 1` is a unit of `ℤ_[p]`. -/
def mk_units {u : ℚ_[p]} (h : ∥u∥ = 1) : units ℤ_[p] :=
let z : ℤ_[p] := ⟨u, le_of_eq h⟩ in ⟨z, z.inv, mul_inv h, inv_mul h⟩
@[simp]
lemma mk_units_eq {u : ℚ_[p]} (h : ∥u∥ = 1) : ((mk_units h : ℤ_[p]) : ℚ_[p]) = u :=
rfl
@[simp] lemma norm_units (u : units ℤ_[p]) : ∥(u : ℤ_[p])∥ = 1 :=
is_unit_iff.mp $ by simp
/-- `unit_coeff hx` is the unit `u` in the unique representation `x = u * p ^ n`.
See `unit_coeff_spec`. -/
def unit_coeff {x : ℤ_[p]} (hx : x ≠ 0) : units ℤ_[p] :=
let u : ℚ_[p] := x*p^(-x.valuation) in
have hu : ∥u∥ = 1,
by simp [hx, nat.fpow_ne_zero_of_pos (by exact_mod_cast hp_prime.1.pos) x.valuation,
norm_eq_pow_val, fpow_neg, inv_mul_cancel, -cast_eq_of_rat_of_nat],
mk_units hu
@[simp] lemma unit_coeff_coe {x : ℤ_[p]} (hx : x ≠ 0) :
(unit_coeff hx : ℚ_[p]) = x * p ^ (-x.valuation) := rfl
lemma unit_coeff_spec {x : ℤ_[p]} (hx : x ≠ 0) :
x = (unit_coeff hx : ℤ_[p]) * p ^ int.nat_abs (valuation x) :=
begin
apply subtype.coe_injective,
push_cast,
have repr : (x : ℚ_[p]) = (unit_coeff hx) * p ^ x.valuation,
{ rw [unit_coeff_coe, mul_assoc, ← fpow_add],
{ simp },
{ exact_mod_cast hp_prime.1.ne_zero } },
convert repr using 2,
rw [← gpow_coe_nat, int.nat_abs_of_nonneg (valuation_nonneg x)],
end
end units
section norm_le_iff
/-! ### Various characterizations of open unit balls -/
lemma norm_le_pow_iff_le_valuation (x : ℤ_[p]) (hx : x ≠ 0) (n : ℕ) :
∥x∥ ≤ p ^ (-n : ℤ) ↔ ↑n ≤ x.valuation :=
begin
rw norm_eq_pow_val hx,
lift x.valuation to ℕ using x.valuation_nonneg with k hk,
simp only [int.coe_nat_le, fpow_neg, gpow_coe_nat],
have aux : ∀ n : ℕ, 0 < (p ^ n : ℝ),
{ apply pow_pos, exact_mod_cast hp_prime.1.pos },
rw [inv_le_inv (aux _) (aux _)],
have : p ^ n ≤ p ^ k ↔ n ≤ k := (strict_mono_pow hp_prime.1.one_lt).le_iff_le,
rw [← this],
norm_cast,
end
lemma mem_span_pow_iff_le_valuation (x : ℤ_[p]) (hx : x ≠ 0) (n : ℕ) :
x ∈ (ideal.span {p ^ n} : ideal ℤ_[p]) ↔ ↑n ≤ x.valuation :=
begin
rw [ideal.mem_span_singleton],
split,
{ rintro ⟨c, rfl⟩,
suffices : c ≠ 0,
{ rw [valuation_p_pow_mul _ _ this, le_add_iff_nonneg_right], apply valuation_nonneg, },
contrapose! hx, rw [hx, mul_zero], },
{ rw [unit_coeff_spec hx] { occs := occurrences.pos [2] },
lift x.valuation to ℕ using x.valuation_nonneg with k hk,
simp only [int.nat_abs_of_nat, units.is_unit, is_unit.dvd_mul_left, int.coe_nat_le],
intro H,
obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le H,
simp only [pow_add, dvd_mul_right], }
end
lemma norm_le_pow_iff_mem_span_pow (x : ℤ_[p]) (n : ℕ) :
∥x∥ ≤ p ^ (-n : ℤ) ↔ x ∈ (ideal.span {p ^ n} : ideal ℤ_[p]) :=
begin
by_cases hx : x = 0,
{ subst hx,
simp only [norm_zero, fpow_neg, gpow_coe_nat, inv_nonneg, iff_true, submodule.zero_mem],
exact_mod_cast nat.zero_le _ },
rw [norm_le_pow_iff_le_valuation x hx, mem_span_pow_iff_le_valuation x hx],
end
lemma norm_le_pow_iff_norm_lt_pow_add_one (x : ℤ_[p]) (n : ℤ) :
∥x∥ ≤ p ^ n ↔ ∥x∥ < p ^ (n + 1) :=
begin
have aux : ∀ n : ℤ, 0 < (p ^ n : ℝ),
{ apply nat.fpow_pos_of_pos, exact hp_prime.1.pos },
by_cases hx0 : x = 0, { simp [hx0, norm_zero, aux, le_of_lt (aux _)], },
rw norm_eq_pow_val hx0,
have h1p : 1 < (p : ℝ), { exact_mod_cast hp_prime.1.one_lt },
have H := fpow_strict_mono h1p,
rw [H.le_iff_le, H.lt_iff_lt, int.lt_add_one_iff],
end
lemma norm_lt_pow_iff_norm_le_pow_sub_one (x : ℤ_[p]) (n : ℤ) :
∥x∥ < p ^ n ↔ ∥x∥ ≤ p ^ (n - 1) :=
by rw [norm_le_pow_iff_norm_lt_pow_add_one, sub_add_cancel]
lemma norm_lt_one_iff_dvd (x : ℤ_[p]) : ∥x∥ < 1 ↔ ↑p ∣ x :=
begin
have := norm_le_pow_iff_mem_span_pow x 1,
rw [ideal.mem_span_singleton, pow_one] at this,
rw [← this, norm_le_pow_iff_norm_lt_pow_add_one],
simp only [gpow_zero, int.coe_nat_zero, int.coe_nat_succ, add_left_neg, zero_add],
end
@[simp] lemma pow_p_dvd_int_iff (n : ℕ) (a : ℤ) : (p ^ n : ℤ_[p]) ∣ a ↔ ↑p ^ n ∣ a :=
by rw [← norm_int_le_pow_iff_dvd, norm_le_pow_iff_mem_span_pow, ideal.mem_span_singleton]
end norm_le_iff
section dvr
/-! ### Discrete valuation ring -/
instance : local_ring ℤ_[p] :=
local_of_nonunits_ideal zero_ne_one $ λ x y, by simp; exact norm_lt_one_add
lemma p_nonnunit : (p : ℤ_[p]) ∈ nonunits ℤ_[p] :=
have (p : ℝ)⁻¹ < 1, from inv_lt_one $ by exact_mod_cast hp_prime.1.one_lt,
by simp [this]
lemma maximal_ideal_eq_span_p : maximal_ideal ℤ_[p] = ideal.span {p} :=
begin
apply le_antisymm,
{ intros x hx,
rw ideal.mem_span_singleton,
simp only [local_ring.mem_maximal_ideal, mem_nonunits] at hx,
rwa ← norm_lt_one_iff_dvd, },
{ rw [ideal.span_le, set.singleton_subset_iff], exact p_nonnunit }
end
lemma prime_p : prime (p : ℤ_[p]) :=
begin
rw [← ideal.span_singleton_prime, ← maximal_ideal_eq_span_p],
{ apply_instance },
{ exact_mod_cast hp_prime.1.ne_zero }
end
lemma irreducible_p : irreducible (p : ℤ_[p]) :=
prime.irreducible prime_p
instance : discrete_valuation_ring ℤ_[p] :=
discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization
⟨p, irreducible_p, λ x hx, ⟨x.valuation.nat_abs, unit_coeff hx,
by rw [mul_comm, ← unit_coeff_spec hx]⟩⟩
lemma ideal_eq_span_pow_p {s : ideal ℤ_[p]} (hs : s ≠ ⊥) :
∃ n : ℕ, s = ideal.span {p ^ n} :=
discrete_valuation_ring.ideal_eq_span_pow_irreducible hs irreducible_p
open cau_seq
instance : is_adic_complete (maximal_ideal ℤ_[p]) ℤ_[p] :=
{ prec' := λ x hx,
begin
simp only [← ideal.one_eq_top, smul_eq_mul, mul_one, smodeq.sub_mem, maximal_ideal_eq_span_p,
ideal.span_singleton_pow, ← norm_le_pow_iff_mem_span_pow] at hx ⊢,
let x' : cau_seq ℤ_[p] norm := ⟨x, _⟩, swap,
{ intros ε hε, obtain ⟨m, hm⟩ := exists_pow_neg_lt p hε,
refine ⟨m, λ n hn, lt_of_le_of_lt _ hm⟩, rw [← neg_sub, norm_neg], exact hx hn },
{ refine ⟨x'.lim, λ n, _⟩,
have : (0:ℝ) < p ^ (-n : ℤ), { apply fpow_pos_of_pos, exact_mod_cast hp_prime.1.pos },
obtain ⟨i, hi⟩ := equiv_def₃ (equiv_lim x') this,
by_cases hin : i ≤ n,
{ exact (hi i le_rfl n hin).le, },
{ push_neg at hin, specialize hi i le_rfl i le_rfl, specialize hx hin.le,
have := nonarchimedean (x n - x i) (x i - x'.lim),
rw [sub_add_sub_cancel] at this,
refine this.trans (max_le_iff.mpr ⟨hx, hi.le⟩), } },
end }
end dvr
end padic_int
|
82a54723d7fa5c5453171fe79ff7c9a02e7cd7c0 | f57570f33b51ef0271f8c366142363d5ae8fff45 | /src/prop_sat.lean | 2706e3fc72bb9b35d2b6d325fe7dd3ec53144c39 | [] | no_license | maxd13/lean-logic | 4083cb3fbb45b423befca7fda7268b8ba85ff3a6 | ddcab46b77adca91b120a5f37afbd48794da8b52 | refs/heads/master | 1,692,257,681,488 | 1,631,740,832,000 | 1,631,740,832,000 | 246,324,437 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,679 | lean | import tactic.tidy tactic.library_search
inductive proposition
| false : proposition
| true : proposition
| atomic : ℕ → proposition
-- | and : proposition → proposition → proposition
| or : proposition → proposition → proposition
| not : proposition → proposition
-- | if_then : proposition → proposition → proposition
-- def proposition.if_then : proposition → proposition → proposition
-- | φ ψ := proposition.or (proposition.not φ) ψ
-- notation p `⇒` q := proposition.if_then p q
-- def proposition.not : proposition → proposition := λ p, p ⇒ proposition.false
def proposition.and : proposition → proposition → proposition
| φ ψ := proposition.not $ proposition.or (proposition.not φ) (proposition.not ψ)
-- def proposition.iff : proposition → proposition → proposition
-- | φ ψ := proposition.and (φ ⇒ ψ) (ψ ⇒ φ)
def proposition.eval : proposition → (ℕ → bool) → bool
| proposition.false _ := ff
| proposition.true _ := tt
| (proposition.atomic n) f := if f n then tt else ff
-- | (proposition.and φ ψ) f := φ.eval f && ψ.eval f
| (proposition.or φ ψ) f := φ.eval f || ψ.eval f
| (proposition.not φ) f := not (φ.eval f)
-- | (proposition.if_then φ ψ) f := not (φ.eval f) || ψ.eval f
def proposition.appears : proposition → ℕ → bool
| proposition.false _ := ff
| proposition.true _ := ff
| (proposition.atomic m) n := if n = m then tt else ff
-- | (proposition.and φ ψ) n := φ.appears n || ψ.appears n
| (proposition.or φ ψ) n := φ.appears n || ψ.appears n
| (proposition.not φ) f := φ.appears f
-- | (proposition.if_then φ ψ) n := φ.appears n || ψ.appears n
def theory.appears : list proposition → ℕ → bool
| [] _ := ff
| (x::xs) n := x.appears n || theory.appears xs n
lemma theory.exists : ∀ (Γ : list proposition) n,
theory.appears Γ n →
∃ φ ∈ Γ, proposition.appears φ n
:=
begin
intros Γ n h,
induction Γ;
simp [theory.appears] at h,
contradiction,
cases h,
existsi Γ_hd,
fsplit, simp,
assumption,
obtain ⟨φ, c₁, c₂⟩ := Γ_ih h,
existsi φ,
fsplit,
simp [c₁],
assumption,
end
def proposition.satisfiable (φ : proposition) :=
∃ I : ℕ → bool, φ.eval I
-- This code here actually implements the sat solver...
-- FROM A TACTIC PROOF!!!
instance sat_solver (φ : proposition) : decidable φ.satisfiable :=
let I₁ := λn : ℕ, tt in
begin
induction φ,
-- case false
left,
intro h,
obtain ⟨I, c⟩ := h,
simp [proposition.eval] at c,
contradiction,
-- case true
right,
existsi I₁,
simp [proposition.eval],
-- case atomic
right,
existsi I₁,
simp [proposition.eval],
-- case and
-- This is actually hard.
-- We only managed to prove that if
-- either side of the conjunction is
-- unsat, then the conjunction is unsat.
-- maybe that is why most algorithms assume
-- that the proposition is on
-- a particular normal form.
-- So when we got to this point,
-- we changed the definition of proposition
-- to use only ∨ and ¬, which is
-- functionally complete.
-- cases φ_ih_a,
-- left,
-- intro h,
-- obtain ⟨I, c⟩ := h,
-- simp [proposition.eval] at c,
-- replace c : φ_a.satisfiable := ⟨I,c.1⟩,
-- contradiction,
-- cases φ_ih_a_1,
-- left,
-- intro h,
-- obtain ⟨I, c⟩ := h,
-- simp [proposition.eval] at c,
-- replace c : φ_a_1.satisfiable := ⟨I,c.2⟩,
-- contradiction,
-- case or
cases φ_ih_a,
cases φ_ih_a_1,
left,
intro h,
obtain ⟨I, c⟩ := h,
simp [proposition.eval] at c,
cases c,
replace c : φ_a.satisfiable := ⟨I,c⟩,
contradiction,
replace c : φ_a_1.satisfiable := ⟨I,c⟩,
contradiction,
right,
obtain ⟨I, c⟩ := φ_ih_a_1,
existsi I,
simp [proposition.eval],
right,
assumption,
right,
obtain ⟨I, c⟩ := φ_ih_a,
existsi I,
simp [proposition.eval],
left,
assumption,
-- case not
cases φ_ih,
right,
have c := not_exists.mp φ_ih I₁,
existsi I₁,
simp [proposition.eval],
simp at c,
rw c,
simp,
admit,
-- case if_then
-- cases φ_ih_a,
-- right,
-- existsi I₁,
-- simp [proposition.eval],
-- have c := not_exists.mp φ_ih_a I₁,
-- simp at c,
-- left,
-- rw c,
-- simp,
-- cases φ_ih_a_1,
-- left,
-- intro h,
-- obtain ⟨I, c⟩ := h,
-- simp [proposition.eval] at c,
end
def proposition.sat : proposition → bool
| φ := if φ.satisfiable then tt else ff
#reduce proposition.false.sat
#reduce proposition.true.sat
#reduce λ n, (proposition.atomic n).sat
#reduce λ n, (proposition.or (proposition.atomic n) proposition.false).sat
#reduce λ n, (proposition.or (proposition.atomic n) (proposition.not $ proposition.atomic n)).sat
-- #reduce λ n, (proposition.and (proposition.atomic n) (proposition.not $ proposition.atomic n)).sat
def proposition.follows (Γ : list proposition) : proposition → Prop
| φ := ∀ (I : ℕ → bool),
(∀ ψ ∈ Γ, proposition.eval ψ I == tt) →
φ.eval I == tt
-- instance proposition_decidable (Γ : list proposition) (φ : proposition) :decidable (φ.follows Γ) :=
-- begin
-- induction φ,
-- admit,
-- right,
-- intros I h,
-- refl,
-- by_cases (theory.appears Γ φ) = tt,
-- have c : Prop,
-- obtain ⟨ψ, c₀, c₁⟩ := theory.exists Γ φ h,
-- replace h₂ := h₂ ψ c₀,
-- right,
-- intros I h₂,
-- dsimp [proposition.eval],
-- simp,
-- -- let ψ := proposition.atomic φ,
-- -- induction Γ;
-- -- simp [theory.appears] at h,
-- -- contradiction,
-- -- have c := h ψ h₂,
-- end
-- def proposition.decide : proposition → set proposition → bool
-- | φ Γ := if φ.follows Γ then tt else ff
|
82d7a4a7897e3406bb3e4aacf9f1df1aeacb0ad8 | 35960c5b117752aca7e3e7767c0b393e4dbd72a7 | /src/typing/props.lean | 689cd7475156723f0b1e86032af65f332a1c6014 | [
"Apache-2.0"
] | permissive | spl/tts | 461dc76b83df8db47e4660d0941dc97e6d4fd7d1 | b65298fea68ce47c8ed3ba3dbce71c1a20dd3481 | refs/heads/master | 1,541,049,198,347 | 1,537,967,023,000 | 1,537,967,029,000 | 119,653,145 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,320 | lean | import .core
namespace tts ------------------------------------------------------------------
namespace typing ---------------------------------------------------------------
variables {V : Type} [decidable_eq V] -- Type of variable names
variables {x y : tagged V} -- Variables
variables {e ex e₁ : exp V} -- Expressions
variables {t : typ V} -- Types
variables {s s' : sch V} -- Type schemes
variables {Γ Γ₁ Γ₂ Γ₃ : env V} -- Environments
variables {L : finset (tagged V)} -- Variable sets
open finmap occurs exp typ sch
theorem weaken_middle :
disjoint (dom Γ₁) (dom Γ₂) →
disjoint (dom Γ₂) (dom Γ₃) →
disjoint (dom Γ₁) (dom Γ₃) →
typing (Γ₁ ∪ Γ₃) e t →
typing (Γ₁ ∪ Γ₂ ∪ Γ₃) e t :=
begin
generalize Γeq : Γ₁ ∪ Γ₃ = Γ₁₃,
intros dj₁₂ dj₂₃ dj₁₃ T,
induction T generalizing Γ₁ Γ₃,
case typing.var : Γ x s ts b ln_eq lts ls {
induction Γeq,
exact var (mem_union_middle_left dj₁₃ dj₂₃ b) ln_eq lts ls,
},
case typing.app : Γ ef ea t₁ t₂ Tf Ta ihf iha {
exact app (ihf Γeq dj₁₂ dj₂₃ dj₁₃) (iha Γeq dj₁₂ dj₂₃ dj₁₃),
},
case typing.lam : v Γ eb t₁ t₂ L lt₁ Ft₂ ihb {
apply lam (L ∪ dom (Γ₁ ∪ Γ₂ ∪ Γ₃)) lt₁,
introv nm,
simp [not_or_distrib, and_assoc] at nm,
rw [←insert_union, ←insert_union],
induction Γeq,
exact ihb nm.1 insert_union
(disjoint_keys_insert_left.mpr ⟨nm.2.1, dj₁₂⟩)
dj₂₃
(disjoint_keys_insert_left.mpr ⟨nm.2.2.2, dj₁₃⟩),
},
case typing.let_ : v Γ ed eb sd tb Ld Lb _ _ ihd ihb {
apply let_ sd (Ld ∪ dom (Γ₁ ∪ Γ₂ ∪ Γ₃)) (Lb ∪ dom (Γ₁ ∪ Γ₂ ∪ Γ₃)),
{ introv ln_eq nd nm,
simp [not_or_distrib, and_assoc] at nm,
exact ihd ln_eq nd (λ _ h, (nm _ h).1) Γeq dj₁₂ dj₂₃ dj₁₃ },
{ introv nm,
simp [not_or_distrib, and_assoc] at nm,
rw [←insert_union, ←insert_union],
induction Γeq,
exact ihb nm.1 insert_union
(disjoint_keys_insert_left.mpr ⟨nm.2.1, dj₁₂⟩)
dj₂₃
(disjoint_keys_insert_left.mpr ⟨nm.2.2.2, dj₁₃⟩) }
}
end
theorem weaken :
disjoint (dom Γ₁) (dom Γ₂) →
typing Γ₂ e t →
typing (Γ₁ ∪ Γ₂) e t :=
begin
intros dj T,
rw ←empty_union Γ₂ at T,
rw ←empty_union Γ₁ at ⊢,
exact weaken_middle (finset.disjoint_empty_left _) dj (finset.disjoint_empty_left _) T
end
lemma ne_of_nm : x ∉ L ∪ dom (Γ₁ ∪ finmap.singleton (y :~ s) ∪ Γ₂) → y ≠ x :=
by simp [not_or_distrib]; tauto
lemma nmL_of_nm : x ∉ L ∪ dom (Γ₁ ∪ finmap.singleton (y :~ s) ∪ Γ₂) → x ∉ L :=
by simp [not_or_distrib]; tauto
lemma nmΓ₂_of_nm : x ∉ L ∪ dom (Γ₁ ∪ finmap.singleton (y :~ s) ∪ Γ₂) → x ∉ dom Γ₂ :=
by simp [not_or_distrib]; tauto
lemma nm_ins : x ∉ dom (Γ₁ ∪ Γ₂) → y ∉ L ∪ dom (Γ₁ ∪ finmap.singleton (x :~ s) ∪ Γ₂) →
x ∉ dom (insert (binding.mk y s') Γ₁ ∪ Γ₂) :=
by simp [not_or_distrib]; tauto
theorem subst_weaken :
disjoint (dom Γ₁) (dom Γ₂) →
x ∉ dom (Γ₁ ∪ Γ₂) →
lc ex →
(∀ {ts : list (typ V)}, s.arity = ts.length → (∀ (t : typ V), t ∈ ts → lc t) → typing Γ₂ ex (open_typs ts s)) →
typing (Γ₁ ∪ finmap.singleton (x :~ s) ∪ Γ₂) e t →
typing (Γ₁ ∪ Γ₂) (subst x ex e) t :=
begin
generalize Γeq : Γ₁ ∪ finmap.singleton (x :~ s) ∪ Γ₂ = Γ₁₂,
intros dj nmd lex Tex T,
induction T generalizing Γ₁ Γ₂ x s ex,
case typing.var : Γ y s' ts b ln_eq lts ls {
induction Γeq,
have b : y :~ s' ∈ Γ₁ ∨ y :~ s' ∈ finmap.singleton (x :~ s) ∨ y :~ s' ∈ Γ₂ :=
(or_assoc _ _).mp (or.imp_left mem_of_mem_union (mem_of_mem_union b)),
by_cases h : x = y,
{ induction h,
simp [not_or_distrib] at nmd,
rcases b with m | m | m,
{ exact absurd (mem_keys_of_mem m) nmd.1 },
{ simp at m, induction m, simp [weaken dj (Tex ln_eq lts)] },
{ exact absurd (mem_keys_of_mem m) nmd.2 } },
{ rcases b with m | m | m,
{ simp [h, var ((mem_union dj).mpr (or.inl m)) ln_eq lts ls] },
{ simp at m, exact absurd m.1.symm h },
{ simp [h, var ((mem_union dj).mpr (or.inr m)) ln_eq lts ls] } } },
case typing.app : Γ ef ea t₁ t₂ Tf Ta ihf iha {
exact app (ihf Γeq dj nmd lex @Tex) (iha Γeq dj nmd lex @Tex) },
case typing.lam : v Γ eb t₁ t₂ L lt₁ Ft₂ ihb {
apply lam (L ∪ dom (Γ₁ ∪ finmap.singleton (x :~ s) ∪ Γ₂)) lt₁,
induction Γeq,
intros y nm,
rw ←subst_open_var lex (ne_of_nm nm),
rw ←insert_union,
exact ihb (nmL_of_nm nm)
(by rw [insert_union, insert_union])
(disjoint_keys_insert_left.mpr ⟨by exact nmΓ₂_of_nm nm, dj⟩)
(nm_ins nmd nm)
lex
@Tex },
case typing.let_ : v Γ ed eb sd tb Ld Lb _ _ ihd ihb {
dsimp at ihd ihb,
apply let_ sd
(Ld ∪ dom (Γ₁ ∪ finmap.singleton (x :~ s) ∪ Γ₂))
(Lb ∪ dom (Γ₁ ∪ finmap.singleton (x :~ s) ∪ Γ₂)),
{ intros xs ln_eq nd nm,
exact ihd ln_eq nd (λ _ h, nmL_of_nm (nm _ h)) Γeq dj nmd lex @Tex },
{ induction Γeq,
intros y nm,
rw ←subst_open_var lex (ne_of_nm nm),
rw ←insert_union,
exact ihb (nmL_of_nm nm)
(by rw [insert_union, insert_union])
(disjoint_keys_insert_left.mpr ⟨by exact nmΓ₂_of_nm nm, dj⟩)
(nm_ins nmd nm)
lex
@Tex } }
end
-- Expression substitution preserves typing.
theorem exp_subst_preservation :
x ∉ dom Γ →
lc ex →
(∀ {ts : list (typ V)}, s.arity = ts.length → (∀ (t : typ V), t ∈ ts → lc t) → typing Γ ex (open_typs ts s)) →
typing (finmap.singleton (x :~ s) ∪ Γ) e₁ t →
typing Γ (subst x ex e₁) t :=
begin
intros nm lex Tex T,
rw ←empty_union (finmap.singleton (x :~ s)) at T,
rw ←empty_union Γ,
exact subst_weaken (finset.disjoint_empty_left _) (by simp [nm]) lex @Tex T
end
-- Typing implies a locally-closed expression
theorem lc_exp (T : typing Γ e t) : lc e :=
begin
induction T,
case typing.var { simp },
case typing.app { simp * },
case typing.lam { simp [exp.lc_body, *], tauto },
case typing.let_ : v _ ed _ _ _ _ _ _ _ ihd {
dsimp at ihd,
have : lc ed := ihd
((fresh.tagged v).pgenl_length_eq _ _)
((fresh.tagged v).pgenl_nodup _ _)
(λ _, (fresh.tagged v).pgenl_not_mem),
simp [exp.lc_body, *],
tauto
}
end
-- Typing implies a locally-closed type
theorem lc_typ (T : typing Γ e t) : lc t :=
begin
induction T,
case typing.var : _ _ _ _ _ ln_eq lts ls {
exact lc_open_typs ls ln_eq lts },
case typing.app : Γ ef ea t₁ t₂ Tf Ta ihf iha {
simp at ihf,
cases ihf with _ lt₂,
exact lt₂ },
case typing.lam : v _ _ _ _ L lt₁ _ ihb {
dsimp at ihb,
simp [lt₁, ihb ((fresh.tagged v).gen_not_mem L)] },
case typing.let_ : v _ _ _ _ _ _ Lb _ _ _ ihb {
exact ihb ((fresh.tagged v).gen_not_mem Lb) }
end
end /- namespace -/ typing -----------------------------------------------------
end /- namespace -/ tts --------------------------------------------------------
|
efec67233175571a63a49dedd159347d8b9c13f6 | 1fd908b06e3f9c1252cb2285ada1102623a67f72 | /hit/quotient.lean | c1701b88b592021f40dba4eae307097fe5bf0db3 | [
"Apache-2.0"
] | permissive | avigad/hott3 | 609a002849182721e7c7ae536d9f1e2956d6d4d3 | f64750cd2de7a81e87d4828246d1369d59f16f43 | refs/heads/master | 1,629,027,243,322 | 1,510,946,717,000 | 1,510,946,717,000 | 103,570,461 | 0 | 0 | null | 1,505,415,620,000 | 1,505,415,620,000 | null | UTF-8 | Lean | false | false | 14,049 | lean | /-
Copyright (c) 2017 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Ulrik Buchholtz
Quotients. This is a quotient without truncation for an arbitrary type-valued binary relation.
See also .set_quotient
-/
/-
The hit quotient is primitive, declared in init.hit.
The constructors are, given {A : Type _} (R : A → A → Type _),
* class_of : A → quotient R (A implicit, R explicit)
* eq_of_rel : Π{a a' : A}, R a a' → class_of a = class_of a' (R explicit)
-/
import arity cubical.squareover types.arrow cubical.pathover2 types.pointed
universes u v w
hott_theory
namespace hott
open hott.eq hott.equiv hott.pi is_trunc pointed hott.sigma
namespace quotient
variables {A : Type _} {R : A → A → Type _}
@[hott, induction, priority 1500] protected def elim {P : Type _} (Pc : A → P) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a')
(x : quotient R) : P :=
begin hinduction x, exact Pc a, exact pathover_of_eq _ (Pp H) end
@[hott, reducible] protected def elim_on {P : Type _} (x : quotient R)
(Pc : A → P) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a') : P :=
quotient.elim Pc Pp x
@[hott] theorem elim_eq_of_rel {P : Type _} (Pc : A → P)
(Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a') {a a' : A} (H : R a a')
: ap (quotient.elim Pc Pp) (eq_of_rel R H) = Pp H :=
begin
apply eq_of_fn_eq_fn_inv ((pathover_constant (eq_of_rel R H)) _ _),
exact sorry --rwr [▸*,-apd_eq_pathover_of_eq_ap,↑quotient.elim,rec_eq_of_rel],
end
@[hott] protected def rec_prop {A : Type _} {R : A → A → Type _} {P : quotient R → Type _}
[H : Πx, is_prop (P x)] (Pc : Π(a : A), P (class_of R a)) (x : quotient R) : P x :=
quotient.rec Pc (λa a' H, is_prop.elimo _ _ _) x
@[hott] protected def elim_prop {P : Type _} [H : is_prop P] (Pc : A → P) (x : quotient R) : P :=
quotient.elim Pc (λa a' H, is_prop.elim _ _) x
@[hott] protected def elim_type (Pc : A → Type _)
(Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a ≃ Pc a') : quotient R → Type _ :=
quotient.elim Pc (λa a' H, ua (Pp H))
@[hott, reducible] protected def elim_type_on (x : quotient R) (Pc : A → Type _)
(Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a ≃ Pc a') : Type _ :=
quotient.elim_type Pc Pp x
@[hott] theorem elim_type_eq_of_rel_fn (Pc : A → Type _)
(Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a ≃ Pc a') {a a' : A} (H : R a a')
: transport (quotient.elim_type Pc Pp) (eq_of_rel R H) = to_fun (Pp H) :=
by exact sorry --rewrite [tr_eq_cast_ap_fn, ↑quotient.elim_type, elim_eq_of_rel]; apply cast_ua_fn
-- rename to elim_type_eq_of_rel_fn_inv
@[hott] theorem elim_type_eq_of_rel_inv (Pc : A → Type _)
(Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a ≃ Pc a') {a a' : A} (H : R a a')
: transport (quotient.elim_type Pc Pp) (eq_of_rel R H)⁻¹ = to_inv (Pp H) :=
by exact sorry --rewrite [tr_eq_cast_ap_fn, ↑quotient.elim_type, ap_inv, elim_eq_of_rel]; apply cast_ua_inv_fn
-- remove '
@[hott] theorem elim_type_eq_of_rel_inv' (Pc : A → Type _)
(Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a ≃ Pc a') {a a' : A} (H : R a a') (x : Pc a')
: transport (quotient.elim_type Pc Pp) (eq_of_rel R H)⁻¹ x = to_inv (Pp H) x :=
ap10 (elim_type_eq_of_rel_inv Pc Pp H) x
@[hott] theorem elim_type_eq_of_rel (Pc : A → Type u)
(Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a ≃ Pc a') {a a' : A} (H : R a a') (p : Pc a)
: transport (quotient.elim_type Pc Pp) (eq_of_rel R H) p = to_fun (Pp H) p :=
ap10 (elim_type_eq_of_rel_fn Pc Pp H) p
@[hott] def elim_type_eq_of_rel' (Pc : A → Type _)
(Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a ≃ Pc a') {a a' : A} (H : R a a') (p : Pc a)
: @pathover _ (class_of _ a) (quotient.elim_type Pc Pp) p _ (eq_of_rel R H) (to_fun (Pp H) p) :=
pathover_of_tr_eq (elim_type_eq_of_rel Pc Pp H p)
@[hott] def elim_type_uncurried (H : Σ(Pc : A → Type _), Π⦃a a' : A⦄ (H : R a a'), Pc a ≃ Pc a')
: quotient R → Type _ :=
quotient.elim_type H.1 H.2
end quotient
namespace quotient
section
variables {A : Type _} (R : A → A → Type _)
/- The dependent universal property -/
@[hott] def quotient_pi_equiv (C : quotient R → Type _) : (Πx, C x) ≃
(Σ(f : Π(a : A), C (class_of R a)), Π⦃a a' : A⦄ (H : R a a'), f a =[eq_of_rel R H] f a') :=
begin
fapply equiv.MK,
{ intro f, exact ⟨λa, f (class_of R a), λa a' H, apd f (eq_of_rel R H)⟩},
{ intros v x, induction v with i p, hinduction x,
exact (i a),
exact (p H)},
{ intro v, induction v with i p,
apply ap (sigma.mk i), apply eq_of_homotopy3, intros a a' H, apply rec_eq_of_rel},
{ intro f, apply eq_of_homotopy, intro x, hinduction x, reflexivity,
apply eq_pathover_dep, rwr rec_eq_of_rel, exact hrflo},
end
end
@[hott] def pquotient {A : Type*} (R : A → A → Type _) : Type* :=
pType.mk (quotient R) (class_of R pt)
/- the flattening lemma -/
namespace flattening
section
parameters {A : Type u} (R : A → A → Type v) (C : A → Type w) (f : Π⦃a a'⦄, R a a' → C a ≃ C a')
include f
variables {a a' : A} {r : R a a'}
private def P := quotient.elim_type C f
@[hott] def flattening_type : Type (max u w) := Σa, C a
private def X := flattening_type
inductive flattening_rel : X → X → Type (max u v w)
| mk : Π⦃a a' : A⦄ (r : R a a') (c : C a), flattening_rel ⟨a, c⟩ ⟨a', f r c⟩
@[hott] def Ppt (c : C a) : sigma P :=
⟨class_of R a, c⟩
@[hott] def Peq (r : R a a') (c : C a) : Ppt c = Ppt (f r c) :=
begin
fapply sigma_eq,
{ apply eq_of_rel R r},
{ refine elim_type_eq_of_rel' C f r c}
end
@[hott] def rec {Q : sigma P → Type _} (Qpt : Π{a : A} (x : C a), Q (Ppt x))
(Qeq : Π⦃a a' : A⦄ (r : R a a') (c : C a), Qpt c =[Peq r c] Qpt (f r c))
(v : sigma P) : Q v :=
begin
induction v with q p,
hinduction q,
{ exact Qpt p},
{ apply pi_pathover_left, intro c,
refine _ ⬝op apdt Qpt (elim_type_eq_of_rel C f H c)⁻¹ᵖ,
refine _ ⬝op (tr_compose Q (Ppt R C f) _ _)⁻¹,
rwr ap_inv,
refine pathover_cancel_right _ (tr_pathover _ _)⁻¹ᵒ,
refine change_path _ (Qeq H c),
symmetry, dsimp [Ppt, Peq],
refine whisker_left _ (ap_dpair _) ⬝ _,
refine (dpair_eq_dpair_con _ _ _ _)⁻¹ ⬝ _,
apply ap (dpair_eq_dpair _),
exact sorry
-- dsimp [elim_type_eq_of_rel',pathover_idp_of_eq],
-- exact (pathover_of_tr_eq_eq_concato _ _ _)⁻¹
},
end
@[hott] def elim {Q : Type _} (Qpt : Π{a : A}, C a → Q)
(Qeq : Π⦃a a' : A⦄ (r : R a a') (c : C a), Qpt c = Qpt (f r c))
(v : sigma P) : Q :=
begin
induction v with q p,
hinduction q,
{ exact Qpt p},
{ apply arrow_pathover_constant_right,
intro c, exact Qeq H c ⬝ ap Qpt (elim_type_eq_of_rel C f H c)⁻¹},
end
@[hott] theorem elim_Peq {Q : Type _} (Qpt : Π{a : A}, C a → Q)
(Qeq : Π⦃a a' : A⦄ (r : R a a') (c : C a), Qpt c = Qpt (f r c)) {a a' : A} (r : R a a')
(c : C a) : ap (elim @Qpt Qeq) (Peq r c) = Qeq r c :=
begin
refine ap_dpair_eq_dpair _ _ _ ⬝ _, exact sorry
-- refine apd011_eq_apo11_apd _ _ _ ⬝ _,
-- rwr [rec_eq_of_rel],
-- refine !apo11_arrow_pathover_constant_right ⬝ _,
-- rwr [↑elim_type_eq_of_rel', to_right_inv !pathover_equiv_tr_eq, ap_inv],
-- apply inv_con_cancel_right
end
open flattening_rel
@[hott] def flattening_lemma : sigma P ≃ quotient flattening_rel :=
begin
fapply equiv.MK,
{ refine elim R C f _ _,
{ intros a c, exact class_of _ ⟨a, c⟩},
{ intros a a' r c, apply eq_of_rel, constructor}},
{ intro q, hinduction q with x x x' H,
{ exact Ppt R C f x.2},
{ induction H, apply Peq}},
{ intro q, hinduction q with x x x' H,
{ induction x with a c, reflexivity},
{ induction H, apply eq_pathover, apply hdeg_square,
refine ap_compose (elim R C f _ _) (quotient.elim _ _) _ ⬝ _,
rwr [elim_eq_of_rel, ap_id],
apply elim_Peq }},
{ refine rec R C f (λa x, idp) _, intros,
apply eq_pathover, apply hdeg_square,
refine ap_compose (quotient.elim _ _) (elim R C f _ _) _ ⬝ _,
rwr [elim_Peq, ap_id],
apply elim_eq_of_rel }
end
end
end flattening
section
open hott.is_equiv hott.equiv prod
variables {A : Type _} (R : A → A → Type _)
{B : Type _} (Q : B → B → Type _)
(f : A → B) (k : Πa a' : A, R a a' → Q (f a) (f a'))
include f k
@[hott] protected def functor : quotient R → quotient Q :=
quotient.elim (λa, class_of Q (f a)) (λa a' r, eq_of_rel Q (k a a' r))
variables [F : is_equiv f] [K : Πa a', is_equiv (k a a')]
include F K
@[hott] protected def functor_inv : quotient Q → quotient R :=
quotient.elim (λb, class_of R (f⁻¹ᶠ b))
(λb b' q, eq_of_rel R ((k (f⁻¹ᶠ b) (f⁻¹ᶠ b'))⁻¹ᶠ
((right_inv f b)⁻¹ ▸ (right_inv f b')⁻¹ ▸ q)))
/- To do: redo this proof -/
@[hott] instance is_equiv : is_equiv (quotient.functor R Q f k) := sorry
-- begin
-- fapply adjointify _ (quotient.functor_inv R Q f k),
-- { intro qb, hinduction qb with b b b' q,
-- { apply ap (class_of Q), apply right_inv },
-- { apply eq_pathover, rwr [ap_id,ap_compose' (quotient.elim _ _)],
-- do 2 krewrite elim_eq_of_rel, rewrite (right_inv (k (f⁻¹ b) (f⁻¹ b'))),
-- have H1 : pathover (λz : B × B, Q z.1 z.2)
-- ((right_inv f b)⁻¹ ▸ (right_inv f b')⁻¹ ▸ q)
-- (prod_eq (right_inv f b) (right_inv f b')) q,
-- begin apply pathover_of_eq_tr, krewrite [prod_eq_inv,prod_eq_transport] end,
-- have H2 : square
-- (ap (λx : (Σz : B × B, Q z.1 z.2), class_of Q x.1.1)
-- (sigma_eq (prod_eq (right_inv f b) (right_inv f b')) H1))
-- (ap (λx : (Σz : B × B, Q z.1 z.2), class_of Q x.1.2)
-- (sigma_eq (prod_eq (right_inv f b) (right_inv f b')) H1))
-- (eq_of_rel Q ((right_inv f b)⁻¹ ▸ (right_inv f b')⁻¹ ▸ q))
-- (eq_of_rel Q q),
-- from
-- natural_square_tr (λw : (Σz : B × B, Q z.1 z.2), eq_of_rel Q w.2)
-- (sigma_eq (prod_eq (right_inv f b) (right_inv f b')) H1),
-- krewrite (ap_compose' (class_of Q)) at H2,
-- krewrite (ap_compose' (λz : B × B, z.1)) at H2,
-- rewrite sigma.ap_fst at H2, rewrite sigma_eq_fst at H2,
-- krewrite prod.ap_fst at H2, krewrite prod_eq_fst at H2,
-- krewrite (ap_compose' (class_of Q) (λx : (Σz : B × B, Q z.1 z.2), x.1.2)) at H2,
-- krewrite (ap_compose' (λz : B × B, z.2)) at H2,
-- rewrite sigma.ap_fst at H2, rewrite sigma_eq_fst at H2,
-- krewrite prod.ap_snd at H2, krewrite prod_eq_snd at H2,
-- apply H2 } },
-- { intro qa, induction qa with a a a' r,
-- { apply ap (class_of R), apply left_inv },
-- { apply eq_pathover, rewrite [ap_id,(ap_compose' (quotient.elim _ _))],
-- do 2 krewrite elim_eq_of_rel,
-- have H1 : pathover (λz : A × A, R z.1 z.2)
-- ((left_inv f a)⁻¹ ▸ (left_inv f a')⁻¹ ▸ r)
-- (prod_eq (left_inv f a) (left_inv f a')) r,
-- begin apply pathover_of_eq_tr, krewrite [prod_eq_inv,prod_eq_transport] end,
-- have H2 : square
-- (ap (λx : (Σz : A × A, R z.1 z.2), class_of R x.1.1)
-- (sigma_eq (prod_eq (left_inv f a) (left_inv f a')) H1))
-- (ap (λx : (Σz : A × A, R z.1 z.2), class_of R x.1.2)
-- (sigma_eq (prod_eq (left_inv f a) (left_inv f a')) H1))
-- (eq_of_rel R ((left_inv f a)⁻¹ ▸ (left_inv f a')⁻¹ ▸ r))
-- (eq_of_rel R r),
-- begin
-- exact
-- natural_square_tr (λw : (Σz : A × A, R z.1 z.2), eq_of_rel R w.2)
-- (sigma_eq (prod_eq (left_inv f a) (left_inv f a')) H1)
-- end,
-- krewrite (ap_compose' (class_of R)) at H2,
-- krewrite (ap_compose' (λz : A × A, z.1)) at H2,
-- rewrite sigma.ap_fst at H2, rewrite sigma_eq_fst at H2,
-- krewrite prod.ap_fst at H2, krewrite prod_eq_fst at H2,
-- krewrite (ap_compose' (class_of R) (λx : (Σz : A × A, R z.1 z.2), x.1.2)) at H2,
-- krewrite (ap_compose' (λz : A × A, z.2)) at H2,
-- rewrite sigma.ap_fst at H2, rewrite sigma_eq_fst at H2,
-- krewrite prod.ap_snd at H2, krewrite prod_eq_snd at H2,
-- have H3 :
-- (k (f⁻¹ (f a)) (f⁻¹ (f a')))⁻¹
-- ((right_inv f (f a))⁻¹ ▸ (right_inv f (f a'))⁻¹ ▸ k a a' r)
-- = (left_inv f a)⁻¹ ▸ (left_inv f a')⁻¹ ▸ r,
-- begin
-- rewrite [adj f a,adj f a',ap_inv',ap_inv'],
-- rewrite [-(tr_compose _ f (left_inv f a')⁻¹ (k a a' r)),
-- -(tr_compose _ f (left_inv f a)⁻¹)],
-- rewrite [-(fn_tr_eq_tr_fn (left_inv f a')⁻¹ (λx, k a x) r),
-- -(fn_tr_eq_tr_fn (left_inv f a)⁻¹
-- (λx, k x (f⁻¹ (f a')))),
-- left_inv (k _ _)]
-- end,
-- rewrite H3, apply H2 } }
-- end
end
section
variables {A : Type _} (R : A → A → Type _)
{B : Type _} (Q : B → B → Type _)
(f : A ≃ B) (k : Πa a' : A, R a a' ≃ Q (f a) (f a'))
include f k
/- This could also be proved using ua, but then it wouldn't compute -/
@[hott] protected def equiv : quotient R ≃ quotient Q :=
equiv.mk (quotient.functor R Q f (λa a', k a a')) (quotient.is_equiv _ _ _ _)
end
end quotient
end hott |
7f584f2f67c701fcd1b6925c10f33a188cb7eedf | 4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d | /tests/lean/run/simp7.lean | c4c64aadf4095dcbcd49ecc50202695bc5babcb8 | [
"Apache-2.0"
] | permissive | subfish-zhou/leanprover-zh_CN.github.io | 30b9fba9bd790720bd95764e61ae796697d2f603 | 8b2985d4a3d458ceda9361ac454c28168d920d3f | refs/heads/master | 1,689,709,967,820 | 1,632,503,056,000 | 1,632,503,056,000 | 409,962,097 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,407 | lean | def f (x : α) := x
theorem ex1 (a : α) (b : List α) : f (a::b = []) = False :=
by simp [f]
def length : List α → Nat
| [] => 0
| a::as => length as + 1
theorem ex2 (a b c : α) (as : List α) : length (a :: b :: as) > length as := by
simp [length]
apply Nat.lt.step
apply Nat.lt_succ_self
def fact : Nat → Nat
| 0 => 1
| x+1 => (x+1) * fact x
theorem ex3 : fact x > 0 := by
induction x with
| zero => decide
| succ x ih =>
simp [fact]
apply Nat.mul_pos
apply Nat.zero_lt_succ
apply ih
def head [Inhabited α] : List α → α
| [] => arbitrary
| a::_ => a
theorem ex4 [Inhabited α] (a : α) (as : List α) : head (a::as) = a :=
by simp [head]
def foo := 10
theorem ex5 (x : Nat) : foo + x = 10 + x := by
simp [foo]
done
def g (x : Nat) : Nat := do
let x := x
return x
theorem ex6 : g x = x := by
simp [g, bind, pure]
def f1 : StateM Nat Unit := do
modify fun x => g x
def f2 : StateM Nat Unit := do
let s ← get
set <| g s
theorem ex7 : f1 = f2 := by
simp [f1, f2, bind, StateT.bind, get, getThe, MonadStateOf.get, StateT.get, pure, set, StateT.set, modify, modifyGet, MonadStateOf.modifyGet, StateT.modifyGet]
def h (x : Nat) : Sum (Nat × Nat) Nat := Sum.inl (x, x)
def bla (x : Nat) :=
match h x with
| Sum.inl (y, z) => y + z
| Sum.inr _ => 0
theorem ex8 (x : Nat) : bla x = x + x := by
simp [bla, h]
|
ec1bf63dba8e18eb282420d55a9d2c03bf1cf17a | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Log.lean | 3076f8489a84a19c334b3df702e0268c8631111f | [
"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 | 4,403 | 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.Util.Sorry
import Lean.Message
namespace Lean
/--
The `MonadLog` interface for logging error messages.
-/
class MonadLog (m : Type → Type) extends MonadFileMap m where
/-- Return the current reference syntax being used to provide position information. -/
getRef : m Syntax
/-- The name of the file being processed. -/
getFileName : m String
/-- Return `true` if errors have been logged. -/
hasErrors : m Bool
/-- Log a new message. -/
logMessage : Message → m Unit
export MonadLog (getFileName logMessage)
instance (m n) [MonadLift m n] [MonadLog m] : MonadLog n where
getRef := liftM (MonadLog.getRef : m _)
getFileMap := liftM (getFileMap : m _)
getFileName := liftM (getFileName : m _)
hasErrors := liftM (MonadLog.hasErrors : m _)
logMessage := fun msg => liftM (logMessage msg : m _ )
variable [Monad m] [MonadLog m] [AddMessageContext m] [MonadOptions m]
/--
Return the position (as `String.pos`) associated with the current reference syntax (i.e., the syntax object returned by `getRef`.)
-/
def getRefPos : m String.Pos := do
let ref ← MonadLog.getRef
return ref.getPos?.getD 0
/--
Return the line and column numbers associated with the current reference syntax (i.e., the syntax object returned by `getRef`.)
-/
def getRefPosition : m Position := do
let fileMap ← getFileMap
return fileMap.toPosition (← getRefPos)
/-- If `warningAsError` is set to `true`, then warning messages are treated as errors. -/
register_builtin_option warningAsError : Bool := {
defValue := false
descr := "treat warnings as errors"
}
/--
Log the message `msgData` at the position provided by `ref` with the given `severity`.
If `getRef` has position information but `ref` does not, we use `getRef`.
We use the `fileMap` to find the line and column numbers for the error message.
-/
def logAt (ref : Syntax) (msgData : MessageData) (severity : MessageSeverity := MessageSeverity.error) : m Unit :=
unless severity == .error && msgData.hasSyntheticSorry do
let severity := if severity == .warning && warningAsError.get (← getOptions) then .error else severity
let ref := replaceRef ref (← MonadLog.getRef)
let pos := ref.getPos?.getD 0
let endPos := ref.getTailPos?.getD pos
let fileMap ← getFileMap
let msgData ← addMessageContext msgData
logMessage { fileName := (← getFileName), pos := fileMap.toPosition pos, endPos := fileMap.toPosition endPos, data := msgData, severity := severity }
/-- Log a new error message using the given message data. The position is provided by `ref`. -/
def logErrorAt (ref : Syntax) (msgData : MessageData) : m Unit :=
logAt ref msgData MessageSeverity.error
/-- Log a new warning message using the given message data. The position is provided by `ref`. -/
def logWarningAt [MonadOptions m] (ref : Syntax) (msgData : MessageData) : m Unit := do
logAt ref msgData .warning
/-- Log a new information message using the given message data. The position is provided by `ref`. -/
def logInfoAt (ref : Syntax) (msgData : MessageData) : m Unit :=
logAt ref msgData MessageSeverity.information
/-- Log a new error/warning/information message using the given message data and `severity`. The position is provided by `getRef`. -/
def log (msgData : MessageData) (severity : MessageSeverity := MessageSeverity.error): m Unit := do
let ref ← MonadLog.getRef
logAt ref msgData severity
/-- Log a new error message using the given message data. The position is provided by `getRef`. -/
def logError (msgData : MessageData) : m Unit :=
log msgData MessageSeverity.error
/-- Log a new warning message using the given message data. The position is provided by `getRef`. -/
def logWarning [MonadOptions m] (msgData : MessageData) : m Unit := do
log msgData (if warningAsError.get (← getOptions) then .error else .warning)
/-- Log a new information message using the given message data. The position is provided by `getRef`. -/
def logInfo (msgData : MessageData) : m Unit :=
log msgData MessageSeverity.information
/-- Log the error message "unknown declaration" -/
def logUnknownDecl (declName : Name) : m Unit :=
logError m!"unknown declaration '{declName}'"
end Lean
|
29f4c1d70ad0b2731ff5532d252805d174aa70ce | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/matrix/basic.lean | 73c91825d03149605d6b9fa8a1d1ba87f8837a9b | [
"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 | 13,337 | lean | /-
Copyright (c) 2018 Ellen Arlt. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin
Matrices
-/
import algebra.module algebra.pi_instances
import data.fintype.basic
universes u v w
def matrix (m n : Type u) [fintype m] [fintype n] (α : Type v) : Type (max u v) :=
m → n → α
namespace matrix
variables {l m n o : Type u} [fintype l] [fintype m] [fintype n] [fintype o]
variables {α : Type v}
section ext
variables {M N : matrix m n α}
theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N :=
⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩
@[ext] theorem ext : (∀ i j, M i j = N i j) → M = N :=
ext_iff.mp
end ext
def transpose (M : matrix m n α) : matrix n m α
| x y := M y x
localized "postfix `ᵀ`:1500 := matrix.transpose" in matrix
def col (w : m → α) : matrix m punit α
| x y := w x
def row (v : n → α) : matrix punit n α
| x y := v y
instance [inhabited α] : inhabited (matrix m n α) := pi.inhabited _
instance [has_add α] : has_add (matrix m n α) := pi.has_add
instance [add_semigroup α] : add_semigroup (matrix m n α) := pi.add_semigroup
instance [add_comm_semigroup α] : add_comm_semigroup (matrix m n α) := pi.add_comm_semigroup
instance [has_zero α] : has_zero (matrix m n α) := pi.has_zero
instance [add_monoid α] : add_monoid (matrix m n α) := pi.add_monoid
instance [add_comm_monoid α] : add_comm_monoid (matrix m n α) := pi.add_comm_monoid
instance [has_neg α] : has_neg (matrix m n α) := pi.has_neg
instance [add_group α] : add_group (matrix m n α) := pi.add_group
instance [add_comm_group α] : add_comm_group (matrix m n α) := pi.add_comm_group
@[simp] theorem zero_val [has_zero α] (i j) : (0 : matrix m n α) i j = 0 := rfl
@[simp] theorem neg_val [has_neg α] (M : matrix m n α) (i j) : (- M) i j = - M i j := rfl
@[simp] theorem add_val [has_add α] (M N : matrix m n α) (i j) : (M + N) i j = M i j + N i j := rfl
section diagonal
variables [decidable_eq n]
def diagonal [has_zero α] (d : n → α) : matrix n n α := λ i j, if i = j then d i else 0
@[simp] theorem diagonal_val_eq [has_zero α] {d : n → α} (i : n) : (diagonal d) i i = d i :=
by simp [diagonal]
@[simp] theorem diagonal_val_ne [has_zero α] {d : n → α} {i j : n} (h : i ≠ j) :
(diagonal d) i j = 0 := by simp [diagonal, h]
theorem diagonal_val_ne' [has_zero α] {d : n → α} {i j : n} (h : j ≠ i) :
(diagonal d) i j = 0 := diagonal_val_ne h.symm
@[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 :=
by simp [diagonal]; refl
section one
variables [has_zero α] [has_one α]
instance : has_one (matrix n n α) := ⟨diagonal (λ _, 1)⟩
@[simp] theorem diagonal_one : (diagonal (λ _, 1) : matrix n n α) = 1 := rfl
theorem one_val {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl
@[simp] theorem one_val_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_val_eq i
@[simp] theorem one_val_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 :=
diagonal_val_ne
theorem one_val_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 :=
diagonal_val_ne'
end one
end diagonal
@[simp] theorem diagonal_add [decidable_eq n] [add_monoid α] (d₁ d₂ : n → α) :
diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) :=
by ext i j; by_cases i = j; simp [h]
protected def mul [has_mul α] [add_comm_monoid α] (M : matrix l m α) (N : matrix m n α) :
matrix l n α :=
λ i k, finset.univ.sum (λ j, M i j * N j k)
localized "infixl ` ⬝ `:75 := matrix.mul" in matrix
theorem mul_val [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} :
(M ⬝ N) i k = finset.univ.sum (λ j, M i j * N j k) := rfl
local attribute [simp] mul_val
instance [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩
@[simp] theorem mul_eq_mul [has_mul α] [add_comm_monoid α] (M N : matrix n n α) :
M * N = M ⬝ N := rfl
theorem mul_val' [has_mul α] [add_comm_monoid α] {M N : matrix n n α} {i k} :
(M * N) i k = finset.univ.sum (λ j, M i j * N j k) := rfl
section semigroup
variables [semiring α]
protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) :
(L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) :=
by classical; funext i k;
simp [finset.mul_sum, finset.sum_mul, mul_assoc];
rw finset.sum_comm
instance : semigroup (matrix n n α) :=
{ mul_assoc := matrix.mul_assoc, ..matrix.has_mul }
end semigroup
@[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) :
-diagonal d = diagonal (λ i, -d i) :=
by ext i j; by_cases i = j; simp [h]
section semiring
variables [semiring α]
@[simp] protected theorem mul_zero (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 :=
by ext i j; simp
@[simp] protected theorem zero_mul (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 :=
by ext i j; simp
protected theorem mul_add (L : matrix m n α) (M N : matrix n o α) : L ⬝ (M + N) = L ⬝ M + L ⬝ N :=
by ext i j; simp [finset.sum_add_distrib, mul_add]
protected theorem add_mul (L M : matrix l m α) (N : matrix m n α) : (L + M) ⬝ N = L ⬝ N + M ⬝ N :=
by ext i j; simp [finset.sum_add_distrib, add_mul]
@[simp] theorem diagonal_mul [decidable_eq m]
(d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j :=
by simp; rw finset.sum_eq_single i; simp [diagonal_val_ne'] {contextual := tt}
@[simp] theorem mul_diagonal [decidable_eq n]
(d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j :=
by simp; rw finset.sum_eq_single j; simp {contextual := tt}
@[simp] protected theorem one_mul [decidable_eq m] (M : matrix m n α) : (1 : matrix m m α) ⬝ M = M :=
by ext i j; rw [← diagonal_one, diagonal_mul, one_mul]
@[simp] protected theorem mul_one [decidable_eq n] (M : matrix m n α) : M ⬝ (1 : matrix n n α) = M :=
by ext i j; rw [← diagonal_one, mul_diagonal, mul_one]
instance [decidable_eq n] : monoid (matrix n n α) :=
{ one_mul := matrix.one_mul,
mul_one := matrix.mul_one,
..matrix.has_one, ..matrix.semigroup }
instance [decidable_eq n] : semiring (matrix n n α) :=
{ mul_zero := matrix.mul_zero,
zero_mul := matrix.zero_mul,
left_distrib := matrix.mul_add,
right_distrib := matrix.add_mul,
..matrix.add_comm_monoid,
..matrix.monoid }
@[simp] theorem diagonal_mul_diagonal [decidable_eq n] (d₁ d₂ : n → α) :
(diagonal d₁) ⬝ (diagonal d₂) = diagonal (λ i, d₁ i * d₂ i) :=
by ext i j; by_cases i = j; simp [h]
theorem diagonal_mul_diagonal' [decidable_eq n] (d₁ d₂ : n → α) :
diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) :=
diagonal_mul_diagonal _ _
lemma is_add_monoid_hom_mul_left (M : matrix l m α) :
is_add_monoid_hom (λ x : matrix m n α, M ⬝ x) :=
{ to_is_add_hom := ⟨matrix.mul_add _⟩, map_zero := matrix.mul_zero _ }
lemma is_add_monoid_hom_mul_right (M : matrix m n α) :
is_add_monoid_hom (λ x : matrix l m α, x ⬝ M) :=
{ to_is_add_hom := ⟨λ _ _, matrix.add_mul _ _ _⟩, map_zero := matrix.zero_mul _ }
protected lemma sum_mul {β : Type*} (s : finset β) (f : β → matrix l m α)
(M : matrix m n α) : s.sum f ⬝ M = s.sum (λ a, f a ⬝ M) :=
(@finset.sum_hom _ _ _ _ _ s f (λ x, x ⬝ M)
/- This line does not type-check without `id` and `: _`. Lean did not recognize that two different
`add_monoid` instances were def-eq -/
(id (@is_add_monoid_hom_mul_right l _ _ _ _ _ _ _ M) : _)).symm
protected lemma mul_sum {β : Type*} (s : finset β) (f : β → matrix m n α)
(M : matrix l m α) : M ⬝ s.sum f = s.sum (λ a, M ⬝ f a) :=
(@finset.sum_hom _ _ _ _ _ s f (λ x, M ⬝ x)
/- This line does not type-check without `id` and `: _`. Lean did not recognize that two different
`add_monoid` instances were def-eq -/
(id (@is_add_monoid_hom_mul_left _ _ n _ _ _ _ _ M) : _)).symm
end semiring
section ring
variables [ring α]
@[simp] theorem neg_mul (M : matrix m n α) (N : matrix n o α) :
(-M) ⬝ N = -(M ⬝ N) := by ext; simp [matrix.mul]
@[simp] theorem mul_neg (M : matrix m n α) (N : matrix n o α) :
M ⬝ (-N) = -(M ⬝ N) := by ext; simp [matrix.mul]
end ring
instance [decidable_eq n] [ring α] : ring (matrix n n α) :=
{ ..matrix.semiring, ..matrix.add_comm_group }
instance [semiring α] : has_scalar α (matrix m n α) := pi.has_scalar
instance {β : Type w} [semiring α] [add_comm_monoid β] [semimodule α β] :
semimodule α (matrix m n β) := pi.semimodule _ _ _
instance {β : Type w} [ring α] [add_comm_group β] [module α β] :
module α (matrix m n β) := { .. matrix.semimodule }
@[simp] lemma smul_val [semiring α] (a : α) (A : matrix m n α) (i : m) (j : n) : (a • A) i j = a * A i j := rfl
section comm_semiring
variables [comm_semiring α]
lemma smul_eq_diagonal_mul [decidable_eq m] (M : matrix m n α) (a : α) :
a • M = diagonal (λ _, a) ⬝ M :=
by { ext, simp }
lemma smul_eq_mul_diagonal [decidable_eq n] (M : matrix m n α) (a : α) :
a • M = M ⬝ diagonal (λ _, a) :=
by { ext, simp [mul_comm] }
@[simp] lemma mul_smul (M : matrix m n α) (a : α) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N :=
begin
ext i j,
unfold matrix.mul has_scalar.smul,
rw finset.mul_sum,
congr,
ext,
ac_refl
end
@[simp] lemma smul_mul (M : matrix m n α) (a : α) (N : matrix n l α) : (a • M) ⬝ N = a • M ⬝ N :=
begin
ext i j,
unfold matrix.mul has_scalar.smul,
rw finset.mul_sum,
congr,
ext,
ac_refl
end
end comm_semiring
section semiring
variables [semiring α]
def vec_mul_vec (w : m → α) (v : n → α) : matrix m n α
| x y := w x * v y
def mul_vec (M : matrix m n α) (v : n → α) : m → α
| x := finset.univ.sum (λy:n, M x y * v y)
def vec_mul (v : m → α) (M : matrix m n α) : n → α
| y := finset.univ.sum (λx:m, v x * M x y)
instance mul_vec.is_add_monoid_hom_left (v : n → α) :
is_add_monoid_hom (λM:matrix m n α, mul_vec M v) :=
{ map_zero := by ext; simp [mul_vec]; refl,
map_add :=
begin
intros x y,
ext m,
rw pi.add_apply (mul_vec x v) (mul_vec y v) m,
simp [mul_vec, finset.sum_add_distrib, right_distrib]
end }
lemma mul_vec_diagonal [decidable_eq m] (v w : m → α) (x : m) :
mul_vec (diagonal v) w x = v x * w x :=
begin
transitivity,
refine finset.sum_eq_single x _ _,
{ assume b _ ne, simp [diagonal, ne.symm] },
{ simp },
{ rw [diagonal_val_eq] }
end
@[simp] lemma mul_vec_one [decidable_eq m] (v : m → α) : mul_vec 1 v = v :=
by { ext, rw [←diagonal_one, mul_vec_diagonal, one_mul] }
lemma vec_mul_vec_eq (w : m → α) (v : n → α) :
vec_mul_vec w v = (col w) ⬝ (row v) :=
by simp [matrix.mul]; refl
end semiring
section transpose
open_locale matrix
/--
Tell `simp` what the entries are in a transposed matrix.
Compare with `mul_val`, `diagonal_val_eq`, etc.
-/
@[simp] lemma transpose_val (M : matrix m n α) (i j) : M.transpose j i = M i j := rfl
@[simp] lemma transpose_transpose (M : matrix m n α) :
Mᵀᵀ = M :=
by ext; refl
@[simp] lemma transpose_zero [has_zero α] : (0 : matrix m n α)ᵀ = 0 :=
by ext i j; refl
@[simp] lemma transpose_one [decidable_eq n] [has_zero α] [has_one α] : (1 : matrix n n α)ᵀ = 1 :=
begin
ext i j,
unfold has_one.one transpose,
by_cases i = j,
{ simp only [h, diagonal_val_eq] },
{ simp only [diagonal_val_ne h, diagonal_val_ne (λ p, h (symm p))] }
end
@[simp] lemma transpose_add [has_add α] (M : matrix m n α) (N : matrix m n α) :
(M + N)ᵀ = Mᵀ + Nᵀ :=
by { ext i j, simp }
@[simp] lemma transpose_mul [comm_ring α] (M : matrix m n α) (N : matrix n l α) :
(M ⬝ N)ᵀ = Nᵀ ⬝ Mᵀ :=
begin
ext i j,
unfold matrix.mul transpose,
congr,
ext,
ac_refl
end
@[simp] lemma transpose_smul [comm_ring α] (c : α)(M : matrix m n α) :
(c • M)ᵀ = c • Mᵀ :=
by { ext i j, refl }
@[simp] lemma transpose_neg [comm_ring α] (M : matrix m n α) :
(- M)ᵀ = - Mᵀ :=
by ext i j; refl
end transpose
def minor (A : matrix m n α) (row : l → m) (col : o → n) : matrix l o α :=
λ i j, A (row i) (col j)
@[reducible]
def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin l) α :=
minor A id (fin.cast_add r)
@[reducible]
def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin r) α :=
minor A id (fin.nat_add l)
@[reducible]
def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin u) (fin n) α :=
minor A (fin.cast_add d) id
@[reducible]
def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin d) (fin n) α :=
minor A (fin.nat_add u) id
@[reducible]
def sub_up_right {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) :
matrix (fin u) (fin r) α :=
sub_up (sub_right A)
@[reducible]
def sub_down_right {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) :
matrix (fin d) (fin r) α :=
sub_down (sub_right A)
@[reducible]
def sub_up_left {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) :
matrix (fin u) (fin (l)) α :=
sub_up (sub_left A)
@[reducible]
def sub_down_left {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) :
matrix (fin d) (fin (l)) α :=
sub_down (sub_left A)
end matrix
|
4171cd41fb40bf5c0508bc011e916cb9e33a1e4d | e6b8240a90527fd55d42d0ec6649253d5d0bd414 | /src/topology/instances/complex.lean | b01ecfbb36577a6f40ce11cd6b3a216e887240e2 | [
"Apache-2.0"
] | permissive | mattearnshaw/mathlib | ac90f9fb8168aa642223bea3ffd0286b0cfde44f | d8dc1445cf8a8c74f8df60b9f7a1f5cf10946666 | refs/heads/master | 1,606,308,351,137 | 1,576,594,130,000 | 1,576,594,130,000 | 228,666,195 | 0 | 0 | Apache-2.0 | 1,576,603,094,000 | 1,576,603,093,000 | null | UTF-8 | Lean | false | false | 6,075 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Topology of the complex numbers.
-/
import data.complex.basic topology.metric_space.basic topology.instances.real
noncomputable theory
open filter metric
open_locale topological_space
namespace complex
-- TODO(Mario): these proofs are all copied from analysis/real. Generalize
-- to normed fields
instance : metric_space ℂ :=
{ dist := λx y, (x - y).abs,
dist_self := by simp [abs_zero],
eq_of_dist_eq_zero := by simp [add_neg_eq_zero],
dist_comm := assume x y, complex.abs_sub _ _,
dist_triangle := assume x y z, complex.abs_sub_le _ _ _ }
theorem dist_eq (x y : ℂ) : dist x y = (x - y).abs := rfl
theorem uniform_continuous_add : uniform_continuous (λp : ℂ × ℂ, p.1 + p.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩
theorem uniform_continuous_neg : uniform_continuous (@has_neg.neg ℂ _) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h,
by rw dist_comm at h; simpa [dist_eq] using h⟩
instance : uniform_add_group ℂ :=
uniform_add_group.mk' uniform_continuous_add uniform_continuous_neg
instance : topological_add_group ℂ := by apply_instance -- short-circuit type class inference
lemma uniform_continuous_inv (s : set ℂ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) :
uniform_continuous (λp:s, p.1⁻¹) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in
⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩
lemma uniform_continuous_abs : uniform_continuous (abs : ℂ → ℝ) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _)⟩
lemma continuous_abs : continuous (abs : ℂ → ℝ) :=
uniform_continuous_abs.continuous
lemma tendsto_inv {r : ℂ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) :=
by rw ← abs_pos at r0; exact
tendsto_of_uniform_continuous_subtype
(uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h))
(mem_nhds_sets (continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0))
lemma continuous_inv : continuous (λa:{r:ℂ // r ≠ 0}, a.val⁻¹) :=
continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩,
tendsto.comp (tendsto_inv hr) (continuous_iff_continuous_at.mp continuous_subtype_val _)
lemma continuous.inv {α} [topological_space α] {f : α → ℂ} (h : ∀a, f a ≠ 0) (hf : continuous f) :
continuous (λa, (f a)⁻¹) :=
show continuous ((has_inv.inv ∘ @subtype.val ℂ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩),
from continuous_inv.comp (continuous_subtype_mk _ hf)
lemma uniform_continuous_mul_const {x : ℂ} : uniform_continuous ((*) x) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, begin
cases no_top (abs x) with y xy,
have y0 := lt_of_le_of_lt (abs_nonneg _) xy,
refine ⟨_, div_pos ε0 y0, λ a b h, _⟩,
rw [dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)],
exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0
end
lemma uniform_continuous_mul (s : set (ℂ × ℂ))
{r₁ r₂ : ℝ} (H : ∀ x ∈ s, abs (x : ℂ × ℂ).1 < r₁ ∧ abs x.2 < r₂) :
uniform_continuous (λp:s, p.1.1 * p.1.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h,
let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩
protected lemma continuous_mul : continuous (λp : ℂ × ℂ, p.1 * p.2) :=
continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩,
tendsto_of_uniform_continuous_subtype
(uniform_continuous_mul
({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1})
(λ x, id))
(mem_nhds_sets
(is_open_prod
(continuous_abs _ $ is_open_gt' (abs a₁ + 1))
(continuous_abs _ $ is_open_gt' (abs a₂ + 1)))
⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩)
local attribute [semireducible] real.le
lemma uniform_continuous_re : uniform_continuous re :=
metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε, ε0, λ _ _, lt_of_le_of_lt (abs_re_le_abs _)⟩)
lemma continuous_re : continuous re := uniform_continuous_re.continuous
lemma uniform_continuous_im : uniform_continuous im :=
metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε, ε0, λ _ _, lt_of_le_of_lt (abs_im_le_abs _)⟩)
lemma continuous_im : continuous im := uniform_continuous_im.continuous
lemma uniform_continuous_of_real : uniform_continuous of_real :=
metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε, ε0, λ _ _,
by rw [real.dist_eq, complex.dist_eq, of_real_eq_coe, of_real_eq_coe, ← of_real_sub, abs_of_real];
exact id⟩)
lemma continuous_of_real : continuous of_real := uniform_continuous_of_real.continuous
instance : topological_ring ℂ :=
{ continuous_mul := complex.continuous_mul, ..complex.topological_add_group }
instance : topological_semiring ℂ := by apply_instance -- short-circuit type class inference
def real_prod_homeo : homeomorph ℂ (ℝ × ℝ) :=
{ to_equiv := real_prod_equiv,
continuous_to_fun := continuous_re.prod_mk continuous_im,
continuous_inv_fun := show continuous (λ p : ℝ × ℝ, complex.mk p.1 p.2),
by simp only [mk_eq_add_mul_I]; exact
(continuous_of_real.comp continuous_fst).add
((continuous_of_real.comp continuous_snd).mul continuous_const) }
instance : proper_space ℂ :=
⟨λx r, begin
refine real_prod_homeo.symm.compact_preimage.1
(compact_of_is_closed_subset
(compact_prod _ _ (proper_space.compact_ball x.re r) (proper_space.compact_ball x.im r))
(continuous_iff_is_closed.1 real_prod_homeo.symm.continuous _ is_closed_ball) _),
exact λ p h, ⟨
le_trans (abs_re_le_abs (⟨p.1, p.2⟩ - x)) h,
le_trans (abs_im_le_abs (⟨p.1, p.2⟩ - x)) h⟩
end⟩
end complex
|
019b71bc33cb05d356023a48c429375df718be54 | 432d948a4d3d242fdfb44b81c9e1b1baacd58617 | /src/data/polynomial/iterated_deriv.lean | 47483af41a068bc8602b6095cd1ad5d1cffbf3ef | [
"Apache-2.0"
] | permissive | JLimperg/aesop3 | 306cc6570c556568897ed2e508c8869667252e8a | a4a116f650cc7403428e72bd2e2c4cda300fe03f | refs/heads/master | 1,682,884,916,368 | 1,620,320,033,000 | 1,620,320,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,059 | lean | /-
Copyright (c) 2020 Jujian Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jujian Zhang
-/
import data.polynomial.derivative
import logic.function.iterate
import data.finset.intervals
import tactic.ring
import tactic.linarith
/-!
# Theory of iterated derivative
We define and prove some lemmas about iterated (formal) derivative for polynomials over a semiring.
-/
noncomputable theory
open finset nat polynomial
open_locale big_operators
namespace polynomial
universes u
variable {R : Type u}
section semiring
variables [semiring R] (r : R) (f p q : polynomial R) (n k : ℕ)
/-- `iterated_deriv f n` is the `n`-th formal derivative of the polynomial `f` -/
def iterated_deriv : polynomial R := derivative ^[n] f
@[simp] lemma iterated_deriv_zero_right : iterated_deriv f 0 = f := rfl
lemma iterated_deriv_succ : iterated_deriv f (n + 1) = (iterated_deriv f n).derivative :=
by rw [iterated_deriv, iterated_deriv, function.iterate_succ']
@[simp] lemma iterated_deriv_zero_left : iterated_deriv (0 : polynomial R) n = 0 :=
begin
induction n with n hn,
{ exact iterated_deriv_zero_right _ },
{ rw [iterated_deriv_succ, hn, derivative_zero] },
end
@[simp] lemma iterated_deriv_add :
iterated_deriv (p + q) n = iterated_deriv p n + iterated_deriv q n :=
begin
induction n with n ih,
{ simp only [iterated_deriv_zero_right], },
{ simp only [iterated_deriv_succ, ih, derivative_add] }
end
@[simp] lemma iterated_deriv_smul : iterated_deriv (r • p) n = r • iterated_deriv p n :=
begin
induction n with n ih,
{ simp only [iterated_deriv_zero_right] },
{ simp only [iterated_deriv_succ, ih, derivative_smul] }
end
@[simp] lemma iterated_deriv_X_zero : iterated_deriv (X : polynomial R) 0 = X :=
by simp only [iterated_deriv_zero_right]
@[simp] lemma iterated_deriv_X_one : iterated_deriv (X : polynomial R) 1 = 1 :=
by simp only [iterated_deriv, derivative_X, function.iterate_one]
@[simp] lemma iterated_deriv_X (h : 1 < n) : iterated_deriv (X : polynomial R) n = 0 :=
begin
induction n with n ih,
{ exfalso, exact not_lt_zero 1 h},
{ simp only [iterated_deriv_succ],
by_cases H : n = 1,
{ rw H, simp only [iterated_deriv_X_one, derivative_one] },
{ replace h : 1 < n := array.push_back_idx h (ne.symm H),
rw ih h, simp only [derivative_zero] } }
end
@[simp] lemma iterated_deriv_C_zero : iterated_deriv (C r) 0 = C r :=
by simp only [iterated_deriv_zero_right]
@[simp] lemma iterated_deriv_C (h : 0 < n) : iterated_deriv (C r) n = 0 :=
begin
induction n with n ih,
{ exfalso, exact nat.lt_asymm h h },
{ by_cases H : n = 0,
{ rw [iterated_deriv_succ, H], simp only [iterated_deriv_C_zero, derivative_C]},
{ replace h : 0 < n := nat.pos_of_ne_zero H,
rw [iterated_deriv_succ, ih h], simp only [derivative_zero] } }
end
@[simp] lemma iterated_deriv_one_zero : iterated_deriv (1 : polynomial R) 0 = 1 :=
by simp only [iterated_deriv_zero_right]
@[simp] lemma iterated_deriv_one : 0 < n → iterated_deriv (1 : polynomial R) n = 0 := λ h,
begin
have eq1 : (1 : polynomial R) = C 1 := by simp only [ring_hom.map_one],
rw eq1, exact iterated_deriv_C _ _ h,
end
end semiring
section ring
variables [ring R] (p q : polynomial R) (n : ℕ)
@[simp] lemma iterated_deriv_neg : iterated_deriv (-p) n = - iterated_deriv p n :=
begin
induction n with n ih,
{ simp only [iterated_deriv_zero_right] },
{ simp only [iterated_deriv_succ, ih, derivative_neg] }
end
@[simp] lemma iterated_deriv_sub :
iterated_deriv (p - q) n = iterated_deriv p n - iterated_deriv q n :=
by rw [sub_eq_add_neg, iterated_deriv_add, iterated_deriv_neg, ←sub_eq_add_neg]
end ring
section comm_semiring
variable [comm_semiring R]
variables (f p q : polynomial R) (n k : ℕ)
lemma coeff_iterated_deriv_as_prod_Ico :
∀ m : ℕ, (iterated_deriv f k).coeff m = (∏ i in Ico m.succ (m + k.succ), i) * (f.coeff (m+k)) :=
begin
induction k with k ih,
{ simp only [add_zero, forall_const, one_mul, Ico.self_eq_empty, eq_self_iff_true,
iterated_deriv_zero_right, prod_empty] },
{ intro m, rw [iterated_deriv_succ, coeff_derivative, ih (m+1), mul_right_comm],
apply congr_arg2,
{ have set_eq : (Ico m.succ (m + k.succ.succ)) = (Ico (m + 1).succ (m + 1 + k.succ)) ∪ {m+1},
{ rw [union_comm, ←insert_eq, Ico.insert_succ_bot, add_succ, add_succ, add_succ _ k,
←succ_eq_add_one, succ_add],
rw succ_eq_add_one,
linarith },
rw [set_eq, prod_union],
apply congr_arg2,
{ refl },
{ simp only [prod_singleton], norm_cast },
{ simp only [succ_pos', disjoint_singleton, and_true, lt_add_iff_pos_right, not_le, Ico.mem],
exact lt_add_one (m + 1) } },
{ exact congr_arg _ (succ_add m k) } },
end
lemma coeff_iterated_deriv_as_prod_range :
∀ m : ℕ, (iterated_deriv f k).coeff m = f.coeff (m + k) * (∏ i in range k, ↑(m + k - i)) :=
begin
induction k with k ih,
{ simp },
intro m,
calc (f.iterated_deriv k.succ).coeff m
= f.coeff (m + k.succ) * (∏ i in range k, ↑(m + k.succ - i)) * (m + 1) :
by rw [iterated_deriv_succ, coeff_derivative, ih m.succ, succ_add, add_succ]
... = f.coeff (m + k.succ) * (∏ i in range k, ↑(m + k.succ - i)) * ↑(m + 1) :
by push_cast
... = f.coeff (m + k.succ) * (∏ i in range k.succ, ↑(m + k.succ - i)) :
by rw [prod_range_succ, nat.add_sub_assoc k.le_succ, succ_sub le_rfl, nat.sub_self, mul_assoc]
end
lemma iterated_deriv_eq_zero_of_nat_degree_lt (h : f.nat_degree < n) : iterated_deriv f n = 0 :=
begin
ext m,
rw [coeff_iterated_deriv_as_prod_range, coeff_zero, coeff_eq_zero_of_nat_degree_lt, zero_mul],
linarith
end
lemma iterated_deriv_mul :
iterated_deriv (p * q) n =
∑ k in range n.succ,
(C (n.choose k : R)) * iterated_deriv p (n - k) * iterated_deriv q k :=
begin
induction n with n IH,
{ simp },
calc (p * q).iterated_deriv n.succ
= (∑ (k : ℕ) in range n.succ,
C ↑(n.choose k) * p.iterated_deriv (n - k) * q.iterated_deriv k).derivative :
by rw [iterated_deriv_succ, IH]
... = ∑ (k : ℕ) in range n.succ,
C ↑(n.choose k) * p.iterated_deriv (n - k + 1) * q.iterated_deriv k +
∑ (k : ℕ) in range n.succ,
C ↑(n.choose k) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) :
by simp_rw [derivative_sum, derivative_mul, derivative_C, zero_mul, zero_add,
iterated_deriv_succ, sum_add_distrib]
... = (∑ (k : ℕ) in range n.succ,
C ↑(n.choose k.succ) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) +
C ↑1 * p.iterated_deriv n.succ * q.iterated_deriv 0) +
∑ (k : ℕ) in range n.succ,
C ↑(n.choose k) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) : _
... = ∑ (k : ℕ) in range n.succ,
C ↑(n.choose k) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) +
∑ (k : ℕ) in range n.succ,
C ↑(n.choose k.succ) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) +
C ↑1 * p.iterated_deriv n.succ * q.iterated_deriv 0 :
by ring
... = ∑ (i : ℕ) in range n.succ,
C ↑((n+1).choose (i+1)) * p.iterated_deriv (n + 1 - (i+1)) * q.iterated_deriv (i+1) +
C ↑1 * p.iterated_deriv n.succ * q.iterated_deriv 0 :
by simp_rw [choose_succ_succ, succ_sub_succ, cast_add, C.map_add, add_mul, sum_add_distrib]
... = ∑ (k : ℕ) in range n.succ.succ,
C ↑(n.succ.choose k) * p.iterated_deriv (n.succ - k) * q.iterated_deriv k :
by rw [sum_range_succ' _ n.succ, choose_zero_right, nat.sub_zero],
congr,
refine (sum_range_succ' _ _).trans (congr_arg2 (+) _ _),
{ rw [sum_range_succ, nat.choose_succ_self, cast_zero, C.map_zero, zero_mul, zero_mul, add_zero],
refine sum_congr rfl (λ k hk, _),
rw mem_range at hk,
congr,
rw [← nat.sub_add_comm (nat.succ_le_of_lt hk), nat.succ_sub_succ] },
{ rw [choose_zero_right, nat.sub_zero] },
end
end comm_semiring
end polynomial
|
e429d3d05a67313065cbbcf4b0651379bbe46aa2 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/nat_bug3.lean | 7c6dcb50a1ace57ab02092eea4475ac6a89300c1 | [
"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 | 732 | lean | import logic
open eq.ops
namespace experiment
inductive nat : Type :=
| zero : nat
| succ : nat → nat
namespace nat
definition plus (x y : nat) : nat
:= nat.rec x (λn r, succ r) y
definition to_nat [coercion] (n : num) : nat
:= num.rec zero (λn, pos_num.rec (succ zero) (λn r, plus r (plus r (succ zero))) (λn r, plus r r) n) n
definition add (x y : nat) : nat
:= plus x y
constant le : nat → nat → Prop
infixl `+` := add
infix `≤` := le
axiom add_one (n:nat) : n + (succ zero) = succ n
axiom add_le_right_inv {n m k : nat} (H : n + k ≤ m + k) : n ≤ m
theorem succ_le_cancel {n m : nat} (H : succ n ≤ succ m) : n ≤ m
:= add_le_right_inv ((add_one m)⁻¹ ▸ (add_one n)⁻¹ ▸ H)
end nat
end experiment
|
0755ea4b4ad8cc5fffe1889d0f5ea80ed4d1a8f1 | 4fa161becb8ce7378a709f5992a594764699e268 | /src/tactic/cancel_denoms.lean | 7f0ad46e2d2fd3929ba05d5d44eace607d5910b9 | [
"Apache-2.0"
] | permissive | laughinggas/mathlib | e4aa4565ae34e46e834434284cb26bd9d67bc373 | 86dcd5cda7a5017c8b3c8876c89a510a19d49aad | refs/heads/master | 1,669,496,232,688 | 1,592,831,995,000 | 1,592,831,995,000 | 274,155,979 | 0 | 0 | Apache-2.0 | 1,592,835,190,000 | 1,592,835,189,000 | null | UTF-8 | Lean | false | false | 9,948 | lean | /-
Copyright (c) 2020 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import data.rat.meta_defs
import tactic.norm_num
import data.tree
import meta.expr
/-!
# A tactic for canceling numeric denominators
This file defines tactics that cancel numeric denominators from field expressions.
As an example, we want to transform a comparison `5*(a/3 + b/4) < c/3` into the equivalent
`5*(4*a + 3*b) < 4*c`.
See also the `field_simp` tactic, which tries to do a similar simplification for field expressions.
The tactics are not related: `field_simp` handles non-numeral denominators, but has limited support
for numerals; `field_simp` does not multiply by new terms to cancel denominators, it just
produces an equal expression.
## Implementation notes
The tooling here was originally written for `linarith`, not intended as an interactive tactic.
The interactive version has been split off because it is sometimes convenient to use on its own.
There are likely some rough edges to it.
Improving this tactic would be a good project for someone interested in learning tactic programming.
-/
namespace cancel_factors
/-! ### Lemmas used in the procedure -/
lemma mul_subst {α} [comm_ring α] {n1 n2 k e1 e2 t1 t2 : α} (h1 : n1 * e1 = t1) (h2 : n2 * e2 = t2)
(h3 : n1*n2 = k) : k * (e1 * e2) = t1 * t2 :=
have h3 : n1 * n2 = k, from h3,
by rw [←h3, mul_comm n1, mul_assoc n2, ←mul_assoc n1, h1, ←mul_assoc n2, mul_comm n2, mul_assoc, h2]
lemma div_subst {α} [field α] {n1 n2 k e1 e2 t1 : α} (h1 : n1 * e1 = t1) (h2 : n2 / e2 = 1)
(h3 : n1*n2 = k) : k * (e1 / e2) = t1 :=
by rw [←h3, mul_assoc, mul_div_comm, h2, ←mul_assoc, h1, mul_comm, one_mul]
lemma cancel_factors_eq_div {α} [field α] {n e e' : α} (h : n*e = e') (h2 : n ≠ 0) :
e = e' / n :=
eq_div_of_mul_eq _ _ h2 $ by rwa mul_comm at h
lemma add_subst {α} [ring α] {n e1 e2 t1 t2 : α} (h1 : n * e1 = t1) (h2 : n * e2 = t2) :
n * (e1 + e2) = t1 + t2 := by simp [left_distrib, *]
lemma sub_subst {α} [ring α] {n e1 e2 t1 t2 : α} (h1 : n * e1 = t1) (h2 : n * e2 = t2) :
n * (e1 - e2) = t1 - t2 := by simp [left_distrib, *, sub_eq_add_neg]
lemma neg_subst {α} [ring α] {n e t : α} (h1 : n * e = t) : n * (-e) = -t := by simp *
lemma cancel_factors_lt {α} [linear_ordered_field α] {a b ad bd a' b' gcd : α} (ha : ad*a = a')
(hb : bd*b = b') (had : 0 < ad) (hbd : 0 < bd) (hgcd : 0 < gcd) :
a < b = ((1/gcd)*(bd*a') < (1/gcd)*(ad*b')) :=
begin
rw [mul_lt_mul_left, ←ha, ←hb, ←mul_assoc, ←mul_assoc, mul_comm bd, mul_lt_mul_left],
exact mul_pos had hbd,
exact one_div_pos_of_pos hgcd
end
lemma cancel_factors_le {α} [linear_ordered_field α] {a b ad bd a' b' gcd : α} (ha : ad*a = a')
(hb : bd*b = b') (had : 0 < ad) (hbd : 0 < bd) (hgcd : 0 < gcd) :
a ≤ b = ((1/gcd)*(bd*a') ≤ (1/gcd)*(ad*b')) :=
begin
rw [mul_le_mul_left, ←ha, ←hb, ←mul_assoc, ←mul_assoc, mul_comm bd, mul_le_mul_left],
exact mul_pos had hbd,
exact one_div_pos_of_pos hgcd
end
lemma cancel_factors_eq {α} [linear_ordered_field α] {a b ad bd a' b' gcd : α} (ha : ad*a = a')
(hb : bd*b = b') (had : 0 < ad) (hbd : 0 < bd) (hgcd : 0 < gcd) :
a = b = ((1/gcd)*(bd*a') = (1/gcd)*(ad*b')) :=
begin
rw [←ha, ←hb, ←mul_assoc bd, ←mul_assoc ad, mul_comm bd],
ext, split,
{ rintro rfl, refl },
{ intro h,
simp only [←mul_assoc] at h,
refine eq_of_mul_eq_mul_left (mul_ne_zero _ _) h,
apply mul_ne_zero, apply div_ne_zero,
all_goals {apply ne_of_gt; assumption <|> exact zero_lt_one}}
end
open tactic expr
/-! ### Computing cancelation factors -/
open tree
/--
`find_cancel_factor e` produces a natural number `n`, such that multiplying `e` by `n` will
be able to cancel all the numeric denominators in `e`. The returned `tree` describes how to
distribute the value `n` over products inside `e`.
-/
meta def find_cancel_factor : expr → ℕ × tree ℕ
| `(%%e1 + %%e2) :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, lcm := v1.lcm v2 in
(lcm, node lcm t1 t2)
| `(%%e1 - %%e2) :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, lcm := v1.lcm v2 in
(lcm, node lcm t1 t2)
| `(%%e1 * %%e2) :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, pd := v1*v2 in
(pd, node pd t1 t2)
| `(%%e1 / %%e2) :=
match e2.to_nonneg_rat with
| some q := let (v1, t1) := find_cancel_factor e1, n := v1.lcm q.num.nat_abs in
(n, node n t1 (node q.num.nat_abs tree.nil tree.nil))
| none := (1, node 1 tree.nil tree.nil)
end
| `(-%%e) := find_cancel_factor e
| _ := (1, node 1 tree.nil tree.nil)
/--
`mk_prod_prf n tr e` produces a proof of `n*e = e'`, where numeric denominators have been
canceled in `e'`, distributing `n` proportionally according to `tr`.
-/
meta def mk_prod_prf : ℕ → tree ℕ → expr → tactic expr
| v (node _ lhs rhs) `(%%e1 + %%e2) :=
do v1 ← mk_prod_prf v lhs e1, v2 ← mk_prod_prf v rhs e2, mk_app ``add_subst [v1, v2]
| v (node _ lhs rhs) `(%%e1 - %%e2) :=
do v1 ← mk_prod_prf v lhs e1, v2 ← mk_prod_prf v rhs e2, mk_app ``sub_subst [v1, v2]
| v (node n lhs@(node ln _ _) rhs) `(%%e1 * %%e2) :=
do tp ← infer_type e1, v1 ← mk_prod_prf ln lhs e1, v2 ← mk_prod_prf (v/ln) rhs e2,
ln' ← tp.of_nat ln, vln' ← tp.of_nat (v/ln), v' ← tp.of_nat v,
ntp ← to_expr ``(%%ln' * %%vln' = %%v'),
(_, npf) ← solve_aux ntp `[norm_num, done],
mk_app ``mul_subst [v1, v2, npf]
| v (node n lhs rhs@(node rn _ _)) `(%%e1 / %%e2) :=
do tp ← infer_type e1, v1 ← mk_prod_prf (v/rn) lhs e1,
rn' ← tp.of_nat rn, vrn' ← tp.of_nat (v/rn), n' ← tp.of_nat n, v' ← tp.of_nat v,
ntp ← to_expr ``(%%rn' / %%e2 = 1),
(_, npf) ← solve_aux ntp `[norm_num, done],
ntp2 ← to_expr ``(%%vrn' * %%n' = %%v'),
(_, npf2) ← solve_aux ntp2 `[norm_num, done],
mk_app ``div_subst [v1, npf, npf2]
| v t `(-%%e) := do v' ← mk_prod_prf v t e, mk_app ``neg_subst [v']
| v _ e :=
do tp ← infer_type e,
v' ← tp.of_nat v,
e' ← to_expr ``(%%v' * %%e),
mk_app `eq.refl [e']
/--
Given `e`, a term with rational division, produces a natural number `n` and a proof of `n*e = e'`,
where `e'` has no division. Assumes "well-behaved" division.
-/
meta def derive (e : expr) : tactic (ℕ × expr) :=
let (n, t) := find_cancel_factor e in
prod.mk n <$> mk_prod_prf n t e <|>
fail!"cancel_factors.derive failed to normalize {e}. Are you sure this is well-behaved division?"
/--
Given `e`, a term with rational divison, produces a natural number `n` and a proof of `e = e' / n`,
where `e'` has no divison. Assumes "well-behaved" division.
-/
meta def derive_div (e : expr) : tactic (ℕ × expr) :=
do (n, p) ← derive e,
tp ← infer_type e,
n' ← tp.of_nat n, tgt ← to_expr ``(%%n' ≠ 0),
(_, pn) ← solve_aux tgt `[norm_num, done],
infer_type p >>= trace, infer_type pn >>= trace,
prod.mk n <$> mk_mapp ``cancel_factors_eq_div [none, none, n', none, none, p, pn]
/--
`find_comp_lemma e` arranges `e` in the form `lhs R rhs`, where `R ∈ {<, ≤, =}`, and returns
`lhs`, `rhs`, and the `cancel_factors` lemma corresponding to `R`.
-/
meta def find_comp_lemma : expr → option (expr × expr × name)
| `(%%a < %%b) := (a, b, ``cancel_factors_lt)
| `(%%a ≤ %%b) := (a, b, ``cancel_factors_le)
| `(%%a = %%b) := (a, b, ``cancel_factors_eq)
| `(%%a ≥ %%b) := (b, a, ``cancel_factors_le)
| `(%%a > %%b) := (b, a, ``cancel_factors_lt)
| _ := none
/--
`cancel_denominators_in_type h` assumes that `h` is of the form `lhs R rhs`,
where `R ∈ {<, ≤, =, ≥, >}`.
It produces an expression `h'` of the form `lhs' R rhs'` and a proof that `h = h'`.
Numeric denominators have been canceled in `lhs'` and `rhs'`.
-/
meta def cancel_denominators_in_type (h : expr) : tactic (expr × expr) :=
do some (lhs, rhs, lem) ← return $ find_comp_lemma h | fail "cannot kill factors",
(al, lhs_p) ← derive lhs,
(ar, rhs_p) ← derive rhs,
let gcd := al.gcd ar,
tp ← infer_type lhs,
al ← tp.of_nat al,
ar ← tp.of_nat ar,
gcd ← tp.of_nat gcd,
al_pos ← to_expr ``(0 < %%al),
ar_pos ← to_expr ``(0 < %%ar),
gcd_pos ← to_expr ``(0 < %%gcd),
(_, al_pos) ← solve_aux al_pos `[norm_num, done],
(_, ar_pos) ← solve_aux ar_pos `[norm_num, done],
(_, gcd_pos) ← solve_aux gcd_pos `[norm_num, done],
pf ← mk_app lem [lhs_p, rhs_p, al_pos, ar_pos, gcd_pos],
pf_tp ← infer_type pf,
return ((find_comp_lemma pf_tp).elim (default _) (prod.fst ∘ prod.snd), pf)
end cancel_factors
/-! ### Interactive version -/
setup_tactic_parser
open tactic expr cancel_factors
/--
`cancel_denoms` attempts to remove numerals from the denominators of fractions.
It works on propositions that are field-valued inequalities.
```lean
variables {α : Type} [linear_ordered_field α] (a b c : α)
example (h : a / 5 + b / 4 < c) : 4*a + 5*b < 20*c :=
begin
cancel_denoms at h,
exact h
end
example (h : a > 0) : a / 5 > 0 :=
begin
cancel_denoms,
exact h
end
```
See also the `field_simp` tactic, which tries to do a similar simplification for field expressions.
The tactics are not related: `field_simp` handles non-numeral denominators, but has limited support
for numerals; `field_simp` does not multiply by new terms to cancel denominators, it just
produces an equal expression.
-/
meta def tactic.interactive.cancel_denoms (l : parse location) : tactic unit :=
do locs ← l.get_locals,
tactic.replace_at cancel_denominators_in_type locs l.include_goal >>= guardb
<|> fail "failed to cancel any denominators",
tactic.interactive.norm_num [simp_arg_type.symm_expr ``(mul_assoc)] l
add_tactic_doc
{ name := "cancel_denoms",
category := doc_category.tactic,
decl_names := [`tactic.interactive.cancel_denoms],
tags := ["simplification"] }
|
a993b087bdf7124cbaa9a6f82a2b3305769e189c | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/group_theory/free_group.lean | f4afcc7a48a1dd0ca3f796d28d49b5ca64272420 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 30,840 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
Free groups as a quotient over the reduction relation `a * x * x⁻¹ * b = a * b`.
First we introduce the one step reduction relation
`free_group.red.step`: w * x * x⁻¹ * v ~> w * v
its reflexive transitive closure:
`free_group.red.trans`
and proof that its join is an equivalence relation.
Then we introduce `free_group α` as a quotient over `free_group.red.step`.
-/
import logic.relation
import algebra.group algebra.group_power
import data.fintype data.list.basic
import group_theory.subgroup
open relation
universes u v w
variables {α : Type u}
local attribute [simp] list.append_eq_has_append
namespace free_group
variables {L L₁ L₂ L₃ L₄ : list (α × bool)}
/-- Reduction step: `w * x * x⁻¹ * v ~> w * v` -/
inductive red.step : list (α × bool) → list (α × bool) → Prop
| bnot {L₁ L₂ x b} : red.step (L₁ ++ (x, b) :: (x, bnot b) :: L₂) (L₁ ++ L₂)
attribute [simp] red.step.bnot
/-- Reflexive-transitive closure of red.step -/
def red : list (α × bool) → list (α × bool) → Prop := refl_trans_gen red.step
@[refl] lemma red.refl : red L L := refl_trans_gen.refl
@[trans] lemma red.trans : red L₁ L₂ → red L₂ L₃ → red L₁ L₃ := refl_trans_gen.trans
namespace red
/-- Predicate asserting that word `w₁` can be reduced to `w₂` in one step, i.e. there are words
`w₃ w₄` and letter `x` such that `w₁ = w₃xx⁻¹w₄` and `w₂ = w₃w₄` -/
theorem step.length : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.length + 2 = L₁.length
| _ _ (@red.step.bnot _ L1 L2 x b) := by rw [list.length_append, list.length_append]; refl
@[simp] lemma step.bnot_rev {x b} : step (L₁ ++ (x, bnot b) :: (x, b) :: L₂) (L₁ ++ L₂) :=
by cases b; from step.bnot
@[simp] lemma step.cons_bnot {x b} : red.step ((x, b) :: (x, bnot b) :: L) L :=
@step.bnot _ [] _ _ _
@[simp] lemma step.cons_bnot_rev {x b} : red.step ((x, bnot b) :: (x, b) :: L) L :=
@red.step.bnot_rev _ [] _ _ _
theorem step.append_left : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₂ L₃ → step (L₁ ++ L₂) (L₁ ++ L₃)
| _ _ _ red.step.bnot := by rw [← list.append_assoc, ← list.append_assoc]; constructor
theorem step.cons {x} (H : red.step L₁ L₂) : red.step (x :: L₁) (x :: L₂) :=
@step.append_left _ [x] _ _ H
theorem step.append_right : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₁ L₂ → step (L₁ ++ L₃) (L₂ ++ L₃)
| _ _ _ red.step.bnot := by simp
lemma not_step_nil : ¬ step [] L :=
begin
generalize h' : [] = L',
assume h,
cases h with L₁ L₂,
simp [list.nil_eq_append_iff] at h',
contradiction
end
lemma step.cons_left_iff {a : α} {b : bool} :
step ((a, b) :: L₁) L₂ ↔ (∃L, step L₁ L ∧ L₂ = (a, b) :: L) ∨ (L₁ = (a, bnot b)::L₂) :=
begin
split,
{ generalize hL : ((a, b) :: L₁ : list _) = L,
assume h,
rcases h with ⟨_ | ⟨p, s'⟩, e, a', b'⟩,
{ simp at hL, simp [*] },
{ simp at hL,
rcases hL with ⟨rfl, rfl⟩,
refine or.inl ⟨s' ++ e, step.bnot, _⟩,
simp } },
{ assume h,
rcases h with ⟨L, h, rfl⟩ | rfl,
{ exact step.cons h },
{ exact step.cons_bnot } }
end
lemma not_step_singleton : ∀ {p : α × bool}, ¬ step [p] L
| (a, b) := by simp [step.cons_left_iff, not_step_nil]
lemma step.cons_cons_iff : ∀{p : α × bool}, step (p :: L₁) (p :: L₂) ↔ step L₁ L₂ :=
by simp [step.cons_left_iff, iff_def, or_imp_distrib] {contextual := tt}
lemma step.append_left_iff : ∀L, step (L ++ L₁) (L ++ L₂) ↔ step L₁ L₂
| [] := by simp
| (p :: l) := by simp [step.append_left_iff l, step.cons_cons_iff]
private theorem step.diamond_aux : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)} {x1 b1 x2 b2},
L₁ ++ (x1, b1) :: (x1, bnot b1) :: L₂ = L₃ ++ (x2, b2) :: (x2, bnot b2) :: L₄ →
L₁ ++ L₂ = L₃ ++ L₄ ∨ ∃ L₅, red.step (L₁ ++ L₂) L₅ ∧ red.step (L₃ ++ L₄) L₅
| [] _ [] _ _ _ _ _ H := by injections; subst_vars; simp
| [] _ [(x3,b3)] _ _ _ _ _ H := by injections; subst_vars; simp
| [(x3,b3)] _ [] _ _ _ _ _ H := by injections; subst_vars; simp
| [] _ ((x3,b3)::(x4,b4)::tl) _ _ _ _ _ H :=
by injections; subst_vars; simp; right; exact ⟨_, red.step.bnot, red.step.cons_bnot⟩
| ((x3,b3)::(x4,b4)::tl) _ [] _ _ _ _ _ H :=
by injections; subst_vars; simp; right; exact ⟨_, red.step.cons_bnot, red.step.bnot⟩
| ((x3,b3)::tl) _ ((x4,b4)::tl2) _ _ _ _ _ H :=
let ⟨H1, H2⟩ := list.cons.inj H in
match step.diamond_aux H2 with
| or.inl H3 := or.inl $ by simp [H1, H3]
| or.inr ⟨L₅, H3, H4⟩ := or.inr
⟨_, step.cons H3, by simpa [H1] using step.cons H4⟩
end
theorem step.diamond : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)},
red.step L₁ L₃ → red.step L₂ L₄ → L₁ = L₂ →
L₃ = L₄ ∨ ∃ L₅, red.step L₃ L₅ ∧ red.step L₄ L₅
| _ _ _ _ red.step.bnot red.step.bnot H := step.diamond_aux H
lemma step.to_red : step L₁ L₂ → red L₁ L₂ :=
refl_trans_gen.single
/-- Church-Rosser theorem for word reduction: If `w1 w2 w3` are words such that `w1` reduces to `w2`
and `w3` respectively, then there is a word `w4` such that `w2` and `w3` reduce to `w4` respectively. -/
theorem church_rosser : red L₁ L₂ → red L₁ L₃ → join red L₂ L₃ :=
relation.church_rosser (assume a b c hab hac,
match b, c, red.step.diamond hab hac rfl with
| b, _, or.inl rfl := ⟨b, by refl, by refl⟩
| b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, hcd.to_red⟩
end)
lemma cons_cons {p} : red L₁ L₂ → red (p :: L₁) (p :: L₂) :=
refl_trans_gen_lift (list.cons p) (assume a b, step.cons)
lemma cons_cons_iff (p) : red (p :: L₁) (p :: L₂) ↔ red L₁ L₂ :=
iff.intro
begin
generalize eq₁ : (p :: L₁ : list _) = LL₁,
generalize eq₂ : (p :: L₂ : list _) = LL₂,
assume h,
induction h using relation.refl_trans_gen.head_induction_on
with L₁ L₂ h₁₂ h ih
generalizing L₁ L₂,
{ subst_vars, cases eq₂, constructor },
{ subst_vars,
cases p with a b,
rw [step.cons_left_iff] at h₁₂,
rcases h₁₂ with ⟨L, h₁₂, rfl⟩ | rfl,
{ exact (ih rfl rfl).head h₁₂ },
{ exact (cons_cons h).tail step.cons_bnot_rev } }
end
cons_cons
lemma append_append_left_iff : ∀L, red (L ++ L₁) (L ++ L₂) ↔ red L₁ L₂
| [] := iff.rfl
| (p :: L) := by simp [append_append_left_iff L, cons_cons_iff]
lemma append_append (h₁ : red L₁ L₃) (h₂ : red L₂ L₄) : red (L₁ ++ L₂) (L₃ ++ L₄) :=
(refl_trans_gen_lift (λL, L ++ L₂) (assume a b, step.append_right) h₁).trans
((append_append_left_iff _).2 h₂)
lemma to_append_iff : red L (L₁ ++ L₂) ↔ (∃L₃ L₄, L = L₃ ++ L₄ ∧ red L₃ L₁ ∧ red L₄ L₂) :=
iff.intro
begin
generalize eq : L₁ ++ L₂ = L₁₂,
assume h,
induction h with L' L₁₂ hLL' h ih generalizing L₁ L₂,
{ exact ⟨_, _, eq.symm, by refl, by refl⟩ },
{ cases h with s e a b,
rcases list.append_eq_append_iff.1 eq with ⟨s', rfl, rfl⟩ | ⟨e', rfl, rfl⟩,
{ have : L₁ ++ (s' ++ ((a, b) :: (a, bnot b) :: e)) = (L₁ ++ s') ++ ((a, b) :: (a, bnot b) :: e),
{ simp },
rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩,
exact ⟨w₁, w₂, rfl, h₁, h₂.tail step.bnot⟩ },
{ have : (s ++ ((a, b) :: (a, bnot b) :: e')) ++ L₂ = s ++ ((a, b) :: (a, bnot b) :: (e' ++ L₂)),
{ simp },
rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩,
exact ⟨w₁, w₂, rfl, h₁.tail step.bnot, h₂⟩ }, }
end
(assume ⟨L₃, L₄, eq, h₃, h₄⟩, eq.symm ▸ append_append h₃ h₄)
/-- The empty word `[]` only reduces to itself. -/
theorem nil_iff : red [] L ↔ L = [] :=
refl_trans_gen_iff_eq (assume l, red.not_step_nil)
/-- A letter only reduces to itself. -/
theorem singleton_iff {x} : red [x] L₁ ↔ L₁ = [x] :=
refl_trans_gen_iff_eq (assume l, not_step_singleton)
/-- If `x` is a letter and `w` is a word such that `xw` reduces to the empty word, then `w` reduces
to `x⁻¹` -/
theorem cons_nil_iff_singleton {x b} : red ((x, b) :: L) [] ↔ red L [(x, bnot b)] :=
iff.intro
(assume h,
have h₁ : red ((x, bnot b) :: (x, b) :: L) [(x, bnot b)], from cons_cons h,
have h₂ : red ((x, bnot b) :: (x, b) :: L) L, from refl_trans_gen.single step.cons_bnot_rev,
let ⟨L', h₁, h₂⟩ := church_rosser h₁ h₂ in
by rw [singleton_iff] at h₁; subst L'; assumption)
(assume h, (cons_cons h).tail step.cons_bnot)
theorem red_iff_irreducible {x1 b1 x2 b2} (h : (x1, b1) ≠ (x2, b2)) :
red [(x1, bnot b1), (x2, b2)] L ↔ L = [(x1, bnot b1), (x2, b2)] :=
begin
apply refl_trans_gen_iff_eq,
generalize eq : [(x1, bnot b1), (x2, b2)] = L',
assume L h',
cases h',
simp [list.cons_eq_append_iff, list.nil_eq_append_iff] at eq,
rcases eq with ⟨rfl, ⟨rfl, rfl⟩, ⟨rfl, rfl⟩, rfl⟩, subst_vars,
simp at h,
contradiction
end
/-- If `x` and `y` are distinct letters and `w₁ w₂` are words such that `xw₁` reduces to `yw₂`, then
`w₁` reduces to `x⁻¹yw₂`. -/
theorem inv_of_red_of_ne {x1 b1 x2 b2}
(H1 : (x1, b1) ≠ (x2, b2))
(H2 : red ((x1, b1) :: L₁) ((x2, b2) :: L₂)) :
red L₁ ((x1, bnot b1) :: (x2, b2) :: L₂) :=
begin
have : red ((x1, b1) :: L₁) ([(x2, b2)] ++ L₂), from H2,
rcases to_append_iff.1 this with ⟨_ | ⟨p, L₃⟩, L₄, eq, h₁, h₂⟩,
{ simp [nil_iff] at h₁, contradiction },
{ cases eq,
show red (L₃ ++ L₄) ([(x1, bnot b1), (x2, b2)] ++ L₂),
apply append_append _ h₂,
have h₁ : red ((x1, bnot b1) :: (x1, b1) :: L₃) [(x1, bnot b1), (x2, b2)],
{ exact cons_cons h₁ },
have h₂ : red ((x1, bnot b1) :: (x1, b1) :: L₃) L₃,
{ exact step.cons_bnot_rev.to_red },
rcases church_rosser h₁ h₂ with ⟨L', h₁, h₂⟩,
rw [red_iff_irreducible H1] at h₁,
rwa [h₁] at h₂ }
end
theorem step.sublist (H : red.step L₁ L₂) : L₂ <+ L₁ :=
by cases H; simp; constructor; constructor; refl
/-- If `w₁ w₂` are words such that `w₁` reduces to `w₂`, then `w₂` is a sublist of `w₁`. -/
theorem sublist : red L₁ L₂ → L₂ <+ L₁ :=
refl_trans_gen_of_transitive_reflexive
(λl, list.sublist.refl l) (λa b c hab hbc, list.sublist.trans hbc hab) (λa b, red.step.sublist)
theorem sizeof_of_step : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.sizeof < L₁.sizeof
| _ _ (@step.bnot _ L1 L2 x b) :=
begin
induction L1 with hd tl ih,
case list.nil
{ dsimp [list.sizeof],
have H : 1 + sizeof (x, b) + (1 + sizeof (x, bnot b) + list.sizeof L2)
= (list.sizeof L2 + 1) + (sizeof (x, b) + sizeof (x, bnot b) + 1),
{ ac_refl },
rw H,
exact nat.le_add_right _ _ },
case list.cons
{ dsimp [list.sizeof],
exact nat.add_lt_add_left ih _ }
end
theorem length (h : red L₁ L₂) : ∃ n, L₁.length = L₂.length + 2 * n :=
begin
induction h with L₂ L₃ h₁₂ h₂₃ ih,
{ exact ⟨0, rfl⟩ },
{ rcases ih with ⟨n, eq⟩,
existsi (1 + n),
simp [mul_add, eq, (step.length h₂₃).symm] }
end
theorem antisymm (h₁₂ : red L₁ L₂) : red L₂ L₁ → L₁ = L₂ :=
match L₁, h₁₂.cases_head with
| _, or.inl rfl := assume h, rfl
| L₁, or.inr ⟨L₃, h₁₃, h₃₂⟩ := assume h₂₁,
let ⟨n, eq⟩ := length (h₃₂.trans h₂₁) in
have list.length L₃ + 0 = list.length L₃ + (2 * n + 2),
by simpa [(step.length h₁₃).symm, add_comm, add_assoc] using eq,
(nat.no_confusion $ nat.add_left_cancel this)
end
end red
theorem equivalence_join_red : equivalence (join (@red α)) :=
equivalence_join_refl_trans_gen $ assume a b c hab hac,
(match b, c, red.step.diamond hab hac rfl with
| b, _, or.inl rfl := ⟨b, by refl, by refl⟩
| b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, refl_trans_gen.single hcd⟩
end)
theorem join_red_of_step (h : red.step L₁ L₂) : join red L₁ L₂ :=
join_of_single reflexive_refl_trans_gen h.to_red
theorem eqv_gen_step_iff_join_red : eqv_gen red.step L₁ L₂ ↔ join red L₁ L₂ :=
iff.intro
(assume h,
have eqv_gen (join red) L₁ L₂ := eqv_gen_mono (assume a b, join_red_of_step) h,
(eqv_gen_iff_of_equivalence $ equivalence_join_red).1 this)
(join_of_equivalence (eqv_gen.is_equivalence _) $ assume a b,
refl_trans_gen_of_equivalence (eqv_gen.is_equivalence _) eqv_gen.rel)
end free_group
/-- The free group over a type, i.e. the words formed by the elements of the type and their formal
inverses, quotient by one step reduction. -/
def free_group (α : Type u) : Type u :=
quot $ @free_group.red.step α
namespace free_group
variables {α} {L L₁ L₂ L₃ L₄ : list (α × bool)}
def mk (L) : free_group α := quot.mk red.step L
@[simp] lemma quot_mk_eq_mk : quot.mk red.step L = mk L := rfl
@[simp] lemma quot_lift_mk (β : Type v) (f : list (α × bool) → β)
(H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) :
quot.lift f H (mk L) = f L := rfl
@[simp] lemma quot_lift_on_mk (β : Type v) (f : list (α × bool) → β)
(H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) :
quot.lift_on (mk L) f H = f L := rfl
instance : has_one (free_group α) := ⟨mk []⟩
lemma one_eq_mk : (1 : free_group α) = mk [] := rfl
instance : has_mul (free_group α) :=
⟨λ x y, quot.lift_on x
(λ L₁, quot.lift_on y (λ L₂, mk $ L₁ ++ L₂) (λ L₂ L₃ H, quot.sound $ red.step.append_left H))
(λ L₁ L₂ H, quot.induction_on y $ λ L₃, quot.sound $ red.step.append_right H)⟩
@[simp] lemma mul_mk : mk L₁ * mk L₂ = mk (L₁ ++ L₂) := rfl
instance : has_inv (free_group α) :=
⟨λx, quot.lift_on x (λ L, mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse)
(assume a b h, quot.sound $ by cases h; simp)⟩
@[simp] lemma inv_mk : (mk L)⁻¹ = mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse := rfl
instance : group (free_group α) :=
{ mul := (*),
one := 1,
inv := has_inv.inv,
mul_assoc := by rintros ⟨L₁⟩ ⟨L₂⟩ ⟨L₃⟩; simp,
one_mul := by rintros ⟨L⟩; refl,
mul_one := by rintros ⟨L⟩; simp [one_eq_mk],
mul_left_inv := by rintros ⟨L⟩; exact (list.rec_on L rfl $
λ ⟨x, b⟩ tl ih, eq.trans (quot.sound $ by simp [one_eq_mk]) ih) }
/-- `of x` is the canonical injection from the type to the free group over that type by sending each
element to the equivalence class of the letter that is the element. -/
def of (x : α) : free_group α :=
mk [(x, tt)]
theorem red.exact : mk L₁ = mk L₂ ↔ join red L₁ L₂ :=
calc (mk L₁ = mk L₂) ↔ eqv_gen red.step L₁ L₂ : iff.intro (quot.exact _) quot.eqv_gen_sound
... ↔ join red L₁ L₂ : eqv_gen_step_iff_join_red
/-- The canonical injection from the type to the free group is an injection. -/
theorem of.inj {x y : α} (H : of x = of y) : x = y :=
let ⟨L₁, hx, hy⟩ := red.exact.1 H in
by simp [red.singleton_iff] at hx hy; cc
section to_group
variables {β : Type v} [group β] (f : α → β) {x y : free_group α}
def to_group.aux : list (α × bool) → β :=
λ L, list.prod $ L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹
theorem red.step.to_group {f : α → β} (H : red.step L₁ L₂) :
to_group.aux f L₁ = to_group.aux f L₂ :=
by cases H with _ _ _ b; cases b; simp [to_group.aux]
/-- If `β` is a group, then any function from `α` to `β`
extends uniquely to a group homomorphism from
the free group over `α` to `β` -/
def to_group : free_group α → β :=
quot.lift (to_group.aux f) $ λ L₁ L₂ H, red.step.to_group H
variable {f}
@[simp] lemma to_group.mk : to_group f (mk L) =
list.prod (L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹) :=
rfl
@[simp] lemma to_group.of {x} : to_group f (of x) = f x :=
one_mul _
instance to_group.is_group_hom : is_group_hom (to_group f) :=
{ map_mul := by rintros ⟨L₁⟩ ⟨L₂⟩; simp }
@[simp] lemma to_group.mul : to_group f (x * y) = to_group f x * to_group f y :=
is_mul_hom.map_mul _ _ _
@[simp] lemma to_group.one : to_group f 1 = 1 :=
is_group_hom.map_one _
@[simp] lemma to_group.inv : to_group f x⁻¹ = (to_group f x)⁻¹ :=
is_group_hom.map_inv _ _
theorem to_group.unique (g : free_group α → β) [is_group_hom g]
(hg : ∀ x, g (of x) = f x) : ∀{x}, g x = to_group f x :=
by rintros ⟨L⟩; exact list.rec_on L (is_group_hom.map_one g)
(λ ⟨x, b⟩ t (ih : g (mk t) = _), bool.rec_on b
(show g ((of x)⁻¹ * mk t) = to_group f (mk ((x, ff) :: t)),
by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih, to_group, to_group.aux])
(show g (of x * mk t) = to_group f (mk ((x, tt) :: t)),
by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih, to_group, to_group.aux]))
theorem to_group.of_eq (x : free_group α) : to_group of x = x :=
eq.symm $ to_group.unique id (λ x, rfl)
theorem to_group.range_subset {s : set β} [is_subgroup s] (H : set.range f ⊆ s) :
set.range (to_group f) ⊆ s :=
by rintros _ ⟨⟨L⟩, rfl⟩; exact list.rec_on L (is_submonoid.one_mem s)
(λ ⟨x, b⟩ tl ih, bool.rec_on b
(by simp at ih ⊢; from is_submonoid.mul_mem
(is_subgroup.inv_mem $ H ⟨x, rfl⟩) ih)
(by simp at ih ⊢; from is_submonoid.mul_mem (H ⟨x, rfl⟩) ih))
theorem to_group.range_eq_closure :
set.range (to_group f) = group.closure (set.range f) :=
set.subset.antisymm
(to_group.range_subset group.subset_closure)
(group.closure_subset $ λ y ⟨x, hx⟩, ⟨of x, by simpa⟩)
end to_group
section map
variables {β : Type v} (f : α → β) {x y : free_group α}
def map.aux (L : list (α × bool)) : list (β × bool) :=
L.map $ λ x, (f x.1, x.2)
/-- Any function from `α` to `β` extends uniquely
to a group homomorphism from the free group
ver `α` to the free group over `β`. -/
def map (x : free_group α) : free_group β :=
x.lift_on (λ L, mk $ map.aux f L) $
λ L₁ L₂ H, quot.sound $ by cases H; simp [map.aux]
instance map.is_group_hom : is_group_hom (map f) :=
{ map_mul := by rintros ⟨L₁⟩ ⟨L₂⟩; simp [map, map.aux] }
variable {f}
@[simp] lemma map.mk : map f (mk L) = mk (L.map (λ x, (f x.1, x.2))) :=
rfl
@[simp] lemma map.id : map id x = x :=
have H1 : (λ (x : α × bool), x) = id := rfl,
by rcases x with ⟨L⟩; simp [H1]
@[simp] lemma map.id' : map (λ z, z) x = x := map.id
theorem map.comp {γ : Type w} {f : α → β} {g : β → γ} {x} :
map g (map f x) = map (g ∘ f) x :=
by rcases x with ⟨L⟩; simp
@[simp] lemma map.of {x} : map f (of x) = of (f x) := rfl
@[simp] lemma map.mul : map f (x * y) = map f x * map f y :=
is_mul_hom.map_mul _ x y
@[simp] lemma map.one : map f 1 = 1 :=
is_group_hom.map_one _
@[simp] lemma map.inv : map f x⁻¹ = (map f x)⁻¹ :=
is_group_hom.map_inv _ x
theorem map.unique (g : free_group α → free_group β) [is_group_hom g]
(hg : ∀ x, g (of x) = of (f x)) : ∀{x}, g x = map f x :=
by rintros ⟨L⟩; exact list.rec_on L (is_group_hom.map_one g)
(λ ⟨x, b⟩ t (ih : g (mk t) = map f (mk t)), bool.rec_on b
(show g ((of x)⁻¹ * mk t) = map f ((of x)⁻¹ * mk t),
by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih])
(show g (of x * mk t) = map f (of x * mk t),
by simp [is_mul_hom.map_mul g, hg, ih]))
/-- Equivalent types give rise to equivalent free groups. -/
def free_group_congr {α β} (e : α ≃ β) : free_group α ≃ free_group β :=
⟨map e, map e.symm,
λ x, by simp [function.comp, map.comp],
λ x, by simp [function.comp, map.comp]⟩
theorem map_eq_to_group : map f x = to_group (of ∘ f) x :=
eq.symm $ map.unique _ $ λ x, by simp
end map
section prod
variables [group α] (x y : free_group α)
/-- If `α` is a group, then any function from `α` to `α`
extends uniquely to a homomorphism from the
free group over `α` to `α`. This is the multiplicative
version of `sum`. -/
def prod : α :=
to_group id x
variables {x y}
@[simp] lemma prod_mk :
prod (mk L) = list.prod (L.map $ λ x, cond x.2 x.1 x.1⁻¹) :=
rfl
@[simp] lemma prod.of {x : α} : prod (of x) = x :=
to_group.of
instance prod.is_group_hom : is_group_hom (@prod α _) :=
to_group.is_group_hom
@[simp] lemma prod.mul : prod (x * y) = prod x * prod y :=
to_group.mul
@[simp] lemma prod.one : prod (1:free_group α) = 1 :=
to_group.one
@[simp] lemma prod.inv : prod x⁻¹ = (prod x)⁻¹ :=
to_group.inv
lemma prod.unique (g : free_group α → α) [is_group_hom g]
(hg : ∀ x, g (of x) = x) {x} :
g x = prod x :=
to_group.unique g hg
end prod
theorem to_group_eq_prod_map {β : Type v} [group β] {f : α → β} {x} :
to_group f x = prod (map f x) :=
eq.symm $ to_group.unique (prod ∘ map f) $ λ _, by simp
section sum
variables [add_group α] (x y : free_group α)
/-- If `α` is a group, then any function from `α` to `α`
extends uniquely to a homomorphism from the
free group over `α` to `α`. This is the additive
version of `prod`. -/
def sum : α :=
@prod (multiplicative _) _ x
variables {x y}
@[simp] lemma sum_mk :
sum (mk L) = list.sum (L.map $ λ x, cond x.2 x.1 (-x.1)) :=
rfl
@[simp] lemma sum.of {x : α} : sum (of x) = x :=
prod.of
instance sum.is_group_hom : is_group_hom (@sum α _) :=
prod.is_group_hom
@[simp] lemma sum.mul : sum (x * y) = sum x + sum y :=
prod.mul
@[simp] lemma sum.one : sum (1:free_group α) = 0 :=
prod.one
@[simp] lemma sum.inv : sum x⁻¹ = -sum x :=
prod.inv
end sum
def free_group_empty_equiv_unit : free_group empty ≃ unit :=
{ to_fun := λ _, (),
inv_fun := λ _, 1,
left_inv := by rintros ⟨_ | ⟨⟨⟨⟩, _⟩, _⟩⟩; refl,
right_inv := λ ⟨⟩, rfl }
def free_group_unit_equiv_int : free_group unit ≃ int :=
{ to_fun := λ x, sum $ map (λ _, 1) x,
inv_fun := λ x, of () ^ x,
left_inv := by rintros ⟨L⟩; exact list.rec_on L rfl
(λ ⟨⟨⟩, b⟩ tl ih, by cases b; simp [gpow_add] at ih ⊢; rw ih; refl),
right_inv := λ x, int.induction_on x (by simp)
(λ i ih, by simp at ih; simp [gpow_add, ih])
(λ i ih, by simp at ih; simp [gpow_add, ih]) }
section category
variables {β : Type u}
instance : monad free_group.{u} :=
{ pure := λ α, of,
map := λ α β, map,
bind := λ α β x f, to_group f x }
@[elab_as_eliminator]
protected theorem induction_on
{C : free_group α → Prop}
(z : free_group α)
(C1 : C 1)
(Cp : ∀ x, C $ pure x)
(Ci : ∀ x, C (pure x) → C (pure x)⁻¹)
(Cm : ∀ x y, C x → C y → C (x * y)) : C z :=
quot.induction_on z $ λ L, list.rec_on L C1 $ λ ⟨x, b⟩ tl ih,
bool.rec_on b (Cm _ _ (Ci _ $ Cp x) ih) (Cm _ _ (Cp x) ih)
@[simp] lemma map_pure (f : α → β) (x : α) : f <$> (pure x : free_group α) = pure (f x) :=
map.of
@[simp] lemma map_one (f : α → β) : f <$> (1 : free_group α) = 1 :=
map.one
@[simp] lemma map_mul (f : α → β) (x y : free_group α) : f <$> (x * y) = f <$> x * f <$> y :=
map.mul
@[simp] lemma map_inv (f : α → β) (x : free_group α) : f <$> (x⁻¹) = (f <$> x)⁻¹ :=
map.inv
@[simp] lemma pure_bind (f : α → free_group β) (x) : pure x >>= f = f x :=
to_group.of
@[simp] lemma one_bind (f : α → free_group β) : 1 >>= f = 1 :=
@@to_group.one _ f
@[simp] lemma mul_bind (f : α → free_group β) (x y : free_group α) : x * y >>= f = (x >>= f) * (y >>= f) :=
to_group.mul
@[simp] lemma inv_bind (f : α → free_group β) (x : free_group α) : x⁻¹ >>= f = (x >>= f)⁻¹ :=
to_group.inv
instance : is_lawful_monad free_group.{u} :=
{ id_map := λ α x, free_group.induction_on x (map_one id) (λ x, map_pure id x)
(λ x ih, by rw [map_inv, ih]) (λ x y ihx ihy, by rw [map_mul, ihx, ihy]),
pure_bind := λ α β x f, pure_bind f x,
bind_assoc := λ α β γ x f g, free_group.induction_on x
(by iterate 3 { rw one_bind }) (λ x, by iterate 2 { rw pure_bind })
(λ x ih, by iterate 3 { rw inv_bind }; rw ih)
(λ x y ihx ihy, by iterate 3 { rw mul_bind }; rw [ihx, ihy]),
bind_pure_comp_eq_map := λ α β f x, free_group.induction_on x
(by rw [one_bind, map_one]) (λ x, by rw [pure_bind, map_pure])
(λ x ih, by rw [inv_bind, map_inv, ih]) (λ x y ihx ihy, by rw [mul_bind, map_mul, ihx, ihy]) }
end category
section reduce
variable [decidable_eq α]
/-- The maximal reduction of a word. It is computable
iff `α` has decidable equality. -/
def reduce (L : list (α × bool)) : list (α × bool) :=
list.rec_on L [] $ λ hd1 tl1 ih,
list.cases_on ih [hd1] $ λ hd2 tl2,
if hd1.1 = hd2.1 ∧ hd1.2 = bnot hd2.2 then tl2
else hd1 :: hd2 :: tl2
@[simp] lemma reduce.cons (x) : reduce (x :: L) =
list.cases_on (reduce L) [x] (λ hd tl,
if x.1 = hd.1 ∧ x.2 = bnot hd.2 then tl
else x :: hd :: tl) := rfl
/-- The first theorem that characterises the function
`reduce`: a word reduces to its maximal reduction. -/
theorem reduce.red : red L (reduce L) :=
begin
induction L with hd1 tl1 ih,
case list.nil
{ constructor },
case list.cons
{ dsimp,
revert ih,
generalize htl : reduce tl1 = TL,
intro ih,
cases TL with hd2 tl2,
case list.nil
{ exact red.cons_cons ih },
case list.cons
{ dsimp,
by_cases h : hd1.fst = hd2.fst ∧ hd1.snd = bnot (hd2.snd),
{ rw [if_pos h],
transitivity,
{ exact red.cons_cons ih },
{ cases hd1, cases hd2, cases h,
dsimp at *, subst_vars,
exact red.step.cons_bnot_rev.to_red } },
{ rw [if_neg h],
exact red.cons_cons ih } } }
end
theorem reduce.not {p : Prop} : ∀ {L₁ L₂ L₃ : list (α × bool)} {x b}, reduce L₁ = L₂ ++ (x, b) :: (x, bnot b) :: L₃ → p
| [] L2 L3 _ _ := λ h, by cases L2; injections
| ((x,b)::L1) L2 L3 x' b' := begin
dsimp,
cases r : reduce L1,
{ dsimp, intro h,
have := congr_arg list.length h,
simp [-add_comm] at this,
exact absurd this dec_trivial },
cases hd with y c,
by_cases x = y ∧ b = bnot c; simp [h]; intro H,
{ rw H at r,
exact @reduce.not L1 ((y,c)::L2) L3 x' b' r },
rcases L2 with _|⟨a, L2⟩,
{ injections, subst_vars,
simp at h, cc },
{ refine @reduce.not L1 L2 L3 x' b' _,
injection H with _ H,
rw [r, H], refl }
end
/-- The second theorem that characterises the
function `reduce`: the maximal reduction of a word
only reduces to itself. -/
theorem reduce.min (H : red (reduce L₁) L₂) : reduce L₁ = L₂ :=
begin
induction H with L1 L' L2 H1 H2 ih,
{ refl },
{ cases H1 with L4 L5 x b,
exact reduce.not H2 }
end
/-- `reduce` is idempotent, i.e. the maximal reduction
of the maximal reduction of a word is the maximal
reduction of the word. -/
theorem reduce.idem : reduce (reduce L) = reduce L :=
eq.symm $ reduce.min reduce.red
theorem reduce.step.eq (H : red.step L₁ L₂) : reduce L₁ = reduce L₂ :=
let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (reduce.red.head H) in
(reduce.min HR13).trans (reduce.min HR23).symm
/-- If a word reduces to another word, then they have
a common maximal reduction. -/
theorem reduce.eq_of_red (H : red L₁ L₂) : reduce L₁ = reduce L₂ :=
let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (red.trans H reduce.red) in
(reduce.min HR13).trans (reduce.min HR23).symm
/-- If two words correspond to the same element in
the free group, then they have a common maximal
reduction. This is the proof that the function that
sends an element of the free group to its maximal
reduction is well-defined. -/
theorem reduce.sound (H : mk L₁ = mk L₂) : reduce L₁ = reduce L₂ :=
let ⟨L₃, H13, H23⟩ := red.exact.1 H in
(reduce.eq_of_red H13).trans (reduce.eq_of_red H23).symm
/-- If two words have a common maximal reduction,
then they correspond to the same element in the free group. -/
theorem reduce.exact (H : reduce L₁ = reduce L₂) : mk L₁ = mk L₂ :=
red.exact.2 ⟨reduce L₂, H ▸ reduce.red, reduce.red⟩
/-- A word and its maximal reduction correspond to
the same element of the free group. -/
theorem reduce.self : mk (reduce L) = mk L :=
reduce.exact reduce.idem
/-- If words `w₁ w₂` are such that `w₁` reduces to `w₂`,
then `w₂` reduces to the maximal reduction of `w₁`. -/
theorem reduce.rev (H : red L₁ L₂) : red L₂ (reduce L₁) :=
(reduce.eq_of_red H).symm ▸ reduce.red
/-- The function that sends an element of the free
group to its maximal reduction. -/
def to_word : free_group α → list (α × bool) :=
quot.lift reduce $ λ L₁ L₂ H, reduce.step.eq H
lemma to_word.mk : ∀{x : free_group α}, mk (to_word x) = x :=
by rintros ⟨L⟩; exact reduce.self
lemma to_word.inj : ∀(x y : free_group α), to_word x = to_word y → x = y :=
by rintros ⟨L₁⟩ ⟨L₂⟩; exact reduce.exact
/-- Constructive Church-Rosser theorem (compare `church_rosser`). -/
def reduce.church_rosser (H12 : red L₁ L₂) (H13 : red L₁ L₃) :
{ L₄ // red L₂ L₄ ∧ red L₃ L₄ } :=
⟨reduce L₁, reduce.rev H12, reduce.rev H13⟩
instance : decidable_eq (free_group α) :=
function.injective.decidable_eq to_word.inj
instance red.decidable_rel : decidable_rel (@red α)
| [] [] := is_true red.refl
| [] (hd2::tl2) := is_false $ λ H, list.no_confusion (red.nil_iff.1 H)
| ((x,b)::tl) [] := match red.decidable_rel tl [(x, bnot b)] with
| is_true H := is_true $ red.trans (red.cons_cons H) $
(@red.step.bnot _ [] [] _ _).to_red
| is_false H := is_false $ λ H2, H $ red.cons_nil_iff_singleton.1 H2
end
| ((x1,b1)::tl1) ((x2,b2)::tl2) := if h : (x1, b1) = (x2, b2)
then match red.decidable_rel tl1 tl2 with
| is_true H := is_true $ h ▸ red.cons_cons H
| is_false H := is_false $ λ H2, H $ h ▸ (red.cons_cons_iff _).1 $ H2
end
else match red.decidable_rel tl1 ((x1,bnot b1)::(x2,b2)::tl2) with
| is_true H := is_true $ (red.cons_cons H).tail red.step.cons_bnot
| is_false H := is_false $ λ H2, H $ red.inv_of_red_of_ne h H2
end
/-- A list containing every word that `w₁` reduces to. -/
def red.enum (L₁ : list (α × bool)) : list (list (α × bool)) :=
list.filter (λ L₂, red L₁ L₂) (list.sublists L₁)
theorem red.enum.sound (H : L₂ ∈ red.enum L₁) : red L₁ L₂ :=
list.of_mem_filter H
theorem red.enum.complete (H : red L₁ L₂) : L₂ ∈ red.enum L₁ :=
list.mem_filter_of_mem (list.mem_sublists.2 $ red.sublist H) H
instance : fintype { L₂ // red L₁ L₂ } :=
fintype.subtype (list.to_finset $ red.enum L₁) $
λ L₂, ⟨λ H, red.enum.sound $ list.mem_to_finset.1 H,
λ H, list.mem_to_finset.2 $ red.enum.complete H⟩
end reduce
end free_group
|
5f807b291ce332323b837647ac625fd11435a8d2 | 80b18137872dad7c3df334b9069d70935b4224f3 | /src/order/filter/basic.lean | ea17d2120f60b5c8b45b9a6b6874a352ceb54550 | [
"Apache-2.0"
] | permissive | Jack-Pumpkinhead/mathlib | 1bcf5692d355dc397847791c137158f01b407535 | da8b23f907f750528539bffa604875b98717fb93 | refs/heads/master | 1,621,299,949,262 | 1,585,480,767,000 | 1,585,480,767,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 99,760 | 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, Jeremy Avigad
-/
import order.galois_connection order.zorn order.copy
import data.set.finite
/-! # Theory of filters on sets
## Main definitions
* `filter` : filter on a set;
* `at_top`, `at_bot`, `cofinite`, `principal` : specific filters;
* `map`, `comap`, `join` : operations on filters;
* `filter_upwards [h₁, ..., hₙ]` : takes a list of proofs `hᵢ : sᵢ ∈ f`, and replaces a goal `s ∈ f`
with `∀ x, x ∈ s₁ → ... → x ∈ sₙ → x ∈ s`;
* `eventually` : `f.eventually p` means `{x | p x} ∈ f`;
* `frequently` : `f.frequently p` means `{x | ¬p x} ∉ f`.
## Notations
* `∀ᶠ x in f, p x` : `f.eventually p`;
* `∃ᶠ x in f, p x` : `f.frequently p`.
* `f ×ᶠ g` : `filter.prod f g`, localized in `filter`.
-/
open set
universes u v w x y
open_locale classical
section order
variables {α : Type u} (r : α → α → Prop)
local infix ` ≼ ` : 50 := r
lemma directed_on_Union {r} {ι : Sort v} {f : ι → set α} (hd : directed (⊆) f)
(h : ∀x, directed_on r (f x)) : directed_on r (⋃x, f x) :=
by simp only [directed_on, exists_prop, mem_Union, exists_imp_distrib]; exact
assume a₁ b₁ fb₁ a₂ b₂ fb₂,
let ⟨z, zb₁, zb₂⟩ := hd b₁ b₂,
⟨x, xf, xa₁, xa₂⟩ := h z a₁ (zb₁ fb₁) a₂ (zb₂ fb₂) in
⟨x, ⟨z, xf⟩, xa₁, xa₂⟩
end order
theorem directed_of_chain {α β r} [is_refl β r] {f : α → β} {c : set α}
(h : zorn.chain (f ⁻¹'o r) c) :
directed r (λx:{a:α // a ∈ c}, f (x.val)) :=
assume ⟨a, ha⟩ ⟨b, hb⟩, classical.by_cases
(assume : a = b, by simp only [this, exists_prop, and_self, subtype.exists];
exact ⟨b, hb, refl _⟩)
(assume : a ≠ b, (h a ha b hb this).elim
(λ h : r (f a) (f b), ⟨⟨b, hb⟩, h, refl _⟩)
(λ h : r (f b) (f a), ⟨⟨a, ha⟩, refl _, h⟩))
/-- A filter `F` on a type `α` is a collection of sets of `α` which contains the whole `α`,
is upwards-closed, and is stable under intersection, -/
structure filter (α : Type*) :=
(sets : set (set α))
(univ_sets : set.univ ∈ sets)
(sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets)
(inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets)
/-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/
@[reducible]
instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩
namespace filter
variables {α : Type u} {f g : filter α} {s t : set α}
lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g
| ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl
lemma filter_eq_iff : f = g ↔ f.sets = g.sets :=
⟨congr_arg _, filter_eq⟩
protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g :=
by rw [filter_eq_iff, ext_iff]
@[ext]
protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g :=
filter.ext_iff.2
lemma univ_mem_sets : univ ∈ f :=
f.univ_sets
lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f → x ⊆ y → y ∈ f :=
f.sets_of_superset
lemma inter_mem_sets : ∀{s t}, s ∈ f → t ∈ f → s ∩ t ∈ f :=
f.inter_sets
lemma univ_mem_sets' (h : ∀ a, a ∈ s) : s ∈ f :=
mem_sets_of_superset univ_mem_sets (assume x _, h x)
lemma mp_sets (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f :=
mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁
lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f :=
⟨λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mp)),
λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mpr))⟩
lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) :
(∀i∈is, s i ∈ f) → (⋂i∈is, s i) ∈ f :=
finite.induction_on hf
(assume hs, by simp only [univ_mem_sets, mem_empty_eq, Inter_neg, Inter_univ, not_false_iff])
(assume i is _ hf hi hs,
have h₁ : s i ∈ f, from hs i (by simp),
have h₂ : (⋂x∈is, s x) ∈ f, from hi $ assume a ha, hs _ $ by simp only [ha, mem_insert_iff, or_true],
by simp [inter_mem_sets h₁ h₂])
lemma Inter_mem_sets_of_fintype {β : Type v} {s : β → set α} [fintype β] (h : ∀i, s i ∈ f) :
(⋂i, s i) ∈ f :=
by simpa using Inter_mem_sets finite_univ (λi hi, h i)
lemma exists_sets_subset_iff : (∃t ∈ f, t ⊆ s) ↔ s ∈ f :=
⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩
lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f) :=
assume s t hst h, mem_sets_of_superset h hst
end filter
namespace tactic.interactive
open tactic interactive
/-- `filter_upwards [h1, ⋯, hn]` replaces a goal of the form `s ∈ f`
and terms `h1 : t1 ∈ f, ⋯, hn : tn ∈ f` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`.
`filter_upwards [h1, ⋯, hn] e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`.
-/
meta def filter_upwards
(s : parse types.pexpr_list)
(e' : parse $ optional types.texpr) : tactic unit :=
do
s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e),
eapplyc `filter.univ_mem_sets',
match e' with
| some e := interactive.exact e
| none := skip
end
end tactic.interactive
namespace filter
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
section principal
/-- The principal filter of `s` is the collection of all supersets of `s`. -/
def principal (s : set α) : filter α :=
{ sets := {t | s ⊆ t},
univ_sets := subset_univ s,
sets_of_superset := assume x y hx hy, subset.trans hx hy,
inter_sets := assume x y, subset_inter }
instance : inhabited (filter α) :=
⟨principal ∅⟩
@[simp] lemma mem_principal_sets {s t : set α} : s ∈ principal t ↔ t ⊆ s := iff.rfl
lemma mem_principal_self (s : set α) : s ∈ principal s := subset.refl _
end principal
section join
/-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/
def join (f : filter (filter α)) : filter α :=
{ sets := {s | {t : filter α | s ∈ t} ∈ f},
univ_sets := by simp only [univ_mem_sets, mem_set_of_eq]; exact univ_mem_sets,
sets_of_superset := assume x y hx xy,
mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy,
inter_sets := assume x y hx hy,
mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ }
@[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} :
s ∈ join f ↔ {t | s ∈ t} ∈ f := iff.rfl
end join
section lattice
instance : partial_order (filter α) :=
{ le := λf g, ∀ ⦃U : set α⦄, U ∈ g → U ∈ f,
le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁,
le_refl := assume a, subset.refl _,
le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ }
theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl
/-- `generate_sets g s`: `s` is in the filter closure of `g`. -/
inductive generate_sets (g : set (set α)) : set α → Prop
| basic {s : set α} : s ∈ g → generate_sets s
| univ {} : generate_sets univ
| superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t
| inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t)
/-- `generate g` is the smallest filter containing the sets `g`. -/
def generate (g : set (set α)) : filter α :=
{ sets := generate_sets g,
univ_sets := generate_sets.univ,
sets_of_superset := assume x y, generate_sets.superset,
inter_sets := assume s t, generate_sets.inter }
lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets :=
iff.intro
(assume h u hu, h $ generate_sets.basic $ hu)
(assume h u hu, hu.rec_on h univ_mem_sets
(assume x y _ hxy hx, mem_sets_of_superset hx hxy)
(assume x y _ _ hx hy, inter_mem_sets hx hy))
/-- `mk_of_closure s hs` constructs a filter on `α` whose elements set is exactly
`s : set (set α)`, provided one gives the assumption `hs : (generate s).sets = s`. -/
protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α :=
{ sets := s,
univ_sets := hs ▸ (univ_mem_sets : univ ∈ generate s),
sets_of_superset := assume x y, hs ▸ (mem_sets_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s),
inter_sets := assume x y, hs ▸ (inter_mem_sets : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) }
lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} :
filter.mk_of_closure s hs = generate s :=
filter.ext $ assume u,
show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.rfl
/-- Galois insertion from sets of sets into filters. -/
def gi_generate (α : Type*) :
@galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets :=
{ gc := assume s f, sets_iff_generate,
le_l_u := assume f u h, generate_sets.basic h,
choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _),
choice_eq := assume s hs, mk_of_closure_sets }
/-- The infimum of filters is the filter generated by intersections
of elements of the two filters. -/
instance : has_inf (filter α) := ⟨λf g : filter α,
{ sets := {s | ∃ (a ∈ f) (b ∈ g), a ∩ b ⊆ s },
univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩,
sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩,
inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩,
⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd,
calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl
... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩
@[simp] lemma mem_inf_sets {f g : filter α} {s : set α} :
s ∈ f ⊓ g ↔ ∃t₁∈f, ∃t₂∈g, t₁ ∩ t₂ ⊆ s := iff.rfl
lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g :=
⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩
lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g :=
⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩
lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α}
(hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g :=
inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht)
instance : has_top (filter α) :=
⟨{ sets := {s | ∀x, x ∈ s},
univ_sets := assume x, mem_univ x,
sets_of_superset := assume x y hx hxy a, hxy (hx a),
inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩
lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀x, x ∈ s) :=
iff.rfl
@[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ :=
by rw [mem_top_sets_iff_forall, eq_univ_iff_forall]
section complete_lattice
/- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately,
we want to have different definitional equalities for the lattice operations. So we define them
upfront and change the lattice operations for the complete lattice instance. -/
private def original_complete_lattice : complete_lattice (filter α) :=
@order_dual.complete_lattice _ (gi_generate α).lift_complete_lattice
local attribute [instance] original_complete_lattice
instance : complete_lattice (filter α) := original_complete_lattice.copy
/- le -/ filter.partial_order.le rfl
/- top -/ (filter.has_top).1
(top_unique $ assume s hs, by have := univ_mem_sets ; finish)
/- bot -/ _ rfl
/- sup -/ _ rfl
/- inf -/ (filter.has_inf).1
begin
ext f g : 2,
exact le_antisymm
(le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right))
(assume s ⟨a, ha, b, hb, hs⟩, show s ∈ complete_lattice.inf f g, from
mem_sets_of_superset (inter_mem_sets
(@inf_le_left (filter α) _ _ _ _ ha)
(@inf_le_right (filter α) _ _ _ _ hb)) hs)
end
/- Sup -/ (join ∘ principal) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm)
/- Inf -/ _ rfl
end complete_lattice
lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl
lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets :=
(gi_generate α).gc.u_inf
lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) :=
(gi_generate α).gc.u_Inf
lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) :=
(gi_generate α).gc.u_infi
lemma generate_empty : filter.generate ∅ = (⊤ : filter α) :=
(gi_generate α).gc.l_bot
lemma generate_univ : filter.generate univ = (⊥ : filter α) :=
mk_of_closure_sets.symm
lemma generate_union {s t : set (set α)} :
filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t :=
(gi_generate α).gc.l_sup
lemma generate_Union {s : ι → set (set α)} :
filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) :=
(gi_generate α).gc.l_supr
@[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α) :=
trivial
@[simp] lemma mem_sup_sets {f g : filter α} {s : set α} :
s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g :=
iff.rfl
@[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} :
x ∈ Sup s ↔ (∀f∈s, x ∈ (f:filter α)) :=
iff.rfl
@[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} :
x ∈ supr f ↔ (∀i, x ∈ f i) :=
by simp only [supr_sets_eq, iff_self, mem_Inter]
@[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ principal s ↔ s ∈ f :=
show (∀{t}, s ⊆ t → t ∈ f) ↔ s ∈ f,
from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩
lemma principal_mono {s t : set α} : principal s ≤ principal t ↔ s ⊆ t :=
by simp only [le_principal_iff, iff_self, mem_principal_sets]
lemma monotone_principal : monotone (principal : set α → filter α) :=
λ _ _, principal_mono.2
@[simp] lemma principal_eq_iff_eq {s t : set α} : principal s = principal t ↔ s = t :=
by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl
@[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (principal s) = Sup s := rfl
/-! ### Lattice equations -/
lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ :=
⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s),
assume : f = ⊥, this.symm ▸ mem_bot_sets⟩
lemma nonempty_of_mem_sets {f : filter α} (hf : f ≠ ⊥) {s : set α} (hs : s ∈ f) :
s.nonempty :=
s.eq_empty_or_nonempty.elim (λ h, absurd hs (h.symm ▸ mt empty_in_sets_eq_bot.mp hf)) id
lemma nonempty_of_ne_bot {f : filter α} (hf : f ≠ ⊥) : nonempty α :=
nonempty_of_exists $ nonempty_of_mem_sets hf univ_mem_sets
lemma filter_eq_bot_of_not_nonempty {f : filter α} (ne : ¬ nonempty α) : f = ⊥ :=
empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩)
lemma forall_sets_nonempty_iff_ne_bot {f : filter α} :
(∀ (s : set α), s ∈ f → s.nonempty) ↔ f ≠ ⊥ :=
⟨λ h hf, empty_not_nonempty (h ∅ $ hf.symm ▸ mem_bot_sets), nonempty_of_mem_sets⟩
lemma mem_sets_of_eq_bot {f : filter α} {s : set α} (h : f ⊓ principal (-s) = ⊥) : s ∈ f :=
have ∅ ∈ f ⊓ principal (- s), from h.symm ▸ mem_bot_sets,
let ⟨s₁, hs₁, s₂, (hs₂ : -s ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in
by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩
lemma eq_Inf_of_mem_sets_iff_exists_mem {S : set (filter α)} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = Inf S :=
le_antisymm (le_Inf $ λ f hf s hs, h.2 ⟨f, hf, hs⟩)
(λ s hs, let ⟨f, hf, hs⟩ := h.1 hs in (Inf_le hf : Inf S ≤ f) hs)
lemma eq_infi_of_mem_sets_iff_exists_mem {f : ι → filter α} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ i, s ∈ f i) :
l = infi f :=
eq_Inf_of_mem_sets_iff_exists_mem $ λ s, h.trans exists_range_iff.symm
lemma eq_binfi_of_mem_sets_iff_exists_mem {f : ι → filter α} {p : ι → Prop} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ i (_ : p i), s ∈ f i) :
l = ⨅ i (_ : p i), f i :=
begin
rw [infi_subtype'],
apply eq_infi_of_mem_sets_iff_exists_mem,
intro s,
exact h.trans ⟨λ ⟨i, pi, si⟩, ⟨⟨i, pi⟩, si⟩, λ ⟨⟨i, pi⟩, si⟩, ⟨i, pi, si⟩⟩
end
lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) :
(infi f).sets = (⋃ i, (f i).sets) :=
let ⟨i⟩ := ne, u := { filter .
sets := (⋃ i, (f i).sets),
univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩,
sets_of_superset := by simp only [mem_Union, exists_imp_distrib];
intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩,
inter_sets :=
begin
simp only [mem_Union, exists_imp_distrib],
assume x y a hx b hy,
rcases h a b with ⟨c, ha, hb⟩,
exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩
end } in
have u = infi f, from eq_infi_of_mem_sets_iff_exists_mem (λ s, by simp only [mem_Union]),
congr_arg filter.sets this.symm
lemma mem_infi {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) (s) :
s ∈ infi f ↔ ∃ i, s ∈ f i :=
by simp only [infi_sets_eq h ne, mem_Union]
@[nolint ge_or_gt] -- Intentional use of `≥`
lemma binfi_sets_eq {f : β → filter α} {s : set β}
(h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) :
(⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) :=
let ⟨i, hi⟩ := ne in
calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl
... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq
(assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end)
⟨⟨i, hi⟩⟩
... = (⨆ t ∈ {t | t ∈ s}, (f t).sets) : by rw [supr_subtype]; refl
@[nolint ge_or_gt] -- Intentional use of `≥`
lemma mem_binfi {f : β → filter α} {s : set β}
(h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) {t : set α} :
t ∈ (⨅ i∈s, f i) ↔ ∃ i ∈ s, t ∈ f i :=
by simp only [binfi_sets_eq h ne, mem_bUnion_iff]
lemma infi_sets_eq_finite (f : ι → filter α) :
(⨅i, f i).sets = (⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets) :=
begin
rw [infi_eq_infi_finset, infi_sets_eq],
exact (directed_of_sup $ λs₁ s₂ hs, infi_le_infi $ λi, infi_le_infi_const $ λh, hs h),
apply_instance
end
lemma mem_infi_finite {f : ι → filter α} (s) :
s ∈ infi f ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets :=
show s ∈ (infi f).sets ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets,
by rw infi_sets_eq_finite
@[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) :=
filter_eq $ set.ext $ assume x,
by simp only [supr_sets_eq, join, mem_sup_sets, iff_self, mem_set_of_eq]
@[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} :
(⨆x, join (f x)) = join (⨆x, f x) :=
filter_eq $ set.ext $ assume x,
by simp only [supr_sets_eq, join, iff_self, mem_Inter, mem_set_of_eq]
instance : bounded_distrib_lattice (filter α) :=
{ le_sup_inf :=
begin
assume x y z s,
simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp],
intros hs t₁ ht₁ t₂ ht₂ hts,
exact ⟨s ∪ t₁,
x.sets_of_superset hs $ subset_union_left _ _,
y.sets_of_superset ht₁ $ subset_union_right _ _,
s ∪ t₂,
x.sets_of_superset hs $ subset_union_left _ _,
z.sets_of_superset ht₂ $ subset_union_right _ _,
subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩
end,
..filter.complete_lattice }
/- the complementary version with ⨆i, f ⊓ g i does not hold! -/
lemma infi_sup_eq {f : filter α} {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g :=
begin
refine le_antisymm _ (le_infi $ assume i, sup_le_sup (le_refl f) $ infi_le _ _),
rintros t ⟨h₁, h₂⟩,
rw [infi_sets_eq_finite] at h₂,
simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂,
rcases h₂ with ⟨s, hs⟩,
suffices : (⨅i, f ⊔ g i) ≤ f ⊔ s.inf (λi, g i.down), { exact this ⟨h₁, hs⟩ },
refine finset.induction_on s _ _,
{ exact le_sup_right_of_le le_top },
{ rintros ⟨i⟩ s his ih,
rw [finset.inf_insert, sup_inf_left],
exact le_inf (infi_le _ _) ih }
end
lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} :
∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⋂a∈s, p a) ⊆ t) :=
show ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⨅a∈s, p a) ≤ t),
begin
simp only [(finset.inf_eq_infi _ _).symm],
refine finset.induction_on s _ _,
{ simp only [finset.not_mem_empty, false_implies_iff, finset.inf_empty, top_le_iff,
imp_true_iff, mem_top_sets, true_and, exists_const],
intros; refl },
{ intros a s has ih t,
simp only [ih, finset.forall_mem_insert, finset.inf_insert, mem_inf_sets,
exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt},
split,
{ intros t₁ ht₁ t₂ p hp ht₂ ht,
existsi function.update p a t₁,
have : ∀a'∈s, function.update p a t₁ a' = p a',
from assume a' ha',
have a' ≠ a, from assume h, has $ h ▸ ha',
function.update_noteq this _ _,
have eq : s.inf (λj, function.update p a t₁ j) = s.inf (λj, p j) :=
finset.inf_congr rfl this,
simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt},
exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht },
assume p hpa hp ht,
exact ⟨p a, hpa, (s.inf p), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ }
end
/-! ### Eventually -/
/-- `f.eventually p` or `∀ᶠ x in f, p x` mean that `{x | p x} ∈ f`. E.g., `∀ᶠ x in at_top, p x`
means that `p` holds true for sufficiently large `x`. -/
protected def eventually (p : α → Prop) (f : filter α) : Prop := {x | p x} ∈ f
notation `∀ᶠ` binders ` in ` f `, ` r:(scoped p, filter.eventually p f) := r
protected lemma eventually.and {p q : α → Prop} {f : filter α} :
f.eventually p → f.eventually q → ∀ᶠ x in f, p x ∧ q x :=
inter_mem_sets
@[simp]
lemma eventually_true (f : filter α) : ∀ᶠ x in f, true := univ_mem_sets
lemma eventually_of_forall {p : α → Prop} (f : filter α) (hp : ∀ x, p x) :
∀ᶠ x in f, p x :=
univ_mem_sets' hp
lemma eventually_false_iff_eq_bot {f : filter α} :
(∀ᶠ x in f, false) ↔ f = ⊥ :=
empty_in_sets_eq_bot
lemma eventually.mp {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x)
(hq : ∀ᶠ x in f, p x → q x) :
∀ᶠ x in f, q x :=
mp_sets hp hq
lemma eventually.mono {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x)
(hq : ∀ x, p x → q x) :
∀ᶠ x in f, q x :=
hp.mp (f.eventually_of_forall hq)
lemma eventually.congr {f : filter α} {β : Type*} {u v : α → β} {p : β → Prop}
(h : ∀ᶠ a in f, u a = v a) (h' : ∀ᶠ a in f, p (u a)) : ∀ᶠ a in f, p (v a) :=
h'.mp (h.mono (by simp {contextual := tt}))
lemma eventually.congr_iff {f : filter α} {β : Type*} {u v : α → β}
(h : ∀ᶠ a in f, u a = v a) (p : β → Prop) :
(∀ᶠ a in f, p (u a)) ↔ (∀ᶠ a in f, p (v a)) :=
⟨h.congr, eventually.congr (h.mp (by simp {contextual := tt}))⟩
@[simp]
lemma eventually_bot {p : α → Prop} : ∀ᶠ x in ⊥, p x := ⟨⟩
@[simp]
lemma eventually_top {p : α → Prop} : (∀ᶠ x in ⊤, p x) ↔ (∀ x, p x) :=
iff.rfl
lemma eventually_sup {p : α → Prop} {f g : filter α} :
(∀ᶠ x in f ⊔ g, p x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in g, p x) :=
iff.rfl
@[simp]
lemma eventually_Sup {p : α → Prop} {fs : set (filter α)} :
(∀ᶠ x in Sup fs, p x) ↔ (∀ f ∈ fs, ∀ᶠ x in f, p x) :=
iff.rfl
@[simp]
lemma eventually_supr {p : α → Prop} {fs : β → filter α} :
(∀ᶠ x in (⨆ b, fs b), p x) ↔ (∀ b, ∀ᶠ x in fs b, p x) :=
mem_supr_sets
@[simp]
lemma eventually_principal {a : set α} {p : α → Prop} :
(∀ᶠ x in principal a, p x) ↔ (∀ x ∈ a, p x) :=
iff.rfl
/-! ### Frequently -/
/-- `f.frequently p` or `∃ᶠ x in f, p x` mean that `{x | ¬p x} ∉ f`. E.g., `∃ᶠ x in at_top, p x`
means that there exist arbitrarily large `x` for which `p` holds true. -/
protected def frequently (p : α → Prop) (f : filter α) : Prop := ¬∀ᶠ x in f, ¬p x
notation `∃ᶠ` binders ` in ` f `, ` r:(scoped p, filter.frequently p f) := r
lemma eventually.frequently {f : filter α} (hf : f ≠ ⊥) {p : α → Prop} (h : ∀ᶠ x in f, p x) :
∃ᶠ x in f, p x :=
begin
assume h',
have := h.and h',
simp only [and_not_self, eventually_false_iff_eq_bot] at this,
exact hf this
end
lemma frequently.mp {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x)
(hpq : ∀ᶠ x in f, p x → q x) :
∃ᶠ x in f, q x :=
mt (λ hq, hq.mp $ hpq.mono $ λ x, mt) h
lemma frequently.mono {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x)
(hpq : ∀ x, p x → q x) :
∃ᶠ x in f, q x :=
h.mp (f.eventually_of_forall hpq)
lemma frequently.and_eventually {p q : α → Prop} {f : filter α}
(hp : ∃ᶠ x in f, p x) (hq : ∀ᶠ x in f, q x) :
∃ᶠ x in f, p x ∧ q x :=
begin
refine mt (λ h, hq.mp $ h.mono _) hp,
assume x hpq hq hp,
exact hpq ⟨hp, hq⟩
end
lemma frequently.exists {p : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) : ∃ x, p x :=
begin
by_contradiction H,
replace H : ∀ᶠ x in f, ¬ p x, from f.eventually_of_forall (not_exists.1 H),
exact hp H
end
lemma eventually.exists {p : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hf : f ≠ ⊥) :
∃ x, p x :=
(hp.frequently hf).exists
lemma frequently_iff_forall_eventually_exists_and {p : α → Prop} {f : filter α} :
(∃ᶠ x in f, p x) ↔ ∀ {q : α → Prop}, (∀ᶠ x in f, q x) → ∃ x, p x ∧ q x :=
⟨assume hp q hq, (hp.and_eventually hq).exists,
assume H hp, by simpa only [and_not_self, exists_false] using H hp⟩
@[simp] lemma not_eventually {p : α → Prop} {f : filter α} :
(¬ ∀ᶠ x in f, p x) ↔ (∃ᶠ x in f, ¬ p x) :=
by simp [filter.frequently]
@[simp] lemma not_frequently {p : α → Prop} {f : filter α} :
(¬ ∃ᶠ x in f, p x) ↔ (∀ᶠ x in f, ¬ p x) :=
by simp only [filter.frequently, not_not]
lemma frequently_true_iff_ne_bot (f : filter α) : (∃ᶠ x in f, true) ↔ f ≠ ⊥ :=
by simp [filter.frequently, -not_eventually, eventually_false_iff_eq_bot]
lemma frequently_false (f : filter α) : ¬ ∃ᶠ x in f, false := by simp
lemma frequently_bot {p : α → Prop} : ¬ ∃ᶠ x in ⊥, p x := by simp
@[simp]
lemma frequently_top {p : α → Prop} : (∃ᶠ x in ⊤, p x) ↔ (∃ x, p x) :=
by simp [filter.frequently]
@[simp]
lemma frequently_principal {a : set α} {p : α → Prop} :
(∃ᶠ x in principal a, p x) ↔ (∃ x ∈ a, p x) :=
by simp [filter.frequently, not_forall]
lemma frequently_sup {p : α → Prop} {f g : filter α} :
(∃ᶠ x in f ⊔ g, p x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in g, p x) :=
by simp only [filter.frequently, eventually_sup, not_and_distrib]
@[simp]
lemma frequently_Sup {p : α → Prop} {fs : set (filter α)} :
(∃ᶠ x in Sup fs, p x) ↔ (∃ f ∈ fs, ∃ᶠ x in f, p x) :=
by simp [filter.frequently, -not_eventually, not_forall]
@[simp]
lemma frequently_supr {p : α → Prop} {fs : β → filter α} :
(∃ᶠ x in (⨆ b, fs b), p x) ↔ (∃ b, ∃ᶠ x in fs b, p x) :=
by simp [filter.frequently, -not_eventually, not_forall]
/- principal equations -/
@[simp] lemma inf_principal {s t : set α} : principal s ⊓ principal t = principal (s ∩ t) :=
le_antisymm
(by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩)
(by simp [le_inf_iff, inter_subset_left, inter_subset_right])
@[simp] lemma sup_principal {s t : set α} : principal s ⊔ principal t = principal (s ∪ t) :=
filter_eq $ set.ext $
by simp only [union_subset_iff, union_subset_iff, mem_sup_sets, forall_const, iff_self, mem_principal_sets]
@[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, principal (s x)) = principal (⋃i, s i) :=
filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, mem_principal_sets, mem_Inter];
exact (@supr_le_iff (set α) _ _ _ _).symm
lemma principal_univ : principal (univ : set α) = ⊤ :=
top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true]
lemma principal_empty : principal (∅ : set α) = ⊥ :=
bot_unique $ assume s _, empty_subset _
@[simp] lemma principal_eq_bot_iff {s : set α} : principal s = ⊥ ↔ s = ∅ :=
empty_in_sets_eq_bot.symm.trans $ mem_principal_sets.trans subset_empty_iff
lemma principal_ne_bot_iff {s : set α} : principal s ≠ ⊥ ↔ s.nonempty :=
(not_congr principal_eq_bot_iff).trans ne_empty_iff_nonempty
lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : -s ∈ f) : f ⊓ principal s = ⊥ :=
empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩
theorem mem_inf_principal (f : filter α) (s t : set α) :
s ∈ f ⊓ principal t ↔ { x | x ∈ t → x ∈ s } ∈ f :=
begin
simp only [mem_inf_sets, mem_principal_sets, exists_prop], split,
{ rintros ⟨u, ul, v, tsubv, uvinter⟩,
apply filter.mem_sets_of_superset ul,
intros x xu xt, exact uvinter ⟨xu, tsubv xt⟩ },
intro h, refine ⟨_, h, t, set.subset.refl t, _⟩,
rintros x ⟨hx, xt⟩,
exact hx xt
end
@[simp] lemma infi_principal_finset {ι : Type w} (s : finset ι) (f : ι → set α) :
(⨅i∈s, principal (f i)) = principal (⋂i∈s, f i) :=
begin
ext t,
simp [mem_infi_sets_finset],
split,
{ rintros ⟨p, hp, ht⟩,
calc (⋂ (i : ι) (H : i ∈ s), f i) ≤ (⋂ (i : ι) (H : i ∈ s), p i) :
infi_le_infi (λi, infi_le_infi (λhi, mem_principal_sets.1 (hp i hi)))
... ≤ t : ht },
{ assume h,
exact ⟨f, λi hi, subset.refl _, h⟩ }
end
@[simp] lemma infi_principal_fintype {ι : Type w} [fintype ι] (f : ι → set α) :
(⨅i, principal (f i)) = principal (⋂i, f i) :=
by simpa using infi_principal_finset finset.univ f
end lattice
section map
/-- The forward map of a filter -/
def map (m : α → β) (f : filter α) : filter β :=
{ sets := preimage m ⁻¹' f.sets,
univ_sets := univ_mem_sets,
sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st,
inter_sets := assume s t hs ht, inter_mem_sets hs ht }
@[simp] lemma map_principal {s : set α} {f : α → β} :
map f (principal s) = principal (set.image f s) :=
filter_eq $ set.ext $ assume a, image_subset_iff.symm
variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] lemma mem_map : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl
lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f :=
f.sets_of_superset hs $ subset_preimage_image m s
lemma range_mem_map : range m ∈ map m f :=
by rw ←image_univ; exact image_mem_map univ_mem_sets
lemma mem_map_sets_iff : t ∈ map m f ↔ (∃s∈f, m '' s ⊆ t) :=
iff.intro
(assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩)
(assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht)
@[simp] lemma map_id : filter.map id f = f :=
filter_eq $ rfl
@[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) :=
funext $ assume _, filter_eq $ rfl
@[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f :=
congr_fun (@@filter.map_compose m m') f
end map
section comap
/-- The inverse map of a filter -/
def comap (m : α → β) (f : filter β) : filter α :=
{ sets := { s | ∃t∈ f, m ⁻¹' t ⊆ s },
univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩,
sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab,
⟨a', ha', subset.trans ma'a ab⟩,
inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩,
⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ }
end comap
/-- The cofinite filter is the filter of subsets whose complements are finite. -/
def cofinite : filter α :=
{ sets := {s | finite (- s)},
univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq],
sets_of_superset := assume s t (hs : finite (-s)) (st: s ⊆ t),
finite_subset hs $ compl_subset_compl.2 st,
inter_sets := assume s t (hs : finite (-s)) (ht : finite (-t)),
by simp only [compl_inter, finite_union, ht, hs, mem_set_of_eq] }
@[simp] lemma mem_cofinite {s : set α} : s ∈ (@cofinite α) ↔ finite (-s) := iff.rfl
lemma cofinite_ne_bot [infinite α] : @cofinite α ≠ ⊥ :=
mt empty_in_sets_eq_bot.mpr $ by { simp only [mem_cofinite, compl_empty], exact infinite_univ }
lemma frequently_cofinite_iff_infinite {p : α → Prop} :
(∃ᶠ x in cofinite, p x) ↔ set.infinite {x | p x} :=
by simp only [filter.frequently, filter.eventually, mem_cofinite, compl_set_of, not_not,
set.infinite]
/-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`.
Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the
applicative instance. -/
def bind (f : filter α) (m : α → filter β) : filter β := join (map m f)
/-- The applicative sequentiation operation. This is not induced by the bind operation. -/
def seq (f : filter (α → β)) (g : filter α) : filter β :=
⟨{ s | ∃u∈ f, ∃t∈ g, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) },
⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩,
assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩,
assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩,
⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁,
assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩
/-- `pure x` is the set of sets that contain `x`. It is equal to `principal {x}` but
with this definition we have `s ∈ pure a` defeq `a ∈ s`. -/
instance : has_pure filter :=
⟨λ (α : Type u) x,
{ sets := {s | x ∈ s},
inter_sets := λ s t, and.intro,
sets_of_superset := λ s t hs hst, hst hs,
univ_sets := trivial }⟩
instance : has_bind filter := ⟨@filter.bind⟩
instance : has_seq filter := ⟨@filter.seq⟩
instance : functor filter := { map := @filter.map }
lemma pure_sets (a : α) : (pure a : filter α).sets = {s | a ∈ s} := rfl
@[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := iff.rfl
lemma pure_eq_principal (a : α) : (pure a : filter α) = principal {a} :=
filter.ext $ λ s, by simp only [mem_pure_sets, mem_principal_sets, singleton_subset_iff]
@[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) :=
filter.ext $ λ s, iff.rfl
@[simp] lemma join_pure (f : filter α) : join (pure f) = f := filter.ext $ λ s, iff.rfl
@[simp] lemma pure_bind (a : α) (m : α → filter β) :
bind (pure a) m = m a :=
by simp only [has_bind.bind, bind, map_pure, join_pure]
section
-- this section needs to be before applicative, otherwise the wrong instance will be chosen
/-- The monad structure on filters. -/
protected def monad : monad filter := { map := @filter.map }
local attribute [instance] filter.monad
protected lemma is_lawful_monad : is_lawful_monad filter :=
{ id_map := assume α f, filter_eq rfl,
pure_bind := assume α β, pure_bind,
bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl,
bind_pure_comp_eq_map := assume α β f x, filter.ext $ λ s,
by simp only [has_bind.bind, bind, functor.map, mem_map, mem_join_sets, mem_set_of_eq,
function.comp, mem_pure_sets] }
end
instance : applicative filter := { map := @filter.map, seq := @filter.seq }
instance : alternative filter :=
{ failure := λα, ⊥,
orelse := λα x y, x ⊔ y }
@[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl
@[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl
/- map and comap equations -/
section map
variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] theorem mem_comap_sets : s ∈ comap m g ↔ ∃t∈ g, m ⁻¹' t ⊆ s := iff.rfl
theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g :=
⟨t, ht, subset.refl _⟩
lemma comap_id : comap id f = f :=
le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst)
lemma comap_comap_comp {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f :=
le_antisymm
(assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩)
(assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩,
⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩)
@[simp] theorem comap_principal {t : set β} : comap m (principal t) = principal (m ⁻¹' t) :=
filter_eq $ set.ext $ assume s,
⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b,
assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩
lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g :=
⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩
lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) :=
assume f g, map_le_iff_le_comap
lemma map_mono : monotone (map m) := (gc_map_comap m).monotone_l
lemma comap_mono : monotone (comap m) := (gc_map_comap m).monotone_u
@[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot
@[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup
@[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) :=
(gc_map_comap m).l_supr
@[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top
@[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf
@[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) :=
(gc_map_comap m).u_infi
lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ :=
by rw [comap_top]; exact le_top
lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _
lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _
@[simp] lemma comap_bot : comap m ⊥ = ⊥ :=
bot_unique $ assume s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩
lemma comap_supr {ι} {f : ι → filter β} {m : α → β} :
comap m (supr f) = (⨆i, comap m (f i)) :=
le_antisymm
(assume s hs,
have ∀i, ∃t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs,
let ⟨t, ht⟩ := classical.axiom_of_choice this in
⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _),
begin
rw [preimage_Union, Union_subset_iff],
assume i,
exact (ht i).2
end⟩)
(supr_le $ assume i, comap_mono $ le_supr _ _)
lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) :=
by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true]
lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ :=
le_antisymm
(assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩,
⟨t₁ ∪ t₂,
⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩,
union_subset hs₁ hs₂⟩)
((@comap_mono _ _ m).le_map_sup _ _)
lemma map_comap {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f :=
le_antisymm
map_comap_le
(assume t' ⟨t, ht, sub⟩, by filter_upwards [ht, hf]; rintros x hxt ⟨y, rfl⟩; exact sub hxt)
lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) :
comap m (map m f) = f :=
have ∀s, preimage m (image m s) = s,
from assume s, preimage_image_eq s h,
le_antisymm
(assume s hs, ⟨
image m s,
f.sets_of_superset hs $ by simp only [this, subset.refl],
by simp only [this, subset.refl]⟩)
le_comap_map
lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f ≤ map m g) : f ≤ g :=
assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)]
assume a has ⟨b, ⟨hbs, hb⟩, h⟩,
have b = a, from hm _ hbs _ has h,
this ▸ hb
lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) :
map m f ≤ map m g ↔ f ≤ g :=
iff.intro (le_of_map_le_map_inj' hsf hsg hm) (λ h, map_mono h)
lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f = map m g) : f = g :=
le_antisymm
(le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h)
(le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm)
lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) :
f = g :=
have comap m (map m f) = comap m (map m g), by rw h,
by rwa [comap_map hm, comap_map hm] at this
theorem le_map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l)
(hf : ∀ y ∈ u, ∃ x, f x = y) :
l ≤ map f (comap f l) :=
assume s ⟨t, tl, ht⟩,
have t ∩ u ⊆ s, from
assume x ⟨xt, xu⟩,
exists.elim (hf x xu) $ λ a faeq,
by { rw ←faeq, apply ht, change f a ∈ t, rw faeq, exact xt },
mem_sets_of_superset (inter_mem_sets tl ul) this
theorem map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l)
(hf : ∀ y ∈ u, ∃ x, f x = y) :
map f (comap f l) = l :=
le_antisymm map_comap_le (le_map_comap_of_surjective' ul hf)
theorem le_map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) :
l ≤ map f (comap f l) :=
le_map_comap_of_surjective' univ_mem_sets (λ y _, hf y)
theorem map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) :
map f (comap f l) = l :=
le_antisymm map_comap_le (le_map_comap_of_surjective hf l)
lemma comap_ne_bot {f : filter β} {m : α → β} (hm : ∀t∈ f, ∃a, m a ∈ t) :
comap m f ≠ ⊥ :=
forall_sets_nonempty_iff_ne_bot.mp $ assume s ⟨t, ht, t_s⟩,
set.nonempty.mono t_s (hm t ht)
lemma comap_ne_bot_of_range_mem {f : filter β} {m : α → β}
(hf : f ≠ ⊥) (hm : range m ∈ f) : comap m f ≠ ⊥ :=
comap_ne_bot $ assume t ht,
let ⟨_, ha, a, rfl⟩ := nonempty_of_mem_sets hf (inter_mem_sets ht hm)
in ⟨a, ha⟩
lemma comap_inf_principal_ne_bot_of_image_mem {f : filter β} {m : α → β}
(hf : f ≠ ⊥) {s : set α} (hs : m '' s ∈ f) : (comap m f ⊓ principal s) ≠ ⊥ :=
begin
refine compl_compl s ▸ mt mem_sets_of_eq_bot _,
rintros ⟨t, ht, hts⟩,
rcases nonempty_of_mem_sets hf (inter_mem_sets hs ht) with ⟨_, ⟨x, hxs, rfl⟩, hxt⟩,
exact absurd hxs (hts hxt)
end
lemma comap_ne_bot_of_surj {f : filter β} {m : α → β}
(hf : f ≠ ⊥) (hm : function.surjective m) : comap m f ≠ ⊥ :=
comap_ne_bot_of_range_mem hf $ univ_mem_sets' hm
lemma comap_ne_bot_of_image_mem {f : filter β} {m : α → β} (hf : f ≠ ⊥)
{s : set α} (hs : m '' s ∈ f) : comap m f ≠ ⊥ :=
ne_bot_of_le_ne_bot (comap_inf_principal_ne_bot_of_image_mem hf hs) inf_le_left
@[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ :=
⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id,
assume h, by simp only [h, eq_self_iff_true, map_bot]⟩
lemma map_ne_bot (hf : f ≠ ⊥) : map m f ≠ ⊥ :=
assume h, hf $ by rwa [map_eq_bot_iff] at h
lemma map_ne_bot_iff (f : α → β) {F : filter α} : map f F ≠ ⊥ ↔ F ≠ ⊥ :=
by rw [not_iff_not, map_eq_bot_iff]
lemma sInter_comap_sets (f : α → β) (F : filter β) :
⋂₀(comap f F).sets = ⋂ U ∈ F, f ⁻¹' U :=
begin
ext x,
suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔
∀ (B : set β), B ∈ F → f x ∈ B,
by simp only [mem_sInter, mem_Inter, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter,
iff_self, mem_Inter, mem_preimage, exists_imp_distrib],
split,
{ intros h U U_in,
simpa only [set.subset.refl, forall_prop_of_true, mem_preimage] using h (f ⁻¹' U) U U_in },
{ intros h V U U_in f_U_V,
exact f_U_V (h U U_in) },
end
end map
lemma map_cong {m₁ m₂ : α → β} {f : filter α} (h : {x | m₁ x = m₂ x} ∈ f) :
map m₁ f = map m₂ f :=
have ∀(m₁ m₂ : α → β) (h : {x | m₁ x = m₂ x} ∈ f), map m₁ f ≤ map m₂ f,
begin
intros m₁ m₂ h s hs,
show {x | m₁ x ∈ s} ∈ f,
filter_upwards [h, hs],
simp only [subset_def, mem_preimage, mem_set_of_eq, forall_true_iff] {contextual := tt}
end,
le_antisymm (this m₁ m₂ h) (this m₂ m₁ $ mem_sets_of_superset h $ assume x, eq.symm)
-- this is a generic rule for monotone functions:
lemma map_infi_le {f : ι → filter α} {m : α → β} :
map m (infi f) ≤ (⨅ i, map m (f i)) :=
le_infi $ assume i, map_mono $ infi_le _ _
lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) (hι : nonempty ι) :
map m (infi f) = (⨅ i, map m (f i)) :=
le_antisymm
map_infi_le
(assume s (hs : preimage m s ∈ infi f),
have ∃i, preimage m s ∈ f i,
by simp only [infi_sets_eq hf hι, mem_Union] at hs; assumption,
let ⟨i, hi⟩ := this in
have (⨅ i, map m (f i)) ≤ principal s, from
infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption,
by simp only [filter.le_principal_iff] at this; assumption)
lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop}
(h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) :
map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) :=
let ⟨i, hi⟩ := ne in
calc map m (⨅i (h : p i), f i) = map m (⨅i:subtype p, f i.val) : by simp only [infi_subtype, eq_self_iff_true]
... = (⨅i:subtype p, map m (f i.val)) : map_infi_eq
(assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end)
⟨⟨i, hi⟩⟩
... = (⨅i (h : p i), map m (f i)) : by simp only [infi_subtype, eq_self_iff_true]
lemma map_inf_le {f g : filter α} {m : α → β} : map m (f ⊓ g) ≤ map m f ⊓ map m g :=
(@map_mono _ _ m).map_inf_le f g
lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g)
(h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g :=
begin
refine le_antisymm map_inf_le (assume s hs, _),
simp only [map, mem_inf_sets, exists_prop, mem_map, mem_preimage, mem_inf_sets] at hs ⊢,
rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩,
refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩,
{ filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ rw [image_inter_on],
{ refine image_subset_iff.2 _,
exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ },
{ exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } }
end
lemma map_inf {f g : filter α} {m : α → β} (h : function.injective m) :
map m (f ⊓ g) = map m f ⊓ map m g :=
map_inf' univ_mem_sets univ_mem_sets (assume x _ y _ hxy, h hxy)
lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α}
(h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f :=
le_antisymm
(assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $
calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true]
... ⊆ preimage m b : preimage_mono h)
(assume b (hb : preimage m b ∈ f),
⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩)
lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f :=
map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq
lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈ f, m '' s ∈ g) :
g ≤ f.map m :=
assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _
protected lemma push_pull (f : α → β) (F : filter α) (G : filter β) :
map f (F ⊓ comap f G) = map f F ⊓ G :=
begin
apply le_antisymm,
{ calc map f (F ⊓ comap f G) ≤ map f F ⊓ (map f $ comap f G) : map_inf_le
... ≤ map f F ⊓ G : inf_le_inf_right (map f F) map_comap_le },
{ rintros U ⟨V, V_in, W, ⟨Z, Z_in, hZ⟩, h⟩,
rw ← image_subset_iff at h,
use [f '' V, image_mem_map V_in, Z, Z_in],
refine subset.trans _ h,
have : f '' (V ∩ f ⁻¹' Z) ⊆ f '' (V ∩ W),
from image_subset _ (inter_subset_inter_right _ ‹_›),
rwa set.push_pull at this }
end
protected lemma push_pull' (f : α → β) (F : filter α) (G : filter β) :
map f (comap f G ⊓ F) = G ⊓ map f F :=
by simp only [filter.push_pull, inf_comm]
section applicative
lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α) :=
mem_singleton a
lemma pure_inj : function.injective (pure : α → filter α) :=
assume a b hab, (filter.ext_iff.1 hab {x | a = x}).1 rfl
@[simp] lemma pure_ne_bot {α : Type u} {a : α} : pure a ≠ (⊥ : filter α) :=
mt empty_in_sets_eq_bot.2 $ not_mem_empty a
@[simp] lemma le_pure_iff {f : filter α} {a : α} : f ≤ pure a ↔ {a} ∈ f :=
⟨λ h, h singleton_mem_pure_sets,
λ h s hs, mem_sets_of_superset h $ singleton_subset_iff.2 hs⟩
lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) :=
iff.rfl
lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, set.seq u t ⊆ s) :=
by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self]
lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} :
s ∈ (f.map m).seq g ↔ (∃t u, t ∈ g ∧ u ∈ f ∧ ∀x∈u, ∀y∈t, m x y ∈ s) :=
iff.intro
(assume ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, assume a, hts _⟩)
(assume ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, assume f ⟨a, has, eq⟩, eq ▸ hts _ has⟩)
lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α}
(hs : s ∈ f) (ht : t ∈ g) : s.seq t ∈ f.seq g :=
⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩
lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β}
(hh : ∀t ∈ f, ∀u ∈ g, set.seq t u ∈ h) : h ≤ seq f g :=
assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $
assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha
lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α}
(hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ :=
le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht)
@[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← singleton_seq, apply seq_mem_seq_sets _ hs,
exact singleton_mem_pure_sets },
{ refine sets_of_superset (map g f) (image_mem_map ht) _,
rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ }
end
@[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← seq_singleton,
exact seq_mem_seq_sets hs singleton_mem_pure_sets },
{ refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _,
rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ }
end
@[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) :
seq h (seq g x) = seq (seq (map (∘) h) g) x :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩,
refine mem_sets_of_superset _
(set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)),
rw ← set.seq_seq,
exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) },
{ rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht),
rw set.seq_seq,
exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv }
end
lemma prod_map_seq_comm (f : filter α) (g : filter β) :
(map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw ← set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu },
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu }
end
instance : is_lawful_functor (filter : Type u → Type u) :=
{ id_map := assume α f, map_id,
comp_map := assume α β γ f g a, map_map.symm }
instance : is_lawful_applicative (filter : Type u → Type u) :=
{ pure_seq_eq_map := assume α β, pure_seq_eq_map,
map_pure := assume α β, map_pure,
seq_pure := assume α β, seq_pure,
seq_assoc := assume α β γ, seq_assoc }
instance : is_comm_applicative (filter : Type u → Type u) :=
⟨assume α β f g, prod_map_seq_comm f g⟩
lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) :
f <*> g = seq f g := rfl
end applicative
/- bind equations -/
section bind
@[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} :
s ∈ bind f m ↔ ∃t ∈ f, ∀x ∈ t, s ∈ m x :=
calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : by simp only [bind, mem_map, iff_self, mem_join_sets, mem_set_of_eq]
... ↔ (∃t ∈ f, t ⊆ {a | s ∈ m a}) : exists_sets_subset_iff.symm
... ↔ (∃t ∈ f, ∀x ∈ t, s ∈ m x) : iff.rfl
lemma bind_mono {f : filter α} {g h : α → filter β} (h₁ : {a | g a ≤ h a} ∈ f) :
bind f g ≤ bind f h :=
assume x h₂, show (_ ∈ f), by filter_upwards [h₁, h₂] assume s gh' h', gh' h'
lemma bind_sup {f g : filter α} {h : α → filter β} :
bind (f ⊔ g) h = bind f h ⊔ bind g h :=
by simp only [bind, sup_join, map_sup, eq_self_iff_true]
lemma bind_mono2 {f g : filter α} {h : α → filter β} (h₁ : f ≤ g) :
bind f h ≤ bind g h :=
assume s h', h₁ h'
lemma principal_bind {s : set α} {f : α → filter β} :
(bind (principal s) f) = (⨆x ∈ s, f x) :=
show join (map f (principal s)) = (⨆x ∈ s, f x),
by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true]
end bind
/-- If `f : ι → filter α` is derected, `ι` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`.
See also `infi_ne_bot_of_directed` for a version assuming `nonempty α` instead of `nonempty ι`. -/
lemma infi_ne_bot_of_directed' {f : ι → filter α} (hn : nonempty ι)
(hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥) : (infi f) ≠ ⊥ :=
begin
intro h,
have he: ∅ ∈ (infi f), from h.symm ▸ (mem_bot_sets : ∅ ∈ (⊥ : filter α)),
obtain ⟨i, hi⟩ : ∃i, ∅ ∈ f i,
from (mem_infi hd hn ∅).1 he,
exact hb i (empty_in_sets_eq_bot.1 hi)
end
/-- If `f : ι → filter α` is derected, `α` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`.
See also `infi_ne_bot_of_directed'` for a version assuming `nonempty ι` instead of `nonempty α`. -/
lemma infi_ne_bot_of_directed {f : ι → filter α}
(hn : nonempty α) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥) : (infi f) ≠ ⊥ :=
if hι : nonempty ι then infi_ne_bot_of_directed' hι hd hb else
assume h : infi f = ⊥,
have univ ⊆ (∅ : set α),
begin
rw [←principal_mono, principal_univ, principal_empty, ←h],
exact (le_infi $ assume i, false.elim $ hι ⟨i⟩)
end,
let ⟨x⟩ := hn in this (mem_univ x)
lemma infi_ne_bot_iff_of_directed' {f : ι → filter α}
(hn : nonempty ι) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) :=
⟨assume ne_bot i, ne_bot_of_le_ne_bot ne_bot (infi_le _ i),
infi_ne_bot_of_directed' hn hd⟩
lemma infi_ne_bot_iff_of_directed {f : ι → filter α}
(hn : nonempty α) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) :=
⟨assume ne_bot i, ne_bot_of_le_ne_bot ne_bot (infi_le _ i),
infi_ne_bot_of_directed hn hd⟩
lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ f i → s ∈ ⨅i, f i :=
show (⨅i, f i) ≤ f i, from infi_le _ _
@[elab_as_eliminator]
lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop}
(uni : p univ)
(ins : ∀{i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂))
(upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s :=
begin
rw [mem_infi_finite] at hs,
simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at hs,
rcases hs with ⟨is, his⟩,
revert s,
refine finset.induction_on is _ _,
{ assume s hs, rwa [mem_top_sets.1 hs] },
{ rintros ⟨i⟩ js his ih s hs,
rw [finset.inf_insert, mem_inf_sets] at hs,
rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩,
exact upw hs (ins hs₁ (ih hs₂)) }
end
/- tendsto -/
/-- `tendsto` is the generic "limit of a function" predicate.
`tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`,
the `f`-preimage of `a` is an `l₁` neighborhood. -/
def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂
lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl
lemma tendsto.eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop}
(hf : tendsto f l₁ l₂) (h : ∀ᶠ y in l₂, p y) :
∀ᶠ x in l₁, p (f x) :=
hf h
lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f :=
map_le_iff_le_comap
lemma tendsto_congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(hl : {x | f₁ x = f₂ x} ∈ l₁) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ :=
by rw [tendsto, tendsto, map_cong hl]
lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(hl : {x | f₁ x = f₂ x} ∈ l₁) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ :=
(tendsto_congr' hl).1 h
theorem tendsto_congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ :=
tendsto_congr' (univ_mem_sets' h)
theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ :=
(tendsto_congr h).1
lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y :=
by simp only [tendsto, map_id, forall_true_iff] {contextual := tt}
lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x
lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ}
(hg : tendsto g y z) (hf : tendsto f x y) : tendsto (g ∘ f) x z :=
calc map (g ∘ f) x = map g (map f x) : by rw [map_map]
... ≤ map g y : map_mono hf
... ≤ z : hg
lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β}
(h : y ≤ x) : tendsto f x z → tendsto f y z :=
le_trans (map_mono h)
lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β}
(h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z :=
le_trans h₂ h₁
lemma tendsto.ne_bot {f : α → β} {x : filter α} {y : filter β} (h : tendsto f x y) (hx : x ≠ ⊥) :
y ≠ ⊥ :=
ne_bot_of_le_ne_bot (map_ne_bot hx) h
lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x)
lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ}
(h : tendsto (f ∘ g) x y) : tendsto f (map g x) y :=
by rwa [tendsto, map_map]
lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} :
tendsto f (map g x) y ↔ tendsto (f ∘ g) x y :=
by rw [tendsto, map_map]; refl
lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x :=
map_comap_le
lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} :
tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c :=
⟨assume h, tendsto_comap.comp h, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩
lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α}
(h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g :=
by rw [tendsto, ← map_compose]; simp only [(∘), map_comap h, tendsto]
lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f :=
begin
refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ),
rw [comap_comap_comp, eq, comap_id],
exact le_refl _
end
lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g :=
begin
refine le_antisymm hφ (le_trans _ (map_mono hψ)),
rw [map_map, eq, map_id],
exact le_refl _
end
lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} :
tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ :=
by simp only [tendsto, le_inf_iff, iff_self]
lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_left) h
lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_right) h
lemma tendsto.inf {f : α → β} {x₁ x₂ : filter α} {y₁ y₂ : filter β}
(h₁ : tendsto f x₁ y₁) (h₂ : tendsto f x₂ y₂) : tendsto f (x₁ ⊓ x₂) (y₁ ⊓ y₂) :=
tendsto_inf.2 ⟨tendsto_inf_left h₁, tendsto_inf_right h₂⟩
lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} :
tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) :=
by simp only [tendsto, iff_self, le_infi_iff]
lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) :
tendsto f (x i) y → tendsto f (⨅i, x i) y :=
tendsto_le_left (infi_le _ _)
lemma tendsto_principal {f : α → β} {l : filter α} {s : set β} :
tendsto f l (principal s) ↔ ∀ᶠ a in l, f a ∈ s :=
by simp only [tendsto, le_principal_iff, mem_map, iff_self, filter.eventually]
lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} :
tendsto f (principal s) (principal t) ↔ ∀a∈s, f a ∈ t :=
by simp only [tendsto, image_subset_iff, le_principal_iff, map_principal, mem_principal_sets]; refl
lemma tendsto_pure {f : α → β} {a : filter α} {b : β} :
tendsto f a (pure b) ↔ {x | f x = b} ∈ a :=
by simp only [tendsto, le_pure_iff, mem_map, mem_singleton_iff]
lemma tendsto_pure_pure (f : α → β) (a : α) :
tendsto f (pure a) (pure (f a)) :=
tendsto_pure.2 rfl
lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λx, b) a (pure b) :=
tendsto_pure.2 $ univ_mem_sets' $ λ _, rfl
lemma tendsto_if {l₁ : filter α} {l₂ : filter β}
{f g : α → β} {p : α → Prop} [decidable_pred p]
(h₀ : tendsto f (l₁ ⊓ principal p) l₂)
(h₁ : tendsto g (l₁ ⊓ principal { x | ¬ p x }) l₂) :
tendsto (λ x, if p x then f x else g x) l₁ l₂ :=
begin
revert h₀ h₁, simp only [tendsto_def, mem_inf_principal],
intros h₀ h₁ s hs,
apply mem_sets_of_superset (inter_mem_sets (h₀ s hs) (h₁ s hs)),
rintros x ⟨hp₀, hp₁⟩, simp only [mem_preimage],
by_cases h : p x,
{ rw if_pos h, exact hp₀ h },
rw if_neg h, exact hp₁ h
end
section prod
variables {s : set α} {t : set β} {f : filter α} {g : filter β}
/- The product filter cannot be defined using the monad structure on filters. For example:
F := do {x ← seq, y ← top, return (x, y)}
hence:
s ∈ F ↔ ∃n, [n..∞] × univ ⊆ s
G := do {y ← top, x ← seq, return (x, y)}
hence:
s ∈ G ↔ ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s
Now ⋃i, [i..∞] × {i} is in G but not in F.
As product filter we want to have F as result.
-/
/-- Product of filters. This is the filter generated by cartesian products
of elements of the component filters. -/
protected def prod (f : filter α) (g : filter β) : filter (α × β) :=
f.comap prod.fst ⊓ g.comap prod.snd
localized "infix ` ×ᶠ `:60 := filter.prod" in filter
lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β}
(hs : s ∈ f) (ht : t ∈ g) : set.prod s t ∈ f ×ᶠ g :=
inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht)
lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} :
s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, set.prod t₁ t₂ ⊆ s) :=
begin
simp only [filter.prod],
split,
exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩,
⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩,
exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩,
⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩
end
lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f :=
tendsto_inf_left tendsto_comap
lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g :=
tendsto_inf_right tendsto_comap
lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ}
(h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (g ×ᶠ h) :=
tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩
lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) :
∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 :=
tendsto_fst.eventually h
lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) :
∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 :=
tendsto_snd.eventually h
lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x)
{lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) :
∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 :=
(ha.prod_inl lb).and (hb.prod_inr la)
lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) :
(⨅i, f i) ×ᶠ g = (⨅i, (f i) ×ᶠ g) :=
by rw [filter.prod, comap_infi, infi_inf i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) :
f ×ᶠ (⨅i, g i) = (⨅i, f ×ᶠ (g i)) :=
by rw [filter.prod, comap_infi, inf_infi i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) :
f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ :=
inf_le_inf (comap_mono hf) (comap_mono hg)
lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} :
(comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) :=
by simp only [filter.prod, comap_comap_comp, eq_self_iff_true, comap_inf]
lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) :=
by simp only [filter.prod, comap_comap_comp, (∘), inf_comm, prod.fst_swap,
eq_self_iff_true, prod.snd_swap, comap_inf]
lemma prod_comm : f ×ᶠ g = map (λp:β×α, (p.2, p.1)) (g ×ᶠ f) :=
by rw [prod_comm', ← map_swap_eq_comap_swap]; refl
lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} :
(map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) :=
le_antisymm
(assume s hs,
let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in
filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $
calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ :
set.prod_image_image_eq
... ⊆ _ : by rwa [image_subset_iff])
((tendsto.comp (le_refl _) tendsto_fst).prod_mk (tendsto.comp (le_refl _) tendsto_snd))
lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) :
map m (f.prod g) = (f.map (λa b, m (a, b))).seq g :=
begin
simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff],
assume s,
split,
exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩,
exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩
end
lemma prod_eq {f : filter α} {g : filter β} : f.prod g = (f.map prod.mk).seq g :=
have h : _ := map_prod id f g, by rwa [map_id] at h
lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} :
(f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) :=
by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm]
@[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod]
@[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod]
@[simp] lemma prod_principal_principal {s : set α} {t : set β} :
(principal s) ×ᶠ (principal t) = principal (set.prod s t) :=
by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl
@[simp] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) :=
by simp [pure_eq_principal]
lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) :=
begin
split,
{ assume h,
rcases mem_prod_iff.1 (empty_in_sets_eq_bot.2 h) with ⟨s, hs, t, ht, hst⟩,
rw [subset_empty_iff, set.prod_eq_empty_iff] at hst,
cases hst with s_eq t_eq,
{ left, exact empty_in_sets_eq_bot.1 (s_eq ▸ hs) },
{ right, exact empty_in_sets_eq_bot.1 (t_eq ▸ ht) } },
{ rintros (rfl | rfl),
exact bot_prod,
exact prod_bot }
end
lemma prod_ne_bot {f : filter α} {g : filter β} : f ×ᶠ g ≠ ⊥ ↔ (f ≠ ⊥ ∧ g ≠ ⊥) :=
by rw [(≠), prod_eq_bot, not_or_distrib]
lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} :
filter.tendsto f (x ×ᶠ y) z ↔
∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W :=
by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self]
end prod
/- at_top and at_bot -/
/-- `at_top` is the filter representing the limit `→ ∞` on an ordered set.
It is generated by the collection of up-sets `{b | a ≤ b}`.
(The preorder need not have a top element for this to be well defined,
and indeed is trivial when a top element exists.) -/
def at_top [preorder α] : filter α := ⨅ a, principal {b | a ≤ b}
/-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set.
It is generated by the collection of down-sets `{b | b ≤ a}`.
(The preorder need not have a bottom element for this to be well defined,
and indeed is trivial when a bottom element exists.) -/
def at_bot [preorder α] : filter α := ⨅ a, principal {b | b ≤ a}
lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ @at_top α _ :=
mem_infi_sets a $ subset.refl _
@[simp] lemma at_top_ne_bot [nonempty α] [semilattice_sup α] : (at_top : filter α) ≠ ⊥ :=
infi_ne_bot_of_directed (by apply_instance)
(assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of,
mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩)
(assume a, principal_ne_bot_iff.2 nonempty_Ici)
@[simp, nolint ge_or_gt]
lemma mem_at_top_sets [nonempty α] [semilattice_sup α] {s : set α} :
s ∈ (at_top : filter α) ↔ ∃a:α, ∀b≥a, b ∈ s :=
let ⟨a⟩ := ‹nonempty α› in
iff.intro
(assume h, infi_sets_induct h ⟨a, by simp only [forall_const, mem_univ, forall_true_iff]⟩
(assume a s₁ s₂ ha ⟨b, hb⟩, ⟨a ⊔ b,
assume c hc, ⟨ha $ le_trans le_sup_left hc, hb _ $ le_trans le_sup_right hc⟩⟩)
(assume s₁ s₂ h ⟨a, ha⟩, ⟨a, assume b hb, h $ ha _ hb⟩))
(assume ⟨a, h⟩, mem_infi_sets a $ assume x, h x)
@[nolint ge_or_gt]
lemma eventually_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} :
(∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ b ≥ a, p b) :=
by simp only [filter.eventually, filter.mem_at_top_sets, mem_set_of_eq]
@[nolint ge_or_gt]
lemma eventually.exists_forall_of_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop}
(h : ∀ᶠ x in at_top, p x) : ∃ a, ∀ b ≥ a, p b :=
eventually_at_top.mp h
@[nolint ge_or_gt]
lemma frequently_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} :
(∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b ≥ a, p b) :=
by simp only [filter.frequently, eventually_at_top, not_exists, not_forall, not_not]
@[nolint ge_or_gt]
lemma frequently.forall_exists_of_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop}
(h : ∃ᶠ x in at_top, p x) : ∀ a, ∃ b ≥ a, p b :=
frequently_at_top.mp h
lemma map_at_top_eq [nonempty α] [semilattice_sup α] {f : α → β} :
at_top.map f = (⨅a, principal $ f '' {a' | a ≤ a'}) :=
calc map f (⨅a, principal {a' | a ≤ a'}) = (⨅a, map f $ principal {a' | a ≤ a'}) :
map_infi_eq (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of,
mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩)
(by apply_instance)
... = (⨅a, principal $ f '' {a' | a ≤ a'}) : by simp only [map_principal, eq_self_iff_true]
lemma tendsto_at_top [preorder β] (m : α → β) (f : filter α) :
tendsto m f at_top ↔ (∀b, {a | b ≤ m a} ∈ f) :=
by simp only [at_top, tendsto_infi, tendsto_principal]; refl
lemma tendsto_at_top_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : {x | f₁ x ≤ f₂ x} ∈ l) :
tendsto f₁ l at_top → tendsto f₂ l at_top :=
assume h₁, (tendsto_at_top _ _).2 $ λ b, mp_sets ((tendsto_at_top _ _).1 h₁ b)
(monotone_mem_sets (λ a ha ha₁, le_trans ha₁ ha) h)
lemma tendsto_at_top_mono [preorder β] (l : filter α) :
monotone (λ f : α → β, tendsto f l at_top) :=
λ f₁ f₂ h, tendsto_at_top_mono' l $ univ_mem_sets' h
section ordered_monoid
variables [ordered_cancel_comm_monoid β] (l : filter α) {f g : α → β}
lemma tendsto_at_top_add_nonneg_left' (hf : {x | 0 ≤ f x} ∈ l) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_mono' l (monotone_mem_sets (λ x, le_add_of_nonneg_left) hf) hg
lemma tendsto_at_top_add_nonneg_left (hf : ∀ x, 0 ≤ f x) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_nonneg_left' l (univ_mem_sets' hf) hg
lemma tendsto_at_top_add_nonneg_right' (hf : tendsto f l at_top) (hg : {x | 0 ≤ g x} ∈ l) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_mono' l (monotone_mem_sets (λ x, le_add_of_nonneg_right) hg) hf
lemma tendsto_at_top_add_nonneg_right (hf : tendsto f l at_top) (hg : ∀ x, 0 ≤ g x) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_nonneg_right' l hf (univ_mem_sets' hg)
lemma tendsto_at_top_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_top) :
tendsto f l at_top :=
(tendsto_at_top _ l).2 $ assume b,
monotone_mem_sets (λ x, le_of_add_le_add_left) ((tendsto_at_top _ _).1 hf (C + b))
lemma tendsto_at_top_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_top) :
tendsto f l at_top :=
(tendsto_at_top _ l).2 $ assume b,
monotone_mem_sets (λ x, le_of_add_le_add_right) ((tendsto_at_top _ _).1 hf (b + C))
lemma tendsto_at_top_of_add_bdd_above_left' (C) (hC : {x | f x ≤ C} ∈ l)
(h : tendsto (λ x, f x + g x) l at_top) :
tendsto g l at_top :=
tendsto_at_top_of_add_const_left l C
(tendsto_at_top_mono' l (monotone_mem_sets (λ x (hx : f x ≤ C), add_le_add_right hx (g x)) hC) h)
lemma tendsto_at_top_of_add_bdd_above_left (C) (hC : ∀ x, f x ≤ C) :
tendsto (λ x, f x + g x) l at_top → tendsto g l at_top :=
tendsto_at_top_of_add_bdd_above_left' l C (univ_mem_sets' hC)
lemma tendsto_at_top_of_add_bdd_above_right' (C) (hC : {x | g x ≤ C} ∈ l)
(h : tendsto (λ x, f x + g x) l at_top) :
tendsto f l at_top :=
tendsto_at_top_of_add_const_right l C
(tendsto_at_top_mono' l (monotone_mem_sets (λ x (hx : g x ≤ C), add_le_add_left hx (f x)) hC) h)
lemma tendsto_at_top_of_add_bdd_above_right (C) (hC : ∀ x, g x ≤ C) :
tendsto (λ x, f x + g x) l at_top → tendsto f l at_top :=
tendsto_at_top_of_add_bdd_above_right' l C (univ_mem_sets' hC)
end ordered_monoid
section ordered_group
variables [ordered_comm_group β] (l : filter α) {f g : α → β}
lemma tendsto_at_top_add_left_of_le' (C : β) (hf : {x | C ≤ f x} ∈ l) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
@tendsto_at_top_of_add_bdd_above_left' _ _ _ l (λ x, -(f x)) (λ x, f x + g x) (-C)
(by simp [hf]) (by simp [hg])
lemma tendsto_at_top_add_left_of_le (C : β) (hf : ∀ x, C ≤ f x) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_left_of_le' l C (univ_mem_sets' hf) hg
lemma tendsto_at_top_add_right_of_le' (C : β) (hf : tendsto f l at_top) (hg : {x | C ≤ g x} ∈ l) :
tendsto (λ x, f x + g x) l at_top :=
@tendsto_at_top_of_add_bdd_above_right' _ _ _ l (λ x, f x + g x) (λ x, -(g x)) (-C)
(by simp [hg]) (by simp [hf])
lemma tendsto_at_top_add_right_of_le (C : β) (hf : tendsto f l at_top) (hg : ∀ x, C ≤ g x) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_right_of_le' l C hf (univ_mem_sets' hg)
lemma tendsto_at_top_add_const_left (C : β) (hf : tendsto f l at_top) :
tendsto (λ x, C + f x) l at_top :=
tendsto_at_top_add_left_of_le' l C (univ_mem_sets' $ λ _, le_refl C) hf
lemma tendsto_at_top_add_const_right (C : β) (hf : tendsto f l at_top) :
tendsto (λ x, f x + C) l at_top :=
tendsto_at_top_add_right_of_le' l C hf (univ_mem_sets' $ λ _, le_refl C)
end ordered_group
open_locale filter
@[nolint ge_or_gt]
lemma tendsto_at_top' [nonempty α] [semilattice_sup α] (f : α → β) (l : filter β) :
tendsto f at_top l ↔ (∀s ∈ l, ∃a, ∀b≥a, f b ∈ s) :=
by simp only [tendsto_def, mem_at_top_sets]; refl
@[nolint ge_or_gt]
theorem tendsto_at_top_principal [nonempty β] [semilattice_sup β] {f : β → α} {s : set α} :
tendsto f at_top (principal s) ↔ ∃N, ∀n≥N, f n ∈ s :=
by rw [tendsto_iff_comap, comap_principal, le_principal_iff, mem_at_top_sets]; refl
/-- A function `f` grows to infinity independent of an order-preserving embedding `e`. -/
lemma tendsto_at_top_embedding {α β γ : Type*} [preorder β] [preorder γ]
{f : α → β} {e : β → γ} {l : filter α}
(hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) :
tendsto (e ∘ f) l at_top ↔ tendsto f l at_top :=
begin
rw [tendsto_at_top, tendsto_at_top],
split,
{ assume hc b,
filter_upwards [hc (e b)] assume a, (hm b (f a)).1 },
{ assume hb c,
rcases hu c with ⟨b, hc⟩,
filter_upwards [hb b] assume a ha, le_trans hc ((hm b (f a)).2 ha) }
end
lemma tendsto_at_top_at_top [nonempty α] [semilattice_sup α] [preorder β] (f : α → β) :
tendsto f at_top at_top ↔ ∀ b : β, ∃ i : α, ∀ a : α, i ≤ a → b ≤ f a :=
iff.trans tendsto_infi $ forall_congr $ assume b, tendsto_at_top_principal
@[nolint ge_or_gt]
lemma tendsto_at_top_at_bot [nonempty α] [decidable_linear_order α] [preorder β] (f : α → β) :
tendsto f at_top at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), i ≤ a → b ≥ f a :=
@tendsto_at_top_at_top α (order_dual β) _ _ _ f
lemma tendsto_at_top_at_top_of_monotone [nonempty α] [semilattice_sup α] [preorder β]
{f : α → β} (hf : monotone f) :
tendsto f at_top at_top ↔ ∀ b : β, ∃ a : α, b ≤ f a :=
(tendsto_at_top_at_top f).trans $ forall_congr $ λ b, exists_congr $ λ a,
⟨λ h, h a (le_refl a), λ h a' ha', le_trans h $ hf ha'⟩
alias tendsto_at_top_at_top_of_monotone ← monotone.tendsto_at_top_at_top
lemma tendsto_finset_range : tendsto finset.range at_top at_top :=
finset.range_mono.tendsto_at_top_at_top.2 finset.exists_nat_subset_range
lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : ∀x, j (i x) = x) :
tendsto (finset.image j) at_top at_top :=
have j ∘ i = id, from funext h,
(finset.image_mono j).tendsto_at_top_at_top.2 $ assume s,
⟨s.image i, by simp only [finset.image_image, this, finset.image_id, le_refl]⟩
lemma prod_at_top_at_top_eq {β₁ β₂ : Type*} [nonempty β₁] [nonempty β₂] [semilattice_sup β₁]
[semilattice_sup β₂] : (@at_top β₁ _) ×ᶠ (@at_top β₂ _) = @at_top (β₁ × β₂) _ :=
by inhabit β₁; inhabit β₂;
simp [at_top, prod_infi_left (default β₁), prod_infi_right (default β₂), infi_prod];
exact infi_comm
lemma prod_map_at_top_eq {α₁ α₂ β₁ β₂ : Type*} [nonempty β₁] [nonempty β₂]
[semilattice_sup β₁] [semilattice_sup β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) :
(map u₁ at_top) ×ᶠ (map u₂ at_top) = map (prod.map u₁ u₂) at_top :=
by rw [prod_map_map_eq, prod_at_top_at_top_eq, prod.map_def]
/-- A function `f` maps upwards closed sets (at_top sets) to upwards closed sets when it is a
Galois insertion. The Galois "insertion" and "connection" is weakened to only require it to be an
insertion and a connetion above `b'`. -/
lemma map_at_top_eq_of_gc [semilattice_sup α] [semilattice_sup β] {f : α → β} (g : β → α) (b' : β)(hf : monotone f) (gc : ∀a, ∀b≥b', f a ≤ b ↔ a ≤ g b) (hgi : ∀b≥b', b ≤ f (g b)) :
map f at_top = at_top :=
begin
rw [@map_at_top_eq α _ ⟨g b'⟩],
refine le_antisymm
(le_infi $ assume b, infi_le_of_le (g (b ⊔ b')) $ principal_mono.2 $ image_subset_iff.2 _)
(le_infi $ assume a, infi_le_of_le (f a ⊔ b') $ principal_mono.2 _),
{ assume a ha, exact (le_trans le_sup_left $ le_trans (hgi _ le_sup_right) $ hf ha) },
{ assume b hb,
have hb' : b' ≤ b := le_trans le_sup_right hb,
exact ⟨g b, (gc _ _ hb').1 (le_trans le_sup_left hb),
le_antisymm ((gc _ _ hb').2 (le_refl _)) (hgi _ hb')⟩ }
end
lemma map_add_at_top_eq_nat (k : ℕ) : map (λa, a + k) at_top = at_top :=
map_at_top_eq_of_gc (λa, a - k) k
(assume a b h, add_le_add_right h k)
(assume a b h, (nat.le_sub_right_iff_add_le h).symm)
(assume a h, by rw [nat.sub_add_cancel h])
lemma map_sub_at_top_eq_nat (k : ℕ) : map (λa, a - k) at_top = at_top :=
map_at_top_eq_of_gc (λa, a + k) 0
(assume a b h, nat.sub_le_sub_right h _)
(assume a b _, nat.sub_le_right_iff_le_add)
(assume b _, by rw [nat.add_sub_cancel])
lemma tendso_add_at_top_nat (k : ℕ) : tendsto (λa, a + k) at_top at_top :=
le_of_eq (map_add_at_top_eq_nat k)
lemma tendso_sub_at_top_nat (k : ℕ) : tendsto (λa, a - k) at_top at_top :=
le_of_eq (map_sub_at_top_eq_nat k)
lemma tendsto_add_at_top_iff_nat {f : ℕ → α} {l : filter α} (k : ℕ) :
tendsto (λn, f (n + k)) at_top l ↔ tendsto f at_top l :=
show tendsto (f ∘ (λn, n + k)) at_top l ↔ tendsto f at_top l,
by rw [← tendsto_map'_iff, map_add_at_top_eq_nat]
lemma map_div_at_top_eq_nat (k : ℕ) (hk : k > 0) : map (λa, a / k) at_top = at_top :=
map_at_top_eq_of_gc (λb, b * k + (k - 1)) 1
(assume a b h, nat.div_le_div_right h)
(assume a b _,
calc a / k ≤ b ↔ a / k < b + 1 : by rw [← nat.succ_eq_add_one, nat.lt_succ_iff]
... ↔ a < (b + 1) * k : nat.div_lt_iff_lt_mul _ _ hk
... ↔ _ :
begin
cases k,
exact (lt_irrefl _ hk).elim,
simp [mul_add, add_mul, nat.succ_add, nat.lt_succ_iff]
end)
(assume b _,
calc b = (b * k) / k : by rw [nat.mul_div_cancel b hk]
... ≤ (b * k + (k - 1)) / k : nat.div_le_div_right $ nat.le_add_right _ _)
/- ultrafilter -/
section ultrafilter
open zorn
variables {f g : filter α}
/-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/
def is_ultrafilter (f : filter α) := f ≠ ⊥ ∧ ∀g, g ≠ ⊥ → g ≤ f → f ≤ g
lemma ultrafilter_unique (hg : is_ultrafilter g) (hf : f ≠ ⊥) (h : f ≤ g) : f = g :=
le_antisymm h (hg.right _ hf h)
lemma le_of_ultrafilter {g : filter α} (hf : is_ultrafilter f) (h : f ⊓ g ≠ ⊥) :
f ≤ g :=
le_of_inf_eq $ ultrafilter_unique hf h inf_le_left
/-- Equivalent characterization of ultrafilters:
A filter f is an ultrafilter if and only if for each set s,
-s belongs to f if and only if s does not belong to f. -/
lemma ultrafilter_iff_compl_mem_iff_not_mem :
is_ultrafilter f ↔ (∀ s, -s ∈ f ↔ s ∉ f) :=
⟨assume hf s,
⟨assume hns hs,
hf.1 $ empty_in_sets_eq_bot.mp $ by convert f.inter_sets hs hns; rw [inter_compl_self],
assume hs,
have f ≤ principal (-s), from
le_of_ultrafilter hf $ assume h, hs $ mem_sets_of_eq_bot $
by simp only [h, eq_self_iff_true, compl_compl],
by simp only [le_principal_iff] at this; assumption⟩,
assume hf,
⟨mt empty_in_sets_eq_bot.mpr ((hf ∅).mp (by convert f.univ_sets; rw [compl_empty])),
assume g hg g_le s hs, classical.by_contradiction $ mt (hf s).mpr $
assume : - s ∈ f,
have s ∩ -s ∈ g, from inter_mem_sets hs (g_le this),
by simp only [empty_in_sets_eq_bot, hg, inter_compl_self] at this; contradiction⟩⟩
lemma mem_or_compl_mem_of_ultrafilter (hf : is_ultrafilter f) (s : set α) :
s ∈ f ∨ - s ∈ f :=
classical.or_iff_not_imp_left.2 (ultrafilter_iff_compl_mem_iff_not_mem.mp hf s).mpr
lemma mem_or_mem_of_ultrafilter {s t : set α} (hf : is_ultrafilter f) (h : s ∪ t ∈ f) :
s ∈ f ∨ t ∈ f :=
(mem_or_compl_mem_of_ultrafilter hf s).imp_right
(assume : -s ∈ f, by filter_upwards [this, h] assume x hnx hx, hx.resolve_left hnx)
lemma mem_of_finite_sUnion_ultrafilter {s : set (set α)} (hf : is_ultrafilter f) (hs : finite s)
: ⋃₀ s ∈ f → ∃t∈s, t ∈ f :=
finite.induction_on hs (by simp only [empty_in_sets_eq_bot, hf.left, mem_empty_eq, sUnion_empty,
forall_prop_of_false, exists_false, not_false_iff, exists_prop_of_false]) $
λ t s' ht' hs' ih, by simp only [exists_prop, mem_insert_iff, set.sUnion_insert]; exact
assume h, (mem_or_mem_of_ultrafilter hf h).elim
(assume : t ∈ f, ⟨t, or.inl rfl, this⟩)
(assume h, let ⟨t, hts', ht⟩ := ih h in ⟨t, or.inr hts', ht⟩)
lemma mem_of_finite_Union_ultrafilter {is : set β} {s : β → set α}
(hf : is_ultrafilter f) (his : finite is) (h : (⋃i∈is, s i) ∈ f) : ∃i∈is, s i ∈ f :=
have his : finite (image s is), from finite_image s his,
have h : (⋃₀ image s is) ∈ f, from by simp only [sUnion_image, set.sUnion_image]; assumption,
let ⟨t, ⟨i, hi, h_eq⟩, (ht : t ∈ f)⟩ := mem_of_finite_sUnion_ultrafilter hf his h in
⟨i, hi, h_eq.symm ▸ ht⟩
lemma ultrafilter_map {f : filter α} {m : α → β} (h : is_ultrafilter f) : is_ultrafilter (map m f) :=
by rw ultrafilter_iff_compl_mem_iff_not_mem at ⊢ h; exact assume s, h (m ⁻¹' s)
lemma ultrafilter_pure {a : α} : is_ultrafilter (pure a) :=
begin
rw ultrafilter_iff_compl_mem_iff_not_mem, intro s,
rw [mem_pure_sets, mem_pure_sets], exact iff.rfl
end
lemma ultrafilter_bind {f : filter α} (hf : is_ultrafilter f) {m : α → filter β}
(hm : ∀ a, is_ultrafilter (m a)) : is_ultrafilter (f.bind m) :=
begin
simp only [ultrafilter_iff_compl_mem_iff_not_mem] at ⊢ hf hm, intro s,
dsimp [bind, join, map, preimage],
simp only [hm], apply hf
end
/-- The ultrafilter lemma: Any proper filter is contained in an ultrafilter. -/
lemma exists_ultrafilter (h : f ≠ ⊥) : ∃u, u ≤ f ∧ is_ultrafilter u :=
let
τ := {f' // f' ≠ ⊥ ∧ f' ≤ f},
r : τ → τ → Prop := λt₁ t₂, t₂.val ≤ t₁.val,
⟨a, ha⟩ := nonempty_of_mem_sets h univ_mem_sets,
top : τ := ⟨f, h, le_refl f⟩,
sup : Π(c:set τ), chain r c → τ :=
λc hc, ⟨⨅a:{a:τ // a ∈ insert top c}, a.val.val,
infi_ne_bot_of_directed ⟨a⟩
(directed_of_chain $ chain_insert hc $ assume ⟨b, _, hb⟩ _ _, or.inl hb)
(assume ⟨⟨a, ha, _⟩, _⟩, ha),
infi_le_of_le ⟨top, mem_insert _ _⟩ (le_refl _)⟩
in
have ∀c (hc: chain r c) a (ha : a ∈ c), r a (sup c hc),
from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ (le_refl _),
have (∃ (u : τ), ∀ (a : τ), r u a → r a u),
from exists_maximal_of_chains_bounded (assume c hc, ⟨sup c hc, this c hc⟩) (assume f₁ f₂ f₃ h₁ h₂, le_trans h₂ h₁),
let ⟨uτ, hmin⟩ := this in
⟨uτ.val, uτ.property.right, uτ.property.left, assume g hg₁ hg₂,
hmin ⟨g, hg₁, le_trans hg₂ uτ.property.right⟩ hg₂⟩
/-- Construct an ultrafilter extending a given filter.
The ultrafilter lemma is the assertion that such a filter exists;
we use the axiom of choice to pick one. -/
noncomputable def ultrafilter_of (f : filter α) : filter α :=
if h : f = ⊥ then ⊥ else classical.epsilon (λu, u ≤ f ∧ is_ultrafilter u)
lemma ultrafilter_of_spec (h : f ≠ ⊥) : ultrafilter_of f ≤ f ∧ is_ultrafilter (ultrafilter_of f) :=
begin
have h' := classical.epsilon_spec (exists_ultrafilter h),
simp only [ultrafilter_of, dif_neg, h, dif_neg, not_false_iff],
simp only at h',
assumption
end
lemma ultrafilter_of_le : ultrafilter_of f ≤ f :=
if h : f = ⊥ then by simp only [ultrafilter_of, dif_pos, h, dif_pos, eq_self_iff_true, le_bot_iff]; exact le_refl _
else (ultrafilter_of_spec h).left
lemma ultrafilter_ultrafilter_of (h : f ≠ ⊥) : is_ultrafilter (ultrafilter_of f) :=
(ultrafilter_of_spec h).right
lemma ultrafilter_of_ultrafilter (h : is_ultrafilter f) : ultrafilter_of f = f :=
ultrafilter_unique h (ultrafilter_ultrafilter_of h.left).left ultrafilter_of_le
/-- A filter equals the intersection of all the ultrafilters which contain it. -/
lemma sup_of_ultrafilters (f : filter α) : f = ⨆ (g) (u : is_ultrafilter g) (H : g ≤ f), g :=
begin
refine le_antisymm _ (supr_le $ λ g, supr_le $ λ u, supr_le $ λ H, H),
intros s hs,
-- If s ∉ f.sets, we'll apply the ultrafilter lemma to the restriction of f to -s.
by_contradiction hs',
let j : (-s) → α := subtype.val,
have j_inv_s : j ⁻¹' s = ∅, by
erw [←preimage_inter_range, subtype.val_range, inter_compl_self, preimage_empty],
let f' := comap j f,
have : f' ≠ ⊥,
{ apply mt empty_in_sets_eq_bot.mpr,
rintro ⟨t, htf, ht⟩,
suffices : t ⊆ s, from absurd (f.sets_of_superset htf this) hs',
rw [subset_empty_iff] at ht,
have : j '' (j ⁻¹' t) = ∅, by rw [ht, image_empty],
erw [image_preimage_eq_inter_range, subtype.val_range, ←subset_compl_iff_disjoint,
set.compl_compl] at this,
exact this },
rcases exists_ultrafilter this with ⟨g', g'f', u'⟩,
simp only [supr_sets_eq, mem_Inter] at hs,
have := hs (g'.map subtype.val) (ultrafilter_map u') (map_le_iff_le_comap.mpr g'f'),
rw [←le_principal_iff, map_le_iff_le_comap, comap_principal, j_inv_s, principal_empty,
le_bot_iff] at this,
exact absurd this u'.1
end
/-- The `tendsto` relation can be checked on ultrafilters. -/
lemma tendsto_iff_ultrafilter (f : α → β) (l₁ : filter α) (l₂ : filter β) :
tendsto f l₁ l₂ ↔ ∀ g, is_ultrafilter g → g ≤ l₁ → g.map f ≤ l₂ :=
⟨assume h g u gx, le_trans (map_mono gx) h,
assume h, by rw [sup_of_ultrafilters l₁]; simpa only [tendsto, map_supr, supr_le_iff]⟩
/-- The ultrafilter monad. The monad structure on ultrafilters is the
restriction of the one on filters. -/
def ultrafilter (α : Type u) : Type u := {f : filter α // is_ultrafilter f}
/-- Push-forward for ultra-filters. -/
def ultrafilter.map (m : α → β) (u : ultrafilter α) : ultrafilter β :=
⟨u.val.map m, ultrafilter_map u.property⟩
/-- The principal ultra-filter associated to a point `x`. -/
def ultrafilter.pure (x : α) : ultrafilter α := ⟨pure x, ultrafilter_pure⟩
/-- Monadic bind for ultra-filters, coming from the one on filters
defined in terms of map and join.-/
def ultrafilter.bind (u : ultrafilter α) (m : α → ultrafilter β) : ultrafilter β :=
⟨u.val.bind (λ a, (m a).val), ultrafilter_bind u.property (λ a, (m a).property)⟩
instance ultrafilter.has_pure : has_pure ultrafilter := ⟨@ultrafilter.pure⟩
instance ultrafilter.has_bind : has_bind ultrafilter := ⟨@ultrafilter.bind⟩
instance ultrafilter.functor : functor ultrafilter := { map := @ultrafilter.map }
instance ultrafilter.monad : monad ultrafilter := { map := @ultrafilter.map }
instance ultrafilter.inhabited [inhabited α] : inhabited (ultrafilter α) := ⟨pure (default _)⟩
/-- The ultra-filter extending the cofinite filter. -/
noncomputable def hyperfilter : filter α := ultrafilter_of cofinite
lemma hyperfilter_le_cofinite : @hyperfilter α ≤ cofinite :=
ultrafilter_of_le
lemma is_ultrafilter_hyperfilter [infinite α] : is_ultrafilter (@hyperfilter α) :=
(ultrafilter_of_spec cofinite_ne_bot).2
theorem nmem_hyperfilter_of_finite [infinite α] {s : set α} (hf : s.finite) :
s ∉ @hyperfilter α :=
λ hy,
have hx : -s ∉ hyperfilter :=
λ hs, (ultrafilter_iff_compl_mem_iff_not_mem.mp is_ultrafilter_hyperfilter s).mp hs hy,
have ht : -s ∈ cofinite.sets := by show -s ∈ {s | _}; rwa [set.mem_set_of_eq, compl_compl],
hx $ hyperfilter_le_cofinite ht
theorem compl_mem_hyperfilter_of_finite [infinite α] {s : set α} (hf : set.finite s) :
-s ∈ @hyperfilter α :=
(ultrafilter_iff_compl_mem_iff_not_mem.mp is_ultrafilter_hyperfilter s).mpr $
nmem_hyperfilter_of_finite hf
theorem mem_hyperfilter_of_finite_compl [infinite α] {s : set α} (hf : set.finite (-s)) :
s ∈ @hyperfilter α :=
s.compl_compl ▸ compl_mem_hyperfilter_of_finite hf
section
local attribute [instance] filter.monad filter.is_lawful_monad
instance ultrafilter.is_lawful_monad : is_lawful_monad ultrafilter :=
{ id_map := assume α f, subtype.eq (id_map f.val),
pure_bind := assume α β a f, subtype.eq (pure_bind a (subtype.val ∘ f)),
bind_assoc := assume α β γ f m₁ m₂, subtype.eq (filter_eq rfl),
bind_pure_comp_eq_map := assume α β f x, subtype.eq (bind_pure_comp_eq_map _ f x.val) }
end
lemma ultrafilter.eq_iff_val_le_val {u v : ultrafilter α} : u = v ↔ u.val ≤ v.val :=
⟨assume h, by rw h; exact le_refl _,
assume h, by rw subtype.ext; apply ultrafilter_unique v.property u.property.1 h⟩
lemma exists_ultrafilter_iff (f : filter α) : (∃ (u : ultrafilter α), u.val ≤ f) ↔ f ≠ ⊥ :=
⟨assume ⟨u, uf⟩, ne_bot_of_le_ne_bot u.property.1 uf,
assume h, let ⟨u, uf, hu⟩ := exists_ultrafilter h in ⟨⟨u, hu⟩, uf⟩⟩
end ultrafilter
end filter
namespace filter
variables {α β γ : Type u} {f : β → filter α} {s : γ → set α}
open list
lemma mem_traverse_sets :
∀(fs : list β) (us : list γ),
forall₂ (λb c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs
| [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _
| (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs)
lemma mem_traverse_sets_iff (fs : list β) (t : set (list α)) :
t ∈ traverse f fs ↔
(∃us:list (set α), forall₂ (λb (s : set α), s ∈ f b) fs us ∧ sequence us ⊆ t) :=
begin
split,
{ induction fs generalizing t,
case nil { simp only [sequence, mem_pure_sets, imp_self, forall₂_nil_left_iff,
exists_eq_left, set.pure_def, singleton_subset_iff, traverse_nil] },
case cons : b fs ih t {
assume ht,
rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩,
rcases ih v hv with ⟨us, hus, hu⟩,
exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } },
{ rintros ⟨us, hus, hs⟩,
exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs }
end
lemma sequence_mono :
∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs
| [] [] forall₂.nil := le_refl _
| (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs)
end filter
open filter
lemma set.infinite_iff_frequently_cofinite {α : Type u} {s : set α} :
set.infinite s ↔ (∃ᶠ x in cofinite, x ∈ s) :=
frequently_cofinite_iff_infinite.symm
/-- For natural numbers the filters `cofinite` and `at_top` coincide. -/
lemma nat.cofinite_eq_at_top : @cofinite ℕ = at_top :=
begin
ext s,
simp only [mem_cofinite, mem_at_top_sets],
split,
{ assume hs,
use (hs.to_finset.sup id) + 1,
assume b hb,
by_contradiction hbs,
have := hs.to_finset.subset_range_sup_succ (finite.mem_to_finset.2 hbs),
exact not_lt_of_le hb (finset.mem_range.1 this) },
{ rintros ⟨N, hN⟩,
apply finite_subset (finite_lt_nat N),
assume n hn,
change n < N,
exact lt_of_not_ge (λ hn', hn $ hN n hn') }
end
|
225637e198910552eb215353e1c71442e337bc8b | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/linear_algebra/linear_independent.lean | 93c9195ec89145d396e35b8e4b00fcc0e71897da | [
"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 | 57,135 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Alexander Bentkamp, Anne Baanen
-/
import linear_algebra.finsupp
import linear_algebra.prod
import data.equiv.fin
import set_theory.cardinal
/-!
# Linear independence
This file defines linear independence in a module or vector space.
It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light.
We define `linear_independent R v` as `ker (finsupp.total ι M R v) = ⊥`. Here `finsupp.total` is the
linear map sending a function `f : ι →₀ R` with finite support to the linear combination of vectors
from `v` with these coefficients. Then we prove that several other statements are equivalent to this
one, including injectivity of `finsupp.total ι M R v` and some versions with explicitly written
linear combinations.
## Main definitions
All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or
vector space and `ι : Type*` is an arbitrary indexing type.
* `linear_independent R v` states that the elements of the family `v` are linearly independent.
* `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)`
on the linearly independent vectors `v`, given `hv : linear_independent R v`
(using classical choice). `linear_independent.repr hv` is provided as a linear map.
## Main statements
We prove several specialized tests for linear independence of families of vectors and of sets of
vectors.
* `fintype.linear_independent_iff`: if `ι` is a finite type, then any function `f : ι → R` has
finite support, so we can reformulate the statement using `∑ i : ι, f i • v i` instead of a sum
over an auxiliary `s : finset ι`;
* `linear_independent_empty_type`: a family indexed by an empty type is linearly independent;
* `linear_independent_unique_iff`: if `ι` is a singleton, then `linear_independent K v` is
equivalent to `v default ≠ 0`;
* linear_independent_option`, `linear_independent_sum`, `linear_independent_fin_cons`,
`linear_independent_fin_succ`: type-specific tests for linear independence of families of vector
fields;
* `linear_independent_insert`, `linear_independent_union`, `linear_independent_pair`,
`linear_independent_singleton`: linear independence tests for set operations.
In many cases we additionally provide dot-style operations (e.g., `linear_independent.union`) to
make the linear independence tests usable as `hv.insert ha` etc.
We also prove that, when working over a division ring,
any family of vectors includes a linear independent subfamily spanning the same subspace.
## Implementation notes
We use families instead of sets because it allows us to say that two identical vectors are linearly
dependent.
If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas
`linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two
worlds.
## Tags
linearly dependent, linear dependence, linearly independent, linear independence
-/
noncomputable theory
open function set submodule
open_locale classical big_operators cardinal
universes u
variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*}
variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*}
section module
variables {v : ι → M}
variables [semiring R] [add_comm_monoid M] [add_comm_monoid M'] [add_comm_monoid M'']
variables [module R M] [module R M'] [module R M'']
variables {a b : R} {x y : M}
variables (R) (v)
/-- `linear_independent R v` states the family of vectors `v` is linearly independent over `R`. -/
def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥
variables {R} {v}
theorem linear_independent_iff : linear_independent R v ↔
∀l, finsupp.total ι M R v l = 0 → l = 0 :=
by simp [linear_independent, linear_map.ker_eq_bot']
theorem linear_independent_iff' : linear_independent R v ↔
∀ s : finset ι, ∀ g : ι → R, ∑ i in s, g i • v i = 0 → ∀ i ∈ s, g i = 0 :=
linear_independent_iff.trans
⟨λ hf s g hg i his, have h : _ := hf (∑ i in s, finsupp.single i (g i)) $
by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc
g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) :
by rw [finsupp.lapply_apply, finsupp.single_eq_same]
... = ∑ j in s, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j)) :
eq.symm $ finset.sum_eq_single i
(λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji])
(λ hnis, hnis.elim his)
... = (∑ j in s, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm
... = 0 : finsupp.ext_iff.1 h i,
λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $
finsupp.mem_support_iff.2 hni⟩
theorem linear_independent_iff'' :
linear_independent R v ↔ ∀ (s : finset ι) (g : ι → R) (hg : ∀ i ∉ s, g i = 0),
∑ i in s, g i • v i = 0 → ∀ i, g i = 0 :=
linear_independent_iff'.trans ⟨λ H s g hg hv i, if his : i ∈ s then H s g hv i his else hg i his,
λ H s g hg i hi, by { convert H s (λ j, if j ∈ s then g j else 0) (λ j hj, if_neg hj)
(by simp_rw [ite_smul, zero_smul, finset.sum_extend_by_zero, hg]) i,
exact (if_pos hi).symm }⟩
theorem not_linear_independent_iff : ¬ linear_independent R v ↔
∃ s : finset ι, ∃ g : ι → R, (∑ i in s, g i • v i) = 0 ∧ (∃ i ∈ s, g i ≠ 0) :=
begin
rw linear_independent_iff',
simp only [exists_prop, not_forall],
end
theorem fintype.linear_independent_iff [fintype ι] :
linear_independent R v ↔ ∀ g : ι → R, ∑ i, g i • v i = 0 → ∀ i, g i = 0 :=
begin
refine ⟨λ H g, by simpa using linear_independent_iff'.1 H finset.univ g,
λ H, linear_independent_iff''.2 $ λ s g hg hs i, H _ _ _⟩,
rw ← hs,
refine (finset.sum_subset (finset.subset_univ _) (λ i _ hi, _)).symm,
rw [hg i hi, zero_smul]
end
/-- A finite family of vectors `v i` is linear independent iff the linear map that sends
`c : ι → R` to `∑ i, c i • v i` has the trivial kernel. -/
theorem fintype.linear_independent_iff' [fintype ι] :
linear_independent R v ↔
(linear_map.lsum R (λ i : ι, R) ℕ (λ i, linear_map.id.smul_right (v i))).ker = ⊥ :=
by simp [fintype.linear_independent_iff, linear_map.ker_eq_bot', funext_iff]
lemma fintype.not_linear_independent_iff [fintype ι] :
¬linear_independent R v ↔ ∃ g : ι → R, (∑ i, g i • v i) = 0 ∧ (∃ i, g i ≠ 0) :=
by simpa using (not_iff_not.2 fintype.linear_independent_iff)
lemma linear_independent_empty_type [is_empty ι] : linear_independent R v :=
linear_independent_iff.mpr $ λ v hv, subsingleton.elim v 0
lemma linear_independent.ne_zero [nontrivial R]
(i : ι) (hv : linear_independent R v) : v i ≠ 0 :=
λ h, @zero_ne_one R _ _ $ eq.symm begin
suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa},
rw linear_independent_iff.1 hv (finsupp.single i 1),
{ simp },
{ simp [h] }
end
/-- A subfamily of a linearly independent family (i.e., a composition with an injective map) is a
linearly independent family. -/
lemma linear_independent.comp
(h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) :=
begin
rw [linear_independent_iff, finsupp.total_comp],
intros l hl,
have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0,
by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp,
ext x,
convert h_map_domain x,
rw [finsupp.map_domain_apply hf]
end
lemma linear_independent.coe_range (i : linear_independent R v) :
linear_independent R (coe : range v → M) :=
by simpa using i.comp _ (range_splitting_injective v)
/-- If `v` is a linearly independent family of vectors and the kernel of a linear map `f` is
disjoint with the submodule spanned by the vectors of `v`, then `f ∘ v` is a linearly independent
family of vectors. See also `linear_independent.map'` for a special case assuming `ker f = ⊥`. -/
lemma linear_independent.map (hv : linear_independent R v) {f : M →ₗ[R] M'}
(hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) :=
begin
rw [disjoint, ← set.image_univ, finsupp.span_image_eq_map_total, map_inf_eq_map_inf_comap,
map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj,
unfold linear_independent at hv ⊢,
rw [hv, le_bot_iff] at hf_inj,
haveI : inhabited M := ⟨0⟩,
rw [finsupp.total_comp, @finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f,
linear_map.ker_comp, hf_inj],
exact λ _, rfl,
end
/-- An injective linear map sends linearly independent families of vectors to linearly independent
families of vectors. See also `linear_independent.map` for a more general statement. -/
lemma linear_independent.map' (hv : linear_independent R v) (f : M →ₗ[R] M')
(hf_inj : f.ker = ⊥) : linear_independent R (f ∘ v) :=
hv.map $ by simp [hf_inj]
/-- If the image of a family of vectors under a linear map is linearly independent, then so is
the original family. -/
lemma linear_independent.of_comp (f : M →ₗ[R] M') (hfv : linear_independent R (f ∘ v)) :
linear_independent R v :=
linear_independent_iff'.2 $ λ s g hg i his,
have ∑ (i : ι) in s, g i • f (v i) = 0,
by simp_rw [← f.map_smul, ← f.map_sum, hg, f.map_zero],
linear_independent_iff'.1 hfv s g this i his
/-- If `f` is an injective linear map, then the family `f ∘ v` is linearly independent
if and only if the family `v` is linearly independent. -/
protected lemma linear_map.linear_independent_iff (f : M →ₗ[R] M') (hf_inj : f.ker = ⊥) :
linear_independent R (f ∘ v) ↔ linear_independent R v :=
⟨λ h, h.of_comp f, λ h, h.map $ by simp only [hf_inj, disjoint_bot_right]⟩
@[nontriviality]
lemma linear_independent_of_subsingleton [subsingleton R] : linear_independent R v :=
linear_independent_iff.2 (λ l hl, subsingleton.elim _ _)
theorem linear_independent_equiv (e : ι ≃ ι') {f : ι' → M} :
linear_independent R (f ∘ e) ↔ linear_independent R f :=
⟨λ h, function.comp.right_id f ▸ e.self_comp_symm ▸ h.comp _ e.symm.injective,
λ h, h.comp _ e.injective⟩
theorem linear_independent_equiv' (e : ι ≃ ι') {f : ι' → M} {g : ι → M} (h : f ∘ e = g) :
linear_independent R g ↔ linear_independent R f :=
h ▸ linear_independent_equiv e
theorem linear_independent_subtype_range {ι} {f : ι → M} (hf : injective f) :
linear_independent R (coe : range f → M) ↔ linear_independent R f :=
iff.symm $ linear_independent_equiv' (equiv.of_injective f hf) rfl
alias linear_independent_subtype_range ↔ linear_independent.of_subtype_range _
theorem linear_independent_image {ι} {s : set ι} {f : ι → M} (hf : set.inj_on f s) :
linear_independent R (λ x : s, f x) ↔ linear_independent R (λ x : f '' s, (x : M)) :=
linear_independent_equiv' (equiv.set.image_of_inj_on _ _ hf) rfl
lemma linear_independent_span (hs : linear_independent R v) :
@linear_independent ι R (span R (range v))
(λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ :=
linear_independent.of_comp (span R (range v)).subtype hs
/-- See `linear_independent.fin_cons` for a family of elements in a vector space. -/
lemma linear_independent.fin_cons' {m : ℕ} (x : M) (v : fin m → M)
(hli : linear_independent R v)
(x_ortho : (∀ (c : R) (y : submodule.span R (set.range v)), c • x + y = (0 : M) → c = 0)) :
linear_independent R (fin.cons x v : fin m.succ → M) :=
begin
rw fintype.linear_independent_iff at hli ⊢,
rintros g total_eq j,
have zero_not_mem : (0 : fin m.succ) ∉ finset.univ.image (fin.succ : fin m → fin m.succ),
{ rw finset.mem_image,
rintro ⟨x, hx, succ_eq⟩,
exact fin.succ_ne_zero _ succ_eq },
simp only [submodule.coe_mk, fin.univ_succ, finset.sum_insert zero_not_mem,
fin.cons_zero, fin.cons_succ,
forall_true_iff, imp_self, fin.succ_inj, finset.sum_image] at total_eq,
have : g 0 = 0,
{ refine x_ortho (g 0) ⟨∑ (i : fin m), g i.succ • v i, _⟩ total_eq,
exact sum_mem _ (λ i _, smul_mem _ _ (subset_span ⟨i, rfl⟩)) },
refine fin.cases this (λ j, _) j,
apply hli (λ i, g i.succ),
simpa only [this, zero_smul, zero_add] using total_eq
end
/-- A set of linearly independent vectors in a module `M` over a semiring `K` is also linearly
independent over a subring `R` of `K`.
The implementation uses minimal assumptions about the relationship between `R`, `K` and `M`.
The version where `K` is an `R`-algebra is `linear_independent.restrict_scalars_algebras`.
-/
lemma linear_independent.restrict_scalars [semiring K] [smul_with_zero R K] [module K M]
[is_scalar_tower R K M]
(hinj : function.injective (λ r : R, r • (1 : K))) (li : linear_independent K v) :
linear_independent R v :=
begin
refine linear_independent_iff'.mpr (λ s g hg i hi, hinj (eq.trans _ (zero_smul _ _).symm)),
refine (linear_independent_iff'.mp li : _) _ _ _ i hi,
simp_rw [smul_assoc, one_smul],
exact hg,
end
/-- Every finite subset of a linearly independent set is linearly independent. -/
lemma linear_independent_finset_map_embedding_subtype
(s : set M) (li : linear_independent R (coe : s → M)) (t : finset s) :
linear_independent R (coe : (finset.map (embedding.subtype s) t) → M) :=
begin
let f : t.map (embedding.subtype s) → s := λ x, ⟨x.1, begin
obtain ⟨x, h⟩ := x,
rw [finset.mem_map] at h,
obtain ⟨a, ha, rfl⟩ := h,
simp only [subtype.coe_prop, embedding.coe_subtype],
end⟩,
convert linear_independent.comp li f _,
rintros ⟨x, hx⟩ ⟨y, hy⟩,
rw [finset.mem_map] at hx hy,
obtain ⟨a, ha, rfl⟩ := hx,
obtain ⟨b, hb, rfl⟩ := hy,
simp only [imp_self, subtype.mk_eq_mk],
end
/--
If every finite set of linearly independent vectors has cardinality at most `n`,
then the same is true for arbitrary sets of linearly independent vectors.
-/
lemma linear_independent_bounded_of_finset_linear_independent_bounded {n : ℕ}
(H : ∀ s : finset M, linear_independent R (λ i : s, (i : M)) → s.card ≤ n) :
∀ s : set M, linear_independent R (coe : s → M) → #s ≤ n :=
begin
intros s li,
apply cardinal.card_le_of,
intro t,
rw ← finset.card_map (embedding.subtype s),
apply H,
apply linear_independent_finset_map_embedding_subtype _ li,
end
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
theorem linear_independent_comp_subtype {s : set ι} :
linear_independent R (v ∘ coe : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 :=
begin
simp only [linear_independent_iff, (∘), finsupp.mem_supported, finsupp.total_apply,
set.subset_def, finset.mem_coe],
split,
{ intros h l hl₁ hl₂,
have := h (l.subtype_domain s) ((finsupp.sum_subtype_domain_index hl₁).trans hl₂),
exact (finsupp.subtype_domain_eq_zero_iff hl₁).1 this },
{ intros h l hl,
refine finsupp.emb_domain_eq_zero.1 (h (l.emb_domain $ function.embedding.subtype s) _ _),
{ suffices : ∀ i hi, ¬l ⟨i, hi⟩ = 0 → i ∈ s, by simpa,
intros, assumption },
{ rwa [finsupp.emb_domain_eq_map_domain, finsupp.sum_map_domain_index],
exacts [λ _, zero_smul _ _, λ _ _ _, add_smul _ _ _] } }
end
lemma linear_dependent_comp_subtype' {s : set ι} :
¬ linear_independent R (v ∘ coe : s → M) ↔
∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ finsupp.total ι M R v f = 0 ∧ f ≠ 0 :=
by simp [linear_independent_comp_subtype]
/-- A version of `linear_dependent_comp_subtype'` with `finsupp.total` unfolded. -/
lemma linear_dependent_comp_subtype {s : set ι} :
¬ linear_independent R (v ∘ coe : s → M) ↔
∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ ∑ i in f.support, f i • v i = 0 ∧ f ≠ 0 :=
linear_dependent_comp_subtype'
theorem linear_independent_subtype {s : set M} :
linear_independent R (λ x, x : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 :=
by apply @linear_independent_comp_subtype _ _ _ id
theorem linear_independent_comp_subtype_disjoint {s : set ι} :
linear_independent R (v ∘ coe : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker :=
by rw [linear_independent_comp_subtype, linear_map.disjoint_ker]
theorem linear_independent_subtype_disjoint {s : set M} :
linear_independent R (λ x, x : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker :=
by apply @linear_independent_comp_subtype_disjoint _ _ _ id
theorem linear_independent_iff_total_on {s : set M} :
linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ :=
by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, map_bot, comap_bot,
linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint, ← map_comap_subtype,
map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff]
lemma linear_independent.restrict_of_comp_subtype {s : set ι}
(hs : linear_independent R (v ∘ coe : s → M)) :
linear_independent R (s.restrict v) :=
hs
variables (R M)
lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) :=
by simp [linear_independent_subtype_disjoint]
variables {R M}
lemma linear_independent.mono {t s : set M} (h : t ⊆ s) :
linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) :=
begin
simp only [linear_independent_subtype_disjoint],
exact (disjoint.mono_left (finsupp.supported_mono h))
end
lemma linear_independent_of_finite (s : set M)
(H : ∀ t ⊆ s, finite t → linear_independent R (λ x, x : t → M)) :
linear_independent R (λ x, x : s → M) :=
linear_independent_subtype.2 $
λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _)
lemma linear_independent_Union_of_directed {η : Type*}
{s : η → set M} (hs : directed (⊆) s)
(h : ∀ i, linear_independent R (λ x, x : s i → M)) :
linear_independent R (λ x, x : (⋃ i, s i) → M) :=
begin
by_cases hη : nonempty η,
{ resetI,
refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _),
rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩,
rcases hs.finset_le fi.to_finset with ⟨i, hi⟩,
exact (h i).mono (subset.trans hI $ Union₂_subset $
λ j hj, hi j (fi.mem_to_finset.2 hj)) },
{ refine (linear_independent_empty _ _).mono _,
rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ }
end
lemma linear_independent_sUnion_of_directed {s : set (set M)}
(hs : directed_on (⊆) s)
(h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) :
linear_independent R (λ x, x : (⋃₀ s) → M) :=
by rw sUnion_eq_Union; exact
linear_independent_Union_of_directed hs.directed_coe (by simpa using h)
lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M}
(hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) :
linear_independent R (λ x, x : (⋃a∈s, t a) → M) :=
by rw bUnion_eq_Union; exact
linear_independent_Union_of_directed (directed_comp.2 $ hs.directed_coe) (by simpa using h)
end subtype
end module
/-! ### Properties which require `ring R` -/
section module
variables {v : ι → M}
variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M'']
variables [module R M] [module R M'] [module R M'']
variables {a b : R} {x y : M}
theorem linear_independent_iff_injective_total : linear_independent R v ↔
function.injective (finsupp.total ι M R v) :=
linear_independent_iff.trans (finsupp.total ι M R v).to_add_monoid_hom.injective_iff.symm
alias linear_independent_iff_injective_total ↔ linear_independent.injective_total _
lemma linear_independent.injective [nontrivial R] (hv : linear_independent R v) :
injective v :=
begin
intros i j hij,
let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1,
have h_total : finsupp.total ι M R v l = 0,
{ simp_rw [linear_map.map_sub, finsupp.total_apply],
simp [hij] },
have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1,
{ rw linear_independent_iff at hv,
simp [eq_add_of_sub_eq' (hv l h_total)] },
simpa [finsupp.single_eq_single_iff] using h_single_eq
end
theorem linear_independent.to_subtype_range {ι} {f : ι → M} (hf : linear_independent R f) :
linear_independent R (coe : range f → M) :=
begin
nontriviality R,
exact (linear_independent_subtype_range hf.injective).2 hf
end
theorem linear_independent.to_subtype_range' {ι} {f : ι → M} (hf : linear_independent R f)
{t} (ht : range f = t) :
linear_independent R (coe : t → M) :=
ht ▸ hf.to_subtype_range
theorem linear_independent.image_of_comp {ι ι'} (s : set ι) (f : ι → ι') (g : ι' → M)
(hs : linear_independent R (λ x : s, g (f x))) :
linear_independent R (λ x : f '' s, g x) :=
begin
nontriviality R,
have : inj_on f s, from inj_on_iff_injective.2 hs.injective.of_comp,
exact (linear_independent_equiv' (equiv.set.image_of_inj_on f s this) rfl).1 hs
end
theorem linear_independent.image {ι} {s : set ι} {f : ι → M}
(hs : linear_independent R (λ x : s, f x)) : linear_independent R (λ x : f '' s, (x : M)) :=
by convert linear_independent.image_of_comp s f id hs
lemma linear_independent.group_smul
{G : Type*} [hG : group G] [distrib_mul_action G R] [distrib_mul_action G M]
[is_scalar_tower G R M] [smul_comm_class G R M] {v : ι → M} (hv : linear_independent R v)
(w : ι → G) : linear_independent R (w • v) :=
begin
rw linear_independent_iff'' at hv ⊢,
intros s g hgs hsum i,
refine (smul_eq_zero_iff_eq (w i)).1 _,
refine hv s (λ i, w i • g i) (λ i hi, _) _ i,
{ dsimp only,
exact (hgs i hi).symm ▸ smul_zero _ },
{ rw [← hsum, finset.sum_congr rfl _],
intros, erw [pi.smul_apply, smul_assoc, smul_comm] },
end
-- This lemma cannot be proved with `linear_independent.group_smul` since the action of
-- `Rˣ` on `R` is not commutative.
lemma linear_independent.units_smul {v : ι → M} (hv : linear_independent R v)
(w : ι → Rˣ) : linear_independent R (w • v) :=
begin
rw linear_independent_iff'' at hv ⊢,
intros s g hgs hsum i,
rw ← (w i).mul_left_eq_zero,
refine hv s (λ i, g i • w i) (λ i hi, _) _ i,
{ dsimp only,
exact (hgs i hi).symm ▸ zero_smul _ _ },
{ rw [← hsum, finset.sum_congr rfl _],
intros,
erw [pi.smul_apply, smul_assoc],
refl }
end
section maximal
universes v w
/--
A linearly independent family is maximal if there is no strictly larger linearly independent family.
-/
@[nolint unused_arguments]
def linear_independent.maximal {ι : Type w} {R : Type u} [semiring R]
{M : Type v} [add_comm_monoid M] [module R M] {v : ι → M} (i : linear_independent R v) : Prop :=
∀ (s : set M) (i' : linear_independent R (coe : s → M)) (h : range v ≤ s), range v = s
/--
An alternative characterization of a maximal linearly independent family,
quantifying over types (in the same universe as `M`) into which the indexing family injects.
-/
lemma linear_independent.maximal_iff {ι : Type w} {R : Type u} [ring R] [nontrivial R]
{M : Type v} [add_comm_group M] [module R M] {v : ι → M} (i : linear_independent R v) :
i.maximal ↔ ∀ (κ : Type v) (w : κ → M) (i' : linear_independent R w)
(j : ι → κ) (h : w ∘ j = v), surjective j :=
begin
fsplit,
{ rintros p κ w i' j rfl,
specialize p (range w) i'.coe_range (range_comp_subset_range _ _),
rw [range_comp, ←@image_univ _ _ w] at p,
exact range_iff_surjective.mp (image_injective.mpr i'.injective p), },
{ intros p w i' h,
specialize p w (coe : w → M) i'
(λ i, ⟨v i, range_subset_iff.mp h i⟩)
(by { ext, simp, }),
have q := congr_arg (λ s, (coe : w → M) '' s) p.range_eq,
dsimp at q,
rw [←image_univ, image_image] at q,
simpa using q, },
end
end maximal
/-- Linear independent families are injective, even if you multiply either side. -/
lemma linear_independent.eq_of_smul_apply_eq_smul_apply {M : Type*} [add_comm_group M] [module R M]
{v : ι → M} (li : linear_independent R v) (c d : R) (i j : ι)
(hc : c ≠ 0) (h : c • v i = d • v j) : i = j :=
begin
let l : ι →₀ R := finsupp.single i c - finsupp.single j d,
have h_total : finsupp.total ι M R v l = 0,
{ simp_rw [linear_map.map_sub, finsupp.total_apply],
simp [h] },
have h_single_eq : finsupp.single i c = finsupp.single j d,
{ rw linear_independent_iff at li,
simp [eq_add_of_sub_eq' (li l h_total)] },
rcases (finsupp.single_eq_single_iff _ _ _ _).mp h_single_eq with ⟨this, _⟩ | ⟨hc, _⟩,
{ exact this },
{ contradiction },
end
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
lemma linear_independent.disjoint_span_image (hv : linear_independent R v) {s t : set ι}
(hs : disjoint s t) :
disjoint (submodule.span R $ v '' s) (submodule.span R $ v '' t) :=
begin
simp only [disjoint_def, finsupp.mem_span_image_iff_total],
rintros _ ⟨l₁, hl₁, rfl⟩ ⟨l₂, hl₂, H⟩,
rw [hv.injective_total.eq_iff] at H, subst l₂,
have : l₁ = 0 := finsupp.disjoint_supported_supported hs (submodule.mem_inf.2 ⟨hl₁, hl₂⟩),
simp [this]
end
lemma linear_independent.not_mem_span_image [nontrivial R] (hv : linear_independent R v) {s : set ι}
{x : ι} (h : x ∉ s) :
v x ∉ submodule.span R (v '' s) :=
begin
have h' : v x ∈ submodule.span R (v '' {x}),
{ rw set.image_singleton,
exact mem_span_singleton_self (v x), },
intro w,
apply linear_independent.ne_zero x hv,
refine disjoint_def.1 (hv.disjoint_span_image _) (v x) h' w,
simpa using h,
end
lemma linear_independent.total_ne_of_not_mem_support [nontrivial R] (hv : linear_independent R v)
{x : ι} (f : ι →₀ R) (h : x ∉ f.support) :
finsupp.total ι M R v f ≠ v x :=
begin
replace h : x ∉ (f.support : set ι) := h,
have p := hv.not_mem_span_image h,
intro w,
rw ←w at p,
rw finsupp.span_image_eq_map_total at p,
simp only [not_exists, not_and, mem_map] at p,
exact p f (f.mem_supported_support R) rfl,
end
lemma linear_independent_sum {v : ι ⊕ ι' → M} :
linear_independent R v ↔ linear_independent R (v ∘ sum.inl) ∧
linear_independent R (v ∘ sum.inr) ∧
disjoint (submodule.span R (range (v ∘ sum.inl))) (submodule.span R (range (v ∘ sum.inr))) :=
begin
rw [range_comp v, range_comp v],
refine ⟨λ h, ⟨h.comp _ sum.inl_injective, h.comp _ sum.inr_injective,
h.disjoint_span_image is_compl_range_inl_range_inr.1⟩, _⟩,
rintro ⟨hl, hr, hlr⟩,
rw [linear_independent_iff'] at *,
intros s g hg i hi,
have : ∑ i in s.preimage sum.inl (sum.inl_injective.inj_on _), (λ x, g x • v x) (sum.inl i) +
∑ i in s.preimage sum.inr (sum.inr_injective.inj_on _), (λ x, g x • v x) (sum.inr i) = 0,
{ rw [finset.sum_preimage', finset.sum_preimage', ← finset.sum_union, ← finset.filter_or],
{ simpa only [← mem_union, range_inl_union_range_inr, mem_univ, finset.filter_true] },
{ exact finset.disjoint_filter.2 (λ x hx, disjoint_left.1 is_compl_range_inl_range_inr.1) } },
{ rw ← eq_neg_iff_add_eq_zero at this,
rw [disjoint_def'] at hlr,
have A := hlr _ (sum_mem _ $ λ i hi, _) _ (neg_mem _ $ sum_mem _ $ λ i hi, _) this,
{ cases i with i i,
{ exact hl _ _ A i (finset.mem_preimage.2 hi) },
{ rw [this, neg_eq_zero] at A,
exact hr _ _ A i (finset.mem_preimage.2 hi) } },
{ exact smul_mem _ _ (subset_span ⟨sum.inl i, mem_range_self _, rfl⟩) },
{ exact smul_mem _ _ (subset_span ⟨sum.inr i, mem_range_self _, rfl⟩) } }
end
lemma linear_independent.sum_type {v' : ι' → M} (hv : linear_independent R v)
(hv' : linear_independent R v')
(h : disjoint (submodule.span R (range v)) (submodule.span R (range v'))) :
linear_independent R (sum.elim v v') :=
linear_independent_sum.2 ⟨hv, hv', h⟩
lemma linear_independent.union {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M))
(hst : disjoint (span R s) (span R t)) :
linear_independent R (λ x, x : (s ∪ t) → M) :=
(hs.sum_type ht $ by simpa).to_subtype_range' $ by simp
lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M}
(hl : ∀i, linear_independent R (λ x, x : f i → M))
(hd : ∀i, ∀t:set ι, finite t → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) :
linear_independent R (λ x, x : (⋃i, f i) → M) :=
begin
rw [Union_eq_Union_finset f],
apply linear_independent_Union_of_directed,
{ apply directed_of_sup,
exact (λ t₁ t₂ ht, Union_mono $ λ i, Union_subset_Union_const $ λ h, ht h) },
assume t,
induction t using finset.induction_on with i s his ih,
{ refine (linear_independent_empty _ _).mono _,
simp },
{ rw [finset.set_bUnion_insert],
refine (hl _).union ih _,
refine (hd i s s.finite_to_set his).mono_right _,
simp only [(span_Union _).symm],
refine span_mono (@supr_le_supr2 (set M) _ _ _ _ _ _),
exact λ i, ⟨i, le_rfl⟩ }
end
lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*}
{f : Π j : η, ιs j → M}
(hindep : ∀j, linear_independent R (f j))
(hd : ∀i, ∀t:set η, finite t → i ∉ t →
disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) :
linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) :=
begin
nontriviality R,
apply linear_independent.of_subtype_range,
{ rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy,
by_cases h_cases : x₁ = y₁,
subst h_cases,
{ apply sigma.eq,
rw linear_independent.injective (hindep _) hxy,
refl },
{ have h0 : f x₁ x₂ = 0,
{ apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁)
(λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)),
rw supr_singleton,
simp only at hxy,
rw hxy,
exact (subset_span (mem_range_self y₂)) },
exact false.elim ((hindep x₁).ne_zero _ h0) } },
rw range_sigma_eq_Union_range,
apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd,
end
end subtype
section repr
variables (hv : linear_independent R v)
/-- Canonical isomorphism between linear combinations and the span of linearly independent vectors.
-/
@[simps] def linear_independent.total_equiv (hv : linear_independent R v) :
(ι →₀ R) ≃ₗ[R] span R (range v) :=
begin
apply linear_equiv.of_bijective
(linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _),
{ rw [← linear_map.ker_eq_bot, linear_map.ker_cod_restrict],
apply hv },
{ rw [← linear_map.range_eq_top, linear_map.range_eq_map, linear_map.map_cod_restrict,
← linear_map.range_le_iff_comap, range_subtype, map_top],
rw finsupp.range_total,
exact le_rfl },
{ intro l,
rw ← finsupp.range_total,
rw linear_map.mem_range,
apply mem_range_self l }
end
/-- Linear combination representing a vector in the span of linearly independent vectors.
Given a family of linearly independent vectors, we can represent any vector in their span as
a linear combination of these vectors. These are provided by this linear map.
It is simply one direction of `linear_independent.total_equiv`. -/
def linear_independent.repr (hv : linear_independent R v) :
span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm
@[simp] lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x :=
subtype.ext_iff.1 (linear_equiv.apply_symm_apply hv.total_equiv x)
lemma linear_independent.total_comp_repr :
(finsupp.total ι M R v).comp hv.repr = submodule.subtype _ :=
linear_map.ext $ hv.total_repr
lemma linear_independent.repr_ker : hv.repr.ker = ⊥ :=
by rw [linear_independent.repr, linear_equiv.ker]
lemma linear_independent.repr_range : hv.repr.range = ⊤ :=
by rw [linear_independent.repr, linear_equiv.range]
lemma linear_independent.repr_eq
{l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) :
hv.repr x = l :=
begin
have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l)
= finsupp.total ι M R v l := rfl,
have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x,
{ rw eq at this,
exact subtype.ext_iff.2 this },
rw ←linear_equiv.symm_apply_apply hv.total_equiv l,
rw ←this,
refl,
end
lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) :
hv.repr x = finsupp.single i 1 :=
begin
apply hv.repr_eq,
simp [finsupp.total_single, hx]
end
lemma linear_independent.span_repr_eq [nontrivial R] (x) :
span.repr R (set.range v) x = (hv.repr x).equiv_map_domain (equiv.of_injective _ hv.injective) :=
begin
have p : (span.repr R (set.range v) x).equiv_map_domain (equiv.of_injective _ hv.injective).symm =
hv.repr x,
{ apply (linear_independent.total_equiv hv).injective,
ext,
simp, },
ext ⟨_, ⟨i, rfl⟩⟩,
simp [←p],
end
-- TODO: why is this so slow?
lemma linear_independent_iff_not_smul_mem_span :
linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) :=
⟨ λ hv i a ha, begin
rw [finsupp.span_image_eq_map_total, mem_map] at ha,
rcases ha with ⟨l, hl, e⟩,
rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl,
by_contra hn,
exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _),
end, λ H, linear_independent_iff.2 $ λ l hl, begin
ext i, simp only [finsupp.zero_apply],
by_contra hn,
refine hn (H i _ _),
refine (finsupp.mem_span_image_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩,
{ rw finsupp.mem_supported',
intros j hj,
have hij : j = i :=
not_not.1
(λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)),
simp [hij] },
{ simp [hl] }
end⟩
variable (R)
lemma exists_maximal_independent' (s : ι → M) :
∃ I : set ι, linear_independent R (λ x : I, s x) ∧
∀ J : set ι, I ⊆ J → linear_independent R (λ x : J, s x) → I = J :=
begin
let indep : set ι → Prop := λ I, linear_independent R (s ∘ coe : I → M),
let X := { I : set ι // indep I },
let r : X → X → Prop := λ I J, I.1 ⊆ J.1,
have key : ∀ c : set X, zorn.chain r c → indep (⋃ (I : X) (H : I ∈ c), I),
{ intros c hc,
dsimp [indep],
rw [linear_independent_comp_subtype],
intros f hsupport hsum,
rcases eq_empty_or_nonempty c with rfl | ⟨a, hac⟩,
{ simpa using hsupport },
haveI : is_refl X r := ⟨λ _, set.subset.refl _⟩,
obtain ⟨I, I_mem, hI⟩ : ∃ I ∈ c, (f.support : set ι) ⊆ I :=
finset.exists_mem_subset_of_subset_bUnion_of_directed_on hac hc.directed_on hsupport,
exact linear_independent_comp_subtype.mp I.2 f hI hsum },
have trans : transitive r := λ I J K, set.subset.trans,
obtain ⟨⟨I, hli : indep I⟩, hmax : ∀ a, r ⟨I, hli⟩ a → r a ⟨I, hli⟩⟩ :=
@zorn.exists_maximal_of_chains_bounded _ r
(λ c hc, ⟨⟨⋃ I ∈ c, (I : set ι), key c hc⟩, λ I, set.subset_bUnion_of_mem⟩) trans,
exact ⟨I, hli, λ J hsub hli, set.subset.antisymm hsub (hmax ⟨J, hli⟩ hsub)⟩,
end
lemma exists_maximal_independent (s : ι → M) : ∃ I : set ι, linear_independent R (λ x : I, s x) ∧
∀ i ∉ I, ∃ a : R, a ≠ 0 ∧ a • s i ∈ span R (s '' I) :=
begin
classical,
rcases exists_maximal_independent' R s with ⟨I, hIlinind, hImaximal⟩,
use [I, hIlinind],
intros i hi,
specialize hImaximal (I ∪ {i}) (by simp),
set J := I ∪ {i} with hJ,
have memJ : ∀ {x}, x ∈ J ↔ x = i ∨ x ∈ I, by simp [hJ],
have hiJ : i ∈ J := by simp,
have h := mt hImaximal _, swap,
{ intro h2,
rw h2 at hi,
exact absurd hiJ hi },
obtain ⟨f, supp_f, sum_f, f_ne⟩ := linear_dependent_comp_subtype.mp h,
have hfi : f i ≠ 0,
{ contrapose hIlinind,
refine linear_dependent_comp_subtype.mpr ⟨f, _, sum_f, f_ne⟩,
simp only [finsupp.mem_supported, hJ] at ⊢ supp_f,
rintro x hx,
refine (memJ.mp (supp_f hx)).resolve_left _,
rintro rfl,
exact hIlinind (finsupp.mem_support_iff.mp hx) },
use [f i, hfi],
have hfi' : i ∈ f.support := finsupp.mem_support_iff.mpr hfi,
rw [← finset.insert_erase hfi', finset.sum_insert (finset.not_mem_erase _ _),
add_eq_zero_iff_eq_neg] at sum_f,
rw sum_f,
refine neg_mem _ (sum_mem _ (λ c hc, smul_mem _ _ (subset_span ⟨c, _, rfl⟩))),
exact (memJ.mp (supp_f (finset.erase_subset _ _ hc))).resolve_left (finset.ne_of_mem_erase hc),
end
end repr
lemma surjective_of_linear_independent_of_span [nontrivial R]
(hv : linear_independent R v) (f : ι' ↪ ι)
(hss : range v ⊆ span R (range (v ∘ f))) :
surjective f :=
begin
intros i,
let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.injective).repr,
let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f,
have h_total_l : finsupp.total ι M R v l = v i,
{ dsimp only [l],
rw finsupp.total_map_domain,
rw (hv.comp f f.injective).total_repr,
{ refl },
{ exact f.injective } },
have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1),
by rw [h_total_l, finsupp.total_single, one_smul],
have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq,
dsimp only [l] at l_eq,
rw ←finsupp.emb_domain_eq_map_domain at l_eq,
rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq
with ⟨i', hi'⟩,
use i',
exact hi'.2
end
lemma eq_of_linear_independent_of_span_subtype [nontrivial R] {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t :=
begin
let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.coe_injective (subtype.mk.inj hab)⟩,
have h_surj : surjective f,
{ apply surjective_of_linear_independent_of_span hs f _,
convert hst; simp [f, comp], },
show s = t,
{ apply subset.antisymm _ h,
intros x hx,
rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩,
convert y.mem,
rw ← subtype.mk.inj hy,
refl }
end
open linear_map
lemma linear_independent.image_subtype {s : set M} {f : M →ₗ[R] M'}
(hs : linear_independent R (λ x, x : s → M))
(hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') :=
begin
rw [← @subtype.range_coe _ s] at hf_inj,
refine (hs.map hf_inj).to_subtype_range' _,
simp [set.range_comp f]
end
lemma linear_independent.inl_union_inr {s : set M} {t : set M'}
(hs : linear_independent R (λ x, x : s → M))
(ht : linear_independent R (λ x, x : t → M')) :
linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') :=
begin
refine (hs.image_subtype _).union (ht.image_subtype _) _; [simp, simp, skip],
simp only [span_image],
simp [disjoint_iff, prod_inf_prod]
end
lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'}
(hv : linear_independent R v) (hv' : linear_independent R v') :
linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) :=
(hv.map' (inl R M M') ker_inl).sum_type (hv'.map' (inr R M M') ker_inr) $
begin
refine is_compl_range_inl_inr.disjoint.mono _ _;
simp only [span_le, range_coe, range_comp_subset_range],
end
/-- Dedekind's linear independence of characters -/
-- See, for example, Keith Conrad's note
-- <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf>
theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [comm_ring L]
[no_zero_divisors L] :
@linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ :=
by letI := classical.dec_eq (G →* L);
letI : mul_action L L := distrib_mul_action.to_mul_action;
-- We prove linear independence by showing that only the trivial linear combination vanishes.
exact linear_independent_iff'.2
-- To do this, we use `finset` induction,
(λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg,
-- Here
-- * `a` is a new character we will insert into the `finset` of characters `s`,
-- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero
-- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero
-- and it remains to prove that `g` vanishes on `insert a s`.
-- We now make the key calculation:
-- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the
-- monoid `G`.
have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G,
-- We prove these expressions are equal by showing
-- the differences of their values on each monoid element `x` is zero
eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x)
(funext $ λ y : G, calc
-- After that, it's just a chase scene.
(∑ i in s, ((g i * i x - g i * a x) • i : G → L)) y
= ∑ i in s, (g i * i x - g i * a x) * i y : finset.sum_apply _ _ _
... = ∑ i in s, (g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl
(λ _ _, sub_mul _ _ _)
... = ∑ i in s, g i * i x * i y - ∑ i in s, g i * a x * i y : finset.sum_sub_distrib
... = (g a * a x * a y + ∑ i in s, g i * i x * i y)
- (g a * a x * a y + ∑ i in s, g i * a x * i y) : by rw add_sub_add_left_eq_sub
... = ∑ i in insert a s, g i * i x * i y - ∑ i in insert a s, g i * a x * i y :
by rw [finset.sum_insert has, finset.sum_insert has]
... = ∑ i in insert a s, g i * i (x * y) - ∑ i in insert a s, a x * (g i * i y) :
congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc]))
(finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm])
... = (∑ i in insert a s, (g i • i : G → L)) (x * y)
- a x * (∑ i in insert a s, (g i • i : G → L)) y :
by rw [finset.sum_apply, finset.sum_apply, finset.mul_sum]; refl
... = 0 - a x * 0 : by rw hg; refl
... = 0 : by rw [mul_zero, sub_zero])
i
his,
-- On the other hand, since `a` is not already in `s`, for any character `i ∈ s`
-- there is some element of the monoid on which it differs from `a`.
have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his,
classical.by_contradiction $ λ h,
have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩,
has $ hia ▸ his,
-- From these two facts we deduce that `g` actually vanishes on `s`,
have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in
have h : g i • i y = g i • a y, from congr_fun (h1 i his) y,
or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy),
-- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish,
-- we deduce that `g a = 0`.
have h4 : g a = 0, from calc
g a = g a * 1 : (mul_one _).symm
... = (g a • a : G → L) 1 : by rw ← a.map_one; refl
... = (∑ i in insert a s, (g i • i : G → L)) 1 : begin
rw finset.sum_eq_single a,
{ intros i his hia, rw finset.mem_insert at his,
rw [h3 i (his.resolve_left hia), zero_smul] },
{ intros haas, exfalso, apply haas, exact finset.mem_insert_self a s }
end
... = 0 : by rw hg; refl,
-- Now we're done; the last two facts together imply that `g` vanishes on every element
-- of `insert a s`.
(finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩)
lemma le_of_span_le_span [nontrivial R] {s t u: set M}
(hl : linear_independent R (coe : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u)
(hst : span R s ≤ span R t) : s ⊆ t :=
begin
have := eq_of_linear_independent_of_span_subtype
(hl.mono (set.union_subset hsu htu))
(set.subset_union_right _ _)
(set.union_subset (set.subset.trans subset_span hst) subset_span),
rw ← this, apply set.subset_union_left
end
lemma span_le_span_iff [nontrivial R] {s t u: set M}
(hl : linear_independent R (coe : u → M)) (hsu : s ⊆ u) (htu : t ⊆ u) :
span R s ≤ span R t ↔ s ⊆ t :=
⟨le_of_span_le_span hl hsu htu, span_mono⟩
end module
section nontrivial
variables [ring R] [nontrivial R] [add_comm_group M] [add_comm_group M']
variables [module R M] [no_zero_smul_divisors R M] [module R M']
variables {v : ι → M} {s t : set M} {x y z : M}
lemma linear_independent_unique_iff
(v : ι → M) [unique ι] :
linear_independent R v ↔ v default ≠ 0 :=
begin
simp only [linear_independent_iff, finsupp.total_unique, smul_eq_zero],
refine ⟨λ h hv, _, λ hv l hl, finsupp.unique_ext $ hl.resolve_right hv⟩,
have := h (finsupp.single default 1) (or.inr hv),
exact one_ne_zero (finsupp.single_eq_zero.1 this)
end
alias linear_independent_unique_iff ↔ _ linear_independent_unique
lemma linear_independent_singleton {x : M} (hx : x ≠ 0) :
linear_independent R (λ x, x : ({x} : set M) → M) :=
linear_independent_unique coe hx
end nontrivial
/-!
### Properties which require `division_ring K`
These can be considered generalizations of properties of linear independence in vector spaces.
-/
section module
variables [division_ring K] [add_comm_group V] [add_comm_group V']
variables [module K V] [module K V']
variables {v : ι → V} {s t : set V} {x y z : V}
open submodule
/- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class
(instead of a data containing type class) -/
lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) :=
begin
simp [mem_span_insert],
rintro a z hz rfl h,
refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩,
have a0 : a ≠ 0, {rintro rfl, simp * at *},
simp [a0, smul_add, smul_smul]
end
lemma linear_independent_iff_not_mem_span :
linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) :=
begin
apply linear_independent_iff_not_smul_mem_span.trans,
split,
{ intros h i h_in_span,
apply one_ne_zero (h i 1 (by simp [h_in_span])) },
{ intros h i a ha,
by_contradiction ha',
exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) }
end
lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) :
linear_independent K (λ b, b : insert x s → V) :=
begin
rw ← union_singleton,
have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem _) hx,
apply hs.union (linear_independent_singleton x0),
rwa [disjoint_span_singleton' x0]
end
lemma linear_independent_option' :
linear_independent K (λ o, option.cases_on' o x v : option ι → V) ↔
linear_independent K v ∧ (x ∉ submodule.span K (range v)) :=
begin
rw [← linear_independent_equiv (equiv.option_equiv_sum_punit ι).symm, linear_independent_sum,
@range_unique _ punit, @linear_independent_unique_iff punit, disjoint_span_singleton],
dsimp [(∘)],
refine ⟨λ h, ⟨h.1, λ hx, h.2.1 $ h.2.2 hx⟩, λ h, ⟨h.1, _, λ hx, (h.2 hx).elim⟩⟩,
rintro rfl,
exact h.2 (zero_mem _)
end
lemma linear_independent.option (hv : linear_independent K v)
(hx : x ∉ submodule.span K (range v)) :
linear_independent K (λ o, option.cases_on' o x v : option ι → V) :=
linear_independent_option'.2 ⟨hv, hx⟩
lemma linear_independent_option {v : option ι → V} :
linear_independent K v ↔
linear_independent K (v ∘ coe : ι → V) ∧ v none ∉ submodule.span K (range (v ∘ coe : ι → V)) :=
by simp only [← linear_independent_option', option.cases_on'_none_coe]
theorem linear_independent_insert' {ι} {s : set ι} {a : ι} {f : ι → V} (has : a ∉ s) :
linear_independent K (λ x : insert a s, f x) ↔
linear_independent K (λ x : s, f x) ∧ f a ∉ submodule.span K (f '' s) :=
by { rw [← linear_independent_equiv ((equiv.option_equiv_sum_punit _).trans
(equiv.set.insert has).symm), linear_independent_option], simp [(∘), range_comp f] }
theorem linear_independent_insert (hxs : x ∉ s) :
linear_independent K (λ b : insert x s, (b : V)) ↔
linear_independent K (λ b : s, (b : V)) ∧ x ∉ submodule.span K s :=
(@linear_independent_insert' _ _ _ _ _ _ _ _ id hxs).trans $ by simp
lemma linear_independent_pair {x y : V} (hx : x ≠ 0) (hy : ∀ a : K, a • x ≠ y) :
linear_independent K (coe : ({x, y} : set V) → V) :=
pair_comm y x ▸ (linear_independent_singleton hx).insert $ mt mem_span_singleton.1
(not_exists.2 hy)
lemma linear_independent_fin_cons {n} {v : fin n → V} :
linear_independent K (fin.cons x v : fin (n + 1) → V) ↔
linear_independent K v ∧ x ∉ submodule.span K (range v) :=
begin
rw [← linear_independent_equiv (fin_succ_equiv n).symm, linear_independent_option],
convert iff.rfl,
{ ext,
-- TODO: why doesn't simp use `fin_succ_equiv_symm_coe` here?
rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] },
{ ext,
rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] }
end
lemma linear_independent_fin_snoc {n} {v : fin n → V} :
linear_independent K (fin.snoc v x : fin (n + 1) → V) ↔
linear_independent K v ∧ x ∉ submodule.span K (range v) :=
by rw [fin.snoc_eq_cons_rotate, linear_independent_equiv, linear_independent_fin_cons]
/-- See `linear_independent.fin_cons'` for an uglier version that works if you
only have a module over a semiring. -/
lemma linear_independent.fin_cons {n} {v : fin n → V} (hv : linear_independent K v)
(hx : x ∉ submodule.span K (range v)) :
linear_independent K (fin.cons x v : fin (n + 1) → V) :=
linear_independent_fin_cons.2 ⟨hv, hx⟩
lemma linear_independent_fin_succ {n} {v : fin (n + 1) → V} :
linear_independent K v ↔
linear_independent K (fin.tail v) ∧ v 0 ∉ submodule.span K (range $ fin.tail v) :=
by rw [← linear_independent_fin_cons, fin.cons_self_tail]
lemma linear_independent_fin_succ' {n} {v : fin (n + 1) → V} :
linear_independent K v ↔
linear_independent K (fin.init v) ∧ v (fin.last _) ∉ submodule.span K (range $ fin.init v) :=
by rw [← linear_independent_fin_snoc, fin.snoc_init_self]
lemma linear_independent_fin2 {f : fin 2 → V} :
linear_independent K f ↔ f 1 ≠ 0 ∧ ∀ a : K, a • f 1 ≠ f 0 :=
by rw [linear_independent_fin_succ, linear_independent_unique_iff, range_unique,
mem_span_singleton, not_exists,
show fin.tail f default = f 1, by rw ← fin.succ_zero_eq_one; refl]
lemma exists_linear_independent_extension (hs : linear_independent K (coe : s → V)) (hst : s ⊆ t) :
∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (coe : b → V) :=
begin
rcases zorn.zorn_subset_nonempty {b | b ⊆ t ∧ linear_independent K (coe : b → V)} _ _
⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩,
{ refine ⟨b, bt, sb, λ x xt, _, bi⟩,
by_contra hn,
apply hn,
rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _),
exact subset_span (mem_insert _ _) },
{ refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩,
{ exact sUnion_subset (λ x xc, (hc xc).1) },
{ exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) },
{ exact subset_sUnion_of_mem } }
end
variables (K t)
lemma exists_linear_independent :
∃ b ⊆ t, span K b = span K t ∧ linear_independent K (coe : b → V) :=
begin
obtain ⟨b, hb₁, -, hb₂, hb₃⟩ :=
exists_linear_independent_extension (linear_independent_empty K V) (set.empty_subset t),
exact ⟨b, hb₁, (span_eq_of_le _ hb₂ (submodule.span_mono hb₁)).symm, hb₃⟩,
end
variables {K t}
/-- `linear_independent.extend` adds vectors to a linear independent set `s ⊆ t` until it spans
all elements of `t`. -/
noncomputable def linear_independent.extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : set V :=
classical.some (exists_linear_independent_extension hs hst)
lemma linear_independent.extend_subset (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : hs.extend hst ⊆ t :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hbt
lemma linear_independent.subset_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : s ⊆ hs.extend hst :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hsb
lemma linear_independent.subset_span_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : t ⊆ span K (hs.extend hst) :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in htb
lemma linear_independent.linear_independent_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : linear_independent K (coe : hs.extend hst → V) :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hli
variables {K V}
-- TODO(Mario): rewrite?
lemma exists_of_linear_independent_of_finite_span {t : finset V}
(hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) :
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card :=
have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) →
∃t':finset V, ↑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_subtype 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),
classical.by_cases
(assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V),
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 [insert_subset_insert hust],
subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩)
(assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in
have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h,
have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from
assume b₃ hb₃,
have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V),
by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl,
subset_union_right],
have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)),
from span_mono this (hss' hb₃),
have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V),
by simpa [insert_eq, -singleton_union, -union_singleton] using hss',
have hb₁ : b₁ ∈ span K (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 [insert_subset, hb₂s, hs']) hst this in
⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]),
hsu, by simp [eq, hb₂t', hb₁t, hb₁s']⟩)),
begin
have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t,
{ ext1 x,
by_cases x ∈ s; simp * },
apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s))
(by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])),
intros u h,
exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}),
h.2.1, by simp only [h.2.2, eq]⟩
end
lemma exists_finite_card_le_of_finite_of_linear_independent_of_span
(ht : finite t) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) :
∃h : finite s, h.to_finset.card ≤ ht.to_finset.card :=
have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption,
let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in
have finite s, from u.finite_to_set.subset hsu,
⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩
end module
|
e1c37f02da9575763528600ddf7921686adb24b8 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/standard/data/list/default.lean | 6db2c909b4180deb70787abc65735cef0a078e03 | [
"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 | 388 | lean | ----------------------------------------------------------------------------------------------------
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Jeremy Avigad
----------------------------------------------------------------------------------------------------
import data.list.basic
|
f57bc287f8396e14ac91a5a7058432b84f01cba8 | c8af905dcd8475f414868d303b2eb0e9d3eb32f9 | /src/data/cpi/semantics/interaction_tensor.lean | 32ac2ac802e8d1862f386a92f99f44293183979b | [
"BSD-3-Clause"
] | permissive | continuouspi/lean-cpi | 81480a13842d67ff5f3698643210d8ed5dd08de4 | 443bf2cb236feadc45a01387099c236ab2b78237 | refs/heads/master | 1,650,307,316,582 | 1,587,033,364,000 | 1,587,033,364,000 | 207,499,661 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,073 | lean | import data.cpi.semantics.space tactic.abel
namespace cpi
variables {ℂ ℍ : Type} {ω : context} {M : affinity ℍ} {conc : ℍ ↪ ℂ} [half_ring ℂ] [decidable_eq ℂ]
/-- The main body of the interaction tensor. Split out into a separate function
to make unfolding possible. -/
private def interaction_tensor_worker [cpi_equiv ℍ ω] (conc : ℍ ↪ ℂ)
: ( prime_species' ℍ ω (context.extend M.arity context.nil)
× (Σ (b y), concretion' ℍ ω (context.extend M.arity context.nil) b y)
× name (context.extend M.arity context.nil))
→ ( prime_species' ℍ ω (context.extend M.arity context.nil)
× (Σ (b y), concretion' ℍ ω (context.extend M.arity context.nil) b y)
× name (context.extend M.arity context.nil))
→ process_space ℂ ℍ ω (context.extend M.arity context.nil)
| ⟨ A, ⟨ bF, yF, F ⟩, x ⟩ ⟨ B, ⟨ bG, yG, G ⟩, y ⟩ :=
option.cases_on (M.f x.to_idx y.to_idx) 0 (λ aff,
if h : bF = yG ∧ yF = bG then begin
rcases h with ⟨ ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
from conc aff • ( to_process_space (cpi_equiv.pseudo_apply F G)
- fin_fn.single A 1 - fin_fn.single B (1 : ℂ)),
end else 0)
/-- Show that the interaction tensor worker is commutitive. -/
private lemma interaction_tensor_worker.comm [cpi_equiv_prop ℍ ω]
: ∀ (A B : prime_species' ℍ ω (context.extend M.arity context.nil)
× (Σ (b y), concretion' ℍ ω (context.extend M.arity context.nil) b y)
× name (context.extend M.arity context.nil))
, interaction_tensor_worker conc A B = interaction_tensor_worker conc B A
| ⟨ A, ⟨ bF, yF, F ⟩, a ⟩ ⟨ B, ⟨ bG, yG, G ⟩, b ⟩ := begin
simp only [interaction_tensor_worker],
rw M.symm a.to_idx b.to_idx,
cases M.f (name.to_idx b) (name.to_idx a),
case option.none { from rfl },
case option.some {
simp only [],
by_cases this : (bF = yG ∧ yF = bG),
{
rcases this with ⟨ ⟨ _ ⟩, ⟨ _ ⟩ ⟩,
let h : bF = bF ∧ yF = yF := ⟨ rfl, rfl ⟩,
let g : yF = yF ∧ bF = bF := ⟨ rfl, rfl ⟩,
simp only [dif_pos h, dif_pos g, cpi_equiv_prop.pseudo_apply_symm],
simp only [sub_eq_add_neg, add_comm, add_left_comm],
},
{
have h : ¬ (bG = yF ∧ yG = bF),
{ rintros ⟨ ⟨ _ ⟩, ⟨ _ ⟩ ⟩, from this ⟨ rfl, rfl ⟩ },
simp only [dif_neg this, dif_neg h],
}
}
end
/-- Compute the interaction tensor between two elements in the interaction
space. -/
def interaction_tensor [cpi_equiv ℍ ω] (conc: ℍ ↪ ℂ)
: interaction_space ℂ ℍ ω (context.extend M.arity context.nil)
→ interaction_space ℂ ℍ ω (context.extend M.arity context.nil)
→ process_space ℂ ℍ ω (context.extend M.arity context.nil)
| x y := fin_fn.bind₂ x y (interaction_tensor_worker conc)
infix ` ⊘ `:73 := interaction_tensor _
notation x ` ⊘[`:73 conc `] ` y:73 := interaction_tensor conc x y
@[simp]
lemma interaction_tensor.zero_left [cpi_equiv ℍ ω]
: ∀ (A : interaction_space ℂ ℍ ω (context.extend M.arity context.nil))
, A ⊘[conc] 0 = 0
| A := fin_fn.bind₂_zero_left A _
@[simp]
lemma interaction_tensor.zero_right [cpi_equiv ℍ ω]
: ∀ (A : interaction_space ℂ ℍ ω (context.extend M.arity context.nil))
, 0 ⊘[conc] A = 0
| A := fin_fn.bind₂_zero_right A _
lemma interaction_tensor.comm [cpi_equiv_prop ℍ ω]
(A B : interaction_space ℂ ℍ ω (context.extend M.arity context.nil))
: A ⊘[conc] B = B ⊘[conc] A := begin
suffices : (λ x y, interaction_tensor_worker conc x y)
= (λ x y, interaction_tensor_worker conc y x),
{ show fin_fn.bind₂ A B (interaction_tensor_worker conc)
= fin_fn.bind₂ B A (λ x y, interaction_tensor_worker conc x y),
-- Sneaky use of η-expanding one function to make sure the rewrite applies.
rw this,
from fin_fn.bind₂_swap A B (interaction_tensor_worker conc) },
from funext (λ x, funext (interaction_tensor_worker.comm x)),
end
@[simp]
lemma interaction_tensor.left_distrib [cpi_equiv ℍ ω]
(A B C : interaction_space ℂ ℍ ω (context.extend M.arity context.nil))
: (A + B) ⊘[conc] C = A ⊘[conc] C + B ⊘[conc] C
:= by simp only [interaction_tensor, fin_fn.bind₂, fin_fn.bind_distrib]
@[simp]
lemma interaction_tensor.right_distrib [cpi_equiv_prop ℍ ω]
(A B C : interaction_space ℂ ℍ ω (context.extend M.arity context.nil))
: A ⊘[conc] (B + C) = A ⊘[conc] B + A ⊘[conc] C
:= calc A ⊘ (B + C)
= (B + C) ⊘ A : interaction_tensor.comm A _
... = B ⊘ A + C ⊘ A : interaction_tensor.left_distrib B C A
... = A ⊘ B + A ⊘ C : by rw [interaction_tensor.comm B, interaction_tensor.comm C]
instance interaction_tensor.monoid_hom_left [cpi_equiv_prop ℍ ω]
(ξ : interaction_space ℂ ℍ ω (context.extend M.arity context.nil))
: is_add_monoid_hom (interaction_tensor conc ξ)
:= { map_add := interaction_tensor.right_distrib ξ,
map_zero := interaction_tensor.zero_left ξ }
end cpi
#lint-
|
84e12498234c500ef150e343b6982effe8efcf72 | 2c096fdfecf64e46ea7bc6ce5521f142b5926864 | /src/Lean/Meta/Coe.lean | c30c414e7a9973fefb9125ba4216baf0cfec5f3c | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | Kha/lean4 | 1005785d2c8797ae266a303968848e5f6ce2fe87 | b99e11346948023cd6c29d248cd8f3e3fb3474cf | refs/heads/master | 1,693,355,498,027 | 1,669,080,461,000 | 1,669,113,138,000 | 184,748,176 | 0 | 0 | Apache-2.0 | 1,665,995,520,000 | 1,556,884,930,000 | Lean | UTF-8 | Lean | false | false | 8,775 | 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.WHNF
import Lean.Meta.Transform
import Lean.Meta.SynthInstance
import Lean.Meta.AppBuilder
namespace Lean.Meta
/--
Return true iff `declName` is one of the auxiliary definitions/projections
used to implement coercions.
-/
def isCoeDecl (declName : Name) : Bool :=
declName == ``Coe.coe || declName == ``CoeTC.coe || declName == ``CoeHead.coe ||
declName == ``CoeTail.coe || declName == ``CoeHTCT.coe || declName == ``CoeDep.coe ||
declName == ``CoeT.coe || declName == ``CoeFun.coe || declName == ``CoeSort.coe ||
declName == ``Lean.Internal.liftCoeM || declName == ``Lean.Internal.coeM
/-- Expand coercions occurring in `e` -/
partial def expandCoe (e : Expr) : MetaM Expr :=
withReducibleAndInstances do
transform e fun e => do
let f := e.getAppFn
if f.isConst then
let declName := f.constName!
if isCoeDecl declName then
if let some e ← unfoldDefinition? e then
return .visit e.headBeta
return .continue
register_builtin_option maxCoeSize : Nat := {
defValue := 16
descr := "maximum number of instances used to construct an automatic coercion"
}
register_builtin_option autoLift : Bool := {
defValue := true
descr := "insert monadic lifts (i.e., `liftM` and coercions) when needed"
}
def trySynthInstanceForCoe (cls : Expr) : MetaM (LOption Expr) := do
trySynthInstance cls (some (maxCoeSize.get (← getOptions)))
/-- Coerces `expr` to `expectedType` using `CoeT`. -/
def coerceSimple? (expr expectedType : Expr) : MetaM (LOption Expr) := do
let eType ← inferType expr
let u ← getLevel eType
let v ← getLevel expectedType
let coeTInstType := mkAppN (mkConst ``CoeT [u, v]) #[eType, expr, expectedType]
match ← trySynthInstanceForCoe coeTInstType with
| .some inst =>
let result ← expandCoe (mkAppN (mkConst ``CoeT.coe [u, v]) #[eType, expr, expectedType, inst])
unless ← isDefEq (← inferType result) expectedType do
throwError "could not coerce{indentExpr expr}\nto{indentExpr expectedType}\ncoerced expression has wrong type:{indentExpr result}"
return .some result
| .undef => return .undef
| .none => return .none
/-- Coerces `expr` to a function type. -/
def coerceToFunction? (expr : Expr) : MetaM (Option Expr) := do
let result ← try mkAppM ``CoeFun.coe #[expr] catch _ => return none
let expanded ← expandCoe result
unless (← whnf (← inferType expanded)).isForall do
throwError "failed to coerce{indentExpr expr}\nto a function, after applying `CoeFun.coe`, result is still not a function{indentExpr expanded}\nthis is often due to incorrect `CoeFun` instances, the synthesized instance was{indentExpr result.appFn!.appArg!}"
return expanded
/-- Coerces `expr` to a type. -/
def coerceToSort? (expr : Expr) : MetaM (Option Expr) := do
let result ← try mkAppM ``CoeSort.coe #[expr] catch _ => return none
let expanded ← expandCoe result
unless (← whnf (← inferType expanded)).isSort do
throwError "failed to coerce{indentExpr expr}\nto a type, after applying `CoeSort.coe`, result is still not a type{indentExpr expanded}\nthis is often due to incorrect `CoeSort` instances, the synthesized instance was{indentExpr result.appFn!.appArg!}"
return expanded
/-- Return `some (m, α)` if `type` can be reduced to an application of the form `m α` using `[reducible]` transparency. -/
def isTypeApp? (type : Expr) : MetaM (Option (Expr × Expr)) := do
let type ← withReducible <| whnf type
match type with
| .app m α => return some ((← instantiateMVars m), (← instantiateMVars α))
| _ => return none
/--
Return `true` if `type` is of the form `m α` where `m` is a `Monad`.
Note that we reduce `type` using transparency `[reducible]`.
-/
def isMonadApp (type : Expr) : MetaM Bool := do
let some (m, _) ← isTypeApp? type | return false
return (← isMonad? m).isSome
/--
Try coercions and monad lifts to make sure `e` has type `expectedType`.
If `expectedType` is of the form `n β`, we try monad lifts and other extensions.
Extensions for monads.
1. Try to unify `n` and `m`. If it succeeds, then we use
```
coeM {m : Type u → Type v} {α β : Type u} [∀ a, CoeT α a β] [Monad m] (x : m α) : m β
```
`n` must be a `Monad` to use this one.
2. If there is monad lift from `m` to `n` and we can unify `α` and `β`, we use
```
liftM : ∀ {m : Type u_1 → Type u_2} {n : Type u_1 → Type u_3} [self : MonadLiftT m n] {α : Type u_1}, m α → n α
```
Note that `n` may not be a `Monad` in this case. This happens quite a bit in code such as
```
def g (x : Nat) : IO Nat := do
IO.println x
pure x
def f {m} [MonadLiftT IO m] : m Nat :=
g 10
```
3. If there is a monad lift from `m` to `n` and a coercion from `α` to `β`, we use
```
liftCoeM {m : Type u → Type v} {n : Type u → Type w} {α β : Type u} [MonadLiftT m n] [∀ a, CoeT α a β] [Monad n] (x : m α) : n β
```
Note that approach 3 does not subsume 1 because it is only applicable if there is a coercion from `α` to `β` for all values in `α`.
This is not the case for example for `pure $ x > 0` when the expected type is `IO Bool`. The given type is `IO Prop`, and
we only have a coercion from decidable propositions. Approach 1 works because it constructs the coercion `CoeT (m Prop) (pure $ x > 0) (m Bool)`
using the instance `pureCoeDepProp`.
Note that, approach 2 is more powerful than `tryCoe`.
Recall that type class resolution never assigns metavariables created by other modules.
Now, consider the following scenario
```lean
def g (x : Nat) : IO Nat := ...
deg h (x : Nat) : StateT Nat IO Nat := do
v ← g x;
IO.Println v;
...
```
Let's assume there is no other occurrence of `v` in `h`.
Thus, we have that the expected of `g x` is `StateT Nat IO ?α`,
and the given type is `IO Nat`. So, even if we add a coercion.
```
instance {α m n} [MonadLiftT m n] {α} : Coe (m α) (n α) := ...
```
It is not applicable because TC would have to assign `?α := Nat`.
On the other hand, TC can easily solve `[MonadLiftT IO (StateT Nat IO)]`
since this goal does not contain any metavariables. And then, we
convert `g x` into `liftM $ g x`.
-/
def coerceMonadLift? (e expectedType : Expr) : MetaM (Option Expr) := do
let expectedType ← instantiateMVars expectedType
let eType ← instantiateMVars (← inferType e)
let some (n, β) ← isTypeApp? expectedType | return none
let some (m, α) ← isTypeApp? eType | return none
if (← isDefEq m n) then
let some monadInst ← isMonad? n | return none
try expandCoe (← mkAppOptM ``Lean.Internal.coeM #[m, α, β, none, monadInst, e]) catch _ => return none
else if autoLift.get (← getOptions) then
try
-- Construct lift from `m` to `n`
let monadLiftType ← mkAppM ``MonadLiftT #[m, n]
let .some monadLiftVal ← trySynthInstance monadLiftType | return none
let u_1 ← getDecLevel α
let u_2 ← getDecLevel eType
let u_3 ← getDecLevel expectedType
let eNew := mkAppN (Lean.mkConst ``liftM [u_1, u_2, u_3]) #[m, n, monadLiftVal, α, e]
let eNewType ← inferType eNew
if (← isDefEq expectedType eNewType) then
return some eNew -- approach 2 worked
else
let some monadInst ← isMonad? n | return none
let u ← getLevel α
let v ← getLevel β
let coeTInstType := Lean.mkForall `a BinderInfo.default α <| mkAppN (mkConst ``CoeT [u, v]) #[α, mkBVar 0, β]
let .some coeTInstVal ← trySynthInstance coeTInstType | return none
let eNew ← expandCoe (mkAppN (Lean.mkConst ``Lean.Internal.liftCoeM [u_1, u_2, u_3]) #[m, n, α, β, monadLiftVal, coeTInstVal, monadInst, e])
let eNewType ← inferType eNew
unless (← isDefEq expectedType eNewType) do return none
return some eNew -- approach 3 worked
catch _ =>
/- If `m` is not a monad, then we try to use `tryCoe?`. -/
return none
else
return none
/-- Coerces `expr` to the type `expectedType`.
Returns `.some coerced` on successful coercion,
`.none` if the expression cannot by coerced to that type,
or `.undef` if we need more metavariable assignments. -/
def coerce? (expr expectedType : Expr) : MetaM (LOption Expr) := do
if let some lifted ← coerceMonadLift? expr expectedType then
return .some lifted
if (← whnfR expectedType).isForall then
if let some fn ← coerceToFunction? expr then
if ← isDefEq (← inferType fn) expectedType then
return .some fn
coerceSimple? expr expectedType
end Lean.Meta
|
bbb7678601d2c739d9fb6c09095a6b63c5cbb479 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/continuous_function/units.lean | 6552ba5f8a04ddb6744a310a82ae9f32bc628154 | [
"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 | 3,660 | lean | /-
Copyright (c) 2022 Jireh Loreaux. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jireh Loreaux
-/
import topology.continuous_function.compact
import analysis.normed_space.units
import algebra.algebra.spectrum
/-!
# Units of continuous functions
This file concerns itself with `C(X, M)ˣ` and `C(X, Mˣ)` when `X` is a topological space
and `M` has some monoid structure compatible with its topology.
-/
variables {X M R 𝕜 : Type*} [topological_space X]
namespace continuous_map
section monoid
variables [monoid M] [topological_space M] [has_continuous_mul M]
/-- Equivalence between continuous maps into the units of a monoid with continuous multiplication
and the units of the monoid of continuous maps. -/
@[to_additive "Equivalence between continuous maps into the additive units of an additive monoid
with continuous addition and the additive units of the additive monoid of continuous maps.", simps]
def units_lift : C(X, Mˣ) ≃ C(X, M)ˣ :=
{ to_fun := λ f,
{ val := ⟨λ x, f x, units.continuous_coe.comp f.continuous⟩,
inv := ⟨λ x, ↑(f x)⁻¹, units.continuous_coe.comp (continuous_inv.comp f.continuous)⟩,
val_inv := ext $ λ x, units.mul_inv _,
inv_val := ext $ λ x, units.inv_mul _ },
inv_fun := λ f,
{ to_fun := λ x, ⟨f x, f⁻¹ x, continuous_map.congr_fun f.mul_inv x,
continuous_map.congr_fun f.inv_mul x⟩,
continuous_to_fun := continuous_induced_rng.2 $ continuous.prod_mk (f : C(X, M)).continuous
$ mul_opposite.continuous_op.comp (↑f⁻¹ : C(X, M)).continuous },
left_inv := λ f, by { ext, refl },
right_inv := λ f, by { ext, refl } }
end monoid
section normed_ring
variables [normed_ring R] [complete_space R]
lemma _root_.normed_ring.is_unit_unit_continuous {f : C(X, R)} (h : ∀ x, is_unit (f x)) :
continuous (λ x, (h x).unit) :=
begin
refine continuous_induced_rng.2 (continuous.prod_mk f.continuous
(mul_opposite.continuous_op.comp (continuous_iff_continuous_at.mpr (λ x, _)))),
have := normed_ring.inverse_continuous_at (h x).unit,
simp only [←ring.inverse_unit, is_unit.unit_spec, ←function.comp_apply] at this ⊢,
exact this.comp (f.continuous_at x),
end
/-- Construct a continuous map into the group of units of a normed ring from a function into the
normed ring and a proof that every element of the range is a unit. -/
@[simps]
noncomputable def units_of_forall_is_unit {f : C(X, R)} (h : ∀ x, is_unit (f x)) : C(X, Rˣ) :=
{ to_fun := λ x, (h x).unit,
continuous_to_fun := normed_ring.is_unit_unit_continuous h }
instance : can_lift C(X, R) C(X, Rˣ) :=
{ coe := λ f, ⟨λ x, f x, units.continuous_coe.comp f.continuous⟩,
cond := λ f, ∀ x, is_unit (f x),
prf := λ f h, ⟨units_of_forall_is_unit h, by { ext, refl }⟩ }
lemma is_unit_iff_forall_is_unit (f : C(X, R)) :
is_unit f ↔ ∀ x, is_unit (f x) :=
iff.intro (λ h, λ x, ⟨units_lift.symm h.unit x, rfl⟩)
(λ h, ⟨(units_of_forall_is_unit h).units_lift, by { ext, refl }⟩)
end normed_ring
section normed_field
variables [normed_field 𝕜] [complete_space 𝕜]
lemma is_unit_iff_forall_ne_zero (f : C(X, 𝕜)) :
is_unit f ↔ ∀ x, f x ≠ 0 :=
by simp_rw [f.is_unit_iff_forall_is_unit, is_unit_iff_ne_zero]
lemma spectrum_eq_range (f : C(X, 𝕜)) :
spectrum 𝕜 f = set.range f :=
begin
ext,
simp only [spectrum.mem_iff, is_unit_iff_forall_ne_zero, not_forall, coe_sub,
pi.sub_apply, algebra_map_apply, algebra.id.smul_eq_mul, mul_one, not_not, set.mem_range,
sub_eq_zero, @eq_comm _ x _]
end
end normed_field
end continuous_map
|
b089c2e0140e1df3bee346eb34c80f5080f85ea4 | d7189ea2ef694124821b033e533f18905b5e87ef | /galois/list/inter.lean | 4d5e3b130c993afc9a4d93307817034a30284329 | [
"Apache-2.0"
] | permissive | digama0/lean-protocol-support | eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59 | cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda | refs/heads/master | 1,625,421,450,627 | 1,506,035,462,000 | 1,506,035,462,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 864 | lean | /- Some theorems about list intersection -/
namespace list
@[simp]
theorem nil_inter {α : Type} [decidable_eq α] (x : list α) : list.nil ∩ x = [] := rfl
@[simp]
theorem cons_inter {α : Type} [decidable_eq α] (a : α) (x y : list α)
: (a::x) ∩ y = if a ∈ y then a :: (x ∩ y) else x ∩ y := rfl
@[simp]
theorem inter_nil {α : Type} [decidable_eq α] (x : list α) : x ∩ list.nil = [] :=
begin
induction x with v xr ind,
{ refl, },
{ simp [cons_inter, ind], },
end
theorem inter_conjunction {α : Type} [decidable_eq α] (e : α) (x y : list α)
: e ∈ (x ∩ y) ↔ e ∈ x ∧ e ∈ y :=
begin
induction x,
case list.nil { simp, },
case list.cons v xr ind {
simp,
by_cases (v ∈ y) with h,
all_goals {
simp [h, ind],
by_cases (e = v) with e_eq_v,
all_goals { simp [*], },
},
},
end
end list
|
c5ef55a493467a09823c02a9053a86bf7bba4ed1 | e0f9ba56b7fedc16ef8697f6caeef5898b435143 | /src/category_theory/full_subcategory.lean | 52513f0576768106e2a55c215cdfe827447aab66 | [
"Apache-2.0"
] | permissive | anrddh/mathlib | 6a374da53c7e3a35cb0298b0cd67824efef362b4 | a4266a01d2dcb10de19369307c986d038c7bb6a6 | refs/heads/master | 1,656,710,827,909 | 1,589,560,456,000 | 1,589,560,456,000 | 264,271,800 | 0 | 0 | Apache-2.0 | 1,589,568,062,000 | 1,589,568,061,000 | null | UTF-8 | Lean | false | false | 3,207 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Reid Barton
-/
import category_theory.fully_faithful
namespace category_theory
universes v u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
section induced
/- Induced categories.
Given a category D and a function F : C → D from a type C to the
objects of D, there is an essentially unique way to give C a
category structure such that F becomes a fully faithful functor,
namely by taking Hom_C(X, Y) = Hom_D(FX, FY). We call this the
category induced from D along F.
As a special case, if C is a subtype of D, this produces the full
subcategory of D on the objects belonging to C. In general the
induced category is equivalent to the full subcategory of D on the
image of F.
-/
/-
It looks odd to make D an explicit argument of `induced_category`,
when it is determined by the argument F anyways. The reason to make D
explicit is in order to control its syntactic form, so that instances
like `induced_category.has_forget₂` (elsewhere) refer to the correct
form of D. This is used to set up several algebraic categories like
def CommMon : Type (u+1) := induced_category Mon (bundled.map @comm_monoid.to_monoid)
-- not `induced_category (bundled monoid) (bundled.map @comm_monoid.to_monoid)`,
-- even though `Mon = bundled monoid`!
-/
variables {C : Type u₁} (D : Type u₂) [category.{v} D]
variables (F : C → D)
include F
def induced_category : Type u₁ := C
variables {D}
instance induced_category.has_coe_to_sort [has_coe_to_sort D] :
has_coe_to_sort (induced_category D F) :=
⟨_, λ c, ↥(F c)⟩
instance induced_category.category : category.{v} (induced_category D F) :=
{ hom := λ X Y, F X ⟶ F Y,
id := λ X, 𝟙 (F X),
comp := λ _ _ _ f g, f ≫ g }
def induced_functor : induced_category D F ⥤ D :=
{ obj := F, map := λ x y f, f }
@[simp] lemma induced_functor.obj {X} : (induced_functor F).obj X = F X := rfl
@[simp] lemma induced_functor.hom {X Y} {f : X ⟶ Y} : (induced_functor F).map f = f := rfl
instance induced_category.full : full (induced_functor F) :=
{ preimage := λ x y f, f }
instance induced_category.faithful : faithful (induced_functor F) := {}
end induced
section full_subcategory
/- A full subcategory is the special case of an induced category with F = subtype.val. -/
variables {C : Type u₂} [category.{v} C]
variables (Z : C → Prop)
instance full_subcategory : category.{v} {X : C // Z X} :=
induced_category.category subtype.val
def full_subcategory_inclusion : {X : C // Z X} ⥤ C :=
induced_functor subtype.val
@[simp] lemma full_subcategory_inclusion.obj {X} :
(full_subcategory_inclusion Z).obj X = X.val := rfl
@[simp] lemma full_subcategory_inclusion.map {X Y} {f : X ⟶ Y} :
(full_subcategory_inclusion Z).map f = f := rfl
instance full_subcategory.ful : full (full_subcategory_inclusion Z) :=
induced_category.full subtype.val
instance full_subcategory.faithful : faithful (full_subcategory_inclusion Z) :=
induced_category.faithful subtype.val
end full_subcategory
end category_theory
|
952a3f9d73aca373b2bda8c59cc046c031ec6361 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/star/big_operators.lean | fd310750c5eae86ab63da8448ee185ad9db3e044 | [
"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 | 937 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import algebra.big_operators.basic
import algebra.star.basic
/-! # Big-operators lemmas about `star` algebraic operations
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
These results are kept separate from `algebra.star.basic` to avoid it needing to import `finset`.
-/
variables {R : Type*}
open_locale big_operators
@[simp] lemma star_prod [comm_monoid R] [star_semigroup R] {α : Type*}
(s : finset α) (f : α → R):
star (∏ x in s, f x) = ∏ x in s, star (f x) :=
map_prod (star_mul_aut : R ≃* R) _ _
@[simp] lemma star_sum [add_comm_monoid R] [star_add_monoid R] {α : Type*}
(s : finset α) (f : α → R):
star (∑ x in s, f x) = ∑ x in s, star (f x) :=
(star_add_equiv : R ≃+ R).map_sum _ _
|
5ece4893c4fcd018b2c0c2b9f2684721c3cf13ba | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/category_theory/products/associator.lean | 6f45d058bd315ca6a8adac2c518b646623cd8325 | [
"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 | 1,599 | 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.products
import category_theory.equivalence
import category_theory.eq_to_hom
universes v₁ v₂ v₃ u₁ u₂ u₃
open category_theory
namespace category_theory.prod
variables (C : Type u₁) [𝒞 : category.{v₁+1} C] (D : Type u₂) [𝒟 : category.{v₂+1} D]
include 𝒞 𝒟
variables (E : Type u₃) [ℰ : category.{v₃+1} E]
include ℰ
def associator : ((C × D) × E) ⥤ (C × (D × E)) :=
{ obj := λ X, (X.1.1, (X.1.2, X.2)),
map := λ _ _ f, (f.1.1, (f.1.2, f.2)) }
@[simp] lemma associator_obj (X) :
(associator C D E).obj X = (X.1.1, (X.1.2, X.2)) :=
rfl
@[simp] lemma associator_map {X Y} (f : X ⟶ Y) :
(associator C D E).map f = (f.1.1, (f.1.2, f.2)) :=
rfl
def inverse_associator : (C × (D × E)) ⥤ ((C × D) × E) :=
{ obj := λ X, ((X.1, X.2.1), X.2.2),
map := λ _ _ f, ((f.1, f.2.1), f.2.2) }
@[simp] lemma inverse_associator_obj (X) :
(inverse_associator C D E).obj X = ((X.1, X.2.1), X.2.2) :=
rfl
@[simp] lemma inverse_associator_map {X Y} (f : X ⟶ Y) :
(inverse_associator C D E).map f = ((f.1, f.2.1), f.2.2) :=
rfl
def associativity : (C × D) × E ≌ C × (D × E) :=
equivalence.mk (associator C D E) (inverse_associator C D E)
(nat_iso.of_components (λ X, eq_to_iso (by simp)) (by tidy))
(nat_iso.of_components (λ X, eq_to_iso (by simp)) (by tidy))
-- TODO pentagon natural transformation? ...satisfying?
end category_theory.prod
|
5e051316aced19f7fe465b2c68b518e132b8761b | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/analysis/box_integral/partition/split.lean | 4e4daf2997f2e06b2835b4b5aed100b37f5c493a | [
"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 | 15,633 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import analysis.box_integral.partition.basic
/-!
# Split a box along one or more hyperplanes
## Main definitions
A hyperplane `{x : ι → ℝ | x i = a}` splits a rectangular box `I : box_integral.box ι` into two
smaller boxes. If `a ∉ Ioo (I.lower i, I.upper i)`, then one of these boxes is empty, so it is not a
box in the sense of `box_integral.box`.
We introduce the following definitions.
* `box_integral.box.split_lower I i a` and `box_integral.box.split_upper I i a` are these boxes (as
`with_bot (box_integral.box ι)`);
* `box_integral.prepartition.split I i a` is the partition of `I` made of these two boxes (or of one
box `I` if one of these boxes is empty);
* `box_integral.prepartition.split_many I s`, where `s : finset (ι × ℝ)` is a finite set of
hyperplanes `{x : ι → ℝ | x i = a}` encoded as pairs `(i, a)`, is the partition of `I` made by
cutting it along all the hyperplanes in `s`.
## Main results
The main result `box_integral.prepartition.exists_Union_eq_diff` says that any prepartition `π` of
`I` admits a prepartition `π'` of `I` that covers exactly `I \ π.Union`. One of these prepartitions
is available as `box_integral.prepartition.compl`.
## Tags
rectangular box, partition, hyperplane
-/
noncomputable theory
open_locale classical big_operators filter
open function set filter
namespace box_integral
variables {ι M : Type*} {n : ℕ}
namespace box
variables {I : box ι} {i : ι} {x : ℝ} {y : ι → ℝ}
/-- Given a box `I` and `x ∈ (I.lower i, I.upper i)`, the hyperplane `{y : ι → ℝ | y i = x}` splits
`I` into two boxes. `box_integral.box.split_lower I i x` is the box `I ∩ {y | y i ≤ x}`
(if it is nonempty). As usual, we represent a box that may be empty as
`with_bot (box_integral.box ι)`. -/
def split_lower (I : box ι) (i : ι) (x : ℝ) : with_bot (box ι) :=
mk' I.lower (update I.upper i (min x (I.upper i)))
@[simp] lemma coe_split_lower : (split_lower I i x : set (ι → ℝ)) = I ∩ {y | y i ≤ x} :=
begin
rw [split_lower, coe_mk'],
ext y,
simp only [mem_univ_pi, mem_Ioc, mem_inter_eq, mem_coe, mem_set_of_eq, forall_and_distrib,
← pi.le_def, le_update_iff, le_min_iff, and_assoc, and_forall_ne i, mem_def],
rw [and_comm (y i ≤ x), pi.le_def]
end
lemma split_lower_le : I.split_lower i x ≤ I := with_bot_coe_subset_iff.1 $ by simp
@[simp] lemma split_lower_eq_bot {i x} : I.split_lower i x = ⊥ ↔ x ≤ I.lower i :=
begin
rw [split_lower, mk'_eq_bot, exists_update_iff I.upper (λ j y, y ≤ I.lower j)],
simp [(I.lower_lt_upper _).not_le]
end
@[simp] lemma split_lower_eq_self : I.split_lower i x = I ↔ I.upper i ≤ x :=
by simp [split_lower, update_eq_iff]
lemma split_lower_def [decidable_eq ι] {i x} (h : x ∈ Ioo (I.lower i) (I.upper i))
(h' : ∀ j, I.lower j < update I.upper i x j :=
(forall_update_iff I.upper (λ j y, I.lower j < y)).2 ⟨h.1, λ j hne, I.lower_lt_upper _⟩) :
I.split_lower i x = (⟨I.lower, update I.upper i x, h'⟩ : box ι) :=
by { simp only [split_lower, mk'_eq_coe, min_eq_left h.2.le], use rfl, congr }
/-- Given a box `I` and `x ∈ (I.lower i, I.upper i)`, the hyperplane `{y : ι → ℝ | y i = x}` splits
`I` into two boxes. `box_integral.box.split_upper I i x` is the box `I ∩ {y | x < y i}`
(if it is nonempty). As usual, we represent a box that may be empty as
`with_bot (box_integral.box ι)`. -/
def split_upper (I : box ι) (i : ι) (x : ℝ) : with_bot (box ι) :=
mk' (update I.lower i (max x (I.lower i))) I.upper
@[simp] lemma coe_split_upper : (split_upper I i x : set (ι → ℝ)) = I ∩ {y | x < y i} :=
begin
rw [split_upper, coe_mk'],
ext y,
simp only [mem_univ_pi, mem_Ioc, mem_inter_eq, mem_coe, mem_set_of_eq, forall_and_distrib,
forall_update_iff I.lower (λ j z, z < y j), max_lt_iff, and_assoc (x < y i),
and_forall_ne i, mem_def],
exact and_comm _ _
end
lemma split_upper_le : I.split_upper i x ≤ I := with_bot_coe_subset_iff.1 $ by simp
@[simp] lemma split_upper_eq_bot {i x} : I.split_upper i x = ⊥ ↔ I.upper i ≤ x :=
begin
rw [split_upper, mk'_eq_bot, exists_update_iff I.lower (λ j y, I.upper j ≤ y)],
simp [(I.lower_lt_upper _).not_le]
end
@[simp] lemma split_upper_eq_self : I.split_upper i x = I ↔ x ≤ I.lower i :=
by simp [split_upper, update_eq_iff]
lemma split_upper_def [decidable_eq ι] {i x} (h : x ∈ Ioo (I.lower i) (I.upper i))
(h' : ∀ j, update I.lower i x j < I.upper j :=
(forall_update_iff I.lower (λ j y, y < I.upper j)).2 ⟨h.2, λ j hne, I.lower_lt_upper _⟩) :
I.split_upper i x = (⟨update I.lower i x, I.upper, h'⟩ : box ι) :=
by { simp only [split_upper, mk'_eq_coe, max_eq_left h.1.le], refine ⟨_, rfl⟩, congr }
lemma disjoint_split_lower_split_upper (I : box ι) (i : ι) (x : ℝ) :
disjoint (I.split_lower i x) (I.split_upper i x) :=
begin
rw [← disjoint_with_bot_coe, coe_split_lower, coe_split_upper],
refine (disjoint.inf_left' _ _).inf_right' _,
exact λ y (hy : y i ≤ x ∧ x < y i), not_lt_of_le hy.1 hy.2
end
lemma split_lower_ne_split_upper (I : box ι) (i : ι) (x : ℝ) :
I.split_lower i x ≠ I.split_upper i x :=
begin
cases le_or_lt x (I.lower i),
{ rw [split_upper_eq_self.2 h, split_lower_eq_bot.2 h], exact with_bot.bot_ne_coe _ },
{ refine (disjoint_split_lower_split_upper I i x).ne _,
rwa [ne.def, split_lower_eq_bot, not_le] }
end
end box
namespace prepartition
variables {I J : box ι} {i : ι} {x : ℝ}
/-- The partition of `I : box ι` into the boxes `I ∩ {y | y ≤ x i}` and `I ∩ {y | x i < y}`.
One of these boxes can be empty, then this partition is just the single-box partition `⊤`. -/
def split (I : box ι) (i : ι) (x : ℝ) : prepartition I :=
of_with_bot {I.split_lower i x, I.split_upper i x}
begin
simp only [finset.mem_insert, finset.mem_singleton],
rintro J (rfl|rfl),
exacts [box.split_lower_le, box.split_upper_le]
end
begin
simp only [finset.coe_insert, finset.coe_singleton, true_and, set.mem_singleton_iff,
pairwise_insert_of_symmetric symmetric_disjoint, pairwise_singleton],
rintro J rfl -,
exact I.disjoint_split_lower_split_upper i x
end
@[simp] lemma mem_split_iff : J ∈ split I i x ↔ ↑J = I.split_lower i x ∨ ↑J = I.split_upper i x :=
by simp [split]
lemma mem_split_iff' : J ∈ split I i x ↔
(J : set (ι → ℝ)) = I ∩ {y | y i ≤ x} ∨ (J : set (ι → ℝ)) = I ∩ {y | x < y i} :=
by simp [mem_split_iff, ← box.with_bot_coe_inj]
@[simp] lemma Union_split (I : box ι) (i : ι) (x : ℝ) : (split I i x).Union = I :=
by simp [split, ← inter_union_distrib_left, ← set_of_or, le_or_lt]
lemma is_partition_split (I : box ι) (i : ι) (x : ℝ) : is_partition (split I i x) :=
is_partition_iff_Union_eq.2 $ Union_split I i x
lemma sum_split_boxes {M : Type*} [add_comm_monoid M] (I : box ι) (i : ι) (x : ℝ) (f : box ι → M) :
∑ J in (split I i x).boxes, f J = (I.split_lower i x).elim 0 f + (I.split_upper i x).elim 0 f :=
by rw [split, sum_of_with_bot, finset.sum_pair (I.split_lower_ne_split_upper i x)]
/-- If `x ∉ (I.lower i, I.upper i)`, then the hyperplane `{y | y i = x}` does not split `I`. -/
lemma split_of_not_mem_Ioo (h : x ∉ Ioo (I.lower i) (I.upper i)) : split I i x = ⊤ :=
begin
refine ((is_partition_top I).eq_of_boxes_subset (λ J hJ, _)).symm,
rcases mem_top.1 hJ with rfl, clear hJ,
rw [mem_boxes, mem_split_iff],
rw [mem_Ioo, not_and_distrib, not_lt, not_lt] at h,
cases h; [right, left],
{ rwa [eq_comm, box.split_upper_eq_self] },
{ rwa [eq_comm, box.split_lower_eq_self] }
end
lemma coe_eq_of_mem_split_of_mem_le {y : ι → ℝ} (h₁ : J ∈ split I i x) (h₂ : y ∈ J) (h₃ : y i ≤ x) :
(J : set (ι → ℝ)) = I ∩ {y | y i ≤ x} :=
(mem_split_iff'.1 h₁).resolve_right $ λ H,
by { rw [← box.mem_coe, H] at h₂, exact h₃.not_lt h₂.2 }
lemma coe_eq_of_mem_split_of_lt_mem {y : ι → ℝ} (h₁ : J ∈ split I i x) (h₂ : y ∈ J) (h₃ : x < y i) :
(J : set (ι → ℝ)) = I ∩ {y | x < y i} :=
(mem_split_iff'.1 h₁).resolve_left $ λ H,
by { rw [← box.mem_coe, H] at h₂, exact h₃.not_le h₂.2 }
@[simp] lemma restrict_split (h : I ≤ J) (i : ι) (x : ℝ) : (split J i x).restrict I = split I i x :=
begin
refine ((is_partition_split J i x).restrict h).eq_of_boxes_subset _,
simp only [finset.subset_iff, mem_boxes, mem_restrict', exists_prop, mem_split_iff'],
have : ∀ s, (I ∩ s : set (ι → ℝ)) ⊆ J, from λ s, (inter_subset_left _ _).trans h,
rintro J₁ ⟨J₂, (H₂|H₂), H₁⟩; [left, right]; simp [H₁, H₂, inter_left_comm ↑I, this],
end
lemma inf_split (π : prepartition I) (i : ι) (x : ℝ) :
π ⊓ split I i x = π.bUnion (λ J, split J i x) :=
bUnion_congr_of_le rfl $ λ J hJ, restrict_split hJ i x
/-- Split a box along many hyperplanes `{y | y i = x}`; each hyperplane is given by the pair
`(i x)`. -/
def split_many (I : box ι) (s : finset (ι × ℝ)) : prepartition I :=
s.inf (λ p, split I p.1 p.2)
@[simp] lemma split_many_empty (I : box ι) : split_many I ∅ = ⊤ := finset.inf_empty
@[simp] lemma split_many_insert (I : box ι) (s : finset (ι × ℝ)) (p : ι × ℝ) :
split_many I (insert p s) = split_many I s ⊓ split I p.1 p.2 :=
by rw [split_many, finset.inf_insert, inf_comm, split_many]
lemma split_many_le_split (I : box ι) {s : finset (ι × ℝ)} {p : ι × ℝ} (hp : p ∈ s) :
split_many I s ≤ split I p.1 p.2 :=
finset.inf_le hp
lemma is_partition_split_many (I : box ι) (s : finset (ι × ℝ)) :
is_partition (split_many I s) :=
finset.induction_on s (by simp only [split_many_empty, is_partition_top]) $
λ a s ha hs, by simpa only [split_many_insert, inf_split]
using hs.bUnion (λ J hJ, is_partition_split _ _ _)
@[simp] lemma Union_split_many (I : box ι) (s : finset (ι × ℝ)) : (split_many I s).Union = I :=
(is_partition_split_many I s).Union_eq
lemma inf_split_many {I : box ι} (π : prepartition I) (s : finset (ι × ℝ)) :
π ⊓ split_many I s = π.bUnion (λ J, split_many J s) :=
begin
induction s using finset.induction_on with p s hp ihp,
{ simp },
{ simp_rw [split_many_insert, ← inf_assoc, ihp, inf_split, bUnion_assoc] }
end
/-- Let `s : finset (ι × ℝ)` be a set of hyperplanes `{x : ι → ℝ | x i = r}` in `ι → ℝ` encoded as
pairs `(i, r)`. Suppose that this set contains all faces of a box `J`. The hyperplanes of `s` split
a box `I` into subboxes. Let `Js` be one of them. If `J` and `Js` have nonempty intersection, then
`Js` is a subbox of `J`. -/
lemma not_disjoint_imp_le_of_subset_of_mem_split_many {I J Js : box ι} {s : finset (ι × ℝ)}
(H : ∀ i, {(i, J.lower i), (i, J.upper i)} ⊆ s) (HJs : Js ∈ split_many I s)
(Hn : ¬disjoint (J : with_bot (box ι)) Js) : Js ≤ J :=
begin
simp only [finset.insert_subset, finset.singleton_subset_iff] at H,
rcases box.not_disjoint_coe_iff_nonempty_inter.mp Hn with ⟨x, hx, hxs⟩,
refine λ y hy i, ⟨_, _⟩,
{ rcases split_many_le_split I (H i).1 HJs with ⟨Jl, Hmem : Jl ∈ split I i (J.lower i), Hle⟩,
have := Hle hxs,
rw [← box.coe_subset_coe, coe_eq_of_mem_split_of_lt_mem Hmem this (hx i).1] at Hle,
exact (Hle hy).2 },
{ rcases split_many_le_split I (H i).2 HJs with ⟨Jl, Hmem : Jl ∈ split I i (J.upper i), Hle⟩,
have := Hle hxs,
rw [← box.coe_subset_coe, coe_eq_of_mem_split_of_mem_le Hmem this (hx i).2] at Hle,
exact (Hle hy).2 }
end
section fintype
variable [fintype ι]
/-- Let `s` be a finite set of boxes in `ℝⁿ = ι → ℝ`. Then there exists a finite set `t₀` of
hyperplanes (namely, the set of all hyperfaces of boxes in `s`) such that for any `t ⊇ t₀`
and any box `I` in `ℝⁿ` the following holds. The hyperplanes from `t` split `I` into subboxes.
Let `J'` be one of them, and let `J` be one of the boxes in `s`. If these boxes have a nonempty
intersection, then `J' ≤ J`. -/
lemma eventually_not_disjoint_imp_le_of_mem_split_many (s : finset (box ι)) :
∀ᶠ t : finset (ι × ℝ) in at_top, ∀ (I : box ι) (J ∈ s) (J' ∈ split_many I t),
¬disjoint (J : with_bot (box ι)) J' → J' ≤ J :=
begin
refine eventually_at_top.2
⟨s.bUnion (λ J, finset.univ.bUnion (λ i, {(i, J.lower i), (i, J.upper i)})),
λ t ht I J hJ J' hJ', not_disjoint_imp_le_of_subset_of_mem_split_many (λ i, _) hJ'⟩,
exact λ p hp, ht (finset.mem_bUnion.2 ⟨J, hJ, finset.mem_bUnion.2 ⟨i, finset.mem_univ _, hp⟩⟩)
end
lemma eventually_split_many_inf_eq_filter (π : prepartition I) :
∀ᶠ t : finset (ι × ℝ) in at_top,
π ⊓ (split_many I t) = (split_many I t).filter (λ J, ↑J ⊆ π.Union) :=
begin
refine (eventually_not_disjoint_imp_le_of_mem_split_many π.boxes).mono (λ t ht, _),
refine le_antisymm ((bUnion_le_iff _).2 $ λ J hJ, _) (le_inf (λ J hJ, _) (filter_le _ _)),
{ refine of_with_bot_mono _,
simp only [finset.mem_image, exists_prop, mem_boxes, mem_filter],
rintro _ ⟨J₁, h₁, rfl⟩ hne,
refine ⟨_, ⟨J₁, ⟨h₁, subset.trans _ (π.subset_Union hJ)⟩, rfl⟩, le_rfl⟩,
exact ht I J hJ J₁ h₁ (mt disjoint_iff.1 hne) },
{ rw mem_filter at hJ,
rcases set.mem_bUnion_iff.1 (hJ.2 J.upper_mem) with ⟨J', hJ', hmem⟩,
refine ⟨J', hJ', ht I _ hJ' _ hJ.1 $ box.not_disjoint_coe_iff_nonempty_inter.2 _⟩,
exact ⟨J.upper, hmem, J.upper_mem⟩ }
end
lemma exists_split_many_inf_eq_filter_of_finite (s : set (prepartition I)) (hs : s.finite) :
∃ t : finset (ι × ℝ), ∀ π ∈ s,
π ⊓ (split_many I t) = (split_many I t).filter (λ J, ↑J ⊆ π.Union) :=
begin
have := λ π (hπ : π ∈ s), eventually_split_many_inf_eq_filter π,
exact (hs.eventually_all.2 this).exists
end
/-- If `π` is a partition of `I`, then there exists a finite set `s` of hyperplanes such that
`split_many I s ≤ π`. -/
lemma is_partition.exists_split_many_le {I : box ι} {π : prepartition I}
(h : is_partition π) : ∃ s, split_many I s ≤ π :=
(eventually_split_many_inf_eq_filter π).exists.imp $ λ s hs,
by { rwa [h.Union_eq, filter_of_true, inf_eq_right] at hs, exact λ J hJ, le_of_mem _ hJ }
/-- For every prepartition `π` of `I` there exists a prepartition that covers exactly
`I \ π.Union`. -/
lemma exists_Union_eq_diff (π : prepartition I) :
∃ π' : prepartition I, π'.Union = I \ π.Union :=
begin
rcases π.eventually_split_many_inf_eq_filter.exists with ⟨s, hs⟩,
use (split_many I s).filter (λ J, ¬(J : set (ι → ℝ)) ⊆ π.Union),
simp [← hs]
end
/-- If `π` is a prepartition of `I`, then `π.compl` is a prepartition of `I`
such that `π.compl.Union = I \ π.Union`. -/
def compl (π : prepartition I) : prepartition I := π.exists_Union_eq_diff.some
@[simp] lemma Union_compl (π : prepartition I) : π.compl.Union = I \ π.Union :=
π.exists_Union_eq_diff.some_spec
/-- Since the definition of `box_integral.prepartition.compl` uses `Exists.some`,
the result depends only on `π.Union`. -/
lemma compl_congr {π₁ π₂ : prepartition I} (h : π₁.Union = π₂.Union) :
π₁.compl = π₂.compl :=
by { dunfold compl, congr' 1, rw h }
lemma is_partition.compl_eq_bot {π : prepartition I} (h : is_partition π) : π.compl = ⊥ :=
by rw [← Union_eq_empty, Union_compl, h.Union_eq, diff_self]
@[simp] lemma compl_top : (⊤ : prepartition I).compl = ⊥ := (is_partition_top I).compl_eq_bot
end fintype
end prepartition
end box_integral
|
f8b9e5bf9bccc8332cd55aaf40c87e27e6a9efee | 367134ba5a65885e863bdc4507601606690974c1 | /src/ring_theory/mv_polynomial/basic.lean | 647856c55ce1962fc6629e67250f1108ee463cd8 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 3,901 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl
-/
import ring_theory.ideal.operations
import linear_algebra.finsupp_vector_space
import algebra.char_p.basic
/-!
# Multivariate polynomials over commutative rings
This file contains basic facts about multivariate polynomials over commutative rings, for example
that the monomials form a basis.
## Main definitions
* `restrict_total_degree σ R m`: the subspace of multivariate polynomials indexed by `σ` over the
commutative ring `R` of total degree at most `m`.
* `restrict_degree σ R m`: the subspace of multivariate polynomials indexed by `σ` over the
commutative ring `R` such that the degree in each individual variable is at most `m`.
## Main statements
* The multivariate polynomial ring over a commutative ring of positive characteristic has positive
characteristic.
* `is_basis_monomials`: shows that the monomials form a basis of the vector space of multivariate
polynomials.
## TODO
Generalise to noncommutative (semi)rings
-/
noncomputable theory
open_locale classical
open set linear_map submodule
open_locale big_operators
universes u v
variables (σ : Type u) (R : Type v) [comm_ring R] (p m : ℕ)
namespace mv_polynomial
section char_p
instance [char_p R p] : char_p (mv_polynomial σ R) p :=
{ cast_eq_zero_iff := λ n, by rw [← C_eq_coe_nat, ← C_0, C_inj, char_p.cast_eq_zero_iff R p] }
end char_p
section homomorphism
lemma map_range_eq_map {R S : Type*} [comm_ring R] [comm_ring S] (p : mv_polynomial σ R)
(f : R →+* S) :
finsupp.map_range f f.map_zero p = map f p :=
begin
rw [← finsupp.sum_single p, finsupp.sum],
-- It's not great that we need to use an `erw` here,
-- but hopefully it will become smoother when we move entirely away from `is_semiring_hom`.
erw [finsupp.map_range_finset_sum (f : R →+ S)],
rw [← (finsupp.support p).sum_hom (map f)],
{ refine finset.sum_congr rfl (assume n _, _),
rw [finsupp.map_range_single, ← monomial, ← monomial, map_monomial], refl, },
apply_instance
end
end homomorphism
section degree
/-- The submodule of polynomials of total degree less than or equal to `m`.-/
def restrict_total_degree : submodule R (mv_polynomial σ R) :=
finsupp.supported _ _ {n | n.sum (λn e, e) ≤ m }
/-- The submodule of polynomials such that the degree with respect to each individual variable is
less than or equal to `m`.-/
def restrict_degree (m : ℕ) : submodule R (mv_polynomial σ R) :=
finsupp.supported _ _ {n | ∀i, n i ≤ m }
variable {R}
lemma mem_restrict_total_degree (p : mv_polynomial σ R) :
p ∈ restrict_total_degree σ R m ↔ p.total_degree ≤ m :=
begin
rw [total_degree, finset.sup_le_iff],
refl
end
lemma mem_restrict_degree (p : mv_polynomial σ R) (n : ℕ) :
p ∈ restrict_degree σ R n ↔ (∀s ∈ p.support, ∀i, (s : σ →₀ ℕ) i ≤ n) :=
begin
rw [restrict_degree, finsupp.mem_supported],
refl
end
lemma mem_restrict_degree_iff_sup (p : mv_polynomial σ R) (n : ℕ) :
p ∈ restrict_degree σ R n ↔ ∀i, p.degrees.count i ≤ n :=
begin
simp only [mem_restrict_degree, degrees, multiset.count_sup, finsupp.count_to_multiset,
finset.sup_le_iff],
exact ⟨assume h n s hs, h s hs n, assume h s hs n, h n s hs⟩
end
variables (σ R)
lemma is_basis_monomials :
is_basis R ((λs, (monomial s 1 : mv_polynomial σ R))) :=
suffices is_basis R (λ (sa : Σ _, unit), (monomial sa.1 1 : mv_polynomial σ R)),
begin
apply is_basis.comp this (λ (s : σ →₀ ℕ), ⟨s, punit.star⟩),
split,
{ intros x y hxy,
simpa using hxy },
{ rintros ⟨x₁, x₂⟩,
use x₁,
rw punit_eq punit.star x₂ }
end,
begin
apply finsupp.is_basis_single (λ _ _, (1 : R)),
intro _,
apply is_basis_singleton_one,
end
end degree
end mv_polynomial
|
b08b85e6ece6aec8f32619cd53cf8ca5cad30e35 | 9a0b1b3a653ea926b03d1495fef64da1d14b3174 | /tidy/lib/options.lean | ca45bc7ccab0f04d64b82cf1f93df52d385b61db | [
"Apache-2.0"
] | permissive | khoek/mathlib-tidy | 8623b27b4e04e7d598164e7eaf248610d58f768b | 866afa6ab597c47f1b72e8fe2b82b97fff5b980f | refs/heads/master | 1,585,598,975,772 | 1,538,659,544,000 | 1,538,659,544,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 152 | lean | meta def options.set_list : options → list (name × bool) → options
| o [] := o
| o ((n, v) :: rest) := (o.set_bool n v).set_list rest |
11fe78c8bedbd0a3f9702679529ee1caa24188a5 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/category_theory/limits/over.lean | 9205c57403fbfab77330f23278446e7aad1551a8 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 5,035 | lean | /-
Copyright (c) 2018 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Reid Barton
-/
import category_theory.comma
import category_theory.limits.preserves
universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation
open category_theory category_theory.limits
variables {J : Type v} [small_category J]
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
variable {X : C}
namespace category_theory.functor
@[simps] def to_cocone (F : J ⥤ over X) : cocone (F ⋙ over.forget) :=
{ X := X,
ι := { app := λ j, (F.obj j).hom } }
@[simps] def to_cone (F : J ⥤ under X) : cone (F ⋙ under.forget) :=
{ X := X,
π := { app := λ j, (F.obj j).hom } }
end category_theory.functor
namespace category_theory.over
@[simps] def colimit (F : J ⥤ over X) [has_colimit (F ⋙ forget)] : cocone F :=
{ X := mk $ colimit.desc (F ⋙ forget) F.to_cocone,
ι :=
{ app := λ j, hom_mk $ colimit.ι (F ⋙ forget) j,
naturality' :=
begin
intros j j' f,
have := colimit.w (F ⋙ forget) f,
tidy
end } }
def forget_colimit_is_colimit (F : J ⥤ over X) [has_colimit (F ⋙ forget)] :
is_colimit (forget.map_cocone (colimit F)) :=
is_colimit.of_iso_colimit (colimit.is_colimit (F ⋙ forget)) (cocones.ext (iso.refl _) (by tidy))
instance : reflects_colimits (forget : over X ⥤ C) :=
{ reflects_colimits_of_shape := λ J 𝒥,
{ reflects_colimit := λ F,
by constructor; exactI λ t ht,
{ desc := λ s, hom_mk (ht.desc (forget.map_cocone s))
begin
apply ht.hom_ext, intro j,
rw [←category.assoc, ht.fac],
transitivity (F.obj j).hom,
exact w (s.ι.app j), -- TODO: How to write (s.ι.app j).w?
exact (w (t.ι.app j)).symm,
end,
fac' := begin
intros s j, ext, exact ht.fac (forget.map_cocone s) j
-- TODO: Ask Simon about multiple ext lemmas for defeq types (comma_morphism & over.category.hom)
end,
uniq' :=
begin
intros s m w,
ext1 j,
exact ht.uniq (forget.map_cocone s) m.left (λ j, congr_arg comma_morphism.left (w j))
end } } }
instance has_colimit {F : J ⥤ over X} [has_colimit (F ⋙ forget)] : has_colimit F :=
{ cocone := colimit F,
is_colimit := reflects_colimit.reflects (forget_colimit_is_colimit F) }
instance has_colimits_of_shape [has_colimits_of_shape J C] :
has_colimits_of_shape J (over X) :=
{ has_colimit := λ F, by apply_instance }
instance has_colimits [has_colimits.{v} C] : has_colimits.{v} (over X) :=
{ has_colimits_of_shape := λ J 𝒥, by resetI; apply_instance }
instance forget_preserves_colimits [has_colimits.{v} C] {X : C} :
preserves_colimits (forget : over X ⥤ C) :=
{ preserves_colimits_of_shape := λ J 𝒥,
{ preserves_colimit := λ F, by exactI
preserves_colimit_of_preserves_colimit_cocone (colimit.is_colimit F) (forget_colimit_is_colimit F) } }
end category_theory.over
namespace category_theory.under
@[simps] def limit (F : J ⥤ under X) [has_limit (F ⋙ forget)] : cone F :=
{ X := mk $ limit.lift (F ⋙ forget) F.to_cone,
π :=
{ app := λ j, hom_mk $ limit.π (F ⋙ forget) j,
naturality' :=
begin
intros j j' f,
have := (limit.w (F ⋙ forget) f).symm,
tidy
end } }
def forget_limit_is_limit (F : J ⥤ under X) [has_limit (F ⋙ forget)] :
is_limit (forget.map_cone (limit F)) :=
is_limit.of_iso_limit (limit.is_limit (F ⋙ forget)) (cones.ext (iso.refl _) (by tidy))
instance : reflects_limits (forget : under X ⥤ C) :=
{ reflects_limits_of_shape := λ J 𝒥,
{ reflects_limit := λ F,
by constructor; exactI λ t ht,
{ lift := λ s, hom_mk (ht.lift (forget.map_cone s))
begin
apply ht.hom_ext, intro j,
rw [category.assoc, ht.fac],
transitivity (F.obj j).hom,
exact w (s.π.app j),
exact (w (t.π.app j)).symm,
end,
fac' := begin
intros s j, ext, exact ht.fac (forget.map_cone s) j
end,
uniq' :=
begin
intros s m w,
ext1 j,
exact ht.uniq (forget.map_cone s) m.right (λ j, congr_arg comma_morphism.right (w j))
end } } }
instance has_limit {F : J ⥤ under X} [has_limit (F ⋙ forget)] : has_limit F :=
{ cone := limit F,
is_limit := reflects_limit.reflects (forget_limit_is_limit F) }
instance has_limits_of_shape [has_limits_of_shape J C] :
has_limits_of_shape J (under X) :=
{ has_limit := λ F, by apply_instance }
instance has_limits [has_limits.{v} C] : has_limits.{v} (under X) :=
{ has_limits_of_shape := λ J 𝒥, by resetI; apply_instance }
instance forget_preserves_limits [has_limits.{v} C] {X : C} :
preserves_limits (forget : under X ⥤ C) :=
{ preserves_limits_of_shape := λ J 𝒥,
{ preserves_limit := λ F, by exactI
preserves_limit_of_preserves_limit_cone (limit.is_limit F) (forget_limit_is_limit F) } }
end category_theory.under
|
f89b759d2b1151767ed1f4ab9f0e6e073e06901f | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/limits/shapes/kernel_pair.lean | fe3a7ec7685a24a768117177065fb5d80fa6d7f4 | [
"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 | 6,411 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.limits.shapes.equalizers
import category_theory.limits.shapes.pullbacks
import category_theory.limits.shapes.regular_mono
/-!
# Kernel pairs
This file defines what it means for a parallel pair of morphisms `a b : R ⟶ X` to be the kernel pair
for a morphism `f`.
Some properties of kernel pairs are given, namely allowing one to transfer between
the kernel pair of `f₁ ≫ f₂` to the kernel pair of `f₁`.
It is also proved that if `f` is a coequalizer of some pair, and `a`,`b` is a kernel pair for `f`
then it is a coequalizer of `a`,`b`.
## Implementation
The definition is essentially just a wrapper for `is_limit (pullback_cone.mk _ _ _)`, but the
constructions given here are useful, yet awkward to present in that language, so a basic API
is developed here.
## TODO
- Internal equivalence relations (or congruences) and the fact that every kernel pair induces one,
and the converse in an effective regular category (WIP by b-mehta).
-/
universes v u u₂
namespace category_theory
open category_theory category_theory.category category_theory.limits
variables {C : Type u} [category.{v} C]
variables {R X Y Z : C} (f : X ⟶ Y) (a b : R ⟶ X)
/--
`is_kernel_pair f a b` expresses that `(a, b)` is a kernel pair for `f`, i.e. `a ≫ f = b ≫ f`
and the square
R → X
↓ ↓
X → Y
is a pullback square.
This is essentially just a convenience wrapper over `is_limit (pullback_cone.mk _ _ _)`.
-/
structure is_kernel_pair :=
(comm : a ≫ f = b ≫ f)
(is_limit : is_limit (pullback_cone.mk _ _ comm))
attribute [reassoc] is_kernel_pair.comm
namespace is_kernel_pair
/-- The data expressing that `(a, b)` is a kernel pair is subsingleton. -/
instance : subsingleton (is_kernel_pair f a b) :=
⟨λ P Q, by { cases P, cases Q, congr, }⟩
/-- If `f` is a monomorphism, then `(𝟙 _, 𝟙 _)` is a kernel pair for `f`. -/
def id_of_mono [mono f] : is_kernel_pair f (𝟙 _) (𝟙 _) :=
{ comm := rfl,
is_limit :=
pullback_cone.is_limit_aux' _ $ λ s,
begin
refine ⟨s.snd, _, comp_id _, λ m m₁ m₂, _⟩,
{ rw [← cancel_mono f, s.condition, pullback_cone.mk_fst, cancel_mono f],
apply comp_id },
rw [← m₂],
apply (comp_id _).symm,
end }
instance [mono f] : inhabited (is_kernel_pair f (𝟙 _) (𝟙 _)) := ⟨id_of_mono f⟩
variables {f a b}
/--
Given a pair of morphisms `p`, `q` to `X` which factor through `f`, they factor through any kernel
pair of `f`.
-/
def lift' {S : C} (k : is_kernel_pair f a b) (p q : S ⟶ X) (w : p ≫ f = q ≫ f) :
{ t : S ⟶ R // t ≫ a = p ∧ t ≫ b = q } :=
pullback_cone.is_limit.lift' k.is_limit _ _ w
/--
If `(a,b)` is a kernel pair for `f₁ ≫ f₂` and `a ≫ f₁ = b ≫ f₁`, then `(a,b)` is a kernel pair for
just `f₁`.
That is, to show that `(a,b)` is a kernel pair for `f₁` it suffices to only show the square
commutes, rather than to additionally show it's a pullback.
-/
def cancel_right {f₁ : X ⟶ Y} {f₂ : Y ⟶ Z} (comm : a ≫ f₁ = b ≫ f₁)
(big_k : is_kernel_pair (f₁ ≫ f₂) a b) :
is_kernel_pair f₁ a b :=
{ comm := comm,
is_limit := pullback_cone.is_limit_aux' _ $ λ s,
begin
let s' : pullback_cone (f₁ ≫ f₂) (f₁ ≫ f₂) :=
pullback_cone.mk s.fst s.snd (s.condition_assoc _),
refine ⟨big_k.is_limit.lift s',
big_k.is_limit.fac _ walking_cospan.left,
big_k.is_limit.fac _ walking_cospan.right,
λ m m₁ m₂, _⟩,
apply big_k.is_limit.hom_ext,
refine ((pullback_cone.mk a b _) : pullback_cone (f₁ ≫ f₂) _).equalizer_ext _ _,
apply m₁.trans (big_k.is_limit.fac s' walking_cospan.left).symm,
apply m₂.trans (big_k.is_limit.fac s' walking_cospan.right).symm,
end }
/--
If `(a,b)` is a kernel pair for `f₁ ≫ f₂` and `f₂` is mono, then `(a,b)` is a kernel pair for
just `f₁`.
The converse of `comp_of_mono`.
-/
def cancel_right_of_mono {f₁ : X ⟶ Y} {f₂ : Y ⟶ Z} [mono f₂]
(big_k : is_kernel_pair (f₁ ≫ f₂) a b) :
is_kernel_pair f₁ a b :=
cancel_right (begin rw [← cancel_mono f₂, assoc, assoc, big_k.comm] end) big_k
/--
If `(a,b)` is a kernel pair for `f₁` and `f₂` is mono, then `(a,b)` is a kernel pair for `f₁ ≫ f₂`.
The converse of `cancel_right_of_mono`.
-/
def comp_of_mono {f₁ : X ⟶ Y} {f₂ : Y ⟶ Z} [mono f₂] (small_k : is_kernel_pair f₁ a b) :
is_kernel_pair (f₁ ≫ f₂) a b :=
{ comm := by rw [small_k.comm_assoc],
is_limit := pullback_cone.is_limit_aux' _ $ λ s,
begin
refine ⟨_, _, _, _⟩,
apply (pullback_cone.is_limit.lift' small_k.is_limit s.fst s.snd _).1,
rw [← cancel_mono f₂, assoc, s.condition, assoc],
apply (pullback_cone.is_limit.lift' small_k.is_limit s.fst s.snd _).2.1,
apply (pullback_cone.is_limit.lift' small_k.is_limit s.fst s.snd _).2.2,
intros m m₁ m₂,
apply small_k.is_limit.hom_ext,
refine ((pullback_cone.mk a b _) : pullback_cone f₁ _).equalizer_ext _ _,
rwa (pullback_cone.is_limit.lift' small_k.is_limit s.fst s.snd _).2.1,
rwa (pullback_cone.is_limit.lift' small_k.is_limit s.fst s.snd _).2.2,
end }
/--
If `(a,b)` is the kernel pair of `f`, and `f` is a coequalizer morphism for some parallel pair, then
`f` is a coequalizer morphism of `a` and `b`.
-/
def to_coequalizer (k : is_kernel_pair f a b) [r : regular_epi f] :
is_colimit (cofork.of_π f k.comm) :=
begin
let t := k.is_limit.lift (pullback_cone.mk _ _ r.w),
have ht : t ≫ a = r.left := k.is_limit.fac _ walking_cospan.left,
have kt : t ≫ b = r.right := k.is_limit.fac _ walking_cospan.right,
apply cofork.is_colimit.mk _ _ _ _,
{ intro s,
apply (cofork.is_colimit.desc' r.is_colimit s.π _).1,
rw [← ht, assoc, s.condition, reassoc_of kt] },
{ intro s,
apply (cofork.is_colimit.desc' r.is_colimit s.π _).2 },
{ intros s m w,
apply r.is_colimit.hom_ext,
rintro ⟨⟩,
change (r.left ≫ f) ≫ m = (r.left ≫ f) ≫ _,
rw [assoc, assoc],
congr' 1,
erw (cofork.is_colimit.desc' r.is_colimit s.π _).2,
apply w walking_parallel_pair.one,
erw (cofork.is_colimit.desc' r.is_colimit s.π _).2,
apply w walking_parallel_pair.one }
end
end is_kernel_pair
end category_theory
|
17c23ba659d641d2784c682300a55a2639bc4146 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/field_theory/separable_degree.lean | 4b99c07615bf50f6c63d2c1f4608e84aff22de99 | [
"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 | 6,379 | lean | /-
Copyright (c) 2021 Jakob Scholbach. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jakob Scholbach
-/
import algebra.algebra.basic
import algebra.char_p.exp_char
import field_theory.separable
/-!
# Separable degree
This file contains basics about the separable degree of a polynomial.
## Main results
- `is_separable_contraction`: is the condition that `g(x^(q^m)) = f(x)` for some `m : ℕ`
- `has_separable_contraction`: the condition of having a separable contraction
- `has_separable_contraction.degree`: the separable degree, defined as the degree of some
separable contraction
- `irreducible_has_separable_contraction`: any irreducible polynomial can be contracted
to a separable polynomial
- `has_separable_contraction.dvd_degree'`: the degree of a separable contraction divides the degree,
in function of the exponential characteristic of the field
- `has_separable_contraction.dvd_degree` and `has_separable_contraction.eq_degree` specialize the
statement of `separable_degree_dvd_degree`
- `is_separable_contraction.degree_eq`: the separable degree is well-defined, implemented as the
statement that the degree of any separable contraction equals `has_separable_contraction.degree`
## Tags
separable degree, degree, polynomial
-/
namespace polynomial
noncomputable theory
open_locale classical
section comm_semiring
variables {F : Type} [comm_semiring F] (q : ℕ)
/-- A separable contraction of a polynomial `f` is a separable polynomial `g` such that
`g(x^(q^m)) = f(x)` for some `m : ℕ`.-/
def is_separable_contraction (f : polynomial F) (g : polynomial F) : Prop :=
g.separable ∧ ∃ m : ℕ, expand F (q^m) g = f
/-- The condition of having a separable contration. -/
def has_separable_contraction (f : polynomial F) : Prop :=
∃ g : polynomial F, is_separable_contraction q f g
variables {q} {f : polynomial F} (hf : has_separable_contraction q f)
/-- A choice of a separable contraction. -/
def has_separable_contraction.contraction : polynomial F := classical.some hf
/-- The separable degree of a polynomial is the degree of a given separable contraction. -/
def has_separable_contraction.degree : ℕ := hf.contraction.nat_degree
/-- The separable degree divides the degree, in function of the exponential characteristic of F. -/
lemma is_separable_contraction.dvd_degree' {g} (hf : is_separable_contraction q f g) :
∃ m : ℕ, g.nat_degree * (q ^ m) = f.nat_degree :=
begin
obtain ⟨m, rfl⟩ := hf.2,
use m,
rw nat_degree_expand,
end
lemma has_separable_contraction.dvd_degree' : ∃ m : ℕ, hf.degree * (q ^ m) = f.nat_degree :=
(classical.some_spec hf).dvd_degree'
/-- The separable degree divides the degree. -/
lemma has_separable_contraction.dvd_degree :
hf.degree ∣ f.nat_degree :=
let ⟨a, ha⟩ := hf.dvd_degree' in dvd.intro (q ^ a) ha
/-- In exponential characteristic one, the separable degree equals the degree. -/
lemma has_separable_contraction.eq_degree {f : polynomial F}
(hf : has_separable_contraction 1 f) : hf.degree = f.nat_degree :=
let ⟨a, ha⟩ := hf.dvd_degree' in by rw [←ha, one_pow a, mul_one]
end comm_semiring
section field
variables {F : Type} [field F]
variables (q : ℕ) {f : polynomial F} (hf : has_separable_contraction q f)
/-- Every irreducible polynomial can be contracted to a separable polynomial.
https://stacks.math.columbia.edu/tag/09H0 -/
lemma irreducible_has_separable_contraction (q : ℕ) [hF : exp_char F q]
(f : polynomial F) [irred : irreducible f] : has_separable_contraction q f :=
begin
casesI hF,
{ exact ⟨f, irred.separable, ⟨0, by rw [pow_zero, expand_one]⟩⟩ },
{ rcases exists_separable_of_irreducible q irred ‹q.prime›.ne_zero with ⟨n, g, hgs, hge⟩,
exact ⟨g, hgs, n, hge⟩, }
end
/-- A helper lemma: if two expansions (along the positive characteristic) of two polynomials `g` and
`g'` agree, and the one with the larger degree is separable, then their degrees are the same. -/
lemma contraction_degree_eq_aux [hq : fact q.prime] [hF : char_p F q]
(g g' : polynomial F) (m m' : ℕ)
(h_expand : expand F (q^m) g = expand F (q^m') g')
(h : m < m') (hg : g.separable):
g.nat_degree = g'.nat_degree :=
begin
obtain ⟨s, rfl⟩ := nat.exists_eq_add_of_lt h,
rw [add_assoc, pow_add, expand_mul] at h_expand,
let aux := expand_injective (pow_pos hq.1.pos m) h_expand,
rw aux at hg,
have := (is_unit_or_eq_zero_of_separable_expand q (s + 1) hq.out.pos hg).resolve_right
s.succ_ne_zero,
rw [aux, nat_degree_expand,
nat_degree_eq_of_degree_eq_some (degree_eq_zero_of_is_unit this),
zero_mul]
end
/-- If two expansions (along the positive characteristic) of two separable polynomials
`g` and `g'` agree, then they have the same degree. -/
theorem contraction_degree_eq_or_insep
[hq : fact q.prime] [char_p F q]
(g g' : polynomial F) (m m' : ℕ)
(h_expand : expand F (q^m) g = expand F (q^m') g')
(hg : g.separable) (hg' : g'.separable) :
g.nat_degree = g'.nat_degree :=
begin
by_cases h : m = m',
{ -- if `m = m'` then we show `g.nat_degree = g'.nat_degree` by unfolding the definitions
rw h at h_expand,
have expand_deg : ((expand F (q ^ m')) g).nat_degree =
(expand F (q ^ m') g').nat_degree, by rw h_expand,
rw [nat_degree_expand (q^m') g, nat_degree_expand (q^m') g'] at expand_deg,
apply nat.eq_of_mul_eq_mul_left (pow_pos hq.1.pos m'),
rw [mul_comm] at expand_deg, rw expand_deg, rw [mul_comm] },
{ cases ne.lt_or_lt h,
{ exact contraction_degree_eq_aux q g g' m m' h_expand h_1 hg },
{ exact (contraction_degree_eq_aux q g' g m' m h_expand.symm h_1 hg').symm, } }
end
/-- The separable degree equals the degree of any separable contraction, i.e., it is unique. -/
theorem is_separable_contraction.degree_eq [hF : exp_char F q]
(g : polynomial F) (hg : is_separable_contraction q f g) :
g.nat_degree = hf.degree :=
begin
casesI hF,
{ rcases hg with ⟨g, m, hm⟩,
rw [one_pow, expand_one] at hm,
rw hf.eq_degree,
rw hm, },
{ rcases hg with ⟨hg, m, hm⟩,
let g' := classical.some hf,
cases (classical.some_spec hf).2 with m' hm',
haveI : fact q.prime := fact_iff.2 hF_hprime,
apply contraction_degree_eq_or_insep q g g' m m',
rw [hm, hm'],
exact hg, exact (classical.some_spec hf).1 }
end
end field
end polynomial
|
b6835593cb2fc72010ed9e5e0c1546136152f8bb | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/meta/converter/interactive_auto.lean | 862a4004d9999cd953883fa828cf3886cabe631a | [] | 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 | 373 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Converter monad for building simplifiers.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.meta.interactive
import Mathlib.Lean3Lib.init.meta.converter.conv
namespace Mathlib
namespace conv
end Mathlib |
25a00882b35273e0d9d9e7f9c700e34b646c9b5d | 43390109ab88557e6090f3245c47479c123ee500 | /src/M1P2/sheet_7.lean | adb7908b67ec42b23c3b80a6c5bb97f234d71740 | [
"Apache-2.0"
] | permissive | Ja1941/xena-UROP-2018 | 41f0956519f94d56b8bf6834a8d39473f4923200 | b111fb87f343cf79eca3b886f99ee15c1dd9884b | refs/heads/master | 1,662,355,955,139 | 1,590,577,325,000 | 1,590,577,325,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,953 | lean | import tactic.norm_num chris_hughes_various.zmod data.nat.prime data.nat.basic data.int.modeq algebra.group_power group_theory.subgroup algebra.group data.set.basic group_theory.order_of_element
open nat
universes u v w x
variables m p : ℕ
variables {G : Type u} {H : Type v}
variables [group G] [group H] [group (zmod 11)] [add_group (zmod m)] [group (zmod p)]
definition is_cyclic (G : Type*) [group G] := ∃ x : G, gpowers x = set.univ
-- *1. Suppose that G is a finite group which contains elements of each of the orders 1, 2, . . . , 10. What is the smallest possible value of |G|? Find a group of this size which does have elements of each of these orders.
--theorem sheet07_q1 (G : Type*) (g : group) : := sorry
-- 2. What is the largest order of an element of S₈?
-- Ans: 15
-- theorem sheet07_q2:
-- 3. Let G be a cyclic group of order n, and g a generator. Show that gk is a generator for G if and only if hcf(k, n) = 1.
-- theorem sheet07_q3:
-- 4. Let G and H be finite groups. Let G×H be the set {(g,h)|g∈G,h∈H} with the binary operation (g1, h1) ∗ (g2, h2) = (g1g2, h1h2).
definition Cart_prod := prod G H
--definition bin_op (G : Type*) (H : Type*) (g₁ g₂ : prod G H) := prod (g₁.1 * g₂.1) (g1.2 * g2.2)
-- (a) Show that (G×H,∗) is a group.
-- theorem sheet07_q4a:
-- (b) Show that if g ∈ G and h ∈ H have orders a, b respectively, then the order of (g,h) in G×H is the lowest common multiple of a and b.
-- theorem sheet07_q4b:
-- (c) Show that G × H is cyclic if and only if G and H are both cyclic, and hcf(|G|,|H|) = 1.
-- theorem sheet07_q4c:
-- 5. Say whether each of the following statements is true or false, giving a counterexample or a brief proof.
-- all these are true lmao
-- (a) For any positive integer m, the group (ℤ_m,+) is cyclic.
--theorem sheet07_q5a (m : ℕ) : is_cyclic (zmod m) → true := sorry
-- (b) ℤ_11 is a cyclic group.
theorem sheet07_q5b : is_cyclic (zmod 11) → true := sorry
-- (c) If p is an odd prime, then ℤ_p has exactly one element of order 2.
--theorem sheet07_q5c (p : ℕ) (hp : prime p) (x : zmod p) : ∃! x ∈ zmod p := sorry
-- (d) If p is a prime number with p ≡ 4 mod 5,then the inverse of [5] in ℤ_p is p+1.
theorem sheet07_q5d (p x : ℕ) (hp : prime p) (hq : p ≡ 4 [MOD 5]) : 5*x ≡ 1 [MOD p] → x = p+1 := sorry
theorem flittle_thm (n p : ℕ) (hp : prime p) : n ^ (p-1) ≡ 1 [MOD p] := sorry
-- 6. (a) Find the remainder when 5^110 is divided by 13.
-- Ans: 12.
instance (n : ℕ) : pos_nat (succ n) := ⟨succ_pos _ ⟩
theorem sheet07_q6a: 5^110 ≡ 12 [MOD 13] := sorry
--begin
--rw ← @zmod.cast_val_of_lt 5 13 dec_trivial,
--rw ← @zmod.cast_val_of_lt 12 13 dec_trivial,
--end
-- (b) Find the inverses of [2] and of [120] in ℤ_9871. (The number 9871 is prime.)
-- Ans: 4936, 7321 respectively
theorem sheet07_q6bi (x : ℕ) : 2*x ≡ 1 [MOD 9871] → x = 4936 := sorry
theorem sheet07_q6bii (x : ℕ) : 120*x ≡ 1 [MOD 9871] → x = 7321 := sorry
-- (c) Use Fermat’s Little Theorem to show that n^17 ≡ n mod 255 for all n ∈ ℤ.
theorem sheet07_q6c (n : ℕ) : n^17 ≡ n [MOD 255] := sorry
-- (d) Prove that if p and q are distinct prime numbers then p^(q-1) + q^(p−1) ≡ 1 mod pq.
theorem sheet07_q6d (p q : ℕ) (hp: prime p) (hq: prime q) : p^(q-1) + q^(p-1) ≡ 1 [MOD p*q] := sorry
-- 7. Let p be an odd prime.
-- (a) Prove that (p − 1)! ≡ −1 mod p (Wilson’s Theorem).
theorem sheet07_q7a (p : ℕ) (hp : prime p) : fact (p-1) ≡ -1 [ZMOD p] := sorry
-- (b) Show that if p ≡ 1 mod 4,then there is x ∈ Z with x^2 ≡ −1 (mod p).
theorem sheet07_q7b (p : ℕ) (hp : prime p) (x : ℤ): p ≡ 1 [ZMOD 4] → x^2 ≡ -1 [ZMOD p] := sorry
-- (c) Show that if p ≠ 2 and there is x∈Z with x^2 ≡−1 modp,then p ≡ 1 mod 4.
theorem sheet07_q7c (p : ℕ) (hp : prime p) (x : ℤ) : p ≠ 2 ∧ x^2 ≡ -1 [ZMOD p] → p ≡ 1 [ZMOD 4] := sorry
|
356450a4aa0577bc0bd7cd81b1d3b1040153129b | 00d2363f9655e2a7618f6b94dda7e2c4e5cf8d19 | /lean_modifications/print_tactic_state_sexp.lean | 113b95c5f38ebdc3cd92136360ca31d08e6311e9 | [
"Apache-2.0"
] | permissive | devjuice1/lean_proof_recording | 927e276e2ab8fb1288f51d9146dcfbf0d6444a87 | bf7c527315deccd35363fa7ca89d97d7b9cb6ac1 | refs/heads/master | 1,692,914,925,585 | 1,633,018,872,000 | 1,633,018,872,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,124 | lean | /- This is a staging area for code which will be inserted
into a Lean file.
The code to be inserted is between the line comments
`PR BEGIN MODIFICATION` and `PR END MODIFICATION`
It will be inserted by `insert_proof_recording_code.py`.
Insert info:
- file: `_target/lean/library/init/meta/tactic.lean`
- location: end of file
Most of this code is carefully written, but
any code labeled "BEGIN/END CUSTOMIZABLE CODE"
encourages customization to change what
is being recorded
-/
prelude
import init.meta.tactic
universes u v
--PR BEGIN MODIFICATION
-- sexp modification
local notation `SEXP_DEPTH_LIMIT` := some (50 : ℕ)
section sexp
inductive sexp (α : Type u) : Type u
| atom : α → sexp
| list : (list sexp) → sexp
meta def sexp.to_format {α : Type u} [has_to_format α] : sexp α → format
| (sexp.atom val) := has_to_format.to_format val
| (sexp.list ses) := format.of_string $ "(" ++ (" ".intercalate (ses.map $ format.to_string ∘ sexp.to_format)) ++ ")"
meta instance sexp_has_to_format {α} [has_to_format α] : has_to_format (sexp α) :=
⟨sexp.to_format⟩
end sexp
section open_binders
open expr
open tactic
@[simp]
private def option.elim {α β} : option α → β → (α → β) → β
| (some x) y f := f x
| none y f := y
private meta def match_lam {elab} : expr elab →
option (name × binder_info × expr elab × expr elab)
| (lam var_name bi type body) := some (var_name, bi, type, body)
| _ := none
private meta def match_pi {elab} : expr elab →
option (name × binder_info × expr elab × expr elab)
| (pi var_name bi type body) := some (var_name, bi, type, body)
| _ := none
private meta structure binder :=
(name : name)
(info : binder_info)
(type : expr)
private meta def mk_binder_replacement (local_or_meta : bool) (b : binder) :
tactic expr :=
if local_or_meta then mk_local' b.name b.info b.type else mk_meta_var b.type
@[inline] meta def get_binder (do_whnf : option (transparency × bool))
(pi_or_lambda : bool) (e : expr) :
tactic (option (name × binder_info × expr × expr)) := do
e ← option.elim do_whnf (pure e) (λ p, whnf e p.1 p.2),
pure $ if pi_or_lambda then match_pi e else match_lam e
private meta def open_n_binders (do_whnf : option (transparency × bool))
(pis_or_lambdas : bool) (locals_or_metas : bool) :
expr → ℕ → tactic (list expr × expr)
| e 0 := pure ([], e)
| e (d + 1) := do
some (name, bi, type, body) ← get_binder do_whnf pis_or_lambdas e | failed,
replacement ← mk_binder_replacement locals_or_metas ⟨name, bi, type⟩,
(rs, rest) ← open_n_binders (body.instantiate_var replacement) d,
pure (replacement :: rs, rest)
private meta def open_n_pis : expr → ℕ → tactic (list expr × expr) :=
open_n_binders none tt tt
private meta def open_n_lambdas : expr → ℕ → tactic (list expr × expr) :=
open_n_binders none ff tt
section sexp_of_expr
open expr
/-- head-reduce a single let expression -/
private meta def tmp_reduce_let : expr → expr
| (elet _ _ v b) := b.instantiate_var v
| e := e
meta def sexp.concat {m} [monad m] [monad_fail m] {α} : (sexp α) → (sexp α) → m (sexp α)
| (sexp.list xs) (sexp.list ys) := pure (sexp.list $ xs ++ ys)
| _ _ := monad_fail.fail "sexp.concat failed"
local infix `<+>`:50 := sexp.concat -- TODO(jesse): just write an applicative instance, don't want to think about `seq` now though
meta def sexp.map {α β : Type*} (f : α → β) : sexp α → sexp β
| (sexp.atom x) := (sexp.atom $ f x)
| (sexp.list xs) := (sexp.list $ list.map sexp.map xs)
meta instance : functor sexp :=
{map := λ α β f, sexp.map f}
def mk_type_ascription : sexp string → sexp string → sexp string := λ s₁ s₂, sexp.list [(sexp.atom ":"), s₁, s₂]
-- TODO(jesse): supply version with even more type annotations
meta def sexp_of_expr : (option ℕ) → expr → tactic (sexp string) := λ fuel ex, do {
match fuel with
| none := pure ()
| (some x) := when (x = 0) $ tactic.fail "sexp_of_expr fuel exhausted"
end,
match ex with
| e@(var k) := (sexp.list [sexp.atom "var"]) <+> (sexp.list [sexp.atom (repr k)])
| e@(sort l) := (sexp.list [sexp.atom "sort"]) <+> (sexp.list [sexp.atom (to_string l)])
| e@(const nm ls) := pure $ sexp.atom nm.to_string
| e@(mvar un pt tp) := do tp_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) tp, pure $ mk_type_ascription (sexp.atom pt.to_string) tp_sexp
| e@(local_const un pt binder_info tp) := do {
tp_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) tp,
-- pure $ flip mk_type_ascription tp_sexp $ sexp.list [sexp.atom pt.to_string] -- note: drop binder info for now
pure (sexp.atom pt.to_string)
}
-- version without "APP" head symbol
| e@(app e₁ e₂) := (λ s₁ s₂, sexp.list [s₁, s₂]) <$> sexp_of_expr ((flip nat.sub 1) <$> fuel) e₁ <*> sexp_of_expr ((flip nat.sub 1) <$> fuel) e₂
-- version with "APP" head symbol -- switch if needed
-- | e@(app e₁ e₂) := (λ s₁ s₂, sexp.list [sexp.atom "APP", s₁, s₂]) <$> sexp_of_expr ((flip nat.sub 1) <$> fuel) e₁ <*> sexp_of_expr ((flip nat.sub 1) <$> fuel) e₂
-- | e@(app e₁ e₂) := sexp.list <$> ((::) <$> (sexp_of_expr ((flip nat.sub 1) <$> fuel) $ get_app_fn e) <*> (get_app_args e).mmap (sexp_of_expr ((flip nat.sub 1) <$> fuel)))
| e@(lam var_name b_info var_type body) := do {
⟨[b], e'⟩ ← open_n_lambdas e 1,
sexp.list <$> do {
b_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) b,
b_tp_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) var_type,
body_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) e',
pure $ [sexp.atom "LAMBDA", mk_type_ascription (b_sexp) b_tp_sexp, body_sexp]
}
}
| e@(pi var_name b_info var_type body) := do {
⟨[b], e'⟩ ← open_n_pis e 1,
sexp.list <$> do {
b_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) b,
b_tp_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) var_type,
body_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) e',
pure $ [sexp.atom "PI", mk_type_ascription (b_sexp) b_tp_sexp, body_sexp]
}
}
-- reduce let expressions before sexpr serialization
| e@(elet var_name var_type var_assignment body) := sexp_of_expr ((flip nat.sub 1) <$> fuel) (tmp_reduce_let e)
| e@(macro md deps) :=
sexp.list <$> do {
deps_sexp_list ← sexp.list <$> (deps.mmap $ sexp_of_expr (((flip nat.sub 1) <$> fuel))),
let deps_sexp := sexp.list [sexp.atom "MACRO_DEPS", deps_sexp_list],
pure $ [sexp.atom "MACRO", sexp.atom (expr.macro_def_name md).to_string, deps_sexp]
}
end
}
end sexp_of_expr
end open_binders
section
private meta def set_bool_option (n : name) (v : bool) : tactic unit :=
do s ← tactic.read,
tactic.write $ tactic_state.set_options s (options.set_bool (tactic_state.get_options s) n v)
private meta def enable_full_names : tactic unit := do {
set_bool_option `pp.full_names true
}
private meta def with_full_names {α} (tac : tactic α) : tactic α :=
tactic.save_options $ enable_full_names *> tac
open sexp tactic
meta def tactic_state.to_sexp (ts : tactic_state) : tactic (sexp string) := do
λ _, interaction_monad.result.success () ts,
-- do { gs ← tactic.get_goals, guard (gs.length = 1) <|>
-- tactic.trace ("[tactic_state.to_sexp] WARNING: NUM GOALS" ++ gs.length.repr) },
with_full_names $ do {
hyps ← tactic.local_context,
annotated_hyps ← hyps.mmap (λ h, prod.mk h <$> tactic.infer_type h),
hyps_sexp ← do {
hyps_sexps ← annotated_hyps.mmap $
function.uncurry $ λ hc ht, mk_type_ascription <$>
sexp_of_expr SEXP_DEPTH_LIMIT hc <*> sexp_of_expr none ht,
pure $ sexp.list $ [sexp.atom "HYPS"] ++ hyps_sexps
},
goal_sexp ← (λ x, sexp.list [sexp.atom "GOAL", x]) <$> (tactic.target >>= sexp_of_expr SEXP_DEPTH_LIMIT),
pure $ sexp.list [sexp.atom "GOAL_STATE", hyps_sexp, goal_sexp]
}
end
local notation `PRINT_TACTIC_STATE` := λ ts, (format.to_string ∘ format.flatten ∘ sexp.to_format) <$> tactic_state.to_sexp ts
-- end sexp modifications
--PR END MODIFICATION
|
f69b65c05d3d0d07bf4124a9a91a4b44ed70bf03 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/topology/maps.lean | 1f6641fd51246cc029252be3a338bc45d8420434 | [
"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 | 25,617 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot
-/
import topology.order
import topology.nhds_set
/-!
# Specific classes of maps between topological spaces
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file introduces the following properties of a map `f : X → Y` between topological spaces:
* `is_open_map f` means the image of an open set under `f` is open.
* `is_closed_map f` means the image of a closed set under `f` is closed.
(Open and closed maps need not be continuous.)
* `inducing f` means the topology on `X` is the one induced via `f` from the topology on `Y`.
These behave like embeddings except they need not be injective. Instead, points of `X` which
are identified by `f` are also inseparable in the topology on `X`.
* `embedding f` means `f` is inducing and also injective. Equivalently, `f` identifies `X` with
a subspace of `Y`.
* `open_embedding f` means `f` is an embedding with open image, so it identifies `X` with an
open subspace of `Y`. Equivalently, `f` is an embedding and an open map.
* `closed_embedding f` similarly means `f` is an embedding with closed image, so it identifies
`X` with a closed subspace of `Y`. Equivalently, `f` is an embedding and a closed map.
* `quotient_map f` is the dual condition to `embedding f`: `f` is surjective and the topology
on `Y` is the one coinduced via `f` from the topology on `X`. Equivalently, `f` identifies
`Y` with a quotient of `X`. Quotient maps are also sometimes known as identification maps.
## References
* <https://en.wikipedia.org/wiki/Open_and_closed_maps>
* <https://en.wikipedia.org/wiki/Embedding#General_topology>
* <https://en.wikipedia.org/wiki/Quotient_space_(topology)#Quotient_map>
## Tags
open map, closed map, embedding, quotient map, identification map
-/
open set filter function
open_locale topology filter
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
section inducing
/-- A function `f : α → β` between topological spaces is inducing if the topology on `α` is induced
by the topology on `β` through `f`, meaning that a set `s : set α` is open iff it is the preimage
under `f` of some open set `t : set β`. -/
@[mk_iff]
structure inducing [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop :=
(induced : tα = tβ.induced f)
variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
lemma inducing_id : inducing (@id α) :=
⟨induced_id.symm⟩
protected lemma inducing.comp {g : β → γ} {f : α → β} (hg : inducing g) (hf : inducing f) :
inducing (g ∘ f) :=
⟨by rw [hf.induced, hg.induced, induced_compose]⟩
lemma inducing_of_inducing_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g)
(hgf : inducing (g ∘ f)) : inducing f :=
⟨le_antisymm
(by rwa ← continuous_iff_le_induced)
(by { rw [hgf.induced, ← continuous_iff_le_induced], apply hg.comp continuous_induced_dom })⟩
lemma inducing_iff_nhds {f : α → β} : inducing f ↔ ∀ a, 𝓝 a = comap f (𝓝 (f a)) :=
(inducing_iff _).trans (induced_iff_nhds_eq f)
lemma inducing.nhds_eq_comap {f : α → β} (hf : inducing f) :
∀ (a : α), 𝓝 a = comap f (𝓝 $ f a) :=
inducing_iff_nhds.1 hf
lemma inducing.nhds_set_eq_comap {f : α → β} (hf : inducing f) (s : set α) :
𝓝ˢ s = comap f (𝓝ˢ (f '' s)) :=
by simp only [nhds_set, Sup_image, comap_supr, hf.nhds_eq_comap, supr_image]
lemma inducing.map_nhds_eq {f : α → β} (hf : inducing f) (a : α) :
(𝓝 a).map f = 𝓝[range f] (f a) :=
hf.induced.symm ▸ map_nhds_induced_eq a
lemma inducing.map_nhds_of_mem {f : α → β} (hf : inducing f) (a : α) (h : range f ∈ 𝓝 (f a)) :
(𝓝 a).map f = 𝓝 (f a) :=
hf.induced.symm ▸ map_nhds_induced_of_mem h
lemma inducing.image_mem_nhds_within {f : α → β} (hf : inducing f) {a : α} {s : set α}
(hs : s ∈ 𝓝 a) : f '' s ∈ 𝓝[range f] (f a) :=
hf.map_nhds_eq a ▸ image_mem_map hs
lemma inducing.tendsto_nhds_iff {ι : Type*}
{f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : inducing g) :
tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) :=
by rw [hg.nhds_eq_comap, tendsto_comap_iff]
lemma inducing.continuous_at_iff {f : α → β} {g : β → γ} (hg : inducing g) {x : α} :
continuous_at f x ↔ continuous_at (g ∘ f) x :=
by simp_rw [continuous_at, inducing.tendsto_nhds_iff hg]
lemma inducing.continuous_iff {f : α → β} {g : β → γ} (hg : inducing g) :
continuous f ↔ continuous (g ∘ f) :=
by simp_rw [continuous_iff_continuous_at, hg.continuous_at_iff]
lemma inducing.continuous_at_iff' {f : α → β} {g : β → γ} (hf : inducing f) {x : α}
(h : range f ∈ 𝓝 (f x)) : continuous_at (g ∘ f) x ↔ continuous_at g (f x) :=
by { simp_rw [continuous_at, filter.tendsto, ← hf.map_nhds_of_mem _ h, filter.map_map] }
protected lemma inducing.continuous {f : α → β} (hf : inducing f) : continuous f :=
hf.continuous_iff.mp continuous_id
protected lemma inducing.inducing_iff {f : α → β} {g : β → γ} (hg : inducing g) :
inducing f ↔ inducing (g ∘ f) :=
begin
refine ⟨λ h, hg.comp h, λ hgf, inducing_of_inducing_compose _ hg.continuous hgf⟩,
rw hg.continuous_iff,
exact hgf.continuous
end
lemma inducing.closure_eq_preimage_closure_image {f : α → β} (hf : inducing f) (s : set α) :
closure s = f ⁻¹' closure (f '' s) :=
by { ext x, rw [set.mem_preimage, ← closure_induced, hf.induced] }
lemma inducing.is_closed_iff {f : α → β} (hf : inducing f) {s : set α} :
is_closed s ↔ ∃ t, is_closed t ∧ f ⁻¹' t = s :=
by rw [hf.induced, is_closed_induced_iff]
lemma inducing.is_closed_iff' {f : α → β} (hf : inducing f) {s : set α} :
is_closed s ↔ ∀ x, f x ∈ closure (f '' s) → x ∈ s :=
by rw [hf.induced, is_closed_induced_iff']
lemma inducing.is_closed_preimage {f : α → β} (h : inducing f) (s : set β) (hs : is_closed s) :
is_closed (f ⁻¹' s) :=
(inducing.is_closed_iff h).mpr ⟨s, hs, rfl⟩
lemma inducing.is_open_iff {f : α → β} (hf : inducing f) {s : set α} :
is_open s ↔ ∃ t, is_open t ∧ f ⁻¹' t = s :=
by rw [hf.induced, is_open_induced_iff]
lemma inducing.dense_iff {f : α → β} (hf : inducing f) {s : set α} :
dense s ↔ ∀ x, f x ∈ closure (f '' s) :=
by simp only [dense, hf.closure_eq_preimage_closure_image, mem_preimage]
end inducing
section embedding
/-- A function between topological spaces is an embedding if it is injective,
and for all `s : set α`, `s` is open iff it is the preimage of an open set. -/
@[mk_iff] structure embedding [tα : topological_space α] [tβ : topological_space β] (f : α → β)
extends inducing f : Prop :=
(inj : injective f)
lemma function.injective.embedding_induced [t : topological_space β]
{f : α → β} (hf : injective f) :
@_root_.embedding α β (t.induced f) t f :=
{ induced := rfl,
inj := hf }
variables [topological_space α] [topological_space β] [topological_space γ]
lemma embedding.mk' (f : α → β) (inj : injective f)
(induced : ∀ a, comap f (𝓝 (f a)) = 𝓝 a) : embedding f :=
⟨inducing_iff_nhds.2 (λ a, (induced a).symm), inj⟩
lemma embedding_id : embedding (@id α) :=
⟨inducing_id, assume a₁ a₂ h, h⟩
lemma embedding.comp {g : β → γ} {f : α → β} (hg : embedding g) (hf : embedding f) :
embedding (g ∘ f) :=
{ inj:= assume a₁ a₂ h, hf.inj $ hg.inj h,
..hg.to_inducing.comp hf.to_inducing }
lemma embedding_of_embedding_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g)
(hgf : embedding (g ∘ f)) : embedding f :=
{ induced := (inducing_of_inducing_compose hf hg hgf.to_inducing).induced,
inj := assume a₁ a₂ h, hgf.inj $ by simp [h, (∘)] }
protected lemma function.left_inverse.embedding {f : α → β} {g : β → α}
(h : left_inverse f g) (hf : continuous f) (hg : continuous g) :
embedding g :=
embedding_of_embedding_compose hg hf $ h.comp_eq_id.symm ▸ embedding_id
lemma embedding.map_nhds_eq {f : α → β} (hf : embedding f) (a : α) :
(𝓝 a).map f = 𝓝[range f] (f a) :=
hf.1.map_nhds_eq a
lemma embedding.map_nhds_of_mem {f : α → β}
(hf : embedding f) (a : α) (h : range f ∈ 𝓝 (f a)) : (𝓝 a).map f = 𝓝 (f a) :=
hf.1.map_nhds_of_mem a h
lemma embedding.tendsto_nhds_iff {ι : Type*}
{f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : embedding g) :
tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) :=
hg.to_inducing.tendsto_nhds_iff
lemma embedding.continuous_iff {f : α → β} {g : β → γ} (hg : embedding g) :
continuous f ↔ continuous (g ∘ f) :=
inducing.continuous_iff hg.1
lemma embedding.continuous {f : α → β} (hf : embedding f) : continuous f :=
inducing.continuous hf.1
lemma embedding.closure_eq_preimage_closure_image {e : α → β} (he : embedding e) (s : set α) :
closure s = e ⁻¹' closure (e '' s) :=
he.1.closure_eq_preimage_closure_image s
/-- The topology induced under an inclusion `f : X → Y` from the discrete topological space `Y`
is the discrete topology on `X`. -/
lemma embedding.discrete_topology {X Y : Type*} [topological_space X] [tY : topological_space Y]
[discrete_topology Y] {f : X → Y} (hf : embedding f) : discrete_topology X :=
discrete_topology_iff_nhds.2 $ λ x, by rw [hf.nhds_eq_comap, nhds_discrete, comap_pure,
← image_singleton, hf.inj.preimage_image, principal_singleton]
end embedding
/-- A function between topological spaces is a quotient map if it is surjective,
and for all `s : set β`, `s` is open iff its preimage is an open set. -/
def quotient_map {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β]
(f : α → β) : Prop :=
surjective f ∧ tβ = tα.coinduced f
lemma quotient_map_iff {α β : Type*} [topological_space α] [topological_space β] {f : α → β} :
quotient_map f ↔ surjective f ∧ ∀ s : set β, is_open s ↔ is_open (f ⁻¹' s) :=
and_congr iff.rfl topological_space_eq_iff
namespace quotient_map
variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
{g : β → γ} {f : α → β}
protected lemma id : quotient_map (@id α) :=
⟨assume a, ⟨a, rfl⟩, coinduced_id.symm⟩
protected lemma comp (hg : quotient_map g) (hf : quotient_map f) :
quotient_map (g ∘ f) :=
⟨hg.left.comp hf.left, by rw [hg.right, hf.right, coinduced_compose]⟩
protected lemma of_quotient_map_compose (hf : continuous f) (hg : continuous g)
(hgf : quotient_map (g ∘ f)) : quotient_map g :=
⟨hgf.1.of_comp,
le_antisymm
(by { rw [hgf.right, ← continuous_iff_coinduced_le], apply continuous_coinduced_rng.comp hf })
(by rwa ← continuous_iff_coinduced_le)⟩
lemma of_inverse {g : β → α} (hf : continuous f) (hg : continuous g) (h : left_inverse g f) :
quotient_map g :=
quotient_map.of_quotient_map_compose hf hg $ h.comp_eq_id.symm ▸ quotient_map.id
protected lemma continuous_iff (hf : quotient_map f) :
continuous g ↔ continuous (g ∘ f) :=
by rw [continuous_iff_coinduced_le, continuous_iff_coinduced_le, hf.right, coinduced_compose]
protected lemma continuous (hf : quotient_map f) : continuous f :=
hf.continuous_iff.mp continuous_id
protected lemma surjective (hf : quotient_map f) : surjective f := hf.1
protected lemma is_open_preimage (hf : quotient_map f) {s : set β} :
is_open (f ⁻¹' s) ↔ is_open s :=
((quotient_map_iff.1 hf).2 s).symm
protected lemma is_closed_preimage (hf : quotient_map f) {s : set β} :
is_closed (f ⁻¹' s) ↔ is_closed s :=
by simp only [← is_open_compl_iff, ← preimage_compl, hf.is_open_preimage]
end quotient_map
/-- A map `f : α → β` is said to be an *open map*, if the image of any open `U : set α`
is open in `β`. -/
def is_open_map [topological_space α] [topological_space β] (f : α → β) :=
∀ U : set α, is_open U → is_open (f '' U)
namespace is_open_map
variables [topological_space α] [topological_space β] [topological_space γ] {f : α → β}
protected lemma id : is_open_map (@id α) := assume s hs, by rwa [image_id]
protected lemma comp
{g : β → γ} {f : α → β} (hg : is_open_map g) (hf : is_open_map f) : is_open_map (g ∘ f) :=
by intros s hs; rw [image_comp]; exact hg _ (hf _ hs)
lemma is_open_range (hf : is_open_map f) : is_open (range f) :=
by { rw ← image_univ, exact hf _ is_open_univ }
lemma image_mem_nhds (hf : is_open_map f) {x : α} {s : set α} (hx : s ∈ 𝓝 x) :
f '' s ∈ 𝓝 (f x) :=
let ⟨t, hts, ht, hxt⟩ := mem_nhds_iff.1 hx in
mem_of_superset (is_open.mem_nhds (hf t ht) (mem_image_of_mem _ hxt)) (image_subset _ hts)
lemma range_mem_nhds (hf : is_open_map f) (x : α) : range f ∈ 𝓝 (f x) :=
hf.is_open_range.mem_nhds $ mem_range_self _
lemma maps_to_interior (hf : is_open_map f) {s : set α} {t : set β} (h : maps_to f s t) :
maps_to f (interior s) (interior t) :=
maps_to'.2 $ interior_maximal (h.mono interior_subset subset.rfl).image_subset
(hf _ is_open_interior)
lemma image_interior_subset (hf : is_open_map f) (s : set α) :
f '' interior s ⊆ interior (f '' s) :=
(hf.maps_to_interior (maps_to_image f s)).image_subset
lemma nhds_le (hf : is_open_map f) (a : α) : 𝓝 (f a) ≤ (𝓝 a).map f :=
le_map $ λ s, hf.image_mem_nhds
lemma of_nhds_le (hf : ∀ a, 𝓝 (f a) ≤ map f (𝓝 a)) : is_open_map f :=
λ s hs, is_open_iff_mem_nhds.2 $ λ b ⟨a, has, hab⟩,
hab ▸ hf _ (image_mem_map $ is_open.mem_nhds hs has)
lemma of_sections {f : α → β}
(h : ∀ x, ∃ g : β → α, continuous_at g (f x) ∧ g (f x) = x ∧ right_inverse g f) :
is_open_map f :=
of_nhds_le $ λ x, let ⟨g, hgc, hgx, hgf⟩ := h x in
calc 𝓝 (f x) = map f (map g (𝓝 (f x))) : by rw [map_map, hgf.comp_eq_id, map_id]
... ≤ map f (𝓝 (g (f x))) : map_mono hgc
... = map f (𝓝 x) : by rw hgx
lemma of_inverse {f : α → β} {f' : β → α}
(h : continuous f') (l_inv : left_inverse f f') (r_inv : right_inverse f f') :
is_open_map f :=
of_sections $ λ x, ⟨f', h.continuous_at, r_inv _, l_inv⟩
/-- A continuous surjective open map is a quotient map. -/
lemma to_quotient_map {f : α → β}
(open_map : is_open_map f) (cont : continuous f) (surj : surjective f) :
quotient_map f :=
quotient_map_iff.2 ⟨surj, λ s, ⟨λ h, h.preimage cont, λ h, surj.image_preimage s ▸ open_map _ h⟩⟩
lemma interior_preimage_subset_preimage_interior (hf : is_open_map f) {s : set β} :
interior (f⁻¹' s) ⊆ f⁻¹' (interior s) :=
hf.maps_to_interior (maps_to_preimage _ _)
lemma preimage_interior_eq_interior_preimage (hf₁ : is_open_map f) (hf₂ : continuous f)
(s : set β) :
f⁻¹' (interior s) = interior (f⁻¹' s) :=
subset.antisymm
(preimage_interior_subset_interior_preimage hf₂)
(interior_preimage_subset_preimage_interior hf₁)
lemma preimage_closure_subset_closure_preimage (hf : is_open_map f) {s : set β} :
f ⁻¹' (closure s) ⊆ closure (f ⁻¹' s) :=
begin
rw ← compl_subset_compl,
simp only [← interior_compl, ← preimage_compl, hf.interior_preimage_subset_preimage_interior]
end
lemma preimage_closure_eq_closure_preimage (hf : is_open_map f) (hfc : continuous f) (s : set β) :
f ⁻¹' (closure s) = closure (f ⁻¹' s) :=
hf.preimage_closure_subset_closure_preimage.antisymm (hfc.closure_preimage_subset s)
lemma preimage_frontier_subset_frontier_preimage (hf : is_open_map f) {s : set β} :
f ⁻¹' (frontier s) ⊆ frontier (f ⁻¹' s) :=
by simpa only [frontier_eq_closure_inter_closure, preimage_inter]
using inter_subset_inter hf.preimage_closure_subset_closure_preimage
hf.preimage_closure_subset_closure_preimage
lemma preimage_frontier_eq_frontier_preimage (hf : is_open_map f) (hfc : continuous f) (s : set β) :
f ⁻¹' (frontier s) = frontier (f ⁻¹' s) :=
by simp only [frontier_eq_closure_inter_closure, preimage_inter, preimage_compl,
hf.preimage_closure_eq_closure_preimage hfc]
end is_open_map
lemma is_open_map_iff_nhds_le [topological_space α] [topological_space β] {f : α → β} :
is_open_map f ↔ ∀(a:α), 𝓝 (f a) ≤ (𝓝 a).map f :=
⟨λ hf, hf.nhds_le, is_open_map.of_nhds_le⟩
lemma is_open_map_iff_interior [topological_space α] [topological_space β] {f : α → β} :
is_open_map f ↔ ∀ s, f '' (interior s) ⊆ interior (f '' s) :=
⟨is_open_map.image_interior_subset, λ hs u hu, subset_interior_iff_is_open.mp $
calc f '' u = f '' (interior u) : by rw hu.interior_eq
... ⊆ interior (f '' u) : hs u⟩
/-- An inducing map with an open range is an open map. -/
protected lemma inducing.is_open_map [topological_space α] [topological_space β] {f : α → β}
(hi : inducing f) (ho : is_open (range f)) :
is_open_map f :=
is_open_map.of_nhds_le $ λ x, (hi.map_nhds_of_mem _ $ is_open.mem_nhds ho $ mem_range_self _).ge
section is_closed_map
variables [topological_space α] [topological_space β]
/-- A map `f : α → β` is said to be a *closed map*, if the image of any closed `U : set α`
is closed in `β`. -/
def is_closed_map (f : α → β) := ∀ U : set α, is_closed U → is_closed (f '' U)
end is_closed_map
namespace is_closed_map
variables [topological_space α] [topological_space β] [topological_space γ]
open function
protected lemma id : is_closed_map (@id α) := assume s hs, by rwa image_id
protected lemma comp {g : β → γ} {f : α → β} (hg : is_closed_map g) (hf : is_closed_map f) :
is_closed_map (g ∘ f) :=
by { intros s hs, rw image_comp, exact hg _ (hf _ hs) }
lemma closure_image_subset {f : α → β} (hf : is_closed_map f) (s : set α) :
closure (f '' s) ⊆ f '' closure s :=
closure_minimal (image_subset _ subset_closure) (hf _ is_closed_closure)
lemma of_inverse {f : α → β} {f' : β → α}
(h : continuous f') (l_inv : left_inverse f f') (r_inv : right_inverse f f') :
is_closed_map f :=
assume s hs,
have f' ⁻¹' s = f '' s, by ext x; simp [mem_image_iff_of_inverse r_inv l_inv],
this ▸ hs.preimage h
lemma of_nonempty {f : α → β} (h : ∀ s, is_closed s → s.nonempty → is_closed (f '' s)) :
is_closed_map f :=
begin
intros s hs, cases eq_empty_or_nonempty s with h2s h2s,
{ simp_rw [h2s, image_empty, is_closed_empty] },
{ exact h s hs h2s }
end
lemma closed_range {f : α → β} (hf : is_closed_map f) : is_closed (range f) :=
@image_univ _ _ f ▸ hf _ is_closed_univ
end is_closed_map
lemma inducing.is_closed_map [topological_space α] [topological_space β]
{f : α → β} (hf : inducing f) (h : is_closed (range f)) : is_closed_map f :=
begin
intros s hs,
rcases hf.is_closed_iff.1 hs with ⟨t, ht, rfl⟩,
rw image_preimage_eq_inter_range,
exact ht.inter h
end
lemma is_closed_map_iff_closure_image [topological_space α] [topological_space β] {f : α → β} :
is_closed_map f ↔ ∀ s, closure (f '' s) ⊆ f '' closure s :=
⟨is_closed_map.closure_image_subset, λ hs c hc, is_closed_of_closure_subset $
calc closure (f '' c) ⊆ f '' (closure c) : hs c
... = f '' c : by rw hc.closure_eq⟩
section open_embedding
variables [topological_space α] [topological_space β] [topological_space γ]
/-- An open embedding is an embedding with open image. -/
@[mk_iff]
structure open_embedding (f : α → β) extends _root_.embedding f : Prop :=
(open_range : is_open $ range f)
lemma open_embedding.is_open_map {f : α → β} (hf : open_embedding f) : is_open_map f :=
hf.to_embedding.to_inducing.is_open_map hf.open_range
lemma open_embedding.map_nhds_eq {f : α → β} (hf : open_embedding f) (a : α) :
map f (𝓝 a) = 𝓝 (f a) :=
hf.to_embedding.map_nhds_of_mem _ $ hf.open_range.mem_nhds $ mem_range_self _
lemma open_embedding.open_iff_image_open {f : α → β} (hf : open_embedding f)
{s : set α} : is_open s ↔ is_open (f '' s) :=
⟨hf.is_open_map s,
λ h, begin
convert ← h.preimage hf.to_embedding.continuous,
apply preimage_image_eq _ hf.inj
end⟩
lemma open_embedding.tendsto_nhds_iff {ι : Type*}
{f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : open_embedding g) :
tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) :=
hg.to_embedding.tendsto_nhds_iff
lemma open_embedding.continuous {f : α → β} (hf : open_embedding f) : continuous f :=
hf.to_embedding.continuous
lemma open_embedding.open_iff_preimage_open {f : α → β} (hf : open_embedding f)
{s : set β} (hs : s ⊆ range f) : is_open s ↔ is_open (f ⁻¹' s) :=
begin
convert ←hf.open_iff_image_open.symm,
rwa [image_preimage_eq_inter_range, inter_eq_self_of_subset_left]
end
lemma open_embedding_of_embedding_open {f : α → β} (h₁ : embedding f)
(h₂ : is_open_map f) : open_embedding f :=
⟨h₁, h₂.is_open_range⟩
lemma open_embedding_iff_embedding_open {f : α → β} :
open_embedding f ↔ embedding f ∧ is_open_map f :=
⟨λ h, ⟨h.1, h.is_open_map⟩, λ h, open_embedding_of_embedding_open h.1 h.2⟩
lemma open_embedding_of_continuous_injective_open {f : α → β} (h₁ : continuous f)
(h₂ : injective f) (h₃ : is_open_map f) : open_embedding f :=
begin
simp only [open_embedding_iff_embedding_open, embedding_iff, inducing_iff_nhds, *, and_true],
exact λ a, le_antisymm (h₁.tendsto _).le_comap
(@comap_map _ _ (𝓝 a) _ h₂ ▸ comap_mono (h₃.nhds_le _))
end
lemma open_embedding_iff_continuous_injective_open {f : α → β} :
open_embedding f ↔ continuous f ∧ injective f ∧ is_open_map f :=
⟨λ h, ⟨h.continuous, h.inj, h.is_open_map⟩,
λ h, open_embedding_of_continuous_injective_open h.1 h.2.1 h.2.2⟩
lemma open_embedding_id : open_embedding (@id α) :=
⟨embedding_id, is_open_map.id.is_open_range⟩
lemma open_embedding.comp {g : β → γ} {f : α → β}
(hg : open_embedding g) (hf : open_embedding f) : open_embedding (g ∘ f) :=
⟨hg.1.comp hf.1, (hg.is_open_map.comp hf.is_open_map).is_open_range⟩
lemma open_embedding.is_open_map_iff {g : β → γ} {f : α → β} (hg : open_embedding g) :
is_open_map f ↔ is_open_map (g ∘ f) :=
by simp only [is_open_map_iff_nhds_le, ← @map_map _ _ _ _ f g, ← hg.map_nhds_eq,
map_le_map_iff hg.inj]
lemma open_embedding.of_comp_iff (f : α → β) {g : β → γ} (hg : open_embedding g) :
open_embedding (g ∘ f) ↔ open_embedding f :=
by simp only [open_embedding_iff_continuous_injective_open, ← hg.is_open_map_iff,
← hg.1.continuous_iff, hg.inj.of_comp_iff]
lemma open_embedding.of_comp (f : α → β) {g : β → γ} (hg : open_embedding g)
(h : open_embedding (g ∘ f)) : open_embedding f :=
(open_embedding.of_comp_iff f hg).1 h
end open_embedding
section closed_embedding
variables [topological_space α] [topological_space β] [topological_space γ]
/-- A closed embedding is an embedding with closed image. -/
@[mk_iff]
structure closed_embedding (f : α → β) extends _root_.embedding f : Prop :=
(closed_range : is_closed $ range f)
variables {f : α → β}
lemma closed_embedding.tendsto_nhds_iff {ι : Type*}
{g : ι → α} {a : filter ι} {b : α} (hf : closed_embedding f) :
tendsto g a (𝓝 b) ↔ tendsto (f ∘ g) a (𝓝 (f b)) :=
hf.to_embedding.tendsto_nhds_iff
lemma closed_embedding.continuous (hf : closed_embedding f) : continuous f :=
hf.to_embedding.continuous
lemma closed_embedding.is_closed_map (hf : closed_embedding f) : is_closed_map f :=
hf.to_embedding.to_inducing.is_closed_map hf.closed_range
lemma closed_embedding.closed_iff_image_closed (hf : closed_embedding f)
{s : set α} : is_closed s ↔ is_closed (f '' s) :=
⟨hf.is_closed_map s,
λ h, begin
convert ←continuous_iff_is_closed.mp hf.continuous _ h,
apply preimage_image_eq _ hf.inj
end⟩
lemma closed_embedding.closed_iff_preimage_closed (hf : closed_embedding f)
{s : set β} (hs : s ⊆ range f) : is_closed s ↔ is_closed (f ⁻¹' s) :=
begin
convert ←hf.closed_iff_image_closed.symm,
rwa [image_preimage_eq_inter_range, inter_eq_self_of_subset_left]
end
lemma closed_embedding_of_embedding_closed (h₁ : embedding f)
(h₂ : is_closed_map f) : closed_embedding f :=
⟨h₁, by convert h₂ univ is_closed_univ; simp⟩
lemma closed_embedding_of_continuous_injective_closed (h₁ : continuous f)
(h₂ : injective f) (h₃ : is_closed_map f) : closed_embedding f :=
begin
refine closed_embedding_of_embedding_closed ⟨⟨_⟩, h₂⟩ h₃,
apply le_antisymm (continuous_iff_le_induced.mp h₁) _,
intro s',
change is_open _ ≤ is_open _,
rw [←is_closed_compl_iff, ←is_closed_compl_iff],
generalize : s'ᶜ = s,
rw is_closed_induced_iff,
refine λ hs, ⟨f '' s, h₃ s hs, _⟩,
rw preimage_image_eq _ h₂
end
lemma closed_embedding_id : closed_embedding (@id α) :=
⟨embedding_id, by convert is_closed_univ; apply range_id⟩
lemma closed_embedding.comp {g : β → γ} {f : α → β}
(hg : closed_embedding g) (hf : closed_embedding f) : closed_embedding (g ∘ f) :=
⟨hg.to_embedding.comp hf.to_embedding, show is_closed (range (g ∘ f)),
by rw [range_comp, ←hg.closed_iff_image_closed]; exact hf.closed_range⟩
lemma closed_embedding.closure_image_eq {f : α → β} (hf : closed_embedding f) (s : set α) :
closure (f '' s) = f '' closure s :=
(hf.is_closed_map.closure_image_subset _).antisymm
(image_closure_subset_closure_image hf.continuous)
end closed_embedding
|
c40002ba592795f7c982495ce1a251f72c857596 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/data/polynomial/degree/trailing_degree.lean | 9281f251d2919a024585a8f62bb8de8b26dd8161 | [
"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 | 12,775 | lean | /-
Copyright (c) 2020 Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Damiano Testa
-/
import data.polynomial.degree.basic
/-!
# Trailing degree of univariate polynomials
## Main definitions
* `trailing_degree p`: the multiplicity of `X` in the polynomial `p`
* `nat_trailing_degree`: a variant of `trailing_degree` that takes values in the natural numbers
* `trailing_coeff`: the coefficient at index `nat_trailing_degree p`
Converts most results about `degree`, `nat_degree` and `leading_coeff` to results about the bottom
end of a polynomial
-/
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
open function polynomial finsupp finset
open_locale big_operators
namespace polynomial
universes u v
variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ}
section semiring
variables [semiring R] {p q r : polynomial R}
/-- `trailing_degree p` is the multiplicity of `x` in the polynomial `p`, i.e. the smallest
`X`-exponent in `p`.
`trailing_degree p = some n` when `p ≠ 0` and `n` is the smallest power of `X` that appears
in `p`, otherwise
`trailing_degree 0 = ⊤`. -/
def trailing_degree (p : polynomial R) : with_top ℕ := p.support.inf some
lemma trailing_degree_lt_wf : well_founded
(λp q : polynomial R, trailing_degree p < trailing_degree q) :=
inv_image.wf trailing_degree (with_top.well_founded_lt nat.lt_wf)
/-- `nat_trailing_degree p` forces `trailing_degree p` to ℕ, by defining nat_trailing_degree 0 = 0. -/
def nat_trailing_degree (p : polynomial R) : ℕ := (trailing_degree p).get_or_else 0
/-- `trailing_coeff p` gives the coefficient of the smallest power of `X` in `p`-/
def trailing_coeff (p : polynomial R) : R := coeff p (nat_trailing_degree p)
/-- a polynomial is `monic_at` if its trailing coefficient is 1 -/
def trailing_monic (p : polynomial R) := trailing_coeff p = (1 : R)
lemma trailing_monic.def : trailing_monic p ↔ trailing_coeff p = 1 := iff.rfl
instance trailing_monic.decidable [decidable_eq R] : decidable (trailing_monic p) :=
by unfold trailing_monic; apply_instance
@[simp] lemma trailing_monic.trailing_coeff {p : polynomial R} (hp : p.trailing_monic) :
trailing_coeff p = 1 := hp
@[simp] lemma trailing_degree_zero : trailing_degree (0 : polynomial R) = ⊤ := rfl
@[simp] lemma nat_trailing_degree_zero : nat_trailing_degree (0 : polynomial R) = 0 := rfl
lemma trailing_degree_eq_top : trailing_degree p = ⊤ ↔ p = 0 :=
⟨λ h, by rw [trailing_degree, ← min_eq_inf_with_top] at h;
exact support_eq_empty.1 (min_eq_none.1 h),
λ h, h.symm ▸ rfl⟩
lemma trailing_degree_eq_nat_trailing_degree (hp : p ≠ 0) : trailing_degree p = (nat_trailing_degree p : with_top ℕ) :=
let ⟨n, hn⟩ :=
not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt trailing_degree_eq_top.1 hp)) in
have hn : trailing_degree p = some n := not_not.1 hn,
by rw [nat_trailing_degree, hn]; refl
lemma trailing_degree_eq_iff_nat_trailing_degree_eq {p : polynomial R} {n : ℕ} (hp : p ≠ 0) :
p.trailing_degree = n ↔ p.nat_trailing_degree = n :=
by rw [trailing_degree_eq_nat_trailing_degree hp, with_top.coe_eq_coe]
lemma trailing_degree_eq_iff_nat_trailing_degree_eq_of_pos {p : polynomial R} {n : ℕ} (hn : 0 < n) :
p.trailing_degree = n ↔ p.nat_trailing_degree = n :=
begin
split,
{ intro H, rwa ← trailing_degree_eq_iff_nat_trailing_degree_eq, rintro rfl,
rw trailing_degree_zero at H, exact option.no_confusion H },
{ intro H, rwa trailing_degree_eq_iff_nat_trailing_degree_eq, rintro rfl,
rw nat_trailing_degree_zero at H, rw H at hn, exact lt_irrefl _ hn }
end
lemma nat_trailing_degree_eq_of_trailing_degree_eq_some {p : polynomial R} {n : ℕ}
(h : trailing_degree p = n) : nat_trailing_degree p = n :=
have hp0 : p ≠ 0, from λ hp0, by rw hp0 at h; exact option.no_confusion h,
option.some_inj.1 $ show (nat_trailing_degree p : with_top ℕ) = n,
by rwa [← trailing_degree_eq_nat_trailing_degree hp0]
@[simp] lemma nat_trailing_degree_le_trailing_degree : ↑(nat_trailing_degree p) ≤ trailing_degree p :=
begin
by_cases hp : p = 0, { rw hp, exact le_top },
rw [trailing_degree_eq_nat_trailing_degree hp],
exact le_refl _
end
lemma nat_trailing_degree_eq_of_trailing_degree_eq [semiring S] {q : polynomial S} (h : trailing_degree p = trailing_degree q) :
nat_trailing_degree p = nat_trailing_degree q :=
by unfold nat_trailing_degree; rw h
lemma le_trailing_degree_of_ne_zero (h : coeff p n ≠ 0) : trailing_degree p ≤ n :=
show @has_le.le (with_top ℕ) _ (p.support.inf some : with_top ℕ) (some n : with_top ℕ),
from finset.inf_le (finsupp.mem_support_iff.2 h)
lemma nat_trailing_degree_le_of_ne_zero (h : coeff p n ≠ 0) : nat_trailing_degree p ≤ n :=
begin
rw [← with_top.coe_le_coe, ← trailing_degree_eq_nat_trailing_degree],
{ exact le_trailing_degree_of_ne_zero h, },
{ assume h, subst h, exact h rfl }
end
lemma trailing_degree_le_trailing_degree (h : coeff q (nat_trailing_degree p) ≠ 0) : trailing_degree q ≤ trailing_degree p :=
begin
by_cases hp : p = 0,
{ rw hp, exact le_top },
{ rw trailing_degree_eq_nat_trailing_degree hp, exact le_trailing_degree_of_ne_zero h }
end
lemma trailing_degree_ne_of_nat_trailing_degree_ne {n : ℕ} :
p.nat_trailing_degree ≠ n → trailing_degree p ≠ n :=
@option.cases_on _ (λ d, d.get_or_else 0 ≠ n → d ≠ n) p.trailing_degree
(λ _ h, option.no_confusion h)
(λ n' h, mt option.some_inj.mp h)
theorem nat_trailing_degree_le_of_trailing_degree_le {n : ℕ} {hp : p ≠ 0} (H : (n : with_top ℕ) ≤ trailing_degree p) : n ≤ nat_trailing_degree p :=
begin
rw trailing_degree_eq_nat_trailing_degree hp at H,
exact with_top.coe_le_coe.mp H,
end
lemma nat_trailing_degree_le_nat_trailing_degree {hq : q ≠ 0} (hpq : p.trailing_degree ≤ q.trailing_degree) : p.nat_trailing_degree ≤ q.nat_trailing_degree :=
begin
by_cases hp : p = 0, { rw [hp, nat_trailing_degree_zero], exact zero_le _ },
rwa [trailing_degree_eq_nat_trailing_degree hp, trailing_degree_eq_nat_trailing_degree hq, with_top.coe_le_coe] at hpq
end
@[simp] lemma trailing_degree_C (ha : a ≠ 0) : trailing_degree (C a) = (0 : with_top ℕ) :=
show inf (ite (a = 0) ∅ {0}) some = 0, by rw if_neg ha; refl
lemma le_trailing_degree_C : (0 : with_top ℕ) ≤ trailing_degree (C a) :=
by by_cases h : a = 0; [rw [h, C_0], rw [trailing_degree_C h]]; [exact bot_le, exact le_refl _]
lemma trailing_degree_one_le : (0 : with_top ℕ) ≤ trailing_degree (1 : polynomial R) :=
by rw [← C_1]; exact le_trailing_degree_C
@[simp] lemma nat_trailing_degree_C (a : R) : nat_trailing_degree (C a) = 0 :=
begin
by_cases ha : a = 0,
{ rw [ha, C_0, nat_trailing_degree_zero], },
{ rw [nat_trailing_degree, trailing_degree_C ha], refl }
end
@[simp] lemma nat_trailing_degree_one : nat_trailing_degree (1 : polynomial R) = 0 := nat_trailing_degree_C 1
@[simp] lemma nat_trailing_degree_nat_cast (n : ℕ) : nat_trailing_degree (n : polynomial R) = 0 :=
by simp only [←C_eq_nat_cast, nat_trailing_degree_C]
@[simp] lemma trailing_degree_monomial (n : ℕ) (ha : a ≠ 0) : trailing_degree (C a * X ^ n) = n :=
by rw [← single_eq_C_mul_X, trailing_degree, monomial, support_single_ne_zero ha]; refl
lemma monomial_le_trailing_degree (n : ℕ) (a : R) : (n : with_top ℕ) ≤ trailing_degree (C a * X ^ n) :=
if h : a = 0 then by rw [h, C_0, zero_mul]; exact le_top else le_of_eq (trailing_degree_monomial n h).symm
lemma coeff_eq_zero_of_trailing_degree_lt (h : (n : with_top ℕ) < trailing_degree p) : coeff p n = 0 :=
not_not.1 (mt le_trailing_degree_of_ne_zero (not_le_of_gt h))
lemma coeff_eq_zero_of_lt_nat_trailing_degree {p : polynomial R} {n : ℕ} (h : n < p.nat_trailing_degree) :
p.coeff n = 0 :=
begin
apply coeff_eq_zero_of_trailing_degree_lt,
by_cases hp : p = 0,
{ subst hp, exact with_top.coe_lt_top n, },
{ rwa [trailing_degree_eq_nat_trailing_degree hp, with_top.coe_lt_coe] },
end
@[simp] lemma coeff_nat_trailing_degree_pred_eq_zero {p : polynomial R} {hp : (0 : with_top ℕ) < nat_trailing_degree p} : p.coeff (p.nat_trailing_degree - 1) = 0 :=
coeff_eq_zero_of_lt_nat_trailing_degree $ nat.sub_lt
((with_top.zero_lt_coe (nat_trailing_degree p)).mp hp) nat.one_pos
theorem le_trailing_degree_C_mul_X_pow (r : R) (n : ℕ) : (n : with_top ℕ) ≤ trailing_degree (C r * X^n) :=
begin
rw [← single_eq_C_mul_X],
refine finset.le_inf (λ b hb, _),
rw list.eq_of_mem_singleton (finsupp.support_single_subset hb),
exact le_refl _,
end
theorem le_trailing_degree_X_pow (n : ℕ) : (n : with_top ℕ) ≤ trailing_degree (X^n : polynomial R) :=
by simpa only [C_1, one_mul] using le_trailing_degree_C_mul_X_pow (1:R) n
theorem le_trailing_degree_X : (1 : with_top ℕ) ≤ trailing_degree (X : polynomial R) :=
by simpa only [C_1, one_mul, pow_one] using le_trailing_degree_C_mul_X_pow (1:R) 1
lemma nat_trailing_degree_X_le : (X : polynomial R).nat_trailing_degree ≤ 1 :=
begin
by_cases h : X = 0,
{ rw [h, nat_trailing_degree_zero],
exact zero_le 1, },
{ unfold nat_trailing_degree,
unfold trailing_degree,
rw [support_X, inf_singleton, option.get_or_else_some],
intro,
apply h,
rw [← mul_one X, ← C_1, a, C_0, mul_zero], },
end
@[simp] lemma trailing_coeff_eq_zero : trailing_coeff p = 0 ↔ p = 0 :=
⟨λ h, by_contradiction $ λ hp, mt mem_support_iff.1
(not_not.2 h) (mem_of_min (trailing_degree_eq_nat_trailing_degree hp)),
λ h, h.symm ▸ leading_coeff_zero⟩
lemma trailing_coeff_nonzero_iff_nonzero : trailing_coeff p ≠ 0 ↔ p ≠ 0 :=
not_congr trailing_coeff_eq_zero
lemma nat_trailing_degree_mem_support_of_nonzero : p ≠ 0 → nat_trailing_degree p ∈ p.support :=
(mem_support_iff_coeff_ne_zero.mpr ∘ trailing_coeff_nonzero_iff_nonzero.mpr)
lemma nat_trailing_degree_le_of_mem_supp (a : ℕ) :
a ∈ p.support → nat_trailing_degree p ≤ a:=
nat_trailing_degree_le_of_ne_zero ∘ mem_support_iff_coeff_ne_zero.mp
lemma nat_trailing_degree_eq_support_min' (h : p ≠ 0) :
nat_trailing_degree p = p.support.min' (nonempty_support_iff.mpr h) :=
begin
apply le_antisymm,
{ apply le_min',
intros y hy,
exact nat_trailing_degree_le_of_mem_supp y hy },
{ apply finset.min'_le,
exact mem_support_iff_coeff_ne_zero.mpr (trailing_coeff_nonzero_iff_nonzero.mpr h), },
end
end semiring
section nonzero_semiring
variables [semiring R] [nontrivial R] {p q : polynomial R}
@[simp] lemma trailing_degree_one : trailing_degree (1 : polynomial R) = (0 : with_top ℕ) :=
trailing_degree_C (show (1 : R) ≠ 0, from zero_ne_one.symm)
@[simp] lemma trailing_degree_X : trailing_degree (X : polynomial R) = 1 :=
begin
unfold X trailing_degree monomial single finsupp.support,
rw if_neg (one_ne_zero : (1 : R) ≠ 0),
refl
end
@[simp] lemma nat_trailing_degree_X : (X : polynomial R).nat_trailing_degree = 1 :=
nat_trailing_degree_eq_of_trailing_degree_eq_some trailing_degree_X
end nonzero_semiring
section ring
variables [ring R]
@[simp] lemma trailing_degree_neg (p : polynomial R) : trailing_degree (-p) = trailing_degree p :=
by unfold trailing_degree; rw support_neg
@[simp] lemma nat_trailing_degree_neg (p : polynomial R) : nat_trailing_degree (-p) = nat_trailing_degree p :=
by simp [nat_trailing_degree]
@[simp] lemma nat_trailing_degree_int_cast (n : ℤ) : nat_trailing_degree (n : polynomial R) = 0 :=
by simp only [←C_eq_int_cast, nat_trailing_degree_C]
end ring
section semiring
variables [semiring R]
/-- The second-lowest coefficient, or 0 for constants -/
def next_coeff_up (p : polynomial R) : R :=
if p.nat_trailing_degree = 0 then 0 else p.coeff (p.nat_trailing_degree + 1)
@[simp]
lemma next_coeff_up_C_eq_zero (c : R) :
next_coeff_up (C c) = 0 := by { rw next_coeff_up, simp }
lemma next_coeff_up_of_pos_nat_trailing_degree (p : polynomial R) (hp : 0 < p.nat_trailing_degree) :
next_coeff_up p = p.coeff (p.nat_trailing_degree + 1) :=
by { rw [next_coeff_up, if_neg], contrapose! hp, simpa }
end semiring
section semiring
variables [semiring R] {p q : polynomial R} {ι : Type*}
lemma coeff_nat_trailing_degree_eq_zero_of_trailing_degree_lt (h : trailing_degree p < trailing_degree q) :
coeff q (nat_trailing_degree p) = 0 :=
begin
refine coeff_eq_zero_of_trailing_degree_lt _,
rcases h with ⟨n, hn, hq⟩,
rw option.mem_def at hn,
simp_rw [option.mem_def] at hq,
unfold nat_trailing_degree,
rw [hn, option.get_or_else_some],
exact ⟨n, ⟨rfl, hq⟩⟩,
end
lemma ne_zero_of_trailing_degree_lt {n : with_top ℕ} (h : trailing_degree p < n) : p ≠ 0 :=
begin
intro p0,
rw (trailing_degree_eq_top.mpr p0) at h,
revert h,
exact dec_trivial,
end
end semiring
end polynomial
|
67c3ade6d1c2d18010c77893b5d08df5ac708ec7 | fa01e273a2a9f22530e6adb1ed7d4f54bb15c8d7 | /src/N2O/Default.lean | 5314a3eeb6f47fca94113ca219f0912a0fb4ed70 | [
"LicenseRef-scancode-mit-taylor-variant",
"LicenseRef-scancode-warranty-disclaimer"
] | permissive | o89/n2o | 4c99afb11fff0a1e3dae6b3bc8a3b7fc42c314ac | 58c1fbf4ef892ed86bdc6b78ec9ca5a403715c2d | refs/heads/master | 1,670,314,676,229 | 1,669,086,375,000 | 1,669,086,375,000 | 200,506,953 | 16 | 6 | null | null | null | null | UTF-8 | Lean | false | false | 51 | lean | import N2O.Data.Default
import N2O.Network.Default
|
850485d616257a1bae61da590653b0a21dde1170 | 5bf112cf7101c6c6303dc3fd0b3179c860e61e56 | /lean/background/default.lean | 1ed9ea41f0a488767195e59dfc0f4375410df5db | [
"Apache-2.0"
] | permissive | fredfeng/formal-encoding | 7ab645f49a553dfad2af03fcb4289e40fc679759 | 024efcf58672ac6b817caa10dfe8cd9708b07f1b | refs/heads/master | 1,597,236,551,123 | 1,568,832,149,000 | 1,568,832,149,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 46 | lean | import .util .number .set .vec .grid .witness
|
c2f86c95993c422aa0238d9d3a73cbb03c9d2309 | 302b541ac2e998a523ae04da7673fd0932ded126 | /tests/simple/case.lean | 4ed7d298db59adf06e4bf76cde9e7bab566856b8 | [] | no_license | mattweingarten/lambdapure | 4aeff69e8e3b8e78ea3c0a2b9b61770ef5a689b1 | f920a4ad78e6b1e3651f30bf8445c9105dfa03a8 | refs/heads/master | 1,680,665,168,790 | 1,618,420,180,000 | 1,618,420,180,000 | 310,816,264 | 2 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 100 | lean | set_option trace.compiler.ir.init true
def case : Nat -> Nat
| 0 => 1
| 1 => 2
| x => x + 1
|
b48929a0a32335b2e88df1ece11c6918d2924e98 | 8f209eb34c0c4b9b6be5e518ebfc767a38bed79c | /code/src/theorems.lean | 4c10fea77fac406f5b9077e3e2859db2b3fe24b9 | [] | no_license | hediet/masters-thesis | 13e3bcacb6227f25f7ec4691fb78cb0363f2dfb5 | dc40c14cc4ed073673615412f36b4e386ee7aac9 | refs/heads/master | 1,680,591,056,302 | 1,617,710,887,000 | 1,617,710,887,000 | 311,762,038 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,016 | lean | import .definitions
import .internal.U
import .internal.R
import .internal.A
import .internal.internal_definitions
import .internal.R_red_removable
import .internal.R_acc_mem_of_reachable
variable [GuardModule]
open GuardModule
theorem 𝒰_semantic: ∀ gdt: Gdt, ∀ env: Env,
(𝒰 gdt).eval env ↔ (gdt.eval env = Result.no_match) :=
begin
assume gdt env,
rw ←@U_eq_𝒰,
simp [U_semantic],
end
theorem ℛ_semantic : ∀ can_prove_empty: CorrectCanProveEmpty, ∀ gdt: Gdt, gdt.disjoint_rhss →
(
let ⟨ a, i, r ⟩ := ℛ can_prove_empty.val (𝒜 gdt)
in
-- Reachable rhss are accessible and neither inaccessible nor redundant.
(∀ env: Env, ∀ rhs: Rhs,
gdt.eval env = Result.value rhs
→ rhs ∈ a \ (i ++ r)
)
∧
-- Redundant rhss can be removed without changing semantics.
Gdt.eval_option (gdt.remove_rhss r.to_finset)
= gdt.eval
: Prop
)
:=
begin
assume can_prove_empty gdt gdt_disjoint,
cases c: ℛ can_prove_empty.val (𝒜 gdt) with a i_r,
cases i_r with i r,
dsimp only,
rw ←R_eq_ℛ at c,
unfold RhsPartition.to_triple at c,
set p := R (Ant.map can_prove_empty.val (𝒜 gdt)) with p_def,
cases c,
have Agdt_def := eq.symm (Ant.mark_inactive_rhss_eq_of_eval_rhss_eq (A_sem_eq_𝒜 gdt)),
split, {
assume env rhs h,
replace Agdt_def := function.funext_iff.1 Agdt_def env,
exact R_acc_mem_of_reachable gdt_disjoint can_prove_empty Agdt_def h p_def,
}, {
have := R_red_removable can_prove_empty gdt_disjoint Agdt_def,
rw ←p_def at this,
exact this,
},
end
theorem 𝒰𝒜_acc_eq (acc: Φ → Φ) (gdt: Gdt): 𝒰𝒜_acc acc gdt = (𝒰_acc acc gdt, 𝒜_acc acc gdt) :=
by induction gdt generalizing acc; try { cases gdt_grd }; simp [𝒰𝒜_acc, 𝒰_acc, 𝒜_acc, *]
|
74fec4d96aab39403c07d4ee4b1efa921cb69970 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/localized/import3.lean | 93a5d1147148f1ff5e3f3ddac737da7b12cb1d3e | [
"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 | 332 | lean | import .import1 .import2
/- this file tests that declararing the same or similar notation in two different files will rarely
cause errors. We will get an error if we add the same command to the same namespace in the same
environment in two different files, and then import them both.
(e.g. if we remove `nat` in file `import2`. -/
|
97fced7fb107490da08a479fc6a5004d042f663b | 88892181780ff536a81e794003fe058062f06758 | /src/projecteuler/problem1.lean | cbd2b4ab497bacb77e06a8105a7eaa0645de273d | [] | no_license | AtnNn/lean-sandbox | fe2c44280444e8bb8146ab8ac391c82b480c0a2e | 8c68afbdc09213173aef1be195da7a9a86060a97 | refs/heads/master | 1,623,004,395,876 | 1,579,969,507,000 | 1,579,969,507,000 | 146,666,368 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,950 | lean | import lib.tactics
import lib.sets
import lib.nat
import lib.lists
namespace problem1
def problem : ℕ :=
set_sum { n | (3 ∣ n ∨ 5 ∣ n) ∧ n < 1000 }
def sum_to (n : ℕ) := n * (n + 1) / 2
def sum_mult_below (n : ℕ) (m : ℕ) := sum_to ((m - 1) / n) * n
def sum_mult35_below (m : ℕ) :=
sum_mult_below 3 m
+ sum_mult_below 5 m
- sum_mult_below 15 m
example : sum_mult35_below 10 = 23 := rfl
def solution := sum_mult35_below 1000
lemma sum_to_suc {n} : sum_to (n + 1) = n + 1 + sum_to n := begin
delta sum_to,
have h₁ := by calc
(n + 1) * (n + 1 + 1) / 2 = (n + 1) * (n + 2) / 2 : by simp
... = (n * n + n * 3 + 2) / 2 : by { simp, ring },
have h₂ := by calc
n + 1 + n * (n + 1) / 2 = (n + 1) + n * (n + 1) / 2 : by simp
... = (n + 1) * (1 + 1) / (1 + 1) + n * (n + 1) / 2 : by rw [←nat_mul_div_eq 1 (n + 1)]
... = ((n + 1) * 2 + n * (n + 1)) / 2 : by simp [mul_distrib_of_dvd, dvd_mul, mul_suc_even]
... = (n * 2 + 1 * 2 + n * n + n * 1) / 2 : by simp [nat.left_distrib, nat.right_distrib]
... = (n * n + n * 3 + 2) / 2 : by { simp, ring },
rw [h₁, h₂]
end
lemma sum_to_eq_sum_iota {n} : sum_to n = list.sum (list.iota n) := begin
induction n with n ih,
{ refl },
{ rw [sum_to_suc, ih, iota_succ, sum_cons] }
end
lemma filter_and {T} {p q : T → Prop} [decidable_pred p] [decidable_pred q] {xs : list T} : list.filter (λ x, (p x ∧ q x)) xs = list.filter p (list.filter q xs) := begin
sorry
end
lemma set_sum_of_sum_mult_below {d k : ℕ} : sum_mult_below d k = set_sum {n | d ∣ n ∧ n < k} := begin
delta set_sum,
delta sum_mult_below,
unfold has_upper_bound.ub,
rw [sum_to_eq_sum_iota],
rw [sum_map_of_sum_mul],
refine congr rfl _,
generalize h : (k - 1) / d = m,
induction m,
{ simp, sorry },
{ sorry }
end
theorem proof : problem = solution := begin
delta problem solution,
delta sum_mult35_below,
sorry
end
end problem1
|
eecdcc8dd5b0fd57599ffeeedd599066d55b5329 | 05b503addd423dd68145d68b8cde5cd595d74365 | /src/topology/metric_space/basic.lean | 305b6a80d702a6b0992e3a6c5746fa58213526c7 | [
"Apache-2.0"
] | permissive | aestriplex/mathlib | 77513ff2b176d74a3bec114f33b519069788811d | e2fa8b2b1b732d7c25119229e3cdfba8370cb00f | refs/heads/master | 1,621,969,960,692 | 1,586,279,279,000 | 1,586,279,279,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 70,218 | lean | /-
Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Metric spaces.
Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel
Many definitions and theorems expected on metric spaces are already introduced on uniform spaces and
topological spaces. For example:
open and closed sets, compactness, completeness, continuity and uniform continuity
-/
import data.real.nnreal topology.metric_space.emetric_space topology.algebra.ordered
open set filter classical topological_space
noncomputable theory
open_locale uniformity topological_space
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
/-- Construct a uniform structure from a distance function and metric space axioms -/
def uniform_space_of_dist
(dist : α → α → ℝ)
(dist_self : ∀ x : α, dist x x = 0)
(dist_comm : ∀ x y : α, dist x y = dist y x)
(dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : uniform_space α :=
uniform_space.of_core {
uniformity := (⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε}),
refl := le_infi $ assume ε, le_infi $
by simp [set.subset_def, id_rel, dist_self, (>)] {contextual := tt},
comp := le_infi $ assume ε, le_infi $ assume h, lift'_le
(mem_infi_sets (ε / 2) $ mem_infi_sets (div_pos_of_pos_of_pos h two_pos) (subset.refl _)) $
have ∀ (a b c : α), dist a c < ε / 2 → dist c b < ε / 2 → dist a b < ε,
from assume a b c hac hcb,
calc dist a b ≤ dist a c + dist c b : dist_triangle _ _ _
... < ε / 2 + ε / 2 : add_lt_add hac hcb
... = ε : by rw [div_add_div_same, add_self_div_two],
by simpa [comp_rel],
symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h,
tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [dist_comm] }
/-- The distance function (given an ambient metric space on `α`), which returns
a nonnegative real number `dist x y` given `x y : α`. -/
class has_dist (α : Type*) := (dist : α → α → ℝ)
export has_dist (dist)
section prio
set_option default_priority 100 -- see Note [default priority]
/-- Metric space
Each metric space induces a canonical `uniform_space` and hence a canonical `topological_space`.
This is enforced in the type class definition, by extending the `uniform_space` structure. When
instantiating a `metric_space` structure, the uniformity fields are not necessary, they will be
filled in by default. In the same way, each metric space induces an emetric space structure.
It is included in the structure, but filled in by default.
When one instantiates a metric space structure, for instance a product structure,
this makes it possible to use a uniform structure and an edistance that are exactly
the ones for the uniform spaces product and the emetric spaces products, thereby
ensuring that everything in defeq in diamonds.-/
class metric_space (α : Type u) extends has_dist α : Type u :=
(dist_self : ∀ x : α, dist x x = 0)
(eq_of_dist_eq_zero : ∀ {x y : α}, dist x y = 0 → x = y)
(dist_comm : ∀ x y : α, dist x y = dist y x)
(dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z)
(edist : α → α → ennreal := λx y, ennreal.of_real (dist x y))
(edist_dist : ∀ x y : α, edist x y = ennreal.of_real (dist x y) . control_laws_tac)
(to_uniform_space : uniform_space α := uniform_space_of_dist dist dist_self dist_comm dist_triangle)
(uniformity_dist : 𝓤 α = ⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε} . control_laws_tac)
end prio
variables [metric_space α]
@[priority 100] -- see Note [lower instance priority]
instance metric_space.to_uniform_space' : uniform_space α :=
metric_space.to_uniform_space α
@[priority 200] -- see Note [lower instance priority]
instance metric_space.to_has_edist : has_edist α := ⟨metric_space.edist⟩
@[simp] theorem dist_self (x : α) : dist x x = 0 := metric_space.dist_self x
theorem eq_of_dist_eq_zero {x y : α} : dist x y = 0 → x = y :=
metric_space.eq_of_dist_eq_zero
theorem dist_comm (x y : α) : dist x y = dist y x := metric_space.dist_comm x y
theorem edist_dist (x y : α) : edist x y = ennreal.of_real (dist x y) :=
metric_space.edist_dist _ x y
@[simp] theorem dist_eq_zero {x y : α} : dist x y = 0 ↔ x = y :=
iff.intro eq_of_dist_eq_zero (assume : x = y, this ▸ dist_self _)
@[simp] theorem zero_eq_dist {x y : α} : 0 = dist x y ↔ x = y :=
by rw [eq_comm, dist_eq_zero]
theorem dist_triangle (x y z : α) : dist x z ≤ dist x y + dist y z :=
metric_space.dist_triangle x y z
theorem dist_triangle_left (x y z : α) : dist x y ≤ dist z x + dist z y :=
by rw dist_comm z; apply dist_triangle
theorem dist_triangle_right (x y z : α) : dist x y ≤ dist x z + dist y z :=
by rw dist_comm y; apply dist_triangle
lemma dist_triangle4 (x y z w : α) :
dist x w ≤ dist x y + dist y z + dist z w :=
calc
dist x w ≤ dist x z + dist z w : dist_triangle x z w
... ≤ (dist x y + dist y z) + dist z w : add_le_add_right (metric_space.dist_triangle x y z) _
lemma dist_triangle4_left (x₁ y₁ x₂ y₂ : α) :
dist x₂ y₂ ≤ dist x₁ y₁ + (dist x₁ x₂ + dist y₁ y₂) :=
by rw [add_left_comm, dist_comm x₁, ← add_assoc]; apply dist_triangle4
lemma dist_triangle4_right (x₁ y₁ x₂ y₂ : α) :
dist x₁ y₁ ≤ dist x₁ x₂ + dist y₁ y₂ + dist x₂ y₂ :=
by rw [add_right_comm, dist_comm y₁]; apply dist_triangle4
/-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/
lemma dist_le_Ico_sum_dist (f : ℕ → α) {m n} (h : m ≤ n) :
dist (f m) (f n) ≤ (finset.Ico m n).sum (λ i, dist (f i) (f (i + 1))) :=
begin
revert n,
apply nat.le_induction,
{ simp only [finset.sum_empty, finset.Ico.self_eq_empty, dist_self] },
{ assume n hn hrec,
calc dist (f m) (f (n+1)) ≤ dist (f m) (f n) + dist _ _ : dist_triangle _ _ _
... ≤ (finset.Ico m n).sum _ + _ : add_le_add hrec (le_refl _)
... = (finset.Ico m (n+1)).sum _ :
by rw [finset.Ico.succ_top hn, finset.sum_insert, add_comm]; simp }
end
/-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/
lemma dist_le_range_sum_dist (f : ℕ → α) (n : ℕ) :
dist (f 0) (f n) ≤ (finset.range n).sum (λ i, dist (f i) (f (i + 1))) :=
finset.Ico.zero_bot n ▸ dist_le_Ico_sum_dist f (nat.zero_le n)
/-- A version of `dist_le_Ico_sum_dist` with each intermediate distance replaced
with an upper estimate. -/
lemma dist_le_Ico_sum_of_dist_le {f : ℕ → α} {m n} (hmn : m ≤ n)
{d : ℕ → ℝ} (hd : ∀ {k}, m ≤ k → k < n → dist (f k) (f (k + 1)) ≤ d k) :
dist (f m) (f n) ≤ (finset.Ico m n).sum d :=
le_trans (dist_le_Ico_sum_dist f hmn) $
finset.sum_le_sum $ λ k hk, hd (finset.Ico.mem.1 hk).1 (finset.Ico.mem.1 hk).2
/-- A version of `dist_le_range_sum_dist` with each intermediate distance replaced
with an upper estimate. -/
lemma dist_le_range_sum_of_dist_le {f : ℕ → α} (n : ℕ)
{d : ℕ → ℝ} (hd : ∀ {k}, k < n → dist (f k) (f (k + 1)) ≤ d k) :
dist (f 0) (f n) ≤ (finset.range n).sum d :=
finset.Ico.zero_bot n ▸ dist_le_Ico_sum_of_dist_le (zero_le n) (λ _ _, hd)
theorem swap_dist : function.swap (@dist α _) = dist :=
by funext x y; exact dist_comm _ _
theorem abs_dist_sub_le (x y z : α) : abs (dist x z - dist y z) ≤ dist x y :=
abs_sub_le_iff.2
⟨sub_le_iff_le_add.2 (dist_triangle _ _ _),
sub_le_iff_le_add.2 (dist_triangle_left _ _ _)⟩
theorem dist_nonneg {x y : α} : 0 ≤ dist x y :=
have 2 * dist x y ≥ 0,
from calc 2 * dist x y = dist x y + dist y x : by rw [dist_comm x y, two_mul]
... ≥ 0 : by rw ← dist_self x; apply dist_triangle,
nonneg_of_mul_nonneg_left this two_pos
@[simp] theorem dist_le_zero {x y : α} : dist x y ≤ 0 ↔ x = y :=
by simpa [le_antisymm_iff, dist_nonneg] using @dist_eq_zero _ _ x y
@[simp] theorem dist_pos {x y : α} : 0 < dist x y ↔ x ≠ y :=
by simpa [-dist_le_zero] using not_congr (@dist_le_zero _ _ x y)
@[simp] theorem abs_dist {a b : α} : abs (dist a b) = dist a b :=
abs_of_nonneg dist_nonneg
theorem eq_of_forall_dist_le {x y : α} (h : ∀ε, ε > 0 → dist x y ≤ ε) : x = y :=
eq_of_dist_eq_zero (eq_of_le_of_forall_le_of_dense dist_nonneg h)
/-- Distance as a nonnegative real number. -/
def nndist (a b : α) : nnreal := ⟨dist a b, dist_nonneg⟩
/--Express `nndist` in terms of `edist`-/
lemma nndist_edist (x y : α) : nndist x y = (edist x y).to_nnreal :=
by simp [nndist, edist_dist, nnreal.of_real, max_eq_left dist_nonneg, ennreal.of_real]
/--Express `edist` in terms of `nndist`-/
lemma edist_nndist (x y : α) : edist x y = ↑(nndist x y) :=
by { rw [edist_dist, nndist, ennreal.of_real_eq_coe_nnreal] }
/--In a metric space, the extended distance is always finite-/
lemma edist_ne_top (x y : α) : edist x y ≠ ⊤ :=
by rw [edist_dist x y]; apply ennreal.coe_ne_top
/--In a metric space, the extended distance is always finite-/
lemma edist_lt_top {α : Type*} [metric_space α] (x y : α) : edist x y < ⊤ :=
ennreal.lt_top_iff_ne_top.2 (edist_ne_top x y)
/--`nndist x x` vanishes-/
@[simp] lemma nndist_self (a : α) : nndist a a = 0 := (nnreal.coe_eq_zero _).1 (dist_self a)
/--Express `dist` in terms of `nndist`-/
lemma dist_nndist (x y : α) : dist x y = ↑(nndist x y) := rfl
/--Express `nndist` in terms of `dist`-/
lemma nndist_dist (x y : α) : nndist x y = nnreal.of_real (dist x y) :=
by rw [dist_nndist, nnreal.of_real_coe]
/--Deduce the equality of points with the vanishing of the nonnegative distance-/
theorem eq_of_nndist_eq_zero {x y : α} : nndist x y = 0 → x = y :=
by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, dist_eq_zero]
theorem nndist_comm (x y : α) : nndist x y = nndist y x :=
by simpa [nnreal.eq_iff.symm] using dist_comm x y
/--Characterize the equality of points with the vanishing of the nonnegative distance-/
@[simp] theorem nndist_eq_zero {x y : α} : nndist x y = 0 ↔ x = y :=
by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, dist_eq_zero]
@[simp] theorem zero_eq_nndist {x y : α} : 0 = nndist x y ↔ x = y :=
by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, zero_eq_dist]
/--Triangle inequality for the nonnegative distance-/
theorem nndist_triangle (x y z : α) : nndist x z ≤ nndist x y + nndist y z :=
by simpa [nnreal.coe_le_coe] using dist_triangle x y z
theorem nndist_triangle_left (x y z : α) : nndist x y ≤ nndist z x + nndist z y :=
by simpa [nnreal.coe_le_coe] using dist_triangle_left x y z
theorem nndist_triangle_right (x y z : α) : nndist x y ≤ nndist x z + nndist y z :=
by simpa [nnreal.coe_le_coe] using dist_triangle_right x y z
/--Express `dist` in terms of `edist`-/
lemma dist_edist (x y : α) : dist x y = (edist x y).to_real :=
by rw [edist_dist, ennreal.to_real_of_real (dist_nonneg)]
namespace metric
/- instantiate metric space as a topology -/
variables {x y z : α} {ε ε₁ ε₂ : ℝ} {s : set α}
/-- `ball x ε` is the set of all points `y` with `dist y x < ε` -/
def ball (x : α) (ε : ℝ) : set α := {y | dist y x < ε}
@[simp] theorem mem_ball : y ∈ ball x ε ↔ dist y x < ε := iff.rfl
theorem mem_ball' : y ∈ ball x ε ↔ dist x y < ε := by rw dist_comm; refl
/-- `closed_ball x ε` is the set of all points `y` with `dist y x ≤ ε` -/
def closed_ball (x : α) (ε : ℝ) := {y | dist y x ≤ ε}
@[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ dist y x ≤ ε := iff.rfl
theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε :=
assume y (hy : _ < _), le_of_lt hy
theorem pos_of_mem_ball (hy : y ∈ ball x ε) : ε > 0 :=
lt_of_le_of_lt dist_nonneg hy
theorem mem_ball_self (h : ε > 0) : x ∈ ball x ε :=
show dist x x < ε, by rw dist_self; assumption
theorem mem_closed_ball_self (h : ε ≥ 0) : x ∈ closed_ball x ε :=
show dist x x ≤ ε, by rw dist_self; assumption
theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε :=
by simp [dist_comm]
theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ :=
λ y (yx : _ < ε₁), lt_of_lt_of_le yx h
theorem closed_ball_subset_closed_ball {α : Type u} [metric_space α] {ε₁ ε₂ : ℝ} {x : α} (h : ε₁ ≤ ε₂) :
closed_ball x ε₁ ⊆ closed_ball x ε₂ :=
λ y (yx : _ ≤ ε₁), le_trans yx h
theorem ball_disjoint (h : ε₁ + ε₂ ≤ dist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩,
not_lt_of_le (dist_triangle_left x y z)
(lt_of_lt_of_le (add_lt_add h₁ h₂) h)
theorem ball_disjoint_same (h : ε ≤ dist x y / 2) : ball x ε ∩ ball y ε = ∅ :=
ball_disjoint $ by rwa [← two_mul, ← le_div_iff' two_pos]
theorem ball_subset (h : dist x y ≤ ε₂ - ε₁) : ball x ε₁ ⊆ ball y ε₂ :=
λ z zx, by rw ← add_sub_cancel'_right ε₁ ε₂; exact
lt_of_le_of_lt (dist_triangle z x y) (add_lt_add_of_lt_of_le zx h)
theorem ball_half_subset (y) (h : y ∈ ball x (ε / 2)) : ball y (ε / 2) ⊆ ball x ε :=
ball_subset $ by rw sub_self_div_two; exact le_of_lt h
theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε :=
⟨_, sub_pos.2 h, ball_subset $ by rw sub_sub_self⟩
theorem ball_eq_empty_iff_nonpos : ε ≤ 0 ↔ ball x ε = ∅ :=
(eq_empty_iff_forall_not_mem.trans
⟨λ h, le_of_not_gt $ λ ε0, h _ $ mem_ball_self ε0,
λ ε0 y h, not_lt_of_le ε0 $ pos_of_mem_ball h⟩).symm
@[simp] lemma ball_zero : ball x 0 = ∅ :=
by rw [← metric.ball_eq_empty_iff_nonpos]
theorem uniformity_basis_dist :
(𝓤 α).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {p:α×α | dist p.1 p.2 < ε}) :=
(metric_space.uniformity_dist α).symm ▸ has_basis_binfi_principal
(λ r (hr : 0 < r) p (hp : 0 < p), ⟨min r p, lt_min hr hp,
λ x (hx : dist _ _ < _), lt_of_lt_of_le hx (min_le_left r p),
λ x (hx : dist _ _ < _), lt_of_lt_of_le hx (min_le_right r p)⟩) $
nonempty_Ioi
/-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`.
For specific bases see `uniformity_basis_dist`, `uniformity_basis_dist_inv_nat_succ`,
and `uniformity_basis_dist_inv_nat_pos`. -/
protected theorem mk_uniformity_basis {β : Type*} {p : β → Prop} {f : β → ℝ}
(hf₀ : ∀ i, p i → 0 < f i) (hf : ∀ ⦃ε⦄, 0 < ε → ∃ i (hi : p i), f i ≤ ε) :
(𝓤 α).has_basis p (λ i, {p:α×α | dist p.1 p.2 < f i}) :=
begin
refine λ s, uniformity_basis_dist.mem_iff.trans _,
split,
{ rintros ⟨ε, ε₀, hε⟩,
obtain ⟨i, hi, H⟩ : ∃ i (hi : p i), f i ≤ ε, from hf ε₀,
exact ⟨i, hi, λ x (hx : _ < _), hε $ lt_of_lt_of_le hx H⟩ },
{ exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, H⟩ }
end
theorem uniformity_basis_dist_inv_nat_succ :
(𝓤 α).has_basis (λ _, true) (λ n:ℕ, {p:α×α | dist p.1 p.2 < 1 / (↑n+1) }) :=
metric.mk_uniformity_basis (λ n _, div_pos zero_lt_one $ nat.cast_add_one_pos n)
(λ ε ε0, (exists_nat_one_div_lt ε0).imp $ λ n hn, ⟨trivial, le_of_lt hn⟩)
theorem uniformity_basis_dist_inv_nat_pos :
(𝓤 α).has_basis (λ n:ℕ, 0<n) (λ n:ℕ, {p:α×α | dist p.1 p.2 < 1 / ↑n }) :=
metric.mk_uniformity_basis (λ n hn, div_pos zero_lt_one $ nat.cast_pos.2 hn)
(λ ε ε0, let ⟨n, hn⟩ := exists_nat_one_div_lt ε0 in ⟨n+1, nat.succ_pos n, le_of_lt hn⟩)
/-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then closed neighborhoods of the diagonal of sizes `{f i | p i}`
form a basis of `𝓤 α`.
Currently we have only one specific basis `uniformity_basis_dist_le` based on this constructor.
More can be easily added if needed in the future. -/
protected theorem mk_uniformity_basis_le {β : Type*} {p : β → Prop} {f : β → ℝ}
(hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) :
(𝓤 α).has_basis p (λ x, {p:α×α | dist p.1 p.2 ≤ f x}) :=
begin
refine λ s, uniformity_basis_dist.mem_iff.trans _,
split,
{ rintros ⟨ε, ε₀, hε⟩,
rcases dense ε₀ with ⟨ε', hε'⟩,
rcases hf ε' hε'.1 with ⟨i, hi, H⟩,
exact ⟨i, hi, λ x (hx : _ ≤ _), hε $ lt_of_le_of_lt (le_trans hx H) hε'.2⟩ },
{ exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, λ x (hx : _ < _), H (le_of_lt hx)⟩ }
end
/-- Contant size closed neighborhoods of the diagonal form a basis
of the uniformity filter. -/
theorem uniformity_basis_dist_le :
(𝓤 α).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {p:α×α | dist p.1 p.2 ≤ ε}) :=
metric.mk_uniformity_basis_le (λ _, id) (λ ε ε₀, ⟨ε, ε₀, le_refl ε⟩)
theorem mem_uniformity_dist {s : set (α×α)} :
s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, dist a b < ε → (a, b) ∈ s) :=
uniformity_basis_dist.mem_uniformity_iff
/-- A constant size neighborhood of the diagonal is an entourage. -/
theorem dist_mem_uniformity {ε:ℝ} (ε0 : 0 < ε) :
{p:α×α | dist p.1 p.2 < ε} ∈ 𝓤 α :=
mem_uniformity_dist.2 ⟨ε, ε0, λ a b, id⟩
theorem uniform_continuous_iff [metric_space β] {f : α → β} :
uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0,
∀{a b:α}, dist a b < δ → dist (f a) (f b) < ε :=
uniformity_basis_dist.uniform_continuous_iff uniformity_basis_dist
theorem uniform_embedding_iff [metric_space β] {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧
∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ :=
uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl
⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (dist_mem_uniformity δ0),
⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 tu in
⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩,
λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in
⟨_, dist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩
/-- A map between metric spaces is a uniform embedding if and only if the distance between `f x`
and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/
theorem uniform_embedding_iff' [metric_space β] {f : α → β} :
uniform_embedding f ↔
(∀ ε > 0, ∃ δ > 0, ∀ {a b : α}, dist a b < δ → dist (f a) (f b) < ε) ∧
(∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ) :=
begin
split,
{ assume h,
exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1,
(uniform_embedding_iff.1 h).2.2⟩ },
{ rintros ⟨h₁, h₂⟩,
refine uniform_embedding_iff.2 ⟨_, uniform_continuous_iff.2 h₁, h₂⟩,
assume x y hxy,
have : dist x y ≤ 0,
{ refine le_of_forall_lt' (λδ δpos, _),
rcases h₂ δ δpos with ⟨ε, εpos, hε⟩,
have : dist (f x) (f y) < ε, by simpa [hxy],
exact hε this },
simpa using this }
end
theorem totally_bounded_iff {s : set α} :
totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε :=
⟨λ H ε ε0, H _ (dist_mem_uniformity ε0),
λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru,
⟨t, ft, h⟩ := H ε ε0 in
⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩
/-- A metric space space is totally bounded if one can reconstruct up to any ε>0 any element of the
space from finitely many data. -/
lemma totally_bounded_of_finite_discretization {α : Type u} [metric_space α] {s : set α}
(H : ∀ε > (0 : ℝ), ∃ (β : Type u) [fintype β] (F : s → β),
∀x y, F x = F y → dist (x:α) y < ε) :
totally_bounded s :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ rw hs, exact totally_bounded_empty },
rcases hs with ⟨x0, hx0⟩,
haveI : inhabited s := ⟨⟨x0, hx0⟩⟩,
refine totally_bounded_iff.2 (λ ε ε0, _),
rcases H ε ε0 with ⟨β, fβ, F, hF⟩,
let Finv := function.inv_fun F,
refine ⟨range (subtype.val ∘ Finv), finite_range _, λ x xs, _⟩,
let x' := Finv (F ⟨x, xs⟩),
have : F x' = F ⟨x, xs⟩ := function.inv_fun_eq ⟨⟨x, xs⟩, rfl⟩,
simp only [set.mem_Union, set.mem_range],
exact ⟨_, ⟨F ⟨x, xs⟩, rfl⟩, hF _ _ this.symm⟩
end
/-- Expressing locally uniform convergence on a set using `dist`. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma tendsto_locally_uniformly_on_iff {ι : Type*} [topological_space β]
{F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} :
tendsto_locally_uniformly_on F f p s ↔
∀ ε > 0, ∀ x ∈ s, ∃ t ∈ nhds_within x s, ∀ᶠ n in p, ∀ y ∈ t, dist (f y) (F n y) < ε :=
begin
refine ⟨λ H ε hε, H _ (dist_mem_uniformity hε), λ H u hu x hx, _⟩,
rcases mem_uniformity_dist.1 hu with ⟨ε, εpos, hε⟩,
rcases H ε εpos x hx with ⟨t, ht, Ht⟩,
exact ⟨t, ht, Ht.mono (λ n hs x hx, hε (hs x hx))⟩
end
/-- Expressing uniform convergence on a set using `dist`. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma tendsto_uniformly_on_iff {ι : Type*}
{F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} :
tendsto_uniformly_on F f p s ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x ∈ s, dist (f x) (F n x) < ε :=
begin
refine ⟨λ H ε hε, H _ (dist_mem_uniformity hε), λ H u hu, _⟩,
rcases mem_uniformity_dist.1 hu with ⟨ε, εpos, hε⟩,
exact (H ε εpos).mono (λ n hs x hx, hε (hs x hx))
end
/-- Expressing locally uniform convergence using `dist`. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma tendsto_locally_uniformly_iff {ι : Type*} [topological_space β]
{F : ι → β → α} {f : β → α} {p : filter ι} :
tendsto_locally_uniformly F f p ↔
∀ ε > 0, ∀ (x : β), ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, dist (f y) (F n y) < ε :=
by simp [← nhds_within_univ, ← tendsto_locally_uniformly_on_univ, tendsto_locally_uniformly_on_iff]
/-- Expressing uniform convergence using `dist`. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma tendsto_uniformly_iff {ι : Type*}
{F : ι → β → α} {f : β → α} {p : filter ι} :
tendsto_uniformly F f p ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x, dist (f x) (F n x) < ε :=
by { rw [← tendsto_uniformly_on_univ, tendsto_uniformly_on_iff], simp }
protected lemma cauchy_iff {f : filter α} :
cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, dist x y < ε :=
uniformity_basis_dist.cauchy_iff
theorem nhds_basis_ball : (𝓝 x).has_basis (λ ε:ℝ, 0 < ε) (ball x) :=
nhds_basis_uniformity uniformity_basis_dist
theorem mem_nhds_iff : s ∈ 𝓝 x ↔ ∃ε>0, ball x ε ⊆ s :=
nhds_basis_ball.mem_iff
theorem nhds_basis_closed_ball : (𝓝 x).has_basis (λ ε:ℝ, 0 < ε) (closed_ball x) :=
nhds_basis_uniformity uniformity_basis_dist_le
theorem nhds_basis_ball_inv_nat_succ :
(𝓝 x).has_basis (λ _, true) (λ n:ℕ, ball x (1 / (↑n+1))) :=
nhds_basis_uniformity uniformity_basis_dist_inv_nat_succ
theorem nhds_basis_ball_inv_nat_pos :
(𝓝 x).has_basis (λ n, 0<n) (λ n:ℕ, ball x (1 / ↑n)) :=
nhds_basis_uniformity uniformity_basis_dist_inv_nat_pos
theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s :=
by simp only [is_open_iff_nhds, mem_nhds_iff, le_principal_iff]
theorem is_open_ball : is_open (ball x ε) :=
is_open_iff.2 $ λ y, exists_ball_subset_ball
theorem ball_mem_nhds (x : α) {ε : ℝ} (ε0 : 0 < ε) : ball x ε ∈ 𝓝 x :=
mem_nhds_sets is_open_ball (mem_ball_self ε0)
theorem nhds_within_basis_ball {s : set α} :
(nhds_within x s).has_basis (λ ε:ℝ, 0 < ε) (λ ε, ball x ε ∩ s) :=
nhds_within_has_basis nhds_basis_ball s
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem mem_nhds_within_iff {t : set α} : s ∈ nhds_within x t ↔ ∃ε>0, ball x ε ∩ t ⊆ s :=
nhds_within_basis_ball.mem_iff
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem tendsto_nhds_within_nhds_within [metric_space β] {t : set β} {f : α → β} {a b} :
tendsto f (nhds_within a s) (nhds_within b t) ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → f x ∈ t ∧ dist (f x) b < ε :=
(nhds_within_basis_ball.tendsto_iff nhds_within_basis_ball).trans $
by simp only [inter_comm, mem_inter_iff, and_imp, mem_ball]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem tendsto_nhds_within_nhds [metric_space β] {f : α → β} {a b} :
tendsto f (nhds_within a s) (𝓝 b) ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → dist (f x) b < ε :=
by { rw [← nhds_within_univ, tendsto_nhds_within_nhds_within],
simp only [mem_univ, true_and] }
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem tendsto_nhds_nhds [metric_space β] {f : α → β} {a b} :
tendsto f (𝓝 a) (𝓝 b) ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, dist x a < δ → dist (f x) b < ε :=
nhds_basis_ball.tendsto_iff nhds_basis_ball
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_at_iff [metric_space β] {f : α → β} {a : α} :
continuous_at f a ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, dist x a < δ → dist (f x) (f a) < ε :=
by rw [continuous_at, tendsto_nhds_nhds]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_within_at_iff [metric_space β] {f : α → β} {a : α} {s : set α} :
continuous_within_at f s a ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → dist (f x) (f a) < ε :=
by rw [continuous_within_at, tendsto_nhds_within_nhds]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_on_iff [metric_space β] {f : α → β} {s : set α} :
continuous_on f s ↔
∀ (b ∈ s) (ε > 0), ∃ δ > 0, ∀a ∈ s, dist a b < δ → dist (f a) (f b) < ε :=
by simp [continuous_on, continuous_within_at_iff]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_iff [metric_space β] {f : α → β} :
continuous f ↔
∀b (ε > 0), ∃ δ > 0, ∀a, dist a b < δ → dist (f a) (f b) < ε :=
continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_nhds
theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} :
tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, dist (u x) a < ε :=
nhds_basis_ball.tendsto_right_iff
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_at_iff' [topological_space β] {f : β → α} {b : β} :
continuous_at f b ↔
∀ ε > 0, ∀ᶠ x in 𝓝 b, dist (f x) (f b) < ε :=
by rw [continuous_at, tendsto_nhds]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_within_at_iff' [topological_space β] {f : β → α} {b : β} {s : set β} :
continuous_within_at f s b ↔
∀ ε > 0, ∀ᶠ x in nhds_within b s, dist (f x) (f b) < ε :=
by rw [continuous_within_at, tendsto_nhds]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_on_iff' [topological_space β] {f : β → α} {s : set β} :
continuous_on f s ↔
∀ (b ∈ s) (ε > 0), ∀ᶠ x in nhds_within b s, dist (f x) (f b) < ε :=
by simp [continuous_on, continuous_within_at_iff']
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_iff' [topological_space β] {f : β → α} :
continuous f ↔ ∀a (ε > 0), ∀ᶠ x in 𝓝 a, dist (f x) (f a) < ε :=
continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds
theorem tendsto_at_top [nonempty β] [semilattice_sup β] {u : β → α} {a : α} :
tendsto u at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, dist (u n) a < ε :=
(at_top_basis.tendsto_iff nhds_basis_ball).trans $
by { simp only [exists_prop, true_and], refl }
end metric
open metric
@[priority 100] -- see Note [lower instance priority]
instance metric_space.to_separated : separated α :=
separated_def.2 $ λ x y h, eq_of_forall_dist_le $
λ ε ε0, le_of_lt (h _ (dist_mem_uniformity ε0))
/-Instantiate a metric space as an emetric space. Before we can state the instance,
we need to show that the uniform structure coming from the edistance and the
distance coincide. -/
/-- Expressing the uniformity in terms of `edist` -/
protected lemma metric.uniformity_basis_edist :
(𝓤 α).has_basis (λ ε:ennreal, 0 < ε) (λ ε, {p | edist p.1 p.2 < ε}) :=
begin
intro t,
refine mem_uniformity_dist.trans ⟨_, _⟩; rintro ⟨ε, ε0, Hε⟩,
{ use [ennreal.of_real ε, ennreal.of_real_pos.2 ε0],
rintros ⟨a, b⟩,
simp only [edist_dist, ennreal.of_real_lt_of_real_iff ε0],
exact Hε },
{ rcases ennreal.lt_iff_exists_real_btwn.1 ε0 with ⟨ε', _, ε0', hε⟩,
rw [ennreal.of_real_pos] at ε0',
refine ⟨ε', ε0', λ a b h, Hε (lt_trans _ hε)⟩,
rwa [edist_dist, ennreal.of_real_lt_of_real_iff ε0'] }
end
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem metric.uniformity_edist : 𝓤 α = (⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε}) :=
metric.uniformity_basis_edist.eq_binfi
/-- A metric space induces an emetric space -/
@[priority 100] -- see Note [lower instance priority]
instance metric_space.to_emetric_space : emetric_space α :=
{ edist := edist,
edist_self := by simp [edist_dist],
eq_of_edist_eq_zero := assume x y h, by simpa [edist_dist] using h,
edist_comm := by simp only [edist_dist, dist_comm]; simp,
edist_triangle := assume x y z, begin
simp only [edist_dist, (ennreal.of_real_add _ _).symm, dist_nonneg],
rw ennreal.of_real_le_of_real_iff _,
{ exact dist_triangle _ _ _ },
{ simpa using add_le_add (dist_nonneg : 0 ≤ dist x y) dist_nonneg }
end,
uniformity_edist := metric.uniformity_edist,
..‹metric_space α› }
/-- Balls defined using the distance or the edistance coincide -/
lemma metric.emetric_ball {x : α} {ε : ℝ} : emetric.ball x (ennreal.of_real ε) = ball x ε :=
begin
ext y,
simp only [emetric.mem_ball, mem_ball, edist_dist],
exact ennreal.of_real_lt_of_real_iff_of_nonneg dist_nonneg
end
/-- Balls defined using the distance or the edistance coincide -/
lemma metric.emetric_ball_nnreal {x : α} {ε : nnreal} : emetric.ball x ε = ball x ε :=
by { convert metric.emetric_ball, simp }
/-- Closed balls defined using the distance or the edistance coincide -/
lemma metric.emetric_closed_ball {x : α} {ε : ℝ} (h : 0 ≤ ε) :
emetric.closed_ball x (ennreal.of_real ε) = closed_ball x ε :=
by ext y; simp [edist_dist]; rw ennreal.of_real_le_of_real_iff h
/-- Closed balls defined using the distance or the edistance coincide -/
lemma metric.emetric_closed_ball_nnreal {x : α} {ε : nnreal} :
emetric.closed_ball x ε = closed_ball x ε :=
by { convert metric.emetric_closed_ball ε.2, simp }
def metric_space.replace_uniformity {α} [U : uniform_space α] (m : metric_space α)
(H : @uniformity _ U = @uniformity _ (metric_space.to_uniform_space α)) :
metric_space α :=
{ dist := @dist _ m.to_has_dist,
dist_self := dist_self,
eq_of_dist_eq_zero := @eq_of_dist_eq_zero _ _,
dist_comm := dist_comm,
dist_triangle := dist_triangle,
edist := edist,
edist_dist := edist_dist,
to_uniform_space := U,
uniformity_dist := H.trans (metric_space.uniformity_dist α) }
/-- One gets a metric space from an emetric space if the edistance
is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the
uniformity are defeq in the metric space and the emetric space. In this definition, the distance
is given separately, to be able to prescribe some expression which is not defeq to the push-forward
of the edistance to reals. -/
def emetric_space.to_metric_space_of_dist {α : Type u} [e : emetric_space α]
(dist : α → α → ℝ)
(edist_ne_top : ∀x y: α, edist x y ≠ ⊤)
(h : ∀x y, dist x y = ennreal.to_real (edist x y)) :
metric_space α :=
let m : metric_space α :=
{ dist := dist,
eq_of_dist_eq_zero := λx y hxy, by simpa [h, ennreal.to_real_eq_zero_iff, edist_ne_top x y] using hxy,
dist_self := λx, by simp [h],
dist_comm := λx y, by simp [h, emetric_space.edist_comm],
dist_triangle := λx y z, begin
simp only [h],
rw [← ennreal.to_real_add (edist_ne_top _ _) (edist_ne_top _ _),
ennreal.to_real_le_to_real (edist_ne_top _ _)],
{ exact edist_triangle _ _ _ },
{ simp [ennreal.add_eq_top, edist_ne_top] }
end,
edist := λx y, edist x y,
edist_dist := λx y, by simp [h, ennreal.of_real_to_real, edist_ne_top] } in
m.replace_uniformity $ by { rw [uniformity_edist, metric.uniformity_edist], refl }
/-- One gets a metric space from an emetric space if the edistance
is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the
uniformity are defeq in the metric space and the emetric space. -/
def emetric_space.to_metric_space {α : Type u} [e : emetric_space α] (h : ∀x y: α, edist x y ≠ ⊤) :
metric_space α :=
emetric_space.to_metric_space_of_dist (λx y, ennreal.to_real (edist x y)) h (λx y, rfl)
/-- A very useful criterion to show that a space is complete is to show that all sequences
which satisfy a bound of the form `dist (u n) (u m) < B N` for all `n m ≥ N` are
converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to
`0`, which makes it possible to use arguments of converging series, while this is impossible
to do in general for arbitrary Cauchy sequences. -/
theorem metric.complete_of_convergent_controlled_sequences (B : ℕ → real) (hB : ∀n, 0 < B n)
(H : ∀u : ℕ → α, (∀N n m : ℕ, N ≤ n → N ≤ m → dist (u n) (u m) < B N) → ∃x, tendsto u at_top (𝓝 x)) :
complete_space α :=
begin
-- this follows from the same criterion in emetric spaces. We just need to translate
-- the convergence assumption from `dist` to `edist`
apply emetric.complete_of_convergent_controlled_sequences (λn, ennreal.of_real (B n)),
{ simp [hB] },
{ assume u Hu,
apply H,
assume N n m hn hm,
rw [← ennreal.of_real_lt_of_real_iff (hB N), ← edist_dist],
exact Hu N n m hn hm }
end
theorem metric.complete_of_cauchy_seq_tendsto :
(∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) → complete_space α :=
emetric.complete_of_cauchy_seq_tendsto
section real
/-- Instantiate the reals as a metric space. -/
instance real.metric_space : metric_space ℝ :=
{ dist := λx y, abs (x - y),
dist_self := by simp [abs_zero],
eq_of_dist_eq_zero := by simp [sub_eq_zero],
dist_comm := assume x y, abs_sub _ _,
dist_triangle := assume x y z, abs_sub_le _ _ _ }
theorem real.dist_eq (x y : ℝ) : dist x y = abs (x - y) := rfl
theorem real.dist_0_eq_abs (x : ℝ) : dist x 0 = abs x :=
by simp [real.dist_eq]
instance : order_topology ℝ :=
order_topology_of_nhds_abs $ λ x, begin
simp only [show ∀ r, {b : ℝ | abs (x - b) < r} = ball x r,
by simp [-sub_eq_add_neg, abs_sub, ball, real.dist_eq]],
apply le_antisymm,
{ simp [le_infi_iff],
exact λ ε ε0, mem_nhds_sets (is_open_ball) (mem_ball_self ε0) },
{ intros s h,
rcases mem_nhds_iff.1 h with ⟨ε, ε0, ss⟩,
exact mem_infi_sets _ (mem_infi_sets ε0 (mem_principal_sets.2 ss)) },
end
lemma closed_ball_Icc {x r : ℝ} : closed_ball x r = Icc (x-r) (x+r) :=
by ext y; rw [mem_closed_ball, dist_comm, real.dist_eq,
abs_sub_le_iff, mem_Icc, ← sub_le_iff_le_add', sub_le]
/-- Special case of the sandwich theorem; see `tendsto_of_tendsto_of_tendsto_of_le_of_le`
and `tendsto_of_tendsto_of_tendsto_of_le_of_le'` for the general case. -/
lemma squeeze_zero {α} {f g : α → ℝ} {t₀ : filter α} (hf : ∀t, 0 ≤ f t) (hft : ∀t, f t ≤ g t)
(g0 : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) :=
tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds g0 hf hft
theorem metric.uniformity_eq_comap_nhds_zero :
𝓤 α = comap (λp:α×α, dist p.1 p.2) (𝓝 (0 : ℝ)) :=
by { ext s,
simp [mem_uniformity_dist, (nhds_basis_ball.comap _).mem_iff, subset_def, real.dist_0_eq_abs] }
lemma cauchy_seq_iff_tendsto_dist_at_top_0 [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ tendsto (λ (n : β × β), dist (u n.1) (u n.2)) at_top (𝓝 0) :=
by rw [cauchy_seq_iff_tendsto, metric.uniformity_eq_comap_nhds_zero, tendsto_comap_iff,
prod.map_def]
end real
section cauchy_seq
variables [nonempty β] [semilattice_sup β]
/-- In a metric space, Cauchy sequences are characterized by the fact that, eventually,
the distance between its elements is arbitrarily small -/
theorem metric.cauchy_seq_iff {u : β → α} :
cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, dist (u m) (u n) < ε :=
uniformity_basis_dist.cauchy_seq_iff
/-- A variation around the metric characterization of Cauchy sequences -/
theorem metric.cauchy_seq_iff' {u : β → α} :
cauchy_seq u ↔ ∀ε>0, ∃N, ∀n≥N, dist (u n) (u N) < ε :=
uniformity_basis_dist.cauchy_seq_iff'
/-- If the distance between `s n` and `s m`, `n, m ≥ N` is bounded above by `b N`
and `b` converges to zero, then `s` is a Cauchy sequence. -/
lemma cauchy_seq_of_le_tendsto_0 {s : β → α} (b : β → ℝ)
(h : ∀ n m N : β, N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) (h₀ : tendsto b at_top (nhds 0)) :
cauchy_seq s :=
metric.cauchy_seq_iff.2 $ λ ε ε0,
(metric.tendsto_at_top.1 h₀ ε ε0).imp $ λ N hN m n hm hn,
calc dist (s m) (s n) ≤ b N : h m n N hm hn
... ≤ abs (b N) : le_abs_self _
... = dist (b N) 0 : by rw real.dist_0_eq_abs; refl
... < ε : (hN _ (le_refl N))
/-- A Cauchy sequence on the natural numbers is bounded. -/
theorem cauchy_seq_bdd {u : ℕ → α} (hu : cauchy_seq u) :
∃ R > 0, ∀ m n, dist (u m) (u n) < R :=
begin
rcases metric.cauchy_seq_iff'.1 hu 1 zero_lt_one with ⟨N, hN⟩,
suffices : ∃ R > 0, ∀ n, dist (u n) (u N) < R,
{ rcases this with ⟨R, R0, H⟩,
exact ⟨_, add_pos R0 R0, λ m n,
lt_of_le_of_lt (dist_triangle_right _ _ _) (add_lt_add (H m) (H n))⟩ },
let R := finset.sup (finset.range N) (λ n, nndist (u n) (u N)),
refine ⟨↑R + 1, add_pos_of_nonneg_of_pos R.2 zero_lt_one, λ n, _⟩,
cases le_or_lt N n,
{ exact lt_of_lt_of_le (hN _ h) (le_add_of_nonneg_left R.2) },
{ have : _ ≤ R := finset.le_sup (finset.mem_range.2 h),
exact lt_of_le_of_lt this (lt_add_of_pos_right _ zero_lt_one) }
end
/-- Yet another metric characterization of Cauchy sequences on integers. This one is often the
most efficient. -/
lemma cauchy_seq_iff_le_tendsto_0 {s : ℕ → α} : cauchy_seq s ↔ ∃ b : ℕ → ℝ,
(∀ n, 0 ≤ b n) ∧
(∀ n m N : ℕ, N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) ∧
tendsto b at_top (𝓝 0) :=
⟨λ hs, begin
/- `s` is a Cauchy sequence. The sequence `b` will be constructed by taking
the supremum of the distances between `s n` and `s m` for `n m ≥ N`.
First, we prove that all these distances are bounded, as otherwise the Sup
would not make sense. -/
let S := λ N, (λ(p : ℕ × ℕ), dist (s p.1) (s p.2)) '' {p | p.1 ≥ N ∧ p.2 ≥ N},
have hS : ∀ N, ∃ x, ∀ y ∈ S N, y ≤ x,
{ rcases cauchy_seq_bdd hs with ⟨R, R0, hR⟩,
refine λ N, ⟨R, _⟩, rintro _ ⟨⟨m, n⟩, _, rfl⟩,
exact le_of_lt (hR m n) },
have bdd : bdd_above (range (λ(p : ℕ × ℕ), dist (s p.1) (s p.2))),
{ rcases cauchy_seq_bdd hs with ⟨R, R0, hR⟩,
use R, rintro _ ⟨⟨m, n⟩, rfl⟩, exact le_of_lt (hR m n) },
-- Prove that it bounds the distances of points in the Cauchy sequence
have ub : ∀ m n N, N ≤ m → N ≤ n → dist (s m) (s n) ≤ Sup (S N) :=
λ m n N hm hn, real.le_Sup _ (hS N) ⟨⟨_, _⟩, ⟨hm, hn⟩, rfl⟩,
have S0m : ∀ n, (0:ℝ) ∈ S n := λ n, ⟨⟨n, n⟩, ⟨le_refl _, le_refl _⟩, dist_self _⟩,
have S0 := λ n, real.le_Sup _ (hS n) (S0m n),
-- Prove that it tends to `0`, by using the Cauchy property of `s`
refine ⟨λ N, Sup (S N), S0, ub, metric.tendsto_at_top.2 (λ ε ε0, _)⟩,
refine (metric.cauchy_seq_iff.1 hs (ε/2) (half_pos ε0)).imp (λ N hN n hn, _),
rw [real.dist_0_eq_abs, abs_of_nonneg (S0 n)],
refine lt_of_le_of_lt (real.Sup_le_ub _ ⟨_, S0m _⟩ _) (half_lt_self ε0),
rintro _ ⟨⟨m', n'⟩, ⟨hm', hn'⟩, rfl⟩,
exact le_of_lt (hN _ _ (le_trans hn hm') (le_trans hn hn'))
end,
λ ⟨b, _, b_bound, b_lim⟩, cauchy_seq_of_le_tendsto_0 b b_bound b_lim⟩
end cauchy_seq
def metric_space.induced {α β} (f : α → β) (hf : function.injective f)
(m : metric_space β) : metric_space α :=
{ dist := λ x y, dist (f x) (f y),
dist_self := λ x, dist_self _,
eq_of_dist_eq_zero := λ x y h, hf (dist_eq_zero.1 h),
dist_comm := λ x y, dist_comm _ _,
dist_triangle := λ x y z, dist_triangle _ _ _,
edist := λ x y, edist (f x) (f y),
edist_dist := λ x y, edist_dist _ _,
to_uniform_space := uniform_space.comap f m.to_uniform_space,
uniformity_dist := begin
apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, dist (f x) (f y)),
refine λ s, mem_comap_sets.trans _,
split; intro H,
{ rcases H with ⟨r, ru, rs⟩,
rcases mem_uniformity_dist.1 ru with ⟨ε, ε0, hε⟩,
refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h },
{ rcases H with ⟨ε, ε0, hε⟩,
exact ⟨_, dist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ }
end }
instance subtype.metric_space {α : Type*} {p : α → Prop} [t : metric_space α] :
metric_space (subtype p) :=
metric_space.induced coe (λ x y, subtype.coe_ext.2) t
theorem subtype.dist_eq {p : α → Prop} (x y : subtype p) : dist x y = dist (x : α) y := rfl
section nnreal
instance : metric_space nnreal := by unfold nnreal; apply_instance
lemma nnreal.dist_eq (a b : nnreal) : dist a b = abs ((a:ℝ) - b) := rfl
lemma nnreal.nndist_eq (a b : nnreal) :
nndist a b = max (a - b) (b - a) :=
begin
wlog h : a ≤ b,
{ apply nnreal.coe_eq.1,
rw [nnreal.sub_eq_zero h, max_eq_right (zero_le $ b - a), ← dist_nndist, nnreal.dist_eq,
nnreal.coe_sub h, abs, neg_sub],
apply max_eq_right,
linarith [nnreal.coe_le_coe.2 h] },
rwa [nndist_comm, max_comm]
end
end nnreal
section prod
instance prod.metric_space_max [metric_space β] : metric_space (α × β) :=
{ dist := λ x y, max (dist x.1 y.1) (dist x.2 y.2),
dist_self := λ x, by simp,
eq_of_dist_eq_zero := λ x y h, begin
cases max_le_iff.1 (le_of_eq h) with h₁ h₂,
exact prod.ext_iff.2 ⟨dist_le_zero.1 h₁, dist_le_zero.1 h₂⟩
end,
dist_comm := λ x y, by simp [dist_comm],
dist_triangle := λ x y z, max_le
(le_trans (dist_triangle _ _ _) (add_le_add (le_max_left _ _) (le_max_left _ _)))
(le_trans (dist_triangle _ _ _) (add_le_add (le_max_right _ _) (le_max_right _ _))),
edist := λ x y, max (edist x.1 y.1) (edist x.2 y.2),
edist_dist := assume x y, begin
have : monotone ennreal.of_real := assume x y h, ennreal.of_real_le_of_real h,
rw [edist_dist, edist_dist, this.map_max.symm]
end,
uniformity_dist := begin
refine uniformity_prod.trans _,
simp only [uniformity_basis_dist.eq_binfi, comap_infi],
rw ← infi_inf_eq, congr, funext,
rw ← infi_inf_eq, congr, funext,
simp [inf_principal, ext_iff, max_lt_iff]
end,
to_uniform_space := prod.uniform_space }
lemma prod.dist_eq [metric_space β] {x y : α × β} :
dist x y = max (dist x.1 y.1) (dist x.2 y.2) := rfl
end prod
theorem uniform_continuous_dist' : uniform_continuous (λp:α×α, dist p.1 p.2) :=
metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε/2, half_pos ε0,
begin
suffices,
{ intros p q h, cases p with p₁ p₂, cases q with q₁ q₂,
cases max_lt_iff.1 h with h₁ h₂, clear h,
dsimp at h₁ h₂ ⊢,
rw real.dist_eq,
refine abs_sub_lt_iff.2 ⟨_, _⟩,
{ revert p₁ p₂ q₁ q₂ h₁ h₂, exact this },
{ apply this; rwa dist_comm } },
intros p₁ p₂ q₁ q₂ h₁ h₂,
have := add_lt_add
(abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₁ q₁ p₂) h₁)).1
(abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₂ q₂ q₁) h₂)).1,
rwa [add_halves, dist_comm p₂, sub_add_sub_cancel, dist_comm q₂] at this
end⟩)
theorem uniform_continuous_dist [uniform_space β] {f g : β → α}
(hf : uniform_continuous f) (hg : uniform_continuous g) :
uniform_continuous (λb, dist (f b) (g b)) :=
uniform_continuous_dist'.comp (hf.prod_mk hg)
theorem continuous_dist : continuous (λp:α×α, dist p.1 p.2) :=
uniform_continuous_dist'.continuous
theorem continuous.dist [topological_space β] {f g : β → α}
(hf : continuous f) (hg : continuous g) : continuous (λb, dist (f b) (g b)) :=
continuous_dist.comp (hf.prod_mk hg)
theorem filter.tendsto.dist {f g : β → α} {x : filter β} {a b : α}
(hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) :
tendsto (λx, dist (f x) (g x)) x (𝓝 (dist a b)) :=
(continuous_dist.tendsto (a, b)).comp (hf.prod_mk_nhds hg)
lemma nhds_comap_dist (a : α) : (𝓝 (0 : ℝ)).comap (λa', dist a' a) = 𝓝 a :=
by simp only [@nhds_eq_comap_uniformity α, metric.uniformity_eq_comap_nhds_zero,
comap_comap_comp, (∘), dist_comm]
lemma tendsto_iff_dist_tendsto_zero {f : β → α} {x : filter β} {a : α} :
(tendsto f x (𝓝 a)) ↔ (tendsto (λb, dist (f b) a) x (𝓝 0)) :=
by rw [← nhds_comap_dist a, tendsto_comap_iff]
lemma uniform_continuous_nndist : uniform_continuous (λp:α×α, nndist p.1 p.2) :=
uniform_continuous_subtype_mk uniform_continuous_dist' _
lemma continuous_nndist : continuous (λp:α×α, nndist p.1 p.2) :=
uniform_continuous_nndist.continuous
lemma continuous.nndist [topological_space β] {f g : β → α}
(hf : continuous f) (hg : continuous g) : continuous (λb, nndist (f b) (g b)) :=
continuous_nndist.comp (hf.prod_mk hg)
theorem filter.tendsto.nndist {f g : β → α} {x : filter β} {a b : α}
(hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) :
tendsto (λx, nndist (f x) (g x)) x (𝓝 (nndist a b)) :=
(continuous_nndist.tendsto (a, b)).comp (hf.prod_mk_nhds hg)
namespace metric
variables {x y z : α} {ε ε₁ ε₂ : ℝ} {s : set α}
theorem is_closed_ball : is_closed (closed_ball x ε) :=
is_closed_le (continuous_id.dist continuous_const) continuous_const
/-- ε-characterization of the closure in metric spaces-/
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem mem_closure_iff {α : Type u} [metric_space α] {s : set α} {a : α} :
a ∈ closure s ↔ ∀ε>0, ∃b ∈ s, dist a b < ε :=
(mem_closure_iff_nhds_basis nhds_basis_ball).trans $
by simp only [mem_ball, dist_comm]
lemma mem_closure_range_iff {α : Type u} [metric_space α] {e : β → α} {a : α} :
a ∈ closure (range e) ↔ ∀ε>0, ∃ k : β, dist a (e k) < ε :=
by simp only [mem_closure_iff, exists_range_iff]
lemma mem_closure_range_iff_nat {α : Type u} [metric_space α] {e : β → α} {a : α} :
a ∈ closure (range e) ↔ ∀n : ℕ, ∃ k : β, dist a (e k) < 1 / ((n : ℝ) + 1) :=
(mem_closure_iff_nhds_basis nhds_basis_ball_inv_nat_succ).trans $
by simp only [mem_ball, dist_comm, exists_range_iff, forall_const]
theorem mem_of_closed' {α : Type u} [metric_space α] {s : set α} (hs : is_closed s)
{a : α} : a ∈ s ↔ ∀ε>0, ∃b ∈ s, dist a b < ε :=
by simpa only [closure_eq_of_is_closed hs] using @mem_closure_iff _ _ s a
end metric
section pi
open finset
variables {π : β → Type*} [fintype β] [∀b, metric_space (π b)]
/-- A finite product of metric spaces is a metric space, with the sup distance. -/
instance metric_space_pi : metric_space (Πb, π b) :=
begin
/- we construct the instance from the emetric space instance to avoid checking again that the
uniformity is the same as the product uniformity, but we register nevertheless a nice formula
for the distance -/
refine emetric_space.to_metric_space_of_dist
(λf g, ((sup univ (λb, nndist (f b) (g b)) : nnreal) : ℝ)) _ _,
show ∀ (x y : Π (b : β), π b), edist x y ≠ ⊤,
{ assume x y,
rw ← lt_top_iff_ne_top,
have : (⊥ : ennreal) < ⊤ := ennreal.coe_lt_top,
simp [edist, this],
assume b,
rw lt_top_iff_ne_top,
exact edist_ne_top (x b) (y b) },
show ∀ (x y : Π (b : β), π b), ↑(sup univ (λ (b : β), nndist (x b) (y b))) =
ennreal.to_real (sup univ (λ (b : β), edist (x b) (y b))),
{ assume x y,
have : sup univ (λ (b : β), edist (x b) (y b)) = ↑(sup univ (λ (b : β), nndist (x b) (y b))),
{ simp [edist_nndist],
refine eq.symm (comp_sup_eq_sup_comp _ _ _),
exact (assume x y h, ennreal.coe_le_coe.2 h), refl },
rw this,
refl }
end
lemma dist_pi_def (f g : Πb, π b) :
dist f g = (sup univ (λb, nndist (f b) (g b)) : nnreal) := rfl
lemma dist_pi_lt_iff {f g : Πb, π b} {r : ℝ} (hr : 0 < r) :
dist f g < r ↔ ∀b, dist (f b) (g b) < r :=
begin
lift r to nnreal using le_of_lt hr,
rw_mod_cast [dist_pi_def, finset.sup_lt_iff],
{ simp [nndist], refl },
{ exact hr }
end
lemma dist_pi_le_iff {f g : Πb, π b} {r : ℝ} (hr : 0 ≤ r) :
dist f g ≤ r ↔ ∀b, dist (f b) (g b) ≤ r :=
begin
lift r to nnreal using hr,
rw_mod_cast [dist_pi_def, finset.sup_le_iff],
simp [nndist],
refl
end
/-- An open ball in a product space is a product of open balls. The assumption `0 < r`
is necessary for the case of the empty product. -/
lemma ball_pi (x : Πb, π b) {r : ℝ} (hr : 0 < r) :
ball x r = { y | ∀b, y b ∈ ball (x b) r } :=
by { ext p, simp [dist_pi_lt_iff hr] }
/-- A closed ball in a product space is a product of closed balls. The assumption `0 ≤ r`
is necessary for the case of the empty product. -/
lemma closed_ball_pi (x : Πb, π b) {r : ℝ} (hr : 0 ≤ r) :
closed_ball x r = { y | ∀b, y b ∈ closed_ball (x b) r } :=
by { ext p, simp [dist_pi_le_iff hr] }
end pi
section compact
/-- Any compact set in a metric space can be covered by finitely many balls of a given positive
radius -/
lemma finite_cover_balls_of_compact {α : Type u} [metric_space α] {s : set α}
(hs : compact s) {e : ℝ} (he : 0 < e) :
∃t ⊆ s, finite t ∧ s ⊆ ⋃x∈t, ball x e :=
begin
apply hs.elim_finite_subcover_image,
{ simp [is_open_ball] },
{ intros x xs,
simp,
exact ⟨x, ⟨xs, by simpa⟩⟩ }
end
alias finite_cover_balls_of_compact ← compact.finite_cover_balls
end compact
section proper_space
open metric
/-- A metric space is proper if all closed balls are compact. -/
class proper_space (α : Type u) [metric_space α] : Prop :=
(compact_ball : ∀x:α, ∀r, compact (closed_ball x r))
/-- If all closed balls of large enough radius are compact, then the space is proper. Especially
useful when the lower bound for the radius is 0. -/
lemma proper_space_of_compact_closed_ball_of_le
(R : ℝ) (h : ∀x:α, ∀r, R ≤ r → compact (closed_ball x r)) :
proper_space α :=
⟨begin
assume x r,
by_cases hr : R ≤ r,
{ exact h x r hr },
{ have : closed_ball x r = closed_ball x R ∩ closed_ball x r,
{ symmetry,
apply inter_eq_self_of_subset_right,
exact closed_ball_subset_closed_ball (le_of_lt (not_le.1 hr)) },
rw this,
exact (h x R (le_refl _)).inter_right is_closed_ball }
end⟩
/- A compact metric space is proper -/
@[priority 100] -- see Note [lower instance priority]
instance proper_of_compact [compact_space α] : proper_space α :=
⟨assume x r, compact_of_is_closed_subset compact_univ is_closed_ball (subset_univ _)⟩
/-- A proper space is locally compact -/
@[priority 100] -- see Note [lower instance priority]
instance locally_compact_of_proper [proper_space α] :
locally_compact_space α :=
begin
apply locally_compact_of_compact_nhds,
intros x,
existsi closed_ball x 1,
split,
{ apply mem_nhds_iff.2,
existsi (1 : ℝ),
simp,
exact ⟨zero_lt_one, ball_subset_closed_ball⟩ },
{ apply proper_space.compact_ball }
end
/-- A proper space is complete -/
@[priority 100] -- see Note [lower instance priority]
instance complete_of_proper [proper_space α] : complete_space α :=
⟨begin
intros f hf,
/- We want to show that the Cauchy filter `f` is converging. It suffices to find a closed
ball (therefore compact by properness) where it is nontrivial. -/
have A : ∃ t ∈ f, ∀ x y ∈ t, dist x y < 1 := (metric.cauchy_iff.1 hf).2 1 zero_lt_one,
rcases A with ⟨t, ⟨t_fset, ht⟩⟩,
rcases nonempty_of_mem_sets hf.1 t_fset with ⟨x, xt⟩,
have : t ⊆ closed_ball x 1 := by intros y yt; simp [dist_comm]; apply le_of_lt (ht x y xt yt),
have : closed_ball x 1 ∈ f := f.sets_of_superset t_fset this,
rcases (compact_iff_totally_bounded_complete.1 (proper_space.compact_ball x 1)).2 f hf (le_principal_iff.2 this)
with ⟨y, _, hy⟩,
exact ⟨y, hy⟩
end⟩
/-- A proper metric space is separable, and therefore second countable. Indeed, any ball is
compact, and therefore admits a countable dense subset. Taking a countable union over the balls
centered at a fixed point and with integer radius, one obtains a countable set which is
dense in the whole space. -/
@[priority 100] -- see Note [lower instance priority]
instance second_countable_of_proper [proper_space α] :
second_countable_topology α :=
begin
/- We show that the space admits a countable dense subset. The case where the space is empty
is special, and trivial. -/
have A : (univ : set α) = ∅ → ∃(s : set α), countable s ∧ closure s = (univ : set α) :=
assume H, ⟨∅, ⟨by simp, by simp; exact H.symm⟩⟩,
have B : (univ : set α).nonempty → ∃(s : set α), countable s ∧ closure s = (univ : set α) :=
begin
/- When the space is not empty, we take a point `x` in the space, and then a countable set
`T r` which is dense in the closed ball `closed_ball x r` for each `r`. Then the set
`t = ⋃ T n` (where the union is over all integers `n`) is countable, as a countable union
of countable sets, and dense in the space by construction. -/
rintros ⟨x, x_univ⟩,
choose T a using show ∀ (r:ℝ), ∃ t ⊆ closed_ball x r, (countable (t : set α) ∧ closed_ball x r = closure t),
from assume r, emetric.countable_closure_of_compact (proper_space.compact_ball _ _),
let t := (⋃n:ℕ, T (n : ℝ)),
have T₁ : countable t := by finish [countable_Union],
have T₂ : closure t ⊆ univ := by simp,
have T₃ : univ ⊆ closure t :=
begin
intros y y_univ,
rcases exists_nat_gt (dist y x) with ⟨n, n_large⟩,
have h : y ∈ closed_ball x (n : ℝ) := by simp; apply le_of_lt n_large,
have h' : closed_ball x (n : ℝ) = closure (T (n : ℝ)) := by finish,
have : y ∈ closure (T (n : ℝ)) := by rwa h' at h,
show y ∈ closure t, from mem_of_mem_of_subset this (by apply closure_mono; apply subset_Union (λ(n:ℕ), T (n:ℝ))),
end,
exact ⟨t, ⟨T₁, subset.antisymm T₂ T₃⟩⟩
end,
haveI : separable_space α := ⟨(eq_empty_or_nonempty univ).elim A B⟩,
apply emetric.second_countable_of_separable,
end
/-- A finite product of proper spaces is proper. -/
instance pi_proper_space {π : β → Type*} [fintype β] [∀b, metric_space (π b)]
[h : ∀b, proper_space (π b)] : proper_space (Πb, π b) :=
begin
refine proper_space_of_compact_closed_ball_of_le 0 (λx r hr, _),
rw closed_ball_pi _ hr,
apply compact_pi_infinite (λb, _),
apply (h b).compact_ball
end
end proper_space
namespace metric
section second_countable
open topological_space
/-- A metric space is second countable if, for every ε > 0, there is a countable set which is ε-dense. -/
lemma second_countable_of_almost_dense_set
(H : ∀ε > (0 : ℝ), ∃ s : set α, countable s ∧ (∀x, ∃y ∈ s, dist x y ≤ ε)) :
second_countable_topology α :=
begin
choose T T_dense using H,
have I1 : ∀n:ℕ, (n:ℝ) + 1 > 0 :=
λn, lt_of_lt_of_le zero_lt_one (le_add_of_nonneg_left (nat.cast_nonneg _)),
have I : ∀n:ℕ, (n+1 : ℝ)⁻¹ > 0 := λn, inv_pos.2 (I1 n),
let t := ⋃n:ℕ, T (n+1)⁻¹ (I n),
have count_t : countable t := by finish [countable_Union],
have clos_t : closure t = univ,
{ refine subset.antisymm (subset_univ _) (λx xuniv, mem_closure_iff.2 (λε εpos, _)),
rcases exists_nat_gt ε⁻¹ with ⟨n, hn⟩,
have : ε⁻¹ < n + 1 := lt_of_lt_of_le hn (le_add_of_nonneg_right zero_le_one),
have nε : ((n:ℝ)+1)⁻¹ < ε := (inv_lt (I1 n) εpos).2 this,
rcases (T_dense (n+1)⁻¹ (I n)).2 x with ⟨y, yT, Dxy⟩,
have : y ∈ t := mem_of_mem_of_subset yT (by apply subset_Union (λ (n:ℕ), T (n+1)⁻¹ (I n))),
exact ⟨y, this, lt_of_le_of_lt Dxy nε⟩ },
haveI : separable_space α := ⟨⟨t, ⟨count_t, clos_t⟩⟩⟩,
exact emetric.second_countable_of_separable α
end
/-- A metric space space is second countable if one can reconstruct up to any ε>0 any element of the
space from countably many data. -/
lemma second_countable_of_countable_discretization {α : Type u} [metric_space α]
(H : ∀ε > (0 : ℝ), ∃ (β : Type u) [encodable β] (F : α → β), ∀x y, F x = F y → dist x y ≤ ε) :
second_countable_topology α :=
begin
cases (univ : set α).eq_empty_or_nonempty with hs hs,
{ haveI : compact_space α := ⟨by rw hs; exact compact_empty⟩, by apply_instance },
rcases hs with ⟨x0, hx0⟩,
letI : inhabited α := ⟨x0⟩,
refine second_countable_of_almost_dense_set (λε ε0, _),
rcases H ε ε0 with ⟨β, fβ, F, hF⟩,
let Finv := function.inv_fun F,
refine ⟨range Finv, ⟨countable_range _, λx, _⟩⟩,
let x' := Finv (F x),
have : F x' = F x := function.inv_fun_eq ⟨x, rfl⟩,
exact ⟨x', mem_range_self _, hF _ _ this.symm⟩
end
end second_countable
end metric
lemma lebesgue_number_lemma_of_metric
{s : set α} {ι} {c : ι → set α} (hs : compact s)
(hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) :
∃ δ > 0, ∀ x ∈ s, ∃ i, ball x δ ⊆ c i :=
let ⟨n, en, hn⟩ := lebesgue_number_lemma hs hc₁ hc₂,
⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 en in
⟨δ, δ0, assume x hx, let ⟨i, hi⟩ := hn x hx in
⟨i, assume y hy, hi (hδ (mem_ball'.mp hy))⟩⟩
lemma lebesgue_number_lemma_of_metric_sUnion
{s : set α} {c : set (set α)} (hs : compact s)
(hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) :
∃ δ > 0, ∀ x ∈ s, ∃ t ∈ c, ball x δ ⊆ t :=
by rw sUnion_eq_Union at hc₂;
simpa using lebesgue_number_lemma_of_metric hs (by simpa) hc₂
namespace metric
/-- Boundedness of a subset of a metric space. We formulate the definition to work
even in the empty space. -/
def bounded (s : set α) : Prop :=
∃C, ∀x y ∈ s, dist x y ≤ C
section bounded
variables {x : α} {s t : set α} {r : ℝ}
@[simp] lemma bounded_empty : bounded (∅ : set α) :=
⟨0, by simp⟩
lemma bounded_iff_mem_bounded : bounded s ↔ ∀ x ∈ s, bounded s :=
⟨λ h _ _, h, λ H,
s.eq_empty_or_nonempty.elim
(λ hs, hs.symm ▸ bounded_empty)
(λ ⟨x, hx⟩, H x hx)⟩
/-- Subsets of a bounded set are also bounded -/
lemma bounded.subset (incl : s ⊆ t) : bounded t → bounded s :=
Exists.imp $ λ C hC x y hx hy, hC x y (incl hx) (incl hy)
/-- Closed balls are bounded -/
lemma bounded_closed_ball : bounded (closed_ball x r) :=
⟨r + r, λ y z hy hz, begin
simp only [mem_closed_ball] at *,
calc dist y z ≤ dist y x + dist z x : dist_triangle_right _ _ _
... ≤ r + r : add_le_add hy hz
end⟩
/-- Open balls are bounded -/
lemma bounded_ball : bounded (ball x r) :=
bounded_closed_ball.subset ball_subset_closed_ball
/-- Given a point, a bounded subset is included in some ball around this point -/
lemma bounded_iff_subset_ball (c : α) : bounded s ↔ ∃r, s ⊆ closed_ball c r :=
begin
split; rintro ⟨C, hC⟩,
{ cases s.eq_empty_or_nonempty with h h,
{ subst s, exact ⟨0, by simp⟩ },
{ rcases h with ⟨x, hx⟩,
exact ⟨C + dist x c, λ y hy, calc
dist y c ≤ dist y x + dist x c : dist_triangle _ _ _
... ≤ C + dist x c : add_le_add_right (hC y x hy hx) _⟩ } },
{ exact bounded_closed_ball.subset hC }
end
/-- The union of two bounded sets is bounded iff each of the sets is bounded -/
@[simp] lemma bounded_union :
bounded (s ∪ t) ↔ bounded s ∧ bounded t :=
⟨λh, ⟨h.subset (by simp), h.subset (by simp)⟩,
begin
rintro ⟨hs, ht⟩,
refine bounded_iff_mem_bounded.2 (λ x _, _),
rw bounded_iff_subset_ball x at hs ht ⊢,
rcases hs with ⟨Cs, hCs⟩, rcases ht with ⟨Ct, hCt⟩,
exact ⟨max Cs Ct, union_subset
(subset.trans hCs $ closed_ball_subset_closed_ball $ le_max_left _ _)
(subset.trans hCt $ closed_ball_subset_closed_ball $ le_max_right _ _)⟩,
end⟩
/-- A finite union of bounded sets is bounded -/
lemma bounded_bUnion {I : set β} {s : β → set α} (H : finite I) :
bounded (⋃i∈I, s i) ↔ ∀i ∈ I, bounded (s i) :=
finite.induction_on H (by simp) $ λ x I _ _ IH,
by simp [or_imp_distrib, forall_and_distrib, IH]
/-- A compact set is bounded -/
lemma bounded_of_compact {s : set α} (h : compact s) : bounded s :=
-- We cover the compact set by finitely many balls of radius 1,
-- and then argue that a finite union of bounded sets is bounded
let ⟨t, ht, fint, subs⟩ := finite_cover_balls_of_compact h zero_lt_one in
bounded.subset subs $ (bounded_bUnion fint).2 $ λ i hi, bounded_ball
alias bounded_of_compact ← compact.bounded
/-- A finite set is bounded -/
lemma bounded_of_finite {s : set α} (h : finite s) : bounded s :=
h.compact.bounded
/-- A singleton is bounded -/
lemma bounded_singleton {x : α} : bounded ({x} : set α) :=
bounded_of_finite $ finite_singleton _
/-- Characterization of the boundedness of the range of a function -/
lemma bounded_range_iff {f : β → α} : bounded (range f) ↔ ∃C, ∀x y, dist (f x) (f y) ≤ C :=
exists_congr $ λ C, ⟨
λ H x y, H _ _ ⟨x, rfl⟩ ⟨y, rfl⟩,
by rintro H _ _ ⟨x, rfl⟩ ⟨y, rfl⟩; exact H x y⟩
/-- In a compact space, all sets are bounded -/
lemma bounded_of_compact_space [compact_space α] : bounded s :=
compact_univ.bounded.subset (subset_univ _)
/-- The Heine–Borel theorem:
In a proper space, a set is compact if and only if it is closed and bounded -/
lemma compact_iff_closed_bounded [proper_space α] :
compact s ↔ is_closed s ∧ bounded s :=
⟨λ h, ⟨closed_of_compact _ h, h.bounded⟩, begin
rintro ⟨hc, hb⟩,
cases s.eq_empty_or_nonempty with h h, {simp [h, compact_empty]},
rcases h with ⟨x, hx⟩,
rcases (bounded_iff_subset_ball x).1 hb with ⟨r, hr⟩,
exact compact_of_is_closed_subset (proper_space.compact_ball x r) hc hr
end⟩
/-- The image of a proper space under an expanding onto map is proper. -/
lemma proper_image_of_proper [proper_space α] [metric_space β] (f : α → β)
(f_cont : continuous f) (hf : range f = univ) (C : ℝ)
(hC : ∀x y, dist x y ≤ C * dist (f x) (f y)) : proper_space β :=
begin
apply proper_space_of_compact_closed_ball_of_le 0 (λx₀ r hr, _),
let K := f ⁻¹' (closed_ball x₀ r),
have A : is_closed K :=
continuous_iff_is_closed.1 f_cont (closed_ball x₀ r) (is_closed_ball),
have B : bounded K := ⟨max C 0 * (r + r), λx y hx hy, calc
dist x y ≤ C * dist (f x) (f y) : hC x y
... ≤ max C 0 * dist (f x) (f y) : mul_le_mul_of_nonneg_right (le_max_left _ _) (dist_nonneg)
... ≤ max C 0 * (dist (f x) x₀ + dist (f y) x₀) :
mul_le_mul_of_nonneg_left (dist_triangle_right (f x) (f y) x₀) (le_max_right _ _)
... ≤ max C 0 * (r + r) : begin
simp only [mem_closed_ball, mem_preimage] at hx hy,
exact mul_le_mul_of_nonneg_left (add_le_add hx hy) (le_max_right _ _)
end⟩,
have : compact K := compact_iff_closed_bounded.2 ⟨A, B⟩,
have C : compact (f '' K) := this.image f_cont,
have : f '' K = closed_ball x₀ r,
by { rw image_preimage_eq_of_subset, rw hf, exact subset_univ _ },
rwa this at C
end
end bounded
section diam
variables {s : set α} {x y z : α}
/-- The diameter of a set in a metric space. To get controllable behavior even when the diameter
should be infinite, we express it in terms of the emetric.diameter -/
def diam (s : set α) : ℝ := ennreal.to_real (emetric.diam s)
/-- The diameter of a set is always nonnegative -/
lemma diam_nonneg : 0 ≤ diam s := ennreal.to_real_nonneg
lemma diam_subsingleton (hs : s.subsingleton) : diam s = 0 :=
by simp only [diam, emetric.diam_subsingleton hs, ennreal.zero_to_real]
/-- The empty set has zero diameter -/
@[simp] lemma diam_empty : diam (∅ : set α) = 0 :=
diam_subsingleton subsingleton_empty
/-- A singleton has zero diameter -/
@[simp] lemma diam_singleton : diam ({x} : set α) = 0 :=
diam_subsingleton subsingleton_singleton
-- Does not work as a simp-lemma, since {x, y} reduces to (insert y {x})
lemma diam_pair : diam ({x, y} : set α) = dist x y :=
by simp only [diam, emetric.diam_pair, dist_edist]
-- Does not work as a simp-lemma, since {x, y, z} reduces to (insert z (insert y {x}))
lemma diam_triple :
metric.diam ({x, y, z} : set α) = max (dist x y) (max (dist y z) (dist x z)) :=
begin
simp only [metric.diam, emetric.diam_triple, dist_edist],
rw [ennreal.to_real_max, ennreal.to_real_max];
apply_rules [ne_of_lt, edist_lt_top, max_lt]
end
/-- If the distance between any two points in a set is bounded by some constant `C`,
then `ennreal.of_real C` bounds the emetric diameter of this set. -/
lemma ediam_le_of_forall_dist_le {C : ℝ} (h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) :
emetric.diam s ≤ ennreal.of_real C :=
emetric.diam_le_of_forall_edist_le $
λ x hx y hy, (edist_dist x y).symm ▸ ennreal.of_real_le_of_real (h x hx y hy)
/-- If the distance between any two points in a set is bounded by some non-negative constant,
this constant bounds the diameter. -/
lemma diam_le_of_forall_dist_le {C : ℝ} (h₀ : 0 ≤ C) (h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) :
diam s ≤ C :=
ennreal.to_real_le_of_le_of_real h₀ (ediam_le_of_forall_dist_le h)
/-- If the distance between any two points in a nonempty set is bounded by some constant,
this constant bounds the diameter. -/
lemma diam_le_of_forall_dist_le_of_nonempty (hs : s.nonempty) {C : ℝ}
(h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) : diam s ≤ C :=
have h₀ : 0 ≤ C, from let ⟨x, hx⟩ := hs in le_trans dist_nonneg (h x hx x hx),
diam_le_of_forall_dist_le h₀ h
/-- The distance between two points in a set is controlled by the diameter of the set. -/
lemma dist_le_diam_of_mem' (h : emetric.diam s ≠ ⊤) (hx : x ∈ s) (hy : y ∈ s) :
dist x y ≤ diam s :=
begin
rw [diam, dist_edist],
rw ennreal.to_real_le_to_real (edist_ne_top _ _) h,
exact emetric.edist_le_diam_of_mem hx hy
end
/-- Characterize the boundedness of a set in terms of the finiteness of its emetric.diameter. -/
lemma bounded_iff_ediam_ne_top : bounded s ↔ emetric.diam s ≠ ⊤ :=
iff.intro
(λ ⟨C, hC⟩, ne_top_of_le_ne_top ennreal.of_real_ne_top
(ediam_le_of_forall_dist_le $ λ x hx y hy, hC x y hx hy))
(λ h, ⟨diam s, λ x y hx hy, dist_le_diam_of_mem' h hx hy⟩)
lemma bounded.ediam_ne_top (h : bounded s) : emetric.diam s ≠ ⊤ :=
bounded_iff_ediam_ne_top.1 h
/-- The distance between two points in a set is controlled by the diameter of the set. -/
lemma dist_le_diam_of_mem (h : bounded s) (hx : x ∈ s) (hy : y ∈ s) : dist x y ≤ diam s :=
dist_le_diam_of_mem' h.ediam_ne_top hx hy
/-- An unbounded set has zero diameter. If you would prefer to get the value ∞, use `emetric.diam`.
This lemma makes it possible to avoid side conditions in some situations -/
lemma diam_eq_zero_of_unbounded (h : ¬(bounded s)) : diam s = 0 :=
begin
simp only [bounded_iff_ediam_ne_top, not_not, ne.def] at h,
simp [diam, h]
end
/-- If `s ⊆ t`, then the diameter of `s` is bounded by that of `t`, provided `t` is bounded. -/
lemma diam_mono {s t : set α} (h : s ⊆ t) (ht : bounded t) : diam s ≤ diam t :=
begin
unfold diam,
rw ennreal.to_real_le_to_real (bounded.subset h ht).ediam_ne_top ht.ediam_ne_top,
exact emetric.diam_mono h
end
/-- The diameter of a union is controlled by the sum of the diameters, and the distance between
any two points in each of the sets. This lemma is true without any side condition, since it is
obviously true if `s ∪ t` is unbounded. -/
lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + dist x y + diam t :=
begin
classical, by_cases H : bounded (s ∪ t),
{ have hs : bounded s, from H.subset (subset_union_left _ _),
have ht : bounded t, from H.subset (subset_union_right _ _),
rw [bounded_iff_ediam_ne_top] at H hs ht,
rw [dist_edist, diam, diam, diam, ← ennreal.to_real_add, ← ennreal.to_real_add,
ennreal.to_real_le_to_real];
repeat { apply ennreal.add_ne_top.2; split }; try { assumption };
try { apply edist_ne_top },
exact emetric.diam_union xs yt },
{ rw [diam_eq_zero_of_unbounded H],
apply_rules [add_nonneg, diam_nonneg, dist_nonneg] }
end
/-- If two sets intersect, the diameter of the union is bounded by the sum of the diameters. -/
lemma diam_union' {t : set α} (h : (s ∩ t).nonempty) : diam (s ∪ t) ≤ diam s + diam t :=
begin
rcases h with ⟨x, ⟨xs, xt⟩⟩,
simpa using diam_union xs xt
end
/-- The diameter of a closed ball of radius `r` is at most `2 r`. -/
lemma diam_closed_ball {r : ℝ} (h : r ≥ 0) : diam (closed_ball x r) ≤ 2 * r :=
diam_le_of_forall_dist_le (mul_nonneg (le_of_lt two_pos) h) $ λa ha b hb, calc
dist a b ≤ dist a x + dist b x : dist_triangle_right _ _ _
... ≤ r + r : add_le_add ha hb
... = 2 * r : by simp [mul_two, mul_comm]
/-- The diameter of a ball of radius `r` is at most `2 r`. -/
lemma diam_ball {r : ℝ} (h : r ≥ 0) : diam (ball x r) ≤ 2 * r :=
le_trans (diam_mono ball_subset_closed_ball bounded_closed_ball) (diam_closed_ball h)
end diam
end metric
|
20b6ec09778eb7c9731ef2bba12c876f0ce5a8ec | 4727251e0cd73359b15b664c3170e5d754078599 | /src/linear_algebra/isomorphisms.lean | 1efb9e39a16a536902ad06ceb143f08b38f797c4 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 5,879 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov
-/
import linear_algebra.quotient
/-!
# Isomorphism theorems for modules.
* The Noether's first, second, and third isomorphism theorems for modules are proved as
`linear_map.quot_ker_equiv_range`, `linear_map.quotient_inf_equiv_sup_quotient` and
`submodule.quotient_quotient_equiv_quotient`.
-/
universes u v
variables {R M M₂ M₃ : Type*}
variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃]
variables [module R M] [module R M₂] [module R M₃]
variables (f : M →ₗ[R] M₂)
/-! The first and second isomorphism theorems for modules. -/
namespace linear_map
open submodule
section isomorphism_laws
/-- The first isomorphism law for modules. The quotient of `M` by the kernel of `f` is linearly
equivalent to the range of `f`. -/
noncomputable def quot_ker_equiv_range : (M ⧸ f.ker) ≃ₗ[R] f.range :=
(linear_equiv.of_injective (f.ker.liftq f $ le_rfl) $
ker_eq_bot.mp $ submodule.ker_liftq_eq_bot _ _ _ (le_refl f.ker)).trans
(linear_equiv.of_eq _ _ $ submodule.range_liftq _ _ _)
/-- The first isomorphism theorem for surjective linear maps. -/
noncomputable def quot_ker_equiv_of_surjective
(f : M →ₗ[R] M₂) (hf : function.surjective f) : (M ⧸ f.ker) ≃ₗ[R] M₂ :=
f.quot_ker_equiv_range.trans
(linear_equiv.of_top f.range (linear_map.range_eq_top.2 hf))
@[simp] lemma quot_ker_equiv_range_apply_mk (x : M) :
(f.quot_ker_equiv_range (submodule.quotient.mk x) : M₂) = f x :=
rfl
@[simp] lemma quot_ker_equiv_range_symm_apply_image (x : M) (h : f x ∈ f.range) :
f.quot_ker_equiv_range.symm ⟨f x, h⟩ = f.ker.mkq x :=
f.quot_ker_equiv_range.symm_apply_apply (f.ker.mkq x)
/--
Canonical linear map from the quotient `p/(p ∩ p')` to `(p+p')/p'`, mapping `x + (p ∩ p')`
to `x + p'`, where `p` and `p'` are submodules of an ambient module.
-/
def quotient_inf_to_sup_quotient (p p' : submodule R M) :
p ⧸ (comap p.subtype (p ⊓ p')) →ₗ[R] _ ⧸ (comap (p ⊔ p').subtype p') :=
(comap p.subtype (p ⊓ p')).liftq
((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin
rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype],
exact comap_mono (inf_le_inf_right _ le_sup_left) end
/--
Second Isomorphism Law : the canonical map from `p/(p ∩ p')` to `(p+p')/p'` as a linear isomorphism.
-/
noncomputable def quotient_inf_equiv_sup_quotient (p p' : submodule R M) :
(p ⧸ (comap p.subtype (p ⊓ p'))) ≃ₗ[R] _ ⧸ (comap (p ⊔ p').subtype p') :=
linear_equiv.of_bijective (quotient_inf_to_sup_quotient p p')
begin
rw [← ker_eq_bot, quotient_inf_to_sup_quotient, ker_liftq_eq_bot],
rw [ker_comp, ker_mkq],
exact λ ⟨x, hx1⟩ hx2, ⟨hx1, hx2⟩
end
begin
rw [← range_eq_top, quotient_inf_to_sup_quotient, range_liftq, eq_top_iff'],
rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩,
use [⟨y, hy⟩], apply (submodule.quotient.eq _).2,
change y - (y + z) ∈ p',
rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff]
end
@[simp] lemma coe_quotient_inf_to_sup_quotient (p p' : submodule R M) :
⇑(quotient_inf_to_sup_quotient p p') = quotient_inf_equiv_sup_quotient p p' := rfl
@[simp] lemma quotient_inf_equiv_sup_quotient_apply_mk (p p' : submodule R M) (x : p) :
quotient_inf_equiv_sup_quotient p p' (submodule.quotient.mk x) =
submodule.quotient.mk (of_le (le_sup_left : p ≤ p ⊔ p') x) :=
rfl
lemma quotient_inf_equiv_sup_quotient_symm_apply_left (p p' : submodule R M)
(x : p ⊔ p') (hx : (x:M) ∈ p) :
(quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) =
submodule.quotient.mk ⟨x, hx⟩ :=
(linear_equiv.symm_apply_eq _).2 $ by simp [of_le_apply]
@[simp] lemma quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff {p p' : submodule R M}
{x : p ⊔ p'} :
(quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 ↔ (x:M) ∈ p' :=
(linear_equiv.symm_apply_eq _).trans $ by simp [of_le_apply]
lemma quotient_inf_equiv_sup_quotient_symm_apply_right (p p' : submodule R M) {x : p ⊔ p'}
(hx : (x:M) ∈ p') :
(quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 :=
quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff.2 hx
end isomorphism_laws
end linear_map
/-! The third isomorphism theorem for modules. -/
namespace submodule
variables (S T : submodule R M) (h : S ≤ T)
/-- The map from the third isomorphism theorem for modules: `(M / S) / (T / S) → M / T`. -/
def quotient_quotient_equiv_quotient_aux :
(M ⧸ S) ⧸ (T.map S.mkq) →ₗ[R] M ⧸ T :=
liftq _ (mapq S T linear_map.id h)
(by { rintro _ ⟨x, hx, rfl⟩, rw [linear_map.mem_ker, mkq_apply, mapq_apply],
exact (quotient.mk_eq_zero _).mpr hx })
@[simp] lemma quotient_quotient_equiv_quotient_aux_mk (x : M ⧸ S) :
quotient_quotient_equiv_quotient_aux S T h (quotient.mk x) = mapq S T linear_map.id h x :=
liftq_apply _ _ _
@[simp] lemma quotient_quotient_equiv_quotient_aux_mk_mk (x : M) :
quotient_quotient_equiv_quotient_aux S T h (quotient.mk (quotient.mk x)) = quotient.mk x :=
by rw [quotient_quotient_equiv_quotient_aux_mk, mapq_apply, linear_map.id_apply]
/-- **Noether's third isomorphism theorem** for modules: `(M / S) / (T / S) ≃ M / T`. -/
def quotient_quotient_equiv_quotient :
((M ⧸ S) ⧸ (T.map S.mkq)) ≃ₗ[R] M ⧸ T :=
{ to_fun := quotient_quotient_equiv_quotient_aux S T h,
inv_fun := mapq _ _ (mkq S) (le_comap_map _ _),
left_inv := λ x, quotient.induction_on' x $ λ x, quotient.induction_on' x $ λ x, by simp,
right_inv := λ x, quotient.induction_on' x $ λ x, by simp,
.. quotient_quotient_equiv_quotient_aux S T h }
end submodule
|
20e122cfe4d9f8a015a419e4ee50ec23af648526 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/measure_theory/group/integration.lean | 04d574f004c5994cbdd35c805f2a1a05b84883e7 | [
"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 | 8,008 | lean | /-
Copyright (c) 2022 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import measure_theory.integral.bochner
import measure_theory.group.measure
import measure_theory.group.action
/-!
# Integration on Groups
We develop properties of integrals with a group as domain.
This file contains properties about integrability, Lebesgue integration and Bochner integration.
-/
namespace measure_theory
open measure topological_space
open_locale ennreal
variables {𝕜 M α G E F : Type*} [measurable_space G]
variables [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] [normed_add_comm_group F]
variables {μ : measure G} {f : G → E} {g : G}
section measurable_inv
variables [group G] [has_measurable_inv G]
@[to_additive]
lemma integrable.comp_inv [is_inv_invariant μ] {f : G → F} (hf : integrable f μ) :
integrable (λ t, f t⁻¹) μ :=
(hf.mono_measure (map_inv_eq_self μ).le).comp_measurable measurable_inv
@[to_additive]
lemma integral_inv_eq_self (f : G → E) (μ : measure G) [is_inv_invariant μ] :
∫ x, f (x⁻¹) ∂μ = ∫ x, f x ∂μ :=
begin
have h : measurable_embedding (λ x : G, x⁻¹) :=
(measurable_equiv.inv G).measurable_embedding,
rw [← h.integral_map, map_inv_eq_self]
end
end measurable_inv
section measurable_mul
variables [group G] [has_measurable_mul G]
/-- Translating a function by left-multiplication does not change its `measure_theory.lintegral`
with respect to a left-invariant measure. -/
@[to_additive "Translating a function by left-addition does not change its
`measure_theory.lintegral` with respect to a left-invariant measure."]
lemma lintegral_mul_left_eq_self [is_mul_left_invariant μ] (f : G → ℝ≥0∞) (g : G) :
∫⁻ x, f (g * x) ∂μ = ∫⁻ x, f x ∂μ :=
begin
convert (lintegral_map_equiv f $ measurable_equiv.mul_left g).symm,
simp [map_mul_left_eq_self μ g]
end
/-- Translating a function by right-multiplication does not change its `measure_theory.lintegral`
with respect to a right-invariant measure. -/
@[to_additive "Translating a function by right-addition does not change its
`measure_theory.lintegral` with respect to a right-invariant measure."]
lemma lintegral_mul_right_eq_self [is_mul_right_invariant μ] (f : G → ℝ≥0∞) (g : G) :
∫⁻ x, f (x * g) ∂μ = ∫⁻ x, f x ∂μ :=
begin
convert (lintegral_map_equiv f $ measurable_equiv.mul_right g).symm,
simp [map_mul_right_eq_self μ g]
end
@[simp, to_additive]
lemma lintegral_div_right_eq_self [is_mul_right_invariant μ] (f : G → ℝ≥0∞) (g : G) :
∫⁻ x, f (x / g) ∂μ = ∫⁻ x, f x ∂μ :=
by simp_rw [div_eq_mul_inv, lintegral_mul_right_eq_self f g⁻¹]
/-- Translating a function by left-multiplication does not change its integral with respect to a
left-invariant measure. -/
@[simp, to_additive "Translating a function by left-addition does not change its integral with
respect to a left-invariant measure."]
lemma integral_mul_left_eq_self [is_mul_left_invariant μ] (f : G → E) (g : G) :
∫ x, f (g * x) ∂μ = ∫ x, f x ∂μ :=
begin
have h_mul : measurable_embedding (λ x, g * x) :=
(measurable_equiv.mul_left g).measurable_embedding,
rw [← h_mul.integral_map, map_mul_left_eq_self]
end
/-- Translating a function by right-multiplication does not change its integral with respect to a
right-invariant measure. -/
@[simp, to_additive "Translating a function by right-addition does not change its integral with
respect to a right-invariant measure."]
lemma integral_mul_right_eq_self [is_mul_right_invariant μ] (f : G → E) (g : G) :
∫ x, f (x * g) ∂μ = ∫ x, f x ∂μ :=
begin
have h_mul : measurable_embedding (λ x, x * g) :=
(measurable_equiv.mul_right g).measurable_embedding,
rw [← h_mul.integral_map, map_mul_right_eq_self]
end
@[simp, to_additive]
lemma integral_div_right_eq_self [is_mul_right_invariant μ] (f : G → E) (g : G) :
∫ x, f (x / g) ∂μ = ∫ x, f x ∂μ :=
by simp_rw [div_eq_mul_inv, integral_mul_right_eq_self f g⁻¹]
/-- If some left-translate of a function negates it, then the integral of the function with respect
to a left-invariant measure is 0. -/
@[to_additive "If some left-translate of a function negates it, then the integral of the function
with respect to a left-invariant measure is 0."]
lemma integral_eq_zero_of_mul_left_eq_neg [is_mul_left_invariant μ] (hf' : ∀ x, f (g * x) = - f x) :
∫ x, f x ∂μ = 0 :=
by simp_rw [← self_eq_neg ℝ E, ← integral_neg, ← hf', integral_mul_left_eq_self]
/-- If some right-translate of a function negates it, then the integral of the function with respect
to a right-invariant measure is 0. -/
@[to_additive "If some right-translate of a function negates it, then the integral of the function
with respect to a right-invariant measure is 0."]
lemma integral_eq_zero_of_mul_right_eq_neg [is_mul_right_invariant μ]
(hf' : ∀ x, f (x * g) = - f x) : ∫ x, f x ∂μ = 0 :=
by simp_rw [← self_eq_neg ℝ E, ← integral_neg, ← hf', integral_mul_right_eq_self]
@[to_additive]
lemma integrable.comp_mul_left {f : G → F} [is_mul_left_invariant μ] (hf : integrable f μ)
(g : G) : integrable (λ t, f (g * t)) μ :=
(hf.mono_measure (map_mul_left_eq_self μ g).le).comp_measurable $ measurable_const_mul g
@[to_additive]
lemma integrable.comp_mul_right {f : G → F} [is_mul_right_invariant μ] (hf : integrable f μ)
(g : G) : integrable (λ t, f (t * g)) μ :=
(hf.mono_measure (map_mul_right_eq_self μ g).le).comp_measurable $ measurable_mul_const g
@[to_additive]
lemma integrable.comp_div_right {f : G → F} [is_mul_right_invariant μ] (hf : integrable f μ)
(g : G) : integrable (λ t, f (t / g)) μ :=
by { simp_rw [div_eq_mul_inv], exact hf.comp_mul_right g⁻¹ }
variables [has_measurable_inv G]
@[to_additive]
lemma integrable.comp_div_left {f : G → F}
[is_inv_invariant μ] [is_mul_left_invariant μ] (hf : integrable f μ) (g : G) :
integrable (λ t, f (g / t)) μ :=
((measure_preserving_div_left μ g).integrable_comp hf.ae_strongly_measurable).mpr hf
@[simp, to_additive]
lemma integrable_comp_div_left (f : G → F)
[is_inv_invariant μ] [is_mul_left_invariant μ] (g : G) :
integrable (λ t, f (g / t)) μ ↔ integrable f μ :=
begin
refine ⟨λ h, _, λ h, h.comp_div_left g⟩,
convert h.comp_inv.comp_mul_left g⁻¹,
simp_rw [div_inv_eq_mul, mul_inv_cancel_left]
end
@[simp, to_additive]
lemma integral_div_left_eq_self (f : G → E) (μ : measure G) [is_inv_invariant μ]
[is_mul_left_invariant μ] (x' : G) : ∫ x, f (x' / x) ∂μ = ∫ x, f x ∂μ :=
by simp_rw [div_eq_mul_inv, integral_inv_eq_self (λ x, f (x' * x)) μ,
integral_mul_left_eq_self f x']
end measurable_mul
section smul
variables [group G] [measurable_space α] [mul_action G α] [has_measurable_smul G α]
@[simp, to_additive]
lemma integral_smul_eq_self {μ : measure α} [smul_invariant_measure G α μ] (f : α → E) {g : G} :
∫ x, f (g • x) ∂μ = ∫ x, f x ∂μ :=
begin
have h : measurable_embedding (λ x : α, g • x) :=
(measurable_equiv.smul g).measurable_embedding,
rw [← h.integral_map, map_smul]
end
end smul
section topological_group
variables [topological_space G] [group G] [topological_group G] [borel_space G]
[is_mul_left_invariant μ]
/-- For nonzero regular left invariant measures, the integral of a continuous nonnegative function
`f` is 0 iff `f` is 0. -/
@[to_additive "For nonzero regular left invariant measures, the integral of a continuous nonnegative
function `f` is 0 iff `f` is 0."]
lemma lintegral_eq_zero_of_is_mul_left_invariant [regular μ] (hμ : μ ≠ 0)
{f : G → ℝ≥0∞} (hf : continuous f) :
∫⁻ x, f x ∂μ = 0 ↔ f = 0 :=
begin
haveI := is_open_pos_measure_of_mul_left_invariant_of_regular hμ,
rw [lintegral_eq_zero_iff hf.measurable, hf.ae_eq_iff_eq μ continuous_zero]
end
end topological_group
end measure_theory
|
0e15b38dac1fbdb4d3ed63de37ec9ab650b6d161 | b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77 | /src/category_theory/monad/limits.lean | c60c8eefe3867324b8e2e68e8ae8683a0520a328 | [
"Apache-2.0"
] | permissive | molodiuc/mathlib | cae2ba3ef1601c1f42ca0b625c79b061b63fef5b | 98ebe5a6739fbe254f9ee9d401882d4388f91035 | refs/heads/master | 1,674,237,127,059 | 1,606,353,533,000 | 1,606,353,533,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,951 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Bhavik Mehta
-/
import category_theory.monad.adjunction
import category_theory.adjunction.limits
namespace category_theory
open category
open category_theory.limits
universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace monad
variables {C : Type u₁} [category.{v₁} C]
variables {T : C ⥤ C} [monad T]
variables {J : Type v₁} [small_category J]
namespace forget_creates_limits
variables (D : J ⥤ algebra T) (c : cone (D ⋙ forget T)) (t : is_limit c)
/-- (Impl) The natural transformation used to define the new cone -/
@[simps] def γ : (D ⋙ forget T ⋙ T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a }
/-- (Impl) This new cone is used to construct the algebra structure -/
@[simps] def new_cone : cone (D ⋙ forget T) :=
{ X := T.obj c.X,
π := (functor.const_comp _ _ T).inv ≫ whisker_right c.π T ≫ (γ D) }
/-- The algebra structure which will be the apex of the new limit cone for `D`. -/
@[simps] def cone_point : algebra T :=
{ A := c.X,
a := t.lift (new_cone D c),
unit' :=
begin
apply t.hom_ext,
intro j,
erw [category.assoc, t.fac (new_cone D c), id_comp],
dsimp,
erw [id_comp, ← category.assoc, ← (η_ T).naturality, functor.id_map, category.assoc,
(D.obj j).unit, comp_id],
end,
assoc' :=
begin
apply t.hom_ext,
intro j,
rw [category.assoc, category.assoc, t.fac (new_cone D c)],
dsimp,
erw id_comp,
slice_lhs 1 2 {rw ← (μ_ T).naturality},
slice_lhs 2 3 {rw (D.obj j).assoc},
slice_rhs 1 2 {rw ← T.map_comp},
rw t.fac (new_cone D c),
dsimp,
erw [id_comp, T.map_comp, category.assoc]
end }
/-- (Impl) Construct the lifted cone in `algebra T` which will be limiting. -/
@[simps] def lifted_cone : cone D :=
{ X := cone_point D c t,
π := { app := λ j, { f := c.π.app j },
naturality' := λ X Y f, by { ext1, dsimp, erw c.w f, simp } } }
/-- (Impl) Prove that the lifted cone is limiting. -/
@[simps]
def lifted_cone_is_limit : is_limit (lifted_cone D c t) :=
{ lift := λ s,
{ f := t.lift ((forget T).map_cone s),
h' :=
begin
apply t.hom_ext, intro j,
have := t.fac ((forget T).map_cone s),
slice_rhs 2 3 {rw t.fac ((forget T).map_cone s) j},
dsimp,
slice_lhs 2 3 {rw t.fac (new_cone D c) j},
dsimp,
rw category.id_comp,
slice_lhs 1 2 {rw ← T.map_comp},
rw t.fac ((forget T).map_cone s) j,
exact (s.π.app j).h
end },
uniq' := λ s m J,
begin
ext1,
apply t.hom_ext,
intro j,
simpa [t.fac (functor.map_cone (forget T) s) j] using congr_arg algebra.hom.f (J j),
end }
end forget_creates_limits
-- Theorem 5.6.5 from [Riehl][riehl2017]
/-- The forgetful functor from the Eilenberg-Moore category creates limits. -/
instance forget_creates_limits : creates_limits (forget T) :=
{ creates_limits_of_shape := λ J 𝒥, by exactI
{ creates_limit := λ D,
creates_limit_of_reflects_iso (λ c t,
{ lifted_cone := forget_creates_limits.lifted_cone D c t,
valid_lift := cones.ext (iso.refl _) (λ j, (id_comp _).symm),
makes_limit := forget_creates_limits.lifted_cone_is_limit _ _ _ } ) } }
/-- `D ⋙ forget T` has a limit, then `D` has a limit. -/
lemma has_limit_of_comp_forget_has_limit (D : J ⥤ algebra T) [has_limit (D ⋙ forget T)] : has_limit D :=
has_limit_of_created D (forget T)
namespace forget_creates_colimits
-- Let's hide the implementation details in a namespace
variables {D : J ⥤ algebra T} (c : cocone (D ⋙ forget T)) (t : is_colimit c)
-- We have a diagram D of shape J in the category of algebras, and we assume that we are given a
-- colimit for its image D ⋙ forget T under the forgetful functor, say its apex is L.
-- We'll construct a colimiting coalgebra for D, whose carrier will also be L.
-- To do this, we must find a map TL ⟶ L. Since T preserves colimits, TL is also a colimit.
-- In particular, it is a colimit for the diagram `(D ⋙ forget T) ⋙ T`
-- so to construct a map TL ⟶ L it suffices to show that L is the apex of a cocone for this diagram.
-- In other words, we need a natural transformation from const L to `(D ⋙ forget T) ⋙ T`.
-- But we already know that L is the apex of a cocone for the diagram `D ⋙ forget T`, so it
-- suffices to give a natural transformation `((D ⋙ forget T) ⋙ T) ⟶ (D ⋙ forget T)`:
/--
(Impl)
The natural transformation given by the algebra structure maps, used to construct a cocone `c` with
apex `colimit (D ⋙ forget T)`.
-/
@[simps] def γ : ((D ⋙ forget T) ⋙ T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a }
/--
(Impl)
A cocone for the diagram `(D ⋙ forget T) ⋙ T` found by composing the natural transformation `γ`
with the colimiting cocone for `D ⋙ forget T`.
-/
@[simps]
def new_cocone : cocone ((D ⋙ forget T) ⋙ T) :=
{ X := c.X,
ι := γ ≫ c.ι }
variable [preserves_colimits_of_shape J T]
/--
(Impl)
Define the map `λ : TL ⟶ L`, which will serve as the structure of the coalgebra on `L`, and
we will show is the colimiting object. We use the cocone constructed by `c` and the fact that
`T` preserves colimits to produce this morphism.
-/
@[reducible]
def lambda : (functor.map_cocone T c).X ⟶ c.X :=
(preserves_colimit.preserves t).desc (new_cocone c)
/-- (Impl) The key property defining the map `λ : TL ⟶ L`. -/
lemma commuting (j : J) :
T.map (c.ι.app j) ≫ lambda c t = (D.obj j).a ≫ c.ι.app j :=
is_colimit.fac (preserves_colimit.preserves t) (new_cocone c) j
/--
(Impl)
Construct the colimiting algebra from the map `λ : TL ⟶ L` given by `lambda`. We are required to
show it satisfies the two algebra laws, which follow from the algebra laws for the image of `D` and
our `commuting` lemma.
-/
@[simps] def cocone_point :
algebra T :=
{ A := c.X,
a := lambda c t,
unit' :=
begin
apply t.hom_ext,
intro j,
erw [comp_id, ← category.assoc, (η_ T).naturality, category.assoc, commuting, ← category.assoc],
erw algebra.unit, apply id_comp
end,
assoc' :=
begin
apply is_colimit.hom_ext (preserves_colimit.preserves (preserves_colimit.preserves t)),
intro j,
erw [← category.assoc, nat_trans.naturality (μ_ T), ← functor.map_cocone_ι, category.assoc,
is_colimit.fac _ (new_cocone c) j],
rw ← category.assoc,
erw [← functor.map_comp, commuting],
dsimp,
erw [← category.assoc, algebra.assoc, category.assoc, functor.map_comp, category.assoc, commuting],
apply_instance, apply_instance
end }
/-- (Impl) Construct the lifted cocone in `algebra T` which will be colimiting. -/
@[simps] def lifted_cocone : cocone D :=
{ X := cocone_point c t,
ι := { app := λ j, { f := c.ι.app j, h' := commuting _ _ _ },
naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } }
/-- (Impl) Prove that the lifted cocone is colimiting. -/
@[simps]
def lifted_cocone_is_colimit : is_colimit (lifted_cocone c t) :=
{ desc := λ s,
{ f := t.desc ((forget T).map_cocone s),
h' :=
begin
dsimp,
apply is_colimit.hom_ext (preserves_colimit.preserves t),
intro j,
rw ← category.assoc, erw ← functor.map_comp,
erw t.fac',
rw ← category.assoc, erw forget_creates_colimits.commuting,
rw category.assoc, rw t.fac',
apply algebra.hom.h,
apply_instance
end },
uniq' := λ s m J, by { ext1, apply t.hom_ext, intro j, simpa using congr_arg algebra.hom.f (J j) } }
end forget_creates_colimits
open forget_creates_colimits
-- TODO: the converse of this is true as well
-- TODO: generalise to monadic functors, as for creating limits
/--
The forgetful functor from the Eilenberg-Moore category for a monad creates any colimit
which the monad itself preserves.
-/
instance forget_creates_colimits [preserves_colimits_of_shape J T] : creates_colimits_of_shape J (forget T) :=
{ creates_colimit := λ D,
creates_colimit_of_reflects_iso $ λ c t,
{ lifted_cocone :=
{ X := cocone_point c t,
ι :=
{ app := λ j, { f := c.ι.app j, h' := commuting _ _ _ },
naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } },
valid_lift := cocones.ext (iso.refl _) (by tidy),
makes_colimit := lifted_cocone_is_colimit _ _ } }
/--
For `D : J ⥤ algebra T`, `D ⋙ forget T` has a colimit, then `D` has a colimit provided colimits
of shape `J` are preserved by `T`.
-/
lemma forget_creates_colimits_of_monad_preserves
[preserves_colimits_of_shape J T] (D : J ⥤ algebra T) [has_colimit (D ⋙ forget T)] :
has_colimit D :=
has_colimit_of_created D (forget T)
end monad
variables {C : Type u₁} [category.{v₁} C] {D : Type u₁} [category.{v₁} D]
variables {J : Type v₁} [small_category J]
instance comp_comparison_forget_has_limit
(F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] :
has_limit ((F ⋙ monad.comparison R) ⋙ monad.forget ((left_adjoint R) ⋙ R)) :=
(@has_limit_of_iso _ _ _ _ (F ⋙ R) _ _ (iso_whisker_left F (monad.comparison_forget R).symm))
instance comp_comparison_has_limit
(F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] :
has_limit (F ⋙ monad.comparison R) :=
monad.has_limit_of_comp_forget_has_limit (F ⋙ monad.comparison R)
/-- Any monadic functor creates limits. -/
def monadic_creates_limits (R : D ⥤ C) [monadic_right_adjoint R] :
creates_limits R :=
creates_limits_of_nat_iso (monad.comparison_forget R)
/-- A monadic functor creates any colimits of shapes it preserves. -/
def monadic_creates_colimits_of_shape_of_preserves_colimits_of_shape (R : D ⥤ C)
[monadic_right_adjoint R] [preserves_colimits_of_shape J R] : creates_colimits_of_shape J R :=
begin
have : preserves_colimits_of_shape J (left_adjoint R ⋙ R),
{ apply category_theory.limits.comp_preserves_colimits_of_shape _ _,
{ haveI := adjunction.left_adjoint_preserves_colimits (adjunction.of_right_adjoint R),
apply_instance },
apply_instance },
resetI,
apply creates_colimits_of_shape_of_nat_iso (monad.comparison_forget R),
apply_instance,
end
/-- A monadic functor creates colimits if it preserves colimits. -/
def monadic_creates_colimits_of_preserves_colimits (R : D ⥤ C) [monadic_right_adjoint R]
[preserves_colimits R] : creates_colimits R :=
{ creates_colimits_of_shape := λ J 𝒥₁,
by exactI monadic_creates_colimits_of_shape_of_preserves_colimits_of_shape _ }
section
/-- If C has limits then any reflective subcategory has limits. -/
lemma has_limits_of_reflective (R : D ⥤ C) [has_limits C] [reflective R] : has_limits D :=
{ has_limits_of_shape := λ J 𝒥, by have := monadic_creates_limits R; exactI
{ has_limit := λ F, has_limit_of_created F R } }
end
end category_theory
|
4c44bb2a950f8ae4c0746bc92cdc2691079a8b97 | 56e5b79a7ab4f2c52e6eb94f76d8100a25273cf3 | /src/backends/bfs/fairseq.lean | 1054a15e1f52ec46dbd2220033e523bdfc0906e0 | [
"Apache-2.0"
] | permissive | DyeKuu/lean-tpe-public | 3a9968f286ca182723ef7e7d97e155d8cb6b1e70 | 750ade767ab28037e80b7a80360d213a875038f8 | refs/heads/master | 1,682,842,633,115 | 1,621,330,793,000 | 1,621,330,793,000 | 368,475,816 | 0 | 0 | Apache-2.0 | 1,621,330,745,000 | 1,621,330,744,000 | null | UTF-8 | Lean | false | false | 6,534 | lean | import evaluation
import utils
namespace fairseq
section fairseq_api
meta structure CompletionRequest : Type :=
(prompt : string)
(max_tokens : int := 16)
(temperature : native.float := 1.0)
(nbest : int := 1)
(beam: int := 10)
meta def default_partial_req : CompletionRequest :=
{
prompt := "",
max_tokens := 6000,
temperature := (1.0 : native.float),
nbest := 1,
beam := 10,
}
/-- this is responsible for validating parameters,
e.g. ensuring floats are between 0 and 1 -/
meta instance : has_to_tactic_json CompletionRequest :=
let validate_max_tokens : int → bool := λ n, n ≤ 10000 in
let validate_float_frac : native.float → bool := λ k, 0 ≤ k ∧ k ≤ 1 in
let validate_and_return {α} (pred : α → bool) : α → tactic α := λ a, ((guard $ pred a) *> pure a <|> tactic.fail "VALIDATE_AND_RETURN FAILED") in
let validate_optional_and_return {α} (pred : α → bool) : option α → tactic (option α) := λ x, do {
match x with
| (some val) := some <$> validate_and_return pred val
| none := pure none
end
} in
let fn : CompletionRequest → tactic json := λ req, match req with
| ⟨prompt, max_tokens, temperature, nbest, beam⟩ := do
max_tokens ← validate_and_return validate_max_tokens max_tokens,
temperature ← validate_and_return validate_float_frac temperature,
nbest ← validate_and_return (λ x, 0 ≤ x ∧ x ≤ (50 : int)) /- don't go overboard with the candidates -/ nbest,
beam ← validate_and_return (λ x, 0 ≤ x ∧ x ≤ (50 : int)) /- don't go overboard with the candidates -/ beam,
let pre_kvs : list (string × option json) := [
("prompt", json.of_string prompt),
("max_tokens", json.of_int max_tokens),
("temperature", json.of_float temperature),
("nbest", json.of_int nbest),
("beam", json.of_int beam)
],
pure $ json.object $ pre_kvs.filter_map (λ ⟨k,mv⟩, prod.mk k <$> mv)
end
in ⟨fn⟩
meta def ENTRY_PT : string := "/Users/Yuhuai/Documents/research/scatter_transformer_fairseq/fairseq_cli/query.py"
meta def MODEL_PATH : string := "/Users/Yuhuai/Documents/research/scatter_transformer_fairseq/lean_multigoal_checkpoints/checkpoint_best.pt"
meta def DATA_PATH : string := "/Users/Yuhuai/Documents/research/scatter_transformer_fairseq/datasets/LeanMultiGoalStepSPBPE4000Bin"
meta def CompletionRequest.to_cmd (entry_pt: string) (model_path : string) (data_path : string) : CompletionRequest → io (io.process.spawn_args)
| req@⟨prompt, max_tokens, temperature, nbest, beam⟩ := do
serialized_req ← io.run_tactic' $ has_to_tactic_json.to_tactic_json req,
pure {
cmd := "python",
args := [
entry_pt
, data_path
, "--path"
, model_path
, "--sentencepiece-model"
, data_path ++ "/model_4000_bpe.model"
, "--json-msg"
, json.unparse serialized_req
]
}
meta def serialize_ts
(req : CompletionRequest)
: tactic_state → tactic CompletionRequest := λ ts, do {
ts_str ← postprocess_tactic_state ts, -- this function is responsible for replacing newlines with tabs and removing the "k goals" line
let prompt : string :=
ts_str,
eval_trace format!"\n \n \n PROMPT: {prompt} \n \n \n ",
pure {
prompt := prompt,
..req}
}
meta def fairseq_api (entry_pt : string) (model_path : string) (data_path : string) : ModelAPI CompletionRequest :=
let get_predictions (response_msg : json) : option json :=
(lift_option $ do
{ (json.array choices) ← response_msg.lookup "choices" | none,
/- `choices` is a list of {text: ..., index: ..., logprobs: ..., finish_reason: ...}-/
texts ← choices.mmap (λ choice, choice.lookup "text"),
-- TODO(yuhuai): populate with `fairseq-generate` logprobs
let dummy_scores := json.of_float <$> list.repeat (0.0 : native.float) texts.length,
pure $ json.array [texts, json.array $ dummy_scores]
}) in
let fn : CompletionRequest → io json := λ req, do {
proc_cmds ← req.to_cmd entry_pt model_path data_path,
response_raw ← io.cmd proc_cmds,
io.put_str_ln' format!"RAW RESPONSE: {response_raw}",
response_msg ← (lift_option $ json.parse response_raw) | io.fail' format!"[fairseq_api] JSON PARSE FAILED {response_raw}",
(do predictions ← lift_option (get_predictions response_msg) | io.fail' format!"[fairseq_api] UNEXPECTED RESPONSE MSG: {response_msg}",
io.put_str_ln' format!"PREDICTIONS: {predictions}",
pure predictions) <|> pure (json.array $ [json.of_string $ format.to_string $ format!"ERROR {response_msg}"])
} in ⟨fn⟩
end fairseq_api
section fairseq_bfs
meta def fairseq_bfs_proof_search_core
(partial_req : fairseq.CompletionRequest)
(entry_pt : string)
(model_path : string)
(data_path : string)
(fuel := 5)
: state_t BFSState tactic unit :=
bfs_core
(fairseq_api entry_pt model_path data_path)
(fairseq.serialize_ts partial_req)
(λ msg n, run_all_beam_candidates (unwrap_lm_response_logprobs $ some "[fairseq_bfs_core]") msg n)
(fuel)
meta def fairseq_bfs_proof_search
(partial_req : fairseq.CompletionRequest)
(entry_pt : string)
(model_path : string)
(data_path : string)
(fuel := 5)
(verbose := ff)
(max_width := 25)
(max_depth := 50)
: tactic unit :=
bfs
(fairseq_api entry_pt model_path data_path)
(fairseq.serialize_ts partial_req)
(λ msg n, run_all_beam_candidates (unwrap_lm_response_logprobs $ some "[fairseq_bfs]") msg n)
(fuel)
(verbose) (max_width) (max_depth)
end fairseq_bfs
section test
-- example : true :=
-- begin
-- trythis "asdf",
-- sorry -- try using fairseq_bfs here
-- end
-- example : true :=
-- begin
-- fairseq_bfs {temperature :=1.0, nbest:=1, beam:=10, ..fairseq_api.default_partial_req}
-- fairseq_api.ENTRY_PT
-- fairseq_api.MODEL_PATH
-- fairseq_api.DATA_PATH,
-- end
-- open nat
-- example (n : ℕ) (m : ℕ) : nat.succ (n + m) = (nat.succ n + m) :=
-- begin
-- -- fairseq_bfs {temperature :=0.7, nbest:=10, beam:=10, ..fairseq_api.default_partial_req}
-- -- fairseq_api.ENTRY_PT
-- -- fairseq_api.MODEL_PATH
-- -- fairseq_api.DATA_PATH,
-- end
-- #eval fairseq_api.CompletionRequest.to_cmd {prompt := "true", nbest := 10, ..fairseq_api.default_partial_req} >>= io.cmd >>=io.put_str_ln
-- #eval fairseq_api.CompletionRequest.to_cmd fairseq_api.default_partial_req >>= io.cmd >>= io.put_str_ln
-- #eval io.cmd {cmd:="echo", args:=["hello"]} >>= io.put_str_ln
-- #eval fairseq_api.serialize_ts fairseq_api.default_partial_req
end test
end fairseq
|
0bae9d10beaeb3b0229bb920e54b2d158482f806 | 367134ba5a65885e863bdc4507601606690974c1 | /src/field_theory/separable.lean | 56ed09a1aba2cee2db94650ba2be11457ba51be6 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 25,674 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau.
-/
import algebra.polynomial.big_operators
import field_theory.minpoly
import field_theory.splitting_field
import field_theory.tower
import algebra.squarefree
/-!
# Separable polynomials
We define a polynomial to be separable if it is coprime with its derivative. We prove basic
properties about separable polynomials here.
## Main definitions
* `polynomial.separable f`: a polynomial `f` is separable iff it is coprime with its derivative.
* `polynomial.expand R p f`: expand the polynomial `f` with coefficients in a
commutative semiring `R` by a factor of p, so `expand R p (∑ aₙ xⁿ)` is `∑ aₙ xⁿᵖ`.
* `polynomial.contract p f`: the opposite of `expand`, so it sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`.
-/
universes u v w
open_locale classical big_operators
open finset
namespace polynomial
section comm_semiring
variables {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S]
/-- A polynomial is separable iff it is coprime with its derivative. -/
def separable (f : polynomial R) : Prop :=
is_coprime f f.derivative
lemma separable_def (f : polynomial R) :
f.separable ↔ is_coprime f f.derivative :=
iff.rfl
lemma separable_def' (f : polynomial R) :
f.separable ↔ ∃ a b : polynomial R, a * f + b * f.derivative = 1 :=
iff.rfl
lemma separable_one : (1 : polynomial R).separable :=
is_coprime_one_left
lemma separable_X_add_C (a : R) : (X + C a).separable :=
by { rw [separable_def, derivative_add, derivative_X, derivative_C, add_zero],
exact is_coprime_one_right }
lemma separable_X : (X : polynomial R).separable :=
by { rw [separable_def, derivative_X], exact is_coprime_one_right }
lemma separable_C (r : R) : (C r).separable ↔ is_unit r :=
by rw [separable_def, derivative_C, is_coprime_zero_right, is_unit_C]
lemma separable.of_mul_left {f g : polynomial R} (h : (f * g).separable) : f.separable :=
begin
have := h.of_mul_left_left, rw derivative_mul at this,
exact is_coprime.of_mul_right_left (is_coprime.of_add_mul_left_right this)
end
lemma separable.of_mul_right {f g : polynomial R} (h : (f * g).separable) : g.separable :=
by { rw mul_comm at h, exact h.of_mul_left }
lemma separable.of_dvd {f g : polynomial R} (hf : f.separable) (hfg : g ∣ f) : g.separable :=
by { rcases hfg with ⟨f', rfl⟩, exact separable.of_mul_left hf }
lemma separable_gcd_left {F : Type*} [field F] {f : polynomial F}
(hf : f.separable) (g : polynomial F) : (euclidean_domain.gcd f g).separable :=
separable.of_dvd hf (euclidean_domain.gcd_dvd_left f g)
lemma separable_gcd_right {F : Type*} [field F] {g : polynomial F}
(f : polynomial F) (hg : g.separable) : (euclidean_domain.gcd f g).separable :=
separable.of_dvd hg (euclidean_domain.gcd_dvd_right f g)
lemma separable.is_coprime {f g : polynomial R} (h : (f * g).separable) : is_coprime f g :=
begin
have := h.of_mul_left_left, rw derivative_mul at this,
exact is_coprime.of_mul_right_right (is_coprime.of_add_mul_left_right this)
end
theorem separable.of_pow' {f : polynomial R} :
∀ {n : ℕ} (h : (f ^ n).separable), is_unit f ∨ (f.separable ∧ n = 1) ∨ n = 0
| 0 := λ h, or.inr $ or.inr rfl
| 1 := λ h, or.inr $ or.inl ⟨pow_one f ▸ h, rfl⟩
| (n+2) := λ h, or.inl $ is_coprime_self.1 h.is_coprime.of_mul_right_left
theorem separable.of_pow {f : polynomial R} (hf : ¬is_unit f) {n : ℕ} (hn : n ≠ 0)
(hfs : (f ^ n).separable) : f.separable ∧ n = 1 :=
(hfs.of_pow'.resolve_left hf).resolve_right hn
theorem separable.map {p : polynomial R} (h : p.separable) {f : R →+* S} : (p.map f).separable :=
let ⟨a, b, H⟩ := h in ⟨a.map f, b.map f,
by rw [derivative_map, ← map_mul, ← map_mul, ← map_add, H, map_one]⟩
variables (R) (p q : ℕ)
/-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/
noncomputable def expand : polynomial R →ₐ[R] polynomial R :=
{ commutes' := λ r, eval₂_C _ _,
.. (eval₂_ring_hom C (X ^ p) : polynomial R →+* polynomial R) }
lemma coe_expand : (expand R p : polynomial R → polynomial R) = eval₂ C (X ^ p) := rfl
variables {R}
lemma expand_eq_sum {f : polynomial R} :
expand R p f = f.sum (λ e a, C a * (X ^ p) ^ e) :=
by { dsimp [expand, eval₂], refl, }
@[simp] lemma expand_C (r : R) : expand R p (C r) = C r := eval₂_C _ _
@[simp] lemma expand_X : expand R p X = X ^ p := eval₂_X _ _
@[simp] lemma expand_monomial (r : R) : expand R p (monomial q r) = monomial (q * p) r :=
by simp_rw [monomial_eq_smul_X, alg_hom.map_smul, alg_hom.map_pow, expand_X, mul_comm, pow_mul]
theorem expand_expand (f : polynomial R) : expand R p (expand R q f) = expand R (p * q) f :=
polynomial.induction_on f (λ r, by simp_rw expand_C)
(λ f g ihf ihg, by simp_rw [alg_hom.map_add, ihf, ihg])
(λ n r ih, by simp_rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X,
alg_hom.map_pow, expand_X, pow_mul])
theorem expand_mul (f : polynomial R) : expand R (p * q) f = expand R p (expand R q f) :=
(expand_expand p q f).symm
@[simp] theorem expand_one (f : polynomial R) : expand R 1 f = f :=
polynomial.induction_on f
(λ r, by rw expand_C)
(λ f g ihf ihg, by rw [alg_hom.map_add, ihf, ihg])
(λ n r ih, by rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X, pow_one])
theorem expand_pow (f : polynomial R) : expand R (p ^ q) f = (expand R p ^[q] f) :=
nat.rec_on q (by rw [pow_zero, expand_one, function.iterate_zero, id]) $ λ n ih,
by rw [function.iterate_succ_apply', pow_succ, expand_mul, ih]
theorem derivative_expand (f : polynomial R) :
(expand R p f).derivative = expand R p f.derivative * (p * X ^ (p - 1)) :=
by rw [coe_expand, derivative_eval₂_C, derivative_pow, derivative_X, mul_one]
theorem coeff_expand {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff n = if p ∣ n then f.coeff (n / p) else 0 :=
begin
simp only [expand_eq_sum],
simp_rw [coeff_sum, ← pow_mul, C_mul_X_pow_eq_monomial, coeff_monomial, finsupp.sum],
split_ifs with h,
{ rw [finset.sum_eq_single (n/p), nat.mul_div_cancel' h, if_pos rfl], refl,
{ intros b hb1 hb2, rw if_neg, intro hb3, apply hb2, rw [← hb3, nat.mul_div_cancel_left b hp] },
{ intro hn, rw finsupp.not_mem_support_iff.1 hn, split_ifs; refl } },
{ rw finset.sum_eq_zero, intros k hk, rw if_neg, exact λ hkn, h ⟨k, hkn.symm⟩, },
end
@[simp] theorem coeff_expand_mul {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff (n * p) = f.coeff n :=
by rw [coeff_expand hp, if_pos (dvd_mul_left _ _), nat.mul_div_cancel _ hp]
@[simp] theorem coeff_expand_mul' {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff (p * n) = f.coeff n :=
by rw [mul_comm, coeff_expand_mul hp]
theorem expand_eq_map_domain (p : ℕ) (f : polynomial R) :
expand R p f = f.map_domain (*p) :=
polynomial.induction_on' f (λ p q hp hq, by simp [*, finsupp.map_domain_add]) $
λ n a, by simp_rw [expand_monomial, monomial_def, finsupp.map_domain_single]
theorem expand_inj {p : ℕ} (hp : 0 < p) {f g : polynomial R} :
expand R p f = expand R p g ↔ f = g :=
⟨λ H, ext $ λ n, by rw [← coeff_expand_mul hp, H, coeff_expand_mul hp], congr_arg _⟩
theorem expand_eq_zero {p : ℕ} (hp : 0 < p) {f : polynomial R} : expand R p f = 0 ↔ f = 0 :=
by rw [← (expand R p).map_zero, expand_inj hp, alg_hom.map_zero]
theorem expand_eq_C {p : ℕ} (hp : 0 < p) {f : polynomial R} {r : R} :
expand R p f = C r ↔ f = C r :=
by rw [← expand_C, expand_inj hp, expand_C]
theorem nat_degree_expand (p : ℕ) (f : polynomial R) :
(expand R p f).nat_degree = f.nat_degree * p :=
begin
cases p.eq_zero_or_pos with hp hp,
{ rw [hp, coe_expand, pow_zero, mul_zero, ← C_1, eval₂_hom, nat_degree_C] },
by_cases hf : f = 0,
{ rw [hf, alg_hom.map_zero, nat_degree_zero, zero_mul] },
have hf1 : expand R p f ≠ 0 := mt (expand_eq_zero hp).1 hf,
rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree hf1],
refine le_antisymm ((degree_le_iff_coeff_zero _ _).2 $ λ n hn, _) _,
{ rw coeff_expand hp, split_ifs with hpn,
{ rw coeff_eq_zero_of_nat_degree_lt, contrapose! hn,
rw [with_bot.coe_le_coe, ← nat.div_mul_cancel hpn], exact nat.mul_le_mul_right p hn },
{ refl } },
{ refine le_degree_of_ne_zero _,
rw [coeff_expand_mul hp, ← leading_coeff], exact mt leading_coeff_eq_zero.1 hf }
end
theorem map_expand {p : ℕ} (hp : 0 < p) {f : R →+* S} {q : polynomial R} :
map f (expand R p q) = expand S p (map f q) :=
by { ext, rw [coeff_map, coeff_expand hp, coeff_expand hp], split_ifs; simp, }
end comm_semiring
section comm_ring
variables {R : Type u} [comm_ring R]
lemma separable_X_sub_C {x : R} : separable (X - C x) :=
by simpa only [sub_eq_add_neg, C_neg] using separable_X_add_C (-x)
lemma separable.mul {f g : polynomial R} (hf : f.separable) (hg : g.separable)
(h : is_coprime f g) : (f * g).separable :=
by { rw [separable_def, derivative_mul], exact ((hf.mul_right h).add_mul_left_right _).mul_left
((h.symm.mul_right hg).mul_add_right_right _) }
lemma separable_prod' {ι : Sort*} {f : ι → polynomial R} {s : finset ι} :
(∀x∈s, ∀y∈s, x ≠ y → is_coprime (f x) (f y)) → (∀x∈s, (f x).separable) →
(∏ x in s, f x).separable :=
finset.induction_on s (λ _ _, separable_one) $ λ a s has ih h1 h2, begin
simp_rw [finset.forall_mem_insert, forall_and_distrib] at h1 h2, rw prod_insert has,
exact h2.1.mul (ih h1.2.2 h2.2) (is_coprime.prod_right $ λ i his, h1.1.2 i his $
ne.symm $ ne_of_mem_of_not_mem his has)
end
lemma separable_prod {ι : Sort*} [fintype ι] {f : ι → polynomial R}
(h1 : pairwise (is_coprime on f)) (h2 : ∀ x, (f x).separable) : (∏ x, f x).separable :=
separable_prod' (λ x hx y hy hxy, h1 x y hxy) (λ x hx, h2 x)
lemma separable.inj_of_prod_X_sub_C [nontrivial R] {ι : Sort*} {f : ι → R} {s : finset ι}
(hfs : (∏ i in s, (X - C (f i))).separable)
{x y : ι} (hx : x ∈ s) (hy : y ∈ s) (hfxy : f x = f y) : x = y :=
begin
by_contra hxy,
rw [← insert_erase hx, prod_insert (not_mem_erase _ _),
← insert_erase (mem_erase_of_ne_of_mem (ne.symm hxy) hy),
prod_insert (not_mem_erase _ _), ← mul_assoc, hfxy, ← pow_two] at hfs,
cases (hfs.of_mul_left.of_pow (by exact not_is_unit_X_sub_C) two_ne_zero).2
end
lemma separable.injective_of_prod_X_sub_C [nontrivial R] {ι : Sort*} [fintype ι] {f : ι → R}
(hfs : (∏ i, (X - C (f i))).separable) : function.injective f :=
λ x y hfxy, hfs.inj_of_prod_X_sub_C (mem_univ _) (mem_univ _) hfxy
lemma is_unit_of_self_mul_dvd_separable {p q : polynomial R}
(hp : p.separable) (hq : q * q ∣ p) : is_unit q :=
begin
obtain ⟨p, rfl⟩ := hq,
apply is_coprime_self.mp,
have : is_coprime (q * (q * p)) (q * (q.derivative * p + q.derivative * p + q * p.derivative)),
{ simp only [← mul_assoc, mul_add],
convert hp,
rw [derivative_mul, derivative_mul],
ring },
exact is_coprime.of_mul_right_left (is_coprime.of_mul_left_left this)
end
end comm_ring
section integral_domain
variables (R : Type u) [integral_domain R]
theorem is_local_ring_hom_expand {p : ℕ} (hp : 0 < p) :
is_local_ring_hom (↑(expand R p) : polynomial R →+* polynomial R) :=
begin
refine ⟨λ f hf1, _⟩, rw ← coe_fn_coe_base at hf1,
have hf2 := eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf1),
rw [coeff_expand hp, if_pos (dvd_zero _), p.zero_div] at hf2,
rw [hf2, is_unit_C] at hf1, rw expand_eq_C hp at hf2, rwa [hf2, is_unit_C]
end
end integral_domain
section field
variables {F : Type u} [field F] {K : Type v} [field K]
theorem separable_iff_derivative_ne_zero {f : polynomial F} (hf : irreducible f) :
f.separable ↔ f.derivative ≠ 0 :=
⟨λ h1 h2, hf.not_unit $ is_coprime_zero_right.1 $ h2 ▸ h1,
λ h, is_coprime_of_dvd (mt and.right h) $ λ g hg1 hg2 ⟨p, hg3⟩ hg4,
let ⟨u, hu⟩ := (hf.is_unit_or_is_unit hg3).resolve_left hg1 in
have f ∣ f.derivative, by { conv_lhs { rw [hg3, ← hu] }, rwa units.mul_right_dvd },
not_lt_of_le (nat_degree_le_of_dvd this h) $ nat_degree_derivative_lt h⟩
theorem separable_map (f : F →+* K) {p : polynomial F} : (p.map f).separable ↔ p.separable :=
by simp_rw [separable_def, derivative_map, is_coprime_map]
section char_p
variables (p : ℕ) [hp : fact p.prime]
include hp
/-- The opposite of `expand`: sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/
noncomputable def contract (f : polynomial F) : polynomial F :=
⟨f.support.preimage (*p) $ λ _ _ _ _, (nat.mul_left_inj hp.pos).1,
λ n, f.coeff (n * p),
λ n, by rw [finset.mem_preimage, mem_support_iff]⟩
theorem coeff_contract (f : polynomial F) (n : ℕ) : (contract p f).coeff n = f.coeff (n * p) := rfl
theorem of_irreducible_expand {f : polynomial F} (hf : irreducible (expand F p f)) :
irreducible f :=
@@of_irreducible_map _ _ _ (is_local_ring_hom_expand F hp.pos) hf
theorem of_irreducible_expand_pow {f : polynomial F} {n : ℕ} :
irreducible (expand F (p ^ n) f) → irreducible f :=
nat.rec_on n (λ hf, by rwa [pow_zero, expand_one] at hf) $ λ n ih hf,
ih $ of_irreducible_expand p $ by rwa [expand_expand]
variables [HF : char_p F p]
include HF
theorem expand_char (f : polynomial F) :
map (frobenius F p) (expand F p f) = f ^ p :=
begin
refine f.induction_on' (λ a b ha hb, _) (λ n a, _),
{ rw [alg_hom.map_add, map_add, ha, hb, add_pow_char], },
{ rw [expand_monomial, map_monomial, single_eq_C_mul_X, single_eq_C_mul_X,
mul_pow, ← C.map_pow, frobenius_def],
ring_exp }
end
theorem map_expand_pow_char (f : polynomial F) (n : ℕ) :
map ((frobenius F p) ^ n) (expand F (p ^ n) f) = f ^ (p ^ n) :=
begin
induction n, {simp [ring_hom.one_def]},
symmetry,
rw [pow_succ', pow_mul, ← n_ih, ← expand_char, pow_succ, ring_hom.mul_def, ← map_map, mul_comm,
expand_mul, ← map_expand (nat.prime.pos hp)],
end
theorem expand_contract {f : polynomial F} (hf : f.derivative = 0) :
expand F p (contract p f) = f :=
begin
ext n, rw [coeff_expand hp.pos, coeff_contract], split_ifs with h,
{ rw nat.div_mul_cancel h },
{ cases n, { exact absurd (dvd_zero p) h },
have := coeff_derivative f n, rw [hf, coeff_zero, zero_eq_mul] at this, cases this, { rw this },
rw [← nat.cast_succ, char_p.cast_eq_zero_iff F p] at this,
exact absurd this h }
end
theorem separable_or {f : polynomial F} (hf : irreducible f) : f.separable ∨
¬f.separable ∧ ∃ g : polynomial F, irreducible g ∧ expand F p g = f :=
if H : f.derivative = 0 then or.inr
⟨by rw [separable_iff_derivative_ne_zero hf, not_not, H],
contract p f,
by haveI := is_local_ring_hom_expand F hp.pos; exact
of_irreducible_map ↑(expand F p) (by rwa ← expand_contract p H at hf),
expand_contract p H⟩
else or.inl $ (separable_iff_derivative_ne_zero hf).2 H
theorem exists_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hf0 : f ≠ 0) :
∃ (n : ℕ) (g : polynomial F), g.separable ∧ expand F (p ^ n) g = f :=
begin
generalize hn : f.nat_degree = N, unfreezingI { revert f },
apply nat.strong_induction_on N, intros N ih f hf hf0 hn,
rcases separable_or p hf with h | ⟨h1, g, hg, hgf⟩,
{ refine ⟨0, f, h, _⟩, rw [pow_zero, expand_one] },
{ cases N with N,
{ rw [nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff] at hn,
rw [hn, separable_C, is_unit_iff_ne_zero, not_not] at h1,
rw [h1, C_0] at hn, exact absurd hn hf0 },
have hg1 : g.nat_degree * p = N.succ,
{ rwa [← nat_degree_expand, hgf] },
have hg2 : g.nat_degree ≠ 0,
{ intro this, rw [this, zero_mul] at hg1, cases hg1 },
have hg3 : g.nat_degree < N.succ,
{ rw [← mul_one g.nat_degree, ← hg1],
exact nat.mul_lt_mul_of_pos_left hp.one_lt (nat.pos_of_ne_zero hg2) },
have hg4 : g ≠ 0,
{ rintro rfl, exact hg2 nat_degree_zero },
rcases ih _ hg3 hg hg4 rfl with ⟨n, g, hg5, rfl⟩, refine ⟨n+1, g, hg5, _⟩,
rw [← hgf, expand_expand, pow_succ] }
end
theorem is_unit_or_eq_zero_of_separable_expand {f : polynomial F} (n : ℕ)
(hf : (expand F (p ^ n) f).separable) : is_unit f ∨ n = 0 :=
begin
rw or_iff_not_imp_right, intro hn,
have hf2 : (expand F (p ^ n) f).derivative = 0,
{ by rw [derivative_expand, nat.cast_pow, char_p.cast_eq_zero,
zero_pow (nat.pos_of_ne_zero hn), zero_mul, mul_zero] },
rw [separable_def, hf2, is_coprime_zero_right, is_unit_iff] at hf, rcases hf with ⟨r, hr, hrf⟩,
rw [eq_comm, expand_eq_C (pow_pos hp.pos _)] at hrf,
rwa [hrf, is_unit_C]
end
theorem unique_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hf0 : f ≠ 0)
(n₁ : ℕ) (g₁ : polynomial F) (hg₁ : g₁.separable) (hgf₁ : expand F (p ^ n₁) g₁ = f)
(n₂ : ℕ) (g₂ : polynomial F) (hg₂ : g₂.separable) (hgf₂ : expand F (p ^ n₂) g₂ = f) :
n₁ = n₂ ∧ g₁ = g₂ :=
begin
revert g₁ g₂, wlog hn : n₁ ≤ n₂ := le_total n₁ n₂ using [n₁ n₂, n₂ n₁] tactic.skip,
unfreezingI { intros, rw le_iff_exists_add at hn, rcases hn with ⟨k, rfl⟩,
rw [← hgf₁, pow_add, expand_mul, expand_inj (pow_pos hp.pos n₁)] at hgf₂, subst hgf₂,
subst hgf₁,
rcases is_unit_or_eq_zero_of_separable_expand p k hg₁ with h | rfl,
{ rw is_unit_iff at h, rcases h with ⟨r, hr, rfl⟩,
simp_rw expand_C at hf, exact absurd (is_unit_C.2 hr) hf.1 },
{ rw [add_zero, pow_zero, expand_one], split; refl } },
exact λ g₁ g₂ hg₁ hgf₁ hg₂ hgf₂, let ⟨hn, hg⟩ :=
this g₂ g₁ hg₂ hgf₂ hg₁ hgf₁ in ⟨hn.symm, hg.symm⟩
end
end char_p
lemma separable_prod_X_sub_C_iff' {ι : Sort*} {f : ι → F} {s : finset ι} :
(∏ i in s, (X - C (f i))).separable ↔ (∀ (x ∈ s) (y ∈ s), f x = f y → x = y) :=
⟨λ hfs x hx y hy hfxy, hfs.inj_of_prod_X_sub_C hx hy hfxy,
λ H, by { rw ← prod_attach, exact separable_prod' (λ x hx y hy hxy,
@pairwise_coprime_X_sub _ _ { x // x ∈ s } (λ x, f x)
(λ x y hxy, subtype.eq $ H x.1 x.2 y.1 y.2 hxy) _ _ hxy)
(λ _ _, separable_X_sub_C) }⟩
lemma separable_prod_X_sub_C_iff {ι : Sort*} [fintype ι] {f : ι → F} :
(∏ i, (X - C (f i))).separable ↔ function.injective f :=
separable_prod_X_sub_C_iff'.trans $ by simp_rw [mem_univ, true_implies_iff]
section splits
open_locale big_operators
variables {i : F →+* K}
lemma not_unit_X_sub_C (a : F) : ¬ is_unit (X - C a) :=
λ h, have one_eq_zero : (1 : with_bot ℕ) = 0, by simpa using degree_eq_zero_of_is_unit h,
one_ne_zero (option.some_injective _ one_eq_zero)
lemma nodup_of_separable_prod {s : multiset F}
(hs : separable (multiset.map (λ a, X - C a) s).prod) : s.nodup :=
begin
rw multiset.nodup_iff_ne_cons_cons,
rintros a t rfl,
refine not_unit_X_sub_C a (is_unit_of_self_mul_dvd_separable hs _),
simpa only [multiset.map_cons, multiset.prod_cons] using mul_dvd_mul_left _ (dvd_mul_right _ _)
end
lemma multiplicity_le_one_of_separable {p q : polynomial F} (hq : ¬ is_unit q)
(hsep : separable p) : multiplicity q p ≤ 1 :=
begin
contrapose! hq,
apply is_unit_of_self_mul_dvd_separable hsep,
rw ← pow_two,
apply multiplicity.pow_dvd_of_le_multiplicity,
exact_mod_cast (enat.add_one_le_of_lt hq)
end
lemma separable.squarefree {p : polynomial F} (hsep : separable p) : squarefree p :=
begin
rw multiplicity.squarefree_iff_multiplicity_le_one p,
intro f,
by_cases hunit : is_unit f,
{ exact or.inr hunit },
exact or.inl (multiplicity_le_one_of_separable hunit hsep)
end
/--If `n ≠ 0` in `F`, then ` X ^ n - a` is separable for any `a ≠ 0`. -/
lemma separable_X_pow_sub_C {n : ℕ} (a : F) (hn : (n : F) ≠ 0) (ha : a ≠ 0) :
separable (X ^ n - C a) :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ exfalso,
rw hzero at hn,
exact hn (refl 0) },
apply (separable_def' (X ^ n - C a)).2,
use [-C (a⁻¹), (C ((a⁻¹) * (↑n)⁻¹) * X)],
have mul_pow_sub : X * X ^ (n - 1) = X ^ n,
{ nth_rewrite 0 [←pow_one X],
rw pow_mul_pow_sub X (nat.succ_le_iff.mpr hpos) },
rw [derivative_sub, derivative_C, sub_zero, derivative_pow X n, derivative_X, mul_one],
have hcalc : C (a⁻¹ * (↑n)⁻¹) * (↑n * (X ^ n)) = C a⁻¹ * (X ^ n),
{ calc C (a⁻¹ * (↑n)⁻¹) * (↑n * (X ^ n))
= C a⁻¹ * C ((↑n)⁻¹) * (C ↑n * (X ^ n)) : by rw [C_mul, C_eq_nat_cast]
... = C a⁻¹ * (C ((↑n)⁻¹) * C ↑n) * (X ^ n) : by ring
... = C a⁻¹ * C ((↑n)⁻¹ * ↑n) * (X ^ n) : by rw [← C_mul]
... = C a⁻¹ * C 1 * (X ^ n) : by field_simp [hn]
... = C a⁻¹ * (X ^ n) : by rw [C_1, mul_one] },
calc -C a⁻¹ * (X ^ n - C a) + C (a⁻¹ * (↑n)⁻¹) * X * (↑n * X ^ (n - 1))
= -C a⁻¹ * (X ^ n - C a) + C (a⁻¹ * (↑n)⁻¹) * (↑n * (X * X ^ (n - 1))) : by ring
... = -C a⁻¹ * (X ^ n - C a) + C a⁻¹ * (X ^ n) : by rw [mul_pow_sub, hcalc]
... = C a⁻¹ * C a : by ring
... = (1 : polynomial F) : by rw [← C_mul, inv_mul_cancel ha, C_1]
end
/--If `n ≠ 0` in `F`, then ` X ^ n - a` is squarefree for any `a ≠ 0`. -/
lemma squarefree_X_pow_sub_C {n : ℕ} (a : F) (hn : (n : F) ≠ 0) (ha : a ≠ 0) :
squarefree (X ^ n - C a) :=
(separable_X_pow_sub_C a hn ha).squarefree
lemma root_multiplicity_le_one_of_separable {p : polynomial F} (hp : p ≠ 0)
(hsep : separable p) (x : F) : root_multiplicity x p ≤ 1 :=
begin
rw [root_multiplicity_eq_multiplicity, dif_neg hp, ← enat.coe_le_coe, enat.coe_get],
exact multiplicity_le_one_of_separable (not_unit_X_sub_C _) hsep
end
lemma count_roots_le_one {p : polynomial F} (hsep : separable p) (x : F) :
p.roots.count x ≤ 1 :=
begin
by_cases hp : p = 0,
{ simp [hp] },
rw count_roots hp,
exact root_multiplicity_le_one_of_separable hp hsep x
end
lemma nodup_roots {p : polynomial F} (hsep : separable p) :
p.roots.nodup :=
multiset.nodup_iff_count_le_one.mpr (count_roots_le_one hsep)
lemma eq_X_sub_C_of_separable_of_root_eq {x : F} {h : polynomial F} (h_ne_zero : h ≠ 0)
(h_sep : h.separable) (h_root : h.eval x = 0) (h_splits : splits i h)
(h_roots : ∀ y ∈ (h.map i).roots, y = i x) : h = (C (leading_coeff h)) * (X - C x) :=
begin
apply polynomial.eq_X_sub_C_of_splits_of_single_root i h_splits,
apply finset.mk.inj,
{ change _ = {i x},
rw finset.eq_singleton_iff_unique_mem,
split,
{ apply finset.mem_mk.mpr,
rw mem_roots (show h.map i ≠ 0, by exact map_ne_zero h_ne_zero),
rw [is_root.def,←eval₂_eq_eval_map,eval₂_hom,h_root],
exact ring_hom.map_zero i },
{ exact h_roots } },
{ exact nodup_roots (separable.map h_sep) },
end
end splits
end field
end polynomial
open polynomial
theorem irreducible.separable {F : Type u} [field F] [char_zero F] {f : polynomial F}
(hf : irreducible f) : f.separable :=
begin
rw [separable_iff_derivative_ne_zero hf, ne, ← degree_eq_bot, degree_derivative_eq], rintro ⟨⟩,
rw [pos_iff_ne_zero, ne, nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff],
refine λ hf1, hf.not_unit _, rw [hf1, is_unit_C, is_unit_iff_ne_zero],
intro hf2, rw [hf2, C_0] at hf1, exact absurd hf1 hf.ne_zero
end
-- TODO: refactor to allow transcendental extensions?
-- See: https://en.wikipedia.org/wiki/Separable_extension#Separability_of_transcendental_extensions
/-- Typeclass for separable field extension: `K` is a separable field extension of `F` iff
the minimal polynomial of every `x : K` is separable. -/
class is_separable (F K : Sort*) [field F] [field K] [algebra F K] : Prop :=
(is_integral' (x : K) : is_integral F x)
(separable' (x : K) : (minpoly F x).separable)
theorem is_separable.is_integral {F K} [field F] [field K] [algebra F K] (h : is_separable F K) :
∀ x : K, is_integral F x := is_separable.is_integral'
theorem is_separable.separable {F K} [field F] [field K] [algebra F K] (h : is_separable F K) :
∀ x : K, (minpoly F x).separable := is_separable.separable'
theorem is_separable_iff {F K} [field F] [field K] [algebra F K] : is_separable F K ↔
∀ x : K, is_integral F x ∧ (minpoly F x).separable :=
⟨λ h x, ⟨h.is_integral x, h.separable x⟩, λ h, ⟨λ x, (h x).1, λ x, (h x).2⟩⟩
instance is_separable_self (F : Type*) [field F] : is_separable F F :=
⟨λ x, is_integral_algebra_map, λ x, by { rw minpoly.eq_X_sub_C', exact separable_X_sub_C }⟩
section is_separable_tower
variables (F K E : Type*) [field F] [field K] [field E] [algebra F K] [algebra F E]
[algebra K E] [is_scalar_tower F K E]
lemma is_separable_tower_top_of_is_separable [h : is_separable F E] : is_separable K E :=
⟨λ x, is_integral_of_is_scalar_tower x (h.is_integral x),
λ x, (h.separable x).map.of_dvd (minpoly.dvd_map_of_is_scalar_tower _ _ _)⟩
lemma is_separable_tower_bot_of_is_separable [h : is_separable F E] : is_separable F K :=
is_separable_iff.2 $ λ x, begin
refine (is_separable_iff.1 h (algebra_map K E x)).imp
is_integral_tower_bot_of_is_integral_field (λ hs, _),
obtain ⟨q, hq⟩ := minpoly.dvd F x
(is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero_field
(minpoly.aeval F ((algebra_map K E) x))),
rw hq at hs,
exact hs.of_mul_left
end
variables {E}
lemma is_separable.of_alg_hom (E' : Type*) [field E'] [algebra F E']
(f : E →ₐ[F] E') [is_separable F E'] : is_separable F E :=
begin
letI : algebra E E' := ring_hom.to_algebra f.to_ring_hom,
haveI : is_scalar_tower F E E' := is_scalar_tower.of_algebra_map_eq (λ x, (f.commutes x).symm),
exact is_separable_tower_bot_of_is_separable F E E',
end
end is_separable_tower
|
381ccb98e17cf2a3cfffb83dc53258a7cd96b1d1 | 453dcd7c0d1ef170b0843a81d7d8caedc9741dce | /analysis/topology/topological_space.lean | c70213c3ebf224301d0e67488f41ec75f7f3482a | [
"Apache-2.0"
] | permissive | amswerdlow/mathlib | 9af77a1f08486d8fa059448ae2d97795bd12ec0c | 27f96e30b9c9bf518341705c99d641c38638dfd0 | refs/heads/master | 1,585,200,953,598 | 1,534,275,532,000 | 1,534,275,532,000 | 144,564,700 | 0 | 0 | null | 1,534,156,197,000 | 1,534,156,197,000 | null | UTF-8 | Lean | false | false | 56,810 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
Theory of topological spaces.
Parts of the formalization is based on the books:
N. Bourbaki: General Topology
I. M. James: Topologies and Uniformities
A major difference is that this formalization is heavily based on the filter library.
-/
import order.filter data.set.countable tactic
open set filter lattice classical
local attribute [instance] prop_decidable
universes u v w
structure topological_space (α : Type u) :=
(is_open : set α → Prop)
(is_open_univ : is_open univ)
(is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t))
(is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s))
attribute [class] topological_space
section topological_space
variables {α : Type u} {β : Type v} {ι : Sort w} {a a₁ a₂ : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop}
lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g
| ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl
section
variables [t : topological_space α]
include t
/-- `is_open s` means that `s` is open in the ambient topological space on `α` -/
def is_open (s : set α) : Prop := topological_space.is_open t s
@[simp]
lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t
lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) :=
topological_space.is_open_inter t s₁ s₂ h₁ h₂
lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) :=
topological_space.is_open_sUnion t s h
end
variables [topological_space α]
lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) :=
have (⋃₀ {s₁, s₂}) = (s₁ ∪ s₂), by simp [union_comm],
this ▸ is_open_sUnion $ show ∀(t : set α), t ∈ ({s₁, s₂} : set (set α)) → is_open t,
by finish
lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) :=
is_open_sUnion $ assume t ⟨i, (heq : t = f i)⟩, heq.symm ▸ h i
lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) :
is_open (⋃i∈s, f i) :=
is_open_Union $ assume i, is_open_Union $ assume hi, h i hi
@[simp] lemma is_open_empty : is_open (∅ : set α) :=
have is_open (⋃₀ ∅ : set α), from is_open_sUnion (assume a, false.elim),
by simp at this; assumption
lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) :=
finite.induction_on hs (by simp) $ λ a s has hs ih h, begin
suffices : is_open (a ∩ ⋂₀ s), { simpa },
exact is_open_inter (h _ $ mem_insert _ _) (ih $ assume t ht, h _ $ mem_insert_of_mem _ ht)
end
lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) :
(∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) :=
finite.induction_on hs
(by simp)
(by simp [or_imp_distrib, _root_.is_open_inter, forall_and_distrib] {contextual := tt})
lemma is_open_const {p : Prop} : is_open {a : α | p} :=
by_cases
(assume : p, begin simp [*]; exact is_open_univ end)
(assume : ¬ p, begin simp [*]; exact is_open_empty end)
lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} :=
is_open_inter
/-- A set is closed if its complement is open -/
def is_closed (s : set α) : Prop := is_open (-s)
@[simp] lemma is_closed_empty : is_closed (∅ : set α) := by simp [is_closed]
@[simp] lemma is_closed_univ : is_closed (univ : set α) := by simp [is_closed]
lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) :=
by simp [is_closed]; exact is_open_inter
lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) :=
by simp [is_closed, compl_sInter]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht
lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) :=
is_closed_sInter $ assume t ⟨i, (heq : t = f i)⟩, heq.symm ▸ h i
@[simp] lemma is_open_compl_iff {s : set α} : is_open (-s) ↔ is_closed s := iff.rfl
@[simp] lemma is_closed_compl_iff {s : set α} : is_closed (-s) ↔ is_open s :=
by rw [←is_open_compl_iff, compl_compl]
lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) :=
is_open_inter h₁ $ is_open_compl_iff.mpr h₂
lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) :=
by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂
lemma is_closed_Union {s : set β} {f : β → set α} (hs : finite s) :
(∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) :=
finite.induction_on hs
(by simp)
(by simp [or_imp_distrib, is_closed_union, forall_and_distrib] {contextual := tt})
lemma is_closed_imp [topological_space α] {p q : α → Prop}
(hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} :=
have {x | p x → q x} = (- {x | p x}) ∪ {x | q x}, from set.ext $ by finish,
by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq
lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} :=
is_open_compl_iff.mpr
/-- The interior of a set `s` is the largest open subset of `s`. -/
def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s}
lemma mem_interior {s : set α} {x : α} :
x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t :=
by simp [interior, and_comm, and.left_comm]
@[simp] lemma is_open_interior {s : set α} : is_open (interior s) :=
is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁
lemma interior_subset {s : set α} : interior s ⊆ s :=
sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂
lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s :=
subset_sUnion_of_mem ⟨h₂, h₁⟩
lemma interior_eq_of_open {s : set α} (h : is_open s) : interior s = s :=
subset.antisymm interior_subset (interior_maximal (subset.refl s) h)
lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s :=
⟨assume h, h ▸ is_open_interior, interior_eq_of_open⟩
lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s :=
by simp [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset]
lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) :
s ⊆ interior t ↔ s ⊆ t :=
⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩
lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t :=
interior_maximal (subset.trans interior_subset h) is_open_interior
@[simp] lemma interior_empty : interior (∅ : set α) = ∅ :=
interior_eq_of_open is_open_empty
@[simp] lemma interior_univ : interior (univ : set α) = univ :=
interior_eq_of_open is_open_univ
@[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s :=
interior_eq_of_open is_open_interior
@[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t :=
subset.antisymm
(subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t))
(interior_maximal (inter_subset_inter interior_subset interior_subset) $ by simp [is_open_inter])
lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) :
interior (s ∪ t) = interior s :=
have interior (s ∪ t) ⊆ s, from
assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩,
classical.by_contradiction $ assume hx₂ : x ∉ s,
have u \ s ⊆ t,
from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂,
have u \ s ⊆ interior t,
by simp [subset_interior_iff_subset_of_open, this, is_open_diff hu₁ h₁],
have u \ s ⊆ ∅,
by rw [h₂] at this; assumption,
this ⟨hx₁, hx₂⟩,
subset.antisymm
(interior_maximal this is_open_interior)
(interior_mono $ subset_union_left _ _)
lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t :=
by rw ← subset_interior_iff_open; simp [subset_def, mem_interior]
/-- The closure of `s` is the smallest closed set containing `s`. -/
def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t}
@[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) :=
is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁
lemma subset_closure {s : set α} : s ⊆ closure s :=
subset_sInter $ assume t ⟨h₁, h₂⟩, h₂
lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t :=
sInter_subset_of_mem ⟨h₂, h₁⟩
lemma closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s :=
subset.antisymm (closure_minimal (subset.refl s) h) subset_closure
lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s :=
⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩
lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) :
closure s ⊆ t ↔ s ⊆ t :=
⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩
lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_minimal (subset.trans h subset_closure) is_closed_closure
@[simp] lemma closure_empty : closure (∅ : set α) = ∅ :=
closure_eq_of_is_closed is_closed_empty
@[simp] lemma closure_univ : closure (univ : set α) = univ :=
closure_eq_of_is_closed is_closed_univ
@[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s :=
closure_eq_of_is_closed is_closed_closure
@[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t :=
subset.antisymm
(closure_minimal (union_subset_union subset_closure subset_closure) $ by simp [is_closed_union])
(union_subset (closure_mono $ subset_union_left _ _) (closure_mono $ subset_union_right _ _))
lemma interior_subset_closure {s : set α} : interior s ⊆ closure s :=
subset.trans interior_subset subset_closure
lemma closure_eq_compl_interior_compl {s : set α} : closure s = - interior (- s) :=
begin
simp [interior, closure],
rw [compl_sUnion, compl_image_set_of],
simp [compl_subset_compl]
end
@[simp] lemma interior_compl_eq {s : set α} : interior (- s) = - closure s :=
by simp [closure_eq_compl_interior_compl]
@[simp] lemma closure_compl_eq {s : set α} : closure (- s) = - interior s :=
by simp [closure_eq_compl_interior_compl]
lemma closure_compl {s : set α} : closure (-s) = - interior s :=
subset.antisymm
(by simp [closure_subset_iff_subset_of_is_closed, compl_subset_compl, subset.refl])
begin
rw [compl_subset_comm, subset_interior_iff_subset_of_open, compl_subset_comm],
exact subset_closure,
exact is_open_compl_iff.mpr is_closed_closure
end
lemma interior_compl {s : set α} : interior (-s) = - closure s :=
calc interior (- s) = - - interior (- s) : by simp
... = - closure (- (- s)) : by rw [closure_compl]
... = - closure s : by simp
theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → o ∩ s ≠ ∅ :=
⟨λ h o oo ao os,
have s ⊆ -o, from λ x xs xo, @ne_empty_of_mem α (o∩s) x ⟨xo, xs⟩ os,
closure_minimal this (is_closed_compl_iff.2 oo) h ao,
λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc,
let ⟨x, hc, hs⟩ := exists_mem_of_ne_empty (H _ h₁ nc) in hc (h₂ hs)⟩
/-- The frontier of a set is the set of points between the closure and interior. -/
def frontier (s : set α) : set α := closure s \ interior s
lemma frontier_eq_closure_inter_closure {s : set α} :
frontier s = closure s ∩ closure (- s) :=
by rw [closure_compl, frontier, diff_eq]
/-- neighbourhood filter -/
def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s)
lemma tendsto_nhds {m : β → α} {f : filter β} (h : ∀s, a ∈ s → is_open s → m ⁻¹' s ∈ f.sets) :
tendsto m f (nhds a) :=
show map m f ≤ (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s),
from le_infi $ assume s, le_infi $ assume ⟨ha, hs⟩, le_principal_iff.mpr $ h s ha hs
lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (nhds a) :=
tendsto_nhds $ assume s ha hs, univ_mem_sets' $ assume _, ha
lemma nhds_sets {a : α} : (nhds a).sets = {s | ∃t⊆s, is_open t ∧ a ∈ t} :=
calc (nhds a).sets = (⋃s∈{s : set α| a ∈ s ∧ is_open s}, (principal s).sets) : infi_sets_eq'
(assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩,
⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, by simp⟩)
⟨univ, by simp⟩
... = {s | ∃t⊆s, is_open t ∧ a ∈ t} :
le_antisymm
(supr_le $ assume i, supr_le $ assume ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩)
(assume t ⟨i, hi₁, hi₂, hi₃⟩, by simp; exact ⟨i, ⟨hi₃, hi₂⟩, hi₁⟩)
lemma map_nhds {a : α} {f : α → β} :
map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) :=
calc map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, map f (principal s)) :
map_binfi_eq
(assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩,
⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, by simp⟩)
⟨univ, by simp⟩
... = _ : by simp
lemma mem_nhds_sets_iff {a : α} {s : set α} :
s ∈ (nhds a).sets ↔ ∃t⊆s, is_open t ∧ a ∈ t :=
by simp [nhds_sets]
lemma mem_of_nhds {a : α} {s : set α} : s ∈ (nhds a).sets → a ∈ s :=
by simp [mem_nhds_sets_iff]; exact assume t ht _ hs, ht hs
lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) :
s ∈ (nhds a).sets :=
by simp [nhds_sets]; exact ⟨s, subset.refl _, hs, ha⟩
lemma return_le_nhds : return ≤ (nhds : α → filter α) :=
assume a, le_infi $ assume s, le_infi $ assume ⟨h₁, _⟩, principal_mono.mpr $ by simp [h₁]
@[simp] lemma nhds_neq_bot {a : α} : nhds a ≠ ⊥ :=
assume : nhds a = ⊥,
have return a = (⊥ : filter α),
from lattice.bot_unique $ this ▸ return_le_nhds a,
pure_neq_bot this
lemma interior_eq_nhds {s : set α} : interior s = {a | nhds a ≤ principal s} :=
set.ext $ by simp [mem_interior, nhds_sets]
lemma mem_interior_iff_mem_nhds {s : set α} {a : α} :
a ∈ interior s ↔ s ∈ (nhds a).sets :=
by simp [interior_eq_nhds]
lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, nhds a ≤ principal s :=
calc is_open s ↔ interior s = s : by rw [interior_eq_iff_open]
... ↔ s ⊆ interior s : ⟨assume h, by simp [*, subset.refl], subset.antisymm interior_subset⟩
... ↔ (∀a∈s, nhds a ≤ principal s) : by rw [interior_eq_nhds]; refl
lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ (nhds a).sets :=
by simpa using @is_open_iff_nhds α _ _
lemma closure_eq_nhds {s : set α} : closure s = {a | nhds a ⊓ principal s ≠ ⊥} :=
calc closure s = - interior (- s) : closure_eq_compl_interior_compl
... = {a | ¬ nhds a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl
... = {a | nhds a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr
(inf_eq_bot_iff_le_compl
(show principal s ⊔ principal (-s) = ⊤, by simp [principal_univ])
(by simp)).symm
theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ (nhds a).sets, t ∩ s ≠ ∅ :=
mem_closure_iff.trans
⟨λ H t ht, subset_ne_empty
(inter_subset_inter_left _ interior_subset)
(H _ is_open_interior (mem_interior_iff_mem_nhds.2 ht)),
λ H o oo ao, H _ (mem_nhds_sets oo ao)⟩
lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s :=
calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed]
... ↔ closure s ⊆ s : ⟨assume h, by simp [*, subset.refl], assume h, subset.antisymm h subset_closure⟩
... ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl
lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) :=
assume a ⟨hs, ht⟩,
have s ∈ (nhds a).sets, from mem_nhds_sets h hs,
have nhds a ⊓ principal s = nhds a, from inf_of_le_left $ by simp [this],
have nhds a ⊓ principal (s ∩ t) ≠ ⊥,
from calc nhds a ⊓ principal (s ∩ t) = nhds a ⊓ (principal s ⊓ principal t) : by simp
... = nhds a ⊓ principal t : by rw [←inf_assoc, this]
... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption,
by rw [closure_eq_nhds]; assumption
lemma closure_diff {s t : set α} : closure s - closure t ⊆ closure (s - t) :=
calc closure s \ closure t = (- closure t) ∩ closure s : by simp [diff_eq, inter_comm]
... ⊆ closure (- closure t ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure
... = closure (s \ closure t) : by simp [diff_eq, inter_comm]
... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure
lemma mem_of_closed_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α}
(hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (hs : is_closed s) (h : f ⁻¹' s ∈ b.sets) : a ∈ s :=
have b.map f ≤ nhds a ⊓ principal s,
from le_trans (le_inf (le_refl _) (le_principal_iff.mpr h)) (inf_le_inf hf (le_refl _)),
is_closed_iff_nhds.mp hs a $ neq_bot_of_le_neq_bot (map_ne_bot hb) this
lemma mem_closure_of_tendsto {f : β → α} {x : filter β} {a : α} {s : set α}
(hf : tendsto f x (nhds a)) (hs : is_closed s) (h : x ⊓ principal (f ⁻¹' s) ≠ ⊥) : a ∈ s :=
is_closed_iff_nhds.mp hs _ $ neq_bot_of_le_neq_bot (@map_ne_bot _ _ _ f h) $
le_inf (le_trans (map_mono $ inf_le_left) hf) $
le_trans (map_mono $ inf_le_right_of_le $ by simp; exact subset.refl _) (@map_vmap_le _ _ _ f)
/- locally finite family [General Topology (Bourbaki, 1995)] -/
section locally_finite
/-- A family of sets in `set α` is locally finite if at every point `x:α`,
there is a neighborhood of `x` which meets only finitely many sets in the family -/
def locally_finite (f : β → set α) :=
∀x:α, ∃t∈(nhds x).sets, finite {i | f i ∩ t ≠ ∅ }
lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f :=
assume x, ⟨univ, univ_mem_sets, finite_subset h $ by simp⟩
lemma locally_finite_subset
{f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ :=
assume a,
let ⟨t, ht₁, ht₂⟩ := hf₂ a in
⟨t, ht₁, finite_subset ht₂ $ assume i hi,
neq_bot_of_le_neq_bot hi $ inter_subset_inter (hf i) $ subset.refl _⟩
lemma is_closed_Union_of_locally_finite {f : β → set α}
(h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) :=
is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i),
have ∀i, a ∈ -f i,
from assume i hi, by simp at h; exact h i hi,
have ∀i, - f i ∈ (nhds a).sets,
by rw [nhds_sets]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩,
let ⟨t, h_sets, (h_fin : finite {i | f i ∩ t ≠ ∅ })⟩ := h₁ a in
calc nhds a ≤ principal (t ∩ (⋂ i∈{i | f i ∩ t ≠ ∅ }, - f i)) :
begin
rw [le_principal_iff],
apply @filter.inter_mem_sets _ (nhds a) _ _ h_sets,
apply @filter.Inter_mem_sets _ (nhds a) _ _ _ h_fin,
exact assume i h, this i
end
... ≤ principal (- ⋃i, f i) :
begin
simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq,
mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists,
not_eq_empty_iff_exists, exists_imp_distrib, (≠)],
exact assume x xt ht i xfi, ht i x xfi xt xfi
end
end locally_finite
/- compact sets -/
section compact
/-- A set `s` is compact if every filter that contains `s` also meets every
neighborhood of some `a ∈ s`. -/
def compact (s : set α) := ∀f, f ≠ ⊥ → f ≤ principal s → ∃a∈s, f ⊓ nhds a ≠ ⊥
lemma compact_of_is_closed_subset {s t : set α}
(hs : compact s) (ht : is_closed t) (h : t ⊆ s) : compact t :=
assume f hnf hsf,
let ⟨a, hsa, (ha : f ⊓ nhds a ≠ ⊥)⟩ := hs f hnf (le_trans hsf $ by simp [h]) in
have ∀a, principal t ⊓ nhds a ≠ ⊥ → a ∈ t,
by intro a; rw [inf_comm]; rw [is_closed_iff_nhds] at ht; exact ht a,
have a ∈ t,
from this a $ neq_bot_of_le_neq_bot ha $ inf_le_inf hsf (le_refl _),
⟨a, this, ha⟩
lemma compact_adherence_nhdset {s t : set α} {f : filter α}
(hs : compact s) (hf₂ : f ≤ principal s) (ht₁ : is_open t) (ht₂ : ∀a∈s, nhds a ⊓ f ≠ ⊥ → a ∈ t) :
t ∈ f.sets :=
classical.by_cases mem_sets_of_neq_bot $
assume : f ⊓ principal (- t) ≠ ⊥,
let ⟨a, ha, (hfa : f ⊓ principal (-t) ⊓ nhds a ≠ ⊥)⟩ := hs _ this $ inf_le_left_of_le hf₂ in
have a ∈ t,
from ht₂ a ha $ neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_left,
have nhds a ⊓ principal (-t) ≠ ⊥,
from neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_right,
have ∀s∈(nhds a ⊓ principal (-t)).sets, s ≠ ∅,
from forall_sets_neq_empty_iff_neq_bot.mpr this,
have false,
from this _ ⟨t, mem_nhds_sets ht₁ ‹a ∈ t›, -t, subset.refl _, subset.refl _⟩ (by simp),
by contradiction
lemma compact_iff_ultrafilter_le_nhds {s : set α} :
compact s ↔ (∀f, ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a) :=
⟨assume hs : compact s, assume f hf hfs,
let ⟨a, ha, h⟩ := hs _ hf.left hfs in
⟨a, ha, le_of_ultrafilter hf h⟩,
assume hs : (∀f, ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a),
assume f hf hfs,
let ⟨a, ha, (h : ultrafilter_of f ≤ nhds a)⟩ :=
hs (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs) in
have ultrafilter_of f ⊓ nhds a ≠ ⊥,
by simp [inf_of_le_left, h]; exact (ultrafilter_ultrafilter_of hf).left,
⟨a, ha, neq_bot_of_le_neq_bot this (inf_le_inf ultrafilter_of_le (le_refl _))⟩⟩
lemma compact_elim_finite_subcover {s : set α} {c : set (set α)}
(hs : compact s) (hc₁ : ∀t∈c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c' :=
classical.by_contradiction $ assume h,
have h : ∀{c'}, c' ⊆ c → finite c' → ¬ s ⊆ ⋃₀ c',
from assume c' h₁ h₂ h₃, h ⟨c', h₁, h₂, h₃⟩,
let
f : filter α := (⨅c':{c' : set (set α) // c' ⊆ c ∧ finite c'}, principal (s - ⋃₀ c')),
⟨a, ha⟩ := @exists_mem_of_ne_empty α s
(assume h', h (empty_subset _) finite_empty $ h'.symm ▸ empty_subset _)
in
have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩
(assume ⟨c₁, hc₁, hc'₁⟩ ⟨c₂, hc₂, hc'₂⟩, ⟨⟨c₁ ∪ c₂, union_subset hc₁ hc₂, finite_union hc'₁ hc'₂⟩,
principal_mono.mpr $ diff_subset_diff_right $ sUnion_mono $ subset_union_left _ _,
principal_mono.mpr $ diff_subset_diff_right $ sUnion_mono $ subset_union_right _ _⟩)
(assume ⟨c', hc'₁, hc'₂⟩, show principal (s \ _) ≠ ⊥, by simp [diff_eq_empty]; exact h hc'₁ hc'₂),
have f ≤ principal s, from infi_le_of_le ⟨∅, empty_subset _, finite_empty⟩ $
show principal (s \ ⋃₀∅) ≤ principal s, by simp; exact subset.refl s,
let
⟨a, ha, (h : f ⊓ nhds a ≠ ⊥)⟩ := hs f ‹f ≠ ⊥› this,
⟨t, ht₁, (ht₂ : a ∈ t)⟩ := hc₂ ha
in
have f ≤ principal (-t),
from infi_le_of_le ⟨{t}, by simp [ht₁], finite_insert _ finite_empty⟩ $
principal_mono.mpr $
show s - ⋃₀{t} ⊆ - t, begin simp; exact assume x ⟨_, hnt⟩, hnt end,
have is_closed (- t), from is_open_compl_iff.mp $ by simp; exact hc₁ t ht₁,
have a ∈ - t, from is_closed_iff_nhds.mp this _ $ neq_bot_of_le_neq_bot h $
le_inf inf_le_right (inf_le_left_of_le ‹f ≤ principal (- t)›),
this ‹a ∈ t›
lemma compact_elim_finite_subcover_image {s : set α} {b : set β} {c : β → set α}
(hs : compact s) (hc₁ : ∀i∈b, is_open (c i)) (hc₂ : s ⊆ ⋃i∈b, c i) :
∃b'⊆b, finite b' ∧ s ⊆ ⋃i∈b', c i :=
if h : b = ∅ then ⟨∅, by simp, by simp, h ▸ hc₂⟩ else
let ⟨i, hi⟩ := exists_mem_of_ne_empty h in
have hc'₁ : ∀i∈c '' b, is_open i, from assume i ⟨j, hj, h⟩, h ▸ hc₁ _ hj,
have hc'₂ : s ⊆ ⋃₀ (c '' b), by simpa,
let ⟨d, hd₁, hd₂, hd₃⟩ := compact_elim_finite_subcover hs hc'₁ hc'₂ in
have ∀x : d, ∃i, i ∈ b ∧ c i = x, from assume ⟨x, hx⟩, hd₁ hx,
let ⟨f', hf⟩ := axiom_of_choice this,
f := λx:set α, (if h : x ∈ d then f' ⟨x, h⟩ else i : β) in
have ∀(x : α) (i : set α), i ∈ d → x ∈ i → (∃ (i : β), i ∈ f '' d ∧ x ∈ c i),
from assume x i hid hxi, ⟨f i, mem_image_of_mem f hid,
by simpa [f, hid, (hf ⟨_, hid⟩).2] using hxi⟩,
⟨f '' d,
assume i ⟨j, hj, h⟩,
h ▸ by simpa [f, hj] using (hf ⟨_, hj⟩).1,
finite_image f hd₂,
subset.trans hd₃ $ by simpa [subset_def]⟩
lemma compact_of_finite_subcover {s : set α}
(h : ∀c, (∀t∈c, is_open t) → s ⊆ ⋃₀ c → ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c') : compact s :=
assume f hfn hfs, classical.by_contradiction $ assume : ¬ (∃x∈s, f ⊓ nhds x ≠ ⊥),
have hf : ∀x∈s, nhds x ⊓ f = ⊥,
by simpa [not_and, inf_comm],
have ¬ ∃x∈s, ∀t∈f.sets, x ∈ closure t,
from assume ⟨x, hxs, hx⟩,
have ∅ ∈ (nhds x ⊓ f).sets, by rw [empty_in_sets_eq_bot, hf x hxs],
let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := by rw [mem_inf_sets] at this; exact this in
have ∅ ∈ (nhds x ⊓ principal t₂).sets,
from (nhds x ⊓ principal t₂).upwards_sets (inter_mem_inf_sets ht₁ (subset.refl t₂)) ht,
have nhds x ⊓ principal t₂ = ⊥,
by rwa [empty_in_sets_eq_bot] at this,
by simp [closure_eq_nhds] at hx; exact hx t₂ ht₂ this,
have ∀x∈s, ∃t∈f.sets, x ∉ closure t, by simpa [_root_.not_forall],
let c := (λt, - closure t) '' f.sets, ⟨c', hcc', hcf, hsc'⟩ := h c
(assume t ⟨s, hs, h⟩, h ▸ is_closed_closure) (by simpa [subset_def]) in
let ⟨b, hb⟩ := axiom_of_choice $
show ∀s:c', ∃t, t ∈ f.sets ∧ - closure t = s,
from assume ⟨x, hx⟩, hcc' hx in
have (⋂s∈c', if h : s ∈ c' then b ⟨s, h⟩ else univ) ∈ f.sets,
from Inter_mem_sets hcf $ assume t ht, by rw [dif_pos ht]; exact (hb ⟨t, ht⟩).left,
have s ∩ (⋂s∈c', if h : s ∈ c' then b ⟨s, h⟩ else univ) ∈ f.sets,
from inter_mem_sets (by simp at hfs; assumption) this,
have ∅ ∈ f.sets,
from f.upwards_sets this $ assume x ⟨hxs, hxi⟩,
let ⟨t, htc', hxt⟩ := (show ∃t ∈ c', x ∈ t, by simpa using hsc' hxs) in
have -closure (b ⟨t, htc'⟩) = t, from (hb _).right,
have x ∈ - t,
from this ▸ (calc x ∈ b ⟨t, htc'⟩ : by simp at hxi; have h := hxi t htc'; rwa [dif_pos htc'] at h
... ⊆ closure (b ⟨t, htc'⟩) : subset_closure
... ⊆ - - closure (b ⟨t, htc'⟩) : by simp; exact subset.refl _),
show false, from this hxt,
hfn $ by rwa [empty_in_sets_eq_bot] at this
lemma compact_iff_finite_subcover {s : set α} :
compact s ↔ (∀c, (∀t∈c, is_open t) → s ⊆ ⋃₀ c → ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c') :=
⟨assume hc c, compact_elim_finite_subcover hc, compact_of_finite_subcover⟩
lemma compact_empty : compact (∅ : set α) :=
assume f hnf hsf, not.elim hnf $
by simpa [empty_in_sets_eq_bot] using hsf
lemma compact_singleton {a : α} : compact ({a} : set α) :=
compact_of_finite_subcover $ assume c hc₁ hc₂,
let ⟨i, hic, hai⟩ := (show ∃i ∈ c, a ∈ i, by simpa using hc₂) in
⟨{i}, by simp [hic], finite_singleton _, by simp [hai]⟩
lemma compact_bUnion_of_compact {s : set β} {f : β → set α} (hs : finite s) :
(∀i ∈ s, compact (f i)) → compact (⋃i ∈ s, f i) :=
assume hf, compact_of_finite_subcover $ assume c c_open c_cover,
have ∀i : subtype s, ∃c' ⊆ c, finite c' ∧ f i ⊆ ⋃₀ c', from
assume ⟨i, hi⟩, compact_elim_finite_subcover (hf i hi) c_open
(calc f i ⊆ ⋃i ∈ s, f i : subset_bUnion_of_mem hi
... ⊆ ⋃₀ c : c_cover),
let ⟨finite_subcovers, h⟩ := axiom_of_choice this in
let c' := ⋃i, finite_subcovers i in
have c' ⊆ c, from Union_subset (λi, (h i).fst),
have finite c', from @finite_Union _ _ hs.fintype _ (λi, (h i).snd.1),
have (⋃i ∈ s, f i) ⊆ ⋃₀ c', from bUnion_subset $ λi hi, calc
f i ⊆ ⋃₀ finite_subcovers ⟨i,hi⟩ : (h ⟨i,hi⟩).snd.2
... ⊆ ⋃₀ c' : sUnion_mono (subset_Union _ _),
⟨c', ‹c' ⊆ c›, ‹finite c'›, this⟩
lemma compact_of_finite {s : set α} (hs : finite s) : compact s :=
let s' : set α := ⋃i ∈ s, {i} in
have e : s' = s, from ext $ λi, by simp,
have compact s', from compact_bUnion_of_compact hs (λ_ _, compact_singleton),
e ▸ this
end compact
/- separation axioms -/
section separation
/-- A T₁ space, also known as a Fréchet space, is a topological space
where for every pair `x ≠ y`, there is an open set containing `x` and not `y`.
Equivalently, every singleton set is closed. -/
class t1_space (α : Type u) [topological_space α] :=
(t1 : ∀x, is_closed ({x} : set α))
lemma is_closed_singleton [t1_space α] {x : α} : is_closed ({x} : set α) :=
t1_space.t1 x
lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : - {x} ∈ (nhds y).sets :=
mem_nhds_sets is_closed_singleton $ by simp; exact h
@[simp] lemma closure_singleton [topological_space α] [t1_space α] {a : α} :
closure ({a} : set α) = {a} :=
closure_eq_of_is_closed is_closed_singleton
/-- A T₂ space, also known as a Hausdorff space, is one in which for every
`x ≠ y` there exists disjoint open sets around `x` and `y`. This is
the most widely used of the separation axioms. -/
class t2_space (α : Type u) [topological_space α] :=
(t2 : ∀x y, x ≠ y → ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅)
lemma t2_separation [t2_space α] {x y : α} (h : x ≠ y) :
∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ :=
t2_space.t2 x y h
instance t2_space.t1_space [topological_space α] [t2_space α] : t1_space α :=
⟨assume x,
have ∀y, y ≠ x ↔ ∃ (i : set α), (x ∉ i ∧ is_open i) ∧ y ∈ i,
from assume y, ⟨assume h',
let ⟨u, v, hu, hv, hy, hx, h⟩ := t2_separation h' in
have x ∉ u,
from assume : x ∈ u,
have x ∈ u ∩ v, from ⟨this, hx⟩,
by rwa [h] at this,
⟨u, ⟨this, hu⟩, hy⟩,
assume ⟨s, ⟨hx, hs⟩, hy⟩ h, hx $ h ▸ hy⟩,
have (-{x} : set α) = (⋃s∈{s : set α | x ∉ s ∧ is_open s}, s),
by apply set.ext; simpa,
show is_open (- {x}),
by rw [this]; exact (is_open_Union $ assume s, is_open_Union $ assume ⟨_, hs⟩, hs)⟩
lemma eq_of_nhds_neq_bot [ht : t2_space α] {x y : α} (h : nhds x ⊓ nhds y ≠ ⊥) : x = y :=
classical.by_contradiction $ assume : x ≠ y,
let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 x y this in
have u ∩ v ∈ (nhds x ⊓ nhds y).sets,
from inter_mem_inf_sets (mem_nhds_sets hu hx) (mem_nhds_sets hv hy),
h $ empty_in_sets_eq_bot.mp $ huv ▸ this
@[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : nhds a = nhds b ↔ a = b :=
⟨assume h, eq_of_nhds_neq_bot $ by simp [h], assume h, h ▸ rfl⟩
@[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : nhds a ≤ nhds b ↔ a = b :=
⟨assume h, eq_of_nhds_neq_bot $ by simp [inf_of_le_left h], assume h, h ▸ le_refl _⟩
lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α}
(hl : l ≠ ⊥) (ha : tendsto f l (nhds a)) (hb : tendsto f l (nhds b)) : a = b :=
eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot (map_ne_bot hl) $ le_inf ha hb
end separation
section regularity
/-- A T₃ space, also known as a regular space (although this condition sometimes
omits T₂), is one in which for every closed `C` and `x ∉ C`, there exist
disjoint open sets containing `x` and `C` respectively. -/
class regular_space (α : Type u) [topological_space α] extends t2_space α :=
(regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ nhds a ⊓ principal t = ⊥)
lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ (nhds a).sets) :
∃t∈(nhds a).sets, t ⊆ s ∧ is_closed t :=
let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in
have ∃t, is_open t ∧ -s' ⊆ t ∧ nhds a ⊓ principal t = ⊥,
from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃),
let ⟨t, ht₁, ht₂, ht₃⟩ := this in
⟨-t,
mem_sets_of_neq_bot $ by simp; exact ht₃,
subset.trans (compl_subset_comm.1 ht₂) h₁,
is_closed_compl_iff.mpr ht₁⟩
end regularity
/- generating sets -/
end topological_space
namespace topological_space
variables {α : Type u}
/-- The least topology containing a collection of basic sets. -/
inductive generate_open (g : set (set α)) : set α → Prop
| basic : ∀s∈g, generate_open s
| univ : generate_open univ
| inter : ∀s t, generate_open s → generate_open t → generate_open (s ∩ t)
| sUnion : ∀k, (∀s∈k, generate_open s) → generate_open (⋃₀ k)
/-- The smallest topological space containing the collection `g` of basic sets -/
def generate_from (g : set (set α)) : topological_space α :=
{ is_open := generate_open g,
is_open_univ := generate_open.univ g,
is_open_inter := generate_open.inter,
is_open_sUnion := generate_open.sUnion }
lemma nhds_generate_from {g : set (set α)} {a : α} :
@nhds α (generate_from g) a = (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) :=
le_antisymm
(infi_le_infi $ assume s, infi_le_infi_const $ assume ⟨as, sg⟩, ⟨as, generate_open.basic _ sg⟩)
(le_infi $ assume s, le_infi $ assume ⟨as, hs⟩,
have ∀s, generate_open g s → a ∈ s → (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) ≤ principal s,
begin
intros s hs,
induction hs,
case generate_open.basic : s hs
{ exact assume as, infi_le_of_le s $ infi_le _ ⟨as, hs⟩ },
case generate_open.univ
{ rw [principal_univ],
exact assume _, le_top },
case generate_open.inter : s t hs' ht' hs ht
{ exact assume ⟨has, hat⟩, calc _ ≤ principal s ⊓ principal t : le_inf (hs has) (ht hat)
... = _ : by simp },
case generate_open.sUnion : k hk' hk
{ exact λ ⟨t, htk, hat⟩, calc _ ≤ principal t : hk t htk hat
... ≤ _ : begin simp; exact subset_sUnion_of_mem htk end }
end,
this s hs as)
end topological_space
section lattice
variables {α : Type u} {β : Type v}
instance : partial_order (topological_space α) :=
{ le := λt s, t.is_open ≤ s.is_open,
le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₁ h₂,
le_refl := assume t, le_refl t.is_open,
le_trans := assume a b c h₁ h₂, @le_trans _ _ a.is_open b.is_open c.is_open h₁ h₂ }
instance : has_Inf (topological_space α) := ⟨λ tt,
{ is_open := λs, ∀t∈tt, topological_space.is_open t s,
is_open_univ := assume t h, t.is_open_univ,
is_open_inter := assume s₁ s₂ h₁ h₂ t ht, t.is_open_inter s₁ s₂ (h₁ t ht) (h₂ t ht),
is_open_sUnion := assume s h t ht, t.is_open_sUnion _ $ assume s' hss', h _ hss' _ ht }⟩
private lemma Inf_le {tt : set (topological_space α)} {t : topological_space α} (h : t ∈ tt) :
Inf tt ≤ t :=
assume s hs, hs t h
private lemma le_Inf {tt : set (topological_space α)} {t : topological_space α} (h : ∀t'∈tt, t ≤ t') :
t ≤ Inf tt :=
assume s hs t' ht', h t' ht' s hs
instance : has_inf (topological_space α) := ⟨λ t₁ t₂,
{ is_open := λs, t₁.is_open s ∧ t₂.is_open s,
is_open_univ := ⟨t₁.is_open_univ, t₂.is_open_univ⟩,
is_open_inter := assume s₁ s₂ ⟨h₁₁, h₁₂⟩ ⟨h₂₁, h₂₂⟩, ⟨t₁.is_open_inter s₁ s₂ h₁₁ h₂₁, t₂.is_open_inter s₁ s₂ h₁₂ h₂₂⟩,
is_open_sUnion := assume s h, ⟨t₁.is_open_sUnion _ $ assume t ht, (h t ht).left, t₂.is_open_sUnion _ $ assume t ht, (h t ht).right⟩ }⟩
instance : has_top (topological_space α) :=
⟨{is_open := λs, true,
is_open_univ := trivial,
is_open_inter := assume a b ha hb, trivial,
is_open_sUnion := assume s h, trivial }⟩
instance {α : Type u} : complete_lattice (topological_space α) :=
{ sup := λa b, Inf {x | a ≤ x ∧ b ≤ x},
le_sup_left := assume a b, le_Inf $ assume x, assume h : a ≤ x ∧ b ≤ x, h.left,
le_sup_right := assume a b, le_Inf $ assume x, assume h : a ≤ x ∧ b ≤ x, h.right,
sup_le := assume a b c h₁ h₂, Inf_le $ show c ∈ {x | a ≤ x ∧ b ≤ x}, from ⟨h₁, h₂⟩,
inf := (⊓),
le_inf := assume a b h h₁ h₂ s hs, ⟨h₁ s hs, h₂ s hs⟩,
inf_le_left := assume a b s ⟨h₁, h₂⟩, h₁,
inf_le_right := assume a b s ⟨h₁, h₂⟩, h₂,
top := ⊤,
le_top := assume a t ht, trivial,
bot := Inf univ,
bot_le := assume a, Inf_le $ mem_univ a,
Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t},
le_Sup := assume s f h, le_Inf $ assume t ht, ht _ h,
Sup_le := assume s f h, Inf_le $ assume t ht, h _ ht,
Inf := Inf,
le_Inf := assume s a, le_Inf,
Inf_le := assume s a, Inf_le,
..topological_space.partial_order }
lemma le_of_nhds_le_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₂ x ≤ @nhds α t₁ x) :
t₁ ≤ t₂ :=
assume s, show @is_open α t₁ s → @is_open α t₂ s,
begin simp [is_open_iff_nhds]; exact assume hs a ha, h _ $ hs _ ha end
lemma eq_of_nhds_eq_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₂ x = @nhds α t₁ x) :
t₁ = t₂ :=
le_antisymm
(le_of_nhds_le_nhds $ assume x, le_of_eq $ h x)
(le_of_nhds_le_nhds $ assume x, le_of_eq $ (h x).symm)
end lattice
section galois_connection
variables {α : Type u} {β : Type v}
/-- Given `f : α → β` and a topology on `β`, the induced topology on `α` is the collection of
sets that are preimages of some open set in `β`. This is the coarsest topology that
makes `f` continuous. -/
def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) :
topological_space α :=
{ is_open := λs, ∃s', t.is_open s' ∧ s = f ⁻¹' s',
is_open_univ := ⟨univ, by simp; exact t.is_open_univ⟩,
is_open_inter := assume s₁ s₂ ⟨s'₁, hs₁, eq₁⟩ ⟨s'₂, hs₂, eq₂⟩,
⟨s'₁ ∩ s'₂, by simp [eq₁, eq₂]; exact t.is_open_inter _ _ hs₁ hs₂⟩,
is_open_sUnion := assume s h,
begin
simp [classical.skolem] at h,
cases h with f hf,
apply exists.intro (⋃(x : set α) (h : x ∈ s), f x h),
simp [sUnion_eq_bUnion, (λx h, (hf x h).right.symm)],
exact (@is_open_Union β _ t _ $ assume i,
show is_open (⋃h, f i h), from @is_open_Union β _ t _ $ assume h, (hf i h).left)
end }
lemma is_closed_induced_iff [t : topological_space β] {s : set α} {f : α → β} :
@is_closed α (t.induced f) s ↔ (∃t, is_closed t ∧ s = f ⁻¹' t) :=
⟨assume ⟨t, ht, heq⟩, ⟨-t, by simp; assumption, by simp [preimage_compl, heq.symm]⟩,
assume ⟨t, ht, heq⟩, ⟨-t, ht, by simp [preimage_compl, heq.symm]⟩⟩
/-- Given `f : α → β` and a topology on `α`, the coinduced topology on `β` is defined
such that `s:set β` is open if the preimage of `s` is open. This is the finest topology that
makes `f` continuous. -/
def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) :
topological_space β :=
{ is_open := λs, t.is_open (f ⁻¹' s),
is_open_univ := by simp; exact t.is_open_univ,
is_open_inter := assume s₁ s₂ h₁ h₂, by simp; exact t.is_open_inter _ _ h₁ h₂,
is_open_sUnion := assume s h, by rw [preimage_sUnion]; exact (@is_open_Union _ _ t _ $ assume i,
show is_open (⋃ (H : i ∈ s), f ⁻¹' i), from
@is_open_Union _ _ t _ $ assume hi, h i hi) }
variables {t t₁ t₂ : topological_space α} {t' : topological_space β} {f : α → β} {g : β → α}
lemma induced_le_iff_le_coinduced {f : α → β } {tα : topological_space α} {tβ : topological_space β} :
tβ.induced f ≤ tα ↔ tβ ≤ tα.coinduced f :=
iff.intro
(assume h s hs, show tα.is_open (f ⁻¹' s), from h _ ⟨s, hs, rfl⟩)
(assume h s ⟨t, ht, hst⟩, hst.symm ▸ h _ ht)
lemma gc_induced_coinduced (f : α → β) :
galois_connection (topological_space.induced f) (topological_space.coinduced f) :=
assume f g, induced_le_iff_le_coinduced
lemma induced_mono (h : t₁ ≤ t₂) : t₁.induced g ≤ t₂.induced g :=
(gc_induced_coinduced g).monotone_l h
lemma coinduced_mono (h : t₁ ≤ t₂) : t₁.coinduced f ≤ t₂.coinduced f :=
(gc_induced_coinduced f).monotone_u h
@[simp] lemma induced_bot : (⊥ : topological_space α).induced g = ⊥ :=
(gc_induced_coinduced g).l_bot
@[simp] lemma induced_sup : (t₁ ⊔ t₂).induced g = t₁.induced g ⊔ t₂.induced g :=
(gc_induced_coinduced g).l_sup
@[simp] lemma induced_supr {ι : Sort w} {t : ι → topological_space α} :
(⨆i, t i).induced g = (⨆i, (t i).induced g) :=
(gc_induced_coinduced g).l_supr
@[simp] lemma coinduced_top : (⊤ : topological_space α).coinduced f = ⊤ :=
(gc_induced_coinduced f).u_top
@[simp] lemma coinduced_inf : (t₁ ⊓ t₂).coinduced f = t₁.coinduced f ⊓ t₂.coinduced f :=
(gc_induced_coinduced f).u_inf
@[simp] lemma coinduced_infi {ι : Sort w} {t : ι → topological_space α} :
(⨅i, t i).coinduced f = (⨅i, (t i).coinduced f) :=
(gc_induced_coinduced f).u_infi
end galois_connection
/- constructions using the complete lattice structure -/
section constructions
open topological_space
variables {α : Type u} {β : Type v}
instance inhabited_topological_space {α : Type u} : inhabited (topological_space α) :=
⟨⊤⟩
lemma t2_space_top : @t2_space α ⊤ :=
{ t2 := assume x y hxy, ⟨{x}, {y}, trivial, trivial, mem_insert _ _, mem_insert _ _,
eq_empty_iff_forall_not_mem.2 $ by intros z hz; simp at hz; cc⟩ }
instance : topological_space empty := ⊤
instance : topological_space unit := ⊤
instance : topological_space bool := ⊤
instance : topological_space ℕ := ⊤
instance : topological_space ℤ := ⊤
instance sierpinski_space : topological_space Prop :=
generate_from {{true}}
instance {p : α → Prop} [t : topological_space α] : topological_space (subtype p) :=
induced subtype.val t
instance {r : α → α → Prop} [t : topological_space α] : topological_space (quot r) :=
coinduced (quot.mk r) t
instance {s : setoid α} [t : topological_space α] : topological_space (quotient s) :=
coinduced quotient.mk t
instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α × β) :=
induced prod.fst t₁ ⊔ induced prod.snd t₂
instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α ⊕ β) :=
coinduced sum.inl t₁ ⊓ coinduced sum.inr t₂
instance {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (sigma β) :=
⨅a, coinduced (sigma.mk a) (t₂ a)
instance Pi.topological_space {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (Πa, β a) :=
⨆a, induced (λf, f a) (t₂ a)
lemma generate_from_le {t : topological_space α} { g : set (set α) } (h : ∀s∈g, is_open s) :
generate_from g ≤ t :=
assume s (hs : generate_open g s), generate_open.rec_on hs h
is_open_univ
(assume s t _ _ hs ht, is_open_inter hs ht)
(assume k _ hk, is_open_sUnion hk)
lemma supr_eq_generate_from {ι : Sort w} { g : ι → topological_space α } :
supr g = generate_from (⋃i, {s | (g i).is_open s}) :=
le_antisymm
(supr_le $ assume i s is_open_s,
generate_open.basic _ $ by simp; exact ⟨i, is_open_s⟩)
(generate_from_le $ assume s,
begin
simp,
exact assume i is_open_s,
have g i ≤ supr g, from le_supr _ _,
this s is_open_s
end)
lemma sup_eq_generate_from { g₁ g₂ : topological_space α } :
g₁ ⊔ g₂ = generate_from {s | g₁.is_open s ∨ g₂.is_open s} :=
le_antisymm
(sup_le (assume s, generate_open.basic _ ∘ or.inl) (assume s, generate_open.basic _ ∘ or.inr))
(generate_from_le $ assume s hs,
have h₁ : g₁ ≤ g₁ ⊔ g₂, from le_sup_left,
have h₂ : g₂ ≤ g₁ ⊔ g₂, from le_sup_right,
or.rec_on hs (h₁ s) (h₂ s))
lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) : @nhds α t₂ a ≤ @nhds α t₁ a :=
infi_le_infi $ assume s, infi_le_infi2 $ assume ⟨ha, hs⟩, ⟨⟨ha, h _ hs⟩, le_refl _⟩
lemma nhds_supr {ι : Sort w} {t : ι → topological_space α} {a : α} :
@nhds α (supr t) a = (⨅i, @nhds α (t i) a) :=
le_antisymm
(le_infi $ assume i, nhds_mono $ le_supr _ _)
begin
rw [supr_eq_generate_from, nhds_generate_from],
exact (le_infi $ assume s, le_infi $ assume ⟨hs, hi⟩,
begin
simp at hi, cases hi with i hi,
exact (infi_le_of_le i $ le_principal_iff.mpr $ @mem_nhds_sets α (t i) _ _ hi hs)
end)
end
lemma nhds_sup {t₁ t₂ : topological_space α} {a : α} :
@nhds α (t₁ ⊔ t₂) a = @nhds α t₁ a ⊓ @nhds α t₂ a :=
calc @nhds α (t₁ ⊔ t₂) a = @nhds α (⨆b:bool, cond b t₁ t₂) a : by rw [supr_bool_eq]
... = (⨅b, @nhds α (cond b t₁ t₂) a) : begin rw [nhds_supr] end
... = @nhds α t₁ a ⊓ @nhds α t₂ a : by rw [infi_bool_eq]
private lemma separated_by_f
[tα : topological_space α] [tβ : topological_space β] [t2_space β]
(f : α → β) (hf : induced f tβ ≤ tα) {x y : α} (h : f x ≠ f y) :
∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ :=
let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in
⟨f ⁻¹' u, f ⁻¹' v, hf _ ⟨u, uo, rfl⟩, hf _ ⟨v, vo, rfl⟩, xu, yv,
by rw [←preimage_inter, uv, preimage_empty]⟩
instance {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) :=
⟨assume x y h,
separated_by_f subtype.val (le_refl _) (mt subtype.eq h)⟩
instance [t₁ : topological_space α] [t2_space α] [t₂ : topological_space β] [t2_space β] :
t2_space (α × β) :=
⟨assume ⟨x₁,x₂⟩ ⟨y₁,y₂⟩ h,
or.elim (not_and_distrib.mp (mt prod.ext_iff.mpr h))
(λ h₁, separated_by_f prod.fst le_sup_left h₁)
(λ h₂, separated_by_f prod.snd le_sup_right h₂)⟩
instance Pi.t2_space {β : α → Type v} [t₂ : Πa, topological_space (β a)] [Πa, t2_space (β a)] :
t2_space (Πa, β a) :=
⟨assume x y h,
let ⟨i, hi⟩ := not_forall.mp (mt funext h) in
separated_by_f (λz, z i) (le_supr _ i) hi⟩
end constructions
namespace topological_space
/- countability axioms
For our applications we are interested that there exists a countable basis, but we do not need the
concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins.
-/
variables {α : Type u} [t : topological_space α]
include t
/-- A topological basis is one that satisfies the necessary conditions so that
it suffices to take unions of the basis sets to get a topology (without taking
finite intersections as well). -/
def is_topological_basis (s : set (set α)) : Prop :=
(∀t₁∈s, ∀t₂∈s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃∈s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) ∧
(⋃₀ s) = univ ∧
t = generate_from s
lemma is_topological_basis_of_subbasis {s : set (set α)} (hs : t = generate_from s) :
is_topological_basis ((λf, ⋂₀ f) '' {f:set (set α) | finite f ∧ f ⊆ s ∧ ⋂₀ f ≠ ∅}) :=
let b' := (λf, ⋂₀ f) '' {f:set (set α) | finite f ∧ f ⊆ s ∧ ⋂₀ f ≠ ∅} in
⟨assume s₁ ⟨t₁, ⟨hft₁, ht₁b, ht₁⟩, eq₁⟩ s₂ ⟨t₂, ⟨hft₂, ht₂b, ht₂⟩, eq₂⟩,
have ie : ⋂₀(t₁ ∪ t₂) = ⋂₀ t₁ ∩ ⋂₀ t₂, from Inf_union,
eq₁ ▸ eq₂ ▸ assume x h,
⟨_, ⟨t₁ ∪ t₂, ⟨finite_union hft₁ hft₂, union_subset ht₁b ht₂b,
by simpa [ie] using ne_empty_of_mem h⟩, ie⟩, h, subset.refl _⟩,
eq_univ_iff_forall.2 $ assume a, ⟨univ, ⟨∅, by simp; exact (@empty_ne_univ _ ⟨a⟩).symm⟩, mem_univ _⟩,
have generate_from s = generate_from b',
from le_antisymm
(generate_from_le $ assume s hs,
by_cases
(assume : s = ∅, by rw [this]; apply @is_open_empty _ _)
(assume : s ≠ ∅, generate_open.basic _ ⟨{s}, by simp [this, hs]⟩))
(generate_from_le $ assume u ⟨t, ⟨hft, htb, ne⟩, eq⟩,
eq ▸ @is_open_sInter _ (generate_from s) _ hft (assume s hs, generate_open.basic _ $ htb hs)),
this ▸ hs⟩
lemma is_topological_basis_of_open_of_nhds {s : set (set α)}
(h_open : ∀ u ∈ s, _root_.is_open u)
(h_nhds : ∀(a:α) (u : set α), a ∈ u → _root_.is_open u → ∃v ∈ s, a ∈ v ∧ v ⊆ u) :
is_topological_basis s :=
⟨assume t₁ ht₁ t₂ ht₂ x ⟨xt₁, xt₂⟩,
h_nhds x (t₁ ∩ t₂) ⟨xt₁, xt₂⟩
(is_open_inter _ _ _ (h_open _ ht₁) (h_open _ ht₂)),
eq_univ_iff_forall.2 $ assume a,
let ⟨u, h₁, h₂, _⟩ := h_nhds a univ trivial (is_open_univ _) in
⟨u, h₁, h₂⟩,
le_antisymm
(assume u hu,
(@is_open_iff_nhds α (generate_from _) _).mpr $ assume a hau,
let ⟨v, hvs, hav, hvu⟩ := h_nhds a u hau hu in
by rw nhds_generate_from; exact infi_le_of_le v (infi_le_of_le ⟨hav, hvs⟩ $ by simp [hvu]))
(generate_from_le h_open)⟩
lemma mem_nhds_of_is_topological_basis {a : α} {s : set α} {b : set (set α)}
(hb : is_topological_basis b) : s ∈ (nhds a).sets ↔ ∃t∈b, a ∈ t ∧ t ⊆ s :=
begin
rw [hb.2.2, nhds_generate_from, infi_sets_eq'],
{ simpa [and_comm, and.left_comm] },
{ exact assume s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩,
have a ∈ s ∩ t, from ⟨hs₁, ht₁⟩,
let ⟨u, hu₁, hu₂, hu₃⟩ := hb.1 _ hs₂ _ ht₂ _ this in
⟨u, ⟨hu₂, hu₁⟩, by simpa using hu₃⟩ },
{ suffices : a ∈ (⋃₀ b), { simpa [and_comm] },
{ rw [hb.2.1], trivial } }
end
lemma is_open_of_is_topological_basis {s : set α} {b : set (set α)}
(hb : is_topological_basis b) (hs : s ∈ b) : _root_.is_open s :=
is_open_iff_mem_nhds.2 $ λ a as,
(mem_nhds_of_is_topological_basis hb).2 ⟨s, hs, as, subset.refl _⟩
lemma mem_basis_subset_of_mem_open {b : set (set α)}
(hb : is_topological_basis b) {a:α} {u : set α} (au : a ∈ u)
(ou : _root_.is_open u) : ∃v ∈ b, a ∈ v ∧ v ⊆ u :=
(mem_nhds_of_is_topological_basis hb).1 $ mem_nhds_sets ou au
lemma sUnion_basis_of_is_open {B : set (set α)}
(hB : is_topological_basis B) {u : set α} (ou : _root_.is_open u) :
∃ S ⊆ B, u = ⋃₀ S :=
⟨{s ∈ B | s ⊆ u}, λ s h, h.1, set.ext $ λ a,
⟨λ ha, let ⟨b, hb, ab, bu⟩ := mem_basis_subset_of_mem_open hB ha ou in
⟨b, ⟨hb, bu⟩, ab⟩,
λ ⟨b, ⟨hb, bu⟩, ab⟩, bu ab⟩⟩
lemma Union_basis_of_is_open {B : set (set α)}
(hB : is_topological_basis B) {u : set α} (ou : _root_.is_open u) :
∃ (β : Type u) (f : β → set α), u = (⋃ i, f i) ∧ ∀ i, f i ∈ B :=
let ⟨S, sb, su⟩ := sUnion_basis_of_is_open hB ou in
⟨S, subtype.val, su.trans set.sUnion_eq_Union, λ ⟨b, h⟩, sb h⟩
variables (α)
/-- A separable space is one with a countable dense subset. -/
class separable_space : Prop :=
(exists_countable_closure_eq_univ : ∃s:set α, countable s ∧ closure s = univ)
/-- A first-countable space is one in which every point has a
countable neighborhood basis. -/
class first_countable_topology : Prop :=
(nhds_generated_countable : ∀a:α, ∃s:set (set α), countable s ∧ nhds a = (⨅t∈s, principal t))
/-- A second-countable space is one with a countable basis. -/
class second_countable_topology : Prop :=
(is_open_generated_countable : ∃b:set (set α), countable b ∧ t = topological_space.generate_from b)
instance second_countable_topology.to_first_countable_topology
[second_countable_topology α] : first_countable_topology α :=
let ⟨b, hb, eq⟩ := second_countable_topology.is_open_generated_countable α in
⟨assume a, ⟨{s | a ∈ s ∧ s ∈ b},
countable_subset (assume x ⟨_, hx⟩, hx) hb, by rw [eq, nhds_generate_from]⟩⟩
lemma is_open_generated_countable_inter [second_countable_topology α] :
∃b:set (set α), countable b ∧ ∅ ∉ b ∧ is_topological_basis b :=
let ⟨b, hb₁, hb₂⟩ := second_countable_topology.is_open_generated_countable α in
let b' := (λs, ⋂₀ s) '' {s:set (set α) | finite s ∧ s ⊆ b ∧ ⋂₀ s ≠ ∅} in
⟨b',
countable_image _ $ countable_subset (by simp {contextual:=tt}) (countable_set_of_finite_subset hb₁),
assume ⟨s, ⟨_, _, hn⟩, hp⟩, hn hp,
is_topological_basis_of_subbasis hb₂⟩
instance second_countable_topology.to_separable_space
[second_countable_topology α] : separable_space α :=
let ⟨b, hb₁, hb₂, hb₃, hb₄, eq⟩ := is_open_generated_countable_inter α in
have nhds_eq : ∀a, nhds a = (⨅ s : {s : set α // a ∈ s ∧ s ∈ b}, principal s.val),
by intro a; rw [eq, nhds_generate_from]; simp [infi_subtype],
have ∀s∈b, ∃a, a ∈ s, from assume s hs, exists_mem_of_ne_empty $ assume eq, hb₂ $ eq ▸ hs,
have ∃f:∀s∈b, α, ∀s h, f s h ∈ s, by simp only [skolem] at this; exact this,
let ⟨f, hf⟩ := this in
⟨⟨(⋃s∈b, ⋃h:s∈b, {f s h}),
countable_bUnion hb₁ (by simp [countable_Union_Prop]),
set.ext $ assume a,
have a ∈ (⋃₀ b), by rw [hb₄]; exact trivial,
let ⟨t, ht₁, ht₂⟩ := this in
have w : {s : set α // a ∈ s ∧ s ∈ b}, from ⟨t, ht₂, ht₁⟩,
suffices (⨅ (x : {s // a ∈ s ∧ s ∈ b}), principal (x.val ∩ ⋃s (h₁ h₂ : s ∈ b), {f s h₂})) ≠ ⊥,
by simpa [closure_eq_nhds, nhds_eq, infi_inf w],
infi_neq_bot_of_directed ⟨a⟩
(assume ⟨s₁, has₁, hs₁⟩ ⟨s₂, has₂, hs₂⟩,
have a ∈ s₁ ∩ s₂, from ⟨has₁, has₂⟩,
let ⟨s₃, hs₃, has₃, hs⟩ := hb₃ _ hs₁ _ hs₂ _ this in
⟨⟨s₃, has₃, hs₃⟩, begin
simp only [le_principal_iff, mem_principal_sets],
simp at hs, split; apply inter_subset_inter_left; simp [hs]
end⟩)
(assume ⟨s, has, hs⟩,
have s ∩ (⋃ (s : set α) (H h : s ∈ b), {f s h}) ≠ ∅,
from ne_empty_of_mem ⟨hf _ hs, mem_bUnion hs $ mem_Union.mpr ⟨hs, by simp⟩⟩,
by simp [this]) ⟩⟩
lemma is_open_sUnion_countable [second_countable_topology α]
(S : set (set α)) (H : ∀ s ∈ S, _root_.is_open s) :
∃ T : set (set α), countable T ∧ T ⊆ S ∧ ⋃₀ T = ⋃₀ S :=
let ⟨B, cB, _, bB⟩ := is_open_generated_countable_inter α in
begin
let B' := {b ∈ B | ∃ s ∈ S, b ⊆ s},
rcases axiom_of_choice (λ b:B', b.2.2) with ⟨f, hf⟩,
change B' → set α at f,
haveI : encodable B' := (countable_subset (sep_subset _ _) cB).to_encodable,
have : range f ⊆ S := range_subset_iff.2 (λ x, (hf x).fst),
exact ⟨_, countable_range f, this,
subset.antisymm (sUnion_subset_sUnion this) $
sUnion_subset $ λ s hs x xs,
let ⟨b, hb, xb, bs⟩ := mem_basis_subset_of_mem_open bB xs (H _ hs) in
⟨_, ⟨⟨_, hb, _, hs, bs⟩, rfl⟩, (hf _).snd xb⟩⟩
end
end topological_space
section limit
variables {α : Type u} [inhabited α] [topological_space α]
open classical
/-- If `f` is a filter, then `lim f` is a limit of the filter, if it exists. -/
noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ nhds a
lemma lim_spec {f : filter α} (h : ∃a, f ≤ nhds a) : f ≤ nhds (lim f) := epsilon_spec h
variables [t2_space α] {f : filter α}
lemma lim_eq {a : α} (hf : f ≠ ⊥) (h : f ≤ nhds a) : lim f = a :=
eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot hf $ le_inf (lim_spec ⟨_, h⟩) h
@[simp] lemma lim_nhds_eq {a : α} : lim (nhds a) = a :=
lim_eq nhds_neq_bot (le_refl _)
@[simp] lemma lim_nhds_eq_of_closure {a : α} {s : set α} (h : a ∈ closure s) :
lim (nhds a ⊓ principal s) = a :=
lim_eq begin rw [closure_eq_nhds] at h, exact h end inf_le_left
end limit
|
17d3f08627836fee6512c4535351aedd9fe41e46 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/meta_tac2.lean | 063f025fbf81e0626c21dcebb93da7e01ce53d4e | [
"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 | 1,183 | lean | set_option pp.all true
open tactic name list
set_option pp.goal.compact true
set_option pp.binder_types true
set_option pp.delayed_abstraction true
example : ∀ (p : Prop), p → p → p :=
by do
intro_lst [`_, `H1, `H2],
trace_state,
trace_result,
trace "---------",
get_local `H1 >>= revert,
trace_state,
trace_result,
intro `H3,
trace_result,
assumption,
trace_result,
return ()
#print "====================="
example : ∀ (p : Prop), p → p → p :=
by do
intro_lst [`_, `H1, `H2],
H1 ← get_local `H1, H2 ← get_local `H2,
revert_lst [H1, H2],
trace_state,
trace_result,
intro `H3,
trace_state,
trace "------------",
trace_result,
(assumption <|> trace "assumption failed"),
intro `H4,
assumption,
trace "------------",
trace_result,
return ()
#print "====================="
example : ∀ (p : Prop), p → p → p :=
by do
intros,
get_local `p >>= revert,
trace_state,
trace_result,
trace "----------",
intro `p,
trace_state,
trace_result,
trace "----------",
intro_lst [`H1, `H2],
assumption,
trace_result,
return ()
|
1cb0f9dff5ad0bdafcda8b0d35ef23bdbaee1c0c | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/control/traversable/basic.lean | 7cc0bcf88e7d8b8721c13370bc8095e05dda1fe7 | [
"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 | 9,528 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Simon Hudon
-/
import control.functor
/-!
# Traversable type class
Type classes for traversing collections. The concepts and laws are taken from
<http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-Traversable.html>
Traversable collections are a generalization of functors. Whereas
functors (such as `list`) allow us to apply a function to every
element, it does not allow functions which external effects encoded in
a monad. Consider for instance a functor `invite : email → io response`
that takes an email address, sends an email and waits for a
response. If we have a list `guests : list email`, using calling
`invite` using `map` gives us the following: `map invite guests : list
(io response)`. It is not what we need. We need something of type `io
(list response)`. Instead of using `map`, we can use `traverse` to
send all the invites: `traverse invite guests : io (list response)`.
`traverse` applies `invite` to every element of `guests` and combines
all the resulting effects. In the example, the effect is encoded in the
monad `io` but any applicative functor is accepted by `traverse`.
For more on how to use traversable, consider the Haskell tutorial:
<https://en.wikibooks.org/wiki/Haskell/Traversable>
## Main definitions
* `traversable` type class - exposes the `traverse` function
* `sequence` - based on `traverse`, turns a collection of effects into an effect returning a collection
* `is_lawful_traversable` - laws for a traversable functor
* `applicative_transformation` - the notion of a natural transformation for applicative functors
## Tags
traversable iterator functor applicative
## References
* "Applicative Programming with Effects", by Conor McBride and Ross Paterson,
Journal of Functional Programming 18:1 (2008) 1-13, online at
<http://www.soi.city.ac.uk/~ross/papers/Applicative.html>
* "The Essence of the Iterator Pattern", by Jeremy Gibbons and Bruno Oliveira,
in Mathematically-Structured Functional Programming, 2006, online at
<http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/#iterator>
* "An Investigation of the Laws of Traversals", by Mauro Jaskelioff and Ondrej Rypacek,
in Mathematically-Structured Functional Programming, 2012,
online at <http://arxiv.org/pdf/1202.2919>
-/
open function (hiding comp)
universes u v w
section applicative_transformation
variables (F : Type u → Type v) [applicative F] [is_lawful_applicative F]
variables (G : Type u → Type w) [applicative G] [is_lawful_applicative G]
/-- A transformation between applicative functors. It a natural
transformation such that `app` preserves the `has_pure.pure` and
`functor.map` (`<*>`) operations. See
`applicative_transformation.preserves_map` for naturality. -/
structure applicative_transformation : Type (max (u+1) v w) :=
(app : Π α : Type u, F α → G α)
(preserves_pure' : ∀ {α : Type u} (x : α), app _ (pure x) = pure x)
(preserves_seq' : ∀ {α β : Type u} (x : F (α → β)) (y : F α), app _ (x <*> y) = app _ x <*> app _ y)
end applicative_transformation
namespace applicative_transformation
variables (F : Type u → Type v) [applicative F] [is_lawful_applicative F]
variables (G : Type u → Type w) [applicative G] [is_lawful_applicative G]
instance : has_coe_to_fun (applicative_transformation F G) :=
{ F := λ _, Π {α}, F α → G α,
coe := λ a, a.app }
variables {F G}
@[simp]
lemma app_eq_coe (η : applicative_transformation F G) : η.app = η := rfl
@[simp]
lemma coe_mk (f : Π (α : Type u), F α → G α) (pp ps) :
⇑(applicative_transformation.mk f pp ps) = f := rfl
protected
lemma congr_fun (η η' : applicative_transformation F G) (h : η = η') {α : Type u} (x : F α) :
η x = η' x :=
congr_arg (λ η'' : applicative_transformation F G, η'' x) h
protected
lemma congr_arg (η : applicative_transformation F G) {α : Type u} {x y : F α} (h : x = y) :
η x = η y :=
congr_arg (λ z : F α, η z) h
lemma coe_inj ⦃η η' : applicative_transformation F G⦄ (h : (η : Π α, F α → G α) = η') : η = η' :=
by { cases η, cases η', congr, exact h }
@[ext]
lemma ext ⦃η η' : applicative_transformation F G⦄ (h : ∀ (α : Type u) (x : F α), η x = η' x) :
η = η' :=
by { apply coe_inj, ext1 α, exact funext (h α) }
lemma ext_iff {η η' : applicative_transformation F G} :
η = η' ↔ ∀ (α : Type u) (x : F α), η x = η' x :=
⟨λ h α x, h ▸ rfl, λ h, ext h⟩
section preserves
variables (η : applicative_transformation F G)
@[functor_norm]
lemma preserves_pure : ∀ {α} (x : α), η (pure x) = pure x := η.preserves_pure'
@[functor_norm]
lemma preserves_seq :
∀ {α β : Type u} (x : F (α → β)) (y : F α), η (x <*> y) = η x <*> η y :=
η.preserves_seq'
@[functor_norm]
lemma preserves_map {α β} (x : α → β) (y : F α) : η (x <$> y) = x <$> η y :=
by rw [← pure_seq_eq_map, η.preserves_seq]; simp with functor_norm
lemma preserves_map' {α β} (x : α → β) : @η _ ∘ functor.map x = functor.map x ∘ @η _ :=
by { ext y, exact preserves_map η x y }
end preserves
/-- The identity applicative transformation from an applicative functor to itself. -/
def id_transformation : applicative_transformation F F :=
{ app := λ α, id,
preserves_pure' := by simp,
preserves_seq' := λ α β x y, by simp }
instance : inhabited (applicative_transformation F F) := ⟨id_transformation⟩
universes s t
variables {H : Type u → Type s} [applicative H] [is_lawful_applicative H]
/-- The composition of applicative transformations. -/
def comp (η' : applicative_transformation G H) (η : applicative_transformation F G) :
applicative_transformation F H :=
{ app := λ α x, η' (η x),
preserves_pure' := λ α x, by simp with functor_norm,
preserves_seq' := λ α β x y, by simp with functor_norm }
@[simp]
lemma comp_apply (η' : applicative_transformation G H) (η : applicative_transformation F G)
{α : Type u} (x : F α) :
η'.comp η x = η' (η x) := rfl
lemma comp_assoc {I : Type u → Type t} [applicative I] [is_lawful_applicative I]
(η'' : applicative_transformation H I)
(η' : applicative_transformation G H)
(η : applicative_transformation F G) :
(η''.comp η').comp η = η''.comp (η'.comp η) := rfl
@[simp]
lemma comp_id (η : applicative_transformation F G) : η.comp id_transformation = η :=
ext $ λ α x, rfl
@[simp]
lemma id_comp (η : applicative_transformation F G) : id_transformation.comp η = η :=
ext $ λ α x, rfl
end applicative_transformation
open applicative_transformation
/-- A traversable functor is a functor along with a way to commute
with all applicative functors (see `sequence`). For example, if `t`
is the traversable functor `list` and `m` is the applicative functor
`io`, then given a function `f : α → io β`, the function `functor.map f` is
`list α → list (io β)`, but `traverse f` is `list α → io (list β)`. -/
class traversable (t : Type u → Type u) extends functor t :=
(traverse : Π {m : Type u → Type u} [applicative m] {α β},
(α → m β) → t α → m (t β))
open functor
export traversable (traverse)
section functions
variables {t : Type u → Type u}
variables {m : Type u → Type v} [applicative m]
variables {α β : Type u}
variables {f : Type u → Type u} [applicative f]
/-- A traversable functor commutes with all applicative functors. -/
def sequence [traversable t] : t (f α) → f (t α) := traverse id
end functions
/-- A traversable functor is lawful if its `traverse` satisfies a
number of additional properties. It must send `id.mk` to `id.mk`,
send the composition of applicative functors to the composition of the
`traverse` of each, send each function `f` to `λ x, f <$> x`, and
satisfy a naturality condition with respect to applicative
transformations. -/
class is_lawful_traversable (t : Type u → Type u) [traversable t]
extends is_lawful_functor t : Type (u+1) :=
(id_traverse : ∀ {α} (x : t α), traverse id.mk x = x )
(comp_traverse : ∀ {F G} [applicative F] [applicative G]
[is_lawful_applicative F] [is_lawful_applicative G]
{α β γ} (f : β → F γ) (g : α → G β) (x : t α),
traverse (comp.mk ∘ map f ∘ g) x =
comp.mk (map (traverse f) (traverse g x)))
(traverse_eq_map_id : ∀ {α β} (f : α → β) (x : t α),
traverse (id.mk ∘ f) x = id.mk (f <$> x))
(naturality : ∀ {F G} [applicative F] [applicative G]
[is_lawful_applicative F] [is_lawful_applicative G]
(η : applicative_transformation F G) {α β} (f : α → F β) (x : t α),
η (traverse f x) = traverse (@η _ ∘ f) x)
instance : traversable id := ⟨λ _ _ _ _, id⟩
instance : is_lawful_traversable id := by refine {..}; intros; refl
section
variables {F : Type u → Type v} [applicative F]
instance : traversable option := ⟨@option.traverse⟩
instance : traversable list := ⟨@list.traverse⟩
end
namespace sum
variables {σ : Type u}
variables {F : Type u → Type u}
variables [applicative F]
/-- Defines a `traverse` function on the second component of a sum type.
This is used to give a `traversable` instance for the functor `σ ⊕ -`. -/
protected def traverse {α β} (f : α → F β) : σ ⊕ α → F (σ ⊕ β)
| (sum.inl x) := pure (sum.inl x)
| (sum.inr x) := sum.inr <$> f x
end sum
instance {σ : Type u} : traversable.{u} (sum σ) := ⟨@sum.traverse _⟩
|
c6bace7902f41210d83893843f820d78dc45445f | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/order/conditionally_complete_lattice.lean | a9ab056ac2ec24a7d2e65e8bfae086f004768cc8 | [
"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 | 40,782 | lean | /-
Copyright (c) 2018 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import data.nat.enat
import data.set.intervals.ord_connected
/-!
# Theory of conditionally complete lattices.
A conditionally complete lattice is a lattice in which every non-empty bounded subset s
has a least upper bound and a greatest lower bound, denoted below by Sup s and Inf s.
Typical examples are real, nat, int with their usual orders.
The theory is very comparable to the theory of complete lattices, except that suitable
boundedness and nonemptiness assumptions have to be added to most statements.
We introduce two predicates bdd_above and bdd_below to express this boundedness, prove
their basic properties, and then go on to prove most useful properties of Sup and Inf
in conditionally complete lattices.
To differentiate the statements between complete lattices and conditionally complete
lattices, we prefix Inf and Sup in the statements by c, giving cInf and cSup. For instance,
Inf_le is a statement in complete lattices ensuring Inf s ≤ x, while cInf_le is the same
statement in conditionally complete lattices with an additional assumption that s is
bounded below.
-/
set_option old_structure_cmd true
open set
variables {α β : Type*} {ι : Sort*}
section
/-!
Extension of Sup and Inf from a preorder `α` to `with_top α` and `with_bot α`
-/
open_locale classical
noncomputable instance {α : Type*} [preorder α] [has_Sup α] : has_Sup (with_top α) :=
⟨λ S, if ⊤ ∈ S then ⊤ else
if bdd_above (coe ⁻¹' S : set α) then ↑(Sup (coe ⁻¹' S : set α)) else ⊤⟩
noncomputable instance {α : Type*} [has_Inf α] : has_Inf (with_top α) :=
⟨λ S, if S ⊆ {⊤} then ⊤ else ↑(Inf (coe ⁻¹' S : set α))⟩
noncomputable instance {α : Type*} [has_Sup α] : has_Sup (with_bot α) :=
⟨(@with_top.has_Inf (order_dual α) _).Inf⟩
noncomputable instance {α : Type*} [preorder α] [has_Inf α] : has_Inf (with_bot α) :=
⟨(@with_top.has_Sup (order_dual α) _ _).Sup⟩
end -- section
/-- A conditionally complete lattice is a lattice in which
every nonempty subset which is bounded above has a supremum, and
every nonempty subset which is bounded below has an infimum.
Typical examples are real numbers or natural numbers.
To differentiate the statements from the corresponding statements in (unconditional)
complete lattices, we prefix Inf and Sup by a c everywhere. The same statements should
hold in both worlds, sometimes with additional assumptions of nonemptiness or
boundedness.-/
class conditionally_complete_lattice (α : Type*) extends lattice α, has_Sup α, has_Inf α :=
(le_cSup : ∀s a, bdd_above s → a ∈ s → a ≤ Sup s)
(cSup_le : ∀ s a, set.nonempty s → a ∈ upper_bounds s → Sup s ≤ a)
(cInf_le : ∀s a, bdd_below s → a ∈ s → Inf s ≤ a)
(le_cInf : ∀s a, set.nonempty s → a ∈ lower_bounds s → a ≤ Inf s)
/-- A conditionally complete linear order is a linear order in which
every nonempty subset which is bounded above has a supremum, and
every nonempty subset which is bounded below has an infimum.
Typical examples are real numbers or natural numbers.
To differentiate the statements from the corresponding statements in (unconditional)
complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should
hold in both worlds, sometimes with additional assumptions of nonemptiness or
boundedness.-/
class conditionally_complete_linear_order (α : Type*)
extends conditionally_complete_lattice α, linear_order α
/-- A conditionally complete linear order with `bot` is a linear order with least element, in which
every nonempty subset which is bounded above has a supremum, and every nonempty subset (necessarily
bounded below) has an infimum. A typical example is the natural numbers.
To differentiate the statements from the corresponding statements in (unconditional)
complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should
hold in both worlds, sometimes with additional assumptions of nonemptiness or
boundedness.-/
class conditionally_complete_linear_order_bot (α : Type*)
extends conditionally_complete_linear_order α, order_bot α :=
(cSup_empty : Sup ∅ = ⊥)
/- A complete lattice is a conditionally complete lattice, as there are no restrictions
on the properties of Inf and Sup in a complete lattice.-/
@[priority 100] -- see Note [lower instance priority]
instance conditionally_complete_lattice_of_complete_lattice [complete_lattice α]:
conditionally_complete_lattice α :=
{ le_cSup := by intros; apply le_Sup; assumption,
cSup_le := by intros; apply Sup_le; assumption,
cInf_le := by intros; apply Inf_le; assumption,
le_cInf := by intros; apply le_Inf; assumption,
..‹complete_lattice α› }
@[priority 100] -- see Note [lower instance priority]
instance conditionally_complete_linear_order_of_complete_linear_order [complete_linear_order α]:
conditionally_complete_linear_order α :=
{ ..conditionally_complete_lattice_of_complete_lattice, .. ‹complete_linear_order α› }
section conditionally_complete_lattice
variables [conditionally_complete_lattice α] {s t : set α} {a b : α}
theorem le_cSup (h₁ : bdd_above s) (h₂ : a ∈ s) : a ≤ Sup s :=
conditionally_complete_lattice.le_cSup s a h₁ h₂
theorem cSup_le (h₁ : s.nonempty) (h₂ : ∀b∈s, b ≤ a) : Sup s ≤ a :=
conditionally_complete_lattice.cSup_le s a h₁ h₂
theorem cInf_le (h₁ : bdd_below s) (h₂ : a ∈ s) : Inf s ≤ a :=
conditionally_complete_lattice.cInf_le s a h₁ h₂
theorem le_cInf (h₁ : s.nonempty) (h₂ : ∀b∈s, a ≤ b) : a ≤ Inf s :=
conditionally_complete_lattice.le_cInf s a h₁ h₂
theorem le_cSup_of_le (_ : bdd_above s) (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s :=
le_trans h (le_cSup ‹bdd_above s› hb)
theorem cInf_le_of_le (_ : bdd_below s) (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a :=
le_trans (cInf_le ‹bdd_below s› hb) h
theorem cSup_le_cSup (_ : bdd_above t) (_ : s.nonempty) (h : s ⊆ t) : Sup s ≤ Sup t :=
cSup_le ‹_› (assume (a) (ha : a ∈ s), le_cSup ‹bdd_above t› (h ha))
theorem cInf_le_cInf (_ : bdd_below t) (_ : s.nonempty) (h : s ⊆ t) : Inf t ≤ Inf s :=
le_cInf ‹_› (assume (a) (ha : a ∈ s), cInf_le ‹bdd_below t› (h ha))
lemma is_lub_cSup (ne : s.nonempty) (H : bdd_above s) : is_lub s (Sup s) :=
⟨assume x, le_cSup H, assume x, cSup_le ne⟩
lemma is_glb_cInf (ne : s.nonempty) (H : bdd_below s) : is_glb s (Inf s) :=
⟨assume x, cInf_le H, assume x, le_cInf ne⟩
lemma is_lub.cSup_eq (H : is_lub s a) (ne : s.nonempty) : Sup s = a :=
(is_lub_cSup ne ⟨a, H.1⟩).unique H
/-- A greatest element of a set is the supremum of this set. -/
lemma is_greatest.cSup_eq (H : is_greatest s a) : Sup s = a :=
H.is_lub.cSup_eq H.nonempty
lemma is_glb.cInf_eq (H : is_glb s a) (ne : s.nonempty) : Inf s = a :=
(is_glb_cInf ne ⟨a, H.1⟩).unique H
/-- A least element of a set is the infimum of this set. -/
lemma is_least.cInf_eq (H : is_least s a) : Inf s = a :=
H.is_glb.cInf_eq H.nonempty
lemma subset_Icc_cInf_cSup (hb : bdd_below s) (ha : bdd_above s) :
s ⊆ Icc (Inf s) (Sup s) :=
λ x hx, ⟨cInf_le hb hx, le_cSup ha hx⟩
theorem cSup_le_iff (hb : bdd_above s) (ne : s.nonempty) : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) :=
is_lub_le_iff (is_lub_cSup ne hb)
theorem le_cInf_iff (hb : bdd_below s) (ne : s.nonempty) : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) :=
le_is_glb_iff (is_glb_cInf ne hb)
lemma cSup_lower_bounds_eq_cInf {s : set α} (h : bdd_below s) (hs : s.nonempty) :
Sup (lower_bounds s) = Inf s :=
(is_lub_cSup h $ hs.mono $ λ x hx y hy, hy hx).unique (is_glb_cInf hs h).is_lub
lemma cInf_upper_bounds_eq_cSup {s : set α} (h : bdd_above s) (hs : s.nonempty) :
Inf (upper_bounds s) = Sup s :=
(is_glb_cInf h $ hs.mono $ λ x hx y hy, hy hx).unique (is_lub_cSup hs h).is_glb
/--Introduction rule to prove that b is the supremum of s: it suffices to check that b
is larger than all elements of s, and that this is not the case of any `w<b`.-/
theorem cSup_intro (_ : s.nonempty) (_ : ∀a∈s, a ≤ b) (H : ∀w, w < b → (∃a∈s, w < a)) : Sup s = b :=
have bdd_above s := ⟨b, by assumption⟩,
have (Sup s < b) ∨ (Sup s = b) := lt_or_eq_of_le (cSup_le ‹_› ‹∀a∈s, a ≤ b›),
have ¬(Sup s < b) :=
assume: Sup s < b,
let ⟨a, _, _⟩ := (H (Sup s) ‹Sup s < b›) in /- a ∈ s, Sup s < a-/
have Sup s < Sup s := lt_of_lt_of_le ‹Sup s < a› (le_cSup ‹bdd_above s› ‹a ∈ s›),
show false, by finish [lt_irrefl (Sup s)],
show Sup s = b, by finish
/--Introduction rule to prove that b is the infimum of s: it suffices to check that b
is smaller than all elements of s, and that this is not the case of any `w>b`.-/
theorem cInf_intro (_ : s.nonempty) (_ : ∀a∈s, b ≤ a) (H : ∀w, b < w → (∃a∈s, a < w)) : Inf s = b :=
have bdd_below s := ⟨b, by assumption⟩,
have (b < Inf s) ∨ (b = Inf s) := lt_or_eq_of_le (le_cInf ‹_› ‹∀a∈s, b ≤ a›),
have ¬(b < Inf s) :=
assume: b < Inf s,
let ⟨a, _, _⟩ := (H (Inf s) ‹b < Inf s›) in /- a ∈ s, a < Inf s-/
have Inf s < Inf s := lt_of_le_of_lt (cInf_le ‹bdd_below s› ‹a ∈ s›) ‹a < Inf s› ,
show false, by finish [lt_irrefl (Inf s)],
show Inf s = b, by finish
/--b < Sup s when there is an element a in s with b < a, when s is bounded above.
This is essentially an iff, except that the assumptions for the two implications are
slightly different (one needs boundedness above for one direction, nonemptiness and linear
order for the other one), so we formulate separately the two implications, contrary to
the complete_lattice case.-/
lemma lt_cSup_of_lt (_ : bdd_above s) (_ : a ∈ s) (_ : b < a) : b < Sup s :=
lt_of_lt_of_le ‹b < a› (le_cSup ‹bdd_above s› ‹a ∈ s›)
/--Inf s < b when there is an element a in s with a < b, when s is bounded below.
This is essentially an iff, except that the assumptions for the two implications are
slightly different (one needs boundedness below for one direction, nonemptiness and linear
order for the other one), so we formulate separately the two implications, contrary to
the complete_lattice case.-/
lemma cInf_lt_of_lt (_ : bdd_below s) (_ : a ∈ s) (_ : a < b) : Inf s < b :=
lt_of_le_of_lt (cInf_le ‹bdd_below s› ‹a ∈ s›) ‹a < b›
/-- If all elements of a nonempty set `s` are less than or equal to all elements
of a nonempty set `t`, then there exists an element between these sets. -/
lemma exists_between_of_forall_le (sne : s.nonempty) (tne : t.nonempty)
(hst : ∀ (x ∈ s) (y ∈ t), x ≤ y) :
(upper_bounds s ∩ lower_bounds t).nonempty :=
⟨Inf t, λ x hx, le_cInf tne $ hst x hx, λ y hy, cInf_le (sne.mono hst) hy⟩
/--The supremum of a singleton is the element of the singleton-/
@[simp] theorem cSup_singleton (a : α) : Sup {a} = a :=
is_greatest_singleton.cSup_eq
/--The infimum of a singleton is the element of the singleton-/
@[simp] theorem cInf_singleton (a : α) : Inf {a} = a :=
is_least_singleton.cInf_eq
/--If a set is bounded below and above, and nonempty, its infimum is less than or equal to
its supremum.-/
theorem cInf_le_cSup (hb : bdd_below s) (ha : bdd_above s) (ne : s.nonempty) : Inf s ≤ Sup s :=
is_glb_le_is_lub (is_glb_cInf ne hb) (is_lub_cSup ne ha) ne
/--The sup of a union of two sets is the max of the suprema of each subset, under the assumptions
that all sets are bounded above and nonempty.-/
theorem cSup_union (hs : bdd_above s) (sne : s.nonempty) (ht : bdd_above t) (tne : t.nonempty) :
Sup (s ∪ t) = Sup s ⊔ Sup t :=
((is_lub_cSup sne hs).union (is_lub_cSup tne ht)).cSup_eq sne.inl
/--The inf of a union of two sets is the min of the infima of each subset, under the assumptions
that all sets are bounded below and nonempty.-/
theorem cInf_union (hs : bdd_below s) (sne : s.nonempty) (ht : bdd_below t) (tne : t.nonempty) :
Inf (s ∪ t) = Inf s ⊓ Inf t :=
((is_glb_cInf sne hs).union (is_glb_cInf tne ht)).cInf_eq sne.inl
/--The supremum of an intersection of two sets is bounded by the minimum of the suprema of each
set, if all sets are bounded above and nonempty.-/
theorem cSup_inter_le (_ : bdd_above s) (_ : bdd_above t) (hst : (s ∩ t).nonempty) :
Sup (s ∩ t) ≤ Sup s ⊓ Sup t :=
begin
apply cSup_le hst, simp only [le_inf_iff, and_imp, set.mem_inter_eq], intros b _ _, split,
apply le_cSup ‹bdd_above s› ‹b ∈ s›,
apply le_cSup ‹bdd_above t› ‹b ∈ t›
end
/--The infimum of an intersection of two sets is bounded below by the maximum of the
infima of each set, if all sets are bounded below and nonempty.-/
theorem le_cInf_inter (_ : bdd_below s) (_ : bdd_below t) (hst : (s ∩ t).nonempty) :
Inf s ⊔ Inf t ≤ Inf (s ∩ t) :=
begin
apply le_cInf hst, simp only [and_imp, set.mem_inter_eq, sup_le_iff], intros b _ _, split,
apply cInf_le ‹bdd_below s› ‹b ∈ s›,
apply cInf_le ‹bdd_below t› ‹b ∈ t›
end
/-- The supremum of insert a s is the maximum of a and the supremum of s, if s is
nonempty and bounded above.-/
theorem cSup_insert (hs : bdd_above s) (sne : s.nonempty) : Sup (insert a s) = a ⊔ Sup s :=
((is_lub_cSup sne hs).insert a).cSup_eq (insert_nonempty a s)
/-- The infimum of insert a s is the minimum of a and the infimum of s, if s is
nonempty and bounded below.-/
theorem cInf_insert (hs : bdd_below s) (sne : s.nonempty) : Inf (insert a s) = a ⊓ Inf s :=
((is_glb_cInf sne hs).insert a).cInf_eq (insert_nonempty a s)
@[simp] lemma cInf_Ici : Inf (Ici a) = a := is_least_Ici.cInf_eq
@[simp] lemma cSup_Iic : Sup (Iic a) = a := is_greatest_Iic.cSup_eq
/--The indexed supremum of two functions are comparable if the functions are pointwise comparable-/
lemma csupr_le_csupr {f g : ι → α} (B : bdd_above (range g)) (H : ∀x, f x ≤ g x) :
supr f ≤ supr g :=
begin
classical, by_cases hι : nonempty ι,
{ have Rf : (range f).nonempty, { exactI range_nonempty _ },
apply cSup_le Rf,
rintros y ⟨x, rfl⟩,
have : g x ∈ range g := ⟨x, rfl⟩,
exact le_cSup_of_le B this (H x) },
{ have Rf : range f = ∅, from range_eq_empty.2 hι,
have Rg : range g = ∅, from range_eq_empty.2 hι,
unfold supr, rw [Rf, Rg] }
end
/--The indexed supremum of a function is bounded above by a uniform bound-/
lemma csupr_le [nonempty ι] {f : ι → α} {c : α} (H : ∀x, f x ≤ c) : supr f ≤ c :=
cSup_le (range_nonempty f) (by rwa forall_range_iff)
/--The indexed supremum of a function is bounded below by the value taken at one point-/
lemma le_csupr {f : ι → α} (H : bdd_above (range f)) (c : ι) : f c ≤ supr f :=
le_cSup H (mem_range_self _)
/--The indexed infimum of two functions are comparable if the functions are pointwise comparable-/
lemma cinfi_le_cinfi {f g : ι → α} (B : bdd_below (range f)) (H : ∀x, f x ≤ g x) :
infi f ≤ infi g :=
begin
classical, by_cases hι : nonempty ι,
{ have Rg : (range g).nonempty, { exactI range_nonempty _ },
apply le_cInf Rg,
rintros y ⟨x, rfl⟩,
have : f x ∈ range f := ⟨x, rfl⟩,
exact cInf_le_of_le B this (H x) },
{ have Rf : range f = ∅, from range_eq_empty.2 hι,
have Rg : range g = ∅, from range_eq_empty.2 hι,
unfold infi, rw [Rf, Rg] }
end
/--The indexed minimum of a function is bounded below by a uniform lower bound-/
lemma le_cinfi [nonempty ι] {f : ι → α} {c : α} (H : ∀x, c ≤ f x) : c ≤ infi f :=
le_cInf (range_nonempty f) (by rwa forall_range_iff)
/--The indexed infimum of a function is bounded above by the value taken at one point-/
lemma cinfi_le {f : ι → α} (H : bdd_below (range f)) (c : ι) : infi f ≤ f c :=
cInf_le H (mem_range_self _)
@[simp] theorem cinfi_const [hι : nonempty ι] {a : α} : (⨅ b:ι, a) = a :=
by rw [infi, range_const, cInf_singleton]
@[simp] theorem csupr_const [hι : nonempty ι] {a : α} : (⨆ b:ι, a) = a :=
by rw [supr, range_const, cSup_singleton]
theorem infi_unique [unique ι] {s : ι → α} : (⨅ i, s i) = s (default ι) :=
have ∀ i, s i = s (default ι) := λ i, congr_arg s (unique.eq_default i),
by simp only [this, cinfi_const]
theorem supr_unique [unique ι] {s : ι → α} : (⨆ i, s i) = s (default ι) :=
have ∀ i, s i = s (default ι) := λ i, congr_arg s (unique.eq_default i),
by simp only [this, csupr_const]
@[simp] theorem infi_unit {f : unit → α} : (⨅ x, f x) = f () :=
by { convert infi_unique, apply_instance }
@[simp] theorem supr_unit {f : unit → α} : (⨆ x, f x) = f () :=
by { convert supr_unique, apply_instance }
/-- Nested intervals lemma: if `f` is a monotonically increasing sequence, `g` is a monotonically
decreasing sequence, and `f n ≤ g n` for all `n`, then `⨆ n, f n` belongs to all the intervals
`[f n, g n]`. -/
lemma csupr_mem_Inter_Icc_of_mono_incr_of_mono_decr [nonempty β] [semilattice_sup β]
{f g : β → α} (hf : monotone f) (hg : ∀ ⦃m n⦄, m ≤ n → g n ≤ g m) (h : ∀ n, f n ≤ g n) :
(⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) :=
begin
inhabit β,
refine mem_Inter.2 (λ n, ⟨le_csupr ⟨g $ default β, forall_range_iff.2 $ λ m, _⟩ _,
csupr_le $ λ m, _⟩); exact forall_le_of_monotone_of_mono_decr hf hg h _ _
end
/-- Nested intervals lemma: if `[f n, g n]` is a monotonically decreasing sequence of nonempty
closed intervals, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/
lemma csupr_mem_Inter_Icc_of_mono_decr_Icc [nonempty β] [semilattice_sup β]
{f g : β → α} (h : ∀ ⦃m n⦄, m ≤ n → Icc (f n) (g n) ⊆ Icc (f m) (g m)) (h' : ∀ n, f n ≤ g n) :
(⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) :=
csupr_mem_Inter_Icc_of_mono_incr_of_mono_decr (λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).1)
(λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).2) h'
/-- Nested intervals lemma: if `[f n, g n]` is a monotonically decreasing sequence of nonempty
closed intervals, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/
lemma csupr_mem_Inter_Icc_of_mono_decr_Icc_nat
{f g : ℕ → α} (h : ∀ n, Icc (f (n + 1)) (g (n + 1)) ⊆ Icc (f n) (g n)) (h' : ∀ n, f n ≤ g n) :
(⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) :=
csupr_mem_Inter_Icc_of_mono_decr_Icc
(@monotone_of_monotone_nat (order_dual $ set α) _ (λ n, Icc (f n) (g n)) h) h'
end conditionally_complete_lattice
instance pi.conditionally_complete_lattice {ι : Type*} {α : Π i : ι, Type*}
[Π i, conditionally_complete_lattice (α i)] :
conditionally_complete_lattice (Π i, α i) :=
{ le_cSup := λ s f ⟨g, hg⟩ hf i, le_cSup ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩
⟨⟨f, hf⟩, rfl⟩,
cSup_le := λ s f hs hf i, cSup_le (by haveI := hs.to_subtype; apply range_nonempty) $
λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i,
cInf_le := λ s f ⟨g, hg⟩ hf i, cInf_le ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩
⟨⟨f, hf⟩, rfl⟩,
le_cInf := λ s f hs hf i, le_cInf (by haveI := hs.to_subtype; apply range_nonempty) $
λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i,
.. pi.lattice, .. pi.has_Sup, .. pi.has_Inf }
section conditionally_complete_linear_order
variables [conditionally_complete_linear_order α] {s t : set α} {a b : α}
/-- When b < Sup s, there is an element a in s with b < a, if s is nonempty and the order is
a linear order. -/
lemma exists_lt_of_lt_cSup (hs : s.nonempty) (hb : b < Sup s) : ∃a∈s, b < a :=
begin
classical, contrapose! hb,
exact cSup_le hs hb
end
/--
Indexed version of the above lemma `exists_lt_of_lt_cSup`.
When `b < supr f`, there is an element `i` such that `b < f i`.
-/
lemma exists_lt_of_lt_csupr [nonempty ι] {f : ι → α} (h : b < supr f) :
∃i, b < f i :=
let ⟨_, ⟨i, rfl⟩, h⟩ := exists_lt_of_lt_cSup (range_nonempty f) h in ⟨i, h⟩
/--When Inf s < b, there is an element a in s with a < b, if s is nonempty and the order is
a linear order.-/
lemma exists_lt_of_cInf_lt (hs : s.nonempty) (hb : Inf s < b) : ∃a∈s, a < b :=
begin
classical, contrapose! hb,
exact le_cInf hs hb
end
/--
Indexed version of the above lemma `exists_lt_of_cInf_lt`
When `infi f < a`, there is an element `i` such that `f i < a`.
-/
lemma exists_lt_of_cinfi_lt [nonempty ι] {f : ι → α} (h : infi f < a) :
(∃i, f i < a) :=
let ⟨_, ⟨i, rfl⟩, h⟩ := exists_lt_of_cInf_lt (range_nonempty f) h in ⟨i, h⟩
/--Introduction rule to prove that b is the supremum of s: it suffices to check that
1) b is an upper bound
2) every other upper bound b' satisfies b ≤ b'.-/
theorem cSup_intro' (_ : s.nonempty)
(h_is_ub : ∀ a ∈ s, a ≤ b) (h_b_le_ub : ∀ub, (∀ a ∈ s, a ≤ ub) → (b ≤ ub)) : Sup s = b :=
le_antisymm
(show Sup s ≤ b, from cSup_le ‹s.nonempty› h_is_ub)
(show b ≤ Sup s, from h_b_le_ub _ $ assume a, le_cSup ⟨b, h_is_ub⟩)
end conditionally_complete_linear_order
section conditionally_complete_linear_order_bot
lemma cSup_empty [conditionally_complete_linear_order_bot α] : (Sup ∅ : α) = ⊥ :=
conditionally_complete_linear_order_bot.cSup_empty
end conditionally_complete_linear_order_bot
namespace nat
open_locale classical
noncomputable instance : has_Inf ℕ :=
⟨λs, if h : ∃n, n ∈ s then @nat.find (λn, n ∈ s) _ h else 0⟩
noncomputable instance : has_Sup ℕ :=
⟨λs, if h : ∃n, ∀a∈s, a ≤ n then @nat.find (λn, ∀a∈s, a ≤ n) _ h else 0⟩
lemma Inf_def {s : set ℕ} (h : s.nonempty) : Inf s = @nat.find (λn, n ∈ s) _ h :=
dif_pos _
lemma Sup_def {s : set ℕ} (h : ∃n, ∀a∈s, a ≤ n) :
Sup s = @nat.find (λn, ∀a∈s, a ≤ n) _ h :=
dif_pos _
@[simp] lemma Inf_eq_zero {s : set ℕ} : Inf s = 0 ↔ 0 ∈ s ∨ s = ∅ :=
begin
cases eq_empty_or_nonempty s,
{ subst h, simp only [or_true, eq_self_iff_true, iff_true, Inf, has_Inf.Inf,
mem_empty_eq, exists_false, dif_neg, not_false_iff] },
{ have := ne_empty_iff_nonempty.mpr h,
simp only [this, or_false, nat.Inf_def, h, nat.find_eq_zero] }
end
lemma Inf_mem {s : set ℕ} (h : s.nonempty) : Inf s ∈ s :=
by { rw [nat.Inf_def h], exact nat.find_spec h }
lemma not_mem_of_lt_Inf {s : set ℕ} {m : ℕ} (hm : m < Inf s) : m ∉ s :=
begin
cases eq_empty_or_nonempty s,
{ subst h, apply not_mem_empty },
{ rw [nat.Inf_def h] at hm, exact nat.find_min h hm }
end
protected lemma Inf_le {s : set ℕ} {m : ℕ} (hm : m ∈ s) : Inf s ≤ m :=
by { rw [nat.Inf_def ⟨m, hm⟩], exact nat.find_min' ⟨m, hm⟩ hm }
lemma nonempty_of_pos_Inf {s : set ℕ} (h : 0 < Inf s) : s.nonempty :=
begin
by_contradiction contra, rw set.not_nonempty_iff_eq_empty at contra,
have h' : Inf s ≠ 0, { exact ne_of_gt h, }, apply h',
rw nat.Inf_eq_zero, right, assumption,
end
lemma nonempty_of_Inf_eq_succ {s : set ℕ} {k : ℕ} (h : Inf s = k + 1) : s.nonempty :=
nonempty_of_pos_Inf (h.symm ▸ (succ_pos k) : Inf s > 0)
lemma eq_Ici_of_nonempty_of_upward_closed {s : set ℕ} (hs : s.nonempty)
(hs' : ∀ (k₁ k₂ : ℕ), k₁ ≤ k₂ → k₁ ∈ s → k₂ ∈ s) : s = Ici (Inf s) :=
ext (λ n, ⟨λ H, nat.Inf_le H, λ H, hs' (Inf s) n H (Inf_mem hs)⟩)
lemma Inf_upward_closed_eq_succ_iff {s : set ℕ}
(hs : ∀ (k₁ k₂ : ℕ), k₁ ≤ k₂ → k₁ ∈ s → k₂ ∈ s) (k : ℕ) :
Inf s = k + 1 ↔ k + 1 ∈ s ∧ k ∉ s :=
begin
split,
{ intro H,
rw [eq_Ici_of_nonempty_of_upward_closed (nonempty_of_Inf_eq_succ H) hs, H, mem_Ici, mem_Ici],
exact ⟨le_refl _, k.not_succ_le_self⟩, },
{ rintro ⟨H, H'⟩,
rw [Inf_def (⟨_, H⟩ : s.nonempty), find_eq_iff],
exact ⟨H, λ n hnk hns, H' $ hs n k (lt_succ_iff.mp hnk) hns⟩, },
end
/-- This instance is necessary, otherwise the lattice operations would be derived via
conditionally_complete_linear_order_bot and marked as noncomputable. -/
instance : lattice ℕ := lattice_of_linear_order
noncomputable instance : conditionally_complete_linear_order_bot ℕ :=
{ Sup := Sup, Inf := Inf,
le_cSup := assume s a hb ha, by rw [Sup_def hb]; revert a ha; exact @nat.find_spec _ _ hb,
cSup_le := assume s a hs ha, by rw [Sup_def ⟨a, ha⟩]; exact nat.find_min' _ ha,
le_cInf := assume s a hs hb,
by rw [Inf_def hs]; exact hb (@nat.find_spec (λn, n ∈ s) _ _),
cInf_le := assume s a hb ha, by rw [Inf_def ⟨a, ha⟩]; exact nat.find_min' _ ha,
cSup_empty :=
begin
simp only [Sup_def, set.mem_empty_eq, forall_const, forall_prop_of_false, not_false_iff,
exists_const],
apply bot_unique (nat.find_min' _ _),
trivial
end,
.. (infer_instance : order_bot ℕ), .. (lattice_of_linear_order : lattice ℕ),
.. (infer_instance : linear_order ℕ) }
end nat
namespace with_top
open_locale classical
variables [conditionally_complete_linear_order_bot α]
/-- The Sup of a non-empty set is its least upper bound for a conditionally
complete lattice with a top. -/
lemma is_lub_Sup' {β : Type*} [conditionally_complete_lattice β]
{s : set (with_top β)} (hs : s.nonempty) : is_lub s (Sup s) :=
begin
split,
{ show ite _ _ _ ∈ _,
split_ifs,
{ intros _ _, exact le_top },
{ rintro (⟨⟩|a) ha,
{ contradiction },
apply some_le_some.2,
exact le_cSup h_1 ha },
{ intros _ _, exact le_top } },
{ show ite _ _ _ ∈ _,
split_ifs,
{ rintro (⟨⟩|a) ha,
{ exact _root_.le_refl _ },
{ exact false.elim (not_top_le_coe a (ha h)) } },
{ rintro (⟨⟩|b) hb,
{ exact le_top },
refine some_le_some.2 (cSup_le _ _),
{ rcases hs with ⟨⟨⟩|b, hb⟩,
{ exact absurd hb h },
{ exact ⟨b, hb⟩ } },
{ intros a ha, exact some_le_some.1 (hb ha) } },
{ rintro (⟨⟩|b) hb,
{ exact _root_.le_refl _ },
{ exfalso, apply h_1, use b, intros a ha, exact some_le_some.1 (hb ha) } } }
end
lemma is_lub_Sup (s : set (with_top α)) : is_lub s (Sup s) :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ rw hs,
show is_lub ∅ (ite _ _ _),
split_ifs,
{ cases h },
{ rw [preimage_empty, cSup_empty], exact is_lub_empty },
{ exfalso, apply h_1, use ⊥, rintro a ⟨⟩ } },
exact is_lub_Sup' hs,
end
/-- The Inf of a bounded-below set is its greatest lower bound for a conditionally
complete lattice with a top. -/
lemma is_glb_Inf' {β : Type*} [conditionally_complete_lattice β]
{s : set (with_top β)} (hs : bdd_below s) : is_glb s (Inf s) :=
begin
split,
{ show ite _ _ _ ∈ _,
split_ifs,
{ intros a ha, exact top_le_iff.2 (set.mem_singleton_iff.1 (h ha)) },
{ rintro (⟨⟩|a) ha,
{ exact le_top },
refine some_le_some.2 (cInf_le _ ha),
rcases hs with ⟨⟨⟩|b, hb⟩,
{ exfalso,
apply h,
intros c hc,
rw [mem_singleton_iff, ←top_le_iff],
exact hb hc },
use b,
intros c hc,
exact some_le_some.1 (hb hc) } },
{ show ite _ _ _ ∈ _,
split_ifs,
{ intros _ _, exact le_top },
{ rintro (⟨⟩|a) ha,
{ exfalso, apply h, intros b hb, exact set.mem_singleton_iff.2 (top_le_iff.1 (ha hb)) },
{ refine some_le_some.2 (le_cInf _ _),
{ classical, contrapose! h,
rintros (⟨⟩|a) ha,
{ exact mem_singleton ⊤ },
{ exact (h ⟨a, ha⟩).elim }},
{ intros b hb,
rw ←some_le_some,
exact ha hb } } } }
end
lemma is_glb_Inf (s : set (with_top α)) : is_glb s (Inf s) :=
begin
by_cases hs : bdd_below s,
{ exact is_glb_Inf' hs },
{ exfalso, apply hs, use ⊥, intros _ _, exact bot_le },
end
noncomputable instance : complete_linear_order (with_top α) :=
{ Sup := Sup, le_Sup := assume s, (is_lub_Sup s).1, Sup_le := assume s, (is_lub_Sup s).2,
Inf := Inf, le_Inf := assume s, (is_glb_Inf s).2, Inf_le := assume s, (is_glb_Inf s).1,
decidable_le := classical.dec_rel _,
.. with_top.linear_order, ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot }
lemma coe_Sup {s : set α} (hb : bdd_above s) : (↑(Sup s) : with_top α) = (⨆a∈s, ↑a) :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ rw [hs, cSup_empty], simp only [set.mem_empty_eq, supr_bot, supr_false], refl },
apply le_antisymm,
{ refine (coe_le_iff.2 $ assume b hb, cSup_le hs $ assume a has, coe_le_coe.1 $ hb ▸ _),
exact (le_supr_of_le a $ le_supr_of_le has $ _root_.le_refl _) },
{ exact (supr_le $ assume a, supr_le $ assume ha, coe_le_coe.2 $ le_cSup hb ha) }
end
lemma coe_Inf {s : set α} (hs : s.nonempty) : (↑(Inf s) : with_top α) = (⨅a∈s, ↑a) :=
let ⟨x, hx⟩ := hs in
have (⨅a∈s, ↑a : with_top α) ≤ x, from infi_le_of_le x $ infi_le_of_le hx $ _root_.le_refl _,
let ⟨r, r_eq, hr⟩ := le_coe_iff.1 this in
le_antisymm
(le_infi $ assume a, le_infi $ assume ha, coe_le_coe.2 $ cInf_le (order_bot.bdd_below s) ha)
begin
refine (r_eq.symm ▸ coe_le_coe.2 $ le_cInf hs $ assume a has, coe_le_coe.1 $ _),
refine (r_eq ▸ infi_le_of_le a _),
exact (infi_le_of_le has $ _root_.le_refl _),
end
end with_top
namespace enat
open_locale classical
noncomputable instance : complete_linear_order enat :=
{ Sup := λ s, with_top_equiv.symm $ Sup (with_top_equiv '' s),
Inf := λ s, with_top_equiv.symm $ Inf (with_top_equiv '' s),
le_Sup := by intros; rw ← with_top_equiv_le; simp; apply le_Sup _; simpa,
Inf_le := by intros; rw ← with_top_equiv_le; simp; apply Inf_le _; simpa,
Sup_le := begin
intros s a h1,
rw [← with_top_equiv_le, with_top_equiv.right_inverse_symm],
apply Sup_le _,
rintros b ⟨x, h2, rfl⟩,
rw with_top_equiv_le,
apply h1,
assumption
end,
le_Inf := begin
intros s a h1,
rw [← with_top_equiv_le, with_top_equiv.right_inverse_symm],
apply le_Inf _,
rintros b ⟨x, h2, rfl⟩,
rw with_top_equiv_le,
apply h1,
assumption
end,
..enat.linear_order,
..enat.bounded_lattice }
end enat
section order_dual
instance (α : Type*) [conditionally_complete_lattice α] :
conditionally_complete_lattice (order_dual α) :=
{ le_cSup := @cInf_le α _,
cSup_le := @le_cInf α _,
le_cInf := @cSup_le α _,
cInf_le := @le_cSup α _,
..order_dual.has_Inf α,
..order_dual.has_Sup α,
..order_dual.lattice α }
instance (α : Type*) [conditionally_complete_linear_order α] :
conditionally_complete_linear_order (order_dual α) :=
{ ..order_dual.conditionally_complete_lattice α,
..order_dual.linear_order α }
end order_dual
namespace monotone
variables [preorder α] [conditionally_complete_lattice β] {f : α → β} (h_mono : monotone f)
/-! A monotone function into a conditionally complete lattice preserves the ordering properties of
`Sup` and `Inf`. -/
lemma le_cSup_image {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_above s) :
f c ≤ Sup (f '' s) :=
le_cSup (map_bdd_above h_mono h_bdd) (mem_image_of_mem f hcs)
lemma cSup_image_le {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ upper_bounds s) :
Sup (f '' s) ≤ f B :=
cSup_le (nonempty.image f hs) (h_mono.mem_upper_bounds_image hB)
lemma cInf_image_le {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_below s) :
Inf (f '' s) ≤ f c :=
@le_cSup_image (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ _ hcs h_bdd
lemma le_cInf_image {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ lower_bounds s) :
f B ≤ Inf (f '' s) :=
@cSup_image_le (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ hs _ hB
end monotone
section with_top_bot
/-!
### Complete lattice structure on `with_top (with_bot α)`
If `α` is a `conditionally_complete_lattice`, then we show that `with_top α` and `with_bot α`
also inherit the structure of conditionally complete lattices. Furthermore, we show
that `with_top (with_bot α)` naturally inherits the structure of a complete lattice. Note that
for α a conditionally complete lattice, `Sup` and `Inf` both return junk values
for sets which are empty or unbounded. The extension of `Sup` to `with_top α` fixes
the unboundedness problem and the extension to `with_bot α` fixes the problem with
the empty set.
This result can be used to show that the extended reals [-∞, ∞] are a complete lattice.
-/
open_locale classical
/-- Adding a top element to a conditionally complete lattice
gives a conditionally complete lattice -/
noncomputable instance with_top.conditionally_complete_lattice
{α : Type*} [conditionally_complete_lattice α] :
conditionally_complete_lattice (with_top α) :=
{ le_cSup := λ S a hS haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS,
cSup_le := λ S a hS haS, (with_top.is_lub_Sup' hS).2 haS,
cInf_le := λ S a hS haS, (with_top.is_glb_Inf' hS).1 haS,
le_cInf := λ S a hS haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS,
..with_top.lattice,
..with_top.has_Sup,
..with_top.has_Inf }
/-- Adding a bottom element to a conditionally complete lattice
gives a conditionally complete lattice -/
noncomputable instance with_bot.conditionally_complete_lattice
{α : Type*} [conditionally_complete_lattice α] :
conditionally_complete_lattice (with_bot α) :=
{ le_cSup := (@with_top.conditionally_complete_lattice (order_dual α) _).cInf_le,
cSup_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cInf,
cInf_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cSup,
le_cInf := (@with_top.conditionally_complete_lattice (order_dual α) _).cSup_le,
..with_bot.lattice,
..with_bot.has_Sup,
..with_bot.has_Inf }
/-- Adding a bottom and a top to a conditionally complete lattice gives a bounded lattice-/
noncomputable instance with_top.with_bot.bounded_lattice {α : Type*}
[conditionally_complete_lattice α] : bounded_lattice (with_top (with_bot α)) :=
{ ..with_top.order_bot,
..with_top.order_top,
..conditionally_complete_lattice.to_lattice _ }
theorem with_bot.cSup_empty {α : Type*} [conditionally_complete_lattice α] :
Sup (∅ : set (with_bot α)) = ⊥ :=
begin
show ite _ _ _ = ⊥,
split_ifs; finish,
end
noncomputable instance with_top.with_bot.complete_lattice {α : Type*}
[conditionally_complete_lattice α] : complete_lattice (with_top (with_bot α)) :=
{ le_Sup := λ S a haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS,
Sup_le := λ S a ha,
begin
cases S.eq_empty_or_nonempty with h,
{ show ite _ _ _ ≤ a,
split_ifs,
{ rw h at h_1, cases h_1 },
{ convert bot_le, convert with_bot.cSup_empty, rw h, refl },
{ exfalso, apply h_2, use ⊥, rw h, rintro b ⟨⟩ } },
{ refine (with_top.is_lub_Sup' h).2 ha }
end,
Inf_le := λ S a haS,
show ite _ _ _ ≤ a,
begin
split_ifs,
{ cases a with a, exact _root_.le_refl _,
cases (h haS); tauto },
{ cases a,
{ exact le_top },
{ apply with_top.some_le_some.2, refine cInf_le _ haS, use ⊥, intros b hb, exact bot_le } }
end,
le_Inf := λ S a haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS,
..with_top.has_Inf,
..with_top.has_Sup,
..with_top.with_bot.bounded_lattice }
end with_top_bot
section subtype
variables (s : set α)
/-! ### Subtypes of conditionally complete linear orders
In this section we give conditions on a subset of a conditionally complete linear order, to ensure
that the subtype is itself conditionally complete.
We check that an `ord_connected` set satisfies these conditions.
TODO There are several possible variants; the `conditionally_complete_linear_order` could be changed
to `conditionally_complete_linear_order_bot` or `complete_linear_order`.
-/
open_locale classical
section has_Sup
variables [has_Sup α]
/-- `has_Sup` structure on a nonempty subset `s` of an object with `has_Sup`. This definition is
non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the
construction of the `conditionally_complete_linear_order` structure. -/
noncomputable def subset_has_Sup [inhabited s] : has_Sup s := {Sup := λ t,
if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s}
local attribute [instance] subset_has_Sup
@[simp] lemma subset_Sup_def [inhabited s] :
@Sup s _ = λ t,
if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s :=
rfl
lemma subset_Sup_of_within [inhabited s] {t : set s} (h : Sup (coe '' t : set α) ∈ s) :
Sup (coe '' t : set α) = (@Sup s _ t : α) :=
by simp [dif_pos h]
end has_Sup
section has_Inf
variables [has_Inf α]
/-- `has_Inf` structure on a nonempty subset `s` of an object with `has_Inf`. This definition is
non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the
construction of the `conditionally_complete_linear_order` structure. -/
noncomputable def subset_has_Inf [inhabited s] : has_Inf s := {Inf := λ t,
if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s}
local attribute [instance] subset_has_Inf
@[simp] lemma subset_Inf_def [inhabited s] :
@Inf s _ = λ t,
if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s :=
rfl
lemma subset_Inf_of_within [inhabited s] {t : set s} (h : Inf (coe '' t : set α) ∈ s) :
Inf (coe '' t : set α) = (@Inf s _ t : α) :=
by simp [dif_pos h]
end has_Inf
variables [conditionally_complete_linear_order α]
local attribute [instance] subset_has_Sup
local attribute [instance] subset_has_Inf
/-- For a nonempty subset of a conditionally complete linear order to be a conditionally complete
linear order, it suffices that it contain the `Sup` of all its nonempty bounded-above subsets, and
the `Inf` of all its nonempty bounded-below subsets. -/
noncomputable def subset_conditionally_complete_linear_order [inhabited s]
(h_Sup : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_above t), Sup (coe '' t : set α) ∈ s)
(h_Inf : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_below t), Inf (coe '' t : set α) ∈ s) :
conditionally_complete_linear_order s :=
{ le_cSup := begin
rintros t c h_bdd hct,
-- The following would be a more natural way to finish, but gives a "deep recursion" error:
-- simpa [subset_Sup_of_within (h_Sup t)] using
-- (strict_mono_coe s).monotone.le_cSup_image hct h_bdd,
have := (subtype.mono_coe s).le_cSup_image hct h_bdd,
rwa subset_Sup_of_within s (h_Sup ⟨c, hct⟩ h_bdd) at this,
end,
cSup_le := begin
rintros t B ht hB,
have := (subtype.mono_coe s).cSup_image_le ht hB,
rwa subset_Sup_of_within s (h_Sup ht ⟨B, hB⟩) at this,
end,
le_cInf := begin
intros t B ht hB,
have := (subtype.mono_coe s).le_cInf_image ht hB,
rwa subset_Inf_of_within s (h_Inf ht ⟨B, hB⟩) at this,
end,
cInf_le := begin
rintros t c h_bdd hct,
have := (subtype.mono_coe s).cInf_image_le hct h_bdd,
rwa subset_Inf_of_within s (h_Inf ⟨c, hct⟩ h_bdd) at this,
end,
..subset_has_Sup s,
..subset_has_Inf s,
..distrib_lattice.to_lattice s,
..(infer_instance : linear_order s) }
section ord_connected
/-- The `Sup` function on a nonempty `ord_connected` set `s` in a conditionally complete linear
order takes values within `s`, for all nonempty bounded-above subsets of `s`. -/
lemma Sup_within_of_ord_connected
{s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_above t) :
Sup (coe '' t : set α) ∈ s :=
begin
obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht,
obtain ⟨B, hB⟩ : ∃ B, B ∈ upper_bounds t := h_bdd,
refine hs.out c.2 B.2 ⟨_, _⟩,
{ exact (subtype.mono_coe s).le_cSup_image hct ⟨B, hB⟩ },
{ exact (subtype.mono_coe s).cSup_image_le ⟨c, hct⟩ hB },
end
/-- The `Inf` function on a nonempty `ord_connected` set `s` in a conditionally complete linear
order takes values within `s`, for all nonempty bounded-below subsets of `s`. -/
lemma Inf_within_of_ord_connected
{s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_below t) :
Inf (coe '' t : set α) ∈ s :=
begin
obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht,
obtain ⟨B, hB⟩ : ∃ B, B ∈ lower_bounds t := h_bdd,
refine hs.out B.2 c.2 ⟨_, _⟩,
{ exact (subtype.mono_coe s).le_cInf_image ⟨c, hct⟩ hB },
{ exact (subtype.mono_coe s).cInf_image_le hct ⟨B, hB⟩ },
end
/-- A nonempty `ord_connected` set in a conditionally complete linear order is naturally a
conditionally complete linear order. -/
noncomputable instance ord_connected_subset_conditionally_complete_linear_order
[inhabited s] [ord_connected s] :
conditionally_complete_linear_order s :=
subset_conditionally_complete_linear_order s Sup_within_of_ord_connected Inf_within_of_ord_connected
end ord_connected
end subtype
|
0802ffcd7320a7a095462767ef983a72b8851bd4 | 618003631150032a5676f229d13a079ac875ff77 | /src/category_theory/limits/shapes/strong_epi.lean | 649a0886ff14ba847358bbc37fe4feeb68872f0e | [
"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 | 2,715 | lean | /-
Copyright (c) 2020 Markus Himmel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Markus Himmel
-/
import category_theory.comma
/-!
# Strong epimorphisms
In this file, we define strong epimorphisms. A strong epimorphism is an epimorphism `f`, such
that for every commutative square with `f` at the top and a monomorphism at the bottom, there is
a diagonal morphism making the two triangles commute. This lift is necessarily unique (as shown in
`comma.lean`).
## Main results
Besides the definition, we show that
* the composition of two strong epimorphisms is a strong epimorphism,
* if `f ≫ g` is a strong epimorphism, then so is `g`,
* if `f` is both a strong epimorphism and a monomorphism, then it is an isomorphism
## Future work
There is also the dual notion of strong monomorphism.
## References
* [F. Borceux, *Handbook of Categorical Algebra 1*][borceux-vol1]
-/
universes v u
namespace category_theory
variables {C : Type u} [category.{v} C]
variables {P Q : C}
/-- A strong epimorphism `f` is an epimorphism such that every commutative square with `f` at the
top and a monomorphism at the bottom has a lift. -/
class strong_epi (f : P ⟶ Q) :=
(epi : epi f)
(has_lift : Π {X Y : C} {u : P ⟶ X} {v : Q ⟶ Y} {z : X ⟶ Y} [mono z] (h : u ≫ z = f ≫ v),
arrow.has_lift $ arrow.hom_mk' h)
attribute [instance] strong_epi.has_lift
@[priority 100]
instance epi_of_strong_epi (f : P ⟶ Q) [strong_epi f] : epi f := strong_epi.epi
section
variables {R : C} (f : P ⟶ Q) (g : Q ⟶ R)
/-- The composition of two strong epimorphisms is a strong epimorphism. -/
def strong_epi_comp [strong_epi f] [strong_epi g] : strong_epi (f ≫ g) :=
{ epi := epi_comp _ _,
has_lift :=
begin
introsI,
have h₀ : u ≫ z = f ≫ g ≫ v, by simpa [category.assoc] using h,
let w : Q ⟶ X := arrow.lift (arrow.hom_mk' h₀),
have h₁ : w ≫ z = g ≫ v, by rw arrow.lift_mk'_right,
exact ⟨(arrow.lift (arrow.hom_mk' h₁) : R ⟶ X), by simp, by simp⟩
end }
/-- If `f ≫ g` is a strong epimorphism, then so is g. -/
def strong_epi_of_strong_epi [strong_epi (f ≫ g)] : strong_epi g :=
{ epi := epi_of_epi f g,
has_lift :=
begin
introsI,
have h₀ : (f ≫ u) ≫ z = (f ≫ g) ≫ v, by simp only [category.assoc, h],
exact ⟨(arrow.lift (arrow.hom_mk' h₀) : R ⟶ X), (cancel_mono z).1 (by simp [h]), by simp⟩,
end }
end
/-- A strong epimorphism that is a monomorphism is an isomorphism. -/
def is_iso_of_mono_of_strong_epi (f : P ⟶ Q) [mono f] [strong_epi f] : is_iso f :=
{ inv := arrow.lift $ arrow.hom_mk' $ show 𝟙 P ≫ f = f ≫ 𝟙 Q, by simp }
end category_theory
|
9df704dcec8d74a82b8e7e4f5410327d706c50fc | e2fc96178628c7451e998a0db2b73877d0648be5 | /src/classes/context_free/closure_properties/intersection.lean | 0b064e334046dd576718657eee9aac6eac0f2f00 | [
"BSD-2-Clause"
] | permissive | madvorak/grammars | cd324ae19b28f7b8be9c3ad010ef7bf0fabe5df2 | 1447343a45fcb7821070f1e20b57288d437323a6 | refs/heads/main | 1,692,383,644,884 | 1,692,032,429,000 | 1,692,032,429,000 | 453,948,141 | 7 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 40,103 | lean | import classes.context_free.basics.pumping
import classes.context_free.basics.elementary
import classes.context_free.closure_properties.concatenation
import classes.context_free.closure_properties.permutation
section defs_over_fin3
private def a_ : fin 3 := 0
private def b_ : fin 3 := 1
private def c_ : fin 3 := 2
private def a : symbol (fin 3) (fin 1) := symbol.terminal a_
private def b : symbol (fin 3) (fin 1) := symbol.terminal b_
private def c : symbol (fin 3) (fin 1) := symbol.terminal c_
private lemma neq_ab : a_ ≠ b_ := by dec_trivial
private lemma neq_ba : b_ ≠ a_ := neq_ab.symm
private lemma neq_ac : a_ ≠ c_ := by dec_trivial
private lemma neq_ca : c_ ≠ a_ := neq_ac.symm
private lemma neq_bc : b_ ≠ c_ := by dec_trivial
private lemma neq_cb : c_ ≠ b_ := neq_bc.symm
private def lang_eq_any : language (fin 3) :=
λ w, ∃ n m : ℕ, w = list.repeat a_ n ++ list.repeat b_ n ++ list.repeat c_ m
private def lang_any_eq : language (fin 3) :=
λ w, ∃ n m : ℕ, w = list.repeat a_ n ++ list.repeat b_ m ++ list.repeat c_ m
private def lang_eq_eq : language (fin 3) :=
λ w, ∃ n : ℕ, w = list.repeat a_ n ++ list.repeat b_ n ++ list.repeat c_ n
end defs_over_fin3
section not_CF
private lemma false_of_uvvxyyz
{_a _b _c : fin 3} {n : ℕ} {u v x y z : list (fin 3)}
(elimin : ∀ s : fin 3, s ≠ _a → s ≠ _b → s ≠ _c → false)
(no_a : _a ∉ v ++ y) (nonempty : (v ++ y).length > 0)
(counts_ab : ∀ (w : list (fin 3)), w ∈ lang_eq_eq → list.count_in w _a = list.count_in w _b)
(counts_ac : ∀ (w : list (fin 3)), w ∈ lang_eq_eq → list.count_in w _a = list.count_in w _c)
(counted_a : list.count_in (u ++ v ++ x ++ y ++ z ++ (v ++ y)) _a = n + 1 + list.count_in (v ++ y) _a)
(counted_b : list.count_in (u ++ v ++ x ++ y ++ z ++ (v ++ y)) _b = n + 1 + list.count_in (v ++ y) _b)
(counted_c : list.count_in (u ++ v ++ x ++ y ++ z ++ (v ++ y)) _c = n + 1 + list.count_in (v ++ y) _c)
(pumping : u ++ v ^ 2 ++ x ++ y ^ 2 ++ z ∈ lang_eq_eq) :
false :=
begin
have extra_not_a : _b ∈ (v ++ y) ∨ _c ∈ (v ++ y),
{
let first_letter := (v ++ y).nth_le 0 nonempty,
have first_letter_b_or_c : first_letter = _b ∨ first_letter = _c,
{
have first_letter_not_a : first_letter ≠ _a,
{
by_contradiction contra,
have yes_a : _a ∈ v ++ y,
{
rw ←contra,
apply list.nth_le_mem,
},
exact no_a yes_a,
},
by_contradiction contr,
push_neg at contr,
cases contr with first_letter_not_b first_letter_not_c,
exact elimin ((v ++ y).nth_le 0 nonempty) first_letter_not_a first_letter_not_b first_letter_not_c,
},
cases first_letter_b_or_c with first_letter_b first_letter_c,
{
left,
rw ←first_letter_b,
apply list.nth_le_mem,
},
{
right,
rw ←first_letter_c,
apply list.nth_le_mem,
},
},
have hab := counts_ab (u ++ v ^ 2 ++ x ++ y ^ 2 ++ z) pumping,
have hac := counts_ac (u ++ v ^ 2 ++ x ++ y ^ 2 ++ z) pumping,
cases pumping with n' pump',
have count_a : list.count_in (u ++ v ^ 2 ++ x ++ y ^ 2 ++ z) _a = n + 1,
{
unfold list.n_times,
simp [- list.append_assoc],
repeat {
rw list.count_in_append,
},
have rearrange :
list.count_in u _a + (list.count_in v _a + list.count_in v _a) + list.count_in x _a +
(list.count_in y _a + list.count_in y _a) + list.count_in z _a =
(list.count_in u _a + list.count_in v _a + list.count_in x _a + list.count_in y _a + list.count_in z _a) +
(list.count_in v _a + list.count_in y _a),
{
ring,
},
have zero_in_v : list.count_in v _a = 0,
{
rw list.mem_append at no_a,
push_neg at no_a,
exact list.count_in_zero_of_notin no_a.left,
},
have zero_in_y : list.count_in y _a = 0,
{
rw list.mem_append at no_a,
push_neg at no_a,
exact list.count_in_zero_of_notin no_a.right,
},
rw rearrange,
repeat {
rw ←list.count_in_append,
},
rw counted_a,
rw list.count_in_append,
rw zero_in_v,
rw zero_in_y,
},
cases extra_not_a with extra_b extra_c,
{
have count_b : list.count_in (u ++ v ^ 2 ++ x ++ y ^ 2 ++ z) _b > n + 1,
{
unfold list.n_times,
simp [- list.append_assoc],
repeat {
rw list.count_in_append,
},
have big_equality :
list.count_in u _b + (list.count_in v _b + list.count_in v _b) + list.count_in x _b +
(list.count_in y _b + list.count_in y _b) + list.count_in z _b =
(list.count_in u _b + list.count_in v _b + list.count_in x _b + list.count_in y _b + list.count_in z _b) +
(list.count_in v _b + list.count_in y _b),
{
ring,
},
rw big_equality,
repeat {
rw ←list.count_in_append,
},
rw counted_b,
have at_least_one_b : list.count_in (v ++ y) _b > 0,
{
exact list.count_in_pos_of_in extra_b,
},
linarith,
},
rw count_a at hab,
rw hab at count_b,
exact has_lt.lt.false count_b,
},
{
have count_c : list.count_in (u ++ v ^ 2 ++ x ++ y ^ 2 ++ z) _c > n + 1,
{
unfold list.n_times,
simp [- list.append_assoc],
repeat {
rw list.count_in_append,
},
have big_equality :
list.count_in u _c + (list.count_in v _c + list.count_in v _c) + list.count_in x _c +
(list.count_in y _c + list.count_in y _c) + list.count_in z _c =
(list.count_in u _c + list.count_in v _c + list.count_in x _c + list.count_in y _c + list.count_in z _c) +
(list.count_in v _c + list.count_in y _c),
{
ring,
},
rw big_equality,
repeat {
rw ←list.count_in_append,
},
rw counted_c,
have at_least_one_c : list.count_in (v ++ y) _c > 0,
{
exact list.count_in_pos_of_in extra_c,
},
linarith,
},
rw count_a at hac,
rw hac at count_c,
exact has_lt.lt.false count_c,
},
end
private lemma notCF_lang_eq_eq : ¬ is_CF lang_eq_eq :=
begin
intro h,
have pum := CF_pumping h,
cases pum with n pump,
specialize pump (list.repeat a_ (n+1) ++ list.repeat b_ (n+1) ++ list.repeat c_ (n+1)),
specialize pump (by { use n + 1, }) (by {
rw list.length_append,
rw list.length_repeat,
calc (list.repeat a_ (n + 1) ++ list.repeat b_ (n + 1)).length + (n + 1)
≥ n + 1 : le_add_self
... ≥ n : nat.le_succ n,
}),
rcases pump with ⟨u, v, x, y, z, concatenating, nonempty, vxy_short, pumping⟩,
specialize pumping 2,
have not_all_letters : a_ ∉ (v ++ y) ∨ b_ ∉ (v ++ y) ∨ c_ ∉ (v ++ y),
{
by_contradiction contr,
push_neg at contr,
rcases contr with ⟨hva, -, hvc⟩,
have vxy_long : (v ++ x ++ y).length > n,
{
by_contradiction contr,
push_neg at contr,
have total_length_exactly : u.length + (v ++ x ++ y).length + z.length = 3 * n + 3,
{
have total_length := congr_arg list.length concatenating,
repeat {
rw list.length_append at total_length,
},
repeat {
rw list.length_repeat at total_length,
},
ring_nf at total_length,
rw ←add_assoc x.length at total_length,
rw ←add_assoc v.length at total_length,
rw ←add_assoc v.length at total_length,
rw ←add_assoc u.length at total_length,
rw ←list.length_append_append at total_length,
exact total_length.symm,
},
have u_short : u.length ≤ n,
{
-- in contradiction with `hva: a_ ∈ v ++ y`
by_contradiction u_too_much,
push_neg at u_too_much,
have relaxed_a : a_ ∈ v ++ x ++ y ++ z,
{
cases (list.mem_append.1 hva) with a_in_v a_in_y,
{
rw list.append_assoc,
rw list.append_assoc,
rw list.mem_append,
left,
exact a_in_v,
},
{
have a_in_yz : a_ ∈ y ++ z,
{
rw list.mem_append,
left,
exact a_in_y,
},
rw list.append_assoc,
rw list.mem_append,
right,
exact a_in_yz,
},
},
repeat {
rw list.append_assoc at concatenating,
},
rcases list.nth_le_of_mem relaxed_a with ⟨nₐ, hnₐ, h_nthₐ⟩,
obtain ⟨h_nth_a_pr, h_nth_a⟩ :
∃ proofoo, (v ++ x ++ y ++ z).nth_le ((nₐ + u.length) - u.length) proofoo = a_,
{
rw nat.add_sub_cancel nₐ u.length,
use hnₐ,
exact h_nthₐ,
},
have lt_len : (nₐ + u.length) < (u ++ (v ++ x ++ y ++ z)).length,
{
rw list.length_append,
linarith,
},
rw ←list.nth_le_append_right le_add_self lt_len at h_nth_a,
have orig_nth_le_eq_a :
∃ proofoo,
(list.repeat a_ (n + 1) ++ (list.repeat b_ (n + 1) ++ list.repeat c_ (n + 1))).nth_le
(nₐ + u.length) proofoo =
a_,
{
have rebracket : u ++ (v ++ (x ++ (y ++ z))) = u ++ (v ++ x ++ y ++ z),
{
simp only [list.append_assoc],
},
rw concatenating,
rw rebracket,
use lt_len,
exact h_nth_a,
},
cases orig_nth_le_eq_a with rrr_nth_le_eq_a_pr rrr_nth_le_eq_a,
rw @list.nth_le_append_right (fin 3)
(list.repeat a_ (n + 1))
(list.repeat b_ (n + 1) ++ list.repeat c_ (n + 1))
(nₐ + u.length)
(by {
rw list.length_repeat,
calc n + 1 ≤ u.length : u_too_much
... ≤ nₐ + u.length : le_add_self,
})
(by {
rw concatenating,
rw ←list.append_assoc x,
rw ←list.append_assoc v,
rw ←list.append_assoc v,
exact lt_len,
}) at rrr_nth_le_eq_a,
have a_in_rb_rc : a_ ∈ (list.repeat b_ (n + 1) ++ list.repeat c_ (n + 1)),
{
rw ←rrr_nth_le_eq_a,
apply list.nth_le_mem,
},
rw list.mem_append at a_in_rb_rc,
cases a_in_rb_rc,
{
rw list.mem_repeat at a_in_rb_rc,
exact neq_ab a_in_rb_rc.right,
},
{
rw list.mem_repeat at a_in_rb_rc,
exact neq_ac a_in_rb_rc.right,
},
},
have z_short : z.length ≤ n,
{
have our_bound : 2 * n + 2 < (u ++ v ++ x ++ y).length,
{
have relaxed_c : c_ ∈ u ++ v ++ x ++ y,
{
cases (list.mem_append.1 hvc) with c_in_v c_in_y,
{
have c_in_uv : c_ ∈ u ++ v,
{
rw list.mem_append,
right,
exact c_in_v,
},
rw list.append_assoc,
rw list.mem_append,
left,
exact c_in_uv,
},
{
rw list.mem_append,
right,
exact c_in_y,
},
},
repeat {
rw list.append_assoc at concatenating,
},
rcases list.nth_le_of_mem relaxed_c with ⟨m, hm, mth_is_c⟩,
have m_big : m ≥ 2 * n + 2,
{
have orig_mth_is_c :
∃ proofoo,
((list.repeat a_ (n + 1) ++ list.repeat b_ (n + 1)) ++ list.repeat c_ (n + 1)).nth_le
m proofoo =
c_,
{
repeat {
rw ←list.append_assoc at concatenating,
},
rw concatenating,
have m_small : m < (u ++ v ++ x ++ y ++ z).length,
{
rw list.length_append,
linarith,
},
rw ←@list.nth_le_append _ _ z m m_small at mth_is_c,
use m_small,
exact mth_is_c,
},
cases orig_mth_is_c with trash mth_is_c,
by_contradiction mle,
push_neg at mle,
have m_lt_len : m < (list.repeat a_ (n + 1) ++ list.repeat b_ (n + 1)).length,
{
rw list.length_append,
rw list.length_repeat,
rw list.length_repeat,
ring_nf,
exact mle,
},
rw list.nth_le_append _ m_lt_len at mth_is_c,
{
have c_in_ra_rb : c_ ∈ (list.repeat a_ (n + 1) ++ list.repeat b_ (n + 1)),
{
rw ←mth_is_c,
apply list.nth_le_mem,
},
rw list.mem_append at c_in_ra_rb,
cases c_in_ra_rb with c_in_ra c_in_rb,
{
rw list.mem_repeat at c_in_ra,
exact neq_ca c_in_ra.right,
},
{
rw list.mem_repeat at c_in_rb,
exact neq_cb c_in_rb.right,
},
},
},
linarith,
},
rw ←list.length_append at total_length_exactly,
rw ←list.append_assoc at total_length_exactly,
rw ←list.append_assoc at total_length_exactly,
linarith,
},
linarith,
},
exact not_le_of_gt vxy_long vxy_short,
},
have counts_ab : ∀ w ∈ lang_eq_eq, list.count_in w a_ = list.count_in w b_,
{
intros w w_in,
cases w_in with w_n w_prop,
rw w_prop,
repeat {
rw list.count_in_append,
},
rw list.count_in_repeat_neq neq_ab,
rw list.count_in_repeat_neq neq_ba,
rw list.count_in_repeat_neq neq_ca,
rw list.count_in_repeat_neq neq_cb,
rw list.count_in_repeat_eq a_,
rw list.count_in_repeat_eq b_,
repeat {
rw add_zero,
},
rw zero_add,
},
have counts_ac : ∀ w ∈ lang_eq_eq, list.count_in w a_ = list.count_in w c_,
{
intros w w_in,
cases w_in with w_n w_prop,
rw w_prop,
repeat {
rw list.count_in_append,
},
rw list.count_in_repeat_neq neq_ac,
rw list.count_in_repeat_neq neq_ca,
rw list.count_in_repeat_neq neq_ba,
rw list.count_in_repeat_neq neq_bc,
rw list.count_in_repeat_eq a_,
rw list.count_in_repeat_eq c_,
repeat {
rw add_zero,
},
rw zero_add,
},
have counts_bc : ∀ w ∈ lang_eq_eq, list.count_in w b_ = list.count_in w c_,
{
intros w w_in,
rw ←counts_ab w w_in,
exact counts_ac w w_in,
},
have counts_ba : ∀ w ∈ lang_eq_eq, list.count_in w b_ = list.count_in w a_,
{
intros w w_in,
symmetry,
exact counts_ab w w_in,
},
have counts_ca : ∀ w ∈ lang_eq_eq, list.count_in w c_ = list.count_in w a_,
{
intros w w_in,
symmetry,
exact counts_ac w w_in,
},
have counts_cb : ∀ w ∈ lang_eq_eq, list.count_in w c_ = list.count_in w b_,
{
intros w w_in,
symmetry,
exact counts_bc w w_in,
},
have counted_letter : ∀ s,
list.count_in (u ++ v ++ x ++ y ++ z ++ (v ++ y)) s =
list.count_in (list.repeat a_ (n+1)) s + list.count_in (list.repeat b_ (n+1)) s +
list.count_in (list.repeat c_ (n+1)) s + list.count_in (v ++ y) s,
{
intro s,
rw ←concatenating,
repeat {
rw list.count_in_append,
},
},
have counted_a : list.count_in (u ++ v ++ x ++ y ++ z ++ (v ++ y)) a_ = n + 1 + list.count_in (v ++ y) a_,
{
rw counted_letter,
rw list.count_in_repeat_eq a_,
rw list.count_in_repeat_neq neq_ba,
rw list.count_in_repeat_neq neq_ca,
},
have counted_b : list.count_in (u ++ v ++ x ++ y ++ z ++ (v ++ y)) b_ = n + 1 + list.count_in (v ++ y) b_,
{
rw counted_letter,
rw list.count_in_repeat_eq b_,
rw list.count_in_repeat_neq neq_cb,
rw list.count_in_repeat_neq neq_ab,
rw zero_add,
},
have counted_c : list.count_in (u ++ v ++ x ++ y ++ z ++ (v ++ y)) c_ = n + 1 + list.count_in (v ++ y) c_,
{
rw counted_letter,
rw list.count_in_repeat_eq c_,
rw list.count_in_repeat_neq neq_ac,
rw list.count_in_repeat_neq neq_bc,
rw zero_add,
},
have elimin_abc : ∀ s : fin 3, s ≠ a_ → s ≠ b_ → s ≠ c_ → false,
{
intros s hsa hsb hsc,
fin_cases s,
{
rw a_ at hsa,
exact hsa rfl,
},
{
rw b_ at hsb,
exact hsb rfl,
},
{
rw c_ at hsc,
exact hsc rfl,
},
},
cases not_all_letters with no_a foo,
{
exact false_of_uvvxyyz elimin_abc no_a nonempty counts_ab counts_ac
counted_a counted_b counted_c pumping,
},
cases foo with no_b no_c,
{
have elimin_bca : ∀ s : fin 3, s ≠ b_ → s ≠ c_ → s ≠ a_ → false,
{
intros s hsb hsc hsa,
exact elimin_abc s hsa hsb hsc,
},
exact false_of_uvvxyyz elimin_bca no_b nonempty counts_bc counts_ba
counted_b counted_c counted_a pumping,
},
{
have elimin_cab : ∀ s : fin 3, s ≠ c_ → s ≠ a_ → s ≠ b_ → false,
{
intros s hsc hsa hsb,
exact elimin_abc s hsa hsb hsc,
},
exact false_of_uvvxyyz elimin_cab no_c nonempty counts_ca counts_cb
counted_c counted_a counted_b pumping,
},
end
end not_CF
section yes_CF
private def lang_aux_ab : language (fin 3) :=
λ w, ∃ n : ℕ, w = list.repeat a_ n ++ list.repeat b_ n
private lemma CF_lang_aux_ab : is_CF lang_aux_ab :=
begin
let S_ : fin 1 := 0,
let S : symbol (fin 3) (fin 1) := symbol.nonterminal S_,
let g := CF_grammar.mk
(fin 1)
S_
[
(S_, [a, S, b]),
(S_, ([] : list (symbol (fin 3) (fin 1))))
],
use g,
apply set.eq_of_subset_of_subset,
{
intros w ass,
change CF_derives g [S] (list.map symbol.terminal w) at ass,
have indu :
∀ v : list (symbol (fin 3) (fin 1)),
CF_derives g [S] v →
∃ n : ℕ,
v = list.repeat a n ++ list.repeat b n ∨
v = list.repeat a n ++ [S] ++ list.repeat b n,
{
intros v hyp,
induction hyp with u u' trash orig hyp_ih,
{
use 0,
right,
refl,
},
rcases orig with ⟨r, rin, p, q, bef, aft⟩,
cases hyp_ih with k ih,
cases ih,
{
exfalso,
rw ih at bef,
have yes_in : symbol.nonterminal r.fst ∈ p ++ [symbol.nonterminal r.fst] ++ q,
{
apply list.mem_append_left,
apply list.mem_append_right,
apply list.mem_cons_self,
},
have not_in : symbol.nonterminal r.fst ∉ list.repeat a k ++ list.repeat b k,
{
rw list.mem_append_eq,
push_neg,
split;
{
rw list.mem_repeat,
push_neg,
intro trash,
apply symbol.no_confusion,
},
},
rw bef at not_in,
exact not_in yes_in,
},
have both_rule_rewrite_S : symbol.nonterminal r.fst = S,
{
cases rin,
{
rw rin,
},
cases rin,
{
rw rin,
},
cases rin,
},
rw bef at ih,
rw both_rule_rewrite_S at ih,
have p_len : p.length = k,
{
by_contradiction contra,
have kth_eq := congr_fun (congr_arg list.nth ih) p.length,
have plengthth_is_S : (p ++ [S] ++ q).nth p.length = some S,
{
rw list.append_assoc,
rw list.nth_append_right (le_of_eq rfl),
{
rw nat.sub_self,
refl,
},
},
rw plengthth_is_S at kth_eq,
cases lt_or_gt_of_ne contra,
{
have plengthth_is_a :
(list.repeat a k ++ [S] ++ list.repeat b k).nth p.length =
some a,
{
rw list.append_assoc,
have plength_small : p.length < (list.repeat a k).length,
{
rw list.length_repeat,
exact h,
},
rw list.nth_append plength_small,
rw list.nth_le_nth plength_small,
apply congr_arg,
exact list.nth_le_repeat a plength_small,
},
rw plengthth_is_a at kth_eq,
have S_neq_a : S ≠ a,
{
apply symbol.no_confusion,
},
rw option.some_inj at kth_eq,
exact S_neq_a kth_eq,
},
{
have plengthth_is_b :
(list.repeat a k ++ [S] ++ list.repeat b k).nth p.length =
some b,
{
have plength_big : (list.repeat a k ++ [S]).length ≤ p.length,
{
rw list.length_append,
rw list.length_repeat,
exact nat.succ_le_iff.mpr h,
},
rw list.nth_append_right plength_big,
have len_within_list : p.length - (list.repeat a k ++ [S]).length < (list.repeat b k).length,
{
have ihlen := congr_arg list.length ih,
simp only [list.length_repeat, list.length_append, list.length_singleton] at *,
have ihlen' : p.length + 1 ≤ k + 1 + k,
{
exact nat.le.intro ihlen,
},
have ihlen'' : p.length < k + 1 + k,
{
exact nat.succ_le_iff.mp ihlen',
},
rw ←tsub_lt_iff_left plength_big at ihlen'',
exact ihlen'',
},
rw list.nth_le_nth len_within_list,
apply congr_arg,
exact list.nth_le_repeat b len_within_list,
},
rw plengthth_is_b at kth_eq,
have S_neq_b : S ≠ b,
{
apply symbol.no_confusion,
},
rw option.some_inj at kth_eq,
exact S_neq_b kth_eq,
},
},
have ihl_len : (p ++ [symbol.nonterminal S_]).length = k + 1,
{
rw list.length_append,
rw p_len,
refl,
},
have ihr_len : (list.repeat a k ++ [S]).length = k + 1,
{
rw list.length_append,
rw list.length_repeat,
refl,
},
have qb : q = list.repeat b k,
{
apply list.append_inj_right ih,
rw ihl_len,
rw ihr_len,
},
have ih_reduced : p ++ [symbol.nonterminal S_] = list.repeat a k ++ [S],
{
rw qb at ih,
rw list.append_left_inj at ih,
exact ih,
},
have pa : p = list.repeat a k,
{
rw list.append_left_inj at ih_reduced,
exact ih_reduced,
},
cases rin,
{
use k + 1,
right,
rw aft,
rw rin,
convert_to
p ++ (S_, [a, S, b]).snd ++ q =
list.repeat a k ++ [a] ++ [S] ++ [b] ++ list.repeat b k,
{
rw list.repeat_add,
rw add_comm,
rw list.repeat_add,
simp only [list.repeat, list.append_assoc],
},
rw [pa, qb],
simp only [list.append_assoc, list.cons_append, list.singleton_append],
},
cases rin,
{
use k,
left,
rw aft,
rw rin,
rw list.append_nil,
rw [pa, qb],
},
exfalso,
exact rin,
},
cases indu (list.map symbol.terminal w) ass with n instantiated,
clear indu,
cases instantiated,
{
use n,
have foo := congr_arg (list.filter_map (
λ z : symbol (fin 3) (fin 1),
match z with
| symbol.terminal t := some t
| symbol.nonterminal _ := none
end
)) instantiated,
rw list.filter_map_append at foo,
rw list.filter_map_map at foo,
rw list.filter_map_some at foo,
rw [foo, a, b],
clear foo,
apply congr_arg2;
{
clear instantiated,
induction n with n ih,
{
refl,
},
rw list.repeat_succ,
rw list.repeat_succ,
rw list.filter_map_cons,
simp only [eq_self_iff_true, true_and, ih],
},
},
exfalso,
have yes_in : S ∈ list.repeat a n ++ [S] ++ list.repeat b n,
{
apply list.mem_append_left,
apply list.mem_append_right,
apply list.mem_cons_self,
},
have not_in : S ∉ list.map symbol.terminal w,
{
intro hyp,
have S_isnt_terminal : ¬ ∃ x, S = symbol.terminal x,
{
tauto,
},
rw list.mem_map at hyp,
cases hyp with y hypo,
push_neg at S_isnt_terminal,
exact S_isnt_terminal y hypo.right.symm,
},
rw instantiated at not_in,
exact not_in yes_in,
},
{
intros w ass,
cases ass with n hw,
change CF_derives g [symbol.nonterminal g.initial] (list.map symbol.terminal w),
rw [hw, list.map_append, list.map_repeat, list.map_repeat, ←a, ←b],
clear hw,
induction n with n ih,
{
convert_to CF_derives g [symbol.nonterminal g.initial] [],
apply CF_deri_of_tran,
use (S_, ([] : list (symbol (fin 3) (fin 1)))),
split_ile,
use [[], []],
split;
refl,
},
convert_to
CF_derives g
[symbol.nonterminal g.initial]
(list.map
symbol.terminal
([a_] ++ (list.repeat a_ n ++ list.repeat b_ n) ++ [b_])
),
{
convert_to
list.repeat a (1 + n) ++ list.repeat b (n + 1) =
list.map symbol.terminal ([a_] ++ (list.repeat a_ n ++ list.repeat b_ n) ++ [b_]),
{
rw add_comm,
},
rw [
list.map_append_append,
list.map_singleton,
list.map_singleton,
list.repeat_add,
list.repeat_add,
a, b
],
simp only [list.repeat, list.append_assoc, list.map_append, list.map_repeat],
},
apply CF_deri_of_tran_deri,
{
use (S_, [a, S, b]),
split_ile,
use [[], []],
split;
refl,
},
rw list.map_append_append,
change
CF_derives g
([a] ++ [S] ++ [b])
([a] ++ list.map symbol.terminal (list.repeat a_ n ++ list.repeat b_ n) ++ [b]),
apply CF_deri_with_prefix_and_postfix,
convert ih,
rw [list.map_append, list.map_repeat, list.map_repeat, a, b],
},
end
private def lang_aux_c : language (fin 3) :=
λ w, ∃ n : ℕ, w = list.repeat c_ n
private lemma CF_lang_aux_c : is_CF lang_aux_c :=
begin
use cfg_symbol_star c_,
unfold lang_aux_c,
apply language_of_cfg_symbol_star,
end
private lemma CF_lang_eq_any : is_CF lang_eq_any :=
begin
have concatenated : lang_eq_any = lang_aux_ab * lang_aux_c,
{
ext1 w,
split,
{
rintro ⟨n, m, hnm⟩,
fconstructor,
use list.repeat a_ n ++ list.repeat b_ n,
use list.repeat c_ m,
split,
{
use n,
},
split,
{
use m,
},
exact hnm.symm,
},
{
rintro ⟨u, v, ⟨n, u_eq⟩, ⟨m, v_eq⟩, eq_w⟩,
use n,
use m,
rw [←eq_w, ←u_eq, ←v_eq],
},
},
rw concatenated,
apply CF_of_CF_c_CF,
exact and.intro CF_lang_aux_ab CF_lang_aux_c,
end
private def lang_aux_a : language (fin 3) :=
λ w, ∃ n : ℕ, w = list.repeat a_ n
private lemma CF_lang_aux_a : is_CF lang_aux_a :=
begin
use cfg_symbol_star a_,
unfold lang_aux_a,
apply language_of_cfg_symbol_star,
end
private def lang_aux_bc : language (fin 3) :=
λ w, ∃ n : ℕ, w = list.repeat b_ n ++ list.repeat c_ n
private def permut : equiv.perm (fin 3) := equiv.mk
(λ x, ite (x = 2) 0 (x + 1))
(λ x, ite (x = 0) 2 (x - 1))
(by {
intro x,
fin_cases x;
refl,
})
(by {
intro x,
fin_cases x;
refl,
})
private lemma CF_lang_aux_bc : is_CF lang_aux_bc :=
begin
have permuted : lang_aux_bc = permute_lang lang_aux_ab permut,
{
have compos : permut.to_fun ∘ permut.inv_fun = id,
{
simp,
},
ext1 w,
split;
{
intro h,
cases h with n hn,
use n,
try
{
rw hn
},
try
{
have other_case := congr_arg (list.map permut.to_fun) hn,
rw list.map_map at other_case,
rw compos at other_case,
rw list.map_id at other_case,
rw other_case,
},
rw list.map_append,
rw list.map_repeat,
rw list.map_repeat,
apply congr_arg2;
refl,
},
},
rw permuted,
exact CF_of_permute_CF permut lang_aux_ab CF_lang_aux_ab,
end
private lemma CF_lang_any_eq : is_CF lang_any_eq :=
begin
have concatenated : lang_any_eq = lang_aux_a * lang_aux_bc,
{
ext1 w,
split,
{
rintro ⟨n, m, hnm⟩,
fconstructor,
use list.repeat a_ n,
use list.repeat b_ m ++ list.repeat c_ m,
split,
{
use n,
},
split,
{
use m,
},
rw ←list.append_assoc,
exact hnm.symm,
},
{
rintro ⟨u, v, ⟨n, hu⟩, ⟨m, hv⟩, h⟩,
use n,
use m,
rw [list.append_assoc, ←h, ←hu, ←hv],
},
},
rw concatenated,
apply CF_of_CF_c_CF,
exact and.intro CF_lang_aux_a CF_lang_aux_bc,
end
end yes_CF
section intersection_inclusions
private lemma intersection_of_lang_eq_eq {w : list (fin 3)} :
w ∈ lang_eq_eq → w ∈ lang_eq_any ⊓ lang_any_eq :=
begin
intro h,
cases h with n hyp,
split;
{
use n,
use n,
exact hyp,
},
end
private lemma doubled_le_singled
(n₁ m₁ n₂ m₂ : ℕ) (n₁pos : n₁ > 0)
(a_ b_ c_ : fin 3) (a_neq_b : a_ ≠ b_) (a_neq_c : a_ ≠ c_)
(equ :
list.repeat a_ n₁ ++ list.repeat b_ n₁ ++ list.repeat c_ m₁ =
list.repeat a_ n₂ ++ list.repeat b_ m₂ ++ list.repeat c_ m₂
) :
n₁ ≤ n₂ :=
begin
by_contradiction contr,
push_neg at contr,
have n₁_le_len₁ : (n₁ - 1) < (list.repeat a_ n₁ ++ (list.repeat b_ n₁ ++ list.repeat c_ m₁)).length,
{
rw ←list.append_assoc,
rw list.length_append,
rw list.length_append,
rw list.length_repeat,
rw add_assoc,
apply nat.lt_add_right,
exact nat.sub_lt n₁pos (nat.succ_pos 0),
},
have n₁_le_len₂ : (n₁ - 1) < (list.repeat a_ n₂ ++ (list.repeat b_ m₂ ++ list.repeat c_ m₂)).length,
{
rw ←list.append_assoc,
have equ_len := congr_arg list.length equ,
rw ←equ_len,
rw list.append_assoc,
exact n₁_le_len₁,
},
have n₁th :
(list.repeat a_ n₁ ++ (list.repeat b_ n₁ ++ list.repeat c_ m₁)).nth_le (n₁ - 1) n₁_le_len₁ =
(list.repeat a_ n₂ ++ (list.repeat b_ m₂ ++ list.repeat c_ m₂)).nth_le (n₁ - 1) n₁_le_len₂,
{
rw list.append_assoc at equ,
rw list.append_assoc at equ,
exact list.nth_le_of_eq equ n₁_le_len₁,
},
have n₁th₁ : (list.repeat a_ n₁ ++ (list.repeat b_ n₁ ++ list.repeat c_ m₁)).nth_le (n₁ - 1) n₁_le_len₁ = a_,
{
have foo : (n₁ - 1) < (list.repeat a_ n₁).length,
{
rw list.length_repeat,
exact nat.sub_lt n₁pos (nat.succ_pos 0),
},
rw list.nth_le_append n₁_le_len₁ foo,
exact list.nth_le_repeat a_ foo,
},
have n₁th₂ : (list.repeat a_ n₂ ++ (list.repeat b_ m₂ ++ list.repeat c_ m₂)).nth_le (n₁ - 1) n₁_le_len₂ ≠ a_,
{
have foo : (list.repeat a_ n₂).length ≤ (n₁ - 1),
{
rw list.length_repeat,
exact nat.le_pred_of_lt contr,
},
rw list.nth_le_append_right foo n₁_le_len₂,
by_contradiction,
have a_in_bc : a_ ∈ (list.repeat b_ m₂ ++ list.repeat c_ m₂),
{
rw ←h,
apply list.nth_le_mem,
},
rw list.mem_append at a_in_bc,
cases a_in_bc,
{
rw list.mem_repeat at a_in_bc,
exact a_neq_b a_in_bc.right,
},
{
rw list.mem_repeat at a_in_bc,
exact a_neq_c a_in_bc.right,
},
},
rw n₁th₁ at n₁th,
rw ←n₁th at n₁th₂,
exact false_of_ne n₁th₂,
end
private lemma doubled_ge_singled
(n₁ m₁ n₂ m₂ : ℕ) (n₂pos : n₂ > 0)
(a_ b_ c_ : fin 3) (a_neq_b : a_ ≠ b_) (a_neq_c : a_ ≠ c_)
(equ :
list.repeat a_ n₁ ++ list.repeat b_ n₁ ++ list.repeat c_ m₁ =
list.repeat a_ n₂ ++ list.repeat b_ m₂ ++ list.repeat c_ m₂
) :
n₁ ≥ n₂ :=
begin
by_contradiction contr,
push_neg at contr,
have n₂_le_len₂ : (n₂ - 1) < (list.repeat a_ n₂ ++ (list.repeat b_ m₂ ++ list.repeat c_ m₂)).length,
{
rw ←list.append_assoc,
rw list.length_append,
rw list.length_append,
rw list.length_repeat,
rw add_assoc,
apply nat.lt_add_right,
exact nat.sub_lt n₂pos (nat.succ_pos 0),
},
have n₂_le_len₁ : (n₂ - 1) < (list.repeat a_ n₁ ++ (list.repeat b_ n₁ ++ list.repeat c_ m₁)).length,
{
rw ←list.append_assoc,
have equ_len := congr_arg list.length equ,
rw equ_len,
rw list.append_assoc,
exact n₂_le_len₂,
},
have n₂th :
(list.repeat a_ n₁ ++ (list.repeat b_ n₁ ++ list.repeat c_ m₁)).nth_le (n₂ - 1) n₂_le_len₁ =
(list.repeat a_ n₂ ++ (list.repeat b_ m₂ ++ list.repeat c_ m₂)).nth_le (n₂ - 1) n₂_le_len₂,
{
rw list.append_assoc at equ,
rw list.append_assoc at equ,
exact list.nth_le_of_eq equ n₂_le_len₁,
},
have n₂th₂ : (list.repeat a_ n₂ ++ (list.repeat b_ m₂ ++ list.repeat c_ m₂)).nth_le (n₂ - 1) n₂_le_len₂ = a_,
{
have foo : (n₂ - 1) < (list.repeat a_ n₂).length,
{
rw list.length_repeat,
exact nat.sub_lt n₂pos (nat.succ_pos 0),
},
rw list.nth_le_append n₂_le_len₂ foo,
exact list.nth_le_repeat a_ foo,
},
have n₂th₁ : (list.repeat a_ n₁ ++ (list.repeat b_ n₁ ++ list.repeat c_ m₁)).nth_le (n₂ - 1) n₂_le_len₁ ≠ a_,
{
have foo : (list.repeat a_ n₁).length ≤ (n₂ - 1),
{
rw list.length_repeat,
exact nat.le_pred_of_lt contr,
},
rw list.nth_le_append_right foo n₂_le_len₁,
by_contradiction,
have a_in_bc : a_ ∈ (list.repeat b_ n₁ ++ list.repeat c_ m₁),
{
rw ←h,
apply list.nth_le_mem,
},
rw list.mem_append at a_in_bc,
cases a_in_bc,
{
rw list.mem_repeat at a_in_bc,
exact a_neq_b a_in_bc.right,
},
{
rw list.mem_repeat at a_in_bc,
exact a_neq_c a_in_bc.right,
},
},
rw n₂th₂ at n₂th,
rw n₂th at n₂th₁,
exact false_of_ne n₂th₁,
end
private lemma lang_eq_eq_of_intersection {w : list (fin 3)} :
w ∈ lang_eq_any ⊓ lang_any_eq → w ∈ lang_eq_eq :=
begin
rintro ⟨⟨n₁, m₁, w_eq₁⟩, ⟨n₂, m₂, w_eq₂⟩⟩,
have equ := eq.trans w_eq₁.symm w_eq₂,
by_cases hn₁ : n₁ = 0,
{
have hn₂ : n₂ = 0,
{
by_contradiction,
have pos := nat.pos_of_ne_zero h,
clear h,
have a_in_equ := congr_arg (λ lis, a_ ∈ lis) equ,
clear equ,
rw hn₁ at a_in_equ,
have a_yes : a_ ∈ list.repeat a_ n₂,
{
rw list.mem_repeat,
exact and.intro (ne_of_lt pos).symm rfl,
},
simp [a_yes] at a_in_equ,
rw list.mem_repeat at a_in_equ,
exact neq_ac a_in_equ.right,
},
have hm₂ : m₂ = 0,
{
by_contradiction,
have pos := nat.pos_of_ne_zero h,
clear h,
have b_in_equ := congr_arg (λ lis, b_ ∈ lis) equ,
clear equ,
rw hn₁ at b_in_equ,
have b_yes : b_ ∈ list.repeat b_ m₂,
{
rw list.mem_repeat,
exact and.intro (ne_of_lt pos).symm rfl,
},
simp [b_yes] at b_in_equ,
rw list.mem_repeat at b_in_equ,
exact neq_bc b_in_equ.right,
},
use 0,
rw hn₂ at w_eq₂,
rw hm₂ at w_eq₂,
exact w_eq₂,
},
have n₁pos : n₁ > 0 := pos_iff_ne_zero.mpr hn₁,
have n₂pos : n₂ > 0,
{
by_contradiction,
have n₂zero : n₂ = 0,
{
push_neg at h,
rw nat.le_zero_iff at h,
exact h,
},
clear h,
rw n₂zero at equ,
simp only [list.repeat_zero, list.nil_append] at equ,
have a_in_equ := congr_arg (λ lis, a_ ∈ lis) equ,
clear equ,
simp only [list.mem_append, eq_iff_iff, list.mem_repeat, or_assoc] at a_in_equ,
have rs_false : (m₂ ≠ 0 ∧ a_ = b_ ∨ m₂ ≠ 0 ∧ a_ = c_) = false,
{
apply eq_false_intro,
push_neg,
split,
{
intro trash,
exact neq_ab,
},
{
intro trash,
exact neq_ac,
},
},
rw ←rs_false,
rw ←a_in_equ,
left,
split,
{
exact hn₁,
},
{
refl,
},
},
have m₂pos : m₂ > 0,
{
by_contradiction,
have m₂zero : m₂ = 0,
{
push_neg at h,
rw nat.le_zero_iff at h,
exact h,
},
clear h,
rw m₂zero at equ,
simp only [list.repeat_zero, list.append_nil] at equ,
have b_in_equ := congr_arg (λ lis, b_ ∈ lis) equ,
clear equ,
simp only [list.mem_append, eq_iff_iff, list.mem_repeat] at b_in_equ,
have := neq_ba,
tauto,
},
have m₁pos : m₁ > 0,
{
by_contradiction,
have m₁zero : m₁ = 0,
{
push_neg at h,
rw nat.le_zero_iff at h,
exact h,
},
clear h,
rw m₁zero at equ,
simp only [list.repeat_zero, list.append_nil] at equ,
have c_in_equ := congr_arg (λ lis, c_ ∈ lis) equ,
clear equ,
simp only [list.mem_append, eq_iff_iff, list.mem_repeat, or_assoc] at c_in_equ,
have ls_false : (n₁ ≠ 0 ∧ c_ = a_ ∨ n₁ ≠ 0 ∧ c_ = b_) = false,
{
apply eq_false_intro,
push_neg,
split,
{
intro trash,
exact neq_ca,
},
{
intro trash,
exact neq_cb,
},
},
rw ls_false at c_in_equ,
rw c_in_equ,
right,
right,
split,
{
exact ne_of_gt m₂pos,
},
{
refl,
},
},
have n_ge : n₁ ≥ n₂,
{
exact doubled_ge_singled n₁ m₁ n₂ m₂ n₂pos a_ b_ c_ neq_ab neq_ac equ,
},
have n_le : n₁ ≤ n₂,
{
exact doubled_le_singled n₁ m₁ n₂ m₂ n₁pos a_ b_ c_ neq_ab neq_ac equ,
},
have m_ge : m₁ ≥ m₂,
{
have rev := congr_arg list.reverse equ,
clear equ,
repeat {
rw list.reverse_append at rev,
},
repeat {
rw list.reverse_repeat at rev,
},
rw ←list.append_assoc at rev,
rw ←list.append_assoc at rev,
exact doubled_le_singled m₂ n₂ m₁ n₁ m₂pos c_ b_ a_ neq_cb neq_ca rev.symm,
},
have m_le : m₁ ≤ m₂,
{
have rev := congr_arg list.reverse equ,
clear equ,
repeat {
rw list.reverse_append at rev,
},
repeat {
rw list.reverse_repeat at rev,
},
rw ←list.append_assoc at rev,
rw ←list.append_assoc at rev,
exact doubled_ge_singled m₂ n₂ m₁ n₁ m₁pos c_ b_ a_ neq_cb neq_ca rev.symm,
},
have eqn : n₁ = n₂ :=
le_antisymm n_le n_ge,
have eqm : m₁ = m₂ :=
le_antisymm m_le m_ge,
have sum_lengs : n₁ + n₁ + m₁ = n₂ + m₂ + m₂,
{
have lengs := congr_arg list.length equ,
repeat {
rw list.length_append at lengs,
},
repeat {
rw list.length_repeat at lengs,
},
exact lengs,
},
have eq₂ : n₂ = m₂,
{
rw eqn at sum_lengs,
rw eqm at sum_lengs,
rw add_left_inj at sum_lengs,
rw add_right_inj at sum_lengs,
exact sum_lengs,
},
rw eq₂ at w_eq₂,
use m₂,
exact w_eq₂,
end
end intersection_inclusions
/-- The class of context-free languages isn't closed under intersection. -/
theorem nnyCF_of_CF_i_CF : ¬ (∀ T : Type, ∀ L₁ : language T, ∀ L₂ : language T,
is_CF L₁ ∧ is_CF L₂ → is_CF (L₁ ⊓ L₂)
) :=
begin
by_contradiction contra,
specialize contra (fin 3) lang_eq_any lang_any_eq ⟨CF_lang_eq_any, CF_lang_any_eq⟩,
apply notCF_lang_eq_eq,
convert contra,
apply set.eq_of_subset_of_subset,
{
apply intersection_of_lang_eq_eq,
},
{
apply lang_eq_eq_of_intersection,
},
end
|
7905a96526bc3d6b1db1832f33f3133fb339abd8 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/category_theory/opposites.lean | 9fc088e1d9fedde4937d911e2dbde4ceebb16d1a | [
"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 | 13,878 | 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.types
import category_theory.equivalence
import data.opposite
universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace category_theory
open opposite
variables {C : Type u₁}
section has_hom
variables [has_hom.{v₁} C]
/-- The hom types of the opposite of a category (or graph).
As with the objects, we'll make this irreducible below.
Use `f.op` and `f.unop` to convert between morphisms of C
and morphisms of Cᵒᵖ.
-/
instance has_hom.opposite : has_hom Cᵒᵖ :=
{ hom := λ X Y, unop Y ⟶ unop X }
/--
The opposite of a morphism in `C`.
-/
def has_hom.hom.op {X Y : C} (f : X ⟶ Y) : op Y ⟶ op X := f
/--
Given a morphism in `Cᵒᵖ`, we can take the "unopposite" back in `C`.
-/
def has_hom.hom.unop {X Y : Cᵒᵖ} (f : X ⟶ Y) : unop Y ⟶ unop X := f
attribute [irreducible] has_hom.opposite
lemma has_hom.hom.op_inj {X Y : C} :
function.injective (has_hom.hom.op : (X ⟶ Y) → (op Y ⟶ op X)) :=
λ _ _ H, congr_arg has_hom.hom.unop H
lemma has_hom.hom.unop_inj {X Y : Cᵒᵖ} :
function.injective (has_hom.hom.unop : (X ⟶ Y) → (unop Y ⟶ unop X)) :=
λ _ _ H, congr_arg has_hom.hom.op H
@[simp] lemma has_hom.hom.unop_op {X Y : C} {f : X ⟶ Y} : f.op.unop = f := rfl
@[simp] lemma has_hom.hom.op_unop {X Y : Cᵒᵖ} {f : X ⟶ Y} : f.unop.op = f := rfl
end has_hom
variables [category.{v₁} C]
/--
The opposite category.
See https://stacks.math.columbia.edu/tag/001M.
-/
instance category.opposite : category.{v₁} Cᵒᵖ :=
{ comp := λ _ _ _ f g, (g.unop ≫ f.unop).op,
id := λ X, (𝟙 (unop X)).op }
@[simp] lemma op_comp {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} :
(f ≫ g).op = g.op ≫ f.op := rfl
@[simp] lemma op_id {X : C} : (𝟙 X).op = 𝟙 (op X) := rfl
@[simp] lemma unop_comp {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : Y ⟶ Z} :
(f ≫ g).unop = g.unop ≫ f.unop := rfl
@[simp] lemma unop_id {X : Cᵒᵖ} : (𝟙 X).unop = 𝟙 (unop X) := rfl
@[simp] lemma unop_id_op {X : C} : (𝟙 (op X)).unop = 𝟙 X := rfl
@[simp] lemma op_id_unop {X : Cᵒᵖ} : (𝟙 (unop X)).op = 𝟙 X := rfl
section
variables (C)
/-- The functor from the double-opposite of a category to the underlying category. -/
@[simps]
def op_op : (Cᵒᵖ)ᵒᵖ ⥤ C :=
{ obj := λ X, unop (unop X),
map := λ X Y f, f.unop.unop }
/-- The functor from a category to its double-opposite. -/
@[simps]
def unop_unop : C ⥤ Cᵒᵖᵒᵖ :=
{ obj := λ X, op (op X),
map := λ X Y f, f.op.op }
/-- The double opposite category is equivalent to the original. -/
@[simps]
def op_op_equivalence : Cᵒᵖᵒᵖ ≌ C :=
{ functor := op_op C,
inverse := unop_unop C,
unit_iso := iso.refl (𝟭 Cᵒᵖᵒᵖ),
counit_iso := iso.refl (unop_unop C ⋙ op_op C) }
end
/--
If `f.op` is an isomorphism `f` must be too.
(This cannot be an instance as it would immediately loop!)
-/
def is_iso_of_op {X Y : C} (f : X ⟶ Y) [is_iso f.op] : is_iso f :=
{ inv := (inv (f.op)).unop,
hom_inv_id' := has_hom.hom.op_inj (by simp),
inv_hom_id' := has_hom.hom.op_inj (by simp) }
namespace functor
section
variables {D : Type u₂} [category.{v₂} D]
variables {C D}
/--
The opposite of a functor, i.e. considering a functor `F : C ⥤ D` as a functor `Cᵒᵖ ⥤ Dᵒᵖ`.
In informal mathematics no distinction is made between these.
-/
@[simps]
protected def op (F : C ⥤ D) : Cᵒᵖ ⥤ Dᵒᵖ :=
{ obj := λ X, op (F.obj (unop X)),
map := λ X Y f, (F.map f.unop).op }
/--
Given a functor `F : Cᵒᵖ ⥤ Dᵒᵖ` we can take the "unopposite" functor `F : C ⥤ D`.
In informal mathematics no distinction is made between these.
-/
@[simps]
protected def unop (F : Cᵒᵖ ⥤ Dᵒᵖ) : C ⥤ D :=
{ obj := λ X, unop (F.obj (op X)),
map := λ X Y f, (F.map f.op).unop }
/-- The isomorphism between `F.op.unop` and `F`. -/
def op_unop_iso (F : C ⥤ D) : F.op.unop ≅ F :=
nat_iso.of_components (λ X, iso.refl _) (by tidy)
/-- The isomorphism between `F.unop.op` and `F`. -/
def unop_op_iso (F : Cᵒᵖ ⥤ Dᵒᵖ) : F.unop.op ≅ F :=
nat_iso.of_components (λ X, iso.refl _) (by tidy)
variables (C D)
/--
Taking the opposite of a functor is functorial.
-/
@[simps]
def op_hom : (C ⥤ D)ᵒᵖ ⥤ (Cᵒᵖ ⥤ Dᵒᵖ) :=
{ obj := λ F, (unop F).op,
map := λ F G α,
{ app := λ X, (α.unop.app (unop X)).op,
naturality' := λ X Y f, has_hom.hom.unop_inj (α.unop.naturality f.unop).symm } }
/--
Take the "unopposite" of a functor is functorial.
-/
@[simps]
def op_inv : (Cᵒᵖ ⥤ Dᵒᵖ) ⥤ (C ⥤ D)ᵒᵖ :=
{ obj := λ F, op F.unop,
map := λ F G α, has_hom.hom.op
{ app := λ X, (α.app (op X)).unop,
naturality' := λ X Y f, has_hom.hom.op_inj $ (α.naturality f.op).symm } }
-- TODO show these form an equivalence
variables {C D}
/--
Another variant of the opposite of functor, turning a functor `C ⥤ Dᵒᵖ` into a functor `Cᵒᵖ ⥤ D`.
In informal mathematics no distinction is made.
-/
@[simps]
protected def left_op (F : C ⥤ Dᵒᵖ) : Cᵒᵖ ⥤ D :=
{ obj := λ X, unop (F.obj (unop X)),
map := λ X Y f, (F.map f.unop).unop }
/--
Another variant of the opposite of functor, turning a functor `Cᵒᵖ ⥤ D` into a functor `C ⥤ Dᵒᵖ`.
In informal mathematics no distinction is made.
-/
@[simps]
protected def right_op (F : Cᵒᵖ ⥤ D) : C ⥤ Dᵒᵖ :=
{ obj := λ X, op (F.obj (op X)),
map := λ X Y f, (F.map f.op).op }
-- TODO show these form an equivalence
instance {F : C ⥤ D} [full F] : full F.op :=
{ preimage := λ X Y f, (F.preimage f.unop).op }
instance {F : C ⥤ D} [faithful F] : faithful F.op :=
{ map_injective' := λ X Y f g h,
has_hom.hom.unop_inj $ by simpa using map_injective F (has_hom.hom.op_inj h) }
/-- If F is faithful then the right_op of F is also faithful. -/
instance right_op_faithful {F : Cᵒᵖ ⥤ D} [faithful F] : faithful F.right_op :=
{ map_injective' := λ X Y f g h, has_hom.hom.op_inj (map_injective F (has_hom.hom.op_inj h)) }
/-- If F is faithful then the left_op of F is also faithful. -/
instance left_op_faithful {F : C ⥤ Dᵒᵖ} [faithful F] : faithful F.left_op :=
{ map_injective' := λ X Y f g h, has_hom.hom.unop_inj (map_injective F (has_hom.hom.unop_inj h)) }
end
end functor
namespace nat_trans
variables {D : Type u₂} [category.{v₂} D]
section
variables {F G : C ⥤ D}
local attribute [semireducible] has_hom.opposite
/-- The opposite of a natural transformation. -/
@[simps] protected def op (α : F ⟶ G) : G.op ⟶ F.op :=
{ app := λ X, (α.app (unop X)).op,
naturality' := begin tidy, erw α.naturality, refl, end }
@[simp] lemma op_id (F : C ⥤ D) : nat_trans.op (𝟙 F) = 𝟙 (F.op) := rfl
/-- The "unopposite" of a natural transformation. -/
@[simps] protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ⟶ G) : G.unop ⟶ F.unop :=
{ app := λ X, (α.app (op X)).unop,
naturality' := begin tidy, erw α.naturality, refl, end }
@[simp] lemma unop_id (F : Cᵒᵖ ⥤ Dᵒᵖ) : nat_trans.unop (𝟙 F) = 𝟙 (F.unop) := rfl
/--
Given a natural transformation `α : F.op ⟶ G.op`,
we can take the "unopposite" of each component obtaining a natural transformation `G ⟶ F`.
-/
@[simps] protected def remove_op (α : F.op ⟶ G.op) : G ⟶ F :=
{ app := λ X, (α.app (op X)).unop,
naturality' :=
begin
intros X Y f,
have := congr_arg has_hom.hom.op (α.naturality f.op),
dsimp at this,
erw this,
refl,
end }
@[simp] lemma remove_op_id (F : C ⥤ D) : nat_trans.remove_op (𝟙 F.op) = 𝟙 F := rfl
end
section
variables {F G : C ⥤ Dᵒᵖ}
local attribute [semireducible] has_hom.opposite
/--
Given a natural transformation `α : F ⟶ G`, for `F G : C ⥤ Dᵒᵖ`,
taking `unop` of each component gives a natural transformation `G.left_op ⟶ F.left_op`.
-/
protected def left_op (α : F ⟶ G) : G.left_op ⟶ F.left_op :=
{ app := λ X, (α.app (unop X)).unop,
naturality' := begin tidy, erw α.naturality, refl, end }
@[simp] lemma left_op_app (α : F ⟶ G) (X) :
(nat_trans.left_op α).app X = (α.app (unop X)).unop :=
rfl
/--
Given a natural transformation `α : F.left_op ⟶ G.left_op`, for `F G : C ⥤ Dᵒᵖ`,
taking `op` of each component gives a natural transformation `G ⟶ F`.
-/
protected def remove_left_op (α : F.left_op ⟶ G.left_op) : G ⟶ F :=
{ app := λ X, (α.app (op X)).op,
naturality' :=
begin
intros X Y f,
have := congr_arg has_hom.hom.op (α.naturality f.op),
dsimp at this,
erw this
end }
@[simp] lemma remove_left_op_app (α : F.left_op ⟶ G.left_op) (X) :
(nat_trans.remove_left_op α).app X = (α.app (op X)).op :=
rfl
end
end nat_trans
namespace iso
variables {X Y : C}
/--
The opposite isomorphism.
-/
protected def op (α : X ≅ Y) : op Y ≅ op X :=
{ hom := α.hom.op,
inv := α.inv.op,
hom_inv_id' := has_hom.hom.unop_inj α.inv_hom_id,
inv_hom_id' := has_hom.hom.unop_inj α.hom_inv_id }
@[simp] lemma op_hom {α : X ≅ Y} : α.op.hom = α.hom.op := rfl
@[simp] lemma op_inv {α : X ≅ Y} : α.op.inv = α.inv.op := rfl
end iso
namespace nat_iso
variables {D : Type u₂} [category.{v₂} D]
variables {F G : C ⥤ D}
/-- The natural isomorphism between opposite functors `G.op ≅ F.op` induced by a natural
isomorphism between the original functors `F ≅ G`. -/
protected def op (α : F ≅ G) : G.op ≅ F.op :=
{ hom := nat_trans.op α.hom,
inv := nat_trans.op α.inv,
hom_inv_id' := begin ext, dsimp, rw ←op_comp, rw α.inv_hom_id_app, refl, end,
inv_hom_id' := begin ext, dsimp, rw ←op_comp, rw α.hom_inv_id_app, refl, end }
@[simp] lemma op_hom (α : F ≅ G) : (nat_iso.op α).hom = nat_trans.op α.hom := rfl
@[simp] lemma op_inv (α : F ≅ G) : (nat_iso.op α).inv = nat_trans.op α.inv := rfl
/-- The natural isomorphism between functors `G ≅ F` induced by a natural isomorphism
between the opposite functors `F.op ≅ G.op`. -/
protected def remove_op (α : F.op ≅ G.op) : G ≅ F :=
{ hom := nat_trans.remove_op α.hom,
inv := nat_trans.remove_op α.inv,
hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end,
inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end }
@[simp] lemma remove_op_hom (α : F.op ≅ G.op) :
(nat_iso.remove_op α).hom = nat_trans.remove_op α.hom := rfl
@[simp] lemma remove_op_inv (α : F.op ≅ G.op) :
(nat_iso.remove_op α).inv = nat_trans.remove_op α.inv := rfl
/-- The natural isomorphism between functors `G.unop ≅ F.unop` induced by a natural isomorphism
between the original functors `F ≅ G`. -/
protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ≅ G) : G.unop ≅ F.unop :=
{ hom := nat_trans.unop α.hom,
inv := nat_trans.unop α.inv,
hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end,
inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end }
@[simp] lemma unop_hom {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ≅ G) :
(nat_iso.unop α).hom = nat_trans.unop α.hom := rfl
@[simp] lemma unop_inv {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ≅ G) :
(nat_iso.unop α).inv = nat_trans.unop α.inv := rfl
end nat_iso
namespace equivalence
variables {D : Type u₂} [category.{v₂} D]
/--
An equivalence between categories gives an equivalence between the opposite categories.
-/
@[simps]
def op (e : C ≌ D) : Cᵒᵖ ≌ Dᵒᵖ :=
{ functor := e.functor.op,
inverse := e.inverse.op,
unit_iso := (nat_iso.op e.unit_iso).symm,
counit_iso := (nat_iso.op e.counit_iso).symm,
functor_unit_iso_comp' := λ X, by { apply has_hom.hom.unop_inj, dsimp, simp, }, }
/--
An equivalence between opposite categories gives an equivalence between the original categories.
-/
@[simps]
def unop (e : Cᵒᵖ ≌ Dᵒᵖ) : C ≌ D :=
{ functor := e.functor.unop,
inverse := e.inverse.unop,
unit_iso := (nat_iso.unop e.unit_iso).symm,
counit_iso := (nat_iso.unop e.counit_iso).symm,
functor_unit_iso_comp' := λ X, by { apply has_hom.hom.op_inj, dsimp, simp, }, }
end equivalence
/-- The equivalence between arrows of the form `A ⟶ B` and `B.unop ⟶ A.unop`. Useful for building
adjunctions.
Note that this (definitionally) gives variants
```
def op_equiv' (A : C) (B : Cᵒᵖ) : (opposite.op A ⟶ B) ≃ (B.unop ⟶ A) :=
op_equiv _ _
def op_equiv'' (A : Cᵒᵖ) (B : C) : (A ⟶ opposite.op B) ≃ (B ⟶ A.unop) :=
op_equiv _ _
def op_equiv''' (A B : C) : (opposite.op A ⟶ opposite.op B) ≃ (B ⟶ A) :=
op_equiv _ _
```
-/
def op_equiv (A B : Cᵒᵖ) : (A ⟶ B) ≃ (B.unop ⟶ A.unop) :=
{ to_fun := λ f, f.unop,
inv_fun := λ g, g.op,
left_inv := λ _, rfl,
right_inv := λ _, rfl }
-- These two are made by hand rather than by simps because simps generates
-- `(op_equiv _ _).to_fun f = ...` rather than the coercion version.
@[simp]
lemma op_equiv_apply (A B : Cᵒᵖ) (f : A ⟶ B) : op_equiv _ _ f = f.unop :=
rfl
@[simp]
lemma op_equiv_symm_apply (A B : Cᵒᵖ) (f : B.unop ⟶ A.unop) : (op_equiv _ _).symm f = f.op :=
rfl
instance subsingleton_of_unop (A B : Cᵒᵖ) [subsingleton (unop B ⟶ unop A)] : subsingleton (A ⟶ B) :=
(op_equiv A B).subsingleton
instance decidable_eq_of_unop (A B : Cᵒᵖ) [decidable_eq (unop B ⟶ unop A)] : decidable_eq (A ⟶ B) :=
(op_equiv A B).decidable_eq
universes v
variables {α : Type v} [preorder α]
/-- Construct a morphism in the opposite of a preorder category from an inequality. -/
def op_hom_of_le {U V : αᵒᵖ} (h : unop V ≤ unop U) : U ⟶ V :=
has_hom.hom.op (hom_of_le h)
lemma le_of_op_hom {U V : αᵒᵖ} (h : U ⟶ V) : unop V ≤ unop U :=
le_of_hom (h.unop)
end category_theory
|
554779a8be45f555097e89e0ea70b151ac8904f6 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/measure_theory/ae_measurable_sequence.lean | 1530f3782f4766df88c2ebbd16cd1db1e0bdf59f | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,452 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.measure_theory.measure_space
import Mathlib.PostPort
universes u_1 u_2 u_4
namespace Mathlib
/-!
# Sequence of measurable functions associated to a sequence of a.e.-measurable functions
We define here tools to prove statements about limits (infi, supr...) of sequences of
`ae_measurable` functions.
Given a sequence of a.e.-measurable functions `f : ι → α → β` with hypothesis
`hf : ∀ i, ae_measurable (f i) μ`, and a pointwise property `p : α → (ι → β) → Prop` such that we
have `hp : ∀ᵐ x ∂μ, p x (λ n, f n x)`, we define a sequence of measurable functions `ae_seq hf p`
and a measurable set `ae_seq_set hf p`, such that
* `μ (ae_seq_set hf p)ᶜ = 0`
* `x ∈ ae_seq_set hf p → ∀ i : ι, ae_seq hf hp i x = f i x`
* `x ∈ ae_seq_set hf p → p x (λ n, f n x)`
-/
/-- If we have the additional hypothesis `∀ᵐ x ∂μ, p x (λ n, f n x)`, this is a measurable set
whose complement has measure 0 such that for all `x ∈ ae_seq_set`, `f i x` is equal to
`(hf i).mk (f i) x` for all `i` and we have the pointwise property `p x (λ n, f n x)`. -/
def ae_seq_set {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} (hf : ∀ (i : ι), ae_measurable (f i)) (p : α → (ι → β) → Prop) : set α :=
measure_theory.to_measurable μ
((set_of fun (x : α) => (∀ (i : ι), f i x = ae_measurable.mk (f i) (hf i) x) ∧ p x fun (n : ι) => f n x)ᶜ)ᶜ
/-- A sequence of measurable functions that are equal to `f` and verify property `p` on the
measurable set `ae_seq_set hf p`. -/
def ae_seq {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} (hf : ∀ (i : ι), ae_measurable (f i)) (p : α → (ι → β) → Prop) : ι → α → β :=
fun (i : ι) (x : α) => ite (x ∈ ae_seq_set hf p) (ae_measurable.mk (f i) (hf i) x) (nonempty.some sorry)
namespace ae_seq
theorem mk_eq_fun_of_mem_ae_seq_set {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} (hf : ∀ (i : ι), ae_measurable (f i)) {x : α} (hx : x ∈ ae_seq_set hf p) (i : ι) : ae_measurable.mk (f i) (hf i) x = f i x := sorry
theorem ae_seq_eq_mk_of_mem_ae_seq_set {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} (hf : ∀ (i : ι), ae_measurable (f i)) {x : α} (hx : x ∈ ae_seq_set hf p) (i : ι) : ae_seq hf p i x = ae_measurable.mk (f i) (hf i) x := sorry
theorem ae_seq_eq_fun_of_mem_ae_seq_set {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} (hf : ∀ (i : ι), ae_measurable (f i)) {x : α} (hx : x ∈ ae_seq_set hf p) (i : ι) : ae_seq hf p i x = f i x := sorry
theorem prop_of_mem_ae_seq_set {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} (hf : ∀ (i : ι), ae_measurable (f i)) {x : α} (hx : x ∈ ae_seq_set hf p) : p x fun (n : ι) => ae_seq hf p n x := sorry
theorem fun_prop_of_mem_ae_seq_set {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} (hf : ∀ (i : ι), ae_measurable (f i)) {x : α} (hx : x ∈ ae_seq_set hf p) : p x fun (n : ι) => f n x := sorry
theorem ae_seq_set_is_measurable {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} {hf : ∀ (i : ι), ae_measurable (f i)} : is_measurable (ae_seq_set hf p) := sorry
theorem measurable {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} (hf : ∀ (i : ι), ae_measurable (f i)) (p : α → (ι → β) → Prop) (i : ι) : measurable (ae_seq hf p i) :=
measurable.ite ae_seq_set_is_measurable (ae_measurable.measurable_mk (hf i))
(dite (Nonempty α) (fun (hα : Nonempty α) => measurable_const)
fun (hα : ¬Nonempty α) => measurable_of_not_nonempty hα fun (x : α) => nonempty.some (_proof_1 i x))
theorem measure_compl_ae_seq_set_eq_zero {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} [encodable ι] (hf : ∀ (i : ι), ae_measurable (f i)) (hp : filter.eventually (fun (x : α) => p x fun (n : ι) => f n x) (measure_theory.measure.ae μ)) : coe_fn μ (ae_seq_set hf pᶜ) = 0 := sorry
theorem ae_seq_eq_mk_ae {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} [encodable ι] (hf : ∀ (i : ι), ae_measurable (f i)) (hp : filter.eventually (fun (x : α) => p x fun (n : ι) => f n x) (measure_theory.measure.ae μ)) : filter.eventually (fun (a : α) => ∀ (i : ι), ae_seq hf p i a = ae_measurable.mk (f i) (hf i) a)
(measure_theory.measure.ae μ) := sorry
theorem ae_seq_eq_fun_ae {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} [encodable ι] (hf : ∀ (i : ι), ae_measurable (f i)) (hp : filter.eventually (fun (x : α) => p x fun (n : ι) => f n x) (measure_theory.measure.ae μ)) : filter.eventually (fun (a : α) => ∀ (i : ι), ae_seq hf p i a = f i a) (measure_theory.measure.ae μ) :=
measure_theory.measure_mono_null
(fun (x : α) => mt fun (hx : x ∈ ae_seq_set hf p) (i : ι) => ae_seq_eq_fun_of_mem_ae_seq_set hf hx i)
(measure_compl_ae_seq_set_eq_zero hf hp)
theorem ae_seq_n_eq_fun_n_ae {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} [encodable ι] (hf : ∀ (i : ι), ae_measurable (f i)) (hp : filter.eventually (fun (x : α) => p x fun (n : ι) => f n x) (measure_theory.measure.ae μ)) (n : ι) : filter.eventually_eq (measure_theory.measure.ae μ) (ae_seq hf p n) (f n) :=
iff.mp measure_theory.ae_all_iff (ae_seq_eq_fun_ae hf hp) n
theorem supr {α : Type u_1} {β : Type u_2} {ι : Type u_4} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure_theory.measure α} {p : α → (ι → β) → Prop} [complete_lattice β] [encodable ι] (hf : ∀ (i : ι), ae_measurable (f i)) (hp : filter.eventually (fun (x : α) => p x fun (n : ι) => f n x) (measure_theory.measure.ae μ)) : filter.eventually_eq (measure_theory.measure.ae μ) (supr fun (n : ι) => ae_seq hf p n) (supr fun (i : ι) => f i) := sorry
|
4fc773095e5a9afe9fcc666e76aa0b1f01a99ac9 | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/closure1.lean | e9d20165a5ff46fcf971496358feca65c7db019c | [
"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 | 1,514 | lean | import Lean
new_frontend
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;
pure ()
#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;
pure ()
#eval tst2
|
3e55e057460897fd6b74c78b5c65c10341706859 | d7189ea2ef694124821b033e533f18905b5e87ef | /galois/network/network_monad.lean | f84d6135244354532be96a11b182e83b4a22dd27 | [
"Apache-2.0"
] | permissive | digama0/lean-protocol-support | eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59 | cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda | refs/heads/master | 1,625,421,450,627 | 1,506,035,462,000 | 1,506,035,462,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,866 | lean | /-
author: Ben Sherman
This file introduces a class `network_monad` that extends monad with
operations for communicating over sockets. These sockets can send and
receive arbitrary length byte sequences as messages. A socket can
either be specifically created by server given an IP
address and port, or created by an external agent when it sends a
connect message to a port on this server.
IP addresses and ports are modeled as byte sequences and 16-bit
numbers respectively, but can be generally treated as abstract
concepts.
-/
import galois.word
import galois.list.member
universes u v
namespace network
section network_monad
/-- This represents a point in time.
Our timing model assumes that all computation including sending
and receiving messages takes zero time. However, time can pass
when an agent is blocked waiting for a message. -/
@[reducible]
def time := ℕ
instance time_has_sub : has_sub time := begin unfold time, apply_instance end
-- This defines an IP address. To be agnostic over the protocol, we
-- just model this as a list of bytes
def ip := list byte
-- A port identifies a public interface to the machine that can
-- accept connections.
def port := word16
-- A remote machine name is given by an ip and port.
def remote_name := ip × port
-- This is the data type returned when trying to receive a message
-- with a timeout with the given list of sockets.
inductive poll_result {socket : Type} (ports : list port) (sockets : list socket) (bound : time) : Type
| timeout {} : poll_result
-- ^ We waited until the elapsed time without receiving a message.
| message {}
: ∀ (elapsed : fin bound) (sock : list.member sockets) (mess : list byte)
(elapsed_pos : 0 < elapsed.val), poll_result
-- ^ We received a message from the socket with the given index.
-- This defines the interface a monad needs to implement to give
-- a semantics to an agent.
class network_monad (m : Type → Type u) extends monad m : Type (u+1) :=
(socket : Type)
-- Create a socket to a machine with the given remote.
(connect : remote_name → m socket)
-- Send a message on the given socket.
-- This is asynchronous and returns immediately.
(send : socket → list byte → m punit)
-- Stop execution until an event in one of the ports or sockets.
(poll : Π (ports : list port) (sockets : list socket) (bound : time), m (poll_result ports sockets bound))
section
parameter {m : Type → Type u}
parameter [inst : network_monad m]
-- Send a message with the target destination.
def send (dest : inst.socket) (msg : list byte) :=
network_monad.send dest msg
-- Pause the process to receive messages until one arrives or a timeout occurs/
def poll (ports : list port) (sockets : list inst.socket) (bound : time)
: m (poll_result ports sockets bound) :=
network_monad.poll ports sockets bound
end
end network_monad
end network
|
97a6959a9f7e34f8da9e9d5088f2cffb84dc0d84 | 342425037bb9207ebcc87b62ab094d311b0787bb | /src/exercises/00_first_proofs.lean | 350335a5e31797c098130053e220677ce6970365 | [
"Apache-2.0"
] | permissive | Denglihua/tutorials | 6d4c219784f34acaa0bfcdfc388e980fd04b0cc2 | 6449169303de57d5f1799c25ac2a51af7062931d | refs/heads/master | 1,673,009,657,280 | 1,604,880,270,000 | 1,604,880,270,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 18,077 | lean | /-
This file is intended for Lean beginners. The goal is to demonstrate what it feels like to prove
things using Lean and mathlib. Complicated definitions and theory building are not covered.
Everything is covered again more slowly and with exercises in the next files.
-/
-- We want real numbers and their basic properties
import data.real.basic
-- We want to be able to use Lean's built-in "help" functionality
import tactic.suggest
-- We want to be able to define functions using the law of excluded middle
noncomputable theory
open_locale classical
/-
Our first goal is to define the set of upper bounds of a set of real numbers.
This is already defined in mathlib (in a more general context), but we repeat
it for the sake of exposition. Right-click "upper_bounds" below to get offered
to jump to mathlib's version
-/
#check upper_bounds
/-- The set of upper bounds of a set of real numbers ℝ -/
def up_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, a ≤ x}
/-- Predicate `is_max a A` means `a` is a maximum of `A` -/
def is_max (a : ℝ) (A : set ℝ) := a ∈ A ∧ a ∈ up_bounds A
/-
In the above definition, the symbol `∧` means "and". We also see the most
visible difference between set theoretic foundations and type theoretic ones
(used by almost all proof assistants). In set theory, everything is a set, and the
only relation you get from foundations are `=` and `∈`. In type theory, there is
a meta-theoretic relation of "typing": `a : ℝ` reads "`a` is a real number" or,
more precisely, "the type of `a` is `ℝ`". Here "meta-theoretic" means this is not a
statement you can prove or disprove inside the theory, it's a fact that is true or
not. Here we impose this fact, in other circumstances, it would be checked by the
Lean kernel.
By contrast, `a ∈ A` is a statement inside the theory. Here it's part of the
definition, in other circumstances it could be something proven inside Lean.
-/
/- For illustrative purposes, we now define an infix version of the above predicate.
It will allow us to write `a is_a_max_of A`, which is closer to a sentence.
-/
infix ` is_a_max_of `:55 := is_max
/-
Let's prove something now! A set of real numbers has at most one maximum. Here
everything left of the final `:` is introducing the objects and assumption. The equality
`x = y` right of the colon is the conclusion.
-/
lemma unique_max (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y :=
begin
-- We first break our assumptions in their two constituent pieces.
-- We are free to choose the name following `with`
cases hx with x_in x_up,
cases hy with y_in y_up,
-- Assumption `x_up` means x isn't less than elements of A, let's apply this to y
specialize x_up y,
-- Assumption `x_up` now needs the information that `y` is indeed in `A`.
specialize x_up y_in,
-- Let's do this quicker with roles swapped
specialize y_up x x_in,
-- We explained to Lean the idea of this proof.
-- Now we know `x ≤ y` and `y ≤ x`, and Lean shouldn't need more help.
-- `linarith` proves equalities and inequalities that follow linearly from
-- the assumption we have.
linarith,
end
/-
The above proof is too long, even if you remove comments. We don't really need the
unpacking steps at the beginning; we can access both parts of the assumption
`hx : x is_a_max_of A` using shortcuts `h.1` and `h.2`. We can also improve
readability without assistance from the tactic state display, clearly announcing
intermediate goals using `have`. This way we get to the following version of the
same proof.
-/
example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y :=
begin
have : x ≤ y, from hy.2 x hx.1,
have : y ≤ x, from hx.2 y hy.1,
linarith,
end
/-
Notice how mathematics based on type theory treats the assumption
`∀ a ∈ A, a ≤ y` as a function turning an element `a` of `A` into the statement
`a ≤ y`. More precisely, this assumption is the abbreviation of
`∀ a : ℝ, a ∈ A → a ≤ y`. The expression `hy.2 x` appearing in the above proof
is then the statement `x ∈ A → x ≤ y`, which itself is a function turning a
statement `x ∈ A` into `x ≤ y` so that the full expression `hy.2 x hx.1` is
indeed a proof of `x ≤ y`.
One could argue a three-line-long proof of this lemma is still two lines too long.
This is debatable, but mathlib's style is to write very short proofs for trivial
lemmas. Those proofs are not easy to read but they are meant to indicate that the
proof is probably not worth reading.
In order to reach this stage, we need to know what `linarith` did for us. It invoked
the lemma `le_antisymm` which says: `x ≤ y → y ≤ x → x = y`. This arrow, which
is used both for function and implication, is right associative. So the statement is
`x ≤ y → (y ≤ x → x = y)` which reads: I will send a proof `p` of `x ≤ y` to a function
sending a proof `q'` of `y ≤ x` to a proof of `x = y`. Hence `le_antisymm p q'` is a
proof of `x = y`.
Using this we can get our one-line proof:
-/
example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y :=
le_antisymm (hy.2 x hx.1) (hx.2 y hy.1)
/-
Such a proof is called a proof term (or a "term mode" proof). Notice it has no `begin`
and `end`. It is directly the kind of low level proof that the Lean kernel is
consuming. Commands like `cases`, `specialize` or `linarith` are called tactics, they
help users constructing proof terms that could be very tedious to write directly.
The most efficient proof style combines tactics with proof terms like our previous
`have : x ≤ y, from hy.2 x hx.1` where `hy.2 x hx.1` is a proof term embeded inside
a tactic mode proof.
In the remaining of this file, we'll be characterizing infima of sets of real numbers
in term of sequences.
-/
/-- The set of lower bounds of a set of real numbers ℝ -/
def low_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, x ≤ a}
/-
We now define `a` is an infimum of `A`. Again there is already a more general version
in mathlib.
-/
def is_inf (x : ℝ) (A : set ℝ) := x is_a_max_of (low_bounds A)
infix ` is_an_inf_of `:55 := is_inf
/-
We need to prove that any number which is greater than the infimum of A is greater
than some element of A.
-/
lemma inf_lt {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) :
∀ y, x < y → ∃ a ∈ A, a < y :=
begin
-- Let `y` be any real number.
intro y,
-- Let's prove the contrapositive
contrapose,
-- The symbol `¬` means negation. Let's ask Lean to rewrite the goal without negation,
-- pushing negation through quantifiers and inequalities
push_neg,
-- Let's assume the premise, calling the assumption `h`
intro h,
-- `h` is exactly saying `y` is a lower bound of `A` so the second part of
-- the infimum assumption `hx` applied to `y` and `h` is exactly what we want.
exact hx.2 y h
end
/-
In the above proof, the sequence `contrapose, push_neg` is so common that it can be
abbreviated to `contrapose!`. With these commands, we enter the gray zone between
proof checking and proof finding. Practical computer proof checking crucially needs
the computer to handle tedious proof steps. In the next proof, we'll start using
`linarith` a bit more seriously, going one step further into automation.
Our next real goal is to prove inequalities for limits of sequences. We extract the
following lemma: if `y ≤ x + ε` for all positive `ε` then `y ≤ x`.
-/
lemma le_of_le_add_eps {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x :=
begin
-- Let's prove the contrapositive, asking Lean to push negations right away.
contrapose!,
-- Assume `h : x < y`.
intro h,
-- We need to find `ε` such that `ε` is positive and `x + ε < y`.
-- Let's use `(y-x)/2`
use ((y-x)/2),
-- we now have two properties to prove. Let's do both in turn, using `linarith`
split,
linarith,
linarith,
end
/-
Note how `linarith` was used for both sub-goals at the end of the above proof.
We could have shortened that using the semi-colon combinator instead of comma,
writing `split ; linarith`.
Next we will study a compressed version of that proof:
-/
example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x :=
begin
contrapose!,
exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩,
end
/-
The angle brackets `⟨` and `⟩` introduce compound data or proofs. A proof
of a `∃ z, P z` statemement is composed of a witness `z₀` and a proof `h` of
`P z₀`. The compound is denoted by `⟨z₀, h⟩`. In the example above, the predicate is
itself compound, it is a conjunction `P z ∧ Q z`. So the proof term should read
`⟨z₀, ⟨h₁, h₂⟩⟩` where `h₁` (resp. `h₂`) is a proof of `P z₀` (resp. `Q z₀`).
But these so-called "anonymous constructor" brackets are right-associative, so we can
get rid of the nested brackets.
The keyword `by` introduces tactic mode inside term mode, it is a shorter version
of the `begin`/`end` pair, which is more convenient for single tactic blocks.
In this example, `begin` enters tactic mode, `exact` leaves it, `by` re-enters it.
Going all the way to a proof term would make the proof much longer, because we
crucially use automation with `contrapose!` and `linarith`. We can still get a one-line
proof using curly braces to gather several tactic invocations, and the `by` abbreviation
instead of `begin`/`end`:
-/
example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x :=
by { contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩ }
/-
One could argue that the above proof is a bit too terse, and we are relying too much
on linarith. Let's have more `linarith` calls for smaller steps. For the sake
of (tiny) variation, we will also assume the premise and argue by contradiction
instead of contraposing.
-/
example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x :=
begin
intro h,
-- Assume the conclusion is false, and call this assumption H.
by_contradiction H,
push_neg at H,
-- Now let's compute.
have key := calc
-- Each line must end with a colon followed by a proof term
-- We want to specialize our assumption `h` to `ε = (y-x)/2` but this is long to
-- type, so let's put a hole `_` that Lean will fill in by comparing the
-- statement we want to prove and our proof term with a hole. As usual,
-- positivity of `(y-x)/2` is proved by `linarith`
y ≤ x + (y-x)/2 : h _ (by linarith)
... = x/2 + y/2 : by ring
... < y : by linarith,
-- our key now says `y < y` (notice how the sequence `≤`, `=`, `<` was correctly
-- merged into a `<`). Let `linarith` find the desired contradiction now.
linarith,
-- alternatively, we could have provided the proof term
-- `exact lt_irrefl y key`
end
/-
Now we are ready for some analysis. Let's set up notation for absolute value
-/
local notation `|`x`|` := abs x
/-
And let's define convergence of sequences of real numbers (of course there is
a much more general definition in mathlib).
-/
/-- The sequence `u` tends to `l` -/
def limit (u : ℕ → ℝ) (l : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε
/-
In the above definition, `u n` denotes the n-th term of the sequence. We can
add parentheses to get `u(n)` but we try to avoid parentheses because they pile up
very quickly
-/
-- If y ≤ u n for all n and u n goes to x then y ≤ x
lemma le_lim {x y : ℝ} {u : ℕ → ℝ} (hu : limit u x) (ineq : ∀ n, y ≤ u n) : y ≤ x :=
begin
-- Let's apply our previous lemma
apply le_of_le_add_eps,
-- We need to prove y ≤ x + ε for all positive ε.
-- Let ε be any positive real
intros ε ε_pos,
-- we now specialize our limit assumption to this `ε`, and immediately
-- fix a `N` as promised by the definition.
cases hu ε ε_pos with N HN,
-- Now we only need to compute until reaching the conclusion
calc
y ≤ u N : ineq N
... = x + (u N - x) : by linarith
-- We'll need `add_le_add` which says `a ≤ b` and `c ≤ d` implies `a + c ≤ b + d`
-- We need a lemma saying `z ≤ |z|`. Because we don't know the name of this lemma,
-- let's use `library_search`. Because searching thourgh the library is slow,
-- Lean will write what it found in the Lean message window when cursor is on
-- that line, so that we can replace it by the lemma. We see `le_max_left` which
-- says `a ≤ max a b`. Actually there is a more specific lemma `le_abs_self`
... ≤ x + |u N - x| : add_le_add (by linarith) (by library_search)
... ≤ x + ε : add_le_add (by linarith) (HN N (by linarith)),
end
/-
The next lemma has been extracted from the main proof in order to discuss numbers.
In ordinary maths, we know that ℕ is *not* contained in `ℝ`, whatever the
construction of real numbers that we use. For instance a natural number is not
an equivalence class of Cauchy sequences. But it's very easy to
pretend otherwise. Formal maths requires slightly more care. In the statement below,
the "type ascription" `(n + 1 : ℝ)` forces Lean to convert the natural number
`n+1` into a real number. The "inclusion" map will be displayed in tactic state
as `↑`. There are various lemmas asserting this map is compatible with addition and
monotone, but we don't want to bother writing their names. The `norm_cast`
tactic is designed to wisely apply those lemmas for us.
-/
lemma inv_succ_pos : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 :=
begin
-- Let `n` be any integer
intro n,
-- Since we don't know the name of the relevant lemma, asserting that the inverse of
-- a positive number is positive, let's state that is suffices
-- to prove that `n+1`, seen as a real number, is positive, and ask `library_search`
suffices : (n + 1 : ℝ) > 0,
{ library_search },
-- Now we want to reduce to a statement about natural numbers, not real numbers
-- coming from natural numbers.
norm_cast,
-- and then get the usual help from `linarith`
linarith,
end
/-
That was a pretty long proof for an obvious fact. And stating it as a lemma feels
stupid, so let's find a way to write it on one line in case we want to include it
in some other proof without stating a lemma. First the `library_search` call
above displays the name of the relevant lemma: `one_div_pos`. We can also
replace the `linarith` call on the last line by `library_search` to learn the name
of the lemma `nat.succ_pos` asserting that the successor of a natural number is
positive. There is also a variant on `norm_cast` that combines it with `exact`.
The term mode analogue of `intro` is `λ`. We get down to:
-/
example : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 :=
λ n, one_div_pos.mpr (by exact_mod_cast nat.succ_pos n)
/-
The next proof uses mostly known things, so we will commment only new aspects.
-/
lemma limit_inv_succ : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1/(n + 1 : ℝ) ≤ ε :=
begin
intros ε ε_pos,
suffices : ∃ N : ℕ, 1/ε ≤ N,
{ -- Because we didn't provide a name for the above statement, Lean called it `this`.
-- Let's fix an `N` that works.
cases this with N HN,
use N,
intros n Hn,
-- Now we want to rewrite the goal using lemmas
-- `div_le_iff' : 0 < b → (a / b ≤ c ↔ a ≤ b * c)`
-- `div_le_iff : 0 < b → (a / b ≤ c ↔ a ≤ c * b)`
-- the second one will be rewritten from right to left, as indicated by `←`.
-- Lean will create a side goal for the required positivity assumption that
-- we don't provide for `div_le_iff'`.
rw [div_le_iff', ← div_le_iff ε_pos],
-- We want to replace assumption `Hn` by its real counter-part so that
-- linarith can find what it needs.
replace Hn : (N : ℝ) ≤ n, exact_mod_cast Hn,
linarith,
-- we are still left with the positivity assumption, but already discussed
-- how to prove it in the preceding lemma
exact_mod_cast nat.succ_pos n },
-- Now we need to prove that sufficient statement.
-- We want to use that `ℝ` is archimedean. So we start typing
-- `exact archimedean_` and hit Ctrl-space to see what completion Lean proposes
-- the lemma `archimedean_iff_nat_le` sounds promising. We select the left to
-- right implication using `.1`. This a generic lemma for fields equiped with
-- a linear (ie total) order. We need to provide a proof that `ℝ` is indeed
-- archimedean. This is done using the `apply_instance` tactic that will be
-- covered elsewhere.
exact archimedean_iff_nat_le.1 (by apply_instance) (1/ε),
end
/-
We can now put all pieces together, with almost no new things to explain.
-/
lemma inf_seq (A : set ℝ) (x : ℝ) :
(x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ ∃ u : ℕ → ℝ, limit u x ∧ ∀ n, u n ∈ A ) :=
begin
split,
{ intro h,
split,
{ exact h.1 },
-- On the next line, we don't need to tell Lean to treat `n+1` as a real number because
-- we add `x` to it, so Lean knows there is only one way to make sense of this expression.
have key : ∀ n : ℕ, ∃ a ∈ A, a < x + 1/(n+1),
{ intro n,
-- we can use the lemma we proved above
apply inf_lt h,
-- and another one we proved!
have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n,
linarith },
-- Now we need to use axiom of (countable) choice
choose u hu using key,
use u,
split,
{ intros ε ε_pos,
-- again we use a lemma we proved, specializing it to our fixed `ε`, and fixing a `N`
cases limit_inv_succ ε ε_pos with N H,
use N,
intros n hn,
have : x ≤ u n, from h.1 _ (hu n).1,
have := calc
u n < x + 1/(n + 1) : (hu n).2
... ≤ x + ε : add_le_add (le_refl x) (H n hn),
rw abs_of_nonneg ; linarith },
{ intro n,
exact (hu n).1 } },
{ intro h,
-- Assumption `h` is made of nested compound statements. We can use the
-- recursive version of `cases` to unpack it in one go.
rcases h with ⟨x_min, u, lim, huA⟩,
split,
exact x_min,
intros y y_mino,
apply le_lim lim,
intro n,
exact y_mino (u n) (huA n) },
end
|
2dbe0314b4578cb3db66d87162d70c37fdb7994c | ff5230333a701471f46c57e8c115a073ebaaa448 | /tests/lean/run/1805.lean | 24be7a5ab8799f3ab7971a660b4b2a33e47c43df | [
"Apache-2.0"
] | permissive | stanford-cs242/lean | f81721d2b5d00bc175f2e58c57b710d465e6c858 | 7bd861261f4a37326dcf8d7a17f1f1f330e4548c | refs/heads/master | 1,600,957,431,849 | 1,576,465,093,000 | 1,576,465,093,000 | 225,779,423 | 0 | 3 | Apache-2.0 | 1,575,433,936,000 | 1,575,433,935,000 | null | UTF-8 | Lean | false | false | 467 | lean | example (x y z x' y' z': ℕ) (h : (x, y, z) = (x', y', z')) : false :=
begin
injection h,
guard_hyp h_1 := x = x',
guard_hyp h_2 := (y, z) = (y', z'),
injection h_2,
guard_hyp h_3 := y = y',
guard_hyp h_4 := z = z',
admit
end
example (x y z x' y' z': ℕ) (h : (x, y, z) = (x', y', z')) : false :=
begin
injections,
guard_hyp h_1 := x = x',
guard_hyp h_2 := (y, z) = (y', z'),
guard_hyp h_3 := y = y',
guard_hyp h_4 := z = z',
admit
end
|
da4739d4612ab462a11c39950635a5de77b76c42 | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/field_theory/finite/polynomial.lean | ab7da8315ac222fa89cdb4024f184f55e3103d26 | [
"Apache-2.0"
] | permissive | troyjlee/mathlib | e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5 | 45e7eb8447555247246e3fe91c87066506c14875 | refs/heads/master | 1,689,248,035,046 | 1,629,470,528,000 | 1,629,470,528,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,681 | 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 field_theory.finite.basic
import field_theory.mv_polynomial
import data.mv_polynomial.expand
import linear_algebra.basic
import linear_algebra.finite_dimensional
/-!
## Polynomials over finite fields
-/
namespace mv_polynomial
variables {σ : Type*}
/-- A polynomial over the integers is divisible by `n : ℕ`
if and only if it is zero over `zmod n`. -/
lemma C_dvd_iff_zmod (n : ℕ) (φ : mv_polynomial σ ℤ) :
C (n:ℤ) ∣ φ ↔ map (int.cast_ring_hom (zmod n)) φ = 0 :=
C_dvd_iff_map_hom_eq_zero _ _ (char_p.int_cast_eq_zero_iff (zmod n) n) _
section frobenius
variables {p : ℕ} [fact p.prime]
lemma frobenius_zmod (f : mv_polynomial σ (zmod p)) :
frobenius _ p f = expand p f :=
begin
apply induction_on f,
{ intro a, rw [expand_C, frobenius_def, ← C_pow, zmod.pow_card], },
{ simp only [alg_hom.map_add, ring_hom.map_add], intros _ _ hf hg, rw [hf, hg] },
{ simp only [expand_X, ring_hom.map_mul, alg_hom.map_mul],
intros _ _ hf, rw [hf, frobenius_def], },
end
lemma expand_zmod (f : mv_polynomial σ (zmod p)) :
expand p f = f ^ p :=
(frobenius_zmod _).symm
end frobenius
end mv_polynomial
namespace mv_polynomial
noncomputable theory
open_locale big_operators classical
open set linear_map submodule
variables {K : Type*} {σ : Type*}
variables [field K] [fintype K] [fintype σ]
def indicator (a : σ → K) : mv_polynomial σ K :=
∏ n, (1 - (X n - C (a n))^(fintype.card K - 1))
lemma eval_indicator_apply_eq_one (a : σ → K) :
eval a (indicator a) = 1 :=
have 0 < fintype.card K - 1,
begin
rw [← finite_field.card_units, fintype.card_pos_iff],
exact ⟨1⟩
end,
by { simp only [indicator, (eval a).map_prod, ring_hom.map_sub,
(eval a).map_one, (eval a).map_pow, eval_X, eval_C,
sub_self, zero_pow this, sub_zero, finset.prod_const_one] }
lemma eval_indicator_apply_eq_zero (a b : σ → K) (h : a ≠ b) :
eval a (indicator b) = 0 :=
have ∃i, a i ≠ b i, by rwa [(≠), function.funext_iff, not_forall] at h,
begin
rcases this with ⟨i, hi⟩,
simp only [indicator, (eval a).map_prod, ring_hom.map_sub,
(eval a).map_one, (eval a).map_pow, eval_X, eval_C,
sub_self, finset.prod_eq_zero_iff],
refine ⟨i, finset.mem_univ _, _⟩,
rw [finite_field.pow_card_sub_one_eq_one, sub_self],
rwa [(≠), sub_eq_zero],
end
lemma degrees_indicator (c : σ → K) :
degrees (indicator c) ≤ ∑ s : σ, (fintype.card K - 1) • {s} :=
begin
rw [indicator],
refine le_trans (degrees_prod _ _) (finset.sum_le_sum $ assume s hs, _),
refine le_trans (degrees_sub _ _) _,
rw [degrees_one, ← bot_eq_zero, bot_sup_eq],
refine le_trans (degrees_pow _ _) (nsmul_le_nsmul_of_le_right _ _),
refine le_trans (degrees_sub _ _) _,
rw [degrees_C, ← bot_eq_zero, sup_bot_eq],
exact degrees_X' _
end
lemma indicator_mem_restrict_degree (c : σ → K) :
indicator c ∈ restrict_degree σ K (fintype.card K - 1) :=
begin
rw [mem_restrict_degree_iff_sup, indicator],
assume n,
refine le_trans (multiset.count_le_of_le _ $ degrees_indicator _) (le_of_eq _),
simp_rw [ ← multiset.coe_count_add_monoid_hom, (multiset.count_add_monoid_hom n).map_sum,
add_monoid_hom.map_nsmul, multiset.coe_count_add_monoid_hom,
multiset.singleton_eq_singleton, nsmul_eq_mul, nat.cast_id],
transitivity,
refine finset.sum_eq_single n _ _,
{ assume b hb ne, rw [multiset.count_cons_of_ne ne.symm, multiset.count_zero, mul_zero] },
{ assume h, exact (h $ finset.mem_univ _).elim },
{ rw [multiset.count_cons_self, multiset.count_zero, mul_one] }
end
section
variables (K σ)
def evalₗ : mv_polynomial σ K →ₗ[K] (σ → K) → K :=
{ to_fun := λ p e, eval e p,
map_add' := λ p q, by { ext x, rw ring_hom.map_add, refl, },
map_smul' := λ a p, by { ext e, rw [smul_eq_C_mul, ring_hom.map_mul, eval_C], refl } }
end
lemma evalₗ_apply (p : mv_polynomial σ K) (e : σ → K) : evalₗ K σ p e = eval e p :=
rfl
lemma map_restrict_dom_evalₗ : (restrict_degree σ K (fintype.card K - 1)).map (evalₗ K σ) = ⊤ :=
begin
refine top_unique (set_like.le_def.2 $ assume e _, mem_map.2 _),
refine ⟨∑ n : σ → K, e n • indicator n, _, _⟩,
{ exact sum_mem _ (assume c _, smul_mem _ _ (indicator_mem_restrict_degree _)) },
{ ext n,
simp only [linear_map.map_sum, @finset.sum_apply (σ → K) (λ_, K) _ _ _ _ _,
pi.smul_apply, linear_map.map_smul],
simp only [evalₗ_apply],
transitivity,
refine finset.sum_eq_single n _ _,
{ assume b _ h,
rw [eval_indicator_apply_eq_zero _ _ h.symm, smul_zero] },
{ assume h, exact (h $ finset.mem_univ n).elim },
{ rw [eval_indicator_apply_eq_one, smul_eq_mul, mul_one] } }
end
end mv_polynomial
namespace mv_polynomial
open_locale classical
open linear_map submodule
universe u
variables (σ : Type u) (K : Type u) [fintype σ] [field K] [fintype K]
@[derive [add_comm_group, module K, inhabited]]
def R : Type u := restrict_degree σ K (fintype.card K - 1)
noncomputable instance decidable_restrict_degree (m : ℕ) :
decidable_pred (∈ {n : σ →₀ ℕ | ∀i, n i ≤ m }) :=
by simp only [set.mem_set_of_eq]; apply_instance
lemma dim_R : module.rank K (R σ K) = fintype.card (σ → K) :=
calc module.rank K (R σ K) =
module.rank K (↥{s : σ →₀ ℕ | ∀ (n : σ), s n ≤ fintype.card K - 1} →₀ K) :
linear_equiv.dim_eq
(finsupp.supported_equiv_finsupp {s : σ →₀ ℕ | ∀n:σ, s n ≤ fintype.card K - 1 })
... = cardinal.mk {s : σ →₀ ℕ | ∀ (n : σ), s n ≤ fintype.card K - 1} :
by rw [finsupp.dim_eq, dim_of_ring, mul_one]
... = cardinal.mk {s : σ → ℕ | ∀ (n : σ), s n < fintype.card K } :
begin
refine quotient.sound ⟨equiv.subtype_equiv finsupp.equiv_fun_on_fintype $ assume f, _⟩,
refine forall_congr (assume n, nat.le_sub_right_iff_add_le _),
exact fintype.card_pos_iff.2 ⟨0⟩
end
... = cardinal.mk (σ → {n // n < fintype.card K}) :
quotient.sound ⟨@equiv.subtype_pi_equiv_pi σ (λ_, ℕ) (λs n, n < fintype.card K)⟩
... = cardinal.mk (σ → fin (fintype.card K)) :
quotient.sound ⟨equiv.arrow_congr (equiv.refl σ) (equiv.fin_equiv_subtype _).symm⟩
... = cardinal.mk (σ → K) :
quotient.sound ⟨equiv.arrow_congr (equiv.refl σ) (fintype.equiv_fin K).symm⟩
... = fintype.card (σ → K) : cardinal.fintype_card _
instance : finite_dimensional K (R σ K) :=
is_noetherian.iff_dim_lt_omega.mpr
(by simpa only [dim_R] using cardinal.nat_lt_omega (fintype.card (σ → K)))
lemma finrank_R : finite_dimensional.finrank K (R σ K) = fintype.card (σ → K) :=
finite_dimensional.finrank_eq_of_dim_eq (dim_R σ K)
def evalᵢ : R σ K →ₗ[K] (σ → K) → K :=
((evalₗ K σ).comp (restrict_degree σ K (fintype.card K - 1)).subtype)
lemma range_evalᵢ : (evalᵢ σ K).range = ⊤ :=
begin
rw [evalᵢ, linear_map.range_comp, range_subtype],
exact map_restrict_dom_evalₗ
end
lemma ker_evalₗ : (evalᵢ σ K).ker = ⊥ :=
begin
refine (ker_eq_bot_iff_range_eq_top_of_finrank_eq_finrank _).mpr (range_evalᵢ _ _),
rw [finite_dimensional.finrank_fintype_fun_eq_card, finrank_R]
end
lemma eq_zero_of_eval_eq_zero (p : mv_polynomial σ K)
(h : ∀v:σ → K, eval v p = 0) (hp : p ∈ restrict_degree σ K (fintype.card K - 1)) :
p = 0 :=
let p' : R σ K := ⟨p, hp⟩ in
have p' ∈ (evalᵢ σ K).ker := by { rw [mem_ker], ext v, exact h v },
show p'.1 = (0 : R σ K).1,
begin
rw [ker_evalₗ, mem_bot] at this,
rw [this]
end
end mv_polynomial
|
90bdd4692b62b3d3335eccd5a592684a7f03216a | 5412d79aa1dc0b521605c38bef9f0d4557b5a29d | /tests/lean/moduleOf.lean | fb8f8790065615d723db3fcba30683636cf9bd5e | [
"Apache-2.0"
] | permissive | smunix/lean4 | a450ec0927dc1c74816a1bf2818bf8600c9fc9bf | 3407202436c141e3243eafbecb4b8720599b970a | refs/heads/master | 1,676,334,875,188 | 1,610,128,510,000 | 1,610,128,521,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 415 | lean | import Lean
def f (x : Nat) := x + x
open Lean
def tst : MetaM Unit := do
IO.println (← getModuleOf `HAdd.hAdd)
IO.println (← getModuleOf `Lean.Core.CoreM)
IO.println (← getModuleOf `Lean.Elab.Term.elabTerm)
IO.println (← getModuleOf `Std.HashMap.insert)
IO.println (← getModuleOf `tst)
IO.println (← getModuleOf `f)
IO.println (← getModuleOf `foo) -- Error: unknown 'foo'
#eval tst
|
8571266a491ab71724be7b55f7160161dcb98717 | 6b7c9c6393bac7cb1c64582a1c62597e24f5bb80 | /src/example.lean | 47e65fcda58258d21ccd35d29aaa6282cc9ccfb4 | [
"Apache-2.0"
] | permissive | alreadydone/lean-gptf | 56a7d9cbd9400af72fb143d60c8774b8cfbc09cb | b4ab1eb2da0178f3dcdc49771d9fed6b50e35d98 | refs/heads/master | 1,679,371,993,063 | 1,614,479,778,000 | 1,614,479,778,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,695 | lean | import tactic.gptf
import data.list.sigma
section gptf
example {α} (a : α) : a = a :=
begin
refl
end
example : ∃ n : ℕ, 8 = 2*n :=
begin
exact ⟨4, rfl⟩
end
example {P Q R : Prop} : P → (P → R) → R :=
begin
intro h1, exact λ h2, h2 h1
end
example {p q r : Prop} (h₁ : p) (h₂ : q) : (p ∧ q) ∨ r :=
begin
exact or.inl ⟨h₁, h₂⟩
end
example {P Q : Prop} : (¬ P) ∧ (¬ Q) → ¬ (P ∨ Q) :=
begin
exact not_or_distrib.mpr -- `gptf {pfx := "exact"}`
end
example {P Q R : Prop} : (P ∧ Q) → ((P → R) → ¬ (Q → ¬ R)) :=
begin
rintros ⟨h₁, h₂⟩ h₃, try {exact λ h, h₁ _ h}, rw [imp_not_comm],
apply not_imp_not.mpr (λ con, _), exact id, apply con, apply h₃,
apply h₁, exact h₂
end
example (n : ℕ) (m : ℕ) : nat.succ n < nat.succ n + 1 :=
begin
{[smt] eblast_using [nat.add_one], exact nat.lt_succ_self _}
end
example : ∀ (F1 F2 F3 : Prop), ((¬F1 ∧ F3) ∨ (F2 ∧ ¬F3)) → (F2 → F1) → (F2 → F3) → ¬F2 :=
begin
intros P Q R H₁ H₂ H₃ H₄,
apply H₁.elim, -- `gptf {pfx := "apply"}`
{ assume h, simp * at * }, -- `gptf`
cc -- `gptf`
end
example : ∀ (f : nat → Prop), f 2 → ∃ x, f x :=
begin
exact λ f hf, ⟨_, hf⟩ -- by `gptf {pfx := "exact"}` :D
end
example {G : Type} [group G] (x y z : G) : (x * z) * (z⁻¹ * y) = x * y :=
begin
simp [mul_assoc]
end
universes u v
example {α : Type u} {β : α → Type v} [_inst_1 : decidable_eq α] {a : α} {l₁ l₂ : list (sigma β)} :
(list.kerase a l₁).kunion (list.kerase a l₂) = list.kerase a (l₁.kunion l₂) :=
begin
induction l₁ generalizing l₂, case list.nil { refl }, simp
end
end gptf
|
17b21b63da022513adc8324ec463108a17c0f438 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/order/filter/archimedean.lean | 1734c6a0a5833d7e532d2da334ec4217afeb9e66 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 9,765 | 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, Yury Kudryashov
-/
import algebra.order.archimedean
import order.filter.at_top_bot
/-!
# `at_top` filter and archimedean (semi)rings/fields
In this file we prove that for a linear ordered archimedean semiring `R` and a function `f : α → ℕ`,
the function `coe ∘ f : α → R` tends to `at_top` along a filter `l` if and only if so does `f`.
We also prove that `coe : ℕ → R` tends to `at_top` along `at_top`, as well as version of these
two results for `ℤ` (and a ring `R`) and `ℚ` (and a field `R`).
-/
variables {α R : Type*}
open filter set
@[simp] lemma nat.comap_coe_at_top [strict_ordered_semiring R] [archimedean R] :
comap (coe : ℕ → R) at_top = at_top :=
comap_embedding_at_top (λ _ _, nat.cast_le) exists_nat_ge
lemma tendsto_coe_nat_at_top_iff [strict_ordered_semiring R] [archimedean R]
{f : α → ℕ} {l : filter α} :
tendsto (λ n, (f n : R)) l at_top ↔ tendsto f l at_top :=
tendsto_at_top_embedding (assume a₁ a₂, nat.cast_le) exists_nat_ge
lemma tendsto_coe_nat_at_top_at_top [strict_ordered_semiring R] [archimedean R] :
tendsto (coe : ℕ → R) at_top at_top :=
nat.mono_cast.tendsto_at_top_at_top exists_nat_ge
@[simp] lemma int.comap_coe_at_top [strict_ordered_ring R] [archimedean R] :
comap (coe : ℤ → R) at_top = at_top :=
comap_embedding_at_top (λ _ _, int.cast_le) $ λ r,
let ⟨n, hn⟩ := exists_nat_ge r in ⟨n, by exact_mod_cast hn⟩
@[simp] lemma int.comap_coe_at_bot [strict_ordered_ring R] [archimedean R] :
comap (coe : ℤ → R) at_bot = at_bot :=
comap_embedding_at_bot (λ _ _, int.cast_le) $ λ r,
let ⟨n, hn⟩ := exists_nat_ge (-r) in ⟨-n, by simpa [neg_le] using hn⟩
lemma tendsto_coe_int_at_top_iff [strict_ordered_ring R] [archimedean R]
{f : α → ℤ} {l : filter α} :
tendsto (λ n, (f n : R)) l at_top ↔ tendsto f l at_top :=
by rw [← tendsto_comap_iff, int.comap_coe_at_top]
lemma tendsto_coe_int_at_bot_iff [strict_ordered_ring R] [archimedean R]
{f : α → ℤ} {l : filter α} :
tendsto (λ n, (f n : R)) l at_bot ↔ tendsto f l at_bot :=
by rw [← tendsto_comap_iff, int.comap_coe_at_bot]
lemma tendsto_coe_int_at_top_at_top [strict_ordered_ring R] [archimedean R] :
tendsto (coe : ℤ → R) at_top at_top :=
int.cast_mono.tendsto_at_top_at_top $ λ b,
let ⟨n, hn⟩ := exists_nat_ge b in ⟨n, by exact_mod_cast hn⟩
@[simp] lemma rat.comap_coe_at_top [linear_ordered_field R] [archimedean R] :
comap (coe : ℚ → R) at_top = at_top :=
comap_embedding_at_top (λ _ _, rat.cast_le) $ λ r, let ⟨n, hn⟩ := exists_nat_ge r in ⟨n, by simpa⟩
@[simp] lemma rat.comap_coe_at_bot [linear_ordered_field R] [archimedean R] :
comap (coe : ℚ → R) at_bot = at_bot :=
comap_embedding_at_bot (λ _ _, rat.cast_le) $ λ r, let ⟨n, hn⟩ := exists_nat_ge (-r)
in ⟨-n, by simpa [neg_le]⟩
lemma tendsto_coe_rat_at_top_iff [linear_ordered_field R] [archimedean R]
{f : α → ℚ} {l : filter α} :
tendsto (λ n, (f n : R)) l at_top ↔ tendsto f l at_top :=
by rw [← tendsto_comap_iff, rat.comap_coe_at_top]
lemma tendsto_coe_rat_at_bot_iff [linear_ordered_field R] [archimedean R]
{f : α → ℚ} {l : filter α} :
tendsto (λ n, (f n : R)) l at_bot ↔ tendsto f l at_bot :=
by rw [← tendsto_comap_iff, rat.comap_coe_at_bot]
lemma at_top_countable_basis_of_archimedean [linear_ordered_semiring R] [archimedean R] :
(at_top : filter R).has_countable_basis (λ n : ℕ, true) (λ n, Ici n) :=
{ countable := to_countable _,
to_has_basis := at_top_basis.to_has_basis
(λ x hx, let ⟨n, hn⟩ := exists_nat_ge x in ⟨n, trivial, Ici_subset_Ici.2 hn⟩)
(λ n hn, ⟨n, trivial, subset.rfl⟩) }
lemma at_bot_countable_basis_of_archimedean [linear_ordered_ring R] [archimedean R] :
(at_bot : filter R).has_countable_basis (λ m : ℤ, true) (λ m, Iic m) :=
{ countable := to_countable _,
to_has_basis := at_bot_basis.to_has_basis
(λ x hx, let ⟨m, hm⟩ := exists_int_lt x in ⟨m, trivial, Iic_subset_Iic.2 hm.le⟩)
(λ m hm, ⟨m, trivial, subset.rfl⟩) }
@[priority 100]
instance at_top_countably_generated_of_archimedean [linear_ordered_semiring R] [archimedean R] :
(at_top : filter R).is_countably_generated :=
at_top_countable_basis_of_archimedean.is_countably_generated
@[priority 100]
instance at_bot_countably_generated_of_archimedean [linear_ordered_ring R] [archimedean R] :
(at_bot : filter R).is_countably_generated :=
at_bot_countable_basis_of_archimedean.is_countably_generated
namespace filter
variables {l : filter α} {f : α → R} {r : R}
section linear_ordered_semiring
variables [linear_ordered_semiring R] [archimedean R]
/-- If a function tends to infinity along a filter, then this function multiplied by a positive
constant (on the left) also tends to infinity. The archimedean assumption is convenient to get a
statement that works on `ℕ`, `ℤ` and `ℝ`, although not necessary (a version in ordered fields is
given in `filter.tendsto.const_mul_at_top`). -/
lemma tendsto.const_mul_at_top' (hr : 0 < r) (hf : tendsto f l at_top) :
tendsto (λx, r * f x) l at_top :=
begin
apply tendsto_at_top.2 (λb, _),
obtain ⟨n : ℕ, hn : 1 ≤ n • r⟩ := archimedean.arch 1 hr,
rw nsmul_eq_mul' at hn,
filter_upwards [tendsto_at_top.1 hf (n * max b 0)] with x hx,
calc b ≤ 1 * max b 0 : by { rw [one_mul], exact le_max_left _ _ }
... ≤ (r * n) * max b 0 : mul_le_mul_of_nonneg_right hn (le_max_right _ _)
... = r * (n * max b 0) : by rw [mul_assoc]
... ≤ r * f x : mul_le_mul_of_nonneg_left hx (le_of_lt hr)
end
/-- If a function tends to infinity along a filter, then this function multiplied by a positive
constant (on the right) also tends to infinity. The archimedean assumption is convenient to get a
statement that works on `ℕ`, `ℤ` and `ℝ`, although not necessary (a version in ordered fields is
given in `filter.tendsto.at_top_mul_const`). -/
lemma tendsto.at_top_mul_const' (hr : 0 < r) (hf : tendsto f l at_top) :
tendsto (λx, f x * r) l at_top :=
begin
apply tendsto_at_top.2 (λb, _),
obtain ⟨n : ℕ, hn : 1 ≤ n • r⟩ := archimedean.arch 1 hr,
have hn' : 1 ≤ (n : R) * r, by rwa nsmul_eq_mul at hn,
filter_upwards [tendsto_at_top.1 hf (max b 0 * n)] with x hx,
calc b ≤ max b 0 * 1 : by { rw [mul_one], exact le_max_left _ _ }
... ≤ max b 0 * (n * r) : mul_le_mul_of_nonneg_left hn' (le_max_right _ _)
... = (max b 0 * n) * r : by rw [mul_assoc]
... ≤ f x * r : mul_le_mul_of_nonneg_right hx (le_of_lt hr)
end
end linear_ordered_semiring
section linear_ordered_ring
variables [linear_ordered_ring R] [archimedean R]
/-- See also `filter.tendsto.at_top_mul_neg_const` for a version of this lemma for
`linear_ordered_field`s which does not require the `archimedean` assumption. -/
lemma tendsto.at_top_mul_neg_const' (hr : r < 0) (hf : tendsto f l at_top) :
tendsto (λx, f x * r) l at_bot :=
by simpa only [tendsto_neg_at_top_iff, mul_neg] using hf.at_top_mul_const' (neg_pos.mpr hr)
/-- See also `filter.tendsto.at_bot_mul_const` for a version of this lemma for
`linear_ordered_field`s which does not require the `archimedean` assumption. -/
lemma tendsto.at_bot_mul_const' (hr : 0 < r) (hf : tendsto f l at_bot) :
tendsto (λx, f x * r) l at_bot :=
begin
simp only [← tendsto_neg_at_top_iff, ← neg_mul] at hf ⊢,
exact hf.at_top_mul_const' hr
end
/-- See also `filter.tendsto.at_bot_mul_neg_const` for a version of this lemma for
`linear_ordered_field`s which does not require the `archimedean` assumption. -/
lemma tendsto.at_bot_mul_neg_const' (hr : r < 0) (hf : tendsto f l at_bot) :
tendsto (λx, f x * r) l at_top :=
by simpa only [mul_neg, tendsto_neg_at_bot_iff] using hf.at_bot_mul_const' (neg_pos.2 hr)
end linear_ordered_ring
section linear_ordered_cancel_add_comm_monoid
variables [linear_ordered_cancel_add_comm_monoid R] [archimedean R]
lemma tendsto.at_top_nsmul_const {f : α → ℕ} (hr : 0 < r) (hf : tendsto f l at_top) :
tendsto (λ x, f x • r) l at_top :=
begin
refine tendsto_at_top.mpr (λ s, _),
obtain ⟨n : ℕ, hn : s ≤ n • r⟩ := archimedean.arch s hr,
exact (tendsto_at_top.mp hf n).mono (λ a ha, hn.trans (nsmul_le_nsmul hr.le ha)),
end
end linear_ordered_cancel_add_comm_monoid
section linear_ordered_add_comm_group
variables [linear_ordered_add_comm_group R] [archimedean R]
lemma tendsto.at_top_nsmul_neg_const {f : α → ℕ} (hr : r < 0) (hf : tendsto f l at_top) :
tendsto (λ x, f x • r) l at_bot :=
by simpa using hf.at_top_nsmul_const (neg_pos.2 hr)
lemma tendsto.at_top_zsmul_const {f : α → ℤ} (hr : 0 < r) (hf : tendsto f l at_top) :
tendsto (λ x, f x • r) l at_top :=
begin
refine tendsto_at_top.mpr (λ s, _),
obtain ⟨n : ℕ, hn : s ≤ n • r⟩ := archimedean.arch s hr,
replace hn : s ≤ (n : ℤ) • r, { simpa, },
exact (tendsto_at_top.mp hf n).mono (λ a ha, hn.trans (zsmul_le_zsmul hr.le ha)),
end
lemma tendsto.at_top_zsmul_neg_const {f : α → ℤ} (hr : r < 0) (hf : tendsto f l at_top) :
tendsto (λ x, f x • r) l at_bot :=
by simpa using hf.at_top_zsmul_const (neg_pos.2 hr)
lemma tendsto.at_bot_zsmul_const {f : α → ℤ} (hr : 0 < r) (hf : tendsto f l at_bot) :
tendsto (λ x, f x • r) l at_bot :=
begin
simp only [← tendsto_neg_at_top_iff, ← neg_zsmul] at hf ⊢,
exact hf.at_top_zsmul_const hr
end
lemma tendsto.at_bot_zsmul_neg_const {f : α → ℤ} (hr : r < 0) (hf : tendsto f l at_bot) :
tendsto (λ x, f x • r) l at_top :=
by simpa using hf.at_bot_zsmul_const (neg_pos.2 hr)
end linear_ordered_add_comm_group
end filter
|
16ca16e4539d8cd6b356d82d4c17faf9c3e02472 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/tactic/omega/nat/main.lean | 543e30cbed430b9d87336b0d4ee55aa01c827614 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 8,986 | lean | /-
Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Seul Baek
-/
/-
Main procedure for linear natural number arithmetic.
-/
import tactic.omega.prove_unsats
import tactic.omega.nat.dnf
import tactic.omega.nat.neg_elim
import tactic.omega.nat.sub_elim
open tactic
namespace omega
namespace nat
open_locale omega.nat
run_cmd mk_simp_attr `sugar_nat
attribute [sugar_nat]
ne not_le not_lt
nat.lt_iff_add_one_le
nat.succ_eq_add_one
or_false false_or
and_true true_and
ge gt mul_add add_mul mul_comm
one_mul mul_one
imp_iff_not_or
iff_iff_not_or_and_or_not
meta def desugar := `[try {simp only with sugar_nat at *}]
lemma univ_close_of_unsat_neg_elim_not (m) (p : preform) :
(neg_elim (¬* p)).unsat → univ_close p (λ _, 0) m :=
begin
intro h1, apply univ_close_of_valid,
apply valid_of_unsat_not, intro h2, apply h1,
apply preform.sat_of_implies_of_sat implies_neg_elim h2,
end
/-- Return expr of proof that argument is free of subtractions -/
meta def preterm.prove_sub_free : preterm → tactic expr
| (& m) := return `(trivial)
| (m ** n) := return `(trivial)
| (t +* s) :=
do x ← preterm.prove_sub_free t,
y ← preterm.prove_sub_free s,
return `(@and.intro (preterm.sub_free %%`(t))
(preterm.sub_free %%`(s)) %%x %%y)
| (_ -* _) := failed
/-- Return expr of proof that argument is free of negations -/
meta def prove_neg_free : preform → tactic expr
| (t =* s) := return `(trivial)
| (t ≤* s) := return `(trivial)
| (p ∨* q) :=
do x ← prove_neg_free p,
y ← prove_neg_free q,
return `(@and.intro (preform.neg_free %%`(p))
(preform.neg_free %%`(q)) %%x %%y)
| (p ∧* q) :=
do x ← prove_neg_free p,
y ← prove_neg_free q,
return `(@and.intro (preform.neg_free %%`(p))
(preform.neg_free %%`(q)) %%x %%y)
| _ := failed
/-- Return expr of proof that argument is free of subtractions -/
meta def prove_sub_free : preform → tactic expr
| (t =* s) :=
do x ← preterm.prove_sub_free t,
y ← preterm.prove_sub_free s,
return `(@and.intro (preterm.sub_free %%`(t))
(preterm.sub_free %%`(s)) %%x %%y)
| (t ≤* s) :=
do x ← preterm.prove_sub_free t,
y ← preterm.prove_sub_free s,
return `(@and.intro (preterm.sub_free %%`(t))
(preterm.sub_free %%`(s)) %%x %%y)
| (¬*p) := prove_sub_free p
| (p ∨* q) :=
do x ← prove_sub_free p,
y ← prove_sub_free q,
return `(@and.intro (preform.sub_free %%`(p))
(preform.sub_free %%`(q)) %%x %%y)
| (p ∧* q) :=
do x ← prove_sub_free p,
y ← prove_sub_free q,
return `(@and.intro (preform.sub_free %%`(p))
(preform.sub_free %%`(q)) %%x %%y)
/-- Given a p : preform, return the expr of a term t : p.unsat, where p is subtraction- and
negation-free. -/
meta def prove_unsat_sub_free (p : preform) : tactic expr :=
do x ← prove_neg_free p,
y ← prove_sub_free p,
z ← prove_unsats (dnf p),
return `(unsat_of_unsat_dnf %%`(p) %%x %%y %%z)
/-- Given a p : preform, return the expr of a term t : p.unsat, where p is negation-free. -/
meta def prove_unsat_neg_free : preform → tactic expr | p :=
match p.sub_terms with
| none := prove_unsat_sub_free p
| (some (t,s)) :=
do x ← prove_unsat_neg_free (sub_elim t s p),
return `(unsat_of_unsat_sub_elim %%`(t) %%`(s) %%`(p) %%x)
end
/-- Given a (m : nat) and (p : preform), return the expr of (t : univ_close m p). -/
meta def prove_univ_close (m : nat) (p : preform) : tactic expr :=
do x ← prove_unsat_neg_free (neg_elim (¬*p)),
to_expr ``(univ_close_of_unsat_neg_elim_not %%`(m) %%`(p) %%x)
/-- Reification to imtermediate shadow syntax that retains exprs -/
meta def to_exprterm : expr → tactic exprterm
| `(%%x * %%y) :=
do m ← eval_expr' nat y,
return (exprterm.exp m x)
| `(%%t1x + %%t2x) :=
do t1 ← to_exprterm t1x,
t2 ← to_exprterm t2x,
return (exprterm.add t1 t2)
| `(%%t1x - %%t2x) :=
do t1 ← to_exprterm t1x,
t2 ← to_exprterm t2x,
return (exprterm.sub t1 t2)
| x :=
( do m ← eval_expr' nat x,
return (exprterm.cst m) ) <|>
( return $ exprterm.exp 1 x )
/-- Reification to imtermediate shadow syntax that retains exprs -/
meta def to_exprform : expr → tactic exprform
| `(%%tx1 = %%tx2) :=
do t1 ← to_exprterm tx1,
t2 ← to_exprterm tx2,
return (exprform.eq t1 t2)
| `(%%tx1 ≤ %%tx2) :=
do t1 ← to_exprterm tx1,
t2 ← to_exprterm tx2,
return (exprform.le t1 t2)
| `(¬ %%px) := do p ← to_exprform px, return (exprform.not p)
| `(%%px ∨ %%qx) :=
do p ← to_exprform px,
q ← to_exprform qx,
return (exprform.or p q)
| `(%%px ∧ %%qx) :=
do p ← to_exprform px,
q ← to_exprform qx,
return (exprform.and p q)
| `(_ → %%px) := to_exprform px
| x := trace "Cannot reify expr : " >> trace x >> failed
/-- List of all unreified exprs -/
meta def exprterm.exprs : exprterm → list expr
| (exprterm.cst _) := []
| (exprterm.exp _ x) := [x]
| (exprterm.add t s) := list.union t.exprs s.exprs
| (exprterm.sub t s) := list.union t.exprs s.exprs
/-- List of all unreified exprs -/
meta def exprform.exprs : exprform → list expr
| (exprform.eq t s) := list.union t.exprs s.exprs
| (exprform.le t s) := list.union t.exprs s.exprs
| (exprform.not p) := p.exprs
| (exprform.or p q) := list.union p.exprs q.exprs
| (exprform.and p q) := list.union p.exprs q.exprs
/-- Reification to an intermediate shadow syntax which eliminates exprs,
but still includes non-canonical terms -/
meta def exprterm.to_preterm (xs : list expr) : exprterm → tactic preterm
| (exprterm.cst k) := return & k
| (exprterm.exp k x) :=
let m := xs.index_of x in
if m < xs.length
then return (k ** m)
else failed
| (exprterm.add xa xb) :=
do a ← xa.to_preterm,
b ← xb.to_preterm,
return (a +* b)
| (exprterm.sub xa xb) :=
do a ← xa.to_preterm,
b ← xb.to_preterm,
return (a -* b)
/-- Reification to an intermediate shadow syntax which eliminates exprs,
but still includes non-canonical terms -/
meta def exprform.to_preform (xs : list expr) : exprform → tactic preform
| (exprform.eq xa xb) :=
do a ← xa.to_preterm xs,
b ← xb.to_preterm xs,
return (a =* b)
| (exprform.le xa xb) :=
do a ← xa.to_preterm xs,
b ← xb.to_preterm xs,
return (a ≤* b)
| (exprform.not xp) :=
do p ← xp.to_preform,
return ¬* p
| (exprform.or xp xq) :=
do p ← xp.to_preform,
q ← xq.to_preform,
return (p ∨* q)
| (exprform.and xp xq) :=
do p ← xp.to_preform,
q ← xq.to_preform,
return (p ∧* q)
/-- Reification to an intermediate shadow syntax which eliminates exprs,
but still includes non-canonical terms. -/
meta def to_preform (x : expr) : tactic (preform × nat) :=
do xf ← to_exprform x,
let xs := xf.exprs,
f ← xf.to_preform xs,
return (f, xs.length)
/-- Return expr of proof of current LNA goal -/
meta def prove : tactic expr :=
do (p,m) ← target >>= to_preform,
trace_if_enabled `omega p,
prove_univ_close m p
/-- Succeed iff argument is expr of ℕ -/
meta def eq_nat (x : expr) : tactic unit :=
if x = `(nat) then skip else failed
/-- Check whether argument is expr of a well-formed formula of LNA-/
meta def wff : expr → tactic unit
| `(¬ %%px) := wff px
| `(%%px ∨ %%qx) := wff px >> wff qx
| `(%%px ∧ %%qx) := wff px >> wff qx
| `(%%px ↔ %%qx) := wff px >> wff qx
| `(%%(expr.pi _ _ px qx)) :=
monad.cond
(if expr.has_var px then return tt else is_prop px)
(wff px >> wff qx)
(eq_nat px >> wff qx)
| `(@has_lt.lt %%dx %%h _ _) := eq_nat dx
| `(@has_le.le %%dx %%h _ _) := eq_nat dx
| `(@eq %%dx _ _) := eq_nat dx
| `(@ge %%dx %%h _ _) := eq_nat dx
| `(@gt %%dx %%h _ _) := eq_nat dx
| `(@ne %%dx _ _) := eq_nat dx
| `(true) := skip
| `(false) := skip
| _ := failed
/-- Succeed iff argument is expr of term whose type is wff -/
meta def wfx (x : expr) : tactic unit :=
infer_type x >>= wff
/-- Intro all universal quantifiers over nat -/
meta def intro_nats_core : tactic unit :=
do x ← target,
match x with
| (expr.pi _ _ `(nat) _) := intro_fresh >> intro_nats_core
| _ := skip
end
meta def intro_nats : tactic unit :=
do (expr.pi _ _ `(nat) _) ← target,
intro_nats_core
/-- If the goal has universal quantifiers over natural, introduce all of them.
Otherwise, revert all hypotheses that are formulas of linear natural number arithmetic. -/
meta def preprocess : tactic unit :=
intro_nats <|> (revert_cond_all wfx >> desugar)
end nat
end omega
open omega.nat
/-- The core omega tactic for natural numbers. -/
meta def omega_nat (is_manual : bool) : tactic unit :=
desugar ; (if is_manual then skip else preprocess) ; prove >>= apply >> skip
|
56e03ece819ab08ed6d2f8fb0f4a3049811623dc | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/logic/function/conjugate_auto.lean | 714141d971ba6f40b5d81947dc25abf8fb7e38ac | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,626 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.logic.function.basic
import Mathlib.PostPort
universes u_1 u_2 u_3
namespace Mathlib
/-!
# Semiconjugate and commuting maps
We define the following predicates:
* `function.semiconj`: `f : α → β` semiconjugates `ga : α → α` to `gb : β → β` if `f ∘ ga = gb ∘ f`;
* `function.semiconj₂: `f : α → β` semiconjugates a binary operation `ga : α → α → α`
to `gb : β → β → β` if `f (ga x y) = gb (f x) (f y)`;
* `f : α → α` commutes with `g : α → α` if `f ∘ g = g ∘ f`, or equivalently `semiconj f g g`.
-/
namespace function
/-- We say that `f : α → β` semiconjugates `ga : α → α` to `gb : β → β` if `f ∘ ga = gb ∘ f`. -/
def semiconj {α : Type u_1} {β : Type u_2} (f : α → β) (ga : α → α) (gb : β → β) :=
∀ (x : α), f (ga x) = gb (f x)
namespace semiconj
protected theorem comp_eq {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α} {gb : β → β}
(h : semiconj f ga gb) : f ∘ ga = gb ∘ f :=
funext h
protected theorem eq {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α} {gb : β → β}
(h : semiconj f ga gb) (x : α) : f (ga x) = gb (f x) :=
h x
theorem comp_right {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α} {ga' : α → α} {gb : β → β}
{gb' : β → β} (h : semiconj f ga gb) (h' : semiconj f ga' gb') :
semiconj f (ga ∘ ga') (gb ∘ gb') :=
sorry
theorem comp_left {α : Type u_1} {β : Type u_2} {γ : Type u_3} {fab : α → β} {fbc : β → γ}
{ga : α → α} {gb : β → β} {gc : γ → γ} (hab : semiconj fab ga gb) (hbc : semiconj fbc gb gc) :
semiconj (fbc ∘ fab) ga gc :=
sorry
theorem id_right {α : Type u_1} {β : Type u_2} {f : α → β} : semiconj f id id := fun (_x : α) => rfl
theorem id_left {α : Type u_1} {ga : α → α} : semiconj id ga ga := fun (_x : α) => rfl
theorem inverses_right {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α} {ga' : α → α}
{gb : β → β} {gb' : β → β} (h : semiconj f ga gb) (ha : right_inverse ga' ga)
(hb : left_inverse gb' gb) : semiconj f ga' gb' :=
sorry
end semiconj
/-- Two maps `f g : α → α` commute if `f ∘ g = g ∘ f`. -/
def commute {α : Type u_1} (f : α → α) (g : α → α) := semiconj f g g
theorem semiconj.commute {α : Type u_1} {f : α → α} {g : α → α} (h : semiconj f g g) :
commute f g :=
h
namespace commute
theorem refl {α : Type u_1} (f : α → α) : commute f f := fun (_x : α) => Eq.refl (f (f _x))
theorem symm {α : Type u_1} {f : α → α} {g : α → α} (h : commute f g) : commute g f :=
fun (x : α) => Eq.symm (h x)
theorem comp_right {α : Type u_1} {f : α → α} {g : α → α} {g' : α → α} (h : commute f g)
(h' : commute f g') : commute f (g ∘ g') :=
semiconj.comp_right h h'
theorem comp_left {α : Type u_1} {f : α → α} {f' : α → α} {g : α → α} (h : commute f g)
(h' : commute f' g) : commute (f ∘ f') g :=
symm (comp_right (symm h) (symm h'))
theorem id_right {α : Type u_1} {f : α → α} : commute f id := semiconj.id_right
theorem id_left {α : Type u_1} {f : α → α} : commute id f := semiconj.id_left
end commute
/-- A map `f` semiconjugates a binary operation `ga` to a binary operation `gb` if
for all `x`, `y` we have `f (ga x y) = gb (f x) (f y)`. E.g., a `monoid_hom`
semiconjugates `(*)` to `(*)`. -/
def semiconj₂ {α : Type u_1} {β : Type u_2} (f : α → β) (ga : α → α → α) (gb : β → β → β) :=
∀ (x y : α), f (ga x y) = gb (f x) (f y)
namespace semiconj₂
protected theorem eq {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α → α} {gb : β → β → β}
(h : semiconj₂ f ga gb) (x : α) (y : α) : f (ga x y) = gb (f x) (f y) :=
h x y
protected theorem comp_eq {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α → α}
{gb : β → β → β} (h : semiconj₂ f ga gb) : bicompr f ga = bicompl gb f f :=
funext fun (x : α) => funext (h x)
theorem id_left {α : Type u_1} (op : α → α → α) : semiconj₂ id op op := fun (_x _x_1 : α) => rfl
theorem comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β} {ga : α → α → α}
{gb : β → β → β} {f' : β → γ} {gc : γ → γ → γ} (hf' : semiconj₂ f' gb gc)
(hf : semiconj₂ f ga gb) : semiconj₂ (f' ∘ f) ga gc :=
sorry
theorem is_associative_right {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α → α}
{gb : β → β → β} [is_associative α ga] (h : semiconj₂ f ga gb) (h_surj : surjective f) :
is_associative β gb :=
sorry
theorem is_associative_left {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α → α}
{gb : β → β → β} [is_associative β gb] (h : semiconj₂ f ga gb) (h_inj : injective f) :
is_associative α ga :=
sorry
theorem is_idempotent_right {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α → α}
{gb : β → β → β} [is_idempotent α ga] (h : semiconj₂ f ga gb) (h_surj : surjective f) :
is_idempotent β gb :=
sorry
theorem is_idempotent_left {α : Type u_1} {β : Type u_2} {f : α → β} {ga : α → α → α}
{gb : β → β → β} [is_idempotent β gb] (h : semiconj₂ f ga gb) (h_inj : injective f) :
is_idempotent α ga :=
sorry
end Mathlib |
706bd9473cb680a2624b6b7f470cd06fdd1bd479 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/topology/G_delta.lean | 158051517fc326c6b38974e811bd88ecccbee1af | [
"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 | 5,026 | 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, Yury Kudryashov
-/
import topology.metric_space.emetric_space
/-!
# `Gδ` sets
In this file we define `Gδ` sets and prove their basic properties.
## Main definitions
* `is_Gδ`: a set `s` is a `Gδ` set if it can be represented as an intersection
of countably many open sets;
* `residual`: the filter of residual sets. A set `s` is called *residual* if it includes a dense
`Gδ` set. In a Baire space (e.g., in a complete (e)metric space), residual sets form a filter.
For technical reasons, we define `residual` in any topological space but the definition agrees
with the description above only in Baire spaces.
## Main results
We prove that finite or countable intersections of Gδ sets is a Gδ set. We also prove that the
continuity set of a function from a topological space to an (e)metric space is a Gδ set.
## Tags
Gδ set, residual set
-/
noncomputable theory
open_locale classical topological_space filter
open filter encodable set
variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*}
section is_Gδ
variable [topological_space α]
/-- A Gδ set is a countable intersection of open sets. -/
def is_Gδ (s : set α) : Prop :=
∃T : set (set α), (∀t ∈ T, is_open t) ∧ countable T ∧ s = (⋂₀ T)
/-- An open set is a Gδ set. -/
lemma is_open.is_Gδ {s : set α} (h : is_open s) : is_Gδ s :=
⟨{s}, by simp [h], countable_singleton _, (set.sInter_singleton _).symm⟩
lemma is_Gδ_univ : is_Gδ (univ : set α) := is_open_univ.is_Gδ
lemma is_Gδ_bInter_of_open {I : set ι} (hI : countable I) {f : ι → set α}
(hf : ∀i ∈ I, is_open (f i)) : is_Gδ (⋂i∈I, f i) :=
⟨f '' I, by rwa ball_image_iff, hI.image _, by rw sInter_image⟩
lemma is_Gδ_Inter_of_open [encodable ι] {f : ι → set α}
(hf : ∀i, is_open (f i)) : is_Gδ (⋂i, f i) :=
⟨range f, by rwa forall_range_iff, countable_range _, by rw sInter_range⟩
/-- A countable intersection of Gδ sets is a Gδ set. -/
lemma is_Gδ_sInter {S : set (set α)} (h : ∀s∈S, is_Gδ s) (hS : countable S) : is_Gδ (⋂₀ S) :=
begin
choose T hT using h,
refine ⟨_, _, _, (sInter_bUnion (λ s hs, (hT s hs).2.2)).symm⟩,
{ simp only [mem_Union],
rintros t ⟨s, hs, tTs⟩,
exact (hT s hs).1 t tTs },
{ exact hS.bUnion (λs hs, (hT s hs).2.1) },
end
lemma is_Gδ_Inter [encodable ι] {s : ι → set α} (hs : ∀ i, is_Gδ (s i)) : is_Gδ (⋂ i, s i) :=
is_Gδ_sInter (forall_range_iff.2 hs) $ countable_range s
lemma is_Gδ_bInter {s : set ι} (hs : countable s) {t : Π i ∈ s, set α}
(ht : ∀ i ∈ s, is_Gδ (t i ‹_›)) : is_Gδ (⋂ i ∈ s, t i ‹_›) :=
begin
rw [bInter_eq_Inter],
haveI := hs.to_encodable,
exact is_Gδ_Inter (λ x, ht x x.2)
end
lemma is_Gδ.inter {s t : set α} (hs : is_Gδ s) (ht : is_Gδ t) : is_Gδ (s ∩ t) :=
by { rw inter_eq_Inter, exact is_Gδ_Inter (bool.forall_bool.2 ⟨ht, hs⟩) }
/-- The union of two Gδ sets is a Gδ set. -/
lemma is_Gδ.union {s t : set α} (hs : is_Gδ s) (ht : is_Gδ t) : is_Gδ (s ∪ t) :=
begin
rcases hs with ⟨S, Sopen, Scount, rfl⟩,
rcases ht with ⟨T, Topen, Tcount, rfl⟩,
rw [sInter_union_sInter],
apply is_Gδ_bInter_of_open (Scount.prod Tcount),
rintros ⟨a, b⟩ hab,
exact is_open.union (Sopen a hab.1) (Topen b hab.2)
end
end is_Gδ
section continuous_at
open topological_space
open_locale uniformity
variables [topological_space α]
lemma is_Gδ_set_of_continuous_at_of_countably_generated_uniformity
[uniform_space β] (hU : is_countably_generated (𝓤 β)) (f : α → β) :
is_Gδ {x | continuous_at f x} :=
begin
rcases hU.exists_antimono_subbasis uniformity_has_basis_open_symmetric with ⟨U, hUo, hU⟩,
simp only [uniform.continuous_at_iff_prod, nhds_prod_eq],
simp only [(nhds_basis_opens _).prod_self.tendsto_iff hU.to_has_basis, forall_prop_of_true,
set_of_forall, id],
refine is_Gδ_Inter (λ k, is_open.is_Gδ $ is_open_iff_mem_nhds.2 $ λ x, _),
rintros ⟨s, ⟨hsx, hso⟩, hsU⟩,
filter_upwards [is_open.mem_nhds hso hsx],
intros y hy,
exact ⟨s, ⟨hy, hso⟩, hsU⟩
end
/-- The set of points where a function is continuous is a Gδ set. -/
lemma is_Gδ_set_of_continuous_at [emetric_space β] (f : α → β) :
is_Gδ {x | continuous_at f x} :=
is_Gδ_set_of_continuous_at_of_countably_generated_uniformity
emetric.uniformity_has_countable_basis _
end continuous_at
/-- A set `s` is called *residual* if it includes a dense `Gδ` set. If `α` is a Baire space
(e.g., a complete metric space), then residual sets form a filter, see `mem_residual`.
For technical reasons we define the filter `residual` in any topological space but in a non-Baire
space it is not useful because it may contain some non-residual sets. -/
def residual (α : Type*) [topological_space α] : filter α :=
⨅ t (ht : is_Gδ t) (ht' : dense t), 𝓟 t
|
d66df91d88e6f273c42787c8b24b17597f6b6b36 | 280e37a1b98242171ef310909eae7a9811cc5303 | /src/NFA.lean | d359dea9b4e633afd16a42c525330cf8bb032712 | [] | no_license | foxthomson/regular | 6655d19258f80272e0740f4f19152e9dc2514d3b | 6c7c691eb226eb0e33a0995b027ba8641f1611bf | refs/heads/master | 1,672,642,114,346 | 1,603,122,446,000 | 1,603,122,446,000 | 300,912,418 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 9,490 | lean | import data.fintype.basic
import data.set.lattice
import DFA
universes u v
variable {α : Type}
structure NFA (alphabet : Type u) :=
[alphabet_fintype : fintype alphabet]
(state : Type v)
[state_fintype : fintype state]
[state_dec : decidable_eq state]
(step : state → alphabet → finset state)
(start : finset state)
(accept_states : finset state)
namespace NFA
-- @[reducible] def start_dec (M : NFA α) := decidable_pred M.start
-- @[reducible] def step_dec (M : NFA α) := Π (S : M.state) (a : α), decidable_pred (M.step S a)
-- instance dec₁ (M : NFA α) := M.start_dec
-- instance dec₂ (M : NFA α) := M.step_dec
-- instance dec₃ (M : NFA α) := M.accept_dec
instance dec (M : NFA α) := M.state_dec
instance fin₁ (M : NFA α) := M.alphabet_fintype
instance fin₂ (M : NFA α) := M.state_fintype
def step_set (M : NFA α) : finset M.state → α → finset M.state :=
λ Ss a, finset.bind Ss (λ S, (M.step S a))
def eval (M : NFA α) : list α → finset M.state := list.foldl M.step_set M.start
def accepts (M : NFA α) (s : list α) : Prop :=
∃ S ∈ M.accept_states, S ∈ M.eval s
def NFA_of_DFA (M : DFA α) : NFA α :=
{ alphabet_fintype := M.alphabet_fintype,
state := M.state,
state_fintype := M.state_fintype,
step := λ S a, {M.step S a},
start := {M.start},
accept_states := M.accept_states }
lemma NFA_of_DFA_eval_match (M : DFA α) [decidable_eq M.state] (s : list α) :
{M.eval s} = (NFA_of_DFA M).eval s :=
begin
change {list.foldl M.step M.start s} = list.foldl (NFA_of_DFA M).step_set {M.start} s,
generalize : M.start = start,
revert start,
induction s with a s ih,
{ tauto },
{ intro start,
rw [list.foldl, list.foldl],
have : (NFA_of_DFA M).step_set {start} a = {M.step start a},
{ rw step_set,
finish },
rw this,
tauto }
end
lemma NFA_of_DFA_correct (M : DFA α) (s : list α) :
M.accepts s ↔ (NFA_of_DFA M).accepts s :=
begin
rw [accepts, DFA.accepts, ←NFA_of_DFA_eval_match],
split,
{ intro h,
use M.eval s,
finish },
{ rintro ⟨ S, hS₁, hS₂ ⟩,
rw finset.mem_singleton at hS₂,
rw hS₂ at hS₁,
assumption }
end
def DFA_of_NFA (M : NFA α) : DFA α :=
{ alphabet_fintype := M.alphabet_fintype,
state := finset M.state,
step := M.step_set,
start := M.start,
accept_states := finset.univ.filter (λ S, ∃ s ∈ S, s ∈ M.accept_states) }
lemma DFA_of_NFA_correct (M : NFA α) (s : list α) :
M.accepts s ↔ M.DFA_of_NFA.accepts s :=
begin
rw [accepts, DFA.accepts, eval, DFA.eval],
change (∃ (S : M.state) (H : S ∈ M.accept_states), S ∈ list.foldl M.step_set M.start s) ↔ list.foldl M.step_set M.start s ∈ finset.univ.filter (λ S : finset M.state, ∃ s ∈ S, s ∈ M.accept_states),
rw finset.mem_filter,
finish
end
end NFA
structure ε_NFA (alphabet : Type u) :=
[alphabet_fintype : fintype alphabet]
(state : Type v)
[state_fintype : fintype state]
[state_dec : decidable_eq state]
(step : state → option alphabet → finset state)
(start : finset state)
(accept_states : finset state)
namespace ε_NFA
instance dec (M : ε_NFA α) := M.state_dec
instance fin₁ (M : ε_NFA α) : fintype α := M.alphabet_fintype
instance fin₂ (M : ε_NFA α) : fintype M.state := M.state_fintype
def step_set' (M : ε_NFA α) : finset M.state → option α → finset M.state :=
λ Ss a, finset.bind Ss (λ S, M.step S a)
inductive ε_closure_set (M : ε_NFA α) (Ss : finset M.state) : M.state → Prop
| base : ∀ (S ∈ Ss), ε_closure_set S
| step : ∀ S T, ε_closure_set S → T ∈ M.step S option.none → ε_closure_set T
def sub_of_compl {β : Type u} [fintype β] [decidable_eq β] : ∀ T U : finset β, Tᶜ ⊆ Uᶜ → U ⊆ T :=
begin
intros T U h x hxU,
by_contra hTc,
rw ←finset.mem_compl at hTc,
have hUc := finset.mem_of_subset h hTc,
finish
end
instance ε_NFA_has_well_founded {β : Type u} [fintype β] [decidable_eq β] : has_well_founded (finset β) :=
{ r := (λ S₁ S₂ : finset β, S₁ᶜ < S₂ᶜ),
wf :=
inv_image.wf _ finset.lt_wf }
def ε_closure (M : ε_NFA α) : finset M.state → finset M.state
| S :=
begin
let S' := S ∪ M.step_set' S none,
by_cases heq : S' = S,
{ exact S },
{ let : S'ᶜ < Sᶜ,
{ have hsub : S'ᶜ ⊆ Sᶜ,
{ intros s hs,
rw finset.mem_compl at hs ⊢,
finish },
use hsub,
{ intro hS,
apply heq,
rw finset.subset.antisymm_iff,
split;
apply sub_of_compl;
assumption } },
exact ε_closure S' }
end
using_well_founded {dec_tac := tactic.assumption}
lemma step_set'_wf (M : ε_NFA α) (S : finset M.state) (hneq : S ∪ M.step_set' S none ≠ S) :
(S ∪ M.step_set' S none)ᶜ < Sᶜ :=
begin
have hsub : (S ∪ M.step_set' S none)ᶜ ⊆ Sᶜ,
{ intros s hs,
rw finset.mem_compl at hs ⊢,
finish },
use hsub,
intro hS,
apply hneq,
rw finset.subset.antisymm_iff,
split;
apply sub_of_compl,
assumption'
end
lemma ε_closure_equiv_ε_closure_set (M : ε_NFA α) :
Π (S : finset M.state) (s : M.state), s ∈ M.ε_closure S ↔ M.ε_closure_set S s
| S :=
begin
have IH := λ T (h : Tᶜ < Sᶜ), ε_closure_equiv_ε_closure_set T,
intro s,
split,
{ intro h,
rw ε_closure at h,
dsimp at h,
split_ifs at h with heq,
{ apply ε_closure_set.base,
assumption },
{ have hwf : (S ∪ M.step_set' S none)ᶜ < Sᶜ := M.step_set'_wf S heq,
have h' : M.ε_closure_set (S ∪ M.step_set' S none) s,
rwa ← IH (S ∪ M.step_set' S none) hwf,
induction h' with t ht t' t d e ih,
{ simp at ht,
cases ht,
{ apply ε_closure_set.base,
assumption },
{ rw step_set' at ht,
simp only [exists_prop, finset.mem_bind] at ht,
cases ht with t' ht,
apply ε_closure_set.step t' t,
{ apply ε_closure_set.base,
tauto },
{ tauto } } },
{ apply ε_closure_set.step t' t,
{ apply ih,
rwa IH (S ∪ M.step_set' S none) hwf },
assumption } } },
{ intro h,
rw ε_closure,
dsimp,
split_ifs with heq;
induction h with t ht t' t ht' ht ih,
{ assumption },
{ rw ←heq,
simp only [finset.mem_union],
right,
rw step_set',
simp only [exists_prop, finset.mem_bind],
use t',
tauto },
all_goals
{ have hwf : (S ∪ M.step_set' S none)ᶜ < Sᶜ := M.step_set'_wf S heq },
{ rw IH (S ∪ M.step_set' S none) hwf,
apply ε_closure_set.base,
rw finset.mem_union,
left,
assumption },
{ rw IH (S ∪ M.step_set' S none) hwf,
apply ε_closure_set.step t' t,
rwa ←IH (S ∪ M.step_set' S none) hwf,
assumption } }
end
using_well_founded {dec_tac := tactic.assumption}
def step_set (M : ε_NFA α) : finset M.state → α → finset M.state :=
λ Ss a, M.ε_closure $ finset.bind Ss (λ S, M.step S (option.some a))
def eval (M : ε_NFA α) : list α → finset M.state :=
list.foldl M.step_set (M.ε_closure M.start)
def accepts (M : ε_NFA α) (s : list α) : Prop :=
∃ S ∈ M.accept_states, S ∈ M.eval s
instance accepts_dec (M : ε_NFA α) : decidable_pred M.accepts :=
begin
intro s,
exact fintype.decidable_exists_fintype
end
def NFA_of_ε_NFA (M : ε_NFA α) : NFA α :=
{ alphabet_fintype := M.alphabet_fintype,
state := M.state,
step := λ S a, M.ε_closure (M.step S (some a)),
start := M.ε_closure M.start,
accept_states := M.accept_states }
lemma NFA_of_ε_NFA_step_set_match (M : ε_NFA α) (Ss : finset M.state) (a : α) :
M.step_set Ss a = M.NFA_of_ε_NFA.step_set Ss a :=
begin
rw [step_set, NFA.step_set],
simp,
ext b,
rw ε_closure_equiv_ε_closure_set,
split,
{ intro h,
-- generalize_hyp hT : (Ss.bind (λ (S : M.state), M.step S (some a))) = Ts at h,
induction h with s h U T hU h ih,
{
-- rw ←hT at h,
simp only [exists_prop, finset.mem_bind] at h ⊢,
cases h with i hi,
rw @finset.mem_bind _ M.state M.state_dec,
use i,
use hi.1,
change s ∈ M.ε_closure (M.step i (some a)),
rw ε_closure_equiv_ε_closure_set,
apply ε_closure_set.base,
tauto },
{ rw @finset.mem_bind _ M.state M.state_dec at ⊢ ih,
rcases ih with ⟨ i, h₁, h₂ ⟩,
existsi i,
existsi h₁,
change T ∈ M.ε_closure (M.step i (some a)),
rw ε_closure_equiv_ε_closure_set,
apply ε_closure_set.step U _,
change U ∈ M.ε_closure (M.step _ (some _)) at h₂,
rw ←ε_closure_equiv_ε_closure_set,
assumption' } },
{ rw @finset.mem_bind _ M.state M.state_dec,
rintro ⟨ s, hsSs, hba ⟩,
change b ∈ M.ε_closure (M.step _ (some _)) at hba,
rw ε_closure_equiv_ε_closure_set at hba,
induction hba with s h U T hU h ih,
{ apply ε_closure_set.base,
finish },
{ specialize ih,
apply ε_closure_set.step,
assumption' } }
end
lemma NFA_of_ε_NFA_eval_match (M : ε_NFA α) (s : list α) :
M.eval s = (NFA_of_ε_NFA M).eval s :=
begin
change list.foldl M.step_set (M.ε_closure M.start) s = list.foldl M.NFA_of_ε_NFA.step_set (M.ε_closure M.start) s,
congr,
ext1,
ext1,
rw NFA_of_ε_NFA_step_set_match
end
lemma NFA_of_ε_NFA_correct (M : ε_NFA α) (s : list α) :
M.accepts s ↔ M.NFA_of_ε_NFA.accepts s :=
begin
rw [accepts, NFA.accepts, NFA_of_ε_NFA_eval_match],
tauto
end
end ε_NFA
|
a8fd59e28763b14a50fc5c0248fbd10523d96d30 | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/tactic/tauto.lean | a3b943fb8fdeb1669235c1da5529c007c2225d5c | [
"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 | 8,319 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import logic.basic tactic.solve_by_elim
namespace tactic
open expr
open tactic.interactive ( casesm constructor_matching )
/--
find all assumptions of the shape `¬ (p ∧ q)` or `¬ (p ∨ q)` and
replace them using de Morgan's law.
-/
meta def distrib_not : tactic unit :=
do hs ← local_context,
hs.for_each $ λ h,
all_goals $
iterate_at_most 3 $
do h ← get_local h.local_pp_name,
e ← infer_type h,
match e with
| `(¬ _ = _) := replace h.local_pp_name ``(mt iff.to_eq %%h)
| `(_ ≠ _) := replace h.local_pp_name ``(mt iff.to_eq %%h)
| `(_ = _) := replace h.local_pp_name ``(eq.to_iff %%h)
| `(¬ (_ ∧ _)) := replace h.local_pp_name ``(not_and_distrib'.mp %%h) <|>
replace h.local_pp_name ``(not_and_distrib.mp %%h)
| `(¬ (_ ∨ _)) := replace h.local_pp_name ``(not_or_distrib.mp %%h)
| `(¬ ¬ _) := replace h.local_pp_name ``(of_not_not %%h)
| `(¬ (_ → (_ : Prop))) := replace h.local_pp_name ``(not_imp.mp %%h)
| `(¬ (_ ↔ _)) := replace h.local_pp_name ``(not_iff.mp %%h)
| `(_ ↔ _) := replace h.local_pp_name ``(iff_iff_and_or_not_and_not.mp %%h) <|>
replace h.local_pp_name ``(iff_iff_and_or_not_and_not.mp (%%h).symm) <|>
() <$ tactic.cases h
| `(_ → _) := replace h.local_pp_name ``(not_or_of_imp %%h)
| _ := failed
end
meta def tauto_state := ref $ expr_map (option (expr × expr))
meta def modify_ref {α : Type} (r : ref α) (f : α → α) :=
read_ref r >>= write_ref r ∘ f
meta def add_refl (r : tauto_state) (e : expr) : tactic (expr × expr) :=
do m ← read_ref r,
p ← mk_mapp `rfl [none,e],
write_ref r $ m.insert e none,
return (e,p)
meta def add_symm_proof (r : tauto_state) (e : expr) : tactic (expr × expr) :=
do env ← get_env,
let rel := e.get_app_fn.const_name,
some symm ← pure $ environment.symm_for env rel
| add_refl r e,
(do e' ← mk_meta_var `(Prop),
iff_t ← to_expr ``(%%e = %%e'),
(_,p) ← solve_aux iff_t
(applyc `iff.to_eq ; () <$ split ; applyc symm),
e' ← instantiate_mvars e',
m ← read_ref r,
write_ref r $ (m.insert e (e',p)).insert e' none,
return (e',p) )
<|> add_refl r e
meta def add_edge (r : tauto_state) (x y p : expr) : tactic unit :=
modify_ref r $ λ m, m.insert x (y,p)
meta def root (r : tauto_state) : expr → tactic (expr × expr) | e :=
do m ← read_ref r,
let record_e : tactic (expr × expr) :=
match e with
| v@(expr.mvar _ _ _) :=
(do (e,p) ← get_assignment v >>= root,
add_edge r v e p,
return (e,p)) <|>
add_refl r e
| _ := add_refl r e
end,
some e' ← pure $ m.find e | record_e,
match e' with
| (some (e',p')) :=
do (e'',p'') ← root e',
p'' ← mk_app `eq.trans [p',p''],
add_edge r e e'' p'',
pure (e'',p'')
| none := prod.mk e <$> mk_mapp `rfl [none,some e]
end
meta def symm_eq (r : tauto_state) : expr → expr → tactic expr | a b :=
do m ← read_ref r,
(a',pa) ← root r a,
(b',pb) ← root r b,
(unify a' b' >> add_refl r a' *> mk_mapp `rfl [none,a]) <|>
do p ← match (a', b') with
| (`(¬ %%a₀), `(¬ %%b₀)) :=
do p ← symm_eq a₀ b₀,
p' ← mk_app `congr_arg [`(not),p],
add_edge r a' b' p',
return p'
| (`(%%a₀ ∧ %%a₁), `(%%b₀ ∧ %%b₁)) :=
do p₀ ← symm_eq a₀ b₀,
p₁ ← symm_eq a₁ b₁,
p' ← to_expr ``(congr (congr_arg and %%p₀) %%p₁),
add_edge r a' b' p',
return p'
| (`(%%a₀ ∨ %%a₁), `(%%b₀ ∨ %%b₁)) :=
do p₀ ← symm_eq a₀ b₀,
p₁ ← symm_eq a₁ b₁,
p' ← to_expr ``(congr (congr_arg or %%p₀) %%p₁),
add_edge r a' b' p',
return p'
| (`(%%a₀ ↔ %%a₁), `(%%b₀ ↔ %%b₁)) :=
(do p₀ ← symm_eq a₀ b₀,
p₁ ← symm_eq a₁ b₁,
p' ← to_expr ``(congr (congr_arg iff %%p₀) %%p₁),
add_edge r a' b' p',
return p') <|>
do p₀ ← symm_eq a₀ b₁,
p₁ ← symm_eq a₁ b₀,
p' ← to_expr ``(eq.trans (congr (congr_arg iff %%p₀) %%p₁)
(iff.to_eq iff.comm ) ),
add_edge r a' b' p',
return p'
| (`(%%a₀ → %%a₁), `(%%b₀ → %%b₁)) :=
if ¬ a₁.has_var ∧ ¬ b₁.has_var then
do p₀ ← symm_eq a₀ b₀,
p₁ ← symm_eq a₁ b₁,
p' ← mk_app `congr_arg [`(implies),p₀,p₁],
add_edge r a' b' p',
return p'
else unify a' b' >> add_refl r a' *> mk_mapp `rfl [none,a]
| (_, _) :=
(do guard $ a'.get_app_fn.is_constant ∧
a'.get_app_fn.const_name = b'.get_app_fn.const_name,
(a'',pa') ← add_symm_proof r a',
guard $ a'' =ₐ b',
pure pa' )
end,
p' ← mk_eq_trans pa p,
add_edge r a' b' p',
mk_eq_symm pb >>= mk_eq_trans p'
meta def find_eq_type (r : tauto_state) : expr → list expr → tactic (expr × expr)
| e [] := failed
| e (H :: Hs) :=
do t ← infer_type H,
t' ← infer_type e,
(prod.mk H <$> symm_eq r e t) <|> find_eq_type e Hs
private meta def contra_p_not_p (r : tauto_state) : list expr → list expr → tactic unit
| [] Hs := failed
| (H1 :: Rs) Hs :=
do t ← (extract_opt_auto_param <$> infer_type H1) >>= whnf,
(do a ← match_not t,
(H2,p) ← find_eq_type r a Hs,
H2 ← to_expr ``( (%%p).mpr %%H2 ),
tgt ← target,
pr ← mk_app `absurd [tgt, H2, H1],
tactic.exact pr)
<|> contra_p_not_p Rs Hs
meta def contradiction_with (r : tauto_state) : tactic unit :=
contradiction <|>
do tactic.try intro1,
ctx ← local_context,
contra_p_not_p r ctx ctx
meta def contradiction_symm :=
using_new_ref (native.rb_map.mk _ _) contradiction_with
meta def assumption_with (r : tauto_state) : tactic unit :=
do { ctx ← local_context,
t ← target,
(H,p) ← find_eq_type r t ctx,
mk_eq_mpr p H >>= tactic.exact }
<|> fail "assumption tactic failed"
meta def assumption_symm :=
using_new_ref (native.rb_map.mk _ _) assumption_with
meta def tautology (c : bool := ff) : tactic unit :=
do when c classical,
using_new_ref (expr_map.mk _) $ λ r,
do try (contradiction_with r);
try (assumption_with r);
repeat (do
gs ← get_goals,
() <$ tactic.intros;
distrib_not;
casesm (some ()) [``(_ ∧ _),``(_ ∨ _),``(Exists _),``(false)];
try (contradiction_with r);
try (target >>= match_or >> refine ``( or_iff_not_imp_left.mpr _));
try (target >>= match_or >> refine ``( or_iff_not_imp_right.mpr _));
() <$ tactic.intros;
constructor_matching (some ()) [``(_ ∧ _),``(_ ↔ _),``(true)];
try (assumption_with r),
gs' ← get_goals,
guard (gs ≠ gs') ) ;
repeat
(reflexivity <|> solve_by_elim <|>
constructor_matching none [``(_ ∧ _),``(_ ↔ _),``(Exists _),``(true)] ) ;
done
open interactive lean.parser
namespace interactive
local postfix `?`:9001 := optional
/--
`tautology` breaks down assumptions of the form `_ ∧ _`, `_ ∨ _`, `_ ↔ _` and `∃ _, _`
and splits a goal of the form `_ ∧ _`, `_ ↔ _` or `∃ _, _` until it can be discharged
using `reflexivity` or `solve_by_elim`
-/
meta def tautology (c : parse $ (tk "!")?) := tactic.tautology c.is_some
/-- Shorter name for the tactic `tautology`. -/
meta def tauto (c : parse $ (tk "!")?) := tautology c
end interactive
end tactic
|
ff9d5fc2a5b2849708797de6a666eb73b2432cd8 | 32da3d0f92cab08875472ef6cacc1931c2b3eafa | /src/measure_theory/measure_space.lean | 04fa8d300e046b1a450afd3520ab4a4a2a0b8f02 | [
"Apache-2.0"
] | permissive | karthiknadig/mathlib | b6073c3748860bfc9a3e55da86afcddba62dc913 | 33a86cfff12d7f200d0010cd03b95e9b69a6c1a5 | refs/heads/master | 1,676,389,371,851 | 1,610,061,127,000 | 1,610,061,127,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 94,921 | 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 measure_theory.outer_measure
import order.filter.countable_Inter
import data.set.accumulate
/-!
# Measure spaces
Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the
extended nonnegative reals that satisfies the following conditions:
1. `μ ∅ = 0`;
2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint
sets is equal to the measure of the individual sets.
Every measure can be canonically extended to an outer measure, so that it assigns values to
all subsets, not just the measurable subsets. On the other hand, a measure that is countably
additive on measurable sets can be restricted to measurable sets to obtain a measure.
In this file a measure is defined to be an outer measure that is countably additive on
measurable sets, with the additional assumption that the outer measure is the canonical
extension of the restricted measure.
Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ennreal`.
We introduce the following typeclasses for measures:
* `probability_measure μ`: `μ univ = 1`;
* `finite_measure μ`: `μ univ < ⊤`;
* `sigma_finite μ`: there exists a countable collection of measurable sets that cover `univ`
where `μ` is finite;
* `locally_finite_measure μ` : `∀ x, ∃ s ∈ 𝓝 x, μ s < ⊤`;
* `has_no_atoms μ` : `∀ x, μ {x} = 0`; possibly should be redefined as
`∀ s, 0 < μ s → ∃ t ⊆ s, 0 < μ t ∧ μ t < μ s`.
Given a measure, the null sets are the sets where `μ s = 0`, where `μ` denotes the corresponding
outer measure (so `s` might not be measurable). We can then define the completion of `μ` as the
measure on the least `σ`-algebra that also contains all null sets, by defining the measure to be `0`
on the null sets.
## Main statements
* `completion` is the completion of a measure to all null measurable sets.
* `measure.of_measurable` and `outer_measure.to_measure` are two important ways to define a measure.
## Implementation notes
Given `μ : measure α`, `μ s` is the value of the *outer measure* applied to `s`.
This conveniently allows us to apply the measure to sets without proving that they are measurable.
We get countable subadditivity for all sets, but only countable additivity for measurable sets.
You often don't want to define a measure via its constructor.
Two ways that are sometimes more convenient:
* `measure.of_measurable` is a way to define a measure by only giving its value on measurable sets
and proving the properties (1) and (2) mentioned above.
* `outer_measure.to_measure` is a way of obtaining a measure from an outer measure by showing that
all measurable sets in the measurable space are Carathéodory measurable.
To prove that two measures are equal, there are multiple options:
* `ext`: two measures are equal if they are equal on all measurable sets.
* `ext_of_generate_from_of_Union`: two measures are equal if they are equal on a π-system generating
the measurable sets, if the π-system contains a spanning increasing sequence of sets where the
measures take finite value (in particular the measures are σ-finite). This is a special case of the
more general `ext_of_generate_from_of_cover`
* `ext_of_generate_finite`: two finite measures are equal if they are equal on a π-system
generating the measurable sets. This is a special case of `ext_of_generate_from_of_Union` using
`C ∪ {univ}`, but is easier to work with.
A `measure_space` is a class that is a measurable space with a canonical measure.
The measure is denoted `volume`.
## References
* <https://en.wikipedia.org/wiki/Measure_(mathematics)>
* <https://en.wikipedia.org/wiki/Complete_measure>
* <https://en.wikipedia.org/wiki/Almost_everywhere>
## Tags
measure, almost everywhere, measure space, completion, null set, null measurable set
-/
noncomputable theory
open classical set filter function measurable_space
open_locale classical topological_space big_operators filter
variables {α β γ δ ι : Type*}
namespace measure_theory
/-- A measure is defined to be an outer measure that is countably additive on
measurable sets, with the additional assumption that the outer measure is the canonical
extension of the restricted measure. -/
structure measure (α : Type*) [measurable_space α] extends outer_measure α :=
(m_Union ⦃f : ℕ → set α⦄ :
(∀ i, is_measurable (f i)) → pairwise (disjoint on f) →
measure_of (⋃ i, f i) = (∑' i, measure_of (f i)))
(trimmed : to_outer_measure.trim = to_outer_measure)
/-- Measure projections for a measure space.
For measurable sets this returns the measure assigned by the `measure_of` field in `measure`.
But we can extend this to _all_ sets, but using the outer measure. This gives us monotonicity and
subadditivity for all sets.
-/
instance measure.has_coe_to_fun [measurable_space α] : has_coe_to_fun (measure α) :=
⟨λ _, set α → ennreal, λ m, m.to_outer_measure⟩
section
variables [measurable_space α] {μ μ₁ μ₂ : measure α} {s s₁ s₂ t : set α}
namespace measure
/-! ### General facts about measures -/
/-- Obtain a measure by giving a countably additive function that sends `∅` to `0`. -/
def of_measurable (m : Π (s : set α), is_measurable s → ennreal)
(m0 : m ∅ is_measurable.empty = 0)
(mU : ∀ {{f : ℕ → set α}} (h : ∀ i, is_measurable (f i)), pairwise (disjoint on f) →
m (⋃ i, f i) (is_measurable.Union h) = (∑' i, m (f i) (h i))) : measure α :=
{ m_Union := λ f hf hd,
show induced_outer_measure m _ m0 (Union f) =
∑' i, induced_outer_measure m _ m0 (f i), begin
rw [induced_outer_measure_eq m0 mU, mU hf hd],
congr, funext n, rw induced_outer_measure_eq m0 mU
end,
trimmed :=
show (induced_outer_measure m _ m0).trim = induced_outer_measure m _ m0, begin
unfold outer_measure.trim,
congr, funext s hs,
exact induced_outer_measure_eq m0 mU hs
end,
..induced_outer_measure m _ m0 }
lemma of_measurable_apply {m : Π (s : set α), is_measurable s → ennreal}
{m0 : m ∅ is_measurable.empty = 0}
{mU : ∀ {{f : ℕ → set α}} (h : ∀ i, is_measurable (f i)), pairwise (disjoint on f) →
m (⋃ i, f i) (is_measurable.Union h) = (∑' i, m (f i) (h i))}
(s : set α) (hs : is_measurable s) : of_measurable m m0 mU s = m s hs :=
induced_outer_measure_eq m0 mU hs
lemma to_outer_measure_injective : injective (to_outer_measure : measure α → outer_measure α) :=
λ ⟨m₁, u₁, h₁⟩ ⟨m₂, u₂, h₂⟩ h, by { congr, exact h }
@[ext] lemma ext (h : ∀ s, is_measurable s → μ₁ s = μ₂ s) : μ₁ = μ₂ :=
to_outer_measure_injective $ by rw [← trimmed, outer_measure.trim_congr h, trimmed]
lemma ext_iff : μ₁ = μ₂ ↔ ∀ s, is_measurable s → μ₁ s = μ₂ s :=
⟨by { rintro rfl s hs, refl }, measure.ext⟩
end measure
@[simp] lemma coe_to_outer_measure : ⇑μ.to_outer_measure = μ := rfl
lemma to_outer_measure_apply (s : set α) : μ.to_outer_measure s = μ s := rfl
lemma measure_eq_trim (s : set α) : μ s = μ.to_outer_measure.trim s :=
by rw μ.trimmed; refl
lemma measure_eq_infi (s : set α) : μ s = ⨅ t (st : s ⊆ t) (ht : is_measurable t), μ t :=
by rw [measure_eq_trim, outer_measure.trim_eq_infi]; refl
/-- A variant of `measure_eq_infi` which has a single `infi`. This is useful when applying a
lemma next that only works for non-empty infima, in which case you can use
`nonempty_measurable_superset`. -/
lemma measure_eq_infi' (μ : measure α) (s : set α) :
μ s = ⨅ t : { t // s ⊆ t ∧ is_measurable t}, μ t :=
by simp_rw [infi_subtype, infi_and, subtype.coe_mk, ← measure_eq_infi]
lemma measure_eq_induced_outer_measure :
μ s = induced_outer_measure (λ s _, μ s) is_measurable.empty μ.empty s :=
measure_eq_trim _
lemma to_outer_measure_eq_induced_outer_measure :
μ.to_outer_measure = induced_outer_measure (λ s _, μ s) is_measurable.empty μ.empty :=
μ.trimmed.symm
lemma measure_eq_extend (hs : is_measurable s) :
μ s = extend (λ t (ht : is_measurable t), μ t) s :=
by { rw [measure_eq_induced_outer_measure, induced_outer_measure_eq_extend _ _ hs],
exact μ.m_Union }
@[simp] lemma measure_empty : μ ∅ = 0 := μ.empty
lemma nonempty_of_measure_ne_zero (h : μ s ≠ 0) : s.nonempty :=
ne_empty_iff_nonempty.1 $ λ h', h $ h'.symm ▸ measure_empty
lemma measure_mono (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := μ.mono h
lemma measure_mono_null (h : s₁ ⊆ s₂) (h₂ : μ s₂ = 0) : μ s₁ = 0 :=
le_zero_iff_eq.1 $ h₂ ▸ measure_mono h
lemma measure_mono_top (h : s₁ ⊆ s₂) (h₁ : μ s₁ = ⊤) : μ s₂ = ⊤ :=
top_unique $ h₁ ▸ measure_mono h
lemma exists_is_measurable_superset_of_measure_eq_zero (h : μ s = 0) :
∃ t, s ⊆ t ∧ is_measurable t ∧ μ t = 0 :=
outer_measure.exists_is_measurable_superset_of_trim_eq_zero (by rw [← measure_eq_trim, h])
lemma exists_is_measurable_superset_iff_measure_eq_zero :
(∃ t, s ⊆ t ∧ is_measurable t ∧ μ t = 0) ↔ μ s = 0 :=
⟨λ ⟨t, hst, _, ht⟩, measure_mono_null hst ht, exists_is_measurable_superset_of_measure_eq_zero⟩
theorem measure_Union_le [encodable β] (s : β → set α) : μ (⋃ i, s i) ≤ (∑' i, μ (s i)) :=
μ.to_outer_measure.Union _
lemma measure_bUnion_le {s : set β} (hs : countable s) (f : β → set α) :
μ (⋃ b ∈ s, f b) ≤ ∑' p : s, μ (f p) :=
begin
haveI := hs.to_encodable,
rw [bUnion_eq_Union],
apply measure_Union_le
end
lemma measure_bUnion_finset_le (s : finset β) (f : β → set α) :
μ (⋃ b ∈ s, f b) ≤ ∑ p in s, μ (f p) :=
begin
rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype],
exact measure_bUnion_le s.countable_to_set f
end
lemma measure_bUnion_lt_top {s : set β} {f : β → set α} (hs : finite s)
(hfin : ∀ i ∈ s, μ (f i) < ⊤) : μ (⋃ i ∈ s, f i) < ⊤ :=
begin
convert (measure_bUnion_finset_le hs.to_finset f).trans_lt _,
{ ext, rw [finite.mem_to_finset] },
apply ennreal.sum_lt_top, simpa only [finite.mem_to_finset]
end
lemma measure_Union_null [encodable β] {s : β → set α} :
(∀ i, μ (s i) = 0) → μ (⋃ i, s i) = 0 :=
μ.to_outer_measure.Union_null
lemma measure_Union_null_iff [encodable ι] {s : ι → set α} :
μ (⋃ i, s i) = 0 ↔ ∀ i, μ (s i) = 0 :=
⟨λ h i, measure_mono_null (subset_Union _ _) h, measure_Union_null⟩
theorem measure_union_le (s₁ s₂ : set α) : μ (s₁ ∪ s₂) ≤ μ s₁ + μ s₂ :=
μ.to_outer_measure.union _ _
lemma measure_union_null : μ s₁ = 0 → μ s₂ = 0 → μ (s₁ ∪ s₂) = 0 :=
μ.to_outer_measure.union_null
lemma measure_union_null_iff : μ (s₁ ∪ s₂) = 0 ↔ μ s₁ = 0 ∧ μ s₂ = 0:=
⟨λ h, ⟨measure_mono_null (subset_union_left _ _) h, measure_mono_null (subset_union_right _ _) h⟩,
λ h, measure_union_null h.1 h.2⟩
lemma measure_Union [encodable β] {f : β → set α}
(hn : pairwise (disjoint on f)) (h : ∀ i, is_measurable (f i)) :
μ (⋃ i, f i) = (∑' i, μ (f i)) :=
begin
rw [measure_eq_extend (is_measurable.Union h),
extend_Union is_measurable.empty _ is_measurable.Union _ hn h],
{ simp [measure_eq_extend, h] },
{ exact μ.empty },
{ exact μ.m_Union }
end
lemma measure_union (hd : disjoint s₁ s₂) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) :
μ (s₁ ∪ s₂) = μ s₁ + μ s₂ :=
begin
rw [union_eq_Union, measure_Union, tsum_fintype, fintype.sum_bool, cond, cond],
exacts [pairwise_disjoint_on_bool.2 hd, λ b, bool.cases_on b h₂ h₁]
end
lemma measure_bUnion {s : set β} {f : β → set α} (hs : countable s)
(hd : pairwise_on s (disjoint on f)) (h : ∀ b ∈ s, is_measurable (f b)) :
μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) :=
begin
haveI := hs.to_encodable,
rw bUnion_eq_Union,
exact measure_Union (hd.on_injective subtype.coe_injective $ λ x, x.2) (λ x, h x x.2)
end
lemma measure_sUnion {S : set (set α)} (hs : countable S)
(hd : pairwise_on S disjoint) (h : ∀ s ∈ S, is_measurable s) :
μ (⋃₀ S) = ∑' s : S, μ s :=
by rw [sUnion_eq_bUnion, measure_bUnion hs hd h]
lemma measure_bUnion_finset {s : finset ι} {f : ι → set α} (hd : pairwise_on ↑s (disjoint on f))
(hm : ∀ b ∈ s, is_measurable (f b)) :
μ (⋃ b ∈ s, f b) = ∑ p in s, μ (f p) :=
begin
rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype],
exact measure_bUnion s.countable_to_set hd hm
end
/-- If `s` is a countable set, then the measure of its preimage can be found as the sum of measures
of the fibers `f ⁻¹' {y}`. -/
lemma tsum_measure_preimage_singleton {s : set β} (hs : countable s) {f : α → β}
(hf : ∀ y ∈ s, is_measurable (f ⁻¹' {y})) :
(∑' b : s, μ (f ⁻¹' {↑b})) = μ (f ⁻¹' s) :=
by rw [← set.bUnion_preimage_singleton, measure_bUnion hs (pairwise_on_disjoint_fiber _ _) hf]
/-- If `s` is a `finset`, then the measure of its preimage can be found as the sum of measures
of the fibers `f ⁻¹' {y}`. -/
lemma sum_measure_preimage_singleton (s : finset β) {f : α → β}
(hf : ∀ y ∈ s, is_measurable (f ⁻¹' {y})) :
∑ b in s, μ (f ⁻¹' {b}) = μ (f ⁻¹' ↑s) :=
by simp only [← measure_bUnion_finset (pairwise_on_disjoint_fiber _ _) hf,
finset.bUnion_preimage_singleton]
lemma measure_diff (h : s₂ ⊆ s₁) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂)
(h_fin : μ s₂ < ⊤) :
μ (s₁ \ s₂) = μ s₁ - μ s₂ :=
begin
refine (ennreal.add_sub_self' h_fin).symm.trans _,
rw [← measure_union disjoint_diff h₂ (h₁.diff h₂), union_diff_cancel h]
end
lemma measure_compl (h₁ : is_measurable s) (h_fin : μ s < ⊤) : μ (sᶜ) = μ univ - μ s :=
by { rw compl_eq_univ_diff, exact measure_diff (subset_univ s) is_measurable.univ h₁ h_fin }
lemma sum_measure_le_measure_univ {s : finset ι} {t : ι → set α} (h : ∀ i ∈ s, is_measurable (t i))
(H : pairwise_on ↑s (disjoint on t)) :
∑ i in s, μ (t i) ≤ μ (univ : set α) :=
by { rw ← measure_bUnion_finset H h, exact measure_mono (subset_univ _) }
lemma tsum_measure_le_measure_univ {s : ι → set α} (hs : ∀ i, is_measurable (s i))
(H : pairwise (disjoint on s)) :
(∑' i, μ (s i)) ≤ μ (univ : set α) :=
begin
rw [ennreal.tsum_eq_supr_sum],
exact supr_le (λ s, sum_measure_le_measure_univ (λ i hi, hs i) (λ i hi j hj hij, H i j hij))
end
/-- Pigeonhole principle for measure spaces: if `∑' i, μ (s i) > μ univ`, then
one of the intersections `s i ∩ s j` is not empty. -/
lemma exists_nonempty_inter_of_measure_univ_lt_tsum_measure (μ : measure α) {s : ι → set α}
(hs : ∀ i, is_measurable (s i)) (H : μ (univ : set α) < ∑' i, μ (s i)) :
∃ i j (h : i ≠ j), (s i ∩ s j).nonempty :=
begin
contrapose! H,
apply tsum_measure_le_measure_univ hs,
exact λ i j hij x hx, H i j hij ⟨x, hx⟩
end
/-- Pigeonhole principle for measure spaces: if `s` is a `finset` and
`∑ i in s, μ (t i) > μ univ`, then one of the intersections `t i ∩ t j` is not empty. -/
lemma exists_nonempty_inter_of_measure_univ_lt_sum_measure (μ : measure α) {s : finset ι}
{t : ι → set α} (h : ∀ i ∈ s, is_measurable (t i)) (H : μ (univ : set α) < ∑ i in s, μ (t i)) :
∃ (i ∈ s) (j ∈ s) (h : i ≠ j), (t i ∩ t j).nonempty :=
begin
contrapose! H,
apply sum_measure_le_measure_univ h,
exact λ i hi j hj hij x hx, H i hi j hj hij ⟨x, hx⟩
end
/-- Continuity from below: the measure of the union of a directed sequence of measurable sets
is the supremum of the measures. -/
lemma measure_Union_eq_supr [encodable ι] {s : ι → set α} (h : ∀ i, is_measurable (s i))
(hd : directed (⊆) s) : μ (⋃ i, s i) = ⨆ i, μ (s i) :=
begin
by_cases hι : nonempty ι, swap,
{ simp only [supr_of_empty hι, Union], exact measure_empty },
resetI,
refine le_antisymm _ (supr_le $ λ i, measure_mono $ subset_Union _ _),
have : ∀ n, is_measurable (disjointed (λ n, ⋃ b ∈ encodable.decode2 ι n, s b) n) :=
is_measurable.disjointed (is_measurable.bUnion_decode2 h),
rw [← encodable.Union_decode2, ← Union_disjointed, measure_Union disjoint_disjointed this,
ennreal.tsum_eq_supr_nat],
simp only [← measure_bUnion_finset (disjoint_disjointed.pairwise_on _) (λ n _, this n)],
refine supr_le (λ n, _),
refine le_trans (_ : _ ≤ μ (⋃ (k ∈ finset.range n) (i ∈ encodable.decode2 ι k), s i)) _,
exact measure_mono (bUnion_subset_bUnion_right (λ k hk, disjointed_subset)),
simp only [← finset.bUnion_option_to_finset, ← finset.bUnion_bind],
generalize : (finset.range n).bind (λ k, (encodable.decode2 ι k).to_finset) = t,
rcases hd.finset_le t with ⟨i, hi⟩,
exact le_supr_of_le i (measure_mono $ bUnion_subset hi)
end
lemma measure_bUnion_eq_supr {s : ι → set α} {t : set ι} (ht : countable t)
(h : ∀ i ∈ t, is_measurable (s i)) (hd : directed_on ((⊆) on s) t) :
μ (⋃ i ∈ t, s i) = ⨆ i ∈ t, μ (s i) :=
begin
haveI := ht.to_encodable,
rw [bUnion_eq_Union, measure_Union_eq_supr (set_coe.forall'.1 h) hd.directed_coe,
supr_subtype'],
refl
end
/-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable
sets is the infimum of the measures. -/
lemma measure_Inter_eq_infi [encodable ι] {s : ι → set α}
(h : ∀ i, is_measurable (s i)) (hd : directed (⊇) s)
(hfin : ∃ i, μ (s i) < ⊤) :
μ (⋂ i, s i) = (⨅ i, μ (s i)) :=
begin
rcases hfin with ⟨k, hk⟩,
rw [← ennreal.sub_sub_cancel (by exact hk) (infi_le _ k), ennreal.sub_infi,
← ennreal.sub_sub_cancel (by exact hk) (measure_mono (Inter_subset _ k)),
← measure_diff (Inter_subset _ k) (h k) (is_measurable.Inter h)
(lt_of_le_of_lt (measure_mono (Inter_subset _ k)) hk),
diff_Inter, measure_Union_eq_supr],
{ congr' 1,
refine le_antisymm (supr_le_supr2 $ λ i, _) (supr_le_supr $ λ i, _),
{ rcases hd i k with ⟨j, hji, hjk⟩,
use j,
rw [← measure_diff hjk (h _) (h _) ((measure_mono hjk).trans_lt hk)],
exact measure_mono (diff_subset_diff_right hji) },
{ rw [ennreal.sub_le_iff_le_add, ← measure_union disjoint_diff.symm ((h k).diff (h i)) (h i),
set.union_comm],
exact measure_mono (diff_subset_iff.1 $ subset.refl _) } },
{ exact λ i, (h k).diff (h i) },
{ exact hd.mono_comp _ (λ _ _, diff_subset_diff_right) }
end
lemma measure_eq_inter_diff (hs : is_measurable s) (ht : is_measurable t) :
μ s = μ (s ∩ t) + μ (s \ t) :=
have hd : disjoint (s ∩ t) (s \ t) := assume a ⟨⟨_, hs⟩, _, hns⟩, hns hs ,
by rw [← measure_union hd (hs.inter ht) (hs.diff ht), inter_union_diff s t]
lemma measure_union_add_inter (hs : is_measurable s) (ht : is_measurable t) :
μ (s ∪ t) + μ (s ∩ t) = μ s + μ t :=
by { rw [measure_eq_inter_diff (hs.union ht) ht, set.union_inter_cancel_right,
union_diff_right, measure_eq_inter_diff hs ht], ac_refl }
/-- Continuity from below: the measure of the union of an increasing sequence of measurable sets
is the limit of the measures. -/
lemma tendsto_measure_Union {s : ℕ → set α} (hs : ∀ n, is_measurable (s n)) (hm : monotone s) :
tendsto (μ ∘ s) at_top (𝓝 (μ (⋃ n, s n))) :=
begin
rw measure_Union_eq_supr hs (directed_of_sup hm),
exact tendsto_at_top_supr (assume n m hnm, measure_mono $ hm hnm)
end
/-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable
sets is the limit of the measures. -/
lemma tendsto_measure_Inter {s : ℕ → set α}
(hs : ∀ n, is_measurable (s n)) (hm : ∀ ⦃n m⦄, n ≤ m → s m ⊆ s n) (hf : ∃ i, μ (s i) < ⊤) :
tendsto (μ ∘ s) at_top (𝓝 (μ (⋂ n, s n))) :=
begin
rw measure_Inter_eq_infi hs (directed_of_sup hm) hf,
exact tendsto_at_top_infi (assume n m hnm, measure_mono $ hm hnm),
end
/-- One direction of the Borel-Cantelli lemma: if (sᵢ) is a sequence of measurable sets such that
∑ μ sᵢ exists, then the limit superior of the sᵢ is a null set. -/
lemma measure_limsup_eq_zero {s : ℕ → set α} (hs : ∀ i, is_measurable (s i))
(hs' : (∑' i, μ (s i)) ≠ ⊤) : μ (limsup at_top s) = 0 :=
begin
rw limsup_eq_infi_supr_of_nat',
-- We will show that both `μ (⨅ n, ⨆ i, s (i + n))` and `0` are the limit of `μ (⊔ i, s (i + n))`
-- as `n` tends to infinity. For the former, we use continuity from above.
refine tendsto_nhds_unique
(tendsto_measure_Inter (λ i, is_measurable.Union (λ b, hs (b + i))) _
⟨0, lt_of_le_of_lt (measure_Union_le s) (ennreal.lt_top_iff_ne_top.2 hs')⟩) _,
{ intros n m hnm x,
simp only [set.mem_Union],
exact λ ⟨i, hi⟩, ⟨i + (m - n), by simpa only [add_assoc, nat.sub_add_cancel hnm] using hi⟩ },
{ -- For the latter, notice that, `μ (⨆ i, s (i + n)) ≤ ∑' s (i + n)`. Since the right hand side
-- converges to `0` by hypothesis, so does the former and the proof is complete.
exact (tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds
(ennreal.tendsto_sum_nat_add (μ ∘ s) hs')
(eventually_of_forall (by simp only [forall_const, zero_le]))
(eventually_of_forall (λ i, measure_Union_le _))) }
end
lemma measure_if {x : β} {t : set β} {s : set α} {μ : measure α} :
μ (if x ∈ t then s else ∅) = indicator t (λ _, μ s) x :=
by { split_ifs; simp [h] }
end
section outer_measure
variables [ms : measurable_space α] {s t : set α}
include ms
/-- Obtain a measure by giving an outer measure where all sets in the σ-algebra are
Carathéodory measurable. -/
def outer_measure.to_measure (m : outer_measure α) (h : ms ≤ m.caratheodory) : measure α :=
measure.of_measurable (λ s _, m s) m.empty
(λ f hf hd, m.Union_eq_of_caratheodory (λ i, h _ (hf i)) hd)
lemma le_to_outer_measure_caratheodory (μ : measure α) : ms ≤ μ.to_outer_measure.caratheodory :=
begin
assume s hs,
rw to_outer_measure_eq_induced_outer_measure,
refine outer_measure.of_function_caratheodory (λ t, le_infi $ λ ht, _),
rw [← measure_eq_extend (ht.inter hs),
← measure_eq_extend (ht.diff hs),
← measure_union _ (ht.inter hs) (ht.diff hs),
inter_union_diff],
exact le_refl _,
exact λ x ⟨⟨_, h₁⟩, _, h₂⟩, h₂ h₁
end
@[simp] lemma to_measure_to_outer_measure (m : outer_measure α) (h : ms ≤ m.caratheodory) :
(m.to_measure h).to_outer_measure = m.trim := rfl
@[simp] lemma to_measure_apply (m : outer_measure α) (h : ms ≤ m.caratheodory)
{s : set α} (hs : is_measurable s) : m.to_measure h s = m s :=
m.trim_eq hs
lemma le_to_measure_apply (m : outer_measure α) (h : ms ≤ m.caratheodory) (s : set α) :
m s ≤ m.to_measure h s :=
m.le_trim s
@[simp] lemma to_outer_measure_to_measure {μ : measure α} :
μ.to_outer_measure.to_measure (le_to_outer_measure_caratheodory _) = μ :=
measure.ext $ λ s, μ.to_outer_measure.trim_eq
end outer_measure
variables [measurable_space α] [measurable_space β] [measurable_space γ]
variables {μ μ₁ μ₂ ν ν' ν₁ ν₂ : measure α} {s s' t : set α}
namespace measure
protected lemma caratheodory (μ : measure α) (hs : is_measurable s) : μ (t ∩ s) + μ (t \ s) = μ t :=
(le_to_outer_measure_caratheodory μ s hs t).symm
/-! ### The `ennreal`-module of measures -/
instance : has_zero (measure α) :=
⟨{ to_outer_measure := 0,
m_Union := λ f hf hd, tsum_zero.symm,
trimmed := outer_measure.trim_zero }⟩
@[simp] theorem zero_to_outer_measure : (0 : measure α).to_outer_measure = 0 := rfl
@[simp, norm_cast] theorem coe_zero : ⇑(0 : measure α) = 0 := rfl
lemma eq_zero_of_not_nonempty (h : ¬nonempty α) (μ : measure α) : μ = 0 :=
ext $ λ s hs, by simp only [eq_empty_of_not_nonempty h s, measure_empty]
instance : inhabited (measure α) := ⟨0⟩
instance : has_add (measure α) :=
⟨λ μ₁ μ₂, {
to_outer_measure := μ₁.to_outer_measure + μ₂.to_outer_measure,
m_Union := λ s hs hd,
show μ₁ (⋃ i, s i) + μ₂ (⋃ i, s i) = ∑' i, μ₁ (s i) + μ₂ (s i),
by rw [ennreal.tsum_add, measure_Union hd hs, measure_Union hd hs],
trimmed := by rw [outer_measure.trim_add, μ₁.trimmed, μ₂.trimmed] }⟩
@[simp] theorem add_to_outer_measure (μ₁ μ₂ : measure α) :
(μ₁ + μ₂).to_outer_measure = μ₁.to_outer_measure + μ₂.to_outer_measure := rfl
@[simp, norm_cast] theorem coe_add (μ₁ μ₂ : measure α) : ⇑(μ₁ + μ₂) = μ₁ + μ₂ := rfl
theorem add_apply (μ₁ μ₂ : measure α) (s : set α) : (μ₁ + μ₂) s = μ₁ s + μ₂ s := rfl
instance add_comm_monoid : add_comm_monoid (measure α) :=
to_outer_measure_injective.add_comm_monoid to_outer_measure zero_to_outer_measure
add_to_outer_measure
instance : has_scalar ennreal (measure α) :=
⟨λ c μ,
{ to_outer_measure := c • μ.to_outer_measure,
m_Union := λ s hs hd, by simp [measure_Union, *, ennreal.tsum_mul_left],
trimmed := by rw [outer_measure.trim_smul, μ.trimmed] }⟩
@[simp] theorem smul_to_outer_measure (c : ennreal) (μ : measure α) :
(c • μ).to_outer_measure = c • μ.to_outer_measure :=
rfl
@[simp, norm_cast] theorem coe_smul (c : ennreal) (μ : measure α) : ⇑(c • μ) = c • μ :=
rfl
theorem smul_apply (c : ennreal) (μ : measure α) (s : set α) : (c • μ) s = c * μ s :=
rfl
instance : semimodule ennreal (measure α) :=
injective.semimodule ennreal ⟨to_outer_measure, zero_to_outer_measure, add_to_outer_measure⟩
to_outer_measure_injective smul_to_outer_measure
/-! ### The complete lattice of measures -/
instance : partial_order (measure α) :=
{ le := λ m₁ m₂, ∀ s, is_measurable s → m₁ s ≤ m₂ s,
le_refl := assume m s hs, le_refl _,
le_trans := assume m₁ m₂ m₃ h₁ h₂ s hs, le_trans (h₁ s hs) (h₂ s hs),
le_antisymm := assume m₁ m₂ h₁ h₂, ext $
assume s hs, le_antisymm (h₁ s hs) (h₂ s hs) }
theorem le_iff : μ₁ ≤ μ₂ ↔ ∀ s, is_measurable s → μ₁ s ≤ μ₂ s := iff.rfl
theorem to_outer_measure_le : μ₁.to_outer_measure ≤ μ₂.to_outer_measure ↔ μ₁ ≤ μ₂ :=
by rw [← μ₂.trimmed, outer_measure.le_trim_iff]; refl
theorem le_iff' : μ₁ ≤ μ₂ ↔ ∀ s, μ₁ s ≤ μ₂ s :=
to_outer_measure_le.symm
theorem lt_iff : μ < ν ↔ μ ≤ ν ∧ ∃ s, is_measurable s ∧ μ s < ν s :=
lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff, not_forall, not_le, exists_prop]
theorem lt_iff' : μ < ν ↔ μ ≤ ν ∧ ∃ s, μ s < ν s :=
lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff', not_forall, not_le]
-- TODO: add typeclasses for `∀ c, monotone ((*) c)` and `∀ c, monotone ((+) c)`
protected lemma add_le_add_left (ν : measure α) (hμ : μ₁ ≤ μ₂) : ν + μ₁ ≤ ν + μ₂ :=
λ s hs, add_le_add_left (hμ s hs) _
protected lemma add_le_add_right (hμ : μ₁ ≤ μ₂) (ν : measure α) : μ₁ + ν ≤ μ₂ + ν :=
λ s hs, add_le_add_right (hμ s hs) _
protected lemma add_le_add (hμ : μ₁ ≤ μ₂) (hν : ν₁ ≤ ν₂) :
μ₁ + ν₁ ≤ μ₂ + ν₂ :=
λ s hs, add_le_add (hμ s hs) (hν s hs)
protected lemma le_add_left (h : μ ≤ ν) : μ ≤ ν' + ν :=
λ s hs, le_add_left (h s hs)
protected lemma le_add_right (h : μ ≤ ν) : μ ≤ ν + ν' :=
λ s hs, le_add_right (h s hs)
section Inf
variables {m : set (measure α)}
lemma Inf_caratheodory (s : set α) (hs : is_measurable s) :
(Inf (to_outer_measure '' m)).caratheodory.is_measurable' s :=
begin
rw [outer_measure.Inf_eq_bounded_by_Inf_gen],
refine outer_measure.bounded_by_caratheodory (λ t, _),
simp only [outer_measure.Inf_gen, le_infi_iff, ball_image_iff, coe_to_outer_measure,
measure_eq_infi t],
intros μ hμ u htu hu,
have hm : ∀ {s t}, s ⊆ t → outer_measure.Inf_gen (to_outer_measure '' m) s ≤ μ t,
{ intros s t hst,
rw [outer_measure.Inf_gen_def],
refine infi_le_of_le (μ.to_outer_measure) (infi_le_of_le (mem_image_of_mem _ hμ) _),
rw [to_outer_measure_apply],
refine measure_mono hst },
rw [measure_eq_inter_diff hu hs],
refine add_le_add (hm $ inter_subset_inter_left _ htu) (hm $ diff_subset_diff_left htu)
end
instance : has_Inf (measure α) :=
⟨λ m, (Inf (to_outer_measure '' m)).to_measure $ Inf_caratheodory⟩
lemma Inf_apply (hs : is_measurable s) : Inf m s = Inf (to_outer_measure '' m) s :=
to_measure_apply _ _ hs
private lemma measure_Inf_le (h : μ ∈ m) : Inf m ≤ μ :=
have Inf (to_outer_measure '' m) ≤ μ.to_outer_measure := Inf_le (mem_image_of_mem _ h),
assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s
private lemma measure_le_Inf (h : ∀ μ' ∈ m, μ ≤ μ') : μ ≤ Inf m :=
have μ.to_outer_measure ≤ Inf (to_outer_measure '' m) :=
le_Inf $ ball_image_of_ball $ assume μ hμ, to_outer_measure_le.2 $ h _ hμ,
assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s
instance : complete_lattice (measure α) :=
{ bot := 0,
bot_le := assume a s hs, by exact bot_le,
/- Adding an explicit `top` makes `leanchecker` fail, see lean#364, disable for now
top := (⊤ : outer_measure α).to_measure (by rw [outer_measure.top_caratheodory]; exact le_top),
le_top := assume a s hs,
by cases s.eq_empty_or_nonempty with h h;
simp [h, to_measure_apply ⊤ _ hs, outer_measure.top_apply],
-/
.. complete_lattice_of_Inf (measure α) (λ ms, ⟨λ _, measure_Inf_le, λ _, measure_le_Inf⟩) }
end Inf
protected lemma zero_le (μ : measure α) : 0 ≤ μ := bot_le
lemma le_zero_iff_eq' : μ ≤ 0 ↔ μ = 0 :=
μ.zero_le.le_iff_eq
@[simp] lemma measure_univ_eq_zero : μ univ = 0 ↔ μ = 0 :=
⟨λ h, bot_unique $ λ s hs, trans_rel_left (≤) (measure_mono (subset_univ s)) h, λ h, h.symm ▸ rfl⟩
/-! ### Pushforward and pullback -/
/-- Lift a linear map between `outer_measure` spaces such that for each measure `μ` every measurable
set is caratheodory-measurable w.r.t. `f μ` to a linear map between `measure` spaces. -/
def lift_linear (f : outer_measure α →ₗ[ennreal] outer_measure β)
(hf : ∀ μ : measure α, ‹_› ≤ (f μ.to_outer_measure).caratheodory) :
measure α →ₗ[ennreal] measure β :=
{ to_fun := λ μ, (f μ.to_outer_measure).to_measure (hf μ),
map_add' := λ μ₁ μ₂, ext $ λ s hs, by simp [hs],
map_smul' := λ c μ, ext $ λ s hs, by simp [hs] }
@[simp] lemma lift_linear_apply {f : outer_measure α →ₗ[ennreal] outer_measure β} (hf)
{s : set β} (hs : is_measurable s) : lift_linear f hf μ s = f μ.to_outer_measure s :=
to_measure_apply _ _ hs
lemma le_lift_linear_apply {f : outer_measure α →ₗ[ennreal] outer_measure β} (hf) (s : set β) :
f μ.to_outer_measure s ≤ lift_linear f hf μ s :=
le_to_measure_apply _ _ s
/-- The pushforward of a measure. It is defined to be `0` if `f` is not a measurable function. -/
def map (f : α → β) : measure α →ₗ[ennreal] measure β :=
if hf : measurable f then
lift_linear (outer_measure.map f) $ λ μ s hs t,
le_to_outer_measure_caratheodory μ _ (hf hs) (f ⁻¹' t)
else 0
/-- We can evaluate the pushforward on measurable sets. For non-measurable sets, see
`measure_theory.measure.le_map_apply` and `measurable_equiv.map_apply`. -/
@[simp] theorem map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : is_measurable s) :
map f μ s = μ (f ⁻¹' s) :=
by simp [map, dif_pos hf, hs]
@[simp] lemma map_id : map id μ = μ :=
ext $ λ s, map_apply measurable_id
lemma map_map {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) :
map g (map f μ) = map (g ∘ f) μ :=
ext $ λ s hs,
by simp [hf, hg, hs, hg hs, hg.comp hf, ← preimage_comp]
lemma map_mono {f : α → β} (hf : measurable f) (h : μ ≤ ν) : map f μ ≤ map f ν :=
λ s hs, by simp [hf, hs, h _ (hf hs)]
/-- Even if `s` is not measurable, we can bound `map f μ s` from below.
See also `measurable_equiv.map_apply`. -/
theorem le_map_apply {f : α → β} (hf : measurable f) (s : set β) : μ (f ⁻¹' s) ≤ map f μ s :=
begin
rw [measure_eq_infi' (map f μ)], refine le_infi _, rintro ⟨t, hst, ht⟩,
convert measure_mono (preimage_mono hst),
exact map_apply hf ht
end
/-- Pullback of a `measure`. If `f` sends each `measurable` set to a `measurable` set, then for each
measurable set `s` we have `comap f μ s = μ (f '' s)`. -/
def comap (f : α → β) : measure β →ₗ[ennreal] measure α :=
if hf : injective f ∧ ∀ s, is_measurable s → is_measurable (f '' s) then
lift_linear (outer_measure.comap f) $ λ μ s hs t,
begin
simp only [coe_to_outer_measure, outer_measure.comap_apply, ← image_inter hf.1,
image_diff hf.1],
apply le_to_outer_measure_caratheodory,
exact hf.2 s hs
end
else 0
lemma comap_apply (f : α → β) (hfi : injective f)
(hf : ∀ s, is_measurable s → is_measurable (f '' s)) (μ : measure β) (hs : is_measurable s) :
comap f μ s = μ (f '' s) :=
begin
rw [comap, dif_pos, lift_linear_apply _ hs, outer_measure.comap_apply, coe_to_outer_measure],
exact ⟨hfi, hf⟩
end
/-! ### Restricting a measure -/
/-- Restrict a measure `μ` to a set `s` as an `ennreal`-linear map. -/
def restrictₗ (s : set α) : measure α →ₗ[ennreal] measure α :=
lift_linear (outer_measure.restrict s) $ λ μ s' hs' t,
begin
suffices : μ (s ∩ t) = μ (s ∩ t ∩ s') + μ (s ∩ t \ s'),
{ simpa [← set.inter_assoc, set.inter_comm _ s, ← inter_diff_assoc] },
exact le_to_outer_measure_caratheodory _ _ hs' _,
end
/-- Restrict a measure `μ` to a set `s`. -/
def restrict (μ : measure α) (s : set α) : measure α := restrictₗ s μ
@[simp] lemma restrictₗ_apply (s : set α) (μ : measure α) :
restrictₗ s μ = μ.restrict s :=
rfl
@[simp] lemma restrict_apply (ht : is_measurable t) : μ.restrict s t = μ (t ∩ s) :=
by simp [← restrictₗ_apply, restrictₗ, ht]
lemma restrict_apply_univ (s : set α) : μ.restrict s univ = μ s :=
by rw [restrict_apply is_measurable.univ, set.univ_inter]
lemma le_restrict_apply (s t : set α) :
μ (t ∩ s) ≤ μ.restrict s t :=
by { rw [restrict, restrictₗ], convert le_lift_linear_apply _ t, simp }
@[simp] lemma restrict_add (μ ν : measure α) (s : set α) :
(μ + ν).restrict s = μ.restrict s + ν.restrict s :=
(restrictₗ s).map_add μ ν
@[simp] lemma restrict_zero (s : set α) : (0 : measure α).restrict s = 0 :=
(restrictₗ s).map_zero
@[simp] lemma restrict_smul (c : ennreal) (μ : measure α) (s : set α) :
(c • μ).restrict s = c • μ.restrict s :=
(restrictₗ s).map_smul c μ
@[simp] lemma restrict_restrict (hs : is_measurable s) :
(μ.restrict t).restrict s = μ.restrict (s ∩ t) :=
ext $ λ u hu, by simp [*, set.inter_assoc]
lemma restrict_apply_eq_zero (ht : is_measurable t) : μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 :=
by rw [restrict_apply ht]
lemma restrict_apply_eq_zero' (hs : is_measurable s) : μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 :=
begin
refine ⟨λ h, le_zero_iff_eq.1 (h ▸ le_restrict_apply _ _), λ h, _⟩,
rcases exists_is_measurable_superset_of_measure_eq_zero h with ⟨t', htt', ht', ht'0⟩,
apply measure_mono_null ((inter_subset _ _ _).1 htt'),
rw [restrict_apply (hs.compl.union ht'), union_inter_distrib_right, compl_inter_self,
set.empty_union],
exact measure_mono_null (inter_subset_left _ _) ht'0
end
@[simp] lemma restrict_eq_zero : μ.restrict s = 0 ↔ μ s = 0 :=
by rw [← measure_univ_eq_zero, restrict_apply_univ]
@[simp] lemma restrict_empty : μ.restrict ∅ = 0 := ext $ λ s hs, by simp [hs]
@[simp] lemma restrict_univ : μ.restrict univ = μ := ext $ λ s hs, by simp [hs]
lemma restrict_union_apply (h : disjoint (t ∩ s) (t ∩ s')) (hs : is_measurable s)
(hs' : is_measurable s') (ht : is_measurable t) :
μ.restrict (s ∪ s') t = μ.restrict s t + μ.restrict s' t :=
begin
simp only [restrict_apply, ht, set.inter_union_distrib_left],
exact measure_union h (ht.inter hs) (ht.inter hs'),
end
lemma restrict_union (h : disjoint s t) (hs : is_measurable s) (ht : is_measurable t) :
μ.restrict (s ∪ t) = μ.restrict s + μ.restrict t :=
ext $ λ t' ht', restrict_union_apply (h.mono inf_le_right inf_le_right) hs ht ht'
lemma restrict_union_add_inter (hs : is_measurable s) (ht : is_measurable t) :
μ.restrict (s ∪ t) + μ.restrict (s ∩ t) = μ.restrict s + μ.restrict t :=
begin
ext1 u hu,
simp only [add_apply, restrict_apply hu, inter_union_distrib_left],
convert measure_union_add_inter (hu.inter hs) (hu.inter ht) using 3,
rw [set.inter_left_comm (u ∩ s), set.inter_assoc, ← set.inter_assoc u u, set.inter_self]
end
@[simp] lemma restrict_add_restrict_compl (hs : is_measurable s) :
μ.restrict s + μ.restrict sᶜ = μ :=
by rw [← restrict_union disjoint_compl_right hs hs.compl, union_compl_self, restrict_univ]
@[simp] lemma restrict_compl_add_restrict (hs : is_measurable s) :
μ.restrict sᶜ + μ.restrict s = μ :=
by rw [add_comm, restrict_add_restrict_compl hs]
lemma restrict_union_le (s s' : set α) : μ.restrict (s ∪ s') ≤ μ.restrict s + μ.restrict s' :=
begin
intros t ht,
suffices : μ (t ∩ s ∪ t ∩ s') ≤ μ (t ∩ s) + μ (t ∩ s'),
by simpa [ht, inter_union_distrib_left],
apply measure_union_le
end
lemma restrict_Union_apply [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s))
(hm : ∀ i, is_measurable (s i)) {t : set α} (ht : is_measurable t) :
μ.restrict (⋃ i, s i) t = ∑' i, μ.restrict (s i) t :=
begin
simp only [restrict_apply, ht, inter_Union],
exact measure_Union (λ i j hij, (hd i j hij).mono inf_le_right inf_le_right)
(λ i, ht.inter (hm i))
end
lemma restrict_Union_apply_eq_supr [encodable ι] {s : ι → set α}
(hm : ∀ i, is_measurable (s i)) (hd : directed (⊆) s) {t : set α} (ht : is_measurable t) :
μ.restrict (⋃ i, s i) t = ⨆ i, μ.restrict (s i) t :=
begin
simp only [restrict_apply ht, inter_Union],
rw [measure_Union_eq_supr],
exacts [λ i, ht.inter (hm i), hd.mono_comp _ (λ s₁ s₂, inter_subset_inter_right _)]
end
lemma restrict_map {f : α → β} (hf : measurable f) {s : set β} (hs : is_measurable s) :
(map f μ).restrict s = map f (μ.restrict $ f ⁻¹' s) :=
ext $ λ t ht, by simp [*, hf ht]
lemma map_comap_subtype_coe (hs : is_measurable s) :
(map (coe : s → α)).comp (comap coe) = restrictₗ s :=
linear_map.ext $ λ μ, ext $ λ t ht,
by rw [restrictₗ_apply, restrict_apply ht, linear_map.comp_apply,
map_apply measurable_subtype_coe ht,
comap_apply (coe : s → α) subtype.val_injective (λ _, hs.subtype_image) _
(measurable_subtype_coe ht), subtype.image_preimage_coe]
/-- Restriction of a measure to a subset is monotone both in set and in measure. -/
@[mono] lemma restrict_mono ⦃s s' : set α⦄ (hs : s ⊆ s') ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) :
μ.restrict s ≤ ν.restrict s' :=
assume t ht,
calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht
... ≤ μ (t ∩ s') : measure_mono $ inter_subset_inter_right _ hs
... ≤ ν (t ∩ s') : le_iff'.1 hμν (t ∩ s')
... = ν.restrict s' t : (restrict_apply ht).symm
lemma restrict_le_self : μ.restrict s ≤ μ :=
assume t ht,
calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht
... ≤ μ t : measure_mono $ inter_subset_left t s
lemma restrict_congr_meas (hs : is_measurable s) :
μ.restrict s = ν.restrict s ↔ ∀ t ⊆ s, is_measurable t → μ t = ν t :=
⟨λ H t hts ht,
by rw [← inter_eq_self_of_subset_left hts, ← restrict_apply ht, H, restrict_apply ht],
λ H, ext $ λ t ht,
by rw [restrict_apply ht, restrict_apply ht, H _ (inter_subset_right _ _) (ht.inter hs)]⟩
lemma restrict_congr_mono (hs : s ⊆ t) (hm : is_measurable s) (h : μ.restrict t = ν.restrict t) :
μ.restrict s = ν.restrict s :=
by rw [← inter_eq_self_of_subset_left hs, ← restrict_restrict hm, h, restrict_restrict hm]
/-- If two measures agree on all measurable subsets of `s` and `t`, then they agree on all
measurable subsets of `s ∪ t`. -/
lemma restrict_union_congr (hsm : is_measurable s) (htm : is_measurable t) :
μ.restrict (s ∪ t) = ν.restrict (s ∪ t) ↔
μ.restrict s = ν.restrict s ∧ μ.restrict t = ν.restrict t :=
begin
refine ⟨λ h, ⟨restrict_congr_mono (subset_union_left _ _) hsm h,
restrict_congr_mono (subset_union_right _ _) htm h⟩, _⟩,
simp only [restrict_congr_meas, hsm, htm, hsm.union htm],
rintros ⟨hs, ht⟩ u hu hum,
rw [measure_eq_inter_diff hum hsm, measure_eq_inter_diff hum hsm,
hs _ (inter_subset_right _ _) (hum.inter hsm),
ht _ (diff_subset_iff.2 hu) (hum.diff hsm)]
end
lemma restrict_finset_bUnion_congr {s : finset ι} {t : ι → set α}
(htm : ∀ i ∈ s, is_measurable (t i)) :
μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔
∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) :=
begin
induction s using finset.induction_on with i s hi hs, { simp },
simp only [finset.mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at htm ⊢,
simp only [finset.bUnion_insert, ← hs htm.2],
exact restrict_union_congr htm.1 (s.is_measurable_bUnion htm.2)
end
lemma restrict_Union_congr [encodable ι] {s : ι → set α} (hm : ∀ i, is_measurable (s i)) :
μ.restrict (⋃ i, s i) = ν.restrict (⋃ i, s i) ↔
∀ i, μ.restrict (s i) = ν.restrict (s i) :=
begin
refine ⟨λ h i, restrict_congr_mono (subset_Union _ _) (hm i) h, λ h, _⟩,
ext1 t ht,
have M : ∀ t : finset ι, is_measurable (⋃ i ∈ t, s i) :=
λ t, t.is_measurable_bUnion (λ i _, hm i),
have D : directed (⊆) (λ t : finset ι, ⋃ i ∈ t, s i) :=
directed_of_sup (λ t₁ t₂ ht, bUnion_subset_bUnion_left ht),
rw [Union_eq_Union_finset],
simp only [restrict_Union_apply_eq_supr M D ht,
(restrict_finset_bUnion_congr (λ i hi, hm i)).2 (λ i hi, h i)],
end
lemma restrict_bUnion_congr {s : set ι} {t : ι → set α} (hc : countable s)
(htm : ∀ i ∈ s, is_measurable (t i)) :
μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔
∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) :=
begin
simp only [bUnion_eq_Union, set_coe.forall'] at htm ⊢,
haveI := hc.to_encodable,
exact restrict_Union_congr htm
end
lemma restrict_sUnion_congr {S : set (set α)} (hc : countable S) (hm : ∀ s ∈ S, is_measurable s) :
μ.restrict (⋃₀ S) = ν.restrict (⋃₀ S) ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s :=
by rw [sUnion_eq_bUnion, restrict_bUnion_congr hc hm]
/-- This lemma shows that `restrict` and `to_outer_measure` commute. Note that the LHS has a
restrict on measures and the RHS has a restrict on outer measures. -/
lemma restrict_to_outer_measure_eq_to_outer_measure_restrict (h : is_measurable s) :
(μ.restrict s).to_outer_measure = outer_measure.restrict s μ.to_outer_measure :=
by simp_rw [restrict, restrictₗ, lift_linear, linear_map.coe_mk, to_measure_to_outer_measure,
outer_measure.restrict_trim h, μ.trimmed]
/-- This lemma shows that `Inf` and `restrict` commute for measures. -/
lemma restrict_Inf_eq_Inf_restrict {m : set (measure α)} (hm : m.nonempty) (ht : is_measurable t) :
(Inf m).restrict t = Inf ((λ μ : measure α, μ.restrict t) '' m) :=
begin
ext1 s hs,
simp_rw [Inf_apply hs, restrict_apply hs, Inf_apply (is_measurable.inter hs ht), set.image_image,
restrict_to_outer_measure_eq_to_outer_measure_restrict ht, ← set.image_image _ to_outer_measure,
← outer_measure.restrict_Inf_eq_Inf_restrict _ (hm.image _),
outer_measure.restrict_apply]
end
/-! ### Extensionality results -/
/-- Two measures are equal if they have equal restrictions on a spanning collection of sets
(formulated using `Union`). -/
lemma ext_iff_of_Union_eq_univ [encodable ι] {s : ι → set α}
(hm : ∀ i, is_measurable (s i)) (hs : (⋃ i, s i) = univ) :
μ = ν ↔ ∀ i, μ.restrict (s i) = ν.restrict (s i) :=
by rw [← restrict_Union_congr hm, hs, restrict_univ, restrict_univ]
alias ext_iff_of_Union_eq_univ ↔ _ measure_theory.measure.ext_of_Union_eq_univ
/-- Two measures are equal if they have equal restrictions on a spanning collection of sets
(formulated using `bUnion`). -/
lemma ext_iff_of_bUnion_eq_univ {S : set ι} {s : ι → set α} (hc : countable S)
(hm : ∀ i ∈ S, is_measurable (s i)) (hs : (⋃ i ∈ S, s i) = univ) :
μ = ν ↔ ∀ i ∈ S, μ.restrict (s i) = ν.restrict (s i) :=
by rw [← restrict_bUnion_congr hc hm, hs, restrict_univ, restrict_univ]
alias ext_iff_of_bUnion_eq_univ ↔ _ measure_theory.measure.ext_of_bUnion_eq_univ
/-- Two measures are equal if they have equal restrictions on a spanning collection of sets
(formulated using `sUnion`). -/
lemma ext_iff_of_sUnion_eq_univ {S : set (set α)} (hc : countable S)
(hm : ∀ s ∈ S, is_measurable s) (hs : (⋃₀ S) = univ) :
μ = ν ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s :=
ext_iff_of_bUnion_eq_univ hc hm $ by rwa ← sUnion_eq_bUnion
alias ext_iff_of_sUnion_eq_univ ↔ _ measure_theory.measure.ext_of_sUnion_eq_univ
lemma ext_of_generate_from_of_cover {S T : set (set α)}
(h_gen : ‹_› = generate_from S) (hc : countable T)
(h_inter : is_pi_system S)
(hm : ∀ t ∈ T, is_measurable t) (hU : ⋃₀ T = univ) (htop : ∀ t ∈ T, μ t < ⊤)
(ST_eq : ∀ (t ∈ T) (s ∈ S), μ (s ∩ t) = ν (s ∩ t)) (T_eq : ∀ t ∈ T, μ t = ν t) :
μ = ν :=
begin
refine ext_of_sUnion_eq_univ hc hm hU (λ t ht, _),
ext1 u hu,
simp only [restrict_apply hu],
refine induction_on_inter h_gen h_inter _ (ST_eq t ht) _ _ hu,
{ simp only [set.empty_inter, measure_empty] },
{ intros v hv hvt,
have := T_eq t ht,
rw [set.inter_comm] at hvt ⊢,
rwa [measure_eq_inter_diff (hm _ ht) hv, measure_eq_inter_diff (hm _ ht) hv, ← hvt,
ennreal.add_right_inj] at this,
exact (measure_mono $ set.inter_subset_left _ _).trans_lt (htop t ht) },
{ intros f hfd hfm h_eq,
have : pairwise (disjoint on λ n, f n ∩ t) :=
λ m n hmn, (hfd m n hmn).mono (inter_subset_left _ _) (inter_subset_left _ _),
simp only [Union_inter, measure_Union this (λ n, is_measurable.inter (hfm n) (hm t ht)), h_eq] }
end
/-- Two measures are equal if they are equal on the π-system generating the σ-algebra,
and they are both finite on a increasing spanning sequence of sets in the π-system.
This lemma is formulated using `sUnion`. -/
lemma ext_of_generate_from_of_cover_subset {S T : set (set α)}
(h_gen : ‹_› = generate_from S)
(h_inter : is_pi_system S)
(h_sub : T ⊆ S) (hc : countable T) (hU : ⋃₀ T = univ) (htop : ∀ s ∈ T, μ s < ⊤)
(h_eq : ∀ s ∈ S, μ s = ν s) :
μ = ν :=
begin
refine ext_of_generate_from_of_cover h_gen hc h_inter _ hU htop _ (λ t ht, h_eq t (h_sub ht)),
{ intros t ht, rw [h_gen], exact generate_measurable.basic _ (h_sub ht) },
{ intros t ht s hs, cases (s ∩ t).eq_empty_or_nonempty with H H,
{ simp only [H, measure_empty] },
{ exact h_eq _ (h_inter _ _ hs (h_sub ht) H) } }
end
/-- Two measures are equal if they are equal on the π-system generating the σ-algebra,
and they are both finite on a increasing spanning sequence of sets in the π-system.
This lemma is formulated using `Union`.
`finite_spanning_sets_in.ext` is a reformulation of this lemma. -/
lemma ext_of_generate_from_of_Union (C : set (set α)) (B : ℕ → set α)
(hA : ‹_› = generate_from C) (hC : is_pi_system C) (h1B : (⋃ i, B i) = univ)
(h2B : ∀ i, B i ∈ C) (hμB : ∀ i, μ (B i) < ⊤) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν :=
begin
refine ext_of_generate_from_of_cover_subset hA hC _ (countable_range B) h1B _ h_eq,
{ rintro _ ⟨i, rfl⟩, apply h2B },
{ rintro _ ⟨i, rfl⟩, apply hμB }
end
/-- The dirac measure. -/
def dirac (a : α) : measure α :=
(outer_measure.dirac a).to_measure (by simp)
lemma dirac_apply' (a : α) (hs : is_measurable s) :
dirac a s = ⨆ h : a ∈ s, 1 :=
to_measure_apply _ _ hs
@[simp] lemma dirac_apply (a : α) (hs : is_measurable s) :
dirac a s = s.indicator 1 a :=
(dirac_apply' a hs).trans $ by { by_cases h : a ∈ s; simp [h] }
lemma dirac_apply_of_mem {a : α} (h : a ∈ s) :
dirac a s = 1 :=
begin
rw [measure_eq_infi, infi_subtype', infi_subtype'],
convert infi_const,
{ ext1 ⟨⟨t, hst⟩, ht⟩,
dsimp only [subtype.coe_mk] at *,
simp only [dirac_apply _ ht, indicator_of_mem (hst h), pi.one_apply] },
{ exact ⟨⟨⟨set.univ, subset_univ _⟩, is_measurable.univ⟩⟩ }
end
/-- Sum of an indexed family of measures. -/
def sum (f : ι → measure α) : measure α :=
(outer_measure.sum (λ i, (f i).to_outer_measure)).to_measure $
le_trans
(by exact le_infi (λ i, le_to_outer_measure_caratheodory _))
(outer_measure.le_sum_caratheodory _)
@[simp] lemma sum_apply (f : ι → measure α) {s : set α} (hs : is_measurable s) :
sum f s = ∑' i, f i s :=
to_measure_apply _ _ hs
lemma le_sum (μ : ι → measure α) (i : ι) : μ i ≤ sum μ :=
λ s hs, by simp only [sum_apply μ hs, ennreal.le_tsum i]
lemma restrict_Union [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s))
(hm : ∀ i, is_measurable (s i)) :
μ.restrict (⋃ i, s i) = sum (λ i, μ.restrict (s i)) :=
ext $ λ t ht, by simp only [sum_apply _ ht, restrict_Union_apply hd hm ht]
lemma restrict_Union_le [encodable ι] {s : ι → set α} :
μ.restrict (⋃ i, s i) ≤ sum (λ i, μ.restrict (s i)) :=
begin
intros t ht,
suffices : μ (⋃ i, t ∩ s i) ≤ ∑' i, μ (t ∩ s i), by simpa [ht, inter_Union],
apply measure_Union_le
end
@[simp] lemma sum_bool (f : bool → measure α) : sum f = f tt + f ff :=
ext $ λ s hs, by simp [hs, tsum_fintype]
@[simp] lemma sum_cond (μ ν : measure α) : sum (λ b, cond b μ ν) = μ + ν :=
sum_bool _
@[simp] lemma restrict_sum (μ : ι → measure α) {s : set α} (hs : is_measurable s) :
(sum μ).restrict s = sum (λ i, (μ i).restrict s) :=
ext $ λ t ht, by simp only [sum_apply, restrict_apply, ht, ht.inter hs]
/-- Counting measure on any measurable space. -/
def count : measure α := sum dirac
lemma count_apply (hs : is_measurable s) : count s = ∑' i : s, 1 :=
by simp only [count, sum_apply, hs, dirac_apply, ← tsum_subtype s 1, pi.one_apply]
@[simp] lemma count_apply_finset [measurable_singleton_class α] (s : finset α) :
count (↑s : set α) = s.card :=
calc count (↑s : set α) = ∑' i : (↑s : set α), (1 : α → ennreal) i : count_apply s.is_measurable
... = ∑ i in s, 1 : s.tsum_subtype 1
... = s.card : by simp
lemma count_apply_finite [measurable_singleton_class α] (s : set α) (hs : finite s) :
count s = hs.to_finset.card :=
by rw [← count_apply_finset, finite.coe_to_finset]
/-- `count` measure evaluates to infinity at infinite sets. -/
lemma count_apply_infinite [measurable_singleton_class α] (hs : s.infinite) : count s = ⊤ :=
begin
by_contra H,
rcases ennreal.exists_nat_gt H with ⟨n, hn⟩,
rcases hs.exists_subset_card_eq n with ⟨t, ht, rfl⟩,
have := lt_of_le_of_lt (measure_mono ht) hn,
simpa [lt_irrefl] using this
end
@[simp] lemma count_apply_eq_top [measurable_singleton_class α] : count s = ⊤ ↔ s.infinite :=
begin
by_cases hs : s.finite,
{ simp [set.infinite, hs, count_apply_finite] },
{ change s.infinite at hs,
simp [hs, count_apply_infinite] }
end
@[simp] lemma count_apply_lt_top [measurable_singleton_class α] : count s < ⊤ ↔ s.finite :=
calc count s < ⊤ ↔ count s ≠ ⊤ : lt_top_iff_ne_top
... ↔ ¬s.infinite : not_congr count_apply_eq_top
... ↔ s.finite : not_not
/-! ### The almost everywhere filter -/
/-- The “almost everywhere” filter of co-null sets. -/
def ae (μ : measure α) : filter α :=
{ sets := {s | μ sᶜ = 0},
univ_sets := by simp,
inter_sets := λ s t hs ht, by simp only [compl_inter, mem_set_of_eq];
exact measure_union_null hs ht,
sets_of_superset := λ s t hs hst, measure_mono_null (set.compl_subset_compl.2 hst) hs }
/-- The filter of sets `s` such that `sᶜ` has finite measure. -/
def cofinite (μ : measure α) : filter α :=
{ sets := {s | μ sᶜ < ⊤},
univ_sets := by simp,
inter_sets := λ s t hs ht, by { simp only [compl_inter, mem_set_of_eq],
calc μ (sᶜ ∪ tᶜ) ≤ μ sᶜ + μ tᶜ : measure_union_le _ _
... < ⊤ : ennreal.add_lt_top.2 ⟨hs, ht⟩ },
sets_of_superset := λ s t hs hst, lt_of_le_of_lt (measure_mono $ compl_subset_compl.2 hst) hs }
lemma mem_cofinite : s ∈ μ.cofinite ↔ μ sᶜ < ⊤ := iff.rfl
lemma compl_mem_cofinite : sᶜ ∈ μ.cofinite ↔ μ s < ⊤ :=
by rw [mem_cofinite, compl_compl]
lemma eventually_cofinite {p : α → Prop} : (∀ᶠ x in μ.cofinite, p x) ↔ μ {x | ¬p x} < ⊤ := iff.rfl
end measure
open measure
notation `∀ᵐ` binders ` ∂` μ `, ` r:(scoped P, filter.eventually P (measure.ae μ)) := r
notation f ` =ᵐ[`:50 μ:50 `] `:0 g:50 := f =ᶠ[measure.ae μ] g
notation f ` ≤ᵐ[`:50 μ:50 `] `:0 g:50 := f ≤ᶠ[measure.ae μ] g
lemma mem_ae_iff {s : set α} : s ∈ μ.ae ↔ μ sᶜ = 0 := iff.rfl
lemma ae_iff {p : α → Prop} : (∀ᵐ a ∂ μ, p a) ↔ μ { a | ¬ p a } = 0 := iff.rfl
lemma compl_mem_ae_iff {s : set α} : sᶜ ∈ μ.ae ↔ μ s = 0 := by simp only [mem_ae_iff, compl_compl]
lemma measure_zero_iff_ae_nmem {s : set α} : μ s = 0 ↔ ∀ᵐ a ∂ μ, a ∉ s :=
compl_mem_ae_iff.symm
@[simp] lemma ae_eq_bot : μ.ae = ⊥ ↔ μ = 0 :=
by rw [← empty_in_sets_eq_bot, mem_ae_iff, compl_empty, measure_univ_eq_zero]
lemma ae_of_all {p : α → Prop} (μ : measure α) : (∀ a, p a) → ∀ᵐ a ∂ μ, p a :=
eventually_of_forall
@[mono] lemma ae_mono {μ ν : measure α} (h : μ ≤ ν) : μ.ae ≤ ν.ae :=
λ s hs, bot_unique $ trans_rel_left (≤) (measure.le_iff'.1 h _) hs
instance : countable_Inter_filter μ.ae :=
⟨begin
intros S hSc hS,
simp only [mem_ae_iff, compl_sInter, sUnion_image, bUnion_eq_Union] at hS ⊢,
haveI := hSc.to_encodable,
exact measure_Union_null (subtype.forall.2 hS)
end⟩
instance ae_is_measurably_generated : is_measurably_generated μ.ae :=
⟨λ s hs, let ⟨t, hst, htm, htμ⟩ := exists_is_measurable_superset_of_measure_eq_zero hs in
⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩
lemma ae_all_iff [encodable ι] {p : α → ι → Prop} :
(∀ᵐ a ∂ μ, ∀ i, p a i) ↔ (∀ i, ∀ᵐ a ∂ μ, p a i) :=
eventually_countable_forall
lemma ae_ball_iff {S : set ι} (hS : countable S) {p : Π (x : α) (i ∈ S), Prop} :
(∀ᵐ x ∂ μ, ∀ i ∈ S, p x i ‹_›) ↔ ∀ i ∈ S, ∀ᵐ x ∂ μ, p x i ‹_› :=
eventually_countable_ball hS
lemma ae_eq_refl (f : α → δ) : f =ᵐ[μ] f := eventually_eq.refl _ _
lemma ae_eq_symm {f g : α → δ} (h : f =ᵐ[μ] g) : g =ᵐ[μ] f :=
h.symm
lemma ae_eq_trans {f g h: α → δ} (h₁ : f =ᵐ[μ] g) (h₂ : g =ᵐ[μ] h) :
f =ᵐ[μ] h :=
h₁.trans h₂
lemma ae_eq_empty : s =ᵐ[μ] (∅ : set α) ↔ μ s = 0 :=
eventually_eq_empty.trans $ by simp [ae_iff]
lemma ae_le_set : s ≤ᵐ[μ] t ↔ μ (s \ t) = 0 :=
calc s ≤ᵐ[μ] t ↔ ∀ᵐ x ∂μ, x ∈ s → x ∈ t : iff.rfl
... ↔ μ (s \ t) = 0 : by simp [ae_iff]; refl
lemma union_ae_eq_right : (s ∪ t : set α) =ᵐ[μ] t ↔ μ (s \ t) = 0 :=
by simp [eventually_le_antisymm_iff, ae_le_set, union_diff_right,
diff_eq_empty.2 (set.subset_union_right _ _)]
lemma diff_ae_eq_self : (s \ t : set α) =ᵐ[μ] s ↔ μ (s ∩ t) = 0 :=
by simp [eventually_le_antisymm_iff, ae_le_set, diff_diff_right,
diff_diff, diff_eq_empty.2 (set.subset_union_right _ _)]
lemma ae_eq_set {s t : set α} :
s =ᵐ[μ] t ↔ μ (s \ t) = 0 ∧ μ (t \ s) = 0 :=
by simp [eventually_le_antisymm_iff, ae_le_set]
lemma mem_ae_map_iff {f : α → β} (hf : measurable f) {s : set β} (hs : is_measurable s) :
s ∈ (map f μ).ae ↔ (f ⁻¹' s) ∈ μ.ae :=
by simp only [mem_ae_iff, map_apply hf hs.compl, preimage_compl]
lemma ae_map_iff {f : α → β} (hf : measurable f) {p : β → Prop} (hp : is_measurable {x | p x}) :
(∀ᵐ y ∂ (map f μ), p y) ↔ ∀ᵐ x ∂ μ, p (f x) :=
mem_ae_map_iff hf hp
lemma ae_restrict_iff {p : α → Prop} (hp : is_measurable {x | p x}) :
(∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x :=
begin
simp only [ae_iff, ← compl_set_of, restrict_apply hp.compl],
congr' with x, simp [and_comm]
end
lemma ae_restrict_iff' {s : set α} {p : α → Prop} (hp : is_measurable s) :
(∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x :=
begin
simp only [ae_iff, ← compl_set_of, restrict_apply_eq_zero' hp],
congr' with x, simp [and_comm]
end
lemma ae_smul_measure {p : α → Prop} (h : ∀ᵐ x ∂μ, p x) (c : ennreal) : ∀ᵐ x ∂(c • μ), p x :=
ae_iff.2 $ by rw [smul_apply, ae_iff.1 h, mul_zero]
lemma ae_smul_measure_iff {p : α → Prop} {c : ennreal} (hc : c ≠ 0) :
(∀ᵐ x ∂(c • μ), p x) ↔ ∀ᵐ x ∂μ, p x :=
by simp [ae_iff, hc]
lemma ae_add_measure_iff {p : α → Prop} {ν} : (∀ᵐ x ∂μ + ν, p x) ↔ (∀ᵐ x ∂μ, p x) ∧ ∀ᵐ x ∂ν, p x :=
add_eq_zero_iff
lemma ae_eq_comp {f : α → β} {g g' : β → δ} (hf : measurable f)
(h : g =ᵐ[measure.map f μ] g') : g ∘ f =ᵐ[μ] g' ∘ f :=
begin
rcases exists_is_measurable_superset_of_measure_eq_zero h with ⟨t, ht, tmeas, tzero⟩,
refine le_antisymm _ bot_le,
calc μ {x | g (f x) ≠ g' (f x)} ≤ μ (f⁻¹' t) : measure_mono (λ x hx, ht hx)
... = 0 : by rwa ← measure.map_apply hf tmeas
end
@[simp] lemma ae_restrict_eq (hs : is_measurable s) : (μ.restrict s).ae = μ.ae ⊓ 𝓟 s :=
begin
ext t,
simp only [mem_inf_principal, mem_ae_iff, restrict_apply_eq_zero' hs, compl_set_of,
not_imp, and_comm (_ ∈ s)],
refl
end
@[simp] lemma ae_restrict_eq_bot {s} : (μ.restrict s).ae = ⊥ ↔ μ s = 0 :=
ae_eq_bot.trans restrict_eq_zero
@[simp] lemma ae_restrict_ne_bot {s} : (μ.restrict s).ae.ne_bot ↔ 0 < μ s :=
(not_congr ae_restrict_eq_bot).trans zero_lt_iff_ne_zero.symm
/-- A version of the Borel-Cantelli lemma: if sᵢ is a sequence of measurable sets such that
∑ μ sᵢ exists, then for almost all x, x does not belong to almost all sᵢ. -/
lemma ae_eventually_not_mem {s : ℕ → set α} (hs : ∀ i, is_measurable (s i))
(hs' : (∑' i, μ (s i)) ≠ ⊤) : ∀ᵐ x ∂ μ, ∀ᶠ n in at_top, x ∉ s n :=
begin
refine measure_mono_null _ (measure_limsup_eq_zero hs hs'),
rw ←set.le_eq_subset,
refine le_Inf (λ t ht x hx, _),
simp only [le_eq_subset, not_exists, eventually_map, exists_prop, ge_iff_le, mem_set_of_eq,
eventually_at_top, mem_compl_eq, not_forall, not_not_mem] at hx ht,
rcases ht with ⟨i, hi⟩,
rcases hx i with ⟨j, ⟨hj, hj'⟩⟩,
exact hi j hj hj'
end
lemma mem_dirac_ae_iff {a : α} (hs : is_measurable s) : s ∈ (dirac a).ae ↔ a ∈ s :=
by by_cases a ∈ s; simp [mem_ae_iff, dirac_apply, hs.compl, indicator_apply, *]
lemma eventually_dirac {a : α} {p : α → Prop} (hp : is_measurable {x | p x}) :
(∀ᵐ x ∂(dirac a), p x) ↔ p a :=
mem_dirac_ae_iff hp
lemma eventually_eq_dirac [measurable_singleton_class β] {a : α} {f : α → β} (hf : measurable f) :
f =ᵐ[dirac a] const α (f a) :=
(eventually_dirac $ show is_measurable (f ⁻¹' {f a}), from hf $ is_measurable_singleton _).2 rfl
lemma dirac_ae_eq [measurable_singleton_class α] (a : α) : (dirac a).ae = pure a :=
begin
ext s,
simp only [mem_ae_iff, mem_pure_sets],
by_cases ha : a ∈ s,
{ simp only [ha, iff_true],
rw [← set.singleton_subset_iff, ← compl_subset_compl] at ha,
refine measure_mono_null ha _,
simp [dirac_apply a (is_measurable_singleton a).compl] },
{ simp only [ha, iff_false, dirac_apply_of_mem (mem_compl ha)],
exact one_ne_zero }
end
lemma eventually_eq_dirac' [measurable_singleton_class α] {a : α} (f : α → δ) :
f =ᵐ[dirac a] const α (f a) :=
by { rw [dirac_ae_eq], show f a = f a, refl }
/-- If `s ⊆ t` modulo a set of measure `0`, then `μ s ≤ μ t`. -/
lemma measure_mono_ae (H : s ≤ᵐ[μ] t) : μ s ≤ μ t :=
calc μ s ≤ μ (s ∪ t) : measure_mono $ subset_union_left s t
... = μ (t ∪ s \ t) : by rw [union_diff_self, set.union_comm]
... ≤ μ t + μ (s \ t) : measure_union_le _ _
... = μ t : by rw [ae_le_set.1 H, add_zero]
alias measure_mono_ae ← filter.eventually_le.measure_le
/-- If two sets are equal modulo a set of measure zero, then `μ s = μ t`. -/
lemma measure_congr (H : s =ᵐ[μ] t) : μ s = μ t :=
le_antisymm H.le.measure_le H.symm.le.measure_le
lemma restrict_mono_ae (h : s ≤ᵐ[μ] t) : μ.restrict s ≤ μ.restrict t :=
begin
intros u hu,
simp only [restrict_apply hu],
exact measure_mono_ae (h.mono $ λ x hx, and.imp id hx)
end
lemma restrict_congr_set (H : s =ᵐ[μ] t) : μ.restrict s = μ.restrict t :=
le_antisymm (restrict_mono_ae H.le) (restrict_mono_ae H.symm.le)
/-- A measure `μ` is called a probability measure if `μ univ = 1`. -/
class probability_measure (μ : measure α) : Prop := (measure_univ : μ univ = 1)
instance measure.dirac.probability_measure {x : α} : probability_measure (dirac x) :=
⟨dirac_apply_of_mem $ mem_univ x⟩
/-- A measure `μ` is called finite if `μ univ < ⊤`. -/
class finite_measure (μ : measure α) : Prop := (measure_univ_lt_top : μ univ < ⊤)
instance restrict.finite_measure (μ : measure α) [hs : fact (μ s < ⊤)] :
finite_measure (μ.restrict s) :=
⟨by simp [hs.elim]⟩
/-- Measure `μ` *has no atoms* if the measure of each singleton is zero.
NB: Wikipedia assumes that for any measurable set `s` with positive `μ`-measure,
there exists a measurable `t ⊆ s` such that `0 < μ t < μ s`. While this implies `μ {x} = 0`,
the converse is not true. -/
class has_no_atoms (μ : measure α) : Prop :=
(measure_singleton : ∀ x, μ {x} = 0)
export probability_measure (measure_univ) has_no_atoms (measure_singleton)
attribute [simp] measure_singleton
lemma measure_lt_top (μ : measure α) [finite_measure μ] (s : set α) : μ s < ⊤ :=
(measure_mono (subset_univ s)).trans_lt finite_measure.measure_univ_lt_top
lemma measure_ne_top (μ : measure α) [finite_measure μ] (s : set α) : μ s ≠ ⊤ :=
ne_of_lt (measure_lt_top μ s)
/-- `le_of_add_le_add_left` is normally applicable to `ordered_cancel_add_comm_monoid`,
but it holds for measures with the additional assumption that μ is finite. -/
lemma measure.le_of_add_le_add_left {μ ν₁ ν₂ : measure α} [finite_measure μ]
(A2 : μ + ν₁ ≤ μ + ν₂) : ν₁ ≤ ν₂ :=
λ S B1, ennreal.le_of_add_le_add_left (measure_theory.measure_lt_top μ S) (A2 S B1)
@[priority 100]
instance probability_measure.to_finite_measure (μ : measure α) [probability_measure μ] :
finite_measure μ :=
⟨by simp only [measure_univ, ennreal.one_lt_top]⟩
lemma probability_measure.ne_zero (μ : measure α) [probability_measure μ] : μ ≠ 0 :=
mt measure_univ_eq_zero.2 $ by simp [measure_univ]
section no_atoms
variables [has_no_atoms μ]
lemma measure_countable (h : countable s) : μ s = 0 :=
begin
rw [← bUnion_of_singleton s, ← le_zero_iff_eq],
refine le_trans (measure_bUnion_le h _) _,
simp
end
lemma measure_finite (h : s.finite) : μ s = 0 :=
measure_countable h.countable
lemma measure_finset (s : finset α) : μ ↑s = 0 :=
measure_finite s.finite_to_set
lemma insert_ae_eq_self (a : α) (s : set α) :
(insert a s : set α) =ᵐ[μ] s :=
union_ae_eq_right.2 $ measure_mono_null (diff_subset _ _) (measure_singleton _)
variables [partial_order α] {a b : α}
lemma Iio_ae_eq_Iic : Iio a =ᵐ[μ] Iic a :=
by simp only [← Iic_diff_right, diff_ae_eq_self,
measure_mono_null (set.inter_subset_right _ _) (measure_singleton a)]
lemma Ioi_ae_eq_Ici : Ioi a =ᵐ[μ] Ici a :=
@Iio_ae_eq_Iic (order_dual α) ‹_› ‹_› _ _ _
lemma Ioo_ae_eq_Ioc : Ioo a b =ᵐ[μ] Ioc a b :=
(ae_eq_refl _).inter Iio_ae_eq_Iic
lemma Ioc_ae_eq_Icc : Ioc a b =ᵐ[μ] Icc a b :=
Ioi_ae_eq_Ici.inter (ae_eq_refl _)
lemma Ioo_ae_eq_Ico : Ioo a b =ᵐ[μ] Ico a b :=
Ioi_ae_eq_Ici.inter (ae_eq_refl _)
lemma Ioo_ae_eq_Icc : Ioo a b =ᵐ[μ] Icc a b :=
Ioi_ae_eq_Ici.inter Iio_ae_eq_Iic
lemma Ico_ae_eq_Icc : Ico a b =ᵐ[μ] Icc a b :=
(ae_eq_refl _).inter Iio_ae_eq_Iic
lemma Ico_ae_eq_Ioc : Ico a b =ᵐ[μ] Ioc a b :=
Ioo_ae_eq_Ico.symm.trans Ioo_ae_eq_Ioc
end no_atoms
namespace measure
/-- A measure is called finite at filter `f` if it is finite at some set `s ∈ f`.
Equivalently, it is eventually finite at `s` in `f.lift' powerset`. -/
def finite_at_filter (μ : measure α) (f : filter α) : Prop := ∃ s ∈ f, μ s < ⊤
lemma finite_at_filter_of_finite (μ : measure α) [finite_measure μ] (f : filter α) :
μ.finite_at_filter f :=
⟨univ, univ_mem_sets, measure_lt_top μ univ⟩
lemma finite_at_filter.exists_mem_basis {μ : measure α} {f : filter α} (hμ : finite_at_filter μ f)
{p : ι → Prop} {s : ι → set α} (hf : f.has_basis p s) :
∃ i (hi : p i), μ (s i) < ⊤ :=
(hf.exists_iff (λ s t hst ht, (measure_mono hst).trans_lt ht)).1 hμ
lemma finite_at_bot (μ : measure α) : μ.finite_at_filter ⊥ :=
⟨∅, mem_bot_sets, by simp only [measure_empty, with_top.zero_lt_top]⟩
/-- `μ` has finite spanning sets in `C` if there is a countable sequence of sets in `C` that have
finite measures. This structure is a type, which is useful if we want to record extra properties
about the sets, such as that they are monotone.
`sigma_finite` is defined in terms of this: `μ` is σ-finite if there exists a sequence of
finite spanning sets in the collection of all measurable sets. -/
@[protect_proj, nolint has_inhabited_instance]
structure finite_spanning_sets_in (μ : measure α) (C : set (set α)) :=
(set : ℕ → set α)
(set_mem : ∀ i, set i ∈ C)
(finite : ∀ i, μ (set i) < ⊤)
(spanning : (⋃ i, set i) = univ)
end measure
open measure
/-- A measure `μ` is called σ-finite if there is a countable collection of sets
`{ A i | i ∈ ℕ }` such that `μ (A i) < ⊤` and `⋃ i, A i = s`. -/
@[class] def sigma_finite (μ : measure α) : Prop :=
nonempty (μ.finite_spanning_sets_in {s | is_measurable s})
/-- If `μ` is σ-finite it has finite spanning sets in the collection of all measurable sets. -/
def measure.to_finite_spanning_sets_in (μ : measure α) [h : sigma_finite μ] :
μ.finite_spanning_sets_in {s | is_measurable s} :=
classical.choice h
/-- A noncomputable way to get a monotone collection of sets that span `univ` and have finite
measure using `classical.some`. This definition satisfies monotonicity in addition to all other
properties in `sigma_finite`. -/
def spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) : set α :=
accumulate μ.to_finite_spanning_sets_in.set i
lemma monotone_spanning_sets (μ : measure α) [sigma_finite μ] :
monotone (spanning_sets μ) :=
monotone_accumulate
lemma is_measurable_spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) :
is_measurable (spanning_sets μ i) :=
is_measurable.Union $ λ j, is_measurable.Union_Prop $
λ hij, μ.to_finite_spanning_sets_in.set_mem j
lemma measure_spanning_sets_lt_top (μ : measure α) [sigma_finite μ] (i : ℕ) :
μ (spanning_sets μ i) < ⊤ :=
measure_bUnion_lt_top (finite_le_nat i) $ λ j _, μ.to_finite_spanning_sets_in.finite j
lemma Union_spanning_sets (μ : measure α) [sigma_finite μ] :
(⋃ i : ℕ, spanning_sets μ i) = univ :=
by simp_rw [spanning_sets, Union_accumulate, μ.to_finite_spanning_sets_in.spanning]
lemma is_countably_spanning_spanning_sets (μ : measure α) [sigma_finite μ] :
is_countably_spanning (range (spanning_sets μ)) :=
⟨spanning_sets μ, mem_range_self, Union_spanning_sets μ⟩
namespace measure
lemma supr_restrict_spanning_sets [sigma_finite μ] (hs : is_measurable s) :
(⨆ i, μ.restrict (spanning_sets μ i) s) = μ s :=
begin
convert (restrict_Union_apply_eq_supr (is_measurable_spanning_sets μ) _ hs).symm,
{ simp [Union_spanning_sets] },
{ exact directed_of_sup (monotone_spanning_sets μ) }
end
namespace finite_spanning_sets_in
variables {C D : set (set α)}
/-- If `μ` has finite spanning sets in `C` and `C ⊆ D` then `μ` has finite spanning sets in `D`. -/
protected def mono (h : μ.finite_spanning_sets_in C) (hC : C ⊆ D) : μ.finite_spanning_sets_in D :=
⟨h.set, λ i, hC (h.set_mem i), h.finite, h.spanning⟩
/-- If `μ` has finite spanning sets in the collection of measurable sets `C`, then `μ` is σ-finite.
-/
protected lemma sigma_finite (h : μ.finite_spanning_sets_in C) (hC : ∀ s ∈ C, is_measurable s) :
sigma_finite μ :=
⟨h.mono hC⟩
/-- An extensionality for measures. It is `ext_of_generate_from_of_Union` formulated in terms of
`finite_spanning_sets_in`. -/
protected lemma ext {ν : measure α} {C : set (set α)} (hA : ‹_› = generate_from C)
(hC : is_pi_system C) (h : μ.finite_spanning_sets_in C) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν :=
ext_of_generate_from_of_Union C _ hA hC h.spanning h.set_mem h.finite h_eq
protected lemma is_countably_spanning (h : μ.finite_spanning_sets_in C) : is_countably_spanning C :=
⟨_, h.set_mem, h.spanning⟩
end finite_spanning_sets_in
lemma sigma_finite_of_not_nonempty (μ : measure α) (hα : ¬ nonempty α) : sigma_finite μ :=
⟨⟨λ _, ∅, λ n, is_measurable.empty, λ n, by simp, by simp [eq_empty_of_not_nonempty hα univ]⟩⟩
lemma sigma_finite_of_countable {S : set (set α)} (hc : countable S)
(hm : ∀ s ∈ S, is_measurable s) (hμ : ∀ s ∈ S, μ s < ⊤) (hU : ⋃₀ S = univ) :
sigma_finite μ :=
begin
by_cases hα : nonempty α,
{ resetI,
have : S.nonempty,
{ clear hc, -- otherwise `rintro rfl` fails. TODO: why?
rw ← ne_empty_iff_nonempty,
rintro rfl,
simpa [eq_comm] using hU },
rcases (countable_iff_exists_surjective_to_subtype this).1 hc with ⟨s, hs⟩,
refine ⟨⟨λ n, s n, λ n, hm _ (s n).coe_prop, λ n, hμ _ (s n).coe_prop, _⟩⟩,
rw [Union, hs.supr_comp, ← hU, sUnion_eq_Union] },
{ exact sigma_finite_of_not_nonempty μ hα }
end
end measure
/-- Every finite measure is σ-finite. -/
@[priority 100]
instance finite_measure.to_sigma_finite (μ : measure α) [finite_measure μ] : sigma_finite μ :=
⟨⟨λ _, univ, λ _, is_measurable.univ, λ _, measure_lt_top μ _, Union_const _⟩⟩
instance restrict.sigma_finite (μ : measure α) [sigma_finite μ] (s : set α) :
sigma_finite (μ.restrict s) :=
begin
refine ⟨⟨spanning_sets μ, is_measurable_spanning_sets μ, λ i, _, Union_spanning_sets μ⟩⟩,
rw [restrict_apply (is_measurable_spanning_sets μ i)],
exact (measure_mono $ inter_subset_left _ _).trans_lt (measure_spanning_sets_lt_top μ i)
end
instance sum.sigma_finite {ι} [fintype ι] (μ : ι → measure α) [∀ i, sigma_finite (μ i)] :
sigma_finite (sum μ) :=
begin
haveI : encodable ι := (encodable.trunc_encodable_of_fintype ι).out,
have : ∀ n, is_measurable (⋂ (i : ι), spanning_sets (μ i) n) :=
λ n, is_measurable.Inter (λ i, is_measurable_spanning_sets (μ i) n),
refine ⟨⟨λ n, ⋂ i, spanning_sets (μ i) n, this, λ n, _, _⟩⟩,
{ rw [sum_apply _ (this n), tsum_fintype, ennreal.sum_lt_top_iff],
rintro i -,
exact (measure_mono $ Inter_subset _ i).trans_lt (measure_spanning_sets_lt_top (μ i) n) },
{ rw [Union_Inter_of_monotone], simp_rw [Union_spanning_sets, Inter_univ],
exact λ i, monotone_spanning_sets (μ i), }
end
instance add.sigma_finite (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] :
sigma_finite (μ + ν) :=
by { rw [← sum_cond], refine @sum.sigma_finite _ _ _ _ _ (bool.rec _ _); simpa }
/-- A measure is called locally finite if it is finite in some neighborhood of each point. -/
class locally_finite_measure [topological_space α] (μ : measure α) : Prop :=
(finite_at_nhds : ∀ x, μ.finite_at_filter (𝓝 x))
@[priority 100]
instance finite_measure.to_locally_finite_measure [topological_space α] (μ : measure α)
[finite_measure μ] :
locally_finite_measure μ :=
⟨λ x, finite_at_filter_of_finite _ _⟩
lemma measure.finite_at_nhds [topological_space α] (μ : measure α)
[locally_finite_measure μ] (x : α) :
μ.finite_at_filter (𝓝 x) :=
locally_finite_measure.finite_at_nhds x
lemma measure.exists_is_open_measure_lt_top [topological_space α] (μ : measure α)
[locally_finite_measure μ] (x : α) :
∃ s : set α, x ∈ s ∧ is_open s ∧ μ s < ⊤ :=
by simpa only [exists_prop, and.assoc]
using (μ.finite_at_nhds x).exists_mem_basis (nhds_basis_opens x)
/-- Two finite measures are equal if they are equal on the π-system generating the σ-algebra
(and `univ`). -/
lemma ext_of_generate_finite (C : set (set α)) (hA : _inst_1 = generate_from C)
(hC : is_pi_system C) {μ ν : measure α}
[finite_measure μ] [finite_measure ν] (hμν : ∀ s ∈ C, μ s = ν s) (h_univ : μ univ = ν univ) :
μ = ν :=
begin
ext1 s hs,
refine induction_on_inter hA hC (by simp) hμν _ _ hs,
{ rintros t h1t h2t, change is_measurable t at h1t, simp [measure_compl, measure_lt_top, *] },
{ rintros f h1f h2f h3f, simp [measure_Union, is_measurable.Union, *] }
end
namespace measure
namespace finite_at_filter
variables {f g : filter α}
lemma filter_mono (h : f ≤ g) : μ.finite_at_filter g → μ.finite_at_filter f :=
λ ⟨s, hs, hμ⟩, ⟨s, h hs, hμ⟩
lemma inf_of_left (h : μ.finite_at_filter f) : μ.finite_at_filter (f ⊓ g) :=
h.filter_mono inf_le_left
lemma inf_of_right (h : μ.finite_at_filter g) : μ.finite_at_filter (f ⊓ g) :=
h.filter_mono inf_le_right
@[simp] lemma inf_ae_iff : μ.finite_at_filter (f ⊓ μ.ae) ↔ μ.finite_at_filter f :=
begin
refine ⟨_, λ h, h.filter_mono inf_le_left⟩,
rintros ⟨s, ⟨t, ht, u, hu, hs⟩, hμ⟩,
suffices : μ t ≤ μ s, from ⟨t, ht, this.trans_lt hμ⟩,
exact measure_mono_ae (mem_sets_of_superset hu (λ x hu ht, hs ⟨ht, hu⟩))
end
alias inf_ae_iff ↔ measure_theory.measure.finite_at_filter.of_inf_ae _
lemma filter_mono_ae (h : f ⊓ μ.ae ≤ g) (hg : μ.finite_at_filter g) : μ.finite_at_filter f :=
inf_ae_iff.1 (hg.filter_mono h)
protected lemma measure_mono (h : μ ≤ ν) : ν.finite_at_filter f → μ.finite_at_filter f :=
λ ⟨s, hs, hν⟩, ⟨s, hs, (measure.le_iff'.1 h s).trans_lt hν⟩
@[mono] protected lemma mono (hf : f ≤ g) (hμ : μ ≤ ν) :
ν.finite_at_filter g → μ.finite_at_filter f :=
λ h, (h.filter_mono hf).measure_mono hμ
protected lemma eventually (h : μ.finite_at_filter f) : ∀ᶠ s in f.lift' powerset, μ s < ⊤ :=
(eventually_lift'_powerset' $ λ s t hst ht, (measure_mono hst).trans_lt ht).2 h
lemma filter_sup : μ.finite_at_filter f → μ.finite_at_filter g → μ.finite_at_filter (f ⊔ g) :=
λ ⟨s, hsf, hsμ⟩ ⟨t, htg, htμ⟩,
⟨s ∪ t, union_mem_sup hsf htg, (measure_union_le s t).trans_lt (ennreal.add_lt_top.2 ⟨hsμ, htμ⟩)⟩
end finite_at_filter
lemma finite_at_nhds_within [topological_space α] (μ : measure α) [locally_finite_measure μ]
(x : α) (s : set α) :
μ.finite_at_filter (𝓝[s] x) :=
(finite_at_nhds μ x).inf_of_left
@[simp] lemma finite_at_principal : μ.finite_at_filter (𝓟 s) ↔ μ s < ⊤ :=
⟨λ ⟨t, ht, hμ⟩, (measure_mono ht).trans_lt hμ, λ h, ⟨s, mem_principal_self s, h⟩⟩
/-! ### Subtraction of measures -/
/-- The measure `μ - ν` is defined to be the least measure `τ` such that `μ ≤ τ + ν`.
It is the equivalent of `(μ - ν) ⊔ 0` if `μ` and `ν` were signed measures.
Compare with `ennreal.has_sub`.
Specifically, note that if you have `α = {1,2}`, and `μ {1} = 2`, `μ {2} = 0`, and
`ν {2} = 2`, `ν {1} = 0`, then `(μ - ν) {1, 2} = 2`. However, if `μ ≤ ν`, and
`ν univ ≠ ⊤`, then `(μ - ν) + ν = μ`. -/
noncomputable instance has_sub {α : Type*} [measurable_space α] : has_sub (measure α) :=
⟨λ μ ν, Inf {τ | μ ≤ τ + ν} ⟩
section measure_sub
lemma sub_def : μ - ν = Inf {d | μ ≤ d + ν} := rfl
lemma sub_eq_zero_of_le (h : μ ≤ ν) : μ - ν = 0 :=
begin
rw [← le_zero_iff_eq', measure.sub_def],
apply @Inf_le (measure α) _ _,
simp [h],
end
/-- This application lemma only works in special circumstances. Given knowledge of
when `μ ≤ ν` and `ν ≤ μ`, a more general application lemma can be written. -/
lemma sub_apply [finite_measure ν] (h₁ : is_measurable s) (h₂ : ν ≤ μ) : (μ - ν) s = μ s - ν s :=
begin
-- We begin by defining `measure_sub`, which will be equal to `(μ - ν)`.
let measure_sub : measure α := @measure_theory.measure.of_measurable α _
(λ (t : set α) (h_t_is_measurable : is_measurable t), (μ t - ν t))
begin
simp
end
begin
intros g h_meas h_disj, simp only, rw ennreal.tsum_sub,
repeat { rw ← measure_theory.measure_Union h_disj h_meas },
apply measure_theory.measure_lt_top, intro i, apply h₂, apply h_meas
end,
-- Now, we demonstrate `μ - ν = measure_sub`, and apply it.
begin
have h_measure_sub_add : (ν + measure_sub = μ),
{ ext t h_t_is_measurable,
simp only [pi.add_apply, coe_add],
rw [measure_theory.measure.of_measurable_apply _ h_t_is_measurable, add_comm,
ennreal.sub_add_cancel_of_le (h₂ t h_t_is_measurable)] },
have h_measure_sub_eq : (μ - ν) = measure_sub,
{ rw measure_theory.measure.sub_def, apply le_antisymm,
{ apply @Inf_le (measure α) (measure.complete_lattice), simp [le_refl, add_comm, h_measure_sub_add] },
apply @le_Inf (measure α) (measure.complete_lattice),
intros d h_d, rw [← h_measure_sub_add, mem_set_of_eq, add_comm d] at h_d,
apply measure.le_of_add_le_add_left h_d },
rw h_measure_sub_eq,
apply measure.of_measurable_apply _ h₁,
end
end
lemma sub_add_cancel_of_le [finite_measure ν] (h₁ : ν ≤ μ) : μ - ν + ν = μ :=
begin
ext s h_s_meas,
rw [add_apply, sub_apply h_s_meas h₁, ennreal.sub_add_cancel_of_le (h₁ s h_s_meas)],
end
end measure_sub
end measure
end measure_theory
open measure_theory measure_theory.measure
namespace measurable_equiv
open equiv
variables [measurable_space α] [measurable_space β] {μ : measure α} {ν : measure β}
/-- If we map a measure along a measurable equivalence, we can compute the measure on all sets
(not just the measurable ones). -/
protected theorem map_apply (f : α ≃ᵐ β) (s : set β) : measure.map f μ s = μ (f ⁻¹' s) :=
begin
refine le_antisymm _ (le_map_apply f.measurable s),
rw [measure_eq_infi' μ],
refine le_infi _, rintro ⟨t, hst, ht⟩,
rw [subtype.coe_mk],
have := f.symm.to_equiv.image_eq_preimage,
simp only [←coe_eq, symm_symm, symm_to_equiv] at this,
rw [← this, image_subset_iff] at hst,
convert measure_mono hst,
rw [map_apply, preimage_preimage],
{ refine congr_arg μ (eq.symm _), convert preimage_id, exact funext f.left_inv },
exacts [f.measurable, f.measurable_inv_fun ht]
end
end measurable_equiv
section is_complete
/-- A measure is complete if every null set is also measurable.
A null set is a subset of a measurable set with measure `0`.
Since every measure is defined as a special case of an outer measure, we can more simply state
that a set `s` is null if `μ s = 0`. -/
@[class] def measure_theory.measure.is_complete {_ : measurable_space α} (μ : measure α) :
Prop :=
∀ s, μ s = 0 → is_measurable s
variables [measurable_space α] {μ : measure α} {s t z : set α}
/-- A set is null measurable if it is the union of a null set and a measurable set. -/
def is_null_measurable (μ : measure α) (s : set α) : Prop :=
∃ t z, s = t ∪ z ∧ is_measurable t ∧ μ z = 0
theorem is_null_measurable_iff : is_null_measurable μ s ↔
∃ t, t ⊆ s ∧ is_measurable t ∧ μ (s \ t) = 0 :=
begin
split,
{ rintro ⟨t, z, rfl, ht, hz⟩,
refine ⟨t, set.subset_union_left _ _, ht, measure_mono_null _ hz⟩,
simp [union_diff_left, diff_subset] },
{ rintro ⟨t, st, ht, hz⟩,
exact ⟨t, _, (union_diff_cancel st).symm, ht, hz⟩ }
end
theorem is_null_measurable_measure_eq (st : t ⊆ s) (hz : μ (s \ t) = 0) : μ s = μ t :=
begin
refine le_antisymm _ (measure_mono st),
have := measure_union_le t (s \ t),
rw [union_diff_cancel st, hz] at this, simpa
end
theorem is_measurable.is_null_measurable (μ : measure α) (hs : is_measurable s) :
is_null_measurable μ s :=
⟨s, ∅, by simp, hs, μ.empty⟩
theorem is_null_measurable_of_complete (μ : measure α) [c : μ.is_complete] :
is_null_measurable μ s ↔ is_measurable s :=
⟨by rintro ⟨t, z, rfl, ht, hz⟩; exact
is_measurable.union ht (c _ hz),
λ h, h.is_null_measurable _⟩
theorem is_null_measurable.union_null (hs : is_null_measurable μ s) (hz : μ z = 0) :
is_null_measurable μ (s ∪ z) :=
begin
rcases hs with ⟨t, z', rfl, ht, hz'⟩,
exact ⟨t, z' ∪ z, set.union_assoc _ _ _, ht, le_zero_iff_eq.1
(le_trans (measure_union_le _ _) $ by simp [hz, hz'])⟩
end
theorem null_is_null_measurable (hz : μ z = 0) : is_null_measurable μ z :=
by simpa using (is_measurable.empty.is_null_measurable _).union_null hz
theorem is_null_measurable.Union_nat {s : ℕ → set α} (hs : ∀ i, is_null_measurable μ (s i)) :
is_null_measurable μ (Union s) :=
begin
choose t ht using assume i, is_null_measurable_iff.1 (hs i),
simp [forall_and_distrib] at ht,
rcases ht with ⟨st, ht, hz⟩,
refine is_null_measurable_iff.2
⟨Union t, Union_subset_Union st, is_measurable.Union ht,
measure_mono_null _ (measure_Union_null hz)⟩,
rw [diff_subset_iff, ← Union_union_distrib],
exact Union_subset_Union (λ i, by rw ← diff_subset_iff)
end
theorem is_measurable.diff_null (hs : is_measurable s) (hz : μ z = 0) :
is_null_measurable μ (s \ z) :=
begin
rw measure_eq_infi at hz,
choose f hf using show ∀ q : {q : ℚ // q > 0}, ∃ t : set α,
z ⊆ t ∧ is_measurable t ∧ μ t < (nnreal.of_real q.1 : ennreal),
{ rintro ⟨ε, ε0⟩,
have : 0 < (nnreal.of_real ε : ennreal), { simpa using ε0 },
rw ← hz at this, simpa [infi_lt_iff] },
refine is_null_measurable_iff.2 ⟨s \ Inter f,
diff_subset_diff_right (subset_Inter (λ i, (hf i).1)),
hs.diff (is_measurable.Inter (λ i, (hf i).2.1)),
measure_mono_null _ (le_zero_iff_eq.1 $ le_of_not_lt $ λ h, _)⟩,
{ exact Inter f },
{ rw [diff_subset_iff, diff_union_self],
exact subset.trans (diff_subset _ _) (subset_union_left _ _) },
rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨ε, ε0', ε0, h⟩,
simp at ε0,
apply not_le_of_lt (lt_trans (hf ⟨ε, ε0⟩).2.2 h),
exact measure_mono (Inter_subset _ _)
end
theorem is_null_measurable.diff_null (hs : is_null_measurable μ s) (hz : μ z = 0) :
is_null_measurable μ (s \ z) :=
begin
rcases hs with ⟨t, z', rfl, ht, hz'⟩,
rw [set.union_diff_distrib],
exact (ht.diff_null hz).union_null (measure_mono_null (diff_subset _ _) hz')
end
theorem is_null_measurable.compl (hs : is_null_measurable μ s) : is_null_measurable μ sᶜ :=
begin
rcases hs with ⟨t, z, rfl, ht, hz⟩,
rw compl_union,
exact ht.compl.diff_null hz
end
theorem is_null_measurable_iff_ae {s : set α} :
is_null_measurable μ s ↔ ∃ t, is_measurable t ∧ s =ᵐ[μ] t :=
begin
simp only [ae_eq_set],
split,
{ assume h,
rcases is_null_measurable_iff.1 h with ⟨t, ts, tmeas, ht⟩,
refine ⟨t, tmeas, ht, _⟩,
rw [diff_eq_empty.2 ts, measure_empty] },
{ rintros ⟨t, tmeas, h₁, h₂⟩,
have : is_null_measurable μ (t ∪ (s \ t)) :=
is_null_measurable.union_null (tmeas.is_null_measurable _) h₁,
have A : is_null_measurable μ ((t ∪ (s \ t)) \ (t \ s)) :=
is_null_measurable.diff_null this h₂,
have : (t ∪ (s \ t)) \ (t \ s) = s,
{ apply subset.antisymm,
{ assume x hx,
simp only [mem_union_eq, not_and, mem_diff, not_not_mem] at hx,
cases hx.1, { exact hx.2 h }, { exact h.1 } },
{ assume x hx,
simp [hx, classical.em (x ∈ t)] } },
rwa this at A }
end
theorem is_null_measurable_iff_sandwich {s : set α} :
is_null_measurable μ s ↔
∃ (t u : set α), is_measurable t ∧ is_measurable u ∧ t ⊆ s ∧ s ⊆ u ∧ μ (u \ t) = 0 :=
begin
split,
{ assume h,
rcases is_null_measurable_iff.1 h with ⟨t, ts, tmeas, ht⟩,
rcases is_null_measurable_iff.1 h.compl with ⟨u', u's, u'meas, hu'⟩,
have A : s ⊆ u'ᶜ := subset_compl_comm.mp u's,
refine ⟨t, u'ᶜ, tmeas, u'meas.compl, ts, A, _⟩,
have : sᶜ \ u' = u'ᶜ \ s, by simp [compl_eq_univ_diff, diff_diff, union_comm],
rw this at hu',
apply le_antisymm _ bot_le,
calc μ (u'ᶜ \ t) ≤ μ ((u'ᶜ \ s) ∪ (s \ t)) :
begin
apply measure_mono,
assume x hx,
simp at hx,
simp [hx, or_comm, classical.em],
end
... ≤ μ (u'ᶜ \ s) + μ (s \ t) : measure_union_le _ _
... = 0 : by rw [ht, hu', zero_add] },
{ rintros ⟨t, u, tmeas, umeas, ts, su, hμ⟩,
refine is_null_measurable_iff.2 ⟨t, ts, tmeas, _⟩,
apply le_antisymm _ bot_le,
calc μ (s \ t) ≤ μ (u \ t) : measure_mono (diff_subset_diff_left su)
... = 0 : hμ }
end
lemma restrict_apply_of_is_null_measurable {s t : set α}
(ht : is_null_measurable (μ.restrict s) t) : μ.restrict s t = μ (t ∩ s) :=
begin
rcases is_null_measurable_iff_sandwich.1 ht with ⟨u, v, umeas, vmeas, ut, tv, huv⟩,
apply le_antisymm _ (le_restrict_apply _ _),
calc μ.restrict s t ≤ μ.restrict s v : measure_mono tv
... = μ (v ∩ s) : restrict_apply vmeas
... ≤ μ ((u ∩ s) ∪ ((v \ u) ∩ s)) : measure_mono $
by { assume x hx, simp at hx, simp [hx, classical.em] }
... ≤ μ (u ∩ s) + μ ((v \ u) ∩ s) : measure_union_le _ _
... = μ (u ∩ s) + μ.restrict s (v \ u) : by rw measure.restrict_apply (vmeas.diff umeas)
... = μ (u ∩ s) : by rw [huv, add_zero]
... ≤ μ (t ∩ s) : measure_mono $ inter_subset_inter_left s ut
end
/-- The measurable space of all null measurable sets. -/
def null_measurable (μ : measure α) : measurable_space α :=
{ is_measurable' := is_null_measurable μ,
is_measurable_empty := is_measurable.empty.is_null_measurable _,
is_measurable_compl := λ s hs, hs.compl,
is_measurable_Union := λ f, is_null_measurable.Union_nat }
/-- Given a measure we can complete it to a (complete) measure on all null measurable sets. -/
def completion (μ : measure α) : @measure_theory.measure α (null_measurable μ) :=
{ to_outer_measure := μ.to_outer_measure,
m_Union := λ s hs hd, show μ (Union s) = ∑' i, μ (s i), begin
choose t ht using assume i, is_null_measurable_iff.1 (hs i),
simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩,
rw is_null_measurable_measure_eq (Union_subset_Union st),
{ rw measure_Union _ ht,
{ congr, funext i,
exact (is_null_measurable_measure_eq (st i) (hz i)).symm },
{ rintro i j ij x ⟨h₁, h₂⟩,
exact hd i j ij ⟨st i h₁, st j h₂⟩ } },
{ refine measure_mono_null _ (measure_Union_null hz),
rw [diff_subset_iff, ← Union_union_distrib],
exact Union_subset_Union (λ i, by rw ← diff_subset_iff) }
end,
trimmed := begin
letI := null_measurable μ,
refine le_antisymm (λ s, _) (outer_measure.le_trim _),
rw outer_measure.trim_eq_infi,
dsimp,
clear _inst,
resetI,
rw measure_eq_infi s,
exact infi_le_infi (λ t, infi_le_infi $ λ st,
infi_le_infi2 $ λ ht, ⟨ht.is_null_measurable _, le_refl _⟩)
end }
instance completion.is_complete (μ : measure α) : (completion μ).is_complete :=
λ z hz, null_is_null_measurable hz
end is_complete
namespace measure_theory
/-- A measure space is a measurable space equipped with a
measure, referred to as `volume`. -/
class measure_space (α : Type*) extends measurable_space α :=
(volume : measure α)
export measure_space (volume)
/-- `volume` is the canonical measure on `α`. -/
add_decl_doc volume
section measure_space
variables [measure_space α] {s₁ s₂ : set α}
notation `∀ᵐ` binders `, ` r:(scoped P, filter.eventually P (measure.ae volume)) := r
/-- The tactic `exact volume`, to be used in optional (`auto_param`) arguments. -/
meta def volume_tac : tactic unit := `[exact measure_theory.measure_space.volume]
end measure_space
end measure_theory
/-!
# Almost everywhere measurable functions
A function is almost everywhere measurable if it coincides almost everywhere with a measurable
function. We define this property, called `ae_measurable f μ`, and discuss several of its properties
that are analogous to properties of measurable functions.
-/
section
open measure_theory
variables [measurable_space α] [measurable_space β]
{f g : α → β} {μ ν : measure α}
/-- A function is almost everywhere measurable if it coincides almost everywhere with a measurable
function. -/
def ae_measurable (f : α → β) (μ : measure α . measure_theory.volume_tac) : Prop :=
∃ g : α → β, measurable g ∧ f =ᵐ[μ] g
lemma measurable.ae_measurable (h : measurable f) : ae_measurable f μ :=
⟨f, h, ae_eq_refl f⟩
namespace ae_measurable
/-- Given an almost everywhere measurable function `f`, associate to it a measurable function
that coincides with it almost everywhere. `f` is explicit in the definition to make sure that
it shows in pretty-printing. -/
def mk (f : α → β) (h : ae_measurable f μ) : α → β := classical.some h
lemma measurable_mk (h : ae_measurable f μ) : measurable (h.mk f) :=
(classical.some_spec h).1
lemma ae_eq_mk (h : ae_measurable f μ) : f =ᵐ[μ] (h.mk f) :=
(classical.some_spec h).2
lemma congr (hf : ae_measurable f μ) (h : f =ᵐ[μ] g) : ae_measurable g μ :=
⟨hf.mk f, hf.measurable_mk, h.symm.trans hf.ae_eq_mk⟩
lemma mono_measure (h : ae_measurable f μ) (h' : ν ≤ μ) : ae_measurable f ν :=
⟨h.mk f, h.measurable_mk, eventually.filter_mono (ae_mono h') h.ae_eq_mk⟩
lemma add_measure {f : α → β} (hμ : ae_measurable f μ) (hν : ae_measurable f ν) :
ae_measurable f (μ + ν) :=
begin
let s := {x | f x ≠ hμ.mk f x},
have : μ s = 0 := hμ.ae_eq_mk,
obtain ⟨t, st, t_meas, μt⟩ : ∃ t, s ⊆ t ∧ is_measurable t ∧ μ t = 0 :=
exists_is_measurable_superset_of_measure_eq_zero this,
let g : α → β := t.piecewise (hν.mk f) (hμ.mk f),
refine ⟨g, measurable.piecewise t_meas hν.measurable_mk hμ.measurable_mk, _⟩,
change μ {x | f x ≠ g x} + ν {x | f x ≠ g x} = 0,
suffices : μ {x | f x ≠ g x} = 0 ∧ ν {x | f x ≠ g x} = 0, by simp [this.1, this.2],
have ht : {x | f x ≠ g x} ⊆ t,
{ assume x hx,
by_contra h,
simp only [g, h, mem_set_of_eq, ne.def, not_false_iff, piecewise_eq_of_not_mem] at hx,
exact h (st hx) },
split,
{ have : μ {x | f x ≠ g x} ≤ μ t := measure_mono ht,
rw μt at this,
exact le_antisymm this bot_le },
{ have : {x | f x ≠ g x} ⊆ {x | f x ≠ hν.mk f x},
{ assume x hx,
simpa [ht hx, g] using hx },
apply le_antisymm _ bot_le,
calc ν {x | f x ≠ g x} ≤ ν {x | f x ≠ hν.mk f x} : measure_mono this
... = 0 : hν.ae_eq_mk }
end
lemma smul_measure (h : ae_measurable f μ) (c : ennreal) :
ae_measurable f (c • μ) :=
⟨h.mk f, h.measurable_mk, ae_smul_measure h.ae_eq_mk c⟩
lemma comp_measurable [measurable_space δ] {f : α → δ} {g : δ → β}
(hg : ae_measurable g (measure.map f μ)) (hf : measurable f) : ae_measurable (g ∘ f) μ :=
⟨(hg.mk g) ∘ f, hg.measurable_mk.comp hf, ae_eq_comp hf hg.ae_eq_mk⟩
lemma prod_mk {γ : Type*} [measurable_space γ] {f : α → β} {g : α → γ}
(hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, (f x, g x)) μ :=
⟨λ a, (hf.mk f a, hg.mk g a), hf.measurable_mk.prod_mk hg.measurable_mk,
eventually_eq.prod_mk hf.ae_eq_mk hg.ae_eq_mk⟩
lemma is_null_measurable (h : ae_measurable f μ) {s : set β} (hs : is_measurable s) :
is_null_measurable μ (f ⁻¹' s) :=
begin
apply is_null_measurable_iff_ae.2,
refine ⟨(h.mk f) ⁻¹' s, h.measurable_mk hs, _⟩,
filter_upwards [h.ae_eq_mk],
assume x hx,
change (f x ∈ s) = ((h.mk f) x ∈ s),
rwa hx
end
end ae_measurable
lemma ae_measurable_congr (h : f =ᵐ[μ] g) :
ae_measurable f μ ↔ ae_measurable g μ :=
⟨λ hf, ae_measurable.congr hf h, λ hg, ae_measurable.congr hg h.symm⟩
@[simp] lemma ae_measurable_add_measure_iff :
ae_measurable f (μ + ν) ↔ ae_measurable f μ ∧ ae_measurable f ν :=
⟨λ h, ⟨h.mono_measure (measure.le_add_right (le_refl _)),
h.mono_measure (measure.le_add_left (le_refl _))⟩,
λ h, h.1.add_measure h.2⟩
@[simp] lemma ae_measurable_const {b : β} : ae_measurable (λ a : α, b) μ :=
measurable_const.ae_measurable
@[simp] lemma ae_measurable_smul_measure_iff {c : ennreal} (hc : c ≠ 0) :
ae_measurable f (c • μ) ↔ ae_measurable f μ :=
⟨λ h, ⟨h.mk f, h.measurable_mk, (ae_smul_measure_iff hc).1 h.ae_eq_mk⟩,
λ h, ⟨h.mk f, h.measurable_mk, (ae_smul_measure_iff hc).2 h.ae_eq_mk⟩⟩
lemma measurable.comp_ae_measurable [measurable_space δ] {f : α → δ} {g : δ → β}
(hg : measurable g) (hf : ae_measurable f μ) : ae_measurable (g ∘ f) μ :=
⟨g ∘ hf.mk f, hg.comp hf.measurable_mk, eventually_eq.fun_comp hf.ae_eq_mk _⟩
end
namespace is_compact
variables [topological_space α] [measurable_space α] {μ : measure α} {s : set α}
lemma finite_measure_of_nhds_within (hs : is_compact s) :
(∀ a ∈ s, μ.finite_at_filter (𝓝[s] a)) → μ s < ⊤ :=
by simpa only [← measure.compl_mem_cofinite, measure.finite_at_filter]
using hs.compl_mem_sets_of_nhds_within
lemma finite_measure [locally_finite_measure μ] (hs : is_compact s) : μ s < ⊤ :=
hs.finite_measure_of_nhds_within $ λ a ha, μ.finite_at_nhds_within _ _
lemma measure_zero_of_nhds_within (hs : is_compact s) :
(∀ a ∈ s, ∃ t ∈ 𝓝[s] a, μ t = 0) → μ s = 0 :=
by simpa only [← compl_mem_ae_iff] using hs.compl_mem_sets_of_nhds_within
end is_compact
lemma metric.bounded.finite_measure [metric_space α] [proper_space α]
[measurable_space α] {μ : measure α} [locally_finite_measure μ] {s : set α}
(hs : metric.bounded s) :
μ s < ⊤ :=
(measure_mono subset_closure).trans_lt (metric.compact_iff_closed_bounded.2
⟨is_closed_closure, metric.bounded_closure_of_bounded hs⟩).finite_measure
|
4bda55c25babfb4e3f36ba7c215563882825d9c1 | 367134ba5a65885e863bdc4507601606690974c1 | /src/topology/category/Top/limits.lean | c79d4532c59978b3826b8bca67e9e7911ea0e78b | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 7,493 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Scott Morrison, Mario Carneiro
-/
import topology.category.Top.basic
import category_theory.limits.types
import category_theory.limits.preserves.basic
/-!
# The category of topological spaces has all limits and colimits
Further, these limits and colimits are preserved by the forgetful functor --- that is, the
underlying types are just the limits in the category of types.
-/
open topological_space
open category_theory
open category_theory.limits
open opposite
universe u
noncomputable theory
namespace Top
variables {J : Type u} [small_category J]
local notation `forget` := forget Top
/--
A choice of limit cone for a functor `F : J ⥤ Top`.
Generally you should just use `limit.cone F`, unless you need the actual definition
(which is in terms of `types.limit_cone`).
-/
def limit_cone (F : J ⥤ Top.{u}) : cone F :=
{ X := ⟨(types.limit_cone (F ⋙ forget)).X, ⨅j,
(F.obj j).str.induced ((types.limit_cone (F ⋙ forget)).π.app j)⟩,
π :=
{ app := λ j, ⟨(types.limit_cone (F ⋙ forget)).π.app j,
continuous_iff_le_induced.mpr (infi_le _ _)⟩,
naturality' := λ j j' f,
continuous_map.coe_inj ((types.limit_cone (F ⋙ forget)).π.naturality f) } }
/--
The chosen cone `Top.limit_cone F` for a functor `F : J ⥤ Top` is a limit cone.
Generally you should just use `limit.is_limit F`, unless you need the actual definition
(which is in terms of `types.limit_cone_is_limit`).
-/
def limit_cone_is_limit (F : J ⥤ Top.{u}) : is_limit (limit_cone F) :=
by { refine is_limit.of_faithful forget (types.limit_cone_is_limit _) (λ s, ⟨_, _⟩) (λ s, rfl),
exact continuous_iff_coinduced_le.mpr (le_infi $ λ j,
coinduced_le_iff_le_induced.mp $ (continuous_iff_coinduced_le.mp (s.π.app j).continuous :
_) ) }
instance Top_has_limits : has_limits.{u} Top.{u} :=
{ has_limits_of_shape := λ J 𝒥, by exactI
{ has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } }
instance forget_preserves_limits : preserves_limits (forget : Top.{u} ⥤ Type u) :=
{ preserves_limits_of_shape := λ J 𝒥,
{ preserves_limit := λ F,
by exactI preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget)) } }
/--
A choice of colimit cocone for a functor `F : J ⥤ Top`.
Generally you should just use `colimit.coone F`, unless you need the actual definition
(which is in terms of `types.colimit_cocone`).
-/
def colimit_cocone (F : J ⥤ Top.{u}) : cocone F :=
{ X := ⟨(types.colimit_cocone (F ⋙ forget)).X, ⨆ j,
(F.obj j).str.coinduced ((types.colimit_cocone (F ⋙ forget)).ι.app j)⟩,
ι :=
{ app := λ j, ⟨(types.colimit_cocone (F ⋙ forget)).ι.app j,
continuous_iff_coinduced_le.mpr (le_supr _ j)⟩,
naturality' := λ j j' f,
continuous_map.coe_inj ((types.colimit_cocone (F ⋙ forget)).ι.naturality f) } }
/--
The chosen cocone `Top.colimit_cocone F` for a functor `F : J ⥤ Top` is a colimit cocone.
Generally you should just use `colimit.is_colimit F`, unless you need the actual definition
(which is in terms of `types.colimit_cocone_is_colimit`).
-/
def colimit_cocone_is_colimit (F : J ⥤ Top.{u}) : is_colimit (colimit_cocone F) :=
by { refine is_colimit.of_faithful forget (types.colimit_cocone_is_colimit _) (λ s, ⟨_, _⟩)
(λ s, rfl),
exact continuous_iff_le_induced.mpr (supr_le $ λ j,
coinduced_le_iff_le_induced.mp $ (continuous_iff_coinduced_le.mp (s.ι.app j).continuous :
_) ) }
instance Top_has_colimits : has_colimits.{u} Top.{u} :=
{ has_colimits_of_shape := λ J 𝒥, by exactI
{ has_colimit := λ F, has_colimit.mk { cocone := colimit_cocone F, is_colimit :=
colimit_cocone_is_colimit F } } }
instance forget_preserves_colimits : preserves_colimits (forget : Top.{u} ⥤ Type u) :=
{ preserves_colimits_of_shape := λ J 𝒥,
{ preserves_colimit := λ F,
by exactI preserves_colimit_of_preserves_colimit_cocone
(colimit_cocone_is_colimit F) (types.colimit_cocone_is_colimit (F ⋙ forget)) } }
end Top
namespace Top
section topological_konig
/-!
## Topological Kőnig's lemma
A topological version of Kőnig's lemma is that the inverse limit of nonempty compact Hausdorff
spaces is nonempty. (Note: this can be generalized further to inverse limits of nonempty compact
T0 spaces, where all the maps are closed maps; see [Stone1979] --- however there is an erratum
for Theorem 4 that the element in the inverse limit can have cofinally many components that are
not closed points.)
-/
variables {J : Type u} [directed_order J]
variables (F : Jᵒᵖ ⥤ Top.{u})
/--
The partial sections of an inverse system of topological spaces from an index `j` are sections
when restricted to all objects less than or equal to `j`.
-/
def partial_sections (j : Jᵒᵖ) : set (Π j, F.obj j) :=
{ u | ∀ {j'} (f : j ⟶ j'), F.map f (u j) = u j'}
lemma partial_sections.nonempty [Π (j : Jᵒᵖ), nonempty (F.obj j)] (j : Jᵒᵖ) :
(partial_sections F j).nonempty :=
begin
classical,
use λ (j' : Jᵒᵖ),
if h : j'.unop ≤ j.unop then
F.map (hom_of_le h).op (classical.arbitrary (F.obj j))
else
classical.arbitrary _,
intros j' fle,
simp only [dif_pos (le_of_hom fle.unop)],
dsimp, simp,
end
lemma partial_sections.directed : directed (⊇) (partial_sections F) :=
begin
intros j j',
obtain ⟨j'', hj''⟩ := directed_order.directed j.unop j'.unop,
use op j'',
split,
{ intros u hu j''' f''',
rw [←hu ((hom_of_le hj''.1).op ≫ f'''), ←hu],
simp only [Top.comp_app, functor.map_comp] },
{ intros u hu j''' f''',
rw [←hu ((hom_of_le hj''.2).op ≫ f'''), ←hu],
simp only [Top.comp_app, functor.map_comp] },
end
lemma partial_sections.closed [Π (j : Jᵒᵖ), t2_space (F.obj j)] (j : Jᵒᵖ) :
is_closed (partial_sections F j) :=
begin
have hps : partial_sections F j =
⋂ (f : Σ j', j ⟶ j'), {u : Π (j : Jᵒᵖ), F.obj j | F.map f.2 (u j) = u f.1},
{ ext u,
simp only [set.mem_Inter, sigma.forall, set.mem_set_of_eq],
exact ⟨λ hu j' f, hu f, λ hu j' f, hu j' f⟩ },
rw hps,
apply is_closed_Inter,
rintros ⟨j', f⟩,
let proj : Π (j' : Jᵒᵖ), C((Π (j : Jᵒᵖ), F.obj j), F.obj j') :=
λ j', ⟨λ u, u j', continuous_apply j'⟩,
exact is_closed_eq
(((F.map f).continuous.comp (proj j).continuous).comp continuous_id)
((proj j').continuous.comp continuous_id),
end
lemma nonempty_limit_cone_of_compact_t2_inverse_system
[Π (j : Jᵒᵖ), nonempty (F.obj j)]
[Π (j : Jᵒᵖ), compact_space (F.obj j)]
[Π (j : Jᵒᵖ), t2_space (F.obj j)] :
nonempty (Top.limit_cone F).X :=
begin
by_cases h : nonempty Jᵒᵖ,
{ haveI := h,
obtain ⟨u, hu⟩ := is_compact.nonempty_Inter_of_directed_nonempty_compact_closed
(partial_sections F) (partial_sections.directed F) (partial_sections.nonempty F)
(λ j, is_closed.compact (partial_sections.closed F j)) (partial_sections.closed F),
use u,
intros j j' f,
specialize hu (partial_sections F j),
simp only [forall_prop_of_true, set.mem_range_self] at hu,
exact hu f, },
{ exact ⟨⟨λ j, (h ⟨j⟩).elim, λ j, (h ⟨j⟩).elim⟩⟩, },
end
end topological_konig
end Top
|
cf028b23c7a19202db6a4228d42b82979a96ba9d | ec040be767d27b10d2f864ddcfdf756aeb7a9a0a | /src/inClassNotes/final/imp.lean | 117af2c513937f67672cd6e1b03f1a606b65a273 | [] | no_license | RoboticPanda77/complogic-s21 | b26a9680dfb98ac650e40539296c0cafc86f5cb4 | 93c5bcc0139c0926cc261075f50a8b1ead6aa40c | refs/heads/master | 1,682,196,614,558 | 1,620,625,035,000 | 1,620,625,035,000 | 337,230,148 | 0 | 0 | null | 1,620,625,036,000 | 1,612,824,240,000 | Lean | UTF-8 | Lean | false | false | 3,012 | lean | import .arith_expr
import .bool_expr
/-
Syntax of our little language (OLL).
OLL supports mutable variables and
values of types arithmetic (bool) and
arithmetic (nat). It has an assignment
command for each of these two types.
It also supports sequential composition
of smaller programs into larger ones.
-/
inductive cmd : Type
| skip
| a_assn (v : var nat) (e : arith_expr)
| b_assn (v : var bool) (e : bool_expr)
| seq (c1 c2 : cmd) : cmd
| ifelse (b : bool_expr) (c1 c2 : cmd)
| while (b : bool_expr) (c : cmd)
open cmd
notation v = e := b_assn v e
notation v = a := a_assn v a
notation c1 ; c2 := seq c1 c2
notation `IF ` b ` THEN ` c1 ` ELSE ` c2 := ifelse b c1 c2
notation `WHILE ` b ` DO ` c ` END`:= while b c
/-
Computational semantics
-/
def c_eval : cmd → env → env
| skip st := st
| (b_assn v e) st := override_bool st v e
| (a_assn v e) st := override_nat st v e
| (c1 ; c2) st := c_eval c2 (c_eval c1 st)
| (IF b THEN c1 ELSE c2) st :=
if (bool_eval b st)
then c_eval c1 st
else c_eval c2 st
| (WHILE b DO c END) st :=
if (bool_eval b st)
then c_eval (c; WHILE b DO c END) st
else st
/-
-- run c; repeat loop.
-- Unroll loop by one run of c
-- Running c can but needn't make b false
-- side effects via big shared memory (BSM)
-/
/-
Logical semantics
The rules are the axioms you can then
use to prove c_sem relations.
-/
inductive c_sem : cmd → env → env → Prop
-- c_sem skip pre pre
| c_sem_skip : ∀ (st : env),
c_sem skip st st
-- c_sem (v =a e) pre post -- arith assignment
| c_sem_arith_assn :
∀ (pre post : env) (v : var nat) (e : arith_expr),
(override_nat pre v e = post) →
c_sem (a_assn v e) pre post
-- c_sem (v =b e) pre post -- bool assignment
| c_sem_bool_assn :
∀ (pre post : env) (v : var bool) (e : bool_expr),
(override_bool pre v e = post) →
c_sem (b_assn v e) pre post
-- c_sem (c1 ; c2) pre post
| c_sem_seq :
∀ (pre is post : env) (c1 c2 : cmd),
c_sem c1 pre is →
c_sem c2 is post →
c_sem (c1 ; c2) pre post
-- c_sem if false c1 c2
| c_sem_if_false :
∀ (pre is post : env) (b : bool_expr) (c1 c2 : cmd),
bool_eval b pre = ff →
c_sem c2 pre post →
c_sem (IF b THEN c1 ELSE c2) pre post
-- c_sem if true c1 c2
| c_sem_if_true :
∀ (pre is post : env)
(b : bool_expr)
(c1 c2 : cmd),
bool_eval b pre = tt →
c_sem c1 pre post →
c_sem (IF b THEN c1 ELSE c2) pre post
-- NEW
-- c_sem (while false do c) pre post
| c_sem_while_false :
∀ (pre : env)
(b : bool_expr)
(c : cmd),
bool_eval b pre = ff →
c_sem (WHILE b DO c END) pre pre
-- c_sem (while true do c) pre post
| c_sem_while_true :
∀ (pre is post : env)
(b : bool_expr)
(c : cmd),
bool_eval b pre = tt →
c_sem c pre is → -- one iteration of c: pre->is
c_sem (WHILE b DO c END) is post →
c_sem (WHILE b DO c END) pre post |
b62a0845e0e846d3fc1c1a5664412bd16f122272 | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /tests/lean/run/meta7.lean | 18109cca65351ab647b8a0d4dd774b2e9f12388d | [
"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 | 4,590 | lean | import Lean.Meta
open Lean
open Lean.Meta
partial def fact : Nat → Nat
| 0 => 1
| n+1 => (n+1)*fact n
set_option trace.Meta.debug true
set_option trace.Meta.check false
def print (msg : MessageData) : MetaM Unit :=
trace[Meta.debug] msg
def checkM (x : MetaM Bool) : MetaM Unit :=
unless (← x) do throwError "check failed"
def ex (x_1 x_2 x_3 : Nat) : Nat × Nat :=
let x := fact (10 + x_1 + x_2 + x_3);
let ty := Nat → Nat;
let f : ty := fun x => x;
let n := 20;
let z := f 10;
(let y : { v : Nat // v = n } := ⟨20, rfl⟩; y.1 + n + f x, z + 10)
def tst1 : MetaM Unit := do
print "----- tst1 -----";
let c ← getConstInfo `ex;
lambdaTelescope c.value?.get! fun xs body =>
withTrackingZeta do
check body;
let ys ← getZetaFVarIds;
let ys := ys.toList.map mkFVar;
print ys;
checkM $ pure $ ys.length == 2;
let c ← mkAuxDefinitionFor `foo body;
print c;
check c;
pure ()
#eval tst1
#print foo
def tst2 : MetaM Unit := do
print "----- tst2 -----";
let nat := mkConst `Nat;
let t0 := mkApp (mkConst `IO) nat;
let t := mkForall `_ BinderInfo.default nat t0;
print t;
check t;
forallBoundedTelescope t (some 1) fun xs b => do
print b;
checkM $ pure $ xs.size == 1;
checkM $ pure $ b == t0;
pure ()
#eval tst2
def tst3 : MetaM Unit := do
print "----- tst2 -----";
let nat := mkConst `Nat;
let t0 := mkApp (mkConst `IO) nat;
let t := t0;
print t;
check t;
forallBoundedTelescope t (some 0) fun xs b => do
print b;
checkM $ pure $ xs.size == 0;
checkM $ pure $ b == t0;
pure ()
#eval tst3
def tst4 : MetaM Unit := do
print "----- tst4 -----";
let nat := mkConst `Nat;
withLocalDeclD `x nat fun x =>
withLocalDeclD `y nat fun y => do
let m ← mkFreshExprMVar nat;
print (← ppGoal m.mvarId!);
let val ← mkAppM `Add.add #[mkNatLit 10, y];
let ⟨zId, nId, subst⟩ ← assertAfter m.mvarId! x.fvarId! `z nat val;
print m;
print (← ppGoal nId);
withMVarContext nId do {
print m!"{subst.apply x} {subst.apply y} {mkFVar zId}";
assignExprMVar nId (← mkAppM `Add.add #[subst.apply x, mkFVar zId]);
print (mkMVar nId)
};
print m;
let expected ← mkAppM `Add.add #[x, val];
checkM (isDefEq m expected);
pure ()
#eval tst4
def tst5 : MetaM Unit := do
print "----- tst5 -----";
let prop := mkSort levelZero;
withLocalDeclD `p prop fun p =>
withLocalDeclD `q prop fun q => do
withLocalDeclD `h₁ p fun h₁ => do
let eq ← mkEq p q;
withLocalDeclD `h₂ eq fun h₂ => do
let m ← mkFreshExprMVar q;
let r ← replaceLocalDecl m.mvarId! h₁.fvarId! q h₂;
print (← ppGoal r.mvarId);
assignExprMVar r.mvarId (mkFVar r.fvarId);
print m;
check m;
pure ()
#eval tst5
def tst6 : MetaM Unit := do
print "----- tst6 -----";
let nat := mkConst `Nat;
withLocalDeclD `x nat fun x =>
withLocalDeclD `y nat fun y => do
let m ← mkFreshExprMVar nat;
print (← ppGoal m.mvarId!);
let val ← mkAppM `Add.add #[mkNatLit 10, y];
let ⟨zId, nId, subst⟩ ← assertAfter m.mvarId! y.fvarId! `z nat val;
print m;
print (← ppGoal nId);
withMVarContext nId do {
print m!"{subst.apply x} {subst.apply y} {mkFVar zId}";
assignExprMVar nId (← mkAppM `Add.add #[subst.apply x, mkFVar zId]);
print (mkMVar nId)
};
print m;
let expected ← mkAppM `Add.add #[x, val];
checkM (isDefEq m expected);
pure ()
#eval tst6
def tst7 : MetaM Unit := do
print "----- tst7 -----";
let nat := mkConst `Nat;
withLocalDeclD `x nat fun x =>
withLocalDeclD `y nat fun y => do
let val ← mkAppM `Add.add #[x, y];
print val;
let val := val.replaceFVars #[x, y] #[mkNatLit 0, mkNatLit 1];
print val;
let expected ← mkAppM `Add.add #[mkNatLit 0, mkNatLit 1];
print expected;
checkM (pure $ val == expected);
pure ()
#eval tst7
def aux := [1, 2, 3].isEmpty
def tst8 : MetaM Unit := do
print "----- tst8 -----"
let t := mkConst `aux
let some t ← unfoldDefinition? t | throwError "unexpected"
let some t ← unfoldDefinition? t | throwError "unexpected"
print t
let t ← whnfCore t
print t
pure ()
#eval tst8
def tst9 : MetaM Unit := do
print "----- tst9 -----"
let defInsts ← getDefaultInstances `OfNat
print (toString defInsts)
pure ()
#eval tst9
mutual
inductive Foo (α : Type) where
| mk : List (Bla α) → Foo α
| leaf : α → Foo α
inductive Bla (α : Type) where
| nil : Bla α
| cons : Foo α → Bla α → Bla α
end
def tst10 : MetaM Unit := do
assert! !(← getConstInfoInduct `List).isNested
assert! (← getConstInfoInduct `Bla).isNested
assert! (← getConstInfoInduct `Foo).isNested
assert! !(← getConstInfoInduct `Prod).isNested
#eval tst10
|
d072ca559b23d99ec91c66c0448800b5e54e0bba | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/nat/succ_pred.lean | 47ef60b006391d56bad441fba345b3e7bf3c4d8e | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 2,102 | lean | /-
Copyright (c) 2021 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import order.succ_pred.basic
/-!
# Successors and predecessors of naturals
In this file, we show that `ℕ` is both an archimedean `succ_order` and an archimedean `pred_order`.
-/
open function order
namespace nat
@[reducible] -- so that Lean reads `nat.succ` through `succ_order.succ`
instance : succ_order ℕ :=
{ succ := succ,
..succ_order.of_succ_le_iff succ (λ a b, iff.rfl) }
@[reducible] -- so that Lean reads `nat.pred` through `pred_order.pred`
instance : pred_order ℕ :=
{ pred := pred,
pred_le := pred_le,
min_of_le_pred := λ a ha, begin
cases a,
{ exact is_min_bot },
{ exact (not_succ_le_self _ ha).elim }
end,
le_pred_of_lt := λ a b h, begin
cases b,
{ exact (a.not_lt_zero h).elim },
{ exact le_of_succ_le_succ h }
end,
le_of_pred_lt := λ a b h, begin
cases a,
{ exact b.zero_le },
{ exact h }
end }
@[simp] lemma succ_eq_succ : order.succ = succ := rfl
@[simp] lemma pred_eq_pred : order.pred = pred := rfl
lemma succ_iterate (a : ℕ) : ∀ n, succ^[n] a = a + n
| 0 := rfl
| (n + 1) := by { rw [function.iterate_succ', add_succ], exact congr_arg _ n.succ_iterate }
lemma pred_iterate (a : ℕ) : ∀ n, pred^[n] a = a - n
| 0 := rfl
| (n + 1) := by { rw [function.iterate_succ', sub_succ], exact congr_arg _ n.pred_iterate }
instance : is_succ_archimedean ℕ :=
⟨λ a b h, ⟨b - a, by rw [succ_eq_succ, succ_iterate, add_tsub_cancel_of_le h]⟩⟩
instance : is_pred_archimedean ℕ :=
⟨λ a b h, ⟨b - a, by rw [pred_eq_pred, pred_iterate, tsub_tsub_cancel_of_le h]⟩⟩
/-! ### Covering relation -/
protected lemma covby_iff_succ_eq {m n : ℕ} : m ⋖ n ↔ m + 1 = n := succ_eq_iff_covby.symm
end nat
@[simp, norm_cast] lemma fin.coe_covby_iff {n : ℕ} {a b : fin n} : (a : ℕ) ⋖ b ↔ a ⋖ b :=
and_congr_right' ⟨λ h c hc, h hc, λ h c ha hb, @h ⟨c, hb.trans b.prop⟩ ha hb⟩
alias fin.coe_covby_iff ↔ _ covby.coe_fin
|
3a7aef7facada14c62d5890e4010cf61154aad12 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/algebra/field.lean | 1fc85894756f59bdb13f2e2f029dc6dd4596150c | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 8,890 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import algebra.ring logic.basic
open set
universe u
variables {α : Type u}
@[priority 100] -- see Note [lower instance priority]
instance division_ring.to_domain [s : division_ring α] : domain α :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h,
classical.by_contradiction $ λ hn,
division_ring.mul_ne_zero (mt or.inl hn) (mt or.inr hn) h
..s }
@[simp] theorem inv_one [division_ring α] : (1⁻¹ : α) = 1 := by rw [inv_eq_one_div, one_div_one]
attribute [simp] inv_inv'
lemma inv_inv'' [division_ring α] (x : α) : x⁻¹⁻¹ = x :=
inv_inv'
lemma inv_involutive' [division_ring α] : function.involutive (has_inv.inv : α → α) :=
@inv_inv' _ _
namespace units
variables [division_ring α] {a b : α}
/-- Embed an element of a division ring into the unit group.
By combining this function with the operations on units,
or the `/ₚ` operation, it is possible to write a division
as a partial function with three arguments. -/
def mk0 (a : α) (ha : a ≠ 0) : units α :=
⟨a, a⁻¹, mul_inv_cancel ha, inv_mul_cancel ha⟩
@[simp] theorem inv_eq_inv (u : units α) : (↑u⁻¹ : α) = u⁻¹ :=
(mul_left_inj u).1 $ by rw [units.mul_inv, mul_inv_cancel]; apply units.ne_zero
@[simp] theorem mk0_val (ha : a ≠ 0) : (mk0 a ha : α) = a := rfl
@[simp] lemma mk0_coe (u : units α) (h : (u : α) ≠ 0) : mk0 (u : α) h = u :=
units.ext rfl
@[simp] lemma mk0_inj {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) :
units.mk0 a ha = units.mk0 b hb ↔ a = b :=
⟨λ h, by injection h, λ h, units.ext h⟩
end units
section division_ring
variables [s : division_ring α] {a b c : α}
include s
lemma div_eq_mul_inv : a / b = a * b⁻¹ := rfl
attribute [simp] div_one zero_div div_self
theorem divp_eq_div (a : α) (u : units α) : a /ₚ u = a / u :=
congr_arg _ $ units.inv_eq_inv _
@[simp] theorem divp_mk0 (a : α) {b : α} (hb : b ≠ 0) :
a /ₚ units.mk0 b hb = a / b :=
divp_eq_div _ _
lemma inv_div : (a / b)⁻¹ = b / a :=
(mul_inv' _ _).trans (by rw inv_inv'; refl)
lemma inv_div_left : a⁻¹ / b = (b * a)⁻¹ :=
(mul_inv' _ _).symm
lemma neg_inv : - a⁻¹ = (- a)⁻¹ :=
by rw [inv_eq_one_div, inv_eq_one_div, div_neg_eq_neg_div]
lemma division_ring.inv_comm_of_comm (h : a ≠ 0) (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ :=
begin
have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ :=
congr_arg (λ x:α, a⁻¹ * x * a⁻¹) H.symm,
rwa [mul_assoc, mul_assoc, mul_inv_cancel, mul_one,
← mul_assoc, inv_mul_cancel, one_mul] at this; exact h
end
lemma div_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 :=
division_ring.mul_ne_zero ha (inv_ne_zero hb)
lemma div_ne_zero_iff (hb : b ≠ 0) : a / b ≠ 0 ↔ a ≠ 0 :=
⟨mt (λ h, by rw [h, zero_div]), λ ha, div_ne_zero ha hb⟩
lemma div_eq_zero_iff (hb : b ≠ 0) : a / b = 0 ↔ a = 0 :=
by haveI := classical.prop_decidable; exact
not_iff_not.1 (div_ne_zero_iff hb)
lemma add_div (a b c : α) : (a + b) / c = a / c + b / c :=
(div_add_div_same _ _ _).symm
lemma div_right_inj (hc : c ≠ 0) : a / c = b / c ↔ a = b :=
by rw [← divp_mk0 _ hc, ← divp_mk0 _ hc, divp_right_inj]
lemma sub_div (a b c : α) : (a - b) / c = a / c - b / c :=
(div_sub_div_same _ _ _).symm
lemma division_ring.inv_inj : a⁻¹ = b⁻¹ ↔ a = b :=
⟨λ h, by rw [← inv_inv'' a, h, inv_inv''], congr_arg (λx,x⁻¹)⟩
lemma division_ring.inv_eq_iff : a⁻¹ = b ↔ b⁻¹ = a :=
by rw [← division_ring.inv_inj, eq_comm, inv_inv'']
lemma div_neg (a : α) : a / -b = -(a / b) :=
by rw [← div_neg_eq_neg_div]
lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a :=
⟨λ h, by rw [← h, div_mul_cancel _ hb],
λ h, by rw [← h, mul_div_cancel _ hb]⟩
end division_ring
@[priority 100] -- see Note [lower instance priority]
instance field.to_integral_domain [F : field α] : integral_domain α :=
{ ..F, ..division_ring.to_domain }
section
variables [field α] {a b c d : α}
lemma div_eq_inv_mul : a / b = b⁻¹ * a := mul_comm _ _
lemma inv_add_inv {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ + b⁻¹ = (a + b) / (a * b) :=
by rw [inv_eq_one_div, inv_eq_one_div, one_div_add_one_div ha hb]
lemma inv_sub_inv {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ - b⁻¹ = (b - a) / (a * b) :=
by rw [inv_eq_one_div, inv_eq_one_div, div_sub_div _ _ ha hb, one_mul, mul_one]
lemma mul_div_right_comm (a b c : α) : (a * b) / c = (a / c) * b :=
(div_mul_eq_mul_div _ _ _).symm
lemma mul_comm_div (a b c : α) : (a / b) * c = a * (c / b) :=
by rw [← mul_div_assoc, mul_div_right_comm]
lemma div_mul_comm (a b c : α) : (a / b) * c = (c / b) * a :=
by rw [div_mul_eq_mul_div, mul_comm, mul_div_right_comm]
lemma mul_div_comm (a b c : α) : a * (b / c) = b * (a / c) :=
by rw [← mul_div_assoc, mul_comm, mul_div_assoc]
lemma field.div_right_comm (a : α) : (a / b) / c = (a / c) / b :=
by rw [div_div_eq_div_mul, div_div_eq_div_mul, mul_comm]
lemma field.div_div_div_cancel_right (a : α) (hc : c ≠ 0) : (a / c) / (b / c) = a / b :=
by rw [div_div_eq_mul_div, div_mul_cancel _ hc]
lemma div_mul_div_cancel (a : α) (hc : c ≠ 0) : (a / c) * (c / b) = a / b :=
by rw [← mul_div_assoc, div_mul_cancel _ hc]
lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b :=
(domain.mul_right_inj (mul_ne_zero' hb hd)).symm.trans $
by rw [← mul_assoc, div_mul_cancel _ hb,
← mul_assoc, mul_right_comm, div_mul_cancel _ hd]
lemma div_eq_iff (hb : b ≠ 0) : a / b = c ↔ a = c * b :=
by simpa using @div_eq_div_iff _ _ a b c 1 hb one_ne_zero
lemma eq_div_iff (hb : b ≠ 0) : c = a / b ↔ c * b = a :=
by simpa using @div_eq_div_iff _ _ c 1 a b one_ne_zero hb
lemma field.div_div_cancel (ha : a ≠ 0) : a / (a / b) = b :=
by rw [div_eq_mul_inv, inv_div, mul_div_cancel' _ ha]
lemma add_div' (a b c : α) (hc : c ≠ 0) :
b + a / c = (b * c + a) / c :=
by simpa using div_add_div b a one_ne_zero hc
lemma sub_div' (a b c : α) (hc : c ≠ 0) :
b - a / c = (b * c - a) / c :=
by simpa using div_sub_div b a one_ne_zero hc
lemma div_add' (a b c : α) (hc : c ≠ 0) :
a / c + b = (a + b * c) / c :=
by rwa [add_comm, add_div', add_comm]
lemma div_sub' (a b c : α) (hc : c ≠ 0) :
a / c - b = (a - c * b) / c :=
by simpa using div_sub_div a b hc one_ne_zero
end
section
variables [field α] {a b c : α}
attribute [simp] inv_zero div_zero
@[simp] lemma inv_eq_zero {a : α} : a⁻¹ = 0 ↔ a = 0 :=
classical.by_cases (assume : a = 0, by simp [*])(assume : a ≠ 0, by simp [*, inv_ne_zero])
end
namespace ring_hom
section
variables {β : Type*} [division_ring α] [division_ring β] (f : α →+* β) {x y : α}
lemma map_ne_zero : f x ≠ 0 ↔ x ≠ 0 :=
⟨mt $ λ h, h.symm ▸ f.map_zero,
λ x0 h, one_ne_zero $ by rw [← f.map_one, ← mul_inv_cancel x0, f.map_mul, h, zero_mul]⟩
lemma map_eq_zero : f x = 0 ↔ x = 0 :=
by haveI := classical.dec; exact not_iff_not.1 f.map_ne_zero
lemma map_inv : f x⁻¹ = (f x)⁻¹ :=
begin
classical, by_cases h : x = 0, by simp [h],
apply (domain.mul_left_inj (f.map_ne_zero.2 h)).1,
rw [mul_inv_cancel (f.map_ne_zero.2 h), ← f.map_mul, mul_inv_cancel h, f.map_one]
end
lemma map_div : f (x / y) = f x / f y :=
(f.map_mul _ _).trans $ congr_arg _ $ f.map_inv
lemma injective : function.injective f :=
f.injective_iff.2
(λ a ha, classical.by_contradiction $ λ ha0,
by simpa [ha, f.map_mul, f.map_one, zero_ne_one]
using congr_arg f (mul_inv_cancel ha0))
end
end ring_hom
namespace is_ring_hom
open ring_hom (of)
section
variables {β : Type*} [division_ring α] [division_ring β]
variables (f : α → β) [is_ring_hom f] {x y : α}
@[simp] lemma map_ne_zero : f x ≠ 0 ↔ x ≠ 0 := (of f).map_ne_zero
@[simp] lemma map_eq_zero : f x = 0 ↔ x = 0 := (of f).map_eq_zero
lemma map_inv : f x⁻¹ = (f x)⁻¹ := (of f).map_inv
lemma map_div : f (x / y) = f x / f y := (of f).map_div
lemma injective : function.injective f := (of f).injective
end
end is_ring_hom
section field_simp
mk_simp_attribute field_simps "The simpset `field_simps` is used by the tactic `field_simp` to
reduce an expression in a field to an expression of the form `n / d` where `n` and `d` are
division-free."
lemma mul_div_assoc' {α : Type*} [division_ring α] (a b c : α) : a * (b / c) = (a * b) / c :=
by simp [mul_div_assoc]
lemma neg_div' {α : Type*} [division_ring α] (a b : α) : - (b / a) = (-b) / a :=
by simp [neg_div]
attribute [field_simps] div_add_div_same inv_eq_one_div div_mul_eq_mul_div div_add' add_div'
div_div_eq_div_mul mul_div_assoc' div_eq_div_iff div_eq_iff eq_div_iff mul_ne_zero'
div_div_eq_mul_div neg_div' two_ne_zero div_sub_div div_sub' sub_div'
end field_simp
|
0f89369e0d32faf692d58b43e9b0b18a921d37a4 | 5749d8999a76f3a8fddceca1f6941981e33aaa96 | /src/topology/algebra/monoid.lean | 5d6945b9b0c2f80d0e193486d4db1c174af8a660 | [
"Apache-2.0"
] | permissive | jdsalchow/mathlib | 13ab43ef0d0515a17e550b16d09bd14b76125276 | 497e692b946d93906900bb33a51fd243e7649406 | refs/heads/master | 1,585,819,143,348 | 1,580,072,892,000 | 1,580,072,892,000 | 154,287,128 | 0 | 0 | Apache-2.0 | 1,540,281,610,000 | 1,540,281,609,000 | null | UTF-8 | Lean | false | false | 5,709 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
Theory of topological monoids.
TODO: generalize `topological_monoid` and `topological_add_monoid` to semigroups, or add a type class
`topological_operator α (*)`.
-/
import topology.constructions topology.continuous_on
import algebra.pi_instances
open classical set lattice filter topological_space
open_locale classical topological_space
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
section topological_monoid
/-- A topological monoid is a monoid in which the multiplication is continuous as a function
`α × α → α`. -/
class topological_monoid (α : Type u) [topological_space α] [monoid α] : Prop :=
(continuous_mul : continuous (λp:α×α, p.1 * p.2))
/-- A topological (additive) monoid is a monoid in which the addition is
continuous as a function `α × α → α`. -/
class topological_add_monoid (α : Type u) [topological_space α] [add_monoid α] : Prop :=
(continuous_add : continuous (λp:α×α, p.1 + p.2))
attribute [to_additive topological_add_monoid] topological_monoid
section
variables [topological_space α] [monoid α] [topological_monoid α]
@[to_additive]
lemma continuous_mul : continuous (λp:α×α, p.1 * p.2) :=
topological_monoid.continuous_mul α
@[to_additive]
lemma continuous.mul [topological_space β] {f : β → α} {g : β → α}
(hf : continuous f) (hg : continuous g) :
continuous (λx, f x * g x) :=
continuous_mul.comp (hf.prod_mk hg)
@[to_additive]
lemma continuous_mul_left (a : α) : continuous (λ b:α, a * b) :=
continuous_const.mul continuous_id
@[to_additive]
lemma continuous_mul_right (a : α) : continuous (λ b:α, b * a) :=
continuous_id.mul continuous_const
@[to_additive]
lemma continuous_on.mul [topological_space β] {f : β → α} {g : β → α} {s : set β}
(hf : continuous_on f s) (hg : continuous_on g s) :
continuous_on (λx, f x * g x) s :=
(continuous_mul.comp_continuous_on (hf.prod hg) : _)
-- @[to_additive continuous_smul]
lemma continuous_pow : ∀ n : ℕ, continuous (λ a : α, a ^ n)
| 0 := by simpa using continuous_const
| (k+1) := show continuous (λ (a : α), a * a ^ k), from continuous_id.mul (continuous_pow _)
@[to_additive]
lemma tendsto_mul {a b : α} : tendsto (λp:α×α, p.fst * p.snd) (𝓝 (a, b)) (𝓝 (a * b)) :=
continuous_iff_continuous_at.mp (topological_monoid.continuous_mul α) (a, b)
@[to_additive]
lemma filter.tendsto.mul {f : β → α} {g : β → α} {x : filter β} {a b : α}
(hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) :
tendsto (λx, f x * g x) x (𝓝 (a * b)) :=
tendsto.comp (by rw [←nhds_prod_eq]; exact tendsto_mul) (hf.prod_mk hg)
@[to_additive]
lemma continuous_at.mul [topological_space β] {f : β → α} {g : β → α} {x : β}
(hf : continuous_at f x) (hg : continuous_at g x) :
continuous_at (λx, f x * g x) x :=
hf.mul hg
@[to_additive]
lemma continuous_within_at.mul [topological_space β] {f : β → α} {g : β → α} {s : set β} {x : β}
(hf : continuous_within_at f s x) (hg : continuous_within_at g s x) :
continuous_within_at (λx, f x * g x) s x :=
hf.mul hg
@[to_additive]
lemma tendsto_list_prod {f : γ → β → α} {x : filter β} {a : γ → α} :
∀l:list γ, (∀c∈l, tendsto (f c) x (𝓝 (a c))) →
tendsto (λb, (l.map (λc, f c b)).prod) x (𝓝 ((l.map a).prod))
| [] _ := by simp [tendsto_const_nhds]
| (f :: l) h :=
begin
simp,
exact (h f (list.mem_cons_self _ _)).mul
(tendsto_list_prod l (assume c hc, h c (list.mem_cons_of_mem _ hc)))
end
@[to_additive]
lemma continuous_list_prod [topological_space β] {f : γ → β → α} (l : list γ)
(h : ∀c∈l, continuous (f c)) :
continuous (λa, (l.map (λc, f c a)).prod) :=
continuous_iff_continuous_at.2 $ assume x, tendsto_list_prod l $ assume c hc,
continuous_iff_continuous_at.1 (h c hc) x
@[to_additive topological_add_monoid]
instance [topological_space β] [monoid β] [topological_monoid β] : topological_monoid (α × β) :=
⟨((continuous_fst.comp continuous_fst).mul (continuous_fst.comp continuous_snd)).prod_mk
((continuous_snd.comp continuous_fst).mul (continuous_snd.comp continuous_snd))⟩
attribute [instance] prod.topological_add_monoid
end
section
variables [topological_space α] [comm_monoid α]
@[to_additive]
lemma is_submonoid.mem_nhds_one (β : set α) [is_submonoid β] (oβ : is_open β) :
β ∈ 𝓝 (1 : α) :=
mem_nhds_sets_iff.2 ⟨β, (by refl), oβ, is_submonoid.one_mem _⟩
variable [topological_monoid α]
@[to_additive]
lemma tendsto_multiset_prod {f : γ → β → α} {x : filter β} {a : γ → α} (s : multiset γ) :
(∀c∈s, tendsto (f c) x (𝓝 (a c))) →
tendsto (λb, (s.map (λc, f c b)).prod) x (𝓝 ((s.map a).prod)) :=
by { rcases s with ⟨l⟩, simp, exact tendsto_list_prod l }
@[to_additive]
lemma tendsto_finset_prod {f : γ → β → α} {x : filter β} {a : γ → α} (s : finset γ) :
(∀c∈s, tendsto (f c) x (𝓝 (a c))) → tendsto (λb, s.prod (λc, f c b)) x (𝓝 (s.prod a)) :=
tendsto_multiset_prod _
@[to_additive]
lemma continuous_multiset_prod [topological_space β] {f : γ → β → α} (s : multiset γ) :
(∀c∈s, continuous (f c)) → continuous (λa, (s.map (λc, f c a)).prod) :=
by { rcases s with ⟨l⟩, simp, exact continuous_list_prod l }
@[to_additive]
lemma continuous_finset_prod [topological_space β] {f : γ → β → α} (s : finset γ) :
(∀c∈s, continuous (f c)) → continuous (λa, s.prod (λc, f c a)) :=
continuous_multiset_prod _
end
end topological_monoid
|
33bac541657ddb0d6faa936d0aae13461dfd2aa9 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/logic/default.lean | 1481ed31ca180c4380e7ed8983882ed9a6dde2eb | [
"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 | 513 | lean | --- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
--- Released under Apache 2.0 license as described in the file LICENSE.
--- Author: Jeremy Avigad
import logic.connectives logic.eq logic.heq
import logic.cast logic.wf logic.wf_k
-- We need unit and prod available for generating constructions used by definitional package
import data.unit.decl data.prod.decl
import logic.quantifiers logic.if
import logic.decidable logic.inhabited logic.nonempty
import logic.instances
import logic.identities
|
2935762c1164d9aac7f4dc23df17a39cdffa9886 | 271e26e338b0c14544a889c31c30b39c989f2e0f | /stage0/src/Init/Util.lean | a59695fab755786d8b78f6c0aa2f6024b5736d99 | [
"Apache-2.0"
] | permissive | dgorokho/lean4 | 805f99b0b60c545b64ac34ab8237a8504f89d7d4 | e949a052bad59b1c7b54a82d24d516a656487d8a | refs/heads/master | 1,607,061,363,851 | 1,578,006,086,000 | 1,578,006,086,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,429 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Data.String.Basic
import Init.Data.ToString
universes u v
/- debugging helper functions -/
@[neverExtract, extern "lean_dbg_trace"]
def dbgTrace {α : Type u} (s : String) (f : Unit → α) : α :=
f ()
/- Display the given message if `a` is shared, that is, RC(a) > 1 -/
@[neverExtract, extern "lean_dbg_trace_if_shared"]
def dbgTraceIfShared {α : Type u} (s : String) (a : α) : α :=
a
@[extern "lean_dbg_sleep"]
def dbgSleep {α : Type u} (ms : UInt32) (f : Unit → α) : α :=
f ()
@[extern c inline "#4"]
unsafe def unsafeCast {α : Type u} {β : Type v} [inh : @& Inhabited β] (a : α) : β := arbitrary _
@[neverExtract, extern "lean_panic_fn"]
constant panic {α : Type u} [Inhabited α] (msg : String) : α := arbitrary _
@[noinline] private def mkPanicMessage (modName : String) (line col : Nat) (msg : String) : String :=
"PANIC at " ++ modName ++ ":" ++ toString line ++ ":" ++ toString col ++ ": " ++ msg
@[neverExtract, inline] def panicWithPos {α : Type u} [Inhabited α] (modName : String) (line col : Nat) (msg : String) : α :=
panic (mkPanicMessage modName line col msg)
-- TODO: should be a macro
@[neverExtract, noinline, nospecialize] def unreachable! {α : Type u} [Inhabited α] : α :=
panic! "unreachable"
|
9ddd7b0302e930b0c3baa80ce6b79d5b86256425 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/category_theory/monoidal/internal/limits.lean | 316286603e9752e997cdecde209f77253d0ee0d8 | [
"Apache-2.0"
] | permissive | Lix0120/mathlib | 0020745240315ed0e517cbf32e738d8f9811dd80 | e14c37827456fc6707f31b4d1d16f1f3a3205e91 | refs/heads/master | 1,673,102,855,024 | 1,604,151,044,000 | 1,604,151,044,000 | 308,930,245 | 0 | 0 | Apache-2.0 | 1,604,164,710,000 | 1,604,163,547,000 | null | UTF-8 | Lean | false | false | 3,099 | 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 category_theory.monoidal.internal.functor_category
import category_theory.monoidal.limits
import category_theory.limits.preserves.basic
/-!
# Limits of monoid objects.
If `C` has limits, so does `Mon_ C`, and the forgetful functor preserves these limits.
(This could potentially replace many individual constructions for concrete categories,
in particular `Mon`, `SemiRing`, `Ring`, and `Algebra R`.)
-/
open category_theory
open category_theory.limits
open category_theory.monoidal
universes v u
noncomputable theory
namespace Mon_
variables {J : Type v} [small_category J]
variables {C : Type u} [category.{v} C] [has_limits C] [monoidal_category.{v} C]
/--
We construct the (candidate) limit of a functor `F : J ⥤ Mon_ C`
by interpreting it as a functor `Mon_ (J ⥤ C)`,
and noting that taking limits is a lax monoidal functor,
and hence sends monoid objects to monoid objects.
-/
@[simps {rhs_md:=semireducible}]
def limit (F : J ⥤ Mon_ C) : Mon_ C :=
lim_lax.map_Mon.obj (Mon_functor_category_equivalence.inverse.obj F)
/--
Implementation of `Mon_.has_limits`: a limiting cone over a functor `F : J ⥤ Mon_ C`.
-/
@[simps]
def limit_cone (F : J ⥤ Mon_ C) : cone F :=
{ X := limit F,
π :=
{ app := λ j, { hom := limit.π (F ⋙ Mon_.forget C) j, },
naturality' := λ j j' f, by { ext, exact (limit.cone (F ⋙ Mon_.forget C)).π.naturality f, } } }
/--
The image of the proposed limit cone for `F : J ⥤ Mon_ C` under the forgetful functor
`forget C : Mon_ C ⥤ C` is isomorphic to the limit cone of `F ⋙ forget C`.
-/
def forget_map_cone_limit_cone_iso (F : J ⥤ Mon_ C) :
(forget C).map_cone (limit_cone F) ≅ limit.cone (F ⋙ forget C) :=
cones.ext (iso.refl _) (λ j, (by tidy))
/--
Implementation of `Mon_.has_limits`:
the proposed cone over a functor `F : J ⥤ Mon_ C` is a limit cone.
-/
@[simps]
def limit_cone_is_limit (F : J ⥤ Mon_ C) : is_limit (limit_cone F) :=
{ lift := λ s,
{ hom := limit.lift (F ⋙ Mon_.forget C) ((Mon_.forget C).map_cone s),
mul_hom' :=
begin
ext, dsimp, simp, dsimp,
slice_rhs 1 2 { rw [←monoidal_category.tensor_comp, limit.lift_π], dsimp, }
end },
fac' := λ s h, by { ext, simp, },
uniq' := λ s m w,
begin
ext,
dsimp, simp only [Mon_.forget_map, limit.lift_π, functor.map_cone_π],
exact congr_arg Mon_.hom.hom (w j),
end, }
instance has_limits : has_limits (Mon_ C) :=
{ has_limits_of_shape := λ J 𝒥, by exactI
{ has_limit := λ F, has_limit.mk
{ cone := limit_cone F,
is_limit := limit_cone_is_limit F } } }
instance forget_preserves_limits : preserves_limits (Mon_.forget C) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F : J ⥤ Mon_ C, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F)
(is_limit.of_iso_limit
(limit.is_limit (F ⋙ Mon_.forget C))
(forget_map_cone_limit_cone_iso F).symm) } }
end Mon_
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.