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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
72fbeecb120904bd3853903a1fc0e5bde464941b | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/algebra/symmetrized.lean | 3eccd1459a78b2dfa3260855727667d0ff6e66cd | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 8,811 | lean | /-
Copyright (c) 2021 Christopher Hoskin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Christopher Hoskin
-/
import algebra.module.basic
import tactic.abel
/-!
# Symmetrized algebra
A commutative multiplication on a real or complex space can be constructed from any multiplication
by "symmetrization" i.e
$$
a \circ b = \frac{1}{2}(ab + ba)
$$
We provide the symmetrized version of a type `α` as `sym_alg α`, with notation `αˢʸᵐ`.
## Implementation notes
The approach taken here is inspired by algebra.opposites. We use Oxford Spellings
(IETF en-GB-oxendict).
## References
* [Hanche-Olsen and Størmer, Jordan Operator Algebras][hancheolsenstormer1984]
-/
open function
/--
The symmetrized algebra has the same underlying space as the original algebra.
-/
def sym_alg (α : Type*) : Type* := α
postfix `ˢʸᵐ`:std.prec.max_plus := sym_alg
namespace sym_alg
variables {α : Type*}
/-- The element of `sym_alg α` that represents `a : α`. -/
@[pattern,pp_nodot]
def sym : α → αˢʸᵐ := id
/-- The element of `α` represented by `x : αˢʸᵐ`. -/
@[pp_nodot]
def unsym : αˢʸᵐ → α := id
@[simp] lemma unsym_sym (a : α) : unsym (sym a) = a := rfl
@[simp] lemma sym_unsym (a : α) : sym (unsym a) = a := rfl
@[simp] lemma sym_comp_unsym : (sym : α → αˢʸᵐ) ∘ unsym = id := rfl
@[simp] lemma unsym_comp_sym : (unsym : αˢʸᵐ → α) ∘ sym = id := rfl
/-- The canonical bijection between `α` and `αˢʸᵐ`. -/
@[simps apply symm_apply { fully_applied := ff }]
def sym_equiv : α ≃ αˢʸᵐ := ⟨sym, unsym, unsym_sym, sym_unsym⟩
lemma sym_bijective : bijective (sym : α → αˢʸᵐ) := sym_equiv.bijective
lemma unsym_bijective : bijective (unsym : αˢʸᵐ → α) := sym_equiv.symm.bijective
lemma sym_injective : injective (sym : α → αˢʸᵐ) := sym_bijective.injective
lemma sym_surjective : surjective (sym : α → αˢʸᵐ) := sym_bijective.surjective
lemma unsym_injective : injective (unsym : αˢʸᵐ → α) := unsym_bijective.injective
lemma unsym_surjective : surjective (unsym : αˢʸᵐ → α) := unsym_bijective.surjective
@[simp] lemma sym_inj {a b : α} : sym a = sym b ↔ a = b := sym_injective.eq_iff
@[simp] lemma unsym_inj {a b : αˢʸᵐ} : unsym a = unsym b ↔ a = b := unsym_injective.eq_iff
instance [nontrivial α] : nontrivial αˢʸᵐ := sym_injective.nontrivial
instance [inhabited α] : inhabited αˢʸᵐ := ⟨sym default⟩
instance [subsingleton α] : subsingleton αˢʸᵐ := unsym_injective.subsingleton
instance [unique α] : unique αˢʸᵐ := unique.mk' _
instance [is_empty α] : is_empty αˢʸᵐ := function.is_empty unsym
@[to_additive]
instance [has_one α] : has_one αˢʸᵐ := { one := sym 1 }
instance [has_add α] : has_add αˢʸᵐ :=
{ add := λ a b, sym (unsym a + unsym b) }
instance [has_sub α] : has_sub αˢʸᵐ := { sub := λ a b, sym (unsym a - unsym b) }
instance [has_neg α] : has_neg αˢʸᵐ :=
{ neg := λ a, sym (-unsym a) }
/- Introduce the symmetrized multiplication-/
instance [has_add α] [has_mul α] [has_one α] [invertible (2 : α)] : has_mul (αˢʸᵐ) :=
{ mul := λ a b, sym (⅟2 * (unsym a * unsym b + unsym b * unsym a)) }
@[to_additive] instance [has_inv α] : has_inv αˢʸᵐ :=
{ inv := λ a, sym $ (unsym a)⁻¹ }
instance (R : Type*) [has_smul R α] : has_smul R αˢʸᵐ :=
{ smul := λ r a, sym (r • unsym a) }
@[simp, to_additive] lemma sym_one [has_one α] : sym (1 : α) = 1 := rfl
@[simp, to_additive] lemma unsym_one [has_one α] : unsym (1 : αˢʸᵐ) = 1 := rfl
@[simp] lemma sym_add [has_add α] (a b : α) : sym (a + b) = sym a + sym b := rfl
@[simp] lemma unsym_add [has_add α] (a b : αˢʸᵐ) : unsym (a + b) = unsym a + unsym b := rfl
@[simp] lemma sym_sub [has_sub α] (a b : α) : sym (a - b) = sym a - sym b := rfl
@[simp] lemma unsym_sub [has_sub α] (a b : αˢʸᵐ) : unsym (a - b) = unsym a - unsym b := rfl
@[simp] lemma sym_neg [has_neg α] (a : α) : sym (-a) = -sym a := rfl
@[simp] lemma unsym_neg [has_neg α] (a : αˢʸᵐ) : unsym (-a) = -unsym a := rfl
lemma mul_def [has_add α] [has_mul α] [has_one α] [invertible (2 : α)] (a b : αˢʸᵐ) :
a * b = sym (⅟2*(unsym a * unsym b + unsym b * unsym a)) := by refl
lemma unsym_mul [has_mul α] [has_add α] [has_one α] [invertible (2 : α)] (a b : αˢʸᵐ) :
unsym (a * b) = ⅟2*(unsym a * unsym b + unsym b * unsym a) := by refl
lemma sym_mul_sym [has_mul α] [has_add α] [has_one α] [invertible (2 : α)] (a b : α) :
sym a * sym b = sym (⅟2*(a * b + b * a)) :=
rfl
@[simp, to_additive] lemma sym_inv [has_inv α] (a : α) : sym (a⁻¹) = (sym a)⁻¹ := rfl
@[simp, to_additive] lemma unsym_inv [has_inv α] (a : αˢʸᵐ) : unsym (a⁻¹) = (unsym a)⁻¹ := rfl
@[simp] lemma sym_smul {R : Type*} [has_smul R α] (c : R) (a : α) : sym (c • a) = c • sym a := rfl
@[simp] lemma unsym_smul {R : Type*} [has_smul R α] (c : R) (a : αˢʸᵐ) :
unsym (c • a) = c • unsym a := rfl
@[simp, to_additive] lemma unsym_eq_one_iff [has_one α] (a : αˢʸᵐ) : a.unsym = 1 ↔ a = 1 :=
unsym_injective.eq_iff' rfl
@[simp, to_additive] lemma sym_eq_one_iff [has_one α] (a : α) : sym a = 1 ↔ a = 1 :=
sym_injective.eq_iff' rfl
@[to_additive] lemma unsym_ne_one_iff [has_one α] (a : αˢʸᵐ) : a.unsym ≠ (1 : α) ↔ a ≠ (1 : αˢʸᵐ) :=
not_congr $ unsym_eq_one_iff a
@[to_additive] lemma sym_ne_one_iff [has_one α] (a : α) : sym a ≠ (1 : αˢʸᵐ) ↔ a ≠ (1 : α) :=
not_congr $ sym_eq_one_iff a
instance [add_comm_semigroup α] : add_comm_semigroup (αˢʸᵐ) :=
unsym_injective.add_comm_semigroup _ unsym_add
instance [add_monoid α] : add_monoid (αˢʸᵐ) :=
unsym_injective.add_monoid _ unsym_zero unsym_add (λ _ _, rfl)
instance [add_group α] : add_group (αˢʸᵐ) :=
unsym_injective.add_group _ unsym_zero
unsym_add unsym_neg unsym_sub (λ _ _, rfl) (λ _ _, rfl)
instance [add_comm_monoid α] : add_comm_monoid (αˢʸᵐ) :=
{ ..sym_alg.add_comm_semigroup, ..sym_alg.add_monoid }
instance [add_comm_group α] : add_comm_group (αˢʸᵐ) :=
{ ..sym_alg.add_comm_monoid, ..sym_alg.add_group }
instance {R : Type*} [semiring R] [add_comm_monoid α] [module R α] : module R αˢʸᵐ :=
function.injective.module R ⟨unsym, unsym_zero, unsym_add⟩ unsym_injective unsym_smul
instance [has_mul α] [has_add α] [has_one α] [invertible (2 : α)] (a : α) [invertible a] :
invertible (sym a) :=
{ inv_of := sym (⅟a),
inv_of_mul_self := begin
rw [sym_mul_sym, mul_inv_of_self, inv_of_mul_self, ←bit0, inv_of_mul_self, sym_one],
end,
mul_inv_of_self := begin
rw [sym_mul_sym, mul_inv_of_self, inv_of_mul_self, ←bit0, inv_of_mul_self, sym_one],
end }
@[simp] lemma inv_of_sym [has_mul α] [has_add α] [has_one α] [invertible (2 : α)] (a : α)
[invertible a] : ⅟(sym a) = sym (⅟a) := rfl
instance [semiring α] [invertible (2 : α)] : non_assoc_semiring (αˢʸᵐ) :=
{ one := 1,
mul := (*),
zero := (0),
zero_mul := λ _, by rw [mul_def, unsym_zero, zero_mul, mul_zero, add_zero, mul_zero, sym_zero],
mul_zero := λ _, by rw [mul_def, unsym_zero, zero_mul, mul_zero, add_zero, mul_zero, sym_zero],
mul_one := λ _, by rw [mul_def, unsym_one, mul_one, one_mul, ←two_mul, inv_of_mul_self_assoc,
sym_unsym],
one_mul := λ _, by rw [mul_def, unsym_one, mul_one, one_mul, ←two_mul, inv_of_mul_self_assoc,
sym_unsym],
left_distrib := λ a b c, match a, b, c with
| sym a, sym b, sym c := begin
rw [sym_mul_sym, sym_mul_sym, ←sym_add, sym_mul_sym, ←sym_add, mul_add a, add_mul _ _ a,
add_add_add_comm, mul_add],
end
end,
right_distrib := λ a b c, match a, b, c with
| sym a, sym b, sym c := begin
rw [sym_mul_sym, sym_mul_sym, ←sym_add, sym_mul_sym, ←sym_add, mul_add c, add_mul _ _ c,
add_add_add_comm, mul_add],
end
end,
..sym_alg.add_comm_monoid, }
/-- The symmetrization of a real (unital, associative) algebra is a non-associative ring. -/
instance [ring α] [invertible (2 : α)] : non_assoc_ring (αˢʸᵐ) :=
{ ..sym_alg.non_assoc_semiring,
..sym_alg.add_comm_group, }
/-! The squaring operation coincides for both multiplications -/
lemma unsym_mul_self [semiring α] [invertible (2 : α)] (a : αˢʸᵐ) :
unsym (a*a) = unsym a * unsym a :=
by rw [mul_def, unsym_sym, ←two_mul, inv_of_mul_self_assoc]
lemma sym_mul_self [semiring α] [invertible (2 : α)] (a : α) : sym (a*a) = sym a * sym a :=
by rw [sym_mul_sym, ←two_mul, inv_of_mul_self_assoc]
lemma mul_comm [has_mul α] [add_comm_semigroup α] [has_one α] [invertible (2 : α)] (a b : αˢʸᵐ) :
a * b = b * a :=
by rw [mul_def, mul_def, add_comm]
end sym_alg
|
5de826c07d50f2c7bf3d559b43c8aea0c66c2da3 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/simp_auto_param.lean | 8e3bb4b8ee21e11ece28ec732f50703adb9b886d | [
"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 | 158 | lean | constant f : nat → nat
axiom fax (x : nat) (h : x > 0 . tactic.assumption) : f x = x
example (a : nat) (h : a > 0) : f (f a) = a :=
begin
simp [fax]
end
|
2932054eda19f192f77b0c8e25e4213ad22d5be2 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/tactic/omega/nat/main.lean | b52a8c6d236f8cea20628d5205fb089cb2d5c9c9 | [
"Apache-2.0"
] | permissive | rspencer01/mathlib | b1e3afa5c121362ef0881012cc116513ab09f18c | c7d36292c6b9234dc40143c16288932ae38fdc12 | refs/heads/master | 1,595,010,346,708 | 1,567,511,503,000 | 1,567,511,503,000 | 206,071,681 | 0 | 0 | Apache-2.0 | 1,567,513,643,000 | 1,567,513,643,000 | null | UTF-8 | Lean | false | false | 5,190 | lean | /-
Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: 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
local notation `&` k := preterm.cst k
local infix ` ** ` : 300 := preterm.var
local notation t ` +* ` s := preterm.add t s
local notation t ` -* ` s := preterm.sub t s
local notation x ` =* ` y := form.eq x y
local notation x ` ≤* ` y := form.le x y
local notation `¬* ` p := form.not p
local notation p ` ∨* ` q := form.or p q
local notation p ` ∧* ` q := form.and p q
run_cmd mk_simp_attr `sugar_nat
attribute [sugar_nat]
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
classical.imp_iff_not_or
classical.iff_iff_not_or_and_or_not
meta def desugar := `[try {simp only with sugar_nat}]
lemma univ_close_of_unsat_neg_elim_not (m) (p : form) :
(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 form.sat_of_implies_of_sat implies_neg_elim h2,
end
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
meta def prove_neg_free : form → 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 (form.neg_free %%`(p))
(form.neg_free %%`(q)) %%x %%y)
| (p ∧* q) :=
do x ← prove_neg_free p,
y ← prove_neg_free q,
return `(@and.intro (form.neg_free %%`(p))
(form.neg_free %%`(q)) %%x %%y)
| _ := failed
meta def prove_sub_free : form → 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 (form.sub_free %%`(p))
(form.sub_free %%`(q)) %%x %%y)
| (p ∧* q) :=
do x ← prove_sub_free p,
y ← prove_sub_free q,
return `(@and.intro (form.sub_free %%`(p))
(form.sub_free %%`(q)) %%x %%y)
/- Given a p : form, return the expr of a term t : p.unsat,
where p is subtraction- and negation-free. -/
meta def prove_unsat_sub_free (p : form) : 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 : form, return the expr of a term t : p.unsat,
where p is negation-free. -/
meta def prove_unsat_neg_free : form → 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 : form),
return the expr of (t : univ_close m p) -/
meta def prove_univ_close (m : nat) (p : form) : tactic expr :=
do x ← prove_unsat_neg_free (neg_elim (¬*p)),
to_expr ``(univ_close_of_unsat_neg_elim_not %%`(m) %%`(p) %%x)
meta def to_preterm : expr → tactic preterm
| (expr.var k) := return (preterm.var 1 k)
| `(%%(expr.var k) * %%mx) :=
do m ← eval_expr nat mx,
return (preterm.var m k)
| `(%%t1x + %%t2x) :=
do t1 ← to_preterm t1x,
t2 ← to_preterm t2x,
return (preterm.add t1 t2)
| `(%%t1x - %%t2x) :=
do t1 ← to_preterm t1x,
t2 ← to_preterm t2x,
return (preterm.sub t1 t2)
| mx :=
do m ← eval_expr nat mx,
return (preterm.cst m)
meta def to_form_core : expr → tactic form
| `(%%tx1 = %%tx2) :=
do t1 ← to_preterm tx1,
t2 ← to_preterm tx2,
return (t1 =* t2)
| `(%%tx1 ≤ %%tx2) :=
do t1 ← to_preterm tx1,
t2 ← to_preterm tx2,
return (t1 ≤* t2)
| `(¬ %%px) := do p ← to_form_core px, return (¬* p)
| `(%%px ∨ %%qx) :=
do p ← to_form_core px,
q ← to_form_core qx,
return (p ∨* q)
| `(%%px ∧ %%qx) :=
do p ← to_form_core px,
q ← to_form_core qx,
return (p ∧* q)
| `(_ → %%px) := to_form_core px
| _ := failed
meta def to_form : nat → expr → tactic (form × nat)
| m `(_ → %%px) := to_form (m+1) px
| m x := do p ← to_form_core x, return (p,m)
meta def prove_lna : tactic expr :=
do (p,m) ← target >>= to_form 0,
prove_univ_close m p
end nat
end omega
open omega.nat
meta def omega_nat : tactic unit :=
desugar >> prove_lna >>= apply >> skip
|
b35747f1ffbcc992b71c25888e7a891084fc283c | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/analysis/normed_space/lattice_ordered_group.lean | c548b6392cfd2369988ab1436b972b6e1ff5ab42 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,857 | lean | /-
Copyright (c) 2021 Christopher Hoskin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Christopher Hoskin
-/
import analysis.normed_space.basic
import algebra.lattice_ordered_group
import topology.order.lattice
/-!
# Normed lattice ordered groups
Motivated by the theory of Banach Lattices, we then define `normed_lattice_add_comm_group` as a
lattice with a covariant normed group addition satisfying the solid axiom.
## Main statements
We show that a normed lattice ordered group is a topological lattice with respect to the norm
topology.
## References
* [Meyer-Nieberg, Banach lattices][MeyerNieberg1991]
## Tags
normed, lattice, ordered, group
-/
/-!
### Normed lattice orderd groups
Motivated by the theory of Banach Lattices, this section introduces normed lattice ordered groups.
-/
local notation `|`a`|` := abs a
/--
Let `α` be a normed commutative group equipped with a partial order covariant with addition, with
respect which `α` forms a lattice. Suppose that `α` is *solid*, that is to say, for `a` and `b` in
`α`, with absolute values `|a|` and `|b|` respectively, `|a| ≤ |b|` implies `∥a∥ ≤ ∥b∥`. Then `α` is
said to be a normed lattice ordered group.
-/
class normed_lattice_add_comm_group (α : Type*)
extends normed_group α, lattice α :=
(add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b)
(solid : ∀ a b : α, |a| ≤ |b| → ∥a∥ ≤ ∥b∥)
lemma solid {α : Type*} [normed_lattice_add_comm_group α] {a b : α} (h : |a| ≤ |b|) : ∥a∥ ≤ ∥b∥ :=
normed_lattice_add_comm_group.solid a b h
/--
A normed lattice ordered group is an ordered additive commutative group
-/
@[priority 100] -- see Note [lower instance priority]
instance normed_lattice_add_comm_group_to_ordered_add_comm_group {α : Type*}
[h : normed_lattice_add_comm_group α] : ordered_add_comm_group α := { ..h }
/--
Let `α` be a normed group with a partial order. Then the order dual is also a normed group.
-/
@[priority 100] -- see Note [lower instance priority]
instance {α : Type*} : Π [normed_group α], normed_group (order_dual α) := id
/--
Let `α` be a normed lattice ordered group and let `a` and `b` be elements of `α`. Then `a⊓-a ≥ b⊓-b`
implies `∥a∥ ≤ ∥b∥`.
-/
lemma dual_solid {α : Type*} [normed_lattice_add_comm_group α] (a b : α) (h: b⊓-b ≤ a⊓-a) :
∥a∥ ≤ ∥b∥ :=
begin
apply solid,
rw abs_eq_sup_neg,
nth_rewrite 0 ← neg_neg a,
rw ← neg_inf_eq_sup_neg,
rw abs_eq_sup_neg,
nth_rewrite 0 ← neg_neg b,
rw ← neg_inf_eq_sup_neg,
finish,
end
/--
Let `α` be a normed lattice ordered group, then the order dual is also a
normed lattice ordered group.
-/
@[priority 100] -- see Note [lower instance priority]
instance {α : Type*} [h: normed_lattice_add_comm_group α] :
normed_lattice_add_comm_group (order_dual α) :=
{ add_le_add_left := begin
intros a b h₁ c,
rw ← order_dual.dual_le,
rw ← order_dual.dual_le at h₁,
apply h.add_le_add_left,
exact h₁,
end,
solid := begin
intros a b h₂,
apply dual_solid,
rw ← order_dual.dual_le at h₂,
finish,
end, }
/--
Let `α` be a normed lattice ordered group, let `a` be an element of `α` and let `|a|` be the
absolute value of `a`. Then `∥|a|∥ = ∥a∥`.
-/
lemma norm_abs_eq_norm {α : Type*} [normed_lattice_add_comm_group α] (a : α) : ∥|a|∥ = ∥a∥ :=
begin
rw le_antisymm_iff,
split,
{ apply normed_lattice_add_comm_group.solid,
rw ← lattice_ordered_comm_group.abs_idempotent a, },
{ apply normed_lattice_add_comm_group.solid,
rw ← lattice_ordered_comm_group.abs_idempotent a, }
end
/--
Let `α` be a normed lattice ordered group. Then the infimum is jointly continuous.
-/
@[priority 100] -- see Note [lower instance priority]
instance normed_lattice_add_comm_group_has_continuous_inf {α : Type*}
[normed_lattice_add_comm_group α] : has_continuous_inf α :=
⟨ continuous_iff_continuous_at.2 $ λ q, tendsto_iff_norm_tendsto_zero.2 $
begin
have : ∀ p : α × α, ∥p.1 ⊓ p.2 - q.1 ⊓ q.2∥ ≤ ∥p.1 - q.1∥ + ∥p.2 - q.2∥,
{
intros,
nth_rewrite_rhs 0 ← norm_abs_eq_norm,
nth_rewrite_rhs 1 ← norm_abs_eq_norm,
apply le_trans _ (norm_add_le (|p.fst - q.fst|) (|p.snd - q.snd|)),
apply normed_lattice_add_comm_group.solid,
rw lattice_ordered_comm_group.abs_pos_eq (|p.fst - q.fst| + |p.snd - q.snd|),
{ calc |p.fst ⊓ p.snd - q.fst ⊓ q.snd| =
|p.fst ⊓ p.snd - q.fst ⊓ p.snd + (q.fst ⊓ p.snd - q.fst ⊓ q.snd)| :
by { rw sub_add_sub_cancel, }
... ≤ |p.fst ⊓ p.snd - q.fst ⊓ p.snd| + |q.fst ⊓ p.snd - q.fst ⊓ q.snd| :
by {apply lattice_ordered_comm_group.abs_triangle,}
... ≤ |p.fst - q.fst | + |p.snd - q.snd| : by {
apply add_le_add,
{ exact
(sup_le_iff.elim_left (lattice_ordered_comm_group.Birkhoff_inequalities _ _ _)).right },
{ rw inf_comm,
nth_rewrite 1 inf_comm,
exact
(sup_le_iff.elim_left (lattice_ordered_comm_group.Birkhoff_inequalities _ _ _)).right }
}, },
{ exact add_nonneg (lattice_ordered_comm_group.abs_pos (p.fst - q.fst))
(lattice_ordered_comm_group.abs_pos (p.snd - q.snd)), }
},
refine squeeze_zero (λ e, norm_nonneg _) this _,
convert (((continuous_fst.tendsto q).sub tendsto_const_nhds).norm).add
(((continuous_snd.tendsto q).sub tendsto_const_nhds).norm),
simp,
end
⟩
/--
Let `α` be a normed lattice ordered group. Then `α` is a topological lattice in the norm topology.
-/
@[priority 100] -- see Note [lower instance priority]
instance normed_lattice_add_comm_group_topological_lattice {α : Type*}
[normed_lattice_add_comm_group α] : topological_lattice α :=
topological_lattice.mk
|
37b1b24adbcfa002a68110d89a13261655781d0d | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/category_theory/monoidal/transport.lean | 0ef8ded16ceffe6fe1d28ab70d0c111c8b272812 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 11,377 | 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.natural_transformation
/-!
# Transport a monoidal structure along an equivalence.
When `C` and `D` are equivalent as categories,
we can transport a monoidal structure on `C` along the equivalence,
obtaining a monoidal structure on `D`.
We don't yet prove anything about this transported structure!
The next step would be to show that the original functor can be upgraded
to a monoidal functor with respect to this new structure.
-/
universes v₁ v₂ u₁ u₂
open category_theory
open category_theory.category
open category_theory.monoidal_category
namespace category_theory.monoidal
variables {C : Type u₁} [category.{v₁} C] [monoidal_category.{v₁} C]
variables {D : Type u₂} [category.{v₂} D]
/--
Transport a monoidal structure along an equivalence of (plain) categories.
-/
@[simps]
def transport (e : C ≌ D) : monoidal_category.{v₂} D :=
{ tensor_obj := λ X Y, e.functor.obj (e.inverse.obj X ⊗ e.inverse.obj Y),
tensor_hom := λ W X Y Z f g, e.functor.map (e.inverse.map f ⊗ e.inverse.map g),
tensor_unit := e.functor.obj (𝟙_ C),
associator := λ X Y Z, e.functor.map_iso
(((e.unit_iso.app _).symm ⊗ iso.refl _) ≪≫
(α_ (e.inverse.obj X) (e.inverse.obj Y) (e.inverse.obj Z)) ≪≫
(iso.refl _ ⊗ (e.unit_iso.app _))),
left_unitor := λ X,
e.functor.map_iso (((e.unit_iso.app _).symm ⊗ iso.refl _) ≪≫
λ_ (e.inverse.obj X)) ≪≫ (e.counit_iso.app _),
right_unitor := λ X,
e.functor.map_iso ((iso.refl _ ⊗ (e.unit_iso.app _).symm) ≪≫
ρ_ (e.inverse.obj X)) ≪≫ (e.counit_iso.app _),
triangle' := λ X Y,
begin
dsimp,
simp only [iso.hom_inv_id_app_assoc, comp_tensor_id, equivalence.unit_inverse_comp, assoc,
equivalence.inv_fun_map, comp_id, functor.map_comp, id_tensor_comp, e.inverse.map_id],
simp only [←e.functor.map_comp],
congr' 2,
slice_lhs 2 3 { rw [←id_tensor_comp], simp, dsimp, rw [tensor_id], },
rw [category.id_comp, ←associator_naturality_assoc, triangle],
end,
pentagon' := λ W X Y Z,
begin
dsimp,
simp only [iso.hom_inv_id_app_assoc, comp_tensor_id, assoc, equivalence.inv_fun_map,
functor.map_comp, id_tensor_comp, e.inverse.map_id],
simp only [←e.functor.map_comp],
congr' 2,
slice_lhs 4 5 { rw [←comp_tensor_id, iso.hom_inv_id_app], dsimp, rw [tensor_id], },
simp only [category.id_comp, category.assoc],
slice_lhs 5 6 { rw [←id_tensor_comp, iso.hom_inv_id_app], dsimp, rw [tensor_id], },
simp only [category.id_comp, category.assoc],
slice_rhs 2 3 { rw [id_tensor_comp_tensor_id, ←tensor_id_comp_id_tensor], },
slice_rhs 1 2 { rw [←tensor_id, ←associator_naturality], },
slice_rhs 3 4 { rw [←tensor_id, associator_naturality], },
slice_rhs 2 3 { rw [←pentagon], },
simp only [category.assoc],
congr' 2,
slice_lhs 1 2 { rw [associator_naturality], },
simp only [category.assoc],
congr' 1,
slice_lhs 1 2
{ rw [←id_tensor_comp, ←comp_tensor_id, iso.hom_inv_id_app],
dsimp, rw [tensor_id, tensor_id], },
simp only [category.id_comp, category.assoc],
end,
left_unitor_naturality' := λ X Y f,
begin
dsimp,
simp only [functor.map_comp, functor.map_id, category.assoc],
erw ←e.counit_iso.hom.naturality,
simp only [functor.comp_map, ←e.functor.map_comp_assoc],
congr' 2,
rw [e.inverse.map_id, id_tensor_comp_tensor_id_assoc, ←tensor_id_comp_id_tensor_assoc,
left_unitor_naturality],
end,
right_unitor_naturality' := λ X Y f,
begin
dsimp,
simp only [functor.map_comp, functor.map_id, category.assoc],
erw ←e.counit_iso.hom.naturality,
simp only [functor.comp_map, ←e.functor.map_comp_assoc],
congr' 2,
rw [e.inverse.map_id, tensor_id_comp_id_tensor_assoc, ←id_tensor_comp_tensor_id_assoc,
right_unitor_naturality],
end,
associator_naturality' := λ X₁ X₂ X₃ Y₁ Y₂ Y₃ f₁ f₂ f₃,
begin
dsimp,
simp only [equivalence.inv_fun_map, functor.map_comp, category.assoc],
simp only [←e.functor.map_comp],
congr' 1,
conv_lhs { rw [←tensor_id_comp_id_tensor] },
slice_lhs 2 3 { rw [id_tensor_comp_tensor_id, ←tensor_id_comp_id_tensor, ←tensor_id], },
simp only [category.assoc],
slice_lhs 3 4 { rw [associator_naturality], },
conv_lhs { simp only [comp_tensor_id], },
slice_lhs 3 4 { rw [←comp_tensor_id, iso.hom_inv_id_app], dsimp, rw [tensor_id], },
simp only [category.id_comp, category.assoc],
slice_lhs 2 3 { rw [associator_naturality], },
simp only [category.assoc],
congr' 2,
slice_lhs 1 1 { rw [←tensor_id_comp_id_tensor], },
slice_lhs 2 3 { rw [←id_tensor_comp, tensor_id_comp_id_tensor], },
slice_lhs 1 2 { rw [tensor_id_comp_id_tensor], },
conv_rhs { congr, skip, rw [←id_tensor_comp_tensor_id, id_tensor_comp], },
simp only [category.assoc],
slice_rhs 1 2 { rw [←id_tensor_comp, iso.hom_inv_id_app], dsimp, rw [tensor_id],},
simp only [category.id_comp, category.assoc],
conv_rhs { rw [id_tensor_comp], },
slice_rhs 2 3 { rw [id_tensor_comp_tensor_id, ←tensor_id_comp_id_tensor], },
slice_rhs 1 2 { rw [id_tensor_comp_tensor_id], },
end, }.
/-- A type synonym for `D`, which will carry the transported monoidal structure. -/
@[derive category, nolint unused_arguments]
def transported (e : C ≌ D) := D
instance (e : C ≌ D) : monoidal_category (transported e) := transport e
instance (e : C ≌ D) : inhabited (transported e) := ⟨𝟙_ _⟩
/--
We can upgrade `e.functor` to a lax monoidal functor from `C` to `D` with the transported structure.
-/
@[simps]
def lax_to_transported (e : C ≌ D) : lax_monoidal_functor C (transported e) :=
{ ε := 𝟙 (e.functor.obj (𝟙_ C)),
μ := λ X Y, e.functor.map (e.unit_inv.app X ⊗ e.unit_inv.app Y),
μ_natural' := λ X Y X' Y' f g,
begin
dsimp,
simp only [equivalence.inv_fun_map, functor.map_comp, tensor_comp, category.assoc],
simp only [←e.functor.map_comp],
congr' 1,
rw [←tensor_comp, iso.hom_inv_id_app, iso.hom_inv_id_app, ←tensor_comp],
dsimp,
rw [comp_id, comp_id],
end,
associativity' := λ X Y Z,
begin
dsimp,
simp only [comp_tensor_id, assoc, equivalence.inv_fun_map, functor.map_comp, id_tensor_comp,
e.inverse.map_id],
simp only [←e.functor.map_comp],
congr' 2,
slice_lhs 3 3 { rw [←tensor_id_comp_id_tensor], },
slice_lhs 2 3 { rw [←comp_tensor_id, iso.hom_inv_id_app], dsimp, rw [tensor_id] },
simp only [id_comp],
slice_rhs 2 3 { rw [←id_tensor_comp, iso.hom_inv_id_app], dsimp, rw [tensor_id] },
simp only [id_comp],
conv_rhs { rw [←id_tensor_comp_tensor_id _ (e.unit_inv.app X)], },
dsimp only [functor.comp_obj],
slice_rhs 3 4 { rw [←id_tensor_comp, iso.hom_inv_id_app], dsimp, rw [tensor_id] },
simp only [id_comp],
simp [associator_naturality],
end,
left_unitality' := λ X,
begin
dsimp,
simp only [tensor_id, assoc, id_comp, functor.map_comp, e.inverse.map_id],
rw equivalence.counit_functor,
simp only [←e.functor.map_comp],
congr' 1,
rw [←left_unitor_naturality],
simp,
end,
right_unitality' := λ X,
begin
dsimp,
simp only [tensor_id, assoc, id_comp, functor.map_comp, e.inverse.map_id],
rw equivalence.counit_functor,
simp only [←e.functor.map_comp],
congr' 1,
rw [←right_unitor_naturality],
simp,
end,
..e.functor, }.
/--
We can upgrade `e.functor` to a monoidal functor from `C` to `D` with the transported structure.
-/
@[simps]
def to_transported (e : C ≌ D) : monoidal_functor C (transported e) :=
{ ε_is_iso := by { dsimp, apply_instance, },
μ_is_iso := λ X Y, by { dsimp, apply_instance, },
..lax_to_transported e, }
/--
We can upgrade `e.inverse` to a lax monoidal functor from `D` with the transported structure to `C`.
-/
@[simps]
def lax_from_transported (e : C ≌ D) : lax_monoidal_functor (transported e) C :=
{ ε := e.unit.app (𝟙_ C),
μ := λ X Y, e.unit.app (e.inverse.obj X ⊗ e.inverse.obj Y),
μ_natural' := λ X Y X' Y' f g,
begin
dsimp,
simp only [iso.hom_inv_id_app_assoc, equivalence.inv_fun_map],
end,
associativity' := λ X Y Z,
begin
dsimp,
simp only [iso.hom_inv_id_app_assoc, assoc, equivalence.inv_fun_map, functor.map_comp],
slice_lhs 1 2 { rw [←comp_tensor_id, iso.hom_inv_id_app], dsimp, rw [tensor_id], },
simp,
end,
left_unitality' := λ X,
begin
dsimp,
simp only [iso.hom_inv_id_app_assoc, equivalence.unit_inverse_comp, assoc,
equivalence.inv_fun_map, comp_id, functor.map_comp],
slice_rhs 1 2 { rw [←comp_tensor_id, iso.hom_inv_id_app], dsimp, rw [tensor_id], },
simp,
end,
right_unitality' := λ X,
begin
dsimp,
simp only [iso.hom_inv_id_app_assoc, equivalence.unit_inverse_comp, assoc,
equivalence.inv_fun_map, comp_id, functor.map_comp],
slice_rhs 1 2 { rw [←id_tensor_comp, iso.hom_inv_id_app], dsimp, rw [tensor_id], },
simp,
end,
..e.inverse, }
/--
We can upgrade `e.inverse` to a monoidal functor from `D` with the transported structure to `C`.
-/
@[simps]
def from_transported (e : C ≌ D) : monoidal_functor (transported e) C :=
{ ε_is_iso := by { dsimp, apply_instance, },
μ_is_iso := λ X Y, by { dsimp, apply_instance, },
..lax_from_transported e, }
/-- The unit isomorphism upgrades to a monoidal isomorphism. -/
@[simps {rhs_md:=semireducible}]
def transported_monoidal_unit_iso (e : C ≌ D) :
lax_monoidal_functor.id C ≅ lax_to_transported e ⊗⋙ lax_from_transported e :=
monoidal_nat_iso.of_components (λ X, e.unit_iso.app X) (λ X Y f, e.unit.naturality f)
(by { dsimp, simp })
(λ X Y,
begin
dsimp, simp only [iso.hom_inv_id_app_assoc, id_comp, equivalence.inv_fun_map],
slice_rhs 1 2 { rw [←tensor_comp, iso.hom_inv_id_app, iso.hom_inv_id_app],
dsimp, rw [tensor_id] },
simp,
end)
/-- The counit isomorphism upgrades to a monoidal isomorphism. -/
@[simps {rhs_md:=semireducible}]
def transported_monoidal_counit_iso (e : C ≌ D) :
lax_from_transported e ⊗⋙ lax_to_transported e ≅ lax_monoidal_functor.id (transported e) :=
monoidal_nat_iso.of_components (λ X, e.counit_iso.app X) (λ X Y f, e.counit.naturality f)
(by { dsimp, simp })
(λ X Y,
begin
dsimp, simp only [iso.hom_inv_id_app_assoc, id_comp, equivalence.inv_fun_map],
simp only [equivalence.counit_functor, ←e.functor.map_id, ←e.functor.map_comp],
congr' 1,
simp [equivalence.inverse_counit],
dsimp,
simp, -- See note [dsimp, simp].
end)
-- We could put these together as an equivalence of monoidal categories,
-- but I don't want to do this quite yet.
-- Etingof-Gelaki-Nikshych-Ostrik "Tensor categories" define an equivalence of monoidal categories
-- as a monoidal functor which, as a functor, is an equivalence.
-- Presumably one can show that the inverse functor can be upgraded to a monoidal
-- functor in a unique way, such that the unit and counit are monoidal natural isomorphisms,
-- but I've never seen this explained or worked it out.
end category_theory.monoidal
|
4dcf5bdbfc541049d79b6d21d6ea6521d659cd55 | bdb33f8b7ea65f7705fc342a178508e2722eb851 | /data/seq/seq.lean | 0b308c83fccbf13df04a3fae5c08366f7b65d1eb | [
"Apache-2.0"
] | permissive | rwbarton/mathlib | 939ae09bf8d6eb1331fc2f7e067d39567e10e33d | c13c5ea701bb1eec057e0a242d9f480a079105e9 | refs/heads/master | 1,584,015,335,862 | 1,524,142,167,000 | 1,524,142,167,000 | 130,614,171 | 0 | 0 | Apache-2.0 | 1,548,902,667,000 | 1,524,437,371,000 | Lean | UTF-8 | Lean | false | false | 25,150 | lean | import data.stream data.lazy_list data.seq.computation logic.basic tactic.interactive
universes u v w
/-
coinductive seq (α : Type u) : Type u
| nil : seq α
| cons : α → seq α → seq α
-/
/-- `seq α` is the type of possibly infinite lists (referred here as sequences).
It is encoded as an infinite stream of options such that if `f n = none`, then
`f m = none` for all `m ≥ n`. -/
def seq (α : Type u) : Type u := { f : stream (option α) // ∀ {n}, f n = none → f (n+1) = none }
/-- `seq1 α` is the type of nonempty sequences. -/
def seq1 (α) := α × seq α
namespace seq
variables {α : Type u} {β : Type v} {γ : Type w}
/-- The empty sequence -/
def nil : seq α := ⟨stream.const none, λn h, rfl⟩
/-- Prepend an element to a sequence -/
def cons (a : α) : seq α → seq α
| ⟨f, al⟩ := ⟨some a :: f, λn h, by {cases n with n, contradiction, exact al h}⟩
/-- Get the nth element of a sequence (if it exists) -/
def nth : seq α → ℕ → option α := subtype.val
/-- Functorial action of the functor `option (α × _)` -/
@[simp] def omap (f : β → γ) : option (α × β) → option (α × γ)
| none := none
| (some (a, b)) := some (a, f b)
/-- Get the first element of a sequence -/
def head (s : seq α) : option α := nth s 0
/-- Get the tail of a sequence (or `nil` if the sequence is `nil`) -/
def tail : seq α → seq α
| ⟨f, al⟩ := ⟨f.tail, λ n, al⟩
protected def mem (a : α) (s : seq α) := some a ∈ s.1
instance : has_mem α (seq α) :=
⟨seq.mem⟩
theorem le_stable (s : seq α) {m n} (h : m ≤ n) :
s.1 m = none → s.1 n = none :=
by {cases s with f al, induction h with n h IH, exacts [id, λ h2, al (IH h2)]}
theorem not_mem_nil (a : α) : a ∉ @nil α :=
λ ⟨n, (h : some a = none)⟩, by injection h
theorem mem_cons (a : α) : ∀ (s : seq α), a ∈ cons a s
| ⟨f, al⟩ := stream.mem_cons (some a) _
theorem mem_cons_of_mem (y : α) {a : α} : ∀ {s : seq α}, a ∈ s → a ∈ cons y s
| ⟨f, al⟩ := stream.mem_cons_of_mem (some y)
theorem eq_or_mem_of_mem_cons {a b : α} : ∀ {s : seq α}, a ∈ cons b s → a = b ∨ a ∈ s
| ⟨f, al⟩ h := (stream.eq_or_mem_of_mem_cons h).imp_left (λh, by injection h)
@[simp] theorem mem_cons_iff {a b : α} {s : seq α} : a ∈ cons b s ↔ a = b ∨ a ∈ s :=
⟨eq_or_mem_of_mem_cons, λo, by cases o with e m;
[{rw e, apply mem_cons}, exact mem_cons_of_mem _ m]⟩
/-- Destructor for a sequence, resulting in either `none` (for `nil`) or
`some (a, s)` (for `cons a s`). -/
def destruct (s : seq α) : option (seq1 α) :=
(λa', (a', s.tail)) <$> nth s 0
theorem destruct_eq_nil {s : seq α} : destruct s = none → s = nil :=
begin
dsimp [destruct],
induction f0 : nth s 0; intro h,
{ apply subtype.eq,
funext n,
induction n with n IH, exacts [f0, s.2 IH] },
{ contradiction }
end
theorem destruct_eq_cons {s : seq α} {a s'} : destruct s = some (a, s') → s = cons a s' :=
begin
dsimp [destruct],
induction f0 : nth s 0 with a'; intro h,
{ contradiction },
{ unfold functor.map at h, dsimp [option.map, option.bind] at h,
cases s with f al,
injections with _ h1 h2,
rw ←h2, apply subtype.eq, dsimp [tail, cons],
rw h1 at f0, rw ←f0,
exact (stream.eta f).symm }
end
@[simp] theorem destruct_nil : destruct (nil : seq α) = none := rfl
@[simp] theorem destruct_cons (a : α) : ∀ s, destruct (cons a s) = some (a, s)
| ⟨f, al⟩ := begin
unfold cons destruct functor.map,
apply congr_arg (λ s, some (a, s)),
apply subtype.eq, dsimp [tail], rw [stream.tail_cons]
end
theorem head_eq_destruct (s : seq α) : head s = prod.fst <$> destruct s :=
by unfold destruct head; cases nth s 0; refl
@[simp] theorem head_nil : head (nil : seq α) = none := rfl
@[simp] theorem head_cons (a : α) (s) : head (cons a s) = some a :=
by rw [head_eq_destruct, destruct_cons]; refl
@[simp] theorem tail_nil : tail (nil : seq α) = nil := rfl
@[simp] theorem tail_cons (a : α) (s) : tail (cons a s) = s :=
by cases s with f al; apply subtype.eq; dsimp [tail, cons]; rw [stream.tail_cons]
def cases_on {C : seq α → Sort v} (s : seq α)
(h1 : C nil) (h2 : ∀ x s, C (cons x s)) : C s := begin
induction H : destruct s with v v,
{ rw destruct_eq_nil H, apply h1 },
{ cases v with a s', rw destruct_eq_cons H, apply h2 }
end
theorem mem_rec_on {C : seq α → Prop} {a s} (M : a ∈ s)
(h1 : ∀ b s', (a = b ∨ C s') → C (cons b s')) : C s :=
begin
cases M with k e, unfold stream.nth at e,
induction k with k IH generalizing s,
{ have TH : s = cons a (tail s),
{ apply destruct_eq_cons,
unfold destruct nth functor.map, rw ←e, refl },
rw TH, apply h1 _ _ (or.inl rfl) },
revert e, apply s.cases_on _ (λ b s', _); intro e,
{ injection e },
{ have h_eq : (cons b s').val (nat.succ k) = s'.val k, { cases s'; refl },
rw [h_eq] at e,
apply h1 _ _ (or.inr (IH e)) }
end
def corec.F (f : β → option (α × β)) : option β → option α × option β
| none := (none, none)
| (some b) := match f b with none := (none, none) | some (a, b') := (some a, some b') end
/-- Corecursor for `seq α` as a coinductive type. Iterates `f` to produce new elements
of the sequence until `none` is obtained. -/
def corec (f : β → option (α × β)) (b : β) : seq α :=
begin
refine ⟨stream.corec' (corec.F f) (some b), λn h, _⟩,
rw stream.corec'_eq,
change stream.corec' (corec.F f) (corec.F f (some b)).2 n = none,
revert h, generalize : some b = o, revert o,
induction n with n IH; intro o,
{ change (corec.F f o).1 = none → (corec.F f (corec.F f o).2).1 = none,
cases o with b; intro h, { refl },
dsimp [corec.F] at h, dsimp [corec.F],
cases f b with s, { refl },
{ cases s with a b', contradiction } },
{ rw [stream.corec'_eq (corec.F f) (corec.F f o).2,
stream.corec'_eq (corec.F f) o],
exact IH (corec.F f o).2 }
end
@[simp] theorem corec_eq (f : β → option (α × β)) (b : β) :
destruct (corec f b) = omap (corec f) (f b) :=
begin
dsimp [corec, destruct, nth],
change stream.corec' (corec.F f) (some b) 0 with (corec.F f (some b)).1,
unfold functor.map, dsimp [corec.F],
induction h : f b with s, { refl },
cases s with a b', dsimp [corec.F, option.bind],
apply congr_arg (λ b', some (a, b')),
apply subtype.eq,
dsimp [corec, tail],
rw [stream.corec'_eq, stream.tail_cons],
dsimp [corec.F], rw h, refl
end
/-- Embed a list as a sequence -/
def of_list (l : list α) : seq α :=
⟨list.nth l, λn h, begin
induction l with a l IH generalizing n, refl,
dsimp [list.nth], cases n with n; dsimp [list.nth] at h,
{ contradiction },
{ apply IH _ h }
end⟩
instance coe_list : has_coe (list α) (seq α) := ⟨of_list⟩
section bisim
variable (R : seq α → seq α → Prop)
local infix ~ := R
def bisim_o : option (seq1 α) → option (seq1 α) → Prop
| none none := true
| (some (a, s)) (some (a', s')) := a = a' ∧ R s s'
| _ _ := false
attribute [simp] bisim_o
def is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → bisim_o R (destruct s₁) (destruct s₂)
-- If two streams are bisimilar, then they are equal
theorem eq_of_bisim (bisim : is_bisimulation R) {s₁ s₂} (r : s₁ ~ s₂) : s₁ = s₂ :=
begin
apply subtype.eq,
apply stream.eq_of_bisim (λx y, ∃ s s' : seq α, s.1 = x ∧ s'.1 = y ∧ R s s'),
dsimp [stream.is_bisimulation],
intros t₁ t₂ e,
exact match t₁, t₂, e with ._, ._, ⟨s, s', rfl, rfl, r⟩ :=
suffices head s = head s' ∧ R (tail s) (tail s'), from
and.imp id (λr, ⟨tail s, tail s',
by cases s; refl, by cases s'; refl, r⟩) this,
begin
have := bisim r, revert r this,
apply cases_on s _ _; intros; apply cases_on s' _ _; intros; intros r this,
{ constructor, refl, assumption },
{ rw [destruct_nil, destruct_cons] at this,
exact false.elim this },
{ rw [destruct_nil, destruct_cons] at this,
exact false.elim this },
{ rw [destruct_cons, destruct_cons] at this,
rw [head_cons, head_cons, tail_cons, tail_cons],
cases this with h1 h2,
constructor, rw h1, exact h2 }
end
end,
exact ⟨s₁, s₂, rfl, rfl, r⟩
end
end bisim
theorem coinduction : ∀ {s₁ s₂ : seq α}, head s₁ = head s₂ →
(∀ (β : Type u) (fr : seq α → β),
fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) → s₁ = s₂
| ⟨f₁, a₁⟩ ⟨f₂, a₂⟩ hh ht :=
subtype.eq (stream.coinduction hh (λ β fr, ht β (λs, fr s.1)))
theorem coinduction2 (s) (f g : seq α → seq β)
(H : ∀ s, bisim_o (λ (s1 s2 : seq β), ∃ (s : seq α), s1 = f s ∧ s2 = g s)
(destruct (f s)) (destruct (g s)))
: f s = g s :=
begin
refine eq_of_bisim (λ s1 s2, ∃ s, s1 = f s ∧ s2 = g s) _ ⟨s, rfl, rfl⟩,
intros s1 s2 h, cases h with s h, cases h with h1 h2,
rw [h1, h2], apply H
end
/-- Embed an infinite stream as a sequence -/
def of_stream (s : stream α) : seq α :=
⟨s.map some, λn h, by contradiction⟩
instance coe_stream : has_coe (stream α) (seq α) := ⟨of_stream⟩
/-- Embed a `lazy_list α` as a sequence. Note that even though this
is non-meta, it will produce infinite sequences if used with
cyclic `lazy_list`s created by meta constructions. -/
def of_lazy_list : lazy_list α → seq α :=
corec (λl, match l with
| lazy_list.nil := none
| lazy_list.cons a l' := some (a, l' ())
end)
instance coe_lazy_list : has_coe (lazy_list α) (seq α) := ⟨of_lazy_list⟩
/-- Translate a sequence into a `lazy_list`. Since `lazy_list` and `list`
are isomorphic as non-meta types, this function is necessarily meta. -/
meta def to_lazy_list : seq α → lazy_list α | s :=
match destruct s with
| none := lazy_list.nil
| some (a, s') := lazy_list.cons a (to_lazy_list s')
end
/-- Translate a sequence to a list. This function will run forever if
run on an infinite sequence. -/
meta def force_to_list (s : seq α) : list α := (to_lazy_list s).to_list
/-- Append two sequences. If `s₁` is infinite, then `s₁ ++ s₂ = s₁`,
otherwise it puts `s₂` at the location of the `nil` in `s₁`. -/
def append (s₁ s₂ : seq α) : seq α :=
@corec α (seq α × seq α) (λ⟨s₁, s₂⟩,
match destruct s₁ with
| none := omap (λs₂, (nil, s₂)) (destruct s₂)
| some (a, s₁') := some (a, s₁', s₂)
end) (s₁, s₂)
/-- Map a function over a sequence. -/
def map (f : α → β) : seq α → seq β | ⟨s, al⟩ :=
⟨s.map (option.map f),
λn, begin
dsimp [stream.map, stream.nth],
induction e : s n; intro,
{ rw al e, assumption }, { contradiction }
end⟩
/-- Flatten a sequence of sequences. (It is required that the
sequences be nonempty to ensure productivity; in the case
of an infinite sequence of `nil`, the first element is never
generated.) -/
def join : seq (seq1 α) → seq α :=
corec (λS, match destruct S with
| none := none
| some ((a, s), S') := some (a, match destruct s with
| none := S'
| some s' := cons s' S'
end)
end)
/-- Remove the first `n` elements from the sequence. -/
def drop (s : seq α) : ℕ → seq α
| 0 := s
| (n+1) := tail (drop n)
attribute [simp] drop
/-- Take the first `n` elements of the sequence (producing a list) -/
def take : ℕ → seq α → list α
| 0 s := []
| (n+1) s := match destruct s with
| none := []
| some (x, r) := list.cons x (take n r)
end
/-- Split a sequence at `n`, producing a finite initial segment
and an infinite tail. -/
def split_at : ℕ → seq α → list α × seq α
| 0 s := ([], s)
| (n+1) s := match destruct s with
| none := ([], nil)
| some (x, s') := let (l, r) := split_at n s' in (list.cons x l, r)
end
/-- Combine two sequences with a function -/
def zip_with (f : α → β → γ) : seq α → seq β → seq γ
| ⟨f₁, a₁⟩ ⟨f₂, a₂⟩ := ⟨λn,
match f₁ n, f₂ n with
| some a, some b := some (f a b)
| _, _ := none
end,
λn, begin
induction h1 : f₁ n,
{ intro H, rw a₁ h1, refl },
induction h2 : f₂ n; dsimp [seq.zip_with._match_1]; intro H,
{ rw a₂ h2, cases f₁ (n + 1); refl },
{ contradiction }
end⟩
/-- Pair two sequences into a sequence of pairs -/
def zip : seq α → seq β → seq (α × β) := zip_with prod.mk
/-- Separate a sequence of pairs into two sequences -/
def unzip (s : seq (α × β)) : seq α × seq β := (map prod.fst s, map prod.snd s)
/-- Convert a sequence which is known to terminate into a list -/
def to_list (s : seq α) (h : ∃ n, ¬ (nth s n).is_some) : list α :=
take (nat.find h) s
/-- Convert a sequence which is known not to terminate into a stream -/
def to_stream (s : seq α) (h : ∀ n, (nth s n).is_some) : stream α :=
λn, option.get (h n)
/-- Convert a sequence into either a list or a stream depending on whether
it is finite or infinite. (Without decidability of the infiniteness predicate,
this is not constructively possible.) -/
def to_list_or_stream (s : seq α) [decidable (∃ n, ¬ (nth s n).is_some)] :
list α ⊕ stream α :=
if h : ∃ n, ¬ (nth s n).is_some
then sum.inl (to_list s h)
else sum.inr (to_stream s (λn, decidable.by_contradiction (λ hn, h ⟨n, hn⟩)))
@[simp] theorem nil_append (s : seq α) : append nil s = s :=
begin
apply coinduction2, intro s,
dsimp [append], rw [corec_eq],
dsimp [append], apply cases_on s _ _,
{ trivial },
{ intros x s,
rw [destruct_cons], dsimp,
exact ⟨rfl, s, rfl, rfl⟩ }
end
@[simp] theorem cons_append (a : α) (s t) : append (cons a s) t = cons a (append s t) :=
destruct_eq_cons $ begin
dsimp [append], rw [corec_eq],
dsimp [append], rw [destruct_cons],
dsimp [append], refl
end
@[simp] theorem append_nil (s : seq α) : append s nil = s :=
begin
apply coinduction2 s, intro s,
apply cases_on s _ _,
{ trivial },
{ intros x s,
rw [cons_append, destruct_cons, destruct_cons], dsimp,
exact ⟨rfl, s, rfl, rfl⟩ }
end
@[simp] theorem append_assoc (s t u : seq α) :
append (append s t) u = append s (append t u) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s t u,
s1 = append (append s t) u ∧ s2 = append s (append t u)),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, t, u, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on t; simp,
{ apply cases_on u; simp,
{ intros x u, refine ⟨nil, nil, u, _, _⟩; simp } },
{ intros x t, refine ⟨nil, t, u, _, _⟩; simp } },
{ intros x s, exact ⟨s, t, u, rfl, rfl⟩ }
end end },
{ exact ⟨s, t, u, rfl, rfl⟩ }
end
@[simp] theorem map_nil (f : α → β) : map f nil = nil := rfl
@[simp] theorem map_cons (f : α → β) (a) : ∀ s, map f (cons a s) = cons (f a) (map f s)
| ⟨s, al⟩ := by apply subtype.eq; dsimp [cons, map]; rw stream.map_cons; refl
@[simp] theorem map_id : ∀ (s : seq α), map id s = s
| ⟨s, al⟩ := begin
apply subtype.eq; dsimp [map],
rw [option.map_id, stream.map_id]; refl
end
@[simp] theorem map_tail (f : α → β) : ∀ s, map f (tail s) = tail (map f s)
| ⟨s, al⟩ := by apply subtype.eq; dsimp [tail, map]; rw stream.map_tail; refl
theorem map_comp (f : α → β) (g : β → γ) : ∀ (s : seq α), map (g ∘ f) s = map g (map f s)
| ⟨s, al⟩ := begin
apply subtype.eq; dsimp [map],
rw stream.map_map,
apply congr_arg (λ f : _ → option γ, stream.map f s),
funext x, cases x with x; refl
end
@[simp] theorem map_append (f : α → β) (s t) : map f (append s t) = append (map f s) (map f t) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s t,
s1 = map f (append s t) ∧ s2 = append (map f s) (map f t)) _ ⟨s, t, rfl, rfl⟩,
intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, t, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on t; simp,
{ intros x t, refine ⟨nil, t, _, _⟩; simp } },
{ intros x s, refine ⟨s, t, rfl, rfl⟩ }
end end
end
@[simp] theorem map_nth (f : α → β) : ∀ s n, nth (map f s) n = (nth s n).map f
| ⟨s, al⟩ n := rfl
instance : functor seq := {map := @map}
instance : is_lawful_functor seq :=
{ id_map := @map_id, comp_map := @map_comp }
@[simp] theorem join_nil : join nil = (nil : seq α) := destruct_eq_nil rfl
@[simp] theorem join_cons_nil (a : α) (S) :
join (cons (a, nil) S) = cons a (join S) :=
destruct_eq_cons $ by simp [join]
@[simp] theorem join_cons_cons (a b : α) (s S) :
join (cons (a, cons b s) S) = cons a (join (cons (b, s) S)) :=
destruct_eq_cons $ by simp [join]
@[simp] theorem join_cons (a : α) (s S) :
join (cons (a, s) S) = cons a (append s (join S)) :=
begin
apply eq_of_bisim (λs1 s2, s1 = s2 ∨
∃ a s S, s1 = join (cons (a, s) S) ∧
s2 = cons a (append s (join S))) _ (or.inr ⟨a, s, S, rfl, rfl⟩),
intros s1 s2 h,
exact match s1, s2, h with
| _, _, (or.inl $ eq.refl s) := begin
apply cases_on s, { trivial },
{ intros x s, rw [destruct_cons], exact ⟨rfl, or.inl rfl⟩ }
end
| ._, ._, (or.inr ⟨a, s, S, rfl, rfl⟩) := begin
apply cases_on s,
{ simp },
{ intros x s, simp, refine or.inr ⟨x, s, S, rfl, rfl⟩ }
end
end
end
@[simp] theorem join_append (S T : seq (seq1 α)) :
join (append S T) = append (join S) (join T) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s S T,
s1 = append s (join (append S T)) ∧
s2 = append s (append (join S) (join T))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, T, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on S; simp,
{ apply cases_on T, { simp },
{ intros s T, cases s with a s; simp,
refine ⟨s, nil, T, _, _⟩; simp } },
{ intros s S, cases s with a s; simp,
exact ⟨s, S, T, rfl, rfl⟩ } },
{ intros x s, exact ⟨s, S, T, rfl, rfl⟩ }
end end },
{ refine ⟨nil, S, T, _, _⟩; simp }
end
@[simp] theorem of_list_nil : of_list [] = (nil : seq α) := rfl
@[simp] theorem of_list_cons (a : α) (l) :
of_list (a :: l) = cons a (of_list l) :=
begin
apply subtype.eq, simp [of_list, cons],
funext n, cases n; simp [list.nth, stream.cons]
end
@[simp] theorem of_stream_cons (a : α) (s) :
of_stream (a :: s) = cons a (of_stream s) :=
by apply subtype.eq; simp [of_stream, cons]; rw stream.map_cons
@[simp] theorem of_list_append (l l' : list α) :
of_list (l ++ l') = append (of_list l) (of_list l') :=
by induction l; simp [*]
@[simp] theorem of_stream_append (l : list α) (s : stream α) :
of_stream (l ++ₛ s) = append (of_list l) (of_stream s) :=
by induction l; simp [*, stream.nil_append_stream, stream.cons_append_stream]
/-- Convert a sequence into a list, embedded in a computation to allow for
the possibility of infinite sequences (in which case the computation
never returns anything). -/
def to_list' {α} (s : seq α) : computation (list α) :=
@computation.corec (list α) (list α × seq α) (λ⟨l, s⟩,
match destruct s with
| none := sum.inl l.reverse
| some (a, s') := sum.inr (a::l, s')
end) ([], s)
theorem dropn_add (s : seq α) (m) : ∀ n, drop s (m + n) = drop (drop s m) n
| 0 := rfl
| (n+1) := congr_arg tail (dropn_add n)
theorem dropn_tail (s : seq α) (n) : drop (tail s) n = drop s (n + 1) :=
by rw add_comm; symmetry; apply dropn_add
theorem nth_tail : ∀ (s : seq α) n, nth (tail s) n = nth s (n + 1)
| ⟨f, al⟩ n := rfl
@[simp] theorem head_dropn (s : seq α) (n) : head (drop s n) = nth s n :=
begin
induction n with n IH generalizing s, { refl },
rw [nat.succ_eq_add_one, ←nth_tail, ←dropn_tail], apply IH
end
theorem mem_map (f : α → β) {a : α} : ∀ {s : seq α}, a ∈ s → f a ∈ map f s
| ⟨g, al⟩ := stream.mem_map (option.map f)
theorem exists_of_mem_map {f} {b : β} : ∀ {s : seq α}, b ∈ map f s → ∃ a, a ∈ s ∧ f a = b
| ⟨g, al⟩ h := let ⟨o, om, oe⟩ := stream.exists_of_mem_map h in
by cases o with a; injection oe with h'; exact ⟨a, om, h'⟩
theorem of_mem_append {s₁ s₂ : seq α} {a : α} (h : a ∈ append s₁ s₂) : a ∈ s₁ ∨ a ∈ s₂ :=
begin
have := h, revert this,
generalize e : append s₁ s₂ = ss, intro h, revert s₁,
apply mem_rec_on h _,
intros b s' o s₁,
apply s₁.cases_on _ (λ c t₁, _); intros m e;
have := congr_arg destruct e,
{ apply or.inr, simpa using m },
{ cases (show a = c ∨ a ∈ append t₁ s₂, by simpa using m) with e' m,
{ rw e', exact or.inl (mem_cons _ _) },
{ cases (show c = b ∧ append t₁ s₂ = s', by simpa) with i1 i2,
cases o with e' IH,
{ simp [i1, e'] },
{ exact or.imp_left (mem_cons_of_mem _) (IH m i2) } } }
end
theorem mem_append_left {s₁ s₂ : seq α} {a : α} (h : a ∈ s₁) : a ∈ append s₁ s₂ :=
by apply mem_rec_on h; intros; simp [*]
end seq
namespace seq1
variables {α : Type u} {β : Type v} {γ : Type w}
open seq
/-- Convert a `seq1` to a sequence. -/
def to_seq : seq1 α → seq α
| (a, s) := cons a s
instance coe_seq : has_coe (seq1 α) (seq α) := ⟨to_seq⟩
/-- Map a function on a `seq1` -/
def map (f : α → β) : seq1 α → seq1 β
| (a, s) := (f a, seq.map f s)
theorem map_id : ∀ (s : seq1 α), map id s = s | ⟨a, s⟩ := by simp [map]
/-- Flatten a nonempty sequence of nonempty sequences -/
def join : seq1 (seq1 α) → seq1 α
| ((a, s), S) := match destruct s with
| none := (a, seq.join S)
| some s' := (a, seq.join (cons s' S))
end
@[simp] theorem join_nil (a : α) (S) : join ((a, nil), S) = (a, seq.join S) := rfl
@[simp] theorem join_cons (a b : α) (s S) :
join ((a, cons b s), S) = (a, seq.join (cons (b, s) S)) :=
by dsimp [join]; rw [destruct_cons]; refl
/-- The `return` operator for the `seq1` monad,
which produces a singleton sequence. -/
def ret (a : α) : seq1 α := (a, nil)
/-- The `bind` operator for the `seq1` monad,
which maps `f` on each element of `s` and appends the results together.
(Not all of `s` may be evaluated, because the first few elements of `s`
may already produce an infinite result.) -/
def bind (s : seq1 α) (f : α → seq1 β) : seq1 β :=
join (map f s)
@[simp] theorem join_map_ret (s : seq α) : seq.join (seq.map ret s) = s :=
by apply coinduction2 s; intro s; apply cases_on s; simp [ret]
@[simp] theorem bind_ret (f : α → β) : ∀ s, bind s (ret ∘ f) = map f s
| ⟨a, s⟩ := begin
dsimp [bind, map], change (λx, ret (f x)) with (ret ∘ f),
rw [map_comp], simp [function.comp, ret]
end
@[simp] theorem ret_bind (a : α) (f : α → seq1 β) : bind (ret a) f = f a :=
begin
simp [ret, bind, map],
cases f a with a s,
apply cases_on s; intros; simp
end
@[simp] theorem map_join' (f : α → β) (S) :
seq.map f (seq.join S) = seq.join (seq.map (map f) S) :=
begin
apply eq_of_bisim (λs1 s2,
∃ s S, s1 = append s (seq.map f (seq.join S)) ∧
s2 = append s (seq.join (seq.map (map f) S))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on S; simp,
{ intros x S, cases x with a s; simp [map],
exact ⟨_, _, rfl, rfl⟩ } },
{ intros x s, refine ⟨s, S, rfl, rfl⟩ }
end end },
{ refine ⟨nil, S, _, _⟩; simp }
end
@[simp] theorem map_join (f : α → β) : ∀ S, map f (join S) = join (map (map f) S)
| ((a, s), S) := by apply cases_on s; intros; simp [map]
@[simp] theorem join_join (SS : seq (seq1 (seq1 α))) :
seq.join (seq.join SS) = seq.join (seq.map join SS) :=
begin
apply eq_of_bisim (λs1 s2,
∃ s SS, s1 = seq.append s (seq.join (seq.join SS)) ∧
s2 = seq.append s (seq.join (seq.map join SS))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, SS, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on SS; simp,
{ intros S SS, cases S with s S; cases s with x s; simp [map],
apply cases_on s; simp,
{ exact ⟨_, _, rfl, rfl⟩ },
{ intros x s,
refine ⟨cons x (append s (seq.join S)), SS, _, _⟩; simp } } },
{ intros x s, exact ⟨s, SS, rfl, rfl⟩ }
end end },
{ refine ⟨nil, SS, _, _⟩; simp }
end
@[simp] theorem bind_assoc (s : seq1 α) (f : α → seq1 β) (g : β → seq1 γ) :
bind (bind s f) g = bind s (λ (x : α), bind (f x) g) :=
begin
cases s with a s,
simp [bind, map],
rw [←map_comp],
change (λ x, join (map g (f x))) with (join ∘ ((map g) ∘ f)),
rw [map_comp _ join],
generalize : seq.map (map g ∘ f) s = SS,
cases map g (f a) with s S,
cases s with a s,
apply cases_on s; intros; apply cases_on S; intros; simp,
{ cases x with x t, apply cases_on t; intros; simp },
{ cases x_1 with y t; simp }
end
instance : monad seq1 :=
{ map := @map,
pure := @ret,
bind := @bind }
instance : is_lawful_monad seq1 :=
{ id_map := @map_id,
bind_pure_comp_eq_map := @bind_ret,
pure_bind := @ret_bind,
bind_assoc := @bind_assoc }
end seq1
|
8102d5cf7020047e5230a41f50b42e5d32a6f433 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/blast_tuple1.lean | 129c77442884cc5cf332a4a74d85f52a3bb65f72 | [
"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 | 841 | lean | import data.nat
open nat
constant tuple.{l} : Type.{l} → nat → Type.{l}
constant nil {A : Type} : tuple A 0
constant cons {A : Type} {n : nat} : A → tuple A n → tuple A (succ n)
constant append {A : Type} {n m : nat} : tuple A n → tuple A m → tuple A (n + m)
infix ` ++ ` := append
axiom append_nil {A : Type} {n : nat} (v : tuple A n) : v ++ nil = v
axiom nil_append {A : Type} {n : nat} (v : tuple A n) : nil ++ v == v
axiom append_assoc {A : Type} {n₁ n₂ n₃ : nat} (v₁ : tuple A n₁) (v₂ : tuple A n₂) (v₃ : tuple A n₃) : (v₁ ++ v₂) ++ v₃ == v₁ ++ (v₂ ++ v₃)
attribute append_nil nil_append append_assoc [simp]
example (A : Type) (n m : nat) (v₁ v₂ : tuple A n) (w₁ w₂ : tuple A m) :
v₁ ++ nil ++ (v₂ ++ w₁) ++ (nil ++ w₂) == v₁ ++ v₂ ++ w₁ ++ w₂ :=
by inst_simp
|
7d145c09068cf56378142356caec750a76b3f3c4 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/category_theory/functor/category.lean | d19024ee0f9a58c769cc966a444ccb17b1b5e5f2 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 5,285 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn
-/
import category_theory.natural_transformation
import category_theory.isomorphism
/-!
# The category of functors and natural transformations between two fixed categories.
We provide the category instance on `C ⥤ D`, with morphisms the natural transformations.
## Universes
If `C` and `D` are both small categories at the same universe level,
this is another small category at that level.
However if `C` and `D` are both large categories at the same universe level,
this is a small category at the next higher level.
-/
namespace category_theory
-- declare the `v`'s first; see `category_theory.category` for an explanation
universes v₁ v₂ v₃ u₁ u₂ u₃
open nat_trans category category_theory.functor
variables (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D]
local attribute [simp] vcomp_app
/--
`functor.category C D` gives the category structure on functors and natural transformations
between categories `C` and `D`.
Notice that if `C` and `D` are both small categories at the same universe level,
this is another small category at that level.
However if `C` and `D` are both large categories at the same universe level,
this is a small category at the next higher level.
-/
instance functor.category : category.{(max u₁ v₂)} (C ⥤ D) :=
{ hom := λ F G, nat_trans F G,
id := λ F, nat_trans.id F,
comp := λ _ _ _ α β, vcomp α β }
variables {C D} {E : Type u₃} [category.{v₃} E]
variables {F G H I : C ⥤ D}
namespace nat_trans
@[simp] lemma vcomp_eq_comp (α : F ⟶ G) (β : G ⟶ H) : vcomp α β = α ≫ β := rfl
lemma vcomp_app' (α : F ⟶ G) (β : G ⟶ H) (X : C) :
(α ≫ β).app X = (α.app X) ≫ (β.app X) := rfl
lemma congr_app {α β : F ⟶ G} (h : α = β) (X : C) : α.app X = β.app X := by rw h
@[simp] lemma id_app (F : C ⥤ D) (X : C) : (𝟙 F : F ⟶ F).app X = 𝟙 (F.obj X) := rfl
@[simp] lemma comp_app {F G H : C ⥤ D} (α : F ⟶ G) (β : G ⟶ H) (X : C) :
(α ≫ β).app X = α.app X ≫ β.app X := rfl
lemma app_naturality {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (X : C) {Y Z : D} (f : Y ⟶ Z) :
((F.obj X).map f) ≫ ((T.app X).app Z) = ((T.app X).app Y) ≫ ((G.obj X).map f) :=
(T.app X).naturality f
lemma naturality_app {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (Z : D) {X Y : C} (f : X ⟶ Y) :
((F.map f).app Z) ≫ ((T.app Y).app Z) = ((T.app X).app Z) ≫ ((G.map f).app Z) :=
congr_fun (congr_arg app (T.naturality f)) Z
/-- A natural transformation is a monomorphism if each component is. -/
lemma mono_app_of_mono (α : F ⟶ G) [∀ (X : C), mono (α.app X)] : mono α :=
⟨λ H g h eq, by { ext X, rw [←cancel_mono (α.app X), ←comp_app, eq, comp_app] }⟩
/-- A natural transformation is an epimorphism if each component is. -/
lemma epi_app_of_epi (α : F ⟶ G) [∀ (X : C), epi (α.app X)] : epi α :=
⟨λ H g h eq, by { ext X, rw [←cancel_epi (α.app X), ←comp_app, eq, comp_app] }⟩
/-- `hcomp α β` is the horizontal composition of natural transformations. -/
@[simps] def hcomp {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) : (F ⋙ H) ⟶ (G ⋙ I) :=
{ app := λ X : C, (β.app (F.obj X)) ≫ (I.map (α.app X)),
naturality' := λ X Y f,
begin
rw [functor.comp_map, functor.comp_map, ←assoc, naturality, assoc,
←map_comp I, naturality, map_comp, assoc]
end }
infix ` ◫ `:80 := hcomp
@[simp] lemma hcomp_id_app {H : D ⥤ E} (α : F ⟶ G) (X : C) : (α ◫ 𝟙 H).app X = H.map (α.app X) :=
by {dsimp, simp} -- See note [dsimp, simp].
lemma id_hcomp_app {H : E ⥤ C} (α : F ⟶ G) (X : E) : (𝟙 H ◫ α).app X = α.app _ := by simp
-- Note that we don't yet prove a `hcomp_assoc` lemma here: even stating it is painful, because we
-- need to use associativity of functor composition. (It's true without the explicit associator,
-- because functor composition is definitionally associative,
-- but relying on the definitional equality causes bad problems with elaboration later.)
lemma exchange {I J K : D ⥤ E} (α : F ⟶ G) (β : G ⟶ H)
(γ : I ⟶ J) (δ : J ⟶ K) : (α ≫ β) ◫ (γ ≫ δ) = (α ◫ γ) ≫ (β ◫ δ) :=
by ext; simp
end nat_trans
open nat_trans
namespace functor
/-- Flip the arguments of a bifunctor. See also `currying.lean`. -/
@[simps] protected def flip (F : C ⥤ (D ⥤ E)) : D ⥤ (C ⥤ E) :=
{ obj := λ k,
{ obj := λ j, (F.obj j).obj k,
map := λ j j' f, (F.map f).app k,
map_id' := λ X, begin rw category_theory.functor.map_id, refl end,
map_comp' := λ X Y Z f g, by rw [map_comp, ←comp_app] },
map := λ c c' f,
{ app := λ j, (F.obj j).map f } }.
end functor
@[simp, reassoc] lemma map_hom_inv_app (F : C ⥤ D ⥤ E) {X Y : C} (e : X ≅ Y) (Z : D) :
(F.map e.hom).app Z ≫ (F.map e.inv).app Z = 𝟙 _ :=
by simp [← nat_trans.comp_app, ← functor.map_comp]
@[simp, reassoc] lemma map_inv_hom_app (F : C ⥤ D ⥤ E) {X Y : C} (e : X ≅ Y) (Z : D) :
(F.map e.inv).app Z ≫ (F.map e.hom).app Z = 𝟙 _ :=
by simp [← nat_trans.comp_app, ← functor.map_comp]
end category_theory
|
e464992abbbdac7ac19d217bc5e3349a906ead8e | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/sheaves/sheaf_auto.lean | 6c0bffd6a0294aaed4529a2f09109c377c6ffa99 | [] | 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 | 4,528 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.sheaves.sheaf_condition.equalizer_products
import Mathlib.category_theory.full_subcategory
import Mathlib.PostPort
universes u v l
namespace Mathlib
/-!
# Sheaves
We define sheaves on a topological space, with values in an arbitrary category with products.
The sheaf condition for a `F : presheaf C X` requires that the morphism
`F.obj U ⟶ ∏ F.obj (U i)` (where `U` is some open set which is the union of the `U i`)
is the equalizer of the two morphisms
`∏ F.obj (U i) ⟶ ∏ F.obj (U i ⊓ U j)`.
We provide the instance `category (sheaf C X)` as the full subcategory of presheaves,
and the fully faithful functor `sheaf.forget : sheaf C X ⥤ presheaf C X`.
## Equivalent conditions
While the "official" definition is in terms of an equalizer diagram,
in `src/topology/sheaves/sheaf_condition/pairwise_intersections.lean`
and in `src/topology/sheaves/sheaf_condition/open_le_cover.lean`
we provide two equivalent conditions (and prove they are equivalent).
The first is that `F.obj U` is the limit point of the diagram consisting of all the `F.obj (U i)`
and `F.obj (U i ⊓ U j)`.
(That is, we explode the equalizer of two products out into its component pieces.)
The second is that `F.obj U` is the limit point of the diagram constisting of all the `F.obj V`,
for those `V : opens X` such that `V ≤ U i` for some `i`.
(This condition is particularly easy to state, and perhaps should become the "official" definition.)
-/
namespace Top
namespace presheaf
/--
The sheaf condition for a `F : presheaf C X` requires that the morphism
`F.obj U ⟶ ∏ F.obj (U i)` (where `U` is some open set which is the union of the `U i`)
is the equalizer of the two morphisms
`∏ F.obj (U i) ⟶ ∏ F.obj (U i) ⊓ (U j)`.
-/
-- One might prefer to work with sets of opens, rather than indexed families,
-- which would reduce the universe level here to `max u v`.
-- However as it's a subsingleton the universe level doesn't matter much.
def sheaf_condition {C : Type u} [category_theory.category C]
[category_theory.limits.has_products C] {X : Top} (F : presheaf C X) :=
{ι : Type v} →
(U : ι → topological_space.opens ↥X) →
category_theory.limits.is_limit (sheaf_condition_equalizer_products.fork F U)
/--
The presheaf valued in `punit` over any topological space is a sheaf.
-/
def sheaf_condition_punit {X : Top} (F : presheaf (category_theory.discrete PUnit) X) :
sheaf_condition F :=
fun (ι : Type v) (U : ι → topological_space.opens ↥X) =>
category_theory.limits.punit_cone_is_limit
-- Let's construct a trivial example, to keep the inhabited linter happy.
protected instance sheaf_condition_inhabited {X : Top}
(F : presheaf (category_theory.discrete PUnit) X) : Inhabited (sheaf_condition F) :=
{ default := sheaf_condition_punit F }
/--
Transfer the sheaf condition across an isomorphism of presheaves.
-/
def sheaf_condition_equiv_of_iso {C : Type u} [category_theory.category C]
[category_theory.limits.has_products C] {X : Top} {F : presheaf C X} {G : presheaf C X}
(α : F ≅ G) : sheaf_condition F ≃ sheaf_condition G :=
sorry
end presheaf
/--
A `sheaf C X` is a presheaf of objects from `C` over a (bundled) topological space `X`,
satisfying the sheaf condition.
-/
structure sheaf (C : Type u) [category_theory.category C] [category_theory.limits.has_products C]
(X : Top)
where
presheaf : presheaf C X
sheaf_condition : presheaf.sheaf_condition presheaf
protected instance sheaf.category_theory.category (C : Type u) [category_theory.category C]
[category_theory.limits.has_products C] (X : Top) : category_theory.category (sheaf C X) :=
category_theory.induced_category.category sheaf.presheaf
-- Let's construct a trivial example, to keep the inhabited linter happy.
protected instance sheaf_inhabited (X : Top) :
Inhabited (sheaf (category_theory.discrete PUnit) X) :=
{ default :=
sheaf.mk (category_theory.functor.star (topological_space.opens ↥Xᵒᵖ)) Inhabited.default }
namespace sheaf
/--
The forgetful functor from sheaves to presheaves.
-/
def forget (C : Type u) [category_theory.category C] [category_theory.limits.has_products C]
(X : Top) : sheaf C X ⥤ presheaf C X :=
category_theory.induced_functor presheaf
end Mathlib |
d5123052ee90821a650bd2e098371fead6d06e94 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/groupoid.lean | a48a7b01338e61890eac30e172851e4465221803 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,992 | lean | /-
Copyright (c) 2018 Reid Barton All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton, Scott Morrison, David Wärn
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.epi_mono
import Mathlib.PostPort
universes v u l u₂
namespace Mathlib
namespace category_theory
/-- A `groupoid` is a category such that all morphisms are isomorphisms. -/
class groupoid (obj : Type u)
extends category obj
where
inv : {X Y : obj} → (X ⟶ Y) → (Y ⟶ X)
inv_comp' : autoParam (∀ {X Y : obj} (f : X ⟶ Y), inv f ≫ f = 𝟙)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
comp_inv' : autoParam (∀ {X Y : obj} (f : X ⟶ Y), f ≫ inv f = 𝟙)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
@[simp] theorem groupoid.inv_comp {obj : Type u} [c : groupoid obj] {X : obj} {Y : obj} (f : X ⟶ Y) : groupoid.inv f ≫ f = 𝟙 := sorry
@[simp] theorem groupoid.comp_inv {obj : Type u} [c : groupoid obj] {X : obj} {Y : obj} (f : X ⟶ Y) : f ≫ groupoid.inv f = 𝟙 := sorry
/--
A `large_groupoid` is a groupoid
where the objects live in `Type (u+1)` while the morphisms live in `Type u`.
-/
/--
def large_groupoid (C : Type (u + 1)) :=
groupoid C
A `small_groupoid` is a groupoid
where the objects and morphisms live in the same universe.
-/
def small_groupoid (C : Type u) :=
groupoid C
protected instance is_iso.of_groupoid {C : Type u} [groupoid C] {X : C} {Y : C} (f : X ⟶ Y) : is_iso f :=
is_iso.mk (groupoid.inv f)
/-- In a groupoid, isomorphisms are equivalent to morphisms. -/
def groupoid.iso_equiv_hom {C : Type u} [groupoid C] (X : C) (Y : C) : (X ≅ Y) ≃ (X ⟶ Y) :=
equiv.mk iso.hom (fun (f : X ⟶ Y) => as_iso f) sorry sorry
/-- A category where every morphism `is_iso` is a groupoid. -/
def groupoid.of_is_iso {C : Type u} [category C] (all_is_iso : {X Y : C} → (f : X ⟶ Y) → is_iso f) : groupoid C :=
groupoid.mk fun (X Y : C) (f : X ⟶ Y) => inv f
/-- A category where every morphism has a `trunc` retraction is computably a groupoid. -/
def groupoid.of_trunc_split_mono {C : Type u} [category C] (all_split_mono : {X Y : C} → (f : X ⟶ Y) → trunc (split_mono f)) : groupoid C :=
groupoid.of_is_iso
fun (X Y : C) (f : X ⟶ Y) =>
trunc.rec_on_subsingleton (all_split_mono f)
fun (a : split_mono f) =>
trunc.rec_on_subsingleton (all_split_mono (retraction f))
fun (a_1 : split_mono (retraction f)) => is_iso.of_mono_retraction
protected instance induced_category.groupoid {C : Type u} (D : Type u₂) [groupoid D] (F : C → D) : groupoid (induced_category D F) :=
groupoid.mk fun (X Y : induced_category D F) (f : X ⟶ Y) => groupoid.inv f
|
9f5a976456e522fea5eef5f1ca5f055ac1c79242 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/topology/uniform_space/compare_reals.lean | 4da0b8014a4e1070eb06c58f0b30d6fa0c041fb6 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 4,836 | lean | /-
Copyright (c) 2019 Patrick MAssot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
-/
import topology.uniform_space.absolute_value
import topology.instances.real
import topology.instances.rat
import topology.uniform_space.completion
/-!
# Comparison of Cauchy reals and Bourbaki reals
In `data.real.basic` real numbers are defined using the so called Cauchy construction (although
it is due to Georg Cantor). More precisely, this construction applies to commutative rings equipped
with an absolute value with values in a linear ordered field.
On the other hand, in the `uniform_space` folder, we construct completions of general uniform
spaces, which allows to construct the Bourbaki real numbers. In this file we build uniformly
continuous bijections from Cauchy reals to Bourbaki reals and back. This is a cross sanity check of
both constructions. Of course those two constructions are variations on the completion idea, simply
with different level of generality. Comparing with Dedekind cuts or quasi-morphisms would be of a
completely different nature.
Note that `metric_space/cau_seq_filter` also relates the notions of Cauchy sequences in metric
spaces and Cauchy filters in general uniform spaces, and `metric_space/completion` makes sure
the completion (as a uniform space) of a metric space is a metric space.
Historical note: mathlib used to define real numbers in an intermediate way, using completion
of uniform spaces but extending multiplication in an ad-hoc way.
TODO:
* Upgrade this isomorphism to a topological ring isomorphism.
* Do the same comparison for p-adic numbers
## Implementation notes
The heavy work is done in `topology/uniform_space/abstract_completion` which provides an abstract
caracterization of completions of uniform spaces, and isomorphisms between them. The only work left
here is to prove the uniform space structure coming from the absolute value on ℚ (with values in ℚ,
not referring to ℝ) coincides with the one coming from the metric space structure (which of course
does use ℝ).
## References
* [N. Bourbaki, *Topologie générale*][bourbaki1966]
## Tags
real numbers, completion, uniform spaces
-/
open set function filter cau_seq uniform_space
/-- The metric space uniform structure on ℚ (which presupposes the existence
of real numbers) agrees with the one coming directly from (abs : ℚ → ℚ). -/
lemma rat.uniform_space_eq :
is_absolute_value.uniform_space (abs : ℚ → ℚ) = pseudo_metric_space.to_uniform_space :=
begin
ext s,
erw [metric.mem_uniformity_dist, is_absolute_value.mem_uniformity],
split ; rintro ⟨ε, ε_pos, h⟩,
{ use [ε, by exact_mod_cast ε_pos],
intros a b hab,
apply h,
rw [rat.dist_eq, abs_sub_comm] at hab,
exact_mod_cast hab },
{ obtain ⟨ε', h', h''⟩ : ∃ ε' : ℚ, 0 < ε' ∧ (ε' : ℝ) < ε, from exists_pos_rat_lt ε_pos,
use [ε', h'],
intros a b hab,
apply h,
rw [rat.dist_eq, abs_sub_comm],
refine lt_trans _ h'',
exact_mod_cast hab }
end
/-- Cauchy reals packaged as a completion of ℚ using the absolute value route. -/
def rational_cau_seq_pkg : @abstract_completion ℚ $ is_absolute_value.uniform_space (abs : ℚ → ℚ) :=
{ space := ℝ,
coe := (coe : ℚ → ℝ),
uniform_struct := by apply_instance,
complete := by apply_instance,
separation := by apply_instance,
uniform_inducing := by { rw rat.uniform_space_eq,
exact rat.uniform_embedding_coe_real.to_uniform_inducing },
dense := rat.dense_embedding_coe_real.dense }
namespace compare_reals
/-- Type wrapper around ℚ to make sure the absolute value uniform space instance is picked up
instead of the metric space one. We proved in rat.uniform_space_eq that they are equal,
but they are not definitionaly equal, so it would confuse the type class system (and probably
also human readers). -/
@[derive comm_ring, derive inhabited] def Q := ℚ
instance : uniform_space Q := is_absolute_value.uniform_space (abs : ℚ → ℚ)
/-- Real numbers constructed as in Bourbaki. -/
@[derive inhabited]
def Bourbakiℝ : Type := completion Q
instance bourbaki.uniform_space: uniform_space Bourbakiℝ := completion.uniform_space Q
/-- Bourbaki reals packaged as a completion of Q using the general theory. -/
def Bourbaki_pkg : abstract_completion Q := completion.cpkg
/-- The uniform bijection between Bourbaki and Cauchy reals. -/
noncomputable def compare_equiv : Bourbakiℝ ≃ᵤ ℝ :=
Bourbaki_pkg.compare_equiv rational_cau_seq_pkg
lemma compare_uc : uniform_continuous (compare_equiv) :=
Bourbaki_pkg.uniform_continuous_compare_equiv _
lemma compare_uc_symm : uniform_continuous (compare_equiv).symm :=
Bourbaki_pkg.uniform_continuous_compare_equiv_symm _
end compare_reals
|
a4cc860a4fde8aa86ec8b310021eb78d94eaf295 | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/group_theory/submonoid/membership.lean | 849d63fdeb40dcca1e8248357f8534f62275b7f1 | [
"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 | 14,028 | 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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard,
Amelia Livingston, Yury Kudryashov
-/
import group_theory.submonoid.operations
import algebra.big_operators.basic
import algebra.free_monoid
import algebra.pointwise
/-!
# Submonoids: membership criteria
In this file we prove various facts about membership in a submonoid:
* `list_prod_mem`, `multiset_prod_mem`, `prod_mem`: if each element of a collection belongs
to a multiplicative submonoid, then so does their product;
* `list_sum_mem`, `multiset_sum_mem`, `sum_mem`: if each element of a collection belongs
to an additive submonoid, then so does their sum;
* `pow_mem`, `nsmul_mem`: if `x ∈ S` where `S` is a multiplicative (resp., additive) submonoid and
`n` is a natural number, then `x^n` (resp., `n • x`) belongs to `S`;
* `mem_supr_of_directed`, `coe_supr_of_directed`, `mem_Sup_of_directed_on`,
`coe_Sup_of_directed_on`: the supremum of a directed collection of submonoid is their union.
* `sup_eq_range`, `mem_sup`: supremum of two submonoids `S`, `T` of a commutative monoid is the set
of products;
* `closure_singleton_eq`, `mem_closure_singleton`: the multiplicative (resp., additive) closure
of `{x}` consists of powers (resp., natural multiples) of `x`.
## Tags
submonoid, submonoids
-/
open_locale big_operators pointwise
variables {M : Type*}
variables {A : Type*}
namespace submonoid
section assoc
variables [monoid M] (S : submonoid M)
@[simp, norm_cast] theorem coe_pow (x : S) (n : ℕ) : ↑(x ^ n) = (x ^ n : M) :=
S.subtype.map_pow x n
@[simp, norm_cast] theorem coe_list_prod (l : list S) : (l.prod : M) = (l.map coe).prod :=
S.subtype.map_list_prod l
@[simp, norm_cast] theorem coe_multiset_prod {M} [comm_monoid M] (S : submonoid M)
(m : multiset S) : (m.prod : M) = (m.map coe).prod :=
S.subtype.map_multiset_prod m
@[simp, norm_cast] theorem coe_finset_prod {ι M} [comm_monoid M] (S : submonoid M)
(f : ι → S) (s : finset ι) :
↑(∏ i in s, f i) = (∏ i in s, f i : M) :=
S.subtype.map_prod f s
/-- Product of a list of elements in a submonoid is in the submonoid. -/
@[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."]
lemma list_prod_mem : ∀ {l : list M}, (∀x ∈ l, x ∈ S) → l.prod ∈ S
| [] h := S.one_mem
| (a::l) h :=
suffices a * l.prod ∈ S, by rwa [list.prod_cons],
have a ∈ S ∧ (∀ x ∈ l, x ∈ S), from list.forall_mem_cons.1 h,
S.mul_mem this.1 (list_prod_mem this.2)
/-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/
@[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is
in the `add_submonoid`."]
lemma multiset_prod_mem {M} [comm_monoid M] (S : submonoid M) (m : multiset M) :
(∀a ∈ m, a ∈ S) → m.prod ∈ S :=
begin
refine quotient.induction_on m (assume l hl, _),
rw [multiset.quot_mk_to_coe, multiset.coe_prod],
exact S.list_prod_mem hl
end
/-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the
submonoid. -/
@[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset`
is in the `add_submonoid`."]
lemma prod_mem {M : Type*} [comm_monoid M] (S : submonoid M)
{ι : Type*} {t : finset ι} {f : ι → M} (h : ∀c ∈ t, f c ∈ S) :
∏ c in t, f c ∈ S :=
S.multiset_prod_mem (t.1.map f) $ λ x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix ▸ h i hi
lemma pow_mem {x : M} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S :=
by simpa only [coe_pow] using ((⟨x, hx⟩ : S) ^ n).coe_prop
end assoc
section non_assoc
variables [mul_one_class M] (S : submonoid M)
open set
@[to_additive]
lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S)
{x : M} :
x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i :=
begin
refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩,
suffices : x ∈ closure (⋃ i, (S i : set M)) → ∃ i, x ∈ S i,
by simpa only [closure_Union, closure_eq (S _)] using this,
refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _),
{ exact hι.elim (λ i, ⟨i, (S i).one_mem⟩) },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
rcases hS i j with ⟨k, hki, hkj⟩,
exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ }
end
@[to_additive]
lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) :
((⨆ i, S i : submonoid M) : set M) = ⋃ i, ↑(S i) :=
set.ext $ λ x, by simp [mem_supr_of_directed hS]
@[to_additive]
lemma mem_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty)
(hS : directed_on (≤) S) {x : M} :
x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s :=
begin
haveI : nonempty S := Sne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk]
end
@[to_additive]
lemma coe_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) :
(↑(Sup S) : set M) = ⋃ s ∈ S, ↑s :=
set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS]
@[to_additive]
lemma mem_sup_left {S T : submonoid M} : ∀ {x : M}, x ∈ S → x ∈ S ⊔ T :=
show S ≤ S ⊔ T, from le_sup_left
@[to_additive]
lemma mem_sup_right {S T : submonoid M} : ∀ {x : M}, x ∈ T → x ∈ S ⊔ T :=
show T ≤ S ⊔ T, from le_sup_right
@[to_additive]
lemma mem_supr_of_mem {ι : Type*} {S : ι → submonoid M} (i : ι) :
∀ {x : M}, x ∈ S i → x ∈ supr S :=
show S i ≤ supr S, from le_supr _ _
@[to_additive]
lemma mem_Sup_of_mem {S : set (submonoid M)} {s : submonoid M}
(hs : s ∈ S) : ∀ {x : M}, x ∈ s → x ∈ Sup S :=
show s ≤ Sup S, from le_Sup hs
end non_assoc
end submonoid
namespace free_monoid
variables {α : Type*}
open submonoid
@[to_additive]
theorem closure_range_of : closure (set.range $ @of α) = ⊤ :=
eq_top_iff.2 $ λ x hx, free_monoid.rec_on x (one_mem _) $ λ x xs hxs,
mul_mem _ (subset_closure $ set.mem_range_self _) hxs
end free_monoid
namespace submonoid
variables [monoid M]
open monoid_hom
lemma closure_singleton_eq (x : M) : closure ({x} : set M) = (powers_hom M x).mrange :=
closure_eq_of_le (set.singleton_subset_iff.2 ⟨multiplicative.of_add 1, pow_one x⟩) $
λ x ⟨n, hn⟩, hn ▸ pow_mem _ (subset_closure $ set.mem_singleton _) _
/-- The submonoid generated by an element of a monoid equals the set of natural number powers of
the element. -/
lemma mem_closure_singleton {x y : M} : y ∈ closure ({x} : set M) ↔ ∃ n:ℕ, x^n=y :=
by rw [closure_singleton_eq, mem_mrange]; refl
lemma mem_closure_singleton_self {y : M} : y ∈ closure ({y} : set M) :=
mem_closure_singleton.2 ⟨1, pow_one y⟩
lemma closure_singleton_one : closure ({1} : set M) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
@[to_additive]
lemma closure_eq_mrange (s : set M) : closure s = (free_monoid.lift (coe : s → M)).mrange :=
by rw [mrange_eq_map, ← free_monoid.closure_range_of, map_mclosure, ← set.range_comp,
free_monoid.lift_comp_of, subtype.range_coe]
@[to_additive]
lemma exists_list_of_mem_closure {s : set M} {x : M} (hx : x ∈ closure s) :
∃ (l : list M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x :=
begin
rw [closure_eq_mrange, mem_mrange] at hx,
rcases hx with ⟨l, hx⟩,
exact ⟨list.map coe l, λ y hy, let ⟨z, hz, hy⟩ := list.mem_map.1 hy in hy ▸ z.2, hx⟩
end
/-- The submonoid generated by an element. -/
def powers (n : M) : submonoid M :=
submonoid.copy (powers_hom M n).mrange (set.range ((^) n : ℕ → M)) $
set.ext (λ n, exists_congr $ λ i, by simp; refl)
@[simp] lemma mem_powers (n : M) : n ∈ powers n := ⟨1, pow_one _⟩
lemma mem_powers_iff (x z : M) : x ∈ powers z ↔ ∃ n : ℕ, z ^ n = x := iff.rfl
lemma powers_eq_closure (n : M) : powers n = closure {n} :=
by { ext, exact mem_closure_singleton.symm }
lemma powers_subset {n : M} {P : submonoid M} (h : n ∈ P) : powers n ≤ P :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := P.pow_mem h i end
end submonoid
namespace submonoid
variables {N : Type*} [comm_monoid N]
open monoid_hom
@[to_additive]
lemma sup_eq_range (s t : submonoid N) : s ⊔ t = (s.subtype.coprod t.subtype).mrange :=
by rw [mrange_eq_map, ← mrange_inl_sup_mrange_inr, map_sup, map_mrange, coprod_comp_inl,
map_mrange, coprod_comp_inr, range_subtype, range_subtype]
@[to_additive]
lemma mem_sup {s t : submonoid N} {x : N} :
x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x :=
by simp only [sup_eq_range, mem_mrange, coprod_apply, prod.exists, set_like.exists,
coe_subtype, subtype.coe_mk]
end submonoid
namespace add_submonoid
variables [add_monoid A]
open set
lemma nsmul_mem (S : add_submonoid A) {x : A} (hx : x ∈ S) :
∀ n : ℕ, n • x ∈ S
| 0 := by { rw zero_nsmul, exact S.zero_mem }
| (n+1) := by { rw [add_nsmul, one_nsmul], exact S.add_mem (nsmul_mem n) hx }
lemma closure_singleton_eq (x : A) : closure ({x} : set A) = (multiples_hom A x).mrange :=
closure_eq_of_le (set.singleton_subset_iff.2 ⟨1, one_nsmul x⟩) $
λ x ⟨n, hn⟩, hn ▸ nsmul_mem _ (subset_closure $ set.mem_singleton _) _
/-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of
natural number multiples of the element. -/
lemma mem_closure_singleton {x y : A} :
y ∈ closure ({x} : set A) ↔ ∃ n:ℕ, n • x = y :=
by rw [closure_singleton_eq, add_monoid_hom.mem_mrange]; refl
lemma closure_singleton_zero : closure ({0} : set A) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton, nsmul_zero]
/-- The additive submonoid generated by an element. -/
def multiples (x : A) : add_submonoid A :=
add_submonoid.copy (multiples_hom A x).mrange (set.range (λ i, nsmul i x : ℕ → A)) $
set.ext (λ n, exists_congr $ λ i, by simp; refl)
@[simp] lemma mem_multiples (x : A) : x ∈ multiples x := ⟨1, one_nsmul _⟩
lemma mem_multiples_iff (x z : A) : x ∈ multiples z ↔ ∃ n : ℕ, n • z = x := iff.rfl
lemma multiples_eq_closure (x : A) : multiples x = closure {x} :=
by { ext, exact mem_closure_singleton.symm }
lemma multiples_subset {x : A} {P : add_submonoid A} (h : x ∈ P) : multiples x ≤ P :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := P.nsmul_mem h i end
attribute [to_additive add_submonoid.multiples] submonoid.powers
attribute [to_additive add_submonoid.mem_multiples] submonoid.mem_powers
attribute [to_additive add_submonoid.mem_multiples_iff] submonoid.mem_powers_iff
attribute [to_additive add_submonoid.multiples_eq_closure] submonoid.powers_eq_closure
attribute [to_additive add_submonoid.multiples_subset] submonoid.powers_subset
end add_submonoid
/-! Lemmas about additive closures of `submonoid`. -/
namespace submonoid
variables {R : Type*} [non_assoc_semiring R] (S : submonoid R) {a b : R}
/-- The product of an element of the additive closure of a multiplicative submonoid `M`
and an element of `M` is contained in the additive closure of `M`. -/
lemma mul_right_mem_add_closure
(ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ S) :
a * b ∈ add_submonoid.closure (S : set R) :=
begin
revert b,
refine add_submonoid.closure_induction ha _ _ _; clear ha a,
{ exact λ r hr b hb, add_submonoid.mem_closure.mpr (λ y hy, hy (S.mul_mem hr hb)) },
{ exact λ b hb, by simp only [zero_mul, (add_submonoid.closure (S : set R)).zero_mem] },
{ simp_rw add_mul,
exact λ r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) }
end
/-- The product of two elements of the additive closure of a submonoid `M` is an element of the
additive closure of `M`. -/
lemma mul_mem_add_closure
(ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ add_submonoid.closure (S : set R)) :
a * b ∈ add_submonoid.closure (S : set R) :=
begin
revert a,
refine add_submonoid.closure_induction hb _ _ _; clear hb b,
{ exact λ r hr b hb, S.mul_right_mem_add_closure hb hr },
{ exact λ b hb, by simp only [mul_zero, (add_submonoid.closure (S : set R)).zero_mem] },
{ simp_rw mul_add,
exact λ r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) }
end
/-- The product of an element of `S` and an element of the additive closure of a multiplicative
submonoid `S` is contained in the additive closure of `S`. -/
lemma mul_left_mem_add_closure (ha : a ∈ S) (hb : b ∈ add_submonoid.closure (S : set R)) :
a * b ∈ add_submonoid.closure (S : set R) :=
S.mul_mem_add_closure (add_submonoid.mem_closure.mpr (λ sT hT, hT ha)) hb
@[to_additive]
lemma mem_closure_inv {G : Type*} [group G] (S : set G) (x : G) :
x ∈ submonoid.closure S⁻¹ ↔ x⁻¹ ∈ submonoid.closure S :=
begin
suffices : ∀ (S : set G) (x : G), x ∈ submonoid.closure S⁻¹ → x⁻¹ ∈ submonoid.closure S,
{ refine ⟨this S x, _⟩,
have := this S⁻¹ x⁻¹,
rw [inv_inv, set.inv_inv] at this,
exact this, },
intros S x hx,
refine submonoid.closure_induction hx (λ x hx, _) _ (λ x y hx hy, _),
{ exact submonoid.subset_closure (set.mem_inv.mp hx), },
{ rw one_inv,
exact submonoid.one_mem _ },
{ rw mul_inv_rev x y,
exact submonoid.mul_mem _ hy hx },
end
end submonoid
section mul_add
lemma of_mul_image_powers_eq_multiples_of_mul [monoid M] {x : M} :
additive.of_mul '' ((submonoid.powers x) : set M) = add_submonoid.multiples (additive.of_mul x) :=
begin
ext,
split,
{ rintros ⟨y, ⟨n, hy1⟩, hy2⟩,
use n,
simpa [← of_mul_pow, hy1] },
{ rintros ⟨n, hn⟩,
refine ⟨x ^ n, ⟨n, rfl⟩, _⟩,
rwa of_mul_pow }
end
lemma of_add_image_multiples_eq_powers_of_add [add_monoid A] {x : A} :
multiplicative.of_add '' ((add_submonoid.multiples x) : set A) =
submonoid.powers (multiplicative.of_add x) :=
begin
symmetry,
rw equiv.eq_image_iff_symm_image_eq,
exact of_mul_image_powers_eq_multiples_of_mul,
end
end mul_add
|
c78c8e01649336c06274823f8f9db30f822e9ab1 | 2c096fdfecf64e46ea7bc6ce5521f142b5926864 | /tests/pkg/misc/Misc.lean | ec165e72ce7158a4b56ba1c15ea907512d20a72c | [
"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 | 360 | lean | import Misc.Foo
import Lean
open Lean Meta
#eval id (α := MetaM Unit) do
assert! (← getEnv).getModuleIdxFor? ``foo == (← getEnv).getModuleIdxFor? `auxDecl1
IO.println <| (← getEnv).getModuleIdxFor? ``Lean.Environment
IO.println <| (← getEnv).getModuleIdxFor? ``foo
IO.println <| (← getEnv).getModuleIdxFor? `auxDecl1
IO.println "worked"
|
7d06646e377ac6e5d3d77408037bab262ebee0e6 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/group_theory/free_product.lean | 8471ceda859890983a18c488306c072cd014e7c1 | [
"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 | 34,166 | lean | /-
Copyright (c) 2021 David Wärn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: David Wärn, Joachim Breitner
-/
import algebra.free_monoid
import group_theory.congruence
import group_theory.is_free_group
import group_theory.subgroup.pointwise
import data.list.chain
import set_theory.cardinal.ordinal
/-!
# The free product of groups or monoids
Given an `ι`-indexed family `M` of monoids, we define their free product (categorical coproduct)
`free_product M`. When `ι` and all `M i` have decidable equality, the free product bijects with the
type `word M` of reduced words. This bijection is constructed by defining an action of
`free_product M` on `word M`.
When `M i` are all groups, `free_product M` is also a group (and the coproduct in the category of
groups).
## Main definitions
- `free_product M`: the free product, defined as a quotient of a free monoid.
- `free_product.of {i} : M i →* free_product M`.
- `free_product.lift : (Π {i}, M i →* N) ≃ (free_product M →* N)`: the universal property.
- `free_product.word M`: the type of reduced words.
- `free_product.word.equiv M : free_product M ≃ word M`.
- `free_product.neword M i j`: an inductive description of non-empty words with first letter from
`M i` and last letter from `M j`, together with an API (`singleton`, `append`, `head`, `tail`,
`to_word`, `prod`, `inv`). Used in the proof of the Ping-Pong-lemma.
- `free_product.lift_injective_of_ping_pong`: The Ping-Pong-lemma, proving injectivity of the
`lift`. See the documentation of that theorem for more information.
## Remarks
There are many answers to the question "what is the free product of a family `M` of monoids?", and
they are all equivalent but not obviously equivalent. We provide two answers. The first, almost
tautological answer is given by `free_product M`, which is a quotient of the type of words in the
alphabet `Σ i, M i`. It's straightforward to define and easy to prove its universal property. But
this answer is not completely satisfactory, because it's difficult to tell when two elements
`x y : free_product M` are distinct since `free_product M` is defined as a quotient.
The second, maximally efficient answer is given by `word M`. An element of `word M` is a word in the
alphabet `Σ i, M i`, where the letter `⟨i, 1⟩` doesn't occur and no adjacent letters share an index
`i`. Since we only work with reduced words, there is no need for quotienting, and it is easy to tell
when two elements are distinct. However it's not obvious that this is even a monoid!
We prove that every element of `free_product M` can be represented by a unique reduced word, i.e.
`free_product M` and `word M` are equivalent types. This means that `word M` can be given a monoid
structure, and it lets us tell when two elements of `free_product M` are distinct.
There is also a completely tautological, maximally inefficient answer given by
`algebra.category.Mon.colimits`. Whereas `free_product M` at least ensures that (any instance of)
associativity holds by reflexivity, in this answer associativity holds because of quotienting. Yet
another answer, which is constructively more satisfying, could be obtained by showing that
`free_product.rel` is confluent.
## References
[van der Waerden, *Free products of groups*][MR25465]
-/
variables {ι : Type*} (M : Π i : ι, Type*) [Π i, monoid (M i)]
/-- A relation on the free monoid on alphabet `Σ i, M i`, relating `⟨i, 1⟩` with `1` and
`⟨i, x⟩ * ⟨i, y⟩` with `⟨i, x * y⟩`. -/
inductive free_product.rel : free_monoid (Σ i, M i) → free_monoid (Σ i, M i) → Prop
| of_one (i : ι) : free_product.rel (free_monoid.of ⟨i, 1⟩) 1
| of_mul {i : ι} (x y : M i) : free_product.rel (free_monoid.of ⟨i, x⟩ * free_monoid.of ⟨i, y⟩)
(free_monoid.of ⟨i, x * y⟩)
/-- The free product (categorical coproduct) of an indexed family of monoids. -/
@[derive [monoid, inhabited]]
def free_product : Type* := (con_gen (free_product.rel M)).quotient
namespace free_product
/-- The type of reduced words. A reduced word cannot contain a letter `1`, and no two adjacent
letters can come from the same summand. -/
@[ext] structure word :=
(to_list : list (Σ i, M i))
(ne_one : ∀ l ∈ to_list, sigma.snd l ≠ 1)
(chain_ne : to_list.chain' (λ l l', sigma.fst l ≠ sigma.fst l'))
variable {M}
/-- The inclusion of a summand into the free product. -/
def of {i : ι} : M i →* free_product M :=
{ to_fun := λ x, con.mk' _ (free_monoid.of $ sigma.mk i x),
map_one' := (con.eq _).mpr (con_gen.rel.of _ _ (free_product.rel.of_one i)),
map_mul' := λ x y, eq.symm $ (con.eq _).mpr (con_gen.rel.of _ _ (free_product.rel.of_mul x y)) }
lemma of_apply {i} (m : M i) : of m = con.mk' _ (free_monoid.of $ sigma.mk i m) := rfl
variables {N : Type*} [monoid N]
/-- See note [partially-applied ext lemmas]. -/
@[ext] lemma ext_hom (f g : free_product M →* N) (h : ∀ i, f.comp (of : M i →* _) = g.comp of) :
f = g :=
(monoid_hom.cancel_right con.mk'_surjective).mp $ free_monoid.hom_eq $ λ ⟨i, x⟩,
by rw [monoid_hom.comp_apply, monoid_hom.comp_apply, ←of_apply,
←monoid_hom.comp_apply, ←monoid_hom.comp_apply, h]
/-- A map out of the free product corresponds to a family of maps out of the summands. This is the
universal property of the free product, charaterizing it as a categorical coproduct. -/
@[simps symm_apply]
def lift : (Π i, M i →* N) ≃ (free_product M →* N) :=
{ to_fun := λ fi, con.lift _ (free_monoid.lift $ λ p : Σ i, M i, fi p.fst p.snd) $ con.con_gen_le
begin
simp_rw [con.rel_eq_coe, con.ker_rel],
rintros _ _ (i | ⟨i, x, y⟩),
{ change free_monoid.lift _ (free_monoid.of _) = free_monoid.lift _ 1,
simp only [monoid_hom.map_one, free_monoid.lift_eval_of], },
{ change free_monoid.lift _ (free_monoid.of _ * free_monoid.of _) =
free_monoid.lift _ (free_monoid.of _),
simp only [monoid_hom.map_mul, free_monoid.lift_eval_of], }
end,
inv_fun := λ f i, f.comp of,
left_inv := by { intro fi, ext i x,
rw [monoid_hom.comp_apply, of_apply, con.lift_mk', free_monoid.lift_eval_of], },
right_inv := by { intro f, ext i x,
simp only [monoid_hom.comp_apply, of_apply, con.lift_mk', free_monoid.lift_eval_of], } }
@[simp] lemma lift_of {N} [monoid N] (fi : Π i, M i →* N) {i} (m : M i) :
lift fi (of m) = fi i m :=
by conv_rhs { rw [←lift.symm_apply_apply fi, lift_symm_apply, monoid_hom.comp_apply] }
@[elab_as_eliminator]
lemma induction_on {C : free_product M → Prop}
(m : free_product M)
(h_one : C 1)
(h_of : ∀ (i) (m : M i), C (of m))
(h_mul : ∀ (x y), C x → C y → C (x * y)) :
C m :=
begin
let S : submonoid (free_product M) := submonoid.mk (set_of C) h_mul h_one,
convert subtype.prop (lift (λ i, of.cod_mrestrict S (h_of i)) m),
change monoid_hom.id _ m = S.subtype.comp _ m,
congr,
ext,
simp [monoid_hom.cod_mrestrict],
end
lemma of_left_inverse [decidable_eq ι] (i : ι) :
function.left_inverse (lift $ pi.mul_single i (monoid_hom.id (M i))) of :=
λ x, by simp only [lift_of, pi.mul_single_eq_same, monoid_hom.id_apply]
lemma of_injective (i : ι) : function.injective ⇑(of : M i →* _) :=
by { classical, exact (of_left_inverse i).injective }
lemma lift_mrange_le {N} [monoid N] (f : Π i, M i →* N) {s : submonoid N}
(h : ∀ i, (f i).mrange ≤ s) : (lift f).mrange ≤ s :=
begin
rintros _ ⟨x, rfl⟩,
induction x using free_product.induction_on with i x x y hx hy,
{ exact s.one_mem, },
{ simp only [lift_of, set_like.mem_coe], exact h i (set.mem_range_self x), },
{ simp only [map_mul, set_like.mem_coe], exact s.mul_mem hx hy, },
end
lemma mrange_eq_supr {N} [monoid N] (f : Π i, M i →* N) :
(lift f).mrange = ⨆ i, (f i).mrange :=
begin
apply le_antisymm (lift_mrange_le f (λ i, le_supr _ i)),
apply supr_le _,
rintros i _ ⟨x, rfl⟩,
exact ⟨of x, by simp only [lift_of]⟩
end
section group
variables (G : ι → Type*) [Π i, group (G i)]
instance : has_inv (free_product G) :=
{ inv := mul_opposite.unop ∘
lift (λ i, (of : G i →* _).op.comp (mul_equiv.inv' (G i)).to_monoid_hom) }
lemma inv_def (x : free_product G) : x⁻¹ = mul_opposite.unop
(lift (λ i, (of : G i →* _).op.comp (mul_equiv.inv' (G i)).to_monoid_hom) x) := rfl
instance : group (free_product G) :=
{ mul_left_inv := begin
intro m,
rw inv_def,
apply m.induction_on,
{ rw [monoid_hom.map_one, mul_opposite.unop_one, one_mul], },
{ intros i m, change of m⁻¹ * of m = 1, rw [←of.map_mul, mul_left_inv, of.map_one], },
{ intros x y hx hy,
rw [monoid_hom.map_mul, mul_opposite.unop_mul, mul_assoc, ← mul_assoc _ x y, hx,
one_mul, hy], },
end,
..free_product.has_inv G,
..free_product.monoid G }
lemma lift_range_le {N} [group N] (f : Π i, G i →* N) {s : subgroup N}
(h : ∀ i, (f i).range ≤ s) : (lift f).range ≤ s :=
begin
rintros _ ⟨x, rfl⟩,
induction x using free_product.induction_on with i x x y hx hy,
{ exact s.one_mem, },
{ simp only [lift_of, set_like.mem_coe], exact h i (set.mem_range_self x), },
{ simp only [map_mul, set_like.mem_coe], exact s.mul_mem hx hy, },
end
lemma range_eq_supr {N} [group N] (f : Π i, G i →* N) :
(lift f).range = ⨆ i, (f i).range :=
begin
apply le_antisymm (lift_range_le _ f (λ i, le_supr _ i)),
apply supr_le _,
rintros i _ ⟨x, rfl⟩,
exact ⟨of x, by simp only [lift_of]⟩
end
end group
namespace word
/-- The empty reduced word. -/
def empty : word M := { to_list := [], ne_one := λ _, false.elim, chain_ne := list.chain'_nil }
instance : inhabited (word M) := ⟨empty⟩
/-- A reduced word determines an element of the free product, given by multiplication. -/
def prod (w : word M) : free_product M :=
list.prod (w.to_list.map $ λ l, of l.snd)
@[simp] lemma prod_empty : prod (empty : word M) = 1 := rfl
/-- `fst_idx w` is `some i` if the first letter of `w` is `⟨i, m⟩` with `m : M i`. If `w` is empty
then it's `none`. -/
def fst_idx (w : word M) : option ι := w.to_list.head'.map sigma.fst
lemma fst_idx_ne_iff {w : word M} {i} :
fst_idx w ≠ some i ↔ ∀ l ∈ w.to_list.head', i ≠ sigma.fst l :=
not_iff_not.mp $ by simp [fst_idx]
variable (M)
/-- Given an index `i : ι`, `pair M i` is the type of pairs `(head, tail)` where `head : M i` and
`tail : word M`, subject to the constraint that first letter of `tail` can't be `⟨i, m⟩`.
By prepending `head` to `tail`, one obtains a new word. We'll show that any word can be uniquely
obtained in this way. -/
@[ext] structure pair (i : ι) :=
(head : M i)
(tail : word M)
(fst_idx_ne : fst_idx tail ≠ some i)
instance (i : ι) : inhabited (pair M i) := ⟨⟨1, empty, by tauto⟩⟩
variable {M}
variables [∀ i, decidable_eq (M i)]
/-- Given a pair `(head, tail)`, we can form a word by prepending `head` to `tail`, except if `head`
is `1 : M i` then we have to just return `word` since we need the result to be reduced. -/
def rcons {i} (p : pair M i) : word M :=
if h : p.head = 1 then p.tail
else { to_list := ⟨i, p.head⟩ :: p.tail.to_list,
ne_one := by { rintros l (rfl | hl), exact h, exact p.tail.ne_one l hl },
chain_ne := p.tail.chain_ne.cons' (fst_idx_ne_iff.mp p.fst_idx_ne) }
/-- Given a word of the form `⟨l :: ls, h1, h2⟩`, we can form a word of the form `⟨ls, _, _⟩`,
dropping the first letter. -/
private def mk_aux {l} (ls : list (Σ i, M i)) (h1 : ∀ l' ∈ l :: ls, sigma.snd l' ≠ 1)
(h2 : (l :: ls).chain' _) : word M :=
⟨ls, λ l' hl, h1 _ (list.mem_cons_of_mem _ hl), h2.tail⟩
lemma cons_eq_rcons {i} {m : M i} {ls h1 h2} :
word.mk (⟨i, m⟩ :: ls) h1 h2 = rcons ⟨m, mk_aux ls h1 h2, fst_idx_ne_iff.mpr h2.rel_head'⟩ :=
by { rw [rcons, dif_neg], refl, exact h1 ⟨i, m⟩ (ls.mem_cons_self _) }
@[simp] lemma prod_rcons {i} (p : pair M i) :
prod (rcons p) = of p.head * prod p.tail :=
if hm : p.head = 1 then by rw [rcons, dif_pos hm, hm, monoid_hom.map_one, one_mul]
else by rw [rcons, dif_neg hm, prod, list.map_cons, list.prod_cons, prod]
lemma rcons_inj {i} : function.injective (rcons : pair M i → word M) :=
begin
rintros ⟨m, w, h⟩ ⟨m', w', h'⟩ he,
by_cases hm : m = 1;
by_cases hm' : m' = 1,
{ simp only [rcons, dif_pos hm, dif_pos hm'] at he, cc, },
{ exfalso, simp only [rcons, dif_pos hm, dif_neg hm'] at he, rw he at h, exact h rfl },
{ exfalso, simp only [rcons, dif_pos hm', dif_neg hm] at he, rw ←he at h', exact h' rfl, },
{ have : m = m' ∧ w.to_list = w'.to_list,
{ simpa only [rcons, dif_neg hm, dif_neg hm', true_and, eq_self_iff_true, subtype.mk_eq_mk,
heq_iff_eq, ←subtype.ext_iff_val] using he },
rcases this with ⟨rfl, h⟩,
congr, exact word.ext _ _ h, }
end
variable [decidable_eq ι]
/-- Given `i : ι`, any reduced word can be decomposed into a pair `p` such that `w = rcons p`. -/
-- This definition is computable but not very nice to look at. Thankfully we don't have to inspect
-- it, since `rcons` is known to be injective.
private def equiv_pair_aux (i) : Π w : word M, { p : pair M i // rcons p = w }
| w@⟨[], _, _⟩ := ⟨⟨1, w, by rintro ⟨⟩⟩, dif_pos rfl⟩
| w@⟨⟨j, m⟩ :: ls, h1, h2⟩ := if ij : i = j then
{ val := { head := ij.symm.rec m,
tail := mk_aux ls h1 h2,
fst_idx_ne := by cases ij; exact fst_idx_ne_iff.mpr h2.rel_head' },
property := by cases ij; exact cons_eq_rcons.symm }
else ⟨⟨1, w, (option.some_injective _).ne (ne.symm ij)⟩, dif_pos rfl⟩
/-- The equivalence between words and pairs. Given a word, it decomposes it as a pair by removing
the first letter if it comes from `M i`. Given a pair, it prepends the head to the tail. -/
def equiv_pair (i) : word M ≃ pair M i :=
{ to_fun := λ w, (equiv_pair_aux i w).val,
inv_fun := rcons,
left_inv := λ w, (equiv_pair_aux i w).property,
right_inv := λ p, rcons_inj (equiv_pair_aux i _).property }
lemma equiv_pair_symm (i) (p : pair M i) : (equiv_pair i).symm p = rcons p := rfl
lemma equiv_pair_eq_of_fst_idx_ne {i} {w : word M} (h : fst_idx w ≠ some i) :
equiv_pair i w = ⟨1, w, h⟩ :=
(equiv_pair i).apply_eq_iff_eq_symm_apply.mpr $ eq.symm (dif_pos rfl)
instance summand_action (i) : mul_action (M i) (word M) :=
{ smul := λ m w, rcons { head := m * (equiv_pair i w).head, ..equiv_pair i w },
one_smul := λ w, by { simp_rw [one_mul], apply (equiv_pair i).symm_apply_eq.mpr, ext; refl },
mul_smul := λ m m' w, by simp only [mul_assoc, ←equiv_pair_symm, equiv.apply_symm_apply], }
instance : mul_action (free_product M) (word M) :=
mul_action.of_End_hom (lift (λ i, mul_action.to_End_hom))
lemma of_smul_def (i) (w : word M) (m : M i) :
of m • w = rcons { head := m * (equiv_pair i w).head, ..equiv_pair i w } := rfl
lemma cons_eq_smul {i} {m : M i} {ls h1 h2} :
word.mk (⟨i, m⟩ :: ls) h1 h2 = of m • mk_aux ls h1 h2 :=
by rw [cons_eq_rcons, of_smul_def, equiv_pair_eq_of_fst_idx_ne _]; simp only [mul_one]
lemma smul_induction {C : word M → Prop}
(h_empty : C empty)
(h_smul : ∀ i (m : M i) w, C w → C (of m • w))
(w : word M) : C w :=
begin
cases w with ls h1 h2,
induction ls with l ls ih,
{ exact h_empty },
cases l with i m,
rw cons_eq_smul,
exact h_smul _ _ _ (ih _ _),
end
@[simp] lemma prod_smul (m) : ∀ w : word M, prod (m • w) = m * prod w :=
begin
apply m.induction_on,
{ intro, rw [one_smul, one_mul] },
{ intros, rw [of_smul_def, prod_rcons, of.map_mul, mul_assoc, ←prod_rcons,
←equiv_pair_symm, equiv.symm_apply_apply] },
{ intros x y hx hy w, rw [mul_smul, hx, hy, mul_assoc] },
end
/-- Each element of the free product corresponds to a unique reduced word. -/
def equiv : free_product M ≃ word M :=
{ to_fun := λ m, m • empty,
inv_fun := λ w, prod w,
left_inv := λ m, by dsimp only; rw [prod_smul, prod_empty, mul_one],
right_inv := begin
apply smul_induction,
{ dsimp only, rw [prod_empty, one_smul], },
{ dsimp only, intros i m w ih, rw [prod_smul, mul_smul, ih], },
end }
instance : decidable_eq (word M) := function.injective.decidable_eq word.ext
instance : decidable_eq (free_product M) := word.equiv.decidable_eq
end word
variable (M)
/-- A `neword M i j` is a representation of a non-empty reduced words where the first letter comes
from `M i` and the last letter comes from `M j`. It can be constructed from singletons and via
concatentation, and thus provides a useful induction principle. -/
@[nolint has_inhabited_instance]
inductive neword : ι → ι → Type (max u_1 u_2)
| singleton : ∀ {i} (x : M i) (hne1 : x ≠ 1), neword i i
| append : ∀ {i j k l} (w₁ : neword i j) (hne : j ≠ k) (w₂ : neword k l), neword i l
variable {M}
namespace neword
open word
/-- The list represented by a given `neword` -/
@[simp]
def to_list : Π {i j} (w : neword M i j), list (Σ i, M i)
| i _ (singleton x hne1) := [⟨i, x⟩]
| _ _ (append w₁ hne w₂) := w₁.to_list ++ w₂.to_list
lemma to_list_ne_nil {i j} (w : neword M i j) : w.to_list ≠ list.nil :=
by { induction w, { rintros ⟨rfl⟩ }, { apply list.append_ne_nil_of_ne_nil_left, assumption } }
/-- The first letter of a `neword` -/
@[simp]
def head : Π {i j} (w : neword M i j), M i
| i _ (singleton x hne1) := x
| _ _ (append w₁ hne w₂) := w₁.head
/-- The last letter of a `neword` -/
@[simp]
def last : Π {i j} (w : neword M i j), M j
| i _ (singleton x hne1) := x
| _ _ (append w₁ hne w₂) := w₂.last
@[simp]
lemma to_list_head' {i j} (w : neword M i j) :
w.to_list.head' = option.some ⟨i, w.head⟩ :=
begin
rw ← option.mem_def,
induction w,
{ rw option.mem_def, reflexivity, },
{ exact list.head'_append w_ih_w₁, },
end
@[simp]
lemma to_list_last' {i j} (w : neword M i j) :
w.to_list.last' = option.some ⟨j, w.last⟩ :=
begin
rw ← option.mem_def,
induction w,
{ rw option.mem_def, reflexivity, },
{ exact list.last'_append w_ih_w₂, },
end
/-- The `word M` represented by a `neword M i j` -/
def to_word {i j} (w : neword M i j) : word M :=
{ to_list := w.to_list,
ne_one :=
begin
induction w,
{ rintros ⟨k,x⟩ ⟨rfl, rfl⟩,
exact w_hne1,
exfalso, apply H, },
{ intros l h,
simp only [to_list, list.mem_append] at h,
cases h,
{ exact w_ih_w₁ _ h, },
{ exact w_ih_w₂ _ h, }, },
end,
chain_ne := begin
induction w,
{ exact list.chain'_singleton _, },
{ apply list.chain'.append w_ih_w₁ w_ih_w₂,
intros x hx y hy,
rw [w_w₁.to_list_last', option.mem_some_iff] at hx,
rw [w_w₂.to_list_head', option.mem_some_iff] at hy,
subst hx, subst hy,
exact w_hne, },
end, }
/-- Every nonempty `word M` can be constructed as a `neword M i j` -/
lemma of_word (w : word M) (h : w ≠ empty) :
∃ i j (w' : neword M i j), w'.to_word = w :=
begin
suffices : ∃ i j (w' : neword M i j), w'.to_word.to_list = w.to_list,
{ obtain ⟨i, j, w, h⟩ := this, refine ⟨i, j, w, _⟩, ext, rw h, },
cases w with l hnot1 hchain,
induction l with x l hi,
{ contradiction, },
{ rw list.forall_mem_cons at hnot1,
cases l with y l,
{ refine ⟨x.1, x.1, singleton x.2 hnot1.1, _ ⟩,
simp [to_word], },
{ rw list.chain'_cons at hchain,
specialize hi hnot1.2 hchain.2 (by rintros ⟨rfl⟩),
obtain ⟨i, j, w', hw' : w'.to_list = y :: l⟩ := hi,
obtain rfl : y = ⟨i, w'.head⟩, by simpa [hw'] using w'.to_list_head',
refine ⟨x.1, j, append (singleton x.2 hnot1.1) hchain.1 w', _⟩,
{ simpa [to_word] using hw', } } }
end
/-- A non-empty reduced word determines an element of the free product, given by multiplication. -/
def prod {i j} (w : neword M i j) := w.to_word.prod
@[simp]
lemma singleton_head {i} (x : M i) (hne_one : x ≠ 1) :
(singleton x hne_one).head = x := rfl
@[simp]
lemma singleton_last {i} (x : M i) (hne_one : x ≠ 1) :
(singleton x hne_one).last = x := rfl
@[simp] lemma prod_singleton {i} (x : M i) (hne_one : x ≠ 1) :
(singleton x hne_one).prod = of x :=
by simp [to_word, prod, word.prod]
@[simp]
lemma append_head {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} :
(append w₁ hne w₂).head = w₁.head := rfl
@[simp]
lemma append_last {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} :
(append w₁ hne w₂).last = w₂.last := rfl
@[simp]
lemma append_prod {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} :
(append w₁ hne w₂).prod = w₁.prod * w₂.prod :=
by simp [to_word, prod, word.prod]
/-- One can replace the first letter in a non-empty reduced word by an element of the same
group -/
def replace_head : Π {i j : ι} (x : M i) (hnotone : x ≠ 1) (w : neword M i j), neword M i j
| _ _ x h (singleton _ _) := singleton x h
| _ _ x h (append w₁ hne w₂) := append (replace_head x h w₁) hne w₂
@[simp]
lemma replace_head_head {i j : ι} (x : M i) (hnotone : x ≠ 1) (w : neword M i j) :
(replace_head x hnotone w).head = x :=
by { induction w, refl, exact w_ih_w₁ _ _, }
/-- One can multiply an element from the left to a non-empty reduced word if it does not cancel
with the first element in the word. -/
def mul_head {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) :
neword M i j := replace_head (x * w.head) hnotone w
@[simp]
lemma mul_head_head {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) :
(mul_head w x hnotone).head = x * w.head :=
by { induction w, refl, exact w_ih_w₁ _ _, }
@[simp]
lemma mul_head_prod {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) :
(mul_head w x hnotone).prod = of x * w.prod :=
begin
unfold mul_head,
induction w,
{ simp [mul_head, replace_head], },
{ specialize w_ih_w₁ _ hnotone, clear w_ih_w₂,
simp [replace_head, ← mul_assoc] at *,
congr' 1, }
end
section group
variables {G : ι → Type*} [Π i, group (G i)]
/-- The inverse of a non-empty reduced word -/
def inv : Π {i j} (w : neword G i j), neword G j i
| _ _ (singleton x h) := singleton x⁻¹ (mt inv_eq_one.mp h)
| _ _ (append w₁ h w₂) := append w₂.inv h.symm w₁.inv
@[simp]
lemma inv_prod {i j} (w : neword G i j) : w.inv.prod = w.prod⁻¹ :=
by induction w; simp [inv, *]
@[simp]
lemma inv_head {i j} (w : neword G i j) : w.inv.head = w.last⁻¹ :=
by induction w; simp [inv, *]
@[simp]
lemma inv_last {i j} (w : neword G i j) : w.inv.last = w.head⁻¹ :=
by induction w; simp [inv, *]
end group
end neword
section ping_pong_lemma
open_locale pointwise
open_locale cardinal
variables [hnontriv : nontrivial ι]
variables {G : Type*} [group G]
variables {H : ι → Type*} [∀ i, group (H i)]
variables (f : Π i, H i →* G)
-- We need many groups or one group with many elements
variables (hcard : 3 ≤ # ι ∨ ∃ i, 3 ≤ # (H i))
-- A group action on α, and the ping-pong sets
variables {α : Type*} [mul_action G α]
variables (X : ι → set α)
variables (hXnonempty : ∀ i, (X i).nonempty)
variables (hXdisj : pairwise (λ i j, disjoint (X i) (X j)))
variables (hpp : pairwise (λ i j, ∀ h : H i, h ≠ 1 → f i h • X j ⊆ X i))
include hpp
lemma lift_word_ping_pong {i j k} (w : neword H i j) (hk : j ≠ k) :
lift f w.prod • X k ⊆ X i :=
begin
rename [i → i', j → j', k → m, hk → hm],
induction w with i x hne_one i j k l w₁ hne w₂ hIw₁ hIw₂ generalizing m; clear i' j',
{ simpa using hpp _ _ hm _ hne_one, },
{ calc lift f (neword.append w₁ hne w₂).prod • X m
= lift f w₁.prod • lift f w₂.prod • X m : by simp [mul_action.mul_smul]
... ⊆ lift f w₁.prod • X k : set_smul_subset_set_smul_iff.mpr (hIw₂ hm)
... ⊆ X i : hIw₁ hne },
end
include X hXnonempty hXdisj
lemma lift_word_prod_nontrivial_of_other_i {i j k} (w : neword H i j)
(hhead : k ≠ i) (hlast : k ≠ j) : lift f w.prod ≠ 1 :=
begin
intro heq1,
have : X k ⊆ X i,
by simpa [heq1] using lift_word_ping_pong f X hpp w hlast.symm,
obtain ⟨x, hx⟩ := hXnonempty k,
exact hXdisj k i hhead ⟨hx, this hx⟩,
end
include hnontriv
lemma lift_word_prod_nontrivial_of_head_eq_last {i} (w : neword H i i) :
lift f w.prod ≠ 1 :=
begin
obtain ⟨k, hk⟩ := exists_ne i,
exact lift_word_prod_nontrivial_of_other_i f X hXnonempty hXdisj hpp w hk hk,
end
lemma lift_word_prod_nontrivial_of_head_card {i j} (w : neword H i j)
(hcard : 3 ≤ # (H i)) (hheadtail : i ≠ j) : lift f w.prod ≠ 1 :=
begin
obtain ⟨h, hn1, hnh⟩ := cardinal.three_le hcard 1 (w.head⁻¹),
have hnot1 : h * w.head ≠ 1, by { rw ← div_inv_eq_mul, exact div_ne_one_of_ne hnh },
let w' : neword H i i := neword.append
(neword.mul_head w h hnot1) hheadtail.symm
(neword.singleton h⁻¹ (inv_ne_one.mpr hn1)),
have hw' : lift f w'.prod ≠ 1 :=
lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w',
intros heq1, apply hw', simp [w', heq1]
end
include hcard
lemma lift_word_prod_nontrivial_of_not_empty {i j} (w : neword H i j) :
lift f w.prod ≠ 1 :=
begin
classical,
cases hcard,
{ obtain ⟨i, h1, h2⟩ := cardinal.three_le hcard i j,
exact lift_word_prod_nontrivial_of_other_i f X hXnonempty hXdisj hpp w h1 h2, },
{ cases hcard with k hcard,
by_cases hh : i = k; by_cases hl : j = k,
{ subst hh, subst hl,
exact lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w, },
{ subst hh,
change j ≠ i at hl,
exact lift_word_prod_nontrivial_of_head_card f X hXnonempty hXdisj hpp w hcard hl.symm, },
{ subst hl,
change i ≠ j at hh,
have : lift f w.inv.prod ≠ 1 :=
lift_word_prod_nontrivial_of_head_card f X hXnonempty hXdisj hpp w.inv hcard hh.symm,
intros heq, apply this, simpa using heq, },
{ change i ≠ k at hh,
change j ≠ k at hl,
obtain ⟨h, hn1, -⟩ := cardinal.three_le hcard 1 1,
let w' : neword H k k := neword.append
(neword.append (neword.singleton h hn1) hh.symm w)
hl (neword.singleton h⁻¹ (inv_ne_one.mpr hn1)) ,
have hw' : lift f w'.prod ≠ 1 :=
lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w',
intros heq1, apply hw', simp [w', heq1], }, }
end
lemma empty_of_word_prod_eq_one {w : word H} (h : lift f w.prod = 1) :
w = word.empty :=
begin
by_contradiction hnotempty,
obtain ⟨i, j, w, rfl⟩ := neword.of_word w hnotempty,
exact lift_word_prod_nontrivial_of_not_empty f hcard X hXnonempty hXdisj hpp w h,
end
/--
The Ping-Pong-Lemma.
Given a group action of `G` on `X` so that the `H i` acts in a specific way on disjoint subsets
`X i` we can prove that `lift f` is injective, and thus the image of `lift f` is isomorphic to the
direct product of the `H i`.
Often the Ping-Pong-Lemma is stated with regard to subgroups `H i` that generate the whole group;
we generalize to arbitrary group homomorphisms `f i : H i →* G` and do not require the group to be
generated by the images.
Usually the Ping-Pong-Lemma requires that one group `H i` has at least three elements. This
condition is only needed if `# ι = 2`, and we accept `3 ≤ # ι` as an alternative.
-/
theorem lift_injective_of_ping_pong:
function.injective (lift f) :=
begin
classical,
apply (injective_iff_map_eq_one (lift f)).mpr,
rw free_product.word.equiv.forall_congr_left',
{ intros w Heq,
dsimp [word.equiv] at *,
{ rw empty_of_word_prod_eq_one f hcard X hXnonempty hXdisj hpp Heq,
reflexivity, }, },
apply_instance,
apply_instance,
end
end ping_pong_lemma
/-- The free product of free groups is itself a free group -/
@[simps]
instance {ι : Type*} (G : ι → Type*) [∀ i, group (G i)] [hG : ∀ i, is_free_group (G i)] :
is_free_group (free_product G) :=
{ generators := Σ i, is_free_group.generators (G i),
mul_equiv :=
monoid_hom.to_mul_equiv
(free_group.lift (λ (x : Σ i, is_free_group.generators (G i)),
free_product.of (is_free_group.of x.2 : G x.1)))
(free_product.lift (λ (i : ι),
(is_free_group.lift (λ (x : is_free_group.generators (G i)),
free_group.of (⟨i, x⟩ : Σ i, is_free_group.generators (G i)))
: G i →* (free_group (Σ i, is_free_group.generators (G i))))))
(by {ext, simp, })
(by {ext, simp, }) }
/-- A free group is a free product of copies of the free_group over one generator. -/
-- NB: One might expect this theorem to be phrased with ℤ, but ℤ is an additive group,
-- and using `multiplicative ℤ` runs into diamond issues.
@[simps]
def _root_.free_group_equiv_free_product {ι : Type u_1} :
free_group ι ≃* free_product (λ (_ : ι), free_group unit) :=
begin
refine monoid_hom.to_mul_equiv _ _ _ _,
exact free_group.lift (λ i, @free_product.of ι _ _ i (free_group.of unit.star)),
exact free_product.lift (λ i, free_group.lift (λ pstar, free_group.of i)),
{ ext i, refl, },
{ ext i a, cases a, refl, },
end
section ping_pong_lemma
open_locale pointwise cardinal
variables [nontrivial ι]
variables {G : Type u_1} [group G] (a : ι → G)
-- A group action on α, and the ping-pong sets
variables {α : Type*} [mul_action G α]
variables (X Y : ι → set α)
variables (hXnonempty : ∀ i, (X i).nonempty)
variables (hXdisj : pairwise (λ i j, disjoint (X i) (X j)))
variables (hYdisj : pairwise (λ i j, disjoint (Y i) (Y j)))
variables (hXYdisj : ∀ i j, disjoint (X i) (Y j))
variables (hX : ∀ i, a i • set.compl (Y i) ⊆ X i)
variables (hY : ∀ i, a⁻¹ i • set.compl (X i) ⊆ Y i)
include hXnonempty hXdisj hYdisj hXYdisj hX hY
/--
The Ping-Pong-Lemma.
Given a group action of `G` on `X` so that the generators of the free groups act in specific
ways on disjoint subsets `X i` and `Y i` we can prove that `lift f` is injective, and thus the image
of `lift f` is isomorphic to the free group.
Often the Ping-Pong-Lemma is stated with regard to group elements that generate the whole group;
we generalize to arbitrary group homomorphisms from the free group to `G` and do not require the
group to be generated by the elements.
-/
theorem _root_.free_group.injective_lift_of_ping_pong :
function.injective (free_group.lift a) :=
begin
-- Step one: express the free group lift via the free product lift
have : free_group.lift a =
(free_product.lift (λ i, free_group.lift (λ _, a i))).comp
(((@free_group_equiv_free_product ι)).to_monoid_hom),
{ ext i, simp, },
rw this, clear this,
refine function.injective.comp _ (mul_equiv.injective _),
-- Step two: Invoke the ping-pong lemma for free products
show function.injective (lift (λ (i : ι), free_group.lift (λ _, a i))),
-- Prepare to instantiate lift_injective_of_ping_pong
let H : ι → Type _ := λ i, free_group unit,
let f : Π i, H i →* G := λ i, free_group.lift (λ _, a i),
let X' : ι → set α := λ i, X i ∪ Y i,
apply lift_injective_of_ping_pong f _ X',
show _ ∨ ∃ i, 3 ≤ # (H i),
{ inhabit ι,
right, use arbitrary ι,
simp only [H],
rw [free_group.free_group_unit_equiv_int.cardinal_eq, cardinal.mk_denumerable],
apply le_of_lt,
simp },
show ∀ i, (X' i).nonempty,
{ exact (λ i, set.nonempty.inl (hXnonempty i)), },
show pairwise (λ i j, disjoint (X' i) (X' j)),
{ intros i j hij,
simp only [X'],
apply disjoint.union_left; apply disjoint.union_right,
{ exact hXdisj i j hij, },
{ exact hXYdisj i j, },
{ exact (hXYdisj j i).symm, },
{ exact hYdisj i j hij, }, },
show pairwise (λ i j, ∀ h : H i, h ≠ 1 → f i h • X' j ⊆ X' i),
{ rintros i j hij,
-- use free_group unit ≃ ℤ
refine free_group.free_group_unit_equiv_int.forall_congr_left'.mpr _,
intros n hne1,
change free_group.lift (λ _, a i) (free_group.of () ^ n) • X' j ⊆ X' i,
simp only [map_zpow, free_group.lift.of],
change a i ^ n • X' j ⊆ X' i,
have hnne0 : n ≠ 0, { rintro rfl, apply hne1, simpa, }, clear hne1,
simp only [X'],
-- Positive and negative powers separately
cases (lt_or_gt_of_ne hnne0).swap with hlt hgt,
{ have h1n : 1 ≤ n := hlt,
calc a i ^ n • X' j ⊆ a i ^ n • (Y i).compl : set_smul_subset_set_smul_iff.mpr $
set.disjoint_iff_subset_compl_right.mp $
disjoint.union_left (hXYdisj j i) (hYdisj j i hij.symm)
... ⊆ X i :
begin
refine int.le_induction _ _ _ h1n,
{ rw zpow_one, exact hX i, },
{ intros n hle hi,
calc (a i ^ (n + 1)) • (Y i).compl
= (a i ^ n * a i) • (Y i).compl : by rw [zpow_add, zpow_one]
... = a i ^ n • (a i • (Y i).compl) : mul_action.mul_smul _ _ _
... ⊆ a i ^ n • X i : set_smul_subset_set_smul_iff.mpr $ hX i
... ⊆ a i ^ n • (Y i).compl : set_smul_subset_set_smul_iff.mpr $
set.disjoint_iff_subset_compl_right.mp (hXYdisj i i)
... ⊆ X i : hi, },
end
... ⊆ X' i : set.subset_union_left _ _, },
{ have h1n : n ≤ -1, { apply int.le_of_lt_add_one, simpa using hgt, },
calc a i ^ n • X' j ⊆ a i ^ n • (X i).compl : set_smul_subset_set_smul_iff.mpr $
set.disjoint_iff_subset_compl_right.mp $
disjoint.union_left (hXdisj j i hij.symm) (hXYdisj i j).symm
... ⊆ Y i :
begin
refine int.le_induction_down _ _ _ h1n,
{ rw [zpow_neg, zpow_one], exact hY i, },
{ intros n hle hi,
calc (a i ^ (n - 1)) • (X i).compl
= (a i ^ n * (a i)⁻¹) • (X i).compl : by rw [zpow_sub, zpow_one]
... = a i ^ n • ((a i)⁻¹ • (X i).compl) : mul_action.mul_smul _ _ _
... ⊆ a i ^ n • Y i : set_smul_subset_set_smul_iff.mpr $ hY i
... ⊆ a i ^ n • (X i).compl : set_smul_subset_set_smul_iff.mpr $
set.disjoint_iff_subset_compl_right.mp (hXYdisj i i).symm
... ⊆ Y i : hi, },
end
... ⊆ X' i : set.subset_union_right _ _, }, },
end
end ping_pong_lemma
end free_product
|
ceec5ded5e655c63aac630dfe0d087b1e2e6359d | f4bff2062c030df03d65e8b69c88f79b63a359d8 | /kb_solns/sets_level02.lean | 7da0e66a35502bccec0f0fed48ea55f70f5028e0 | [
"Apache-2.0"
] | permissive | adastra7470/real-number-game | 776606961f52db0eb824555ed2f8e16f92216ea3 | f9dcb7d9255a79b57e62038228a23346c2dc301b | refs/heads/master | 1,669,221,575,893 | 1,594,669,800,000 | 1,594,669,800,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,909 | lean | import game.sets.L01defs -- hide
namespace xena -- hide
variable X : Type -- hide
/-
# Chapter 1 : Sets
## Level 2
-/
/-
Working with sets is very similar to working with propositions.
Let's now prove that any set $A$ is included in its union with
any other set $B$, or $A ⊆ A ∪ B$.
Our goal is definitionally equivalent to `∀ x ∈ A, x ∈ (A ∪ B)`.
The definition of `x ∈ (A ∪ B)` is `x ∈ A ∨ x ∈ B`.
You should already know the tactics needed to prove this goal, so give
it a try before checking the hints.
-/
/- Hint : Hint : The proof steps may become clearer if you change the goal.
Use the `change` tactic and the definitions give above:
`change ∀ x ∈ A, x ∈ A ∨ x ∈ B,`
This will change your goal to the definitionally equivalent
`∀ x : X, x ∈ A ⇾ x ∈ A ∨ x ∈ B`
Start your proof of `∀ (x : X) ...` in the way you learned in the previous level.
You will then have a statement of propositional form `α → β ∨ γ`. See if you can use your knowledge of propositions to solve this!
-/
/- Hint : Hint : After introducing your terms, you'll need to prove the `left` side of a disjunction.
With or without the `change` lines, you can introduce the
hypotheses we need by using
`intros x hx,`
Now the equivalence with the world of propositions will become apparent.
To prove that the union of two sets is inhabited is to prove the disjunction
$P ∨ Q$ of two propositions. In this case, $P$ is our statement $x ∈ A$.
Choosing `left,` will change our goal to the first disjunct.
You should now be able to easily finish the proof.
-/
/- Lemma
If $A$ and $B$ are sets of any type $X$, then
$$ A \subseteq A\cup B.$$
-/
theorem subset_union_left (A B : set X) : A ⊆ A ∪ B :=
begin
--change ∀ (x : α), x ∈ A → x ∈ A ∪ B, --they may want to do this
intros x hx,
left, exact hx, done
end
end xena --hide
|
7958d35245b7ab55ac80827c7be5047b9d294e7b | 3268ab3a126f0fef71459fbf170dc38efe5d0506 | /colimit/sequence.hlean | 0f1d9df2306d73ba2cec7d6403d179e0323c3438 | [
"Apache-2.0"
] | permissive | soraismus/Spectral | f043fed1a4e02ddfeba531769b2980eb817471f4 | 32512bf47db3a1b932856e7ed7c7830b1fc07ef0 | refs/heads/master | 1,585,628,705,579 | 1,538,609,948,000 | 1,538,609,974,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,467 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Egbert Rijke
-/
import ..move_to_lib types.fin types.trunc
open nat eq equiv sigma sigma.ops is_equiv is_trunc trunc prod fiber function is_conn
namespace seq_colim
definition seq_diagram [reducible] (A : ℕ → Type) : Type := Π⦃n⦄, A n → A (succ n)
structure Seq_diagram : Type :=
(carrier : ℕ → Type)
(struct : seq_diagram carrier)
definition is_equiseq [reducible] {A : ℕ → Type} (f : seq_diagram A) : Type :=
forall (n : ℕ), is_equiv (@f n)
structure Equi_seq : Type :=
(carrier : ℕ → Type)
(maps : seq_diagram carrier)
(prop : is_equiseq maps)
protected abbreviation Mk [constructor] := Seq_diagram.mk
attribute Seq_diagram.carrier [coercion]
attribute Seq_diagram.struct [coercion]
variables {A A' : ℕ → Type} (f : seq_diagram A) (f' : seq_diagram A') {n m k : ℕ}
include f
definition lrep {n m : ℕ} (H : n ≤ m) : A n → A m :=
begin
induction H with m H fs,
{ exact id },
{ exact @f m ∘ fs }
end
definition lrep_irrel_pathover {n m m' : ℕ} (H₁ : n ≤ m) (H₂ : n ≤ m') (p : m = m') (a : A n) :
lrep f H₁ a =[p] lrep f H₂ a :=
apo (λm H, lrep f H a) !is_prop.elimo
definition lrep_irrel {n m : ℕ} (H₁ H₂ : n ≤ m) (a : A n) : lrep f H₁ a = lrep f H₂ a :=
ap (λH, lrep f H a) !is_prop.elim
definition lrep_eq_transport {n m : ℕ} (H : n ≤ m) (p : n = m) (a : A n) : lrep f H a = transport A p a :=
begin induction p, exact lrep_irrel f H (nat.le_refl n) a end
definition lrep_irrel2 {n m : ℕ} (H₁ H₂ : n ≤ m) (a : A n) :
lrep_irrel f (le.step H₁) (le.step H₂) a = ap (@f m) (lrep_irrel f H₁ H₂ a) :=
begin
have H₁ = H₂, from !is_prop.elim, induction this,
refine ap02 _ !is_prop_elim_self ⬝ _ ⬝ ap02 _(ap02 _ !is_prop_elim_self⁻¹),
reflexivity
end
definition lrep_eq_lrep_irrel {n m m' : ℕ} (H₁ : n ≤ m) (H₂ : n ≤ m') (a₁ a₂ : A n) (p : m = m') :
(lrep f H₁ a₁ = lrep f H₁ a₂) ≃ (lrep f H₂ a₁ = lrep f H₂ a₂) :=
equiv_apd011 (λm H, lrep f H a₁ = lrep f H a₂) (is_prop.elimo p H₁ H₂)
definition lrep_eq_lrep_irrel_natural {n m m' : ℕ} {H₁ : n ≤ m} (H₂ : n ≤ m') {a₁ a₂ : A n}
(p : m = m') (q : lrep f H₁ a₁ = lrep f H₁ a₂) :
lrep_eq_lrep_irrel f (le.step H₁) (le.step H₂) a₁ a₂ (ap succ p) (ap (@f m) q) =
ap (@f m') (lrep_eq_lrep_irrel f H₁ H₂ a₁ a₂ p q) :=
begin
esimp [lrep_eq_lrep_irrel],
symmetry,
refine fn_tro_eq_tro_fn2 _ (λa₁ a₂, ap (@f _)) q ⬝ _,
refine ap (λx, x ▸o _) (@is_prop.elim _ _ _ _),
apply is_trunc_pathover
end
definition is_equiv_lrep [constructor] [Hf : is_equiseq f] {n m : ℕ} (H : n ≤ m) :
is_equiv (lrep f H) :=
begin
induction H with m H Hlrepf,
{ apply is_equiv_id },
{ exact is_equiv_compose (@f _) (lrep f H) _ _ },
end
local attribute is_equiv_lrep [instance]
definition lrep_back [reducible] [Hf : is_equiseq f] {n m : ℕ} (H : n ≤ m) : A m → A n :=
(lrep f H)⁻¹ᶠ
section generalized_lrep
-- definition lrep_pathover_lrep0 [reducible] (k : ℕ) (a : A 0) : lrep f k a =[nat.zero_add k] lrep0 f k a :=
-- begin induction k with k p, constructor, exact pathover_ap A succ (apo f p) end
/- lreplace le_of_succ_le with this -/
definition lrep_f {n m : ℕ} (H : succ n ≤ m) (a : A n) :
lrep f H (f a) = lrep f (le_of_succ_le H) a :=
begin
induction H with m H p,
{ reflexivity },
{ exact ap (@f m) p }
end
definition lrep_lrep {n m k : ℕ} (H1 : n ≤ m) (H2 : m ≤ k) (a : A n) :
lrep f H2 (lrep f H1 a) = lrep f (nat.le_trans H1 H2) a :=
begin
induction H2 with k H2 p,
{ reflexivity },
{ exact ap (@f k) p }
end
-- -- this should be a squareover
-- definition lrep_lrep_succ (k l : ℕ) (a : A n) :
-- lrep_lrep f k (succ l) a = change_path (idontwanttoprovethis n l k)
-- (lrep_f f k (lrep f l a) ⬝o
-- lrep_lrep f (succ k) l a ⬝o
-- pathover_ap _ (λz, n + z) (apd (λz, lrep f z a) (succ_add l k)⁻¹ᵖ)) :=
-- begin
-- induction k with k IH,
-- { constructor},
-- { exact sorry}
-- end
definition f_lrep {n m : ℕ} (H : n ≤ m) (a : A n) : f (lrep f H a) = lrep f (le.step H) a := idp
definition rep (m : ℕ) (a : A n) : A (n + m) :=
lrep f (le_add_right n m) a
definition rep0 (m : ℕ) (a : A 0) : A m :=
lrep f (zero_le m) a
definition rep_pathover_rep0 {n : ℕ} (a : A 0) : rep f n a =[nat.zero_add n] rep0 f n a :=
!lrep_irrel_pathover
-- definition old_rep (m : ℕ) (a : A n) : A (n + m) :=
-- by induction m with m y; exact a; exact f y
-- definition rep_eq_old_rep (m : ℕ) (a : A n) : rep f m a = old_rep f m a :=
-- by induction m with m p; reflexivity; exact ap (@f _) p
definition rep_f (k : ℕ) (a : A n) :
pathover A (rep f k (f a)) (succ_add n k) (rep f (succ k) a) :=
begin
induction k with k IH,
{ constructor },
{ unfold [succ_add], apply pathover_ap, exact apo f IH}
end
definition rep_rep (k l : ℕ) (a : A n) :
pathover A (rep f k (rep f l a)) (nat.add_assoc n l k) (rep f (l + k) a) :=
begin
induction k with k IH,
{ constructor},
{ apply pathover_ap, exact apo f IH}
end
variables {f f'}
definition is_trunc_fun_lrep (k : ℕ₋₂) (H : n ≤ m) (H2 : Πn, is_trunc_fun k (@f n)) :
is_trunc_fun k (lrep f H) :=
begin induction H with m H IH, apply is_trunc_fun_id, exact is_trunc_fun_compose k (H2 m) IH end
definition is_conn_fun_lrep (k : ℕ₋₂) (H : n ≤ m) (H2 : Πn, is_conn_fun k (@f n)) :
is_conn_fun k (lrep f H) :=
begin induction H with m H IH, apply is_conn_fun_id, exact is_conn_fun_compose k (H2 m) IH end
definition lrep_natural (τ : Π⦃n⦄, A n → A' n) (p : Π⦃n⦄ (a : A n), τ (f a) = f' (τ a))
{n m : ℕ} (H : n ≤ m) (a : A n) : τ (lrep f H a) = lrep f' H (τ a) :=
begin
induction H with m H IH, reflexivity, exact p (lrep f H a) ⬝ ap (@f' m) IH
end
definition rep_natural (τ : Π⦃n⦄, A n → A' n) (p : Π⦃n⦄ (a : A n), τ (f a) = f' (τ a))
{n : ℕ} (k : ℕ) (a : A n) : τ (rep f k a) = rep f' k (τ a) :=
lrep_natural τ p _ a
definition rep0_natural (τ : Π⦃n⦄, A n → A' n) (p : Π⦃n⦄ (a : A n), τ (f a) = f' (τ a))
(k : ℕ) (a : A 0) : τ (rep0 f k a) = rep0 f' k (τ a) :=
lrep_natural τ p _ a
variables (f f')
end generalized_lrep
section shift
definition shift_diag [unfold_full] : seq_diagram (λn, A (succ n)) :=
λn a, f a
definition kshift_diag [unfold_full] (k : ℕ) : seq_diagram (λn, A (k + n)) :=
λn a, f a
definition kshift_diag' [unfold_full] (k : ℕ) : seq_diagram (λn, A (n + k)) :=
λn a, transport A (succ_add n k)⁻¹ (f a)
definition lrep_kshift_diag {n m k : ℕ} (H : m ≤ k) (a : A (n + m)) :
lrep (kshift_diag f n) H a = lrep f (nat.add_le_add_left2 H n) a :=
by induction H with k H p; reflexivity; exact ap (@f _) p
end shift
section constructions
omit f
definition constant_seq (X : Type) : seq_diagram (λ n, X) :=
λ n x, x
definition seq_diagram_arrow_left [unfold_full] (X : Type) : seq_diagram (λn, X → A n) :=
λn g x, f (g x)
definition seq_diagram_prod [unfold_full] : seq_diagram (λn, A n × A' n) :=
λn, prod_functor (@f n) (@f' n)
open fin
definition seq_diagram_fin [unfold_full] : seq_diagram fin :=
lift_succ
definition id0_seq [unfold_full] (a₁ a₂ : A 0) : ℕ → Type :=
λ k, rep0 f k a₁ = rep0 f k a₂
definition id0_seq_diagram [unfold_full] (a₁ a₂ : A 0) : seq_diagram (id0_seq f a₁ a₂) :=
λ (k : ℕ) (p : rep0 f k a₁ = rep0 f k a₂), ap (@f k) p
definition id_seq [unfold_full] (n : ℕ) (a₁ a₂ : A n) : ℕ → Type :=
λ k, rep f k a₁ = rep f k a₂
definition id_seq_diagram [unfold_full] (n : ℕ) (a₁ a₂ : A n) : seq_diagram (id_seq f n a₁ a₂) :=
λ (k : ℕ) (p : rep f k a₁ = rep f k a₂), ap (@f (n + k)) p
definition trunc_diagram [unfold_full] (k : ℕ₋₂) (f : seq_diagram A) :
seq_diagram (λn, trunc k (A n)) :=
λn, trunc_functor k (@f n)
end constructions
section over
variable {A}
variable (P : Π⦃n⦄, A n → Type)
definition seq_diagram_over : Type := Π⦃n⦄ {a : A n}, P a → P (f a)
definition weakened_sequence [unfold_full] : seq_diagram_over f (λn a, A' n) :=
λn a a', f' a'
definition id0_seq_diagram_over [unfold_full] (a₀ : A 0) :
seq_diagram_over f (λn a, rep0 f n a₀ = a) :=
λn a p, ap (@f n) p
variable (g : seq_diagram_over f P)
variables {f P}
definition seq_diagram_of_over [unfold_full] {n : ℕ} (a : A n) :
seq_diagram (λk, P (rep f k a)) :=
λk p, g p
definition seq_diagram_sigma [unfold 6] : seq_diagram (λn, Σ(x : A n), P x) :=
λn v, ⟨f v.1, g v.2⟩
variables (f P)
theorem rep_f_equiv [constructor] (a : A n) (k : ℕ) :
P (lrep f (le_add_right (succ n) k) (f a)) ≃ P (lrep f (le_add_right n (succ k)) a) :=
equiv_apd011 P (rep_f f k a)
theorem rep_rep_equiv [constructor] (a : A n) (k l : ℕ) :
P (rep f (l + k) a) ≃ P (rep f k (rep f l a)) :=
(equiv_apd011 P (rep_rep f k l a))⁻¹ᵉ
end over
omit f
-- do we need to generalize this to the case where the bottom sequence consists of equivalences?
definition seq_diagram_pi {X : Type} {A : X → ℕ → Type} (g : Π⦃x n⦄, A x n → A x (succ n)) :
seq_diagram (λn, Πx, A x n) :=
λn f x, g (f x)
variables {f f'}
definition seq_diagram_over_fiber (g : Π⦃n⦄, A' n → A n)
(p : Π⦃n⦄ (a : A' n), g (f' a) = f (g a)) : seq_diagram_over f (λn, fiber (@g n)) :=
λk a, fiber_functor (@f' k) (@f k) (@p k) idp
definition seq_diagram_fiber (g : Π⦃n⦄, A' n → A n) (p : Π⦃n⦄ (a : A' n), g (f' a) = f (g a))
{n : ℕ} (a : A n) : seq_diagram (λk, fiber (@g (n + k)) (rep f k a)) :=
seq_diagram_of_over (seq_diagram_over_fiber g p) a
definition seq_diagram_fiber0 (g : Π⦃n⦄, A' n → A n) (p : Π⦃n⦄ (a : A' n), g (f' a) = f (g a))
(a : A 0) : seq_diagram (λk, fiber (@g k) (rep0 f k a)) :=
λk, fiber_functor (@f' k) (@f k) (@p k) idp
end seq_colim
|
77b97dec8ce34ccb4aecfd0b15336e20cf9c85d9 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/data/polynomial/basic.lean | ae594c56244da2a5ba52e2b68ed27510cb9abf6c | [
"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 | 6,935 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import tactic.ring_exp
import tactic.chain
import algebra.monoid_algebra
import data.finset.sort
/-!
# Theory of univariate polynomials
Polynomials are represented as `add_monoid_algebra R ℕ`, where `R` is a commutative semiring.
In this file, we define `polynomial`, provide basic instances, and prove an `ext` lemma.
-/
noncomputable theory
/-- `polynomial R` is the type of univariate polynomials over `R`.
Polynomials should be seen as (semi-)rings with the additional constructor `X`.
The embedding from `R` is called `C`. -/
def polynomial (R : Type*) [semiring R] := add_monoid_algebra R ℕ
open finsupp add_monoid_algebra
open_locale big_operators
namespace polynomial
universes u
variables {R : Type u} {a : R} {m n : ℕ}
section semiring
variables [semiring R] {p q : polynomial R}
instance : inhabited (polynomial R) := finsupp.inhabited
instance : semiring (polynomial R) := add_monoid_algebra.semiring
instance : has_scalar R (polynomial R) := add_monoid_algebra.has_scalar
instance : semimodule R (polynomial R) := add_monoid_algebra.semimodule
instance subsingleton [subsingleton R] : subsingleton (polynomial R) :=
⟨λ _ _, ext (λ _, subsingleton.elim _ _)⟩
@[simp] lemma support_zero : (0 : polynomial R).support = ∅ := rfl
/-- `monomial s a` is the monomial `a * X^s` -/
def monomial (n : ℕ) (a : R) : polynomial R := finsupp.single n a
@[simp] lemma monomial_zero_right (n : ℕ) :
monomial n (0 : R) = 0 :=
by simp [monomial]
lemma monomial_add (n : ℕ) (r s : R) :
monomial n (r + s) = monomial n r + monomial n s :=
by simp [monomial]
lemma monomial_mul_monomial (n m : ℕ) (r s : R) :
monomial n r * monomial m s = monomial (n + m) (r * s) :=
by simp only [monomial, single_mul_single]
/-- `X` is the polynomial variable (aka indeterminant). -/
def X : polynomial R := monomial 1 1
/-- `X` commutes with everything, even when the coefficients are noncommutative. -/
lemma X_mul : X * p = p * X :=
by { ext, simp [X, monomial, add_monoid_algebra.mul_apply, sum_single_index, add_comm] }
lemma X_pow_mul {n : ℕ} : X^n * p = p * X^n :=
begin
induction n with n ih,
{ simp, },
{ conv_lhs { rw pow_succ', },
rw [mul_assoc, X_mul, ←mul_assoc, ih, mul_assoc, ←pow_succ'], }
end
lemma X_pow_mul_assoc {n : ℕ} : (p * X^n) * q = (p * q) * X^n :=
by rw [mul_assoc, X_pow_mul, ←mul_assoc]
lemma commute_X (p : polynomial R) : commute X p := X_mul
/-- coeff p n is the coefficient of X^n in p -/
def coeff (p : polynomial R) := p.to_fun
@[simp] lemma coeff_mk (s) (f) (h) : coeff (finsupp.mk s f h : polynomial R) = f := rfl
lemma coeff_monomial : coeff (monomial n a) m = if n = m then a else 0 :=
by { dsimp [monomial, single, finsupp.single], congr, }
/--
This lemma is needed for occasions when we break through the abstraction from
`polynomial` to `finsupp`; ideally it wouldn't be necessary at all.
-/
lemma coeff_single : coeff (single n a) m = if n = m then a else 0 :=
coeff_monomial
@[simp] lemma coeff_zero (n : ℕ) : coeff (0 : polynomial R) n = 0 := rfl
@[simp] lemma coeff_one_zero : coeff (1 : polynomial R) 0 = 1 := coeff_monomial
@[simp] lemma coeff_X_one : coeff (X : polynomial R) 1 = 1 := coeff_monomial
@[simp] lemma coeff_X_zero : coeff (X : polynomial R) 0 = 0 := coeff_monomial
lemma coeff_X : coeff (X : polynomial R) n = if 1 = n then 1 else 0 := coeff_monomial
theorem ext_iff {p q : polynomial R} : p = q ↔ ∀ n, coeff p n = coeff q n :=
⟨λ h n, h ▸ rfl, finsupp.ext⟩
@[ext] lemma ext {p q : polynomial R} : (∀ n, coeff p n = coeff q n) → p = q :=
(@ext_iff _ _ p q).2
-- this has the same content as the subsingleton
lemma eq_zero_of_eq_zero (h : (0 : R) = (1 : R)) (p : polynomial R) : p = 0 :=
by rw [←one_smul R p, ←h, zero_smul]
lemma support_monomial (n) (a : R) (H : a ≠ 0) : (monomial n a).support = singleton n :=
begin
ext,
have m3 : a_1 ∈ (monomial n a).support ↔ coeff (monomial n a) a_1 ≠ 0 := (monomial n a).mem_support_to_fun a_1,
rw [finset.mem_singleton, m3, coeff_monomial],
split_ifs,
{ rwa [h, eq_self_iff_true, iff_true], },
{ rw [← @not_not (a_1=n)],
apply not_congr,
rw [eq_self_iff_true, true_iff, ← ne.def],
symmetry,
assumption, },
end
lemma support_monomial' (n) (a : R) : (monomial n a).support ⊆ singleton n :=
begin
by_cases h : a = 0,
{ rw [h, monomial_zero_right, support_zero],
exact finset.empty_subset {n}, },
{ rw support_monomial n a h,
exact finset.subset.refl {n}, },
end
lemma X_pow_eq_monomial (n) : X ^ n = monomial n (1:R) :=
begin
induction n with n hn,
{ refl, },
{ conv_rhs {rw nat.succ_eq_add_one, congr, skip, rw ← mul_one (1:R)},
rw [← monomial_mul_monomial, ← hn, pow_succ, X_mul, X], },
end
lemma support_X_pow (H : ¬ (1:R) = 0) (n : ℕ) : (X^n : polynomial R).support = singleton n :=
begin
convert support_monomial n 1 H,
exact X_pow_eq_monomial n,
end
lemma support_X_empty (H : (1:R)=0) : (X : polynomial R).support = ∅ :=
begin
rw [X, H, monomial_zero_right, support_zero],
end
lemma support_X (H : ¬ (1 : R) = 0) : (X : polynomial R).support = singleton 1 :=
begin
rw [← pow_one X, support_X_pow H 1],
end
end semiring
section comm_semiring
variables [comm_semiring R]
instance : comm_semiring (polynomial R) := add_monoid_algebra.comm_semiring
instance : comm_monoid (polynomial R) := comm_semiring.to_comm_monoid (polynomial R)
end comm_semiring
section ring
variables [ring R]
instance : ring (polynomial R) := add_monoid_algebra.ring
@[simp] lemma coeff_neg (p : polynomial R) (n : ℕ) : coeff (-p) n = -coeff p n := rfl
@[simp]
lemma coeff_sub (p q : polynomial R) (n : ℕ) : coeff (p - q) n = coeff p n - coeff q n := rfl
end ring
instance [comm_ring R] : comm_ring (polynomial R) := add_monoid_algebra.comm_ring
section nonzero_semiring
variables [semiring R] [nontrivial R]
instance : nontrivial (polynomial R) :=
begin
refine nontrivial_of_ne 0 1 _, intro h,
have := coeff_zero 0, revert this, rw h, simp,
end
lemma X_ne_zero : (X : polynomial R) ≠ 0 :=
mt (congr_arg (λ p, coeff p 1)) (by simp)
end nonzero_semiring
section repr
variables [semiring R]
local attribute [instance, priority 100] classical.prop_decidable
instance [has_repr R] : has_repr (polynomial R) :=
⟨λ p, if p = 0 then "0"
else (p.support.sort (≤)).foldr
(λ n a, a ++ (if a = "" then "" else " + ") ++
if n = 0
then "C (" ++ repr (coeff p n) ++ ")"
else if n = 1
then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X"
else if (coeff p n) = 1 then "X ^ " ++ repr n
else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""⟩
end repr
end polynomial
|
93afda6b20ae18a02bed6f296e142a929df3909d | 46125763b4dbf50619e8846a1371029346f4c3db | /src/data/complex/exponential.lean | 3ebba555aac9afc2ed37b8bf17e61eede2283dd9 | [
"Apache-2.0"
] | permissive | thjread/mathlib | a9d97612cedc2c3101060737233df15abcdb9eb1 | 7cffe2520a5518bba19227a107078d83fa725ddc | refs/heads/master | 1,615,637,696,376 | 1,583,953,063,000 | 1,583,953,063,000 | 246,680,271 | 0 | 0 | Apache-2.0 | 1,583,960,875,000 | 1,583,960,875,000 | null | UTF-8 | Lean | false | false | 47,836 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir
-/
import algebra.archimedean algebra.geom_sum
import data.nat.choose data.complex.basic
import tactic.linarith
local notation `abs'` := _root_.abs
open is_absolute_value
open_locale classical
section
open real is_absolute_value finset
lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ}
(h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k :=
begin
assume l k hkm hkl,
generalize hp : l - k = p,
have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp,
subst this,
clear hkl hp,
induction p with p ih,
{ simp },
{ exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih }
end
variables {α : Type*} {β : Type*} [ring β]
[discrete_linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv]
lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a)
(hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f :=
λ ε ε0,
let ⟨k, hk⟩ := archimedean.arch a ε0 in
have h : ∃ l, ∀ n ≥ m, a - add_monoid.smul l ε < f n :=
⟨k + k + 1, λ n hnm, lt_of_lt_of_le
(show a - add_monoid.smul (k + (k + 1)) ε < -abs (f n),
from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin
rw [neg_sub, lt_sub_iff_add_lt, add_monoid.add_smul],
exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk
(lt_add_of_pos_left _ ε0)),
end))
(neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩,
let l := nat.find h in
have hl : ∀ (n : ℕ), n ≥ m → f n > a - add_monoid.smul l ε := nat.find_spec h,
have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _))
(lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))),
begin
cases classical.not_forall.1
(nat.find_min h (nat.pred_lt hl0)) with i hi,
rw [not_imp, not_lt] at hi,
existsi i,
assume j hj,
have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj,
rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'],
exact calc f i ≤ a - add_monoid.smul (nat.pred l) ε : hi.2
... = a - add_monoid.smul l ε + ε :
by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_smul',
sub_add, add_sub_cancel] }
... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _
end
lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a)
(hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f :=
begin
refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _
(-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2,
ext,
exact neg_neg _
end
lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) →
is_cau_seq abs (λ n, (range n).sum g) → is_cau_seq abv (λ n, (range n).sum f) :=
begin
assume hm hg ε ε0,
cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi,
existsi max n i,
assume j ji,
have hi₁ := hi j (le_trans (le_max_right n i) ji),
have hi₂ := hi (max n i) (le_max_right n i),
have sub_le := abs_sub_le ((range j).sum g) ((range i).sum g) ((range (max n i)).sum g),
have := add_lt_add hi₁ hi₂,
rw [abs_sub ((range (max n i)).sum g), add_halves ε] at this,
refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this,
generalize hk : j - max n i = k,
clear this hi₂ hi₁ hi ε0 ε hg sub_le,
rw nat.sub_eq_iff_eq_add ji at hk,
rw hk,
clear hk ji j,
induction k with k' hi,
{ simp [abv_zero abv] },
{ dsimp at *,
simp only [nat.succ_add, sum_range_succ, sub_eq_add_neg, add_assoc],
refine le_trans (abv_add _ _ _) _,
exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi },
end
lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, (range m).sum (λ n, abv (f n))) →
is_cau_seq abv (λ m, (range m).sum f) :=
is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _)
lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv]
(x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, (range n).sum (λ m, x ^ m)) :=
have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1,
is_cau_series_of_abv_cau
begin
simp only [abv_pow abv] {eta := ff},
have : (λ (m : ℕ), (range m).sum (λ n, (abv x) ^ n)) =
λ m, geom_series (abv x) m := rfl,
simp only [this, geom_sum hx1'] {eta := ff},
conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] },
refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _,
{ assume n hn,
rw abs_of_nonneg,
refine div_le_div_of_le_of_pos (sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _))
(sub_pos.2 hx1),
refine div_nonneg (sub_nonneg.2 _) (sub_pos.2 hx1),
clear hn,
induction n with n ih,
{ simp },
{ rw [_root_.pow_succ, ← one_mul (1 : α)],
refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } },
{ assume n hn,
refine div_le_div_of_le_of_pos (sub_le_sub_left _ _) (sub_pos.2 hx1),
rw [← one_mul (_ ^ n), _root_.pow_succ],
exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) }
end
lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) : is_cau_seq abs (λ m, (range m).sum (λ n, a * x ^ n)) :=
have is_cau_seq abs (λ m, a * (range m).sum (λ n, x ^ n)) := (cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2,
by simpa only [mul_sum]
lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α)
(hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) :
is_cau_seq abv (λ m, (range m).sum f) :=
have har1 : abs r < 1, by rwa abs_of_nonneg hr0,
begin
refine is_cau_series_of_abv_le_cau n.succ _ (is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1),
assume m hmn,
cases classical.em (r = 0) with r_zero r_ne_zero,
{ have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn,
have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])),
simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] },
generalize hk : m - n.succ = k,
have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero),
replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk,
induction k with k ih generalizing m n,
{ rw [hk, zero_add, mul_right_comm, ← pow_inv _ _ r_ne_zero, ← div_eq_mul_inv, mul_div_cancel],
exact (ne_of_lt (pow_pos r_pos _)).symm },
{ have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp),
rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc],
exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn))
(mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) }
end
lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) :
(range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) =
(range n).sum (λ m, (range (n - m)).sum (f m)) :=
have h₁ : ((range n).sigma (range ∘ nat.succ)).sum
(λ (a : Σ m, ℕ), f (a.2) (a.1 - a.2)) =
(range n).sum (λ m, (range (m + 1)).sum
(λ k, f k (m - k))) := sum_sigma,
have h₂ : ((range n).sigma (λ m, range (n - m))).sum (λ a : Σ (m : ℕ), ℕ, f (a.1) (a.2)) =
(range n).sum (λ m, sum (range (n - m)) (f m)) := sum_sigma,
h₁ ▸ h₂ ▸ sum_bij
(λ a _, ⟨a.2, a.1 - a.2⟩)
(λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1,
have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2,
mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁),
mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩)
(λ _ _, rfl)
(λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h,
have ha : a₁ < n ∧ a₂ ≤ a₁ :=
⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩,
have hb : b₁ < n ∧ b₂ ≤ b₁ :=
⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩,
have h : a₂ = b₂ ∧ _ := sigma.mk.inj h,
have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2),
sigma.mk.inj_iff.2
⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl,
(heq_of_eq h.1)⟩)
(λ ⟨a₁, a₂⟩ ha,
have ha : a₁ < n ∧ a₂ < n - a₁ :=
⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩,
⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2),
mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩,
sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩)
lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) :
abv (s.sum f) ≤ s.sum (abv ∘ f) :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s (by simp [abv_zero abv])
(λ a s has ih, by rw [sum_insert has, sum_insert has];
exact le_trans (abv_add abv _ _) (add_le_add_left ih _))
lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α}
{n m : ℕ} (hnm : n ≤ m) : (range m).sum f - (range n).sum f =
((range m).filter (λ k, n ≤ k)).sum f :=
begin
rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)),
sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'],
refine finset.sum_congr
(finset.ext.2 $ λ a, ⟨λ h, by simp at *; finish,
λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm,
by simp * at *⟩)
(λ _ _, rfl),
end
lemma cauchy_product {a b : ℕ → β}
(ha : is_cau_seq abs (λ m, (range m).sum (λ n, abv (a n))))
(hb : is_cau_seq abv (λ m, (range m).sum b)) (ε : α) (ε0 : 0 < ε) :
∃ i : ℕ, ∀ j ≥ i, abv ((range j).sum a * (range j).sum b -
(range j).sum (λ n, (range (n + 1)).sum (λ m, a m * b (n - m)))) < ε :=
let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in
let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in
have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0),
have hPε0 : 0 < ε / (2 * P),
from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0),
let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in
have hQε0 : 0 < ε / (4 * Q),
from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))),
let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in
⟨2 * (max N M + 1), λ K hK,
have h₁ : sum (range K) (λ m, (range (m + 1)).sum (λ k, a k * b (m - k))) =
sum (range K) (λ m, sum (range (K - m)) (λ n, a m * b n)),
by simpa using sum_range_diag_flip K (λ m n, a m * b n),
have h₂ : (λ i, (range (K - i)).sum (λ k, a i * b k)) = (λ i, a i * (range (K - i)).sum b),
by simp [finset.mul_sum],
have h₃ : (range K).sum (λ i, a i * (range (K - i)).sum b) =
(range K).sum (λ i, a i * ((range (K - i)).sum b - (range K).sum b))
+ (range K).sum (λ i, a i * (range K).sum b),
by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm],
have two_mul_two : (4 : α) = 2 * 2, by norm_num,
have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0,
have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0,
have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε,
by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)),
two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves],
have hNMK : max N M + 1 < K,
from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK,
have hKN : N < K,
from calc N ≤ max N M : le_max_left _ _
... < max N M + 1 : nat.lt_succ_self _
... < K : hNMK,
have hsumlesum : (range (max N M + 1)).sum (λ i, abv (a i) *
abv ((range (K - i)).sum b - (range K).sum b)) ≤ (range (max N M + 1)).sum
(λ i, abv (a i) * (ε / (2 * P))),
from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left
(le_of_lt (hN (K - m) K
(nat.le_sub_left_of_add_le (le_trans
(by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ))
(le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK))
(le_of_lt hKN))) (abv_nonneg abv _)),
have hsumltP : sum (range (max N M + 1)) (λ n, abv (a n)) < P :=
calc sum (range (max N M + 1)) (λ n, abv (a n))
= abs (sum (range (max N M + 1)) (λ n, abv (a n))) :
eq.symm (abs_of_nonneg (sum_nonneg (λ x h, abv_nonneg abv (a x))))
... < P : hP (max N M + 1),
begin
rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv],
refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _,
suffices : (range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) +
((range K).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) -(range (max N M + 1)).sum
(λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b))) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q),
{ rw hε at this, simpa [abv_mul abv] },
refine add_lt_add (lt_of_le_of_lt hsumlesum
(by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _,
rw sum_range_sub_sum_range (le_of_lt hNMK),
exact calc sum ((range K).filter (λ k, max N M + 1 ≤ k))
(λ i, abv (a i) * abv (sum (range (K - i)) b - sum (range K) b))
≤ sum ((range K).filter (λ k, max N M + 1 ≤ k)) (λ i, abv (a i) * (2 * Q)) :
sum_le_sum (λ n hn, begin
refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _),
rw sub_eq_add_neg,
refine le_trans (abv_add _ _ _) _,
rw [two_mul, abv_neg abv],
exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)),
end)
... < ε / (4 * Q) * (2 * Q) :
by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)];
refine (mul_lt_mul_right $ by rw two_mul;
exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))
(lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2
(lt_of_le_of_lt (le_abs_self _)
(hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK))
(nat.le_succ_of_le (le_max_right _ _))))
end⟩
end
open finset
open cau_seq
namespace complex
lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs
(λ n, (range n).sum (λ m, abs (z ^ m / nat.fact m))) :=
let ⟨n, hn⟩ := exists_nat_gt (abs z) in
have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn,
series_ratio_test n (complex.abs z / n) (div_nonneg_of_nonneg_of_pos (complex.abs_nonneg _) hn0)
(by rwa [div_lt_iff hn0, one_mul])
(λ m hm,
by rw [abs_abs, abs_abs, nat.fact_succ, pow_succ,
mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc,
mul_div_right_comm, abs_mul, abs_div, abs_cast_nat];
exact mul_le_mul_of_nonneg_right
(div_le_div_of_le_left (abs_nonneg _) hn0
(nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _))
noncomputable theory
lemma is_cau_exp (z : ℂ) : is_cau_seq abs (λ n, (range n).sum (λ m, z ^ m / nat.fact m)) :=
is_cau_series_of_abv_cau (is_cau_abs_exp z)
def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨λ n, (range n).sum (λ m, z ^ m / nat.fact m), is_cau_exp z⟩
def exp (z : ℂ) : ℂ := lim (exp' z)
def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2
def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2
def tan (z : ℂ) : ℂ := sin z / cos z
def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2
def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2
def tanh (z : ℂ) : ℂ := sinh z / cosh z
end complex
namespace real
open complex
def exp (x : ℝ) : ℝ := (exp x).re
def sin (x : ℝ) : ℝ := (sin x).re
def cos (x : ℝ) : ℝ := (cos x).re
def tan (x : ℝ) : ℝ := (tan x).re
def sinh (x : ℝ) : ℝ := (sinh x).re
def cosh (x : ℝ) : ℝ := (cosh x).re
def tanh (x : ℝ) : ℝ := (tanh x).re
end real
namespace complex
variables (x y : ℂ)
@[simp] lemma exp_zero : exp 0 = 1 :=
lim_eq_of_equiv_const $
λ ε ε0, ⟨1, λ j hj, begin
convert ε0,
cases j,
{ exact absurd hj (not_le_of_gt zero_lt_one) },
{ dsimp [exp'],
induction j with j ih,
{ dsimp [exp']; simp },
{ rw ← ih dec_trivial,
simp only [sum_range_succ, pow_succ],
simp } }
end⟩
lemma exp_add : exp (x + y) = exp x * exp y :=
show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) =
lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩)
* lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩),
from
have hj : ∀ j : ℕ, (range j).sum
(λ m, (x + y) ^ m / m.fact) = (range j).sum
(λ i, (range (i + 1)).sum (λ k, x ^ k / k.fact *
(y ^ (i - k) / (i - k).fact))),
from assume j,
finset.sum_congr rfl (λ m hm, begin
rw [add_pow, div_eq_mul_inv, sum_mul],
refine finset.sum_congr rfl (λ i hi, _),
have h₁ : (nat.choose m i : ℂ) ≠ 0 := nat.cast_ne_zero.2
(nat.pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))),
have h₂ := nat.choose_mul_fact_mul_fact (nat.le_of_lt_succ $ finset.mem_range.1 hi),
rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'],
simp only [mul_left_comm (nat.choose m i : ℂ), mul_assoc, mul_left_comm (nat.choose m i : ℂ)⁻¹,
mul_comm (nat.choose m i : ℂ)],
rw inv_mul_cancel h₁,
simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm]
end),
by rw lim_mul_lim;
exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj];
exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y)))
attribute [irreducible] complex.exp
lemma exp_list_sum (l : list ℂ) : exp l.sum = (l.map exp).prod :=
@monoid_hom.map_list_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ l
lemma exp_multiset_sum (s : multiset ℂ) : exp s.sum = (s.map exp).prod :=
@monoid_hom.map_multiset_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ s
lemma exp_sum {α : Type*} (s : finset α) (f : α → ℂ) : exp (s.sum f) = s.prod (exp ∘ f) :=
@monoid_hom.map_prod α (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ f s
lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n
| 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero]
| (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul]
lemma exp_ne_zero : exp x ≠ 0 :=
λ h, @zero_ne_one ℂ _ $
by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp
lemma exp_neg : exp (-x) = (exp x)⁻¹ :=
by rw [← domain.mul_left_inj (exp_ne_zero x), ← exp_add];
simp [mul_inv_cancel (exp_ne_zero x)]
lemma exp_sub : exp (x - y) = exp x / exp y :=
by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv]
@[simp] lemma exp_conj : exp (conj x) = conj (exp x) :=
begin
dsimp [exp],
rw [← lim_conj],
refine congr_arg lim (cau_seq.ext (λ _, _)),
dsimp [exp', function.comp, cau_seq_conj],
rw ← sum_hom _ conj,
refine sum_congr rfl (λ n hn, _),
rw [conj_div, conj_pow, ← of_real_nat_cast, conj_of_real]
end
@[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x :=
eq_conj_iff_re.1 $ by rw [← exp_conj, conj_of_real]
@[simp] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x :=
of_real_exp_of_real_re _
@[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 :=
by rw [← of_real_exp_of_real_re, of_real_im]
lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl
lemma two_sinh : 2 * sinh x = exp x - exp (-x) :=
mul_div_cancel' _ two_ne_zero'
lemma two_cosh : 2 * cosh x = exp x + exp (-x) :=
mul_div_cancel' _ two_ne_zero'
@[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh]
@[simp] lemma sinh_neg : sinh (-x) = -sinh x :=
by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul]
private lemma sinh_add_aux {a b c d : ℂ} :
(a - b) * (c + d) + (a + b) * (c - d) = 2 * (a * c - b * d) := by ring
lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y :=
begin
rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), two_sinh,
exp_add, neg_add, exp_add, eq_comm,
mul_add, ← mul_assoc, two_sinh, mul_left_comm, two_sinh,
← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_add,
mul_left_comm, two_cosh, ← mul_assoc, two_cosh],
exact sinh_add_aux
end
@[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh]
@[simp] lemma cosh_neg : cosh (-x) = cosh x :=
by simp [add_comm, cosh, exp_neg]
private lemma cosh_add_aux {a b c d : ℂ} :
(a + b) * (c + d) + (a - b) * (c - d) = 2 * (a * c + b * d) := by ring
lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y :=
begin
rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), two_cosh,
exp_add, neg_add, exp_add, eq_comm,
mul_add, ← mul_assoc, two_cosh, ← mul_assoc, two_sinh,
← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_add,
mul_left_comm, two_cosh, mul_left_comm, two_sinh],
exact cosh_add_aux
end
lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y :=
by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg]
lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y :=
by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg]
lemma sinh_conj : sinh (conj x) = conj (sinh x) :=
by rw [sinh, ← conj_neg, exp_conj, exp_conj, ← conj_sub, sinh, conj_div, conj_two]
@[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x :=
eq_conj_iff_re.1 $ by rw [← sinh_conj, conj_of_real]
@[simp] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x :=
of_real_sinh_of_real_re _
@[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 :=
by rw [← of_real_sinh_of_real_re, of_real_im]
lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl
lemma cosh_conj : cosh (conj x) = conj (cosh x) :=
by rw [cosh, ← conj_neg, exp_conj, exp_conj, ← conj_add, cosh, conj_div, conj_two]
@[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x :=
eq_conj_iff_re.1 $ by rw [← cosh_conj, conj_of_real]
@[simp] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x :=
of_real_cosh_of_real_re _
@[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 :=
by rw [← of_real_cosh_of_real_re, of_real_im]
lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl
lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl
@[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh]
@[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div]
lemma tanh_conj : tanh (conj x) = conj (tanh x) :=
by rw [tanh, sinh_conj, cosh_conj, ← conj_div, tanh]
@[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x :=
eq_conj_iff_re.1 $ by rw [← tanh_conj, conj_of_real]
@[simp] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x :=
of_real_tanh_of_real_re _
@[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 :=
by rw [← of_real_tanh_of_real_re, of_real_im]
lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl
lemma cosh_add_sinh : cosh x + sinh x = exp x :=
by rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_add,
two_cosh, two_sinh, add_add_sub_cancel, two_mul]
lemma sinh_add_cosh : sinh x + cosh x = exp x :=
by rw [add_comm, cosh_add_sinh]
lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) :=
by rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_sub,
two_cosh, two_sinh, add_sub_sub_cancel, two_mul]
lemma cosh_sq_sub_sinh_sq : cosh x ^ 2 - sinh x ^ 2 = 1 :=
by rw [sq_sub_sq, cosh_add_sinh, cosh_sub_sinh, ← exp_add, add_neg_self, exp_zero]
@[simp] lemma sin_zero : sin 0 = 0 := by simp [sin]
@[simp] lemma sin_neg : sin (-x) = -sin x :=
by simp [sin, sub_eq_add_neg, exp_neg, (neg_div _ _).symm, add_mul]
lemma two_sin : 2 * sin x = (exp (-x * I) - exp (x * I)) * I :=
mul_div_cancel' _ two_ne_zero'
lemma two_cos : 2 * cos x = exp (x * I) + exp (-x * I) :=
mul_div_cancel' _ two_ne_zero'
lemma sinh_mul_I : sinh (x * I) = sin x * I :=
by rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), two_sinh,
← mul_assoc, two_sin, mul_assoc, I_mul_I, mul_neg_one,
neg_sub, neg_mul_eq_neg_mul]
lemma cosh_mul_I : cosh (x * I) = cos x :=
by rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), two_cosh,
two_cos, neg_mul_eq_neg_mul]
lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y :=
by rw [← domain.mul_right_inj I_ne_zero, ← sinh_mul_I,
add_mul, add_mul, mul_right_comm, ← sinh_mul_I,
mul_assoc, ← sinh_mul_I, ← cosh_mul_I, ← cosh_mul_I, sinh_add]
@[simp] lemma cos_zero : cos 0 = 1 := by simp [cos]
@[simp] lemma cos_neg : cos (-x) = cos x :=
by simp [cos, sub_eq_add_neg, exp_neg, add_comm]
private lemma cos_add_aux {a b c d : ℂ} :
(a + b) * (c + d) - (b - a) * (d - c) * (-1) =
2 * (a * c + b * d) := by ring
lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y :=
by rw [← cosh_mul_I, add_mul, cosh_add, cosh_mul_I, cosh_mul_I,
sinh_mul_I, sinh_mul_I, mul_mul_mul_comm, I_mul_I,
mul_neg_one, sub_eq_add_neg]
lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y :=
by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg]
lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y :=
by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg]
lemma sin_conj : sin (conj x) = conj (sin x) :=
by rw [← domain.mul_right_inj I_ne_zero, ← sinh_mul_I,
← conj_neg_I, ← conj_mul, ← conj_mul, sinh_conj,
mul_neg_eq_neg_mul_symm, sinh_neg, sinh_mul_I, mul_neg_eq_neg_mul_symm]
@[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x :=
eq_conj_iff_re.1 $ by rw [← sin_conj, conj_of_real]
@[simp] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x :=
of_real_sin_of_real_re _
@[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 :=
by rw [← of_real_sin_of_real_re, of_real_im]
lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl
lemma cos_conj : cos (conj x) = conj (cos x) :=
by rw [← cosh_mul_I, ← conj_neg_I, ← conj_mul, ← cosh_mul_I,
cosh_conj, mul_neg_eq_neg_mul_symm, cosh_neg]
@[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x :=
eq_conj_iff_re.1 $ by rw [← cos_conj, conj_of_real]
@[simp] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x :=
of_real_cos_of_real_re _
@[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 :=
by rw [← of_real_cos_of_real_re, of_real_im]
lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl
@[simp] lemma tan_zero : tan 0 = 0 := by simp [tan]
lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl
@[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div]
lemma tan_conj : tan (conj x) = conj (tan x) :=
by rw [tan, sin_conj, cos_conj, ← conj_div, tan]
@[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x :=
eq_conj_iff_re.1 $ by rw [← tan_conj, conj_of_real]
@[simp] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x :=
of_real_tan_of_real_re _
@[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 :=
by rw [← of_real_tan_of_real_re, of_real_im]
lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl
lemma cos_add_sin_I : cos x + sin x * I = exp (x * I) :=
by rw [← cosh_add_sinh, sinh_mul_I, cosh_mul_I]
lemma cos_sub_sin_I : cos x - sin x * I = exp (-x * I) :=
by rw [← neg_mul_eq_neg_mul, ← cosh_sub_sinh, sinh_mul_I, cosh_mul_I]
lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 :=
eq.trans
(by rw [cosh_mul_I, sinh_mul_I, mul_pow, I_sq, mul_neg_one, sub_neg_eq_add, add_comm])
(cosh_sq_sub_sinh_sq (x * I))
lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 :=
by rw [two_mul, cos_add, ← pow_two, ← pow_two]
lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 :=
by rw [cos_two_mul', eq_sub_iff_add_eq.2 (sin_sq_add_cos_sq x),
← sub_add, sub_add_eq_add_sub, two_mul]
lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x :=
by rw [two_mul, sin_add, two_mul, add_mul, mul_comm]
lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 :=
by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero', -one_div_eq_inv]
lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 :=
by { rw [←sin_sq_add_cos_sq x], simp }
lemma exp_mul_I : exp (x * I) = cos x + sin x * I :=
(cos_add_sin_I _).symm
lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) :=
by rw [exp_add, exp_mul_I]
lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) :=
by rw [← exp_add_mul_I, re_add_im]
theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) : (cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I :=
begin
rw [← exp_mul_I, ← exp_mul_I],
induction n with n ih,
{ rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] },
{ rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] }
end
end complex
namespace real
open complex
variables (x y : ℝ)
@[simp] lemma exp_zero : exp 0 = 1 :=
by simp [real.exp]
lemma exp_add : exp (x + y) = exp x * exp y :=
by simp [exp_add, exp]
lemma exp_list_sum (l : list ℝ) : exp l.sum = (l.map exp).prod :=
@monoid_hom.map_list_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ l
lemma exp_multiset_sum (s : multiset ℝ) : exp s.sum = (s.map exp).prod :=
@monoid_hom.map_multiset_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ s
lemma exp_sum {α : Type*} (s : finset α) (f : α → ℝ) : exp (s.sum f) = s.prod (exp ∘ f) :=
@monoid_hom.map_prod α (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ f s
lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n
| 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero]
| (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul]
lemma exp_ne_zero : exp x ≠ 0 :=
λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at *
lemma exp_neg : exp (-x) = (exp x)⁻¹ :=
by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg,
of_real_inv, of_real_exp]
lemma exp_sub : exp (x - y) = exp x / exp y :=
by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv]
@[simp] lemma sin_zero : sin 0 = 0 := by simp [sin]
@[simp] lemma sin_neg : sin (-x) = -sin x :=
by simp [sin, exp_neg, (neg_div _ _).symm, add_mul]
lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y :=
by rw [← of_real_inj]; simp [sin, sin_add]
@[simp] lemma cos_zero : cos 0 = 1 := by simp [cos]
@[simp] lemma cos_neg : cos (-x) = cos x :=
by simp [cos, exp_neg]
lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y :=
by rw ← of_real_inj; simp [cos, cos_add]
lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y :=
by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg]
lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y :=
by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg]
lemma tan_eq_sin_div_cos : tan x = sin x / cos x :=
if h : complex.cos x = 0 then by simp [sin, cos, tan, *, complex.tan, div_eq_mul_inv] at *
else
by rw [sin, cos, tan, complex.tan, ← of_real_inj, div_eq_mul_inv, mul_re];
simp [norm_sq, (div_div_eq_div_mul _ _ _).symm, div_self h]; refl
@[simp] lemma tan_zero : tan 0 = 0 := by simp [tan]
@[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div]
lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 :=
of_real_inj.1 $ by simpa using sin_sq_add_cos_sq x
lemma sin_sq_le_one : sin x ^ 2 ≤ 1 :=
by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_right' (pow_two_nonneg _)
lemma cos_sq_le_one : cos x ^ 2 ≤ 1 :=
by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_left' (pow_two_nonneg _)
lemma abs_sin_le_one : abs' (sin x) ≤ 1 :=
(mul_self_le_mul_self_iff (_root_.abs_nonneg (sin x)) (by exact zero_le_one)).2 $
by rw [← _root_.abs_mul, abs_mul_self, mul_one, ← pow_two];
apply sin_sq_le_one
lemma abs_cos_le_one : abs' (cos x) ≤ 1 :=
(mul_self_le_mul_self_iff (_root_.abs_nonneg (cos x)) (by exact zero_le_one)).2 $
by rw [← _root_.abs_mul, abs_mul_self, mul_one, ← pow_two];
apply cos_sq_le_one
lemma sin_le_one : sin x ≤ 1 :=
(abs_le.1 (abs_sin_le_one _)).2
lemma cos_le_one : cos x ≤ 1 :=
(abs_le.1 (abs_cos_le_one _)).2
lemma neg_one_le_sin : -1 ≤ sin x :=
(abs_le.1 (abs_sin_le_one _)).1
lemma neg_one_le_cos : -1 ≤ cos x :=
(abs_le.1 (abs_cos_le_one _)).1
lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 :=
by rw ← of_real_inj; simp [cos_two_mul]
lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x :=
by rw ← of_real_inj; simp [sin_two_mul]
lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 :=
of_real_inj.1 $ by simpa using cos_square x
lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 :=
eq_sub_iff_add_eq.2 $ sin_sq_add_cos_sq _
@[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh]
@[simp] lemma sinh_neg : sinh (-x) = -sinh x :=
by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul]
lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y :=
by rw ← of_real_inj; simp [sinh_add]
@[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh]
@[simp] lemma cosh_neg : cosh (-x) = cosh x :=
by simp [cosh, exp_neg]
lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y :=
by rw ← of_real_inj; simp [cosh, cosh_add]
lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y :=
by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg]
lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y :=
by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg]
lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x :=
of_real_inj.1 $ by simp [tanh_eq_sinh_div_cosh]
@[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh]
@[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div]
open is_absolute_value
/- TODO make this private and prove ∀ x -/
lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x :=
calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') :
le_lim (cau_seq.le_of_exists ⟨2,
λ j hj, show x + (1 : ℝ) ≤ ((range j).sum (λ m, (x ^ m / m.fact : ℂ))).re,
from have h₁ : (((λ m : ℕ, (x ^ m / m.fact : ℂ)) ∘ nat.succ) 0).re = x, by simp,
have h₂ : ((x : ℂ) ^ 0 / nat.fact 0).re = 1, by simp,
begin
rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ',
add_re, add_re, h₁, h₂, add_assoc,
← @sum_hom _ _ _ _ _ _ _ complex.re
(is_add_group_hom.to_is_add_monoid_hom _)],
refine le_add_of_nonneg_of_le (sum_nonneg (λ m hm, _)) (le_refl _), dsimp [-nat.fact_succ],
rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re],
exact div_nonneg (pow_nonneg hx _) (nat.cast_pos.2 (nat.fact_pos _)),
end⟩)
... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re]
lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x :=
by linarith [add_one_le_exp_of_nonneg hx]
lemma exp_pos (x : ℝ) : 0 < exp x :=
(le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp)
(λ h, by rw [← neg_neg x, real.exp_neg];
exact inv_pos (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h))))
@[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x :=
abs_of_pos (exp_pos _)
lemma exp_strict_mono : strict_mono exp :=
λ x y h, by rw [← sub_add_cancel y x, real.exp_add];
exact (lt_mul_iff_one_lt_left (exp_pos _)).2
(lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith)))
lemma exp_lt_exp {x y : ℝ} : exp x < exp y ↔ x < y := exp_strict_mono.lt_iff_lt
lemma exp_le_exp {x y : ℝ} : exp x ≤ exp y ↔ x ≤ y := exp_strict_mono.le_iff_le
lemma exp_injective : function.injective exp := exp_strict_mono.injective
@[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 :=
by rw [← exp_zero, exp_injective.eq_iff]
lemma one_lt_exp_iff {x : ℝ} : 1 < exp x ↔ 0 < x :=
by rw [← exp_zero, exp_lt_exp]
lemma exp_lt_one_iff {x : ℝ} : exp x < 1 ↔ x < 0 :=
by rw [← exp_zero, exp_lt_exp]
end real
namespace complex
lemma sum_div_fact_le {α : Type*} [discrete_linear_ordered_field α] (n j : ℕ) (hn : 0 < n) :
(sum (filter (λ k, n ≤ k) (range j)) (λ m : ℕ, (1 / m.fact : α))) ≤ n.succ * (n.fact * n)⁻¹ :=
calc (filter (λ k, n ≤ k) (range j)).sum (λ m : ℕ, (1 / m.fact : α))
= (range (j - n)).sum (λ m, 1 / (m + n).fact) :
sum_bij (λ m _, m - n)
(λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2
(by simp at hm; tauto))
(λ m hm, by rw nat.sub_add_cancel; simp at *; tauto)
(λ a₁ a₂ ha₁ ha₂ h,
by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add, add_right_inj, eq_comm] at h;
simp at *; tauto)
(λ b hb, ⟨b + n, mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩,
by rw nat.add_sub_cancel⟩)
... ≤ (range (j - n)).sum (λ m, (nat.fact n * n.succ ^ m)⁻¹) :
begin
refine sum_le_sum (assume m n, _),
rw [one_div_eq_inv, inv_le_inv],
{ rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm],
exact nat.fact_mul_pow_le_fact },
{ exact nat.cast_pos.2 (nat.fact_pos _) },
{ exact mul_pos (nat.cast_pos.2 (nat.fact_pos _)) (pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) },
end
... = (nat.fact n)⁻¹ * (range (j - n)).sum (λ m, n.succ⁻¹ ^ m) :
by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.fact_succ, mul_comm, inv_pow']
... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n.fact * n) :
have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1
(mt nat.succ_inj (nat.pos_iff_ne_zero.1 hn)),
have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _),
have h₃ : (n.fact * n : α) ≠ 0, from mul_ne_zero (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.fact_pos _)))
(nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)),
have h₄ : (n.succ - 1 : α) = n, by simp,
by rw [← geom_series_def, geom_sum_inv h₁ h₂, eq_div_iff_mul_eq _ _ h₃, mul_comm _ (n.fact * n : α),
← mul_assoc (n.fact⁻¹ : α), ← mul_inv', h₄, ← mul_assoc (n.fact * n : α),
mul_comm (n : α) n.fact, mul_inv_cancel h₃];
simp [mul_add, add_mul, mul_assoc, mul_comm]
... ≤ n.succ / (n.fact * n) :
begin
refine (div_le_div_right (mul_pos _ _)).2 _,
exact nat.cast_pos.2 (nat.fact_pos _),
exact nat.cast_pos.2 hn,
exact sub_le_self _ (mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _))
end
lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) :
abs (exp x - (range n).sum (λ m, x ^ m / m.fact)) ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) :=
begin
rw [← lim_const ((range n).sum _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs],
refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩),
show abs ((range j).sum (λ m, x ^ m / m.fact) - (range n).sum (λ m, x ^ m / m.fact))
≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹),
rw sum_range_sub_sum_range hj,
exact calc abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ m / m.fact : ℂ)))
= abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ n * (x ^ (m - n) / m.fact) : ℂ))) :
congr_arg abs (sum_congr rfl (λ m hm, by rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel']; simp at hm; tauto))
... ≤ sum (filter (λ k, n ≤ k) (range j)) (λ m, abs (x ^ n * (_ / m.fact))) : abv_sum_le_sum_abv _ _
... ≤ sum (filter (λ k, n ≤ k) (range j)) (λ m, abs x ^ n * (1 / m.fact)) :
begin
refine sum_le_sum (λ m hm, _),
rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat],
refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _,
exact nat.cast_pos.2 (nat.fact_pos _),
rw abv_pow abs,
exact (pow_le_one _ (abs_nonneg _) hx),
exact pow_nonneg (abs_nonneg _) _
end
... = abs x ^ n * (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (1 / m.fact : ℝ))) :
by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm]
... ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) :
mul_le_mul_of_nonneg_left (sum_div_fact_le _ _ hn) (pow_nonneg (abs_nonneg _) _)
end
lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) :
abs (exp x - 1) ≤ 2 * abs x :=
calc abs (exp x - 1) = abs (exp x - (range 1).sum (λ m, x ^ m / m.fact)) :
by simp [sum_range_succ]
... ≤ abs x ^ 1 * ((nat.succ 1) * (nat.fact 1 * (1 : ℕ))⁻¹) :
exp_bound hx dec_trivial
... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm]
lemma abs_exp_sub_one_sub_id_le {x : ℂ} (hx : abs x ≤ 1) :
abs (exp x - 1 - x) ≤ (abs x)^2 :=
calc abs (exp x - 1 - x) = abs (exp x - (range 2).sum (λ m, x ^ m / m.fact)) :
by simp [sub_eq_add_neg, sum_range_succ]
... ≤ (abs x)^2 * (nat.succ 2 * (nat.fact 2 * (2 : ℕ))⁻¹) :
exp_bound hx dec_trivial
... ≤ (abs x)^2 * 1 :
mul_le_mul_of_nonneg_left (by norm_num) (pow_two_nonneg (abs x))
... = (abs x)^2 :
by rw [mul_one]
end complex
namespace real
open complex finset
lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) :=
calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) :
by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv]
... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) :
by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)]
... = abs (((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) +
((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)))) / 2) :
congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin
simp only [sum_range_succ],
simp [pow_succ],
apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring
end)
... ≤ abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) / 2) +
abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) / 2) :
by rw add_div; exact abs_add _ _
... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 +
abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) :
by simp [complex.abs_div]
... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 +
(complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) :
add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial))
((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial))
... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc]
lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) :=
calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) :
by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv]
... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) :
by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _),
div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num]
... = abs ((((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) -
(complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) * I) / 2) :
congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin
simp only [sum_range_succ],
simp [pow_succ],
apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring
end)
... ≤ abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) * I / 2) +
abs (-((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) * I) / 2) :
by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _
... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 +
abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) :
by simp [add_comm, complex.abs_div, complex.abs_mul]
... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 +
(complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) :
add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial))
((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial))
... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc]
lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x :=
calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) :
sub_pos.2 $ lt_sub_iff_add_lt.2
(calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2
≤ 1 * (5 / 96) + 1 / 2 :
add_le_add
(mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num))
((div_le_div_right (by norm_num)).2 (by rw [pow_two, ← abs_mul_self, _root_.abs_mul];
exact mul_le_one hx (abs_nonneg _) hx))
... < 1 : by norm_num)
... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2
lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x :=
calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) :
sub_pos.2 $ lt_sub_iff_add_lt.2
(calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6
≤ x * (5 / 96) + x / 6 :
add_le_add
(mul_le_mul_of_nonneg_right
(calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _)
(by rwa _root_.abs_of_nonneg (le_of_lt hx0))
dec_trivial
... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num))
((div_le_div_right (by norm_num)).2
(calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial
... = x : pow_one _))
... < x : by linarith)
... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound
(by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2
lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x :=
have x / 2 ≤ 1, from div_le_of_le_mul (by norm_num) (by simpa),
calc 0 < 2 * sin (x / 2) * cos (x / 2) :
mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this))
(cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))]))
... = sin x : by rw [← sin_two_mul, two_mul, add_halves]
lemma cos_one_le : cos 1 ≤ 2 / 3 :=
calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) :
sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1
... ≤ 2 / 3 : by norm_num
lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (by simp)
lemma cos_two_neg : cos 2 < 0 :=
calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm
... = _ : real.cos_two_mul 1
... ≤ 2 * (2 / 3) ^ 2 - 1 :
sub_le_sub_right (mul_le_mul_of_nonneg_left
(by rw [pow_two, pow_two]; exact
mul_self_le_mul_self (le_of_lt cos_one_pos)
cos_one_le)
(by norm_num)) _
... < 0 : by norm_num
end real
namespace complex
lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 :=
have _ := real.sin_sq_add_cos_sq x,
by simp [add_comm, abs, norm_sq, pow_two, *, sin_of_real_re, cos_of_real_re, mul_re] at *
lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re :=
by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y,
abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I,
← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)),
abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one];
exact ⟨λ h, real.exp_injective h, congr_arg _⟩
@[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x :=
by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _))
end complex
|
5c875ad8ae298158630c7d5db6fdd338f1d780c9 | f083c4ed5d443659f3ed9b43b1ca5bb037ddeb58 | /group_theory/free_group.lean | 54b57fa91b4232c4d8188ddfb15327e2f905087c | [
"Apache-2.0"
] | permissive | semorrison/mathlib | 1be6f11086e0d24180fec4b9696d3ec58b439d10 | 20b4143976dad48e664c4847b75a85237dca0a89 | refs/heads/master | 1,583,799,212,170 | 1,535,634,130,000 | 1,535,730,505,000 | 129,076,205 | 0 | 0 | Apache-2.0 | 1,551,697,998,000 | 1,523,442,265,000 | Lean | UTF-8 | Lean | false | false | 28,879 | 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 data.quot
import group_theory.subgroup
open relation
variables {α : Type*}
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.refl _
| (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*) : Type* :=
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*) (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*) (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 := λ x y z, quot.induction_on x $ λ L₁,
quot.induction_on y $ λ L₂, quot.induction_on z $ λ L₃, by simp,
one_mul := λ x, quot.induction_on x $ λ L, rfl,
mul_one := λ x, quot.induction_on x $ λ L, by simp [one_eq_mk],
mul_left_inv := λ x, quot.induction_on x $ λ L, 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*} [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) :=
⟨λ x y, quot.induction_on x $ λ L₁, quot.induction_on y $ λ L₂, by simp⟩
@[simp] lemma to_group.mul : to_group f (x * y) = to_group f x * to_group f y :=
is_group_hom.mul _ _ _
@[simp] lemma to_group.one : to_group f 1 = 1 :=
is_group_hom.one _
@[simp] lemma to_group.inv : to_group f x⁻¹ = (to_group f x)⁻¹ :=
is_group_hom.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 :=
quot.induction_on x $ λ L, list.rec_on L (is_group_hom.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_group_hom.mul g, is_group_hom.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_group_hom.mul g, is_group_hom.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 :=
λ y ⟨x, H1⟩, H1 ▸ (quot.induction_on x $ λ L,
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*} (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) :=
⟨λ x y, quot.induction_on x $ λ L₁, quot.induction_on y $ λ L₂,
by 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,
quot.induction_on x $ λ L, by simp [H1]
@[simp] lemma map.id' : map (λ z, z) x = x := map.id
theorem map.comp {γ : Type*} {f : α → β} {g : β → γ} {x} :
map g (map f x) = map (g ∘ f) x :=
quot.induction_on x $ λ L, by 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_group_hom.mul _ x y
@[simp] lemma map.one : map f 1 = 1 :=
is_group_hom.one _
@[simp] lemma map.inv : map f x⁻¹ = (map f x)⁻¹ :=
is_group_hom.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 :=
quot.induction_on x $ λ L, list.rec_on L (is_group_hom.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_group_hom.mul g, is_group_hom.inv g, hg, ih])
(show g (of x * mk t) = map f (of x * mk t),
by simp [is_group_hom.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*} [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.sum : 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 := λ x, quot.induction_on x $ λ L,
match L with [] := rfl end,
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 := λ x, quot.induction_on x $ λ L, 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 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
def to_word.mk {x : free_group α} : mk (to_word x) = x :=
quot.induction_on x $ λ L₁, reduce.self
def to_word.inj (x y : free_group α) : to_word x = to_word y → x = y :=
quot.induction_on x $ λ L₁, quot.induction_on y $ λ L₂, 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
|
2a1671355ec2cac2850960f6bacb1fdff73618d4 | e030b0259b777fedcdf73dd966f3f1556d392178 | /library/init/meta/smt/interactive.lean | 458141d38c616d189dfcc890d0d0dfaca25e8129 | [
"Apache-2.0"
] | permissive | fgdorais/lean | 17b46a095b70b21fa0790ce74876658dc5faca06 | c3b7c54d7cca7aaa25328f0a5660b6b75fe26055 | refs/heads/master | 1,611,523,590,686 | 1,484,412,902,000 | 1,484,412,902,000 | 38,489,734 | 0 | 0 | null | 1,435,923,380,000 | 1,435,923,379,000 | null | UTF-8 | Lean | false | false | 8,940 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.smt.smt_tactic init.meta.interactive
namespace smt_tactic
meta def skip : smt_tactic unit :=
return ()
meta def solve_goals : smt_tactic unit :=
repeat close
meta def step {α : Type} (tac : smt_tactic α) : smt_tactic unit :=
tac >> solve_goals
meta def execute (tac : smt_tactic unit) : tactic unit :=
using_smt tac
meta def execute_with (cfg : smt_config) (tac : smt_tactic unit) : tactic unit :=
using_smt_core cfg tac
namespace interactive
open interactive.types
meta def itactic : Type :=
smt_tactic unit
meta def intros : raw_ident_list → smt_tactic unit
| [] := smt_tactic.intros
| hs := smt_tactic.intro_lst hs
/--
Try to close main goal by using equalities implied by the congruence
closure module.
-/
meta def close : smt_tactic unit :=
smt_tactic.close
/--
Produce new facts using heuristic lemma instantiation based on E-matching.
This tactic tries to match patterns from lemmas in the main goal with terms
in the main goal. The set of lemmas is populated with theorems
tagged with the attribute specified at smt_config.em_attr, and lemmas
added using tactics such as `smt_tactic.add_lemmas`.
The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`.
-/
meta def ematch : smt_tactic unit :=
smt_tactic.ematch
meta def apply (q : qexpr0) : smt_tactic unit :=
tactic.interactive.apply q
meta def fapply (q : qexpr0) : smt_tactic unit :=
tactic.interactive.fapply q
meta def apply_instance : smt_tactic unit :=
tactic.apply_instance
meta def change (q : qexpr0) : smt_tactic unit :=
tactic.interactive.change q
meta def exact (q : qexpr0) : smt_tactic unit :=
tactic.interactive.exact q
meta def assert (h : ident) (c : colon_tk) (q : qexpr0) : smt_tactic unit :=
do e ← tactic.to_expr_strict q,
smt_tactic.assert h e
meta def define (h : ident) (c : colon_tk) (q : qexpr0) : smt_tactic unit :=
do e ← tactic.to_expr_strict q,
smt_tactic.define h e
meta def assertv (h : ident) (c : colon_tk) (q₁ : qexpr0) (a : assign_tk) (q₂ : qexpr0) : smt_tactic unit :=
do t ← tactic.to_expr_strict q₁,
v ← tactic.to_expr_strict `((%%q₂ : %%t)),
smt_tactic.assertv h t v
meta def definev (h : ident) (c : colon_tk) (q₁ : qexpr0) (a : assign_tk) (q₂ : qexpr0) : smt_tactic unit :=
do t ← tactic.to_expr_strict q₁,
v ← tactic.to_expr_strict `((%%q₂ : %%t)),
smt_tactic.definev h t v
meta def note (h : ident) (a : assign_tk) (q : qexpr0) : smt_tactic unit :=
do p ← tactic.to_expr_strict q,
smt_tactic.note h p
meta def pose (h : ident) (a : assign_tk) (q : qexpr0) : smt_tactic unit :=
do p ← tactic.to_expr_strict q,
smt_tactic.pose h p
meta def add_fact (q : qexpr0) : smt_tactic unit :=
do h ← tactic.get_unused_name `h none,
p ← tactic.to_expr_strict q,
smt_tactic.note h p
meta def trace_state : smt_tactic unit :=
smt_tactic.trace_state
meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit :=
tactic.trace a
meta def destruct (q : qexpr0) : smt_tactic unit :=
do p ← tactic.to_expr_strict q,
smt_tactic.destruct p
meta def by_cases (q : qexpr0) : smt_tactic unit :=
do p ← tactic.to_expr_strict q,
smt_tactic.by_cases p
meta def by_contradiction : smt_tactic unit :=
smt_tactic.by_contradiction
meta def by_contra : smt_tactic unit :=
smt_tactic.by_contradiction
open tactic (resolve_name transparency to_expr)
private meta def report_invalid_em_lemma {α : Type} (n : name) : tactic α :=
fail ("invalid ematch lemma '" ++ to_string n ++ "'")
private meta def add_lemma_name (md : transparency) (lhs_lemma : bool) (n : name) : smt_tactic unit :=
do
e ← resolve_name n,
match e with
| expr.const n _ := (add_ematch_lemma_from_decl_core md lhs_lemma n) <|> report_invalid_em_lemma n
| expr.local_const _ _ _ _ := (add_ematch_lemma_core md lhs_lemma e) <|> report_invalid_em_lemma n
| _ := tactic.report_resolve_name_failure e n
end
private meta def add_lemma_pexpr (md : transparency) (lhs_lemma : bool) (p : pexpr) : smt_tactic unit :=
let e := pexpr.to_raw_expr p in
match e with
| (expr.const c []) := add_lemma_name md lhs_lemma c
| (expr.local_const c _ _ _) := add_lemma_name md lhs_lemma c
| _ := do new_e ← to_expr p, add_ematch_lemma_core md lhs_lemma new_e
end
private meta def add_lemma_pexprs (md : transparency) (lhs_lemma : bool) : list pexpr → smt_tactic unit
| [] := return ()
| (p::ps) := add_lemma_pexpr md lhs_lemma p >> add_lemma_pexprs ps
meta def add_lemma (l : qexpr_list_or_qexpr0) : smt_tactic unit :=
add_lemma_pexprs reducible ff l
meta def add_lhs_lemma (l : qexpr_list_or_qexpr0) : smt_tactic unit :=
add_lemma_pexprs reducible tt l
private meta def add_eqn_lemmas_for_core (md : transparency) : list name → smt_tactic unit
| [] := return ()
| (c::cs) := do
e ← resolve_name c,
match e with
| expr.const n _ := add_ematch_eqn_lemmas_for_core md n >> add_eqn_lemmas_for_core cs
| _ := fail $ "'" ++ to_string c ++ "' is not a constant"
end
meta def add_eqn_lemmas_for (ids : raw_ident_list) : smt_tactic unit :=
add_eqn_lemmas_for_core reducible ids
meta def add_eqn_lemmas (ids : raw_ident_list) : smt_tactic unit :=
add_eqn_lemmas_for ids
private meta def add_hinst_lemma_from_name (md : transparency) (lhs_lemma : bool) (n : name) (hs : hinst_lemmas) : smt_tactic hinst_lemmas :=
do
e ← resolve_name n,
match e with
| expr.const n _ :=
(do h ← hinst_lemma.mk_from_decl_core md n lhs_lemma, return $ hs^.add h)
<|>
(do hs₁ ← mk_ematch_eqn_lemmas_for_core md n, return $ hs^.merge hs₁)
<|>
report_invalid_em_lemma n
| expr.local_const _ _ _ _ :=
(do h ← hinst_lemma.mk_core md e lhs_lemma, return $ hs^.add h)
<|>
report_invalid_em_lemma n
| _ := tactic.report_resolve_name_failure e n
end
private meta def add_hinst_lemma_from_pexpr (md : transparency) (lhs_lemma : bool) (p : pexpr) (hs : hinst_lemmas) : smt_tactic hinst_lemmas :=
let e := pexpr.to_raw_expr p in
match e with
| (expr.const c []) := add_hinst_lemma_from_name md lhs_lemma c hs
| (expr.local_const c _ _ _) := add_hinst_lemma_from_name md lhs_lemma c hs
| _ := do new_e ← to_expr p, h ← hinst_lemma.mk_core md new_e lhs_lemma, return $ hs^.add h
end
private meta def add_hinst_lemmas_from_pexprs (md : transparency) (lhs_lemma : bool) : list pexpr → hinst_lemmas → smt_tactic hinst_lemmas
| [] hs := return hs
| (p::ps) hs := do hs₁ ← add_hinst_lemma_from_pexpr md lhs_lemma p hs, add_hinst_lemmas_from_pexprs ps hs₁
meta def ematch_using (l : qexpr_list_or_qexpr0) : smt_tactic unit :=
do hs ← add_hinst_lemmas_from_pexprs reducible ff l hinst_lemmas.mk,
smt_tactic.ematch_using hs
/-- Try the given tactic, and do nothing if it fails. -/
meta def try (t : itactic) : smt_tactic unit :=
smt_tactic.try t
/-- Keep applying the given tactic until it fails. -/
meta def repeat (t : itactic) : smt_tactic unit :=
smt_tactic.repeat t
/-- Apply the given tactic to all remaining goals. -/
meta def all_goals (t : itactic) : smt_tactic unit :=
smt_tactic.all_goals t
meta def induction (p : qexpr0) (rec_name : using_ident) (ids : with_ident_list) : smt_tactic unit :=
slift (tactic.interactive.induction p rec_name ids)
/-- Simplify the target type of the main goal. -/
meta def simp (hs : opt_qexpr_list) (attr_names : with_ident_list) (ids : without_ident_list) : smt_tactic unit :=
tactic.interactive.simp hs attr_names ids []
meta def ctx_simp (hs : opt_qexpr_list) (attr_names : with_ident_list) (ids : without_ident_list) : smt_tactic unit :=
tactic.interactive.ctx_simp hs attr_names ids []
/-- Simplify the target type of the main goal using simplification lemmas and the current set of hypotheses. -/
meta def simp_using_hs (hs : opt_qexpr_list) (attr_names : with_ident_list) (ids : without_ident_list) : smt_tactic unit :=
tactic.interactive.simp_using_hs hs attr_names ids
meta def dsimp (es : opt_qexpr_list) (attr_names : with_ident_list) (ids : without_ident_list) : smt_tactic unit :=
tactic.interactive.dsimp es attr_names ids []
/-- Keep applying heuristic instantiation until the current goal is solved, or it fails. -/
meta def eblast : smt_tactic unit :=
smt_tactic.eblast
/-- Keep applying heuristic instantiation using the given lemmas until the current goal is solved, or it fails. -/
meta def eblast_using (l : qexpr_list_or_qexpr0) : smt_tactic unit :=
do hs ← add_hinst_lemmas_from_pexprs reducible ff l hinst_lemmas.mk,
smt_tactic.repeat (smt_tactic.ematch_using hs >> smt_tactic.try smt_tactic.close)
end interactive
end smt_tactic
|
ec539419d7d7fea96f44f461c4dea4b8dba39b7d | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /test/solve_by_elim.lean | 5f25a59b593ba07b2ed72e2fcd49e1166e8a1781 | [
"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 | 2,702 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Scott Morrison
-/
import tactic.solve_by_elim
example {a b : Prop} (h₀ : a → b) (h₁ : a) : b :=
begin
apply_assumption,
apply_assumption,
end
example {X : Type} (x : X) : x = x :=
by solve_by_elim
example : true :=
by solve_by_elim
example {a b : Prop} (h₀ : a → b) (h₁ : a) : b :=
by solve_by_elim
example {α : Type} {a b : α → Prop} (h₀ : ∀ x : α, b x = a x) (y : α) : a y = b y :=
by solve_by_elim
example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y :=
by solve_by_elim
example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y :=
begin
success_if_fail { solve_by_elim only [], },
success_if_fail { solve_by_elim only [h₀], },
solve_by_elim only [h₀, congr_fun]
end
example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y :=
by solve_by_elim [h₀]
example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y :=
begin
success_if_fail { solve_by_elim [*, -h₀] },
solve_by_elim [*]
end
example {α β : Type} (a b : α) (f : α → β) (i : function.injective f) (h : f a = f b) : a = b :=
begin
success_if_fail { solve_by_elim only [i] },
success_if_fail { solve_by_elim only [h] },
solve_by_elim only [i,h]
end
@[user_attribute]
meta def ex : user_attribute := {
name := `ex,
descr := "An example attribute for testing solve_by_elim."
}
@[ex] def f : ℕ := 0
example : ℕ := by solve_by_elim [f]
example : ℕ :=
begin
success_if_fail { solve_by_elim },
success_if_fail { solve_by_elim [-f] with ex },
solve_by_elim with ex,
end
example {α : Type} {p : α → Prop} (h₀ : ∀ x, p x) (y : α) : p y :=
begin
apply_assumption,
end
open tactic
example : true :=
begin
(do gs ← get_goals,
set_goals [],
success_if_fail `[solve_by_elim],
set_goals gs),
trivial
end
example {α : Type} (r : α → α → Prop) (f : α → α → α)
(l : ∀ a b c : α, r a b → r a (f b c) → r a c)
(a b c : α) (h₁ : r a b) (h₂ : r a (f b c)) : r a c :=
begin
solve_by_elim,
end
-- Verifying that `solve_by_elim*` acts on all remaining goals.
example (n : ℕ) : ℕ × ℕ :=
begin
split,
solve_by_elim*,
end
-- Verifying that `solve_by_elim*` backtracks when given multiple goals.
example (n m : ℕ) (f : ℕ → ℕ → Prop) (h : f n m) : ∃ p : ℕ × ℕ, f p.1 p.2 :=
begin
repeat { split },
solve_by_elim*,
end
example {a b c : ℕ} (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c :=
begin
apply le_trans,
solve_by_elim { backtrack_all_goals := true },
end
|
6cdad5ba10a29c29c72e0d37f444dea79345cdbb | b561a44b48979a98df50ade0789a21c79ee31288 | /stage0/src/Init/Prelude.lean | 737332a7f5e7eb3025c1256ca85d428cb2aa3091 | [
"Apache-2.0"
] | permissive | 3401ijk/lean4 | 97659c475ebd33a034fed515cb83a85f75ccfb06 | a5b1b8de4f4b038ff752b9e607b721f15a9a4351 | refs/heads/master | 1,693,933,007,651 | 1,636,424,845,000 | 1,636,424,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 78,715 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
universe u v w
@[inline] def id {α : Sort u} (a : α) : α := a
abbrev Function.comp {α : Sort u} {β : Sort v} {δ : Sort w} (f : β → δ) (g : α → β) : α → δ :=
fun x => f (g x)
abbrev Function.const {α : Sort u} (β : Sort v) (a : α) : β → α :=
fun x => a
set_option checkBinderAnnotations false in
@[reducible] def inferInstance {α : Sort u} [i : α] : α := i
set_option checkBinderAnnotations false in
@[reducible] def inferInstanceAs (α : Sort u) [i : α] : α := i
set_option bootstrap.inductiveCheckResultingUniverse false in
inductive PUnit : Sort u where
| unit : PUnit
/-- An abbreviation for `PUnit.{0}`, its most common instantiation.
This Type should be preferred over `PUnit` where possible to avoid
unnecessary universe parameters. -/
abbrev Unit : Type := PUnit
@[matchPattern] abbrev Unit.unit : Unit := PUnit.unit
/-- Auxiliary unsafe constant used by the Compiler when erasing proofs from code. -/
unsafe axiom lcProof {α : Prop} : α
/-- Auxiliary unsafe constant used by the Compiler to mark unreachable code. -/
unsafe axiom lcUnreachable {α : Sort u} : α
inductive True : Prop where
| intro : True
inductive False : Prop
inductive Empty : Type
set_option bootstrap.inductiveCheckResultingUniverse false in
inductive PEmpty : Sort u where
def Not (a : Prop) : Prop := a → False
@[macroInline] def False.elim {C : Sort u} (h : False) : C :=
False.rec (fun _ => C) h
@[macroInline] def absurd {a : Prop} {b : Sort v} (h₁ : a) (h₂ : Not a) : b :=
False.elim (h₂ h₁)
inductive Eq {α : Sort u} (a : α) : α → Prop where
| refl {} : Eq a a
@[simp] abbrev Eq.ndrec.{u1, u2} {α : Sort u2} {a : α} {motive : α → Sort u1} (m : motive a) {b : α} (h : Eq a b) : motive b :=
Eq.rec (motive := fun α _ => motive α) m h
@[matchPattern] def rfl {α : Sort u} {a : α} : Eq a a := Eq.refl a
@[simp] theorem id_eq (a : α) : Eq (id a) a := rfl
theorem Eq.subst {α : Sort u} {motive : α → Prop} {a b : α} (h₁ : Eq a b) (h₂ : motive a) : motive b :=
Eq.ndrec h₂ h₁
theorem Eq.symm {α : Sort u} {a b : α} (h : Eq a b) : Eq b a :=
h ▸ rfl
theorem Eq.trans {α : Sort u} {a b c : α} (h₁ : Eq a b) (h₂ : Eq b c) : Eq a c :=
h₂ ▸ h₁
@[macroInline] def cast {α β : Sort u} (h : Eq α β) (a : α) : β :=
Eq.rec (motive := fun α _ => α) a h
theorem congrArg {α : Sort u} {β : Sort v} {a₁ a₂ : α} (f : α → β) (h : Eq a₁ a₂) : Eq (f a₁) (f a₂) :=
h ▸ rfl
theorem congr {α : Sort u} {β : Sort v} {f₁ f₂ : α → β} {a₁ a₂ : α} (h₁ : Eq f₁ f₂) (h₂ : Eq a₁ a₂) : Eq (f₁ a₁) (f₂ a₂) :=
h₁ ▸ h₂ ▸ rfl
theorem congrFun {α : Sort u} {β : α → Sort v} {f g : (x : α) → β x} (h : Eq f g) (a : α) : Eq (f a) (g a) :=
h ▸ rfl
/-
Initialize the Quotient Module, which effectively adds the following definitions:
constant Quot {α : Sort u} (r : α → α → Prop) : Sort u
constant Quot.mk {α : Sort u} (r : α → α → Prop) (a : α) : Quot r
constant Quot.lift {α : Sort u} {r : α → α → Prop} {β : Sort v} (f : α → β) :
(∀ a b : α, r a b → Eq (f a) (f b)) → Quot r → β
constant Quot.ind {α : Sort u} {r : α → α → Prop} {β : Quot r → Prop} :
(∀ a : α, β (Quot.mk r a)) → ∀ q : Quot r, β q
-/
init_quot
inductive HEq {α : Sort u} (a : α) : {β : Sort u} → β → Prop where
| refl {} : HEq a a
@[matchPattern] protected def HEq.rfl {α : Sort u} {a : α} : HEq a a :=
HEq.refl a
theorem eq_of_heq {α : Sort u} {a a' : α} (h : HEq a a') : Eq a a' :=
have : (α β : Sort u) → (a : α) → (b : β) → HEq a b → (h : Eq α β) → Eq (cast h a) b :=
fun α β a b h₁ =>
HEq.rec (motive := fun {β} (b : β) (h : HEq a b) => (h₂ : Eq α β) → Eq (cast h₂ a) b)
(fun (h₂ : Eq α α) => rfl)
h₁
this α α a a' h rfl
structure Prod (α : Type u) (β : Type v) where
fst : α
snd : β
attribute [unbox] Prod
/-- Similar to `Prod`, but `α` and `β` can be propositions.
We use this Type internally to automatically generate the brecOn recursor. -/
structure PProd (α : Sort u) (β : Sort v) where
fst : α
snd : β
/-- Similar to `Prod`, but `α` and `β` are in the same universe. -/
structure MProd (α β : Type u) where
fst : α
snd : β
structure And (a b : Prop) : Prop where
intro :: (left : a) (right : b)
inductive Or (a b : Prop) : Prop where
| inl (h : a) : Or a b
| inr (h : b) : Or a b
theorem Or.intro_left (b : Prop) (h : a) : Or a b :=
Or.inl h
theorem Or.intro_right (a : Prop) (h : b) : Or a b :=
Or.inr h
theorem Or.elim {c : Prop} (h : Or a b) (left : a → c) (right : b → c) : c :=
match h with
| Or.inl h => left h
| Or.inr h => right h
inductive Bool : Type where
| false : Bool
| true : Bool
export Bool (false true)
/- Remark: Subtype must take a Sort instead of Type because of the axiom strongIndefiniteDescription. -/
structure Subtype {α : Sort u} (p : α → Prop) where
val : α
property : p val
/-- Gadget for optional parameter support. -/
@[reducible] def optParam (α : Sort u) (default : α) : Sort u := α
/-- Gadget for marking output parameters in type classes. -/
@[reducible] def outParam (α : Sort u) : Sort u := α
/-- Auxiliary Declaration used to implement the notation (a : α) -/
@[reducible] def typedExpr (α : Sort u) (a : α) : α := a
/-- Auxiliary Declaration used to implement the named patterns `x@p` -/
@[reducible] def namedPattern {α : Sort u} (x a : α) : α := a
/- Auxiliary axiom used to implement `sorry`. -/
@[extern "lean_sorry", neverExtract]
axiom sorryAx (α : Sort u) (synthetic := true) : α
theorem eq_false_of_ne_true : {b : Bool} → Not (Eq b true) → Eq b false
| true, h => False.elim (h rfl)
| false, h => rfl
theorem eq_true_of_ne_false : {b : Bool} → Not (Eq b false) → Eq b true
| true, h => rfl
| false, h => False.elim (h rfl)
theorem ne_false_of_eq_true : {b : Bool} → Eq b true → Not (Eq b false)
| true, _ => fun h => Bool.noConfusion h
| false, h => Bool.noConfusion h
theorem ne_true_of_eq_false : {b : Bool} → Eq b false → Not (Eq b true)
| true, h => Bool.noConfusion h
| false, _ => fun h => Bool.noConfusion h
class Inhabited (α : Sort u) where
mk {} :: (default : α)
constant arbitrary [Inhabited α] : α :=
Inhabited.default
instance : Inhabited (Sort u) where
default := PUnit
instance (α : Sort u) {β : Sort v} [Inhabited β] : Inhabited (α → β) where
default := fun _ => arbitrary
instance (α : Sort u) {β : α → Sort v} [(a : α) → Inhabited (β a)] : Inhabited ((a : α) → β a) where
default := fun _ => arbitrary
deriving instance Inhabited for Bool
/-- Universe lifting operation from Sort to Type -/
structure PLift (α : Sort u) : Type u where
up :: (down : α)
/- Bijection between α and PLift α -/
theorem PLift.up_down {α : Sort u} : ∀ (b : PLift α), Eq (up (down b)) b
| up a => rfl
theorem PLift.down_up {α : Sort u} (a : α) : Eq (down (up a)) a :=
rfl
/- Pointed types -/
structure PointedType where
(type : Type u)
(val : type)
instance : Inhabited PointedType.{u} where
default := { type := PUnit.{u+1}, val := ⟨⟩ }
/-- Universe lifting operation -/
structure ULift.{r, s} (α : Type s) : Type (max s r) where
up :: (down : α)
/- Bijection between α and ULift.{v} α -/
theorem ULift.up_down {α : Type u} : ∀ (b : ULift.{v} α), Eq (up (down b)) b
| up a => rfl
theorem ULift.down_up {α : Type u} (a : α) : Eq (down (up.{v} a)) a :=
rfl
class inductive Decidable (p : Prop) where
| isFalse (h : Not p) : Decidable p
| isTrue (h : p) : Decidable p
@[inlineIfReduce, nospecialize] def Decidable.decide (p : Prop) [h : Decidable p] : Bool :=
Decidable.casesOn (motive := fun _ => Bool) h (fun _ => false) (fun _ => true)
export Decidable (isTrue isFalse decide)
abbrev DecidablePred {α : Sort u} (r : α → Prop) :=
(a : α) → Decidable (r a)
abbrev DecidableRel {α : Sort u} (r : α → α → Prop) :=
(a b : α) → Decidable (r a b)
abbrev DecidableEq (α : Sort u) :=
(a b : α) → Decidable (Eq a b)
def decEq {α : Sort u} [s : DecidableEq α] (a b : α) : Decidable (Eq a b) :=
s a b
theorem decide_eq_true : [s : Decidable p] → p → Eq (decide p) true
| isTrue _, _ => rfl
| isFalse h₁, h₂ => absurd h₂ h₁
theorem decide_eq_false : [s : Decidable p] → Not p → Eq (decide p) false
| isTrue h₁, h₂ => absurd h₁ h₂
| isFalse h, _ => rfl
theorem of_decide_eq_true [s : Decidable p] : Eq (decide p) true → p := fun h =>
match (generalizing := false) s with
| isTrue h₁ => h₁
| isFalse h₁ => absurd h (ne_true_of_eq_false (decide_eq_false h₁))
theorem of_decide_eq_false [s : Decidable p] : Eq (decide p) false → Not p := fun h =>
match (generalizing := false) s with
| isTrue h₁ => absurd h (ne_false_of_eq_true (decide_eq_true h₁))
| isFalse h₁ => h₁
@[inline] instance : DecidableEq Bool :=
fun a b => match a, b with
| false, false => isTrue rfl
| false, true => isFalse (fun h => Bool.noConfusion h)
| true, false => isFalse (fun h => Bool.noConfusion h)
| true, true => isTrue rfl
class BEq (α : Type u) where
beq : α → α → Bool
open BEq (beq)
instance [DecidableEq α] : BEq α where
beq a b := decide (Eq a b)
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
@[macroInline] def dite {α : Sort u} (c : Prop) [h : Decidable c] (t : c → α) (e : Not c → α) : α :=
Decidable.casesOn (motive := fun _ => α) h e t
/- if-then-else -/
@[macroInline] def ite {α : Sort u} (c : Prop) [h : Decidable c] (t e : α) : α :=
Decidable.casesOn (motive := fun _ => α) h (fun _ => e) (fun _ => t)
@[macroInline] instance {p q} [dp : Decidable p] [dq : Decidable q] : Decidable (And p q) :=
match dp with
| isTrue hp =>
match dq with
| isTrue hq => isTrue ⟨hp, hq⟩
| isFalse hq => isFalse (fun h => hq (And.right h))
| isFalse hp =>
isFalse (fun h => hp (And.left h))
@[macroInline] instance [dp : Decidable p] [dq : Decidable q] : Decidable (Or p q) :=
match dp with
| isTrue hp => isTrue (Or.inl hp)
| isFalse hp =>
match dq with
| isTrue hq => isTrue (Or.inr hq)
| isFalse hq =>
isFalse fun h => match h with
| Or.inl h => hp h
| Or.inr h => hq h
instance [dp : Decidable p] : Decidable (Not p) :=
match dp with
| isTrue hp => isFalse (absurd hp)
| isFalse hp => isTrue hp
/- Boolean operators -/
@[macroInline] def cond {α : Type u} (c : Bool) (x y : α) : α :=
match c with
| true => x
| false => y
@[macroInline] def or (x y : Bool) : Bool :=
match x with
| true => true
| false => y
@[macroInline] def and (x y : Bool) : Bool :=
match x with
| false => false
| true => y
@[inline] def not : Bool → Bool
| true => false
| false => true
inductive Nat where
| zero : Nat
| succ (n : Nat) : Nat
instance : Inhabited Nat where
default := Nat.zero
/- For numeric literals notation -/
class OfNat (α : Type u) (n : Nat) where
ofNat : α
@[defaultInstance 100] /- low prio -/
instance (n : Nat) : OfNat Nat n where
ofNat := n
class LE (α : Type u) where le : α → α → Prop
class LT (α : Type u) where lt : α → α → Prop
@[reducible] def GE.ge {α : Type u} [LE α] (a b : α) : Prop := LE.le b a
@[reducible] def GT.gt {α : Type u} [LT α] (a b : α) : Prop := LT.lt b a
@[inline] def max [LT α] [DecidableRel (@LT.lt α _)] (a b : α) : α :=
ite (LT.lt b a) a b
@[inline] def min [LE α] [DecidableRel (@LE.le α _)] (a b : α) : α :=
ite (LE.le a b) a b
/-- Transitive chaining of proofs, used e.g. by `calc`. -/
class Trans (r : α → β → Prop) (s : β → γ → Prop) (t : outParam (α → γ → Prop)) where
trans : r a b → s b c → t a c
export Trans (trans)
instance (r : α → γ → Prop) : Trans Eq r r where
trans heq h' := heq ▸ h'
instance (r : α → β → Prop) : Trans r Eq r where
trans h' heq := heq ▸ h'
class HAdd (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hAdd : α → β → γ
class HSub (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hSub : α → β → γ
class HMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hMul : α → β → γ
class HDiv (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hDiv : α → β → γ
class HMod (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hMod : α → β → γ
class HPow (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hPow : α → β → γ
class HAppend (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hAppend : α → β → γ
class HOrElse (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hOrElse : α → (Unit → β) → γ
class HAndThen (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hAndThen : α → (Unit → β) → γ
class HAnd (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hAnd : α → β → γ
class HXor (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hXor : α → β → γ
class HOr (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hOr : α → β → γ
class HShiftLeft (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hShiftLeft : α → β → γ
class HShiftRight (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hShiftRight : α → β → γ
class Add (α : Type u) where
add : α → α → α
class Sub (α : Type u) where
sub : α → α → α
class Mul (α : Type u) where
mul : α → α → α
class Neg (α : Type u) where
neg : α → α
class Div (α : Type u) where
div : α → α → α
class Mod (α : Type u) where
mod : α → α → α
class Pow (α : Type u) (β : Type v) where
pow : α → β → α
class Append (α : Type u) where
append : α → α → α
class OrElse (α : Type u) where
orElse : α → (Unit → α) → α
class AndThen (α : Type u) where
andThen : α → (Unit → α) → α
class AndOp (α : Type u) where
and : α → α → α
class Xor (α : Type u) where
xor : α → α → α
class OrOp (α : Type u) where
or : α → α → α
class Complement (α : Type u) where
complement : α → α
class ShiftLeft (α : Type u) where
shiftLeft : α → α → α
class ShiftRight (α : Type u) where
shiftRight : α → α → α
@[defaultInstance]
instance [Add α] : HAdd α α α where
hAdd a b := Add.add a b
@[defaultInstance]
instance [Sub α] : HSub α α α where
hSub a b := Sub.sub a b
@[defaultInstance]
instance [Mul α] : HMul α α α where
hMul a b := Mul.mul a b
@[defaultInstance]
instance [Div α] : HDiv α α α where
hDiv a b := Div.div a b
@[defaultInstance]
instance [Mod α] : HMod α α α where
hMod a b := Mod.mod a b
@[defaultInstance]
instance [Pow α β] : HPow α β α where
hPow a b := Pow.pow a b
@[defaultInstance]
instance [Append α] : HAppend α α α where
hAppend a b := Append.append a b
@[defaultInstance]
instance [OrElse α] : HOrElse α α α where
hOrElse a b := OrElse.orElse a b
@[defaultInstance]
instance [AndThen α] : HAndThen α α α where
hAndThen a b := AndThen.andThen a b
@[defaultInstance]
instance [AndOp α] : HAnd α α α where
hAnd a b := AndOp.and a b
@[defaultInstance]
instance [Xor α] : HXor α α α where
hXor a b := Xor.xor a b
@[defaultInstance]
instance [OrOp α] : HOr α α α where
hOr a b := OrOp.or a b
@[defaultInstance]
instance [ShiftLeft α] : HShiftLeft α α α where
hShiftLeft a b := ShiftLeft.shiftLeft a b
@[defaultInstance]
instance [ShiftRight α] : HShiftRight α α α where
hShiftRight a b := ShiftRight.shiftRight a b
open HAdd (hAdd)
open HMul (hMul)
open HPow (hPow)
open HAppend (hAppend)
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_add"]
protected def Nat.add : (@& Nat) → (@& Nat) → Nat
| a, Nat.zero => a
| a, Nat.succ b => Nat.succ (Nat.add a b)
instance : Add Nat where
add := Nat.add
/- We mark the following definitions as pattern to make sure they can be used in recursive equations,
and reduced by the equation Compiler. -/
attribute [matchPattern] Nat.add Add.add HAdd.hAdd Neg.neg
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_mul"]
protected def Nat.mul : (@& Nat) → (@& Nat) → Nat
| a, 0 => 0
| a, Nat.succ b => Nat.add (Nat.mul a b) a
instance : Mul Nat where
mul := Nat.mul
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_pow"]
protected def Nat.pow (m : @& Nat) : (@& Nat) → Nat
| 0 => 1
| succ n => Nat.mul (Nat.pow m n) m
instance : Pow Nat Nat where
pow := Nat.pow
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_dec_eq"]
def Nat.beq : (@& Nat) → (@& Nat) → Bool
| zero, zero => true
| zero, succ m => false
| succ n, zero => false
| succ n, succ m => beq n m
theorem Nat.eq_of_beq_eq_true : {n m : Nat} → Eq (beq n m) true → Eq n m
| zero, zero, h => rfl
| zero, succ m, h => Bool.noConfusion h
| succ n, zero, h => Bool.noConfusion h
| succ n, succ m, h =>
have : Eq (beq n m) true := h
have : Eq n m := eq_of_beq_eq_true this
this ▸ rfl
theorem Nat.ne_of_beq_eq_false : {n m : Nat} → Eq (beq n m) false → Not (Eq n m)
| zero, zero, h₁, h₂ => Bool.noConfusion h₁
| zero, succ m, h₁, h₂ => Nat.noConfusion h₂
| succ n, zero, h₁, h₂ => Nat.noConfusion h₂
| succ n, succ m, h₁, h₂ =>
have : Eq (beq n m) false := h₁
Nat.noConfusion h₂ (fun h₂ => absurd h₂ (ne_of_beq_eq_false this))
@[extern "lean_nat_dec_eq"]
protected def Nat.decEq (n m : @& Nat) : Decidable (Eq n m) :=
match h:beq n m with
| true => isTrue (eq_of_beq_eq_true h)
| false => isFalse (ne_of_beq_eq_false h)
@[inline] instance : DecidableEq Nat := Nat.decEq
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_dec_le"]
def Nat.ble : @& Nat → @& Nat → Bool
| zero, zero => true
| zero, succ m => true
| succ n, zero => false
| succ n, succ m => ble n m
protected inductive Nat.le (n : Nat) : Nat → Prop
| refl : Nat.le n n
| step {m} : Nat.le n m → Nat.le n (succ m)
instance : LE Nat where
le := Nat.le
protected def Nat.lt (n m : Nat) : Prop :=
Nat.le (succ n) m
instance : LT Nat where
lt := Nat.lt
theorem Nat.not_succ_le_zero : ∀ (n : Nat), LE.le (succ n) 0 → False
| 0, h => nomatch h
| succ n, h => nomatch h
theorem Nat.not_lt_zero (n : Nat) : Not (LT.lt n 0) :=
not_succ_le_zero n
theorem Nat.zero_le : (n : Nat) → LE.le 0 n
| zero => Nat.le.refl
| succ n => Nat.le.step (zero_le n)
theorem Nat.succ_le_succ : LE.le n m → LE.le (succ n) (succ m)
| Nat.le.refl => Nat.le.refl
| Nat.le.step h => Nat.le.step (succ_le_succ h)
theorem Nat.zero_lt_succ (n : Nat) : LT.lt 0 (succ n) :=
succ_le_succ (zero_le n)
theorem Nat.le_step (h : LE.le n m) : LE.le n (succ m) :=
Nat.le.step h
protected theorem Nat.le_trans {n m k : Nat} : LE.le n m → LE.le m k → LE.le n k
| h, Nat.le.refl => h
| h₁, Nat.le.step h₂ => Nat.le.step (Nat.le_trans h₁ h₂)
protected theorem Nat.lt_trans {n m k : Nat} (h₁ : LT.lt n m) : LT.lt m k → LT.lt n k :=
Nat.le_trans (le_step h₁)
theorem Nat.le_succ (n : Nat) : LE.le n (succ n) :=
Nat.le.step Nat.le.refl
theorem Nat.le_succ_of_le {n m : Nat} (h : LE.le n m) : LE.le n (succ m) :=
Nat.le_trans h (le_succ m)
protected theorem Nat.le_refl (n : Nat) : LE.le n n :=
Nat.le.refl
theorem Nat.succ_pos (n : Nat) : LT.lt 0 (succ n) :=
zero_lt_succ n
set_option bootstrap.genMatcherCode false in
@[extern c inline "lean_nat_sub(#1, lean_box(1))"]
def Nat.pred : (@& Nat) → Nat
| 0 => 0
| succ a => a
theorem Nat.pred_le_pred : {n m : Nat} → LE.le n m → LE.le (pred n) (pred m)
| _, _, Nat.le.refl => Nat.le.refl
| 0, succ m, Nat.le.step h => h
| succ n, succ m, Nat.le.step h => Nat.le_trans (le_succ _) h
theorem Nat.le_of_succ_le_succ {n m : Nat} : LE.le (succ n) (succ m) → LE.le n m :=
pred_le_pred
theorem Nat.le_of_lt_succ {m n : Nat} : LT.lt m (succ n) → LE.le m n :=
le_of_succ_le_succ
protected theorem Nat.eq_or_lt_of_le : {n m: Nat} → LE.le n m → Or (Eq n m) (LT.lt n m)
| zero, zero, h => Or.inl rfl
| zero, succ n, h => Or.inr (Nat.succ_le_succ (Nat.zero_le _))
| succ n, zero, h => absurd h (not_succ_le_zero _)
| succ n, succ m, h =>
have : LE.le n m := Nat.le_of_succ_le_succ h
match Nat.eq_or_lt_of_le this with
| Or.inl h => Or.inl (h ▸ rfl)
| Or.inr h => Or.inr (succ_le_succ h)
protected theorem Nat.lt_or_ge (n m : Nat) : Or (LT.lt n m) (GE.ge n m) :=
match m with
| zero => Or.inr (zero_le n)
| succ m =>
match Nat.lt_or_ge n m with
| Or.inl h => Or.inl (le_succ_of_le h)
| Or.inr h =>
match Nat.eq_or_lt_of_le h with
| Or.inl h1 => Or.inl (h1 ▸ Nat.le_refl _)
| Or.inr h1 => Or.inr h1
theorem Nat.not_succ_le_self : (n : Nat) → Not (LE.le (succ n) n)
| 0 => not_succ_le_zero _
| succ n => fun h => absurd (le_of_succ_le_succ h) (not_succ_le_self n)
protected theorem Nat.lt_irrefl (n : Nat) : Not (LT.lt n n) :=
Nat.not_succ_le_self n
protected theorem Nat.lt_of_le_of_lt {n m k : Nat} (h₁ : LE.le n m) (h₂ : LT.lt m k) : LT.lt n k :=
Nat.le_trans (Nat.succ_le_succ h₁) h₂
protected theorem Nat.le_antisymm {n m : Nat} (h₁ : LE.le n m) (h₂ : LE.le m n) : Eq n m :=
match h₁ with
| Nat.le.refl => rfl
| Nat.le.step h => absurd (Nat.lt_of_le_of_lt h h₂) (Nat.lt_irrefl n)
protected theorem Nat.lt_of_le_of_ne {n m : Nat} (h₁ : LE.le n m) (h₂ : Not (Eq n m)) : LT.lt n m :=
match Nat.lt_or_ge n m with
| Or.inl h₃ => h₃
| Or.inr h₃ => absurd (Nat.le_antisymm h₁ h₃) h₂
theorem Nat.le_of_ble_eq_true (h : Eq (Nat.ble n m) true) : LE.le n m :=
match n, m with
| 0, _ => Nat.zero_le _
| succ _, succ _ => Nat.succ_le_succ (le_of_ble_eq_true h)
theorem Nat.ble_self_eq_true : (n : Nat) → Eq (Nat.ble n n) true
| 0 => rfl
| succ n => ble_self_eq_true n
theorem Nat.ble_succ_eq_true : {n m : Nat} → Eq (Nat.ble n m) true → Eq (Nat.ble n (succ m)) true
| 0, _, _ => rfl
| succ n, succ m, h => ble_succ_eq_true (n := n) h
theorem Nat.ble_eq_true_of_le (h : LE.le n m) : Eq (Nat.ble n m) true :=
match h with
| Nat.le.refl => Nat.ble_self_eq_true n
| Nat.le.step h => Nat.ble_succ_eq_true (ble_eq_true_of_le h)
theorem Nat.not_le_of_not_ble_eq_true (h : Not (Eq (Nat.ble n m) true)) : Not (LE.le n m) :=
fun h' => absurd (Nat.ble_eq_true_of_le h') h
@[extern "lean_nat_dec_le"]
instance Nat.decLe (n m : @& Nat) : Decidable (LE.le n m) :=
dite (Eq (Nat.ble n m) true) (fun h => isTrue (Nat.le_of_ble_eq_true h)) (fun h => isFalse (Nat.not_le_of_not_ble_eq_true h))
@[extern "lean_nat_dec_lt"]
instance Nat.decLt (n m : @& Nat) : Decidable (LT.lt n m) :=
decLe (succ n) m
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_sub"]
protected def Nat.sub : (@& Nat) → (@& Nat) → Nat
| a, 0 => a
| a, succ b => pred (Nat.sub a b)
instance : Sub Nat where
sub := Nat.sub
@[extern "lean_system_platform_nbits"] constant System.Platform.getNumBits : Unit → Subtype fun (n : Nat) => Or (Eq n 32) (Eq n 64) :=
fun _ => ⟨64, Or.inr rfl⟩ -- inhabitant
def System.Platform.numBits : Nat :=
(getNumBits ()).val
theorem System.Platform.numBits_eq : Or (Eq numBits 32) (Eq numBits 64) :=
(getNumBits ()).property
structure Fin (n : Nat) where
val : Nat
isLt : LT.lt val n
theorem Fin.eq_of_val_eq {n} : ∀ {i j : Fin n}, Eq i.val j.val → Eq i j
| ⟨v, h⟩, ⟨_, _⟩, rfl => rfl
theorem Fin.val_eq_of_eq {n} {i j : Fin n} (h : Eq i j) : Eq i.val j.val :=
h ▸ rfl
theorem Fin.ne_of_val_ne {n} {i j : Fin n} (h : Not (Eq i.val j.val)) : Not (Eq i j) :=
fun h' => absurd (val_eq_of_eq h') h
instance (n : Nat) : DecidableEq (Fin n) :=
fun i j =>
match decEq i.val j.val with
| isTrue h => isTrue (Fin.eq_of_val_eq h)
| isFalse h => isFalse (Fin.ne_of_val_ne h)
instance {n} : LT (Fin n) where
lt a b := LT.lt a.val b.val
instance {n} : LE (Fin n) where
le a b := LE.le a.val b.val
instance Fin.decLt {n} (a b : Fin n) : Decidable (LT.lt a b) := Nat.decLt ..
instance Fin.decLe {n} (a b : Fin n) : Decidable (LE.le a b) := Nat.decLe ..
def UInt8.size : Nat := 256
structure UInt8 where
val : Fin UInt8.size
attribute [extern "lean_uint8_of_nat_mk"] UInt8.mk
attribute [extern "lean_uint8_to_nat"] UInt8.val
@[extern "lean_uint8_of_nat"]
def UInt8.ofNatCore (n : @& Nat) (h : LT.lt n UInt8.size) : UInt8 := {
val := { val := n, isLt := h }
}
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint8_dec_eq"]
def UInt8.decEq (a b : UInt8) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h => isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => UInt8.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq UInt8 := UInt8.decEq
instance : Inhabited UInt8 where
default := UInt8.ofNatCore 0 (by decide)
def UInt16.size : Nat := 65536
structure UInt16 where
val : Fin UInt16.size
attribute [extern "lean_uint16_of_nat_mk"] UInt16.mk
attribute [extern "lean_uint16_to_nat"] UInt16.val
@[extern "lean_uint16_of_nat"]
def UInt16.ofNatCore (n : @& Nat) (h : LT.lt n UInt16.size) : UInt16 := {
val := { val := n, isLt := h }
}
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint16_dec_eq"]
def UInt16.decEq (a b : UInt16) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h => isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => UInt16.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq UInt16 := UInt16.decEq
instance : Inhabited UInt16 where
default := UInt16.ofNatCore 0 (by decide)
def UInt32.size : Nat := 4294967296
structure UInt32 where
val : Fin UInt32.size
attribute [extern "lean_uint32_of_nat_mk"] UInt32.mk
attribute [extern "lean_uint32_to_nat"] UInt32.val
@[extern "lean_uint32_of_nat"]
def UInt32.ofNatCore (n : @& Nat) (h : LT.lt n UInt32.size) : UInt32 := {
val := { val := n, isLt := h }
}
@[extern "lean_uint32_to_nat"]
def UInt32.toNat (n : UInt32) : Nat := n.val.val
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint32_dec_eq"]
def UInt32.decEq (a b : UInt32) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h => isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => UInt32.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq UInt32 := UInt32.decEq
instance : Inhabited UInt32 where
default := UInt32.ofNatCore 0 (by decide)
instance : LT UInt32 where
lt a b := LT.lt a.val b.val
instance : LE UInt32 where
le a b := LE.le a.val b.val
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint32_dec_lt"]
def UInt32.decLt (a b : UInt32) : Decidable (LT.lt a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (LT.lt n m))
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint32_dec_le"]
def UInt32.decLe (a b : UInt32) : Decidable (LE.le a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (LE.le n m))
instance (a b : UInt32) : Decidable (LT.lt a b) := UInt32.decLt a b
instance (a b : UInt32) : Decidable (LE.le a b) := UInt32.decLe a b
def UInt64.size : Nat := 18446744073709551616
structure UInt64 where
val : Fin UInt64.size
attribute [extern "lean_uint64_of_nat_mk"] UInt64.mk
attribute [extern "lean_uint64_to_nat"] UInt64.val
@[extern "lean_uint64_of_nat"]
def UInt64.ofNatCore (n : @& Nat) (h : LT.lt n UInt64.size) : UInt64 := {
val := { val := n, isLt := h }
}
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint64_dec_eq"]
def UInt64.decEq (a b : UInt64) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h => isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => UInt64.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq UInt64 := UInt64.decEq
instance : Inhabited UInt64 where
default := UInt64.ofNatCore 0 (by decide)
def USize.size : Nat := hPow 2 System.Platform.numBits
theorem usize_size_eq : Or (Eq USize.size 4294967296) (Eq USize.size 18446744073709551616) :=
show Or (Eq (hPow 2 System.Platform.numBits) 4294967296) (Eq (hPow 2 System.Platform.numBits) 18446744073709551616) from
match System.Platform.numBits, System.Platform.numBits_eq with
| _, Or.inl rfl => Or.inl (by decide)
| _, Or.inr rfl => Or.inr (by decide)
structure USize where
val : Fin USize.size
attribute [extern "lean_usize_of_nat_mk"] USize.mk
attribute [extern "lean_usize_to_nat"] USize.val
@[extern "lean_usize_of_nat"]
def USize.ofNatCore (n : @& Nat) (h : LT.lt n USize.size) : USize := {
val := { val := n, isLt := h }
}
set_option bootstrap.genMatcherCode false in
@[extern "lean_usize_dec_eq"]
def USize.decEq (a b : USize) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h =>isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => USize.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq USize := USize.decEq
instance : Inhabited USize where
default := USize.ofNatCore 0 (match USize.size, usize_size_eq with
| _, Or.inl rfl => by decide
| _, Or.inr rfl => by decide)
@[extern "lean_usize_of_nat"]
def USize.ofNat32 (n : @& Nat) (h : LT.lt n 4294967296) : USize := {
val := {
val := n
isLt := match USize.size, usize_size_eq with
| _, Or.inl rfl => h
| _, Or.inr rfl => Nat.lt_trans h (by decide)
}
}
abbrev Nat.isValidChar (n : Nat) : Prop :=
Or (LT.lt n 0xd800) (And (LT.lt 0xdfff n) (LT.lt n 0x110000))
abbrev UInt32.isValidChar (n : UInt32) : Prop :=
n.toNat.isValidChar
/-- The `Char` Type represents an unicode scalar value.
See http://www.unicode.org/glossary/#unicode_scalar_value). -/
structure Char where
val : UInt32
valid : val.isValidChar
private theorem isValidChar_UInt32 {n : Nat} (h : n.isValidChar) : LT.lt n UInt32.size :=
match h with
| Or.inl h => Nat.lt_trans h (by decide)
| Or.inr ⟨_, h⟩ => Nat.lt_trans h (by decide)
@[extern "lean_uint32_of_nat"]
private def Char.ofNatAux (n : @& Nat) (h : n.isValidChar) : Char :=
{ val := ⟨{ val := n, isLt := isValidChar_UInt32 h }⟩, valid := h }
@[noinline, matchPattern]
def Char.ofNat (n : Nat) : Char :=
dite (n.isValidChar)
(fun h => Char.ofNatAux n h)
(fun _ => { val := ⟨{ val := 0, isLt := by decide }⟩, valid := Or.inl (by decide) })
theorem Char.eq_of_val_eq : ∀ {c d : Char}, Eq c.val d.val → Eq c d
| ⟨v, h⟩, ⟨_, _⟩, rfl => rfl
theorem Char.val_eq_of_eq : ∀ {c d : Char}, Eq c d → Eq c.val d.val
| _, _, rfl => rfl
theorem Char.ne_of_val_ne {c d : Char} (h : Not (Eq c.val d.val)) : Not (Eq c d) :=
fun h' => absurd (val_eq_of_eq h') h
theorem Char.val_ne_of_ne {c d : Char} (h : Not (Eq c d)) : Not (Eq c.val d.val) :=
fun h' => absurd (eq_of_val_eq h') h
instance : DecidableEq Char :=
fun c d =>
match decEq c.val d.val with
| isTrue h => isTrue (Char.eq_of_val_eq h)
| isFalse h => isFalse (Char.ne_of_val_ne h)
def Char.utf8Size (c : Char) : UInt32 :=
let v := c.val
ite (LE.le v (UInt32.ofNatCore 0x7F (by decide)))
(UInt32.ofNatCore 1 (by decide))
(ite (LE.le v (UInt32.ofNatCore 0x7FF (by decide)))
(UInt32.ofNatCore 2 (by decide))
(ite (LE.le v (UInt32.ofNatCore 0xFFFF (by decide)))
(UInt32.ofNatCore 3 (by decide))
(UInt32.ofNatCore 4 (by decide))))
inductive Option (α : Type u) where
| none : Option α
| some (val : α) : Option α
attribute [unbox] Option
export Option (none some)
instance {α} : Inhabited (Option α) where
default := none
@[macroInline] def Option.getD : Option α → α → α
| some x, _ => x
| none, e => e
inductive List (α : Type u) where
| nil : List α
| cons (head : α) (tail : List α) : List α
instance {α} : Inhabited (List α) where
default := List.nil
protected def List.hasDecEq {α: Type u} [DecidableEq α] : (a b : List α) → Decidable (Eq a b)
| nil, nil => isTrue rfl
| cons a as, nil => isFalse (fun h => List.noConfusion h)
| nil, cons b bs => isFalse (fun h => List.noConfusion h)
| cons a as, cons b bs =>
match decEq a b with
| isTrue hab =>
match List.hasDecEq as bs with
| isTrue habs => isTrue (hab ▸ habs ▸ rfl)
| isFalse nabs => isFalse (fun h => List.noConfusion h (fun _ habs => absurd habs nabs))
| isFalse nab => isFalse (fun h => List.noConfusion h (fun hab _ => absurd hab nab))
instance {α : Type u} [DecidableEq α] : DecidableEq (List α) := List.hasDecEq
@[specialize]
def List.foldl {α β} (f : α → β → α) : (init : α) → List β → α
| a, nil => a
| a, cons b l => foldl f (f a b) l
def List.set : List α → Nat → α → List α
| cons a as, 0, b => cons b as
| cons a as, Nat.succ n, b => cons a (set as n b)
| nil, _, _ => nil
def List.length : List α → Nat
| nil => 0
| cons a as => HAdd.hAdd (length as) 1
def List.lengthTRAux : List α → Nat → Nat
| nil, n => n
| cons a as, n => lengthTRAux as (Nat.succ n)
def List.lengthTR (as : List α) : Nat :=
lengthTRAux as 0
@[simp] theorem List.length_cons {α} (a : α) (as : List α) : Eq (cons a as).length as.length.succ :=
rfl
def List.concat {α : Type u} : List α → α → List α
| nil, b => cons b nil
| cons a as, b => cons a (concat as b)
def List.get {α : Type u} : (as : List α) → (i : Nat) → LT.lt i as.length → α
| nil, i, h => absurd h (Nat.not_lt_zero _)
| cons a as, 0, h => a
| cons a as, Nat.succ i, h =>
have : LT.lt i.succ as.length.succ := length_cons .. ▸ h
get as i (Nat.le_of_succ_le_succ this)
structure String where
data : List Char
attribute [extern "lean_string_mk"] String.mk
attribute [extern "lean_string_data"] String.data
@[extern "lean_string_dec_eq"]
def String.decEq (s₁ s₂ : @& String) : Decidable (Eq s₁ s₂) :=
match s₁, s₂ with
| ⟨s₁⟩, ⟨s₂⟩ =>
dite (Eq s₁ s₂) (fun h => isTrue (congrArg _ h)) (fun h => isFalse (fun h' => String.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq String := String.decEq
/-- A byte position in a `String`. Internally, `String`s are UTF-8 encoded.
Codepoint positions (counting the Unicode codepoints rather than bytes)
are represented by plain `Nat`s instead.
Indexing a `String` by a byte position is constant-time, while codepoint
positions need to be translated internally to byte positions in linear-time. -/
abbrev String.Pos := Nat
structure Substring where
str : String
startPos : String.Pos
stopPos : String.Pos
@[inline] def Substring.bsize : Substring → Nat
| ⟨_, b, e⟩ => e.sub b
def String.csize (c : Char) : Nat :=
c.utf8Size.toNat
private def String.utf8ByteSizeAux : List Char → Nat → Nat
| List.nil, r => r
| List.cons c cs, r => utf8ByteSizeAux cs (hAdd r (csize c))
@[extern "lean_string_utf8_byte_size"]
def String.utf8ByteSize : (@& String) → Nat
| ⟨s⟩ => utf8ByteSizeAux s 0
@[inline] def String.bsize (s : String) : Nat :=
utf8ByteSize s
@[inline] def String.toSubstring (s : String) : Substring := {
str := s
startPos := 0
stopPos := s.bsize
}
@[extern c inline "#3"]
unsafe def unsafeCast {α : Type u} {β : Type v} (a : α) : β :=
cast lcProof (PUnit.{v})
@[neverExtract, extern "lean_panic_fn"]
constant panicCore {α : Type u} [Inhabited α] (msg : String) : α
/--
This is workaround for `panic` occurring in monadic code. See issue #695.
The `panicCore` definition cannot be specialized since it is an extern.
When `panic` occurs in monadic code, the `Inhabited α` parameter depends on a `[inst : Monad m]` instance.
The `inst` parameter will not be eliminated during specialization if it occurs inside of a binder (to avoid work duplication), and
will prevent the the actual monad from being "copied" to the code being specialized. When we reimplement the specializer, we
may consider copying `inst` if it also occurs outside binders or if it is an instance.
-/
@[noinline, neverExtract]
def panic {α : Type u} [Inhabited α] (msg : String) : α :=
panicCore msg
/-
The Compiler has special support for arrays.
They are implemented using dynamic arrays: https://en.wikipedia.org/wiki/Dynamic_array
-/
structure Array (α : Type u) where
data : List α
attribute [extern "lean_array_data"] Array.data
attribute [extern "lean_array_mk"] Array.mk
/- The parameter `c` is the initial capacity -/
@[extern "lean_mk_empty_array_with_capacity"]
def Array.mkEmpty {α : Type u} (c : @& Nat) : Array α := {
data := List.nil
}
def Array.empty {α : Type u} : Array α :=
mkEmpty 0
@[reducible, extern "lean_array_get_size"]
def Array.size {α : Type u} (a : @& Array α) : Nat :=
a.data.length
@[extern "lean_array_fget"]
def Array.get {α : Type u} (a : @& Array α) (i : @& Fin a.size) : α :=
a.data.get i.val i.isLt
@[inline] def Array.getD (a : Array α) (i : Nat) (v₀ : α) : α :=
dite (LT.lt i a.size) (fun h => a.get ⟨i, h⟩) (fun _ => v₀)
/- "Comfortable" version of `fget`. It performs a bound check at runtime. -/
@[extern "lean_array_get"]
def Array.get! {α : Type u} [Inhabited α] (a : @& Array α) (i : @& Nat) : α :=
Array.getD a i arbitrary
def Array.getOp {α : Type u} [Inhabited α] (self : Array α) (idx : Nat) : α :=
self.get! idx
@[extern "lean_array_push"]
def Array.push {α : Type u} (a : Array α) (v : α) : Array α := {
data := List.concat a.data v
}
@[extern "lean_array_fset"]
def Array.set (a : Array α) (i : @& Fin a.size) (v : α) : Array α := {
data := a.data.set i.val v
}
@[inline] def Array.setD (a : Array α) (i : Nat) (v : α) : Array α :=
dite (LT.lt i a.size) (fun h => a.set ⟨i, h⟩ v) (fun _ => a)
@[extern "lean_array_set"]
def Array.set! (a : Array α) (i : @& Nat) (v : α) : Array α :=
Array.setD a i v
-- Slower `Array.append` used in quotations.
protected def Array.appendCore {α : Type u} (as : Array α) (bs : Array α) : Array α :=
let rec loop (i : Nat) (j : Nat) (as : Array α) : Array α :=
dite (LT.lt j bs.size)
(fun hlt =>
match i with
| 0 => as
| Nat.succ i' => loop i' (hAdd j 1) (as.push (bs.get ⟨j, hlt⟩)))
(fun _ => as)
loop bs.size 0 as
@[inlineIfReduce]
def List.toArrayAux : List α → Array α → Array α
| nil, r => r
| cons a as, r => toArrayAux as (r.push a)
@[inlineIfReduce]
def List.redLength : List α → Nat
| nil => 0
| cons _ as => as.redLength.succ
@[inline, matchPattern, export lean_list_to_array]
def List.toArray (as : List α) : Array α :=
as.toArrayAux (Array.mkEmpty as.redLength)
class Bind (m : Type u → Type v) where
bind : {α β : Type u} → m α → (α → m β) → m β
export Bind (bind)
class Pure (f : Type u → Type v) where
pure {α : Type u} : α → f α
export Pure (pure)
class Functor (f : Type u → Type v) : Type (max (u+1) v) where
map : {α β : Type u} → (α → β) → f α → f β
mapConst : {α β : Type u} → α → f β → f α := Function.comp map (Function.const _)
class Seq (f : Type u → Type v) : Type (max (u+1) v) where
seq : {α β : Type u} → f (α → β) → (Unit → f α) → f β
class SeqLeft (f : Type u → Type v) : Type (max (u+1) v) where
seqLeft : {α β : Type u} → f α → (Unit → f β) → f α
class SeqRight (f : Type u → Type v) : Type (max (u+1) v) where
seqRight : {α β : Type u} → f α → (Unit → f β) → f β
class Applicative (f : Type u → Type v) extends Functor f, Pure f, Seq f, SeqLeft f, SeqRight f where
map := fun x y => Seq.seq (pure x) fun _ => y
seqLeft := fun a b => Seq.seq (Functor.map (Function.const _) a) b
seqRight := fun a b => Seq.seq (Functor.map (Function.const _ id) a) b
class Monad (m : Type u → Type v) extends Applicative m, Bind m : Type (max (u+1) v) where
map f x := bind x (Function.comp pure f)
seq f x := bind f fun y => Functor.map y (x ())
seqLeft x y := bind x fun a => bind (y ()) (fun _ => pure a)
seqRight x y := bind x fun _ => y ()
instance {α : Type u} {m : Type u → Type v} [Monad m] : Inhabited (α → m α) where
default := pure
instance {α : Type u} {m : Type u → Type v} [Monad m] [Inhabited α] : Inhabited (m α) where
default := pure arbitrary
-- A fusion of Haskell's `sequence` and `map`
def Array.sequenceMap {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : α → m β) : m (Array β) :=
let rec loop (i : Nat) (j : Nat) (bs : Array β) : m (Array β) :=
dite (LT.lt j as.size)
(fun hlt =>
match i with
| 0 => pure bs
| Nat.succ i' => Bind.bind (f (as.get ⟨j, hlt⟩)) fun b => loop i' (hAdd j 1) (bs.push b))
(fun _ => bs)
loop as.size 0 Array.empty
/-- A Function for lifting a computation from an inner Monad to an outer Monad.
Like [MonadTrans](https://hackage.haskell.org/package/transformers-0.5.5.0/docs/Control-Monad-Trans-Class.html),
but `n` does not have to be a monad transformer.
Alternatively, an implementation of [MonadLayer](https://hackage.haskell.org/package/layers-0.1/docs/Control-Monad-Layer.html#t:MonadLayer) without `layerInvmap` (so far). -/
class MonadLift (m : Type u → Type v) (n : Type u → Type w) where
monadLift : {α : Type u} → m α → n α
/-- The reflexive-transitive closure of `MonadLift`.
`monadLift` is used to transitively lift monadic computations such as `StateT.get` or `StateT.put s`.
Corresponds to [MonadLift](https://hackage.haskell.org/package/layers-0.1/docs/Control-Monad-Layer.html#t:MonadLift). -/
class MonadLiftT (m : Type u → Type v) (n : Type u → Type w) where
monadLift : {α : Type u} → m α → n α
export MonadLiftT (monadLift)
abbrev liftM := @monadLift
instance (m n o) [MonadLift n o] [MonadLiftT m n] : MonadLiftT m o where
monadLift x := MonadLift.monadLift (m := n) (monadLift x)
instance (m) : MonadLiftT m m where
monadLift x := x
/-- A functor in the category of monads. Can be used to lift monad-transforming functions.
Based on pipes' [MFunctor](https://hackage.haskell.org/package/pipes-2.4.0/docs/Control-MFunctor.html),
but not restricted to monad transformers.
Alternatively, an implementation of [MonadTransFunctor](http://duairc.netsoc.ie/layers-docs/Control-Monad-Layer.html#t:MonadTransFunctor). -/
class MonadFunctor (m : Type u → Type v) (n : Type u → Type w) where
monadMap {α : Type u} : ({β : Type u} → m β → m β) → n α → n α
/-- The reflexive-transitive closure of `MonadFunctor`.
`monadMap` is used to transitively lift Monad morphisms -/
class MonadFunctorT (m : Type u → Type v) (n : Type u → Type w) where
monadMap {α : Type u} : ({β : Type u} → m β → m β) → n α → n α
export MonadFunctorT (monadMap)
instance (m n o) [MonadFunctor n o] [MonadFunctorT m n] : MonadFunctorT m o where
monadMap f := MonadFunctor.monadMap (m := n) (monadMap (m := m) f)
instance monadFunctorRefl (m) : MonadFunctorT m m where
monadMap f := f
inductive Except (ε : Type u) (α : Type v) where
| error : ε → Except ε α
| ok : α → Except ε α
attribute [unbox] Except
instance {ε : Type u} {α : Type v} [Inhabited ε] : Inhabited (Except ε α) where
default := Except.error arbitrary
/-- An implementation of [MonadError](https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Except.html#t:MonadError) -/
class MonadExceptOf (ε : Type u) (m : Type v → Type w) where
throw {α : Type v} : ε → m α
tryCatch {α : Type v} : m α → (ε → m α) → m α
abbrev throwThe (ε : Type u) {m : Type v → Type w} [MonadExceptOf ε m] {α : Type v} (e : ε) : m α :=
MonadExceptOf.throw e
abbrev tryCatchThe (ε : Type u) {m : Type v → Type w} [MonadExceptOf ε m] {α : Type v} (x : m α) (handle : ε → m α) : m α :=
MonadExceptOf.tryCatch x handle
/-- Similar to `MonadExceptOf`, but `ε` is an outParam for convenience -/
class MonadExcept (ε : outParam (Type u)) (m : Type v → Type w) where
throw {α : Type v} : ε → m α
tryCatch {α : Type v} : m α → (ε → m α) → m α
export MonadExcept (throw tryCatch)
instance (ε : outParam (Type u)) (m : Type v → Type w) [MonadExceptOf ε m] : MonadExcept ε m where
throw := throwThe ε
tryCatch := tryCatchThe ε
namespace MonadExcept
variable {ε : Type u} {m : Type v → Type w}
@[inline] protected def orElse [MonadExcept ε m] {α : Type v} (t₁ : m α) (t₂ : Unit → m α) : m α :=
tryCatch t₁ fun _ => t₂ ()
instance [MonadExcept ε m] {α : Type v} : OrElse (m α) where
orElse := MonadExcept.orElse
end MonadExcept
/-- An implementation of [ReaderT](https://hackage.haskell.org/package/transformers-0.5.5.0/docs/Control-Monad-Trans-Reader.html#t:ReaderT) -/
def ReaderT (ρ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) :=
ρ → m α
instance (ρ : Type u) (m : Type u → Type v) (α : Type u) [Inhabited (m α)] : Inhabited (ReaderT ρ m α) where
default := fun _ => arbitrary
@[inline] def ReaderT.run {ρ : Type u} {m : Type u → Type v} {α : Type u} (x : ReaderT ρ m α) (r : ρ) : m α :=
x r
namespace ReaderT
section
variable {ρ : Type u} {m : Type u → Type v} {α : Type u}
instance : MonadLift m (ReaderT ρ m) where
monadLift x := fun _ => x
instance (ε) [MonadExceptOf ε m] : MonadExceptOf ε (ReaderT ρ m) where
throw e := liftM (m := m) (throw e)
tryCatch := fun x c r => tryCatchThe ε (x r) (fun e => (c e) r)
end
section
variable {ρ : Type u} {m : Type u → Type v} [Monad m] {α β : Type u}
@[inline] protected def read : ReaderT ρ m ρ :=
pure
@[inline] protected def pure (a : α) : ReaderT ρ m α :=
fun r => pure a
@[inline] protected def bind (x : ReaderT ρ m α) (f : α → ReaderT ρ m β) : ReaderT ρ m β :=
fun r => bind (x r) fun a => f a r
@[inline] protected def map (f : α → β) (x : ReaderT ρ m α) : ReaderT ρ m β :=
fun r => Functor.map f (x r)
instance : Monad (ReaderT ρ m) where
pure := ReaderT.pure
bind := ReaderT.bind
map := ReaderT.map
instance (ρ m) [Monad m] : MonadFunctor m (ReaderT ρ m) where
monadMap f x := fun ctx => f (x ctx)
@[inline] protected def adapt {ρ' : Type u} [Monad m] {α : Type u} (f : ρ' → ρ) : ReaderT ρ m α → ReaderT ρ' m α :=
fun x r => x (f r)
end
end ReaderT
/-- An implementation of [MonadReader](https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Reader-Class.html#t:MonadReader).
It does not contain `local` because this Function cannot be lifted using `monadLift`.
Instead, the `MonadReaderAdapter` class provides the more general `adaptReader` Function.
Note: This class can be seen as a simplification of the more "principled" definition
```
class MonadReader (ρ : outParam (Type u)) (n : Type u → Type u) where
lift {α : Type u} : ({m : Type u → Type u} → [Monad m] → ReaderT ρ m α) → n α
```
-/
class MonadReaderOf (ρ : Type u) (m : Type u → Type v) where
read : m ρ
@[inline] def readThe (ρ : Type u) {m : Type u → Type v} [MonadReaderOf ρ m] : m ρ :=
MonadReaderOf.read
/-- Similar to `MonadReaderOf`, but `ρ` is an outParam for convenience -/
class MonadReader (ρ : outParam (Type u)) (m : Type u → Type v) where
read : m ρ
export MonadReader (read)
instance (ρ : Type u) (m : Type u → Type v) [MonadReaderOf ρ m] : MonadReader ρ m where
read := readThe ρ
instance {ρ : Type u} {m : Type u → Type v} {n : Type u → Type w} [MonadLift m n] [MonadReaderOf ρ m] : MonadReaderOf ρ n where
read := liftM (m := m) read
instance {ρ : Type u} {m : Type u → Type v} [Monad m] : MonadReaderOf ρ (ReaderT ρ m) where
read := ReaderT.read
class MonadWithReaderOf (ρ : Type u) (m : Type u → Type v) where
withReader {α : Type u} : (ρ → ρ) → m α → m α
@[inline] def withTheReader (ρ : Type u) {m : Type u → Type v} [MonadWithReaderOf ρ m] {α : Type u} (f : ρ → ρ) (x : m α) : m α :=
MonadWithReaderOf.withReader f x
class MonadWithReader (ρ : outParam (Type u)) (m : Type u → Type v) where
withReader {α : Type u} : (ρ → ρ) → m α → m α
export MonadWithReader (withReader)
instance (ρ : Type u) (m : Type u → Type v) [MonadWithReaderOf ρ m] : MonadWithReader ρ m where
withReader := withTheReader ρ
instance {ρ : Type u} {m : Type u → Type v} {n : Type u → Type v} [MonadFunctor m n] [MonadWithReaderOf ρ m] : MonadWithReaderOf ρ n where
withReader f := monadMap (m := m) (withTheReader ρ f)
instance {ρ : Type u} {m : Type u → Type v} [Monad m] : MonadWithReaderOf ρ (ReaderT ρ m) where
withReader f x := fun ctx => x (f ctx)
/-- An implementation of [MonadState](https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-State-Class.html).
In contrast to the Haskell implementation, we use overlapping instances to derive instances
automatically from `monadLift`. -/
class MonadStateOf (σ : Type u) (m : Type u → Type v) where
/- Obtain the top-most State of a Monad stack. -/
get : m σ
/- Set the top-most State of a Monad stack. -/
set : σ → m PUnit
/- Map the top-most State of a Monad stack.
Note: `modifyGet f` may be preferable to `do s <- get; let (a, s) := f s; put s; pure a`
because the latter does not use the State linearly (without sufficient inlining). -/
modifyGet {α : Type u} : (σ → Prod α σ) → m α
export MonadStateOf (set)
abbrev getThe (σ : Type u) {m : Type u → Type v} [MonadStateOf σ m] : m σ :=
MonadStateOf.get
@[inline] abbrev modifyThe (σ : Type u) {m : Type u → Type v} [MonadStateOf σ m] (f : σ → σ) : m PUnit :=
MonadStateOf.modifyGet fun s => (PUnit.unit, f s)
@[inline] abbrev modifyGetThe {α : Type u} (σ : Type u) {m : Type u → Type v} [MonadStateOf σ m] (f : σ → Prod α σ) : m α :=
MonadStateOf.modifyGet f
/-- Similar to `MonadStateOf`, but `σ` is an outParam for convenience -/
class MonadState (σ : outParam (Type u)) (m : Type u → Type v) where
get : m σ
set : σ → m PUnit
modifyGet {α : Type u} : (σ → Prod α σ) → m α
export MonadState (get modifyGet)
instance (σ : Type u) (m : Type u → Type v) [MonadStateOf σ m] : MonadState σ m where
set := MonadStateOf.set
get := getThe σ
modifyGet f := MonadStateOf.modifyGet f
@[inline] def modify {σ : Type u} {m : Type u → Type v} [MonadState σ m] (f : σ → σ) : m PUnit :=
modifyGet fun s => (PUnit.unit, f s)
@[inline] def getModify {σ : Type u} {m : Type u → Type v} [MonadState σ m] [Monad m] (f : σ → σ) : m σ :=
modifyGet fun s => (s, f s)
-- NOTE: The Ordering of the following two instances determines that the top-most `StateT` Monad layer
-- will be picked first
instance {σ : Type u} {m : Type u → Type v} {n : Type u → Type w} [MonadLift m n] [MonadStateOf σ m] : MonadStateOf σ n where
get := liftM (m := m) MonadStateOf.get
set s := liftM (m := m) (MonadStateOf.set s)
modifyGet f := monadLift (m := m) (MonadState.modifyGet f)
namespace EStateM
inductive Result (ε σ α : Type u) where
| ok : α → σ → Result ε σ α
| error : ε → σ → Result ε σ α
variable {ε σ α : Type u}
instance [Inhabited ε] [Inhabited σ] : Inhabited (Result ε σ α) where
default := Result.error arbitrary arbitrary
end EStateM
open EStateM (Result) in
def EStateM (ε σ α : Type u) := σ → Result ε σ α
namespace EStateM
variable {ε σ α β : Type u}
instance [Inhabited ε] : Inhabited (EStateM ε σ α) where
default := fun s => Result.error arbitrary s
@[inline] protected def pure (a : α) : EStateM ε σ α := fun s =>
Result.ok a s
@[inline] protected def set (s : σ) : EStateM ε σ PUnit := fun _ =>
Result.ok ⟨⟩ s
@[inline] protected def get : EStateM ε σ σ := fun s =>
Result.ok s s
@[inline] protected def modifyGet (f : σ → Prod α σ) : EStateM ε σ α := fun s =>
match f s with
| (a, s) => Result.ok a s
@[inline] protected def throw (e : ε) : EStateM ε σ α := fun s =>
Result.error e s
/-- Auxiliary instance for saving/restoring the "backtrackable" part of the state. -/
class Backtrackable (δ : outParam (Type u)) (σ : Type u) where
save : σ → δ
restore : σ → δ → σ
@[inline] protected def tryCatch {δ} [Backtrackable δ σ] {α} (x : EStateM ε σ α) (handle : ε → EStateM ε σ α) : EStateM ε σ α := fun s =>
let d := Backtrackable.save s
match x s with
| Result.error e s => handle e (Backtrackable.restore s d)
| ok => ok
@[inline] protected def orElse {δ} [Backtrackable δ σ] (x₁ : EStateM ε σ α) (x₂ : Unit → EStateM ε σ α) : EStateM ε σ α := fun s =>
let d := Backtrackable.save s;
match x₁ s with
| Result.error _ s => x₂ () (Backtrackable.restore s d)
| ok => ok
@[inline] def adaptExcept {ε' : Type u} (f : ε → ε') (x : EStateM ε σ α) : EStateM ε' σ α := fun s =>
match x s with
| Result.error e s => Result.error (f e) s
| Result.ok a s => Result.ok a s
@[inline] protected def bind (x : EStateM ε σ α) (f : α → EStateM ε σ β) : EStateM ε σ β := fun s =>
match x s with
| Result.ok a s => f a s
| Result.error e s => Result.error e s
@[inline] protected def map (f : α → β) (x : EStateM ε σ α) : EStateM ε σ β := fun s =>
match x s with
| Result.ok a s => Result.ok (f a) s
| Result.error e s => Result.error e s
@[inline] protected def seqRight (x : EStateM ε σ α) (y : Unit → EStateM ε σ β) : EStateM ε σ β := fun s =>
match x s with
| Result.ok _ s => y () s
| Result.error e s => Result.error e s
instance : Monad (EStateM ε σ) where
bind := EStateM.bind
pure := EStateM.pure
map := EStateM.map
seqRight := EStateM.seqRight
instance {δ} [Backtrackable δ σ] : OrElse (EStateM ε σ α) where
orElse := EStateM.orElse
instance : MonadStateOf σ (EStateM ε σ) where
set := EStateM.set
get := EStateM.get
modifyGet := EStateM.modifyGet
instance {δ} [Backtrackable δ σ] : MonadExceptOf ε (EStateM ε σ) where
throw := EStateM.throw
tryCatch := EStateM.tryCatch
@[inline] def run (x : EStateM ε σ α) (s : σ) : Result ε σ α :=
x s
@[inline] def run' (x : EStateM ε σ α) (s : σ) : Option α :=
match run x s with
| Result.ok v _ => some v
| Result.error .. => none
@[inline] def dummySave : σ → PUnit := fun _ => ⟨⟩
@[inline] def dummyRestore : σ → PUnit → σ := fun s _ => s
/- Dummy default instance -/
instance nonBacktrackable : Backtrackable PUnit σ where
save := dummySave
restore := dummyRestore
end EStateM
class Hashable (α : Sort u) where
hash : α → UInt64
export Hashable (hash)
@[extern "lean_uint64_to_usize"]
constant UInt64.toUSize (u : UInt64) : USize
@[extern "lean_usize_to_uint64"]
constant USize.toUInt64 (u : USize) : UInt64
@[extern "lean_uint64_mix_hash"]
constant mixHash (u₁ u₂ : UInt64) : UInt64
@[extern "lean_string_hash"]
protected constant String.hash (s : @& String) : UInt64
instance : Hashable String where
hash := String.hash
namespace Lean
/- Hierarchical names -/
inductive Name where
| anonymous : Name
| str : Name → String → UInt64 → Name
| num : Name → Nat → UInt64 → Name
instance : Inhabited Name where
default := Name.anonymous
protected def Name.hash : Name → UInt64
| Name.anonymous => UInt64.ofNatCore 1723 (by decide)
| Name.str p s h => h
| Name.num p v h => h
instance : Hashable Name where
hash := Name.hash
namespace Name
@[export lean_name_mk_string]
def mkStr (p : Name) (s : String) : Name :=
Name.str p s (mixHash (hash p) (hash s))
@[export lean_name_mk_numeral]
def mkNum (p : Name) (v : Nat) : Name :=
Name.num p v (mixHash (hash p) (dite (LT.lt v UInt64.size) (fun h => UInt64.ofNatCore v h) (fun _ => UInt64.ofNatCore 17 (by decide))))
def mkSimple (s : String) : Name :=
mkStr Name.anonymous s
@[extern "lean_name_eq"]
protected def beq : (@& Name) → (@& Name) → Bool
| anonymous, anonymous => true
| str p₁ s₁ _, str p₂ s₂ _ => and (BEq.beq s₁ s₂) (Name.beq p₁ p₂)
| num p₁ n₁ _, num p₂ n₂ _ => and (BEq.beq n₁ n₂) (Name.beq p₁ p₂)
| _, _ => false
instance : BEq Name where
beq := Name.beq
protected def append : Name → Name → Name
| n, anonymous => n
| n, str p s _ => Name.mkStr (Name.append n p) s
| n, num p d _ => Name.mkNum (Name.append n p) d
instance : Append Name where
append := Name.append
end Name
/- Syntax -/
/-- Source information of tokens. -/
inductive SourceInfo where
/-
Token from original input with whitespace and position information.
`leading` will be inferred after parsing by `Syntax.updateLeading`. During parsing,
it is not at all clear what the preceding token was, especially with backtracking. -/
| original (leading : Substring) (pos : String.Pos) (trailing : Substring) (endPos : String.Pos)
/-
Synthesized token (e.g. from a quotation) annotated with a span from the original source.
In the delaborator, we "misuse" this constructor to store synthetic positions identifying
subterms. -/
| synthetic (pos : String.Pos) (endPos : String.Pos)
/- Synthesized token without position information. -/
| protected none
instance : Inhabited SourceInfo := ⟨SourceInfo.none⟩
namespace SourceInfo
def getPos? (info : SourceInfo) (originalOnly := false) : Option String.Pos :=
match info, originalOnly with
| original (pos := pos) .., _ => some pos
| synthetic (pos := pos) .., false => some pos
| _, _ => none
end SourceInfo
abbrev SyntaxNodeKind := Name
/- Syntax AST -/
/--
Syntax objects used by the parser, macro expander, delaborator, etc.
-/
inductive Syntax where
| missing : Syntax
| /--
Node in the syntax tree.
The `info` field is used by the delaborator
to store the position of the subexpression
corresponding to this node.
The parser sets the `info` field to `none`.
(Remark: the `node` constructor
did not have an `info` field in previous versions.
This caused a bug in the interactive widgets,
where the popup for `a + b` was the same as for `a`.
The delaborator used to associate subexpressions
with pretty-printed syntax by setting
the (string) position of the first atom/identifier
to the (expression) position of the subexpression.
For example, both `a` and `a + b`
have the same first identifier,
and so their infos got mixed up.)
-/ node (info : SourceInfo) (kind : SyntaxNodeKind) (args : Array Syntax) : Syntax
| atom (info : SourceInfo) (val : String) : Syntax
| ident (info : SourceInfo) (rawVal : Substring) (val : Name) (preresolved : List (Prod Name (List String))) : Syntax
instance : Inhabited Syntax where
default := Syntax.missing
/- Builtin kinds -/
def choiceKind : SyntaxNodeKind := `choice
def nullKind : SyntaxNodeKind := `null
def groupKind : SyntaxNodeKind := `group
def identKind : SyntaxNodeKind := `ident
def strLitKind : SyntaxNodeKind := `strLit
def charLitKind : SyntaxNodeKind := `charLit
def numLitKind : SyntaxNodeKind := `numLit
def scientificLitKind : SyntaxNodeKind := `scientificLit
def nameLitKind : SyntaxNodeKind := `nameLit
def fieldIdxKind : SyntaxNodeKind := `fieldIdx
def interpolatedStrLitKind : SyntaxNodeKind := `interpolatedStrLitKind
def interpolatedStrKind : SyntaxNodeKind := `interpolatedStrKind
namespace Syntax
def getKind (stx : Syntax) : SyntaxNodeKind :=
match stx with
| Syntax.node _ k args => k
-- We use these "pseudo kinds" for antiquotation kinds.
-- For example, an antiquotation `$id:ident` (using Lean.Parser.Term.ident)
-- is compiled to ``if stx.isOfKind `ident ...``
| Syntax.missing => `missing
| Syntax.atom _ v => Name.mkSimple v
| Syntax.ident .. => identKind
def setKind (stx : Syntax) (k : SyntaxNodeKind) : Syntax :=
match stx with
| Syntax.node info _ args => Syntax.node info k args
| _ => stx
def isOfKind (stx : Syntax) (k : SyntaxNodeKind) : Bool :=
beq stx.getKind k
def getArg (stx : Syntax) (i : Nat) : Syntax :=
match stx with
| Syntax.node _ _ args => args.getD i Syntax.missing
| _ => Syntax.missing
-- Add `stx[i]` as sugar for `stx.getArg i`
@[inline] def getOp (self : Syntax) (idx : Nat) : Syntax :=
self.getArg idx
def getArgs (stx : Syntax) : Array Syntax :=
match stx with
| Syntax.node _ _ args => args
| _ => Array.empty
def getNumArgs (stx : Syntax) : Nat :=
match stx with
| Syntax.node _ _ args => args.size
| _ => 0
def isMissing : Syntax → Bool
| Syntax.missing => true
| _ => false
def isNodeOf (stx : Syntax) (k : SyntaxNodeKind) (n : Nat) : Bool :=
and (stx.isOfKind k) (beq stx.getNumArgs n)
def isIdent : Syntax → Bool
| ident _ _ _ _ => true
| _ => false
def getId : Syntax → Name
| ident _ _ val _ => val
| _ => Name.anonymous
def matchesNull (stx : Syntax) (n : Nat) : Bool :=
isNodeOf stx nullKind n
def matchesIdent (stx : Syntax) (id : Name) : Bool :=
and stx.isIdent (beq stx.getId id)
def setArgs (stx : Syntax) (args : Array Syntax) : Syntax :=
match stx with
| node info k _ => node info k args
| stx => stx
def setArg (stx : Syntax) (i : Nat) (arg : Syntax) : Syntax :=
match stx with
| node info k args => node info k (args.setD i arg)
| stx => stx
/-- Retrieve the left-most node or leaf's info in the Syntax tree. -/
partial def getHeadInfo? : Syntax → Option SourceInfo
| atom info _ => some info
| ident info .. => some info
| node SourceInfo.none _ args =>
let rec loop (i : Nat) : Option SourceInfo :=
match decide (LT.lt i args.size) with
| true => match getHeadInfo? (args.get! i) with
| some info => some info
| none => loop (hAdd i 1)
| false => none
loop 0
| node info _ _ => some info
| _ => none
/-- Retrieve the left-most leaf's info in the Syntax tree, or `none` if there is no token. -/
partial def getHeadInfo (stx : Syntax) : SourceInfo :=
match stx.getHeadInfo? with
| some info => info
| none => SourceInfo.none
def getPos? (stx : Syntax) (originalOnly := false) : Option String.Pos :=
stx.getHeadInfo.getPos? originalOnly
partial def getTailPos? (stx : Syntax) (originalOnly := false) : Option String.Pos :=
match stx, originalOnly with
| atom (SourceInfo.original (endPos := pos) ..) .., _ => some pos
| atom (SourceInfo.synthetic (endPos := pos) ..) _, false => some pos
| ident (SourceInfo.original (endPos := pos) ..) .., _ => some pos
| ident (SourceInfo.synthetic (endPos := pos) ..) .., false => some pos
| node (SourceInfo.original (endPos := pos) ..) .., _ => some pos
| node (SourceInfo.synthetic (endPos := pos) ..) .., false => some pos
| node _ _ args, _ =>
let rec loop (i : Nat) : Option String.Pos :=
match decide (LT.lt i args.size) with
| true => match getTailPos? (args.get! ((args.size.sub i).sub 1)) originalOnly with
| some info => some info
| none => loop (hAdd i 1)
| false => none
loop 0
| _, _ => none
/--
An array of syntax elements interspersed with separators. Can be coerced to/from `Array Syntax` to automatically
remove/insert the separators. -/
structure SepArray (sep : String) where
elemsAndSeps : Array Syntax
end Syntax
def SourceInfo.fromRef (ref : Syntax) : SourceInfo :=
match ref.getPos?, ref.getTailPos? with
| some pos, some tailPos => SourceInfo.synthetic pos tailPos
| _, _ => SourceInfo.none
def mkAtom (val : String) : Syntax :=
Syntax.atom SourceInfo.none val
def mkAtomFrom (src : Syntax) (val : String) : Syntax :=
Syntax.atom (SourceInfo.fromRef src) val
/- Parser descriptions -/
inductive ParserDescr where
| const (name : Name)
| unary (name : Name) (p : ParserDescr)
| binary (name : Name) (p₁ p₂ : ParserDescr)
| node (kind : SyntaxNodeKind) (prec : Nat) (p : ParserDescr)
| trailingNode (kind : SyntaxNodeKind) (prec lhsPrec : Nat) (p : ParserDescr)
| symbol (val : String)
| nonReservedSymbol (val : String) (includeIdent : Bool)
| cat (catName : Name) (rbp : Nat)
| parser (declName : Name)
| nodeWithAntiquot (name : String) (kind : SyntaxNodeKind) (p : ParserDescr)
| sepBy (p : ParserDescr) (sep : String) (psep : ParserDescr) (allowTrailingSep : Bool := false)
| sepBy1 (p : ParserDescr) (sep : String) (psep : ParserDescr) (allowTrailingSep : Bool := false)
instance : Inhabited ParserDescr where
default := ParserDescr.symbol ""
abbrev TrailingParserDescr := ParserDescr
/-
Runtime support for making quotation terms auto-hygienic, by mangling identifiers
introduced by them with a "macro scope" supplied by the context. Details to appear in a
paper soon.
-/
abbrev MacroScope := Nat
/-- Macro scope used internally. It is not available for our frontend. -/
def reservedMacroScope := 0
/-- First macro scope available for our frontend -/
def firstFrontendMacroScope := hAdd reservedMacroScope 1
class MonadRef (m : Type → Type) where
getRef : m Syntax
withRef {α} : Syntax → m α → m α
export MonadRef (getRef)
instance (m n : Type → Type) [MonadLift m n] [MonadFunctor m n] [MonadRef m] : MonadRef n where
getRef := liftM (getRef : m _)
withRef ref x := monadMap (m := m) (MonadRef.withRef ref) x
def replaceRef (ref : Syntax) (oldRef : Syntax) : Syntax :=
match ref.getPos? with
| some _ => ref
| _ => oldRef
@[inline] def withRef {m : Type → Type} [Monad m] [MonadRef m] {α} (ref : Syntax) (x : m α) : m α :=
bind getRef fun oldRef =>
let ref := replaceRef ref oldRef
MonadRef.withRef ref x
/-- A monad that supports syntax quotations. Syntax quotations (in term
position) are monadic values that when executed retrieve the current "macro
scope" from the monad and apply it to every identifier they introduce
(independent of whether this identifier turns out to be a reference to an
existing declaration, or an actually fresh binding during further
elaboration). We also apply the position of the result of `getRef` to each
introduced symbol, which results in better error positions than not applying
any position. -/
class MonadQuotation (m : Type → Type) extends MonadRef m where
-- Get the fresh scope of the current macro invocation
getCurrMacroScope : m MacroScope
getMainModule : m Name
/- Execute action in a new macro invocation context. This transformer should be
used at all places that morally qualify as the beginning of a "macro call",
e.g. `elabCommand` and `elabTerm` in the case of the elaborator. However, it
can also be used internally inside a "macro" if identifiers introduced by
e.g. different recursive calls should be independent and not collide. While
returning an intermediate syntax tree that will recursively be expanded by
the elaborator can be used for the same effect, doing direct recursion inside
the macro guarded by this transformer is often easier because one is not
restricted to passing a single syntax tree. Modelling this helper as a
transformer and not just a monadic action ensures that the current macro
scope before the recursive call is restored after it, as expected. -/
withFreshMacroScope {α : Type} : m α → m α
export MonadQuotation (getCurrMacroScope getMainModule withFreshMacroScope)
def MonadRef.mkInfoFromRefPos [Monad m] [MonadRef m] : m SourceInfo := do
SourceInfo.fromRef (← getRef)
instance {m n : Type → Type} [MonadFunctor m n] [MonadLift m n] [MonadQuotation m] : MonadQuotation n where
getCurrMacroScope := liftM (m := m) getCurrMacroScope
getMainModule := liftM (m := m) getMainModule
withFreshMacroScope := monadMap (m := m) withFreshMacroScope
/-
We represent a name with macro scopes as
```
<actual name>._@.(<module_name>.<scopes>)*.<module_name>._hyg.<scopes>
```
Example: suppose the module name is `Init.Data.List.Basic`, and name is `foo.bla`, and macroscopes [2, 5]
```
foo.bla._@.Init.Data.List.Basic._hyg.2.5
```
We may have to combine scopes from different files/modules.
The main modules being processed is always the right most one.
This situation may happen when we execute a macro generated in
an imported file in the current file.
```
foo.bla._@.Init.Data.List.Basic.2.1.Init.Lean.Expr_hyg.4
```
The delimiter `_hyg` is used just to improve the `hasMacroScopes` performance.
-/
def Name.hasMacroScopes : Name → Bool
| str _ s _ => beq s "_hyg"
| num p _ _ => hasMacroScopes p
| _ => false
private def eraseMacroScopesAux : Name → Name
| Name.str p s _ => match beq s "_@" with
| true => p
| false => eraseMacroScopesAux p
| Name.num p _ _ => eraseMacroScopesAux p
| Name.anonymous => Name.anonymous
@[export lean_erase_macro_scopes]
def Name.eraseMacroScopes (n : Name) : Name :=
match n.hasMacroScopes with
| true => eraseMacroScopesAux n
| false => n
private def simpMacroScopesAux : Name → Name
| Name.num p i _ => Name.mkNum (simpMacroScopesAux p) i
| n => eraseMacroScopesAux n
/- Helper function we use to create binder names that do not need to be unique. -/
@[export lean_simp_macro_scopes]
def Name.simpMacroScopes (n : Name) : Name :=
match n.hasMacroScopes with
| true => simpMacroScopesAux n
| false => n
structure MacroScopesView where
name : Name
imported : Name
mainModule : Name
scopes : List MacroScope
instance : Inhabited MacroScopesView where
default := ⟨arbitrary, arbitrary, arbitrary, arbitrary⟩
def MacroScopesView.review (view : MacroScopesView) : Name :=
match view.scopes with
| List.nil => view.name
| List.cons _ _ =>
let base := (Name.mkStr (hAppend (hAppend (Name.mkStr view.name "_@") view.imported) view.mainModule) "_hyg")
view.scopes.foldl Name.mkNum base
private def assembleParts : List Name → Name → Name
| List.nil, acc => acc
| List.cons (Name.str _ s _) ps, acc => assembleParts ps (Name.mkStr acc s)
| List.cons (Name.num _ n _) ps, acc => assembleParts ps (Name.mkNum acc n)
| _, acc => panic "Error: unreachable @ assembleParts"
private def extractImported (scps : List MacroScope) (mainModule : Name) : Name → List Name → MacroScopesView
| n@(Name.str p str _), parts =>
match beq str "_@" with
| true => { name := p, mainModule := mainModule, imported := assembleParts parts Name.anonymous, scopes := scps }
| false => extractImported scps mainModule p (List.cons n parts)
| n@(Name.num p str _), parts => extractImported scps mainModule p (List.cons n parts)
| _, _ => panic "Error: unreachable @ extractImported"
private def extractMainModule (scps : List MacroScope) : Name → List Name → MacroScopesView
| n@(Name.str p str _), parts =>
match beq str "_@" with
| true => { name := p, mainModule := assembleParts parts Name.anonymous, imported := Name.anonymous, scopes := scps }
| false => extractMainModule scps p (List.cons n parts)
| n@(Name.num p num _), acc => extractImported scps (assembleParts acc Name.anonymous) n List.nil
| _, _ => panic "Error: unreachable @ extractMainModule"
private def extractMacroScopesAux : Name → List MacroScope → MacroScopesView
| Name.num p scp _, acc => extractMacroScopesAux p (List.cons scp acc)
| Name.str p str _, acc => extractMainModule acc p List.nil -- str must be "_hyg"
| _, _ => panic "Error: unreachable @ extractMacroScopesAux"
/--
Revert all `addMacroScope` calls. `v = extractMacroScopes n → n = v.review`.
This operation is useful for analyzing/transforming the original identifiers, then adding back
the scopes (via `MacroScopesView.review`). -/
def extractMacroScopes (n : Name) : MacroScopesView :=
match n.hasMacroScopes with
| true => extractMacroScopesAux n List.nil
| false => { name := n, scopes := List.nil, imported := Name.anonymous, mainModule := Name.anonymous }
def addMacroScope (mainModule : Name) (n : Name) (scp : MacroScope) : Name :=
match n.hasMacroScopes with
| true =>
let view := extractMacroScopes n
match beq view.mainModule mainModule with
| true => Name.mkNum n scp
| false =>
{ view with
imported := view.scopes.foldl Name.mkNum (hAppend view.imported view.mainModule)
mainModule := mainModule
scopes := List.cons scp List.nil
}.review
| false =>
Name.mkNum (Name.mkStr (hAppend (Name.mkStr n "_@") mainModule) "_hyg") scp
@[inline] def MonadQuotation.addMacroScope {m : Type → Type} [MonadQuotation m] [Monad m] (n : Name) : m Name :=
bind getMainModule fun mainModule =>
bind getCurrMacroScope fun scp =>
pure (Lean.addMacroScope mainModule n scp)
def defaultMaxRecDepth := 512
def maxRecDepthErrorMessage : String :=
"maximum recursion depth has been reached (use `set_option maxRecDepth <num>` to increase limit)"
namespace Macro
/- References -/
private constant MethodsRefPointed : PointedType.{0}
private def MethodsRef : Type := MethodsRefPointed.type
structure Context where
methods : MethodsRef
mainModule : Name
currMacroScope : MacroScope
currRecDepth : Nat := 0
maxRecDepth : Nat := defaultMaxRecDepth
ref : Syntax
inductive Exception where
| error : Syntax → String → Exception
| unsupportedSyntax : Exception
structure State where
macroScope : MacroScope
traceMsgs : List (Prod Name String) := List.nil
deriving Inhabited
end Macro
abbrev MacroM := ReaderT Macro.Context (EStateM Macro.Exception Macro.State)
abbrev Macro := Syntax → MacroM Syntax
namespace Macro
instance : MonadRef MacroM where
getRef := bind read fun ctx => pure ctx.ref
withRef := fun ref x => withReader (fun ctx => { ctx with ref := ref }) x
def addMacroScope (n : Name) : MacroM Name :=
bind read fun ctx =>
pure (Lean.addMacroScope ctx.mainModule n ctx.currMacroScope)
def throwUnsupported {α} : MacroM α :=
throw Exception.unsupportedSyntax
def throwError {α} (msg : String) : MacroM α :=
bind getRef fun ref =>
throw (Exception.error ref msg)
def throwErrorAt {α} (ref : Syntax) (msg : String) : MacroM α :=
withRef ref (throwError msg)
@[inline] protected def withFreshMacroScope {α} (x : MacroM α) : MacroM α :=
bind (modifyGet (fun s => (s.macroScope, { s with macroScope := hAdd s.macroScope 1 }))) fun fresh =>
withReader (fun ctx => { ctx with currMacroScope := fresh }) x
@[inline] def withIncRecDepth {α} (ref : Syntax) (x : MacroM α) : MacroM α :=
bind read fun ctx =>
match beq ctx.currRecDepth ctx.maxRecDepth with
| true => throw (Exception.error ref maxRecDepthErrorMessage)
| false => withReader (fun ctx => { ctx with currRecDepth := hAdd ctx.currRecDepth 1 }) x
instance : MonadQuotation MacroM where
getCurrMacroScope ctx := pure ctx.currMacroScope
getMainModule ctx := pure ctx.mainModule
withFreshMacroScope := Macro.withFreshMacroScope
structure Methods where
expandMacro? : Syntax → MacroM (Option Syntax)
getCurrNamespace : MacroM Name
hasDecl : Name → MacroM Bool
resolveNamespace? : Name → MacroM (Option Name)
resolveGlobalName : Name → MacroM (List (Prod Name (List String)))
deriving Inhabited
unsafe def mkMethodsImp (methods : Methods) : MethodsRef :=
unsafeCast methods
@[implementedBy mkMethodsImp]
constant mkMethods (methods : Methods) : MethodsRef := MethodsRefPointed.val
instance : Inhabited MethodsRef where
default := mkMethods arbitrary
unsafe def getMethodsImp : MacroM Methods :=
bind read fun ctx => pure (unsafeCast (ctx.methods))
@[implementedBy getMethodsImp] constant getMethods : MacroM Methods
/-- `expandMacro? stx` return `some stxNew` if `stx` is a macro, and `stxNew` is its expansion. -/
def expandMacro? (stx : Syntax) : MacroM (Option Syntax) := do
(← getMethods).expandMacro? stx
/-- Return `true` if the environment contains a declaration with name `declName` -/
def hasDecl (declName : Name) : MacroM Bool := do
(← getMethods).hasDecl declName
def getCurrNamespace : MacroM Name := do
(← getMethods).getCurrNamespace
def resolveNamespace? (n : Name) : MacroM (Option Name) := do
(← getMethods).resolveNamespace? n
def resolveGlobalName (n : Name) : MacroM (List (Prod Name (List String))) := do
(← getMethods).resolveGlobalName n
def trace (clsName : Name) (msg : String) : MacroM Unit := do
modify fun s => { s with traceMsgs := List.cons (Prod.mk clsName msg) s.traceMsgs }
end Macro
export Macro (expandMacro?)
namespace PrettyPrinter
abbrev UnexpandM := EStateM Unit Unit
/--
Function that tries to reverse macro expansions as a post-processing step of delaboration.
While less general than an arbitrary delaborator, it can be declared without importing `Lean`.
Used by the `[appUnexpander]` attribute. -/
-- a `kindUnexpander` could reasonably be added later
abbrev Unexpander := Syntax → UnexpandM Syntax
-- unexpanders should not need to introduce new names
instance : MonadQuotation UnexpandM where
getRef := pure Syntax.missing
withRef := fun _ => id
getCurrMacroScope := pure 0
getMainModule := pure `_fakeMod
withFreshMacroScope := id
end PrettyPrinter
end Lean
|
172393e992079618dad89af8b8d4c078703d2fb7 | 750acab0c635b67751bcfec43c5411aa3941c441 | /hilbert.lean | 7a6cc3de0663483ff063e9f4778271e3a23807ac | [] | no_license | nthomas103/lean_work | 912f8e662cdd73ba97f5d3655ddb8a5d2cd204c9 | 7e9785cae2b60a77b41922fd5d5b159a1fae415c | refs/heads/master | 1,586,739,169,355 | 1,455,759,226,000 | 1,455,759,226,000 | 50,978,095 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,104 | lean | import standard algebra.module theories.topology.basic
import theories.analysis.normed_space theories.analysis.ivt
import theories.analysis.metric_space
open real nat algebra prod function analysis classical
open fin set topology
definition vector [reducible] (T : Type) (n : ℕ) := fin n → T
definition δ {n} : fin n → fin n → ℝ :=
λ i j, if i = j then 1 else 0
/-attribute [simp] smul_left_distrib smul_right_distrib mul_smul one_smul
attribute [simp] zero_smul smul_zero neg_smul neg_one_smul smul_neg
attribute [simp] smul_sub_left_distrib sub_smul_right_distrib
-/
--function space is module
section fn_module
variables {R S : Type}
variable [comm_ring R]
definition fn_add (f g : S → R) (x : S) := f x + g x
definition fn_smul (a : R) (f : S → R) (x : S) := a * f x
definition fn_zero (x : S) : R := 0
definition fn_neg (f : S → R) (x : S) := - f x
attribute [reducible] fn_add fn_smul fn_zero fn_neg
proposition fn_add_assoc (f g h : S → R) :
(fn_add (fn_add f g) h) = (fn_add f (fn_add g h)) :=
by blast
proposition fn_zero_add (f : S → R) :
fn_add fn_zero f = f :=
by blast
proposition fn_add_zero (f : S → R) :
fn_add f fn_zero = f :=
by blast
proposition fn_add_left_inv (f : S → R) :
fn_add (fn_neg f) f = fn_zero :=
by blast
proposition fn_add_comm (f g : S → R) :
fn_add f g = fn_add g f :=
by blast
-- attribute [simp] fn_add_assoc fn_zero_add fn_add_zero fn_add_left_inv
--attribute [simp] fn_add_comm
proposition fn_smul_left_distrib : ∀ (a : R) (f g : S → R),
fn_smul a (fn_add f g) = (fn_add (fn_smul a f) (fn_smul a g)) :=
λ a f g, funext (λ x, !left_distrib)
proposition fn_smul_right_distrib : ∀ (a b : R) (f : S → R),
fn_smul (a + b) f = fn_add (fn_smul a f) (fn_smul b f) :=
λ a f g, funext (λ x, !right_distrib)
proposition fn_mul_smul : ∀ (a b : R) (f : S → R),
fn_smul (a * b) f = fn_smul a (fn_smul b f) :=
by blast
proposition fn_one_smul : ∀ (f : S → R), fn_smul 1 f = f :=
by blast
definition fn_module [instance] : left_module R (S → R) :=
⦃
left_module,
smul := fn_smul,
add := fn_add,
add_assoc := fn_add_assoc,
zero := fn_zero,
zero_add := fn_zero_add,
add_zero := fn_add_zero,
neg := fn_neg,
add_left_inv := fn_add_left_inv,
add_comm := fn_add_comm,
smul_left_distrib := fn_smul_left_distrib,
smul_right_distrib := fn_smul_right_distrib,
mul_smul := fn_mul_smul,
one_smul := fn_one_smul
⦄
definition fn_mul (f g : S → R) (x : S) := (f x) * (g x)
definition fn_one (x : S) : R := 1
-- definition fn_inv
attribute [reducible] fn_mul fn_one
proposition fn_mul_assoc (f g h : S → R) :
(fn_mul (fn_mul f g) h) = (fn_mul f (fn_mul g h)) :=
by blast
proposition fn_one_mul (f : S → R) :
(fn_mul fn_one f) = f :=
by blast
proposition fn_mul_one (f : S → R) :
(fn_mul f fn_one) = f :=
by blast
proposition fn_mul_comm (f g : S → R) :
(fn_mul f g) = (fn_mul g f) :=
by blast
-- attribute [simp] fn_mul_assoc fn_one_mul fn_mul_one fn_mul_comm
definition fn_comm_monoid [instance] : comm_monoid (S → R) :=
⦃ comm_monoid,
mul := fn_mul,
one := fn_one,
mul_assoc := fn_mul_assoc,
one_mul := fn_one_mul,
mul_one := fn_mul_one,
mul_comm := fn_mul_comm
⦄
end fn_module
section vs
variables {R M : Type} {ringR : ring R} {moduleRM : left_module R M}
include ringR moduleRM
lemma unique_additive_identity (z : M)
(h₁ : ∀ x, x + z = x) (h₂ : ∀ x, z + x = x) :
z = 0 :=
sorry
-- lemma unique_additive_inverse
end vs
definition is_subspace [class] (R : Type) [ring R]
{V : Type} [left_module R V]
(U : set V) := left_module R (subtype U)
structure subspace [class] (R : Type) [ring R]
{V : Type} [left_module R V]
(U : set V) :=
(zero_in : 0 ∈ U)
(add_in : ∀ u w, u ∈ U → w ∈ U → u + w ∈ U)
(smul_in : ∀ (a : R) u, u ∈ U → a • u ∈ U)
definition subs [instance] {R : Type} [ring R] {V : Type} [left_module R V]
(U : set V) [subspace R U] : is_subspace R U :=
sorry
definition sum_of_subspaces (R : Type) [ring R] {V : Type} [left_module R V]
(U₁ U₂ : set V) [is_subspace R U₁] [is_subspace R U₂] :=
{ u | ∃ u₁ u₂, u₁ ∈ U₁ ∧ u₂ ∈ U₂ ∧ u = u₁ + u₂}
definition sum_of_subspaces_bigop
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (U : vector (set V) n) {h : ∀ i, is_subspace R (U i)} :=
{ u₀ | ∃ u, (∀ i, (u i) ∈ (U i)) ∧ u₀ = ∑ i ← upto n, (u i)}
definition sum_of_subspaces_is_subspace [instance]
(R : Type) [ring R] {V : Type} [left_module R V]
(U₁ U₂ : set V) [is_subspace R U₁] [is_subspace R U₂] :
is_subspace R (sum_of_subspaces R U₁ U₂) := sorry
theorem sum_of_subspaces_is_smallest
(R : Type) [ring R] {V : Type} [left_module R V]
(U₁ U₂ : set V) [is_subspace R U₁] [is_subspace R U₂]
(U : set V) (h : is_subspace R U) :
(sum_of_subspaces R U₁ U₂) ⊆ U := sorry
definition is_direct_sum (R : Type) [ring R] {V : Type} [left_module R V]
(U₁ U₂ : set V) [is_subspace R U₁] [is_subspace R U₂] :=
∀ w, w ∈ (sum_of_subspaces R U₁ U₂) →
∃! u₁ u₂, u₁ ∈ U₁ → u₂ ∈ U₂ → w = u₁ + u₂
theorem direct_sum_condition
(R : Type) [ring R] {V : Type} [left_module R V]
(U₁ U₂ : set V) [is_subspace R U₁] [is_subspace R U₂] :
(is_direct_sum R U₁ U₂) ↔
(∀ u₁ u₂, u₁ ∈ U₁ → u₂ ∈ U₂ → u₁ + u₂ = 0 → u₁ = 0 ∧ u₂ = 0) := sorry
theorem direct_sum_intersection
(R : Type) [ring R] {V : Type} [left_module R V]
(U₁ U₂ : set V) [is_subspace R U₁] [is_subspace R U₂] :
(is_direct_sum R U₁ U₂) ↔ U₁ ∩ U₂ = '{0} := sorry
--finite dimensional vector spaces
definition linear_combination [reducible]
{R V : Type} [ring R] [left_module R V]
{n} (a : vector R n) (v : vector V n) :=
∑ i ← upto n, (a i)•(v i)
definition span [reducible]
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (v : vector V n) :=
{ w | ∃ a : vector R n, w = linear_combination a v}
--what about span of 0-dim vector? can we prove that it is '{0}?
definition span_is_subspace [instance]
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (v : vector V n) : is_subspace R (span R v) :=
sorry
theorem span_is_smallest_subspace
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (v : vector V n)
(U : set V) [is_subspace R U] (h : ∀ i, v i ∈ U) :
(span R v) ⊆ U := sorry
definition spans
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (v : vector V n) (U : set V) :=
span R v = U
definition is_finite_dim_vs [class]
(R : Type) [ring R] (V : Type) [left_module R V] : Prop :=
∃ (n : ℕ) (v : vector V n), spans R v univ
definition linearly_independent [reducible]
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (v : vector V n) :=
∀ a : vector R n, linear_combination a v = 0 → a = (λ i, 0)
definition linearly_dependent [reducible]
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (v : vector V n) :=
∃ a : vector R n, linear_combination a v = 0
--do we have ∀ v, linearly_independent R v ↔ ¬ linearly_dependent R v ?
definition is_basis
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (v : vector V n) :=
linearly_independent R v ∧ spans R v univ
theorem basis_criterion
(R : Type) [ring R] {V : Type} [left_module R V]
{n} (v : vector V n) :
is_basis R v ↔ ∀ u, ∃! a : vector R n, u = linear_combination a v :=
sorry
theorem fdvs_basis
(R : Type) [ring R] {V : Type} [left_module R V]
(fdvs : is_finite_dim_vs R V) :
∃ (n : ℕ) (v : vector V n), is_basis R v :=
sorry
definition eigenvalue
(R : Type) [ring R] {V : Type} [left_module R V]
(T : V → V) [is_linear_map R T] (a : R) :=
∃ v, v ≠ 0 ∧ T v = a • v
--misc properties of square roots
attribute [simp] sqrt_spec abs_of_nonneg abs_of_neg
lemma sqrt_zero [simp] : sqrt (0:ℝ) = 0 :=
or.elim (eq_zero_or_eq_zero_of_mul_eq_zero (sqrt_spec 0 (le_of_eq rfl)))
id id
lemma eq_zero_of_sqrt_eq_zero (a : ℝ) (h₁ : a ≥ 0) (h₂ : sqrt a = 0) :
a = 0 :=
calc a = sqrt a * sqrt a : sqrt_spec a h₁
... = 0 : by simp
lemma mul_sqrt_eq_sqrt_mul [simp] (a b : ℝ) (ha : a ≥ 0) (hb : b ≥ 0) :
sqrt (a * b) = (sqrt a) * (sqrt b) := sorry
lemma sqrt_sqr [simp] (a : ℝ) : sqrt (a * a) = abs a :=
dite (a ≥ 0)
(λ h, by inst_simp)
(λ nh,
assert (a < 0), from lt_of_not_ge nh,
assert (-a ≥ 0), from neg_nonneg_of_nonpos (le_of_lt this),
calc sqrt (a * a) = sqrt (-a * -a) : by inst_simp
... = abs a : by inst_simp)
--attribute [simp] linear_map_zero linear_map_additive
--attribute [simp] linear_map_homogeneous
--definition of real inner product spaces
structure real_inner_product_space [class] (V : Type)
extends real_vector_space V :=
(ip : V → V → ℝ)
(positivity : ∀ v, ip v v ≥ 0)
(ip_zero : ip zero zero = 0)
(eq_zero_of_ip_eq_zero : ∀ v, ip v v = 0 → v = zero)
(add_left : ∀ u v w, (ip (add u v) w) = (ip u w) + (ip v w))
(homog_left : ∀ (a : ℝ) u v, ip (smul a u) v = a * (ip u v))
(symm : ∀ u v, ip u v = ip v u)
notation ⟨v,w⟩ := real_inner_product_space.ip v w
section rips
variables {V : Type} [real_inner_product_space V]
variables (u v w : V) (a : ℝ)
lemma positivity [intro!] : ⟨v,v⟩ ≥ 0 :=
!real_inner_product_space.positivity
lemma eq_zero_of_ip_eq_zero [intro!] : ⟨v,v⟩ = 0 → v = 0 :=
!real_inner_product_space.eq_zero_of_ip_eq_zero
lemma ip_zero : ⟨(0:V),0⟩ = 0 :=
!real_inner_product_space.ip_zero
lemma add_left : ⟨u+v,w⟩ = ⟨u,w⟩ + ⟨v,w⟩ :=
!real_inner_product_space.add_left
lemma homog_left : ⟨a•u,v⟩ = a*⟨u,v⟩ :=
!real_inner_product_space.homog_left
lemma symm : ⟨u,v⟩ = ⟨v,u⟩ :=
!real_inner_product_space.symm
attribute [simp] ip_zero add_left homog_left symm
theorem add_right : ⟨u,v+w⟩ = ⟨u,v⟩ + ⟨u,w⟩ := by inst_simp
theorem homog_right : ⟨u,a•v⟩ = a*⟨u,v⟩ := by inst_simp
noncomputable definition ip_linear [instance] :
is_linear_map ℝ (λ v₀, ⟨v₀,u⟩) := sorry
theorem zero_left [simp] : ⟨0,u⟩ = 0 :=
sorry--linear_map_zero (λ v₀, ⟨v₀,u⟩)
theorem zero_right [simp] : ⟨u,0⟩ = 0 := by inst_simp
definition orthogonal [reducible] := ⟨u,v⟩ = 0
theorem orth_0 : orthogonal 0 u := by inst_simp
theorem orth_self : orthogonal u u → u = 0 := by grind
definition is_orthonormal [reducible] {n} (e : vector V n) :=
∀ i j, ⟨e i, e j⟩ = δ i j
theorem lin_ind_of_on {n} (e : vector V n) [is_orthonormal e] :
linearly_independent ℝ e := sorry
noncomputable definition is_onb [reducible] [class]
{n} (e : vector V n) :=
is_basis ℝ e ∧ is_orthonormal e
theorem lin_comb_of_onb1 {n} (e : vector V n) [is_onb e]
(v : V) :
v = ∑ i ← upto n, ⟨v,(e i)⟩•(e i) := sorry
end rips
section sqrt_norm
variables {V : Type} [real_inner_product_space V]
--define Hilbert space norm
noncomputable definition sqrt_norm [instance] : has_norm V :=
⦃ has_norm,
norm := λ v : V, sqrt ⟨v,v⟩
⦄
attribute [reducible] has_norm.norm
--prove properties of Hilbert space norm
lemma norm_zero : ∥(0:V)∥ = 0 := calc
sqrt ⟨0,0⟩ = sqrt 0 : {ip_zero}
... = 0 : by simp
lemma eq_zero_of_norm_eq_zero (u : V) (h : ∥u∥ = 0) : u = 0 :=
eq_zero_of_ip_eq_zero u (eq_zero_of_sqrt_eq_zero ⟨u,u⟩ !positivity h)
lemma norm_smul (a : ℝ) (v : V) : ∥a•v∥ = abs a * ∥v∥ := calc
∥a•v∥ = sqrt (a * ⟨v,a•v⟩) : {!homog_left}
... = sqrt (a * a * ⟨v,v⟩) : by inst_simp
... = sqrt (a * a) * ∥v∥ : !mul_sqrt_eq_sqrt_mul !mul_self_nonneg !positivity
... = abs a * ∥v∥ : by inst_simp
attribute [simp] norm_zero norm_smul pow_two
theorem pythagorean [simp] (u v : V) (h : orthogonal u v) :
∥u + v∥^2 = ∥u∥^2 + ∥v∥^2 := calc
∥u + v∥^2 = ⟨u+v,u+v⟩ : sorry --shouldn't this work?
... = ⟨u,u⟩ + ⟨v,v⟩ : by inst_simp
... = ∥u∥^2 + ∥v∥^2 : sorry
--theorem orth_decomp (u v : V) {h : v ≠ 0} :
theorem cauchy_schwarz (u v : V) : abs ⟨u,v⟩ = ∥u∥*∥v∥ := sorry
lemma norm_triangle (u v : V) : ∥u + v∥ ≤ ∥u∥ + ∥v∥ :=
sorry --too complicated for now, need cauchy-schwarz
theorem parallelogram_eq (u v : V) :
∥u+v∥^2 + ∥u-v∥^2 = 2*(∥u∥^2+∥v∥^2) := sorry
theorem norm_of_on_lin_comb {n} (a : vector ℝ n)
(e : vector V n) [is_orthonormal e] :
∥linear_combination a e∥^2 = ∑ i ← upto n, (a i)^2 := sorry
theorem lin_comb_of_onb2 {n} (e : vector V n) [is_onb e]
(v : V) :
∥v∥^2 = ∑ i ← upto n, ⟨v,(e i)⟩^2 := sorry
end sqrt_norm
noncomputable definition real_inner_product_space_to_normed_vector_space
[reducible] [trans_instance] (V : Type)
[ips : real_inner_product_space V] : normed_vector_space V :=
⦃ normed_vector_space,
norm_zero := norm_zero,
eq_zero_of_norm_eq_zero := eq_zero_of_norm_eq_zero,
norm_triangle := norm_triangle,
norm_smul := norm_smul,
add_assoc := real_inner_product_space.add_assoc,
zero_add := real_inner_product_space.zero_add,
add_zero := real_inner_product_space.add_zero,
add_left_inv := real_inner_product_space.add_left_inv,
add_comm := real_inner_product_space.add_comm,
smul_left_distrib := real_inner_product_space.smul_left_distrib,
smul_right_distrib := real_inner_product_space.smul_right_distrib,
mul_smul := real_inner_product_space.mul_smul,
one_smul := real_inner_product_space.one_smul
⦄
structure real_hilbert_space [class] (V : Type)
extends ips : real_inner_product_space V :=
(complete : ∀ X, @cauchy V (@normed_vector_space_to_metric_space V
(@real_inner_product_space_to_normed_vector_space V ips)) X →
@converges_seq V (@normed_vector_space_to_metric_space V
(@real_inner_product_space_to_normed_vector_space V ips)) X)
noncomputable definition real_hilbert_space_to_banach_space
[reducible] [trans_instance]
(V : Type) [real_hilbert_space V] :
banach_space V :=
⦃ banach_space,
complete := real_hilbert_space.complete
⦄
-- euclidean spaces
definition R [reducible] (n : ℕ) := vector ℝ n
protected lemma ext {n} {x y : R n}
(h : ∀ i , x i = y i) : x = y :=
funext (λ i, h i)
noncomputable definition euclidean_space [instance] {n : ℕ} :
real_hilbert_space (R n) :=
⦃ real_hilbert_space,
add := λ x y i, (x i) + (y i),
add_assoc := λ x y z, ext (λ i, !add.assoc),
zero := λ i, 0,
zero_add := λ x, ext (λ i, !zero_add),
add_zero := λ x, ext (λ i, !add_zero),
neg := λ x i, -(x i),
add_left_inv := λ x, ext (λ i, !add.left_inv),
add_comm := λ x y, ext (λ i, !add.comm),
smul := λ a x i, a * (x i),
smul_left_distrib := λ r x y, ext (λ i, !smul_left_distrib),
smul_right_distrib := λ r s x, ext (λ i, !smul_right_distrib),
mul_smul := λ r s x, ext (λ i, !mul_smul),
one_smul := λ x, ext (λ i, !one_smul),
ip := λ x y, ∑ i ← upto n, ((x i) * (y i)),
positivity := sorry,
ip_zero := !Suml_zero,
eq_zero_of_ip_eq_zero := sorry,
add_left := sorry,
homog_left := sorry,
symm := sorry,
complete := sorry
⦄
structure neighborhood [class] {X} [topology X] (p : X) (V : set X) :=
(U : set X)
(U_open : Open U)
(p_in_U : p ∈ U)
(U_in_V : U ⊆ V)
structure homeomorphism [class] {M N : Type} [topology M] [topology N]
(f : M → N) :=
(bij : bijective f)
(contin : continuous f)
(f_inv : N → M)
(inv : left_inverse f_inv f)
(inv_contin : continuous f_inv)
definition homeomorphic (M N : Type) [topology M] [topology N] :=
Σ (f : M → N), homeomorphism f
structure locally_euclidean [class] (X : Type) extends T : topology X :=
(dim : ℕ)
(homeo : ∀ p : X, Σ (V : set X) (nbhd : @neighborhood _ T p V),
@homeomorphic X (R dim) T _)
structure manifold [class] (X : Type)
extends locally_euclidean X, T2_space X
structure atlas [class] (X : Type) [M : manifold X] :=
(I : Type)
(U : I → set X)
(chart : Π (α : I) x, x ∈ U α → R (manifold.dim X))
--(homeo : Π (α : I), homeomorphism (chart α))
(covers : (⋃ (α : I), U α) = univ)
/-definition transition_map {X : Type} [manifold X] [A : atlas X]
(α β : atlas.I) (x : X) (xmem : x ∈ atlas.U α ∩ atlas.U β) :=
(atlas.chart β (atlas.chart α x sorry)⁻¹ sorry)-/
definition Rn_T2 [instance] {n : ℕ} : T2_space (R n) := sorry
theorem id_contin {X : Type} [T : topology X] : continuous (@id X) :=
assume (x : X) (U : set X) (fxU : id x ∈ U) (U_open : Open U),
exists.intro U (and.intro fxU (and.intro U_open sorry))
noncomputable definition Rn_homeo_Rn (n : ℕ) : homeomorphic (R n) (R n) :=
sigma.mk id
(homeomorphism.mk bijective_id id_contin id (λ x, !left_id) id_contin)
definition Rn_manifold [instance] {n : ℕ} : manifold (R n) :=
sorry
definition e_basis {n} (i : fin n) : R n := λ j, δ i j
noncomputable definition differentiable [class] {n m : ℕ}
(f : R n → R m) (a : R n) :=
∃ (Df : R n → R m) (ilm : is_linear_map ℝ Df),
(λ h, ∥f (a + h) - f a - Df h∥ / ∥h∥) ⟶ 0 at 0
definition D {n m : ℕ} (f : R n → R m) (a : R n) : R m := sorry
theorem chain_rule [instance] {n m p : ℕ} (a : R n)
(f : R n → R m) [differentiable f a]
(g : R m → R p) [differentiable g (f a)] :
differentiable (g ∘ f) a := sorry
theorem d_const {n m : ℕ} {y : R m} : ∀ a, D (λ x : R n, y) a = (λ i, 0) :=
sorry
theorem d_linear {n m : ℕ} (f : R n → R m) [is_linear_map ℝ f] :
∀ a, D f a = f a :=
sorry
definition R_to_R1 [coercion] (x : ℝ) : R 1 := sorry
definition R1_to_R (x : R 1) : ℝ := sorry
definition d_component1 [instance] {n m : ℕ} (f : R n → R m) (a : R n)
[differentiable f a] (i : fin m) : differentiable (λ x, f x i) a :=
sorry
definition d_component2 [instance] {n m : ℕ} (f : vector (R n → ℝ) m)
(a : R n) (i : fin m) [differentiable (f i) a] :
differentiable (λ x j, f j x) a :=
sorry
definition d_component3 {n m : ℕ} (f : R n → R m) (a : R n)
[differentiable f a] :
D f a = (λ i, R1_to_R (D (λ x, f x i) a)) :=
-- D f a ==(λ i, D (λ x, f x i) a) :=
sorry
definition d_add' :
D (λ x : R 2, ∑ i ← upto 2, x i) = (λ x : R 2, ∑ i ← upto 2, x i) :=
sorry
/-definition d_mul' (a : R 2) :
D (λ x : R 2, ∏ i ← upto 2, x i) a ==
(λ x : R 2, (a (fin.mk 1 (by blast))) * (x (fin.mk 0 (by blast))) + (a (fin.mk 0 (by blast))) * (x (fin.mk 1 (by blast)))) a :=
sorry-/
definition d_add_diff [instance] {n : ℕ} (f g : R n → ℝ) (a : R n)
[differentiable f a] [differentiable g a] :
differentiable (f + g) a :=
sorry
definition d_add_mul [instance] {n : ℕ} (f g : R n → ℝ) (a : R n)
[differentiable f a] [differentiable g a] :
differentiable (f * g) a :=
sorry
definition d_add {n : ℕ} (f g : R n → ℝ) (a : R n)
[differentiable f a] [differentiable g a] :
D (f + g) a = D f a + D g a :=
sorry
definition d_mul {n : ℕ} (f g : R n → ℝ) (a : R n)
[differentiable f a] [differentiable g a] :
D (f * g) a = (g a) * D f a + (f a) * D g a :=
sorry
--definition d_div {n : ℕ} (f g : R n → ℝ) (a : R n)
attribute [simp] d_add d_mul
/-definition differentiable' {M : Type} [manifold M]
(f : M → ℝ) (p : M) := sorry-/
/-∀ (α : atlas.I M) (memp : p ∈ (atlas.U α)),
differentiable (f ∘ (λ p' memp', atlas.chart α p')) (λ p, memp)-/
|
1e31434a52c37c2f574677ba0e304f995ff24b68 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/order/priestley.lean | 8cbdc517584256a7bc9258eab83683be207571be | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 2,794 | lean | /-
Copyright (c) 2022 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import order.upper_lower.basic
import topology.separation
/-!
# Priestley spaces
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines Priestley spaces. A Priestley space is an ordered compact topological space such
that any two distinct points can be separated by a clopen upper set.
## Main declarations
* `priestley_space`: Prop-valued mixin stating the Priestley separation axiom: Any two distinct
points can be separated by a clopen upper set.
## Implementation notes
We do not include compactness in the definition, so a Priestley space is to be declared as follows:
`[preorder α] [topological_space α] [compact_space α] [priestley_space α]`
## References
* [Wikipedia, *Priestley space*](https://en.wikipedia.org/wiki/Priestley_space)
* [Davey, Priestley *Introduction to Lattices and Order*][davey_priestley]
-/
open set
variables {α : Type*}
/-- A Priestley space is an ordered topological space such that any two distinct points can be
separated by a clopen upper set. Compactness is often assumed, but we do not include it here. -/
class priestley_space (α : Type*) [preorder α] [topological_space α] :=
(priestley {x y : α} : ¬ x ≤ y → ∃ U : set α, is_clopen U ∧ is_upper_set U ∧ x ∈ U ∧ y ∉ U)
variables [topological_space α]
section preorder
variables [preorder α] [priestley_space α] {x y : α}
lemma exists_clopen_upper_of_not_le :
¬ x ≤ y → ∃ U : set α, is_clopen U ∧ is_upper_set U ∧ x ∈ U ∧ y ∉ U :=
priestley_space.priestley
lemma exists_clopen_lower_of_not_le (h : ¬ x ≤ y) :
∃ U : set α, is_clopen U ∧ is_lower_set U ∧ x ∉ U ∧ y ∈ U :=
let ⟨U, hU, hU', hx, hy⟩ := exists_clopen_upper_of_not_le h in
⟨Uᶜ, hU.compl, hU'.compl, not_not.2 hx, hy⟩
end preorder
section partial_order
variables [partial_order α] [priestley_space α] {x y : α}
lemma exists_clopen_upper_or_lower_of_ne (h : x ≠ y) :
∃ U : set α, is_clopen U ∧ (is_upper_set U ∨ is_lower_set U) ∧ x ∈ U ∧ y ∉ U :=
begin
obtain (h | h) := h.not_le_or_not_le,
{ exact (exists_clopen_upper_of_not_le h).imp (λ U, and.imp_right $ and.imp_left or.inl) },
{ obtain ⟨U, hU, hU', hy, hx⟩ := exists_clopen_lower_of_not_le h,
exact ⟨U, hU, or.inr hU', hx, hy⟩ }
end
@[priority 100] -- See note [lower instance priority]
instance priestley_space.to_t2_space : t2_space α :=
⟨λ x y h, let ⟨U, hU, _, hx, hy⟩ := exists_clopen_upper_or_lower_of_ne h in
⟨U, Uᶜ, hU.is_open, hU.compl.is_open, hx, hy, disjoint_compl_right⟩⟩
end partial_order
|
d0ae6802b6f87b79a9da702d0accbf3b15367b5b | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/elabissues/patternIssue.lean | 9ad88efeffc7f37f89e0ce851f47c86490895b3e | [
"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 | 214 | lean | inductive Moo : Type
| M1 : Moo
| M2 : Moo
inductive Foo : Type
| mk₁ : Moo → Foo
| mk₂ : Foo
def bar : Foo → String
| Foo.mk₁ M1 => "mk₁ M1"
| _ => "else"
#eval bar (Foo.mk₁ Moo.M2) -- "mk₁ M1"
|
92d925a8ff6028ed0eb10e8146948eb9ab95a923 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/meta/widget/html_cmd.lean | b5d54efd2ccffc3182744529757f9adab8398a83 | [] | 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 | 482 | lean | /-
Copyright (c) E.W.Ayers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: E.W.Ayers
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.meta.widget.basic
import Mathlib.Lean3Lib.init.meta.lean.parser
import Mathlib.Lean3Lib.init.meta.interactive_base
import Mathlib.Lean3Lib.init.data.punit
namespace Mathlib
/-- Accepts terms with the type `component tactic_state empty` or `html empty` and
renders them interactively. -/
|
9ab26a0dc0666df77d9cf55ce2f3c4a95bec87df | 8e2026ac8a0660b5a490dfb895599fb445bb77a0 | /library/data/vector.lean | 7fe2f6614b3f58663edc235b90204ac7cbfa5ef6 | [
"Apache-2.0"
] | permissive | pcmoritz/lean | 6a8575115a724af933678d829b4f791a0cb55beb | 35eba0107e4cc8a52778259bb5392300267bfc29 | refs/heads/master | 1,607,896,326,092 | 1,490,752,175,000 | 1,490,752,175,000 | 86,612,290 | 0 | 0 | null | 1,490,809,641,000 | 1,490,809,641,000 | null | UTF-8 | Lean | false | false | 3,689 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
Tuples are lists of a fixed size.
It is implemented as a subtype.
-/
import data.list
universes u v w
def vector (α : Type u) (n : ℕ) := { l : list α // l.length = n }
namespace vector
variables {α : Type u} {β : Type v} {φ : Type w}
variable {n : ℕ}
instance [decidable_eq α] : decidable_eq (vector α n) :=
begin unfold vector, apply_instance end
@[pattern] def nil : vector α 0 := ⟨[], rfl⟩
@[pattern] def cons : α → vector α n → vector α (nat.succ n)
| a ⟨ v, h ⟩ := ⟨ a::v, congr_arg nat.succ h ⟩
@[reducible] def length (v : vector α n) : ℕ := n
notation a :: b := cons a b
notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l
open nat
def head : vector α (nat.succ n) → α
| ⟨ [], h ⟩ := by contradiction
| ⟨ a :: v, h ⟩ := a
theorem head_cons (a : α) : Π (v : vector α n), head (a :: v) = a
| ⟨ l, h ⟩ := rfl
def tail : vector α (succ n) → vector α n
| ⟨ [], h ⟩ := by contradiction
| ⟨ a :: v, h ⟩ := ⟨ v, congr_arg pred h ⟩
theorem tail_cons (a : α) : Π (v : vector α n), tail (a :: v) = v
| ⟨ l, h ⟩ := rfl
def to_list : vector α n → list α | ⟨ l, h ⟩ := l
def append {n m : nat} : vector α n → vector α m → vector α (n + m)
| ⟨ l₁, h₁ ⟩ ⟨ l₂, h₂ ⟩ := ⟨ l₁ ++ l₂, by simp_using_hs ⟩
/- map -/
def map (f : α → β) : vector α n → vector β n
| ⟨ l, h ⟩ := ⟨ list.map f l, by simp_using_hs ⟩
@[simp] lemma map_nil (f : α → β) : map f nil = nil := rfl
lemma map_cons (f : α → β) (a : α) : Π (v : vector α n), map f (a::v) = f a :: map f v
| ⟨l,h⟩ := rfl
def map₂ (f : α → β → φ) : vector α n → vector β n → vector φ n
| ⟨ x, _ ⟩ ⟨ y, _ ⟩ := ⟨ list.map₂ f x y, by simp_using_hs ⟩
def repeat (a : α) (n : ℕ) : vector α n :=
⟨ list.repeat a n, list.length_repeat a n ⟩
def dropn (i : ℕ) : vector α n → vector α (n - i)
| ⟨l, p⟩ := ⟨ list.dropn i l, by simp_using_hs ⟩
def taken (i : ℕ) : vector α n → vector α (min i n)
| ⟨l, p⟩ := ⟨ list.taken i l, by simp_using_hs ⟩
section accum
open prod
variable {σ : Type}
def map_accumr (f : α → σ → σ × β) : vector α n → σ → σ × vector β n
| ⟨ x, px ⟩ c :=
let res := list.map_accumr f x c in
⟨ res.1, res.2, by simp_using_hs ⟩
def map_accumr₂ {α β σ φ : Type} (f : α → β → σ → σ × φ)
: vector α n → vector β n → σ → σ × vector φ n
| ⟨ x, px ⟩ ⟨ y, py ⟩ c :=
let res := list.map_accumr₂ f x y c in
⟨ res.1, res.2, by simp_using_hs ⟩
end accum
protected lemma eq {n : ℕ} : ∀ (a1 a2 : vector α n), to_list a1 = to_list a2 → a1 = a2
| ⟨x, h1⟩ ⟨._, h2⟩ rfl := rfl
@[simp] lemma to_list_mk (v : list α) (P : list.length v = n) : to_list (subtype.mk v P) = v :=
rfl
@[simp] lemma to_list_nil : to_list [] = @list.nil α :=
rfl
@[simp] lemma to_list_cons (a : α) (v : vector α n) : to_list (a :: v) = a :: to_list v :=
begin cases v, reflexivity end
@[simp] lemma to_list_append {n m : nat} (v : vector α n) (w : vector α m) : to_list (append v w) = to_list v ++ to_list w :=
begin cases v, cases w, reflexivity end
@[simp] lemma to_list_dropn {n m : ℕ} (v : vector α m) : to_list (dropn n v) = list.dropn n (to_list v) :=
begin cases v, reflexivity end
@[simp] lemma to_list_taken {n m : ℕ} (v : vector α m) : to_list (taken n v) = list.taken n (to_list v) :=
begin cases v, reflexivity end
end vector
|
9f3d81b09f00308f7d877df573b007e7cea1c166 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/analysis/inner_product_space/calculus.lean | 9f95e4b6a1992add2eb84f9520ce5763fafd59fb | [
"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 | 15,342 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import analysis.inner_product_space.pi_L2
import analysis.special_functions.sqrt
/-!
# Calculus in inner product spaces
In this file we prove that the inner product and square of the norm in an inner space are
infinitely `ℝ`-smooth. In order to state these results, we need a `normed_space ℝ E`
instance. Though we can deduce this structure from `inner_product_space 𝕜 E`, this instance may be
not definitionally equal to some other “natural” instance. So, we assume `[normed_space ℝ E]`.
We also prove that functions to a `euclidean_space` are (higher) differentiable if and only if
their components are. This follows from the corresponding fact for finite product of normed spaces,
and from the equivalence of norms in finite dimensions.
## TODO
The last part of the file should be generalized to `pi_Lp`.
-/
noncomputable theory
open is_R_or_C real filter
open_locale big_operators classical topological_space
section deriv_inner
variables {𝕜 E F : Type*} [is_R_or_C 𝕜]
variables [inner_product_space 𝕜 E] [inner_product_space ℝ F]
local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y
variables [normed_space ℝ E]
/-- Derivative of the inner product. -/
def fderiv_inner_clm (p : E × E) : E × E →L[ℝ] 𝕜 := is_bounded_bilinear_map_inner.deriv p
@[simp] lemma fderiv_inner_clm_apply (p x : E × E) :
fderiv_inner_clm p x = ⟪p.1, x.2⟫ + ⟪x.1, p.2⟫ := rfl
lemma cont_diff_inner {n} : cont_diff ℝ n (λ p : E × E, ⟪p.1, p.2⟫) :=
is_bounded_bilinear_map_inner.cont_diff
lemma cont_diff_at_inner {p : E × E} {n} :
cont_diff_at ℝ n (λ p : E × E, ⟪p.1, p.2⟫) p :=
cont_diff_inner.cont_diff_at
lemma differentiable_inner : differentiable ℝ (λ p : E × E, ⟪p.1, p.2⟫) :=
is_bounded_bilinear_map_inner.differentiable_at
variables {G : Type*} [normed_add_comm_group G] [normed_space ℝ G]
{f g : G → E} {f' g' : G →L[ℝ] E} {s : set G} {x : G} {n : ℕ∞}
include 𝕜
lemma cont_diff_within_at.inner (hf : cont_diff_within_at ℝ n f s x)
(hg : cont_diff_within_at ℝ n g s x) :
cont_diff_within_at ℝ n (λ x, ⟪f x, g x⟫) s x :=
cont_diff_at_inner.comp_cont_diff_within_at x (hf.prod hg)
lemma cont_diff_at.inner (hf : cont_diff_at ℝ n f x)
(hg : cont_diff_at ℝ n g x) :
cont_diff_at ℝ n (λ x, ⟪f x, g x⟫) x :=
hf.inner hg
lemma cont_diff_on.inner (hf : cont_diff_on ℝ n f s) (hg : cont_diff_on ℝ n g s) :
cont_diff_on ℝ n (λ x, ⟪f x, g x⟫) s :=
λ x hx, (hf x hx).inner (hg x hx)
lemma cont_diff.inner (hf : cont_diff ℝ n f) (hg : cont_diff ℝ n g) :
cont_diff ℝ n (λ x, ⟪f x, g x⟫) :=
cont_diff_inner.comp (hf.prod hg)
lemma has_fderiv_within_at.inner (hf : has_fderiv_within_at f f' s x)
(hg : has_fderiv_within_at g g' s x) :
has_fderiv_within_at (λ t, ⟪f t, g t⟫) ((fderiv_inner_clm (f x, g x)).comp $ f'.prod g') s x :=
(is_bounded_bilinear_map_inner.has_fderiv_at (f x, g x)).comp_has_fderiv_within_at x (hf.prod hg)
lemma has_strict_fderiv_at.inner (hf : has_strict_fderiv_at f f' x)
(hg : has_strict_fderiv_at g g' x) :
has_strict_fderiv_at (λ t, ⟪f t, g t⟫) ((fderiv_inner_clm (f x, g x)).comp $ f'.prod g') x :=
(is_bounded_bilinear_map_inner.has_strict_fderiv_at (f x, g x)).comp x (hf.prod hg)
lemma has_fderiv_at.inner (hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) :
has_fderiv_at (λ t, ⟪f t, g t⟫) ((fderiv_inner_clm (f x, g x)).comp $ f'.prod g') x :=
(is_bounded_bilinear_map_inner.has_fderiv_at (f x, g x)).comp x (hf.prod hg)
lemma has_deriv_within_at.inner {f g : ℝ → E} {f' g' : E} {s : set ℝ} {x : ℝ}
(hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) :
has_deriv_within_at (λ t, ⟪f t, g t⟫) (⟪f x, g'⟫ + ⟪f', g x⟫) s x :=
by simpa using (hf.has_fderiv_within_at.inner hg.has_fderiv_within_at).has_deriv_within_at
lemma has_deriv_at.inner {f g : ℝ → E} {f' g' : E} {x : ℝ} :
has_deriv_at f f' x → has_deriv_at g g' x →
has_deriv_at (λ t, ⟪f t, g t⟫) (⟪f x, g'⟫ + ⟪f', g x⟫) x :=
by simpa only [← has_deriv_within_at_univ] using has_deriv_within_at.inner
lemma differentiable_within_at.inner (hf : differentiable_within_at ℝ f s x)
(hg : differentiable_within_at ℝ g s x) :
differentiable_within_at ℝ (λ x, ⟪f x, g x⟫) s x :=
((differentiable_inner _).has_fderiv_at.comp_has_fderiv_within_at x
(hf.prod hg).has_fderiv_within_at).differentiable_within_at
lemma differentiable_at.inner (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) :
differentiable_at ℝ (λ x, ⟪f x, g x⟫) x :=
(differentiable_inner _).comp x (hf.prod hg)
lemma differentiable_on.inner (hf : differentiable_on ℝ f s) (hg : differentiable_on ℝ g s) :
differentiable_on ℝ (λ x, ⟪f x, g x⟫) s :=
λ x hx, (hf x hx).inner (hg x hx)
lemma differentiable.inner (hf : differentiable ℝ f) (hg : differentiable ℝ g) :
differentiable ℝ (λ x, ⟪f x, g x⟫) :=
λ x, (hf x).inner (hg x)
lemma fderiv_inner_apply (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x) (y : G) :
fderiv ℝ (λ t, ⟪f t, g t⟫) x y = ⟪f x, fderiv ℝ g x y⟫ + ⟪fderiv ℝ f x y, g x⟫ :=
by { rw [(hf.has_fderiv_at.inner hg.has_fderiv_at).fderiv], refl }
lemma deriv_inner_apply {f g : ℝ → E} {x : ℝ} (hf : differentiable_at ℝ f x)
(hg : differentiable_at ℝ g x) :
deriv (λ t, ⟪f t, g t⟫) x = ⟪f x, deriv g x⟫ + ⟪deriv f x, g x⟫ :=
(hf.has_deriv_at.inner hg.has_deriv_at).deriv
lemma cont_diff_norm_sq : cont_diff ℝ n (λ x : E, ∥x∥ ^ 2) :=
begin
simp only [sq, ← inner_self_eq_norm_mul_norm],
exact (re_clm : 𝕜 →L[ℝ] ℝ).cont_diff.comp (cont_diff_id.inner cont_diff_id)
end
lemma cont_diff.norm_sq (hf : cont_diff ℝ n f) :
cont_diff ℝ n (λ x, ∥f x∥ ^ 2) :=
cont_diff_norm_sq.comp hf
lemma cont_diff_within_at.norm_sq (hf : cont_diff_within_at ℝ n f s x) :
cont_diff_within_at ℝ n (λ y, ∥f y∥ ^ 2) s x :=
cont_diff_norm_sq.cont_diff_at.comp_cont_diff_within_at x hf
lemma cont_diff_at.norm_sq (hf : cont_diff_at ℝ n f x) :
cont_diff_at ℝ n (λ y, ∥f y∥ ^ 2) x :=
hf.norm_sq
lemma cont_diff_at_norm {x : E} (hx : x ≠ 0) : cont_diff_at ℝ n norm x :=
have ∥id x∥ ^ 2 ≠ 0, from pow_ne_zero _ (norm_pos_iff.2 hx).ne',
by simpa only [id, sqrt_sq, norm_nonneg] using cont_diff_at_id.norm_sq.sqrt this
lemma cont_diff_at.norm (hf : cont_diff_at ℝ n f x) (h0 : f x ≠ 0) :
cont_diff_at ℝ n (λ y, ∥f y∥) x :=
(cont_diff_at_norm h0).comp x hf
lemma cont_diff_at.dist (hf : cont_diff_at ℝ n f x) (hg : cont_diff_at ℝ n g x)
(hne : f x ≠ g x) :
cont_diff_at ℝ n (λ y, dist (f y) (g y)) x :=
by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) }
lemma cont_diff_within_at.norm (hf : cont_diff_within_at ℝ n f s x) (h0 : f x ≠ 0) :
cont_diff_within_at ℝ n (λ y, ∥f y∥) s x :=
(cont_diff_at_norm h0).comp_cont_diff_within_at x hf
lemma cont_diff_within_at.dist (hf : cont_diff_within_at ℝ n f s x)
(hg : cont_diff_within_at ℝ n g s x) (hne : f x ≠ g x) :
cont_diff_within_at ℝ n (λ y, dist (f y) (g y)) s x :=
by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) }
lemma cont_diff_on.norm_sq (hf : cont_diff_on ℝ n f s) :
cont_diff_on ℝ n (λ y, ∥f y∥ ^ 2) s :=
(λ x hx, (hf x hx).norm_sq)
lemma cont_diff_on.norm (hf : cont_diff_on ℝ n f s) (h0 : ∀ x ∈ s, f x ≠ 0) :
cont_diff_on ℝ n (λ y, ∥f y∥) s :=
λ x hx, (hf x hx).norm (h0 x hx)
lemma cont_diff_on.dist (hf : cont_diff_on ℝ n f s)
(hg : cont_diff_on ℝ n g s) (hne : ∀ x ∈ s, f x ≠ g x) :
cont_diff_on ℝ n (λ y, dist (f y) (g y)) s :=
λ x hx, (hf x hx).dist (hg x hx) (hne x hx)
lemma cont_diff.norm (hf : cont_diff ℝ n f) (h0 : ∀ x, f x ≠ 0) :
cont_diff ℝ n (λ y, ∥f y∥) :=
cont_diff_iff_cont_diff_at.2 $ λ x, hf.cont_diff_at.norm (h0 x)
lemma cont_diff.dist (hf : cont_diff ℝ n f) (hg : cont_diff ℝ n g)
(hne : ∀ x, f x ≠ g x) :
cont_diff ℝ n (λ y, dist (f y) (g y)) :=
cont_diff_iff_cont_diff_at.2 $
λ x, hf.cont_diff_at.dist hg.cont_diff_at (hne x)
omit 𝕜
lemma has_strict_fderiv_at_norm_sq (x : F) :
has_strict_fderiv_at (λ x, ∥x∥ ^ 2) (bit0 (innerSL x : F →L[ℝ] ℝ)) x :=
begin
simp only [sq, ← inner_self_eq_norm_mul_norm],
convert (has_strict_fderiv_at_id x).inner (has_strict_fderiv_at_id x),
ext y,
simp [bit0, real_inner_comm],
end
include 𝕜
lemma differentiable_at.norm_sq (hf : differentiable_at ℝ f x) :
differentiable_at ℝ (λ y, ∥f y∥ ^ 2) x :=
(cont_diff_at_id.norm_sq.differentiable_at le_rfl).comp x hf
lemma differentiable_at.norm (hf : differentiable_at ℝ f x) (h0 : f x ≠ 0) :
differentiable_at ℝ (λ y, ∥f y∥) x :=
((cont_diff_at_norm h0).differentiable_at le_rfl).comp x hf
lemma differentiable_at.dist (hf : differentiable_at ℝ f x) (hg : differentiable_at ℝ g x)
(hne : f x ≠ g x) :
differentiable_at ℝ (λ y, dist (f y) (g y)) x :=
by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) }
lemma differentiable.norm_sq (hf : differentiable ℝ f) : differentiable ℝ (λ y, ∥f y∥ ^ 2) :=
λ x, (hf x).norm_sq
lemma differentiable.norm (hf : differentiable ℝ f) (h0 : ∀ x, f x ≠ 0) :
differentiable ℝ (λ y, ∥f y∥) :=
λ x, (hf x).norm (h0 x)
lemma differentiable.dist (hf : differentiable ℝ f) (hg : differentiable ℝ g)
(hne : ∀ x, f x ≠ g x) :
differentiable ℝ (λ y, dist (f y) (g y)) :=
λ x, (hf x).dist (hg x) (hne x)
lemma differentiable_within_at.norm_sq (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ y, ∥f y∥ ^ 2) s x :=
(cont_diff_at_id.norm_sq.differentiable_at le_rfl).comp_differentiable_within_at x hf
lemma differentiable_within_at.norm (hf : differentiable_within_at ℝ f s x) (h0 : f x ≠ 0) :
differentiable_within_at ℝ (λ y, ∥f y∥) s x :=
((cont_diff_at_id.norm h0).differentiable_at le_rfl).comp_differentiable_within_at x hf
lemma differentiable_within_at.dist (hf : differentiable_within_at ℝ f s x)
(hg : differentiable_within_at ℝ g s x) (hne : f x ≠ g x) :
differentiable_within_at ℝ (λ y, dist (f y) (g y)) s x :=
by { simp only [dist_eq_norm], exact (hf.sub hg).norm (sub_ne_zero.2 hne) }
lemma differentiable_on.norm_sq (hf : differentiable_on ℝ f s) :
differentiable_on ℝ (λ y, ∥f y∥ ^ 2) s :=
λ x hx, (hf x hx).norm_sq
lemma differentiable_on.norm (hf : differentiable_on ℝ f s) (h0 : ∀ x ∈ s, f x ≠ 0) :
differentiable_on ℝ (λ y, ∥f y∥) s :=
λ x hx, (hf x hx).norm (h0 x hx)
lemma differentiable_on.dist (hf : differentiable_on ℝ f s) (hg : differentiable_on ℝ g s)
(hne : ∀ x ∈ s, f x ≠ g x) :
differentiable_on ℝ (λ y, dist (f y) (g y)) s :=
λ x hx, (hf x hx).dist (hg x hx) (hne x hx)
end deriv_inner
section pi_like
open continuous_linear_map
variables {𝕜 ι H : Type*} [is_R_or_C 𝕜] [normed_add_comm_group H] [normed_space 𝕜 H]
[fintype ι] {f : H → euclidean_space 𝕜 ι} {f' : H →L[𝕜] euclidean_space 𝕜 ι} {t : set H} {y : H}
lemma differentiable_within_at_euclidean :
differentiable_within_at 𝕜 f t y ↔ ∀ i, differentiable_within_at 𝕜 (λ x, f x i) t y :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_differentiable_within_at_iff, differentiable_within_at_pi],
refl
end
lemma differentiable_at_euclidean :
differentiable_at 𝕜 f y ↔ ∀ i, differentiable_at 𝕜 (λ x, f x i) y :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_differentiable_at_iff, differentiable_at_pi],
refl
end
lemma differentiable_on_euclidean :
differentiable_on 𝕜 f t ↔ ∀ i, differentiable_on 𝕜 (λ x, f x i) t :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_differentiable_on_iff, differentiable_on_pi],
refl
end
lemma differentiable_euclidean :
differentiable 𝕜 f ↔ ∀ i, differentiable 𝕜 (λ x, f x i) :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_differentiable_iff, differentiable_pi],
refl
end
lemma has_strict_fderiv_at_euclidean :
has_strict_fderiv_at f f' y ↔ ∀ i, has_strict_fderiv_at (λ x, f x i)
(euclidean_space.proj i ∘L f') y :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_has_strict_fderiv_at_iff, has_strict_fderiv_at_pi'],
refl
end
lemma has_fderiv_within_at_euclidean :
has_fderiv_within_at f f' t y ↔ ∀ i, has_fderiv_within_at (λ x, f x i)
(euclidean_space.proj i ∘L f') t y :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_has_fderiv_within_at_iff, has_fderiv_within_at_pi'],
refl
end
lemma cont_diff_within_at_euclidean {n : ℕ∞} :
cont_diff_within_at 𝕜 n f t y ↔ ∀ i, cont_diff_within_at 𝕜 n (λ x, f x i) t y :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_cont_diff_within_at_iff, cont_diff_within_at_pi],
refl
end
lemma cont_diff_at_euclidean {n : ℕ∞} :
cont_diff_at 𝕜 n f y ↔ ∀ i, cont_diff_at 𝕜 n (λ x, f x i) y :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_cont_diff_at_iff, cont_diff_at_pi],
refl
end
lemma cont_diff_on_euclidean {n : ℕ∞} :
cont_diff_on 𝕜 n f t ↔ ∀ i, cont_diff_on 𝕜 n (λ x, f x i) t :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_cont_diff_on_iff, cont_diff_on_pi],
refl
end
lemma cont_diff_euclidean {n : ℕ∞} :
cont_diff 𝕜 n f ↔ ∀ i, cont_diff 𝕜 n (λ x, f x i) :=
begin
rw [← (euclidean_space.equiv ι 𝕜).comp_cont_diff_iff, cont_diff_pi],
refl
end
end pi_like
section diffeomorph_unit_ball
open metric (hiding mem_nhds_iff)
variables {n : ℕ∞} {E : Type*} [inner_product_space ℝ E]
lemma cont_diff_homeomorph_unit_ball :
cont_diff ℝ n $ λ (x : E), (homeomorph_unit_ball x : E) :=
begin
suffices : cont_diff ℝ n (λ x, (1 + ∥x∥^2).sqrt⁻¹), { exact this.smul cont_diff_id, },
have h : ∀ (x : E), 0 < 1 + ∥x∥ ^ 2 := λ x, by positivity,
refine cont_diff.inv _ (λ x, real.sqrt_ne_zero'.mpr (h x)),
exact (cont_diff_const.add cont_diff_norm_sq).sqrt (λ x, (h x).ne.symm),
end
lemma cont_diff_on_homeomorph_unit_ball_symm
{f : E → E} (h : ∀ y (hy : y ∈ ball (0 : E) 1), f y = homeomorph_unit_ball.symm ⟨y, hy⟩) :
cont_diff_on ℝ n f $ ball 0 1 :=
begin
intros y hy,
apply cont_diff_at.cont_diff_within_at,
have hf : f =ᶠ[𝓝 y] λ y, (1 - ∥(y : E)∥^2).sqrt⁻¹ • (y : E),
{ rw eventually_eq_iff_exists_mem,
refine ⟨ball (0 : E) 1, mem_nhds_iff.mpr ⟨ball (0 : E) 1, set.subset.refl _, is_open_ball, hy⟩,
λ z hz, _⟩,
rw h z hz,
refl, },
refine cont_diff_at.congr_of_eventually_eq _ hf,
suffices : cont_diff_at ℝ n (λy, (1 - ∥(y : E)∥^2).sqrt⁻¹) y, { exact this.smul cont_diff_at_id },
have h : 0 < 1 - ∥(y : E)∥^2, by rwa [mem_ball_zero_iff, ← _root_.abs_one, ← abs_norm_eq_norm,
← sq_lt_sq, one_pow, ← sub_pos] at hy,
refine cont_diff_at.inv _ (real.sqrt_ne_zero'.mpr h),
refine cont_diff_at.comp _ (cont_diff_at_sqrt h.ne.symm) _,
exact cont_diff_at_const.sub cont_diff_norm_sq.cont_diff_at,
end
end diffeomorph_unit_ball
|
ff3053a52ac6fcdddff79b08393876cfd2365250 | de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41 | /old/test/lang_test_Q.lean | 6da6964ee6f044a5f30181829399c63400e445bf | [] | no_license | kevinsullivan/lang | d3e526ba363dc1ddf5ff1c2f36607d7f891806a7 | e9d869bff94fb13ad9262222a6f3c4aafba82d5e | refs/heads/master | 1,687,840,064,795 | 1,628,047,969,000 | 1,628,047,969,000 | 282,210,749 | 0 | 1 | null | 1,608,153,830,000 | 1,595,592,637,000 | Lean | UTF-8 | Lean | false | false | 3,148 | lean | import ..imperative_DSL.physlang
#check 1
/-
uncertain how this should get printed
-/
abbreviation K := ℚ
/-
If we want to include in a different space, whether it be a 2nd time or geom,
problems come up here. Either we share the same space in the environment,
or we add in new space type arguments to env. This does not appear to scale to a user
creating arbitrary frames or spaces.
-/
def env1 : @env.env K _ _ (std_frame K TIME) (std_space K TIME) := env.env.init K
--Bind a time object to variable and in environment
def assign_t1 :=
cmd.assign_time ⟨⟨"t1"⟩⟩ (lang.time.time_expr.lit (mk_time (std_space K TIME) 5 ))
def env2 := cmd_eval K env1 assign_t1
--Bind a time object to variable and in environment
def assign_t2 :=
cmd.assign_time ⟨⟨"t2"⟩⟩ (lang.time.time_expr.var (⟨⟨"t1"⟩⟩ : lang.time.time_var (std_space K TIME)))
def env3 := cmd_eval K env2 assign_t2
--When it is computable, we have access to the coordinates just fine.
#eval (lang.time_eval.time_eval (std_space K TIME) env3.t (lang.time.time_expr.var (⟨⟨"t1"⟩⟩ : lang.time.time_var (std_space K TIME))))
.to_point.to_pt.to_prod
--Bind a duration object by subtracting times (which are evaluated from variables)
def assign_d1 :=
let eval_t1 := (lang.time_eval.time_eval (std_space K TIME) env3.t (lang.time.time_expr.var (⟨⟨"t1"⟩⟩ : lang.time.time_var (std_space K TIME)))) in
let eval_t2 := (lang.time_eval.time_eval (std_space K TIME) env3.t (lang.time.time_expr.var (⟨⟨"t2"⟩⟩ : lang.time.time_var (std_space K TIME)))) in
cmd.assign_duration ⟨⟨"d1"⟩⟩ (lang.time.duration_expr.lit (
eval_t1
-ᵥ
eval_t2
))
def env4 := cmd_eval K env3 assign_d1
def assign_robot_frame :=
let eval_t1 := (lang.time_eval.time_eval (std_space K TIME) env3.t (lang.time.time_expr.var (⟨⟨"t1"⟩⟩ : lang.time.time_var (std_space K TIME)))) in
let eval_d1 := (lang.time_eval.duration_eval (std_space K TIME) env3.t (lang.time.duration_expr.var (⟨⟨"d1"⟩⟩ : lang.time.duration_var (std_space K TIME)))) in
--starts getting ugly - cmd needs some explicit arguments, which hardly make sense
--@cmd.assign_fm K _ _ (std_frame K TIME) (std_space K TIME)
@cmd.assign_fm K _ _ (std_frame K TIME) (std_space K TIME) ⟨⟨"robot"⟩⟩ (lang.math.fm_expr.lit (mk_frame eval_t1.to_point eval_d1.to_vectr))
def env5 := cmd_eval K env4 assign_robot_frame
def assign_robot_space :=
let eval_robot := (lang.math_eval.fm_eval (std_frame K TIME)/-<-what is this doing here-/
env4.m (lang.math.fm_expr.var (⟨⟨"robot"⟩⟩))) in
--@cmd.assign_spc K _ _ (std_frame K TIME) (std_space K TIME) ⟨⟨"robot"⟩⟩ (mk_space K eval_robot)
--odd but functional.
@cmd.assign_spc K _ _ (eval_robot) (mk_space K eval_robot) ⟨⟨"robot"⟩⟩ (lang.math.sp_expr.lit (mk_space K eval_robot))
def env6 := cmd_eval K env5 /-and then...-/ assign_robot_space
/-
This is where things break down because, as mentioned above, environment only supports 1 frame due to the necessary implicit arguments
How to fix this (just at the lang level) is something I don't have a quick answer for.
-/ |
543dac31b5657857b8821cb43f9ed3948d703ce7 | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/algebra/euclidean_domain.lean | 51cbe64d627aafccd70a2264959c2367cc8e335a | [
"Apache-2.0"
] | permissive | jumpy4/mathlib | d3829e75173012833e9f15ac16e481e17596de0f | af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13 | refs/heads/master | 1,693,508,842,818 | 1,636,203,271,000 | 1,636,203,271,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,130 | lean | /-
Copyright (c) 2018 Louis Carlin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Louis Carlin, Mario Carneiro
-/
import data.int.basic
import algebra.field
/-!
# Euclidean domains
This file introduces Euclidean domains and provides the extended Euclidean algorithm. To be precise,
a slightly more general version is provided which is sometimes called a transfinite Euclidean domain
and differs in the fact that the degree function need not take values in `ℕ` but can take values in
any well-ordered set. Transfinite Euclidean domains were introduced by Motzkin and examples which
don't satisfy the classical notion were provided independently by Hiblot and Nagata.
## Main definitions
* `euclidean_domain`: Defines Euclidean domain with functions `quotient` and `remainder`. Instances
of `has_div` and `has_mod` are provided, so that one can write `a = b * (a / b) + a % b`.
* `gcd`: defines the greatest common divisors of two elements of a Euclidean domain.
* `xgcd`: given two elements `a b : R`, `xgcd a b` defines the pair `(x, y)` such that
`x * a + y * b = gcd a b`.
* `lcm`: defines the lowest common multiple of two elements `a` and `b` of a Euclidean domain as
`a * b / (gcd a b)`
## Main statements
* `gcd_eq_gcd_ab`: states Bézout's lemma for Euclidean domains.
* `int.euclidean_domain`: shows that `ℤ` is a Euclidean domain.
* `field.to_euclidean_domain`: shows that any field is a Euclidean domain.
## Notation
`≺` denotes the well founded relation on the Euclidean domain, e.g. in the example of the polynomial
ring over a field, `p ≺ q` for polynomials `p` and `q` if and only if the degree of `p` is less than
the degree of `q`.
## Implementation details
Instead of working with a valuation, `euclidean_domain` is implemented with the existence of a well
founded relation `r` on the integral domain `R`, which in the example of `ℤ` would correspond to
setting `i ≺ j` for integers `i` and `j` if the absolute value of `i` is smaller than the absolute
value of `j`.
## References
* [Th. Motzkin, *The Euclidean algorithm*][MR32592]
* [J.-J. Hiblot, *Des anneaux euclidiens dont le plus petit algorithme n'est pas à valeurs finies*]
[MR399081]
* [M. Nagata, *On Euclid algorithm*][MR541021]
## Tags
Euclidean domain, transfinite Euclidean domain, Bézout's lemma
-/
universe u
/-- A `euclidean_domain` is an non-trivial commutative ring with a division and a remainder,
satisfying `b * (a / b) + a % b = a`.
The definition of a euclidean domain usually includes a valuation function `R → ℕ`.
This definition is slightly generalised to include a well founded relation
`r` with the property that `r (a % b) b`, instead of a valuation. -/
@[protect_proj without mul_left_not_lt r_well_founded]
class euclidean_domain (R : Type u) extends comm_ring R, nontrivial R :=
(quotient : R → R → R)
(quotient_zero : ∀ a, quotient a 0 = 0)
(remainder : R → R → R)
(quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a)
(r : R → R → Prop)
(r_well_founded : well_founded r)
(remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b)
(mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a)
namespace euclidean_domain
variable {R : Type u}
variables [euclidean_domain R]
local infix ` ≺ `:50 := euclidean_domain.r
@[priority 70] -- see Note [lower instance priority]
instance : has_div R := ⟨euclidean_domain.quotient⟩
@[priority 70] -- see Note [lower instance priority]
instance : has_mod R := ⟨euclidean_domain.remainder⟩
theorem div_add_mod (a b : R) : b * (a / b) + a % b = a :=
euclidean_domain.quotient_mul_add_remainder_eq _ _
lemma mod_add_div (a b : R) : a % b + b * (a / b) = a :=
(add_comm _ _).trans (div_add_mod _ _)
lemma mod_add_div' (m k : R) : m % k + (m / k) * k = m :=
by { rw mul_comm, exact mod_add_div _ _ }
lemma div_add_mod' (m k : R) : (m / k) * k + m % k = m :=
by { rw mul_comm, exact div_add_mod _ _ }
lemma mod_eq_sub_mul_div {R : Type*} [euclidean_domain R] (a b : R) :
a % b = a - b * (a / b) :=
calc a % b = b * (a / b) + a % b - b * (a / b) : (add_sub_cancel' _ _).symm
... = a - b * (a / b) : by rw div_add_mod
theorem mod_lt : ∀ a {b : R}, b ≠ 0 → (a % b) ≺ b :=
euclidean_domain.remainder_lt
theorem mul_right_not_lt {a : R} (b) (h : a ≠ 0) : ¬(a * b) ≺ b :=
by { rw mul_comm, exact mul_left_not_lt b h }
lemma mul_div_cancel_left {a : R} (b) (a0 : a ≠ 0) : a * b / a = b :=
eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h,
begin
have := mul_left_not_lt a h,
rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this,
exact this (mod_lt _ a0)
end
lemma mul_div_cancel (a) {b : R} (b0 : b ≠ 0) : a * b / b = a :=
by { rw mul_comm, exact mul_div_cancel_left a b0 }
@[simp] lemma mod_zero (a : R) : a % 0 = a :=
by simpa only [zero_mul, zero_add] using div_add_mod a 0
@[simp] lemma mod_eq_zero {a b : R} : a % b = 0 ↔ b ∣ a :=
⟨λ h, by { rw [← div_add_mod a b, h, add_zero], exact dvd_mul_right _ _ },
λ ⟨c, e⟩, begin
rw [e, ← add_left_cancel_iff, div_add_mod, add_zero],
haveI := classical.dec,
by_cases b0 : b = 0,
{ simp only [b0, zero_mul] },
{ rw [mul_div_cancel_left _ b0] }
end⟩
@[simp] lemma mod_self (a : R) : a % a = 0 :=
mod_eq_zero.2 dvd_rfl
lemma dvd_mod_iff {a b c : R} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a :=
by rw [dvd_add_iff_right (h.mul_right _), div_add_mod]
lemma lt_one (a : R) : a ≺ (1:R) → a = 0 :=
by { haveI := classical.dec, exact
not_imp_not.1 (λ h, by simpa only [one_mul] using mul_left_not_lt 1 h) }
lemma val_dvd_le : ∀ a b : R, b ∣ a → a ≠ 0 → ¬a ≺ b
| _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (mt (by { rintro rfl, exact mul_zero _ }) ha)
@[simp] lemma mod_one (a : R) : a % 1 = 0 :=
mod_eq_zero.2 (one_dvd _)
@[simp] lemma zero_mod (b : R) : 0 % b = 0 :=
mod_eq_zero.2 (dvd_zero _)
@[simp, priority 900] lemma div_zero (a : R) : a / 0 = 0 :=
euclidean_domain.quotient_zero a
@[simp, priority 900] lemma zero_div {a : R} : 0 / a = 0 :=
classical.by_cases
(λ a0 : a = 0, a0.symm ▸ div_zero 0)
(λ a0, by simpa only [zero_mul] using mul_div_cancel 0 a0)
@[simp, priority 900] lemma div_self {a : R} (a0 : a ≠ 0) : a / a = 1 :=
by simpa only [one_mul] using mul_div_cancel 1 a0
lemma eq_div_of_mul_eq_left {a b c : R} (hb : b ≠ 0) (h : a * b = c) : a = c / b :=
by rw [← h, mul_div_cancel _ hb]
lemma eq_div_of_mul_eq_right {a b c : R} (ha : a ≠ 0) (h : a * b = c) : b = c / a :=
by rw [← h, mul_div_cancel_left _ ha]
theorem mul_div_assoc (x : R) {y z : R} (h : z ∣ y) : x * y / z = x * (y / z) :=
begin
classical, by_cases hz : z = 0,
{ subst hz, rw [div_zero, div_zero, mul_zero] },
rcases h with ⟨p, rfl⟩,
rw [mul_div_cancel_left _ hz, mul_left_comm, mul_div_cancel_left _ hz]
end
section
open_locale classical
@[elab_as_eliminator]
theorem gcd.induction {P : R → R → Prop} : ∀ a b : R,
(∀ x, P 0 x) →
(∀ a b, a ≠ 0 → P (b % a) a → P a b) →
P a b
| a := λ b H0 H1, if a0 : a = 0 then a0.symm ▸ H0 _ else
have h:_ := mod_lt b a0,
H1 _ _ a0 (gcd.induction (b%a) a H0 H1)
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]}
end
section gcd
variable [decidable_eq R]
/-- `gcd a b` is a (non-unique) element such that `gcd a b ∣ a` `gcd a b ∣ b`, and for
any element `c` such that `c ∣ a` and `c ∣ b`, then `c ∣ gcd a b` -/
def gcd : R → R → R
| a := λ b, if a0 : a = 0 then b else
have h:_ := mod_lt b a0,
gcd (b%a) a
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]}
@[simp] theorem gcd_zero_left (a : R) : gcd 0 a = a :=
by { rw gcd, exact if_pos rfl }
@[simp] theorem gcd_zero_right (a : R) : gcd a 0 = a :=
by { rw gcd, split_ifs; simp only [h, zero_mod, gcd_zero_left] }
theorem gcd_val (a b : R) : gcd a b = gcd (b % a) a :=
by { rw gcd, split_ifs; [simp only [h, mod_zero, gcd_zero_right], refl]}
theorem gcd_dvd (a b : R) : gcd a b ∣ a ∧ gcd a b ∣ b :=
gcd.induction a b
(λ b, by { rw gcd_zero_left, exact ⟨dvd_zero _, dvd_rfl⟩ })
(λ a b aneq ⟨IH₁, IH₂⟩, by { rw gcd_val,
exact ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩ })
theorem gcd_dvd_left (a b : R) : gcd a b ∣ a := (gcd_dvd a b).left
theorem gcd_dvd_right (a b : R) : gcd a b ∣ b := (gcd_dvd a b).right
protected theorem gcd_eq_zero_iff {a b : R} :
gcd a b = 0 ↔ a = 0 ∧ b = 0 :=
⟨λ h, by simpa [h] using gcd_dvd a b,
by { rintro ⟨rfl, rfl⟩, exact gcd_zero_right _ }⟩
theorem dvd_gcd {a b c : R} : c ∣ a → c ∣ b → c ∣ gcd a b :=
gcd.induction a b
(λ _ _ H, by simpa only [gcd_zero_left] using H)
(λ a b a0 IH ca cb, by { rw gcd_val,
exact IH ((dvd_mod_iff ca).2 cb) ca })
theorem gcd_eq_left {a b : R} : gcd a b = a ↔ a ∣ b :=
⟨λ h, by {rw ← h, apply gcd_dvd_right },
λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩
@[simp] theorem gcd_one_left (a : R) : gcd 1 a = 1 :=
gcd_eq_left.2 (one_dvd _)
@[simp] theorem gcd_self (a : R) : gcd a a = a :=
gcd_eq_left.2 dvd_rfl
/--
An implementation of the extended GCD algorithm.
At each step we are computing a triple `(r, s, t)`, where `r` is the next value of the GCD
algorithm, to compute the greatest common divisor of the input (say `x` and `y`), and `s` and `t`
are the coefficients in front of `x` and `y` to obtain `r` (i.e. `r = s * x + t * y`).
The function `xgcd_aux` takes in two triples, and from these recursively computes the next triple:
```
xgcd_aux (r, s, t) (r', s', t') = xgcd_aux (r' % r, s' - (r' / r) * s, t' - (r' / r) * t) (r, s, t)
```
-/
def xgcd_aux : R → R → R → R → R → R → R × R × R
| r := λ s t r' s' t',
if hr : r = 0 then (r', s', t')
else
have r' % r ≺ r, from mod_lt _ hr,
let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]}
@[simp] theorem xgcd_zero_left {s t r' s' t' : R} : xgcd_aux 0 s t r' s' t' = (r', s', t') :=
by { unfold xgcd_aux, exact if_pos rfl }
theorem xgcd_aux_rec {r s t r' s' t' : R} (h : r ≠ 0) :
xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t :=
by { conv {to_lhs, rw [xgcd_aux]}, exact if_neg h}
/-- Use the extended GCD algorithm to generate the `a` and `b` values
satisfying `gcd x y = x * a + y * b`. -/
def xgcd (x y : R) : R × R := (xgcd_aux x 1 0 y 0 1).2
/-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_a (x y : R) : R := (xgcd x y).1
/-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_b (x y : R) : R := (xgcd x y).2
@[simp] theorem gcd_a_zero_left {s : R} : gcd_a 0 s = 0 :=
by { unfold gcd_a, rw [xgcd, xgcd_zero_left] }
@[simp] theorem gcd_b_zero_left {s : R} : gcd_b 0 s = 1 :=
by { unfold gcd_b, rw [xgcd, xgcd_zero_left] }
@[simp] theorem xgcd_aux_fst (x y : R) : ∀ s t s' t',
(xgcd_aux x s t y s' t').1 = gcd x y :=
gcd.induction x y (by { intros, rw [xgcd_zero_left, gcd_zero_left] })
(λ x y h IH s t s' t', by { simp only [xgcd_aux_rec h, if_neg h, IH], rw ← gcd_val })
theorem xgcd_aux_val (x y : R) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) :=
by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1, prod.mk.eta]
theorem xgcd_val (x y : R) : xgcd x y = (gcd_a x y, gcd_b x y) :=
prod.mk.eta.symm
private def P (a b : R) : R × R × R → Prop | (r, s, t) := (r : R) = a * s + b * t
theorem xgcd_aux_P (a b : R) {r r' : R} : ∀ {s t s' t'}, P a b (r, s, t) →
P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') :=
gcd.induction r r' (by { intros, simpa only [xgcd_zero_left] }) $ λ x y h IH s t s' t' p p', begin
rw [xgcd_aux_rec h], refine IH _ p, unfold P at p p' ⊢,
rw [mul_sub, mul_sub, add_sub, sub_add_eq_add_sub, ← p', sub_sub,
mul_comm _ s, ← mul_assoc, mul_comm _ t, ← mul_assoc, ← add_mul, ← p,
mod_eq_sub_mul_div]
end
/-- An explicit version of **Bézout's lemma** for Euclidean domains. -/
theorem gcd_eq_gcd_ab (a b : R) : (gcd a b : R) = a * gcd_a a b + b * gcd_b a b :=
by { have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1
(by rw [P, mul_one, mul_zero, add_zero]) (by rw [P, mul_one, mul_zero, zero_add]),
rwa [xgcd_aux_val, xgcd_val] at this }
@[priority 70] -- see Note [lower instance priority]
instance (R : Type*) [e : euclidean_domain R] : is_domain R :=
by { haveI := classical.dec_eq R, exact
{ eq_zero_or_eq_zero_of_mul_eq_zero :=
λ a b h, (or_iff_not_and_not.2 $ λ h0,
h0.1 $ by rw [← mul_div_cancel a h0.2, h, zero_div]),
..e }}
end gcd
section lcm
variables [decidable_eq R]
/-- `lcm a b` is a (non-unique) element such that `a ∣ lcm a b` `b ∣ lcm a b`, and for
any element `c` such that `a ∣ c` and `b ∣ c`, then `lcm a b ∣ c` -/
def lcm (x y : R) : R :=
x * y / gcd x y
theorem dvd_lcm_left (x y : R) : x ∣ lcm x y :=
classical.by_cases
(assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ })
(λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).2 in ⟨z, eq.symm $ eq_div_of_mul_eq_left hxy $
by rw [mul_right_comm, mul_assoc, ← hz]⟩)
theorem dvd_lcm_right (x y : R) : y ∣ lcm x y :=
classical.by_cases
(assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ })
(λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).1 in ⟨z, eq.symm $ eq_div_of_mul_eq_right hxy $
by rw [← mul_assoc, mul_right_comm, ← hz]⟩)
theorem lcm_dvd {x y z : R} (hxz : x ∣ z) (hyz : y ∣ z) : lcm x y ∣ z :=
begin
rw lcm, by_cases hxy : gcd x y = 0,
{ rw [hxy, div_zero], rw euclidean_domain.gcd_eq_zero_iff at hxy, rwa hxy.1 at hxz },
rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩,
suffices : x * y ∣ z * gcd x y,
{ cases this with p hp, use p,
generalize_hyp : gcd x y = g at hxy hs hp ⊢, subst hs,
rw [mul_left_comm, mul_div_cancel_left _ hxy, ← mul_left_inj' hxy, hp],
rw [← mul_assoc], simp only [mul_right_comm] },
rw [gcd_eq_gcd_ab, mul_add], apply dvd_add,
{ rw mul_left_comm, exact mul_dvd_mul_left _ (hyz.mul_right _) },
{ rw [mul_left_comm, mul_comm], exact mul_dvd_mul_left _ (hxz.mul_right _) }
end
@[simp] lemma lcm_dvd_iff {x y z : R} : lcm x y ∣ z ↔ x ∣ z ∧ y ∣ z :=
⟨λ hz, ⟨(dvd_lcm_left _ _).trans hz, (dvd_lcm_right _ _).trans hz⟩,
λ ⟨hxz, hyz⟩, lcm_dvd hxz hyz⟩
@[simp] lemma lcm_zero_left (x : R) : lcm 0 x = 0 :=
by rw [lcm, zero_mul, zero_div]
@[simp] lemma lcm_zero_right (x : R) : lcm x 0 = 0 :=
by rw [lcm, mul_zero, zero_div]
@[simp] lemma lcm_eq_zero_iff {x y : R} : lcm x y = 0 ↔ x = 0 ∨ y = 0 :=
begin
split,
{ intro hxy, rw [lcm, mul_div_assoc _ (gcd_dvd_right _ _), mul_eq_zero] at hxy,
apply or_of_or_of_imp_right hxy, intro hy,
by_cases hgxy : gcd x y = 0,
{ rw euclidean_domain.gcd_eq_zero_iff at hgxy, exact hgxy.2 },
{ rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩,
generalize_hyp : gcd x y = g at hr hs hy hgxy ⊢, subst hs,
rw [mul_div_cancel_left _ hgxy] at hy, rw [hy, mul_zero] } },
rintro (hx | hy),
{ rw [hx, lcm_zero_left] },
{ rw [hy, lcm_zero_right] }
end
@[simp] lemma gcd_mul_lcm (x y : R) : gcd x y * lcm x y = x * y :=
begin
rw lcm, by_cases h : gcd x y = 0,
{ rw [h, zero_mul], rw euclidean_domain.gcd_eq_zero_iff at h, rw [h.1, zero_mul] },
rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩,
generalize_hyp : gcd x y = g at h hr ⊢, subst hr,
rw [mul_assoc, mul_div_cancel_left _ h]
end
end lcm
end euclidean_domain
instance int.euclidean_domain : euclidean_domain ℤ :=
{ add := (+),
mul := (*),
one := 1,
zero := 0,
neg := has_neg.neg,
quotient := (/),
quotient_zero := int.div_zero,
remainder := (%),
quotient_mul_add_remainder_eq := λ a b, int.div_add_mod _ _,
r := λ a b, a.nat_abs < b.nat_abs,
r_well_founded := measure_wf (λ a, int.nat_abs a),
remainder_lt := λ a b b0, int.coe_nat_lt.1 $
by { rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), ← int.abs_eq_nat_abs],
exact int.mod_lt _ b0 },
mul_left_not_lt := λ a b b0, not_lt_of_ge $
by {rw [← mul_one a.nat_abs, int.nat_abs_mul],
exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) },
.. int.comm_ring,
.. int.nontrivial }
@[priority 100] -- see Note [lower instance priority]
instance field.to_euclidean_domain {K : Type u} [field K] : euclidean_domain K :=
{ add := (+),
mul := (*),
one := 1,
zero := 0,
neg := has_neg.neg,
quotient := (/),
remainder := λ a b, a - a * b / b,
quotient_zero := div_zero,
quotient_mul_add_remainder_eq := λ a b,
by { classical, by_cases b = 0; simp [h, mul_div_cancel'] },
r := λ a b, a = 0 ∧ b ≠ 0,
r_well_founded := well_founded.intro $ λ a, acc.intro _ $ λ b ⟨hb, hna⟩,
acc.intro _ $ λ c ⟨hc, hnb⟩, false.elim $ hnb hb,
remainder_lt := λ a b hnb, by simp [hnb],
mul_left_not_lt := λ a b hnb ⟨hab, hna⟩, or.cases_on (mul_eq_zero.1 hab) hna hnb,
.. ‹field K› }
|
d5c00e5fbfd5a54325208257dc6604a3888e1ff6 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/lean/run/new_inductive.lean | 11f6e58bb6cd4a5b091cd17a8f4e2e6376af9094 | [
"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 | 1,899 | lean | universes u v
inductive myList (α : Type u)
| nil : myList
| cons : α → myList → myList
inductive myPair (α : Type u) (β : Type v)
| mk : α → β → myPair
mutual inductive bla, boo (α : Type u) (m : α → Type v)
with bla : Nat → Type (max u v)
| mk₁ (n : Nat) : α → boo n → bla (n+1)
| mk₂ (a : α) : m a → String → bla 0
with boo : Nat → Type (max u v)
| mk₃ (n : Nat) : bla n → bla (n+1) → boo (n+2)
#print bla
inductive Term (α : Type) (β : Type)
| var : α → bla Term (fun _ => Term) 10 → Term
| foo (p : Nat → myPair Term (myList Term)) (n : β) : myList (myList Term) → Term
#print Term
#print Term.below
#check @Term.casesOn
#print Term.noConfusionType
#print Term.noConfusion
inductive arrow (α β : Type)
| mk (s : Nat → myPair α β) : arrow
mutual inductive tst1, tst2
with tst1 : Type
| mk : (arrow (myPair tst2 Bool) tst2) → tst1
with tst2 : Type
| mk : tst1 → tst2
#check @tst1.casesOn
#check @tst2.casesOn
#check @tst1.recOn
namespace test
inductive Rbnode (α : Type u)
| leaf {} : Rbnode
| redNode (lchild : Rbnode) (val : α) (rchild : Rbnode) : Rbnode
| blackNode (lchild : Rbnode) (val : α) (rchild : Rbnode) : Rbnode
#check @Rbnode.brecOn
namespace Rbnode
variables {α : Type u}
constant insert (lt : α → α → Prop) [DecidableRel lt] (t : Rbnode α) (x : α) : Rbnode α := Rbnode.leaf
inductive WellFormed (lt : α → α → Prop) : Rbnode α → Prop
| leafWff : WellFormed leaf
| insertWff {n n' : Rbnode α} {x : α} (s : DecidableRel lt) : WellFormed n → n' = insert lt n x → WellFormed n'
end Rbnode
def Rbtree (α : Type u) (lt : α → α → Prop) : Type u :=
{t : Rbnode α // t.WellFormed lt }
inductive Trie
| Empty : Trie
| mk : Char → Rbnode (myPair Char Trie) → Trie
#print Trie.rec
#print Trie.noConfusion
end test
|
3cc85788b16d02944740ea35c72b60c0de2a2f8b | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/calculus/tangent_cone.lean | 10009a260f2712e031e1db684f947da53d6e5ec1 | [
"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 | 20,165 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.convex.topology
import analysis.normed_space.basic
import analysis.specific_limits.basic
/-!
# Tangent cone
In this file, we define two predicates `unique_diff_within_at 𝕜 s x` and `unique_diff_on 𝕜 s`
ensuring that, if a function has two derivatives, then they have to coincide. As a direct
definition of this fact (quantifying on all target types and all functions) would depend on
universes, we use a more intrinsic definition: if all the possible tangent directions to the set
`s` at the point `x` span a dense subset of the whole subset, it is easy to check that the
derivative has to be unique.
Therefore, we introduce the set of all tangent directions, named `tangent_cone_at`,
and express `unique_diff_within_at` and `unique_diff_on` in terms of it.
One should however think of this definition as an implementation detail: the only reason to
introduce the predicates `unique_diff_within_at` and `unique_diff_on` is to ensure the uniqueness
of the derivative. This is why their names reflect their uses, and not how they are defined.
## Implementation details
Note that this file is imported by `fderiv.lean`. Hence, derivatives are not defined yet. The
property of uniqueness of the derivative is therefore proved in `fderiv.lean`, but based on the
properties of the tangent cone we prove here.
-/
variables (𝕜 : Type*) [nontrivially_normed_field 𝕜]
open filter set
open_locale topology
section tangent_cone
variables {E : Type*} [add_comm_monoid E] [module 𝕜 E] [topological_space E]
/-- The set of all tangent directions to the set `s` at the point `x`. -/
def tangent_cone_at (s : set E) (x : E) : set E :=
{y : E | ∃(c : ℕ → 𝕜) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧
(tendsto (λn, ‖c n‖) at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))}
/-- A property ensuring that the tangent cone to `s` at `x` spans a dense subset of the whole space.
The main role of this property is to ensure that the differential within `s` at `x` is unique,
hence this name. The uniqueness it asserts is proved in `unique_diff_within_at.eq` in `fderiv.lean`.
To avoid pathologies in dimension 0, we also require that `x` belongs to the closure of `s` (which
is automatic when `E` is not `0`-dimensional).
-/
@[mk_iff] structure unique_diff_within_at (s : set E) (x : E) : Prop :=
(dense_tangent_cone : dense ((submodule.span 𝕜 (tangent_cone_at 𝕜 s x)) : set E))
(mem_closure : x ∈ closure s)
/-- A property ensuring that the tangent cone to `s` at any of its points spans a dense subset of
the whole space. The main role of this property is to ensure that the differential along `s` is
unique, hence this name. The uniqueness it asserts is proved in `unique_diff_on.eq` in
`fderiv.lean`. -/
def unique_diff_on (s : set E) : Prop :=
∀x ∈ s, unique_diff_within_at 𝕜 s x
end tangent_cone
variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F]
variables {G : Type*} [normed_add_comm_group G] [normed_space ℝ G]
variables {𝕜} {x y : E} {s t : set E}
section tangent_cone
/- This section is devoted to the properties of the tangent cone. -/
open normed_field
lemma tangent_cone_univ : tangent_cone_at 𝕜 univ x = univ :=
begin
refine univ_subset_iff.1 (λy hy, _),
rcases exists_one_lt_norm 𝕜 with ⟨w, hw⟩,
refine ⟨λn, w^n, λn, (w^n)⁻¹ • y, univ_mem' (λn, mem_univ _), _, _⟩,
{ simp only [norm_pow],
exact tendsto_pow_at_top_at_top_of_one_lt hw },
{ convert tendsto_const_nhds,
ext n,
have : w ^ n * (w ^ n)⁻¹ = 1,
{ apply mul_inv_cancel,
apply pow_ne_zero,
simpa [norm_eq_zero] using (ne_of_lt (lt_trans zero_lt_one hw)).symm },
rw [smul_smul, this, one_smul] }
end
lemma tangent_cone_mono (h : s ⊆ t) :
tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x :=
begin
rintros y ⟨c, d, ds, ctop, clim⟩,
exact ⟨c, d, mem_of_superset ds (λn hn, h hn), ctop, clim⟩
end
/-- Auxiliary lemma ensuring that, under the assumptions defining the tangent cone,
the sequence `d` tends to 0 at infinity. -/
lemma tangent_cone_at.lim_zero {α : Type*} (l : filter α) {c : α → 𝕜} {d : α → E}
(hc : tendsto (λn, ‖c n‖) l at_top) (hd : tendsto (λn, c n • d n) l (𝓝 y)) :
tendsto d l (𝓝 0) :=
begin
have A : tendsto (λn, ‖c n‖⁻¹) l (𝓝 0) := tendsto_inv_at_top_zero.comp hc,
have B : tendsto (λn, ‖c n • d n‖) l (𝓝 ‖y‖) :=
(continuous_norm.tendsto _).comp hd,
have C : tendsto (λn, ‖c n‖⁻¹ * ‖c n • d n‖) l (𝓝 (0 * ‖y‖)) := A.mul B,
rw zero_mul at C,
have : ∀ᶠ n in l, ‖c n‖⁻¹ * ‖c n • d n‖ = ‖d n‖,
{ apply (eventually_ne_of_tendsto_norm_at_top hc 0).mono (λn hn, _),
rw [norm_smul, ← mul_assoc, inv_mul_cancel, one_mul],
rwa [ne.def, norm_eq_zero] },
have D : tendsto (λ n, ‖d n‖) l (𝓝 0) :=
tendsto.congr' this C,
rw tendsto_zero_iff_norm_tendsto_zero,
exact D
end
lemma tangent_cone_mono_nhds (h : 𝓝[s] x ≤ 𝓝[t] x) :
tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x :=
begin
rintros y ⟨c, d, ds, ctop, clim⟩,
refine ⟨c, d, _, ctop, clim⟩,
suffices : tendsto (λ n, x + d n) at_top (𝓝[t] x),
from tendsto_principal.1 (tendsto_inf.1 this).2,
refine (tendsto_inf.2 ⟨_, tendsto_principal.2 ds⟩).mono_right h,
simpa only [add_zero] using tendsto_const_nhds.add (tangent_cone_at.lim_zero at_top ctop clim)
end
/-- Tangent cone of `s` at `x` depends only on `𝓝[s] x`. -/
lemma tangent_cone_congr (h : 𝓝[s] x = 𝓝[t] x) :
tangent_cone_at 𝕜 s x = tangent_cone_at 𝕜 t x :=
subset.antisymm
(tangent_cone_mono_nhds $ le_of_eq h)
(tangent_cone_mono_nhds $ le_of_eq h.symm)
/-- Intersecting with a neighborhood of the point does not change the tangent cone. -/
lemma tangent_cone_inter_nhds (ht : t ∈ 𝓝 x) :
tangent_cone_at 𝕜 (s ∩ t) x = tangent_cone_at 𝕜 s x :=
tangent_cone_congr (nhds_within_restrict' _ ht).symm
/-- The tangent cone of a product contains the tangent cone of its left factor. -/
lemma subset_tangent_cone_prod_left {t : set F} {y : F} (ht : y ∈ closure t) :
linear_map.inl 𝕜 E F '' (tangent_cone_at 𝕜 s x) ⊆ tangent_cone_at 𝕜 (s ×ˢ t) (x, y) :=
begin
rintros _ ⟨v, ⟨c, d, hd, hc, hy⟩, rfl⟩,
have : ∀n, ∃d', y + d' ∈ t ∧ ‖c n • d'‖ < ((1:ℝ)/2)^n,
{ assume n,
rcases mem_closure_iff_nhds.1 ht _ (eventually_nhds_norm_smul_sub_lt (c n) y
(pow_pos one_half_pos n)) with ⟨z, hz, hzt⟩,
exact ⟨z - y, by simpa using hzt, by simpa using hz⟩ },
choose d' hd' using this,
refine ⟨c, λn, (d n, d' n), _, hc, _⟩,
show ∀ᶠ n in at_top, (x, y) + (d n, d' n) ∈ s ×ˢ t,
{ filter_upwards [hd] with n hn,
simp [hn, (hd' n).1] },
{ apply tendsto.prod_mk_nhds hy _,
refine squeeze_zero_norm (λn, (hd' n).2.le) _,
exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one }
end
/-- The tangent cone of a product contains the tangent cone of its right factor. -/
lemma subset_tangent_cone_prod_right {t : set F} {y : F}
(hs : x ∈ closure s) :
linear_map.inr 𝕜 E F '' (tangent_cone_at 𝕜 t y) ⊆ tangent_cone_at 𝕜 (s ×ˢ t) (x, y) :=
begin
rintros _ ⟨w, ⟨c, d, hd, hc, hy⟩, rfl⟩,
have : ∀n, ∃d', x + d' ∈ s ∧ ‖c n • d'‖ < ((1:ℝ)/2)^n,
{ assume n,
rcases mem_closure_iff_nhds.1 hs _ (eventually_nhds_norm_smul_sub_lt (c n) x
(pow_pos one_half_pos n)) with ⟨z, hz, hzs⟩,
exact ⟨z - x, by simpa using hzs, by simpa using hz⟩ },
choose d' hd' using this,
refine ⟨c, λn, (d' n, d n), _, hc, _⟩,
show ∀ᶠ n in at_top, (x, y) + (d' n, d n) ∈ s ×ˢ t,
{ filter_upwards [hd] with n hn,
simp [hn, (hd' n).1] },
{ apply tendsto.prod_mk_nhds _ hy,
refine squeeze_zero_norm (λn, (hd' n).2.le) _,
exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one }
end
/-- The tangent cone of a product contains the tangent cone of each factor. -/
lemma maps_to_tangent_cone_pi {ι : Type*} [decidable_eq ι] {E : ι → Type*}
[Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)]
{s : Π i, set (E i)} {x : Π i, E i} {i : ι} (hi : ∀ j ≠ i, x j ∈ closure (s j)) :
maps_to (linear_map.single i : E i →ₗ[𝕜] Π j, E j) (tangent_cone_at 𝕜 (s i) (x i))
(tangent_cone_at 𝕜 (set.pi univ s) x) :=
begin
rintros w ⟨c, d, hd, hc, hy⟩,
have : ∀ n (j ≠ i), ∃ d', x j + d' ∈ s j ∧ ‖c n • d'‖ < (1 / 2 : ℝ) ^ n,
{ assume n j hj,
rcases mem_closure_iff_nhds.1 (hi j hj) _ (eventually_nhds_norm_smul_sub_lt (c n) (x j)
(pow_pos one_half_pos n)) with ⟨z, hz, hzs⟩,
exact ⟨z - x j, by simpa using hzs, by simpa using hz⟩ },
choose! d' hd's hcd',
refine ⟨c, λ n, function.update (d' n) i (d n), hd.mono (λ n hn j hj', _), hc,
tendsto_pi_nhds.2 $ λ j, _⟩,
{ rcases em (j = i) with rfl|hj; simp * },
{ rcases em (j = i) with rfl|hj,
{ simp [hy] },
{ suffices : tendsto (λ n, c n • d' n j) at_top (𝓝 0), by simpa [hj],
refine squeeze_zero_norm (λ n, (hcd' n j hj).le) _,
exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one } }
end
/-- If a subset of a real vector space contains an open segment, then the direction of this
segment belongs to the tangent cone at its endpoints. -/
lemma mem_tangent_cone_of_open_segment_subset {s : set G} {x y : G} (h : open_segment ℝ x y ⊆ s) :
y - x ∈ tangent_cone_at ℝ s x :=
begin
let c := λn:ℕ, (2:ℝ)^(n+1),
let d := λn:ℕ, (c n)⁻¹ • (y-x),
refine ⟨c, d, filter.univ_mem' (λn, h _), _, _⟩,
show x + d n ∈ open_segment ℝ x y,
{ rw open_segment_eq_image,
refine ⟨(c n)⁻¹, ⟨_, _⟩, _⟩,
{ rw inv_pos, apply pow_pos, norm_num },
{ apply inv_lt_one, apply one_lt_pow _ (nat.succ_ne_zero _), norm_num },
{ simp only [d, sub_smul, smul_sub, one_smul], abel } },
show filter.tendsto (λ (n : ℕ), ‖c n‖) filter.at_top filter.at_top,
{ have : (λ (n : ℕ), ‖c n‖) = c,
by { ext n, exact abs_of_nonneg (pow_nonneg (by norm_num) _) },
rw this,
exact (tendsto_pow_at_top_at_top_of_one_lt (by norm_num)).comp (tendsto_add_at_top_nat 1) },
show filter.tendsto (λ (n : ℕ), c n • d n) filter.at_top (𝓝 (y - x)),
{ have : (λ (n : ℕ), c n • d n) = (λn, y - x),
{ ext n,
simp only [d, smul_smul],
rw [mul_inv_cancel, one_smul],
exact pow_ne_zero _ (by norm_num) },
rw this,
apply tendsto_const_nhds }
end
/-- If a subset of a real vector space contains a segment, then the direction of this
segment belongs to the tangent cone at its endpoints. -/
lemma mem_tangent_cone_of_segment_subset {s : set G} {x y : G} (h : segment ℝ x y ⊆ s) :
y - x ∈ tangent_cone_at ℝ s x :=
mem_tangent_cone_of_open_segment_subset ((open_segment_subset_segment ℝ x y).trans h)
end tangent_cone
section unique_diff
/-!
### Properties of `unique_diff_within_at` and `unique_diff_on`
This section is devoted to properties of the predicates
`unique_diff_within_at` and `unique_diff_on`. -/
lemma unique_diff_on.unique_diff_within_at {s : set E} {x} (hs : unique_diff_on 𝕜 s) (h : x ∈ s) :
unique_diff_within_at 𝕜 s x :=
hs x h
lemma unique_diff_within_at_univ : unique_diff_within_at 𝕜 univ x :=
by { rw [unique_diff_within_at_iff, tangent_cone_univ], simp }
lemma unique_diff_on_univ : unique_diff_on 𝕜 (univ : set E) :=
λx hx, unique_diff_within_at_univ
lemma unique_diff_on_empty : unique_diff_on 𝕜 (∅ : set E) :=
λ x hx, hx.elim
lemma unique_diff_within_at.mono_nhds (h : unique_diff_within_at 𝕜 s x)
(st : 𝓝[s] x ≤ 𝓝[t] x) :
unique_diff_within_at 𝕜 t x :=
begin
simp only [unique_diff_within_at_iff] at *,
rw [mem_closure_iff_nhds_within_ne_bot] at h ⊢,
exact ⟨h.1.mono $ submodule.span_mono $ tangent_cone_mono_nhds st,
h.2.mono st⟩
end
lemma unique_diff_within_at.mono (h : unique_diff_within_at 𝕜 s x) (st : s ⊆ t) :
unique_diff_within_at 𝕜 t x :=
h.mono_nhds $ nhds_within_mono _ st
lemma unique_diff_within_at_congr (st : 𝓝[s] x = 𝓝[t] x) :
unique_diff_within_at 𝕜 s x ↔ unique_diff_within_at 𝕜 t x :=
⟨λ h, h.mono_nhds $ le_of_eq st, λ h, h.mono_nhds $ le_of_eq st.symm⟩
lemma unique_diff_within_at_inter (ht : t ∈ 𝓝 x) :
unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x :=
unique_diff_within_at_congr $ (nhds_within_restrict' _ ht).symm
lemma unique_diff_within_at.inter (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝 x) :
unique_diff_within_at 𝕜 (s ∩ t) x :=
(unique_diff_within_at_inter ht).2 hs
lemma unique_diff_within_at_inter' (ht : t ∈ 𝓝[s] x) :
unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x :=
unique_diff_within_at_congr $ (nhds_within_restrict'' _ ht).symm
lemma unique_diff_within_at.inter' (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝[s] x) :
unique_diff_within_at 𝕜 (s ∩ t) x :=
(unique_diff_within_at_inter' ht).2 hs
lemma unique_diff_within_at_of_mem_nhds (h : s ∈ 𝓝 x) : unique_diff_within_at 𝕜 s x :=
by simpa only [univ_inter] using unique_diff_within_at_univ.inter h
lemma is_open.unique_diff_within_at (hs : is_open s) (xs : x ∈ s) : unique_diff_within_at 𝕜 s x :=
unique_diff_within_at_of_mem_nhds (is_open.mem_nhds hs xs)
lemma unique_diff_on.inter (hs : unique_diff_on 𝕜 s) (ht : is_open t) : unique_diff_on 𝕜 (s ∩ t) :=
λx hx, (hs x hx.1).inter (is_open.mem_nhds ht hx.2)
lemma is_open.unique_diff_on (hs : is_open s) : unique_diff_on 𝕜 s :=
λx hx, is_open.unique_diff_within_at hs hx
/-- The product of two sets of unique differentiability at points `x` and `y` has unique
differentiability at `(x, y)`. -/
lemma unique_diff_within_at.prod {t : set F} {y : F}
(hs : unique_diff_within_at 𝕜 s x) (ht : unique_diff_within_at 𝕜 t y) :
unique_diff_within_at 𝕜 (s ×ˢ t) (x, y) :=
begin
rw [unique_diff_within_at_iff] at ⊢ hs ht,
rw [closure_prod_eq],
refine ⟨_, hs.2, ht.2⟩,
have : _ ≤ submodule.span 𝕜 (tangent_cone_at 𝕜 (s ×ˢ t) (x, y)) :=
submodule.span_mono (union_subset (subset_tangent_cone_prod_left ht.2)
(subset_tangent_cone_prod_right hs.2)),
rw [linear_map.span_inl_union_inr, set_like.le_def] at this,
exact (hs.1.prod ht.1).mono this
end
lemma unique_diff_within_at.univ_pi (ι : Type*) [finite ι] (E : ι → Type*)
[Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)]
(s : Π i, set (E i)) (x : Π i, E i) (h : ∀ i, unique_diff_within_at 𝕜 (s i) (x i)) :
unique_diff_within_at 𝕜 (set.pi univ s) x :=
begin
classical,
simp only [unique_diff_within_at_iff, closure_pi_set] at h ⊢,
refine ⟨(dense_pi univ (λ i _, (h i).1)).mono _, λ i _, (h i).2⟩,
norm_cast,
simp only [← submodule.supr_map_single, supr_le_iff, linear_map.map_span, submodule.span_le,
← maps_to'],
exact λ i, (maps_to_tangent_cone_pi $ λ j hj, (h j).2).mono subset.rfl submodule.subset_span
end
lemma unique_diff_within_at.pi (ι : Type*) [finite ι] (E : ι → Type*)
[Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)]
(s : Π i, set (E i)) (x : Π i, E i) (I : set ι)
(h : ∀ i ∈ I, unique_diff_within_at 𝕜 (s i) (x i)) :
unique_diff_within_at 𝕜 (set.pi I s) x :=
begin
classical,
rw [← set.univ_pi_piecewise],
refine unique_diff_within_at.univ_pi _ _ _ _ (λ i, _),
by_cases hi : i ∈ I; simp [*, unique_diff_within_at_univ],
end
/-- The product of two sets of unique differentiability is a set of unique differentiability. -/
lemma unique_diff_on.prod {t : set F} (hs : unique_diff_on 𝕜 s) (ht : unique_diff_on 𝕜 t) :
unique_diff_on 𝕜 (s ×ˢ t) :=
λ ⟨x, y⟩ h, unique_diff_within_at.prod (hs x h.1) (ht y h.2)
/-- The finite product of a family of sets of unique differentiability is a set of unique
differentiability. -/
lemma unique_diff_on.pi (ι : Type*) [finite ι] (E : ι → Type*)
[Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)]
(s : Π i, set (E i)) (I : set ι) (h : ∀ i ∈ I, unique_diff_on 𝕜 (s i)) :
unique_diff_on 𝕜 (set.pi I s) :=
λ x hx, unique_diff_within_at.pi _ _ _ _ _ $ λ i hi, h i hi (x i) (hx i hi)
/-- The finite product of a family of sets of unique differentiability is a set of unique
differentiability. -/
lemma unique_diff_on.univ_pi (ι : Type*) [finite ι] (E : ι → Type*)
[Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)]
(s : Π i, set (E i)) (h : ∀ i, unique_diff_on 𝕜 (s i)) :
unique_diff_on 𝕜 (set.pi univ s) :=
unique_diff_on.pi _ _ _ _ $ λ i _, h i
/-- In a real vector space, a convex set with nonempty interior is a set of unique
differentiability at every point of its closure. -/
theorem unique_diff_within_at_convex {s : set G} (conv : convex ℝ s) (hs : (interior s).nonempty)
{x : G} (hx : x ∈ closure s) : unique_diff_within_at ℝ s x :=
begin
rcases hs with ⟨y, hy⟩,
suffices : y - x ∈ interior (tangent_cone_at ℝ s x),
{ refine ⟨dense.of_closure _, hx⟩,
simp [(submodule.span ℝ (tangent_cone_at ℝ s x)).eq_top_of_nonempty_interior'
⟨y - x, interior_mono submodule.subset_span this⟩] },
rw [mem_interior_iff_mem_nhds],
replace hy : interior s ∈ 𝓝 y := is_open.mem_nhds is_open_interior hy,
apply mem_of_superset ((is_open_map_sub_right x).image_mem_nhds hy),
rintros _ ⟨z, zs, rfl⟩,
refine mem_tangent_cone_of_open_segment_subset (subset.trans _ interior_subset),
exact conv.open_segment_closure_interior_subset_interior hx zs,
end
/-- In a real vector space, a convex set with nonempty interior is a set of unique
differentiability. -/
theorem unique_diff_on_convex {s : set G} (conv : convex ℝ s) (hs : (interior s).nonempty) :
unique_diff_on ℝ s :=
λ x xs, unique_diff_within_at_convex conv hs (subset_closure xs)
lemma unique_diff_on_Ici (a : ℝ) : unique_diff_on ℝ (Ici a) :=
unique_diff_on_convex (convex_Ici a) $ by simp only [interior_Ici, nonempty_Ioi]
lemma unique_diff_on_Iic (a : ℝ) : unique_diff_on ℝ (Iic a) :=
unique_diff_on_convex (convex_Iic a) $ by simp only [interior_Iic, nonempty_Iio]
lemma unique_diff_on_Ioi (a : ℝ) : unique_diff_on ℝ (Ioi a) :=
is_open_Ioi.unique_diff_on
lemma unique_diff_on_Iio (a : ℝ) : unique_diff_on ℝ (Iio a) :=
is_open_Iio.unique_diff_on
lemma unique_diff_on_Icc {a b : ℝ} (hab : a < b) : unique_diff_on ℝ (Icc a b) :=
unique_diff_on_convex (convex_Icc a b) $ by simp only [interior_Icc, nonempty_Ioo, hab]
lemma unique_diff_on_Ico (a b : ℝ) : unique_diff_on ℝ (Ico a b) :=
if hab : a < b
then unique_diff_on_convex (convex_Ico a b) $ by simp only [interior_Ico, nonempty_Ioo, hab]
else by simp only [Ico_eq_empty hab, unique_diff_on_empty]
lemma unique_diff_on_Ioc (a b : ℝ) : unique_diff_on ℝ (Ioc a b) :=
if hab : a < b
then unique_diff_on_convex (convex_Ioc a b) $ by simp only [interior_Ioc, nonempty_Ioo, hab]
else by simp only [Ioc_eq_empty hab, unique_diff_on_empty]
lemma unique_diff_on_Ioo (a b : ℝ) : unique_diff_on ℝ (Ioo a b) :=
is_open_Ioo.unique_diff_on
/-- The real interval `[0, 1]` is a set of unique differentiability. -/
lemma unique_diff_on_Icc_zero_one : unique_diff_on ℝ (Icc (0:ℝ) 1) :=
unique_diff_on_Icc zero_lt_one
lemma unique_diff_within_at_Ioo {a b t : ℝ} (ht : t ∈ set.Ioo a b) :
unique_diff_within_at ℝ (set.Ioo a b) t :=
is_open.unique_diff_within_at is_open_Ioo ht
lemma unique_diff_within_at_Ioi (a : ℝ) : unique_diff_within_at ℝ (Ioi a) a :=
unique_diff_within_at_convex (convex_Ioi a) (by simp) (by simp)
lemma unique_diff_within_at_Iio (a : ℝ) : unique_diff_within_at ℝ (Iio a) a :=
unique_diff_within_at_convex (convex_Iio a) (by simp) (by simp)
end unique_diff
|
2084047b8ed70f56d6e727267e6a027d9f488e58 | 46125763b4dbf50619e8846a1371029346f4c3db | /src/measure_theory/indicator_function.lean | 9338e638f01f7bdb80f58dff691d62664fa120d7 | [
"Apache-2.0"
] | permissive | thjread/mathlib | a9d97612cedc2c3101060737233df15abcdb9eb1 | 7cffe2520a5518bba19227a107078d83fa725ddc | refs/heads/master | 1,615,637,696,376 | 1,583,953,063,000 | 1,583,953,063,000 | 246,680,271 | 0 | 0 | Apache-2.0 | 1,583,960,875,000 | 1,583,960,875,000 | null | UTF-8 | Lean | false | false | 5,671 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
-/
import data.indicator_function
import measure_theory.measure_space
import analysis.normed_space.basic
/-!
# Indicator function
Properties of indicator functions.
## Tags
indicator, characteristic
-/
noncomputable theory
open_locale classical
open set measure_theory filter
universes u v
variables {α : Type u} {β : Type v}
section has_zero
variables [has_zero β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_congr_ae [measure_space α] (h : ∀ₘ a, a ∈ s → f a = g a) :
∀ₘ a, indicator s f a = indicator s g a :=
begin
filter_upwards [h],
simp only [mem_set_of_eq, indicator],
assume a ha,
split_ifs with h₁,
{ exact ha h₁ },
refl
end
lemma indicator_congr_of_set [measure_space α] (h : ∀ₘ a, a ∈ s ↔ a ∈ t) :
∀ₘ a, indicator s f a = indicator t f a :=
begin
filter_upwards [h],
simp only [mem_set_of_eq, indicator],
assume a ha,
split_ifs with h₁ h₂ h₂,
{ refl },
{ have := ha.1 h₁, contradiction },
{ have := ha.2 h₂, contradiction },
refl
end
end has_zero
section has_add
variables [add_monoid β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_union_ae [measure_space α] {β : Type*} [add_monoid β]
(h : ∀ₘ a, a ∉ s ∩ t) (f : α → β) :
∀ₘ a, indicator (s ∪ t) f a = indicator s f a + indicator t f a :=
begin
filter_upwards [h],
simp only [mem_set_of_eq],
assume a ha,
exact indicator_union_of_not_mem_inter ha _
end
end has_add
section norm
variables [normed_group β] {s t : set α} {f g : α → β} {a : α}
lemma norm_indicator_le_of_subset (h : s ⊆ t) (f : α → β) (a : α) :
∥indicator s f a∥ ≤ ∥indicator t f a∥ :=
begin
simp only [indicator],
split_ifs with h₁ h₂,
{ refl },
{ exact absurd (h h₁) h₂ },
{ simp only [norm_zero, norm_nonneg] },
refl
end
lemma norm_indicator_le_norm_self (f : α → β) (a : α) : ∥indicator s f a∥ ≤ ∥f a∥ :=
by { convert norm_indicator_le_of_subset (subset_univ s) f a, rw indicator_univ }
lemma norm_indicator_eq_indicator_norm (f : α → β) (a : α) : ∥indicator s f a∥ = indicator s (λa, ∥f a∥) a :=
by { simp only [indicator], split_ifs, { refl }, rw norm_zero }
lemma indicator_norm_le_norm_self (f : α → β) (a : α) : indicator s (λa, ∥f a∥) a ≤ ∥f a∥ :=
by { rw ← norm_indicator_eq_indicator_norm, exact norm_indicator_le_norm_self _ _ }
end norm
section order
variables [has_zero β] [preorder β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_le_indicator_ae [measure_space α] (h : ∀ₘ a, a ∈ s → f a ≤ g a) :
∀ₘ a, indicator s f a ≤ indicator s g a :=
begin
filter_upwards [h],
simp only [mem_set_of_eq, indicator],
assume a h,
split_ifs with ha,
{ exact h ha },
refl
end
end order
section tendsto
variables {ι : Type*} [lattice.semilattice_sup ι] [has_zero β]
lemma tendsto_indicator_of_monotone [nonempty ι] (s : ι → set α) (hs : monotone s) (f : α → β)
(a : α) : tendsto (λi, indicator (s i) f a) at_top (pure $ indicator (Union s) f a) :=
begin
by_cases h : ∃i, a ∈ s i,
{ simp only [tendsto_pure, mem_at_top_sets, mem_set_of_eq],
rcases h with ⟨i, hi⟩,
use i, assume n hn,
rw [indicator_of_mem (hs hn hi) _, indicator_of_mem ((subset_Union _ _) hi) _] },
{ rw [not_exists] at h,
have : (λi, indicator (s i) f a) = λi, 0 := funext (λi, indicator_of_not_mem (h i) _),
rw this,
have : indicator (Union s) f a = 0,
{ apply indicator_of_not_mem, simpa only [not_exists, mem_Union] },
rw this,
exact tendsto_const_pure }
end
lemma tendsto_indicator_of_antimono [nonempty ι] (s : ι → set α) (hs : ∀i j, i ≤ j → s j ⊆ s i) (f : α → β)
(a : α) : tendsto (λi, indicator (s i) f a) at_top (pure $ indicator (Inter s) f a) :=
begin
by_cases h : ∃i, a ∉ s i,
{ simp only [tendsto_pure, mem_at_top_sets, mem_set_of_eq],
rcases h with ⟨i, hi⟩,
use i, assume n hn,
rw [indicator_of_not_mem _ _, indicator_of_not_mem _ _],
{ simp only [mem_Inter, not_forall], exact ⟨i, hi⟩ },
{ assume h, have := hs i _ hn h, contradiction } },
{ simp only [not_exists, not_not_mem] at h,
have : (λi, indicator (s i) f a) = λi, f a := funext (λi, indicator_of_mem (h i) _),
rw this,
have : indicator (Inter s) f a = f a,
{ apply indicator_of_mem, simpa only [mem_Inter] },
rw this,
exact tendsto_const_pure }
end
lemma tendsto_indicator_bUnion_finset (s : ι → set α) (f : α → β) (a : α) :
tendsto (λ (n : finset ι), indicator (⋃i∈n, s i) f a) at_top (pure $ indicator (Union s) f a) :=
begin
by_cases h : ∃i, a ∈ s i,
{ simp only [mem_at_top_sets, tendsto_pure, mem_set_of_eq, ge_iff_le, finset.le_iff_subset],
rcases h with ⟨i, hi⟩,
use {i}, assume n hn,
replace hn : i ∈ n := hn (finset.mem_singleton_self _),
rw [indicator_of_mem, indicator_of_mem],
{ rw [mem_Union], use i, assumption },
{ rw [mem_Union], use i, rw [mem_Union], use hn, assumption } },
{ rw [not_exists] at h,
have : (λ (n : finset ι), indicator (⋃ (i : ι) (H : i ∈ n), s i) f a) = λi, 0,
{ funext,
rw indicator_of_not_mem,
simp only [not_exists, exists_prop, mem_Union, not_and],
intros, apply h },
rw this,
have : indicator (Union s) f a = 0,
{ apply indicator_of_not_mem, simpa only [not_exists, mem_Union] },
rw this,
exact tendsto_const_pure }
end
end tendsto
|
8be9f7dd49f1e6c03175d73b4bbe7223936cfd74 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/standard/logic/axioms/piext.lean | 56c17084b5a02e5cf8bf094945b50f012af737c6 | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,511 | lean | ----------------------------------------------------------------------------------------------------
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Leonardo de Moura
----------------------------------------------------------------------------------------------------
import logic.classes.inhabited logic.connectives.cast
-- Pi extensionality
axiom piext {A : Type} {B B' : A → Type} {H : inhabited (Π x, B x)} :
(Π x, B x) = (Π x, B' x) → B = B'
theorem cast_app {A : Type} {B B' : A → Type} (H : (Π x, B x) = (Π x, B' x)) (f : Π x, B x)
(a : A) : cast H f a == f a :=
have Hi [fact] : inhabited (Π x, B x), from inhabited_intro f,
have Hb : B = B', from piext H,
cast_app' Hb f a
theorem hcongr1 {A : Type} {B B' : A → Type} {f : Π x, B x} {f' : Π x, B' x} (a : A)
(H : f == f') : f a == f' a :=
have Hi [fact] : inhabited (Π x, B x), from inhabited_intro f,
have Hb : B = B', from piext (type_eq H),
hcongr1' a H Hb
theorem hcongr {A A' : Type} {B : A → Type} {B' : A' → Type}
{f : Π x, B x} {f' : Π x, B' x} {a : A} {a' : A'}
(Hff' : f == f') (Haa' : a == a') : f a == f' a' :=
have H1 : ∀ (B B' : A → Type) (f : Π x, B x) (f' : Π x, B' x), f == f' → f a == f' a, from
take B B' f f' e, hcongr1 a e,
have H2 : ∀ (B : A → Type) (B' : A' → Type) (f : Π x, B x) (f' : Π x, B' x),
f == f' → f a == f' a', from hsubst Haa' H1,
H2 B B' f f' Hff'
|
30eafac6406953d391f2727e6a45975a9a626614 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/data/fin_enum.lean | 920a968caf215cc8eaa84388d36df026aa1e5a16 | [
"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 | 9,007 | lean | /-
Copyright (c) 2019 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author(s): Simon Hudon
-/
import category.monad.basic
import data.list.basic
import data.equiv.basic
import data.finset
import data.fintype
/-!
Type class for finitely enumerable types. The property is stronger
than `fintype` in that it assigns each element a rank in a finite
enumeration.
-/
open finset (hiding singleton)
/-- `fin_enum α` means that `α` is finite and can be enumerated in some order,
i.e. `α` has an explicit bijection with `fin n` for some n. -/
class fin_enum (α : Sort*) :=
(card : ℕ)
(equiv : α ≃ fin card)
[dec_eq : decidable_eq α]
attribute [instance, priority 100] fin_enum.dec_eq
namespace fin_enum
variables {α : Type*}
/-- transport a `fin_enum` instance across an equivalence -/
def of_equiv (α) {β} [fin_enum α] (h : β ≃ α) : fin_enum β :=
{ card := card α,
equiv := h.trans (equiv α),
dec_eq := equiv.decidable_eq_of_equiv (h.trans (equiv _)) }
/-- create a `fin_enum` instance from an exhaustive list without duplicates -/
def of_nodup_list [decidable_eq α] (xs : list α) (h : ∀ x : α, x ∈ xs) (h' : list.nodup xs) : fin_enum α :=
{ card := xs.length,
equiv := ⟨λ x, ⟨xs.index_of x,by rw [list.index_of_lt_length]; apply h⟩,
λ ⟨i,h⟩, xs.nth_le _ h,
λ x, by simp [of_nodup_list._match_1],
λ ⟨i,h⟩, by simp [of_nodup_list._match_1,*]; rw list.nth_le_index_of; apply list.nodup_erase_dup ⟩ }
/-- create a `fin_enum` instance from an exhaustive list; duplicates are removed -/
def of_list [decidable_eq α] (xs : list α) (h : ∀ x : α, x ∈ xs) : fin_enum α :=
of_nodup_list xs.erase_dup (by simp *) (list.nodup_erase_dup _)
/-- create an exhaustive list of the values of a given type -/
def to_list (α) [fin_enum α] : list α :=
(list.fin_range (card α)).map (equiv α).symm
open function
@[simp] lemma mem_to_list [fin_enum α] (x : α) : x ∈ to_list α :=
by simp [to_list]; existsi equiv α x; simp
@[simp] lemma nodup_to_list [fin_enum α] : list.nodup (to_list α) :=
by simp [to_list]; apply list.nodup_map; [apply equiv.injective, apply list.nodup_fin_range]
/-- create a `fin_enum` instance using a surjection -/
def of_surjective {β} (f : β → α) [decidable_eq α] [fin_enum β] (h : surjective f) : fin_enum α :=
of_list ((to_list β).map f) (by intro; simp; exact h _)
/-- create a `fin_enum` instance using an injection -/
noncomputable def of_injective {α β} (f : α → β) [decidable_eq α] [fin_enum β] (h : injective f) : fin_enum α :=
of_list ((to_list β).filter_map (partial_inv f))
begin
intro x,
simp only [mem_to_list, true_and, list.mem_filter_map],
use f x,
simp only [h, function.partial_inv_left],
end
instance pempty : fin_enum pempty :=
of_list [] (λ x, pempty.elim x)
instance empty : fin_enum empty :=
of_list [] (λ x, empty.elim x)
instance punit : fin_enum punit :=
of_list [punit.star] (λ x, by cases x; simp)
instance prod {β} [fin_enum α] [fin_enum β] : fin_enum (α × β) :=
of_list ( (to_list α).product (to_list β) ) (λ x, by cases x; simp)
instance sum {β} [fin_enum α] [fin_enum β] : fin_enum (α ⊕ β) :=
of_list ( (to_list α).map sum.inl ++ (to_list β).map sum.inr ) (λ x, by cases x; simp)
instance fin {n} : fin_enum (fin n) :=
of_list (list.fin_range _) (by simp)
instance quotient.enum [fin_enum α] (s : setoid α)
[decidable_rel ((≈) : α → α → Prop)] : fin_enum (quotient s) :=
fin_enum.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩))
/-- enumerate all finite sets of a given type -/
def finset.enum [decidable_eq α] : list α → list (finset α)
| [] := [∅]
| (x :: xs) :=
do r ← finset.enum xs,
[r,{x} ∪ r]
@[simp] lemma finset.mem_enum [decidable_eq α] (s : finset α) (xs : list α) : s ∈ finset.enum xs ↔ ∀ x ∈ s, x ∈ xs :=
begin
induction xs generalizing s; simp [*,finset.enum],
{ simp [finset.eq_empty_iff_forall_not_mem,(∉)], refl },
{ split, rintro ⟨a,h,h'⟩ x hx,
cases h',
{ right, apply h, subst a, exact hx, },
{ simp only [h', mem_union, mem_singleton] at hx ⊢, cases hx,
{ exact or.inl hx },
{ exact or.inr (h _ hx) } },
intro h, existsi s \ ({xs_hd} : finset α),
simp only [and_imp, union_comm, mem_sdiff, insert_empty_eq_singleton, mem_singleton],
simp only [or_iff_not_imp_left] at h,
existsi h,
by_cases xs_hd ∈ s,
{ have : finset.singleton xs_hd ⊆ s, simp only [has_subset.subset, *, forall_eq, mem_singleton],
simp only [union_sdiff_of_subset this, or_true, finset.union_sdiff_of_subset, eq_self_iff_true], },
{ left, symmetry, simp only [sdiff_eq_self],
intro a, simp only [and_imp, mem_inter, mem_singleton, not_mem_empty],
intros h₀ h₁, subst a, apply h h₀, } }
end
instance finset.fin_enum [fin_enum α] : fin_enum (finset α) :=
of_list (finset.enum (to_list α)) (by intro; simp)
instance subtype.fin_enum [fin_enum α] (p : α → Prop) [decidable_pred p] : fin_enum {x // p x} :=
of_list ((to_list α).filter_map $ λ x, if h : p x then some ⟨_,h⟩ else none) (by rintro ⟨x,h⟩; simp; existsi x; simp *)
instance (β : α → Type*)
[fin_enum α] [∀ a, fin_enum (β a)] : fin_enum (sigma β) :=
of_list
((to_list α).bind $ λ a, (to_list (β a)).map $ sigma.mk a)
(by intro x; cases x; simp)
instance psigma.fin_enum {β : α → Type*} [fin_enum α] [∀ a, fin_enum (β a)] :
fin_enum (Σ' a, β a) :=
fin_enum.of_equiv _ (equiv.psigma_equiv_sigma _)
instance psigma.fin_enum_prop_left {α : Prop} {β : α → Type*} [∀ a, fin_enum (β a)] [decidable α] :
fin_enum (Σ' a, β a) :=
if h : α then of_list ((to_list (β h)).map $ psigma.mk h) (λ ⟨a,Ba⟩, by simp)
else of_list [] (λ ⟨a,Ba⟩, (h a).elim)
instance psigma.fin_enum_prop_right {β : α → Prop} [fin_enum α] [∀ a, decidable (β a)] :
fin_enum (Σ' a, β a) :=
fin_enum.of_equiv {a // β a} ⟨λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, rfl, λ ⟨x, y⟩, rfl⟩
instance psigma.fin_enum_prop_prop {α : Prop} {β : α → Prop} [decidable α] [∀ a, decidable (β a)] :
fin_enum (Σ' a, β a) :=
if h : ∃ a, β a
then of_list [⟨h.fst,h.snd⟩] (by rintro ⟨⟩; simp)
else of_list [] (λ a, (h ⟨a.fst,a.snd⟩).elim)
@[priority 100]
instance [fin_enum α] : fintype α :=
{ elems := univ.map (equiv α).symm.to_embedding,
complete := by intros; simp; existsi (equiv α x); simp }
/-- For `pi.cons x xs y f` create a function where every `i ∈ xs` is mapped to `f i` and
`x` is mapped to `y` -/
def pi.cons {β : α → Type*} [decidable_eq α] (x : α) (xs : list α) (y : β x)
(f : Π a, a ∈ xs → β a) :
Π a, a ∈ (x :: xs : list α) → β a
| b h :=
if h' : b = x then cast (by rw h') y
else f b (list.mem_of_ne_of_mem h' h)
/-- Given `f` a function whose domain is `x :: xs`, produce a function whose domain
is restricted to `xs`. -/
def pi.tail {α : Type*} {β : α → Type*} {x : α} {xs : list α}
(f : Π a, a ∈ (x :: xs : list α) → β a) :
Π a, a ∈ xs → β a
| a h := f a (list.mem_cons_of_mem _ h)
/-- `pi xs f` creates the list of functions `g` such that, for `x ∈ xs`, `g x ∈ f x` -/
def pi {α : Type*} {β : α → Type*} [decidable_eq α] : Π xs : list α, (Π a, list (β a)) → list (Π a, a ∈ xs → β a)
| [] fs := [λ x h, h.elim]
| (x :: xs) fs :=
fin_enum.pi.cons x xs <$> fs x <*> pi xs fs
lemma mem_pi {α : Type*} {β : α → Type*} [fin_enum α] [∀a, fin_enum (β a)] (xs : list α) (f : Π a, a ∈ xs → β a) :
f ∈ pi xs (λ x, to_list (β x)) :=
begin
induction xs; simp [pi,-list.map_eq_map] with monad_norm functor_norm,
{ ext a ⟨ ⟩ },
{ existsi pi.cons xs_hd xs_tl (f _ (list.mem_cons_self _ _)),
split, exact ⟨_,rfl⟩,
existsi pi.tail f, split,
{ apply xs_ih, },
{ ext x h, simp [pi.cons], split_ifs, subst x, refl, refl }, }
end
/-- enumerate all functions whose domain and range are finitely enumerable -/
def pi.enum {α : Type*} (β : α → Type*) [fin_enum α] [∀a, fin_enum (β a)] : list (Π a, β a) :=
(pi (to_list α) (λ x, to_list (β x))).map (λ f x, f x (mem_to_list _))
lemma pi.mem_enum {α : Type*} {β : α → Type*} [fin_enum α] [∀a, fin_enum (β a)] (f : Π a, β a) : f ∈ pi.enum β :=
by simp [pi.enum]; refine ⟨λ a h, f a, mem_pi _ _, rfl⟩
instance pi.fin_enum {α : Type*} {β : α → Type*}
[fin_enum α] [∀a, fin_enum (β a)] : fin_enum (Πa, β a) :=
of_list (pi.enum _) (λ x, pi.mem_enum _)
instance pfun_fin_enum (p : Prop) [decidable p] (α : p → Type*)
[Π hp, fin_enum (α hp)] : fin_enum (Π hp : p, α hp) :=
if hp : p then of_list ( (to_list (α hp)).map $ λ x hp', x ) (by intro; simp; exact ⟨x hp,rfl⟩)
else of_list [λ hp', (hp hp').elim] (by intro; simp; ext hp'; cases hp hp')
end fin_enum
|
05382a97e0bd5cfd153ef3939d1a9f660de77890 | e0b0b1648286e442507eb62344760d5cd8d13f2d | /tests/lean/interactive/plainGoal.lean | 894a63cc728eda4abfa88448b0821e0b11b74ae1 | [
"Apache-2.0"
] | permissive | MULXCODE/lean4 | 743ed389e05e26e09c6a11d24607ad5a697db39b | 4675817a9e89824eca37192364cd47a4027c6437 | refs/heads/master | 1,682,231,879,857 | 1,620,423,501,000 | 1,620,423,501,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 859 | lean | example : α → α := by
--^ $/lean/plainGoal
--^ $/lean/plainGoal
intro a
--^ $/lean/plainGoal
--^ $/lean/plainGoal
--v $/lean/plainGoal
focus
apply a
example : α → α := by
--^ $/lean/plainGoal
example : 0 + n = n := by
induction n with
| zero => simp; simp
--^ $/lean/plainGoal
| succ
--^ $/lean/plainGoal
example : α → α := by
intro a; apply a
--^ $/lean/plainGoal
example (h1 : n = m) (h2 : m = 0) : 0 = n := by
rw [h1, h2]
--^ $/lean/plainGoal
--^ $/lean/plainGoal
--^ $/lean/plainGoal
example : 0 + n = n := by
induction n
focus
--^ $/lean/plainGoal
rfl
-- TODO: goal state after dedent
example : 0 + n = n := by
induction n with
--^ $/lean/plainGoal
example : 0 + n = n := by
cases n with
--^ $/lean/plainGoal
|
20028b17ea566fca2bc521aa1229bea27e3cff71 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/hott/obtain_hott.hlean | 3f6c0178af1720ef6a8d5348dbd3d9b76e590291 | [
"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 | 961 | hlean | open prod sigma
theorem tst2 (A B C D : Type) : (A × B) × (C × D) → C × B × A :=
assume p : (A × B) × (C × D),
obtain [a b] [c d], from p,
(c, (b, a))
theorem tst22 (A B C D : Type) : (A × B) × (C × D) → C × B × A :=
assume p,
obtain [a b] [c d], from p,
(c, (b, a))
theorem tst3 (A B C D : Type) : A × B × C × D → C × B × A :=
assume p,
obtain a b c d, from p,
(c, (b, a))
example (p q : nat → nat → Type) : (Σ x y, p x y × q x y × q y x) → Σ x y, p x y :=
assume ex,
obtain x y pxy qxy qyx, from ex,
⟨x, y, pxy⟩
example (p : nat → nat → Type): (Σ x, p x x) → (Σ x y, p x y) :=
assume sig,
obtain x pxx, from sig,
⟨x, x, pxx⟩
open nat
definition even (a : nat) := Σ x, a = 2*x
example (a b : nat) (H₁ : even a) (H₂ : even b) : even (a+b) :=
obtain x (Hx : a = 2*x), from H₁,
obtain y (Hy : b = 2*y), from H₂,
⟨x+y,
calc a+b = 2*x + 2*y : by rewrite [Hx, Hy]
... = 2*(x+y) : sorry⟩
|
7ffd1f3d2f2b4107ebe548f5fcccb34e04351651 | 618003631150032a5676f229d13a079ac875ff77 | /src/tactic/transport.lean | 6a977d2e05e85ca988e8cb1df1e787677220ee4c | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 5,386 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner, Simon Hudon, Scott Morrison
-/
import tactic.equiv_rw
/-!
## The `transport` tactic
`transport` attempts to move an `s : S α` expression across an equivalence `e : α ≃ β` to solve
a goal of the form `S β`, by building the new object field by field, taking each field of `s`
and rewriting it along `e` using the `equiv_rw` tactic.
We try to ensure good definitional properties, so that, for example, when we transport a `monoid α`
to a `monoid β`, the new multiplication is definitionally `λ x y, e (e.symm a * e.symm b)`.
-/
namespace tactic
open tactic.interactive
mk_simp_attribute transport_simps
"The simpset `transport_simps` is used by the tactic `transport`
to simplify certain expressions involving application of equivalences,
and trivial `eq.rec` or `ep.mpr` conversions.
It's probably best not to adjust it without understanding the algorithm used by `transport`."
attribute [transport_simps]
eq_rec_constant
eq_mpr_rfl
equiv.to_fun_as_coe
equiv.arrow_congr'_apply
equiv.symm_apply_apply
-- we use `apply_eq_iff_eq_symm_apply` rather than `apply_eq_iff_eq`,
-- as many axioms have a constant on the right-hand-side
equiv.apply_eq_iff_eq_symm_apply
/--
Given `s : S α` for some structure `S` depending on a type `α`,
and an equivalence `e : α ≃ β`,
try to produce an `S β`,
by transporting data and axioms across `e` using `equiv_rw`.
-/
@[nolint unused_arguments] -- At present we don't actually use `s`; it's inferred in the `mk_app` call.
meta def transport (s e : expr) : tactic unit :=
do
(_, α, β) ← infer_type e >>= relation_lhs_rhs <|>
fail format!"second argument to `transport` was not an equivalence-type relation",
-- We explode the goal into individual fields using `refine_struct`.
-- Later we'll want to also consider falling back to `fconstructor`,
-- but for now this suffices.
seq' `[refine_struct { .. }]
-- We now deal with each field sequentially.
-- Since we may pass goals through to the user if the heuristics here fail,
-- we wrap everything in `propagate_tags`.
(propagate_tags (try (do
-- Look up the name of the current field being processed
f ← get_current_field,
-- Note the value in the original structure.
-- (This `mk_mapp` call with second argument inferred only works for typeclasses!)
mk_mapp f [α, none] >>= note f none,
-- We now run different algorithms,
-- depending on whether we're transporting data or a proposition.
b ← target >>= is_prop,
-- In order to achieve good definitional properties,
-- we use `simp_result` to intercept the synthesized term and simplify it,
-- in particular simplifying along `eq_rec_constant`.
if ¬ b then simp_result (do
-- For data fields, simply rewrite them using `equiv_rw`.
equiv_rw_hyp f e,
get_local f >>= exact)
else try (do
-- The goal probably has messy expressions produced by `equiv_rw` acting on early data fields,
-- so we clean up a little.
try unfold_projs_target,
`[simp only [] with transport_simps],
-- If the field is an equation in `β`, try to use injectivity of the equivalence
-- to turn it into an equation in `α`.
-- (If the left hand side of the equation involved an operation we've already transported,
-- this step has already been achieved by the `transport_simps`, so we `try` this step.)
try $ under_binders $ to_expr ``((%%e).symm.injective) >>= apply,
-- Finally, rewrite the original field value using the equivalence `e`, and try to close
-- the goal using
equiv_rw_hyp f e,
get_local f >>= exact))))
namespace interactive
setup_tactic_parser
/--
Given a goal `⊢ S β` for some type class `S`, and an equivalence `e : α ≃ β`.
`transport using e` will look for a hypothesis `s : S α`,
and attempt to close the goal by transporting `s` across the equivalence `e`.
```lean
example {α : Type} [ring α] {β : Type} (e : α ≃ β) : ring β :=
by transport using e.
```
You can specify the object to transport using `transport s using e`.
`transport` works by attempting to copy each of the operations and axiom fields of `s`,
rewriting them using `equiv_rw e` and defining a new structure using these rewritten fields.
If it fails to fill in all the new fields, `transport` will produce new subgoals.
It's probably best to think about which missing `simp` lemmas would have allowed `transport`
to finish, rather than solving these goals by hand.
(This may require looking at the implementation of `tranport` to understand its algorithm;
there are several examples of "transport-by-hand" at the end of `test/equiv_rw.lean`,
which `transport` is an abstraction of.)
-/
meta def transport (s : parse texpr?) (e : parse $ (tk "using" *> texpr)) : itactic :=
do
s ← match s with
| some s := to_expr s
| none := (do
t ← target,
let n := t.get_app_fn.const_name,
ctx ← local_context,
ctx.any_of (λ e, (do t ← infer_type e, guard (t.get_app_fn.const_name = n), return e))) <|>
fail "`transport` could not find an appropriate source object. Try `transport s using e`."
end,
e ← to_expr e,
tactic.transport s e
end interactive
end tactic
|
9860557f3642b46508225acc1c37a5a3cbb7c348 | 7617a8700fc3eebd9200f1e620ee65b3565d2eff | /src/vc0/determ.lean | 29077a588c47e884e95037e0ae854b6a777a01d0 | [] | no_license | digama0/vc0 | 9747eee791c5c2f58eb53264ca6263ee53de0f87 | b8b192c8c139e0b5a25a7284b93ed53cdf7fd7a5 | refs/heads/master | 1,626,186,940,436 | 1,592,385,972,000 | 1,592,385,972,000 | 158,556,766 | 5 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,696 | lean | import vc0.basic
namespace c0
open ast
namespace value
theorem step_comp.determ {op v₁ v₂ b₁ b₂}
(h₁ : step_comp op v₁ v₂ b₁)
(h₂ : step_comp op v₁ v₂ b₂) : b₁ = b₂ :=
begin
suffices : ∀ {op' v₁' v₂'} (e : (op, v₁, v₂) = (op', v₁', v₂'))
(h₂ : step_comp op' v₁' v₂' b₂), b₁ = b₂,
from this rfl h₂,
clear h₂, intros,
induction h₁; induction h₂; cases e; refl
end
theorem step_binop.determ {op v₁ v₂ w₁ w₂}
(h₁ : step_binop op v₁ v₂ w₁) (h₂ : step_binop op v₁ v₂ w₂) : w₁ = w₂ :=
begin
generalize_hyp eo : op = op' at h₂,
generalize_hyp e₁ : v₁ = v₁' at h₂,
generalize_hyp e₂ : v₂ = v₂' at h₂,
induction h₁; induction h₂; cases eo; cases e₁; cases e₂; try {refl},
cases h₁_a.determ h₂_a, refl
end
theorem step_unop.determ {op v w₁ w₂}
(h₁ : step_unop op v w₁) (h₂ : step_unop op v w₂) : w₁ = w₂ :=
by cases h₁; cases h₂; refl
theorem default.determ {Γ} (ok : okind Γ) {ts v₁ v₂}
(h₁ : default Γ ts v₁)
(h₂ : default Γ ts v₂) : v₁ = v₂ :=
begin
induction h₁ generalizing v₂; try {cases h₂}; try {refl},
{ cases get_sdef_determ ok h₁_a h₂_a,
exact h₁_ih h₂_a_1 },
{ generalize_hyp e : alist.cons h₁_Δ h₁_x h₁_τ h₁_h = Δ' at h₂,
cases h₂, {cases e},
rcases alist.cons_inj e with ⟨⟨⟩, rfl⟩,
cases h₁_ih_a h₂_a, cases h₁_ih_a_1 h₂_a_1, refl }
end
theorem is_nth.determ {n v v₁ v₂}
(h₁ : is_nth n v v₁) (h₂ : is_nth n v v₂) : v₁ = v₂ :=
by induction h₁ generalizing v₂; cases h₂;
[refl, {cases h₁_ih h₂_a, refl}]
end value
namespace addr
theorem get.determ {H η a v₁ v₂}
(h₁ : get H η a v₁) (h₂ : get H η a v₂) : v₁ = v₂ :=
begin
induction h₁ generalizing v₂; cases h₂,
{ exact option.mem_unique h₁_a h₂_a },
{ exact option.mem_unique h₁_a h₂_a },
{ cases h₁_ih h₂_a_1, refl },
{ cases h₁_ih h₂_a_1, refl },
{ cases h₁_ih h₂_a_1, exact h₁_a_2.determ h₂_a_2 },
{ cases value.of_map_inj (h₁_ih h₂_a_1),
exact option.mem_unique h₁_a_2 h₂_a_2 }
end
theorem get_len.determ {H η a v₁ v₂}
(h₁ : get_len H η a v₁)
(h₂ : get_len H η a v₂) : v₁ = v₂ :=
by cases h₁; cases h₂; cases h₁_a_1.determ h₂_a_1; refl
theorem update_at.determ
{α} {R : α → α → Prop} (Rd : ∀ x y₁ y₂, R x y₁ → R x y₂ → y₁ = y₂) :
∀ {n l l₁ l₂}, list.update_at R n l l₁ → list.update_at R n l l₂ → l₁ = l₂
| _ _ _ _ (@list.update_at.one _ _ a b l r) (@list.update_at.one _ _ _ b' _ r') :=
by rw Rd _ _ _ r r'
| _ _ _ _ (@list.update_at.cons _ _ n a l r h) (@list.update_at.cons _ _ _ _ _ r' h') :=
by rw update_at.determ h h'
theorem at_head.determ
{R : value → value → Prop} (Rd : ∀ x y₁ y₂, R x y₁ → R x y₂ → y₁ = y₂)
(x y₁ y₂) (h₁ : value.at_head R x y₁) (h₂ : value.at_head R x y₂) : y₁ = y₂ :=
by cases h₁; cases h₂; rw Rd _ _ _ h₁_a h₂_a
theorem at_tail.determ
{R : value → value → Prop} (Rd : ∀ x y₁ y₂, R x y₁ → R x y₂ → y₁ = y₂)
(x y₁ y₂) (h₁ : value.at_tail R x y₁) (h₂ : value.at_tail R x y₂) : y₁ = y₂ :=
by cases h₁; cases h₂; rw Rd _ _ _ h₁_a h₂_a
theorem at_nth'.determ
{R : value → value → Prop} (Rd : ∀ x y₁ y₂, R x y₁ → R x y₂ → y₁ = y₂)
: ∀ {n} x y₁ y₂, value.at_nth' R n x y₁ → value.at_nth' R n x y₂ → y₁ = y₂
| 0 := at_head.determ Rd
| (n+1) := at_tail.determ at_nth'.determ
theorem at_nth.determ
{R : value → value → Prop} (Rd : ∀ x y₁ y₂, R x y₁ → R x y₂ → y₁ = y₂)
{n} (x y₁ y₂) (h₁ : value.at_nth R n x y₁) (h₂ : value.at_nth R n x y₂) : y₁ = y₂ :=
by cases h₁; cases h₂; rw at_nth'.determ Rd _ _ _ h₁_a_1 h₂_a_1
theorem at_field.determ
{R : value → value → Prop} (Rd : ∀ x y₁ y₂, R x y₁ → R x y₂ → y₁ = y₂)
{f} (x y₁ y₂) (h₁ : value.at_field R f x y₁) (h₂ : value.at_field R f x y₂) : y₁ = y₂ :=
begin
rcases h₁ with ⟨_, _, vs, x, y, r, m, e, rfl⟩,
rcases h₂ with ⟨_, _, vs', x', y', r', m', e', rfl⟩,
cases value.of_map_inj (e.symm.trans e'),
cases option.mem_unique m m',
rw Rd _ _ _ r r'
end
theorem update.determ {H η a H₁ η₁ H₂ η₂}
{R : value → value → Prop} (Rd : ∀ x y₁ y₂, R x y₁ → R x y₂ → y₁ = y₂)
(h₁ : update H η R a H₁ η₁)
(h₂ : update H η R a H₂ η₂) : (H₁, η₁) = (H₂, η₂) :=
begin
induction h₁ generalizing H₂ η₂; cases h₂,
{ cases update_at.determ Rd h₁_a h₂_a, refl },
{ substs h₁_η' η₂, cases option.mem_unique h₁_a h₂_a,
cases Rd _ _ _ h₁_a_1 h₂_a_1, refl },
{ exact h₁_ih (at_head.determ Rd) h₂_a_1 },
{ exact h₁_ih (at_tail.determ Rd) h₂_a_1 },
{ exact h₁_ih (at_nth.determ Rd) h₂_a_1 },
{ exact h₁_ih (at_field.determ Rd) h₂_a_1 }
end
theorem eq.determ {v : value} (_ : value) : ∀ y₁ y₂, v = y₁ → v = y₂ → y₁ = y₂
| _ _ rfl h := h
end addr
theorem step_deref.determ {C a K s₁ s₂}
(h₁ : step_deref C a K s₁) (h₂ : step_deref C a K s₂) : s₁ = s₂ :=
by cases h₁; cases h₂; [refl, {cases h₁_a_1.determ h₂_a_1, refl}]
theorem step_ret.determ {C v s₁ s₂}
(h₁ : step_ret C v s₁) (h₂ : step_ret C v s₂) : s₁ = s₂ :=
by cases h₁; cases h₂; refl
theorem step_call.determ {Γ : ast} (ok : Γ.okind)
{vs xτs η₁ η₂}
(h₁ : step_call xτs vs η₁) (h₂ : step_call xτs vs η₂) : η₁ = η₂ :=
begin
induction h₁ with Δ x τ v vs η h sc IH generalizing η₂,
{ cases h₂, refl },
{ generalize_hyp e₁ : alist.cons Δ x τ _ = Δ' at h₂,
cases h₂, rcases alist.cons_inj e₁ with ⟨⟨⟩, rfl⟩,
cases IH h₂_a, refl }
end
theorem step_alloc.determ {C v K s₁ s₂}
(sa₁ : step_alloc C v K s₁) (sa₂ : step_alloc C v K s₂) : s₁ = s₂ :=
by cases sa₁; cases sa₂; refl
theorem index_not_lt_zero {i : int32} {n : ℕ} (e : (i : ℤ) = n) : ¬ i < 0 :=
not_lt_of_le $ by rw [← int32.coe_le, e, int32.coe_zero]; apply int.coe_nat_nonneg
theorem index_not_lt_zero_or {i : int32} {j n : ℕ}
(e : (i : ℤ) = j) (lt : j < n) : ¬ (i < 0 ∨ (n : ℤ) ≤ i)
| (or.inl h) := index_not_lt_zero e h
| (or.inr h) := not_lt_of_le h $ by rwa [e, int.coe_nat_lt]
inductive io_equiv : io → state → io → state → Prop
| none {s} : io_equiv none s none s
| some {i o₁ o₂ s₁ s₂} : (o₁ = o₂ → s₁ = s₂) →
io_equiv (some (i, o₁)) s₁ (some (i, o₂)) s₂
theorem determ {Γ : ast} (ok : Γ.ok) {s o₁ s₁ o₂ s₂}
(h₁ : step Γ s o₁ s₁) (h₂ : step Γ s o₂ s₂) : io_equiv o₁ s₁ o₂ s₂ :=
begin
cases h₁,
case c0.step.asgn_var₁ : C lv x e K h {
cases h₂,
case c0.step.asgn₁ : _ _ _ _ h' { rw h' at h, cases h },
case c0.step.asgn_var₁ : _ _ _ _ y h' {
cases option.mem_unique h h', constructor } },
case c0.step.asgn₁ : C lv e K h {
cases h₂,
case c0.step.asgn_var₁ : _ _ _ _ x h' { rw h at h', cases h' },
case c0.step.asgn₁ : h' { constructor } },
case c0.step.asgn₃ : H H' S η η' a v K h {
cases h₂,
rcases h.determ addr.eq.determ h₂_a_1 with ⟨rfl, rfl⟩,
constructor },
case c0.step.asnop₂ : _ C a op e K h {
cases h₂, cases step_deref.determ h h₂_a_1, constructor },
case c0.step.ret₂ : _ C v h {
cases h₂, cases h.determ h₂_a, constructor },
case c0.step.ret_none : _ C v h {
cases h₂, cases h.determ h₂_a, constructor },
case c0.step.nop₁ : _ C h {
cases h₂, cases h.determ h₂_a, constructor },
case c0.step.var : C i v K h {
cases h₂, cases option.mem_unique h h₂_a, constructor },
case c0.step.binop₃ : C op v₁ v₂ v K h {
cases h₂; cases h.determ h₂_a; constructor },
case c0.step.binop_err : C op v₁ v₂ err K h {
cases h₂; cases h.determ h₂_a; constructor },
case c0.step.unop₂ : C op v v₁ K h {
cases h₂, cases h.determ h₂_a, constructor },
case c0.step.call₂ : H S η η₁ f τ₁ xτs₁ s₁ vs K hb₁ sc₁ {
cases h₂,
case c0.step.call₂ : _ _ _ _ _ _ η₂ τ₂ xτs₂ s₂ hb₂ sc₂ {
cases hb₁.determ ok.ind hb₂,
cases sc₁.determ ok.ind sc₂,
constructor },
case c0.step.call_extern : _ _ _ _ _ _ H' v' h' {
cases ok.header_no_def h' ⟨_, _, _, hb₁⟩ } },
case c0.step.call_extern : H S η f vs H' v K h {
cases h₂,
case c0.step.call₂ : _ _ _ _ _ _ η₂ τ₂ xτs₂ s₂ hb₂ sc₂ {
cases ok.header_no_def h ⟨_, _, _, hb₂⟩ },
case c0.step.call_extern : H' v' h' {
constructor, rintro ⟨⟩, refl } },
case c0.step.deref' : _ C a K h {
cases h₂, cases h.determ h₂_a_1, constructor },
case c0.step.alloc_ref : _ C τ τ' v K tτ v0 sa {
cases h₂,
cases tτ.determ ok.ind h₂_a,
cases v0.determ ok.ind h₂_a_1,
cases sa.determ h₂_a_2, constructor },
case c0.step.alloc_arr₁ : C τ τ' e K tτ {
cases h₂, cases tτ.determ ok.ind h₂_a, constructor },
case c0.step.alloc_arr₂ : _ C τ v K i n e v0 sa {
cases h₂,
case c0.step.alloc_arr₂ : _ _ _ _ _ v' n' e' v0' sa' {
cases v0.determ ok.ind v0',
cases int.coe_nat_inj (e.symm.trans e'),
cases sa.determ sa', constructor },
case c0.step.alloc_arr_err : _ _ _ _ h' {
cases index_not_lt_zero e h' } },
case c0.step.alloc_arr_err : C τ i K h {
cases h₂,
case c0.step.alloc_arr₂ : _ _ _ _ _ v' n' e' v0' sa' {
cases index_not_lt_zero e' h },
case c0.step.alloc_arr_err : h' { constructor } },
case c0.step.addr_index₃ : C a n K i j hl e lt {
cases h₂,
case c0.step.addr_index₃ : _ _ _ _ n' j' hl' e' lt' {
cases int.coe_nat_inj (e.symm.trans e'), constructor },
case c0.step.addr_index_err₂ : _ _ _ _ n' hl' lt' {
cases hl.determ hl',
cases index_not_lt_zero_or e lt lt' } },
case c0.step.addr_index_err₂ : C a n K i hl lt {
cases h₂,
case c0.step.addr_index₃ : _ _ _ _ n' j' hl' e' lt' {
cases hl.determ hl',
cases index_not_lt_zero_or e' lt' lt },
case c0.step.addr_index_err₂ : n' hl' lt' { constructor } },
all_goals {{ cases h₂; constructor }}
end
theorem determ' {Γ : ast} (ok : Γ.ok) {s o s₁ s₂}
(h₁ : step Γ s o s₁) (h₂ : step Γ s o s₂) : s₁ = s₂ :=
by cases determ ok h₁ h₂; [refl, exact a rfl]
end c0
|
45abf15806f84828611c415ba4b263df46436b61 | a1179fa077c09acc49e4fbc8d67084ba89ac4f4c | /src/sublattice.lean | b174f36e32424a8ecc3e25c814dc2124aada96e5 | [] | no_license | Seeram/Lean-proof-assistant | 11ca0ca0e0446bacdd1773c4c481a3653b2f1074 | e672d46e0e5f39d8de2933ad4f4cac095ca6094f | refs/heads/master | 1,682,754,224,366 | 1,620,959,431,000 | 1,620,959,431,000 | 299,000,950 | 0 | 1 | null | 1,620,680,462,000 | 1,601,200,258,000 | Lean | UTF-8 | Lean | false | false | 2,914 | lean | import order.lattice
variables { L : Type* } [ lattice L]
set_option old_structure_cmd true
structure sublattice ( L : Type* ) [lattice L] :=
( carrier : set L)
( inf_mem' {a b : L} : a ∈ carrier → b ∈ carrier → a ⊓ b ∈ carrier )
( sup_mem' {a b : L} : a ∈ carrier → b ∈ carrier → a ⊔ b ∈ carrier )
namespace sublattice
instance : has_coe ( sublattice L ) (set L)
:=
{ coe := sublattice.carrier }
instance : has_mem L ( sublattice L )
:=
⟨ λ m S, m ∈ ( S : set L) ⟩
instance : has_coe_to_sort ( sublattice L )
:=
⟨ Type*, λ S, { x : L // x ∈ S } ⟩
lemma mem_coe {SL : sublattice L} {x : L} :
x ∈ SL.carrier ↔ x ∈ SL
:=
iff.rfl
protected lemma sublattice.exists {SL : sublattice L} {p : SL → Prop} :
(∃ x : SL, p x) ↔ ∃ x ∈ SL, p ⟨x, ‹x ∈ SL›⟩
:=
set_coe.exists
protected lemma sublattice.forall {SL : sublattice L} {p : SL → Prop} :
(∀ x : SL, p x) ↔ ∀ x ∈ SL, p ⟨x, ‹x ∈ SL›⟩
:=
set_coe.forall
theorem ext' ⦃ A B : sublattice L ⦄ (h : (A : set L) = B) :
A = B
:=
by cases A; cases B; congr'
theorem ext {A B : sublattice L} (h : ∀ x, x ∈ A ↔ x ∈ B) :
A = B
:=
ext' $ set.ext h
def copy (SL : sublattice L) (S : set L) (hs : S = SL) :
sublattice L
:=
{
carrier := S,
inf_mem' := hs.symm ▸ SL.inf_mem',
sup_mem' := hs.symm ▸ SL.sup_mem'
}
variable { SL : sublattice L}
lemma coe_copy {B : sublattice L} {A : set L} (hs : A = SL) :
(SL.copy A hs : set L) = A
:=
rfl
lemma copy_eq {s : set L} (hs : s = SL) :
SL.copy s hs = SL
:=
ext' hs
variable (SL)
theorem inf_mem {a b : L} :
a ∈ SL → b ∈ SL → a ⊓ b ∈ SL
:=
sublattice.inf_mem' SL
theorem sup_mem {a b : L} :
a ∈ SL → b ∈ SL → a ⊔ b ∈ SL
:=
sublattice.sup_mem' SL
lemma coe_injective :
function.injective (coe : SL → L)
:=
subtype.val_injective
instance to_lattice {M : Type*} [lattice L] (SL : sublattice L) :
lattice L
:=
SL.coe_injective.lattice
end sublattice
variables [lattice α] {a b c d : α}
open function
protected def function.injective.lattice { β : Type*}
[ has_inf β ] [ has_sup β ]
(f : β → α )
(hf: injective f)
(inf : ∀ a b, f(a ⊓ b) = f(a) ⊓ f(b) )
(sup : ∀ a b, f(a ⊔ b) = f(a) ⊔ f(b) )
:
lattice α
:=
{
le := ( ≤ ),
le_refl := λ x, le_refl x,
le_trans := λ x y z, le_trans,
le_antisymm := ,
-- le_antisymm := λ x y hx hy, ,
sup := ( ⊔ ),
le_sup_left := λ x y, show x ≤ _, from ,
le_sup_right := λ x y, show y ≤ _, from ,
sup_le := λ x y z hxz hyz, lfp_le $ sup_le (sup_le hxz hyz) (z.2.symm ▸ le_refl z.1),
inf := ( ⊓ ),
inf_le_left := λ x y, show _ ≤ x, from ,
inf_le_right := λ x y, show _ ≤ y, from ,
le_inf := λ x y z hxy hxz, ,
}
end lattice |
6e7b39011dbef983db591caf7b50881a5b1ad376 | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/init/algebra/ordered_group.lean | 207acd2577f850b3ee919d28688237a06e36312f | [
"Apache-2.0"
] | permissive | Solertis/lean | 491e0939957486f664498fbfb02546e042699958 | 84188c5aa1673fdf37a082b2de8562dddf53df3f | refs/heads/master | 1,610,174,257,606 | 1,486,263,620,000 | 1,486,263,620,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 20,206 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
prelude
import init.algebra.order init.algebra.group
/- Make sure instances defined in this file have lower priority than the ones
defined for concrete structures -/
set_option default_priority 100
universe variable u
class ordered_cancel_comm_monoid (α : Type u)
extends add_comm_monoid α, add_left_cancel_semigroup α,
add_right_cancel_semigroup α, order_pair α :=
(add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b)
(le_of_add_le_add_left : ∀ a b c : α, a + b ≤ a + c → b ≤ c)
(add_lt_add_left : ∀ a b : α, a < b → ∀ c : α, c + a < c + b)
(lt_of_add_lt_add_left : ∀ a b c : α, a + b < a + c → b < c)
section ordered_cancel_comm_monoid
variable {α : Type u}
variable [s : ordered_cancel_comm_monoid α]
lemma add_le_add_left {a b : α} (h : a ≤ b) (c : α) : c + a ≤ c + b :=
@ordered_cancel_comm_monoid.add_le_add_left α s a b h c
lemma le_of_add_le_add_left {a b c : α} (h : a + b ≤ a + c) : b ≤ c :=
@ordered_cancel_comm_monoid.le_of_add_le_add_left α s a b c h
lemma add_lt_add_left {a b : α} (h : a < b) (c : α) : c + a < c + b :=
@ordered_cancel_comm_monoid.add_lt_add_left α s a b h c
lemma lt_of_add_lt_add_left {a b c : α} (h : a + b < a + c) : b < c :=
@ordered_cancel_comm_monoid.lt_of_add_lt_add_left α s a b c h
end ordered_cancel_comm_monoid
section ordered_cancel_comm_monoid
variable {α : Type u}
variable [ordered_cancel_comm_monoid α]
lemma add_le_add_right {a b : α} (h : a ≤ b) (c : α) : a + c ≤ b + c :=
add_comm c a ▸ add_comm c b ▸ add_le_add_left h c
theorem add_lt_add_right {a b : α} (h : a < b) (c : α) : a + c < b + c :=
begin
rw [add_comm a c, add_comm b c],
exact (add_lt_add_left h c)
end
lemma add_le_add {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d :=
le_trans (add_le_add_right h₁ c) (add_le_add_left h₂ b)
lemma le_add_of_nonneg_right {a b : α} (h : b ≥ 0) : a ≤ a + b :=
have a + b ≥ a + 0, from add_le_add_left h a,
by rwa add_zero at this
lemma le_add_of_nonneg_left {a b : α} (h : b ≥ 0) : a ≤ b + a :=
have 0 + a ≤ b + a, from add_le_add_right h a,
by rwa zero_add at this
lemma add_lt_add {a b c d : α} (h₁ : a < b) (h₂ : c < d) : a + c < b + d :=
lt_trans (add_lt_add_right h₁ c) (add_lt_add_left h₂ b)
lemma add_lt_add_of_le_of_lt {a b c d : α} (h₁ : a ≤ b) (h₂ : c < d) : a + c < b + d :=
lt_of_le_of_lt (add_le_add_right h₁ c) (add_lt_add_left h₂ b)
lemma add_lt_add_of_lt_of_le {a b c d : α} (h₁ : a < b) (h₂ : c ≤ d) : a + c < b + d :=
lt_of_lt_of_le (add_lt_add_right h₁ c) (add_le_add_left h₂ b)
lemma lt_add_of_pos_right (a : α) {b : α} (h : b > 0) : a < a + b :=
have a + 0 < a + b, from add_lt_add_left h a,
by rwa [add_zero] at this
lemma lt_add_of_pos_left (a : α) {b : α} (h : b > 0) : a < b + a :=
have 0 + a < b + a, from add_lt_add_right h a,
by rwa [zero_add] at this
lemma le_of_add_le_add_right {a b c : α} (h : a + b ≤ c + b) : a ≤ c :=
le_of_add_le_add_left
(show b + a ≤ b + c, begin rw [add_comm b a, add_comm b c], assumption end)
lemma lt_of_add_lt_add_right {a b c : α} (h : a + b < c + b) : a < c :=
lt_of_add_lt_add_left
(show b + a < b + c, begin rw [add_comm b a, add_comm b c], assumption end)
-- here we start using properties of zero.
lemma add_nonneg {a b : α} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=
zero_add (0:α) ▸ (add_le_add ha hb)
lemma add_pos {a b : α} (ha : 0 < a) (hb : 0 < b) : 0 < a + b :=
zero_add (0:α) ▸ (add_lt_add ha hb)
lemma add_pos_of_pos_of_nonneg {a b : α} (ha : 0 < a) (hb : 0 ≤ b) : 0 < a + b :=
zero_add (0:α) ▸ (add_lt_add_of_lt_of_le ha hb)
lemma add_pos_of_nonneg_of_pos {a b : α} (ha : 0 ≤ a) (hb : 0 < b) : 0 < a + b :=
zero_add (0:α) ▸ (add_lt_add_of_le_of_lt ha hb)
lemma add_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : a + b ≤ 0 :=
zero_add (0:α) ▸ (add_le_add ha hb)
lemma add_neg {a b : α} (ha : a < 0) (hb : b < 0) : a + b < 0 :=
zero_add (0:α) ▸ (add_lt_add ha hb)
lemma add_neg_of_neg_of_nonpos {a b : α} (ha : a < 0) (hb : b ≤ 0) : a + b < 0 :=
zero_add (0:α) ▸ (add_lt_add_of_lt_of_le ha hb)
lemma add_neg_of_nonpos_of_neg {a b : α} (ha : a ≤ 0) (hb : b < 0) : a + b < 0 :=
zero_add (0:α) ▸ (add_lt_add_of_le_of_lt ha hb)
lemma add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg
{a b : α} (ha : 0 ≤ a) (hb : 0 ≤ b) : a + b = 0 ↔ a = 0 ∧ b = 0 :=
iff.intro
(assume hab : a + b = 0,
have ha' : a ≤ 0, from
calc
a = a + 0 : by rw add_zero
... ≤ a + b : add_le_add_left hb _
... = 0 : hab,
have haz : a = 0, from le_antisymm ha' ha,
have hb' : b ≤ 0, from
calc
b = 0 + b : by rw zero_add
... ≤ a + b : by exact add_le_add_right ha _
... = 0 : hab,
have hbz : b = 0, from le_antisymm hb' hb,
and.intro haz hbz)
(assume ⟨ha', hb'⟩,
by rw [ha', hb', add_zero])
lemma le_add_of_nonneg_of_le {a b c : α} (ha : 0 ≤ a) (hbc : b ≤ c) : b ≤ a + c :=
zero_add b ▸ add_le_add ha hbc
lemma le_add_of_le_of_nonneg {a b c : α} (hbc : b ≤ c) (ha : 0 ≤ a) : b ≤ c + a :=
add_zero b ▸ add_le_add hbc ha
lemma lt_add_of_pos_of_le {a b c : α} (ha : 0 < a) (hbc : b ≤ c) : b < a + c :=
zero_add b ▸ add_lt_add_of_lt_of_le ha hbc
lemma lt_add_of_le_of_pos {a b c : α} (hbc : b ≤ c) (ha : 0 < a) : b < c + a :=
add_zero b ▸ add_lt_add_of_le_of_lt hbc ha
lemma add_le_of_nonpos_of_le {a b c : α} (ha : a ≤ 0) (hbc : b ≤ c) : a + b ≤ c :=
zero_add c ▸ add_le_add ha hbc
lemma add_le_of_le_of_nonpos {a b c : α} (hbc : b ≤ c) (ha : a ≤ 0) : b + a ≤ c :=
add_zero c ▸ add_le_add hbc ha
lemma add_lt_of_neg_of_le {a b c : α} (ha : a < 0) (hbc : b ≤ c) : a + b < c :=
zero_add c ▸ add_lt_add_of_lt_of_le ha hbc
lemma add_lt_of_le_of_neg {a b c : α} (hbc : b ≤ c) (ha : a < 0) : b + a < c :=
add_zero c ▸ add_lt_add_of_le_of_lt hbc ha
lemma lt_add_of_nonneg_of_lt {a b c : α} (ha : 0 ≤ a) (hbc : b < c) : b < a + c :=
zero_add b ▸ add_lt_add_of_le_of_lt ha hbc
lemma lt_add_of_lt_of_nonneg {a b c : α} (hbc : b < c) (ha : 0 ≤ a) : b < c + a :=
add_zero b ▸ add_lt_add_of_lt_of_le hbc ha
lemma lt_add_of_pos_of_lt {a b c : α} (ha : 0 < a) (hbc : b < c) : b < a + c :=
zero_add b ▸ add_lt_add ha hbc
lemma lt_add_of_lt_of_pos {a b c : α} (hbc : b < c) (ha : 0 < a) : b < c + a :=
add_zero b ▸ add_lt_add hbc ha
lemma add_lt_of_nonpos_of_lt {a b c : α} (ha : a ≤ 0) (hbc : b < c) : a + b < c :=
zero_add c ▸ add_lt_add_of_le_of_lt ha hbc
lemma add_lt_of_lt_of_nonpos {a b c : α} (hbc : b < c) (ha : a ≤ 0) : b + a < c :=
add_zero c ▸ add_lt_add_of_lt_of_le hbc ha
lemma add_lt_of_neg_of_lt {a b c : α} (ha : a < 0) (hbc : b < c) : a + b < c :=
zero_add c ▸ add_lt_add ha hbc
lemma add_lt_of_lt_of_neg {a b c : α} (hbc : b < c) (ha : a < 0) : b + a < c :=
add_zero c ▸ add_lt_add hbc ha
end ordered_cancel_comm_monoid
class ordered_comm_group (α : Type u) extends add_comm_group α, order_pair α :=
(add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b)
(add_lt_add_left : ∀ a b : α, a < b → ∀ c : α, c + a < c + b)
section ordered_comm_group
variable {α : Type u}
variable [ordered_comm_group α]
lemma ordered_comm_group.le_of_add_le_add_left {a b c : α} (h : a + b ≤ a + c) : b ≤ c :=
have -a + (a + b) ≤ -a + (a + c), from ordered_comm_group.add_le_add_left _ _ h _,
begin simp [neg_add_cancel_left] at this, assumption end
lemma ordered_comm_group.lt_of_add_lt_add_left {a b c : α} (h : a + b < a + c) : b < c :=
have -a + (a + b) < -a + (a + c), from ordered_comm_group.add_lt_add_left _ _ h _,
begin simp [neg_add_cancel_left] at this, assumption end
end ordered_comm_group
instance ordered_comm_group.to_ordered_cancel_comm_monoid (α : Type u) [s : ordered_comm_group α] : ordered_cancel_comm_monoid α :=
{ s with
add_left_cancel := @add_left_cancel α _,
add_right_cancel := @add_right_cancel α _,
le_of_add_le_add_left := @ordered_comm_group.le_of_add_le_add_left α _,
lt_of_add_lt_add_left := @ordered_comm_group.lt_of_add_lt_add_left α _ }
section ordered_comm_group
variables {α : Type u} [ordered_comm_group α]
lemma neg_le_neg {a b : α} (h : a ≤ b) : -b ≤ -a :=
have 0 ≤ -a + b, from add_left_neg a ▸ add_le_add_left h (-a),
have 0 + -b ≤ -a + b + -b, from add_le_add_right this (-b),
by rwa [add_neg_cancel_right, zero_add] at this
lemma le_of_neg_le_neg {a b : α} (h : -b ≤ -a) : a ≤ b :=
suffices -(-a) ≤ -(-b), from
begin simp [neg_neg] at this, assumption end,
neg_le_neg h
lemma nonneg_of_neg_nonpos {a : α} (h : -a ≤ 0) : 0 ≤ a :=
have -a ≤ -0, by rwa neg_zero,
le_of_neg_le_neg this
lemma neg_nonpos_of_nonneg {a : α} (h : 0 ≤ a) : -a ≤ 0 :=
have -a ≤ -0, from neg_le_neg h,
by rwa neg_zero at this
lemma nonpos_of_neg_nonneg {a : α} (h : 0 ≤ -a) : a ≤ 0 :=
have -0 ≤ -a, by rwa neg_zero,
le_of_neg_le_neg this
lemma neg_nonneg_of_nonpos {a : α} (h : a ≤ 0) : 0 ≤ -a :=
have -0 ≤ -a, from neg_le_neg h,
by rwa neg_zero at this
lemma neg_lt_neg {a b : α} (h : a < b) : -b < -a :=
have 0 < -a + b, from add_left_neg a ▸ add_lt_add_left h (-a),
have 0 + -b < -a + b + -b, from add_lt_add_right this (-b),
by rwa [add_neg_cancel_right, zero_add] at this
lemma lt_of_neg_lt_neg {a b : α} (h : -b < -a) : a < b :=
neg_neg a ▸ neg_neg b ▸ neg_lt_neg h
lemma pos_of_neg_neg {a : α} (h : -a < 0) : 0 < a :=
have -a < -0, by rwa neg_zero,
lt_of_neg_lt_neg this
lemma neg_neg_of_pos {a : α} (h : 0 < a) : -a < 0 :=
have -a < -0, from neg_lt_neg h,
by rwa neg_zero at this
lemma neg_of_neg_pos {a : α} (h : 0 < -a) : a < 0 :=
have -0 < -a, by rwa neg_zero,
lt_of_neg_lt_neg this
lemma neg_pos_of_neg {a : α} (h : a < 0) : 0 < -a :=
have -0 < -a, from neg_lt_neg h,
by rwa neg_zero at this
lemma le_neg_of_le_neg {a b : α} (h : a ≤ -b) : b ≤ -a :=
begin
note h := neg_le_neg h,
rwa neg_neg at h
end
lemma neg_le_of_neg_le {a b : α} (h : -a ≤ b) : -b ≤ a :=
begin
note h := neg_le_neg h,
rwa neg_neg at h
end
lemma lt_neg_of_lt_neg {a b : α} (h : a < -b) : b < -a :=
begin
note h := neg_lt_neg h,
rwa neg_neg at h
end
lemma neg_lt_of_neg_lt {a b : α} (h : -a < b) : -b < a :=
begin
note h := neg_lt_neg h,
rwa neg_neg at h
end
lemma sub_nonneg_of_le {a b : α} (h : b ≤ a) : 0 ≤ a - b :=
begin
note h := add_le_add_right h (-b),
rwa add_right_neg at h
end
lemma le_of_sub_nonneg {a b : α} (h : 0 ≤ a - b) : b ≤ a :=
begin
note h := add_le_add_right h b,
rwa [sub_add_cancel, zero_add] at h
end
lemma sub_nonpos_of_le {a b : α} (h : a ≤ b) : a - b ≤ 0 :=
begin
note h := add_le_add_right h (-b),
rwa add_right_neg at h
end
lemma le_of_sub_nonpos {a b : α} (h : a - b ≤ 0) : a ≤ b :=
begin
note h := add_le_add_right h b,
rwa [sub_add_cancel, zero_add] at h
end
lemma sub_pos_of_lt {a b : α} (h : b < a) : 0 < a - b :=
begin
note h := add_lt_add_right h (-b),
rwa add_right_neg at h
end
lemma lt_of_sub_pos {a b : α} (h : 0 < a - b) : b < a :=
begin
note h := add_lt_add_right h b,
rwa [sub_add_cancel, zero_add] at h
end
lemma sub_neg_of_lt {a b : α} (h : a < b) : a - b < 0 :=
begin
note h := add_lt_add_right h (-b),
rwa add_right_neg at h
end
lemma lt_of_sub_neg {a b : α} (h : a - b < 0) : a < b :=
begin
note h := add_lt_add_right h b,
rwa [sub_add_cancel, zero_add] at h
end
lemma add_le_of_le_neg_add {a b c : α} (h : b ≤ -a + c) : a + b ≤ c :=
begin
note h := add_le_add_left h a,
rwa add_neg_cancel_left at h
end
lemma le_neg_add_of_add_le {a b c : α} (h : a + b ≤ c) : b ≤ -a + c :=
begin
note h := add_le_add_left h (-a),
rwa neg_add_cancel_left at h
end
lemma add_le_of_le_sub_left {a b c : α} (h : b ≤ c - a) : a + b ≤ c :=
begin
note h := add_le_add_left h a,
rwa [-add_sub_assoc, add_comm a c, add_sub_cancel] at h
end
lemma le_sub_left_of_add_le {a b c : α} (h : a + b ≤ c) : b ≤ c - a :=
begin
note h := add_le_add_right h (-a),
rwa [add_comm a b, add_neg_cancel_right] at h
end
lemma add_le_of_le_sub_right {a b c : α} (h : a ≤ c - b) : a + b ≤ c :=
begin
note h := add_le_add_right h b,
rwa sub_add_cancel at h
end
lemma le_sub_right_of_add_le {a b c : α} (h : a + b ≤ c) : a ≤ c - b :=
begin
note h := add_le_add_right h (-b),
rwa add_neg_cancel_right at h
end
lemma le_add_of_neg_add_le {a b c : α} (h : -b + a ≤ c) : a ≤ b + c :=
begin
note h := add_le_add_left h b,
rwa add_neg_cancel_left at h
end
lemma neg_add_le_of_le_add {a b c : α} (h : a ≤ b + c) : -b + a ≤ c :=
begin
note h := add_le_add_left h (-b),
rwa neg_add_cancel_left at h
end
lemma le_add_of_sub_left_le {a b c : α} (h : a - b ≤ c) : a ≤ b + c :=
begin
note h := add_le_add_right h b,
rwa [sub_add_cancel, add_comm] at h
end
lemma sub_left_le_of_le_add {a b c : α} (h : a ≤ b + c) : a - b ≤ c :=
begin
note h := add_le_add_right h (-b),
rwa [add_comm b c, add_neg_cancel_right] at h
end
lemma le_add_of_sub_right_le {a b c : α} (h : a - c ≤ b) : a ≤ b + c :=
begin
note h := add_le_add_right h c,
rwa sub_add_cancel at h
end
lemma sub_right_le_of_le_add {a b c : α} (h : a ≤ b + c) : a - c ≤ b :=
begin
note h := add_le_add_right h (-c),
rwa add_neg_cancel_right at h
end
lemma le_add_of_neg_add_le_left {a b c : α} (h : -b + a ≤ c) : a ≤ b + c :=
begin
rw add_comm at h,
exact le_add_of_sub_left_le h
end
lemma neg_add_le_left_of_le_add {a b c : α} (h : a ≤ b + c) : -b + a ≤ c :=
begin
rw add_comm,
exact sub_left_le_of_le_add h
end
lemma le_add_of_neg_add_le_right {a b c : α} (h : -c + a ≤ b) : a ≤ b + c :=
begin
rw add_comm at h,
exact le_add_of_sub_right_le h
end
lemma neg_add_le_right_of_le_add {a b c : α} (h : a ≤ b + c) : -c + a ≤ b :=
begin
rw add_comm at h,
apply neg_add_le_left_of_le_add h
end
lemma le_add_of_neg_le_sub_left {a b c : α} (h : -a ≤ b - c) : c ≤ a + b :=
le_add_of_neg_add_le_left (add_le_of_le_sub_right h)
lemma neg_le_sub_left_of_le_add {a b c : α} (h : c ≤ a + b) : -a ≤ b - c :=
begin
note h := le_neg_add_of_add_le (sub_left_le_of_le_add h),
rwa add_comm at h
end
lemma le_add_of_neg_le_sub_right {a b c : α} (h : -b ≤ a - c) : c ≤ a + b :=
le_add_of_sub_right_le (add_le_of_le_sub_left h)
lemma neg_le_sub_right_of_le_add {a b c : α} (h : c ≤ a + b) : -b ≤ a - c :=
le_sub_left_of_add_le (sub_right_le_of_le_add h)
lemma sub_le_of_sub_le {a b c : α} (h : a - b ≤ c) : a - c ≤ b :=
sub_left_le_of_le_add (le_add_of_sub_right_le h)
lemma sub_le_sub_left {a b : α} (h : a ≤ b) (c : α) : c - b ≤ c - a :=
add_le_add_left (neg_le_neg h) c
lemma sub_le_sub_right {a b : α} (h : a ≤ b) (c : α) : a - c ≤ b - c :=
add_le_add_right h (-c)
lemma sub_le_sub {a b c d : α} (hab : a ≤ b) (hcd : c ≤ d) : a - d ≤ b - c :=
add_le_add hab (neg_le_neg hcd)
lemma add_lt_of_lt_neg_add {a b c : α} (h : b < -a + c) : a + b < c :=
begin
note h := add_lt_add_left h a,
rwa add_neg_cancel_left at h
end
lemma lt_neg_add_of_add_lt {a b c : α} (h : a + b < c) : b < -a + c :=
begin
note h := add_lt_add_left h (-a),
rwa neg_add_cancel_left at h
end
lemma add_lt_of_lt_sub_left {a b c : α} (h : b < c - a) : a + b < c :=
begin
note h := add_lt_add_left h a,
rwa [-add_sub_assoc, add_comm a c, add_sub_cancel] at h
end
lemma lt_sub_left_of_add_lt {a b c : α} (h : a + b < c) : b < c - a :=
begin
note h := add_lt_add_right h (-a),
rwa [add_comm a b, add_neg_cancel_right] at h
end
lemma add_lt_of_lt_sub_right {a b c : α} (h : a < c - b) : a + b < c :=
begin
note h := add_lt_add_right h b,
rwa sub_add_cancel at h
end
lemma lt_sub_right_of_add_lt {a b c : α} (h : a + b < c) : a < c - b :=
begin
note h := add_lt_add_right h (-b),
rwa add_neg_cancel_right at h
end
lemma lt_add_of_neg_add_lt {a b c : α} (h : -b + a < c) : a < b + c :=
begin
note h := add_lt_add_left h b,
rwa add_neg_cancel_left at h
end
lemma neg_add_lt_of_lt_add {a b c : α} (h : a < b + c) : -b + a < c :=
begin
note h := add_lt_add_left h (-b),
rwa neg_add_cancel_left at h
end
lemma lt_add_of_sub_left_lt {a b c : α} (h : a - b < c) : a < b + c :=
begin
note h := add_lt_add_right h b,
rwa [sub_add_cancel, add_comm] at h
end
lemma sub_left_lt_of_lt_add {a b c : α} (h : a < b + c) : a - b < c :=
begin
note h := add_lt_add_right h (-b),
rwa [add_comm b c, add_neg_cancel_right] at h
end
lemma lt_add_of_sub_right_lt {a b c : α} (h : a - c < b) : a < b + c :=
begin
note h := add_lt_add_right h c,
rwa sub_add_cancel at h
end
lemma sub_right_lt_of_lt_add {a b c : α} (h : a < b + c) : a - c < b :=
begin
note h := add_lt_add_right h (-c),
rwa add_neg_cancel_right at h
end
lemma lt_add_of_neg_add_lt_left {a b c : α} (h : -b + a < c) : a < b + c :=
begin
rw add_comm at h,
exact lt_add_of_sub_left_lt h
end
lemma neg_add_lt_left_of_lt_add {a b c : α} (h : a < b + c) : -b + a < c :=
begin
rw add_comm,
exact sub_left_lt_of_lt_add h
end
lemma lt_add_of_neg_add_lt_right {a b c : α} (h : -c + a < b) : a < b + c :=
begin
rw add_comm at h,
exact lt_add_of_sub_right_lt h
end
lemma neg_add_lt_right_of_lt_add {a b c : α} (h : a < b + c) : -c + a < b :=
begin
rw add_comm at h,
apply neg_add_lt_left_of_lt_add h
end
lemma lt_add_of_neg_lt_sub_left {a b c : α} (h : -a < b - c) : c < a + b :=
lt_add_of_neg_add_lt_left (add_lt_of_lt_sub_right h)
lemma neg_lt_sub_left_of_lt_add {a b c : α} (h : c < a + b) : -a < b - c :=
begin
note h := lt_neg_add_of_add_lt (sub_left_lt_of_lt_add h),
rwa add_comm at h
end
lemma lt_add_of_neg_lt_sub_right {a b c : α} (h : -b < a - c) : c < a + b :=
lt_add_of_sub_right_lt (add_lt_of_lt_sub_left h)
lemma neg_lt_sub_right_of_lt_add {a b c : α} (h : c < a + b) : -b < a - c :=
lt_sub_left_of_add_lt (sub_right_lt_of_lt_add h)
lemma sub_lt_of_sub_lt {a b c : α} (h : a - b < c) : a - c < b :=
sub_left_lt_of_lt_add (lt_add_of_sub_right_lt h)
lemma sub_lt_sub_left {a b : α} (h : a < b) (c : α) : c - b < c - a :=
add_lt_add_left (neg_lt_neg h) c
lemma sub_lt_sub_right {a b : α} (h : a < b) (c : α) : a - c < b - c :=
add_lt_add_right h (-c)
lemma sub_lt_sub {a b c d : α} (hab : a < b) (hcd : c < d) : a - d < b - c :=
add_lt_add hab (neg_lt_neg hcd)
lemma sub_lt_sub_of_le_of_lt {a b c d : α} (hab : a ≤ b) (hcd : c < d) : a - d < b - c :=
add_lt_add_of_le_of_lt hab (neg_lt_neg hcd)
lemma sub_lt_sub_of_lt_of_le {a b c d : α} (hab : a < b) (hcd : c ≤ d) : a - d < b - c :=
add_lt_add_of_lt_of_le hab (neg_le_neg hcd)
lemma sub_le_self (a : α) {b : α} (h : b ≥ 0) : a - b ≤ a :=
calc
a - b = a + -b : rfl
... ≤ a + 0 : add_le_add_left (neg_nonpos_of_nonneg h) _
... = a : by rw add_zero
lemma sub_lt_self (a : α) {b : α} (h : b > 0) : a - b < a :=
calc
a - b = a + -b : rfl
... < a + 0 : add_lt_add_left (neg_neg_of_pos h) _
... = a : by rw add_zero
lemma add_le_add_three {a b c d e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) :
a + b + c ≤ d + e + f :=
begin
apply le_trans,
apply add_le_add,
apply add_le_add,
repeat {assumption},
apply le_refl
end
end ordered_comm_group
class decidable_linear_ordered_comm_group (α : Type u)
extends add_comm_group α, decidable_linear_order α :=
(add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b)
(add_lt_add_left : ∀ a b : α, a < b → ∀ c : α, c + a < c + b)
instance decidable_linear_ordered_comm_group.to_ordered_comm_group (α : Type u)
[s : decidable_linear_ordered_comm_group α] : ordered_comm_group α :=
{s with
le_of_lt := @le_of_lt α _,
lt_of_le_of_lt := @lt_of_le_of_lt α _,
lt_of_lt_of_le := @lt_of_lt_of_le α _ }
class decidable_linear_ordered_cancel_comm_monoid (α : Type u)
extends ordered_cancel_comm_monoid α, decidable_linear_order α
|
2b5d5cfcd56f51b746acb91189ca63420ffcd782 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/1820.lean | d4860d6701e794d1b9c13edbf21ff02fe3f2f66a | [
"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 | 242 | lean | def f (p : nat × nat) : nat × nat := {p with}
structure P (A : Type) extends prod A A :=
(third : A)
def g (s : P nat) : nat × nat := {s with}
example : g (P.mk (1, 2) 3) = (1, 2) :=
rfl
example : g ⟨⟨1, 2⟩, 3⟩ = (1, 2) :=
rfl
|
71e927246514c4bff1e670ddd677c7e35ca28967 | 32317185abf7e7c963f4c67c190aec61af6b3628 | /library/data/real/basic.lean | 472a7c11825cfdf44d3e9100c998b6ecd3d16561 | [
"Apache-2.0"
] | permissive | Andrew-Zipperer-unorganized/lean | 198a2317f21198cd8d26e7085e484b86277f17f7 | dcb35008e1474a0abebe632b1dced120e5f8c009 | refs/heads/master | 1,622,526,520,945 | 1,453,576,559,000 | 1,454,612,842,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 42,486 | lean | /-
Copyright (c) 2015 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
The real numbers, constructed as equivalence classes of Cauchy sequences of rationals.
This construction follows Bishop and Bridges (1985).
The construction of the reals is arranged in four files.
- basic.lean proves properties about regular sequences of rationals in the namespace rat_seq,
defines ℝ to be the quotient type of regular sequences mod equivalence, and shows ℝ is a ring
in namespace real. No classical axioms are used.
- order.lean defines an order on regular sequences and lifts the order to ℝ. In the namespace real,
ℝ is shown to be an ordered ring. No classical axioms are used.
- division.lean defines the inverse of a regular sequence and lifts this to ℝ. If a sequence is
equivalent to the 0 sequence, its inverse is the zero sequence. In the namespace real, ℝ is shown
to be an ordered field. This construction is classical.
- complete.lean
-/
import data.nat data.rat.order data.pnat
open nat eq pnat
open - [coercion] rat
local postfix `⁻¹` := pnat.inv
-- small helper lemmas
private theorem s_mul_assoc_lemma_3 (a b n : ℕ+) (p : ℚ) :
p * ((a * n)⁻¹ + (b * n)⁻¹) = p * (a⁻¹ + b⁻¹) * n⁻¹ :=
by rewrite [rat.mul_assoc, right_distrib, *pnat.inv_mul_eq_mul_inv]
private theorem s_mul_assoc_lemma_4 {n : ℕ+} {ε q : ℚ} (Hε : ε > 0) (Hq : q > 0)
(H : n ≥ pceil (q / ε)) :
q * n⁻¹ ≤ ε :=
begin
note H2 := pceil_helper H (div_pos_of_pos_of_pos Hq Hε),
note H3 := mul_le_of_le_div (div_pos_of_pos_of_pos Hq Hε) H2,
rewrite -(one_mul ε),
apply mul_le_mul_of_mul_div_le,
repeat assumption
end
private theorem find_thirds (a b : ℚ) (H : b > 0) : ∃ n : ℕ+, a + n⁻¹ + n⁻¹ + n⁻¹ < a + b :=
let n := pceil (of_nat 4 / b) in
have of_nat 3 * n⁻¹ < b, from calc
of_nat 3 * n⁻¹ < of_nat 4 * n⁻¹
: mul_lt_mul_of_pos_right dec_trivial !pnat.inv_pos
... ≤ of_nat 4 * (b / of_nat 4)
: mul_le_mul_of_nonneg_left (!inv_pceil_div dec_trivial H) !of_nat_nonneg
... = b / of_nat 4 * of_nat 4 : mul.comm
... = b : !div_mul_cancel dec_trivial,
exists.intro n (calc
a + n⁻¹ + n⁻¹ + n⁻¹ = a + (1 + 1 + 1) * n⁻¹ : by rewrite [+right_distrib, +rat.one_mul, -+add.assoc]
... = a + of_nat 3 * n⁻¹ : {show 1+1+1=of_nat 3, from dec_trivial}
... < a + b : rat.add_lt_add_left this a)
private theorem squeeze {a b : ℚ} (H : ∀ j : ℕ+, a ≤ b + j⁻¹ + j⁻¹ + j⁻¹) : a ≤ b :=
begin
apply le_of_not_gt,
intro Hb,
cases exists_add_lt_and_pos_of_lt Hb with [c, Hc],
cases find_thirds b c (and.right Hc) with [j, Hbj],
have Ha : a > b + j⁻¹ + j⁻¹ + j⁻¹, from lt.trans Hbj (and.left Hc),
apply (not_le_of_gt Ha) !H
end
private theorem rewrite_helper (a b c d : ℚ) : a * b - c * d = a * (b - d) + (a - c) * d :=
by rewrite [mul_sub_left_distrib, mul_sub_right_distrib, add_sub, sub_add_cancel]
private theorem rewrite_helper3 (a b c d e f g: ℚ) : a * (b + c) - (d * e + f * g) =
(a * b - d * e) + (a * c - f * g) :=
by rewrite [left_distrib, add_sub_comm]
private theorem rewrite_helper4 (a b c d : ℚ) : a * b - c * d = (a * b - a * d) + (a * d - c * d) :=
by rewrite[add_sub, sub_add_cancel]
private theorem rewrite_helper5 (a b x y : ℚ) : a - b = (a - x) + (x - y) + (y - b) :=
by rewrite[*add_sub, *sub_add_cancel]
private theorem rewrite_helper7 (a b c d x : ℚ) :
a * b * c - d = (b * c) * (a - x) + (x * b * c - d) :=
begin
have ∀ (a b c : ℚ), a * b * c = b * c * a,
begin
intros a b c,
rewrite (mul.right_comm b c a),
rewrite (mul.comm b a)
end,
rewrite [mul_sub_left_distrib, add_sub],
calc
a * b * c - d = a * b * c - x * b * c + x * b * c - d : sub_add_cancel
... = b * c * a - b * c * x + x * b * c - d :
begin
rewrite [this a b c, this x b c]
end
end
private theorem ineq_helper (a b : ℚ) (k m n : ℕ+) (H : a ≤ (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹)
(H2 : b ≤ (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹) :
(rat_of_pnat k) * a + b * (rat_of_pnat k) ≤ m⁻¹ + n⁻¹ :=
assert H3 : (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹ = (2 * k)⁻¹ * (m⁻¹ + n⁻¹),
begin
rewrite [left_distrib, *pnat.inv_mul_eq_mul_inv],
rewrite (mul.comm k⁻¹)
end,
have H' : a ≤ (2 * k)⁻¹ * (m⁻¹ + n⁻¹),
begin
rewrite H3 at H,
exact H
end,
have H2' : b ≤ (2 * k)⁻¹ * (m⁻¹ + n⁻¹),
begin
rewrite H3 at H2,
exact H2
end,
have a + b ≤ k⁻¹ * (m⁻¹ + n⁻¹), from calc
a + b ≤ (2 * k)⁻¹ * (m⁻¹ + n⁻¹) + (2 * k)⁻¹ * (m⁻¹ + n⁻¹) : add_le_add H' H2'
... = ((2 * k)⁻¹ + (2 * k)⁻¹) * (m⁻¹ + n⁻¹) : by rewrite right_distrib
... = k⁻¹ * (m⁻¹ + n⁻¹) : by rewrite (pnat.add_halves k),
calc (rat_of_pnat k) * a + b * (rat_of_pnat k)
= (rat_of_pnat k) * a + (rat_of_pnat k) * b : by rewrite (mul.comm b)
... = (rat_of_pnat k) * (a + b) : left_distrib
... ≤ (rat_of_pnat k) * (k⁻¹ * (m⁻¹ + n⁻¹)) :
iff.mp (!le_iff_mul_le_mul_left !rat_of_pnat_is_pos) this
... = m⁻¹ + n⁻¹ :
by rewrite[-mul.assoc, pnat.inv_cancel_left, one_mul]
private theorem factor_lemma (a b c d e : ℚ) : abs (a + b + c - (d + (b + e))) = abs ((a - d) + (c - e)) :=
!congr_arg (calc
a + b + c - (d + (b + e)) = a + b + c - (d + b + e) : rat.add_assoc
... = a + b - (d + b) + (c - e) : add_sub_comm
... = a + b - b - d + (c - e) : sub_add_eq_sub_sub_swap
... = a - d + (c - e) : add_sub_cancel)
private theorem factor_lemma_2 (a b c d : ℚ) : (a + b) + (c + d) = (a + c) + (d + b) :=
begin
note H := (binary.comm4 add.comm add.assoc a b c d),
rewrite [add.comm b d at H],
exact H
end
--------------------------------------
-- define cauchy sequences and equivalence. show equivalence actually is one
namespace rat_seq
notation `seq` := ℕ+ → ℚ
definition regular (s : seq) := ∀ m n : ℕ+, abs (s m - s n) ≤ m⁻¹ + n⁻¹
definition equiv (s t : seq) := ∀ n : ℕ+, abs (s n - t n) ≤ n⁻¹ + n⁻¹
infix `≡` := equiv
theorem equiv.refl (s : seq) : s ≡ s :=
begin
intros,
rewrite [sub_self, abs_zero],
apply add_invs_nonneg
end
theorem equiv.symm (s t : seq) (H : s ≡ t) : t ≡ s :=
begin
intros,
rewrite [-abs_neg, neg_sub],
exact H n
end
theorem bdd_of_eq {s t : seq} (H : s ≡ t) :
∀ j : ℕ+, ∀ n : ℕ+, n ≥ 2 * j → abs (s n - t n) ≤ j⁻¹ :=
begin
intros [j, n, Hn],
apply le.trans,
apply H,
rewrite -(pnat.add_halves j),
apply add_le_add,
apply inv_ge_of_le Hn,
apply inv_ge_of_le Hn
end
theorem eq_of_bdd {s t : seq} (Hs : regular s) (Ht : regular t)
(H : ∀ j : ℕ+, ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ j⁻¹) : s ≡ t :=
begin
intros,
have Hj : (∀ j : ℕ+, abs (s n - t n) ≤ n⁻¹ + n⁻¹ + j⁻¹ + j⁻¹ + j⁻¹), begin
intros,
cases H j with [Nj, HNj],
rewrite [-(sub_add_cancel (s n) (s (max j Nj))), +sub_eq_add_neg,
add.assoc (s n + -s (max j Nj)), ↑regular at *],
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
apply add_le_add,
apply Hs,
rewrite [-(sub_add_cancel (s (max j Nj)) (t (max j Nj))), add.assoc],
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
apply rat.add_le_add_left,
apply add_le_add,
apply HNj (max j Nj) (pnat.max_right j Nj),
apply Ht,
have hsimp : ∀ m : ℕ+, n⁻¹ + m⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) = n⁻¹ + n⁻¹ + j⁻¹ + (m⁻¹ + m⁻¹),
from λm, calc
n⁻¹ + m⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) = n⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) + m⁻¹ : add.right_comm
... = n⁻¹ + (j⁻¹ + m⁻¹ + n⁻¹) + m⁻¹ : add.assoc
... = n⁻¹ + (n⁻¹ + (j⁻¹ + m⁻¹)) + m⁻¹ : add.comm
... = n⁻¹ + n⁻¹ + j⁻¹ + (m⁻¹ + m⁻¹) :
by rewrite[-*add.assoc],
rewrite hsimp,
have Hms : (max j Nj)⁻¹ + (max j Nj)⁻¹ ≤ j⁻¹ + j⁻¹, begin
apply add_le_add,
apply inv_ge_of_le (pnat.max_left j Nj),
apply inv_ge_of_le (pnat.max_left j Nj),
end,
apply (calc
n⁻¹ + n⁻¹ + j⁻¹ + ((max j Nj)⁻¹ + (max j Nj)⁻¹) ≤ n⁻¹ + n⁻¹ + j⁻¹ + (j⁻¹ + j⁻¹) :
rat.add_le_add_left Hms
... = n⁻¹ + n⁻¹ + j⁻¹ + j⁻¹ + j⁻¹ : by rewrite *rat.add_assoc)
end,
apply squeeze Hj
end
theorem eq_of_bdd_var {s t : seq} (Hs : regular s) (Ht : regular t)
(H : ∀ ε : ℚ, ε > 0 → ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ ε) : s ≡ t :=
begin
apply eq_of_bdd,
repeat assumption,
intros,
apply H,
apply pnat.inv_pos
end
theorem bdd_of_eq_var {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) :
∀ ε : ℚ, ε > 0 → ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ ε :=
begin
intro ε Hε,
cases pnat_bound Hε with [N, HN],
existsi 2 * N,
intro n Hn,
apply rat.le_trans,
apply bdd_of_eq Heq N n Hn,
exact HN -- assumption -- TODO: something funny here; what is 11.source.to_has_le_2?
end
theorem equiv.trans (s t u : seq) (Hs : regular s) (Ht : regular t) (Hu : regular u)
(H : s ≡ t) (H2 : t ≡ u) : s ≡ u :=
begin
apply eq_of_bdd Hs Hu,
intros,
existsi 2 * (2 * j),
intro n Hn,
rewrite [-sub_add_cancel (s n) (t n), *sub_eq_add_neg, add.assoc],
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
have Hst : abs (s n - t n) ≤ (2 * j)⁻¹, from bdd_of_eq H _ _ Hn,
have Htu : abs (t n - u n) ≤ (2 * j)⁻¹, from bdd_of_eq H2 _ _ Hn,
rewrite -(pnat.add_halves j),
apply add_le_add,
exact Hst, exact Htu
end
-----------------------------------
-- define operations on cauchy sequences. show operations preserve regularity
private definition K (s : seq) : ℕ+ := pnat.pos (ubound (abs (s pone)) + 1 + 1) dec_trivial
private theorem canon_bound {s : seq} (Hs : regular s) (n : ℕ+) : abs (s n) ≤ rat_of_pnat (K s) :=
calc
abs (s n) = abs (s n - s pone + s pone) : by rewrite sub_add_cancel
... ≤ abs (s n - s pone) + abs (s pone) : abs_add_le_abs_add_abs
... ≤ n⁻¹ + pone⁻¹ + abs (s pone) : add_le_add_right !Hs
... = n⁻¹ + (1 + abs (s pone)) : by rewrite [pone_inv, rat.add_assoc]
... ≤ 1 + (1 + abs (s pone)) : add_le_add_right (inv_le_one n)
... = abs (s pone) + (1 + 1) :
by rewrite [add.comm 1 (abs (s pone)), add.comm 1, rat.add_assoc]
... ≤ of_nat (ubound (abs (s pone))) + (1 + 1) : add_le_add_right (!ubound_ge)
... = of_nat (ubound (abs (s pone)) + (1 + 1)) : of_nat_add
... = of_nat (ubound (abs (s pone)) + 1 + 1) : add.assoc
... = rat_of_pnat (K s) : by esimp
theorem bdd_of_regular {s : seq} (H : regular s) : ∃ b : ℚ, ∀ n : ℕ+, s n ≤ b :=
begin
existsi rat_of_pnat (K s),
intros,
apply rat.le_trans,
apply le_abs_self,
apply canon_bound H
end
theorem bdd_of_regular_strict {s : seq} (H : regular s) : ∃ b : ℚ, ∀ n : ℕ+, s n < b :=
begin
cases bdd_of_regular H with [b, Hb],
existsi b + 1,
intro n,
apply rat.lt_of_le_of_lt,
apply Hb,
apply lt_add_of_pos_right,
apply zero_lt_one
end
definition K₂ (s t : seq) := max (K s) (K t)
private theorem K₂_symm (s t : seq) : K₂ s t = K₂ t s :=
if H : K s < K t then
(assert H1 : K₂ s t = K t, from pnat.max_eq_right H,
assert H2 : K₂ t s = K t, from pnat.max_eq_left (pnat.not_lt_of_ge (pnat.le_of_lt H)),
by rewrite [H1, -H2])
else
(assert H1 : K₂ s t = K s, from pnat.max_eq_left H,
if J : K t < K s then
(assert H2 : K₂ t s = K s, from pnat.max_eq_right J, by rewrite [H1, -H2])
else
(assert Heq : K t = K s, from
pnat.eq_of_le_of_ge (pnat.le_of_not_gt H) (pnat.le_of_not_gt J),
by rewrite [↑K₂, Heq]))
theorem canon_2_bound_left (s t : seq) (Hs : regular s) (n : ℕ+) :
abs (s n) ≤ rat_of_pnat (K₂ s t) :=
calc
abs (s n) ≤ rat_of_pnat (K s) : canon_bound Hs n
... ≤ rat_of_pnat (K₂ s t) : rat_of_pnat_le_of_pnat_le (!pnat.max_left)
theorem canon_2_bound_right (s t : seq) (Ht : regular t) (n : ℕ+) :
abs (t n) ≤ rat_of_pnat (K₂ s t) :=
calc
abs (t n) ≤ rat_of_pnat (K t) : canon_bound Ht n
... ≤ rat_of_pnat (K₂ s t) : rat_of_pnat_le_of_pnat_le (!pnat.max_right)
definition sadd (s t : seq) : seq := λ n, (s (2 * n)) + (t (2 * n))
theorem reg_add_reg {s t : seq} (Hs : regular s) (Ht : regular t) : regular (sadd s t) :=
begin
rewrite [↑regular at *, ↑sadd],
intros,
rewrite add_sub_comm,
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
rewrite add_halves_double,
apply add_le_add,
apply Hs,
apply Ht
end
definition smul (s t : seq) : seq := λ n : ℕ+, (s ((K₂ s t) * 2 * n)) * (t ((K₂ s t) * 2 * n))
theorem reg_mul_reg {s t : seq} (Hs : regular s) (Ht : regular t) : regular (smul s t) :=
begin
rewrite [↑regular at *, ↑smul],
intros,
rewrite rewrite_helper,
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
apply add_le_add,
rewrite abs_mul,
apply mul_le_mul_of_nonneg_right,
apply canon_2_bound_left s t Hs,
apply abs_nonneg,
rewrite abs_mul,
apply mul_le_mul_of_nonneg_left,
apply canon_2_bound_right s t Ht,
apply abs_nonneg,
apply ineq_helper,
apply Ht,
apply Hs
end
definition sneg (s : seq) : seq := λ n : ℕ+, - (s n)
theorem reg_neg_reg {s : seq} (Hs : regular s) : regular (sneg s) :=
begin
rewrite [↑regular at *, ↑sneg],
intros,
rewrite [-abs_neg, neg_sub, sub_neg_eq_add, add.comm],
apply Hs
end
-----------------------------------
-- show properties of +, *, -
definition zero : seq := λ n, 0
definition one : seq := λ n, 1
theorem s_add_comm (s t : seq) : sadd s t ≡ sadd t s :=
begin
esimp [sadd],
intro n,
rewrite [sub_add_eq_sub_sub, add_sub_cancel, sub_self, abs_zero],
apply add_invs_nonneg
end
theorem s_add_assoc (s t u : seq) (Hs : regular s) (Hu : regular u) :
sadd (sadd s t) u ≡ sadd s (sadd t u) :=
begin
rewrite [↑sadd, ↑equiv, ↑regular at *],
intros,
rewrite factor_lemma,
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
rotate 1,
apply add_le_add_right,
apply inv_two_mul_le_inv,
rewrite [-(pnat.add_halves (2 * n)), -(pnat.add_halves n), factor_lemma_2],
apply add_le_add,
apply Hs,
apply Hu
end
theorem s_mul_comm (s t : seq) : smul s t ≡ smul t s :=
begin
rewrite ↑smul,
intros n,
rewrite [*(K₂_symm s t), rat.mul_comm, sub_self, abs_zero],
apply add_invs_nonneg
end
private definition DK (s t : seq) := (K₂ s t) * 2
private theorem DK_rewrite (s t : seq) : (K₂ s t) * 2 = DK s t := rfl
private definition TK (s t u : seq) := (DK (λ (n : ℕ+), s (mul (DK s t) n) * t (mul (DK s t) n)) u)
private theorem TK_rewrite (s t u : seq) :
(DK (λ (n : ℕ+), s (mul (DK s t) n) * t (mul (DK s t) n)) u) = TK s t u := rfl
private theorem s_mul_assoc_lemma (s t u : seq) (a b c d : ℕ+) :
abs (s a * t a * u b - s c * t d * u d) ≤ abs (t a) * abs (u b) * abs (s a - s c) +
abs (s c) * abs (t a) * abs (u b - u d) + abs (s c) * abs (u d) * abs (t a - t d) :=
begin
rewrite (rewrite_helper7 _ _ _ _ (s c)),
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
rewrite rat.add_assoc,
apply add_le_add,
rewrite 2 abs_mul,
apply le.refl,
rewrite [*rat.mul_assoc, -mul_sub_left_distrib, -left_distrib, abs_mul],
apply mul_le_mul_of_nonneg_left,
rewrite rewrite_helper,
apply le.trans,
apply abs_add_le_abs_add_abs,
apply add_le_add,
rewrite abs_mul, apply rat.le_refl,
rewrite [abs_mul, rat.mul_comm], apply rat.le_refl,
apply abs_nonneg
end
private definition Kq (s : seq) := rat_of_pnat (K s) + 1
private theorem Kq_bound {s : seq} (H : regular s) : ∀ n, abs (s n) ≤ Kq s :=
begin
intros,
apply le_of_lt,
apply lt_of_le_of_lt,
apply canon_bound H,
apply lt_add_of_pos_right,
apply zero_lt_one
end
private theorem Kq_bound_nonneg {s : seq} (H : regular s) : 0 ≤ Kq s :=
le.trans !abs_nonneg (Kq_bound H 2)
private theorem Kq_bound_pos {s : seq} (H : regular s) : 0 < Kq s :=
have H1 : 0 ≤ rat_of_pnat (K s), from rat.le_trans (!abs_nonneg) (canon_bound H 2),
add_pos_of_nonneg_of_pos H1 rat.zero_lt_one
private theorem s_mul_assoc_lemma_5 {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(a b c : ℕ+) : abs (t a) * abs (u b) * abs (s a - s c) ≤ (Kq t) * (Kq u) * (a⁻¹ + c⁻¹) :=
begin
repeat apply mul_le_mul,
apply Kq_bound Ht,
apply Kq_bound Hu,
apply abs_nonneg,
apply Kq_bound_nonneg Ht,
apply Hs,
apply abs_nonneg,
apply rat.mul_nonneg,
apply Kq_bound_nonneg Ht,
apply Kq_bound_nonneg Hu,
end
private theorem s_mul_assoc_lemma_2 {s t u : seq} (Hs : regular s) (Ht : regular t)
(Hu : regular u) (a b c d : ℕ+) :
abs (t a) * abs (u b) * abs (s a - s c) + abs (s c) * abs (t a) * abs (u b - u d)
+ abs (s c) * abs (u d) * abs (t a - t d) ≤
(Kq t) * (Kq u) * (a⁻¹ + c⁻¹) + (Kq s) * (Kq t) * (b⁻¹ + d⁻¹) + (Kq s) * (Kq u) * (a⁻¹ + d⁻¹) :=
begin
apply add_le_add_three,
repeat (assumption | apply mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Hs,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (assumption | apply mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Hu,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (assumption | apply mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Ht,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (apply Kq_bound_nonneg; assumption)
end
theorem s_mul_assoc {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) :
smul (smul s t) u ≡ smul s (smul t u) :=
begin
apply eq_of_bdd_var,
repeat apply reg_mul_reg,
apply Hs,
apply Ht,
apply Hu,
apply reg_mul_reg Hs,
apply reg_mul_reg Ht Hu,
intros,
apply exists.intro,
intros,
rewrite [↑smul, *DK_rewrite, *TK_rewrite, -*pnat.mul_assoc, -*mul.assoc],
apply rat.le_trans,
apply s_mul_assoc_lemma,
apply rat.le_trans,
apply s_mul_assoc_lemma_2,
apply Hs,
apply Ht,
apply Hu,
rewrite [*s_mul_assoc_lemma_3, -distrib_three_right],
apply s_mul_assoc_lemma_4,
apply a,
repeat apply add_pos,
repeat apply mul_pos,
apply Kq_bound_pos Ht,
apply Kq_bound_pos Hu,
apply add_pos,
repeat apply pnat.inv_pos,
repeat apply rat.mul_pos,
apply Kq_bound_pos Hs,
apply Kq_bound_pos Ht,
apply add_pos,
repeat apply pnat.inv_pos,
repeat apply rat.mul_pos,
apply Kq_bound_pos Hs,
apply Kq_bound_pos Hu,
apply add_pos,
repeat apply pnat.inv_pos,
apply a_1
end
theorem zero_is_reg : regular zero :=
begin
rewrite [↑regular, ↑zero],
intros,
rewrite [sub_zero, abs_zero],
apply add_invs_nonneg
end
theorem s_zero_add (s : seq) (H : regular s) : sadd zero s ≡ s :=
begin
rewrite [↑sadd, ↑zero, ↑equiv, ↑regular at H],
intros,
rewrite [rat.zero_add],
apply rat.le_trans,
apply H,
apply add_le_add,
apply inv_two_mul_le_inv,
apply rat.le_refl
end
theorem s_add_zero (s : seq) (H : regular s) : sadd s zero ≡ s :=
begin
rewrite [↑sadd, ↑zero, ↑equiv, ↑regular at H],
intros,
rewrite [rat.add_zero],
apply rat.le_trans,
apply H,
apply add_le_add,
apply inv_two_mul_le_inv,
apply rat.le_refl
end
theorem s_neg_cancel (s : seq) (H : regular s) : sadd (sneg s) s ≡ zero :=
begin
rewrite [↑sadd, ↑sneg, ↑regular at H, ↑zero, ↑equiv],
intros,
rewrite [neg_add_eq_sub, sub_self, sub_zero, abs_zero],
apply add_invs_nonneg
end
theorem neg_s_cancel (s : seq) (H : regular s) : sadd s (sneg s) ≡ zero :=
begin
apply equiv.trans,
rotate 3,
apply s_add_comm,
apply s_neg_cancel s H,
repeat (apply reg_add_reg | apply reg_neg_reg | assumption),
apply zero_is_reg
end
theorem add_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Hv : regular v) (Esu : s ≡ u) (Etv : t ≡ v) : sadd s t ≡ sadd u v :=
begin
rewrite [↑sadd, ↑equiv at *],
intros,
rewrite [add_sub_comm, add_halves_double],
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply add_le_add,
apply Esu,
apply Etv
end
set_option tactic.goal_names false
private theorem mul_bound_helper {s t : seq} (Hs : regular s) (Ht : regular t) (a b c : ℕ+)
(j : ℕ+) :
∃ N : ℕ+, ∀ n : ℕ+, n ≥ N → abs (s (a * n) * t (b * n) - s (c * n) * t (c * n)) ≤ j⁻¹ :=
begin
existsi pceil (((rat_of_pnat (K s)) * (b⁻¹ + c⁻¹) + (a⁻¹ + c⁻¹) *
(rat_of_pnat (K t))) * (rat_of_pnat j)),
intros n Hn,
rewrite rewrite_helper4,
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
rotate 1,
show n⁻¹ * ((rat_of_pnat (K s)) * (b⁻¹ + c⁻¹)) +
n⁻¹ * ((a⁻¹ + c⁻¹) * (rat_of_pnat (K t))) ≤ j⁻¹, begin
rewrite -left_distrib,
apply rat.le_trans,
apply mul_le_mul_of_nonneg_right,
apply pceil_helper Hn,
{ repeat (apply mul_pos | apply add_pos | apply rat_of_pnat_is_pos |
apply pnat.inv_pos) },
apply rat.le_of_lt,
apply add_pos,
apply rat.mul_pos,
apply rat_of_pnat_is_pos,
apply add_pos,
apply pnat.inv_pos,
apply pnat.inv_pos,
apply rat.mul_pos,
apply add_pos,
apply pnat.inv_pos,
apply pnat.inv_pos,
apply rat_of_pnat_is_pos,
have H : (rat_of_pnat (K s) * (b⁻¹ + c⁻¹) + (a⁻¹ + c⁻¹) * rat_of_pnat (K t)) ≠ 0, begin
apply ne_of_gt,
repeat (apply mul_pos | apply add_pos | apply rat_of_pnat_is_pos | apply pnat.inv_pos),
end,
rewrite (!div_helper H),
apply rat.le_refl
end,
apply add_le_add,
rewrite [-mul_sub_left_distrib, abs_mul],
apply rat.le_trans,
apply mul_le_mul,
apply canon_bound,
apply Hs,
apply Ht,
apply abs_nonneg,
apply rat.le_of_lt,
apply rat_of_pnat_is_pos,
rewrite [*pnat.inv_mul_eq_mul_inv, -right_distrib, -rat.mul_assoc, rat.mul_comm],
apply mul_le_mul_of_nonneg_left,
apply rat.le_refl,
apply rat.le_of_lt,
apply pnat.inv_pos,
rewrite [-mul_sub_right_distrib, abs_mul],
apply rat.le_trans,
apply mul_le_mul,
apply Hs,
apply canon_bound,
apply Ht,
apply abs_nonneg,
apply add_invs_nonneg,
rewrite [*pnat.inv_mul_eq_mul_inv, -right_distrib, mul.comm _ n⁻¹, rat.mul_assoc],
apply mul_le_mul,
repeat apply rat.le_refl,
apply rat.le_of_lt,
apply rat.mul_pos,
apply add_pos,
repeat apply pnat.inv_pos,
apply rat_of_pnat_is_pos,
apply rat.le_of_lt,
apply pnat.inv_pos
end
theorem s_distrib {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) :
smul s (sadd t u) ≡ sadd (smul s t) (smul s u) :=
begin
apply eq_of_bdd,
repeat (assumption | apply reg_add_reg | apply reg_mul_reg),
intros,
let exh1 := λ a b c, mul_bound_helper Hs Ht a b c (2 * j),
apply exists.elim,
apply exh1,
rotate 3,
intros N1 HN1,
let exh2 := λ d e f, mul_bound_helper Hs Hu d e f (2 * j),
apply exists.elim,
apply exh2,
rotate 3,
intros N2 HN2,
existsi max N1 N2,
intros n Hn,
rewrite [↑sadd at *, ↑smul, rewrite_helper3, -pnat.add_halves j, -*pnat.mul_assoc at *],
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply add_le_add,
apply HN1,
apply pnat.le_trans,
apply pnat.max_left N1 N2,
apply Hn,
apply HN2,
apply pnat.le_trans,
apply pnat.max_right N1 N2,
apply Hn
end
theorem mul_zero_equiv_zero {s t : seq} (Hs : regular s) (Ht : regular t) (Htz : t ≡ zero) :
smul s t ≡ zero :=
begin
apply eq_of_bdd_var,
apply reg_mul_reg Hs Ht,
apply zero_is_reg,
intro ε Hε,
let Bd := bdd_of_eq_var Ht zero_is_reg Htz (ε / (Kq s))
(div_pos_of_pos_of_pos Hε (Kq_bound_pos Hs)),
cases Bd with [N, HN],
existsi N,
intro n Hn,
rewrite [↑equiv at Htz, ↑zero at *, sub_zero, ↑smul, abs_mul],
apply le.trans,
apply mul_le_mul,
apply Kq_bound Hs,
have HN' : ∀ (n : ℕ+), N ≤ n → abs (t n) ≤ ε / Kq s,
from λ n, (eq.subst (sub_zero (t n)) (HN n)),
apply HN',
apply pnat.le_trans Hn,
apply pnat.mul_le_mul_left,
apply abs_nonneg,
apply le_of_lt (Kq_bound_pos Hs),
rewrite (mul_div_cancel' (ne.symm (ne_of_lt (Kq_bound_pos Hs)))),
apply le.refl
end
private theorem neg_bound_eq_bound (s : seq) : K (sneg s) = K s :=
by rewrite [↑K, ↑sneg, abs_neg]
private theorem neg_bound2_eq_bound2 (s t : seq) : K₂ s (sneg t) = K₂ s t :=
by rewrite [↑K₂, neg_bound_eq_bound]
private theorem sneg_def (s : seq) : (λ (n : ℕ+), -(s n)) = sneg s := rfl
theorem mul_neg_equiv_neg_mul {s t : seq} : smul s (sneg t) ≡ sneg (smul s t) :=
begin
rewrite [↑equiv, ↑smul],
intros,
rewrite [↑sneg, *sub_neg_eq_add, -neg_mul_eq_mul_neg, add.comm, *sneg_def,
*neg_bound2_eq_bound2, add.right_inv, abs_zero],
apply add_invs_nonneg
end
theorem equiv_of_diff_equiv_zero {s t : seq} (Hs : regular s) (Ht : regular t)
(H : sadd s (sneg t) ≡ zero) : s ≡ t :=
begin
have hsimp : ∀ a b c d e : ℚ, a + b + c + (d + e) = b + d + a + e + c, from
λ a b c d e, calc
a + b + c + (d + e) = a + b + (d + e) + c : add.right_comm
... = a + (b + d) + e + c : by rewrite[-*add.assoc]
... = b + d + a + e + c : add.comm,
apply eq_of_bdd Hs Ht,
intros,
note He := bdd_of_eq H,
existsi 2 * (2 * (2 * j)),
intros n Hn,
rewrite (rewrite_helper5 _ _ (s (2 * n)) (t (2 * n))),
apply rat.le_trans,
apply abs_add_three,
apply rat.le_trans,
apply add_le_add_three,
apply Hs,
rewrite [↑sadd at He, ↑sneg at He, ↑zero at He],
let He' := λ a b c, eq.subst !sub_zero (He a b c),
apply (He' _ _ Hn),
apply Ht,
rewrite [hsimp, pnat.add_halves, -(pnat.add_halves j), -(pnat.add_halves (2 * j)), -*rat.add_assoc],
apply add_le_add_right,
apply add_le_add_three,
repeat (apply rat.le_trans; apply inv_ge_of_le Hn; apply inv_two_mul_le_inv)
end
theorem s_sub_cancel (s : seq) : sadd s (sneg s) ≡ zero :=
begin
rewrite [↑equiv, ↑sadd, ↑sneg, ↑zero],
intros,
rewrite [sub_zero, add.right_inv, abs_zero],
apply add_invs_nonneg
end
theorem diff_equiv_zero_of_equiv {s t : seq} (Hs : regular s) (Ht : regular t) (H : s ≡ t) :
sadd s (sneg t) ≡ zero :=
begin
apply equiv.trans,
rotate 4,
apply s_sub_cancel t,
rotate 2,
apply zero_is_reg,
apply add_well_defined,
repeat (assumption | apply reg_neg_reg),
apply equiv.refl,
repeat (assumption | apply reg_add_reg | apply reg_neg_reg)
end
private theorem mul_well_defined_half1 {s t u : seq} (Hs : regular s) (Ht : regular t)
(Hu : regular u) (Etu : t ≡ u) : smul s t ≡ smul s u :=
begin
apply equiv_of_diff_equiv_zero,
rotate 2,
apply equiv.trans,
rotate 3,
apply equiv.symm,
apply add_well_defined,
rotate 4,
apply equiv.refl,
apply mul_neg_equiv_neg_mul,
apply equiv.trans,
rotate 3,
apply equiv.symm,
apply s_distrib,
rotate 3,
apply mul_zero_equiv_zero,
rotate 2,
apply diff_equiv_zero_of_equiv,
repeat (assumption | apply reg_mul_reg | apply reg_neg_reg | apply reg_add_reg |
apply zero_is_reg)
end
private theorem mul_well_defined_half2 {s t u : seq} (Hs : regular s) (Ht : regular t)
(Hu : regular u) (Est : s ≡ t) : smul s u ≡ smul t u :=
begin
apply equiv.trans,
rotate 3,
apply s_mul_comm,
apply equiv.trans,
rotate 3,
apply mul_well_defined_half1,
rotate 2,
apply Ht,
rotate 1,
apply s_mul_comm,
repeat (assumption | apply reg_mul_reg)
end
theorem mul_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Hv : regular v) (Esu : s ≡ u) (Etv : t ≡ v) : smul s t ≡ smul u v :=
begin
apply equiv.trans,
exact reg_mul_reg Hs Ht,
exact reg_mul_reg Hs Hv,
exact reg_mul_reg Hu Hv,
apply mul_well_defined_half1,
repeat assumption,
apply mul_well_defined_half2,
repeat assumption
end
theorem neg_well_defined {s t : seq} (Est : s ≡ t) : sneg s ≡ sneg t :=
begin
rewrite [↑sneg, ↑equiv at *],
intros,
rewrite [-abs_neg, neg_sub, sub_neg_eq_add, add.comm],
apply Est
end
theorem one_is_reg : regular one :=
begin
rewrite [↑regular, ↑one],
intros,
rewrite [sub_self, abs_zero],
apply add_invs_nonneg
end
theorem s_one_mul {s : seq} (H : regular s) : smul one s ≡ s :=
begin
intros,
rewrite [↑smul, ↑one, rat.one_mul],
apply rat.le_trans,
apply H,
apply add_le_add_right,
apply pnat.inv_mul_le_inv
end
theorem s_mul_one {s : seq} (H : regular s) : smul s one ≡ s :=
begin
apply equiv.trans,
apply reg_mul_reg H one_is_reg,
rotate 2,
apply s_mul_comm,
apply s_one_mul H,
apply reg_mul_reg one_is_reg H,
apply H
end
theorem zero_nequiv_one : ¬ zero ≡ one :=
begin
intro Hz,
rewrite [↑equiv at Hz, ↑zero at Hz, ↑one at Hz],
note H := Hz (2 * 2),
rewrite [zero_sub at H, abs_neg at H, pnat.add_halves at H],
have H' : pone⁻¹ ≤ 2⁻¹, from calc
pone⁻¹ = 1 : by rewrite -pone_inv
... = abs 1 : abs_of_pos zero_lt_one
... ≤ 2⁻¹ : H,
let H'' := ge_of_inv_le H',
apply absurd (one_lt_two) (pnat.not_lt_of_ge H'')
end
---------------------------------------------
-- constant sequences
definition const (a : ℚ) : seq := λ n, a
theorem const_reg (a : ℚ) : regular (const a) :=
begin
intros,
rewrite [↑const, sub_self, abs_zero],
apply add_invs_nonneg
end
theorem add_consts (a b : ℚ) : sadd (const a) (const b) ≡ const (a + b) :=
by apply equiv.refl
theorem mul_consts (a b : ℚ) : smul (const a) (const b) ≡ const (a * b) :=
by apply equiv.refl
theorem neg_const (a : ℚ) : sneg (const a) ≡ const (-a) :=
by apply equiv.refl
section
open rat
lemma eq_of_const_equiv {a b : ℚ} (H : const a ≡ const b) : a = b :=
have H₁ : ∀ n : ℕ+, abs (a - b) ≤ n⁻¹ + n⁻¹, from H,
eq_of_forall_abs_sub_le
(take ε,
suppose ε > 0,
have ε / 2 > 0, begin exact div_pos_of_pos_of_pos this two_pos end,
obtain n (Hn : n⁻¹ ≤ ε / 2), from pnat_bound this,
show abs (a - b) ≤ ε, from calc
abs (a - b) ≤ n⁻¹ + n⁻¹ : H₁ n
... ≤ ε / 2 + ε / 2 : add_le_add Hn Hn
... = ε : add_halves)
end
---------------------------------------------
-- create the type of regular sequences and lift theorems
record reg_seq : Type :=
(sq : seq) (is_reg : regular sq)
definition requiv (s t : reg_seq) := (reg_seq.sq s) ≡ (reg_seq.sq t)
definition requiv.refl (s : reg_seq) : requiv s s := equiv.refl (reg_seq.sq s)
definition requiv.symm (s t : reg_seq) (H : requiv s t) : requiv t s :=
equiv.symm (reg_seq.sq s) (reg_seq.sq t) H
definition requiv.trans (s t u : reg_seq) (H : requiv s t) (H2 : requiv t u) : requiv s u :=
equiv.trans _ _ _ (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) H H2
definition radd (s t : reg_seq) : reg_seq :=
reg_seq.mk (sadd (reg_seq.sq s) (reg_seq.sq t))
(reg_add_reg (reg_seq.is_reg s) (reg_seq.is_reg t))
infix + := radd
definition rmul (s t : reg_seq) : reg_seq :=
reg_seq.mk (smul (reg_seq.sq s) (reg_seq.sq t))
(reg_mul_reg (reg_seq.is_reg s) (reg_seq.is_reg t))
infix * := rmul
definition rneg (s : reg_seq) : reg_seq :=
reg_seq.mk (sneg (reg_seq.sq s)) (reg_neg_reg (reg_seq.is_reg s))
prefix - := rneg
definition radd_well_defined {s t u v : reg_seq} (H : requiv s u) (H2 : requiv t v) :
requiv (s + t) (u + v) :=
add_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) H H2
definition rmul_well_defined {s t u v : reg_seq} (H : requiv s u) (H2 : requiv t v) :
requiv (s * t) (u * v) :=
mul_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) H H2
definition rneg_well_defined {s t : reg_seq} (H : requiv s t) : requiv (-s) (-t) :=
neg_well_defined H
theorem requiv_is_equiv : equivalence requiv :=
mk_equivalence requiv requiv.refl requiv.symm requiv.trans
definition reg_seq.to_setoid [instance] : setoid reg_seq :=
⦃setoid, r := requiv, iseqv := requiv_is_equiv⦄
definition r_zero : reg_seq :=
reg_seq.mk (zero) (zero_is_reg)
definition r_one : reg_seq :=
reg_seq.mk (one) (one_is_reg)
theorem r_add_comm (s t : reg_seq) : requiv (s + t) (t + s) :=
s_add_comm (reg_seq.sq s) (reg_seq.sq t)
theorem r_add_assoc (s t u : reg_seq) : requiv (s + t + u) (s + (t + u)) :=
s_add_assoc (reg_seq.sq s) (reg_seq.sq t) (reg_seq.sq u) (reg_seq.is_reg s) (reg_seq.is_reg u)
theorem r_zero_add (s : reg_seq) : requiv (r_zero + s) s :=
s_zero_add (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_add_zero (s : reg_seq) : requiv (s + r_zero) s :=
s_add_zero (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_neg_cancel (s : reg_seq) : requiv (-s + s) r_zero :=
s_neg_cancel (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_mul_comm (s t : reg_seq) : requiv (s * t) (t * s) :=
s_mul_comm (reg_seq.sq s) (reg_seq.sq t)
theorem r_mul_assoc (s t u : reg_seq) : requiv (s * t * u) (s * (t * u)) :=
s_mul_assoc (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u)
theorem r_mul_one (s : reg_seq) : requiv (s * r_one) s :=
s_mul_one (reg_seq.is_reg s)
theorem r_one_mul (s : reg_seq) : requiv (r_one * s) s :=
s_one_mul (reg_seq.is_reg s)
theorem r_distrib (s t u : reg_seq) : requiv (s * (t + u)) (s * t + s * u) :=
s_distrib (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u)
theorem r_zero_nequiv_one : ¬ requiv r_zero r_one :=
zero_nequiv_one
definition r_const (a : ℚ) : reg_seq := reg_seq.mk (const a) (const_reg a)
theorem r_add_consts (a b : ℚ) : requiv (r_const a + r_const b) (r_const (a + b)) := add_consts a b
theorem r_mul_consts (a b : ℚ) : requiv (r_const a * r_const b) (r_const (a * b)) := mul_consts a b
theorem r_neg_const (a : ℚ) : requiv (-r_const a) (r_const (-a)) := neg_const a
end rat_seq
----------------------------------------------
-- take quotients to get ℝ and show it's a comm ring
open rat_seq
definition real := quot reg_seq.to_setoid
namespace real
notation `ℝ` := real
protected definition prio := num.pred rat.prio
protected definition add (x y : ℝ) : ℝ :=
(quot.lift_on₂ x y (λ a b, quot.mk (a + b))
(take a b c d : reg_seq, take Hab : requiv a c, take Hcd : requiv b d,
quot.sound (radd_well_defined Hab Hcd)))
--infix [priority real.prio] + := add
protected definition mul (x y : ℝ) : ℝ :=
(quot.lift_on₂ x y (λ a b, quot.mk (a * b))
(take a b c d : reg_seq, take Hab : requiv a c, take Hcd : requiv b d,
quot.sound (rmul_well_defined Hab Hcd)))
--infix [priority real.prio] * := mul
protected definition neg (x : ℝ) : ℝ :=
(quot.lift_on x (λ a, quot.mk (-a)) (take a b : reg_seq, take Hab : requiv a b,
quot.sound (rneg_well_defined Hab)))
--prefix [priority real.prio] `-` := neg
definition real_has_add [reducible] [instance] [priority real.prio] : has_add real :=
has_add.mk real.add
definition real_has_mul [reducible] [instance] [priority real.prio] : has_mul real :=
has_mul.mk real.mul
definition real_has_neg [reducible] [instance] [priority real.prio] : has_neg real :=
has_neg.mk real.neg
protected definition sub [reducible] (a b : ℝ) : real := a + (-b)
definition real_has_sub [reducible] [instance] [priority real.prio] : has_sub real :=
has_sub.mk real.sub
open rat -- no coercions before
definition of_rat [coercion] (a : ℚ) : ℝ := quot.mk (r_const a)
definition of_int [coercion] (i : ℤ) : ℝ := int.to.real i
definition of_nat [coercion] (n : ℕ) : ℝ := nat.to.real n
definition of_num [coercion] [reducible] (n : num) : ℝ := of_rat (rat.of_num n)
definition real_has_zero [reducible] [instance] [priority real.prio] : has_zero real :=
has_zero.mk (of_rat 0)
definition real_has_one [reducible] [instance] [priority real.prio] : has_one real :=
has_one.mk (of_rat 1)
theorem real_zero_eq_rat_zero : (0:real) = of_rat (0:rat) :=
rfl
theorem real_one_eq_rat_one : (1:real) = of_rat (1:rat) :=
rfl
protected theorem add_comm (x y : ℝ) : x + y = y + x :=
quot.induction_on₂ x y (λ s t, quot.sound (r_add_comm s t))
protected theorem add_assoc (x y z : ℝ) : x + y + z = x + (y + z) :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_add_assoc s t u))
protected theorem zero_add (x : ℝ) : 0 + x = x :=
quot.induction_on x (λ s, quot.sound (r_zero_add s))
protected theorem add_zero (x : ℝ) : x + 0 = x :=
quot.induction_on x (λ s, quot.sound (r_add_zero s))
protected theorem neg_cancel (x : ℝ) : -x + x = 0 :=
quot.induction_on x (λ s, quot.sound (r_neg_cancel s))
protected theorem mul_assoc (x y z : ℝ) : x * y * z = x * (y * z) :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_mul_assoc s t u))
protected theorem mul_comm (x y : ℝ) : x * y = y * x :=
quot.induction_on₂ x y (λ s t, quot.sound (r_mul_comm s t))
protected theorem one_mul (x : ℝ) : 1 * x = x :=
quot.induction_on x (λ s, quot.sound (r_one_mul s))
protected theorem mul_one (x : ℝ) : x * 1 = x :=
quot.induction_on x (λ s, quot.sound (r_mul_one s))
protected theorem left_distrib (x y z : ℝ) : x * (y + z) = x * y + x * z :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_distrib s t u))
protected theorem right_distrib (x y z : ℝ) : (x + y) * z = x * z + y * z :=
by rewrite [real.mul_comm, real.left_distrib, {x * _}real.mul_comm, {y * _}real.mul_comm]
protected theorem zero_ne_one : ¬ (0 : ℝ) = 1 :=
take H : 0 = 1,
absurd (quot.exact H) (r_zero_nequiv_one)
protected definition comm_ring [reducible] : comm_ring ℝ :=
begin
fapply comm_ring.mk,
exact real.add,
exact real.add_assoc,
exact of_num 0,
exact real.zero_add,
exact real.add_zero,
exact real.neg,
exact real.neg_cancel,
exact real.add_comm,
exact real.mul,
exact real.mul_assoc,
apply of_num 1,
apply real.one_mul,
apply real.mul_one,
apply real.left_distrib,
apply real.right_distrib,
apply real.mul_comm
end
theorem of_int_eq (a : ℤ) : of_int a = of_rat (rat.of_int a) := rfl
theorem of_nat_eq (a : ℕ) : of_nat a = of_rat (rat.of_nat a) := rfl
theorem of_rat.inj {x y : ℚ} (H : of_rat x = of_rat y) : x = y :=
eq_of_const_equiv (quot.exact H)
theorem eq_of_of_rat_eq_of_rat {x y : ℚ} (H : of_rat x = of_rat y) : x = y :=
of_rat.inj H
theorem of_rat_eq_of_rat_iff (x y : ℚ) : of_rat x = of_rat y ↔ x = y :=
iff.intro eq_of_of_rat_eq_of_rat !congr_arg
theorem of_int.inj {a b : ℤ} (H : of_int a = of_int b) : a = b :=
rat.of_int.inj (of_rat.inj H)
theorem eq_of_of_int_eq_of_int {a b : ℤ} (H : of_int a = of_int b) : a = b :=
of_int.inj H
theorem of_int_eq_of_int_iff (a b : ℤ) : of_int a = of_int b ↔ a = b :=
iff.intro of_int.inj !congr_arg
theorem of_nat.inj {a b : ℕ} (H : of_nat a = of_nat b) : a = b :=
int.of_nat.inj (of_int.inj H)
theorem eq_of_of_nat_eq_of_nat {a b : ℕ} (H : of_nat a = of_nat b) : a = b :=
of_nat.inj H
theorem of_nat_eq_of_nat_iff (a b : ℕ) : of_nat a = of_nat b ↔ a = b :=
iff.intro of_nat.inj !congr_arg
theorem of_rat_add (a b : ℚ) : of_rat (a + b) = of_rat a + of_rat b :=
quot.sound (r_add_consts a b)
theorem of_rat_neg (a : ℚ) : of_rat (-a) = -of_rat a :=
eq.symm (quot.sound (r_neg_const a))
theorem of_rat_mul (a b : ℚ) : of_rat (a * b) = of_rat a * of_rat b :=
quot.sound (r_mul_consts a b)
theorem of_rat_zero : of_rat 0 = 0 := rfl
theorem of_rat_one : of_rat 1 = 1 := rfl
open int
theorem of_int_add (a b : ℤ) : of_int (a + b) = of_int a + of_int b :=
by rewrite [of_int_eq, rat.of_int_add, of_rat_add]
theorem of_int_neg (a : ℤ) : of_int (-a) = -of_int a :=
by rewrite [of_int_eq, rat.of_int_neg, of_rat_neg]
theorem of_int_mul (a b : ℤ) : of_int (a * b) = of_int a * of_int b :=
by rewrite [of_int_eq, rat.of_int_mul, of_rat_mul]
theorem of_nat_add (a b : ℕ) : of_nat (a + b) = of_nat a + of_nat b :=
by rewrite [of_nat_eq, rat.of_nat_add, of_rat_add]
theorem of_nat_mul (a b : ℕ) : of_nat (a * b) = of_nat a * of_nat b :=
by rewrite [of_nat_eq, rat.of_nat_mul, of_rat_mul]
theorem add_half_of_rat (n : ℕ+) : of_rat (2 * n)⁻¹ + of_rat (2 * n)⁻¹ = of_rat (n⁻¹) :=
by rewrite [-of_rat_add, pnat.add_halves]
theorem one_add_one : 1 + 1 = (2 : ℝ) := rfl
end real
|
301a5f6054a4772724f12b05e2bd2786b430c1fc | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/continuous_function/compact.lean | 30844763f0d4dd061c1b97a3e505add21a3eb084 | [
"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 | 15,975 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import topology.continuous_function.bounded
import topology.uniform_space.compact_separated
import topology.compact_open
import topology.sets.compacts
/-!
# Continuous functions on a compact space
Continuous functions `C(α, β)` from a compact space `α` to a metric space `β`
are automatically bounded, and so acquire various structures inherited from `α →ᵇ β`.
This file transfers these structures, and restates some lemmas
characterising these structures.
If you need a lemma which is proved about `α →ᵇ β` but not for `C(α, β)` when `α` is compact,
you should restate it here. You can also use
`bounded_continuous_function.equiv_continuous_map_of_compact` to move functions back and forth.
-/
noncomputable theory
open_locale topological_space classical nnreal bounded_continuous_function big_operators
open set filter metric
open bounded_continuous_function
namespace continuous_map
variables {α β E : Type*} [topological_space α] [compact_space α] [metric_space β] [normed_group E]
section
variables (α β)
/--
When `α` is compact, the bounded continuous maps `α →ᵇ β` are
equivalent to `C(α, β)`.
-/
@[simps { fully_applied := ff }]
def equiv_bounded_of_compact : C(α, β) ≃ (α →ᵇ β) :=
⟨mk_of_compact, to_continuous_map, λ f, by { ext, refl, }, λ f, by { ext, refl, }⟩
lemma uniform_inducing_equiv_bounded_of_compact :
uniform_inducing (equiv_bounded_of_compact α β) :=
uniform_inducing.mk'
begin
simp only [has_basis_compact_convergence_uniformity.mem_iff, uniformity_basis_dist_le.mem_iff],
exact λ s, ⟨λ ⟨⟨a, b⟩, ⟨ha, ⟨ε, hε, hb⟩⟩, hs⟩, ⟨{p | ∀ x, (p.1 x, p.2 x) ∈ b},
⟨ε, hε, λ _ h x, hb (by exact (dist_le hε.le).mp h x)⟩, λ f g h, hs (by exact λ x hx, h x)⟩,
λ ⟨t, ⟨ε, hε, ht⟩, hs⟩, ⟨⟨set.univ, {p | dist p.1 p.2 ≤ ε}⟩, ⟨compact_univ, ⟨ε, hε, λ _ h, h⟩⟩,
λ ⟨f, g⟩ h, hs _ _ (ht (by exact (dist_le hε.le).mpr (λ x, h x (mem_univ x))))⟩⟩,
end
lemma uniform_embedding_equiv_bounded_of_compact :
uniform_embedding (equiv_bounded_of_compact α β) :=
{ inj := (equiv_bounded_of_compact α β).injective,
.. uniform_inducing_equiv_bounded_of_compact α β }
/--
When `α` is compact, the bounded continuous maps `α →ᵇ 𝕜` are
additively equivalent to `C(α, 𝕜)`.
-/
@[simps apply symm_apply { fully_applied := ff }]
def add_equiv_bounded_of_compact [add_monoid β] [has_lipschitz_add β] :
C(α, β) ≃+ (α →ᵇ β) :=
({ .. to_continuous_map_add_hom α β,
.. (equiv_bounded_of_compact α β).symm, } : (α →ᵇ β) ≃+ C(α, β)).symm
instance : metric_space C(α, β) :=
(uniform_embedding_equiv_bounded_of_compact α β).comap_metric_space _
/--
When `α` is compact, and `β` is a metric space, the bounded continuous maps `α →ᵇ β` are
isometric to `C(α, β)`.
-/
@[simps to_equiv apply symm_apply { fully_applied := ff }]
def isometric_bounded_of_compact :
C(α, β) ≃ᵢ (α →ᵇ β) :=
{ isometry_to_fun := λ x y, rfl,
to_equiv := equiv_bounded_of_compact α β }
end
@[simp] lemma _root_.bounded_continuous_function.dist_mk_of_compact (f g : C(α, β)) :
dist (mk_of_compact f) (mk_of_compact g) = dist f g := rfl
@[simp] lemma _root_.bounded_continuous_function.dist_to_continuous_map (f g : α →ᵇ β) :
dist (f.to_continuous_map) (g.to_continuous_map) = dist f g := rfl
open bounded_continuous_function
section
variables {α β} {f g : C(α, β)} {C : ℝ}
/-- The pointwise distance is controlled by the distance between functions, by definition. -/
lemma dist_apply_le_dist (x : α) : dist (f x) (g x) ≤ dist f g :=
by simp only [← dist_mk_of_compact, dist_coe_le_dist, ← mk_of_compact_apply]
/-- The distance between two functions is controlled by the supremum of the pointwise distances -/
lemma dist_le (C0 : (0 : ℝ) ≤ C) : dist f g ≤ C ↔ ∀x:α, dist (f x) (g x) ≤ C :=
by simp only [← dist_mk_of_compact, dist_le C0, mk_of_compact_apply]
lemma dist_le_iff_of_nonempty [nonempty α] :
dist f g ≤ C ↔ ∀ x, dist (f x) (g x) ≤ C :=
by simp only [← dist_mk_of_compact, dist_le_iff_of_nonempty, mk_of_compact_apply]
lemma dist_lt_iff_of_nonempty [nonempty α] :
dist f g < C ↔ ∀x:α, dist (f x) (g x) < C :=
by simp only [← dist_mk_of_compact, dist_lt_iff_of_nonempty_compact, mk_of_compact_apply]
lemma dist_lt_of_nonempty [nonempty α] (w : ∀x:α, dist (f x) (g x) < C) : dist f g < C :=
(dist_lt_iff_of_nonempty).2 w
lemma dist_lt_iff (C0 : (0 : ℝ) < C) :
dist f g < C ↔ ∀x:α, dist (f x) (g x) < C :=
by simp only [← dist_mk_of_compact, dist_lt_iff_of_compact C0, mk_of_compact_apply]
end
instance [complete_space β] : complete_space (C(α, β)) :=
(isometric_bounded_of_compact α β).complete_space
/-- See also `continuous_map.continuous_eval'` -/
@[continuity] lemma continuous_eval : continuous (λ p : C(α, β) × α, p.1 p.2) :=
continuous_eval.comp ((isometric_bounded_of_compact α β).continuous.prod_map continuous_id)
/-- See also `continuous_map.continuous_eval_const` -/
@[continuity] lemma continuous_eval_const (x : α) : continuous (λ f : C(α, β), f x) :=
continuous_eval.comp (continuous_id.prod_mk continuous_const)
/-- See also `continuous_map.continuous_coe'` -/
lemma continuous_coe : @continuous (C(α, β)) (α → β) _ _ coe_fn :=
continuous_pi continuous_eval_const
-- TODO at some point we will need lemmas characterising this norm!
-- At the moment the only way to reason about it is to transfer `f : C(α,E)` back to `α →ᵇ E`.
instance : has_norm C(α, E) :=
{ norm := λ x, dist x 0 }
@[simp] lemma _root_.bounded_continuous_function.norm_mk_of_compact (f : C(α, E)) :
∥mk_of_compact f∥ = ∥f∥ := rfl
@[simp] lemma _root_.bounded_continuous_function.norm_to_continuous_map_eq (f : α →ᵇ E) :
∥f.to_continuous_map∥ = ∥f∥ :=
rfl
open bounded_continuous_function
instance : normed_group C(α, E) :=
{ dist_eq := λ x y, by
rw [← norm_mk_of_compact, ← dist_mk_of_compact, dist_eq_norm, mk_of_compact_sub] }
section
variables (f : C(α, E))
-- The corresponding lemmas for `bounded_continuous_function` are stated with `{f}`,
-- and so can not be used in dot notation.
lemma norm_coe_le_norm (x : α) : ∥f x∥ ≤ ∥f∥ :=
(mk_of_compact f).norm_coe_le_norm x
/-- Distance between the images of any two points is at most twice the norm of the function. -/
lemma dist_le_two_norm (x y : α) : dist (f x) (f y) ≤ 2 * ∥f∥ :=
(mk_of_compact f).dist_le_two_norm x y
/-- The norm of a function is controlled by the supremum of the pointwise norms -/
lemma norm_le {C : ℝ} (C0 : (0 : ℝ) ≤ C) : ∥f∥ ≤ C ↔ ∀x:α, ∥f x∥ ≤ C :=
@bounded_continuous_function.norm_le _ _ _ _
(mk_of_compact f) _ C0
lemma norm_le_of_nonempty [nonempty α] {M : ℝ} : ∥f∥ ≤ M ↔ ∀ x, ∥f x∥ ≤ M :=
@bounded_continuous_function.norm_le_of_nonempty _ _ _ _ _ (mk_of_compact f) _
lemma norm_lt_iff {M : ℝ} (M0 : 0 < M) : ∥f∥ < M ↔ ∀ x, ∥f x∥ < M :=
@bounded_continuous_function.norm_lt_iff_of_compact _ _ _ _ _ (mk_of_compact f) _ M0
lemma norm_lt_iff_of_nonempty [nonempty α] {M : ℝ} :
∥f∥ < M ↔ ∀ x, ∥f x∥ < M :=
@bounded_continuous_function.norm_lt_iff_of_nonempty_compact _ _ _ _ _ _ (mk_of_compact f) _
lemma apply_le_norm (f : C(α, ℝ)) (x : α) : f x ≤ ∥f∥ :=
le_trans (le_abs.mpr (or.inl (le_refl (f x)))) (f.norm_coe_le_norm x)
lemma neg_norm_le_apply (f : C(α, ℝ)) (x : α) : -∥f∥ ≤ f x :=
le_trans (neg_le_neg (f.norm_coe_le_norm x)) (neg_le.mp (neg_le_abs_self (f x)))
lemma norm_eq_supr_norm : ∥f∥ = ⨆ x : α, ∥f x∥ :=
(mk_of_compact f).norm_eq_supr_norm
end
section
variables {R : Type*} [normed_ring R]
instance : normed_ring C(α,R) :=
{ norm_mul := λ f g, norm_mul_le (mk_of_compact f) (mk_of_compact g),
..(infer_instance : normed_group C(α,R)) }
end
section
variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E]
instance : normed_space 𝕜 C(α,E) :=
{ norm_smul_le := λ c f, le_of_eq (norm_smul c (mk_of_compact f)) }
section
variables (α 𝕜 E)
/--
When `α` is compact and `𝕜` is a normed field,
the `𝕜`-algebra of bounded continuous maps `α →ᵇ β` is
`𝕜`-linearly isometric to `C(α, β)`.
-/
def linear_isometry_bounded_of_compact :
C(α, E) ≃ₗᵢ[𝕜] (α →ᵇ E) :=
{ map_smul' := λ c f, by { ext, simp, },
norm_map' := λ f, rfl,
.. add_equiv_bounded_of_compact α E }
end
-- this lemma and the next are the analogues of those autogenerated by `@[simps]` for
-- `equiv_bounded_of_compact`, `add_equiv_bounded_of_compact`
@[simp] lemma linear_isometry_bounded_of_compact_symm_apply (f : α →ᵇ E) :
(linear_isometry_bounded_of_compact α E 𝕜).symm f = f.to_continuous_map :=
rfl
@[simp] lemma linear_isometry_bounded_of_compact_apply_apply (f : C(α, E)) (a : α) :
(linear_isometry_bounded_of_compact α E 𝕜 f) a = f a :=
rfl
@[simp]
lemma linear_isometry_bounded_of_compact_to_isometric :
(linear_isometry_bounded_of_compact α E 𝕜).to_isometric = (isometric_bounded_of_compact α E) :=
rfl
@[simp]
lemma linear_isometry_bounded_of_compact_to_add_equiv :
(linear_isometry_bounded_of_compact α E 𝕜).to_linear_equiv.to_add_equiv =
(add_equiv_bounded_of_compact α E) :=
rfl
@[simp]
lemma linear_isometry_bounded_of_compact_of_compact_to_equiv :
(linear_isometry_bounded_of_compact α E 𝕜).to_linear_equiv.to_equiv =
(equiv_bounded_of_compact α E) :=
rfl
end
section
variables {𝕜 : Type*} {γ : Type*} [normed_field 𝕜] [normed_ring γ] [normed_algebra 𝕜 γ]
instance : normed_algebra 𝕜 C(α, γ) :=
{ ..continuous_map.normed_space }
end
end continuous_map
namespace continuous_map
section uniform_continuity
variables {α β : Type*}
variables [metric_space α] [compact_space α] [metric_space β]
/-!
We now set up some declarations making it convenient to use uniform continuity.
-/
lemma uniform_continuity
(f : C(α, β)) (ε : ℝ) (h : 0 < ε) :
∃ δ > 0, ∀ {x y}, dist x y < δ → dist (f x) (f y) < ε :=
metric.uniform_continuous_iff.mp
(compact_space.uniform_continuous_of_continuous f.continuous) ε h
/--
An arbitrarily chosen modulus of uniform continuity for a given function `f` and `ε > 0`.
-/
-- This definition allows us to separate the choice of some `δ`,
-- and the corresponding use of `dist a b < δ → dist (f a) (f b) < ε`,
-- even across different declarations.
def modulus (f : C(α, β)) (ε : ℝ) (h : 0 < ε) : ℝ :=
classical.some (uniform_continuity f ε h)
lemma modulus_pos (f : C(α, β)) {ε : ℝ} {h : 0 < ε} : 0 < f.modulus ε h :=
(classical.some_spec (uniform_continuity f ε h)).fst
lemma dist_lt_of_dist_lt_modulus
(f : C(α, β)) (ε : ℝ) (h : 0 < ε) {a b : α} (w : dist a b < f.modulus ε h) :
dist (f a) (f b) < ε :=
(classical.some_spec (uniform_continuity f ε h)).snd w
end uniform_continuity
end continuous_map
section comp_left
variables (X : Type*) {𝕜 β γ : Type*} [topological_space X] [compact_space X]
[nondiscrete_normed_field 𝕜]
variables [normed_group β] [normed_space 𝕜 β] [normed_group γ] [normed_space 𝕜 γ]
open continuous_map
/--
Postcomposition of continuous functions into a normed module by a continuous linear map is a
continuous linear map.
Transferred version of `continuous_linear_map.comp_left_continuous_bounded`,
upgraded version of `continuous_linear_map.comp_left_continuous`,
similar to `linear_map.comp_left`. -/
protected def continuous_linear_map.comp_left_continuous_compact (g : β →L[𝕜] γ) :
C(X, β) →L[𝕜] C(X, γ) :=
(linear_isometry_bounded_of_compact X γ 𝕜).symm.to_linear_isometry.to_continuous_linear_map.comp $
(g.comp_left_continuous_bounded X).comp $
(linear_isometry_bounded_of_compact X β 𝕜).to_linear_isometry.to_continuous_linear_map
@[simp] lemma continuous_linear_map.to_linear_comp_left_continuous_compact (g : β →L[𝕜] γ) :
(g.comp_left_continuous_compact X : C(X, β) →ₗ[𝕜] C(X, γ)) = g.comp_left_continuous 𝕜 X :=
by { ext f, refl }
@[simp] lemma continuous_linear_map.comp_left_continuous_compact_apply (g : β →L[𝕜] γ)
(f : C(X, β)) (x : X) :
g.comp_left_continuous_compact X f x = g (f x) :=
rfl
end comp_left
namespace continuous_map
/-!
We now setup variations on `comp_right_* f`, where `f : C(X, Y)`
(that is, precomposition by a continuous map),
as a morphism `C(Y, T) → C(X, T)`, respecting various types of structure.
In particular:
* `comp_right_continuous_map`, the bundled continuous map (for this we need `X Y` compact).
* `comp_right_homeomorph`, when we precompose by a homeomorphism.
* `comp_right_alg_hom`, when `T = R` is a topological ring.
-/
section comp_right
/--
Precomposition by a continuous map is itself a continuous map between spaces of continuous maps.
-/
def comp_right_continuous_map {X Y : Type*} (T : Type*)
[topological_space X] [compact_space X] [topological_space Y] [compact_space Y] [normed_group T]
(f : C(X, Y)) : C(C(Y, T), C(X, T)) :=
{ to_fun := λ g, g.comp f,
continuous_to_fun :=
begin
refine metric.continuous_iff.mpr _,
intros g ε ε_pos,
refine ⟨ε, ε_pos, λ g' h, _⟩,
rw continuous_map.dist_lt_iff ε_pos at h ⊢,
{ exact λ x, h (f x), },
end }
@[simp] lemma comp_right_continuous_map_apply {X Y : Type*} (T : Type*)
[topological_space X] [compact_space X] [topological_space Y] [compact_space Y] [normed_group T]
(f : C(X, Y)) (g : C(Y, T)) :
(comp_right_continuous_map T f) g = g.comp f :=
rfl
/--
Precomposition by a homeomorphism is itself a homeomorphism between spaces of continuous maps.
-/
def comp_right_homeomorph {X Y : Type*} (T : Type*)
[topological_space X] [compact_space X] [topological_space Y] [compact_space Y] [normed_group T]
(f : X ≃ₜ Y) : C(Y, T) ≃ₜ C(X, T) :=
{ to_fun := comp_right_continuous_map T f.to_continuous_map,
inv_fun := comp_right_continuous_map T f.symm.to_continuous_map,
left_inv := by tidy,
right_inv := by tidy, }
/--
Precomposition of functions into a normed ring by continuous map is an algebra homomorphism.
-/
def comp_right_alg_hom {X Y : Type*} (R : Type*)
[topological_space X] [topological_space Y] [normed_comm_ring R] (f : C(X, Y)) :
C(Y, R) →ₐ[R] C(X, R) :=
{ to_fun := λ g, g.comp f,
map_zero' := by { ext, simp, },
map_add' := λ g₁ g₂, by { ext, simp, },
map_one' := by { ext, simp, },
map_mul' := λ g₁ g₂, by { ext, simp, },
commutes' := λ r, by { ext, simp, }, }
@[simp] lemma comp_right_alg_hom_apply {X Y : Type*} (R : Type*)
[topological_space X] [topological_space Y] [normed_comm_ring R] (f : C(X, Y)) (g : C(Y, R)) :
(comp_right_alg_hom R f) g = g.comp f :=
rfl
lemma comp_right_alg_hom_continuous {X Y : Type*} (R : Type*)
[topological_space X] [compact_space X] [topological_space Y] [compact_space Y]
[normed_comm_ring R] (f : C(X, Y)) :
continuous (comp_right_alg_hom R f) :=
begin
change continuous (comp_right_continuous_map R f),
continuity,
end
end comp_right
section weierstrass
open topological_space
variables {X : Type*} [topological_space X] [t2_space X] [locally_compact_space X]
variables {E : Type*} [normed_group E] [complete_space E]
lemma summable_of_locally_summable_norm {ι : Type*} {F : ι → C(X, E)}
(hF : ∀ K : compacts X, summable (λ i, ∥(F i).restrict K∥)) :
summable F :=
begin
refine (continuous_map.exists_tendsto_compact_open_iff_forall _).2 (λ K hK, _),
lift K to compacts X using hK,
have A : ∀ s : finset ι, restrict ↑K (∑ i in s, F i) = ∑ i in s, restrict K (F i),
{ intro s, ext1 x, simp },
simpa only [has_sum, A] using summable_of_summable_norm (hF K)
end
end weierstrass
end continuous_map
|
ca8ca250bbd6e41952a2e34c784b858ab460c51e | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/inner_product_space/positive.lean | 8b35a1d815b431a1855d26dc9ba71e780efa2e24 | [
"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 | 4,409 | lean | /-
Copyright (c) 2022 Anatole Dedecker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anatole Dedecker
-/
import analysis.inner_product_space.adjoint
/-!
# Positive operators
In this file we define positive operators in a Hilbert space. We follow Bourbaki's choice
of requiring self adjointness in the definition.
## Main definitions
* `is_positive` : a continuous linear map is positive if it is self adjoint and
`∀ x, 0 ≤ re ⟪T x, x⟫`
## Main statements
* `continuous_linear_map.is_positive.conj_adjoint` : if `T : E →L[𝕜] E` is positive,
then for any `S : E →L[𝕜] F`, `S ∘L T ∘L S†` is also positive.
* `continuous_linear_map.is_positive_iff_complex` : in a ***complex*** hilbert space,
checking that `⟪T x, x⟫` is a nonnegative real number for all `x` suffices to prove that
`T` is positive
## References
* [Bourbaki, *Topological Vector Spaces*][bourbaki1987]
## Tags
Positive operator
-/
open inner_product_space is_R_or_C continuous_linear_map
open_locale inner_product complex_conjugate
namespace continuous_linear_map
variables {𝕜 E F : Type*} [is_R_or_C 𝕜]
variables [normed_add_comm_group E] [normed_add_comm_group F]
variables [inner_product_space 𝕜 E] [inner_product_space 𝕜 F]
variables [complete_space E] [complete_space F]
local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y
/-- A continuous linear endomorphism `T` of a Hilbert space is **positive** if it is self adjoint
and `∀ x, 0 ≤ re ⟪T x, x⟫`. -/
def is_positive (T : E →L[𝕜] E) : Prop :=
is_self_adjoint T ∧ ∀ x, 0 ≤ T.re_apply_inner_self x
lemma is_positive.is_self_adjoint {T : E →L[𝕜] E} (hT : is_positive T) :
is_self_adjoint T :=
hT.1
lemma is_positive.inner_nonneg_left {T : E →L[𝕜] E} (hT : is_positive T) (x : E) :
0 ≤ re ⟪T x, x⟫ :=
hT.2 x
lemma is_positive.inner_nonneg_right {T : E →L[𝕜] E} (hT : is_positive T) (x : E) :
0 ≤ re ⟪x, T x⟫ :=
by rw inner_re_symm; exact hT.inner_nonneg_left x
lemma is_positive_zero : is_positive (0 : E →L[𝕜] E) :=
begin
refine ⟨is_self_adjoint_zero _, λ x, _⟩,
change 0 ≤ re ⟪_, _⟫,
rw [zero_apply, inner_zero_left, zero_hom_class.map_zero]
end
lemma is_positive_one : is_positive (1 : E →L[𝕜] E) :=
⟨is_self_adjoint_one _, λ x, inner_self_nonneg⟩
lemma is_positive.add {T S : E →L[𝕜] E} (hT : T.is_positive)
(hS : S.is_positive) : (T + S).is_positive :=
begin
refine ⟨hT.is_self_adjoint.add hS.is_self_adjoint, λ x, _⟩,
rw [re_apply_inner_self, add_apply, inner_add_left, map_add],
exact add_nonneg (hT.inner_nonneg_left x) (hS.inner_nonneg_left x)
end
lemma is_positive.conj_adjoint {T : E →L[𝕜] E}
(hT : T.is_positive) (S : E →L[𝕜] F) : (S ∘L T ∘L S†).is_positive :=
begin
refine ⟨hT.is_self_adjoint.conj_adjoint S, λ x, _⟩,
rw [re_apply_inner_self, comp_apply, ← adjoint_inner_right],
exact hT.inner_nonneg_left _
end
lemma is_positive.adjoint_conj {T : E →L[𝕜] E}
(hT : T.is_positive) (S : F →L[𝕜] E) : (S† ∘L T ∘L S).is_positive :=
begin
convert hT.conj_adjoint (S†),
rw adjoint_adjoint
end
lemma is_positive.conj_orthogonal_projection (U : submodule 𝕜 E) {T : E →L[𝕜] E}
(hT : T.is_positive) [complete_space U] :
(U.subtypeL ∘L orthogonal_projection U ∘L T ∘L U.subtypeL ∘L
orthogonal_projection U).is_positive :=
begin
have := hT.conj_adjoint (U.subtypeL ∘L orthogonal_projection U),
rwa (orthogonal_projection_is_self_adjoint U).adjoint_eq at this
end
lemma is_positive.orthogonal_projection_comp {T : E →L[𝕜] E}
(hT : T.is_positive) (U : submodule 𝕜 E) [complete_space U] :
(orthogonal_projection U ∘L T ∘L U.subtypeL).is_positive :=
begin
have := hT.conj_adjoint (orthogonal_projection U : E →L[𝕜] U),
rwa [U.adjoint_orthogonal_projection] at this,
end
section complex
variables {E' : Type*} [normed_add_comm_group E'] [inner_product_space ℂ E'] [complete_space E']
lemma is_positive_iff_complex (T : E' →L[ℂ] E') :
is_positive T ↔ ∀ x, (re ⟪T x, x⟫_ℂ : ℂ) = ⟪T x, x⟫_ℂ ∧ 0 ≤ re ⟪T x, x⟫_ℂ :=
begin
simp_rw [is_positive, forall_and_distrib, is_self_adjoint_iff_is_symmetric,
linear_map.is_symmetric_iff_inner_map_self_real, conj_eq_iff_re],
refl
end
end complex
end continuous_linear_map
|
006906688e3aa658ab22812ad86b6d7213140ab0 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/584c.lean | cd4abd8b67d97d21ef0b80509d5d123b86d91722 | [
"Apache-2.0"
] | permissive | soonhokong/lean-osx | 4a954262c780e404c1369d6c06516161d07fcb40 | 3670278342d2f4faa49d95b46d86642d7875b47c | refs/heads/master | 1,611,410,334,552 | 1,474,425,686,000 | 1,474,425,686,000 | 12,043,103 | 5 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 494 | lean | section
universe variables u parameter (A : Type u)
section
parameters (a b : A)
parameter (H : a = b)
definition tst₁ := a
parameter {A}
definition tst₂ := a
lemma symm₂ : b = a := eq.symm H
parameters {a b}
lemma foo (c : A) (h₂ : c = b) : c = a :=
eq.trans h₂ symm₂
end
parameter (a : A)
definition tst₃ := a
end
check @tst₁
check @tst₂ -- A is implicit
check @symm₂
check @tst₃ -- A is explicit again
check @foo
|
aedbe9d1c3b22d7f11619c3a33ab00768ea551d5 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /stage0/src/Init/Data/UInt.lean | 4ed037ca6d8696a9c02d4d83852dd8b578ca2d60 | [
"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 | 15,086 | lean | /-
Copyright (c) 2018 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.Fin.Basic
import Init.System.Platform
open Nat
def uint8Sz : Nat := 256
structure UInt8 :=
(val : Fin uint8Sz)
@[extern "lean_uint8_of_nat"]
def UInt8.ofNat (n : @& Nat) : UInt8 := ⟨Fin.ofNat n⟩
abbrev Nat.toUInt8 := UInt8.ofNat
@[extern "lean_uint8_to_nat"]
def UInt8.toNat (n : UInt8) : Nat := n.val.val
@[extern c inline "#1 + #2"]
def UInt8.add (a b : UInt8) : UInt8 := ⟨a.val + b.val⟩
@[extern c inline "#1 - #2"]
def UInt8.sub (a b : UInt8) : UInt8 := ⟨a.val - b.val⟩
@[extern c inline "#1 * #2"]
def UInt8.mul (a b : UInt8) : UInt8 := ⟨a.val * b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 / #2"]
def UInt8.div (a b : UInt8) : UInt8 := ⟨a.val / b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 % #2"]
def UInt8.mod (a b : UInt8) : UInt8 := ⟨a.val % b.val⟩
@[extern "lean_uint8_modn"]
def UInt8.modn (a : UInt8) (n : @& Nat) : UInt8 := ⟨a.val %ₙ n⟩
@[extern c inline "#1 & #2"]
def UInt8.land (a b : UInt8) : UInt8 := ⟨Fin.land a.val b.val⟩
@[extern c inline "#1 | #2"]
def UInt8.lor (a b : UInt8) : UInt8 := ⟨Fin.lor a.val b.val⟩
def UInt8.lt (a b : UInt8) : Prop := a.val < b.val
def UInt8.le (a b : UInt8) : Prop := a.val ≤ b.val
instance : HasZero UInt8 := ⟨UInt8.ofNat 0⟩
instance : HasOne UInt8 := ⟨UInt8.ofNat 1⟩
instance : HasOfNat UInt8 := ⟨UInt8.ofNat⟩
instance : HasAdd UInt8 := ⟨UInt8.add⟩
instance : HasSub UInt8 := ⟨UInt8.sub⟩
instance : HasMul UInt8 := ⟨UInt8.mul⟩
instance : HasMod UInt8 := ⟨UInt8.mod⟩
instance : HasModN UInt8 := ⟨UInt8.modn⟩
instance : HasDiv UInt8 := ⟨UInt8.div⟩
instance : HasLess UInt8 := ⟨UInt8.lt⟩
instance : HasLessEq UInt8 := ⟨UInt8.le⟩
instance : Inhabited UInt8 := ⟨0⟩
@[extern c inline "#1 == #2"]
def UInt8.decEq (a b : UInt8) : Decidable (a = b) :=
UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m =>
if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt8.noConfusion h' (fun h' => absurd h' h))
@[extern c inline "#1 < #2"]
def UInt8.decLt (a b : UInt8) : Decidable (a < b) :=
UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m =>
inferInstanceAs (Decidable (n < m))
@[extern c inline "#1 <= #2"]
def UInt8.decLe (a b : UInt8) : Decidable (a ≤ b) :=
UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m =>
inferInstanceAs (Decidable (n <= m))
instance : DecidableEq UInt8 := UInt8.decEq
instance UInt8.hasDecidableLt (a b : UInt8) : Decidable (a < b) := UInt8.decLt a b
instance UInt8.hasDecidableLe (a b : UInt8) : Decidable (a ≤ b) := UInt8.decLe a b
def uint16Sz : Nat := 65536
structure UInt16 :=
(val : Fin uint16Sz)
@[extern "lean_uint16_of_nat"]
def UInt16.ofNat (n : @& Nat) : UInt16 := ⟨Fin.ofNat n⟩
abbrev Nat.toUInt16 := UInt16.ofNat
@[extern "lean_uint16_to_nat"]
def UInt16.toNat (n : UInt16) : Nat := n.val.val
@[extern c inline "#1 + #2"]
def UInt16.add (a b : UInt16) : UInt16 := ⟨a.val + b.val⟩
@[extern c inline "#1 - #2"]
def UInt16.sub (a b : UInt16) : UInt16 := ⟨a.val - b.val⟩
@[extern c inline "#1 * #2"]
def UInt16.mul (a b : UInt16) : UInt16 := ⟨a.val * b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 / #2"]
def UInt16.div (a b : UInt16) : UInt16 := ⟨a.val / b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 % #2"]
def UInt16.mod (a b : UInt16) : UInt16 := ⟨a.val % b.val⟩
@[extern "lean_uint16_modn"]
def UInt16.modn (a : UInt16) (n : @& Nat) : UInt16 := ⟨a.val %ₙ n⟩
@[extern c inline "#1 & #2"]
def UInt16.land (a b : UInt16) : UInt16 := ⟨Fin.land a.val b.val⟩
@[extern c inline "#1 | #2"]
def UInt16.lor (a b : UInt16) : UInt16 := ⟨Fin.lor a.val b.val⟩
def UInt16.lt (a b : UInt16) : Prop := a.val < b.val
def UInt16.le (a b : UInt16) : Prop := a.val ≤ b.val
instance : HasZero UInt16 := ⟨UInt16.ofNat 0⟩
instance : HasOne UInt16 := ⟨UInt16.ofNat 1⟩
instance : HasOfNat UInt16 := ⟨UInt16.ofNat⟩
instance : HasAdd UInt16 := ⟨UInt16.add⟩
instance : HasSub UInt16 := ⟨UInt16.sub⟩
instance : HasMul UInt16 := ⟨UInt16.mul⟩
instance : HasMod UInt16 := ⟨UInt16.mod⟩
instance : HasModN UInt16 := ⟨UInt16.modn⟩
instance : HasDiv UInt16 := ⟨UInt16.div⟩
instance : HasLess UInt16 := ⟨UInt16.lt⟩
instance : HasLessEq UInt16 := ⟨UInt16.le⟩
instance : Inhabited UInt16 := ⟨0⟩
@[extern c inline "#1 == #2"]
def UInt16.decEq (a b : UInt16) : Decidable (a = b) :=
UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m =>
if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt16.noConfusion h' (fun h' => absurd h' h))
@[extern c inline "#1 < #2"]
def UInt16.decLt (a b : UInt16) : Decidable (a < b) :=
UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m =>
inferInstanceAs (Decidable (n < m))
@[extern c inline "#1 <= #2"]
def UInt16.decLe (a b : UInt16) : Decidable (a ≤ b) :=
UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m =>
inferInstanceAs (Decidable (n <= m))
instance : DecidableEq UInt16 := UInt16.decEq
instance UInt16.hasDecidableLt (a b : UInt16) : Decidable (a < b) := UInt16.decLt a b
instance UInt16.hasDecidableLe (a b : UInt16) : Decidable (a ≤ b) := UInt16.decLe a b
def uint32Sz : Nat := 4294967296
structure UInt32 :=
(val : Fin uint32Sz)
@[extern "lean_uint32_of_nat"]
def UInt32.ofNat (n : @& Nat) : UInt32 := ⟨Fin.ofNat n⟩
@[extern "lean_uint32_of_nat"]
def UInt32.ofNat' (n : Nat) (h : n < uint32Sz) : UInt32 := ⟨⟨n, h⟩⟩
abbrev Nat.toUInt32 := UInt32.ofNat
@[extern "lean_uint32_to_nat"]
def UInt32.toNat (n : UInt32) : Nat := n.val.val
@[extern c inline "#1 + #2"]
def UInt32.add (a b : UInt32) : UInt32 := ⟨a.val + b.val⟩
@[extern c inline "#1 - #2"]
def UInt32.sub (a b : UInt32) : UInt32 := ⟨a.val - b.val⟩
@[extern c inline "#1 * #2"]
def UInt32.mul (a b : UInt32) : UInt32 := ⟨a.val * b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 / #2"]
def UInt32.div (a b : UInt32) : UInt32 := ⟨a.val / b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 % #2"]
def UInt32.mod (a b : UInt32) : UInt32 := ⟨a.val % b.val⟩
@[extern "lean_uint32_modn"]
def UInt32.modn (a : UInt32) (n : @& Nat) : UInt32 := ⟨a.val %ₙ n⟩
@[extern c inline "#1 & #2"]
def UInt32.land (a b : UInt32) : UInt32 := ⟨Fin.land a.val b.val⟩
@[extern c inline "#1 | #2"]
def UInt32.lor (a b : UInt32) : UInt32 := ⟨Fin.lor a.val b.val⟩
def UInt32.lt (a b : UInt32) : Prop := a.val < b.val
def UInt32.le (a b : UInt32) : Prop := a.val ≤ b.val
instance : HasZero UInt32 := ⟨UInt32.ofNat 0⟩
instance : HasOne UInt32 := ⟨UInt32.ofNat 1⟩
instance : HasOfNat UInt32 := ⟨UInt32.ofNat⟩
instance : HasAdd UInt32 := ⟨UInt32.add⟩
instance : HasSub UInt32 := ⟨UInt32.sub⟩
instance : HasMul UInt32 := ⟨UInt32.mul⟩
instance : HasMod UInt32 := ⟨UInt32.mod⟩
instance : HasModN UInt32 := ⟨UInt32.modn⟩
instance : HasDiv UInt32 := ⟨UInt32.div⟩
instance : HasLess UInt32 := ⟨UInt32.lt⟩
instance : HasLessEq UInt32 := ⟨UInt32.le⟩
instance : Inhabited UInt32 := ⟨0⟩
@[extern c inline "#1 == #2"]
def UInt32.decEq (a b : UInt32) : Decidable (a = b) :=
UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m =>
if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt32.noConfusion h' (fun h' => absurd h' h))
@[extern c inline "#1 < #2"]
def UInt32.decLt (a b : UInt32) : Decidable (a < b) :=
UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m =>
inferInstanceAs (Decidable (n < m))
@[extern c inline "#1 <= #2"]
def UInt32.decLe (a b : UInt32) : Decidable (a ≤ b) :=
UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m =>
inferInstanceAs (Decidable (n <= m))
@[extern c inline "#1 << #2"]
constant UInt32.shiftLeft (a b : UInt32) : UInt32 := (arbitrary Nat).toUInt32
@[extern c inline "#1 >> #2"]
constant UInt32.shiftRight (a b : UInt32) : UInt32 := (arbitrary Nat).toUInt32
instance : DecidableEq UInt32 := UInt32.decEq
instance UInt32.hasDecidableLt (a b : UInt32) : Decidable (a < b) := UInt32.decLt a b
instance UInt32.hasDecidableLe (a b : UInt32) : Decidable (a ≤ b) := UInt32.decLe a b
def uint64Sz : Nat := 18446744073709551616
structure UInt64 :=
(val : Fin uint64Sz)
@[extern "lean_uint64_of_nat"]
def UInt64.ofNat (n : @& Nat) : UInt64 := ⟨Fin.ofNat n⟩
abbrev Nat.toUInt64 := UInt64.ofNat
@[extern "lean_uint64_to_nat"]
def UInt64.toNat (n : UInt64) : Nat := n.val.val
@[extern c inline "#1 + #2"]
def UInt64.add (a b : UInt64) : UInt64 := ⟨a.val + b.val⟩
@[extern c inline "#1 - #2"]
def UInt64.sub (a b : UInt64) : UInt64 := ⟨a.val - b.val⟩
@[extern c inline "#1 * #2"]
def UInt64.mul (a b : UInt64) : UInt64 := ⟨a.val * b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 / #2"]
def UInt64.div (a b : UInt64) : UInt64 := ⟨a.val / b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 % #2"]
def UInt64.mod (a b : UInt64) : UInt64 := ⟨a.val % b.val⟩
@[extern "lean_uint64_modn"]
def UInt64.modn (a : UInt64) (n : @& Nat) : UInt64 := ⟨a.val %ₙ n⟩
@[extern c inline "#1 & #2"]
def UInt64.land (a b : UInt64) : UInt64 := ⟨Fin.land a.val b.val⟩
@[extern c inline "#1 | #2"]
def UInt64.lor (a b : UInt64) : UInt64 := ⟨Fin.lor a.val b.val⟩
def UInt64.lt (a b : UInt64) : Prop := a.val < b.val
def UInt64.le (a b : UInt64) : Prop := a.val ≤ b.val
@[extern c inline "((uint8_t)#1)"]
def UInt64.toUInt8 (a : UInt64) : UInt8 := a.toNat.toUInt8
@[extern c inline "((uint16_t)#1)"]
def UInt64.toUInt16 (a : UInt64) : UInt16 := a.toNat.toUInt16
@[extern c inline "((uint32_t)#1)"]
def UInt64.toUInt32 (a : UInt64) : UInt32 := a.toNat.toUInt32
@[extern c inline "((uint64_t)#1)"]
def UInt32.toUInt64 (a : UInt32) : UInt64 := a.toNat.toUInt64
-- TODO(Leo): give reference implementation for shiftLeft and shiftRight, and define them for other UInt types
@[extern c inline "#1 << #2"]
constant UInt64.shiftLeft (a b : UInt64) : UInt64 := (arbitrary Nat).toUInt64
@[extern c inline "#1 >> #2"]
constant UInt64.shiftRight (a b : UInt64) : UInt64 := (arbitrary Nat).toUInt64
instance : HasZero UInt64 := ⟨UInt64.ofNat 0⟩
instance : HasOne UInt64 := ⟨UInt64.ofNat 1⟩
instance : HasOfNat UInt64 := ⟨UInt64.ofNat⟩
instance : HasAdd UInt64 := ⟨UInt64.add⟩
instance : HasSub UInt64 := ⟨UInt64.sub⟩
instance : HasMul UInt64 := ⟨UInt64.mul⟩
instance : HasMod UInt64 := ⟨UInt64.mod⟩
instance : HasModN UInt64 := ⟨UInt64.modn⟩
instance : HasDiv UInt64 := ⟨UInt64.div⟩
instance : HasLess UInt64 := ⟨UInt64.lt⟩
instance : HasLessEq UInt64 := ⟨UInt64.le⟩
instance : Inhabited UInt64 := ⟨0⟩
@[extern c inline "(uint64_t)#1"]
def Bool.toUInt64 (b : Bool) : UInt64 := if b then 1 else 0
@[extern c inline "#1 == #2"]
def UInt64.decEq (a b : UInt64) : Decidable (a = b) :=
UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m =>
if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt64.noConfusion h' (fun h' => absurd h' h))
@[extern c inline "#1 < #2"]
def UInt64.decLt (a b : UInt64) : Decidable (a < b) :=
UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m =>
inferInstanceAs (Decidable (n < m))
@[extern c inline "#1 <= #2"]
def UInt64.decLe (a b : UInt64) : Decidable (a ≤ b) :=
UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m =>
inferInstanceAs (Decidable (n <= m))
instance : DecidableEq UInt64 := UInt64.decEq
instance UInt64.hasDecidableLt (a b : UInt64) : Decidable (a < b) := UInt64.decLt a b
instance UInt64.hasDecidableLe (a b : UInt64) : Decidable (a ≤ b) := UInt64.decLe a b
def usizeSz : Nat := (2:Nat) ^ System.Platform.numBits
structure USize :=
(val : Fin usizeSz)
theorem usizeSzGt0 : usizeSz > 0 :=
Nat.posPowOfPos System.Platform.numBits (Nat.zeroLtSucc _)
@[extern "lean_usize_of_nat"]
def USize.ofNat (n : @& Nat) : USize := ⟨Fin.ofNat' n usizeSzGt0⟩
abbrev Nat.toUSize := USize.ofNat
@[extern "lean_usize_to_nat"]
def USize.toNat (n : USize) : Nat := n.val.val
@[extern c inline "#1 + #2"]
def USize.add (a b : USize) : USize := ⟨a.val + b.val⟩
@[extern c inline "#1 - #2"]
def USize.sub (a b : USize) : USize := ⟨a.val - b.val⟩
@[extern c inline "#1 * #2"]
def USize.mul (a b : USize) : USize := ⟨a.val * b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 / #2"]
def USize.div (a b : USize) : USize := ⟨a.val / b.val⟩
@[extern c inline "#2 == 0 ? 0 : #1 % #2"]
def USize.mod (a b : USize) : USize := ⟨a.val % b.val⟩
@[extern "lean_usize_modn"]
def USize.modn (a : USize) (n : @& Nat) : USize := ⟨a.val %ₙ n⟩
@[extern c inline "#1 & #2"]
def USize.land (a b : USize) : USize := ⟨Fin.land a.val b.val⟩
@[extern c inline "#1 | #2"]
def USize.lor (a b : USize) : USize := ⟨Fin.lor a.val b.val⟩
@[extern c inline "#1"]
def UInt32.toUSize (a : UInt32) : USize := a.toNat.toUSize
@[extern c inline "((size_t)#1)"]
def UInt64.toUSize (a : UInt64) : USize := a.toNat.toUSize
@[extern c inline "(uint32_t)#1"]
def USize.toUInt32 (a : USize) : UInt32 := a.toNat.toUInt32
-- TODO(Leo): give reference implementation for shiftLeft and shiftRight, and define them for other UInt types
@[extern c inline "#1 << #2"]
constant USize.shiftLeft (a b : USize) : USize := (arbitrary Nat).toUSize
@[extern c inline "#1 >> #2"]
constant USize.shiftRight (a b : USize) : USize := (arbitrary Nat).toUSize
def USize.lt (a b : USize) : Prop := a.val < b.val
def USize.le (a b : USize) : Prop := a.val ≤ b.val
instance : HasZero USize := ⟨USize.ofNat 0⟩
instance : HasOne USize := ⟨USize.ofNat 1⟩
instance : HasOfNat USize := ⟨USize.ofNat⟩
instance : HasAdd USize := ⟨USize.add⟩
instance : HasSub USize := ⟨USize.sub⟩
instance : HasMul USize := ⟨USize.mul⟩
instance : HasMod USize := ⟨USize.mod⟩
instance : HasModN USize := ⟨USize.modn⟩
instance : HasDiv USize := ⟨USize.div⟩
instance : HasLess USize := ⟨USize.lt⟩
instance : HasLessEq USize := ⟨USize.le⟩
instance : Inhabited USize := ⟨0⟩
@[extern c inline "#1 == #2"]
def USize.decEq (a b : USize) : Decidable (a = b) :=
USize.casesOn a $ fun n => USize.casesOn b $ fun m =>
if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => USize.noConfusion h' (fun h' => absurd h' h))
@[extern c inline "#1 < #2"]
def USize.decLt (a b : USize) : Decidable (a < b) :=
USize.casesOn a $ fun n => USize.casesOn b $ fun m =>
inferInstanceAs (Decidable (n < m))
@[extern c inline "#1 <= #2"]
def USize.decLe (a b : USize) : Decidable (a ≤ b) :=
USize.casesOn a $ fun n => USize.casesOn b $ fun m =>
inferInstanceAs (Decidable (n <= m))
instance : DecidableEq USize := USize.decEq
instance USize.hasDecidableLt (a b : USize) : Decidable (a < b) := USize.decLt a b
instance USize.hasDecidableLe (a b : USize) : Decidable (a ≤ b) := USize.decLe a b
theorem USize.modnLt {m : Nat} : ∀ (u : USize), m > 0 → USize.toNat (u %ₙ m) < m
| ⟨u⟩, h => Fin.modnLt u h
|
92ef31e52a707ad6abc2fc09df37a8e9ae85dd19 | 05f637fa14ac28031cb1ea92086a0f4eb23ff2b1 | /tests/lean/tst1.lean | 6a80d242d6d75fde82662562d33651851984fadb | [
"Apache-2.0"
] | permissive | codyroux/lean0.1 | 1ce92751d664aacff0529e139083304a7bbc8a71 | 0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef | refs/heads/master | 1,610,830,535,062 | 1,402,150,480,000 | 1,402,150,480,000 | 19,588,851 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,146 | lean | -- Define a "fake" type to simulate the natural numbers. This is just a test.
variable N : Type
variable lt : N -> N -> Bool
variable zero : N
variable one : N
variable two : N
variable three : N
infix 50 < : lt
axiom two_lt_three : two < three
definition vector (A : Type) (n : N) : Type := forall (i : N) (H : i < n), A
definition const {A : Type} (n : N) (d : A) : vector A n := fun (i : N) (H : i < n), d
definition update {A : Type} {n : N} (v : vector A n) (i : N) (d : A) : vector A n := fun (j : N) (H : j < n), if (j = i) then d else (v j H)
definition select {A : Type} {n : N} (v : vector A n) (i : N) (H : i < n) : A := v i H
definition map {A B C : Type} {n : N} (f : A -> B -> C) (v1 : vector A n) (v2 : vector B n) : vector C n := fun (i : N) (H : i < n), f (v1 i H) (v2 i H)
print environment 10
check select (update (const three false) two true) two two_lt_three
eval select (update (const three false) two true) two two_lt_three
check update (const three false) two true
print "\n--------"
check @select
print "\nmap type ---> "
check @map
print "\nmap normal form --> "
eval @map
print "\nupdate normal form --> "
eval @update
|
d2c53f88a3e1b2718045c94bbfc630f2188a0156 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/fun_like/embedding.lean | e896144a9f438b2099cdcaf3cda35d71d29ee20a | [
"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 | 5,789 | lean | /-
Copyright (c) 2021 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import data.fun_like.basic
/-!
# Typeclass for a type `F` with an injective map to `A ↪ B`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> https://github.com/leanprover-community/mathlib4/pull/541
> Any changes to this file require a corresponding PR to mathlib4.
This typeclass is primarily for use by embeddings such as `rel_embedding`.
## Basic usage of `embedding_like`
A typical type of embedding should be declared as:
```
structure my_embedding (A B : Type*) [my_class A] [my_class B] :=
(to_fun : A → B)
(injective' : function.injective to_fun)
(map_op' : ∀ {x y : A}, to_fun (my_class.op x y) = my_class.op (to_fun x) (to_fun y))
namespace my_embedding
variables (A B : Type*) [my_class A] [my_class B]
-- This instance is optional if you follow the "Embedding class" design below:
instance : embedding_like (my_embedding A B) A B :=
{ coe := my_embedding.to_fun,
coe_injective' := λ f g h, by cases f; cases g; congr',
injective' := my_embedding.injective' }
/-- Helper instance for when there's too many metavariables to directly
apply `fun_like.to_coe_fn`. -/
instance : has_coe_to_fun (my_embedding A B) (λ _, A → B) := ⟨my_embedding.to_fun⟩
@[simp] lemma to_fun_eq_coe {f : my_embedding A B} : f.to_fun = (f : A → B) := rfl
@[ext] theorem ext {f g : my_embedding A B} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h
/-- Copy of a `my_embedding` with a new `to_fun` equal to the old one. Useful to fix definitional
equalities. -/
protected def copy (f : my_embedding A B) (f' : A → B) (h : f' = ⇑f) : my_embedding A B :=
{ to_fun := f',
injective' := h.symm ▸ f.injective',
map_op' := h.symm ▸ f.map_op' }
end my_embedding
```
This file will then provide a `has_coe_to_fun` instance and various
extensionality and simp lemmas.
## Embedding classes extending `embedding_like`
The `embedding_like` design provides further benefits if you put in a bit more work.
The first step is to extend `embedding_like` to create a class of those types satisfying
the axioms of your new type of morphisms.
Continuing the example above:
```
section
set_option old_structure_cmd true
/-- `my_embedding_class F A B` states that `F` is a type of `my_class.op`-preserving embeddings.
You should extend this class when you extend `my_embedding`. -/
class my_embedding_class (F : Type*) (A B : out_param $ Type*) [my_class A] [my_class B]
extends embedding_like F A B :=
(map_op : ∀ (f : F) (x y : A), f (my_class.op x y) = my_class.op (f x) (f y))
end
@[simp] lemma map_op {F A B : Type*} [my_class A] [my_class B] [my_embedding_class F A B]
(f : F) (x y : A) : f (my_class.op x y) = my_class.op (f x) (f y) :=
my_embedding_class.map_op
-- You can replace `my_embedding.embedding_like` with the below instance:
instance : my_embedding_class (my_embedding A B) A B :=
{ coe := my_embedding.to_fun,
coe_injective' := λ f g h, by cases f; cases g; congr',
injective' := my_embedding.injective',
map_op := my_embedding.map_op' }
-- [Insert `has_coe_to_fun`, `to_fun_eq_coe`, `ext` and `copy` here]
```
The second step is to add instances of your new `my_embedding_class` for all types extending
`my_embedding`.
Typically, you can just declare a new class analogous to `my_embedding_class`:
```
structure cooler_embedding (A B : Type*) [cool_class A] [cool_class B]
extends my_embedding A B :=
(map_cool' : to_fun cool_class.cool = cool_class.cool)
section
set_option old_structure_cmd true
class cooler_embedding_class (F : Type*) (A B : out_param $ Type*) [cool_class A] [cool_class B]
extends my_embedding_class F A B :=
(map_cool : ∀ (f : F), f cool_class.cool = cool_class.cool)
end
@[simp] lemma map_cool {F A B : Type*} [cool_class A] [cool_class B] [cooler_embedding_class F A B]
(f : F) : f cool_class.cool = cool_class.cool :=
my_embedding_class.map_op
-- You can also replace `my_embedding.embedding_like` with the below instance:
instance : cool_embedding_class (cool_embedding A B) A B :=
{ coe := cool_embedding.to_fun,
coe_injective' := λ f g h, by cases f; cases g; congr',
injective' := my_embedding.injective',
map_op := cool_embedding.map_op',
map_cool := cool_embedding.map_cool' }
-- [Insert `has_coe_to_fun`, `to_fun_eq_coe`, `ext` and `copy` here]
```
Then any declaration taking a specific type of morphisms as parameter can instead take the
class you just defined:
```
-- Compare with: lemma do_something (f : my_embedding A B) : sorry := sorry
lemma do_something {F : Type*} [my_embedding_class F A B] (f : F) : sorry := sorry
```
This means anything set up for `my_embedding`s will automatically work for `cool_embedding_class`es,
and defining `cool_embedding_class` only takes a constant amount of effort,
instead of linearly increasing the work per `my_embedding`-related declaration.
-/
set_option old_structure_cmd true
/-- The class `embedding_like F α β` expresses that terms of type `F` have an
injective coercion to injective functions `α ↪ β`.
-/
class embedding_like (F : Sort*) (α β : out_param Sort*)
extends fun_like F α (λ _, β) :=
(injective' : ∀ (f : F), @function.injective α β (coe f))
namespace embedding_like
variables {F α β γ : Sort*} [i : embedding_like F α β]
include i
protected lemma injective (f : F) : function.injective f := injective' f
@[simp] lemma apply_eq_iff_eq (f : F) {x y : α} : f x = f y ↔ x = y :=
(embedding_like.injective f).eq_iff
omit i
@[simp] lemma comp_injective {F : Sort*} [embedding_like F β γ] (f : α → β) (e : F) :
function.injective (e ∘ f) ↔ function.injective f :=
(embedding_like.injective e).of_comp_iff f
end embedding_like
|
7c30af2a98d25f30008c613db489f876397fd64a | e8159fb1a58abf09f7d75106ba3aa6c1c504c038 | /theorem-proving-in-lean/5.8.2. tactic combinators.lean | 3426df65c2ac61db41fab1af9c5966a8d3779943 | [] | no_license | ndcroos/lean-snippets | cbf53e777a72964b6875a8c98bdd7eb02e74e246 | b66736347cd80a4143e43397f359dbdf9cbcd491 | refs/heads/master | 1,584,160,922,314 | 1,525,542,053,000 | 1,525,542,053,000 | 131,447,243 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 178 | lean | /-
Use tactic combinators to obtain a one line proof of the following:
example (p q r : Prop) (hp : p) :
(p ∨ q ∨ r) ∧ (q ∨ p ∨ r) ∧ (q ∨ r ∨ p) :=
by admit
-/
|
1866aa41f05cf8025bc257a4f9f6ea18d2556cb9 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/monoidal/CommMon_.lean | a1927522cdd782cc05c751dbed11113ebdb6d6e3 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,945 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.monoidal.braided
import Mathlib.category_theory.monoidal.Mon_
import Mathlib.PostPort
universes v₁ u₁ l u₂ v₂ u_1 u_2
namespace Mathlib
/-!
# The category of commutative monoids in a braided monoidal category.
-/
/--
A commutative monoid object internal to a monoidal category.
-/
structure CommMon_ (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C]
extends Mon_ C
where
mul_comm' : autoParam (category_theory.iso.hom β_ ≫ Mon_.mul _to_Mon_ = Mon_.mul _to_Mon_)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
@[simp] theorem CommMon_.mul_comm {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (c : CommMon_ C) : category_theory.iso.hom β_ ≫ Mon_.mul (CommMon_.to_Mon_ c) = Mon_.mul (CommMon_.to_Mon_ c) := sorry
@[simp] theorem CommMon_.mul_comm_assoc {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (c : CommMon_ C) {X' : C} (f' : Mon_.X (CommMon_.to_Mon_ c) ⟶ X') : category_theory.iso.hom β_ ≫ Mon_.mul (CommMon_.to_Mon_ c) ≫ f' = Mon_.mul (CommMon_.to_Mon_ c) ≫ f' := sorry
namespace CommMon_
/--
The trivial commutative monoid object. We later show this is initial in `CommMon_ C`.
-/
def trivial (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] : CommMon_ C :=
mk (Mon_.mk (Mon_.X (Mon_.trivial C)) (Mon_.one (Mon_.trivial C)) (Mon_.mul (Mon_.trivial C)))
protected instance inhabited (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] : Inhabited (CommMon_ C) :=
{ default := trivial C }
protected instance category_theory.category {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] : category_theory.category (CommMon_ C) :=
category_theory.induced_category.category to_Mon_
@[simp] theorem id_hom {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (A : CommMon_ C) : Mon_.hom.hom 𝟙 = 𝟙 :=
rfl
@[simp] theorem comp_hom {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] {R : CommMon_ C} {S : CommMon_ C} {T : CommMon_ C} (f : R ⟶ S) (g : S ⟶ T) : Mon_.hom.hom (f ≫ g) = Mon_.hom.hom f ≫ Mon_.hom.hom g :=
rfl
/-- The forgetful functor from commutative monoid objects to monoid objects. -/
def forget₂_Mon_ (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] : CommMon_ C ⥤ Mon_ C :=
category_theory.induced_functor to_Mon_
@[simp] theorem forget₂_Mon_obj_one (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (A : CommMon_ C) : Mon_.one (category_theory.functor.obj (forget₂_Mon_ C) A) = Mon_.one (to_Mon_ A) :=
rfl
@[simp] theorem forget₂_Mon_obj_mul (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (A : CommMon_ C) : Mon_.mul (category_theory.functor.obj (forget₂_Mon_ C) A) = Mon_.mul (to_Mon_ A) :=
rfl
@[simp] theorem forget₂_Mon_map_hom (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] {A : CommMon_ C} {B : CommMon_ C} (f : A ⟶ B) : Mon_.hom.hom (category_theory.functor.map (forget₂_Mon_ C) f) = Mon_.hom.hom f :=
rfl
protected instance unique_hom_from_trivial {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (A : CommMon_ C) : unique (trivial C ⟶ A) :=
Mon_.unique_hom_from_trivial (to_Mon_ A)
protected instance category_theory.limits.has_initial {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] : category_theory.limits.has_initial (CommMon_ C) :=
category_theory.limits.has_initial_of_unique (trivial C)
end CommMon_
namespace category_theory.lax_braided_functor
/--
A lax braided functor takes commutative monoid objects to commutative monoid objects.
That is, a lax braided functor `F : C ⥤ D` induces a functor `CommMon_ C ⥤ CommMon_ D`.
-/
@[simp] theorem map_CommMon_map {C : Type u₁} [category C] [monoidal_category C] [braided_category C] {D : Type u₂} [category D] [monoidal_category D] [braided_category D] (F : lax_braided_functor C D) (A : CommMon_ C) (B : CommMon_ C) (f : A ⟶ B) : functor.map (map_CommMon F) f = functor.map (lax_monoidal_functor.map_Mon (to_lax_monoidal_functor F)) f :=
Eq.refl (functor.map (map_CommMon F) f)
/-- `map_CommMon` is functorial in the lax braided functor. -/
def map_CommMon_functor (C : Type u₁) [category C] [monoidal_category C] [braided_category C] (D : Type u₂) [category D] [monoidal_category D] [braided_category D] : lax_braided_functor C D ⥤ CommMon_ C ⥤ CommMon_ D :=
functor.mk map_CommMon
fun (F G : lax_braided_functor C D) (α : F ⟶ G) =>
nat_trans.mk
fun (A : CommMon_ C) =>
Mon_.hom.mk (nat_trans.app (monoidal_nat_trans.to_nat_trans α) (Mon_.X (CommMon_.to_Mon_ A)))
end category_theory.lax_braided_functor
namespace CommMon_
namespace equiv_lax_braided_functor_punit
/-- Implementation of `CommMon_.equiv_lax_braided_functor_punit`. -/
@[simp] theorem lax_braided_to_CommMon_map (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (F : category_theory.lax_braided_functor (category_theory.discrete PUnit) C) (G : category_theory.lax_braided_functor (category_theory.discrete PUnit) C) (α : F ⟶ G) : category_theory.functor.map (lax_braided_to_CommMon C) α =
category_theory.nat_trans.app
(category_theory.functor.map
(category_theory.lax_braided_functor.map_CommMon_functor (category_theory.discrete PUnit) C) α)
(trivial (category_theory.discrete PUnit)) :=
Eq.refl (category_theory.functor.map (lax_braided_to_CommMon C) α)
/-- Implementation of `CommMon_.equiv_lax_braided_functor_punit`. -/
@[simp] theorem CommMon_to_lax_braided_obj_to_lax_monoidal_functor_to_functor_obj (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (A : CommMon_ C) (_x : category_theory.discrete PUnit) : category_theory.functor.obj
(category_theory.lax_monoidal_functor.to_functor
(category_theory.lax_braided_functor.to_lax_monoidal_functor
(category_theory.functor.obj (CommMon_to_lax_braided C) A)))
_x =
Mon_.X (to_Mon_ A) := sorry
/-- Implementation of `CommMon_.equiv_lax_braided_functor_punit`. -/
@[simp] theorem unit_iso_hom_app_to_nat_trans_app (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (X : category_theory.lax_braided_functor (category_theory.discrete PUnit) C) : ∀ (X_1 : category_theory.discrete PUnit),
category_theory.nat_trans.app
(category_theory.monoidal_nat_trans.to_nat_trans
(category_theory.nat_trans.app (category_theory.iso.hom (unit_iso C)) X))
X_1 =
category_theory.eq_to_hom
(congr_arg
(category_theory.functor.obj
(category_theory.lax_monoidal_functor.to_functor
(category_theory.lax_braided_functor.to_lax_monoidal_functor X)))
(unit_iso._proof_1 X_1)) := sorry
/-- Implementation of `CommMon_.equiv_lax_braided_functor_punit`. -/
@[simp] theorem counit_iso_inv_app_hom (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] (X : CommMon_ C) : Mon_.hom.hom (category_theory.nat_trans.app (category_theory.iso.inv (counit_iso C)) X) = 𝟙 :=
Eq.refl 𝟙
end equiv_lax_braided_functor_punit
/--
Commutative monoid objects in `C` are "just" braided lax monoidal functors from the trivial
braided monoidal category to `C`.
-/
@[simp] theorem equiv_lax_braided_functor_punit_functor (C : Type u₁) [category_theory.category C] [category_theory.monoidal_category C] [category_theory.braided_category C] : category_theory.equivalence.functor (equiv_lax_braided_functor_punit C) =
equiv_lax_braided_functor_punit.lax_braided_to_CommMon C :=
Eq.refl (category_theory.equivalence.functor (equiv_lax_braided_functor_punit C))
|
ac48442436ae77e4eb0e96d499e654a70ac415b9 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/analysis/convex/slope.lean | 4fac058af33e965116a935b0898d2e72c283c5e5 | [
"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 | 5,344 | lean | /-
Copyright (c) 2021 Yury Kudriashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudriashov, Malo Jaffré
-/
import analysis.convex.function
/-!
# Slopes of convex functions
This file relates convexity/concavity of functions in a linearly ordered field and the monotonicity
of their slopes.
The main use is to show convexity/concavity from monotonicity of the derivative.
-/
variables {𝕜 : Type*} [linear_ordered_field 𝕜] {s : set 𝕜} {f : 𝕜 → 𝕜}
/-- If `f : 𝕜 → 𝕜` is convex, then for any three points `x < y < z` the slope of the secant line of
`f` on `[x, y]` is less than the slope of the secant line of `f` on `[x, z]`. -/
lemma convex_on.slope_mono_adjacent (hf : convex_on 𝕜 s f)
{x y z : 𝕜} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) :
(f y - f x) / (y - x) ≤ (f z - f y) / (z - y) :=
begin
have hxz := hxy.trans hyz,
rw ←sub_pos at hxy hxz hyz,
suffices : f y / (y - x) + f y / (z - y) ≤ f x / (y - x) + f z / (z - y),
{ ring_nf at this ⊢, linarith },
set a := (z - y) / (z - x),
set b := (y - x) / (z - x),
have hy : a • x + b • z = y, by { field_simp, rw div_eq_iff; [ring, linarith] },
have key, from
hf.2 hx hz
(show 0 ≤ a, by apply div_nonneg; linarith)
(show 0 ≤ b, by apply div_nonneg; linarith)
(show a + b = 1, by { field_simp, rw div_eq_iff; [ring, linarith] }),
rw hy at key,
replace key := mul_le_mul_of_nonneg_left key hxz.le,
field_simp [hxy.ne', hyz.ne', hxz.ne', mul_comm (z - x) _] at key ⊢,
rw div_le_div_right,
{ linarith },
{ nlinarith }
end
/-- If `f : 𝕜 → 𝕜` is concave, then for any three points `x < y < z` the slope of the secant line of
`f` on `[x, y]` is greater than the slope of the secant line of `f` on `[x, z]`. -/
lemma concave_on.slope_anti_adjacent (hf : concave_on 𝕜 s f) {x y z : 𝕜} (hx : x ∈ s)
(hz : z ∈ s) (hxy : x < y) (hyz : y < z) :
(f z - f y) / (z - y) ≤ (f y - f x) / (y - x) :=
begin
rw [←neg_le_neg_iff, ←neg_sub_neg (f x), ←neg_sub_neg (f y)],
simp_rw [←pi.neg_apply, ←neg_div, neg_sub],
exact convex_on.slope_mono_adjacent hf.neg hx hz hxy hyz,
end
/-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is
less than the slope of the secant line of `f` on `[x, z]`, then `f` is convex. -/
lemma convex_on_of_slope_mono_adjacent (hs : convex 𝕜 s)
(hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z →
(f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) :
convex_on 𝕜 s f :=
linear_order.convex_on_of_lt hs
begin
assume x z hx hz hxz a b ha hb hab,
let y := a * x + b * z,
have hxy : x < y,
{ rw [← one_mul x, ← hab, add_mul],
exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ },
have hyz : y < z,
{ rw [← one_mul z, ← hab, add_mul],
exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ },
have : (f y - f x) * (z - y) ≤ (f z - f y) * (y - x),
from (div_le_div_iff (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz),
have hxz : 0 < z - x, from sub_pos.2 (hxy.trans hyz),
have ha : (z - y) / (z - x) = a,
{ rw [eq_comm, ← sub_eq_iff_eq_add'] at hab,
simp_rw [div_eq_iff hxz.ne', y, ←hab], ring },
have hb : (y - x) / (z - x) = b,
{ rw [eq_comm, ← sub_eq_iff_eq_add] at hab,
simp_rw [div_eq_iff hxz.ne', y, ←hab], ring },
rwa [sub_mul, sub_mul, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le, ← mul_add,
sub_add_sub_cancel, ← le_div_iff hxz, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x),
mul_comm (f z), ha, hb] at this,
end
/-- If for any three points `x < y < z`, the slope of the secant line of `f : 𝕜 → 𝕜` on `[x, y]` is
greater than the slope of the secant line of `f` on `[x, z]`, then `f` is concave. -/
lemma concave_on_of_slope_anti_adjacent (hs : convex 𝕜 s)
(hf : ∀ {x y z : 𝕜}, x ∈ s → z ∈ s → x < y → y < z →
(f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) : concave_on 𝕜 s f :=
begin
rw ←neg_convex_on_iff,
refine convex_on_of_slope_mono_adjacent hs (λ x y z hx hz hxy hyz, _),
rw ←neg_le_neg_iff,
simp_rw [←neg_div, neg_sub, pi.neg_apply, neg_sub_neg],
exact hf hx hz hxy hyz,
end
/-- A function `f : 𝕜 → 𝕜` is convex iff for any three points `x < y < z` the slope of the secant
line of `f` on `[x, y]` is less than the slope of the secant line of `f` on `[x, z]`. -/
lemma convex_on_iff_slope_mono_adjacent :
convex_on 𝕜 s f ↔ convex 𝕜 s ∧
∀ ⦃x y z : 𝕜⦄, x ∈ s → z ∈ s → x < y → y < z →
(f y - f x) / (y - x) ≤ (f z - f y) / (z - y) :=
⟨λ h, ⟨h.1, λ x y z, h.slope_mono_adjacent⟩, λ h, convex_on_of_slope_mono_adjacent h.1 h.2⟩
/-- A function `f : 𝕜 → 𝕜` is concave iff for any three points `x < y < z` the slope of the secant
line of `f` on `[x, y]` is greater than the slope of the secant line of `f` on `[x, z]`. -/
lemma concave_on_iff_slope_anti_adjacent :
concave_on 𝕜 s f ↔ convex 𝕜 s ∧
∀ ⦃x y z : 𝕜⦄, x ∈ s → z ∈ s → x < y → y < z →
(f z - f y) / (z - y) ≤ (f y - f x) / (y - x) :=
⟨λ h, ⟨h.1, λ x y z, h.slope_anti_adjacent⟩, λ h, concave_on_of_slope_anti_adjacent h.1 h.2⟩
|
e994c64067815bca9fade1fb597fc59b7936fe61 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/ring_theory/polynomial/gauss_lemma.lean | c0681583f97d54ec52730343a1d00d3224965cd4 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 8,008 | lean | /-
Copyright (c) 2020 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import ring_theory.int.basic
import ring_theory.localization.integral
/-!
# Gauss's Lemma
Gauss's Lemma is one of a few results pertaining to irreducibility of primitive polynomials.
## Main Results
- `polynomial.is_primitive.irreducible_iff_irreducible_map_fraction_map`:
A primitive polynomial is irreducible iff it is irreducible in a fraction field.
- `polynomial.is_primitive.int.irreducible_iff_irreducible_map_cast`:
A primitive polynomial over `ℤ` is irreducible iff it is irreducible over `ℚ`.
- `polynomial.is_primitive.dvd_iff_fraction_map_dvd_fraction_map`:
Two primitive polynomials divide each other iff they do in a fraction field.
- `polynomial.is_primitive.int.dvd_iff_map_cast_dvd_map_cast`:
Two primitive polynomials over `ℤ` divide each other if they do in `ℚ`.
-/
open_locale non_zero_divisors polynomial
variables {R : Type*} [comm_ring R] [is_domain R]
namespace polynomial
section normalized_gcd_monoid
variable [normalized_gcd_monoid R]
section
variables {S : Type*} [comm_ring S] [is_domain S] {φ : R →+* S} (hinj : function.injective φ)
variables {f : R[X]} (hf : f.is_primitive)
include hinj hf
lemma is_primitive.is_unit_iff_is_unit_map_of_injective :
is_unit f ↔ is_unit (map φ f) :=
begin
refine ⟨(map_ring_hom φ).is_unit_map, λ h, _⟩,
rcases is_unit_iff.1 h with ⟨_, ⟨u, rfl⟩, hu⟩,
have hdeg := degree_C u.ne_zero,
rw [hu, degree_map_eq_of_injective hinj] at hdeg,
rw [eq_C_of_degree_eq_zero hdeg, is_primitive_iff_content_eq_one,
content_C, normalize_eq_one] at hf,
rwa [eq_C_of_degree_eq_zero hdeg, is_unit_C],
end
lemma is_primitive.irreducible_of_irreducible_map_of_injective (h_irr : irreducible (map φ f)) :
irreducible f :=
begin
refine ⟨λ h, h_irr.not_unit (is_unit.map (map_ring_hom φ) h), _⟩,
intros a b h,
rcases h_irr.is_unit_or_is_unit (by rw [h, polynomial.map_mul]) with hu | hu,
{ left,
rwa (hf.is_primitive_of_dvd (dvd.intro _ h.symm)).is_unit_iff_is_unit_map_of_injective hinj },
right,
rwa (hf.is_primitive_of_dvd (dvd.intro_left _ h.symm)).is_unit_iff_is_unit_map_of_injective hinj
end
end
section fraction_map
variables {K : Type*} [field K] [algebra R K] [is_fraction_ring R K]
lemma is_primitive.is_unit_iff_is_unit_map {p : R[X]} (hp : p.is_primitive) :
is_unit p ↔ is_unit (p.map (algebra_map R K)) :=
hp.is_unit_iff_is_unit_map_of_injective (is_fraction_ring.injective _ _)
open is_localization
lemma is_unit_or_eq_zero_of_is_unit_integer_normalization_prim_part
{p : K[X]} (h0 : p ≠ 0) (h : is_unit (integer_normalization R⁰ p).prim_part) :
is_unit p :=
begin
rcases is_unit_iff.1 h with ⟨_, ⟨u, rfl⟩, hu⟩,
obtain ⟨⟨c, c0⟩, hc⟩ := integer_normalization_map_to_map R⁰ p,
rw [subtype.coe_mk, algebra.smul_def, algebra_map_apply] at hc,
apply is_unit_of_mul_is_unit_right,
rw [← hc, (integer_normalization R⁰ p).eq_C_content_mul_prim_part, ← hu,
← ring_hom.map_mul, is_unit_iff],
refine ⟨algebra_map R K ((integer_normalization R⁰ p).content * ↑u),
is_unit_iff_ne_zero.2 (λ con, _), by simp⟩,
replace con := (injective_iff_map_eq_zero (algebra_map R K)).1
(is_fraction_ring.injective _ _) _ con,
rw [mul_eq_zero, content_eq_zero_iff, is_fraction_ring.integer_normalization_eq_zero_iff] at con,
rcases con with con | con,
{ apply h0 con },
{ apply units.ne_zero _ con },
end
/-- **Gauss's Lemma** states that a primitive polynomial is irreducible iff it is irreducible in the
fraction field. -/
theorem is_primitive.irreducible_iff_irreducible_map_fraction_map
{p : R[X]} (hp : p.is_primitive) :
irreducible p ↔ irreducible (p.map (algebra_map R K)) :=
begin
refine ⟨λ hi, ⟨λ h, hi.not_unit (hp.is_unit_iff_is_unit_map.2 h), λ a b hab, _⟩,
hp.irreducible_of_irreducible_map_of_injective (is_fraction_ring.injective _ _)⟩,
obtain ⟨⟨c, c0⟩, hc⟩ := integer_normalization_map_to_map R⁰ a,
obtain ⟨⟨d, d0⟩, hd⟩ := integer_normalization_map_to_map R⁰ b,
rw [algebra.smul_def, algebra_map_apply, subtype.coe_mk] at hc hd,
rw mem_non_zero_divisors_iff_ne_zero at c0 d0,
have hcd0 : c * d ≠ 0 := mul_ne_zero c0 d0,
rw [ne.def, ← C_eq_zero] at hcd0,
have h1 : C c * C d * p = integer_normalization R⁰ a * integer_normalization R⁰ b,
{ apply map_injective (algebra_map R K) (is_fraction_ring.injective _ _) _,
rw [polynomial.map_mul, polynomial.map_mul, polynomial.map_mul, hc, hd, map_C, map_C, hab],
ring },
obtain ⟨u, hu⟩ : associated (c * d) (content (integer_normalization R⁰ a) *
content (integer_normalization R⁰ b)),
{ rw [← dvd_dvd_iff_associated, ← normalize_eq_normalize_iff, normalize.map_mul,
normalize.map_mul, normalize_content, normalize_content,
← mul_one (normalize c * normalize d), ← hp.content_eq_one, ← content_C, ← content_C,
← content_mul, ← content_mul, ← content_mul, h1] },
rw [← ring_hom.map_mul, eq_comm,
(integer_normalization R⁰ a).eq_C_content_mul_prim_part,
(integer_normalization R⁰ b).eq_C_content_mul_prim_part, mul_assoc,
mul_comm _ (C _ * _), ← mul_assoc, ← mul_assoc, ← ring_hom.map_mul, ← hu, ring_hom.map_mul,
mul_assoc, mul_assoc, ← mul_assoc (C ↑u)] at h1,
have h0 : (a ≠ 0) ∧ (b ≠ 0),
{ classical,
rw [ne.def, ne.def, ← decidable.not_or_iff_and_not, ← mul_eq_zero, ← hab],
intro con,
apply hp.ne_zero (map_injective (algebra_map R K) (is_fraction_ring.injective _ _) _),
simp [con] },
rcases hi.is_unit_or_is_unit (mul_left_cancel₀ hcd0 h1).symm with h | h,
{ right,
apply is_unit_or_eq_zero_of_is_unit_integer_normalization_prim_part h0.2
(is_unit_of_mul_is_unit_right h) },
{ left,
apply is_unit_or_eq_zero_of_is_unit_integer_normalization_prim_part h0.1 h },
end
lemma is_primitive.dvd_of_fraction_map_dvd_fraction_map {p q : R[X]}
(hp : p.is_primitive) (hq : q.is_primitive)
(h_dvd : p.map (algebra_map R K) ∣ q.map (algebra_map R K)) : p ∣ q :=
begin
rcases h_dvd with ⟨r, hr⟩,
obtain ⟨⟨s, s0⟩, hs⟩ := integer_normalization_map_to_map R⁰ r,
rw [subtype.coe_mk, algebra.smul_def, algebra_map_apply] at hs,
have h : p ∣ q * C s,
{ use (integer_normalization R⁰ r),
apply map_injective (algebra_map R K) (is_fraction_ring.injective _ _),
rw [polynomial.map_mul, polynomial.map_mul, hs, hr, mul_assoc, mul_comm r],
simp },
rw [← hp.dvd_prim_part_iff_dvd, prim_part_mul, hq.prim_part_eq,
associated.dvd_iff_dvd_right] at h,
{ exact h },
{ symmetry,
rcases is_unit_prim_part_C s with ⟨u, hu⟩,
use u,
rw hu },
iterate 2 { apply mul_ne_zero hq.ne_zero,
rw [ne.def, C_eq_zero],
contrapose! s0,
simp [s0, mem_non_zero_divisors_iff_ne_zero] }
end
variables (K)
lemma is_primitive.dvd_iff_fraction_map_dvd_fraction_map {p q : R[X]}
(hp : p.is_primitive) (hq : q.is_primitive) :
(p ∣ q) ↔ (p.map (algebra_map R K) ∣ q.map (algebra_map R K)) :=
⟨λ ⟨a,b⟩, ⟨a.map (algebra_map R K), b.symm ▸ polynomial.map_mul (algebra_map R K)⟩,
λ h, hp.dvd_of_fraction_map_dvd_fraction_map hq h⟩
end fraction_map
/-- **Gauss's Lemma** for `ℤ` states that a primitive integer polynomial is irreducible iff it is
irreducible over `ℚ`. -/
theorem is_primitive.int.irreducible_iff_irreducible_map_cast
{p : ℤ[X]} (hp : p.is_primitive) :
irreducible p ↔ irreducible (p.map (int.cast_ring_hom ℚ)) :=
hp.irreducible_iff_irreducible_map_fraction_map
lemma is_primitive.int.dvd_iff_map_cast_dvd_map_cast (p q : ℤ[X])
(hp : p.is_primitive) (hq : q.is_primitive) :
(p ∣ q) ↔ (p.map (int.cast_ring_hom ℚ) ∣ q.map (int.cast_ring_hom ℚ)) :=
hp.dvd_iff_fraction_map_dvd_fraction_map ℚ hq
end normalized_gcd_monoid
end polynomial
|
bb5c0c21ecfaae5619b15f15055795a8fa5ae829 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/fin.lean | 252834abbb256cb03970ff6eb5ef9ed92a3a94f4 | [
"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 | 68,518 | lean | /-
Copyright (c) 2017 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis, Keeley Hoek
-/
import data.nat.cast
import data.int.basic
import tactic.localized
import tactic.apply_fun
import order.rel_iso
/-!
# The finite type with `n` elements
`fin n` is the type whose elements are natural numbers smaller than `n`.
This file expands on the development in the core library.
## Main definitions
### Induction principles
* `fin_zero_elim` : Elimination principle for the empty set `fin 0`, generalizes `fin.elim0`.
* `fin.succ_rec` : Define `C n i` by induction on `i : fin n` interpreted
as `(0 : fin (n - i)).succ.succ…`. This function has two arguments: `H0 n` defines
`0`-th element `C (n+1) 0` of an `(n+1)`-tuple, and `Hs n i` defines `(i+1)`-st element
of `(n+1)`-tuple based on `n`, `i`, and `i`-th element of `n`-tuple.
* `fin.succ_rec_on` : same as `fin.succ_rec` but `i : fin n` is the first argument;
* `fin.induction` : Define `C i` by induction on `i : fin (n + 1)`, separating into the
`nat`-like base cases of `C 0` and `C (i.succ)`.
* `fin.induction_on` : same as `fin.induction` but with `i : fin (n + 1)` as the first argument.
### Casts
* `cast_lt i h` : embed `i` into a `fin` where `h` proves it belongs into;
* `cast_le h` : embed `fin n` into `fin m`, `h : n ≤ m`;
* `cast eq` : embed `fin n` into `fin m`, `eq : n = m`;
* `cast_add m` : embed `fin n` into `fin (n+m)`;
* `cast_succ` : embed `fin n` into `fin (n+1)`;
* `succ_above p` : embed `fin n` into `fin (n + 1)` with a hole around `p`;
* `pred_above (p : fin n) i` : embed `i : fin (n+1)` into `fin n` by subtracting one if `p < i`;
* `cast_pred` : embed `fin (n + 2)` into `fin (n + 1)` by mapping `last (n + 1)` to `last n`;
* `sub_nat i h` : subtract `m` from `i ≥ m`, generalizes `fin.pred`;
* `add_nat m i` : add `m` on `i` on the right, generalizes `fin.succ`;
* `nat_add n i` adds `n` on `i` on the left;
* `clamp n m` : `min n m` as an element of `fin (m + 1)`;
### Operation on tuples
We interpret maps `Π i : fin n, α i` as tuples `(α 0, …, α (n-1))`.
If `α i` is a constant map, then tuples are isomorphic (but not definitionally equal)
to `vector`s.
We define the following operations:
* `tail` : the tail of an `n+1` tuple, i.e., its last `n` entries;
* `cons` : adding an element at the beginning of an `n`-tuple, to get an `n+1`-tuple;
* `init` : the beginning of an `n+1` tuple, i.e., its first `n` entries;
* `snoc` : adding an element at the end of an `n`-tuple, to get an `n+1`-tuple. The name `snoc`
comes from `cons` (i.e., adding an element to the left of a tuple) read in reverse order.
* `insert_nth` : insert an element to a tuple at a given position.
* `find p` : returns the first index `n` where `p n` is satisfied, and `none` if it is never
satisfied.
### Misc definitions
* `fin.last n` : The greatest value of `fin (n+1)`.
-/
universes u v
open fin nat function
/-- Elimination principle for the empty set `fin 0`, dependent version. -/
def fin_zero_elim {α : fin 0 → Sort u} (x : fin 0) : α x := x.elim0
lemma fact.succ.pos {n} : fact (0 < succ n) := ⟨zero_lt_succ _⟩
lemma fact.bit0.pos {n} [h : fact (0 < n)] : fact (0 < bit0 n) :=
⟨nat.zero_lt_bit0 $ ne_of_gt h.1⟩
lemma fact.bit1.pos {n} : fact (0 < bit1 n) :=
⟨nat.zero_lt_bit1 _⟩
lemma fact.pow.pos {p n : ℕ} [h : fact $ 0 < p] : fact (0 < p ^ n) :=
⟨pow_pos h.1 _⟩
localized "attribute [instance] fact.succ.pos" in fin_fact
localized "attribute [instance] fact.bit0.pos" in fin_fact
localized "attribute [instance] fact.bit1.pos" in fin_fact
localized "attribute [instance] fact.pow.pos" in fin_fact
namespace fin
variables {n m : ℕ} {a b : fin n}
instance fin_to_nat (n : ℕ) : has_coe (fin n) nat := ⟨subtype.val⟩
section coe
/-!
### coercions and constructions
-/
@[simp] protected lemma eta (a : fin n) (h : (a : ℕ) < n) : (⟨(a : ℕ), h⟩ : fin n) = a :=
by cases a; refl
@[ext]
lemma ext {a b : fin n} (h : (a : ℕ) = b) : a = b := eq_of_veq h
lemma ext_iff (a b : fin n) : a = b ↔ (a : ℕ) = b :=
iff.intro (congr_arg _) fin.eq_of_veq
lemma coe_injective {n : ℕ} : injective (coe : fin n → ℕ) := subtype.coe_injective
lemma eq_iff_veq (a b : fin n) : a = b ↔ a.1 = b.1 :=
⟨veq_of_eq, eq_of_veq⟩
lemma ne_iff_vne (a b : fin n) : a ≠ b ↔ a.1 ≠ b.1 :=
⟨vne_of_ne, ne_of_vne⟩
@[simp] lemma mk_eq_subtype_mk (a : ℕ) (h : a < n) : mk a h = ⟨a, h⟩ := rfl
protected lemma mk.inj_iff {n a b : ℕ} {ha : a < n} {hb : b < n} :
(⟨a, ha⟩ : fin n) = ⟨b, hb⟩ ↔ a = b :=
subtype.mk_eq_mk
lemma mk_val {m n : ℕ} (h : m < n) : (⟨m, h⟩ : fin n).val = m := rfl
lemma eq_mk_iff_coe_eq {k : ℕ} {hk : k < n} : a = ⟨k, hk⟩ ↔ (a : ℕ) = k :=
fin.eq_iff_veq a ⟨k, hk⟩
@[simp, norm_cast] lemma coe_mk {m n : ℕ} (h : m < n) : ((⟨m, h⟩ : fin n) : ℕ) = m := rfl
lemma mk_coe (i : fin n) : (⟨i, i.property⟩ : fin n) = i :=
fin.eta _ _
lemma coe_eq_val (a : fin n) : (a : ℕ) = a.val := rfl
@[simp] lemma val_eq_coe (a : fin n) : a.val = a := rfl
/-- Assume `k = l`. If two functions defined on `fin k` and `fin l` are equal on each element,
then they coincide (in the heq sense). -/
protected lemma heq_fun_iff {α : Sort*} {k l : ℕ} (h : k = l) {f : fin k → α} {g : fin l → α} :
f == g ↔ (∀ (i : fin k), f i = g ⟨(i : ℕ), h ▸ i.2⟩) :=
by { induction h, simp [heq_iff_eq, function.funext_iff] }
protected lemma heq_ext_iff {k l : ℕ} (h : k = l) {i : fin k} {j : fin l} :
i == j ↔ (i : ℕ) = (j : ℕ) :=
by { induction h, simp [ext_iff] }
lemma exists_iff {p : fin n → Prop} : (∃ i, p i) ↔ ∃ i h, p ⟨i, h⟩ :=
⟨λ h, exists.elim h (λ ⟨i, hi⟩ hpi, ⟨i, hi, hpi⟩),
λ h, exists.elim h (λ i hi, ⟨⟨i, hi.fst⟩, hi.snd⟩)⟩
lemma forall_iff {p : fin n → Prop} : (∀ i, p i) ↔ ∀ i h, p ⟨i, h⟩ :=
⟨λ h i hi, h ⟨i, hi⟩, λ h ⟨i, hi⟩, h i hi⟩
end coe
section order
/-!
### order
-/
lemma is_lt (i : fin n) : (i : ℕ) < n := i.2
lemma is_le (i : fin (n + 1)) : (i : ℕ) ≤ n := le_of_lt_succ i.is_lt
lemma lt_iff_coe_lt_coe : a < b ↔ (a : ℕ) < b := iff.rfl
lemma le_iff_coe_le_coe : a ≤ b ↔ (a : ℕ) ≤ b := iff.rfl
lemma mk_lt_of_lt_coe {a : ℕ} (h : a < b) : (⟨a, h.trans b.is_lt⟩ : fin n) < b := h
lemma mk_le_of_le_coe {a : ℕ} (h : a ≤ b) : (⟨a, h.trans_lt b.is_lt⟩ : fin n) ≤ b := h
/-- `a < b` as natural numbers if and only if `a < b` in `fin n`. -/
@[norm_cast, simp] lemma coe_fin_lt {n : ℕ} {a b : fin n} : (a : ℕ) < (b : ℕ) ↔ a < b :=
iff.rfl
/-- `a ≤ b` as natural numbers if and only if `a ≤ b` in `fin n`. -/
@[norm_cast, simp] lemma coe_fin_le {n : ℕ} {a b : fin n} : (a : ℕ) ≤ (b : ℕ) ↔ a ≤ b :=
iff.rfl
instance {n : ℕ} : linear_order (fin n) :=
{ le := (≤), lt := (<),
decidable_le := fin.decidable_le,
decidable_lt := fin.decidable_lt,
decidable_eq := fin.decidable_eq _,
..linear_order.lift (coe : fin n → ℕ) (@fin.eq_of_veq _) }
/-- The inclusion map `fin n → ℕ` is a relation embedding. -/
def coe_embedding (n) : (fin n) ↪o ℕ :=
⟨⟨coe, @fin.eq_of_veq _⟩, λ a b, iff.rfl⟩
/-- The ordering on `fin n` is a well order. -/
instance fin.lt.is_well_order (n) : is_well_order (fin n) (<) :=
(coe_embedding n).is_well_order
/-- Use the ordering on `fin n` for checking recursive definitions.
For example, the following definition is not accepted by the termination checker,
unless we declare the `has_well_founded` instance:
```lean
def factorial {n : ℕ} : fin n → ℕ
| ⟨0, _⟩ := 1
| ⟨i + 1, hi⟩ := (i + 1) * factorial ⟨i, i.lt_succ_self.trans hi⟩
```
-/
instance {n : ℕ} : has_well_founded (fin n) :=
⟨_, measure_wf coe⟩
@[simp] lemma coe_zero {n : ℕ} : ((0 : fin (n+1)) : ℕ) = 0 := rfl
attribute [simp] val_zero
@[simp] lemma val_zero' (n) : (0 : fin (n+1)).val = 0 := rfl
@[simp] lemma mk_zero : (⟨0, nat.succ_pos'⟩ : fin (n + 1)) = (0 : fin _) := rfl
lemma zero_le (a : fin (n + 1)) : 0 ≤ a := zero_le a.1
lemma pos_iff_ne_zero (a : fin (n+1)) : 0 < a ↔ a ≠ 0 :=
begin
split,
{ rintros h rfl, exact lt_irrefl _ h, },
{ rintros h,
apply (@pos_iff_ne_zero _ _ (a : ℕ)).mpr,
cases a,
rintro w,
apply h,
simp at w,
subst w,
refl, },
end
/-- The greatest value of `fin (n+1)` -/
def last (n : ℕ) : fin (n+1) := ⟨_, n.lt_succ_self⟩
@[simp, norm_cast] lemma coe_last (n : ℕ) : (last n : ℕ) = n := rfl
lemma last_val (n : ℕ) : (last n).val = n := rfl
theorem le_last (i : fin (n+1)) : i ≤ last n :=
le_of_lt_succ i.is_lt
instance : bounded_lattice (fin (n + 1)) :=
{ top := last n,
le_top := le_last,
bot := 0,
bot_le := zero_le,
.. fin.linear_order, .. lattice_of_linear_order }
lemma last_pos : (0 : fin (n + 2)) < last (n + 1) :=
by simp [lt_iff_coe_lt_coe]
lemma eq_last_of_not_lt {i : fin (n+1)} (h : ¬ (i : ℕ) < n) : i = last n :=
le_antisymm (le_last i) (not_lt.1 h)
section
variables {α : Type*} [preorder α]
open set
/-- If `e` is an `order_iso` between `fin n` and `fin m`, then `n = m` and `e` is the identity
map. In this lemma we state that for each `i : fin n` we have `(e i : ℕ) = (i : ℕ)`. -/
@[simp] lemma coe_order_iso_apply (e : fin n ≃o fin m) (i : fin n) : (e i : ℕ) = i :=
begin
rcases i with ⟨i, hi⟩,
rw [subtype.coe_mk],
induction i using nat.strong_induction_on with i h,
refine le_antisymm (forall_lt_iff_le.1 $ λ j hj, _) (forall_lt_iff_le.1 $ λ j hj, _),
{ have := e.symm.lt_iff_lt.2 (mk_lt_of_lt_coe hj),
rw e.symm_apply_apply at this,
convert this,
simpa using h _ this (e.symm _).is_lt },
{ rwa [← h j hj (hj.trans hi), ← lt_iff_coe_lt_coe, e.lt_iff_lt] }
end
instance order_iso_subsingleton : subsingleton (fin n ≃o α) :=
⟨λ e e', by { ext i,
rw [← e.symm.apply_eq_iff_eq, e.symm_apply_apply, ← e'.trans_apply, ext_iff,
coe_order_iso_apply] }⟩
instance order_iso_subsingleton' : subsingleton (α ≃o fin n) :=
order_iso.symm_injective.subsingleton
instance order_iso_unique : unique (fin n ≃o fin n) := unique.mk' _
/-- Two strictly monotone functions from `fin n` are equal provided that their ranges
are equal. -/
lemma strict_mono_unique {f g : fin n → α} (hf : strict_mono f) (hg : strict_mono g)
(h : range f = range g) : f = g :=
have (hf.order_iso f).trans (order_iso.set_congr _ _ h) = hg.order_iso g,
from subsingleton.elim _ _,
congr_arg (function.comp (coe : range g → α)) (funext $ rel_iso.ext_iff.1 this)
/-- Two order embeddings of `fin n` are equal provided that their ranges are equal. -/
lemma order_embedding_eq {f g : fin n ↪o α} (h : range f = range g) : f = g :=
rel_embedding.ext $ funext_iff.1 $ strict_mono_unique f.strict_mono g.strict_mono h
end
/-- A function `f` on `fin n` is strictly monotone if and only if `f i < f (i+1)` for all `i`. -/
lemma strict_mono_iff_lt_succ {α : Type*} [preorder α] {f : fin n → α} :
strict_mono f ↔ ∀ i (h : i + 1 < n), f ⟨i, lt_of_le_of_lt (nat.le_succ i) h⟩ < f ⟨i+1, h⟩ :=
begin
split,
{ assume H i hi,
apply H,
exact nat.lt_succ_self _ },
{ assume H,
have A : ∀ i j (h : i < j) (h' : j < n), f ⟨i, lt_trans h h'⟩ < f ⟨j, h'⟩,
{ assume i j h h',
induction h with k h IH,
{ exact H _ _ },
{ exact lt_trans (IH (nat.lt_of_succ_lt h')) (H _ _) } },
assume i j hij,
convert A (i : ℕ) (j : ℕ) hij j.2; ext; simp only [subtype.coe_eta] }
end
end order
section add
/-!
### addition, numerals, and coercion from nat
-/
/-- convert a `ℕ` to `fin n`, provided `n` is positive -/
def of_nat' [h : fact (0 < n)] (i : ℕ) : fin n := ⟨i%n, mod_lt _ h.1⟩
lemma one_val {n : ℕ} : (1 : fin (n+1)).val = 1 % (n+1) := rfl
lemma coe_one' {n : ℕ} : ((1 : fin (n+1)) : ℕ) = 1 % (n+1) := rfl
@[simp] lemma val_one {n : ℕ} : (1 : fin (n+2)).val = 1 := rfl
@[simp] lemma coe_one {n : ℕ} : ((1 : fin (n+2)) : ℕ) = 1 := rfl
@[simp] lemma mk_one : (⟨1, nat.succ_lt_succ (nat.succ_pos n)⟩ : fin (n + 2)) = (1 : fin _) := rfl
instance {n : ℕ} : nontrivial (fin (n + 2)) := ⟨⟨0, 1, dec_trivial⟩⟩
section monoid
@[simp] protected lemma add_zero (k : fin (n + 1)) : k + 0 = k :=
by simp [eq_iff_veq, add_def, mod_eq_of_lt (is_lt k)]
@[simp] protected lemma zero_add (k : fin (n + 1)) : (0 : fin (n + 1)) + k = k :=
by simp [eq_iff_veq, add_def, mod_eq_of_lt (is_lt k)]
instance add_comm_monoid (n : ℕ) : add_comm_monoid (fin (n + 1)) :=
{ add := (+),
add_assoc := by simp [eq_iff_veq, add_def, add_assoc],
zero := 0,
zero_add := fin.zero_add,
add_zero := fin.add_zero,
add_comm := by simp [eq_iff_veq, add_def, add_comm] }
end monoid
lemma val_add {n : ℕ} : ∀ a b : fin n, (a + b).val = (a.val + b.val) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_add {n : ℕ} : ∀ a b : fin n, ((a + b : fin n) : ℕ) = (a + b) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_add_eq_ite {n : ℕ} (a b : fin n) :
(↑(a + b) : ℕ) = if n ≤ a + b then a + b - n else a + b :=
by rw [fin.coe_add, nat.add_mod_eq_ite,
nat.mod_eq_of_lt (show ↑a < n, from a.2), nat.mod_eq_of_lt (show ↑b < n, from b.2)]
lemma coe_bit0 {n : ℕ} (k : fin n) : ((bit0 k : fin n) : ℕ) = bit0 (k : ℕ) % n :=
by { cases k, refl }
lemma coe_bit1 {n : ℕ} (k : fin (n + 1)) :
((bit1 k : fin (n + 1)) : ℕ) = bit1 (k : ℕ) % (n + 1) :=
begin
cases n, { cases k with k h, cases k, {show _ % _ = _, simp}, cases h with _ h, cases h },
simp [bit1, fin.coe_bit0, fin.coe_add, fin.coe_one],
end
lemma coe_add_one_of_lt {n : ℕ} {i : fin n.succ} (h : i < last _) :
(↑(i + 1) : ℕ) = i + 1 :=
begin
-- First show that `((1 : fin n.succ) : ℕ) = 1`, because `n.succ` is at least 2.
cases n,
{ cases h },
-- Then just unfold the definitions.
rw [fin.coe_add, fin.coe_one, nat.mod_eq_of_lt (nat.succ_lt_succ _)],
exact h
end
@[simp] lemma last_add_one : ∀ n, last n + 1 = 0
| 0 := subsingleton.elim _ _
| (n + 1) := by { ext, rw [coe_add, coe_zero, coe_last, coe_one, nat.mod_self] }
lemma coe_add_one {n : ℕ} (i : fin (n + 1)) :
((i + 1 : fin (n + 1)) : ℕ) = if i = last _ then 0 else i + 1 :=
begin
rcases (le_last i).eq_or_lt with rfl|h,
{ simp },
{ simpa [h.ne] using coe_add_one_of_lt h }
end
section bit
@[simp] lemma mk_bit0 {m n : ℕ} (h : bit0 m < n) :
(⟨bit0 m, h⟩ : fin n) = (bit0 ⟨m, (nat.le_add_right m m).trans_lt h⟩ : fin _) :=
eq_of_veq (nat.mod_eq_of_lt h).symm
@[simp] lemma mk_bit1 {m n : ℕ} (h : bit1 m < n + 1) :
(⟨bit1 m, h⟩ : fin (n + 1)) = (bit1 ⟨m, (nat.le_add_right m m).trans_lt
((m + m).lt_succ_self.trans h)⟩ : fin _) :=
begin
ext,
simp only [bit1, bit0] at h,
simp only [bit1, bit0, coe_add, coe_one', coe_mk, ←nat.add_mod, nat.mod_eq_of_lt h],
end
end bit
@[simp] lemma val_two {n : ℕ} : (2 : fin (n+3)).val = 2 := rfl
@[simp] lemma coe_two {n : ℕ} : ((2 : fin (n+3)) : ℕ) = 2 := rfl
section of_nat_coe
@[simp]
lemma of_nat_eq_coe (n : ℕ) (a : ℕ) : (of_nat a : fin (n+1)) = a :=
begin
induction a with a ih, { refl },
ext, show (a+1) % (n+1) = subtype.val (a+1 : fin (n+1)),
{ rw [val_add, ← ih, of_nat],
exact add_mod _ _ _ }
end
/-- Converting an in-range number to `fin (n + 1)` produces a result
whose value is the original number. -/
lemma coe_val_of_lt {n : ℕ} {a : ℕ} (h : a < n + 1) :
((a : fin (n + 1)).val) = a :=
begin
rw ←of_nat_eq_coe,
exact nat.mod_eq_of_lt h
end
/-- Converting the value of a `fin (n + 1)` to `fin (n + 1)` results
in the same value. -/
lemma coe_val_eq_self {n : ℕ} (a : fin (n + 1)) : (a.val : fin (n + 1)) = a :=
begin
rw fin.eq_iff_veq,
exact coe_val_of_lt a.property
end
/-- Coercing an in-range number to `fin (n + 1)`, and converting back
to `ℕ`, results in that number. -/
lemma coe_coe_of_lt {n : ℕ} {a : ℕ} (h : a < n + 1) :
((a : fin (n + 1)) : ℕ) = a :=
coe_val_of_lt h
/-- Converting a `fin (n + 1)` to `ℕ` and back results in the same
value. -/
@[simp] lemma coe_coe_eq_self {n : ℕ} (a : fin (n + 1)) : ((a : ℕ) : fin (n + 1)) = a :=
coe_val_eq_self a
lemma coe_nat_eq_last (n) : (n : fin (n + 1)) = fin.last n :=
by { rw [←fin.of_nat_eq_coe, fin.of_nat, fin.last], simp only [nat.mod_eq_of_lt n.lt_succ_self] }
lemma le_coe_last (i : fin (n + 1)) : i ≤ n :=
by { rw fin.coe_nat_eq_last, exact fin.le_last i }
end of_nat_coe
lemma add_one_pos (i : fin (n + 1)) (h : i < fin.last n) : (0 : fin (n + 1)) < i + 1 :=
begin
cases n,
{ exact absurd h (nat.not_lt_zero _) },
{ rw [lt_iff_coe_lt_coe, coe_last, ←add_lt_add_iff_right 1] at h,
rw [lt_iff_coe_lt_coe, coe_add, coe_zero, coe_one, nat.mod_eq_of_lt h],
exact nat.zero_lt_succ _ }
end
lemma one_pos : (0 : fin (n + 2)) < 1 := succ_pos 0
lemma zero_ne_one : (0 : fin (n + 2)) ≠ 1 := ne_of_lt one_pos
@[simp] lemma zero_eq_one_iff : (0 : fin (n + 1)) = 1 ↔ n = 0 :=
begin
split,
{ cases n; intro h,
{ refl },
{ have := zero_ne_one, contradiction } },
{ rintro rfl, refl }
end
@[simp] lemma one_eq_zero_iff : (1 : fin (n + 1)) = 0 ↔ n = 0 :=
by rw [eq_comm, zero_eq_one_iff]
end add
section succ
/-!
### succ and casts into larger fin types
-/
@[simp] lemma coe_succ (j : fin n) : (j.succ : ℕ) = j + 1 :=
by cases j; simp [fin.succ]
lemma succ_pos (a : fin n) : (0 : fin (n + 1)) < a.succ := by simp [lt_iff_coe_lt_coe]
/-- `fin.succ` as an `order_embedding` -/
def succ_embedding (n : ℕ) : fin n ↪o fin (n + 1) :=
order_embedding.of_strict_mono fin.succ $ λ ⟨i, hi⟩ ⟨j, hj⟩ h, succ_lt_succ h
@[simp] lemma coe_succ_embedding : ⇑(succ_embedding n) = fin.succ := rfl
@[simp] lemma succ_le_succ_iff : a.succ ≤ b.succ ↔ a ≤ b :=
(succ_embedding n).le_iff_le
@[simp] lemma succ_lt_succ_iff : a.succ < b.succ ↔ a < b :=
(succ_embedding n).lt_iff_lt
lemma succ_injective (n : ℕ) : injective (@fin.succ n) :=
(succ_embedding n).injective
@[simp] lemma succ_inj {a b : fin n} : a.succ = b.succ ↔ a = b :=
(succ_injective n).eq_iff
lemma succ_ne_zero {n} : ∀ k : fin n, fin.succ k ≠ 0
| ⟨k, hk⟩ heq := nat.succ_ne_zero k $ (ext_iff _ _).1 heq
@[simp] lemma succ_zero_eq_one : fin.succ (0 : fin (n + 1)) = 1 := rfl
@[simp] lemma succ_one_eq_two : fin.succ (1 : fin (n + 2)) = 2 := rfl
@[simp] lemma succ_mk (n i : ℕ) (h : i < n) : fin.succ ⟨i, h⟩ = ⟨i + 1, nat.succ_lt_succ h⟩ :=
rfl
lemma mk_succ_pos (i : ℕ) (h : i < n) : (0 : fin (n + 1)) < ⟨i.succ, add_lt_add_right h 1⟩ :=
by { rw [lt_iff_coe_lt_coe, coe_zero], exact nat.succ_pos i }
lemma one_lt_succ_succ (a : fin n) : (1 : fin (n + 2)) < a.succ.succ :=
begin
cases n,
{ exact fin_zero_elim a },
{ rw [←succ_zero_eq_one, succ_lt_succ_iff], exact succ_pos a }
end
lemma succ_succ_ne_one (a : fin n) : fin.succ (fin.succ a) ≠ 1 := ne_of_gt (one_lt_succ_succ a)
/-- `cast_lt i h` embeds `i` into a `fin` where `h` proves it belongs into. -/
def cast_lt (i : fin m) (h : i.1 < n) : fin n := ⟨i.1, h⟩
@[simp] lemma coe_cast_lt (i : fin m) (h : i.1 < n) : (cast_lt i h : ℕ) = i := rfl
@[simp] lemma cast_lt_mk (i n m : ℕ) (hn : i < n) (hm : i < m) : cast_lt ⟨i, hn⟩ hm = ⟨i, hm⟩ := rfl
/-- `cast_le h i` embeds `i` into a larger `fin` type. -/
def cast_le (h : n ≤ m) : fin n ↪o fin m :=
order_embedding.of_strict_mono (λ a, cast_lt a (lt_of_lt_of_le a.2 h)) $ λ a b h, h
@[simp] lemma coe_cast_le (h : n ≤ m) (i : fin n) : (cast_le h i : ℕ) = i := rfl
@[simp] lemma cast_le_mk (i n m : ℕ) (hn : i < n) (h : n ≤ m) :
cast_le h ⟨i, hn⟩ = ⟨i, lt_of_lt_of_le hn h⟩ := rfl
@[simp] lemma cast_le_zero {n m : ℕ} (h : n.succ ≤ m.succ) :
cast_le h 0 = 0 :=
by simp [eq_iff_veq]
@[simp] lemma range_cast_le {n k : ℕ} (h : n ≤ k) :
set.range (cast_le h) = {i | (i : ℕ) < n} :=
set.ext (λ x, ⟨λ ⟨y, hy⟩, hy ▸ y.2, λ hx, ⟨⟨x, hx⟩, fin.ext rfl⟩⟩)
@[simp] lemma coe_of_injective_cast_le_symm {n k : ℕ} (h : n ≤ k) (i : fin k) (hi) :
((equiv.of_injective _ (cast_le h).injective).symm ⟨i, hi⟩ : ℕ) = i :=
begin
rw ← coe_cast_le,
exact congr_arg coe (equiv.apply_of_injective_symm _ _ _)
end
/-- `cast eq i` embeds `i` into a equal `fin` type. -/
def cast (eq : n = m) : fin n ≃o fin m :=
{ to_equiv := ⟨cast_le eq.le, cast_le eq.symm.le, λ a, eq_of_veq rfl, λ a, eq_of_veq rfl⟩,
map_rel_iff' := λ a b, iff.rfl }
@[simp] lemma symm_cast (h : n = m) : (cast h).symm = cast h.symm := rfl
lemma coe_cast (h : n = m) (i : fin n) : (cast h i : ℕ) = i := rfl
@[simp] lemma cast_mk (h : n = m) (i : ℕ) (hn : i < n) :
cast h ⟨i, hn⟩ = ⟨i, lt_of_lt_of_le hn h.le⟩ := rfl
@[simp] lemma cast_trans {k : ℕ} (h : n = m) (h' : m = k) {i : fin n} :
cast h' (cast h i) = cast (eq.trans h h') i := rfl
@[simp] lemma cast_refl (h : n = n := rfl) : cast h = order_iso.refl (fin n) :=
by { ext, refl }
/-- While in many cases `fin.cast` is better than `equiv.cast`/`cast`, sometimes we want to apply
a generic theorem about `cast`. -/
lemma cast_to_equiv (h : n = m) : (cast h).to_equiv = equiv.cast (h ▸ rfl) :=
by { subst h, simp }
/-- While in many cases `fin.cast` is better than `equiv.cast`/`cast`, sometimes we want to apply
a generic theorem about `cast`. -/
lemma cast_eq_cast (h : n = m) : (cast h : fin n → fin m) = _root_.cast (h ▸ rfl) :=
by { subst h, ext, simp }
/-- `cast_add m i` embeds `i : fin n` in `fin (n+m)`. -/
def cast_add (m) : fin n ↪o fin (n + m) := cast_le $ le_add_right n m
@[simp] lemma coe_cast_add (m : ℕ) (i : fin n) : (cast_add m i : ℕ) = i := rfl
@[simp] lemma cast_add_mk (m : ℕ) (i : ℕ) (h : i < n) :
cast_add m ⟨i, h⟩ = ⟨i, lt_add_right i n m h⟩ := rfl
/-- `cast_succ i` embeds `i : fin n` in `fin (n+1)`. -/
def cast_succ : fin n ↪o fin (n + 1) := cast_add 1
@[simp] lemma coe_cast_succ (i : fin n) : (i.cast_succ : ℕ) = i := rfl
@[simp] lemma cast_succ_mk (n i : ℕ) (h : i < n) : cast_succ ⟨i, h⟩ = ⟨i, nat.lt.step h⟩ := rfl
lemma cast_succ_lt_succ (i : fin n) : i.cast_succ < i.succ :=
lt_iff_coe_lt_coe.2 $ by simp only [coe_cast_succ, coe_succ, nat.lt_succ_self]
lemma le_cast_succ_iff {i : fin (n + 1)} {j : fin n} : i ≤ j.cast_succ ↔ i < j.succ :=
by simpa [lt_iff_coe_lt_coe, le_iff_coe_le_coe] using nat.succ_le_succ_iff.symm
@[simp] lemma succ_last (n : ℕ) : (last n).succ = last (n.succ) := rfl
@[simp] lemma succ_eq_last_succ {n : ℕ} (i : fin n.succ) :
i.succ = last (n + 1) ↔ i = last n :=
by rw [← succ_last, (succ_injective _).eq_iff]
@[simp] lemma cast_succ_cast_lt (i : fin (n + 1)) (h : (i : ℕ) < n) : cast_succ (cast_lt i h) = i :=
fin.eq_of_veq rfl
@[simp] lemma cast_lt_cast_succ {n : ℕ} (a : fin n) (h : (a : ℕ) < n) :
cast_lt (cast_succ a) h = a :=
by cases a; refl
@[simp] lemma cast_succ_lt_cast_succ_iff : a.cast_succ < b.cast_succ ↔ a < b :=
(@cast_succ n).lt_iff_lt
lemma cast_succ_injective (n : ℕ) : injective (@fin.cast_succ n) :=
(cast_succ : fin n ↪o _).injective
lemma cast_succ_inj {a b : fin n} : a.cast_succ = b.cast_succ ↔ a = b :=
(cast_succ_injective n).eq_iff
lemma cast_succ_lt_last (a : fin n) : cast_succ a < last n := lt_iff_coe_lt_coe.mpr a.is_lt
@[simp] lemma cast_succ_zero : cast_succ (0 : fin (n + 1)) = 0 := rfl
@[simp] lemma cast_succ_one {n : ℕ} : fin.cast_succ (1 : fin (n + 2)) = 1 := rfl
/-- `cast_succ i` is positive when `i` is positive -/
lemma cast_succ_pos {i : fin (n + 1)} (h : 0 < i) : 0 < cast_succ i :=
by simpa [lt_iff_coe_lt_coe] using h
@[simp] lemma cast_succ_eq_zero_iff (a : fin (n + 1)) : a.cast_succ = 0 ↔ a = 0 :=
subtype.ext_iff.trans $ (subtype.ext_iff.trans $ by exact iff.rfl).symm
lemma cast_succ_ne_zero_iff (a : fin (n + 1)) : a.cast_succ ≠ 0 ↔ a ≠ 0 :=
not_iff_not.mpr $ cast_succ_eq_zero_iff a
lemma cast_succ_fin_succ (n : ℕ) (j : fin n) :
cast_succ (fin.succ j) = fin.succ (cast_succ j) :=
by simp [fin.ext_iff]
@[norm_cast, simp] lemma coe_eq_cast_succ : (a : fin (n + 1)) = a.cast_succ :=
begin
ext,
exact coe_val_of_lt (nat.lt.step a.is_lt),
end
@[simp] lemma coe_succ_eq_succ : a.cast_succ + 1 = a.succ :=
begin
cases n,
{ exact fin_zero_elim a },
{ simp [a.is_lt, eq_iff_veq, add_def, nat.mod_eq_of_lt] }
end
lemma lt_succ : a.cast_succ < a.succ :=
by { rw [cast_succ, lt_iff_coe_lt_coe, coe_cast_add, coe_succ], exact lt_add_one a.val }
@[simp] lemma range_cast_succ {n : ℕ} :
set.range (cast_succ : fin n → fin n.succ) = {i | (i : ℕ) < n} :=
range_cast_le _
@[simp] lemma coe_of_injective_cast_succ_symm {n : ℕ} (i : fin n.succ) (hi) :
((equiv.of_injective cast_succ (cast_succ_injective _)).symm ⟨i, hi⟩ : ℕ) = i :=
begin
rw ← coe_cast_succ,
exact congr_arg coe (equiv.apply_of_injective_symm _ _ _)
end
/-- `add_nat m i` adds `m` to `i`, generalizes `fin.succ`. -/
def add_nat (m) : fin n ↪o fin (n + m) :=
order_embedding.of_strict_mono (λ i, ⟨(i : ℕ) + m, add_lt_add_right i.2 _⟩) $
λ i j h, lt_iff_coe_lt_coe.2 $ add_lt_add_right h _
@[simp] lemma coe_add_nat (m : ℕ) (i : fin n) : (add_nat m i : ℕ) = i + m := rfl
/-- `nat_add n i` adds `n` to `i` "on the left". -/
def nat_add (n) {m} : fin m ↪o fin (n + m) :=
order_embedding.of_strict_mono (λ i, ⟨n + (i : ℕ), add_lt_add_left i.2 _⟩) $
λ i j h, lt_iff_coe_lt_coe.2 $ add_lt_add_left h _
@[simp] lemma coe_nat_add (n : ℕ) {m : ℕ} (i : fin m) : (nat_add n i : ℕ) = n + i := rfl
lemma nat_add_zero {n : ℕ} : fin.nat_add 0 = (fin.cast (zero_add n).symm).to_rel_embedding :=
by { ext, apply zero_add }
end succ
section rec
/-!
### recursion and induction principles
-/
/-- Define `C n i` by induction on `i : fin n` interpreted as `(0 : fin (n - i)).succ.succ…`.
This function has two arguments: `H0 n` defines `0`-th element `C (n+1) 0` of an `(n+1)`-tuple,
and `Hs n i` defines `(i+1)`-st element of `(n+1)`-tuple based on `n`, `i`, and `i`-th element
of `n`-tuple. -/
@[elab_as_eliminator] def succ_rec
{C : Π n, fin n → Sort*}
(H0 : Π n, C (succ n) 0)
(Hs : Π n i, C n i → C (succ n) i.succ) : Π {n : ℕ} (i : fin n), C n i
| 0 i := i.elim0
| (succ n) ⟨0, _⟩ := H0 _
| (succ n) ⟨succ i, h⟩ := Hs _ _ (succ_rec ⟨i, lt_of_succ_lt_succ h⟩)
/-- Define `C n i` by induction on `i : fin n` interpreted as `(0 : fin (n - i)).succ.succ…`.
This function has two arguments: `H0 n` defines `0`-th element `C (n+1) 0` of an `(n+1)`-tuple,
and `Hs n i` defines `(i+1)`-st element of `(n+1)`-tuple based on `n`, `i`, and `i`-th element
of `n`-tuple.
A version of `fin.succ_rec` taking `i : fin n` as the first argument. -/
@[elab_as_eliminator] def succ_rec_on {n : ℕ} (i : fin n)
{C : Π n, fin n → Sort*}
(H0 : Π n, C (succ n) 0)
(Hs : Π n i, C n i → C (succ n) i.succ) : C n i :=
i.succ_rec H0 Hs
@[simp] theorem succ_rec_on_zero {C : ∀ n, fin n → Sort*} {H0 Hs} (n) :
@fin.succ_rec_on (succ n) 0 C H0 Hs = H0 n :=
rfl
@[simp] theorem succ_rec_on_succ {C : ∀ n, fin n → Sort*} {H0 Hs} {n} (i : fin n) :
@fin.succ_rec_on (succ n) i.succ C H0 Hs = Hs n i (fin.succ_rec_on i H0 Hs) :=
by cases i; refl
/--
Define `C i` by induction on `i : fin (n + 1)` via induction on the underlying `nat` value.
This function has two arguments: `h0` handles the base case on `C 0`,
and `hs` defines the inductive step using `C i.cast_succ`.
-/
@[elab_as_eliminator] def induction
{C : fin (n + 1) → Sort*}
(h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) :
Π (i : fin (n + 1)), C i :=
begin
rintro ⟨i, hi⟩,
induction i with i IH,
{ rwa [fin.mk_zero] },
{ refine hs ⟨i, lt_of_succ_lt_succ hi⟩ _,
exact IH (lt_of_succ_lt hi) }
end
/--
Define `C i` by induction on `i : fin (n + 1)` via induction on the underlying `nat` value.
This function has two arguments: `h0` handles the base case on `C 0`,
and `hs` defines the inductive step using `C i.cast_succ`.
A version of `fin.induction` taking `i : fin (n + 1)` as the first argument.
-/
@[elab_as_eliminator] def induction_on (i : fin (n + 1))
{C : fin (n + 1) → Sort*}
(h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) : C i :=
induction h0 hs i
/-- Define `f : Π i : fin n.succ, C i` by separately handling the cases `i = 0` and
`i = j.succ`, `j : fin n`. -/
@[elab_as_eliminator] def cases
{C : fin (succ n) → Sort*} (H0 : C 0) (Hs : Π i : fin n, C (i.succ)) :
Π (i : fin (succ n)), C i :=
induction H0 (λ i _, Hs i)
@[simp] theorem cases_zero {n} {C : fin (succ n) → Sort*} {H0 Hs} : @fin.cases n C H0 Hs 0 = H0 :=
rfl
@[simp] theorem cases_succ {n} {C : fin (succ n) → Sort*} {H0 Hs} (i : fin n) :
@fin.cases n C H0 Hs i.succ = Hs i :=
by cases i; refl
@[simp] theorem cases_succ' {n} {C : fin (succ n) → Sort*} {H0 Hs} {i : ℕ} (h : i + 1 < n + 1) :
@fin.cases n C H0 Hs ⟨i.succ, h⟩ = Hs ⟨i, lt_of_succ_lt_succ h⟩ :=
by cases i; refl
lemma forall_fin_succ {P : fin (n+1) → Prop} :
(∀ i, P i) ↔ P 0 ∧ (∀ i:fin n, P i.succ) :=
⟨λ H, ⟨H 0, λ i, H _⟩, λ ⟨H0, H1⟩ i, fin.cases H0 H1 i⟩
lemma exists_fin_succ {P : fin (n+1) → Prop} :
(∃ i, P i) ↔ P 0 ∨ (∃i:fin n, P i.succ) :=
⟨λ ⟨i, h⟩, fin.cases or.inl (λ i hi, or.inr ⟨i, hi⟩) i h,
λ h, or.elim h (λ h, ⟨0, h⟩) $ λ⟨i, hi⟩, ⟨i.succ, hi⟩⟩
end rec
section pred
/-!
### pred
-/
@[simp] lemma coe_pred (j : fin (n+1)) (h : j ≠ 0) : (j.pred h : ℕ) = j - 1 :=
by { cases j, refl }
@[simp] lemma succ_pred : ∀(i : fin (n+1)) (h : i ≠ 0), (i.pred h).succ = i
| ⟨0, h⟩ hi := by contradiction
| ⟨n + 1, h⟩ hi := rfl
@[simp] lemma pred_succ (i : fin n) {h : i.succ ≠ 0} : i.succ.pred h = i :=
by { cases i, refl }
@[simp] lemma pred_mk_succ (i : ℕ) (h : i < n + 1) :
fin.pred ⟨i + 1, add_lt_add_right h 1⟩ (ne_of_vne (ne_of_gt (mk_succ_pos i h))) = ⟨i, h⟩ :=
by simp only [ext_iff, coe_pred, coe_mk, nat.add_sub_cancel]
-- This is not a simp lemma by default, because `pred_mk_succ` is nicer when it applies.
lemma pred_mk {n : ℕ} (i : ℕ) (h : i < n + 1) (w) :
fin.pred ⟨i, h⟩ w =
⟨i - 1, by rwa nat.sub_lt_right_iff_lt_add (nat.pos_of_ne_zero (fin.vne_of_ne w))⟩ :=
rfl
@[simp] lemma pred_le_pred_iff {n : ℕ} {a b : fin n.succ} {ha : a ≠ 0} {hb : b ≠ 0} :
a.pred ha ≤ b.pred hb ↔ a ≤ b :=
by rw [←succ_le_succ_iff, succ_pred, succ_pred]
@[simp] lemma pred_lt_pred_iff {n : ℕ} {a b : fin n.succ} {ha : a ≠ 0} {hb : b ≠ 0} :
a.pred ha < b.pred hb ↔ a < b :=
by rw [←succ_lt_succ_iff, succ_pred, succ_pred]
@[simp] lemma pred_inj :
∀ {a b : fin (n + 1)} {ha : a ≠ 0} {hb : b ≠ 0}, a.pred ha = b.pred hb ↔ a = b
| ⟨0, _⟩ b ha hb := by contradiction
| ⟨i+1, _⟩ ⟨0, _⟩ ha hb := by contradiction
| ⟨i+1, hi⟩ ⟨j+1, hj⟩ ha hb := by simp [fin.eq_iff_veq]
@[simp] lemma pred_one {n : ℕ} : fin.pred (1 : fin (n + 2)) (ne.symm (ne_of_lt one_pos)) = 0 := rfl
lemma pred_add_one (i : fin (n + 2)) (h : (i : ℕ) < n + 1) :
pred (i + 1) (ne_of_gt (add_one_pos _ (lt_iff_coe_lt_coe.mpr h))) = cast_lt i h :=
begin
rw [ext_iff, coe_pred, coe_cast_lt, coe_add, coe_one, mod_eq_of_lt, nat.add_sub_cancel],
exact add_lt_add_right h 1,
end
/-- `sub_nat i h` subtracts `m` from `i`, generalizes `fin.pred`. -/
def sub_nat (m) (i : fin (n + m)) (h : m ≤ (i : ℕ)) : fin n :=
⟨(i : ℕ) - m, by { rw [nat.sub_lt_right_iff_lt_add h], exact i.is_lt }⟩
@[simp] lemma coe_sub_nat (i : fin (n + m)) (h : m ≤ i) : (i.sub_nat m h : ℕ) = i - m :=
rfl
@[simp] lemma pred_cast_succ_succ (i : fin n) :
pred (cast_succ i.succ) (ne_of_gt (cast_succ_pos i.succ_pos)) = i.cast_succ :=
by simp [eq_iff_veq]
end pred
section add_group
open nat int
/-- Negation on `fin n` -/
instance (n : ℕ) : has_neg (fin n) :=
⟨λ a, ⟨(n - a) % n, nat.mod_lt _ (lt_of_le_of_lt (nat.zero_le _) a.2)⟩⟩
/-- Abelian group structure on `fin (n+1)`. -/
instance (n : ℕ) : add_comm_group (fin (n+1)) :=
{ add_left_neg := λ ⟨a, ha⟩, fin.ext $ trans (nat.mod_add_mod _ _ _) $
by { rw [fin.coe_mk, fin.coe_zero, nat.sub_add_cancel, nat.mod_self], exact le_of_lt ha },
sub_eq_add_neg := λ ⟨a, ha⟩ ⟨b, hb⟩, fin.ext $
show (a + (n + 1 - b)) % (n + 1) = (a + (n + 1 - b) % (n + 1)) % (n + 1), by simp,
sub := fin.sub,
..fin.add_comm_monoid n,
..fin.has_neg n.succ }
protected lemma coe_neg (a : fin n) : ((-a : fin n) : ℕ) = (n - a) % n := rfl
protected lemma coe_sub (a b : fin n) : ((a - b : fin n) : ℕ) = (a + (n - b)) % n :=
by cases a; cases b; refl
end add_group
section succ_above
lemma succ_above_aux (p : fin (n + 1)) :
strict_mono (λ i : fin n, if i.cast_succ < p then i.cast_succ else i.succ) :=
(cast_succ : fin n ↪o _).strict_mono.ite (succ_embedding n).strict_mono
(λ i j hij hj, lt_trans ((cast_succ : fin n ↪o _).lt_iff_lt.2 hij) hj)
(λ i, (cast_succ_lt_succ i).le)
/-- `succ_above p i` embeds `fin n` into `fin (n + 1)` with a hole around `p`. -/
def succ_above (p : fin (n + 1)) : fin n ↪o fin (n + 1) :=
order_embedding.of_strict_mono _ p.succ_above_aux
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
embeds `i` by `cast_succ` when the resulting `i.cast_succ < p`. -/
lemma succ_above_below (p : fin (n + 1)) (i : fin n) (h : i.cast_succ < p) :
p.succ_above i = i.cast_succ :=
by { rw [succ_above], exact if_pos h }
@[simp] lemma succ_above_ne_zero_zero {a : fin (n + 2)} (ha : a ≠ 0) : a.succ_above 0 = 0 :=
begin
rw fin.succ_above_below,
{ refl },
{ exact bot_lt_iff_ne_bot.mpr ha }
end
lemma succ_above_eq_zero_iff {a : fin (n + 2)} {b : fin (n + 1)} (ha : a ≠ 0) :
a.succ_above b = 0 ↔ b = 0 :=
by simp only [←succ_above_ne_zero_zero ha, order_embedding.eq_iff_eq]
lemma succ_above_ne_zero {a : fin (n + 2)} {b : fin (n + 1)} (ha : a ≠ 0) (hb : b ≠ 0) :
a.succ_above b ≠ 0 :=
mt (succ_above_eq_zero_iff ha).mp hb
/-- Embedding `fin n` into `fin (n + 1)` with a hole around zero embeds by `succ`. -/
@[simp] lemma succ_above_zero : ⇑(succ_above (0 : fin (n + 1))) = fin.succ := rfl
/-- Embedding `fin n` into `fin (n + 1)` with a hole around `last n` embeds by `cast_succ`. -/
@[simp] lemma succ_above_last : succ_above (fin.last n) = cast_succ :=
by { ext, simp only [succ_above_below, cast_succ_lt_last] }
lemma succ_above_last_apply (i : fin n) : succ_above (fin.last n) i = i.cast_succ :=
by rw succ_above_last
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
embeds `i` by `succ` when the resulting `p < i.succ`. -/
lemma succ_above_above (p : fin (n + 1)) (i : fin n) (h : p ≤ i.cast_succ) :
p.succ_above i = i.succ :=
by simp [succ_above, h.not_lt]
/-- Embedding `i : fin n` into `fin (n + 1)` is always about some hole `p`. -/
lemma succ_above_lt_ge (p : fin (n + 1)) (i : fin n) : i.cast_succ < p ∨ p ≤ i.cast_succ :=
lt_or_ge (cast_succ i) p
/-- Embedding `i : fin n` into `fin (n + 1)` is always about some hole `p`. -/
lemma succ_above_lt_gt (p : fin (n + 1)) (i : fin n) : i.cast_succ < p ∨ p < i.succ :=
or.cases_on (succ_above_lt_ge p i)
(λ h, or.inl h) (λ h, or.inr (lt_of_le_of_lt h (cast_succ_lt_succ i)))
/-- Embedding `i : fin n` into `fin (n + 1)` using a pivot `p` that is greater
results in a value that is less than `p`. -/
@[simp] lemma succ_above_lt_iff (p : fin (n + 1)) (i : fin n) :
p.succ_above i < p ↔ i.cast_succ < p :=
begin
refine iff.intro _ _,
{ intro h,
cases succ_above_lt_ge p i with H H,
{ exact H },
{ rw succ_above_above _ _ H at h,
exact lt_trans (cast_succ_lt_succ i) h } },
{ intro h,
rw succ_above_below _ _ h,
exact h }
end
/-- Embedding `i : fin n` into `fin (n + 1)` using a pivot `p` that is lesser
results in a value that is greater than `p`. -/
lemma lt_succ_above_iff (p : fin (n + 1)) (i : fin n) : p < p.succ_above i ↔ p ≤ i.cast_succ :=
begin
refine iff.intro _ _,
{ intro h,
cases succ_above_lt_ge p i with H H,
{ rw succ_above_below _ _ H at h,
exact le_of_lt h },
{ exact H } },
{ intro h,
rw succ_above_above _ _ h,
exact lt_of_le_of_lt h (cast_succ_lt_succ i) },
end
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
never results in `p` itself -/
theorem succ_above_ne (p : fin (n + 1)) (i : fin n) : p.succ_above i ≠ p :=
begin
intro eq,
by_cases H : i.cast_succ < p,
{ simpa [lt_irrefl, ←succ_above_below _ _ H, eq] using H },
{ simpa [←succ_above_above _ _ (le_of_not_lt H), eq] using cast_succ_lt_succ i }
end
/-- Embedding a positive `fin n` results in a positive fin (n + 1)` -/
lemma succ_above_pos (p : fin (n + 2)) (i : fin (n + 1)) (h : 0 < i) : 0 < p.succ_above i :=
begin
by_cases H : i.cast_succ < p,
{ simpa [succ_above_below _ _ H] using cast_succ_pos h },
{ simpa [succ_above_above _ _ (le_of_not_lt H)] using succ_pos _ },
end
/-- The range of `p.succ_above` is everything except `p`. -/
lemma range_succ_above (p : fin (n + 1)) : set.range (p.succ_above) = { i | i ≠ p } :=
begin
ext,
simp only [set.mem_range, ne.def, set.mem_set_of_eq],
split,
{ rintro ⟨y, rfl⟩,
exact succ_above_ne _ _ },
{ intro h,
cases lt_or_gt_of_ne h with H H,
{ refine ⟨x.cast_lt _, _⟩,
{ exact lt_of_lt_of_le H p.le_last },
{ rw succ_above_below,
{ simp },
{ exact H } } },
{ refine ⟨x.pred _, _⟩,
{ exact (ne_of_lt (lt_of_le_of_lt p.zero_le H)).symm },
{ rw succ_above_above,
{ simp },
{ simpa [le_iff_coe_le_coe] using nat.le_pred_of_lt H } } } }
end
/-- Given a fixed pivot `x : fin (n + 1)`, `x.succ_above` is injective -/
lemma succ_above_right_injective {x : fin (n + 1)} : injective (succ_above x) :=
(succ_above x).injective
/-- Given a fixed pivot `x : fin (n + 1)`, `x.succ_above` is injective -/
lemma succ_above_right_inj {x : fin (n + 1)} :
x.succ_above a = x.succ_above b ↔ a = b :=
succ_above_right_injective.eq_iff
/-- `succ_above` is injective at the pivot -/
lemma succ_above_left_injective : injective (@succ_above n) :=
λ _ _ h, by simpa [range_succ_above] using congr_arg (λ f : fin n ↪o fin (n + 1), (set.range f)ᶜ) h
/-- `succ_above` is injective at the pivot -/
lemma succ_above_left_inj {x y : fin (n + 1)} :
x.succ_above = y.succ_above ↔ x = y :=
succ_above_left_injective.eq_iff
@[simp] lemma zero_succ_above {n : ℕ} (i : fin n) :
(0 : fin (n + 1)).succ_above i = i.succ :=
rfl
@[simp] lemma succ_succ_above_zero {n : ℕ} (i : fin (n + 1)) :
(i.succ).succ_above 0 = 0 :=
succ_above_below _ _ (succ_pos _)
@[simp] lemma succ_succ_above_succ {n : ℕ} (i : fin (n + 1)) (j : fin n) :
(i.succ).succ_above j.succ = (i.succ_above j).succ :=
(lt_or_ge j.cast_succ i).elim
(λ h, have h' : j.succ.cast_succ < i.succ, by simpa [lt_iff_coe_lt_coe] using h,
by { ext, simp [succ_above_below _ _ h, succ_above_below _ _ h'] })
(λ h, have h' : i.succ ≤ j.succ.cast_succ, by simpa [le_iff_coe_le_coe] using h,
by { ext, simp [succ_above_above _ _ h, succ_above_above _ _ h'] })
@[simp] lemma one_succ_above_zero {n : ℕ} :
(1 : fin (n + 2)).succ_above 0 = 0 :=
succ_succ_above_zero 0
/-- By moving `succ` to the outside of this expression, we create opportunities for further
simplification using `succ_above_zero` or `succ_succ_above_zero`. -/
@[simp] lemma succ_succ_above_one {n : ℕ} (i : fin (n + 2)) :
(i.succ).succ_above 1 = (i.succ_above 0).succ :=
succ_succ_above_succ i 0
@[simp] lemma one_succ_above_succ {n : ℕ} (j : fin n) :
(1 : fin (n + 2)).succ_above j.succ = j.succ.succ :=
succ_succ_above_succ 0 j
@[simp] lemma one_succ_above_one {n : ℕ} :
(1 : fin (n + 3)).succ_above 1 = 2 :=
succ_succ_above_succ 0 0
end succ_above
section pred_above
/-- `pred_above p i` embeds `i : fin (n+1)` into `fin n` by subtracting one if `p < i`. -/
def pred_above (p : fin n) (i : fin (n+1)) : fin n :=
if h : p.cast_succ < i then
i.pred (ne_of_lt (lt_of_le_of_lt (zero_le p.cast_succ) h)).symm
else
i.cast_lt (lt_of_le_of_lt (le_of_not_lt h) p.2)
lemma pred_above_right_monotone (p : fin n) : monotone p.pred_above :=
λ a b H,
begin
dsimp [pred_above],
split_ifs with ha hb hb,
all_goals { simp only [le_iff_coe_le_coe, coe_pred], },
{ exact pred_le_pred H, },
{ calc _ ≤ _ : nat.pred_le _
... ≤ _ : H, },
{ simp at ha, exact le_pred_of_lt (lt_of_le_of_lt ha hb), },
{ exact H, },
end
lemma pred_above_left_monotone (i : fin (n + 1)) : monotone (λ p, pred_above p i) :=
λ a b H,
begin
dsimp [pred_above],
split_ifs with ha hb hb,
all_goals { simp only [le_iff_coe_le_coe, coe_pred] },
{ exact pred_le _, },
{ have : b < a := cast_succ_lt_cast_succ_iff.mpr (hb.trans_le (le_of_not_gt ha)),
exact absurd H this.not_le }
end
/-- `cast_pred` embeds `i : fin (n + 2)` into `fin (n + 1)`
by lowering just `last (n + 1)` to `last n`. -/
def cast_pred (i : fin (n + 2)) : fin (n + 1) :=
pred_above (last n) i
@[simp] lemma cast_pred_zero : cast_pred (0 : fin (n + 2)) = 0 := rfl
@[simp] lemma cast_pred_one : cast_pred (1 : fin (n + 2)) = 1 :=
by { cases n, apply subsingleton.elim, refl }
@[simp] theorem pred_above_zero {i : fin (n + 2)} (hi : i ≠ 0) :
pred_above 0 i = i.pred hi :=
begin
dsimp [pred_above],
rw dif_pos,
exact (pos_iff_ne_zero _).mpr hi,
end
@[simp] lemma cast_pred_last : cast_pred (last (n + 1)) = last n :=
by simp [eq_iff_veq, cast_pred, pred_above, cast_succ_lt_last]
@[simp] lemma cast_pred_mk (n i : ℕ) (h : i < n + 1) :
cast_pred ⟨i, lt_succ_of_lt h⟩ = ⟨i, h⟩ :=
begin
have : ¬cast_succ (last n) < ⟨i, lt_succ_of_lt h⟩,
{ simpa [lt_iff_coe_lt_coe] using le_of_lt_succ h },
simp [cast_pred, pred_above, this]
end
lemma pred_above_below (p : fin (n + 1)) (i : fin (n + 2)) (h : i ≤ p.cast_succ) :
p.pred_above i = i.cast_pred :=
begin
have : i ≤ (last n).cast_succ := h.trans p.le_last,
simp [pred_above, cast_pred, h.not_lt, this.not_lt]
end
@[simp] lemma pred_above_last : pred_above (fin.last n) = cast_pred := rfl
lemma pred_above_last_apply (i : fin n) : pred_above (fin.last n) i = i.cast_pred :=
by rw pred_above_last
lemma pred_above_above (p : fin n) (i : fin (n + 1)) (h : p.cast_succ < i) :
p.pred_above i = i.pred (p.cast_succ.zero_le.trans_lt h).ne.symm :=
by simp [pred_above, h]
lemma cast_pred_monotone : monotone (@cast_pred n) :=
pred_above_right_monotone (last _)
/-- Sending `fin (n+1)` to `fin n` by subtracting one from anything above `p`
then back to `fin (n+1)` with a gap around `p` is the identity away from `p`. -/
@[simp] lemma succ_above_pred_above {p : fin n} {i : fin (n + 1)} (h : i ≠ p.cast_succ) :
p.cast_succ.succ_above (p.pred_above i) = i :=
begin
dsimp [pred_above, succ_above],
rcases p with ⟨p, _⟩,
rcases i with ⟨i, _⟩,
cases lt_or_le i p with H H,
{ rw dif_neg, rw if_pos, refl, exact H, simp, apply le_of_lt H, },
{ rw dif_pos, rw if_neg,
swap 3, -- For some reason `simp` doesn't fire fully unless we discharge the third goal.
{ exact lt_of_le_of_ne H (ne.symm h), },
{ simp, },
{ simp only [subtype.mk_eq_mk, ne.def, fin.cast_succ_mk] at h,
simp only [pred, subtype.mk_lt_mk, not_lt],
exact nat.le_pred_of_lt (nat.lt_of_le_and_ne H (ne.symm h)), }, },
end
/-- Sending `fin n` into `fin (n + 1)` with a gap at `p`
then back to `fin n` by subtracting one from anything above `p` is the identity. -/
@[simp] lemma pred_above_succ_above (p : fin n) (i : fin n) :
p.pred_above (p.cast_succ.succ_above i) = i :=
begin
dsimp [pred_above, succ_above],
rcases p with ⟨p, _⟩,
rcases i with ⟨i, _⟩,
split_ifs,
{ rw dif_neg,
{ refl },
{ simp_rw [if_pos h],
simp only [subtype.mk_lt_mk, not_lt],
exact le_of_lt h, }, },
{ rw dif_pos,
{ refl, },
{ simp_rw [if_neg h],
exact lt_succ_iff.mpr (not_lt.mp h), }, },
end
lemma cast_succ_pred_eq_pred_cast_succ {a : fin (n + 1)} (ha : a ≠ 0)
(ha' := a.cast_succ_ne_zero_iff.mpr ha) : (a.pred ha).cast_succ = a.cast_succ.pred ha' :=
by { cases a, refl }
/-- `pred` commutes with `succ_above`. -/
lemma pred_succ_above_pred {a : fin (n + 2)} {b : fin (n + 1)} (ha : a ≠ 0) (hb : b ≠ 0)
(hk := succ_above_ne_zero ha hb) :
(a.pred ha).succ_above (b.pred hb) = (a.succ_above b).pred hk :=
begin
obtain hbelow | habove := lt_or_le b.cast_succ a, -- `rwa` uses them
{ rw fin.succ_above_below,
{ rwa [cast_succ_pred_eq_pred_cast_succ , fin.pred_inj, fin.succ_above_below] },
{ rwa [cast_succ_pred_eq_pred_cast_succ , pred_lt_pred_iff] } },
{ rw fin.succ_above_above,
have : (b.pred hb).succ = b.succ.pred (fin.succ_ne_zero _), by rw [succ_pred, pred_succ],
{ rwa [this, fin.pred_inj, fin.succ_above_above] },
{ rwa [cast_succ_pred_eq_pred_cast_succ , fin.pred_le_pred_iff] } }
end
@[simp] theorem cast_pred_cast_succ (i : fin (n + 1)) :
cast_pred i.cast_succ = i :=
by simp [cast_pred, pred_above, le_last]
lemma cast_succ_cast_pred {i : fin (n + 2)} (h : i < last _) : cast_succ i.cast_pred = i :=
begin
rw [cast_pred, pred_above, dif_neg],
{ simp [fin.eq_iff_veq] },
{ exact h.not_le }
end
lemma coe_cast_pred_le_self (i : fin (n + 2)) : (i.cast_pred : ℕ) ≤ i :=
begin
rcases i.le_last.eq_or_lt with rfl|h,
{ simp },
{ rw [cast_pred, pred_above, dif_neg],
{ simp },
{ simpa [lt_iff_coe_lt_coe, le_iff_coe_le_coe, lt_succ_iff] using h } }
end
lemma coe_cast_pred_lt_iff {i : fin (n + 2)} : (i.cast_pred : ℕ) < i ↔ i = fin.last _ :=
begin
rcases i.le_last.eq_or_lt with rfl|H,
{ simp },
{ simp only [ne_of_lt H],
rw ←cast_succ_cast_pred H,
simp }
end
lemma lt_last_iff_coe_cast_pred {i : fin (n + 2)} : i < fin.last _ ↔ (i.cast_pred : ℕ) = i :=
begin
rcases i.le_last.eq_or_lt with rfl|H,
{ simp },
{ simp only [H],
rw ←cast_succ_cast_pred H,
simp }
end
lemma forall_iff_succ_above {p : fin (n + 1) → Prop} (i : fin (n + 1)) :
(∀ j, p j) ↔ p i ∧ ∀ j, p (i.succ_above j) :=
⟨λ h, ⟨h _, λ j, h _⟩,
λ h j, if hj : j = i then (hj.symm ▸ h.1) else
begin
cases n,
{ convert h.1 },
{ cases lt_or_gt_of_ne hj with lt gt,
{ rcases j.zero_le.eq_or_lt with rfl|H,
{ convert h.2 0, rw succ_above_below; simp [lt] },
{ have ltl : j < last _ := lt.trans_le i.le_last,
convert h.2 j.cast_pred,
simp [succ_above_below, cast_succ_cast_pred ltl, lt] } },
{ convert h.2 (j.pred (i.zero_le.trans_lt gt).ne.symm),
rw succ_above_above;
simp [le_cast_succ_iff, gt.lt] } }
end⟩
end pred_above
/-- `min n m` as an element of `fin (m + 1)` -/
def clamp (n m : ℕ) : fin (m + 1) := of_nat $ min n m
@[simp] lemma coe_clamp (n m : ℕ) : (clamp n m : ℕ) = min n m :=
nat.mod_eq_of_lt $ nat.lt_succ_iff.mpr $ min_le_right _ _
section tuple
/-!
### Tuples
We can think of the type `Π(i : fin n), α i` as `n`-tuples of elements of possibly varying type
`α i`. A particular case is `fin n → α` of elements with all the same type. Here are some relevant
operations, first about adding or removing elements at the beginning of a tuple.
-/
/-- There is exactly one tuple of size zero. -/
example (α : fin 0 → Sort u) : unique (Π i : fin 0, α i) :=
by apply_instance
@[simp] lemma tuple0_le {α : Π i : fin 0, Type*} [Π i, preorder (α i)] (f g : Π i, α i) : f ≤ g :=
fin_zero_elim
variables {α : fin (n+1) → Type u} (x : α 0) (q : Πi, α i) (p : Π(i : fin n), α (i.succ))
(i : fin n) (y : α i.succ) (z : α 0)
/-- The tail of an `n+1` tuple, i.e., its last `n` entries. -/
def tail (q : Πi, α i) : (Π(i : fin n), α (i.succ)) := λ i, q i.succ
lemma tail_def {n : ℕ} {α : fin (n+1) → Type*} {q : Π i, α i} :
tail (λ k : fin (n+1), q k) = (λ k : fin n, q k.succ) := rfl
/-- Adding an element at the beginning of an `n`-tuple, to get an `n+1`-tuple. -/
def cons (x : α 0) (p : Π(i : fin n), α (i.succ)) : Πi, α i :=
λ j, fin.cases x p j
@[simp] lemma tail_cons : tail (cons x p) = p :=
by simp [tail, cons]
@[simp] lemma cons_succ : cons x p i.succ = p i :=
by simp [cons]
@[simp] lemma cons_zero : cons x p 0 = x :=
by simp [cons]
/-- Updating a tuple and adding an element at the beginning commute. -/
@[simp] lemma cons_update : cons x (update p i y) = update (cons x p) i.succ y :=
begin
ext j,
by_cases h : j = 0,
{ rw h, simp [ne.symm (succ_ne_zero i)] },
{ let j' := pred j h,
have : j'.succ = j := succ_pred j h,
rw [← this, cons_succ],
by_cases h' : j' = i,
{ rw h', simp },
{ have : j'.succ ≠ i.succ, by rwa [ne.def, succ_inj],
rw [update_noteq h', update_noteq this, cons_succ] } }
end
/-- Adding an element at the beginning of a tuple and then updating it amounts to adding it
directly. -/
lemma update_cons_zero : update (cons x p) 0 z = cons z p :=
begin
ext j,
by_cases h : j = 0,
{ rw h, simp },
{ simp only [h, update_noteq, ne.def, not_false_iff],
let j' := pred j h,
have : j'.succ = j := succ_pred j h,
rw [← this, cons_succ, cons_succ] }
end
/-- Concatenating the first element of a tuple with its tail gives back the original tuple -/
@[simp] lemma cons_self_tail : cons (q 0) (tail q) = q :=
begin
ext j,
by_cases h : j = 0,
{ rw h, simp },
{ let j' := pred j h,
have : j'.succ = j := succ_pred j h,
rw [← this, tail, cons_succ] }
end
/-- Updating the first element of a tuple does not change the tail. -/
@[simp] lemma tail_update_zero : tail (update q 0 z) = tail q :=
by { ext j, simp [tail, fin.succ_ne_zero] }
/-- Updating a nonzero element and taking the tail commute. -/
@[simp] lemma tail_update_succ :
tail (update q i.succ y) = update (tail q) i y :=
begin
ext j,
by_cases h : j = i,
{ rw h, simp [tail] },
{ simp [tail, (fin.succ_injective n).ne h, h] }
end
lemma comp_cons {α : Type*} {β : Type*} (g : α → β) (y : α) (q : fin n → α) :
g ∘ (cons y q) = cons (g y) (g ∘ q) :=
begin
ext j,
by_cases h : j = 0,
{ rw h, refl },
{ let j' := pred j h,
have : j'.succ = j := succ_pred j h,
rw [← this, cons_succ, comp_app, cons_succ] }
end
lemma comp_tail {α : Type*} {β : Type*} (g : α → β) (q : fin n.succ → α) :
g ∘ (tail q) = tail (g ∘ q) :=
by { ext j, simp [tail] }
lemma le_cons [Π i, preorder (α i)] {x : α 0} {q : Π i, α i} {p : Π i : fin n, α i.succ} :
q ≤ cons x p ↔ q 0 ≤ x ∧ tail q ≤ p :=
forall_fin_succ.trans $ and_congr iff.rfl $ forall_congr $ λ j, by simp [tail]
lemma cons_le [Π i, preorder (α i)] {x : α 0} {q : Π i, α i} {p : Π i : fin n, α i.succ} :
cons x p ≤ q ↔ x ≤ q 0 ∧ p ≤ tail q :=
@le_cons _ (λ i, order_dual (α i)) _ x q p
@[simp]
lemma range_cons {α : Type*} {n : ℕ} (x : α) (b : fin n → α) :
set.range (fin.cons x b : fin n.succ → α) = insert x (set.range b) :=
begin
ext y,
simp only [set.mem_range, set.mem_insert_iff],
split,
{ rintros ⟨i, rfl⟩,
refine cases (or.inl (cons_zero _ _)) (λ i, or.inr ⟨i, _⟩) i,
rw cons_succ },
{ rintros (rfl | ⟨i, hi⟩),
{ exact ⟨0, fin.cons_zero _ _⟩ },
{ refine ⟨i.succ, _⟩,
rw [cons_succ, hi] } }
end
/-- `fin.append ho u v` appends two vectors of lengths `m` and `n` to produce
one of length `o = m + n`. `ho` provides control of definitional equality
for the vector length. -/
def append {α : Type*} {o : ℕ} (ho : o = m + n) (u : fin m → α) (v : fin n → α) : fin o → α :=
λ i, if h : (i : ℕ) < m
then u ⟨i, h⟩
else v ⟨(i : ℕ) - m, (nat.sub_lt_left_iff_lt_add (le_of_not_lt h)).2 (ho ▸ i.property)⟩
@[simp] lemma fin_append_apply_zero {α : Type*} {o : ℕ} (ho : (o + 1) = (m + 1) + n)
(u : fin (m + 1) → α) (v : fin n → α) :
fin.append ho u v 0 = u 0 := rfl
end tuple
section tuple_right
/-! In the previous section, we have discussed inserting or removing elements on the left of a
tuple. In this section, we do the same on the right. A difference is that `fin (n+1)` is constructed
inductively from `fin n` starting from the left, not from the right. This implies that Lean needs
more help to realize that elements belong to the right types, i.e., we need to insert casts at
several places. -/
variables {α : fin (n+1) → Type u} (x : α (last n)) (q : Πi, α i) (p : Π(i : fin n), α i.cast_succ)
(i : fin n) (y : α i.cast_succ) (z : α (last n))
/-- The beginning of an `n+1` tuple, i.e., its first `n` entries -/
def init (q : Πi, α i) (i : fin n) : α i.cast_succ :=
q i.cast_succ
lemma init_def {n : ℕ} {α : fin (n+1) → Type*} {q : Π i, α i} :
init (λ k : fin (n+1), q k) = (λ k : fin n, q k.cast_succ) := rfl
/-- Adding an element at the end of an `n`-tuple, to get an `n+1`-tuple. The name `snoc` comes from
`cons` (i.e., adding an element to the left of a tuple) read in reverse order. -/
def snoc (p : Π(i : fin n), α i.cast_succ) (x : α (last n)) (i : fin (n+1)) : α i :=
if h : i.val < n
then _root_.cast (by rw fin.cast_succ_cast_lt i h) (p (cast_lt i h))
else _root_.cast (by rw eq_last_of_not_lt h) x
@[simp] lemma init_snoc : init (snoc p x) = p :=
begin
ext i,
have h' := fin.cast_lt_cast_succ i i.is_lt,
simp [init, snoc, i.is_lt, h'],
convert cast_eq rfl (p i)
end
@[simp] lemma snoc_cast_succ : snoc p x i.cast_succ = p i :=
begin
have : i.cast_succ.val < n := i.is_lt,
have h' := fin.cast_lt_cast_succ i i.is_lt,
simp [snoc, this, h'],
convert cast_eq rfl (p i)
end
@[simp] lemma snoc_last : snoc p x (last n) = x :=
by { simp [snoc] }
/-- Updating a tuple and adding an element at the end commute. -/
@[simp] lemma snoc_update : snoc (update p i y) x = update (snoc p x) i.cast_succ y :=
begin
ext j,
by_cases h : j.val < n,
{ simp only [snoc, h, dif_pos],
by_cases h' : j = cast_succ i,
{ have C1 : α i.cast_succ = α j, by rw h',
have E1 : update (snoc p x) i.cast_succ y j = _root_.cast C1 y,
{ have : update (snoc p x) j (_root_.cast C1 y) j = _root_.cast C1 y, by simp,
convert this,
{ exact h'.symm },
{ exact heq_of_cast_eq (congr_arg α (eq.symm h')) rfl } },
have C2 : α i.cast_succ = α (cast_succ (cast_lt j h)),
by rw [cast_succ_cast_lt, h'],
have E2 : update p i y (cast_lt j h) = _root_.cast C2 y,
{ have : update p (cast_lt j h) (_root_.cast C2 y) (cast_lt j h) = _root_.cast C2 y,
by simp,
convert this,
{ simp [h, h'] },
{ exact heq_of_cast_eq C2 rfl } },
rw [E1, E2],
exact eq_rec_compose _ _ _ },
{ have : ¬(cast_lt j h = i),
by { assume E, apply h', rw [← E, cast_succ_cast_lt] },
simp [h', this, snoc, h] } },
{ rw eq_last_of_not_lt h,
simp [ne.symm (ne_of_lt (cast_succ_lt_last i))] }
end
/-- Adding an element at the beginning of a tuple and then updating it amounts to adding it
directly. -/
lemma update_snoc_last : update (snoc p x) (last n) z = snoc p z :=
begin
ext j,
by_cases h : j.val < n,
{ have : j ≠ last n := ne_of_lt h,
simp [h, update_noteq, this, snoc] },
{ rw eq_last_of_not_lt h,
simp }
end
/-- Concatenating the first element of a tuple with its tail gives back the original tuple -/
@[simp] lemma snoc_init_self : snoc (init q) (q (last n)) = q :=
begin
ext j,
by_cases h : j.val < n,
{ have : j ≠ last n := ne_of_lt h,
simp [h, update_noteq, this, snoc, init, cast_succ_cast_lt],
have A : cast_succ (cast_lt j h) = j := cast_succ_cast_lt _ _,
rw ← cast_eq rfl (q j),
congr' 1; rw A },
{ rw eq_last_of_not_lt h,
simp }
end
/-- Updating the last element of a tuple does not change the beginning. -/
@[simp] lemma init_update_last : init (update q (last n) z) = init q :=
by { ext j, simp [init, ne_of_lt, cast_succ_lt_last] }
/-- Updating an element and taking the beginning commute. -/
@[simp] lemma init_update_cast_succ :
init (update q i.cast_succ y) = update (init q) i y :=
begin
ext j,
by_cases h : j = i,
{ rw h, simp [init] },
{ simp [init, h] }
end
/-- `tail` and `init` commute. We state this lemma in a non-dependent setting, as otherwise it
would involve a cast to convince Lean that the two types are equal, making it harder to use. -/
lemma tail_init_eq_init_tail {β : Type*} (q : fin (n+2) → β) :
tail (init q) = init (tail q) :=
by { ext i, simp [tail, init, cast_succ_fin_succ] }
/-- `cons` and `snoc` commute. We state this lemma in a non-dependent setting, as otherwise it
would involve a cast to convince Lean that the two types are equal, making it harder to use. -/
lemma cons_snoc_eq_snoc_cons {β : Type*} (a : β) (q : fin n → β) (b : β) :
@cons n.succ (λ i, β) a (snoc q b) = snoc (cons a q) b :=
begin
ext i,
by_cases h : i = 0,
{ rw h, refl },
set j := pred i h with ji,
have : i = j.succ, by rw [ji, succ_pred],
rw [this, cons_succ],
by_cases h' : j.val < n,
{ set k := cast_lt j h' with jk,
have : j = k.cast_succ, by rw [jk, cast_succ_cast_lt],
rw [this, ← cast_succ_fin_succ],
simp },
rw [eq_last_of_not_lt h', succ_last],
simp
end
lemma comp_snoc {α : Type*} {β : Type*} (g : α → β) (q : fin n → α) (y : α) :
g ∘ (snoc q y) = snoc (g ∘ q) (g y) :=
begin
ext j,
by_cases h : j.val < n,
{ have : j ≠ last n := ne_of_lt h,
simp [h, this, snoc, cast_succ_cast_lt] },
{ rw eq_last_of_not_lt h,
simp }
end
lemma comp_init {α : Type*} {β : Type*} (g : α → β) (q : fin n.succ → α) :
g ∘ (init q) = init (g ∘ q) :=
by { ext j, simp [init] }
end tuple_right
section insert_nth
variables {α : fin (n+1) → Type u} {β : Type v}
/-- Insert an element into a tuple at a given position, auxiliary definition.
For the general definition, see `insert_nth`. -/
def insert_nth' {α : fin (n + 2) → Type u} (i : fin (n + 2)) (x : α i)
(p : Π j : fin (n + 1), α (i.succ_above j)) (j : fin (n + 2)) : α j :=
if h : i = j
then _root_.cast (congr_arg α h) x
else if h' : j < i then _root_.cast (congr_arg α $ begin
obtain ⟨k, hk⟩ : ∃ (k : fin (n + 1)), k.cast_succ = j,
{ refine ⟨⟨(j : ℕ), _⟩, _⟩,
{ exact lt_of_lt_of_le h' i.is_le, },
{ simp },
},
subst hk,
simp [succ_above_below, h'],
end)
(p j.cast_pred) else _root_.cast (congr_arg α $ begin
have lt : i < j := lt_of_le_of_ne (le_of_not_lt h') h,
have : j ≠ 0 := (ne_of_gt (lt_of_le_of_lt i.zero_le lt)),
rw [←succ_pred j this, ←le_cast_succ_iff] at lt,
simp [pred_above_zero this, succ_above_above _ _ lt]
end) (p (fin.pred_above 0 j))
/-- Insert an element into a tuple at a given position. For `i = 0` see `fin.cons`,
for `i = fin.last n` see `fin.snoc`. -/
def insert_nth : Π {n : ℕ} {α : fin (n + 1) → Type u} (i : fin (n + 1)) (x : α i)
(p : Π j : fin n, α (i.succ_above j)) (j : fin (n + 1)), α j
| 0 _ _ x _ _ := _root_.cast (by congr) x
| (n + 1) _ i x p j := insert_nth' i x p j
@[simp] lemma insert_nth_apply_same (i : fin (n + 1)) (x : α i) (p : Π j, α (i.succ_above j)) :
insert_nth i x p i = x :=
by { cases n; simp [insert_nth, insert_nth'] }
@[simp] lemma insert_nth_apply_succ_above (i : fin (n + 1)) (x : α i) (p : Π j, α (i.succ_above j))
(j : fin n) :
insert_nth i x p (i.succ_above j) = p j :=
begin
cases n,
{ exact j.elim0 },
simp only [insert_nth, insert_nth', dif_neg (succ_above_ne _ _).symm],
cases succ_above_lt_ge i j with h h,
{ rw dif_pos,
refine eq_of_heq ((cast_heq _ _).trans _),
{ simp [h] },
{ congr,
simp [succ_above_below, h] } },
{ rw dif_neg,
refine eq_of_heq ((cast_heq _ _).trans _),
{ simp [h] },
{ congr,
simp [succ_above_above, h, succ_ne_zero] } }
end
@[simp] lemma insert_nth_comp_succ_above (i : fin (n + 1)) (x : β) (p : fin n → β) :
insert_nth i x p ∘ i.succ_above = p :=
funext $ insert_nth_apply_succ_above i x p
lemma insert_nth_eq_iff {i : fin (n + 1)} {x : α i} {p : Π j, α (i.succ_above j)} {q : Π j, α j} :
i.insert_nth x p = q ↔ q i = x ∧ p = (λ j, q (i.succ_above j)) :=
by simp [funext_iff, forall_iff_succ_above i, eq_comm]
lemma eq_insert_nth_iff {i : fin (n + 1)} {x : α i} {p : Π j, α (i.succ_above j)} {q : Π j, α j} :
q = i.insert_nth x p ↔ q i = x ∧ p = (λ j, q (i.succ_above j)) :=
eq_comm.trans insert_nth_eq_iff
lemma insert_nth_zero (x : α 0) (p : Π j : fin n, α (succ_above 0 j)) :
insert_nth 0 x p = cons x (λ j, _root_.cast (congr_arg α (congr_fun succ_above_zero j)) (p j)) :=
begin
refine insert_nth_eq_iff.2 ⟨by simp, _⟩,
ext j,
convert (cons_succ _ _ _).symm
end
@[simp] lemma insert_nth_zero' (x : β) (p : fin n → β) :
@insert_nth _ (λ _, β) 0 x p = cons x p :=
by simp [insert_nth_zero]
lemma insert_nth_last (x : α (last n)) (p : Π j : fin n, α ((last n).succ_above j)) :
insert_nth (last n) x p =
snoc (λ j, _root_.cast (congr_arg α (succ_above_last_apply j)) (p j)) x :=
begin
refine insert_nth_eq_iff.2 ⟨by simp, _⟩,
ext j,
apply eq_of_heq,
transitivity snoc (λ j, _root_.cast (congr_arg α (succ_above_last_apply j)) (p j)) x j.cast_succ,
{ rw [snoc_cast_succ], exact (cast_heq _ _).symm },
{ apply congr_arg_heq,
rw [succ_above_last] }
end
@[simp] lemma insert_nth_last' (x : β) (p : fin n → β) :
@insert_nth _ (λ _, β) (last n) x p = snoc p x :=
by simp [insert_nth_last]
variables [Π i, preorder (α i)]
lemma insert_nth_le_iff {i : fin (n + 1)} {x : α i} {p : Π j, α (i.succ_above j)} {q : Π j, α j} :
i.insert_nth x p ≤ q ↔ x ≤ q i ∧ p ≤ (λ j, q (i.succ_above j)) :=
by simp [pi.le_def, forall_iff_succ_above i]
lemma le_insert_nth_iff {i : fin (n + 1)} {x : α i} {p : Π j, α (i.succ_above j)} {q : Π j, α j} :
q ≤ i.insert_nth x p ↔ q i ≤ x ∧ (λ j, q (i.succ_above j)) ≤ p :=
by simp [pi.le_def, forall_iff_succ_above i]
open set
lemma insert_nth_mem_Icc {i : fin (n + 1)} {x : α i} {p : Π j, α (i.succ_above j)}
{q₁ q₂ : Π j, α j} :
i.insert_nth x p ∈ Icc q₁ q₂ ↔
x ∈ Icc (q₁ i) (q₂ i) ∧ p ∈ Icc (λ j, q₁ (i.succ_above j)) (λ j, q₂ (i.succ_above j)) :=
by simp only [mem_Icc, insert_nth_le_iff, le_insert_nth_iff, and.assoc, and.left_comm]
lemma preimage_insert_nth_Icc_of_mem {i : fin (n + 1)} {x : α i} {q₁ q₂ : Π j, α j}
(hx : x ∈ Icc (q₁ i) (q₂ i)) :
i.insert_nth x ⁻¹' (Icc q₁ q₂) = Icc (λ j, q₁ (i.succ_above j)) (λ j, q₂ (i.succ_above j)) :=
set.ext $ λ p, by simp only [mem_preimage, insert_nth_mem_Icc, hx, true_and]
lemma preimage_insert_nth_Icc_of_not_mem {i : fin (n + 1)} {x : α i} {q₁ q₂ : Π j, α j}
(hx : x ∉ Icc (q₁ i) (q₂ i)) :
i.insert_nth x ⁻¹' (Icc q₁ q₂) = ∅ :=
set.ext $ λ p, by simp only [mem_preimage, insert_nth_mem_Icc, hx, false_and, mem_empty_eq]
end insert_nth
section find
/-- `find p` returns the first index `n` where `p n` is satisfied, and `none` if it is never
satisfied. -/
def find : Π {n : ℕ} (p : fin n → Prop) [decidable_pred p], option (fin n)
| 0 p _ := none
| (n+1) p _ := by resetI; exact option.cases_on
(@find n (λ i, p (i.cast_lt (nat.lt_succ_of_lt i.2))) _)
(if h : p (fin.last n) then some (fin.last n) else none)
(λ i, some (i.cast_lt (nat.lt_succ_of_lt i.2)))
/-- If `find p = some i`, then `p i` holds -/
lemma find_spec : Π {n : ℕ} (p : fin n → Prop) [decidable_pred p] {i : fin n}
(hi : i ∈ by exactI fin.find p), p i
| 0 p I i hi := option.no_confusion hi
| (n+1) p I i hi := begin
dsimp [find] at hi,
resetI,
cases h : find (λ i : fin n, (p (i.cast_lt (nat.lt_succ_of_lt i.2)))) with j,
{ rw h at hi,
dsimp at hi,
split_ifs at hi with hl hl,
{ exact option.some_inj.1 hi ▸ hl },
{ exact option.no_confusion hi } },
{ rw h at hi,
rw [← option.some_inj.1 hi],
exact find_spec _ h }
end
/-- `find p` does not return `none` if and only if `p i` holds at some index `i`. -/
lemma is_some_find_iff : Π {n : ℕ} {p : fin n → Prop} [decidable_pred p],
by exactI (find p).is_some ↔ ∃ i, p i
| 0 p _ := iff_of_false (λ h, bool.no_confusion h) (λ ⟨i, _⟩, fin_zero_elim i)
| (n+1) p _ := ⟨λ h, begin
rw [option.is_some_iff_exists] at h,
cases h with i hi,
exactI ⟨i, find_spec _ hi⟩
end, λ ⟨⟨i, hin⟩, hi⟩,
begin
resetI,
dsimp [find],
cases h : find (λ i : fin n, (p (i.cast_lt (nat.lt_succ_of_lt i.2)))) with j,
{ split_ifs with hl hl,
{ exact option.is_some_some },
{ have := (@is_some_find_iff n (λ x, p (x.cast_lt (nat.lt_succ_of_lt x.2))) _).2
⟨⟨i, lt_of_le_of_ne (nat.le_of_lt_succ hin)
(λ h, by clear_aux_decl; cases h; exact hl hi)⟩, hi⟩,
rw h at this,
exact this } },
{ simp }
end⟩
/-- `find p` returns `none` if and only if `p i` never holds. -/
lemma find_eq_none_iff {n : ℕ} {p : fin n → Prop} [decidable_pred p] :
find p = none ↔ ∀ i, ¬ p i :=
by rw [← not_exists, ← is_some_find_iff]; cases (find p); simp
/-- If `find p` returns `some i`, then `p j` does not hold for `j < i`, i.e., `i` is minimal among
the indices where `p` holds. -/
lemma find_min : Π {n : ℕ} {p : fin n → Prop} [decidable_pred p] {i : fin n}
(hi : i ∈ by exactI fin.find p) {j : fin n} (hj : j < i), ¬ p j
| 0 p _ i hi j hj hpj := option.no_confusion hi
| (n+1) p _ i hi ⟨j, hjn⟩ hj hpj := begin
resetI,
dsimp [find] at hi,
cases h : find (λ i : fin n, (p (i.cast_lt (nat.lt_succ_of_lt i.2)))) with k,
{ rw [h] at hi,
split_ifs at hi with hl hl,
{ have := option.some_inj.1 hi,
subst this,
rw [find_eq_none_iff] at h,
exact h ⟨j, hj⟩ hpj },
{ exact option.no_confusion hi } },
{ rw h at hi,
dsimp at hi,
have := option.some_inj.1 hi,
subst this,
exact find_min h (show (⟨j, lt_trans hj k.2⟩ : fin n) < k, from hj) hpj }
end
lemma find_min' {p : fin n → Prop} [decidable_pred p] {i : fin n}
(h : i ∈ fin.find p) {j : fin n} (hj : p j) : i ≤ j :=
le_of_not_gt (λ hij, find_min h hij hj)
lemma nat_find_mem_find {p : fin n → Prop} [decidable_pred p]
(h : ∃ i, ∃ hin : i < n, p ⟨i, hin⟩) :
(⟨nat.find h, (nat.find_spec h).fst⟩ : fin n) ∈ find p :=
let ⟨i, hin, hi⟩ := h in
begin
cases hf : find p with f,
{ rw [find_eq_none_iff] at hf,
exact (hf ⟨i, hin⟩ hi).elim },
{ refine option.some_inj.2 (le_antisymm _ _),
{ exact find_min' hf (nat.find_spec h).snd },
{ exact nat.find_min' _ ⟨f.2, by convert find_spec p hf;
exact fin.eta _ _⟩ } }
end
lemma mem_find_iff {p : fin n → Prop} [decidable_pred p] {i : fin n} :
i ∈ fin.find p ↔ p i ∧ ∀ j, p j → i ≤ j :=
⟨λ hi, ⟨find_spec _ hi, λ _, find_min' hi⟩,
begin
rintros ⟨hpi, hj⟩,
cases hfp : fin.find p,
{ rw [find_eq_none_iff] at hfp,
exact (hfp _ hpi).elim },
{ exact option.some_inj.2 (le_antisymm (find_min' hfp hpi) (hj _ (find_spec _ hfp))) }
end⟩
lemma find_eq_some_iff {p : fin n → Prop} [decidable_pred p] {i : fin n} :
fin.find p = some i ↔ p i ∧ ∀ j, p j → i ≤ j :=
mem_find_iff
lemma mem_find_of_unique {p : fin n → Prop} [decidable_pred p]
(h : ∀ i j, p i → p j → i = j) {i : fin n} (hi : p i) : i ∈ fin.find p :=
mem_find_iff.2 ⟨hi, λ j hj, le_of_eq $ h i j hi hj⟩
end find
@[simp]
lemma coe_of_nat_eq_mod (m n : ℕ) :
((n : fin (succ m)) : ℕ) = n % succ m :=
by rw [← of_nat_eq_coe]; refl
@[simp] lemma coe_of_nat_eq_mod' (m n : ℕ) [I : fact (0 < m)] :
(@fin.of_nat' _ I n : ℕ) = n % m :=
rfl
section mul
/-!
### mul
-/
lemma val_mul {n : ℕ} : ∀ a b : fin n, (a * b).val = (a.val * b.val) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_mul {n : ℕ} : ∀ a b : fin n, ((a * b : fin n) : ℕ) = (a * b) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
@[simp] protected lemma mul_one (k : fin (n + 1)) : k * 1 = k :=
by { cases n, simp, simp [eq_iff_veq, mul_def, mod_eq_of_lt (is_lt k)] }
@[simp] protected lemma one_mul (k : fin (n + 1)) : (1 : fin (n + 1)) * k = k :=
by { cases n, simp, simp [eq_iff_veq, mul_def, mod_eq_of_lt (is_lt k)] }
@[simp] protected lemma mul_zero (k : fin (n + 1)) : k * 0 = 0 :=
by simp [eq_iff_veq, mul_def]
@[simp] protected lemma zero_mul (k : fin (n + 1)) : (0 : fin (n + 1)) * k = 0 :=
by simp [eq_iff_veq, mul_def]
end mul
end fin
|
9f1553b6053a354eeb4d9b1b801e0e49305b7b1a | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/group_theory/congruence.lean | 4d5d10def7e96c0ee64fc9df6f30550b9ca41260 | [
"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 | 51,439 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston
-/
import group_theory.submonoid.operations
import data.equiv.mul_add
import data.setoid.basic
import algebra.group.prod
/-!
# Congruence relations
This file defines congruence relations: equivalence relations that preserve a binary operation,
which in this case is multiplication or addition. The principal definition is a `structure`
extending a `setoid` (an equivalence relation), and the inductive definition of the smallest
congruence relation containing a binary relation is also given (see `con_gen`).
The file also proves basic properties of the quotient of a type by a congruence relation, and the
complete lattice of congruence relations on a type. We then establish an order-preserving bijection
between the set of congruence relations containing a congruence relation `c` and the set of
congruence relations on the quotient by `c`.
The second half of the file concerns congruence relations on monoids, in which case the
quotient by the congruence relation is also a monoid. There are results about the universal
property of quotients of monoids, and the isomorphism theorems for monoids.
## Implementation notes
The inductive definition of a congruence relation could be a nested inductive type, defined using
the equivalence closure of a binary relation `eqv_gen`, but the recursor generated does not work.
A nested inductive definition could conceivably shorten proofs, because they would allow invocation
of the corresponding lemmas about `eqv_gen`.
The lemmas `refl`, `symm` and `trans` are not tagged with `@[refl]`, `@[symm]`, and `@[trans]`
respectively as these tags do not work on a structure coerced to a binary relation.
There is a coercion from elements of a type to the element's equivalence class under a
congruence relation.
A congruence relation on a monoid `M` can be thought of as a submonoid of `M × M` for which
membership is an equivalence relation, but whilst this fact is established in the file, it is not
used, since this perspective adds more layers of definitional unfolding.
## Tags
congruence, congruence relation, quotient, quotient by congruence relation, monoid,
quotient monoid, isomorphism theorems
-/
variables (M : Type*) {N : Type*} {P : Type*}
open function setoid
/-- A congruence relation on a type with an addition is an equivalence relation which
preserves addition. -/
structure add_con [has_add M] extends setoid M :=
(add' : ∀ {w x y z}, r w x → r y z → r (w + y) (x + z))
/-- A congruence relation on a type with a multiplication is an equivalence relation which
preserves multiplication. -/
@[to_additive add_con] structure con [has_mul M] extends setoid M :=
(mul' : ∀ {w x y z}, r w x → r y z → r (w * y) (x * z))
/-- The equivalence relation underlying an additive congruence relation. -/
add_decl_doc add_con.to_setoid
/-- The equivalence relation underlying a multiplicative congruence relation. -/
add_decl_doc con.to_setoid
variables {M}
/-- The inductively defined smallest additive congruence relation containing a given binary
relation. -/
inductive add_con_gen.rel [has_add M] (r : M → M → Prop) : M → M → Prop
| of : Π x y, r x y → add_con_gen.rel x y
| refl : Π x, add_con_gen.rel x x
| symm : Π x y, add_con_gen.rel x y → add_con_gen.rel y x
| trans : Π x y z, add_con_gen.rel x y → add_con_gen.rel y z → add_con_gen.rel x z
| add : Π w x y z, add_con_gen.rel w x → add_con_gen.rel y z → add_con_gen.rel (w + y) (x + z)
/-- The inductively defined smallest multiplicative congruence relation containing a given binary
relation. -/
@[to_additive add_con_gen.rel]
inductive con_gen.rel [has_mul M] (r : M → M → Prop) : M → M → Prop
| of : Π x y, r x y → con_gen.rel x y
| refl : Π x, con_gen.rel x x
| symm : Π x y, con_gen.rel x y → con_gen.rel y x
| trans : Π x y z, con_gen.rel x y → con_gen.rel y z → con_gen.rel x z
| mul : Π w x y z, con_gen.rel w x → con_gen.rel y z → con_gen.rel (w * y) (x * z)
/-- The inductively defined smallest multiplicative congruence relation containing a given binary
relation. -/
@[to_additive add_con_gen "The inductively defined smallest additive congruence relation containing
a given binary relation."]
def con_gen [has_mul M] (r : M → M → Prop) : con M :=
⟨⟨con_gen.rel r, ⟨con_gen.rel.refl, con_gen.rel.symm, con_gen.rel.trans⟩⟩, con_gen.rel.mul⟩
namespace con
section
variables [has_mul M] [has_mul N] [has_mul P] (c : con M)
@[to_additive]
instance : inhabited (con M) :=
⟨con_gen empty_relation⟩
/-- A coercion from a congruence relation to its underlying binary relation. -/
@[to_additive "A coercion from an additive congruence relation to its underlying binary relation."]
instance : has_coe_to_fun (con M) (λ _, M → M → Prop) := ⟨λ c, λ x y, @setoid.r _ c.to_setoid x y⟩
@[simp, to_additive] lemma rel_eq_coe (c : con M) : c.r = c := rfl
/-- Congruence relations are reflexive. -/
@[to_additive "Additive congruence relations are reflexive."]
protected lemma refl (x) : c x x := c.to_setoid.refl' x
/-- Congruence relations are symmetric. -/
@[to_additive "Additive congruence relations are symmetric."]
protected lemma symm : ∀ {x y}, c x y → c y x := λ _ _ h, c.to_setoid.symm' h
/-- Congruence relations are transitive. -/
@[to_additive "Additive congruence relations are transitive."]
protected lemma trans : ∀ {x y z}, c x y → c y z → c x z :=
λ _ _ _ h, c.to_setoid.trans' h
/-- Multiplicative congruence relations preserve multiplication. -/
@[to_additive "Additive congruence relations preserve addition."]
protected lemma mul : ∀ {w x y z}, c w x → c y z → c (w * y) (x * z) :=
λ _ _ _ _ h1 h2, c.mul' h1 h2
@[simp, to_additive] lemma rel_mk {s : setoid M} {h a b} :
con.mk s h a b ↔ r a b :=
iff.rfl
/-- Given a type `M` with a multiplication, a congruence relation `c` on `M`, and elements of `M`
`x, y`, `(x, y) ∈ M × M` iff `x` is related to `y` by `c`. -/
@[to_additive "Given a type `M` with an addition, `x, y ∈ M`, and an additive congruence relation
`c` on `M`, `(x, y) ∈ M × M` iff `x` is related to `y` by `c`."]
instance : has_mem (M × M) (con M) := ⟨λ x c, c x.1 x.2⟩
variables {c}
/-- The map sending a congruence relation to its underlying binary relation is injective. -/
@[to_additive "The map sending an additive congruence relation to its underlying binary relation
is injective."]
lemma ext' {c d : con M} (H : c.r = d.r) : c = d :=
by { rcases c with ⟨⟨⟩⟩, rcases d with ⟨⟨⟩⟩, cases H, congr, }
/-- Extensionality rule for congruence relations. -/
@[ext, to_additive "Extensionality rule for additive congruence relations."]
lemma ext {c d : con M} (H : ∀ x y, c x y ↔ d x y) : c = d :=
ext' $ by ext; apply H
/-- The map sending a congruence relation to its underlying equivalence relation is injective. -/
@[to_additive "The map sending an additive congruence relation to its underlying equivalence
relation is injective."]
lemma to_setoid_inj {c d : con M} (H : c.to_setoid = d.to_setoid) : c = d :=
ext $ ext_iff.1 H
/-- Iff version of extensionality rule for congruence relations. -/
@[to_additive "Iff version of extensionality rule for additive congruence relations."]
lemma ext_iff {c d : con M} : (∀ x y, c x y ↔ d x y) ↔ c = d :=
⟨ext, λ h _ _, h ▸ iff.rfl⟩
/-- Two congruence relations are equal iff their underlying binary relations are equal. -/
@[to_additive "Two additive congruence relations are equal iff their underlying binary relations
are equal."]
lemma ext'_iff {c d : con M} : c.r = d.r ↔ c = d :=
⟨ext', λ h, h ▸ rfl⟩
/-- The kernel of a multiplication-preserving function as a congruence relation. -/
@[to_additive "The kernel of an addition-preserving function as an additive congruence relation."]
def mul_ker (f : M → P) (h : ∀ x y, f (x * y) = f x * f y) : con M :=
{ to_setoid := setoid.ker f,
mul' := λ _ _ _ _ h1 h2, by { dsimp [setoid.ker, on_fun] at *, rw [h, h1, h2, h], } }
/-- Given types with multiplications `M, N`, the product of two congruence relations `c` on `M` and
`d` on `N`: `(x₁, x₂), (y₁, y₂) ∈ M × N` are related by `c.prod d` iff `x₁` is related to `y₁`
by `c` and `x₂` is related to `y₂` by `d`. -/
@[to_additive prod "Given types with additions `M, N`, the product of two congruence relations
`c` on `M` and `d` on `N`: `(x₁, x₂), (y₁, y₂) ∈ M × N` are related by `c.prod d` iff `x₁`
is related to `y₁` by `c` and `x₂` is related to `y₂` by `d`."]
protected def prod (c : con M) (d : con N) : con (M × N) :=
{ mul' := λ _ _ _ _ h1 h2, ⟨c.mul h1.1 h2.1, d.mul h1.2 h2.2⟩, ..c.to_setoid.prod d.to_setoid }
/-- The product of an indexed collection of congruence relations. -/
@[to_additive "The product of an indexed collection of additive congruence relations."]
def pi {ι : Type*} {f : ι → Type*} [Π i, has_mul (f i)]
(C : Π i, con (f i)) : con (Π i, f i) :=
{ mul' := λ _ _ _ _ h1 h2 i, (C i).mul (h1 i) (h2 i), ..@pi_setoid _ _ $ λ i, (C i).to_setoid }
variables (c)
-- Quotients
/-- Defining the quotient by a congruence relation of a type with a multiplication. -/
@[to_additive "Defining the quotient by an additive congruence relation of a type with
an addition."]
protected def quotient := quotient $ c.to_setoid
/-- Coercion from a type with a multiplication to its quotient by a congruence relation.
See Note [use has_coe_t]. -/
@[to_additive "Coercion from a type with an addition to its quotient by an additive congruence
relation", priority 0]
instance : has_coe_t M c.quotient := ⟨@quotient.mk _ c.to_setoid⟩
/-- The quotient by a decidable congruence relation has decidable equality. -/
@[to_additive "The quotient by a decidable additive congruence relation has decidable equality.",
priority 500] -- Lower the priority since it unifies with any quotient type.
instance [d : ∀ a b, decidable (c a b)] : decidable_eq c.quotient :=
@quotient.decidable_eq M c.to_setoid d
@[simp, to_additive] lemma quot_mk_eq_coe {M : Type*} [has_mul M] (c : con M) (x : M) :
quot.mk c x = (x : c.quotient) :=
rfl
/-- The function on the quotient by a congruence relation `c` induced by a function that is
constant on `c`'s equivalence classes. -/
@[elab_as_eliminator, to_additive "The function on the quotient by a congruence relation `c`
induced by a function that is constant on `c`'s equivalence classes."]
protected def lift_on {β} {c : con M} (q : c.quotient) (f : M → β)
(h : ∀ a b, c a b → f a = f b) : β := quotient.lift_on' q f h
/-- The binary function on the quotient by a congruence relation `c` induced by a binary function
that is constant on `c`'s equivalence classes. -/
@[elab_as_eliminator, to_additive "The binary function on the quotient by a congruence relation `c`
induced by a binary function that is constant on `c`'s equivalence classes."]
protected def lift_on₂ {β} {c : con M} (q r : c.quotient) (f : M → M → β)
(h : ∀ a₁ a₂ b₁ b₂, c a₁ b₁ → c a₂ b₂ → f a₁ a₂ = f b₁ b₂) : β := quotient.lift_on₂' q r f h
/-- A version of `quotient.hrec_on₂'` for quotients by `con`. -/
@[to_additive "A version of `quotient.hrec_on₂'` for quotients by `add_con`."]
protected def hrec_on₂ {cM : con M} {cN : con N} {φ : cM.quotient → cN.quotient → Sort*}
(a : cM.quotient) (b : cN.quotient)
(f : Π (x : M) (y : N), φ x y) (h : ∀ x y x' y', cM x x' → cN y y' → f x y == f x' y') :
φ a b :=
quotient.hrec_on₂' a b f h
@[simp, to_additive] lemma hrec_on₂_coe {cM : con M} {cN : con N}
{φ : cM.quotient → cN.quotient → Sort*} (a : M) (b : N)
(f : Π (x : M) (y : N), φ x y) (h : ∀ x y x' y', cM x x' → cN y y' → f x y == f x' y') :
con.hrec_on₂ ↑a ↑b f h = f a b :=
rfl
variables {c}
/-- The inductive principle used to prove propositions about the elements of a quotient by a
congruence relation. -/
@[elab_as_eliminator, to_additive "The inductive principle used to prove propositions about
the elements of a quotient by an additive congruence relation."]
protected lemma induction_on {C : c.quotient → Prop} (q : c.quotient) (H : ∀ x : M, C x) : C q :=
quotient.induction_on' q H
/-- A version of `con.induction_on` for predicates which take two arguments. -/
@[elab_as_eliminator, to_additive "A version of `add_con.induction_on` for predicates which take
two arguments."]
protected lemma induction_on₂ {d : con N} {C : c.quotient → d.quotient → Prop}
(p : c.quotient) (q : d.quotient) (H : ∀ (x : M) (y : N), C x y) : C p q :=
quotient.induction_on₂' p q H
variables (c)
/-- Two elements are related by a congruence relation `c` iff they are represented by the same
element of the quotient by `c`. -/
@[simp, to_additive "Two elements are related by an additive congruence relation `c` iff they
are represented by the same element of the quotient by `c`."]
protected lemma eq {a b : M} : (a : c.quotient) = b ↔ c a b :=
quotient.eq'
/-- The multiplication induced on the quotient by a congruence relation on a type with a
multiplication. -/
@[to_additive "The addition induced on the quotient by an additive congruence relation on a type
with an addition."]
instance has_mul : has_mul c.quotient :=
⟨λ x y, quotient.lift_on₂' x y (λ w z, ((w * z : M) : c.quotient))
$ λ _ _ _ _ h1 h2, c.eq.2 $ c.mul h1 h2⟩
/-- The kernel of the quotient map induced by a congruence relation `c` equals `c`. -/
@[simp, to_additive "The kernel of the quotient map induced by an additive congruence relation
`c` equals `c`."]
lemma mul_ker_mk_eq : mul_ker (coe : M → c.quotient) (λ x y, rfl) = c :=
ext $ λ x y, quotient.eq'
variables {c}
/-- The coercion to the quotient of a congruence relation commutes with multiplication (by
definition). -/
@[simp, to_additive "The coercion to the quotient of an additive congruence relation commutes with
addition (by definition)."]
lemma coe_mul (x y : M) : (↑(x * y) : c.quotient) = ↑x * ↑y := rfl
/-- Definition of the function on the quotient by a congruence relation `c` induced by a function
that is constant on `c`'s equivalence classes. -/
@[simp, to_additive "Definition of the function on the quotient by an additive congruence
relation `c` induced by a function that is constant on `c`'s equivalence classes."]
protected lemma lift_on_coe {β} (c : con M) (f : M → β)
(h : ∀ a b, c a b → f a = f b) (x : M) :
con.lift_on (x : c.quotient) f h = f x := rfl
/-- Makes an isomorphism of quotients by two congruence relations, given that the relations are
equal. -/
@[to_additive "Makes an additive isomorphism of quotients by two additive congruence relations,
given that the relations are equal."]
protected def congr {c d : con M} (h : c = d) : c.quotient ≃* d.quotient :=
{ map_mul' := λ x y, by rcases x; rcases y; refl,
..quotient.congr (equiv.refl M) $ by apply ext_iff.2 h }
-- The complete lattice of congruence relations on a type
/-- For congruence relations `c, d` on a type `M` with a multiplication, `c ≤ d` iff `∀ x y ∈ M`,
`x` is related to `y` by `d` if `x` is related to `y` by `c`. -/
@[to_additive "For additive congruence relations `c, d` on a type `M` with an addition, `c ≤ d` iff
`∀ x y ∈ M`, `x` is related to `y` by `d` if `x` is related to `y` by `c`."]
instance : has_le (con M) := ⟨λ c d, ∀ ⦃x y⦄, c x y → d x y⟩
/-- Definition of `≤` for congruence relations. -/
@[to_additive "Definition of `≤` for additive congruence relations."]
theorem le_def {c d : con M} : c ≤ d ↔ ∀ {x y}, c x y → d x y := iff.rfl
/-- The infimum of a set of congruence relations on a given type with a multiplication. -/
@[to_additive "The infimum of a set of additive congruence relations on a given type with
an addition."]
instance : has_Inf (con M) :=
⟨λ S, ⟨⟨λ x y, ∀ c : con M, c ∈ S → c x y,
⟨λ x c hc, c.refl x, λ _ _ h c hc, c.symm $ h c hc,
λ _ _ _ h1 h2 c hc, c.trans (h1 c hc) $ h2 c hc⟩⟩,
λ _ _ _ _ h1 h2 c hc, c.mul (h1 c hc) $ h2 c hc⟩⟩
/-- The infimum of a set of congruence relations is the same as the infimum of the set's image
under the map to the underlying equivalence relation. -/
@[to_additive "The infimum of a set of additive congruence relations is the same as the infimum of
the set's image under the map to the underlying equivalence relation."]
lemma Inf_to_setoid (S : set (con M)) : (Inf S).to_setoid = Inf (to_setoid '' S) :=
setoid.ext' $ λ x y, ⟨λ h r ⟨c, hS, hr⟩, by rw ←hr; exact h c hS,
λ h c hS, h c.to_setoid ⟨c, hS, rfl⟩⟩
/-- The infimum of a set of congruence relations is the same as the infimum of the set's image
under the map to the underlying binary relation. -/
@[to_additive "The infimum of a set of additive congruence relations is the same as the infimum
of the set's image under the map to the underlying binary relation."]
lemma Inf_def (S : set (con M)) : ⇑(Inf S) = Inf (@set.image (con M) (M → M → Prop) coe_fn S) :=
by { ext, simp only [Inf_image, infi_apply, infi_Prop_eq], refl }
@[to_additive]
instance : partial_order (con M) :=
{ le := (≤),
lt := λ c d, c ≤ d ∧ ¬d ≤ c,
le_refl := λ c _ _, id,
le_trans := λ c1 c2 c3 h1 h2 x y h, h2 $ h1 h,
lt_iff_le_not_le := λ _ _, iff.rfl,
le_antisymm := λ c d hc hd, ext $ λ x y, ⟨λ h, hc h, λ h, hd h⟩ }
/-- The complete lattice of congruence relations on a given type with a multiplication. -/
@[to_additive "The complete lattice of additive congruence relations on a given type with
an addition."]
instance : complete_lattice (con M) :=
{ inf := λ c d, ⟨(c.to_setoid ⊓ d.to_setoid), λ _ _ _ _ h1 h2, ⟨c.mul h1.1 h2.1, d.mul h1.2 h2.2⟩⟩,
inf_le_left := λ _ _ _ _ h, h.1,
inf_le_right := λ _ _ _ _ h, h.2,
le_inf := λ _ _ _ hb hc _ _ h, ⟨hb h, hc h⟩,
top := { mul' := by tauto, ..setoid.complete_lattice.top},
le_top := λ _ _ _ h, trivial,
bot := { mul' := λ _ _ _ _ h1 h2, h1 ▸ h2 ▸ rfl, ..setoid.complete_lattice.bot},
bot_le := λ c x y h, h ▸ c.refl x,
.. complete_lattice_of_Inf (con M) $ assume s,
⟨λ r hr x y h, (h : ∀ r ∈ s, (r : con M) x y) r hr, λ r hr x y h r' hr', hr hr' h⟩ }
/-- The infimum of two congruence relations equals the infimum of the underlying binary
operations. -/
@[to_additive "The infimum of two additive congruence relations equals the infimum of the
underlying binary operations."]
lemma inf_def {c d : con M} : (c ⊓ d).r = c.r ⊓ d.r := rfl
/-- Definition of the infimum of two congruence relations. -/
@[to_additive "Definition of the infimum of two additive congruence relations."]
theorem inf_iff_and {c d : con M} {x y} : (c ⊓ d) x y ↔ c x y ∧ d x y := iff.rfl
/-- The inductively defined smallest congruence relation containing a binary relation `r` equals
the infimum of the set of congruence relations containing `r`. -/
@[to_additive add_con_gen_eq "The inductively defined smallest additive congruence relation
containing a binary relation `r` equals the infimum of the set of additive congruence relations
containing `r`."]
theorem con_gen_eq (r : M → M → Prop) :
con_gen r = Inf {s : con M | ∀ x y, r x y → s x y} :=
le_antisymm
(λ x y H, con_gen.rel.rec_on H (λ _ _ h _ hs, hs _ _ h) (con.refl _) (λ _ _ _, con.symm _)
(λ _ _ _ _ _, con.trans _)
$ λ w x y z _ _ h1 h2 c hc, c.mul (h1 c hc) $ h2 c hc)
(Inf_le (λ _ _, con_gen.rel.of _ _))
/-- The smallest congruence relation containing a binary relation `r` is contained in any
congruence relation containing `r`. -/
@[to_additive add_con_gen_le "The smallest additive congruence relation containing a binary
relation `r` is contained in any additive congruence relation containing `r`."]
theorem con_gen_le {r : M → M → Prop} {c : con M} (h : ∀ x y, r x y → @setoid.r _ c.to_setoid x y) :
con_gen r ≤ c :=
by rw con_gen_eq; exact Inf_le h
/-- Given binary relations `r, s` with `r` contained in `s`, the smallest congruence relation
containing `s` contains the smallest congruence relation containing `r`. -/
@[to_additive add_con_gen_mono "Given binary relations `r, s` with `r` contained in `s`, the
smallest additive congruence relation containing `s` contains the smallest additive congruence
relation containing `r`."]
theorem con_gen_mono {r s : M → M → Prop} (h : ∀ x y, r x y → s x y) :
con_gen r ≤ con_gen s :=
con_gen_le $ λ x y hr, con_gen.rel.of _ _ $ h x y hr
/-- Congruence relations equal the smallest congruence relation in which they are contained. -/
@[simp, to_additive add_con_gen_of_add_con "Additive congruence relations equal the smallest
additive congruence relation in which they are contained."]
lemma con_gen_of_con (c : con M) : con_gen c = c :=
le_antisymm (by rw con_gen_eq; exact Inf_le (λ _ _, id)) con_gen.rel.of
/-- The map sending a binary relation to the smallest congruence relation in which it is
contained is idempotent. -/
@[simp, to_additive add_con_gen_idem "The map sending a binary relation to the smallest additive
congruence relation in which it is contained is idempotent."]
lemma con_gen_idem (r : M → M → Prop) :
con_gen (con_gen r) = con_gen r :=
con_gen_of_con _
/-- The supremum of congruence relations `c, d` equals the smallest congruence relation containing
the binary relation '`x` is related to `y` by `c` or `d`'. -/
@[to_additive sup_eq_add_con_gen "The supremum of additive congruence relations `c, d` equals the
smallest additive congruence relation containing the binary relation '`x` is related to `y`
by `c` or `d`'."]
lemma sup_eq_con_gen (c d : con M) :
c ⊔ d = con_gen (λ x y, c x y ∨ d x y) :=
begin
rw con_gen_eq,
apply congr_arg Inf,
simp only [le_def, or_imp_distrib, ← forall_and_distrib]
end
/-- The supremum of two congruence relations equals the smallest congruence relation containing
the supremum of the underlying binary operations. -/
@[to_additive "The supremum of two additive congruence relations equals the smallest additive
congruence relation containing the supremum of the underlying binary operations."]
lemma sup_def {c d : con M} : c ⊔ d = con_gen (c.r ⊔ d.r) :=
by rw sup_eq_con_gen; refl
/-- The supremum of a set of congruence relations `S` equals the smallest congruence relation
containing the binary relation 'there exists `c ∈ S` such that `x` is related to `y` by
`c`'. -/
@[to_additive Sup_eq_add_con_gen "The supremum of a set of additive congruence relations `S` equals
the smallest additive congruence relation containing the binary relation 'there exists `c ∈ S`
such that `x` is related to `y` by `c`'."]
lemma Sup_eq_con_gen (S : set (con M)) :
Sup S = con_gen (λ x y, ∃ c : con M, c ∈ S ∧ c x y) :=
begin
rw con_gen_eq,
apply congr_arg Inf,
ext,
exact ⟨λ h _ _ ⟨r, hr⟩, h hr.1 hr.2,
λ h r hS _ _ hr, h _ _ ⟨r, hS, hr⟩⟩,
end
/-- The supremum of a set of congruence relations is the same as the smallest congruence relation
containing the supremum of the set's image under the map to the underlying binary relation. -/
@[to_additive "The supremum of a set of additive congruence relations is the same as the smallest
additive congruence relation containing the supremum of the set's image under the map to the
underlying binary relation."]
lemma Sup_def {S : set (con M)} :
Sup S = con_gen (Sup (@set.image (con M) (M → M → Prop) coe_fn S)) :=
begin
rw [Sup_eq_con_gen, Sup_image],
congr' with x y,
simp only [Sup_image, supr_apply, supr_Prop_eq, exists_prop, rel_eq_coe]
end
variables (M)
/-- There is a Galois insertion of congruence relations on a type with a multiplication `M` into
binary relations on `M`. -/
@[to_additive "There is a Galois insertion of additive congruence relations on a type with
an addition `M` into binary relations on `M`."]
protected def gi :
@galois_insertion (M → M → Prop) (con M) _ _ con_gen coe_fn :=
{ choice := λ r h, con_gen r,
gc := λ r c, ⟨λ H _ _ h, H $ con_gen.rel.of _ _ h, λ H, con_gen_of_con c ▸ con_gen_mono H⟩,
le_l_u := λ x, (con_gen_of_con x).symm ▸ le_refl x,
choice_eq := λ _ _, rfl }
variables {M} (c)
/-- Given a function `f`, the smallest congruence relation containing the binary relation on `f`'s
image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)`
by a congruence relation `c`.' -/
@[to_additive "Given a function `f`, the smallest additive congruence relation containing the
binary relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the
elements of `f⁻¹(y)` by an additive congruence relation `c`.'"]
def map_gen (f : M → N) : con N :=
con_gen $ λ x y, ∃ a b, f a = x ∧ f b = y ∧ c a b
/-- Given a surjective multiplicative-preserving function `f` whose kernel is contained in a
congruence relation `c`, the congruence relation on `f`'s codomain defined by '`x ≈ y` iff the
elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by `c`.' -/
@[to_additive "Given a surjective addition-preserving function `f` whose kernel is contained in
an additive congruence relation `c`, the additive congruence relation on `f`'s codomain defined
by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by `c`.'"]
def map_of_surjective (f : M → N) (H : ∀ x y, f (x * y) = f x * f y) (h : mul_ker f H ≤ c)
(hf : surjective f) : con N :=
{ mul' := λ w x y z ⟨a, b, hw, hx, h1⟩ ⟨p, q, hy, hz, h2⟩,
⟨a * p, b * q, by rw [H, hw, hy], by rw [H, hx, hz], c.mul h1 h2⟩,
..c.to_setoid.map_of_surjective f h hf }
/-- A specialization of 'the smallest congruence relation containing a congruence relation `c`
equals `c`'. -/
@[to_additive "A specialization of 'the smallest additive congruence relation containing
an additive congruence relation `c` equals `c`'."]
lemma map_of_surjective_eq_map_gen {c : con M} {f : M → N} (H : ∀ x y, f (x * y) = f x * f y)
(h : mul_ker f H ≤ c) (hf : surjective f) :
c.map_gen f = c.map_of_surjective f H h hf :=
by rw ←con_gen_of_con (c.map_of_surjective f H h hf); refl
/-- Given types with multiplications `M, N` and a congruence relation `c` on `N`, a
multiplication-preserving map `f : M → N` induces a congruence relation on `f`'s domain
defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `c`.' -/
@[to_additive "Given types with additions `M, N` and an additive congruence relation `c` on `N`,
an addition-preserving map `f : M → N` induces an additive congruence relation on `f`'s domain
defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `c`.' "]
def comap (f : M → N) (H : ∀ x y, f (x * y) = f x * f y) (c : con N) : con M :=
{ mul' := λ w x y z h1 h2, show c (f (w * y)) (f (x * z)), by rw [H, H]; exact c.mul h1 h2,
..c.to_setoid.comap f }
@[simp, to_additive] lemma comap_rel {f : M → N} (H : ∀ x y, f (x * y) = f x * f y)
{c : con N} {x y : M} :
comap f H c x y ↔ c (f x) (f y) :=
iff.rfl
section
open _root_.quotient
/-- Given a congruence relation `c` on a type `M` with a multiplication, the order-preserving
bijection between the set of congruence relations containing `c` and the congruence relations
on the quotient of `M` by `c`. -/
@[to_additive "Given an additive congruence relation `c` on a type `M` with an addition,
the order-preserving bijection between the set of additive congruence relations containing `c` and
the additive congruence relations on the quotient of `M` by `c`."]
def correspondence : {d // c ≤ d} ≃o (con c.quotient) :=
{ to_fun := λ d, d.1.map_of_surjective coe _
(by rw mul_ker_mk_eq; exact d.2) $ @exists_rep _ c.to_setoid,
inv_fun := λ d, ⟨comap (coe : M → c.quotient) (λ x y, rfl) d, λ _ _ h,
show d _ _, by rw c.eq.2 h; exact d.refl _ ⟩,
left_inv := λ d, subtype.ext_iff_val.2 $ ext $ λ _ _,
⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in
d.1.trans (d.1.symm $ d.2 $ c.eq.1 hx) $ d.1.trans H $ d.2 $ c.eq.1 hy,
λ h, ⟨_, _, rfl, rfl, h⟩⟩,
right_inv := λ d, let Hm : mul_ker (coe : M → c.quotient) (λ x y, rfl) ≤
comap (coe : M → c.quotient) (λ x y, rfl) d :=
λ x y h, show d _ _, by rw mul_ker_mk_eq at h; exact c.eq.2 h ▸ d.refl _ in
ext $ λ x y, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in hx ▸ hy ▸ H,
con.induction_on₂ x y $ λ w z h, ⟨w, z, rfl, rfl, h⟩⟩,
map_rel_iff' := λ s t, ⟨λ h _ _ hs, let ⟨a, b, hx, hy, ht⟩ := h ⟨_, _, rfl, rfl, hs⟩ in
t.1.trans (t.1.symm $ t.2 $ eq_rel.1 hx) $ t.1.trans ht $ t.2 $ eq_rel.1 hy,
λ h _ _ hs, let ⟨a, b, hx, hy, Hs⟩ := hs in ⟨a, b, hx, hy, h Hs⟩⟩ }
end
end
section mul_one_class
variables {M} [mul_one_class M] [mul_one_class N] [mul_one_class P] (c : con M)
/-- The quotient of a monoid by a congruence relation is a monoid. -/
@[to_additive "The quotient of an `add_monoid` by an additive congruence relation is
an `add_monoid`."]
instance mul_one_class : mul_one_class c.quotient :=
{ one := ((1 : M) : c.quotient),
mul := (*),
mul_one := λ x, quotient.induction_on' x $ λ _, congr_arg coe $ mul_one _,
one_mul := λ x, quotient.induction_on' x $ λ _, congr_arg coe $ one_mul _ }
variables {c}
/-- The 1 of the quotient of a monoid by a congruence relation is the equivalence class of the
monoid's 1. -/
@[simp, to_additive "The 0 of the quotient of an `add_monoid` by an additive congruence relation
is the equivalence class of the `add_monoid`'s 0."]
lemma coe_one : ((1 : M) : c.quotient) = 1 := rfl
variables (M c)
/-- The submonoid of `M × M` defined by a congruence relation on a monoid `M`. -/
@[to_additive "The `add_submonoid` of `M × M` defined by an additive congruence
relation on an `add_monoid` `M`."]
protected def submonoid : submonoid (M × M) :=
{ carrier := { x | c x.1 x.2 },
one_mem' := c.iseqv.1 1,
mul_mem' := λ _ _, c.mul }
variables {M c}
/-- The congruence relation on a monoid `M` from a submonoid of `M × M` for which membership
is an equivalence relation. -/
@[to_additive "The additive congruence relation on an `add_monoid` `M` from
an `add_submonoid` of `M × M` for which membership is an equivalence relation."]
def of_submonoid (N : submonoid (M × M)) (H : equivalence (λ x y, (x, y) ∈ N)) : con M :=
{ r := λ x y, (x, y) ∈ N,
iseqv := H,
mul' := λ _ _ _ _, N.mul_mem }
/-- Coercion from a congruence relation `c` on a monoid `M` to the submonoid of `M × M` whose
elements are `(x, y)` such that `x` is related to `y` by `c`. -/
@[to_additive "Coercion from a congruence relation `c` on an `add_monoid` `M`
to the `add_submonoid` of `M × M` whose elements are `(x, y)` such that `x`
is related to `y` by `c`."]
instance to_submonoid : has_coe (con M) (submonoid (M × M)) := ⟨λ c, c.submonoid M⟩
@[to_additive] lemma mem_coe {c : con M} {x y} :
(x, y) ∈ (↑c : submonoid (M × M)) ↔ (x, y) ∈ c := iff.rfl
@[to_additive]
theorem to_submonoid_inj (c d : con M) (H : (c : submonoid (M × M)) = d) : c = d :=
ext $ λ x y, show (x, y) ∈ (c : submonoid (M × M)) ↔ (x, y) ∈ ↑d, by rw H
@[to_additive]
lemma le_iff {c d : con M} : c ≤ d ↔ (c : submonoid (M × M)) ≤ d :=
⟨λ h x H, h H, λ h x y hc, h $ show (x, y) ∈ c, from hc⟩
/-- The kernel of a monoid homomorphism as a congruence relation. -/
@[to_additive "The kernel of an `add_monoid` homomorphism as an additive congruence relation."]
def ker (f : M →* P) : con M := mul_ker f f.3
/-- The definition of the congruence relation defined by a monoid homomorphism's kernel. -/
@[simp, to_additive "The definition of the additive congruence relation defined by an `add_monoid`
homomorphism's kernel."]
lemma ker_rel (f : M →* P) {x y} : ker f x y ↔ f x = f y := iff.rfl
/-- There exists an element of the quotient of a monoid by a congruence relation (namely 1). -/
@[to_additive "There exists an element of the quotient of an `add_monoid` by a congruence relation
(namely 0)."]
instance quotient.inhabited : inhabited c.quotient := ⟨((1 : M) : c.quotient)⟩
variables (c)
/-- The natural homomorphism from a monoid to its quotient by a congruence relation. -/
@[to_additive "The natural homomorphism from an `add_monoid` to its quotient by an additive
congruence relation."]
def mk' : M →* c.quotient := ⟨coe, rfl, λ _ _, rfl⟩
variables (x y : M)
/-- The kernel of the natural homomorphism from a monoid to its quotient by a congruence
relation `c` equals `c`. -/
@[simp, to_additive "The kernel of the natural homomorphism from an `add_monoid` to its quotient by
an additive congruence relation `c` equals `c`."]
lemma mk'_ker : ker c.mk' = c := ext $ λ _ _, c.eq
variables {c}
/-- The natural homomorphism from a monoid to its quotient by a congruence relation is
surjective. -/
@[to_additive "The natural homomorphism from an `add_monoid` to its quotient by a congruence
relation is surjective."]
lemma mk'_surjective : surjective c.mk' :=
quotient.surjective_quotient_mk'
@[simp, to_additive] lemma coe_mk' : (c.mk' : M → c.quotient) = coe := rfl
/-- The elements related to `x ∈ M`, `M` a monoid, by the kernel of a monoid homomorphism are
those in the preimage of `f(x)` under `f`. -/
@[to_additive "The elements related to `x ∈ M`, `M` an `add_monoid`, by the kernel of
an `add_monoid` homomorphism are those in the preimage of `f(x)` under `f`. "]
lemma ker_apply_eq_preimage {f : M →* P} (x) : (ker f) x = f ⁻¹' {f x} :=
set.ext $ λ x,
⟨λ h, set.mem_preimage.2 $ set.mem_singleton_iff.2 h.symm,
λ h, (set.mem_singleton_iff.1 $ set.mem_preimage.1 h).symm⟩
/-- Given a monoid homomorphism `f : N → M` and a congruence relation `c` on `M`, the congruence
relation induced on `N` by `f` equals the kernel of `c`'s quotient homomorphism composed with
`f`. -/
@[to_additive "Given an `add_monoid` homomorphism `f : N → M` and an additive congruence relation
`c` on `M`, the additive congruence relation induced on `N` by `f` equals the kernel of `c`'s
quotient homomorphism composed with `f`."]
lemma comap_eq {f : N →* M} : comap f f.map_mul c = ker (c.mk'.comp f) :=
ext $ λ x y, show c _ _ ↔ c.mk' _ = c.mk' _, by rw ←c.eq; refl
variables (c) (f : M →* P)
/-- The homomorphism on the quotient of a monoid by a congruence relation `c` induced by a
homomorphism constant on `c`'s equivalence classes. -/
@[to_additive "The homomorphism on the quotient of an `add_monoid` by an additive congruence
relation `c` induced by a homomorphism constant on `c`'s equivalence classes."]
def lift (H : c ≤ ker f) : c.quotient →* P :=
{ to_fun := λ x, con.lift_on x f $ λ _ _ h, H h,
map_one' := by rw ←f.map_one; refl,
map_mul' := λ x y, con.induction_on₂ x y $ λ m n, f.map_mul m n ▸ rfl }
variables {c f}
/-- The diagram describing the universal property for quotients of monoids commutes. -/
@[to_additive "The diagram describing the universal property for quotients of `add_monoid`s
commutes."]
lemma lift_mk' (H : c ≤ ker f) (x) :
c.lift f H (c.mk' x) = f x := rfl
/-- The diagram describing the universal property for quotients of monoids commutes. -/
@[simp, to_additive "The diagram describing the universal property for quotients of `add_monoid`s
commutes."]
lemma lift_coe (H : c ≤ ker f) (x : M) :
c.lift f H x = f x := rfl
/-- The diagram describing the universal property for quotients of monoids commutes. -/
@[simp, to_additive "The diagram describing the universal property for quotients of `add_monoid`s
commutes."]
theorem lift_comp_mk' (H : c ≤ ker f) :
(c.lift f H).comp c.mk' = f := by ext; refl
/-- Given a homomorphism `f` from the quotient of a monoid by a congruence relation, `f` equals the
homomorphism on the quotient induced by `f` composed with the natural map from the monoid to
the quotient. -/
@[simp, to_additive "Given a homomorphism `f` from the quotient of an `add_monoid` by an additive
congruence relation, `f` equals the homomorphism on the quotient induced by `f` composed with the
natural map from the `add_monoid` to the quotient."]
lemma lift_apply_mk' (f : c.quotient →* P) :
c.lift (f.comp c.mk') (λ x y h, show f ↑x = f ↑y, by rw c.eq.2 h) = f :=
by ext; rcases x; refl
/-- Homomorphisms on the quotient of a monoid by a congruence relation are equal if they
are equal on elements that are coercions from the monoid. -/
@[to_additive "Homomorphisms on the quotient of an `add_monoid` by an additive congruence relation
are equal if they are equal on elements that are coercions from the `add_monoid`."]
lemma lift_funext (f g : c.quotient →* P) (h : ∀ a : M, f a = g a) : f = g :=
begin
rw [←lift_apply_mk' f, ←lift_apply_mk' g],
congr' 1,
exact monoid_hom.ext_iff.2 h,
end
/-- The uniqueness part of the universal property for quotients of monoids. -/
@[to_additive "The uniqueness part of the universal property for quotients of `add_monoid`s."]
theorem lift_unique (H : c ≤ ker f) (g : c.quotient →* P)
(Hg : g.comp c.mk' = f) : g = c.lift f H :=
lift_funext g (c.lift f H) $ λ x, by { subst f, refl }
/-- Given a congruence relation `c` on a monoid and a homomorphism `f` constant on `c`'s
equivalence classes, `f` has the same image as the homomorphism that `f` induces on the
quotient. -/
@[to_additive "Given an additive congruence relation `c` on an `add_monoid` and a homomorphism `f`
constant on `c`'s equivalence classes, `f` has the same image as the homomorphism that `f` induces
on the quotient."]
theorem lift_range (H : c ≤ ker f) : (c.lift f H).mrange = f.mrange :=
submonoid.ext $ λ x, ⟨by rintros ⟨⟨y⟩, hy⟩; exact ⟨y, hy⟩, λ ⟨y, hy⟩, ⟨↑y, hy⟩⟩
/-- Surjective monoid homomorphisms constant on a congruence relation `c`'s equivalence classes
induce a surjective homomorphism on `c`'s quotient. -/
@[to_additive "Surjective `add_monoid` homomorphisms constant on an additive congruence
relation `c`'s equivalence classes induce a surjective homomorphism on `c`'s quotient."]
lemma lift_surjective_of_surjective (h : c ≤ ker f) (hf : surjective f) :
surjective (c.lift f h) :=
λ y, exists.elim (hf y) $ λ w hw, ⟨w, (lift_mk' h w).symm ▸ hw⟩
variables (c f)
/-- Given a monoid homomorphism `f` from `M` to `P`, the kernel of `f` is the unique congruence
relation on `M` whose induced map from the quotient of `M` to `P` is injective. -/
@[to_additive "Given an `add_monoid` homomorphism `f` from `M` to `P`, the kernel of `f`
is the unique additive congruence relation on `M` whose induced map from the quotient of `M`
to `P` is injective."]
lemma ker_eq_lift_of_injective (H : c ≤ ker f) (h : injective (c.lift f H)) :
ker f = c :=
to_setoid_inj $ ker_eq_lift_of_injective f H h
variables {c}
/-- The homomorphism induced on the quotient of a monoid by the kernel of a monoid homomorphism. -/
@[to_additive "The homomorphism induced on the quotient of an `add_monoid` by the kernel
of an `add_monoid` homomorphism."]
def ker_lift : (ker f).quotient →* P :=
(ker f).lift f $ λ _ _, id
variables {f}
/-- The diagram described by the universal property for quotients of monoids, when the congruence
relation is the kernel of the homomorphism, commutes. -/
@[simp, to_additive "The diagram described by the universal property for quotients
of `add_monoid`s, when the additive congruence relation is the kernel of the homomorphism,
commutes."]
lemma ker_lift_mk (x : M) : ker_lift f x = f x := rfl
/-- Given a monoid homomorphism `f`, the induced homomorphism on the quotient by `f`'s kernel has
the same image as `f`. -/
@[simp, to_additive "Given an `add_monoid` homomorphism `f`, the induced homomorphism
on the quotient by `f`'s kernel has the same image as `f`."]
lemma ker_lift_range_eq : (ker_lift f).mrange = f.mrange :=
lift_range $ λ _ _, id
/-- A monoid homomorphism `f` induces an injective homomorphism on the quotient by `f`'s kernel. -/
@[to_additive "An `add_monoid` homomorphism `f` induces an injective homomorphism on the quotient
by `f`'s kernel."]
lemma ker_lift_injective (f : M →* P) : injective (ker_lift f) :=
λ x y, quotient.induction_on₂' x y $ λ _ _, (ker f).eq.2
/-- Given congruence relations `c, d` on a monoid such that `d` contains `c`, `d`'s quotient
map induces a homomorphism from the quotient by `c` to the quotient by `d`. -/
@[to_additive "Given additive congruence relations `c, d` on an `add_monoid` such that `d`
contains `c`, `d`'s quotient map induces a homomorphism from the quotient by `c` to the quotient
by `d`."]
def map (c d : con M) (h : c ≤ d) : c.quotient →* d.quotient :=
c.lift d.mk' $ λ x y hc, show (ker d.mk') x y, from
(mk'_ker d).symm ▸ h hc
/-- Given congruence relations `c, d` on a monoid such that `d` contains `c`, the definition of
the homomorphism from the quotient by `c` to the quotient by `d` induced by `d`'s quotient
map. -/
@[to_additive "Given additive congruence relations `c, d` on an `add_monoid` such that `d`
contains `c`, the definition of the homomorphism from the quotient by `c` to the quotient by `d`
induced by `d`'s quotient map."]
lemma map_apply {c d : con M} (h : c ≤ d) (x) :
c.map d h x = c.lift d.mk' (λ x y hc, d.eq.2 $ h hc) x := rfl
variables (c)
/-- The first isomorphism theorem for monoids. -/
@[to_additive "The first isomorphism theorem for `add_monoid`s."]
noncomputable def quotient_ker_equiv_range (f : M →* P) : (ker f).quotient ≃* f.mrange :=
{ map_mul' := monoid_hom.map_mul _,
..equiv.of_bijective
((@mul_equiv.to_monoid_hom (ker_lift f).mrange _ _ _
$ mul_equiv.submonoid_congr ker_lift_range_eq).comp (ker_lift f).mrange_restrict) $
(equiv.bijective _).comp
⟨λ x y h, ker_lift_injective f $ by rcases x; rcases y; injections,
λ ⟨w, z, hz⟩, ⟨z, by rcases hz; rcases _x; refl⟩⟩ }
/-- The first isomorphism theorem for monoids in the case of a homomorphism with right inverse. -/
@[to_additive "The first isomorphism theorem for `add_monoid`s in the case of a homomorphism
with right inverse.", simps]
def quotient_ker_equiv_of_right_inverse (f : M →* P) (g : P → M)
(hf : function.right_inverse g f) :
(ker f).quotient ≃* P :=
{ to_fun := ker_lift f,
inv_fun := coe ∘ g,
left_inv := λ x, ker_lift_injective _ (by rw [function.comp_app, ker_lift_mk, hf]),
right_inv := hf,
.. ker_lift f }
/-- The first isomorphism theorem for monoids in the case of a surjective homomorphism.
For a `computable` version, see `con.quotient_ker_equiv_of_right_inverse`.
-/
@[to_additive "The first isomorphism theorem for `add_monoid`s in the case of a surjective
homomorphism.
For a `computable` version, see `add_con.quotient_ker_equiv_of_right_inverse`.
"]
noncomputable def quotient_ker_equiv_of_surjective (f : M →* P) (hf : surjective f) :
(ker f).quotient ≃* P :=
quotient_ker_equiv_of_right_inverse _ _ hf.has_right_inverse.some_spec
/-- The second isomorphism theorem for monoids. -/
@[to_additive "The second isomorphism theorem for `add_monoid`s."]
noncomputable def comap_quotient_equiv (f : N →* M) :
(comap f f.map_mul c).quotient ≃* (c.mk'.comp f).mrange :=
(con.congr comap_eq).trans $ quotient_ker_equiv_range $ c.mk'.comp f
/-- The third isomorphism theorem for monoids. -/
@[to_additive "The third isomorphism theorem for `add_monoid`s."]
def quotient_quotient_equiv_quotient (c d : con M) (h : c ≤ d) :
(ker (c.map d h)).quotient ≃* d.quotient :=
{ map_mul' := λ x y, con.induction_on₂ x y $ λ w z, con.induction_on₂ w z $ λ a b,
show _ = d.mk' a * d.mk' b, by rw ←d.mk'.map_mul; refl,
..quotient_quotient_equiv_quotient c.to_setoid d.to_setoid h }
end mul_one_class
section monoids
/-- Multiplicative congruence relations preserve natural powers. -/
@[to_additive add_con.nsmul "Additive congruence relations preserve natural scaling."]
protected lemma pow {M : Type*} [monoid M] (c : con M) :
∀ (n : ℕ) {w x}, c w x → c (w ^ n) (x ^ n)
| 0 w x h := by simpa using c.refl _
| (nat.succ n) w x h := by simpa [pow_succ] using c.mul h (pow n h)
@[to_additive]
instance {M : Type*} [mul_one_class M] (c : con M) : has_one c.quotient :=
{ one := ((1 : M) : c.quotient) }
instance _root_.add_con.quotient.has_nsmul
{M : Type*} [add_monoid M] (c : add_con M) : has_scalar ℕ c.quotient :=
{ smul := λ n x, quotient.lift_on' x (λ w, ((n • w : M) : c.quotient))
$ λ x y h, c.eq.2 $ c.nsmul n h}
@[to_additive add_con.quotient.has_nsmul]
instance {M : Type*} [monoid M] (c : con M) : has_pow c.quotient ℕ :=
{ pow := λ x n, quotient.lift_on' x (λ w, ((w ^ n : M) : c.quotient))
$ λ x y h, c.eq.2 $ c.pow n h}
/-- The quotient of a semigroup by a congruence relation is a semigroup. -/
@[to_additive "The quotient of an `add_semigroup` by an additive congruence relation is
an `add_semigroup`."]
instance semigroup {M : Type*} [semigroup M] (c : con M) : semigroup c.quotient :=
function.surjective.semigroup _ quotient.surjective_quotient_mk' (λ _ _, rfl)
/-- The quotient of a commutative semigroup by a congruence relation is a semigroup. -/
@[to_additive "The quotient of an `add_comm_semigroup` by an additive congruence relation is
an `add_semigroup`."]
instance comm_semigroup {M : Type*} [comm_semigroup M] (c : con M) : comm_semigroup c.quotient :=
function.surjective.comm_semigroup _ quotient.surjective_quotient_mk' (λ _ _, rfl)
/-- The quotient of a monoid by a congruence relation is a monoid. -/
@[to_additive "The quotient of an `add_monoid` by an additive congruence relation is
an `add_monoid`."]
instance monoid {M : Type*} [monoid M] (c : con M) : monoid c.quotient :=
function.surjective.monoid_pow _ quotient.surjective_quotient_mk' rfl (λ _ _, rfl) (λ _ _, rfl)
/-- The quotient of a `comm_monoid` by a congruence relation is a `comm_monoid`. -/
@[to_additive "The quotient of an `add_comm_monoid` by an additive congruence
relation is an `add_comm_monoid`."]
instance comm_monoid {M : Type*} [comm_monoid M] (c : con M) :
comm_monoid c.quotient :=
{ ..c.comm_semigroup, ..c.monoid}
end monoids
section groups
variables {M} [group M] [group N] [group P] (c : con M)
/-- Multiplicative congruence relations preserve inversion. -/
@[to_additive "Additive congruence relations preserve negation."]
protected lemma inv : ∀ {w x}, c w x → c w⁻¹ x⁻¹ :=
λ x y h, by simpa using c.symm (c.mul (c.mul (c.refl x⁻¹) h) (c.refl y⁻¹))
/-- Multiplicative congruence relations preserve division. -/
@[to_additive "Additive congruence relations preserve subtraction."]
protected lemma div : ∀ {w x y z}, c w x → c y z → c (w / y) (x / z) :=
λ w x y z h1 h2, by simpa only [div_eq_mul_inv] using c.mul h1 (c.inv h2)
/-- Multiplicative congruence relations preserve integer powers. -/
@[to_additive add_con.zsmul "Additive congruence relations preserve integer scaling."]
protected lemma zpow : ∀ (n : ℤ) {w x}, c w x → c (w ^ n) (x ^ n)
| (int.of_nat n) w x h := by simpa only [zpow_of_nat] using c.pow _ h
| -[1+ n] w x h := by simpa only [zpow_neg_succ_of_nat] using c.inv (c.pow _ h)
/-- The inversion induced on the quotient by a congruence relation on a type with a
inversion. -/
@[to_additive "The negation induced on the quotient by an additive congruence relation on a type
with an negation."]
instance has_inv : has_inv c.quotient :=
⟨λ x, quotient.lift_on' x (λ w, ((w⁻¹ : M) : c.quotient))
$ λ x y h, c.eq.2 $ c.inv h⟩
/-- The division induced on the quotient by a congruence relation on a type with a
division. -/
@[to_additive "The subtraction induced on the quotient by an additive congruence relation on a type
with a subtraction."]
instance has_div : has_div c.quotient :=
⟨λ x y, quotient.lift_on₂' x y (λ w z, ((w / z : M) : c.quotient))
$ λ _ _ _ _ h1 h2, c.eq.2 $ c.div h1 h2⟩
/-- The integer scaling induced on the quotient by a congruence relation on a type with a
subtraction. -/
instance _root_.add_con.quotient.has_zsmul
{M : Type*} [add_group M] (c : add_con M) : has_scalar ℤ c.quotient :=
⟨λ z x, quotient.lift_on' x (λ w, ((z • w : M) : c.quotient))
$ λ x y h, c.eq.2 $ c.zsmul z h⟩
/-- The integer power induced on the quotient by a congruence relation on a type with a
division. -/
@[to_additive add_con.quotient.has_zsmul]
instance has_zpow : has_pow c.quotient ℤ :=
⟨λ x z, quotient.lift_on' x (λ w, ((w ^ z : M) : c.quotient))
$ λ x y h, c.eq.2 $ c.zpow z h⟩
/-- The quotient of a group by a congruence relation is a group. -/
@[to_additive "The quotient of an `add_group` by an additive congruence relation is
an `add_group`."]
instance group : group c.quotient :=
function.surjective.group_pow _ quotient.surjective_quotient_mk' rfl
(λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
end groups
section units
variables {α : Type*} [monoid M] {c : con M}
/-- In order to define a function `(con.quotient c)ˣ → α` on the units of `con.quotient c`,
where `c : con M` is a multiplicative congruence on a monoid, it suffices to define a function `f`
that takes elements `x y : M` with proofs of `c (x * y) 1` and `c (y * x) 1`, and returns an element
of `α` provided that `f x y _ _ = f x' y' _ _` whenever `c x x'` and `c y y'`. -/
@[to_additive lift_on_add_units] def lift_on_units (u : units c.quotient)
(f : Π (x y : M), c (x * y) 1 → c (y * x) 1 → α)
(Hf : ∀ x y hxy hyx x' y' hxy' hyx', c x x' → c y y' → f x y hxy hyx = f x' y' hxy' hyx') :
α :=
begin
refine @con.hrec_on₂ M M _ _ c c (λ x y, x * y = 1 → y * x = 1 → α)
(u : c.quotient) (↑u⁻¹ : c.quotient)
(λ (x y : M) (hxy : (x * y : c.quotient) = 1) (hyx : (y * x : c.quotient) = 1),
f x y (c.eq.1 hxy) (c.eq.1 hyx)) (λ x y x' y' hx hy, _) u.3 u.4,
ext1, { rw [c.eq.2 hx, c.eq.2 hy] },
rintro Hxy Hxy' -,
ext1, { rw [c.eq.2 hx, c.eq.2 hy] },
rintro Hyx Hyx' -,
exact heq_of_eq (Hf _ _ _ _ _ _ _ _ hx hy)
end
/-- In order to define a function `(con.quotient c)ˣ → α` on the units of `con.quotient c`,
where `c : con M` is a multiplicative congruence on a monoid, it suffices to define a function `f`
that takes elements `x y : M` with proofs of `c (x * y) 1` and `c (y * x) 1`, and returns an element
of `α` provided that `f x y _ _ = f x' y' _ _` whenever `c x x'` and `c y y'`. -/
add_decl_doc add_con.lift_on_add_units
@[simp, to_additive]
lemma lift_on_units_mk (f : Π (x y : M), c (x * y) 1 → c (y * x) 1 → α)
(Hf : ∀ x y hxy hyx x' y' hxy' hyx', c x x' → c y y' → f x y hxy hyx = f x' y' hxy' hyx')
(x y : M) (hxy hyx) :
lift_on_units ⟨(x : c.quotient), y, hxy, hyx⟩ f Hf = f x y (c.eq.1 hxy) (c.eq.1 hyx) :=
rfl
@[elab_as_eliminator, to_additive induction_on_add_units]
lemma induction_on_units {p : units c.quotient → Prop} (u : units c.quotient)
(H : ∀ (x y : M) (hxy : c (x * y) 1) (hyx : c (y * x) 1), p ⟨x, y, c.eq.2 hxy, c.eq.2 hyx⟩) :
p u :=
begin
rcases u with ⟨⟨x⟩, ⟨y⟩, h₁, h₂⟩,
exact H x y (c.eq.1 h₁) (c.eq.1 h₂)
end
end units
end con
|
ccea730f6c627027fac713c9884370c1e504ee9a | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/ring_theory/discrete_valuation_ring.lean | 39e408c5cf81177ec136ff55527a4b9543427b07 | [
"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 | 16,260 | lean | /-
Copyright (c) 2020 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard
-/
import ring_theory.principal_ideal_domain
import order.conditionally_complete_lattice
import ring_theory.multiplicity
import ring_theory.valuation.basic
/-!
# Discrete valuation rings
This file defines discrete valuation rings (DVRs) and develops a basic interface
for them.
## Important definitions
There are various definitions of a DVR in the literature; we define a DVR to be a local PID
which is not a field (the first definition in Wikipedia) and prove that this is equivalent
to being a PID with a unique non-zero prime ideal (the definition in Serre's
book "Local Fields").
Let R be an integral domain, assumed to be a principal ideal ring and a local ring.
* `discrete_valuation_ring R` : a predicate expressing that R is a DVR
### Definitions
* `add_val R : add_valuation R enat` : the additive valuation on a DVR.
## Implementation notes
It's a theorem that an element of a DVR is a uniformizer if and only if it's irreducible.
We do not hence define `uniformizer` at all, because we can use `irreducible` instead.
## Tags
discrete valuation ring
-/
open_locale classical
universe u
open ideal local_ring
/-- An integral domain is a *discrete valuation ring* (DVR) if it's a local PID which
is not a field. -/
class discrete_valuation_ring (R : Type u) [integral_domain R]
extends is_principal_ideal_ring R, local_ring R : Prop :=
(not_a_field' : maximal_ideal R ≠ ⊥)
namespace discrete_valuation_ring
variables (R : Type u) [integral_domain R] [discrete_valuation_ring R]
lemma not_a_field : maximal_ideal R ≠ ⊥ := not_a_field'
variable {R}
open principal_ideal_ring
/-- An element of a DVR is irreducible iff it is a uniformizer, that is, generates the
maximal ideal of R -/
theorem irreducible_iff_uniformizer (ϖ : R) :
irreducible ϖ ↔ maximal_ideal R = ideal.span {ϖ} :=
⟨λ hϖ, (eq_maximal_ideal (is_maximal_of_irreducible hϖ)).symm,
begin
intro h,
have h2 : ¬(is_unit ϖ) := show ϖ ∈ maximal_ideal R,
from h.symm ▸ submodule.mem_span_singleton_self ϖ,
refine ⟨h2, _⟩,
intros a b hab,
by_contra h,
push_neg at h,
obtain ⟨ha : a ∈ maximal_ideal R, hb : b ∈ maximal_ideal R⟩ := h,
rw h at ha hb,
rw mem_span_singleton' at ha hb,
rcases ha with ⟨a, rfl⟩,
rcases hb with ⟨b, rfl⟩,
rw (show a * ϖ * (b * ϖ) = ϖ * (ϖ * (a * b)), by ring) at hab,
have h3 := eq_zero_of_mul_eq_self_right _ hab.symm,
{ apply not_a_field R,
simp [h, h3] },
{ intro hh, apply h2,
refine is_unit_of_dvd_one ϖ _,
use a * b, exact hh.symm }
end⟩
variable (R)
/-- Uniformisers exist in a DVR -/
theorem exists_irreducible : ∃ ϖ : R, irreducible ϖ :=
by {simp_rw [irreducible_iff_uniformizer],
exact (is_principal_ideal_ring.principal $ maximal_ideal R).principal}
/-- Uniformisers exist in a DVR -/
theorem exists_prime : ∃ ϖ : R, prime ϖ :=
(exists_irreducible R).imp (λ _, principal_ideal_ring.irreducible_iff_prime.1)
/-- an integral domain is a DVR iff it's a PID with a unique non-zero prime ideal -/
theorem iff_pid_with_one_nonzero_prime (R : Type u) [integral_domain R] :
discrete_valuation_ring R ↔ is_principal_ideal_ring R ∧ ∃! P : ideal R, P ≠ ⊥ ∧ is_prime P :=
begin
split,
{ intro RDVR,
rcases id RDVR with ⟨RPID, Rlocal, Rnotafield⟩,
split, assumption,
resetI,
use local_ring.maximal_ideal R,
split, split,
{ assumption },
{ apply_instance } ,
{ rintro Q ⟨hQ1, hQ2⟩,
obtain ⟨q, rfl⟩ := (is_principal_ideal_ring.principal Q).1,
have hq : q ≠ 0,
{ rintro rfl,
apply hQ1,
simp },
erw span_singleton_prime hq at hQ2,
replace hQ2 := irreducible_of_prime hQ2,
rw irreducible_iff_uniformizer at hQ2,
exact hQ2.symm } },
{ rintro ⟨RPID, Punique⟩,
haveI : local_ring R := local_of_unique_nonzero_prime R Punique,
refine {not_a_field' := _},
rcases Punique with ⟨P, ⟨hP1, hP2⟩, hP3⟩,
have hPM : P ≤ maximal_ideal R := le_maximal_ideal (hP2.1),
intro h, rw [h, le_bot_iff] at hPM, exact hP1 hPM }
end
lemma associated_of_irreducible {a b : R} (ha : irreducible a) (hb : irreducible b) :
associated a b :=
begin
rw irreducible_iff_uniformizer at ha hb,
rw [←span_singleton_eq_span_singleton, ←ha, hb],
end
end discrete_valuation_ring
namespace discrete_valuation_ring
variable (R : Type*)
/-- Alternative characterisation of discrete valuation rings. -/
def has_unit_mul_pow_irreducible_factorization [integral_domain R] : Prop :=
∃ p : R, irreducible p ∧ ∀ {x : R}, x ≠ 0 → ∃ (n : ℕ), associated (p ^ n) x
namespace has_unit_mul_pow_irreducible_factorization
variables {R} [integral_domain R] (hR : has_unit_mul_pow_irreducible_factorization R)
include hR
lemma unique_irreducible ⦃p q : R⦄ (hp : irreducible p) (hq : irreducible q) :
associated p q :=
begin
rcases hR with ⟨ϖ, hϖ, hR⟩,
suffices : ∀ {p : R} (hp : irreducible p), associated p ϖ,
{ apply associated.trans (this hp) (this hq).symm, },
clear hp hq p q,
intros p hp,
obtain ⟨n, hn⟩ := hR hp.ne_zero,
have : irreducible (ϖ ^ n) := irreducible_of_associated hn.symm hp,
rcases lt_trichotomy n 1 with (H|rfl|H),
{ obtain rfl : n = 0, { clear hn this, revert H n, exact dec_trivial },
simpa only [not_irreducible_one, pow_zero] using this, },
{ simpa only [pow_one] using hn.symm, },
{ obtain ⟨n, rfl⟩ : ∃ k, n = 1 + k + 1 := nat.exists_eq_add_of_lt H,
rw pow_succ at this,
rcases this.is_unit_or_is_unit rfl with H0|H0,
{ exact (hϖ.not_unit H0).elim, },
{ rw [add_comm, pow_succ] at H0,
exact (hϖ.not_unit (is_unit_of_mul_is_unit_left H0)).elim } }
end
/-- An integral domain in which there is an irreducible element `p`
such that every nonzero element is associated to a power of `p` is a unique factorization domain.
See `discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization`. -/
theorem to_unique_factorization_monoid : unique_factorization_monoid R :=
let p := classical.some hR in
let spec := classical.some_spec hR in
unique_factorization_monoid.of_exists_prime_factors $ λ x hx,
begin
use multiset.repeat p (classical.some (spec.2 hx)),
split,
{ intros q hq,
have hpq := multiset.eq_of_mem_repeat hq,
rw hpq,
refine ⟨spec.1.ne_zero, spec.1.not_unit, _⟩,
intros a b h,
by_cases ha : a = 0,
{ rw ha, simp only [true_or, dvd_zero], },
by_cases hb : b = 0,
{ rw hb, simp only [or_true, dvd_zero], },
obtain ⟨m, u, rfl⟩ := spec.2 ha,
rw [mul_assoc, mul_left_comm, is_unit.dvd_mul_left _ _ _ (units.is_unit _)] at h,
rw is_unit.dvd_mul_right (units.is_unit _),
by_cases hm : m = 0,
{ simp only [hm, one_mul, pow_zero] at h ⊢, right, exact h },
left,
obtain ⟨m, rfl⟩ := nat.exists_eq_succ_of_ne_zero hm,
apply dvd_mul_of_dvd_left (dvd_refl _) _ },
{ rw [multiset.prod_repeat], exact (classical.some_spec (spec.2 hx)), }
end
omit hR
lemma of_ufd_of_unique_irreducible [unique_factorization_monoid R]
(h₁ : ∃ p : R, irreducible p)
(h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) :
has_unit_mul_pow_irreducible_factorization R :=
begin
obtain ⟨p, hp⟩ := h₁,
refine ⟨p, hp, _⟩,
intros x hx,
cases wf_dvd_monoid.exists_factors x hx with fx hfx,
refine ⟨fx.card, _⟩,
have H := hfx.2,
rw ← associates.mk_eq_mk_iff_associated at H ⊢,
rw [← H, ← associates.prod_mk, associates.mk_pow, ← multiset.prod_repeat],
congr' 1,
symmetry,
rw multiset.eq_repeat,
simp only [true_and, and_imp, multiset.card_map, eq_self_iff_true,
multiset.mem_map, exists_imp_distrib],
rintros _ q hq rfl,
rw associates.mk_eq_mk_iff_associated,
apply h₂ (hfx.1 _ hq) hp,
end
end has_unit_mul_pow_irreducible_factorization
lemma aux_pid_of_ufd_of_unique_irreducible
(R : Type u) [integral_domain R] [unique_factorization_monoid R]
(h₁ : ∃ p : R, irreducible p)
(h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) :
is_principal_ideal_ring R :=
begin
constructor,
intro I,
by_cases I0 : I = ⊥, { rw I0, use 0, simp only [set.singleton_zero, submodule.span_zero], },
obtain ⟨x, hxI, hx0⟩ : ∃ x ∈ I, x ≠ (0:R) := I.ne_bot_iff.mp I0,
obtain ⟨p, hp, H⟩ :=
has_unit_mul_pow_irreducible_factorization.of_ufd_of_unique_irreducible h₁ h₂,
have ex : ∃ n : ℕ, p ^ n ∈ I,
{ obtain ⟨n, u, rfl⟩ := H hx0,
refine ⟨n, _⟩,
simpa only [units.mul_inv_cancel_right] using I.mul_mem_right ↑u⁻¹ hxI, },
constructor,
use p ^ (nat.find ex),
show I = ideal.span _,
apply le_antisymm,
{ intros r hr,
by_cases hr0 : r = 0,
{ simp only [hr0, submodule.zero_mem], },
obtain ⟨n, u, rfl⟩ := H hr0,
simp only [mem_span_singleton, units.is_unit, is_unit.dvd_mul_right],
apply pow_dvd_pow,
apply nat.find_min',
simpa only [units.mul_inv_cancel_right] using I.mul_mem_right ↑u⁻¹ hr, },
{ erw submodule.span_singleton_le_iff_mem,
exact nat.find_spec ex, },
end
/--
A unique factorization domain with at least one irreducible element
in which all irreducible elements are associated
is a discrete valuation ring.
-/
lemma of_ufd_of_unique_irreducible {R : Type u} [integral_domain R] [unique_factorization_monoid R]
(h₁ : ∃ p : R, irreducible p)
(h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) :
discrete_valuation_ring R :=
begin
rw iff_pid_with_one_nonzero_prime,
haveI PID : is_principal_ideal_ring R := aux_pid_of_ufd_of_unique_irreducible R h₁ h₂,
obtain ⟨p, hp⟩ := h₁,
refine ⟨PID, ⟨ideal.span {p}, ⟨_, _⟩, _⟩⟩,
{ rw submodule.ne_bot_iff,
refine ⟨p, ideal.mem_span_singleton.mpr (dvd_refl p), hp.ne_zero⟩, },
{ rwa [ideal.span_singleton_prime hp.ne_zero,
← unique_factorization_monoid.irreducible_iff_prime], },
{ intro I,
rw ← submodule.is_principal.span_singleton_generator I,
rintro ⟨I0, hI⟩,
apply span_singleton_eq_span_singleton.mpr,
apply h₂ _ hp,
erw [ne.def, span_singleton_eq_bot] at I0,
rwa [unique_factorization_monoid.irreducible_iff_prime, ← ideal.span_singleton_prime I0], },
end
/--
An integral domain in which there is an irreducible element `p`
such that every nonzero element is associated to a power of `p`
is a discrete valuation ring.
-/
lemma of_has_unit_mul_pow_irreducible_factorization {R : Type u} [integral_domain R]
(hR : has_unit_mul_pow_irreducible_factorization R) :
discrete_valuation_ring R :=
begin
letI : unique_factorization_monoid R := hR.to_unique_factorization_monoid,
apply of_ufd_of_unique_irreducible _ hR.unique_irreducible,
unfreezingI { obtain ⟨p, hp, H⟩ := hR, exact ⟨p, hp⟩, },
end
section
variables [integral_domain R] [discrete_valuation_ring R]
variable {R}
lemma associated_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) :
∃ (n : ℕ), associated x (ϖ ^ n) :=
begin
have : wf_dvd_monoid R := is_noetherian_ring.wf_dvd_monoid,
cases wf_dvd_monoid.exists_factors x hx with fx hfx,
unfreezingI { use fx.card },
have H := hfx.2,
rw ← associates.mk_eq_mk_iff_associated at H ⊢,
rw [← H, ← associates.prod_mk, associates.mk_pow, ← multiset.prod_repeat],
congr' 1,
rw multiset.eq_repeat,
simp only [true_and, and_imp, multiset.card_map, eq_self_iff_true,
multiset.mem_map, exists_imp_distrib],
rintros _ _ _ rfl,
rw associates.mk_eq_mk_iff_associated,
refine associated_of_irreducible _ _ hirr,
apply hfx.1,
assumption
end
lemma eq_unit_mul_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) :
∃ (n : ℕ) (u : units R), x = u * ϖ ^ n :=
begin
obtain ⟨n, hn⟩ := associated_pow_irreducible hx hirr,
obtain ⟨u, rfl⟩ := hn.symm,
use [n, u],
apply mul_comm,
end
open submodule.is_principal
lemma ideal_eq_span_pow_irreducible {s : ideal R} (hs : s ≠ ⊥) {ϖ : R} (hirr : irreducible ϖ) :
∃ n : ℕ, s = ideal.span {ϖ ^ n} :=
begin
have gen_ne_zero : generator s ≠ 0,
{ rw [ne.def, ← eq_bot_iff_generator_eq_zero], assumption },
rcases associated_pow_irreducible gen_ne_zero hirr with ⟨n, u, hnu⟩,
use n,
have : span _ = _ := span_singleton_generator s,
rw [← this, ← hnu, span_singleton_eq_span_singleton],
use u
end
lemma unit_mul_pow_congr_pow {p q : R} (hp : irreducible p) (hq : irreducible q)
(u v : units R) (m n : ℕ) (h : ↑u * p ^ m = v * q ^ n) :
m = n :=
begin
have key : associated (multiset.repeat p m).prod (multiset.repeat q n).prod,
{ rw [multiset.prod_repeat, multiset.prod_repeat, associated],
refine ⟨u * v⁻¹, _⟩,
simp only [units.coe_mul],
rw [mul_left_comm, ← mul_assoc, h, mul_right_comm, units.mul_inv, one_mul], },
have := multiset.card_eq_card_of_rel (unique_factorization_monoid.factors_unique _ _ key),
{ simpa only [multiset.card_repeat] },
all_goals
{ intros x hx, replace hx := multiset.eq_of_mem_repeat hx,
unfreezingI { subst hx, assumption } },
end
lemma unit_mul_pow_congr_unit {ϖ : R} (hirr : irreducible ϖ) (u v : units R) (m n : ℕ)
(h : ↑u * ϖ ^ m = v * ϖ ^ n) :
u = v :=
begin
obtain rfl : m = n := unit_mul_pow_congr_pow hirr hirr u v m n h,
rw ← sub_eq_zero at h,
rw [← sub_mul, mul_eq_zero] at h,
cases h,
{ rw sub_eq_zero at h, exact_mod_cast h },
{ apply (hirr.ne_zero (pow_eq_zero h)).elim, }
end
/-!
## The additive valuation on a DVR
-/
open multiplicity
/-- The `enat`-valued additive valuation on a DVR -/
noncomputable def add_val (R : Type u) [integral_domain R] [discrete_valuation_ring R] :
add_valuation R enat :=
add_valuation (classical.some_spec (exists_prime R))
lemma add_val_def (r : R) (u : units R) {ϖ : R} (hϖ : irreducible ϖ) (n : ℕ) (hr : r = u * ϖ ^ n) :
add_val R r = n :=
by rw [add_val, add_valuation_apply, hr,
eq_of_associated_left (associated_of_irreducible R hϖ
(irreducible_of_prime (classical.some_spec (exists_prime R)))),
eq_of_associated_right (associated.symm ⟨u, mul_comm _ _⟩),
multiplicity_pow_self_of_prime (principal_ideal_ring.irreducible_iff_prime.1 hϖ)]
lemma add_val_def' (u : units R) {ϖ : R} (hϖ : irreducible ϖ) (n : ℕ) :
add_val R ((u : R) * ϖ ^ n) = n :=
add_val_def _ u hϖ n rfl
@[simp] lemma add_val_zero : add_val R 0 = ⊤ :=
(add_val R).map_zero
@[simp] lemma add_val_one : add_val R 1 = 0 :=
(add_val R).map_one
@[simp] lemma add_val_uniformizer {ϖ : R} (hϖ : irreducible ϖ) : add_val R ϖ = 1 :=
add_val_def ϖ 1 hϖ 1 (by simp)
@[simp] lemma add_val_mul {a b : R} :
add_val R (a * b) = add_val R a + add_val R b :=
(add_val R).map_mul _ _
lemma add_val_pow (a : R) (n : ℕ) : add_val R (a ^ n) = n • add_val R a :=
(add_val R).map_pow _ _
lemma add_val_eq_top_iff {a : R} : add_val R a = ⊤ ↔ a = 0 :=
begin
have hi := (irreducible_of_prime (classical.some_spec (exists_prime R))),
split,
{ contrapose,
intro h,
obtain ⟨n, ha⟩ := associated_pow_irreducible h hi,
obtain ⟨u, rfl⟩ := ha.symm,
rw [mul_comm, add_val_def' u hi n],
exact enat.coe_ne_top _ },
{ rintro rfl,
exact add_val_zero }
end
lemma add_val_le_iff_dvd {a b : R} : add_val R a ≤ add_val R b ↔ a ∣ b :=
begin
have hp := classical.some_spec (exists_prime R),
split; intro h,
{ by_cases ha0 : a = 0,
{ rw [ha0, add_val_zero, top_le_iff, add_val_eq_top_iff] at h,
rw h,
apply dvd_zero },
obtain ⟨n, ha⟩ := associated_pow_irreducible ha0 (irreducible_of_prime hp),
rw [add_val, add_valuation_apply, add_valuation_apply,
multiplicity_le_multiplicity_iff] at h,
exact dvd.trans (dvd_of_associated ha) (h n (dvd_of_associated ha.symm)), },
{ rw [add_val, add_valuation_apply, add_valuation_apply],
exact multiplicity_le_multiplicity_of_dvd_right h }
end
lemma add_val_add {a b : R} :
min (add_val R a) (add_val R b) ≤ add_val R (a + b) :=
(add_val R).map_add _ _
end
end discrete_valuation_ring
|
bcdf39f5a54ada6424ae8356b2f8d06002eafa94 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Lean/ToExpr.lean | bc81fc99fc774a9b6087bfe94eaef783dd9a5eb6 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 3,092 | 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.Expr
universe u
namespace Lean
/--
We use the `ToExpr` type class to convert values of type `α` into
expressions that denote these values in Lean.
Example:
```
toExpr true = .const ``Bool.true []
```
-/
class ToExpr (α : Type u) where
/-- Convert a value `a : α` into an expression that denotes `a` -/
toExpr : α → Expr
/-- Expression representing the type `α` -/
toTypeExpr : Expr
export ToExpr (toExpr toTypeExpr)
instance : ToExpr Nat where
toExpr := mkNatLit
toTypeExpr := mkConst ``Nat
instance : ToExpr Bool where
toExpr := fun b => if b then mkConst ``Bool.true else mkConst ``Bool.false
toTypeExpr := mkConst ``Bool
instance : ToExpr Char where
toExpr := fun c => mkApp (mkConst ``Char.ofNat) (toExpr c.toNat)
toTypeExpr := mkConst ``Char
instance : ToExpr String where
toExpr := mkStrLit
toTypeExpr := mkConst ``String
instance : ToExpr Unit where
toExpr := fun _ => mkConst `Unit.unit
toTypeExpr := mkConst ``Unit
private def Name.toExprAux : Name → Expr
| .anonymous => mkConst ``Lean.Name.anonymous
| .str p s ..=> mkApp2 (mkConst ``Lean.Name.str) (toExprAux p) (toExpr s)
| .num p n ..=> mkApp2 (mkConst ``Lean.Name.num) (toExprAux p) (toExpr n)
instance : ToExpr Name where
toExpr := Name.toExprAux
toTypeExpr := mkConst ``Name
instance [ToExpr α] : ToExpr (Option α) :=
let type := toTypeExpr α
{ toExpr := fun o => match o with
| none => mkApp (mkConst ``Option.none [levelZero]) type
| some a => mkApp2 (mkConst ``Option.some [levelZero]) type (toExpr a),
toTypeExpr := mkApp (mkConst ``Option [levelZero]) type }
private def List.toExprAux [ToExpr α] (nilFn : Expr) (consFn : Expr) : List α → Expr
| [] => nilFn
| a::as => mkApp2 consFn (toExpr a) (toExprAux nilFn consFn as)
instance [ToExpr α] : ToExpr (List α) :=
let type := toTypeExpr α
let nil := mkApp (mkConst ``List.nil [levelZero]) type
let cons := mkApp (mkConst ``List.cons [levelZero]) type
{ toExpr := List.toExprAux nil cons,
toTypeExpr := mkApp (mkConst ``List [levelZero]) type }
instance [ToExpr α] : ToExpr (Array α) :=
let type := toTypeExpr α
{ toExpr := fun as => mkApp2 (mkConst ``List.toArray [levelZero]) type (toExpr as.toList),
toTypeExpr := mkApp (mkConst ``Array [levelZero]) type }
instance [ToExpr α] [ToExpr β] : ToExpr (α × β) :=
let αType := toTypeExpr α
let βType := toTypeExpr β
{ toExpr := fun ⟨a, b⟩ => mkApp4 (mkConst ``Prod.mk [levelZero, levelZero]) αType βType (toExpr a) (toExpr b),
toTypeExpr := mkApp2 (mkConst ``Prod [levelZero, levelZero]) αType βType }
def Expr.toCtorIfLit : Expr → Expr
| .lit (.natVal v) =>
if v == 0 then mkConst ``Nat.zero
else mkApp (mkConst ``Nat.succ) (mkRawNatLit (v-1))
| .lit (.strVal v) =>
mkApp (mkConst ``String.mk) (toExpr v.toList)
| e => e
end Lean
|
dc2af3f6a65af445c668dd4c7cd0d36f2f4368f2 | 61c3861020ef87c6c325fc3c3dcbabf5d6b07985 | /types/prod.lean | cc7cb917857fc037228029e4cbc72ba58f614929 | [
"Apache-2.0"
] | permissive | jonas-frey/hott3 | a623be2959e8a713c03fa1b0f34bf76a561dfa87 | a944051b4eb5919bdc70978ee15fcbb48a824811 | refs/heads/master | 1,628,408,720,559 | 1,510,267,042,000 | 1,510,267,042,000 | 106,760,764 | 0 | 0 | null | 1,507,856,238,000 | 1,507,856,238,000 | null | UTF-8 | Lean | false | false | 12,610 | lean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Jakob von Raumer
Ported from Coq HoTT
Theorems about products
-/
import ..init
universes u v w
hott_theory
namespace hott
open eq equiv is_equiv is_trunc prod unit
variables {A : Type _}
{A': Type _}
{B : Type _}
{B' : Type _}
{C : Type _}
{D : Type _}
{P : A → Type _} {Q : A → Type _}
{a a' a'' : A} {b b₁ b₂ b' b'' : B} {u v w : A × B}
namespace prod
/- Paths in a product space -/
@[hott] protected def eta (u : A × B) : (fst u, snd u) = u :=
by cases u; reflexivity
@[hott] def pair_eq (pa : a = a') (pb : b = b') : (a, b) = (a', b') :=
ap011 prod.mk pa pb
@[hott] def prod_eq (H₁ : u.1 = v.1) (H₂ : u.2 = v.2) : u = v :=
by cases u; cases v; exact pair_eq H₁ H₂
@[hott] def eq_fst (p : u = v) : u.1 = v.1 :=
ap fst p
@[hott] def eq_snd (p : u = v) : u.2 = v.2 :=
ap snd p
postfix `..1`:(max+1) := eq_fst
postfix `..2`:(max+1) := eq_snd
@[hott] protected def ap_fst (p : u = v) : ap fst p = p..1 := idp
@[hott] protected def ap_snd (p : u = v) : ap snd p = p..2 := idp
@[hott] def pair_prod_eq (p : u.1 = v.1) (q : u.2 = v.2)
: ((prod_eq p q)..1, (prod_eq p q)..2) = (p, q) :=
by induction u; induction v; dsimp at *; induction p; induction q; reflexivity
@[hott] def prod_eq_fst (p : u.1 = v.1) (q : u.2 = v.2) : (prod_eq p q)..1 = p :=
(pair_prod_eq p q)..1
@[hott] def prod_eq_snd (p : u.1 = v.1) (q : u.2 = v.2) : (prod_eq p q)..2 = q :=
(pair_prod_eq p q)..2
@[hott] def prod_eq_eta (p : u = v) : prod_eq (p..1) (p..2) = p :=
by induction p; induction u; reflexivity
-- the uncurried version of prod_eq. We will prove that this is an equivalence
@[hott] def prod_eq_unc (H : u.1 = v.1 × u.2 = v.2) : u = v :=
by cases H with H₁ H₂; exact prod_eq H₁ H₂
@[hott] def pair_prod_eq_unc : Π(pq : u.1 = v.1 × u.2 = v.2),
((prod_eq_unc pq)..1, (prod_eq_unc pq)..2) = pq
| (pq₁, pq₂) := pair_prod_eq pq₁ pq₂
@[hott] def prod_eq_unc_fst (pq : u.1 = v.1 × u.2 = v.2) : (prod_eq_unc pq)..1 = pq.1 :=
(pair_prod_eq_unc pq)..1
@[hott] def prod_eq_unc_snd (pq : u.1 = v.1 × u.2 = v.2) : (prod_eq_unc pq)..2 = pq.2 :=
(pair_prod_eq_unc pq)..2
@[hott] def prod_eq_unc_eta (p : u = v) : prod_eq_unc (p..1, p..2) = p :=
prod_eq_eta p
@[hott, instance] def is_equiv_prod_eq (u v : A × B)
: is_equiv (prod_eq_unc : u.1 = v.1 × u.2 = v.2 → u = v) :=
adjointify prod_eq_unc
(λp, (p..1, p..2))
prod_eq_unc_eta
pair_prod_eq_unc
@[hott] def prod_eq_equiv (u v : A × B) : (u = v) ≃ (u.1 = v.1 × u.2 = v.2) :=
(equiv.mk prod_eq_unc (by apply_instance))⁻¹ᵉ
@[hott] def pair_eq_pair_equiv (a a' : A) (b b' : B) :
((a, b) = (a', b')) ≃ (a = a' × b = b') :=
prod_eq_equiv (a, b) (a', b')
@[hott] def ap_prod_mk_left (p : a = a') : ap (λa, prod.mk a b) p = prod_eq p idp :=
ap_eq_ap011_left prod.mk p b
@[hott] def ap_prod_mk_right (p : b = b') : ap (λb, prod.mk a b) p = prod_eq idp p :=
ap_eq_ap011_right prod.mk a p
@[hott] def pair_eq_eta {A B : Type _} {u v : A × B}
(p : u = v) : pair_eq (p..1) (p..2) = prod.eta u ⬝ p ⬝ (prod.eta v)⁻¹ :=
by induction p; induction u; reflexivity
@[hott] def prod_eq_eq {A B : Type _} {u v : A × B}
{p₁ q₁ : u.1 = v.1} {p₂ q₂ : u.2 = v.2} (α₁ : p₁ = q₁) (α₂ : p₂ = q₂)
: prod_eq p₁ p₂ = prod_eq q₁ q₂ :=
by induction α₁; induction α₂; reflexivity
@[hott] def prod_eq_assemble {A B : Type _} {u v : A × B}
{p q : u = v} (α₁ : p..1 = q..1) (α₂ : p..2 = q..2) : p = q :=
(prod_eq_eta p)⁻¹ ⬝ prod.prod_eq_eq α₁ α₂ ⬝ prod_eq_eta q
@[hott] def eq_fst_concat {A B : Type _} {u v w : A × B}
(p : u = v) (q : v = w)
: (p ⬝ q)..1 = p..1 ⬝ q..1 :=
by induction q; reflexivity
@[hott] def eq_snd_concat {A B : Type _} {u v w : A × B}
(p : u = v) (q : v = w)
: (p ⬝ q)..2 = p..2 ⬝ q..2 :=
by induction q; reflexivity
/- Groupoid structure -/
@[hott] def prod_eq_inv (p : a = a') (q : b = b') :
(@prod_eq _ _ (a,b) (a',b') p q)⁻¹ = prod_eq p⁻¹ q⁻¹ :=
by induction p; induction q; reflexivity
@[hott] def prod_eq_concat (p : a = a') (p' : a' = a'') (q : b = b') (q' : b' = b'')
: @prod_eq _ _ (a,b) (a',b') p q ⬝ @prod_eq _ _ (a',b') (a'',b'') p' q' = prod_eq (p ⬝ p') (q ⬝ q') :=
by induction p; induction q; induction p'; induction q'; reflexivity
@[hott] def prod_eq_concat_idp (p : a = a') (q : b = b')
: @prod_eq _ _ (a,b) (a',b) p idp ⬝ @prod_eq _ _ (a',b) (a',b') idp q = prod_eq p q :=
by induction p; induction q; reflexivity
/- Transport -/
@[hott] def prod_transport (p : a = a') (u : P a × Q a)
: p ▸ u = (p ▸ u.1, p ▸ u.2) :=
by induction p; induction u; reflexivity
@[hott] def prod_eq_transport (p : a = a') (q : b = b') {R : A × B → Type _} (r : R (a, b))
: @prod_eq _ _ (a,b) (a',b') p q ▸ r = p ▸ (q ▸ r) :=
by induction p; induction q; reflexivity
/- Pathovers -/
@[hott] def etao (p : a = a') (bc : P a × Q a) : bc =[p; λ a, P a × Q a] (p ▸ bc.1, p ▸ bc.2) :=
by induction p; induction bc; apply idpo
@[hott] def prod_pathover (p : a = a') (u : P a × Q a) (v : P a' × Q a')
(r : u.1 =[p] v.1) (s : u.2 =[p] v.2) : u =[p; λ a, P a × Q a] v :=
begin
induction u, induction v, dsimp at *, induction r,
refine idp_rec_on s _,
apply idpo
end
@[hott] def prod_pathover_equiv {A : Type _} {B C : A → Type _} {a a' : A} (p : a = a')
(x : B a × C a) (x' : B a' × C a') : x =[p; λ a, B a × C a] x' ≃ x.1 =[p] x'.1 × x.2 =[p] x'.2 :=
begin
fapply equiv.MK,
{ intro q, induction q, constructor; constructor },
{ intro v, induction v with q r, exact prod_pathover _ _ _ q r },
{ intro v, induction v with q r, induction x with b c, induction x' with b' c',
dsimp at *, induction q, refine idp_rec_on r _, reflexivity },
{ intro q, induction q, induction x with b c, reflexivity }
end
/-
TODO:
* define the projections from the type u =[p] v
* show that the uncurried version of prod_pathover is an equivalence
-/
/- Functorial action -/
variables (f : A → A') (g : B → B')
@[hott] def prod_functor (u : A × B) : A' × B' :=
(f u.1, g u.2)
@[hott] def ap_prod_functor (p : u.1 = v.1) (q : u.2 = v.2)
: ap (prod_functor f g) (prod_eq p q) = prod_eq (ap f p) (ap g q) :=
by induction u; induction v; dsimp at *; induction p; induction q; reflexivity
/- Helpers for functions of two arguments -/
@[hott] def ap_diagonal {a a' : A} (p : a = a')
: ap (λx : A, (x,x)) p = prod_eq p p :=
by induction p; constructor
@[hott] def ap_binary (m : A → B → C) (p : a = a') (q : b = b')
: ap (λz : A × B, m z.1 z.2) (@prod_eq _ _ (a,b) (a',b') p q)
= ap (m a) q ⬝ ap (λx : A, m x b') p :=
by induction p; induction q; constructor
@[hott] def ap_prod_elim {A B C : Type _} {a a' : A} {b b' : B} (m : A → B → C)
(p : a = a') (q : b = b') : @ap (A × B) C (λ ab, prod.rec m ab) (a,b) (a',b') (prod_eq p q)
= ap (m a) q ⬝ ap (λx : A, m x b') p :=
by induction p; induction q; constructor
@[hott] def ap_prod_elim_idp {A B C : Type _} {a a' : A} (m : A → B → C)
(p : a = a') (b : B) : @ap (A × B) C (λ ab, prod.rec m ab) (a,b) (a',b) (prod_eq p idp) = ap (λx : A, m x b) p :=
ap_prod_elim m p idp ⬝ idp_con _
/- Equivalences -/
@[hott, instance] def is_equiv_prod_functor [H : is_equiv f] [H : is_equiv g]
: is_equiv (prod_functor f g) :=
begin
apply adjointify _ (prod_functor f⁻¹ᶠ g⁻¹ᶠ);
intro u; induction u; dsimp [prod_functor],
{rwr [right_inv f, right_inv g]},
{rwr [left_inv f, left_inv g]},
end
@[hott] def prod_equiv_prod_of_is_equiv [H : is_equiv f] [H : is_equiv g]
: A × B ≃ A' × B' :=
equiv.mk (prod_functor f g) (by apply_instance)
@[hott] def prod_equiv_prod (f : A ≃ A') (g : B ≃ B') : A × B ≃ A' × B' :=
equiv.mk (prod_functor f g) (by apply_instance)
-- rename
@[hott] def prod_equiv_prod_left (g : B ≃ B') : A × B ≃ A × B' :=
prod_equiv_prod equiv.rfl g
-- rename
@[hott] def prod_equiv_prod_right (f : A ≃ A') : A × B ≃ A' × B :=
prod_equiv_prod f equiv.rfl
/- Symmetry -/
@[hott, instance] def is_equiv_flip (A B : Type _)
: is_equiv (flip : A × B → B × A) :=
adjointify flip flip (λ ⟨b,a⟩, idp) (λ ⟨a,b⟩, idp)
@[hott] def prod_comm_equiv (A B : Type _) : A × B ≃ B × A :=
equiv.mk flip (by apply_instance)
/- Associativity -/
@[hott] def prod_assoc_equiv (A B C : Type _) : A × (B × C) ≃ (A × B) × C :=
begin
fapply equiv.MK,
{ intro z, induction z with a z, induction z with b c, exact ⟨⟨a, b⟩, c⟩},
{ intro z, induction z with z c, induction z with a b, exact ⟨a, ⟨b, c⟩⟩},
{ intro z, induction z with z c, induction z with a b, reflexivity},
{ intro z, induction z with a z, induction z with b c, reflexivity},
end
@[hott] def prod_contr_equiv (A : Type u) (B : Type v) [H : is_contr B] : A × B ≃ A :=
equiv.MK fst
(λx, (x, by apply center))
(λx, idp)
(λ ⟨a,b⟩, pair_eq idp (center_eq _))
@[hott] def prod_unit_equiv (A : Type _) : A × unit ≃ A :=
by apply prod_contr_equiv
@[hott] def prod_empty_equiv (A : Type _) : A × empty ≃ empty :=
begin
fapply equiv.MK,
{ intro x, cases x with a e, cases e },
{ intro e, cases e },
{ intro e, cases e },
{ intro x, cases x with a e, cases e }
end
/- Universal mapping properties -/
@[hott, instance] def is_equiv_prod_rec (P : A × B → Type _)
: is_equiv (prod.rec : (Πa b, P (a, b)) → Πu, P u) :=
adjointify _
(λg a b, g (a, b))
(λg, eq_of_homotopy (λu, by induction u;reflexivity))
(λf, idp)
@[hott] def equiv_prod_rec (P : A × B → Type _) : (Πa b, P (a, b)) ≃ (Πu, P u) :=
equiv.mk prod.rec (by apply_instance)
@[hott] def imp_imp_equiv_prod_imp (A B C : Type _) : (A → B → C) ≃ (A × B → C) :=
equiv_prod_rec (λ _, C)
@[hott] def prod_corec_unc {P Q : A → Type _} (u : (Πa, P a) × (Πa, Q a)) (a : A)
: P a × Q a :=
(u.1 a, u.2 a)
@[hott] def is_equiv_prod_corec (P Q : A → Type _)
: is_equiv (prod_corec_unc : (Πa, P a) × (Πa, Q a) → Πa, P a × Q a) :=
adjointify _
(λg, (λa, (g a).1, λa, (g a).2))
(by intro g; apply eq_of_homotopy; intro a; dsimp [prod_corec_unc]; induction g a; reflexivity)
(by intro h; induction h with f g; reflexivity)
@[hott] def equiv_prod_corec (P Q : A → Type _)
: ((Πa, P a) × (Πa, Q a)) ≃ (Πa, P a × Q a) :=
equiv.mk _ (by apply is_equiv_prod_corec)
@[hott] def imp_prod_imp_equiv_imp_prod (A B C : Type _)
: (A → B) × (A → C) ≃ (A → (B × C)) :=
by apply equiv_prod_corec
@[hott] def is_trunc_prod (A B : Type _) (n : trunc_index) [HA : is_trunc n A] [HB : is_trunc n B]
: is_trunc n (A × B) :=
begin
induction n with n IH generalizing A B HA HB,
{ fapply is_contr.mk,
constructor; apply center,
intro u, apply prod_eq; apply center_eq},
{ apply @is_trunc_succ_intro _ _ _, intros u v,
apply @is_trunc_equiv_closed_rev (u=v) ((u.fst = v.fst) × (u.snd = v.snd)) _ _ _,
apply prod_eq_equiv,
exact @IH _ _ _ _}
end
end prod
attribute [instance] prod.is_trunc_prod
namespace prod
/- pointed products -/
open pointed
@[hott, instance] def pointed_prod (A B : Type _) [H1 : pointed A] [H2 : pointed B]
: pointed (A × B) :=
pointed.mk (pt,pt)
@[hott] def pprod (A B : Type*) : Type* :=
pointed.mk' (A × B)
infixr ` ×* `:35 := pprod
@[hott] def pfst {A B : Type*} : A ×* B →* A :=
pmap.mk fst idp
@[hott] def psnd {A B : Type*} : A ×* B →* B :=
pmap.mk snd idp
@[hott] def tprod {n : trunc_index} (A B : n-Type) : n-Type :=
trunctype.mk (A × B) (by apply_instance)
infixr `×t`:30 := tprod
@[hott] def ptprod {n : ℕ₋₂} (A B : n-Type*) : n-Type* :=
ptrunctype.mk' n (A × B)
@[hott] def pprod_functor {A B C D : Type*} (f : A →* C) (g : B →* D) : A ×* B →* C ×* D :=
pmap.mk (prod_functor f g) (prod_eq (respect_pt f) (respect_pt g))
end prod
end hott |
c5ecd1e32cb58835b2669eae3530f55caf67acc6 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/algebra/direct_limit.lean | 2a2ffa9e9b9f7087604fce20f2088298c27500b1 | [
"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 | 25,502 | lean | /-
Copyright (c) 2019 Kenny Lau, Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes
Direct limit of modules, abelian groups, rings, and fields.
See Atiyah-Macdonald PP.32-33, Matsumura PP.269-270
Generalizes the notion of "union", or "gluing", of incomparable modules over the same ring,
or incomparable abelian groups, or rings, or fields.
It is constructed as a quotient of the free module (for the module case) or quotient of
the free commutative ring (for the ring case) instead of a quotient of the disjoint union
so as to make the operations (addition etc.) "computable".
-/
import linear_algebra.direct_sum_module
import algebra.big_operators
import ring_theory.free_comm_ring
import ring_theory.ideal_operations
universes u v w u₁
open submodule
variables {R : Type u} [ring R]
variables {ι : Type v} [nonempty ι]
variables [decidable_eq ι] [directed_order ι]
variables (G : ι → Type w) [Π i, decidable_eq (G i)]
/-- A directed system is a functor from the category (directed poset) to another category.
This is used for abelian groups and rings and fields because their maps are not bundled.
See module.directed_system -/
class directed_system (f : Π i j, i ≤ j → G i → G j) : Prop :=
(map_self [] : ∀ i x h, f i i h x = x)
(map_map [] : ∀ i j k hij hjk x, f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x)
namespace module
variables [Π i, add_comm_group (G i)] [Π i, module R (G i)]
/-- A directed system is a functor from the category (directed poset) to the category of R-modules. -/
class directed_system (f : Π i j, i ≤ j → G i →ₗ[R] G j) : Prop :=
(map_self [] : ∀ i x h, f i i h x = x)
(map_map [] : ∀ i j k hij hjk x, f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x)
variables (f : Π i j, i ≤ j → G i →ₗ[R] G j) [directed_system G f]
/-- The direct limit of a directed system is the modules glued together along the maps. -/
def direct_limit : Type (max v w) :=
(span R $ { a | ∃ (i j) (H : i ≤ j) x,
direct_sum.lof R ι G i x - direct_sum.lof R ι G j (f i j H x) = a }).quotient
namespace direct_limit
instance : add_comm_group (direct_limit G f) := quotient.add_comm_group _
instance : module R (direct_limit G f) := quotient.module _
variables (R ι)
/-- The canonical map from a component to the direct limit. -/
def of (i) : G i →ₗ[R] direct_limit G f :=
(mkq _).comp $ direct_sum.lof R ι G i
variables {R ι G f}
@[simp] lemma of_f {i j hij x} : (of R ι G f j (f i j hij x)) = of R ι G f i x :=
eq.symm $ (submodule.quotient.eq _).2 $ subset_span ⟨i, j, hij, x, rfl⟩
/-- Every element of the direct limit corresponds to some element in
some component of the directed system. -/
theorem exists_of (z : direct_limit G f) : ∃ i x, of R ι G f i x = z :=
nonempty.elim (by apply_instance) $ assume ind : ι,
quotient.induction_on' z $ λ z, direct_sum.induction_on z
⟨ind, 0, linear_map.map_zero _⟩
(λ i x, ⟨i, x, rfl⟩)
(λ p q ⟨i, x, ihx⟩ ⟨j, y, ihy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in
⟨k, f i k hik x + f j k hjk y, by rw [linear_map.map_add, of_f, of_f, ihx, ihy]; refl⟩)
@[elab_as_eliminator]
protected theorem induction_on {C : direct_limit G f → Prop} (z : direct_limit G f)
(ih : ∀ i x, C (of R ι G f i x)) : C z :=
let ⟨i, x, h⟩ := exists_of z in h ▸ ih i x
variables {P : Type u₁} [add_comm_group P] [module R P] (g : Π i, G i →ₗ[R] P)
variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x)
include Hg
variables (R ι G f)
/-- The universal property of the direct limit: maps from the components to another module
that respect the directed system structure (i.e. make some diagram commute) give rise
to a unique map out of the direct limit. -/
def lift : direct_limit G f →ₗ[R] P :=
liftq _ (direct_sum.to_module R ι P g)
(span_le.2 $ λ a ⟨i, j, hij, x, hx⟩, by rw [← hx, mem_coe, linear_map.sub_mem_ker_iff,
direct_sum.to_module_lof, direct_sum.to_module_lof, Hg])
variables {R ι G f}
omit Hg
lemma lift_of {i} (x) : lift R ι G f g Hg (of R ι G f i x) = g i x :=
direct_sum.to_module_lof R _ _
theorem lift_unique (F : direct_limit G f →ₗ[R] P) (x) :
F x = lift R ι G f (λ i, F.comp $ of R ι G f i)
(λ i j hij x, by rw [linear_map.comp_apply, of_f]; refl) x :=
direct_limit.induction_on x $ λ i x, by rw lift_of; refl
section totalize
open_locale classical
variables (G f)
noncomputable def totalize : Π i j, G i →ₗ[R] G j :=
λ i j, if h : i ≤ j then f i j h else 0
variables {G f}
lemma totalize_apply (i j x) :
totalize G f i j x = if h : i ≤ j then f i j h x else 0 :=
if h : i ≤ j
then by dsimp only [totalize]; rw [dif_pos h, dif_pos h]
else by dsimp only [totalize]; rw [dif_neg h, dif_neg h, linear_map.zero_apply]
end totalize
lemma to_module_totalize_of_le {x : direct_sum ι G} {i j : ι}
(hij : i ≤ j) (hx : ∀ k ∈ x.support, k ≤ i) :
direct_sum.to_module R ι (G j) (λ k, totalize G f k j) x =
f i j hij (direct_sum.to_module R ι (G i) (λ k, totalize G f k i) x) :=
begin
rw [← @dfinsupp.sum_single ι G _ _ _ x],
unfold dfinsupp.sum,
simp only [linear_map.map_sum],
refine finset.sum_congr rfl (λ k hk, _),
rw direct_sum.single_eq_lof R k (x k),
simp [totalize_apply, hx k hk, le_trans (hx k hk) hij, directed_system.map_map f]
end
lemma of.zero_exact_aux {x : direct_sum ι G} (H : submodule.quotient.mk x = (0 : direct_limit G f)) :
∃ j, (∀ k ∈ x.support, k ≤ j) ∧ direct_sum.to_module R ι (G j) (λ i, totalize G f i j) x = (0 : G j) :=
nonempty.elim (by apply_instance) $ assume ind : ι,
span_induction ((quotient.mk_eq_zero _).1 H)
(λ x ⟨i, j, hij, y, hxy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in
⟨k, begin
clear_,
subst hxy,
split,
{ intros i0 hi0,
rw [dfinsupp.mem_support_iff, dfinsupp.sub_apply, ← direct_sum.single_eq_lof,
← direct_sum.single_eq_lof, dfinsupp.single_apply, dfinsupp.single_apply] at hi0,
split_ifs at hi0 with hi hj hj, { rwa hi at hik }, { rwa hi at hik }, { rwa hj at hjk },
exfalso, apply hi0, rw sub_zero },
simp [linear_map.map_sub, totalize_apply, hik, hjk,
directed_system.map_map f, direct_sum.apply_eq_component,
direct_sum.component.of],
end⟩)
⟨ind, λ _ h, (finset.not_mem_empty _ h).elim, linear_map.map_zero _⟩
(λ x y ⟨i, hi, hxi⟩ ⟨j, hj, hyj⟩,
let ⟨k, hik, hjk⟩ := directed_order.directed i j in
⟨k, λ l hl,
(finset.mem_union.1 (dfinsupp.support_add hl)).elim
(λ hl, le_trans (hi _ hl) hik)
(λ hl, le_trans (hj _ hl) hjk),
by simp [linear_map.map_add, hxi, hyj,
to_module_totalize_of_le hik hi,
to_module_totalize_of_le hjk hj]⟩)
(λ a x ⟨i, hi, hxi⟩,
⟨i, λ k hk, hi k (dfinsupp.support_smul hk),
by simp [linear_map.map_smul, hxi]⟩)
/-- A component that corresponds to zero in the direct limit is already zero in some
bigger module in the directed system. -/
theorem of.zero_exact {i x} (H : of R ι G f i x = 0) :
∃ j hij, f i j hij x = (0 : G j) :=
let ⟨j, hj, hxj⟩ := of.zero_exact_aux H in
if hx0 : x = 0 then ⟨i, le_refl _, by simp [hx0]⟩
else
have hij : i ≤ j, from hj _ $
by simp [direct_sum.apply_eq_component, hx0],
⟨j, hij, by simpa [totalize_apply, hij] using hxj⟩
end direct_limit
end module
namespace add_comm_group
variables [Π i, add_comm_group (G i)]
/-- The direct limit of a directed system is the abelian groups glued together along the maps. -/
def direct_limit (f : Π i j, i ≤ j → G i → G j)
[Π i j hij, is_add_group_hom (f i j hij)] [directed_system G f] : Type* :=
@module.direct_limit ℤ _ ι _ _ _ G _ _ _
(λ i j hij, (add_monoid_hom.of $ f i j hij).to_int_linear_map)
⟨directed_system.map_self f, directed_system.map_map f⟩
namespace direct_limit
variables (f : Π i j, i ≤ j → G i → G j)
variables [Π i j hij, is_add_group_hom (f i j hij)] [directed_system G f]
lemma directed_system :
module.directed_system G (λ i j hij, (add_monoid_hom.of $ f i j hij).to_int_linear_map) :=
⟨directed_system.map_self f, directed_system.map_map f⟩
local attribute [instance] directed_system
instance : add_comm_group (direct_limit G f) :=
module.direct_limit.add_comm_group G (λ i j hij, (add_monoid_hom.of $f i j hij).to_int_linear_map)
set_option class.instance_max_depth 50
/-- The canonical map from a component to the direct limit. -/
def of (i) : G i → direct_limit G f :=
module.direct_limit.of ℤ ι G (λ i j hij, (add_monoid_hom.of $ f i j hij).to_int_linear_map) i
variables {G f}
instance of.is_add_group_hom (i) : is_add_group_hom (of G f i) :=
linear_map.is_add_group_hom _
@[simp] lemma of_f {i j} (hij) (x) : of G f j (f i j hij x) = of G f i x :=
module.direct_limit.of_f
@[simp] lemma of_zero (i) : of G f i 0 = 0 := is_add_group_hom.map_zero _
@[simp] lemma of_add (i x y) : of G f i (x + y) = of G f i x + of G f i y := is_add_hom.map_add _ _ _
@[simp] lemma of_neg (i x) : of G f i (-x) = -of G f i x := is_add_group_hom.map_neg _ _
@[simp] lemma of_sub (i x y) : of G f i (x - y) = of G f i x - of G f i y := is_add_group_hom.map_sub _ _ _
@[elab_as_eliminator]
protected theorem induction_on {C : direct_limit G f → Prop} (z : direct_limit G f)
(ih : ∀ i x, C (of G f i x)) : C z :=
module.direct_limit.induction_on z ih
/-- A component that corresponds to zero in the direct limit is already zero in some
bigger module in the directed system. -/
theorem of.zero_exact (i x) (h : of G f i x = 0) : ∃ j hij, f i j hij x = 0 :=
module.direct_limit.of.zero_exact h
variables (P : Type u₁) [add_comm_group P]
variables (g : Π i, G i → P) [Π i, is_add_group_hom (g i)]
variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x)
variables (G f)
/-- The universal property of the direct limit: maps from the components to another abelian group
that respect the directed system structure (i.e. make some diagram commute) give rise
to a unique map out of the direct limit. -/
def lift : direct_limit G f → P :=
module.direct_limit.lift ℤ ι G (λ i j hij, (add_monoid_hom.of $ f i j hij).to_int_linear_map)
(λ i, (add_monoid_hom.of $ g i).to_int_linear_map) Hg
variables {G f}
instance lift.is_add_group_hom : is_add_group_hom (lift G f P g Hg) :=
linear_map.is_add_group_hom _
@[simp] lemma lift_of (i x) : lift G f P g Hg (of G f i x) = g i x :=
module.direct_limit.lift_of _ _ _
@[simp] lemma lift_zero : lift G f P g Hg 0 = 0 := is_add_group_hom.map_zero _
@[simp] lemma lift_add (x y) : lift G f P g Hg (x + y) = lift G f P g Hg x + lift G f P g Hg y := is_add_hom.map_add _ _ _
@[simp] lemma lift_neg (x) : lift G f P g Hg (-x) = -lift G f P g Hg x := is_add_group_hom.map_neg _ _
@[simp] lemma lift_sub (x y) : lift G f P g Hg (x - y) = lift G f P g Hg x - lift G f P g Hg y := is_add_group_hom.map_sub _ _ _
lemma lift_unique (F : direct_limit G f → P) [is_add_group_hom F] (x) :
F x = @lift _ _ _ _ G _ _ f _ _ P _ (λ i x, F $ of G f i x) (λ i, is_add_group_hom.comp _ _)
(λ i j hij x, by dsimp; rw of_f) x :=
direct_limit.induction_on x $ λ i x, by rw lift_of
end direct_limit
end add_comm_group
namespace ring
variables [Π i, comm_ring (G i)]
variables (f : Π i j, i ≤ j → G i → G j)
variables [Π i j hij, is_ring_hom (f i j hij)]
variables [directed_system G f]
open free_comm_ring
/-- The direct limit of a directed system is the rings glued together along the maps. -/
def direct_limit : Type (max v w) :=
(ideal.span { a | (∃ i j H x, of (⟨j, f i j H x⟩ : Σ i, G i) - of ⟨i, x⟩ = a) ∨
(∃ i, of (⟨i, 1⟩ : Σ i, G i) - 1 = a) ∨
(∃ i x y, of (⟨i, x + y⟩ : Σ i, G i) - (of ⟨i, x⟩ + of ⟨i, y⟩) = a) ∨
(∃ i x y, of (⟨i, x * y⟩ : Σ i, G i) - (of ⟨i, x⟩ * of ⟨i, y⟩) = a) }).quotient
namespace direct_limit
instance : comm_ring (direct_limit G f) :=
ideal.quotient.comm_ring _
instance : ring (direct_limit G f) :=
comm_ring.to_ring _
/-- The canonical map from a component to the direct limit. -/
def of (i) (x : G i) : direct_limit G f :=
ideal.quotient.mk _ $ of ⟨i, x⟩
variables {G f}
instance of.is_ring_hom (i) : is_ring_hom (of G f i) :=
{ map_one := ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inl ⟨i, rfl⟩,
map_mul := λ x y, ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inr $ or.inr ⟨i, x, y, rfl⟩,
map_add := λ x y, ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inr $ or.inl ⟨i, x, y, rfl⟩ }
@[simp] lemma of_f {i j} (hij) (x) : of G f j (f i j hij x) = of G f i x :=
ideal.quotient.eq.2 $ subset_span $ or.inl ⟨i, j, hij, x, rfl⟩
@[simp] lemma of_zero (i) : of G f i 0 = 0 := is_ring_hom.map_zero _
@[simp] lemma of_one (i) : of G f i 1 = 1 := is_ring_hom.map_one _
@[simp] lemma of_add (i x y) : of G f i (x + y) = of G f i x + of G f i y := is_ring_hom.map_add _
@[simp] lemma of_neg (i x) : of G f i (-x) = -of G f i x := is_ring_hom.map_neg _
@[simp] lemma of_sub (i x y) : of G f i (x - y) = of G f i x - of G f i y := is_ring_hom.map_sub _
@[simp] lemma of_mul (i x y) : of G f i (x * y) = of G f i x * of G f i y := is_ring_hom.map_mul _
@[simp] lemma of_pow (i x) (n : ℕ) : of G f i (x ^ n) = of G f i x ^ n := is_semiring_hom.map_pow _ _ _
/-- Every element of the direct limit corresponds to some element in
some component of the directed system. -/
theorem exists_of (z : direct_limit G f) : ∃ i x, of G f i x = z :=
nonempty.elim (by apply_instance) $ assume ind : ι,
quotient.induction_on' z $ λ x, free_abelian_group.induction_on x
⟨ind, 0, of_zero ind⟩
(λ s, multiset.induction_on s
⟨ind, 1, of_one ind⟩
(λ a s ih, let ⟨i, x⟩ := a, ⟨j, y, hs⟩ := ih, ⟨k, hik, hjk⟩ := directed_order.directed i j in
⟨k, f i k hik x * f j k hjk y, by rw [of_mul, of_f, of_f, hs]; refl⟩))
(λ s ⟨i, x, ih⟩, ⟨i, -x, by rw [of_neg, ih]; refl⟩)
(λ p q ⟨i, x, ihx⟩ ⟨j, y, ihy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in
⟨k, f i k hik x + f j k hjk y, by rw [of_add, of_f, of_f, ihx, ihy]; refl⟩)
@[elab_as_eliminator] theorem induction_on {C : direct_limit G f → Prop} (z : direct_limit G f)
(ih : ∀ i x, C (of G f i x)) : C z :=
let ⟨i, x, hx⟩ := exists_of z in hx ▸ ih i x
section of_zero_exact
open_locale classical
variables (G f)
lemma of.zero_exact_aux2 {x : free_comm_ring Σ i, G i} {s t} (hxs : is_supported x s) {j k}
(hj : ∀ z : Σ i, G i, z ∈ s → z.1 ≤ j) (hk : ∀ z : Σ i, G i, z ∈ t → z.1 ≤ k)
(hjk : j ≤ k) (hst : s ⊆ t) :
f j k hjk (lift (λ ix : s, f ix.1.1 j (hj ix ix.2) ix.1.2) (restriction s x)) =
lift (λ ix : t, f ix.1.1 k (hk ix ix.2) ix.1.2) (restriction t x) :=
begin
refine ring.in_closure.rec_on hxs _ _ _ _,
{ rw [restriction_one, lift_one, is_ring_hom.map_one (f j k hjk), restriction_one, lift_one] },
{ rw [restriction_neg, restriction_one, lift_neg, lift_one,
is_ring_hom.map_neg (f j k hjk), is_ring_hom.map_one (f j k hjk),
restriction_neg, restriction_one, lift_neg, lift_one] },
{ rintros _ ⟨p, hps, rfl⟩ n ih,
rw [restriction_mul, lift_mul, is_ring_hom.map_mul (f j k hjk), ih, restriction_mul, lift_mul,
restriction_of, dif_pos hps, lift_of, restriction_of, dif_pos (hst hps), lift_of],
dsimp only, rw directed_system.map_map f, refl },
{ rintros x y ihx ihy,
rw [restriction_add, lift_add, is_ring_hom.map_add (f j k hjk), ihx, ihy, restriction_add, lift_add] }
end
variables {G f}
lemma of.zero_exact_aux {x : free_comm_ring Σ i, G i} (H : ideal.quotient.mk _ x = (0 : direct_limit G f)) :
∃ j s, ∃ H : (∀ k : Σ i, G i, k ∈ s → k.1 ≤ j), is_supported x s ∧
lift (λ ix : s, f ix.1.1 j (H ix ix.2) ix.1.2) (restriction s x) = (0 : G j) :=
begin
refine span_induction (ideal.quotient.eq_zero_iff_mem.1 H) _ _ _ _,
{ rintros x (⟨i, j, hij, x, rfl⟩ | ⟨i, rfl⟩ | ⟨i, x, y, rfl⟩ | ⟨i, x, y, rfl⟩),
{ refine ⟨j, {⟨i, x⟩, ⟨j, f i j hij x⟩}, _,
is_supported_sub (is_supported_of.2 $ or.inl rfl) (is_supported_of.2 $ or.inr $ or.inl rfl), _⟩,
{ rintros k (rfl | ⟨rfl | h⟩), refl, exact hij, cases h },
{ rw [restriction_sub, lift_sub, restriction_of, dif_pos, restriction_of, dif_pos, lift_of, lift_of],
dsimp only, rw directed_system.map_map f, exact sub_self _,
{ left, refl }, { right, left, refl }, } },
{ refine ⟨i, {⟨i, 1⟩}, _, is_supported_sub (is_supported_of.2 $ or.inl rfl) is_supported_one, _⟩,
{ rintros k (rfl | h), refl, cases h },
{ rw [restriction_sub, lift_sub, restriction_of, dif_pos, restriction_one, lift_of, lift_one],
dsimp only, rw [is_ring_hom.map_one (f i i _), sub_self], exact _inst_7 i i _, { left, refl } } },
{ refine ⟨i, {⟨i, x+y⟩, ⟨i, x⟩, ⟨i, y⟩}, _,
is_supported_sub (is_supported_of.2 $ or.inr $ or.inr $ or.inl rfl)
(is_supported_add (is_supported_of.2 $ or.inr $ or.inl rfl) (is_supported_of.2 $ or.inl rfl)), _⟩,
{ rintros k (rfl | ⟨rfl | ⟨rfl | hk⟩⟩), refl, refl, refl, cases hk },
{ rw [restriction_sub, restriction_add, restriction_of, restriction_of, restriction_of,
dif_pos, dif_pos, dif_pos, lift_sub, lift_add, lift_of, lift_of, lift_of],
dsimp only, rw is_ring_hom.map_add (f i i _), exact sub_self _,
{ right, right, left, refl }, { apply_instance }, { left, refl }, { right, left, refl } } },
{ refine ⟨i, {⟨i, x*y⟩, ⟨i, x⟩, ⟨i, y⟩}, _,
is_supported_sub (is_supported_of.2 $ or.inr $ or.inr $ or.inl rfl)
(is_supported_mul (is_supported_of.2 $ or.inr $ or.inl rfl) (is_supported_of.2 $ or.inl rfl)), _⟩,
{ rintros k (rfl | ⟨rfl | ⟨rfl | hk⟩⟩), refl, refl, refl, cases hk },
{ rw [restriction_sub, restriction_mul, restriction_of, restriction_of, restriction_of,
dif_pos, dif_pos, dif_pos, lift_sub, lift_mul, lift_of, lift_of, lift_of],
dsimp only, rw is_ring_hom.map_mul (f i i _), exact sub_self _,
{ right, right, left, refl }, { apply_instance }, { left, refl }, { right, left, refl } } } },
{ refine nonempty.elim (by apply_instance) (assume ind : ι, _),
refine ⟨ind, ∅, λ _, false.elim, is_supported_zero, _⟩,
rw [restriction_zero, lift_zero] },
{ rintros x y ⟨i, s, hi, hxs, ihs⟩ ⟨j, t, hj, hyt, iht⟩,
rcases directed_order.directed i j with ⟨k, hik, hjk⟩,
have : ∀ z : Σ i, G i, z ∈ s ∪ t → z.1 ≤ k,
{ rintros z (hz | hz), exact le_trans (hi z hz) hik, exact le_trans (hj z hz) hjk },
refine ⟨k, s ∪ t, this, is_supported_add (is_supported_upwards hxs $ set.subset_union_left s t)
(is_supported_upwards hyt $ set.subset_union_right s t), _⟩,
{ rw [restriction_add, lift_add,
← of.zero_exact_aux2 G f hxs hi this hik (set.subset_union_left s t),
← of.zero_exact_aux2 G f hyt hj this hjk (set.subset_union_right s t),
ihs, is_ring_hom.map_zero (f i k hik), iht, is_ring_hom.map_zero (f j k hjk), zero_add] } },
{ rintros x y ⟨j, t, hj, hyt, iht⟩, rw smul_eq_mul,
rcases exists_finset_support x with ⟨s, hxs⟩,
rcases (s.image sigma.fst).exists_le with ⟨i, hi⟩,
rcases directed_order.directed i j with ⟨k, hik, hjk⟩,
have : ∀ z : Σ i, G i, z ∈ ↑s ∪ t → z.1 ≤ k,
{ rintros z (hz | hz), exact le_trans (hi z.1 $ finset.mem_image.2 ⟨z, hz, rfl⟩) hik, exact le_trans (hj z hz) hjk },
refine ⟨k, ↑s ∪ t, this, is_supported_mul (is_supported_upwards hxs $ set.subset_union_left ↑s t)
(is_supported_upwards hyt $ set.subset_union_right ↑s t), _⟩,
rw [restriction_mul, lift_mul,
← of.zero_exact_aux2 G f hyt hj this hjk (set.subset_union_right ↑s t),
iht, is_ring_hom.map_zero (f j k hjk), mul_zero] }
end
/-- A component that corresponds to zero in the direct limit is already zero in some
bigger module in the directed system. -/
lemma of.zero_exact {i x} (hix : of G f i x = 0) : ∃ j, ∃ hij : i ≤ j, f i j hij x = 0 :=
let ⟨j, s, H, hxs, hx⟩ := of.zero_exact_aux hix in
have hixs : (⟨i, x⟩ : Σ i, G i) ∈ s, from is_supported_of.1 hxs,
⟨j, H ⟨i, x⟩ hixs, by rw [restriction_of, dif_pos hixs, lift_of] at hx; exact hx⟩
end of_zero_exact
/-- If the maps in the directed system are injective, then the canonical maps
from the components to the direct limits are injective. -/
theorem of_inj (hf : ∀ i j hij, function.injective (f i j hij)) (i) :
function.injective (of G f i) :=
begin
suffices : ∀ x, of G f i x = 0 → x = 0,
{ intros x y hxy, rw ← sub_eq_zero_iff_eq, apply this,
rw [is_ring_hom.map_sub (of G f i), hxy, sub_self] },
intros x hx, rcases of.zero_exact hx with ⟨j, hij, hfx⟩,
apply hf i j hij, rw [hfx, is_ring_hom.map_zero (f i j hij)]
end
variables (P : Type u₁) [comm_ring P]
variables (g : Π i, G i → P) [Π i, is_ring_hom (g i)]
variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x)
include Hg
open free_comm_ring
variables (G f)
/-- The universal property of the direct limit: maps from the components to another ring
that respect the directed system structure (i.e. make some diagram commute) give rise
to a unique map out of the direct limit.
We don't use this function as the canonical form because Lean 3 fails to automatically coerce
it to a function; use `lift` instead. -/
def lift_hom : direct_limit G f →+* P :=
ideal.quotient.lift _ (free_comm_ring.lift_hom $ λ x, g x.1 x.2) begin
suffices : ideal.span _ ≤
ideal.comap (free_comm_ring.lift_hom (λ (x : Σ (i : ι), G i), g (x.fst) (x.snd))) ⊥,
{ intros x hx, exact (mem_bot P).1 (this hx) },
rw ideal.span_le, intros x hx,
rw [mem_coe, ideal.mem_comap, mem_bot],
rcases hx with ⟨i, j, hij, x, rfl⟩ | ⟨i, rfl⟩ | ⟨i, x, y, rfl⟩ | ⟨i, x, y, rfl⟩;
simp only [coe_lift_hom, lift_sub, lift_of, Hg, lift_one, lift_add, lift_mul,
is_ring_hom.map_one (g i), is_ring_hom.map_add (g i), is_ring_hom.map_mul (g i), sub_self]
end
/-- The universal property of the direct limit: maps from the components to another ring
that respect the directed system structure (i.e. make some diagram commute) give rise
to a unique map out of the direct limit. -/
def lift : direct_limit G f → P := lift_hom G f P g Hg
instance lift_is_ring_hom : is_ring_hom (lift G f P g Hg) := (lift_hom G f P g Hg).is_ring_hom
variables {G f}
omit Hg
@[simp] lemma lift_of (i x) : lift G f P g Hg (of G f i x) = g i x := free_comm_ring.lift_of _ _
@[simp] lemma lift_zero : lift G f P g Hg 0 = 0 := (lift_hom G f P g Hg).map_zero
@[simp] lemma lift_one : lift G f P g Hg 1 = 1 := (lift_hom G f P g Hg).map_one
@[simp] lemma lift_add (x y) : lift G f P g Hg (x + y) = lift G f P g Hg x + lift G f P g Hg y :=
(lift_hom G f P g Hg).map_add x y
@[simp] lemma lift_neg (x) : lift G f P g Hg (-x) = -lift G f P g Hg x :=
(lift_hom G f P g Hg).map_neg x
@[simp] lemma lift_sub (x y) : lift G f P g Hg (x - y) = lift G f P g Hg x - lift G f P g Hg y :=
(lift_hom G f P g Hg).map_sub x y
@[simp] lemma lift_mul (x y) : lift G f P g Hg (x * y) = lift G f P g Hg x * lift G f P g Hg y :=
(lift_hom G f P g Hg).map_mul x y
@[simp] lemma lift_pow (x) (n : ℕ) : lift G f P g Hg (x ^ n) = lift G f P g Hg x ^ n :=
(lift_hom G f P g Hg).map_pow x n
local attribute [instance, priority 100] is_ring_hom.comp
theorem lift_unique (F : direct_limit G f → P) [is_ring_hom F] (x) :
F x = lift G f P (λ i x, F $ of G f i x) (λ i j hij x, by rw [of_f]) x :=
direct_limit.induction_on x $ λ i x, by rw lift_of
end direct_limit
end ring
namespace field
variables [Π i, field (G i)]
variables (f : Π i j, i ≤ j → G i → G j) [Π i j hij, is_ring_hom (f i j hij)]
variables [directed_system G f]
namespace direct_limit
instance nonzero_comm_ring : nonzero_comm_ring (ring.direct_limit G f) :=
{ zero_ne_one := nonempty.elim (by apply_instance) $ assume i : ι, begin
change (0 : ring.direct_limit G f) ≠ 1,
rw ← ring.direct_limit.of_one,
intros H, rcases ring.direct_limit.of.zero_exact H.symm with ⟨j, hij, hf⟩,
rw is_ring_hom.map_one (f i j hij) at hf,
exact one_ne_zero hf
end,
.. ring.direct_limit.comm_ring G f }
theorem exists_inv {p : ring.direct_limit G f} : p ≠ 0 → ∃ y, p * y = 1 :=
ring.direct_limit.induction_on p $ λ i x H,
⟨ring.direct_limit.of G f i (x⁻¹), by erw [← ring.direct_limit.of_mul,
mul_inv_cancel (assume h : x = 0, H $ by rw [h, ring.direct_limit.of_zero]),
ring.direct_limit.of_one]⟩
section
open_locale classical
noncomputable def inv (p : ring.direct_limit G f) : ring.direct_limit G f :=
if H : p = 0 then 0 else classical.some (direct_limit.exists_inv G f H)
protected theorem mul_inv_cancel {p : ring.direct_limit G f} (hp : p ≠ 0) : p * inv G f p = 1 :=
by rw [inv, dif_neg hp, classical.some_spec (direct_limit.exists_inv G f hp)]
protected theorem inv_mul_cancel {p : ring.direct_limit G f} (hp : p ≠ 0) : inv G f p * p = 1 :=
by rw [_root_.mul_comm, direct_limit.mul_inv_cancel G f hp]
protected noncomputable def field : field (ring.direct_limit G f) :=
{ inv := inv G f,
mul_inv_cancel := λ p, direct_limit.mul_inv_cancel G f,
inv_zero := dif_pos rfl,
.. direct_limit.nonzero_comm_ring G f }
end
end direct_limit
end field
|
29a7d9f476b67195d1feaff678966b03fef9229e | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/analysis/normed_space/add_torsor_bases.lean | fe7cb7765458d9ce0fd02cf96bc8328a2bfec6e0 | [
"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,959 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import analysis.normed_space.banach
import analysis.normed_space.finite_dimension
import analysis.convex.combination
import linear_algebra.affine_space.basis
import linear_algebra.affine_space.finite_dimensional
/-!
# Bases in normed affine spaces.
This file contains results about bases in normed affine spaces.
## Main definitions:
* `continuous_barycentric_coord`
* `is_open_map_barycentric_coord`
* `interior_convex_hull_aff_basis`
* `exists_subset_affine_independent_span_eq_top_of_open`
* `interior_convex_hull_nonempty_iff_aff_span_eq_top`
-/
section barycentric
variables {ι 𝕜 E P : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜]
variables [normed_group E] [normed_space 𝕜 E] [finite_dimensional 𝕜 E]
variables [metric_space P] [normed_add_torsor E P]
variables (b : affine_basis ι 𝕜 P)
@[continuity]
lemma continuous_barycentric_coord (i : ι) : continuous (b.coord i) :=
(b.coord i).continuous_of_finite_dimensional
local attribute [instance] finite_dimensional.complete
lemma is_open_map_barycentric_coord [nontrivial ι] (i : ι) :
is_open_map (b.coord i) :=
(b.coord i).is_open_map (continuous_barycentric_coord b i) (b.surjective_coord i)
end barycentric
open set
/-- Given a finite-dimensional normed real vector space, the interior of the convex hull of an
affine basis is the set of points whose barycentric coordinates are strictly positive with respect
to this basis.
TODO Restate this result for affine spaces (instead of vector spaces) once the definition of
convexity is generalised to this setting. -/
lemma interior_convex_hull_aff_basis {ι E : Type*} [fintype ι] [normed_group E] [normed_space ℝ E]
(b : affine_basis ι ℝ E) :
interior (convex_hull ℝ (range b.points)) = { x | ∀ i, 0 < b.coord i x } :=
begin
cases subsingleton_or_nontrivial ι with h h,
{ -- The zero-dimensional case.
haveI := h,
suffices : range (b.points) = univ, { simp [this], },
refine affine_subspace.eq_univ_of_subsingleton_span_eq_top _ b.tot,
rw ← image_univ,
exact subsingleton.image subsingleton_of_subsingleton b.points, },
{ -- The positive-dimensional case.
haveI : finite_dimensional ℝ E,
{ classical,
obtain ⟨i⟩ := (infer_instance : nonempty ι),
exact finite_dimensional.of_fintype_basis (b.basis_of i), },
have : convex_hull ℝ (range b.points) = ⋂ i, (b.coord i)⁻¹' Ici 0,
{ rw convex_hull_affine_basis_eq_nonneg_barycentric b, ext, simp, },
ext,
simp only [this, interior_Inter_of_fintype, ← is_open_map.preimage_interior_eq_interior_preimage
(is_open_map_barycentric_coord b _) (continuous_barycentric_coord b _),
interior_Ici, mem_Inter, mem_set_of_eq, mem_Ioi, mem_preimage], },
end
variables {V P : Type*} [normed_group V] [normed_space ℝ V] [metric_space P] [normed_add_torsor V P]
include V
open affine_map
/-- Given a set `s` of affine-independent points belonging to an open set `u`, we may extend `s` to
an affine basis, all of whose elements belong to `u`. -/
lemma exists_subset_affine_independent_span_eq_top_of_open {s u : set P} (hu : is_open u)
(hsu : s ⊆ u) (hne : s.nonempty) (h : affine_independent ℝ (coe : s → P)) :
∃ t : set P, s ⊆ t ∧ t ⊆ u ∧ affine_independent ℝ (coe : t → P) ∧ affine_span ℝ t = ⊤ :=
begin
obtain ⟨q, hq⟩ := hne,
obtain ⟨ε, hε, hεu⟩ := metric.is_open_iff.mp hu q (hsu hq),
obtain ⟨t, ht₁, ht₂, ht₃⟩ := exists_subset_affine_independent_affine_span_eq_top h,
let f : P → P := λ y, line_map q y (ε / 2 / (dist y q)),
have hf : ∀ y, f y ∈ u,
{ intros y,
apply hεu,
simp only [metric.mem_ball, f, line_map_apply, dist_vadd_left, norm_smul, real.norm_eq_abs,
dist_eq_norm_vsub V y q],
cases eq_or_ne (∥y -ᵥ q∥) 0 with hyq hyq, { rwa [hyq, mul_zero], },
rw [← norm_pos_iff, norm_norm] at hyq,
calc abs (ε / 2 / ∥y -ᵥ q∥) * ∥y -ᵥ q∥
= ε / 2 / ∥y -ᵥ q∥ * ∥y -ᵥ q∥ : by rw [abs_div, abs_of_pos (half_pos hε), abs_of_pos hyq]
... = ε / 2 : div_mul_cancel _ (ne_of_gt hyq)
... < ε : half_lt_self hε, },
have hεyq : ∀ (y ∉ s), ε / 2 / dist y q ≠ 0,
{ simp only [ne.def, div_eq_zero_iff, or_false, dist_eq_zero, bit0_eq_zero, one_ne_zero,
not_or_distrib, ne_of_gt hε, true_and, not_false_iff],
exact λ y h1 h2, h1 (h2.symm ▸ hq) },
classical,
let w : t → ℝˣ := λ p, if hp : (p : P) ∈ s then 1 else units.mk0 _ (hεyq ↑p hp),
refine ⟨set.range (λ (p : t), line_map q p (w p : ℝ)), _, _, _, _⟩,
{ intros p hp, use ⟨p, ht₁ hp⟩, simp [w, hp], },
{ intros y hy,
simp only [set.mem_range, set_coe.exists, subtype.coe_mk] at hy,
obtain ⟨p, hp, hyq⟩ := hy,
by_cases hps : p ∈ s;
simp only [w, hps, line_map_apply_one, units.coe_mk0, dif_neg, dif_pos, not_false_iff,
units.coe_one, subtype.coe_mk] at hyq;
rw ← hyq;
[exact hsu hps, exact hf p], },
{ exact (ht₂.units_line_map ⟨q, ht₁ hq⟩ w).range, },
{ rw [affine_span_eq_affine_span_line_map_units (ht₁ hq) w, ht₃], },
end
lemma interior_convex_hull_nonempty_iff_aff_span_eq_top [finite_dimensional ℝ V] {s : set V} :
(interior (convex_hull ℝ s)).nonempty ↔ affine_span ℝ s = ⊤ :=
begin
split,
{ rintros ⟨x, hx⟩,
obtain ⟨u, hu₁, hu₂, hu₃⟩ := mem_interior.mp hx,
let t : set V := {x},
obtain ⟨b, hb₁, hb₂, hb₃, hb₄⟩ := exists_subset_affine_independent_span_eq_top_of_open hu₂
(singleton_subset_iff.mpr hu₃) (singleton_nonempty x)
(affine_independent_of_subsingleton ℝ (coe : t → V)),
rw [eq_top_iff, ← hb₄, ← affine_span_convex_hull s],
mono,
exact hb₂.trans hu₁, },
{ intros h,
obtain ⟨t, hts, h_tot, h_ind⟩ := exists_affine_independent ℝ V s,
suffices : (interior (convex_hull ℝ (range (coe : t → V)))).nonempty,
{ rw [subtype.range_coe_subtype, set_of_mem_eq] at this,
apply nonempty.mono _ this,
mono* },
haveI : fintype t := fintype_of_fin_dim_affine_independent ℝ h_ind,
use finset.centroid ℝ (finset.univ : finset t) (coe : t → V),
rw [h, ← @set_of_mem_eq V t, ← subtype.range_coe_subtype] at h_tot,
let b : affine_basis t ℝ V := ⟨coe, h_ind, h_tot⟩,
rw interior_convex_hull_aff_basis b,
have htne : (finset.univ : finset t).nonempty,
{ simpa [finset.univ_nonempty_iff] using
affine_subspace.nonempty_of_affine_span_eq_top ℝ V V h_tot, },
simp [finset.centroid_def, b.coord_apply_combination_of_mem (finset.mem_univ _)
(finset.sum_centroid_weights_eq_one_of_nonempty ℝ (finset.univ : finset t) htne),
finset.centroid_weights_apply, nat.cast_pos, inv_pos, finset.card_pos.mpr htne], },
end
|
3dd9965d7ebc3c3d1d23be07ef65b4f39699be22 | 8f209eb34c0c4b9b6be5e518ebfc767a38bed79c | /code/src/internal/tactics.lean | fe56557063f5c57f48b834de0ff05129790b17d9 | [] | 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 | 1,836 | lean | import tactic
open tactic expr
namespace tactic
namespace interactive
setup_tactic_parser
private meta def try_specialize_all (v: pexpr) : tactic unit :=
do
ctx ← local_context,
ctx.mall (λ e,
do
try `[ specialize %%e %%v ],
return tt
),
skip
meta def induction_ant_disjoint (hp : parse cases_arg_p) (h : parse (tk "from" *> ident)) (rec_name : parse using_ident) (ids : parse with_ident_list)
(revert : parse $ (tk "generalizing" *> ident*)?) : tactic unit :=
do
induction hp rec_name ids revert,
propagate_tags `[
clear ant_disjoint
],
rotate_right 1,
propagate_tags `[
unfold Ant.disjoint_rhss at ant_disjoint,
try_specialize_all ```(ant_disjoint)
--clear ant_disjoint
],
rotate_right 1,
propagate_tags `[
unfold Ant.disjoint_rhss at ant_disjoint,
try_specialize_all ```(ant_disjoint.1),
try_specialize_all ```(ant_disjoint.2.1),
replace ant_disjoint := ant_disjoint.2.2
],
rotate_right 1,
skip
meta def inline (h : parse parser.pexpr): tactic unit :=
do
m ← match_local_const h,
rewrite
{ rules:= [{pos := {line := 43, column := 11}, symm := ff, rule := h}], end_pos:=none }
loc.wildcard,
tactic.clear_lst [m.fst]
meta def R_diverge_cases: tactic unit :=
`[
have R_diverge_cases := R_diverge_cases ant_tr ant_a,
cases R_diverge_cases,
cases R_diverge_cases with r R_diverge_cases,
cases R_diverge_cases with rs R_diverge_cases,
rw R_diverge_cases.2.2 at *,
have R_ant_tr := R_diverge_cases.2.1,
have ant_a := R_diverge_cases.1,
clear R_diverge_cases,
rotate 1,
rw R_diverge_cases.2 at *,
replace R_diverge_cases := R_diverge_cases.1,
rotate 1
]
end interactive
end tactic
|
b7118768fa75dfc7897462b1672d28c4af50f62e | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Init/Data/Range.lean | c9791906fca735eb02b3e19c40677d1ce717afc0 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 3,266 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Meta
namespace Std
-- We put `Range` in `Init` because we want the notation `[i:j]` without importing `Std`
-- We don't put `Range` in the top-level namespace to avoid collisions with user defined types
structure Range where
start : Nat := 0
stop : Nat
step : Nat := 1
instance : Membership Nat Range where
mem i r := r.start ≤ i ∧ i < r.stop
namespace Range
universe u v
@[inline] protected def forIn {β : Type u} {m : Type u → Type v} [Monad m] (range : Range) (init : β) (f : Nat → β → m (ForInStep β)) : m β :=
-- pass `stop` and `step` separately so the `range` object can be eliminated through inlining
let rec @[specialize] loop (fuel i stop step : Nat) (b : β) : m β := do
if i ≥ stop then
return b
else match fuel with
| 0 => pure b
| fuel+1 => match (← f i b) with
| ForInStep.done b => pure b
| ForInStep.yield b => loop fuel (i + step) stop step b
loop range.stop range.start range.stop range.step init
instance : ForIn m Range Nat where
forIn := Range.forIn
@[inline] protected def forIn' {β : Type u} {m : Type u → Type v} [Monad m] (range : Range) (init : β) (f : (i : Nat) → i ∈ range → β → m (ForInStep β)) : m β :=
let rec @[specialize] loop (start stop step : Nat) (f : (i : Nat) → start ≤ i ∧ i < stop → β → m (ForInStep β)) (fuel i : Nat) (hl : start ≤ i) (b : β) : m β := do
if hu : i < stop then
match fuel with
| 0 => pure b
| fuel+1 => match (← f i ⟨hl, hu⟩ b) with
| ForInStep.done b => pure b
| ForInStep.yield b => loop start stop step f fuel (i + step) (Nat.le_trans hl (Nat.le_add_right ..)) b
else
return b
loop range.start range.stop range.step f range.stop range.start (Nat.le_refl ..) init
instance : ForIn' m Range Nat inferInstance where
forIn' := Range.forIn'
@[inline] protected def forM {m : Type u → Type v} [Monad m] (range : Range) (f : Nat → m PUnit) : m PUnit :=
let rec @[specialize] loop (fuel i stop step : Nat) : m PUnit := do
if i ≥ stop then
pure ⟨⟩
else match fuel with
| 0 => pure ⟨⟩
| fuel+1 => f i; loop fuel (i + step) stop step
loop range.stop range.start range.stop range.step
instance : ForM m Range Nat where
forM := Range.forM
syntax:max "[" ":" term "]" : term
syntax:max "[" term ":" term "]" : term
syntax:max "[" ":" term ":" term "]" : term
syntax:max "[" term ":" term ":" term "]" : term
macro_rules
| `([ : $stop]) => `({ stop := $stop : Range })
| `([ $start : $stop ]) => `({ start := $start, stop := $stop : Range })
| `([ $start : $stop : $step ]) => `({ start := $start, stop := $stop, step := $step : Range })
| `([ : $stop : $step ]) => `({ stop := $stop, step := $step : Range })
end Range
end Std
theorem Membership.mem.upper {i : Nat} {r : Std.Range} (h : i ∈ r) : i < r.stop := by
simp [Membership.mem] at h
exact h.2
theorem Membership.mem.lower {i : Nat} {r : Std.Range} (h : i ∈ r) : r.start ≤ i := by
simp [Membership.mem] at h
exact h.1
|
5e4df86ab6874ebd409894a0f6d7bf7224fef900 | ec62863c729b7eedee77b86d974f2c529fa79d25 | /24/a.lean | 5a13d336d005fc8581421333227df487e3a93270 | [] | no_license | rwbarton/advent-of-lean-4 | 2ac9b17ba708f66051e3d8cd694b0249bc433b65 | 417c7e2718253ba7148c0279fcb251b6fc291477 | refs/heads/main | 1,675,917,092,057 | 1,609,864,581,000 | 1,609,864,581,000 | 317,700,289 | 24 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 922 | lean | import Std.Data.HashSet
open Std
instance : Hashable Int where
hash n := hash n.toNat
@[reducible] def Hex := Int × Int
def Hex.x : Hex → Int := Prod.fst
def Hex.y : Hex → Int := Prod.snd
instance : Add Hex where
add h₁ h₂ := ⟨h₁.x + h₂.x, h₁.y + h₂.y⟩
def parseLine : List Char → Hex
| [] => ⟨0, 0⟩
| 'e' :: rest => ⟨2, 0⟩ + parseLine rest
| 'n' :: 'e' :: rest => ⟨1, 1⟩ + parseLine rest
| 's' :: 'e' :: rest => ⟨1, -1⟩ + parseLine rest
| 'w' :: rest => ⟨-2, 0⟩ + parseLine rest
| 'n' :: 'w' :: rest => ⟨-1, 1⟩ + parseLine rest
| 's' :: 'w' :: rest => ⟨-1, -1⟩ + parseLine rest
| _ => panic! "invalid parse"
def main : IO Unit := do
let input ← IO.FS.lines "a.in"
let mut black := HashSet.empty
for h in input.map (parseLine ∘ String.toList) do
if black.contains h
then black := black.erase h
else black := black.insert h
IO.print s!"{black.size}\n"
|
4093ad9dde4c438bf6784be4e5b8678754c52fdc | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/group_theory/torsion.lean | d68a355364f9883a98c2eb9ac4feaf47119df3a0 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 14,840 | lean | /-
Copyright (c) 2022 Julian Berman. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Julian Berman
-/
import algebra.is_prime_pow
import group_theory.exponent
import group_theory.order_of_element
import group_theory.p_group
import group_theory.quotient_group
import group_theory.submonoid.operations
/-!
# Torsion groups
This file defines torsion groups, i.e. groups where all elements have finite order.
## Main definitions
* `monoid.is_torsion` a predicate asserting `G` is torsion, i.e. that all
elements are of finite order.
* `comm_group.torsion G`, the torsion subgroup of an abelian group `G`
* `comm_monoid.torsion G`, the above stated for commutative monoids
* `monoid.is_torsion_free`, asserting no nontrivial elements have finite order in `G`
* `add_monoid.is_torsion` and `add_monoid.is_torsion_free` the additive versions of the above
## Implementation
All torsion monoids are really groups (which is proven here as `monoid.is_torsion.group`), but since
the definition can be stated on monoids it is implemented on `monoid` to match other declarations in
the group theory library.
## Tags
periodic group, aperiodic group, torsion subgroup, torsion abelian group
## Future work
* generalize to π-torsion(-free) groups for a set of primes π
* free, free solvable and free abelian groups are torsion free
* complete direct and free products of torsion free groups are torsion free
* groups which are residually finite p-groups with respect to 2 distinct primes are torsion free
-/
variables {G H : Type*}
namespace monoid
variables (G) [monoid G]
/-- A predicate on a monoid saying that all elements are of finite order. -/
@[to_additive "A predicate on an additive monoid saying that all elements are of finite order."]
def is_torsion := ∀ g : G, is_of_fin_order g
/-- A monoid is not a torsion monoid if it has an element of infinite order. -/
@[simp, to_additive
"An additive monoid is not a torsion monoid if it has an element of infinite order."]
lemma not_is_torsion_iff : ¬ is_torsion G ↔ ∃ g : G, ¬is_of_fin_order g :=
by rw [is_torsion, not_forall]
end monoid
open monoid
/-- Torsion monoids are really groups. -/
@[to_additive "Torsion additive monoids are really additive groups"]
noncomputable def is_torsion.group [monoid G] (tG : is_torsion G) : group G :=
{ inv := λ g, g ^ (order_of g - 1),
mul_left_inv := λ g,
begin
erw [←pow_succ', tsub_add_cancel_of_le, pow_order_of_eq_one],
exact order_of_pos' (tG g)
end,
..‹monoid G› }
section group
variables [group G] {N : subgroup G} [group H]
/-- Subgroups of torsion groups are torsion groups. -/
@[to_additive "Subgroups of additive torsion groups are additive torsion groups."]
lemma is_torsion.subgroup (tG : is_torsion G) (H : subgroup G) : is_torsion H :=
λ h, (is_of_fin_order_iff_coe H.to_submonoid h).mpr $ tG h
/-- The image of a surjective torsion group homomorphism is torsion. -/
@[to_additive add_is_torsion.of_surjective
"The image of a surjective additive torsion group homomorphism is torsion."]
lemma is_torsion.of_surjective {f : G →* H} (hf : function.surjective f) (tG : is_torsion G) :
is_torsion H :=
λ h, begin
obtain ⟨g, hg⟩ := hf h,
rw ←hg,
exact f.is_of_fin_order (tG g),
end
/-- Torsion groups are closed under extensions. -/
@[to_additive add_is_torsion.extension_closed
"Additive torsion groups are closed under extensions."]
lemma is_torsion.extension_closed
{f : G →* H} (hN : N = f.ker) (tH : is_torsion H) (tN : is_torsion N) :
is_torsion G :=
λ g, (is_of_fin_order_iff_pow_eq_one _).mpr $ begin
obtain ⟨ngn, ngnpos, hngn⟩ := (is_of_fin_order_iff_pow_eq_one _).mp (tH $ f g),
have hmem := f.mem_ker.mpr ((f.map_pow g ngn).trans hngn),
lift g ^ ngn to N using hN.symm ▸ hmem with gn,
obtain ⟨nn, nnpos, hnn⟩ := (is_of_fin_order_iff_pow_eq_one _).mp (tN gn),
exact ⟨ngn * nn, mul_pos ngnpos nnpos, by rw [pow_mul, ←h, ←subgroup.coe_pow,
hnn, subgroup.coe_one]⟩
end
/-- The image of a quotient is torsion iff the group is torsion. -/
@[to_additive add_is_torsion.quotient_iff
"The image of a quotient is additively torsion iff the group is torsion."]
lemma is_torsion.quotient_iff
{f : G →* H} (hf : function.surjective f) (hN : N = f.ker) (tN : is_torsion N) :
is_torsion H ↔ is_torsion G :=
⟨λ tH, is_torsion.extension_closed hN tH tN, λ tG, is_torsion.of_surjective hf tG⟩
/-- If a group exponent exists, the group is torsion. -/
@[to_additive exponent_exists.is_add_torsion
"If a group exponent exists, the group is additively torsion."]
lemma exponent_exists.is_torsion (h : exponent_exists G) : is_torsion G := λ g, begin
obtain ⟨n, npos, hn⟩ := h,
exact (is_of_fin_order_iff_pow_eq_one g).mpr ⟨n, npos, hn g⟩,
end
/-- The group exponent exists for any bounded torsion group. -/
@[to_additive is_add_torsion.exponent_exists
"The group exponent exists for any bounded additive torsion group."]
lemma is_torsion.exponent_exists
(tG : is_torsion G) (bounded : (set.range (λ g : G, order_of g)).finite) :
exponent_exists G :=
exponent_exists_iff_ne_zero.mpr $
(exponent_ne_zero_iff_range_order_of_finite (λ g, order_of_pos' (tG g))).mpr bounded
/-- Finite groups are torsion groups. -/
@[to_additive is_add_torsion_of_finite "Finite additive groups are additive torsion groups."]
lemma is_torsion_of_finite [finite G] : is_torsion G :=
exponent_exists.is_torsion $ exponent_exists_iff_ne_zero.mpr exponent_ne_zero_of_finite
end group
section module
-- A (semi/)ring of scalars and a commutative monoid of elements
variables (R M : Type*) [add_comm_monoid M]
namespace add_monoid
/-- A module whose scalars are additively torsion is additively torsion. -/
lemma is_torsion.module_of_torsion [semiring R] [module R M] (tR : is_torsion R) :
is_torsion M := λ f, (is_of_fin_add_order_iff_nsmul_eq_zero _).mpr $ begin
obtain ⟨n, npos, hn⟩ := (is_of_fin_add_order_iff_nsmul_eq_zero _).mp (tR 1),
exact ⟨n, npos, by simp only [nsmul_eq_smul_cast R _ f, ←nsmul_one, hn, zero_smul]⟩,
end
/-- A module with a finite ring of scalars is additively torsion. -/
lemma is_torsion.module_of_finite [ring R] [finite R] [module R M] : is_torsion M :=
(is_add_torsion_of_finite : is_torsion R).module_of_torsion _ _
end add_monoid
end module
section comm_monoid
variables (G) [comm_monoid G]
namespace comm_monoid
/--
The torsion submonoid of a commutative monoid.
(Note that by `monoid.is_torsion.group` torsion monoids are truthfully groups.)
-/
@[to_additive add_torsion "The torsion submonoid of an additive commutative monoid."]
def torsion : submonoid G :=
{ carrier := {x | is_of_fin_order x},
one_mem' := is_of_fin_order_one,
mul_mem' := λ _ _ hx hy, hx.mul hy }
variable {G}
/-- Torsion submonoids are torsion. -/
@[to_additive "Additive torsion submonoids are additively torsion."]
lemma torsion.is_torsion : is_torsion $ torsion G :=
λ ⟨_, n, npos, hn⟩,
⟨n, npos, subtype.ext $
by rw [mul_left_iterate, _root_.mul_one, submonoid.coe_pow,
subtype.coe_mk, submonoid.coe_one, (is_periodic_pt_mul_iff_pow_eq_one _).mp hn]⟩
variables (G) (p : ℕ) [hp : fact p.prime]
include hp
/-- The `p`-primary component is the submonoid of elements with order prime-power of `p`. -/
@[to_additive
"The `p`-primary component is the submonoid of elements with additive order prime-power of `p`.",
simps]
def primary_component : submonoid G :=
{ carrier := {g | ∃ n : ℕ, order_of g = p ^ n},
one_mem' := ⟨0, by rw [pow_zero, order_of_one]⟩,
mul_mem' := λ g₁ g₂ hg₁ hg₂, exists_order_of_eq_prime_pow_iff.mpr $ begin
obtain ⟨m, hm⟩ := exists_order_of_eq_prime_pow_iff.mp hg₁,
obtain ⟨n, hn⟩ := exists_order_of_eq_prime_pow_iff.mp hg₂,
exact ⟨m + n, by rw [mul_pow, pow_add, pow_mul, hm, one_pow, monoid.one_mul,
mul_comm, pow_mul, hn, one_pow]⟩,
end }
variables {G} {p}
/-- Elements of the `p`-primary component have order `p^n` for some `n`. -/
@[to_additive "Elements of the `p`-primary component have additive order `p^n` for some `n`"]
lemma primary_component.exists_order_of_eq_prime_pow (g : comm_monoid.primary_component G p) :
∃ n : ℕ, order_of g = p ^ n :=
by simpa [primary_component] using g.property
/-- The `p`- and `q`-primary components are disjoint for `p ≠ q`. -/
@[to_additive "The `p`- and `q`-primary components are disjoint for `p ≠ q`."]
lemma primary_component.disjoint {p' : ℕ} [hp' : fact p'.prime] (hne : p ≠ p') :
disjoint (comm_monoid.primary_component G p) (comm_monoid.primary_component G p') :=
submonoid.disjoint_def.mpr $ λ g hgp hgp',
begin
obtain ⟨n, hn⟩ := primary_component.exists_order_of_eq_prime_pow ⟨g, set_like.mem_coe.mp hgp⟩,
obtain ⟨n', hn'⟩ := primary_component.exists_order_of_eq_prime_pow ⟨g, set_like.mem_coe.mp hgp'⟩,
have := mt (eq_of_prime_pow_eq (nat.prime_iff.mp hp.out) (nat.prime_iff.mp hp'.out)),
simp only [not_forall, exists_prop, not_lt, le_zero_iff, and_imp] at this,
rw [←order_of_submonoid, set_like.coe_mk] at hn hn',
have hnzero := this (hn.symm.trans hn') hne,
rwa [hnzero, pow_zero, order_of_eq_one_iff] at hn,
end
end comm_monoid
open comm_monoid (torsion)
namespace monoid.is_torsion
variable {G}
/-- The torsion submonoid of a torsion monoid is `⊤`. -/
@[simp, to_additive "The additive torsion submonoid of an additive torsion monoid is `⊤`."]
lemma torsion_eq_top (tG : is_torsion G) : torsion G = ⊤ := by ext; tauto
/-- A torsion monoid is isomorphic to its torsion submonoid. -/
@[to_additive "An additive torsion monoid is isomorphic to its torsion submonoid.", simps]
def torsion_mul_equiv (tG : is_torsion G) : torsion G ≃* G :=
(mul_equiv.submonoid_congr tG.torsion_eq_top).trans submonoid.top_equiv
end monoid.is_torsion
/-- Torsion submonoids of a torsion submonoid are isomorphic to the submonoid. -/
@[simp, to_additive add_comm_monoid.torsion.of_torsion
"Additive torsion submonoids of an additive torsion submonoid are isomorphic to the submonoid."]
def torsion.of_torsion : (torsion (torsion G)) ≃* (torsion G) :=
monoid.is_torsion.torsion_mul_equiv comm_monoid.torsion.is_torsion
end comm_monoid
section comm_group
variables (G) [comm_group G]
namespace comm_group
/-- The torsion subgroup of an abelian group. -/
@[to_additive "The torsion subgroup of an additive abelian group."]
def torsion : subgroup G := { comm_monoid.torsion G with inv_mem' := λ x, is_of_fin_order.inv }
/-- The torsion submonoid of an abelian group equals the torsion subgroup as a submonoid. -/
@[to_additive add_torsion_eq_add_torsion_submonoid
"The additive torsion submonoid of an abelian group equals the torsion subgroup as a submonoid."]
lemma torsion_eq_torsion_submonoid : comm_monoid.torsion G = (torsion G).to_submonoid := rfl
variables (p : ℕ) [hp : fact p.prime]
include hp
/-- The `p`-primary component is the subgroup of elements with order prime-power of `p`. -/
@[to_additive
"The `p`-primary component is the subgroup of elements with additive order prime-power of `p`.",
simps]
def primary_component : subgroup G :=
{ comm_monoid.primary_component G p with inv_mem' := λ g ⟨n, hn⟩, ⟨n, (order_of_inv g).trans hn⟩ }
variables {G} {p}
/-- The `p`-primary component is a `p` group. -/
lemma primary_component.is_p_group : is_p_group p $ primary_component G p :=
λ g, (propext exists_order_of_eq_prime_pow_iff.symm).mpr
(comm_monoid.primary_component.exists_order_of_eq_prime_pow g)
end comm_group
end comm_group
namespace monoid
variables (G) [monoid G]
/-- A predicate on a monoid saying that only 1 is of finite order. -/
@[to_additive "A predicate on an additive monoid saying that only 0 is of finite order."]
def is_torsion_free := ∀ g : G, g ≠ 1 → ¬is_of_fin_order g
/-- A nontrivial monoid is not torsion-free if any nontrivial element has finite order. -/
@[simp, to_additive
"An additive monoid is not torsion free if any nontrivial element has finite order."]
lemma not_is_torsion_free_iff : ¬ (is_torsion_free G) ↔ ∃ g : G, g ≠ 1 ∧ is_of_fin_order g :=
by simp_rw [is_torsion_free, ne.def, not_forall, not_not, exists_prop]
end monoid
section group
open monoid
variables [group G]
/-- A nontrivial torsion group is not torsion-free. -/
@[to_additive add_monoid.is_torsion.not_torsion_free
"A nontrivial additive torsion group is not torsion-free."]
lemma is_torsion.not_torsion_free [hN : nontrivial G] : is_torsion G → ¬is_torsion_free G :=
λ tG, (not_is_torsion_free_iff _).mpr $ begin
obtain ⟨x, hx⟩ := (nontrivial_iff_exists_ne (1 : G)).mp hN,
exact ⟨x, hx, tG x⟩,
end
/-- A nontrivial torsion-free group is not torsion. -/
@[to_additive add_monoid.is_torsion_free.not_torsion
"A nontrivial torsion-free additive group is not torsion."]
lemma is_torsion_free.not_torsion [hN : nontrivial G] : is_torsion_free G → ¬is_torsion G :=
λ tfG, (not_is_torsion_iff _).mpr $ begin
obtain ⟨x, hx⟩ := (nontrivial_iff_exists_ne (1 : G)).mp hN,
exact ⟨x, (tfG x) hx⟩,
end
/-- Subgroups of torsion-free groups are torsion-free. -/
@[to_additive "Subgroups of additive torsion-free groups are additively torsion-free."]
lemma is_torsion_free.subgroup (tG : is_torsion_free G) (H : subgroup G) : is_torsion_free H :=
λ h hne, (is_of_fin_order_iff_coe H.to_submonoid h).not.mpr $
tG h $ by norm_cast; simp [hne, not_false_iff]
/-- Direct products of torsion free groups are torsion free. -/
@[to_additive add_monoid.is_torsion_free.prod
"Direct products of additive torsion free groups are torsion free."]
lemma is_torsion_free.prod
{η : Type*} {Gs : η → Type*} [∀ i, group (Gs i)] (tfGs : ∀ i, is_torsion_free (Gs i)) :
is_torsion_free $ Π i, Gs i :=
λ w hne h, hne $ funext $ λ i, not_not.mp $ mt (tfGs i (w i)) $ not_not.mpr $ h.apply i
end group
section comm_group
open monoid (is_torsion_free)
open comm_group (torsion)
variables (G) [comm_group G]
/-- Quotienting a group by its torsion subgroup yields a torsion free group. -/
@[to_additive add_is_torsion_free.quotient_torsion
"Quotienting a group by its additive torsion subgroup yields an additive torsion free group."]
lemma is_torsion_free.quotient_torsion : is_torsion_free $ G ⧸ torsion G :=
λ g hne hfin, hne $ begin
induction g using quotient_group.induction_on',
obtain ⟨m, mpos, hm⟩ := (is_of_fin_order_iff_pow_eq_one _).mp hfin,
obtain ⟨n, npos, hn⟩ :=
(is_of_fin_order_iff_pow_eq_one _).mp ((quotient_group.eq_one_iff _).mp hm),
exact (quotient_group.eq_one_iff g).mpr
((is_of_fin_order_iff_pow_eq_one _).mpr ⟨m * n, mul_pos mpos npos, (pow_mul g m n).symm ▸ hn⟩),
end
end comm_group
|
05eedd1ca56f3a27efab04ddbb1ba951c4cd1699 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/logic/function.lean | 43ea7e7c491ad12910e75703a6f8b16ae6e905e9 | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 9,154 | lean | /-
Copyright (c) 2016 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
Miscellaneous function constructions and lemmas.
-/
import logic.basic data.option.defs
universes u v w
namespace function
section
variables {α : Sort u} {β : Sort v} {f : α → β}
lemma hfunext {α α': Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : Πa, β a} {f' : Πa, β' a}
(hα : α = α') (h : ∀a a', a == a' → f a == f' a') : f == f' :=
begin
subst hα,
have : ∀a, f a == f' a,
{ intro a, exact h a a (heq.refl a) },
have : β = β',
{ funext a, exact type_eq_of_heq (this a) },
subst this,
apply heq_of_eq,
funext a,
exact eq_of_heq (this a)
end
lemma funext_iff {β : α → Sort*} {f₁ f₂ : Π (x : α), β x} : f₁ = f₂ ↔ (∀a, f₁ a = f₂ a) :=
iff.intro (assume h a, h ▸ rfl) funext
lemma comp_apply {α : Sort u} {β : Sort v} {φ : Sort w} (f : β → φ) (g : α → β) (a : α) :
(f ∘ g) a = f (g a) := rfl
@[simp] theorem injective.eq_iff (I : injective f) {a b : α} :
f a = f b ↔ a = b :=
⟨@I _ _, congr_arg f⟩
lemma injective.ne (hf : function.injective f) {a₁ a₂ : α} : a₁ ≠ a₂ → f a₁ ≠ f a₂ :=
mt (assume h, hf h)
def injective.decidable_eq [decidable_eq β] (I : injective f) : decidable_eq α
| a b := decidable_of_iff _ I.eq_iff
instance decidable_eq_pfun (p : Prop) [decidable p] (α : p → Type*)
[Π hp, decidable_eq (α hp)] : decidable_eq (Π hp, α hp)
| f g := decidable_of_iff (∀ hp, f hp = g hp) funext_iff.symm
theorem cantor_surjective {α} (f : α → α → Prop) : ¬ function.surjective f | h :=
let ⟨D, e⟩ := h (λ a, ¬ f a a) in
(iff_not_self (f D D)).1 $ iff_of_eq (congr_fun e D)
theorem cantor_injective {α : Type*} (f : (α → Prop) → α) :
¬ function.injective f | i :=
cantor_surjective (λ a b, ∀ U, a = f U → U b) $
surjective_of_has_right_inverse ⟨f, λ U, funext $
λ a, propext ⟨λ h, h U rfl, λ h' U' e, i e ▸ h'⟩⟩
/-- `g` is a partial inverse to `f` (an injective but not necessarily
surjective function) if `g y = some x` implies `f x = y`, and `g y = none`
implies that `y` is not in the range of `f`. -/
def is_partial_inv {α β} (f : α → β) (g : β → option α) : Prop :=
∀ x y, g y = some x ↔ f x = y
theorem is_partial_inv_left {α β} {f : α → β} {g} (H : is_partial_inv f g) (x) : g (f x) = some x :=
(H _ _).2 rfl
theorem injective_of_partial_inv {α β} {f : α → β} {g} (H : is_partial_inv f g) : injective f :=
λ a b h, option.some.inj $ ((H _ _).2 h).symm.trans ((H _ _).2 rfl)
theorem injective_of_partial_inv_right {α β} {f : α → β} {g} (H : is_partial_inv f g)
(x y b) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y :=
((H _ _).1 h₁).symm.trans ((H _ _).1 h₂)
theorem left_inverse.comp_eq_id {f : α → β} {g : β → α} (h : left_inverse f g) : f ∘ g = id :=
funext h
theorem right_inverse.comp_eq_id {f : α → β} {g : β → α} (h : right_inverse f g) : g ∘ f = id :=
funext h
theorem left_inverse.comp {γ} {f : α → β} {g : β → α} {h : β → γ} {i : γ → β}
(hf : left_inverse f g) (hh : left_inverse h i) : left_inverse (h ∘ f) (g ∘ i) :=
assume a, show h (f (g (i a))) = a, by rw [hf (i a), hh a]
theorem right_inverse.comp {γ} {f : α → β} {g : β → α} {h : β → γ} {i : γ → β}
(hf : right_inverse f g) (hh : right_inverse h i) : right_inverse (h ∘ f) (g ∘ i) :=
left_inverse.comp hh hf
local attribute [instance] classical.prop_decidable
/-- We can use choice to construct explicitly a partial inverse for
a given injective function `f`. -/
noncomputable def partial_inv {α β} (f : α → β) (b : β) : option α :=
if h : ∃ a, f a = b then some (classical.some h) else none
theorem partial_inv_of_injective {α β} {f : α → β} (I : injective f) :
is_partial_inv f (partial_inv f) | a b :=
⟨λ h, if h' : ∃ a, f a = b then begin
rw [partial_inv, dif_pos h'] at h,
injection h with h, subst h,
apply classical.some_spec h'
end else by rw [partial_inv, dif_neg h'] at h; contradiction,
λ e, e ▸ have h : ∃ a', f a' = f a, from ⟨_, rfl⟩,
(dif_pos h).trans (congr_arg _ (I $ classical.some_spec h))⟩
theorem partial_inv_left {α β} {f : α → β} (I : injective f) : ∀ x, partial_inv f (f x) = some x :=
is_partial_inv_left (partial_inv_of_injective I)
end
section inv_fun
variables {α : Type u} [inhabited α] {β : Sort v} {f : α → β} {s : set α} {a : α} {b : β}
local attribute [instance] classical.prop_decidable
/-- Construct the inverse for a function `f` on domain `s`. -/
noncomputable def inv_fun_on (f : α → β) (s : set α) (b : β) : α :=
if h : ∃a, a ∈ s ∧ f a = b then classical.some h else default α
theorem inv_fun_on_pos (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b :=
by rw [bex_def] at h; rw [inv_fun_on, dif_pos h]; exact classical.some_spec h
theorem inv_fun_on_mem (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s := (inv_fun_on_pos h).left
theorem inv_fun_on_eq (h : ∃a∈s, f a = b) : f (inv_fun_on f s b) = b := (inv_fun_on_pos h).right
theorem inv_fun_on_eq' (h : ∀ x y ∈ s, f x = f y → x = y) (ha : a ∈ s) :
inv_fun_on f s (f a) = a :=
have ∃a'∈s, f a' = f a, from ⟨a, ha, rfl⟩,
h _ _ (inv_fun_on_mem this) ha (inv_fun_on_eq this)
theorem inv_fun_on_neg (h : ¬ ∃a∈s, f a = b) : inv_fun_on f s b = default α :=
by rw [bex_def] at h; rw [inv_fun_on, dif_neg h]
/-- The inverse of a function (which is a left inverse if `f` is injective
and a right inverse if `f` is surjective). -/
noncomputable def inv_fun (f : α → β) : β → α := inv_fun_on f set.univ
theorem inv_fun_eq (h : ∃a, f a = b) : f (inv_fun f b) = b :=
inv_fun_on_eq $ let ⟨a, ha⟩ := h in ⟨a, trivial, ha⟩
lemma inv_fun_neg (h : ¬ ∃ a, f a = b) : inv_fun f b = default α :=
by refine inv_fun_on_neg (mt _ h); exact assume ⟨a, _, ha⟩, ⟨a, ha⟩
theorem inv_fun_eq_of_injective_of_right_inverse {g : β → α}
(hf : injective f) (hg : right_inverse g f) : inv_fun f = g :=
funext $ assume b,
hf begin rw [hg b], exact inv_fun_eq ⟨g b, hg b⟩ end
lemma right_inverse_inv_fun (hf : surjective f) : right_inverse (inv_fun f) f :=
assume b, inv_fun_eq $ hf b
lemma left_inverse_inv_fun (hf : injective f) : left_inverse (inv_fun f) f :=
assume b,
have f (inv_fun f (f b)) = f b,
from inv_fun_eq ⟨b, rfl⟩,
hf this
lemma inv_fun_surjective (hf : injective f) : surjective (inv_fun f) :=
surjective_of_has_right_inverse ⟨_, left_inverse_inv_fun hf⟩
lemma inv_fun_comp (hf : injective f) : inv_fun f ∘ f = id := funext $ left_inverse_inv_fun hf
lemma injective.has_left_inverse (hf : injective f) : has_left_inverse f :=
⟨inv_fun f, left_inverse_inv_fun hf⟩
lemma injective_iff_has_left_inverse : injective f ↔ has_left_inverse f :=
⟨injective.has_left_inverse, injective_of_has_left_inverse⟩
end inv_fun
section surj_inv
variables {α : Sort u} {β : Sort v} {f : α → β}
/-- The inverse of a surjective function. (Unlike `inv_fun`, this does not require
`α` to be inhabited.) -/
noncomputable def surj_inv {f : α → β} (h : surjective f) (b : β) : α := classical.some (h b)
lemma surj_inv_eq (h : surjective f) (b) : f (surj_inv h b) = b := classical.some_spec (h b)
lemma right_inverse_surj_inv (hf : surjective f) : right_inverse (surj_inv hf) f :=
surj_inv_eq hf
lemma left_inverse_surj_inv (hf : bijective f) : left_inverse (surj_inv hf.2) f :=
right_inverse_of_injective_of_left_inverse hf.1 (right_inverse_surj_inv hf.2)
lemma surjective.has_right_inverse (hf : surjective f) : has_right_inverse f :=
⟨_, right_inverse_surj_inv hf⟩
lemma surjective_iff_has_right_inverse : surjective f ↔ has_right_inverse f :=
⟨surjective.has_right_inverse, surjective_of_has_right_inverse⟩
lemma bijective_iff_has_inverse : bijective f ↔ ∃ g, left_inverse g f ∧ right_inverse g f :=
⟨λ hf, ⟨_, left_inverse_surj_inv hf, right_inverse_surj_inv hf.2⟩,
λ ⟨g, gl, gr⟩, ⟨injective_of_left_inverse gl, surjective_of_has_right_inverse ⟨_, gr⟩⟩⟩
lemma injective_surj_inv (h : surjective f) : injective (surj_inv h) :=
injective_of_has_left_inverse ⟨f, right_inverse_surj_inv h⟩
end surj_inv
section update
variables {α : Sort u} {β : α → Sort v} [decidable_eq α]
def update (f : Πa, β a) (a' : α) (v : β a') (a : α) : β a :=
if h : a = a' then eq.rec v h.symm else f a
@[simp] lemma update_same {a : α} {v : β a} {f : Πa, β a} : update f a v a = v :=
dif_pos rfl
@[simp] lemma update_noteq {a a' : α} {v : β a'} {f : Πa, β a} (h : a ≠ a') : update f a' v a = f a :=
dif_neg h
end update
lemma uncurry_def {α β γ} (f : α → β → γ) : uncurry f = (λp, f p.1 p.2) :=
funext $ assume ⟨a, b⟩, rfl
def restrict {α β} (f : α → β) (s : set α) : subtype s → β := λ x, f x.val
theorem restrict_eq {α β} (f : α → β) (s : set α): function.restrict f s = f ∘ (@subtype.val _ s) := rfl
end function
|
1549387d98e7cc2790db3e0d94e616696e2d6b6c | 1a0ffdcf53257a17fc84389d095ed455abfee1f4 | /src/mobius.lean | 21aa581882c9479ecde09a614a31f698eab4830e | [] | no_license | Nolrai/mobius | e0add98c20bbdd2cc3b2b4a1c5b85615698f13d9 | 34c972f4eb458d8c625cd0d74c50b78629e8d833 | refs/heads/master | 1,669,056,745,051 | 1,594,030,395,000 | 1,594,030,395,000 | 277,259,867 | 0 | 0 | null | 1,594,485,089,000 | 1,593,937,214,000 | Lean | UTF-8 | Lean | false | false | 2,725 | lean | import data.matrix.basic
import data.matrix.notation
import data.complex.basic
import wheel
noncomputable def non_zero : submonoid ℂ :=
{ carrier := (≠ 0),
one_mem' := by {unfold has_mem.mem set.mem, norm_num},
mul_mem' :=
begin unfold has_mem.mem set.mem, intros a b a0 b0 ab0
, have h : a = 0 ∨ b = 0
, {apply eq_zero_or_eq_zero_of_mul_eq_zero ab0}
, cases h; contradiction
end
}
-- the Reimann sphere with an addition point to represent 0/0.
-- The term wheel is inspired by the topological picture ⊙ of the projective line together with an extra point ⊥ = 0/0.
inductive ReimannWheel
| mk : ℂ → ReimannWheel
| inf : ReimannWheel
| bot : ReimannWheel
notation `ℂ∞` := ReimannWheel
open ReimannWheel
def add : ℂ∞ → ℂ∞ → ℂ∞
| (mk z₀) (mk z₁) := mk (z₀ + z₁)
| bot _ := bot
| _ bot := bot
| inf _ := inf
| _ inf := inf
lemma ReimannWheel.add_assoc : associative add :=
begin intros x y z
, cases x; cases y; cases z; unfold add
, rw add_assoc
end
instance ReimannWheel.has_zero : has_zero ReimannWheel := ⟨mk 0⟩
lemma mk_0_eq_0 : mk 0 = 0 := by {unfold has_zero.zero}
lemma ReimannWheel.add_zero : ∀ x, add x 0 = x
| (mk z) := by {rw ← mk_0_eq_0, unfold add, rw add_zero}
| inf := by {rw ← mk_0_eq_0, unfold add}
| bot := by {rw ← mk_0_eq_0, unfold add}
lemma ReimannWheel.zero_add : ∀ x, add 0 x = x
| (mk z) := by {rw ← mk_0_eq_0, unfold add, rw zero_add}
| inf := by {rw ← mk_0_eq_0, unfold add}
| bot := by {rw ← mk_0_eq_0, unfold add}
lemma ReimannWheel.add_comm : ∀ x y, add x y = add y x
| x y := by {cases x; cases y; unfold add; rw add_comm}
noncomputable def mul : ℂ∞ → ℂ∞ → ℂ∞
| (mk z₀) (mk z₁) := mk (z₀ * z₁)
| bot _ := bot
| _ bot := bot
| inf (mk z) := if z = 0 then bot else inf
| (mk z) inf := if z = 0 then bot else inf
| inf inf := inf
lemma ReimannWheel.mul_assoc : associative mul :=
begin intros x y z
, cases x; cases y; cases z; unfold mul
, {rw mul_assoc}
end
instance ReimannWheelIsWheel : wheel ReimannWheel :=
{ add := add,
add_assoc := by {unfold has_add.add, apply ReimannWheel.add_assoc},
zero_add := by {unfold has_add.add, apply ReimannWheel.zero_add},
add_zero := by {unfold has_add.add, apply ReimannWheel.add_zero},
add_comm := by {unfold has_add.add, apply ReimannWheel.add_comm},
mul := mul,
mul_assoc := by {unfold has_mul.mul, apply ReimannWheel.mul_assoc},
one := mk 1,
one_mul := _,
mul_one := _,
mul_comm := _,
inv := _,
inv_involution := _,
inv_mult := _,
left_distrib := _,
left_distrib_cancel := _,
zero_mult_zero := _,
pull_out_zero := _,
bot_absorbsive := _ }
notation `M2` T := matrix (fin 2) (fin 2) T
|
63ff1fd579a6a11397663a987303ad69d3d1b14c | 26ac254ecb57ffcb886ff709cf018390161a9225 | /test/conv/apply_congr.lean | b3803216f8c16aec5a3e3971de6b69b3ce60c05a | [
"Apache-2.0"
] | permissive | eric-wieser/mathlib | 42842584f584359bbe1fc8b88b3ff937c8acd72d | d0df6b81cd0920ad569158c06a3fd5abb9e63301 | refs/heads/master | 1,669,546,404,255 | 1,595,254,668,000 | 1,595,254,668,000 | 281,173,504 | 0 | 0 | Apache-2.0 | 1,595,263,582,000 | 1,595,263,581,000 | null | UTF-8 | Lean | false | false | 3,154 | lean | /-
Copyright (c) 2019 Lucas Allen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Lucas Allen, Scott Morrison
-/
import algebra.big_operators
import data.finsupp
import tactic.converter.apply_congr
import tactic.interactive
example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) :
finset.sum S f = finset.sum S g :=
begin
conv_lhs {
-- If we just call `congr` here, in the second goal we're helpless,
-- because we are only given the opportunity to rewrite `f`.
-- However `apply_congr` uses the appropriate `@[congr]` lemma,
-- so we get to rewrite `f x`, in the presence of the crucial `H : x ∈ S` hypothesis.
apply_congr,
skip,
simp [h, H],
}
end
-- Again, with some `guard` statements.
example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) :
finset.sum S f = finset.sum S g :=
begin
conv_lhs {
apply_congr finset.sum_congr,
-- (See the note about get_goals/set_goals inside apply_congr)
(do ng ← tactic.num_goals, guard $ ng = 2),
guard_target S,
skip,
guard_target f x,
simp [h, H]
}
end
-- Verify we can `rw` as well as `simp`.
example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) :
finset.sum S f = finset.sum S g :=
by conv_lhs { apply_congr, skip, rw h x H, }
-- Check that the appropriate `@[congr]` lemma is automatically selected.
example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) :
finset.prod S f = finset.prod S g :=
by conv_lhs { apply_congr, skip, simp [h, H], }
example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) :
finset.fold (+) 0 f S = finset.fold (+) 0 g S :=
begin
-- This time, the automatically selected congruence lemma is "too good"!
-- `finset.sum_congr` matches, and so the `conv` block actually
-- rewrites the left hand side into a `finset.sum`.
conv_lhs { apply_congr, skip, simp [h, H], },
-- So we need a `refl` to identify that we're done.
refl,
end
-- This can be avoided by selecting the congruence lemma by hand.
example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) :
finset.fold (+) 0 f S = finset.fold (+) 0 g S :=
begin
conv_lhs { apply_congr finset.fold_congr, simp [h, H], },
end
example (f : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = 0) :
finset.sum S f = 0 :=
begin
conv_lhs { apply_congr, skip, simp [h, H], },
simp,
end
-- An example using `finsupp.sum`
open_locale classical
example {k G : Type} [semiring k] [group G]
(g : G →₀ k) (a₁ x : G) (b₁ : k)
(t : ∀ (a₂ : G), a₁ * a₂ = x ↔ a₁⁻¹ * x = a₂) :
g.sum (λ (a₂ : G) (b₂ : k), ite (a₁ * a₂ = x) (b₁ * b₂) 0) = b₁ * g (a₁⁻¹ * x) :=
begin
-- In fact, `congr` works fine here, because our rewrite works globally.
conv_lhs { apply_congr, skip, dsimp, rw t, },
rw finset.sum_ite_eq g.support, -- it's a pity we can't just use `simp` here.
split_ifs,
{ refl, },
{ simp [finsupp.not_mem_support_iff.1 h], },
end
example : true :=
begin
success_if_fail { conv { apply_congr, }, },
trivial
end
|
04c9514510913f0cb93732c30fcbea329e526710 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/analysis/special_functions/pow.lean | 014d93414a15859008be1b0cd8b6b48cad72ba63 | [
"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 | 85,321 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel,
Rémy Degenne, David Loeffler
-/
import analysis.special_functions.complex.log
/-!
# Power function on `ℂ`, `ℝ`, `ℝ≥0`, and `ℝ≥0∞`
We construct the power functions `x ^ y` where
* `x` and `y` are complex numbers,
* or `x` and `y` are real numbers,
* or `x` is a nonnegative real number and `y` is a real number;
* or `x` is a number from `[0, +∞]` (a.k.a. `ℝ≥0∞`) and `y` is a real number.
We also prove basic properties of these functions.
-/
noncomputable theory
open_locale classical real topological_space nnreal ennreal filter big_operators
open filter finset set
namespace complex
/-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal
determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for
`y ≠ 0`. -/
noncomputable def cpow (x y : ℂ) : ℂ :=
if x = 0
then if y = 0
then 1
else 0
else exp (log x * y)
noncomputable instance : has_pow ℂ ℂ := ⟨cpow⟩
@[simp] lemma cpow_eq_pow (x y : ℂ) : cpow x y = x ^ y := rfl
lemma cpow_def (x y : ℂ) : x ^ y =
if x = 0
then if y = 0
then 1
else 0
else exp (log x * y) := rfl
lemma cpow_def_of_ne_zero {x : ℂ} (hx : x ≠ 0) (y : ℂ) : x ^ y = exp (log x * y) := if_neg hx
@[simp] lemma cpow_zero (x : ℂ) : x ^ (0 : ℂ) = 1 := by simp [cpow_def]
@[simp] lemma cpow_eq_zero_iff (x y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 :=
by { simp only [cpow_def], split_ifs; simp [*, exp_ne_zero] }
@[simp] lemma zero_cpow {x : ℂ} (h : x ≠ 0) : (0 : ℂ) ^ x = 0 :=
by simp [cpow_def, *]
lemma zero_cpow_eq_iff {x : ℂ} {a : ℂ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) :=
begin
split,
{ intros hyp,
simp [cpow_def] at hyp,
by_cases x = 0,
{ subst h, simp only [if_true, eq_self_iff_true] at hyp, right, exact ⟨rfl, hyp.symm⟩},
{ rw if_neg h at hyp, left, exact ⟨h, hyp.symm⟩, }, },
{ rintro (⟨h, rfl⟩|⟨rfl,rfl⟩),
{ exact zero_cpow h, },
{ exact cpow_zero _, }, },
end
lemma eq_zero_cpow_iff {x : ℂ} {a : ℂ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) :=
by rw [←zero_cpow_eq_iff, eq_comm]
@[simp] lemma cpow_one (x : ℂ) : x ^ (1 : ℂ) = x :=
if hx : x = 0 then by simp [hx, cpow_def]
else by rw [cpow_def, if_neg (one_ne_zero : (1 : ℂ) ≠ 0), if_neg hx, mul_one, exp_log hx]
@[simp] lemma one_cpow (x : ℂ) : (1 : ℂ) ^ x = 1 :=
by rw cpow_def; split_ifs; simp [one_ne_zero, *] at *
lemma cpow_add {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z :=
by simp [cpow_def]; simp [*, exp_add, mul_add] at *
lemma cpow_mul {x y : ℂ} (z : ℂ) (h₁ : -π < (log x * y).im) (h₂ : (log x * y).im ≤ π) :
x ^ (y * z) = (x ^ y) ^ z :=
begin
simp only [cpow_def],
split_ifs;
simp [*, exp_ne_zero, log_exp h₁ h₂, mul_assoc] at *
end
lemma cpow_neg (x y : ℂ) : x ^ -y = (x ^ y)⁻¹ :=
by simp [cpow_def]; split_ifs; simp [exp_neg]
lemma cpow_sub {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y - z) = x ^ y / x ^ z :=
by rw [sub_eq_add_neg, cpow_add _ _ hx, cpow_neg, div_eq_mul_inv]
lemma cpow_neg_one (x : ℂ) : x ^ (-1 : ℂ) = x⁻¹ :=
by simpa using cpow_neg x 1
@[simp, norm_cast] lemma cpow_nat_cast (x : ℂ) : ∀ (n : ℕ), x ^ (n : ℂ) = x ^ n
| 0 := by simp
| (n + 1) := if hx : x = 0 then by simp only [hx, pow_succ,
complex.zero_cpow (nat.cast_ne_zero.2 (nat.succ_ne_zero _)), zero_mul]
else by simp [cpow_add, hx, pow_add, cpow_nat_cast n]
@[simp] lemma cpow_two (x : ℂ) : x ^ (2 : ℂ) = x ^ 2 :=
by { rw ← cpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] }
@[simp, norm_cast] lemma cpow_int_cast (x : ℂ) : ∀ (n : ℤ), x ^ (n : ℂ) = x ^ n
| (n : ℕ) := by simp; refl
| -[1+ n] := by rw zpow_neg_succ_of_nat;
simp only [int.neg_succ_of_nat_coe, int.cast_neg, complex.cpow_neg, inv_eq_one_div,
int.cast_coe_nat, cpow_nat_cast]
lemma cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℂ)) ^ n = x :=
begin
suffices : im (log x * n⁻¹) ∈ Ioc (-π) π,
{ rw [← cpow_nat_cast, ← cpow_mul _ this.1 this.2, inv_mul_cancel, cpow_one],
exact_mod_cast hn },
rw [mul_comm, ← of_real_nat_cast, ← of_real_inv, of_real_mul_im, ← div_eq_inv_mul],
rw [← pos_iff_ne_zero] at hn,
have hn' : 0 < (n : ℝ), by assumption_mod_cast,
have hn1 : 1 ≤ (n : ℝ), by exact_mod_cast (nat.succ_le_iff.2 hn),
split,
{ rw lt_div_iff hn',
calc -π * n ≤ -π * 1 : mul_le_mul_of_nonpos_left hn1 (neg_nonpos.2 real.pi_pos.le)
... = -π : mul_one _
... < im (log x) : neg_pi_lt_log_im _ },
{ rw div_le_iff hn',
calc im (log x) ≤ π : log_im_le_pi _
... = π * 1 : (mul_one π).symm
... ≤ π * n : mul_le_mul_of_nonneg_left hn1 real.pi_pos.le }
end
end complex
section lim
open complex
variables {α : Type*}
lemma zero_cpow_eq_nhds {b : ℂ} (hb : b ≠ 0) :
(0 : ℂ).cpow =ᶠ[𝓝 b] 0 :=
begin
suffices : ∀ᶠ (x : ℂ) in (𝓝 b), x ≠ 0,
from this.mono (λ x hx, by rw [cpow_eq_pow, zero_cpow hx, pi.zero_apply]),
exact is_open.eventually_mem is_open_ne hb,
end
lemma cpow_eq_nhds {a b : ℂ} (ha : a ≠ 0) :
(λ x, x.cpow b) =ᶠ[𝓝 a] λ x, exp (log x * b) :=
begin
suffices : ∀ᶠ (x : ℂ) in (𝓝 a), x ≠ 0,
from this.mono (λ x hx, by { dsimp only, rw [cpow_eq_pow, cpow_def_of_ne_zero hx], }),
exact is_open.eventually_mem is_open_ne ha,
end
lemma cpow_eq_nhds' {p : ℂ × ℂ} (hp_fst : p.fst ≠ 0) :
(λ x, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) :=
begin
suffices : ∀ᶠ (x : ℂ × ℂ) in (𝓝 p), x.1 ≠ 0,
from this.mono (λ x hx, by { dsimp only, rw cpow_def_of_ne_zero hx, }),
refine is_open.eventually_mem _ hp_fst,
change is_open {x : ℂ × ℂ | x.1 = 0}ᶜ,
rw is_open_compl_iff,
exact is_closed_eq continuous_fst continuous_const,
end
lemma continuous_at_const_cpow {a b : ℂ} (ha : a ≠ 0) : continuous_at (cpow a) b :=
begin
have cpow_eq : cpow a = λ b, exp (log a * b),
by { ext1 b, rw [cpow_eq_pow, cpow_def_of_ne_zero ha], },
rw cpow_eq,
exact continuous_exp.continuous_at.comp (continuous_at.mul continuous_at_const continuous_at_id),
end
lemma continuous_at_const_cpow' {a b : ℂ} (h : b ≠ 0) : continuous_at (cpow a) b :=
begin
by_cases ha : a = 0,
{ rw [ha, continuous_at_congr (zero_cpow_eq_nhds h)], exact continuous_at_const, },
{ exact continuous_at_const_cpow ha, },
end
/-- The function `z ^ w` is continuous in `(z, w)` provided that `z` does not belong to the interval
`(-∞, 0]` on the real line. See also `complex.continuous_at_cpow_zero_of_re_pos` for a version that
works for `z = 0` but assumes `0 < re w`. -/
lemma continuous_at_cpow {p : ℂ × ℂ} (hp_fst : 0 < p.fst.re ∨ p.fst.im ≠ 0) :
continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) p :=
begin
have hp_fst_ne_zero : p.fst ≠ 0,
by { intro h, cases hp_fst; { rw h at hp_fst, simpa using hp_fst, }, },
rw continuous_at_congr (cpow_eq_nhds' hp_fst_ne_zero),
refine continuous_exp.continuous_at.comp _,
refine continuous_at.mul (continuous_at.comp _ continuous_fst.continuous_at)
continuous_snd.continuous_at,
exact continuous_at_clog hp_fst,
end
lemma continuous_at_cpow_const {a b : ℂ} (ha : 0 < a.re ∨ a.im ≠ 0) :
continuous_at (λ x, cpow x b) a :=
tendsto.comp (@continuous_at_cpow (a, b) ha) (continuous_at_id.prod continuous_at_const)
lemma filter.tendsto.cpow {l : filter α} {f g : α → ℂ} {a b : ℂ} (hf : tendsto f l (𝓝 a))
(hg : tendsto g l (𝓝 b)) (ha : 0 < a.re ∨ a.im ≠ 0) :
tendsto (λ x, f x ^ g x) l (𝓝 (a ^ b)) :=
(@continuous_at_cpow (a,b) ha).tendsto.comp (hf.prod_mk_nhds hg)
lemma filter.tendsto.const_cpow {l : filter α} {f : α → ℂ} {a b : ℂ} (hf : tendsto f l (𝓝 b))
(h : a ≠ 0 ∨ b ≠ 0) :
tendsto (λ x, a ^ f x) l (𝓝 (a ^ b)) :=
begin
cases h,
{ exact (continuous_at_const_cpow h).tendsto.comp hf, },
{ exact (continuous_at_const_cpow' h).tendsto.comp hf, },
end
variables [topological_space α] {f g : α → ℂ} {s : set α} {a : α}
lemma continuous_within_at.cpow (hf : continuous_within_at f s a) (hg : continuous_within_at g s a)
(h0 : 0 < (f a).re ∨ (f a).im ≠ 0) :
continuous_within_at (λ x, f x ^ g x) s a :=
hf.cpow hg h0
lemma continuous_within_at.const_cpow {b : ℂ} (hf : continuous_within_at f s a)
(h : b ≠ 0 ∨ f a ≠ 0) :
continuous_within_at (λ x, b ^ f x) s a :=
hf.const_cpow h
lemma continuous_at.cpow (hf : continuous_at f a) (hg : continuous_at g a)
(h0 : 0 < (f a).re ∨ (f a).im ≠ 0) :
continuous_at (λ x, f x ^ g x) a :=
hf.cpow hg h0
lemma continuous_at.const_cpow {b : ℂ} (hf : continuous_at f a) (h : b ≠ 0 ∨ f a ≠ 0) :
continuous_at (λ x, b ^ f x) a :=
hf.const_cpow h
lemma continuous_on.cpow (hf : continuous_on f s) (hg : continuous_on g s)
(h0 : ∀ a ∈ s, 0 < (f a).re ∨ (f a).im ≠ 0) :
continuous_on (λ x, f x ^ g x) s :=
λ a ha, (hf a ha).cpow (hg a ha) (h0 a ha)
lemma continuous_on.const_cpow {b : ℂ} (hf : continuous_on f s) (h : b ≠ 0 ∨ ∀ a ∈ s, f a ≠ 0) :
continuous_on (λ x, b ^ f x) s :=
λ a ha, (hf a ha).const_cpow (h.imp id $ λ h, h a ha)
lemma continuous.cpow (hf : continuous f) (hg : continuous g)
(h0 : ∀ a, 0 < (f a).re ∨ (f a).im ≠ 0) :
continuous (λ x, f x ^ g x) :=
continuous_iff_continuous_at.2 $ λ a, (hf.continuous_at.cpow hg.continuous_at (h0 a))
lemma continuous.const_cpow {b : ℂ} (hf : continuous f) (h : b ≠ 0 ∨ ∀ a, f a ≠ 0) :
continuous (λ x, b ^ f x) :=
continuous_iff_continuous_at.2 $ λ a, (hf.continuous_at.const_cpow $ h.imp id $ λ h, h a)
lemma continuous_on.cpow_const {b : ℂ} (hf : continuous_on f s)
(h : ∀ (a : α), a ∈ s → 0 < (f a).re ∨ (f a).im ≠ 0) :
continuous_on (λ x, (f x) ^ b) s :=
hf.cpow continuous_on_const h
end lim
namespace real
/-- The real power function `x^y`, defined as the real part of the complex power function.
For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`.
For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex
determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (πy)`. -/
noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re
noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩
@[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl
lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl
lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y =
if x = 0
then if y = 0
then 1
else 0
else exp (log x * y) :=
by simp only [rpow_def, complex.cpow_def];
split_ifs;
simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul, -is_R_or_C.of_real_mul,
(complex.of_real_mul _ _).symm, complex.exp_of_real_re] at *
lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) :=
by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)]
lemma exp_mul (x y : ℝ) : exp (x * y) = (exp x) ^ y :=
by rw [rpow_def_of_pos (exp_pos _), log_exp]
@[simp] lemma exp_one_rpow (x : ℝ) : exp 1 ^ x = exp x := by rw [←exp_mul, one_mul]
lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 :=
by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] }
open_locale real
lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) :=
begin
rw [rpow_def, complex.cpow_def, if_neg],
have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I,
{ simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx,
complex.abs_of_real, complex.of_real_mul], ring },
{ rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos,
← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul,
complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im,
real.log_neg_eq_log],
ring },
{ rw complex.of_real_eq_zero, exact ne_of_lt hx }
end
lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y =
if x = 0
then if y = 0
then 1
else 0
else exp (log x * y) * cos (y * π) :=
by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _
lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y :=
by rw rpow_def_of_pos hx; apply exp_pos
@[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def]
@[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 :=
by simp [rpow_def, *]
lemma zero_rpow_eq_iff {x : ℝ} {a : ℝ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) :=
begin
split,
{ intros hyp,
simp [rpow_def] at hyp,
by_cases x = 0,
{ subst h,
simp only [complex.one_re, complex.of_real_zero, complex.cpow_zero] at hyp,
exact or.inr ⟨rfl, hyp.symm⟩},
{ rw complex.zero_cpow (complex.of_real_ne_zero.mpr h) at hyp,
exact or.inl ⟨h, hyp.symm⟩, }, },
{ rintro (⟨h,rfl⟩|⟨rfl,rfl⟩),
{ exact zero_rpow h, },
{ exact rpow_zero _, }, },
end
lemma eq_zero_rpow_iff {x : ℝ} {a : ℝ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) :=
by rw [←zero_rpow_eq_iff, eq_comm]
@[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def]
@[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def]
lemma zero_rpow_le_one (x : ℝ) : (0 : ℝ) ^ x ≤ 1 :=
by { by_cases h : x = 0; simp [h, zero_le_one] }
lemma zero_rpow_nonneg (x : ℝ) : 0 ≤ (0 : ℝ) ^ x :=
by { by_cases h : x = 0; simp [h, zero_le_one] }
lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y :=
by rw [rpow_def_of_nonneg hx];
split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)]
lemma abs_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : |x ^ y| = |x| ^ y :=
begin
have h_rpow_nonneg : 0 ≤ x ^ y, from real.rpow_nonneg_of_nonneg hx_nonneg _,
rw [abs_eq_self.mpr hx_nonneg, abs_eq_self.mpr h_rpow_nonneg],
end
lemma abs_rpow_le_abs_rpow (x y : ℝ) : |x ^ y| ≤ |x| ^ y :=
begin
cases le_or_lt 0 x with hx hx,
{ rw [abs_rpow_of_nonneg hx] },
{ rw [abs_of_neg hx, rpow_def_of_neg hx, rpow_def_of_pos (neg_pos.2 hx), log_neg_eq_log,
abs_mul, abs_of_pos (exp_pos _)],
exact mul_le_of_le_one_right (exp_pos _).le (abs_cos_le_one _) }
end
lemma abs_rpow_le_exp_log_mul (x y : ℝ) : |x ^ y| ≤ exp (log x * y) :=
begin
refine (abs_rpow_le_abs_rpow x y).trans _,
by_cases hx : x = 0,
{ by_cases hy : y = 0; simp [hx, hy, zero_le_one] },
{ rw [rpow_def_of_pos (abs_pos.2 hx), log_abs] }
end
lemma norm_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : ∥x ^ y∥ = ∥x∥ ^ y :=
by { simp_rw real.norm_eq_abs, exact abs_rpow_of_nonneg hx_nonneg, }
end real
namespace complex
lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) :=
by simp [real.rpow_def_of_nonneg hx, complex.cpow_def]; split_ifs; simp [complex.of_real_log hx]
lemma of_real_cpow_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℂ) :
(x : ℂ) ^ y = ((-x) : ℂ) ^ y * exp (π * I * y) :=
begin
rcases hx.eq_or_lt with rfl|hlt,
{ rcases eq_or_ne y 0 with rfl|hy; simp * },
have hne : (x : ℂ) ≠ 0, from of_real_ne_zero.mpr hlt.ne,
rw [cpow_def_of_ne_zero hne, cpow_def_of_ne_zero (neg_ne_zero.2 hne), ← exp_add, ← add_mul,
log, log, abs_neg, arg_of_real_of_neg hlt, ← of_real_neg,
arg_of_real_of_nonneg (neg_nonneg.2 hx), of_real_zero, zero_mul, add_zero]
end
lemma abs_cpow_of_ne_zero {z : ℂ} (hz : z ≠ 0) (w : ℂ) :
abs (z ^ w) = abs z ^ w.re / real.exp (arg z * im w) :=
by rw [cpow_def_of_ne_zero hz, abs_exp, mul_re, log_re, log_im, real.exp_sub,
real.rpow_def_of_pos (abs_pos.2 hz)]
lemma abs_cpow_le (z w : ℂ) : abs (z ^ w) ≤ abs z ^ w.re / real.exp (arg z * im w) :=
begin
rcases ne_or_eq z 0 with hz|rfl; [exact (abs_cpow_of_ne_zero hz w).le, rw abs_zero],
rcases eq_or_ne w 0 with rfl|hw, { simp },
rw [zero_cpow hw, abs_zero],
exact div_nonneg (real.rpow_nonneg_of_nonneg le_rfl _) (real.exp_pos _).le
end
@[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y :=
by rcases eq_or_ne x 0 with rfl|hx; [rcases eq_or_ne y 0 with rfl|hy, skip];
simp [*, abs_cpow_of_ne_zero]
@[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) :=
by rw ← abs_cpow_real; simp [-abs_cpow_real]
lemma abs_cpow_eq_rpow_re_of_pos {x : ℝ} (hx : 0 < x) (y : ℂ) : abs (x ^ y) = x ^ y.re :=
by rw [abs_cpow_of_ne_zero (of_real_ne_zero.mpr hx.ne'), arg_of_real_of_nonneg hx.le, zero_mul,
real.exp_zero, div_one, abs_of_nonneg hx.le]
lemma abs_cpow_eq_rpow_re_of_nonneg {x : ℝ} (hx : 0 ≤ x) {y : ℂ} (hy : re y ≠ 0) :
abs (x ^ y) = x ^ re y :=
begin
rcases hx.eq_or_lt with rfl|hlt,
{ rw [of_real_zero, zero_cpow, abs_zero, real.zero_rpow hy],
exact ne_of_apply_ne re hy },
{ exact abs_cpow_eq_rpow_re_of_pos hlt y }
end
end complex
namespace real
variables {x y z : ℝ}
lemma rpow_add (hx : 0 < x) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z :=
by simp only [rpow_def_of_pos hx, mul_add, exp_add]
lemma rpow_add' (hx : 0 ≤ x) (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z :=
begin
rcases hx.eq_or_lt with rfl|pos,
{ rw [zero_rpow h, zero_eq_mul],
have : y ≠ 0 ∨ z ≠ 0, from not_and_distrib.1 (λ ⟨hy, hz⟩, h $ hy.symm ▸ hz.symm ▸ zero_add 0),
exact this.imp zero_rpow zero_rpow },
{ exact rpow_add pos _ _ }
end
lemma rpow_add_of_nonneg (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 ≤ z) :
x ^ (y + z) = x ^ y * x ^ z :=
begin
rcases hy.eq_or_lt with rfl|hy,
{ rw [zero_add, rpow_zero, one_mul] },
exact rpow_add' hx (ne_of_gt $ add_pos_of_pos_of_nonneg hy hz)
end
/-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for
`x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish.
The inequality is always true, though, and given in this lemma. -/
lemma le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) :=
begin
rcases le_iff_eq_or_lt.1 hx with H|pos,
{ by_cases h : y + z = 0,
{ simp only [H.symm, h, rpow_zero],
calc (0 : ℝ) ^ y * 0 ^ z ≤ 1 * 1 :
mul_le_mul (zero_rpow_le_one y) (zero_rpow_le_one z) (zero_rpow_nonneg z) zero_le_one
... = 1 : by simp },
{ simp [rpow_add', ← H, h] } },
{ simp [rpow_add pos] }
end
lemma rpow_sum_of_pos {ι : Type*} {a : ℝ} (ha : 0 < a) (f : ι → ℝ) (s : finset ι) :
a ^ (∑ x in s, f x) = ∏ x in s, a ^ f x :=
@add_monoid_hom.map_sum ℝ ι (additive ℝ) _ _ ⟨λ x : ℝ, (a ^ x : ℝ), rpow_zero a, rpow_add ha⟩ f s
lemma rpow_sum_of_nonneg {ι : Type*} {a : ℝ} (ha : 0 ≤ a) {s : finset ι} {f : ι → ℝ}
(h : ∀ x ∈ s, 0 ≤ f x) :
a ^ (∑ x in s, f x) = ∏ x in s, a ^ f x :=
begin
induction s using finset.cons_induction with i s hi ihs,
{ rw [sum_empty, finset.prod_empty, rpow_zero] },
{ rw forall_mem_cons at h,
rw [sum_cons, prod_cons, ← ihs h.2, rpow_add_of_nonneg ha h.1 (sum_nonneg h.2)] }
end
lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z :=
by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _),
complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx];
simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm,
complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos]
lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ :=
by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at *
lemma rpow_sub {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z :=
by simp only [sub_eq_add_neg, rpow_add hx, rpow_neg (le_of_lt hx), div_eq_mul_inv]
lemma rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y - z ≠ 0) :
x ^ (y - z) = x ^ y / x ^ z :=
by { simp only [sub_eq_add_neg] at h ⊢, simp only [rpow_add' hx h, rpow_neg hx, div_eq_mul_inv] }
lemma rpow_add_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y + n) = x ^ y * x ^ n :=
by rw [rpow_def, complex.of_real_add, complex.cpow_add _ _ (complex.of_real_ne_zero.mpr hx),
complex.of_real_int_cast, complex.cpow_int_cast, ← complex.of_real_zpow, mul_comm,
complex.of_real_mul_re, ← rpow_def, mul_comm]
lemma rpow_add_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y + n) = x ^ y * x ^ n :=
rpow_add_int hx y n
lemma rpow_sub_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y - n) = x ^ y / x ^ n :=
by simpa using rpow_add_int hx y (-n)
lemma rpow_sub_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n :=
rpow_sub_int hx y n
lemma rpow_add_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y + 1) = x ^ y * x :=
by simpa using rpow_add_nat hx y 1
lemma rpow_sub_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y - 1) = x ^ y / x :=
by simpa using rpow_sub_nat hx y 1
@[simp, norm_cast] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n :=
by simp only [rpow_def, ← complex.of_real_zpow, complex.cpow_int_cast,
complex.of_real_int_cast, complex.of_real_re]
@[simp, norm_cast] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n :=
rpow_int_cast x n
@[simp] lemma rpow_two (x : ℝ) : x ^ (2 : ℝ) = x ^ 2 :=
by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] }
lemma rpow_neg_one (x : ℝ) : x ^ (-1 : ℝ) = x⁻¹ :=
begin
suffices H : x ^ ((-1 : ℤ) : ℝ) = x⁻¹, by rwa [int.cast_neg, int.cast_one] at H,
simp only [rpow_int_cast, zpow_one, zpow_neg],
end
lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z :=
begin
iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *,
{ have hx : 0 < x,
{ cases lt_or_eq_of_le h with h₂ h₂, { exact h₂ },
exfalso, apply h_2, exact eq.symm h₂ },
have hy : 0 < y,
{ cases lt_or_eq_of_le h₁ with h₂ h₂, { exact h₂ },
exfalso, apply h_3, exact eq.symm h₂ },
rw [log_mul (ne_of_gt hx) (ne_of_gt hy), add_mul, exp_add]},
{ exact h₁ },
{ exact h },
{ exact mul_nonneg h h₁ },
end
lemma inv_rpow (hx : 0 ≤ x) (y : ℝ) : (x⁻¹)^y = (x^y)⁻¹ :=
by simp only [← rpow_neg_one, ← rpow_mul hx, mul_comm]
lemma div_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x^z / y^z :=
by simp only [div_eq_mul_inv, mul_rpow hx (inv_nonneg.2 hy), inv_rpow hy]
lemma log_rpow {x : ℝ} (hx : 0 < x) (y : ℝ) : log (x^y) = y * (log x) :=
begin
apply exp_injective,
rw [exp_log (rpow_pos_of_pos hx y), ← exp_log hx, mul_comm, rpow_def_of_pos (exp_pos (log x)) y],
end
lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z :=
begin
rw le_iff_eq_or_lt at hx, cases hx,
{ rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ },
rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp],
exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz
end
lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z :=
begin
rcases eq_or_lt_of_le h₁ with rfl|h₁', { refl },
rcases eq_or_lt_of_le h₂ with rfl|h₂', { simp },
exact le_of_lt (rpow_lt_rpow h h₁' h₂')
end
lemma rpow_lt_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y :=
⟨lt_imp_lt_of_le_imp_le $ λ h, rpow_le_rpow hy h (le_of_lt hz), λ h, rpow_lt_rpow hx h hz⟩
lemma rpow_le_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y :=
le_iff_le_iff_lt_iff_lt.2 $ rpow_lt_rpow_iff hy hx hz
lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z :=
begin
repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]},
rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx),
end
lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z :=
begin
repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]},
rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx),
end
@[simp] lemma rpow_le_rpow_left_iff (hx : 1 < x) : x ^ y ≤ x ^ z ↔ y ≤ z :=
begin
have x_pos : 0 < x := lt_trans zero_lt_one hx,
rw [←log_le_log (rpow_pos_of_pos x_pos y) (rpow_pos_of_pos x_pos z),
log_rpow x_pos, log_rpow x_pos, mul_le_mul_right (log_pos hx)],
end
@[simp] lemma rpow_lt_rpow_left_iff (hx : 1 < x) : x ^ y < x ^ z ↔ y < z :=
by rw [lt_iff_not_le, rpow_le_rpow_left_iff hx, lt_iff_not_le]
lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) :
x^y < x^z :=
begin
repeat {rw [rpow_def_of_pos hx0]},
rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1),
end
lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) :
x^y ≤ x^z :=
begin
repeat {rw [rpow_def_of_pos hx0]},
rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1),
end
@[simp] lemma rpow_le_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) :
x ^ y ≤ x ^ z ↔ z ≤ y :=
begin
rw [←log_le_log (rpow_pos_of_pos hx0 y) (rpow_pos_of_pos hx0 z),
log_rpow hx0, log_rpow hx0, mul_le_mul_right_of_neg (log_neg hx0 hx1)],
end
@[simp] lemma rpow_lt_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) :
x ^ y < x ^ z ↔ z < y :=
by rw [lt_iff_not_le, rpow_le_rpow_left_iff_of_base_lt_one hx0 hx1, lt_iff_not_le]
lemma rpow_lt_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x^z < 1 :=
by { rw ← one_rpow z, exact rpow_lt_rpow hx1 hx2 hz }
lemma rpow_le_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 :=
by { rw ← one_rpow z, exact rpow_le_rpow hx1 hx2 hz }
lemma rpow_lt_one_of_one_lt_of_neg {x z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 :=
by { convert rpow_lt_rpow_of_exponent_lt hx hz, exact (rpow_zero x).symm }
lemma rpow_le_one_of_one_le_of_nonpos {x z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 :=
by { convert rpow_le_rpow_of_exponent_le hx hz, exact (rpow_zero x).symm }
lemma one_lt_rpow {x z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z :=
by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz }
lemma one_le_rpow {x z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x^z :=
by { rw ← one_rpow z, exact rpow_le_rpow zero_le_one hx hz }
lemma one_lt_rpow_of_pos_of_lt_one_of_neg (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) :
1 < x^z :=
by { convert rpow_lt_rpow_of_exponent_gt hx1 hx2 hz, exact (rpow_zero x).symm }
lemma one_le_rpow_of_pos_of_le_one_of_nonpos (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) :
1 ≤ x^z :=
by { convert rpow_le_rpow_of_exponent_ge hx1 hx2 hz, exact (rpow_zero x).symm }
lemma rpow_lt_one_iff_of_pos (hx : 0 < x) : x ^ y < 1 ↔ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y :=
by rw [rpow_def_of_pos hx, exp_lt_one_iff, mul_neg_iff, log_pos_iff hx, log_neg_iff hx]
lemma rpow_lt_one_iff (hx : 0 ≤ x) : x ^ y < 1 ↔ x = 0 ∧ y ≠ 0 ∨ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y :=
begin
rcases hx.eq_or_lt with (rfl|hx),
{ rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, zero_lt_one] },
{ simp [rpow_lt_one_iff_of_pos hx, hx.ne.symm] }
end
lemma one_lt_rpow_iff_of_pos (hx : 0 < x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ x < 1 ∧ y < 0 :=
by rw [rpow_def_of_pos hx, one_lt_exp_iff, mul_pos_iff, log_pos_iff hx, log_neg_iff hx]
lemma one_lt_rpow_iff (hx : 0 ≤ x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ 0 < x ∧ x < 1 ∧ y < 0 :=
begin
rcases hx.eq_or_lt with (rfl|hx),
{ rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, (@zero_lt_one ℝ _ _).not_lt] },
{ simp [one_lt_rpow_iff_of_pos hx, hx] }
end
lemma rpow_le_rpow_of_exponent_ge' (hx0 : 0 ≤ x) (hx1 : x ≤ 1) (hz : 0 ≤ z) (hyz : z ≤ y) :
x^y ≤ x^z :=
begin
rcases eq_or_lt_of_le hx0 with rfl | hx0',
{ rcases eq_or_lt_of_le hz with rfl | hz',
{ exact (rpow_zero 0).symm ▸ (rpow_le_one hx0 hx1 hyz), },
rw [zero_rpow, zero_rpow]; linarith, },
{ exact rpow_le_rpow_of_exponent_ge hx0' hx1 hyz, },
end
lemma rpow_left_inj_on {x : ℝ} (hx : x ≠ 0) :
inj_on (λ y : ℝ, y^x) {y : ℝ | 0 ≤ y} :=
begin
rintros y hy z hz (hyz : y ^ x = z ^ x),
rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul hy, rpow_mul hz, hyz]
end
lemma le_rpow_iff_log_le (hx : 0 < x) (hy : 0 < y) :
x ≤ y^z ↔ real.log x ≤ z * real.log y :=
by rw [←real.log_le_log hx (real.rpow_pos_of_pos hy z), real.log_rpow hy]
lemma le_rpow_of_log_le (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x ≤ z * real.log y) :
x ≤ y^z :=
begin
obtain hx | rfl := hx.lt_or_eq,
{ exact (le_rpow_iff_log_le hx hy).2 h },
exact (real.rpow_pos_of_pos hy z).le,
end
lemma lt_rpow_iff_log_lt (hx : 0 < x) (hy : 0 < y) :
x < y^z ↔ real.log x < z * real.log y :=
by rw [←real.log_lt_log_iff hx (real.rpow_pos_of_pos hy z), real.log_rpow hy]
lemma lt_rpow_of_log_lt (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x < z * real.log y) :
x < y^z :=
begin
obtain hx | rfl := hx.lt_or_eq,
{ exact (lt_rpow_iff_log_lt hx hy).2 h },
exact real.rpow_pos_of_pos hy z,
end
lemma rpow_le_one_iff_of_pos (hx : 0 < x) : x ^ y ≤ 1 ↔ 1 ≤ x ∧ y ≤ 0 ∨ x ≤ 1 ∧ 0 ≤ y :=
by rw [rpow_def_of_pos hx, exp_le_one_iff, mul_nonpos_iff, log_nonneg_iff hx, log_nonpos_iff hx]
/-- Bound for `|log x * x ^ t|` in the interval `(0, 1]`, for positive real `t`. -/
lemma abs_log_mul_self_rpow_lt (x t : ℝ) (h1 : 0 < x) (h2 : x ≤ 1) (ht : 0 < t) :
|log x * x ^ t| < 1 / t :=
begin
rw lt_div_iff ht,
have := abs_log_mul_self_lt (x ^ t) (rpow_pos_of_pos h1 t) (rpow_le_one h1.le h2 ht.le),
rwa [log_rpow h1, mul_assoc, abs_mul, abs_of_pos ht, mul_comm] at this
end
lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : n ≠ 0) :
(x ^ n) ^ (n⁻¹ : ℝ) = x :=
have hn0 : (n : ℝ) ≠ 0, from nat.cast_ne_zero.2 hn,
by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one]
lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : n ≠ 0) :
(x ^ (n⁻¹ : ℝ)) ^ n = x :=
have hn0 : (n : ℝ) ≠ 0, from nat.cast_ne_zero.2 hn,
by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one]
lemma continuous_at_const_rpow {a b : ℝ} (h : a ≠ 0) : continuous_at (rpow a) b :=
begin
have : rpow a = λ x : ℝ, ((a : ℂ) ^ (x : ℂ)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], },
rw this,
refine complex.continuous_re.continuous_at.comp _,
refine (continuous_at_const_cpow _).comp complex.continuous_of_real.continuous_at,
norm_cast,
exact h,
end
lemma continuous_at_const_rpow' {a b : ℝ} (h : b ≠ 0) : continuous_at (rpow a) b :=
begin
have : rpow a = λ x : ℝ, ((a : ℂ) ^ (x : ℂ)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], },
rw this,
refine complex.continuous_re.continuous_at.comp _,
refine (continuous_at_const_cpow' _).comp complex.continuous_of_real.continuous_at,
norm_cast,
exact h,
end
lemma rpow_eq_nhds_of_neg {p : ℝ × ℝ} (hp_fst : p.fst < 0) :
(λ x : ℝ × ℝ, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) * cos (x.2 * π) :=
begin
suffices : ∀ᶠ (x : ℝ × ℝ) in (𝓝 p), x.1 < 0,
from this.mono (λ x hx, by { dsimp only, rw rpow_def_of_neg hx, }),
exact is_open.eventually_mem (is_open_lt continuous_fst continuous_const) hp_fst,
end
lemma rpow_eq_nhds_of_pos {p : ℝ × ℝ} (hp_fst : 0 < p.fst) :
(λ x : ℝ × ℝ, x.1 ^ x.2) =ᶠ[𝓝 p] λ x, exp (log x.1 * x.2) :=
begin
suffices : ∀ᶠ (x : ℝ × ℝ) in (𝓝 p), 0 < x.1,
from this.mono (λ x hx, by { dsimp only, rw rpow_def_of_pos hx, }),
exact is_open.eventually_mem (is_open_lt continuous_const continuous_fst) hp_fst,
end
lemma continuous_at_rpow_of_ne (p : ℝ × ℝ) (hp : p.1 ≠ 0) :
continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p :=
begin
rw ne_iff_lt_or_gt at hp,
cases hp,
{ rw continuous_at_congr (rpow_eq_nhds_of_neg hp),
refine continuous_at.mul _ (continuous_cos.continuous_at.comp _),
{ refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at),
refine (continuous_at_log _).comp continuous_fst.continuous_at,
exact hp.ne, },
{ exact continuous_snd.continuous_at.mul continuous_at_const, }, },
{ rw continuous_at_congr (rpow_eq_nhds_of_pos hp),
refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at),
refine (continuous_at_log _).comp continuous_fst.continuous_at,
exact hp.lt.ne.symm, },
end
lemma continuous_at_rpow_of_pos (p : ℝ × ℝ) (hp : 0 < p.2) :
continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p :=
begin
cases p with x y,
obtain hx|rfl := ne_or_eq x 0,
{ exact continuous_at_rpow_of_ne (x, y) hx },
have A : tendsto (λ p : ℝ × ℝ, exp (log p.1 * p.2)) (𝓝[≠] 0 ×ᶠ 𝓝 y) (𝓝 0) :=
tendsto_exp_at_bot.comp
((tendsto_log_nhds_within_zero.comp tendsto_fst).at_bot_mul hp tendsto_snd),
have B : tendsto (λ p : ℝ × ℝ, p.1 ^ p.2) (𝓝[≠] 0 ×ᶠ 𝓝 y) (𝓝 0) :=
squeeze_zero_norm (λ p, abs_rpow_le_exp_log_mul p.1 p.2) A,
have C : tendsto (λ p : ℝ × ℝ, p.1 ^ p.2) (𝓝[{0}] 0 ×ᶠ 𝓝 y) (pure 0),
{ rw [nhds_within_singleton, tendsto_pure, pure_prod, eventually_map],
exact (lt_mem_nhds hp).mono (λ y hy, zero_rpow hy.ne') },
simpa only [← sup_prod, ← nhds_within_union, compl_union_self, nhds_within_univ, nhds_prod_eq,
continuous_at, zero_rpow hp.ne'] using B.sup (C.mono_right (pure_le_nhds _))
end
lemma continuous_at_rpow (p : ℝ × ℝ) (h : p.1 ≠ 0 ∨ 0 < p.2) :
continuous_at (λ p : ℝ × ℝ, p.1 ^ p.2) p :=
h.elim (λ h, continuous_at_rpow_of_ne p h) (λ h, continuous_at_rpow_of_pos p h)
lemma continuous_at_rpow_const (x : ℝ) (q : ℝ) (h : x ≠ 0 ∨ 0 < q) :
continuous_at (λ (x : ℝ), x ^ q) x :=
begin
change continuous_at ((λ p : ℝ × ℝ, p.1 ^ p.2) ∘ (λ y : ℝ, (y, q))) x,
apply continuous_at.comp,
{ exact continuous_at_rpow (x, q) h },
{ exact (continuous_id'.prod_mk continuous_const).continuous_at }
end
end real
section
variable {α : Type*}
lemma filter.tendsto.rpow {l : filter α} {f g : α → ℝ} {x y : ℝ}
(hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) :
tendsto (λ t, f t ^ g t) l (𝓝 (x ^ y)) :=
(real.continuous_at_rpow (x, y) h).tendsto.comp (hf.prod_mk_nhds hg)
lemma filter.tendsto.rpow_const {l : filter α} {f : α → ℝ} {x p : ℝ}
(hf : tendsto f l (𝓝 x)) (h : x ≠ 0 ∨ 0 ≤ p) :
tendsto (λ a, f a ^ p) l (𝓝 (x ^ p)) :=
if h0 : 0 = p then h0 ▸ by simp [tendsto_const_nhds]
else hf.rpow tendsto_const_nhds (h.imp id $ λ h', h'.lt_of_ne h0)
variables [topological_space α] {f g : α → ℝ} {s : set α} {x : α} {p : ℝ}
lemma continuous_at.rpow (hf : continuous_at f x) (hg : continuous_at g x) (h : f x ≠ 0 ∨ 0 < g x) :
continuous_at (λ t, f t ^ g t) x :=
hf.rpow hg h
lemma continuous_within_at.rpow (hf : continuous_within_at f s x) (hg : continuous_within_at g s x)
(h : f x ≠ 0 ∨ 0 < g x) :
continuous_within_at (λ t, f t ^ g t) s x :=
hf.rpow hg h
lemma continuous_on.rpow (hf : continuous_on f s) (hg : continuous_on g s)
(h : ∀ x ∈ s, f x ≠ 0 ∨ 0 < g x) :
continuous_on (λ t, f t ^ g t) s :=
λ t ht, (hf t ht).rpow (hg t ht) (h t ht)
lemma continuous.rpow (hf : continuous f) (hg : continuous g) (h : ∀ x, f x ≠ 0 ∨ 0 < g x) :
continuous (λ x, f x ^ g x) :=
continuous_iff_continuous_at.2 $ λ x, (hf.continuous_at.rpow hg.continuous_at (h x))
lemma continuous_within_at.rpow_const (hf : continuous_within_at f s x) (h : f x ≠ 0 ∨ 0 ≤ p) :
continuous_within_at (λ x, f x ^ p) s x :=
hf.rpow_const h
lemma continuous_at.rpow_const (hf : continuous_at f x) (h : f x ≠ 0 ∨ 0 ≤ p) :
continuous_at (λ x, f x ^ p) x :=
hf.rpow_const h
lemma continuous_on.rpow_const (hf : continuous_on f s) (h : ∀ x ∈ s, f x ≠ 0 ∨ 0 ≤ p) :
continuous_on (λ x, f x ^ p) s :=
λ x hx, (hf x hx).rpow_const (h x hx)
lemma continuous.rpow_const (hf : continuous f) (h : ∀ x, f x ≠ 0 ∨ 0 ≤ p) :
continuous (λ x, f x ^ p) :=
continuous_iff_continuous_at.2 $ λ x, hf.continuous_at.rpow_const (h x)
end
namespace real
variables {z x y : ℝ}
section sqrt
lemma sqrt_eq_rpow (x : ℝ) : sqrt x = x ^ (1/(2:ℝ)) :=
begin
obtain h | h := le_or_lt 0 x,
{ rw [← mul_self_inj_of_nonneg (sqrt_nonneg _) (rpow_nonneg_of_nonneg h _), mul_self_sqrt h,
← sq, ← rpow_nat_cast, ← rpow_mul h],
norm_num },
{ have : 1 / (2:ℝ) * π = π / (2:ℝ), ring,
rw [sqrt_eq_zero_of_nonpos h.le, rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] }
end
end sqrt
end real
section limits
open real filter
/-- The function `x ^ y` tends to `+∞` at `+∞` for any positive real `y`. -/
lemma tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ, x ^ y) at_top at_top :=
begin
rw tendsto_at_top_at_top,
intro b,
use (max b 0) ^ (1/y),
intros x hx,
exact le_of_max_le_left
(by { convert rpow_le_rpow (rpow_nonneg_of_nonneg (le_max_right b 0) (1/y)) hx (le_of_lt hy),
rw [← rpow_mul (le_max_right b 0), (eq_div_iff (ne_of_gt hy)).mp rfl, rpow_one] }),
end
/-- The function `x ^ (-y)` tends to `0` at `+∞` for any positive real `y`. -/
lemma tendsto_rpow_neg_at_top {y : ℝ} (hy : 0 < y) : tendsto (λ x : ℝ, x ^ (-y)) at_top (𝓝 0) :=
tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) (λ x hx, (rpow_neg (le_of_lt hx) y).symm))
(tendsto_rpow_at_top hy).inv_tendsto_at_top
/-- The function `x ^ (a / (b * x + c))` tends to `1` at `+∞`, for any real numbers `a`, `b`, and
`c` such that `b` is nonzero. -/
lemma tendsto_rpow_div_mul_add (a b c : ℝ) (hb : 0 ≠ b) :
tendsto (λ x, x ^ (a / (b*x+c))) at_top (𝓝 1) :=
begin
refine tendsto.congr' _ ((tendsto_exp_nhds_0_nhds_1.comp
(by simpa only [mul_zero, pow_one] using ((@tendsto_const_nhds _ _ _ a _).mul
(tendsto_div_pow_mul_exp_add_at_top b c 1 hb)))).comp tendsto_log_at_top),
apply eventually_eq_of_mem (Ioi_mem_at_top (0:ℝ)),
intros x hx,
simp only [set.mem_Ioi, function.comp_app] at hx ⊢,
rw [exp_log hx, ← exp_log (rpow_pos_of_pos hx (a / (b * x + c))), log_rpow hx (a / (b * x + c))],
field_simp,
end
/-- The function `x ^ (1 / x)` tends to `1` at `+∞`. -/
lemma tendsto_rpow_div : tendsto (λ x, x ^ ((1:ℝ) / x)) at_top (𝓝 1) :=
by { convert tendsto_rpow_div_mul_add (1:ℝ) _ (0:ℝ) zero_ne_one, funext, congr' 2, ring }
/-- The function `x ^ (-1 / x)` tends to `1` at `+∞`. -/
lemma tendsto_rpow_neg_div : tendsto (λ x, x ^ (-(1:ℝ) / x)) at_top (𝓝 1) :=
by { convert tendsto_rpow_div_mul_add (-(1:ℝ)) _ (0:ℝ) zero_ne_one, funext, congr' 2, ring }
/-- The function `exp(x) / x ^ s` tends to `+∞` at `+∞`, for any real number `s`. -/
lemma tendsto_exp_div_rpow_at_top (s : ℝ) : tendsto (λ x : ℝ, exp x / x ^ s) at_top at_top :=
begin
cases archimedean_iff_nat_lt.1 (real.archimedean) s with n hn,
refine tendsto_at_top_mono' _ _ (tendsto_exp_div_pow_at_top n),
filter_upwards [eventually_gt_at_top (0 : ℝ), eventually_ge_at_top (1 : ℝ)] with x hx₀ hx₁,
rw [div_le_div_left (exp_pos _) (pow_pos hx₀ _) (rpow_pos_of_pos hx₀ _), ←rpow_nat_cast],
exact rpow_le_rpow_of_exponent_le hx₁ hn.le,
end
/-- The function `exp (b * x) / x ^ s` tends to `+∞` at `+∞`, for any real `s` and `b > 0`. -/
lemma tendsto_exp_mul_div_rpow_at_top (s : ℝ) (b : ℝ) (hb : 0 < b) :
tendsto (λ x : ℝ, exp (b * x) / x ^ s) at_top at_top :=
begin
refine ((tendsto_rpow_at_top hb).comp (tendsto_exp_div_rpow_at_top (s / b))).congr' _,
filter_upwards [eventually_ge_at_top (0 : ℝ)] with x hx₀,
simp [div_rpow, (exp_pos x).le, rpow_nonneg_of_nonneg, ←rpow_mul, ←exp_mul, mul_comm x, hb.ne', *]
end
/-- The function `x ^ s * exp (-b * x)` tends to `0` at `+∞`, for any real `s` and `b > 0`. -/
lemma tendsto_rpow_mul_exp_neg_mul_at_top_nhds_0 (s : ℝ) (b : ℝ) (hb : 0 < b):
tendsto (λ x : ℝ, x ^ s * exp (-b * x)) at_top (𝓝 0) :=
begin
refine (tendsto_exp_mul_div_rpow_at_top s b hb).inv_tendsto_at_top.congr' _,
filter_upwards with x using by simp [exp_neg, inv_div, div_eq_mul_inv _ (exp _)]
end
namespace asymptotics
variables {α : Type*} {r c : ℝ} {l : filter α} {f g : α → ℝ}
lemma is_O_with.rpow (h : is_O_with c l f g) (hc : 0 ≤ c) (hr : 0 ≤ r) (hg : 0 ≤ᶠ[l] g) :
is_O_with (c ^ r) l (λ x, f x ^ r) (λ x, g x ^ r) :=
begin
apply is_O_with.of_bound,
filter_upwards [hg, h.bound] with x hgx hx,
calc |f x ^ r| ≤ |f x| ^ r : abs_rpow_le_abs_rpow _ _
... ≤ (c * |g x|) ^ r : rpow_le_rpow (abs_nonneg _) hx hr
... = c ^ r * |g x ^ r| : by rw [mul_rpow hc (abs_nonneg _), abs_rpow_of_nonneg hgx]
end
lemma is_O.rpow (hr : 0 ≤ r) (hg : 0 ≤ᶠ[l] g) (h : f =O[l] g) :
(λ x, f x ^ r) =O[l] (λ x, g x ^ r) :=
let ⟨c, hc, h'⟩ := h.exists_nonneg in (h'.rpow hc hr hg).is_O
lemma is_o.rpow (hr : 0 < r) (hg : 0 ≤ᶠ[l] g) (h : f =o[l] g) :
(λ x, f x ^ r) =o[l] (λ x, g x ^ r) :=
is_o.of_is_O_with $ λ c hc, ((h.forall_is_O_with (rpow_pos_of_pos hc r⁻¹)).rpow
(rpow_nonneg_of_nonneg hc.le _) hr.le hg).congr_const
(by rw [←rpow_mul hc.le, inv_mul_cancel hr.ne', rpow_one])
end asymptotics
open asymptotics
/-- `x ^ s = o(exp(b * x))` as `x → ∞` for any real `s` and positive `b`. -/
lemma is_o_rpow_exp_pos_mul_at_top (s : ℝ) {b : ℝ} (hb : 0 < b) :
(λ x : ℝ, x ^ s) =o[at_top] (λ x, exp (b * x)) :=
iff.mpr (is_o_iff_tendsto $ λ x h, absurd h (exp_pos _).ne') $
by simpa only [div_eq_mul_inv, exp_neg, neg_mul]
using tendsto_rpow_mul_exp_neg_mul_at_top_nhds_0 s b hb
/-- `x ^ k = o(exp(b * x))` as `x → ∞` for any integer `k` and positive `b`. -/
lemma is_o_zpow_exp_pos_mul_at_top (k : ℤ) {b : ℝ} (hb : 0 < b) :
(λ x : ℝ, x ^ k) =o[at_top] (λ x, exp (b * x)) :=
by simpa only [rpow_int_cast] using is_o_rpow_exp_pos_mul_at_top k hb
/-- `x ^ k = o(exp(b * x))` as `x → ∞` for any natural `k` and positive `b`. -/
lemma is_o_pow_exp_pos_mul_at_top (k : ℕ) {b : ℝ} (hb : 0 < b) :
(λ x : ℝ, x ^ k) =o[at_top] (λ x, exp (b * x)) :=
is_o_zpow_exp_pos_mul_at_top k hb
/-- `x ^ s = o(exp x)` as `x → ∞` for any real `s`. -/
lemma is_o_rpow_exp_at_top (s : ℝ) : (λ x : ℝ, x ^ s) =o[at_top] exp :=
by simpa only [one_mul] using is_o_rpow_exp_pos_mul_at_top s one_pos
lemma is_o_log_rpow_at_top {r : ℝ} (hr : 0 < r) : log =o[at_top] (λ x, x ^ r) :=
calc log =O[at_top] (λ x, r * log x) : is_O_self_const_mul _ hr.ne' _ _
... =ᶠ[at_top] (λ x, log (x ^ r)) :
(eventually_gt_at_top 0).mono $ λ x hx, (log_rpow hx _).symm
... =o[at_top] (λ x, x ^ r) : is_o_log_id_at_top.comp_tendsto (tendsto_rpow_at_top hr)
lemma is_o_log_rpow_rpow_at_top {s : ℝ} (r : ℝ) (hs : 0 < s) :
(λ x, log x ^ r) =o[at_top] (λ x, x ^ s) :=
let r' := max r 1 in
have hr : 0 < r', from lt_max_iff.2 $ or.inr one_pos,
have H : 0 < s / r', from div_pos hs hr,
calc (λ x, log x ^ r) =O[at_top] (λ x, log x ^ r') :
is_O.of_bound 1 $ (tendsto_log_at_top.eventually_ge_at_top 1).mono $ λ x hx,
have hx₀ : 0 ≤ log x, from zero_le_one.trans hx,
by simp [norm_eq_abs, abs_rpow_of_nonneg, abs_rpow_of_nonneg hx₀,
rpow_le_rpow_of_exponent_le (hx.trans (le_abs_self _))]
... =o[at_top] (λ x, (x ^ (s / r')) ^ r') :
(is_o_log_rpow_at_top H).rpow hr $ (tendsto_rpow_at_top H).eventually $ eventually_ge_at_top 0
... =ᶠ[at_top] (λ x, x ^ s) :
(eventually_ge_at_top 0).mono $ λ x hx, by simp only [← rpow_mul hx, div_mul_cancel _ hr.ne']
lemma is_o_abs_log_rpow_rpow_nhds_zero {s : ℝ} (r : ℝ) (hs : s < 0) :
(λ x, |log x| ^ r) =o[𝓝[>] 0] (λ x, x ^ s) :=
((is_o_log_rpow_rpow_at_top r (neg_pos.2 hs)).comp_tendsto tendsto_inv_zero_at_top).congr'
(mem_of_superset (Icc_mem_nhds_within_Ioi $ set.left_mem_Ico.2 one_pos) $
λ x hx, by simp [abs_of_nonpos, log_nonpos hx.1 hx.2])
(eventually_mem_nhds_within.mono $ λ x hx,
by rw [function.comp_app, inv_rpow hx.out.le, rpow_neg hx.out.le, inv_inv])
lemma is_o_log_rpow_nhds_zero {r : ℝ} (hr : r < 0) : log =o[𝓝[>] 0] (λ x, x ^ r) :=
(is_o_abs_log_rpow_rpow_nhds_zero 1 hr).neg_left.congr'
(mem_of_superset (Icc_mem_nhds_within_Ioi $ set.left_mem_Ico.2 one_pos) $
λ x hx, by simp [abs_of_nonpos (log_nonpos hx.1 hx.2)])
eventually_eq.rfl
lemma tendsto_log_div_rpow_nhds_zero {r : ℝ} (hr : r < 0) :
tendsto (λ x, log x / x ^ r) (𝓝[>] 0) (𝓝 0) :=
(is_o_log_rpow_nhds_zero hr).tendsto_div_nhds_zero
lemma tendsto_log_mul_rpow_nhds_zero {r : ℝ} (hr : 0 < r) :
tendsto (λ x, log x * x ^ r) (𝓝[>] 0) (𝓝 0) :=
(tendsto_log_div_rpow_nhds_zero $ neg_lt_zero.2 hr).congr' $
eventually_mem_nhds_within.mono $ λ x hx, by rw [rpow_neg hx.out.le, div_inv_eq_mul]
end limits
namespace complex
/-- See also `complex.continuous_at_cpow` and `complex.continuous_at_cpow_of_re_pos`. -/
lemma continuous_at_cpow_zero_of_re_pos {z : ℂ} (hz : 0 < z.re) :
continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) (0, z) :=
begin
have hz₀ : z ≠ 0, from ne_of_apply_ne re hz.ne',
rw [continuous_at, zero_cpow hz₀, tendsto_zero_iff_norm_tendsto_zero],
refine squeeze_zero (λ _, norm_nonneg _) (λ _, abs_cpow_le _ _) _,
simp only [div_eq_mul_inv, ← real.exp_neg],
refine tendsto.zero_mul_is_bounded_under_le _ _,
{ convert (continuous_fst.norm.tendsto _).rpow ((continuous_re.comp continuous_snd).tendsto _) _;
simp [hz, real.zero_rpow hz.ne'] },
{ simp only [(∘), real.norm_eq_abs, abs_of_pos (real.exp_pos _)],
rcases exists_gt (|im z|) with ⟨C, hC⟩,
refine ⟨real.exp (π * C), eventually_map.2 _⟩,
refine (((continuous_im.comp continuous_snd).abs.tendsto (_, z)).eventually
(gt_mem_nhds hC)).mono (λ z hz, real.exp_le_exp.2 $ (neg_le_abs_self _).trans _),
rw _root_.abs_mul,
exact mul_le_mul (abs_le.2 ⟨(neg_pi_lt_arg _).le, arg_le_pi _⟩) hz.le
(_root_.abs_nonneg _) real.pi_pos.le }
end
/-- See also `complex.continuous_at_cpow` for a version that assumes `p.1 ≠ 0` but makes no
assumptions about `p.2`. -/
lemma continuous_at_cpow_of_re_pos {p : ℂ × ℂ} (h₁ : 0 ≤ p.1.re ∨ p.1.im ≠ 0) (h₂ : 0 < p.2.re) :
continuous_at (λ x : ℂ × ℂ, x.1 ^ x.2) p :=
begin
cases p with z w,
rw [← not_lt_zero_iff, lt_iff_le_and_ne, not_and_distrib, ne.def, not_not, not_le_zero_iff] at h₁,
rcases h₁ with h₁|(rfl : z = 0),
exacts [continuous_at_cpow h₁, continuous_at_cpow_zero_of_re_pos h₂]
end
/-- See also `complex.continuous_at_cpow_const` for a version that assumes `z ≠ 0` but makes no
assumptions about `w`. -/
lemma continuous_at_cpow_const_of_re_pos {z w : ℂ} (hz : 0 ≤ re z ∨ im z ≠ 0) (hw : 0 < re w) :
continuous_at (λ x, x ^ w) z :=
tendsto.comp (@continuous_at_cpow_of_re_pos (z, w) hz hw)
(continuous_at_id.prod continuous_at_const)
lemma continuous_of_real_cpow_const {y : ℂ} (hs : 0 < y.re) : continuous (λ x, x ^ y : ℝ → ℂ) :=
begin
rw continuous_iff_continuous_at, intro x,
cases le_or_lt 0 x with hx hx,
{ refine (continuous_at_cpow_const_of_re_pos _ hs).comp continuous_of_real.continuous_at,
exact or.inl hx },
{ suffices : continuous_on (λ x, x ^ y : ℝ → ℂ) (set.Iio 0),
from continuous_on.continuous_at this (Iio_mem_nhds hx),
have : eq_on (λ x, x ^ y : ℝ → ℂ) (λ x, ((-x) : ℂ) ^ y * exp (π * I * y)) (set.Iio 0),
from λ y hy, of_real_cpow_of_nonpos (le_of_lt hy) _,
refine (continuous_on.mul (λ y hy, _) continuous_on_const).congr this,
refine continuous_of_real.continuous_within_at.neg.cpow continuous_within_at_const _,
left, simpa using hy }
end
end complex
namespace nnreal
/-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ ` as the
restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`,
one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/
noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 :=
⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩
noncomputable instance : has_pow ℝ≥0 ℝ := ⟨rpow⟩
@[simp] lemma rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl
@[simp, norm_cast] lemma coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl
@[simp] lemma rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 :=
nnreal.eq $ real.rpow_zero _
@[simp] lemma rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 :=
begin
rw [← nnreal.coe_eq, coe_rpow, ← nnreal.coe_eq_zero],
exact real.rpow_eq_zero_iff_of_nonneg x.2
end
@[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 :=
nnreal.eq $ real.zero_rpow h
@[simp] lemma rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x :=
nnreal.eq $ real.rpow_one _
@[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 :=
nnreal.eq $ real.one_rpow _
lemma rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z :=
nnreal.eq $ real.rpow_add (pos_iff_ne_zero.2 hx) _ _
lemma rpow_add' (x : ℝ≥0) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z :=
nnreal.eq $ real.rpow_add' x.2 h
lemma rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z :=
nnreal.eq $ real.rpow_mul x.2 y z
lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ :=
nnreal.eq $ real.rpow_neg x.2 _
lemma rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x ⁻¹ :=
by simp [rpow_neg]
lemma rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z :=
nnreal.eq $ real.rpow_sub (pos_iff_ne_zero.2 hx) y z
lemma rpow_sub' (x : ℝ≥0) {y z : ℝ} (h : y - z ≠ 0) :
x ^ (y - z) = x ^ y / x ^ z :=
nnreal.eq $ real.rpow_sub' x.2 h
lemma rpow_inv_rpow_self {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ y) ^ (1 / y) = x :=
by field_simp [← rpow_mul]
lemma rpow_self_rpow_inv {y : ℝ} (hy : y ≠ 0) (x : ℝ≥0) : (x ^ (1 / y)) ^ y = x :=
by field_simp [← rpow_mul]
lemma inv_rpow (x : ℝ≥0) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ :=
nnreal.eq $ real.inv_rpow x.2 y
lemma div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z :=
nnreal.eq $ real.div_rpow x.2 y.2 z
lemma sqrt_eq_rpow (x : ℝ≥0) : sqrt x = x ^ (1/(2:ℝ)) :=
begin
refine nnreal.eq _,
push_cast,
exact real.sqrt_eq_rpow x.1,
end
@[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n :=
nnreal.eq $ by simpa only [coe_rpow, coe_pow] using real.rpow_nat_cast x n
@[simp] lemma rpow_two (x : ℝ≥0) : x ^ (2 : ℝ) = x ^ 2 :=
by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] }
lemma mul_rpow {x y : ℝ≥0} {z : ℝ} : (x*y)^z = x^z * y^z :=
nnreal.eq $ real.mul_rpow x.2 y.2
lemma rpow_le_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z :=
real.rpow_le_rpow x.2 h₁ h₂
lemma rpow_lt_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z :=
real.rpow_lt_rpow x.2 h₁ h₂
lemma rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y :=
real.rpow_lt_rpow_iff x.2 y.2 hz
lemma rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y :=
real.rpow_le_rpow_iff x.2 y.2 hz
lemma le_rpow_one_div_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y :=
by rw [← rpow_le_rpow_iff hz, rpow_self_rpow_inv hz.ne']
lemma rpow_one_div_le_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ (1 / z) ≤ y ↔ x ≤ y ^ z :=
by rw [← rpow_le_rpow_iff hz, rpow_self_rpow_inv hz.ne']
lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x^y < x^z :=
real.rpow_lt_rpow_of_exponent_lt hx hyz
lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z :=
real.rpow_le_rpow_of_exponent_le hx hyz
lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) :
x^y < x^z :=
real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz
lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) :
x^y ≤ x^z :=
real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz
lemma rpow_pos {p : ℝ} {x : ℝ≥0} (hx_pos : 0 < x) : 0 < x^p :=
begin
have rpow_pos_of_nonneg : ∀ {p : ℝ}, 0 < p → 0 < x^p,
{ intros p hp_pos,
rw ←zero_rpow hp_pos.ne',
exact rpow_lt_rpow hx_pos hp_pos },
rcases lt_trichotomy 0 p with hp_pos|rfl|hp_neg,
{ exact rpow_pos_of_nonneg hp_pos },
{ simp only [zero_lt_one, rpow_zero] },
{ rw [←neg_neg p, rpow_neg, inv_pos],
exact rpow_pos_of_nonneg (neg_pos.mpr hp_neg) },
end
lemma rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx1 : x < 1) (hz : 0 < z) : x^z < 1 :=
real.rpow_lt_one (coe_nonneg x) hx1 hz
lemma rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 :=
real.rpow_le_one x.2 hx2 hz
lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 :=
real.rpow_lt_one_of_one_lt_of_neg hx hz
lemma rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 :=
real.rpow_le_one_of_one_le_of_nonpos hx hz
lemma one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z :=
real.one_lt_rpow hx hz
lemma one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z :=
real.one_le_rpow h h₁
lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1)
(hz : z < 0) : 1 < x^z :=
real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz
lemma one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1)
(hz : z ≤ 0) : 1 ≤ x^z :=
real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz
lemma rpow_le_self_of_le_one {x : ℝ≥0} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x :=
begin
rcases eq_bot_or_bot_lt x with rfl | (h : 0 < x),
{ have : z ≠ 0 := by linarith,
simp [this] },
nth_rewrite 1 ←nnreal.rpow_one x,
exact nnreal.rpow_le_rpow_of_exponent_ge h hx h_one_le,
end
lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective (λ y : ℝ≥0, y^x) :=
λ y z hyz, by simpa only [rpow_inv_rpow_self hx] using congr_arg (λ y, y ^ (1 / x)) hyz
lemma rpow_eq_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ z = y ^ z ↔ x = y :=
(rpow_left_injective hz).eq_iff
lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective (λ y : ℝ≥0, y^x) :=
λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩
lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective (λ y : ℝ≥0, y^x) :=
⟨rpow_left_injective hx, rpow_left_surjective hx⟩
lemma eq_rpow_one_div_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x = y ^ (1 / z) ↔ x ^ z = y :=
by rw [← rpow_eq_rpow_iff hz, rpow_self_rpow_inv hz]
lemma rpow_one_div_eq_iff {x y : ℝ≥0} {z : ℝ} (hz : z ≠ 0) : x ^ (1 / z) = y ↔ x = y ^ z :=
by rw [← rpow_eq_rpow_iff hz, rpow_self_rpow_inv hz]
lemma pow_nat_rpow_nat_inv (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) :
(x ^ n) ^ (n⁻¹ : ℝ) = x :=
by { rw [← nnreal.coe_eq, coe_rpow, nnreal.coe_pow], exact real.pow_nat_rpow_nat_inv x.2 hn }
lemma rpow_nat_inv_pow_nat (x : ℝ≥0) {n : ℕ} (hn : n ≠ 0) :
(x ^ (n⁻¹ : ℝ)) ^ n = x :=
by { rw [← nnreal.coe_eq, nnreal.coe_pow, coe_rpow], exact real.rpow_nat_inv_pow_nat x.2 hn }
lemma continuous_at_rpow {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) :
continuous_at (λp:ℝ≥0×ℝ, p.1^p.2) (x, y) :=
begin
have : (λp:ℝ≥0×ℝ, p.1^p.2) = real.to_nnreal ∘ (λp:ℝ×ℝ, p.1^p.2) ∘ (λp:ℝ≥0 × ℝ, (p.1.1, p.2)),
{ ext p,
rw [coe_rpow, real.coe_to_nnreal _ (real.rpow_nonneg_of_nonneg p.1.2 _)],
refl },
rw this,
refine continuous_real_to_nnreal.continuous_at.comp (continuous_at.comp _ _),
{ apply real.continuous_at_rpow,
simp at h,
rw ← (nnreal.coe_eq_zero x) at h,
exact h },
{ exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at }
end
lemma _root_.real.to_nnreal_rpow_of_nonneg {x y : ℝ} (hx : 0 ≤ x) :
real.to_nnreal (x ^ y) = (real.to_nnreal x) ^ y :=
begin
nth_rewrite 0 ← real.coe_to_nnreal x hx,
rw [←nnreal.coe_rpow, real.to_nnreal_coe],
end
end nnreal
namespace real
variables {n : ℕ}
lemma exists_rat_pow_btwn_rat_aux (hn : n ≠ 0) (x y : ℝ) (h : x < y) (hy : 0 < y) :
∃ q : ℚ, 0 < q ∧ x < q^n ∧ ↑q^n < y :=
begin
have hn' : 0 < (n : ℝ) := by exact_mod_cast hn.bot_lt,
obtain ⟨q, hxq, hqy⟩ := exists_rat_btwn (rpow_lt_rpow (le_max_left 0 x) (max_lt hy h) $
inv_pos.mpr hn'),
have := rpow_nonneg_of_nonneg (le_max_left 0 x) n⁻¹,
have hq := this.trans_lt hxq,
replace hxq := rpow_lt_rpow this hxq hn',
replace hqy := rpow_lt_rpow hq.le hqy hn',
rw [rpow_nat_cast, rpow_nat_cast, rpow_nat_inv_pow_nat _ hn] at hxq hqy,
exact ⟨q, by exact_mod_cast hq, (le_max_right _ _).trans_lt hxq, hqy⟩,
{ exact le_max_left _ _ },
{ exact hy.le }
end
lemma exists_rat_pow_btwn_rat (hn : n ≠ 0) {x y : ℚ} (h : x < y) (hy : 0 < y) :
∃ q : ℚ, 0 < q ∧ x < q^n ∧ q^n < y :=
by apply_mod_cast exists_rat_pow_btwn_rat_aux hn x y; assumption
/-- There is a rational power between any two positive elements of an archimedean ordered field. -/
lemma exists_rat_pow_btwn {α : Type*} [linear_ordered_field α] [archimedean α] (hn : n ≠ 0)
{x y : α} (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ (q^n : α) < y :=
begin
obtain ⟨q₂, hx₂, hy₂⟩ := exists_rat_btwn (max_lt h hy),
obtain ⟨q₁, hx₁, hq₁₂⟩ := exists_rat_btwn hx₂,
have : (0 : α) < q₂ := (le_max_right _ _).trans_lt hx₂,
norm_cast at hq₁₂ this,
obtain ⟨q, hq, hq₁, hq₂⟩ := exists_rat_pow_btwn_rat hn hq₁₂ this,
refine ⟨q, hq, (le_max_left _ _).trans_lt $ hx₁.trans _, hy₂.trans' _⟩; assumption_mod_cast,
end
end real
open filter
lemma filter.tendsto.nnrpow {α : Type*} {f : filter α} {u : α → ℝ≥0} {v : α → ℝ} {x : ℝ≥0} {y : ℝ}
(hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) :
tendsto (λ a, (u a) ^ (v a)) f (𝓝 (x ^ y)) :=
tendsto.comp (nnreal.continuous_at_rpow h) (hx.prod_mk_nhds hy)
namespace nnreal
lemma continuous_at_rpow_const {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 ≤ y) :
continuous_at (λ z, z^y) x :=
h.elim (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inl h)) $
λ h, h.eq_or_lt.elim
(λ h, h ▸ by simp only [rpow_zero, continuous_at_const])
(λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inr h))
lemma continuous_rpow_const {y : ℝ} (h : 0 ≤ y) :
continuous (λ x : ℝ≥0, x^y) :=
continuous_iff_continuous_at.2 $ λ x, continuous_at_rpow_const (or.inr h)
theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) :
tendsto (λ (x : ℝ≥0), x ^ y) at_top at_top :=
begin
rw filter.tendsto_at_top_at_top,
intros b,
obtain ⟨c, hc⟩ := tendsto_at_top_at_top.mp (tendsto_rpow_at_top hy) b,
use c.to_nnreal,
intros a ha,
exact_mod_cast hc a (real.to_nnreal_le_iff_le_coe.mp ha),
end
end nnreal
namespace ennreal
/-- The real power function `x^y` on extended nonnegative reals, defined for `x : ℝ≥0∞` and
`y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values
for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and
`⊤ ^ x = 1 / 0 ^ x`). -/
noncomputable def rpow : ℝ≥0∞ → ℝ → ℝ≥0∞
| (some x) y := if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0)
| none y := if 0 < y then ⊤ else if y = 0 then 1 else 0
noncomputable instance : has_pow ℝ≥0∞ ℝ := ⟨rpow⟩
@[simp] lemma rpow_eq_pow (x : ℝ≥0∞) (y : ℝ) : rpow x y = x ^ y := rfl
@[simp] lemma rpow_zero {x : ℝ≥0∞} : x ^ (0 : ℝ) = 1 :=
by cases x; { dsimp only [(^), rpow], simp [lt_irrefl] }
lemma top_rpow_def (y : ℝ) : (⊤ : ℝ≥0∞) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 :=
rfl
@[simp] lemma top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ℝ≥0∞) ^ y = ⊤ :=
by simp [top_rpow_def, h]
@[simp] lemma top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ℝ≥0∞) ^ y = 0 :=
by simp [top_rpow_def, asymm h, ne_of_lt h]
@[simp] lemma zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ℝ≥0∞) ^ y = 0 :=
begin
rw [← ennreal.coe_zero, ← ennreal.some_eq_coe],
dsimp only [(^), rpow],
simp [h, asymm h, ne_of_gt h],
end
@[simp] lemma zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ℝ≥0∞) ^ y = ⊤ :=
begin
rw [← ennreal.coe_zero, ← ennreal.some_eq_coe],
dsimp only [(^), rpow],
simp [h, ne_of_gt h],
end
lemma zero_rpow_def (y : ℝ) : (0 : ℝ≥0∞) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ :=
begin
rcases lt_trichotomy 0 y with H|rfl|H,
{ simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] },
{ simp [lt_irrefl] },
{ simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] }
end
@[simp] lemma zero_rpow_mul_self (y : ℝ) : (0 : ℝ≥0∞) ^ y * 0 ^ y = 0 ^ y :=
by { rw zero_rpow_def, split_ifs, exacts [zero_mul _, one_mul _, top_mul_top] }
@[norm_cast] lemma coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) :
(x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) :=
begin
rw [← ennreal.some_eq_coe],
dsimp only [(^), rpow],
simp [h]
end
@[norm_cast] lemma coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) :
(x : ℝ≥0∞) ^ y = (x ^ y : ℝ≥0) :=
begin
by_cases hx : x = 0,
{ rcases le_iff_eq_or_lt.1 h with H|H,
{ simp [hx, H.symm] },
{ simp [hx, zero_rpow_of_pos H, nnreal.zero_rpow (ne_of_gt H)] } },
{ exact coe_rpow_of_ne_zero hx _ }
end
lemma coe_rpow_def (x : ℝ≥0) (y : ℝ) :
(x : ℝ≥0∞) ^ y = if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) := rfl
@[simp] lemma rpow_one (x : ℝ≥0∞) : x ^ (1 : ℝ) = x :=
begin
cases x,
{ exact dif_pos zero_lt_one },
{ change ite _ _ _ = _,
simp,
exact λ _, zero_le_one.not_lt }
end
@[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0∞) ^ x = 1 :=
by { rw [← coe_one, coe_rpow_of_ne_zero one_ne_zero], simp }
@[simp] lemma rpow_eq_zero_iff {x : ℝ≥0∞} {y : ℝ} :
x ^ y = 0 ↔ (x = 0 ∧ 0 < y) ∨ (x = ⊤ ∧ y < 0) :=
begin
cases x,
{ rcases lt_trichotomy y 0 with H|H|H;
simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] },
{ by_cases h : x = 0,
{ rcases lt_trichotomy y 0 with H|H|H;
simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] },
{ simp [coe_rpow_of_ne_zero h, h] } }
end
@[simp] lemma rpow_eq_top_iff {x : ℝ≥0∞} {y : ℝ} :
x ^ y = ⊤ ↔ (x = 0 ∧ y < 0) ∨ (x = ⊤ ∧ 0 < y) :=
begin
cases x,
{ rcases lt_trichotomy y 0 with H|H|H;
simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] },
{ by_cases h : x = 0,
{ rcases lt_trichotomy y 0 with H|H|H;
simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] },
{ simp [coe_rpow_of_ne_zero h, h] } }
end
lemma rpow_eq_top_iff_of_pos {x : ℝ≥0∞} {y : ℝ} (hy : 0 < y) : x ^ y = ⊤ ↔ x = ⊤ :=
by simp [rpow_eq_top_iff, hy, asymm hy]
lemma rpow_eq_top_of_nonneg (x : ℝ≥0∞) {y : ℝ} (hy0 : 0 ≤ y) : x ^ y = ⊤ → x = ⊤ :=
begin
rw ennreal.rpow_eq_top_iff,
intro h,
cases h,
{ exfalso, rw lt_iff_not_ge at h, exact h.right hy0, },
{ exact h.left, },
end
lemma rpow_ne_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y ≠ ⊤ :=
mt (ennreal.rpow_eq_top_of_nonneg x hy0) h
lemma rpow_lt_top_of_nonneg {x : ℝ≥0∞} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y < ⊤ :=
lt_top_iff_ne_top.mpr (ennreal.rpow_ne_top_of_nonneg hy0 h)
lemma rpow_add {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z :=
begin
cases x, { exact (h'x rfl).elim },
have : x ≠ 0 := λ h, by simpa [h] using hx,
simp [coe_rpow_of_ne_zero this, nnreal.rpow_add this]
end
lemma rpow_neg (x : ℝ≥0∞) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ :=
begin
cases x,
{ rcases lt_trichotomy y 0 with H|H|H;
simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] },
{ by_cases h : x = 0,
{ rcases lt_trichotomy y 0 with H|H|H;
simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] },
{ have A : x ^ y ≠ 0, by simp [h],
simp [coe_rpow_of_ne_zero h, ← coe_inv A, nnreal.rpow_neg] } }
end
lemma rpow_sub {x : ℝ≥0∞} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y - z) = x ^ y / x ^ z :=
by rw [sub_eq_add_neg, rpow_add _ _ hx h'x, rpow_neg, div_eq_mul_inv]
lemma rpow_neg_one (x : ℝ≥0∞) : x ^ (-1 : ℝ) = x ⁻¹ :=
by simp [rpow_neg]
lemma rpow_mul (x : ℝ≥0∞) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z :=
begin
cases x,
{ rcases lt_trichotomy y 0 with Hy|Hy|Hy;
rcases lt_trichotomy z 0 with Hz|Hz|Hz;
simp [Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos,
mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] },
{ by_cases h : x = 0,
{ rcases lt_trichotomy y 0 with Hy|Hy|Hy;
rcases lt_trichotomy z 0 with Hz|Hz|Hz;
simp [h, Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos,
mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] },
{ have : x ^ y ≠ 0, by simp [h],
simp [coe_rpow_of_ne_zero h, coe_rpow_of_ne_zero this, nnreal.rpow_mul] } }
end
@[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0∞) (n : ℕ) : x ^ (n : ℝ) = x ^ n :=
begin
cases x,
{ cases n;
simp [top_rpow_of_pos (nat.cast_add_one_pos _), top_pow (nat.succ_pos _)] },
{ simp [coe_rpow_of_nonneg _ (nat.cast_nonneg n)] }
end
@[simp] lemma rpow_two (x : ℝ≥0∞) : x ^ (2 : ℝ) = x ^ 2 :=
by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] }
lemma mul_rpow_eq_ite (x y : ℝ≥0∞) (z : ℝ) :
(x * y) ^ z = if (x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0) ∧ z < 0 then ⊤ else x ^ z * y ^ z :=
begin
rcases eq_or_ne z 0 with rfl|hz, { simp },
replace hz := hz.lt_or_lt,
wlog hxy : x ≤ y := le_total x y using [x y, y x] tactic.skip,
{ rcases eq_or_ne x 0 with rfl|hx0,
{ induction y using with_top.rec_top_coe; cases hz with hz hz; simp [*, hz.not_lt] },
rcases eq_or_ne y 0 with rfl|hy0, { exact (hx0 (bot_unique hxy)).elim },
induction x using with_top.rec_top_coe, { cases hz with hz hz; simp [hz, top_unique hxy] },
induction y using with_top.rec_top_coe, { cases hz with hz hz; simp * },
simp only [*, false_and, and_false, false_or, if_false],
norm_cast at *,
rw [coe_rpow_of_ne_zero (mul_ne_zero hx0 hy0), nnreal.mul_rpow] },
{ convert this using 2; simp only [mul_comm, and_comm, or_comm] }
end
lemma mul_rpow_of_ne_top {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) :
(x * y) ^ z = x^z * y^z :=
by simp [*, mul_rpow_eq_ite]
@[norm_cast] lemma coe_mul_rpow (x y : ℝ≥0) (z : ℝ) :
((x : ℝ≥0∞) * y) ^ z = x^z * y^z :=
mul_rpow_of_ne_top coe_ne_top coe_ne_top z
lemma mul_rpow_of_ne_zero {x y : ℝ≥0∞} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) :
(x * y) ^ z = x ^ z * y ^ z :=
by simp [*, mul_rpow_eq_ite]
lemma mul_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) :
(x * y) ^ z = x ^ z * y ^ z :=
by simp [hz.not_lt, mul_rpow_eq_ite]
lemma inv_rpow (x : ℝ≥0∞) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ :=
begin
rcases eq_or_ne y 0 with rfl|hy, { simp only [rpow_zero, inv_one] },
replace hy := hy.lt_or_lt,
rcases eq_or_ne x 0 with rfl|h0, { cases hy; simp * },
rcases eq_or_ne x ⊤ with rfl|h_top, { cases hy; simp * },
apply eq_inv_of_mul_eq_one_left,
rw [← mul_rpow_of_ne_zero (inv_ne_zero.2 h_top) h0, inv_mul_cancel h0 h_top, one_rpow]
end
lemma div_rpow_of_nonneg (x y : ℝ≥0∞) {z : ℝ} (hz : 0 ≤ z) :
(x / y) ^ z = x ^ z / y ^ z :=
by rw [div_eq_mul_inv, mul_rpow_of_nonneg _ _ hz, inv_rpow, div_eq_mul_inv]
lemma strict_mono_rpow_of_pos {z : ℝ} (h : 0 < z) : strict_mono (λ x : ℝ≥0∞, x ^ z) :=
begin
intros x y hxy,
lift x to ℝ≥0 using ne_top_of_lt hxy,
rcases eq_or_ne y ∞ with rfl|hy,
{ simp only [top_rpow_of_pos h, coe_rpow_of_nonneg _ h.le, coe_lt_top] },
{ lift y to ℝ≥0 using hy,
simp only [coe_rpow_of_nonneg _ h.le, nnreal.rpow_lt_rpow (coe_lt_coe.1 hxy) h, coe_lt_coe] }
end
lemma monotone_rpow_of_nonneg {z : ℝ} (h : 0 ≤ z) : monotone (λ x : ℝ≥0∞, x ^ z) :=
h.eq_or_lt.elim (λ h0, h0 ▸ by simp only [rpow_zero, monotone_const])
(λ h0, (strict_mono_rpow_of_pos h0).monotone)
/-- Bundles `λ x : ℝ≥0∞, x ^ y` into an order isomorphism when `y : ℝ` is positive,
where the inverse is `λ x : ℝ≥0∞, x ^ (1 / y)`. -/
@[simps apply] def order_iso_rpow (y : ℝ) (hy : 0 < y) : ℝ≥0∞ ≃o ℝ≥0∞ :=
(strict_mono_rpow_of_pos hy).order_iso_of_right_inverse (λ x, x ^ y) (λ x, x ^ (1 / y))
(λ x, by { dsimp, rw [←rpow_mul, one_div_mul_cancel hy.ne.symm, rpow_one] })
lemma order_iso_rpow_symm_apply (y : ℝ) (hy : 0 < y) :
(order_iso_rpow y hy).symm = order_iso_rpow (1 / y) (one_div_pos.2 hy) :=
by { simp only [order_iso_rpow, one_div_one_div], refl }
lemma rpow_le_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z :=
monotone_rpow_of_nonneg h₂ h₁
lemma rpow_lt_rpow {x y : ℝ≥0∞} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z :=
strict_mono_rpow_of_pos h₂ h₁
lemma rpow_le_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y :=
(strict_mono_rpow_of_pos hz).le_iff_le
lemma rpow_lt_rpow_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y :=
(strict_mono_rpow_of_pos hz).lt_iff_lt
lemma le_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y :=
begin
nth_rewrite 0 ←rpow_one x,
nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z hz.ne',
rw [rpow_mul, ←one_div, @rpow_le_rpow_iff _ _ (1/z) (by simp [hz])],
end
lemma lt_rpow_one_div_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x < y ^ (1 / z) ↔ x ^ z < y :=
begin
nth_rewrite 0 ←rpow_one x,
nth_rewrite 0 ←@_root_.mul_inv_cancel _ _ z (ne_of_lt hz).symm,
rw [rpow_mul, ←one_div, @rpow_lt_rpow_iff _ _ (1/z) (by simp [hz])],
end
lemma rpow_one_div_le_iff {x y : ℝ≥0∞} {z : ℝ} (hz : 0 < z) : x ^ (1 / z) ≤ y ↔ x ≤ y ^ z :=
begin
nth_rewrite 0 ← ennreal.rpow_one y,
nth_rewrite 1 ← @_root_.mul_inv_cancel _ _ z hz.ne.symm,
rw [ennreal.rpow_mul, ← one_div, ennreal.rpow_le_rpow_iff (one_div_pos.2 hz)],
end
lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0∞} {y z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) :
x^y < x^z :=
begin
lift x to ℝ≥0 using hx',
rw [one_lt_coe_iff] at hx,
simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)),
nnreal.rpow_lt_rpow_of_exponent_lt hx hyz]
end
lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0∞} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z :=
begin
cases x,
{ rcases lt_trichotomy y 0 with Hy|Hy|Hy;
rcases lt_trichotomy z 0 with Hz|Hz|Hz;
simp [Hy, Hz, top_rpow_of_neg, top_rpow_of_pos, le_refl];
linarith },
{ simp only [one_le_coe_iff, some_eq_coe] at hx,
simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)),
nnreal.rpow_le_rpow_of_exponent_le hx hyz] }
end
lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0∞} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) :
x^y < x^z :=
begin
lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top),
simp at hx0 hx1,
simp [coe_rpow_of_ne_zero (ne_of_gt hx0), nnreal.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz]
end
lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0∞} {y z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) :
x^y ≤ x^z :=
begin
lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx1 coe_lt_top),
by_cases h : x = 0,
{ rcases lt_trichotomy y 0 with Hy|Hy|Hy;
rcases lt_trichotomy z 0 with Hz|Hz|Hz;
simp [Hy, Hz, h, zero_rpow_of_neg, zero_rpow_of_pos, le_refl];
linarith },
{ simp at hx1,
simp [coe_rpow_of_ne_zero h,
nnreal.rpow_le_rpow_of_exponent_ge (bot_lt_iff_ne_bot.mpr h) hx1 hyz] }
end
lemma rpow_le_self_of_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x :=
begin
nth_rewrite 1 ←ennreal.rpow_one x,
exact ennreal.rpow_le_rpow_of_exponent_ge hx h_one_le,
end
lemma le_rpow_self_of_one_le {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (h_one_le : 1 ≤ z) : x ≤ x ^ z :=
begin
nth_rewrite 0 ←ennreal.rpow_one x,
exact ennreal.rpow_le_rpow_of_exponent_le hx h_one_le,
end
lemma rpow_pos_of_nonneg {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hp_nonneg : 0 ≤ p) : 0 < x^p :=
begin
by_cases hp_zero : p = 0,
{ simp [hp_zero, ennreal.zero_lt_one], },
{ rw ←ne.def at hp_zero,
have hp_pos := lt_of_le_of_ne hp_nonneg hp_zero.symm,
rw ←zero_rpow_of_pos hp_pos, exact rpow_lt_rpow hx_pos hp_pos, },
end
lemma rpow_pos {p : ℝ} {x : ℝ≥0∞} (hx_pos : 0 < x) (hx_ne_top : x ≠ ⊤) : 0 < x^p :=
begin
cases lt_or_le 0 p with hp_pos hp_nonpos,
{ exact rpow_pos_of_nonneg hx_pos (le_of_lt hp_pos), },
{ rw [←neg_neg p, rpow_neg, inv_pos],
exact rpow_ne_top_of_nonneg (by simp [hp_nonpos]) hx_ne_top, },
end
lemma rpow_lt_one {x : ℝ≥0∞} {z : ℝ} (hx : x < 1) (hz : 0 < z) : x^z < 1 :=
begin
lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx le_top),
simp only [coe_lt_one_iff] at hx,
simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.rpow_lt_one hx hz],
end
lemma rpow_le_one {x : ℝ≥0∞} {z : ℝ} (hx : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 :=
begin
lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx coe_lt_top),
simp only [coe_le_one_iff] at hx,
simp [coe_rpow_of_nonneg _ hz, nnreal.rpow_le_one hx hz],
end
lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 :=
begin
cases x,
{ simp [top_rpow_of_neg hz, ennreal.zero_lt_one] },
{ simp only [some_eq_coe, one_lt_coe_iff] at hx,
simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)),
nnreal.rpow_lt_one_of_one_lt_of_neg hx hz] },
end
lemma rpow_le_one_of_one_le_of_neg {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : z < 0) : x^z ≤ 1 :=
begin
cases x,
{ simp [top_rpow_of_neg hz, ennreal.zero_lt_one] },
{ simp only [one_le_coe_iff, some_eq_coe] at hx,
simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)),
nnreal.rpow_le_one_of_one_le_of_nonpos hx (le_of_lt hz)] },
end
lemma one_lt_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z :=
begin
cases x,
{ simp [top_rpow_of_pos hz] },
{ simp only [some_eq_coe, one_lt_coe_iff] at hx,
simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_lt_rpow hx hz] }
end
lemma one_le_rpow {x : ℝ≥0∞} {z : ℝ} (hx : 1 ≤ x) (hz : 0 < z) : 1 ≤ x^z :=
begin
cases x,
{ simp [top_rpow_of_pos hz] },
{ simp only [one_le_coe_iff, some_eq_coe] at hx,
simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_le_rpow hx (le_of_lt hz)] },
end
lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1)
(hz : z < 0) : 1 < x^z :=
begin
lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx2 le_top),
simp only [coe_lt_one_iff, coe_pos] at ⊢ hx1 hx2,
simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz],
end
lemma one_le_rpow_of_pos_of_le_one_of_neg {x : ℝ≥0∞} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1)
(hz : z < 0) : 1 ≤ x^z :=
begin
lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx2 coe_lt_top),
simp only [coe_le_one_iff, coe_pos] at ⊢ hx1 hx2,
simp [coe_rpow_of_ne_zero (ne_of_gt hx1),
nnreal.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 (le_of_lt hz)],
end
lemma to_nnreal_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_nnreal) ^ z = (x ^ z).to_nnreal :=
begin
rcases lt_trichotomy z 0 with H|H|H,
{ cases x, { simp [H, ne_of_lt] },
by_cases hx : x = 0,
{ simp [hx, H, ne_of_lt] },
{ simp [coe_rpow_of_ne_zero hx] } },
{ simp [H] },
{ cases x, { simp [H, ne_of_gt] },
simp [coe_rpow_of_nonneg _ (le_of_lt H)] }
end
lemma to_real_rpow (x : ℝ≥0∞) (z : ℝ) : (x.to_real) ^ z = (x ^ z).to_real :=
by rw [ennreal.to_real, ennreal.to_real, ←nnreal.coe_rpow, ennreal.to_nnreal_rpow]
lemma of_real_rpow_of_pos {x p : ℝ} (hx_pos : 0 < x) :
ennreal.of_real x ^ p = ennreal.of_real (x ^ p) :=
begin
simp_rw ennreal.of_real,
rw [coe_rpow_of_ne_zero, coe_eq_coe, real.to_nnreal_rpow_of_nonneg hx_pos.le],
simp [hx_pos],
end
lemma of_real_rpow_of_nonneg {x p : ℝ} (hx_nonneg : 0 ≤ x) (hp_nonneg : 0 ≤ p) :
ennreal.of_real x ^ p = ennreal.of_real (x ^ p) :=
begin
by_cases hp0 : p = 0,
{ simp [hp0], },
by_cases hx0 : x = 0,
{ rw ← ne.def at hp0,
have hp_pos : 0 < p := lt_of_le_of_ne hp_nonneg hp0.symm,
simp [hx0, hp_pos, hp_pos.ne.symm], },
rw ← ne.def at hx0,
exact of_real_rpow_of_pos (hx_nonneg.lt_of_ne hx0.symm),
end
lemma rpow_left_injective {x : ℝ} (hx : x ≠ 0) :
function.injective (λ y : ℝ≥0∞, y^x) :=
begin
intros y z hyz,
dsimp only at hyz,
rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul, rpow_mul, hyz],
end
lemma rpow_left_surjective {x : ℝ} (hx : x ≠ 0) :
function.surjective (λ y : ℝ≥0∞, y^x) :=
λ y, ⟨y ^ x⁻¹, by simp_rw [←rpow_mul, _root_.inv_mul_cancel hx, rpow_one]⟩
lemma rpow_left_bijective {x : ℝ} (hx : x ≠ 0) :
function.bijective (λ y : ℝ≥0∞, y^x) :=
⟨rpow_left_injective hx, rpow_left_surjective hx⟩
theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) :
tendsto (λ (x : ℝ≥0∞), x ^ y) (𝓝 ⊤) (𝓝 ⊤) :=
begin
rw tendsto_nhds_top_iff_nnreal,
intros x,
obtain ⟨c, _, hc⟩ :=
(at_top_basis_Ioi.tendsto_iff at_top_basis_Ioi).mp (nnreal.tendsto_rpow_at_top hy) x trivial,
have hc' : set.Ioi (↑c) ∈ 𝓝 (⊤ : ℝ≥0∞) := Ioi_mem_nhds coe_lt_top,
refine eventually_of_mem hc' _,
intros a ha,
by_cases ha' : a = ⊤,
{ simp [ha', hy] },
lift a to ℝ≥0 using ha',
change ↑c < ↑a at ha,
rw coe_rpow_of_nonneg _ hy.le,
exact_mod_cast hc a (by exact_mod_cast ha),
end
private lemma continuous_at_rpow_const_of_pos {x : ℝ≥0∞} {y : ℝ} (h : 0 < y) :
continuous_at (λ a : ℝ≥0∞, a ^ y) x :=
begin
by_cases hx : x = ⊤,
{ rw [hx, continuous_at],
convert tendsto_rpow_at_top h,
simp [h] },
lift x to ℝ≥0 using hx,
rw continuous_at_coe_iff,
convert continuous_coe.continuous_at.comp
(nnreal.continuous_at_rpow_const (or.inr h.le)) using 1,
ext1 x,
simp [coe_rpow_of_nonneg _ h.le]
end
@[continuity]
lemma continuous_rpow_const {y : ℝ} : continuous (λ a : ℝ≥0∞, a ^ y) :=
begin
apply continuous_iff_continuous_at.2 (λ x, _),
rcases lt_trichotomy 0 y with hy|rfl|hy,
{ exact continuous_at_rpow_const_of_pos hy },
{ simp, exact continuous_at_const },
{ obtain ⟨z, hz⟩ : ∃ z, y = -z := ⟨-y, (neg_neg _).symm⟩,
have z_pos : 0 < z, by simpa [hz] using hy,
simp_rw [hz, rpow_neg],
exact continuous_inv.continuous_at.comp (continuous_at_rpow_const_of_pos z_pos) }
end
lemma tendsto_const_mul_rpow_nhds_zero_of_pos {c : ℝ≥0∞} (hc : c ≠ ∞) {y : ℝ} (hy : 0 < y) :
tendsto (λ x : ℝ≥0∞, c * x ^ y) (𝓝 0) (𝓝 0) :=
begin
convert ennreal.tendsto.const_mul (ennreal.continuous_rpow_const.tendsto 0) _,
{ simp [hy] },
{ exact or.inr hc }
end
end ennreal
lemma filter.tendsto.ennrpow_const {α : Type*} {f : filter α} {m : α → ℝ≥0∞} {a : ℝ≥0∞} (r : ℝ)
(hm : tendsto m f (𝓝 a)) :
tendsto (λ x, (m x) ^ r) f (𝓝 (a ^ r)) :=
(ennreal.continuous_rpow_const.tendsto a).comp hm
namespace norm_num
open tactic
theorem rpow_pos (a b : ℝ) (b' : ℕ) (c : ℝ) (hb : (b':ℝ) = b) (h : a ^ b' = c) : a ^ b = c :=
by rw [← h, ← hb, real.rpow_nat_cast]
theorem rpow_neg (a b : ℝ) (b' : ℕ) (c c' : ℝ)
(a0 : 0 ≤ a) (hb : (b':ℝ) = b) (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' :=
by rw [← hc, ← h, ← hb, real.rpow_neg a0, real.rpow_nat_cast]
/-- Evaluate `real.rpow a b` where `a` is a rational numeral and `b` is an integer.
(This cannot go via the generalized version `prove_rpow'` because `rpow_pos` has a side condition;
we do not attempt to evaluate `a ^ b` where `a` and `b` are both negative because it comes
out to some garbage.) -/
meta def prove_rpow (a b : expr) : tactic (expr × expr) := do
na ← a.to_rat,
ic ← mk_instance_cache `(ℝ),
match match_sign b with
| sum.inl b := do
(ic, a0) ← guard (na ≥ 0) >> prove_nonneg ic a,
nc ← mk_instance_cache `(ℕ),
(ic, nc, b', hb) ← prove_nat_uncast ic nc b,
(ic, c, h) ← prove_pow a na ic b',
cr ← c.to_rat,
(ic, c', hc) ← prove_inv ic c cr,
pure (c', (expr.const ``rpow_neg []).mk_app [a, b, b', c, c', a0, hb, h, hc])
| sum.inr ff := pure (`(1:ℝ), expr.const ``real.rpow_zero [] a)
| sum.inr tt := do
nc ← mk_instance_cache `(ℕ),
(ic, nc, b', hb) ← prove_nat_uncast ic nc b,
(ic, c, h) ← prove_pow a na ic b',
pure (c, (expr.const ``rpow_pos []).mk_app [a, b, b', c, hb, h])
end
/-- Generalized version of `prove_cpow`, `prove_nnrpow`, `prove_ennrpow`. -/
meta def prove_rpow' (pos neg zero : name) (α β one a b : expr) : tactic (expr × expr) := do
na ← a.to_rat,
icα ← mk_instance_cache α,
icβ ← mk_instance_cache β,
match match_sign b with
| sum.inl b := do
nc ← mk_instance_cache `(ℕ),
(icβ, nc, b', hb) ← prove_nat_uncast icβ nc b,
(icα, c, h) ← prove_pow a na icα b',
cr ← c.to_rat,
(icα, c', hc) ← prove_inv icα c cr,
pure (c', (expr.const neg []).mk_app [a, b, b', c, c', hb, h, hc])
| sum.inr ff := pure (one, expr.const zero [] a)
| sum.inr tt := do
nc ← mk_instance_cache `(ℕ),
(icβ, nc, b', hb) ← prove_nat_uncast icβ nc b,
(icα, c, h) ← prove_pow a na icα b',
pure (c, (expr.const pos []).mk_app [a, b, b', c, hb, h])
end
open_locale nnreal ennreal
theorem cpow_pos (a b : ℂ) (b' : ℕ) (c : ℂ) (hb : b = b') (h : a ^ b' = c) : a ^ b = c :=
by rw [← h, hb, complex.cpow_nat_cast]
theorem cpow_neg (a b : ℂ) (b' : ℕ) (c c' : ℂ)
(hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' :=
by rw [← hc, ← h, hb, complex.cpow_neg, complex.cpow_nat_cast]
theorem nnrpow_pos (a : ℝ≥0) (b : ℝ) (b' : ℕ) (c : ℝ≥0)
(hb : b = b') (h : a ^ b' = c) : a ^ b = c :=
by rw [← h, hb, nnreal.rpow_nat_cast]
theorem nnrpow_neg (a : ℝ≥0) (b : ℝ) (b' : ℕ) (c c' : ℝ≥0)
(hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' :=
by rw [← hc, ← h, hb, nnreal.rpow_neg, nnreal.rpow_nat_cast]
theorem ennrpow_pos (a : ℝ≥0∞) (b : ℝ) (b' : ℕ) (c : ℝ≥0∞)
(hb : b = b') (h : a ^ b' = c) : a ^ b = c :=
by rw [← h, hb, ennreal.rpow_nat_cast]
theorem ennrpow_neg (a : ℝ≥0∞) (b : ℝ) (b' : ℕ) (c c' : ℝ≥0∞)
(hb : b = b') (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' :=
by rw [← hc, ← h, hb, ennreal.rpow_neg, ennreal.rpow_nat_cast]
/-- Evaluate `complex.cpow a b` where `a` is a rational numeral and `b` is an integer. -/
meta def prove_cpow : expr → expr → tactic (expr × expr) :=
prove_rpow' ``cpow_pos ``cpow_neg ``complex.cpow_zero `(ℂ) `(ℂ) `(1:ℂ)
/-- Evaluate `nnreal.rpow a b` where `a` is a rational numeral and `b` is an integer. -/
meta def prove_nnrpow : expr → expr → tactic (expr × expr) :=
prove_rpow' ``nnrpow_pos ``nnrpow_neg ``nnreal.rpow_zero `(ℝ≥0) `(ℝ) `(1:ℝ≥0)
/-- Evaluate `ennreal.rpow a b` where `a` is a rational numeral and `b` is an integer. -/
meta def prove_ennrpow : expr → expr → tactic (expr × expr) :=
prove_rpow' ``ennrpow_pos ``ennrpow_neg ``ennreal.rpow_zero `(ℝ≥0∞) `(ℝ) `(1:ℝ≥0∞)
/-- Evaluates expressions of the form `rpow a b`, `cpow a b` and `a ^ b` in the special case where
`b` is an integer and `a` is a positive rational (so it's really just a rational power). -/
@[norm_num] meta def eval_rpow_cpow : expr → tactic (expr × expr)
| `(@has_pow.pow _ _ real.has_pow %%a %%b) := b.to_int >> prove_rpow a b
| `(real.rpow %%a %%b) := b.to_int >> prove_rpow a b
| `(@has_pow.pow _ _ complex.has_pow %%a %%b) := b.to_int >> prove_cpow a b
| `(complex.cpow %%a %%b) := b.to_int >> prove_cpow a b
| `(@has_pow.pow _ _ nnreal.real.has_pow %%a %%b) := b.to_int >> prove_nnrpow a b
| `(nnreal.rpow %%a %%b) := b.to_int >> prove_nnrpow a b
| `(@has_pow.pow _ _ ennreal.real.has_pow %%a %%b) := b.to_int >> prove_ennrpow a b
| `(ennreal.rpow %%a %%b) := b.to_int >> prove_ennrpow a b
| _ := tactic.failed
end norm_num
|
c630ae2c57fd4b705dca0ad7746f9afff04972ac | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/fin/succ_pred.lean | 1a8277e5e971f285c56dadc5b2716efd1e50af7d | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,997 | lean | /-
Copyright (c) 2022 Eric Rodriguez. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Rodriguez
-/
import data.fin.basic
import order.succ_pred.basic
/-!
# Successors and predecessors of `fin n`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file, we show that `fin n` is both a `succ_order` and a `pred_order`. Note that they are
also archimedean, but this is derived from the general instance for well-orderings as opposed
to a specific `fin` instance.
-/
namespace fin
instance : ∀ {n : ℕ}, succ_order (fin n)
| 0 := by constructor; exact elim0
| (n+1) :=
_root_.succ_order.of_core (λ i, if i < fin.last n then i + 1 else i)
begin
intros a ha b,
rw [is_max_iff_eq_top, eq_top_iff, not_le, top_eq_last] at ha,
rw [if_pos ha, lt_iff_coe_lt_coe, le_iff_coe_le_coe, coe_add_one_of_lt ha],
exact nat.lt_iff_add_one_le
end
begin
intros a ha,
rw [is_max_iff_eq_top, top_eq_last] at ha,
rw [if_neg ha.not_lt],
end
@[simp] lemma succ_eq {n : ℕ} : succ_order.succ = λ a, if a < fin.last n then a + 1 else a := rfl
@[simp] lemma succ_apply {n : ℕ} (a) :
succ_order.succ a = if a < fin.last n then a + 1 else a := rfl
instance : ∀ {n : ℕ}, pred_order (fin n)
| 0 := by constructor; exact elim0
| (n+1) :=
_root_.pred_order.of_core (λ x, if x = 0 then 0 else x - 1)
begin
intros a ha b,
rw [is_min_iff_eq_bot, eq_bot_iff, not_le, bot_eq_zero] at ha,
rw [if_neg ha.ne', lt_iff_coe_lt_coe, le_iff_coe_le_coe, coe_sub_one,
if_neg ha.ne', le_tsub_iff_right, iff.comm],
exact nat.lt_iff_add_one_le,
exact ha
end
begin
intros a ha,
rw [is_min_iff_eq_bot, bot_eq_zero] at ha,
rwa [if_pos ha, eq_comm],
end
@[simp] lemma pred_eq {n} : pred_order.pred = λ a : fin (n + 1), if a = 0 then 0 else a - 1 := rfl
@[simp] lemma pred_apply {n : ℕ} (a : fin (n + 1)) :
pred_order.pred a = if a = 0 then 0 else a - 1 := rfl
end fin
|
d6cccf73a733021e97cbbae80654c726cc3de934 | bb31430994044506fa42fd667e2d556327e18dfe | /src/algebra/support.lean | 79286fe5e723250f2bb0dbc0822d2aa3761bfd05 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 12,640 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import order.conditionally_complete_lattice.basic
import data.set.finite
import algebra.big_operators.basic
import algebra.group.prod
import algebra.group.pi
import algebra.module.basic
import group_theory.group_action.pi
/-!
# Support of a function
In this file we define `function.support f = {x | f x ≠ 0}` and prove its basic properties.
We also define `function.mul_support f = {x | f x ≠ 1}`.
-/
open set
open_locale big_operators
namespace function
variables {α β A B M N P R S G M₀ G₀ : Type*} {ι : Sort*}
section has_one
variables [has_one M] [has_one N] [has_one P]
/-- `support` of a function is the set of points `x` such that `f x ≠ 0`. -/
def support [has_zero A] (f : α → A) : set α := {x | f x ≠ 0}
/-- `mul_support` of a function is the set of points `x` such that `f x ≠ 1`. -/
@[to_additive] def mul_support (f : α → M) : set α := {x | f x ≠ 1}
@[to_additive] lemma mul_support_eq_preimage (f : α → M) : mul_support f = f ⁻¹' {1}ᶜ := rfl
@[to_additive] lemma nmem_mul_support {f : α → M} {x : α} :
x ∉ mul_support f ↔ f x = 1 :=
not_not
@[to_additive] lemma compl_mul_support {f : α → M} :
(mul_support f)ᶜ = {x | f x = 1} :=
ext $ λ x, nmem_mul_support
@[simp, to_additive] lemma mem_mul_support {f : α → M} {x : α} :
x ∈ mul_support f ↔ f x ≠ 1 :=
iff.rfl
@[simp, to_additive] lemma mul_support_subset_iff {f : α → M} {s : set α} :
mul_support f ⊆ s ↔ ∀ x, f x ≠ 1 → x ∈ s :=
iff.rfl
@[to_additive] lemma mul_support_subset_iff' {f : α → M} {s : set α} :
mul_support f ⊆ s ↔ ∀ x ∉ s, f x = 1 :=
forall_congr $ λ x, not_imp_comm
@[to_additive] lemma mul_support_eq_iff {f : α → M} {s : set α} :
mul_support f = s ↔ ((∀ x, x ∈ s → f x ≠ 1) ∧ (∀ x, x ∉ s → f x = 1)) :=
by simp only [set.ext_iff, mem_mul_support, ne.def, imp_not_comm, ← forall_and_distrib,
← iff_def, ← xor_iff_not_iff', ← xor_iff_iff_not]
@[to_additive] lemma mul_support_disjoint_iff {f : α → M} {s : set α} :
disjoint (mul_support f) s ↔ eq_on f 1 s :=
by simp_rw [←subset_compl_iff_disjoint_right, mul_support_subset_iff', not_mem_compl_iff, eq_on,
pi.one_apply]
@[to_additive] lemma disjoint_mul_support_iff {f : α → M} {s : set α} :
disjoint s (mul_support f) ↔ eq_on f 1 s :=
by rw [disjoint.comm, mul_support_disjoint_iff]
@[simp, to_additive] lemma mul_support_eq_empty_iff {f : α → M} :
mul_support f = ∅ ↔ f = 1 :=
by { simp_rw [← subset_empty_iff, mul_support_subset_iff', funext_iff], simp }
@[simp, to_additive] lemma mul_support_nonempty_iff {f : α → M} :
(mul_support f).nonempty ↔ f ≠ 1 :=
by rw [nonempty_iff_ne_empty, ne.def, mul_support_eq_empty_iff]
@[to_additive]
lemma range_subset_insert_image_mul_support (f : α → M) :
range f ⊆ insert 1 (f '' mul_support f) :=
by simpa only [range_subset_iff, mem_insert_iff, or_iff_not_imp_left]
using λ x (hx : x ∈ mul_support f), mem_image_of_mem f hx
@[simp, to_additive] lemma mul_support_one' : mul_support (1 : α → M) = ∅ :=
mul_support_eq_empty_iff.2 rfl
@[simp, to_additive] lemma mul_support_one : mul_support (λ x : α, (1 : M)) = ∅ :=
mul_support_one'
@[to_additive] lemma mul_support_const {c : M} (hc : c ≠ 1) :
mul_support (λ x : α, c) = set.univ :=
by { ext x, simp [hc] }
@[to_additive] lemma mul_support_binop_subset (op : M → N → P) (op1 : op 1 1 = 1)
(f : α → M) (g : α → N) :
mul_support (λ x, op (f x) (g x)) ⊆ mul_support f ∪ mul_support g :=
λ x hx, not_or_of_imp (λ hf hg, hx $ by simp only [hf, hg, op1])
@[to_additive] lemma mul_support_sup [semilattice_sup M] (f g : α → M) :
mul_support (λ x, f x ⊔ g x) ⊆ mul_support f ∪ mul_support g :=
mul_support_binop_subset (⊔) sup_idem f g
@[to_additive] lemma mul_support_inf [semilattice_inf M] (f g : α → M) :
mul_support (λ x, f x ⊓ g x) ⊆ mul_support f ∪ mul_support g :=
mul_support_binop_subset (⊓) inf_idem f g
@[to_additive] lemma mul_support_max [linear_order M] (f g : α → M) :
mul_support (λ x, max (f x) (g x)) ⊆ mul_support f ∪ mul_support g :=
mul_support_sup f g
@[to_additive] lemma mul_support_min [linear_order M] (f g : α → M) :
mul_support (λ x, min (f x) (g x)) ⊆ mul_support f ∪ mul_support g :=
mul_support_inf f g
@[to_additive] lemma mul_support_supr [conditionally_complete_lattice M] [nonempty ι]
(f : ι → α → M) :
mul_support (λ x, ⨆ i, f i x) ⊆ ⋃ i, mul_support (f i) :=
begin
rw mul_support_subset_iff',
simp only [mem_Union, not_exists, nmem_mul_support],
intros x hx,
simp only [hx, csupr_const]
end
@[to_additive] lemma mul_support_infi [conditionally_complete_lattice M] [nonempty ι]
(f : ι → α → M) :
mul_support (λ x, ⨅ i, f i x) ⊆ ⋃ i, mul_support (f i) :=
@mul_support_supr _ Mᵒᵈ ι ⟨(1:M)⟩ _ _ f
@[to_additive] lemma mul_support_comp_subset {g : M → N} (hg : g 1 = 1) (f : α → M) :
mul_support (g ∘ f) ⊆ mul_support f :=
λ x, mt $ λ h, by simp only [(∘), *]
@[to_additive] lemma mul_support_subset_comp {g : M → N} (hg : ∀ {x}, g x = 1 → x = 1)
(f : α → M) :
mul_support f ⊆ mul_support (g ∘ f) :=
λ x, mt hg
@[to_additive] lemma mul_support_comp_eq (g : M → N) (hg : ∀ {x}, g x = 1 ↔ x = 1)
(f : α → M) :
mul_support (g ∘ f) = mul_support f :=
set.ext $ λ x, not_congr hg
@[to_additive] lemma mul_support_comp_eq_preimage (g : β → M) (f : α → β) :
mul_support (g ∘ f) = f ⁻¹' mul_support g :=
rfl
@[to_additive support_prod_mk] lemma mul_support_prod_mk (f : α → M) (g : α → N) :
mul_support (λ x, (f x, g x)) = mul_support f ∪ mul_support g :=
set.ext $ λ x, by simp only [mul_support, not_and_distrib, mem_union, mem_set_of_eq,
prod.mk_eq_one, ne.def]
@[to_additive support_prod_mk'] lemma mul_support_prod_mk' (f : α → M × N) :
mul_support f = mul_support (λ x, (f x).1) ∪ mul_support (λ x, (f x).2) :=
by simp only [← mul_support_prod_mk, prod.mk.eta]
@[to_additive] lemma mul_support_along_fiber_subset (f : α × β → M) (a : α) :
mul_support (λ b, f (a, b)) ⊆ (mul_support f).image prod.snd :=
by tidy
@[simp, to_additive] lemma mul_support_along_fiber_finite_of_finite
(f : α × β → M) (a : α) (h : (mul_support f).finite) :
(mul_support (λ b, f (a, b))).finite :=
(h.image prod.snd).subset (mul_support_along_fiber_subset f a)
end has_one
@[to_additive] lemma mul_support_mul [mul_one_class M] (f g : α → M) :
mul_support (λ x, f x * g x) ⊆ mul_support f ∪ mul_support g :=
mul_support_binop_subset (*) (one_mul _) f g
@[to_additive] lemma mul_support_pow [monoid M] (f : α → M) (n : ℕ) :
mul_support (λ x, f x ^ n) ⊆ mul_support f :=
begin
induction n with n hfn,
{ simpa only [pow_zero, mul_support_one] using empty_subset _ },
{ simpa only [pow_succ] using (mul_support_mul f _).trans (union_subset subset.rfl hfn) }
end
section division_monoid
variables [division_monoid G] (f g : α → G)
@[simp, to_additive]
lemma mul_support_inv : mul_support (λ x, (f x)⁻¹) = mul_support f := ext $ λ _, inv_ne_one
@[simp, to_additive] lemma mul_support_inv' : mul_support f⁻¹ = mul_support f := mul_support_inv f
@[to_additive] lemma mul_support_mul_inv :
mul_support (λ x, f x * (g x)⁻¹) ⊆ mul_support f ∪ mul_support g :=
mul_support_binop_subset (λ a b, a * b⁻¹) (by simp) f g
@[to_additive] lemma mul_support_div :
mul_support (λ x, f x / g x) ⊆ mul_support f ∪ mul_support g :=
mul_support_binop_subset (/) one_div_one f g
end division_monoid
lemma support_smul [has_zero R] [has_zero M] [smul_with_zero R M] [no_zero_smul_divisors R M]
(f : α → R) (g : α → M) :
support (f • g) = support f ∩ support g :=
ext $ λ x, smul_ne_zero_iff
@[simp] lemma support_mul [mul_zero_class R] [no_zero_divisors R] (f g : α → R) :
support (λ x, f x * g x) = support f ∩ support g :=
support_smul f g
@[simp] lemma support_mul_subset_left [mul_zero_class R] (f g : α → R) :
support (λ x, f x * g x) ⊆ support f :=
λ x hfg hf, hfg $ by simp only [hf, zero_mul]
@[simp] lemma support_mul_subset_right [mul_zero_class R] (f g : α → R) :
support (λ x, f x * g x) ⊆ support g :=
λ x hfg hg, hfg $ by simp only [hg, mul_zero]
lemma support_smul_subset_right [add_monoid A] [monoid B] [distrib_mul_action B A]
(b : B) (f : α → A) :
support (b • f) ⊆ support f :=
λ x hbf hf, hbf $ by rw [pi.smul_apply, hf, smul_zero]
lemma support_smul_subset_left [has_zero M] [has_zero β] [smul_with_zero M β]
(f : α → M) (g : α → β) :
support (f • g) ⊆ support f :=
λ x hfg hf, hfg $ by rw [pi.smul_apply', hf, zero_smul]
lemma support_const_smul_of_ne_zero [semiring R] [add_comm_monoid M] [module R M]
[no_zero_smul_divisors R M] (c : R) (g : α → M) (hc : c ≠ 0) :
support (c • g) = support g :=
ext $ λ x, by simp only [hc, mem_support, pi.smul_apply, ne.def, smul_eq_zero, false_or]
@[simp] lemma support_inv [group_with_zero G₀] (f : α → G₀) :
support (λ x, (f x)⁻¹) = support f :=
set.ext $ λ x, not_congr inv_eq_zero
@[simp] lemma support_div [group_with_zero G₀] (f g : α → G₀) :
support (λ x, f x / g x) = support f ∩ support g :=
by simp [div_eq_mul_inv]
@[to_additive] lemma mul_support_prod [comm_monoid M] (s : finset α) (f : α → β → M) :
mul_support (λ x, ∏ i in s, f i x) ⊆ ⋃ i ∈ s, mul_support (f i) :=
begin
rw mul_support_subset_iff',
simp only [mem_Union, not_exists, nmem_mul_support],
exact λ x, finset.prod_eq_one
end
lemma support_prod_subset [comm_monoid_with_zero A] (s : finset α) (f : α → β → A) :
support (λ x, ∏ i in s, f i x) ⊆ ⋂ i ∈ s, support (f i) :=
λ x hx, mem_Inter₂.2 $ λ i hi H, hx $ finset.prod_eq_zero hi H
lemma support_prod [comm_monoid_with_zero A] [no_zero_divisors A] [nontrivial A]
(s : finset α) (f : α → β → A) :
support (λ x, ∏ i in s, f i x) = ⋂ i ∈ s, support (f i) :=
set.ext $ λ x, by
simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists]
lemma mul_support_one_add [has_one R] [add_left_cancel_monoid R] (f : α → R) :
mul_support (λ x, 1 + f x) = support f :=
set.ext $ λ x, not_congr add_right_eq_self
lemma mul_support_one_add' [has_one R] [add_left_cancel_monoid R] (f : α → R) :
mul_support (1 + f) = support f :=
mul_support_one_add f
lemma mul_support_add_one [has_one R] [add_right_cancel_monoid R] (f : α → R) :
mul_support (λ x, f x + 1) = support f :=
set.ext $ λ x, not_congr add_left_eq_self
lemma mul_support_add_one' [has_one R] [add_right_cancel_monoid R] (f : α → R) :
mul_support (f + 1) = support f :=
mul_support_add_one f
lemma mul_support_one_sub' [has_one R] [add_group R] (f : α → R) :
mul_support (1 - f) = support f :=
by rw [sub_eq_add_neg, mul_support_one_add', support_neg']
lemma mul_support_one_sub [has_one R] [add_group R] (f : α → R) :
mul_support (λ x, 1 - f x) = support f :=
mul_support_one_sub' f
end function
namespace set
open function
variables {α β M : Type*} [has_one M] {f : α → M}
@[to_additive] lemma image_inter_mul_support_eq {s : set β} {g : β → α} :
(g '' s ∩ mul_support f) = g '' (s ∩ mul_support (f ∘ g)) :=
by rw [mul_support_comp_eq_preimage f g, image_inter_preimage]
end set
namespace pi
variables {A : Type*} {B : Type*} [decidable_eq A] [has_one B] {a : A} {b : B}
open function
@[to_additive] lemma mul_support_mul_single_subset : mul_support (mul_single a b) ⊆ {a} :=
λ x hx, by_contra $ λ hx', hx $ mul_single_eq_of_ne hx' _
@[to_additive] lemma mul_support_mul_single_one : mul_support (mul_single a (1 : B)) = ∅ :=
by simp
@[simp, to_additive] lemma mul_support_mul_single_of_ne (h : b ≠ 1) :
mul_support (mul_single a b) = {a} :=
mul_support_mul_single_subset.antisymm $
λ x (hx : x = a), by rwa [mem_mul_support, hx, mul_single_eq_same]
@[to_additive] lemma mul_support_mul_single [decidable_eq B] :
mul_support (mul_single a b) = if b = 1 then ∅ else {a} := by { split_ifs with h; simp [h] }
@[to_additive]
lemma mul_support_mul_single_disjoint {b' : B} (hb : b ≠ 1) (hb' : b' ≠ 1) {i j : A} :
disjoint (mul_support (mul_single i b)) (mul_support (mul_single j b')) ↔ i ≠ j :=
by rw [mul_support_mul_single_of_ne hb, mul_support_mul_single_of_ne hb', disjoint_singleton]
end pi
|
28e39a3e6aff2cb4506b4f98f1f38b078647cde6 | 69bc7d0780be17e452d542a93f9599488f1c0c8e | /exam_2_rev.lean | fa294ae9024a107d0377be85e28f5985c1e61117 | [] | no_license | joek13/cs2102-notes | b7352285b1d1184fae25594f89f5926d74e6d7b4 | 25bb18788641b20af9cf3c429afe1da9b2f5eafb | refs/heads/master | 1,673,461,162,867 | 1,575,561,090,000 | 1,575,561,090,000 | 207,573,549 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 849 | lean | def there_is_no_largest_nat := ¬ ∃ (n : nat), ∀ (k : nat), n > k
inductive friend : Type
| harsh : friend
| joe : friend
| will : friend
| sullivan : friend
inductive taller : friend → friend → Prop
| trans : ∀ (a b c : friend), (taller a b) → (taller b c) → taller a c
open friend
def joe_taller_harsh := taller joe harsh
def harsh_taller_kevin := taller harsh sullivan
def joe_taller_kevin := taller joe sullivan
axiom tallest : ∀ (f : friend), taller will f
def mtransitive {α : Type} (r : α → α → Prop) : (Prop) := ∀ (a b c : α), r a b → r b c → r a c
example : taller joe harsh → taller harsh sullivan → taller joe sullivan :=
begin
apply taller.trans,
end
def myreverse {α : Type} : list α → list α
| list.nil := []
| (list.cons a t) := list.append (myreverse t) [a]
#eval myreverse [1,2,3] |
49eefe315e0ccb926e86905fb862dfdd078c66b1 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/order/filter/lift.lean | 01669badfeaeef0ea447893382001328c34073e5 | [
"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 | 21,239 | 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
-/
import order.filter.bases
/-!
# Lift filters along filter and set functions
-/
open set
open_locale classical filter
namespace filter
variables {α : Type*} {β : Type*} {γ : Type*} {ι : Sort*}
section lift
/-- A variant on `bind` using a function `g` taking a set instead of a member of `α`.
This is essentially a push-forward along a function mapping each set to a filter. -/
protected def lift (f : filter α) (g : set α → filter β) :=
⨅s ∈ f, g s
variables {f f₁ f₂ : filter α} {g g₁ g₂ : set α → filter β}
/-- If `(p : ι → Prop, s : ι → set α)` is a basis of a filter `f`, `g` is a monotone function
`set α → filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → set α)` is a basis
of the filter `g (s i)`, then `(λ (i : ι) (x : β i), p i ∧ pg i x, λ (i : ι) (x : β i), sg i x)`
is a basis of the filter `f.lift g`.
This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using
`has_basis` one has to use `Σ i, β i` as the index type, see `filter.has_basis.lift`.
This lemma states the corresponding `mem_iff` statement without using a sigma type. -/
lemma has_basis.mem_lift_iff {ι} {p : ι → Prop} {s : ι → set α} {f : filter α}
(hf : f.has_basis p s) {β : ι → Type*} {pg : Π i, β i → Prop} {sg : Π i, β i → set γ}
{g : set α → filter γ} (hg : ∀ i, (g $ s i).has_basis (pg i) (sg i)) (gm : monotone g)
{s : set γ} :
s ∈ f.lift g ↔ ∃ (i : ι) (hi : p i) (x : β i) (hx : pg i x), sg i x ⊆ s :=
begin
refine (mem_binfi _ ⟨univ, univ_sets _⟩).trans _,
{ intros t₁ ht₁ t₂ ht₂,
exact ⟨t₁ ∩ t₂, inter_mem_sets ht₁ ht₂, gm $ inter_subset_left _ _,
gm $ inter_subset_right _ _⟩ },
{ simp only [← (hg _).mem_iff],
exact hf.exists_iff (λ t₁ t₂ ht H, gm ht H) }
end
/-- If `(p : ι → Prop, s : ι → set α)` is a basis of a filter `f`, `g` is a monotone function
`set α → filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → set α)` is a basis
of the filter `g (s i)`, then `(λ (i : ι) (x : β i), p i ∧ pg i x, λ (i : ι) (x : β i), sg i x)`
is a basis of the filter `f.lift g`.
This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using
`has_basis` one has to use `Σ i, β i` as the index type. See also `filter.has_basis.mem_lift_iff`
for the corresponding `mem_iff` statement formulated without using a sigma type. -/
lemma has_basis.lift {ι} {p : ι → Prop} {s : ι → set α} {f : filter α} (hf : f.has_basis p s)
{β : ι → Type*} {pg : Π i, β i → Prop} {sg : Π i, β i → set γ} {g : set α → filter γ}
(hg : ∀ i, (g $ s i).has_basis (pg i) (sg i)) (gm : monotone g) :
(f.lift g).has_basis (λ i : Σ i, β i, p i.1 ∧ pg i.1 i.2) (λ i : Σ i, β i, sg i.1 i.2) :=
begin
refine ⟨λ t, (hf.mem_lift_iff hg gm).trans _⟩,
simp [sigma.exists, and_assoc, exists_and_distrib_left]
end
lemma mem_lift_sets (hg : monotone g) {s : set β} :
s ∈ f.lift g ↔ ∃t∈f, s ∈ g t :=
(f.basis_sets.mem_lift_iff (λ s, (g s).basis_sets) hg).trans $
by simp only [id, ← exists_sets_subset_iff]
lemma mem_lift {s : set β} {t : set α} (ht : t ∈ f) (hs : s ∈ g t) :
s ∈ f.lift g :=
le_principal_iff.mp $ show f.lift g ≤ 𝓟 s,
from infi_le_of_le t $ infi_le_of_le ht $ le_principal_iff.mpr hs
lemma lift_le {f : filter α} {g : set α → filter β} {h : filter β} {s : set α}
(hs : s ∈ f) (hg : g s ≤ h) : f.lift g ≤ h :=
infi_le_of_le s $ infi_le_of_le hs $ hg
lemma le_lift {f : filter α} {g : set α → filter β} {h : filter β}
(hh : ∀s∈f, h ≤ g s) : h ≤ f.lift g :=
le_infi $ assume s, le_infi $ assume hs, hh s hs
lemma lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ :=
infi_le_infi $ assume s, infi_le_infi2 $ assume hs, ⟨hf hs, hg s⟩
lemma lift_mono' (hg : ∀s∈f, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ :=
infi_le_infi $ assume s, infi_le_infi $ assume hs, hg s hs
lemma map_lift_eq {m : β → γ} (hg : monotone g) : map m (f.lift g) = f.lift (map m ∘ g) :=
have monotone (map m ∘ g),
from map_mono.comp hg,
filter.ext $ λ s,
by simp only [mem_lift_sets hg, mem_lift_sets this, exists_prop, mem_map, function.comp_app]
lemma comap_lift_eq {m : γ → β} (hg : monotone g) : comap m (f.lift g) = f.lift (comap m ∘ g) :=
have monotone (comap m ∘ g),
from comap_mono.comp hg,
begin
ext,
simp only [mem_lift_sets hg, mem_lift_sets this, mem_comap_sets, exists_prop, mem_lift_sets],
exact ⟨λ ⟨b, ⟨a, ha, hb⟩, hs⟩, ⟨a, ha, b, hb, hs⟩, λ ⟨a, ha, b, hb, hs⟩, ⟨b, ⟨a, ha, hb⟩, hs⟩⟩
end
theorem comap_lift_eq2 {m : β → α} {g : set β → filter γ} (hg : monotone g) :
(comap m f).lift g = f.lift (g ∘ preimage m) :=
le_antisymm
(le_infi $ assume s, le_infi $ assume hs,
infi_le_of_le (preimage m s) $ infi_le _ ⟨s, hs, subset.refl _⟩)
(le_infi $ assume s, le_infi $ assume ⟨s', hs', (h_sub : preimage m s' ⊆ s)⟩,
infi_le_of_le s' $ infi_le_of_le hs' $ hg h_sub)
lemma map_lift_eq2 {g : set β → filter γ} {m : α → β} (hg : monotone g) :
(map m f).lift g = f.lift (g ∘ image m) :=
le_antisymm
(infi_le_infi2 $ assume s, ⟨image m s,
infi_le_infi2 $ assume hs, ⟨
f.sets_of_superset hs $ assume a h, mem_image_of_mem _ h,
le_refl _⟩⟩)
(infi_le_infi2 $ assume t, ⟨preimage m t,
infi_le_infi2 $ assume ht, ⟨ht,
hg $ assume x, assume h : x ∈ m '' preimage m t,
let ⟨y, hy, h_eq⟩ := h in
show x ∈ t, from h_eq ▸ hy⟩⟩)
lemma lift_comm {g : filter β} {h : set α → set β → filter γ} :
f.lift (λs, g.lift (h s)) = g.lift (λt, f.lift (λs, h s t)) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj,
infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi)
(le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj,
infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi)
lemma lift_assoc {h : set β → filter γ} (hg : monotone g) :
(f.lift g).lift h = f.lift (λs, (g s).lift h) :=
le_antisymm
(le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht,
infi_le_of_le t $ infi_le _ $ (mem_lift_sets hg).mpr ⟨_, hs, ht⟩)
(le_infi $ assume t, le_infi $ assume ht,
let ⟨s, hs, h'⟩ := (mem_lift_sets hg).mp ht in
infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le t $ infi_le _ h')
lemma lift_lift_same_le_lift {g : set α → set α → filter β} :
f.lift (λs, f.lift (g s)) ≤ f.lift (λs, g s s) :=
le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le _ hs
lemma lift_lift_same_eq_lift {g : set α → set α → filter β}
(hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)) :
f.lift (λs, f.lift (g s)) = f.lift (λs, g s s) :=
le_antisymm
lift_lift_same_le_lift
(le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht,
infi_le_of_le (s ∩ t) $
infi_le_of_le (inter_mem_sets hs ht) $
calc g (s ∩ t) (s ∩ t) ≤ g s (s ∩ t) : hg₂ (s ∩ t) (inter_subset_left _ _)
... ≤ g s t : hg₁ s (inter_subset_right _ _))
lemma lift_principal {s : set α} (hg : monotone g) :
(𝓟 s).lift g = g s :=
le_antisymm
(infi_le_of_le s $ infi_le _ $ subset.refl _)
(le_infi $ assume t, le_infi $ assume hi, hg hi)
theorem monotone_lift [preorder γ] {f : γ → filter α} {g : γ → set α → filter β}
(hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift (g c)) :=
assume a b h, lift_mono (hf h) (hg h)
lemma lift_ne_bot_iff (hm : monotone g) : (ne_bot $ f.lift g) ↔ (∀s∈f, ne_bot (g s)) :=
begin
rw [filter.lift, infi_subtype', infi_ne_bot_iff_of_directed', subtype.forall'],
{ rintros ⟨s, hs⟩ ⟨t, ht⟩,
exact ⟨⟨s ∩ t, inter_mem_sets hs ht⟩, hm (inter_subset_left s t), hm (inter_subset_right s t)⟩ }
end
@[simp] lemma lift_const {f : filter α} {g : filter β} : f.lift (λx, g) = g :=
le_antisymm (lift_le univ_mem_sets $ le_refl g) (le_lift $ assume s hs, le_refl g)
@[simp] lemma lift_inf {f : filter α} {g h : set α → filter β} :
f.lift (λx, g x ⊓ h x) = f.lift g ⊓ f.lift h :=
by simp only [filter.lift, infi_inf_eq, eq_self_iff_true]
@[simp] lemma lift_principal2 {f : filter α} : f.lift 𝓟 = f :=
le_antisymm
(assume s hs, mem_lift hs (mem_principal_self s))
(le_infi $ assume s, le_infi $ assume hs, by simp only [hs, le_principal_iff])
lemma lift_infi {f : ι → filter α} {g : set α → filter β}
[hι : nonempty ι] (hg : ∀{s t}, g s ⊓ g t = g (s ∩ t)) : (infi f).lift g = (⨅i, (f i).lift g) :=
le_antisymm
(le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _))
(assume s,
have g_mono : monotone g,
from assume s t h, le_of_inf_eq $ eq.trans hg $ congr_arg g $ inter_eq_self_of_subset_left h,
have ∀t∈(infi f), (⨅ (i : ι), filter.lift (f i) g) ≤ g t,
from assume t ht, infi_sets_induct ht
(let ⟨i⟩ := hι in infi_le_of_le i $ infi_le_of_le univ $ infi_le _ univ_mem_sets)
(assume i s₁ s₂ hs₁ hs₂,
@hg s₁ s₂ ▸ le_inf (infi_le_of_le i $ infi_le_of_le s₁ $ infi_le _ hs₁) hs₂)
(assume s₁ s₂ hs₁ hs₂, le_trans hs₂ $ g_mono hs₁),
begin
simp only [mem_lift_sets g_mono, exists_imp_distrib],
exact assume t ht hs, this t ht hs
end)
end lift
section lift'
/-- Specialize `lift` to functions `set α → set β`. This can be viewed as a generalization of `map`.
This is essentially a push-forward along a function mapping each set to a set. -/
protected def lift' (f : filter α) (h : set α → set β) :=
f.lift (𝓟 ∘ h)
variables {f f₁ f₂ : filter α} {h h₁ h₂ : set α → set β}
lemma mem_lift' {t : set α} (ht : t ∈ f) : h t ∈ (f.lift' h) :=
le_principal_iff.mp $ show f.lift' h ≤ 𝓟 (h t),
from infi_le_of_le t $ infi_le_of_le ht $ le_refl _
lemma has_basis.lift' {ι} {p : ι → Prop} {s} (hf : f.has_basis p s) (hh : monotone h) :
(f.lift' h).has_basis p (h ∘ s) :=
begin
refine ⟨λ t, (hf.mem_lift_iff _ (monotone_principal.comp hh)).trans _⟩,
show ∀ i, (𝓟 (h (s i))).has_basis (λ j : unit, true) (λ (j : unit), h (s i)),
from λ i, has_basis_principal _,
simp only [exists_const]
end
lemma mem_lift'_sets (hh : monotone h) {s : set β} : s ∈ (f.lift' h) ↔ (∃t∈f, h t ⊆ s) :=
mem_lift_sets $ monotone_principal.comp hh
lemma eventually_lift'_iff (hh : monotone h) {p : β → Prop} :
(∀ᶠ y in f.lift' h, p y) ↔ (∃ t ∈ f, ∀ y ∈ h t, p y) :=
mem_lift'_sets hh
lemma lift'_le {f : filter α} {g : set α → set β} {h : filter β} {s : set α}
(hs : s ∈ f) (hg : 𝓟 (g s) ≤ h) : f.lift' g ≤ h :=
lift_le hs hg
lemma lift'_mono (hf : f₁ ≤ f₂) (hh : h₁ ≤ h₂) : f₁.lift' h₁ ≤ f₂.lift' h₂ :=
lift_mono hf $ assume s, principal_mono.mpr $ hh s
lemma lift'_mono' (hh : ∀s∈f, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ :=
infi_le_infi $ assume s, infi_le_infi $ assume hs, principal_mono.mpr $ hh s hs
lemma lift'_cong (hh : ∀s∈f, h₁ s = h₂ s) : f.lift' h₁ = f.lift' h₂ :=
le_antisymm (lift'_mono' $ assume s hs, le_of_eq $ hh s hs) (lift'_mono' $ assume s hs, le_of_eq $ (hh s hs).symm)
lemma map_lift'_eq {m : β → γ} (hh : monotone h) : map m (f.lift' h) = f.lift' (image m ∘ h) :=
calc map m (f.lift' h) = f.lift (map m ∘ 𝓟 ∘ h) :
map_lift_eq $ monotone_principal.comp hh
... = f.lift' (image m ∘ h) : by simp only [(∘), filter.lift', map_principal, eq_self_iff_true]
lemma map_lift'_eq2 {g : set β → set γ} {m : α → β} (hg : monotone g) :
(map m f).lift' g = f.lift' (g ∘ image m) :=
map_lift_eq2 $ monotone_principal.comp hg
theorem comap_lift'_eq {m : γ → β} (hh : monotone h) :
comap m (f.lift' h) = f.lift' (preimage m ∘ h) :=
calc comap m (f.lift' h) = f.lift (comap m ∘ 𝓟 ∘ h) :
comap_lift_eq $ monotone_principal.comp hh
... = f.lift' (preimage m ∘ h) : by simp only [(∘), filter.lift', comap_principal, eq_self_iff_true]
theorem comap_lift'_eq2 {m : β → α} {g : set β → set γ} (hg : monotone g) :
(comap m f).lift' g = f.lift' (g ∘ preimage m) :=
comap_lift_eq2 $ monotone_principal.comp hg
lemma lift'_principal {s : set α} (hh : monotone h) :
(𝓟 s).lift' h = 𝓟 (h s) :=
lift_principal $ monotone_principal.comp hh
lemma lift'_pure {a : α} (hh : monotone h) :
(pure a : filter α).lift' h = 𝓟 (h {a}) :=
by rw [← principal_singleton, lift'_principal hh]
lemma lift'_bot (hh : monotone h) : (⊥ : filter α).lift' h = 𝓟 (h ∅) :=
by rw [← principal_empty, lift'_principal hh]
lemma principal_le_lift' {t : set β} (hh : ∀s∈f, t ⊆ h s) :
𝓟 t ≤ f.lift' h :=
le_infi $ assume s, le_infi $ assume hs, principal_mono.mpr (hh s hs)
theorem monotone_lift' [preorder γ] {f : γ → filter α} {g : γ → set α → set β}
(hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift' (g c)) :=
assume a b h, lift'_mono (hf h) (hg h)
lemma lift_lift'_assoc {g : set α → set β} {h : set β → filter γ}
(hg : monotone g) (hh : monotone h) :
(f.lift' g).lift h = f.lift (λs, h (g s)) :=
calc (f.lift' g).lift h = f.lift (λs, (𝓟 (g s)).lift h) :
lift_assoc (monotone_principal.comp hg)
... = f.lift (λs, h (g s)) : by simp only [lift_principal, hh, eq_self_iff_true]
lemma lift'_lift'_assoc {g : set α → set β} {h : set β → set γ}
(hg : monotone g) (hh : monotone h) :
(f.lift' g).lift' h = f.lift' (λs, h (g s)) :=
lift_lift'_assoc hg (monotone_principal.comp hh)
lemma lift'_lift_assoc {g : set α → filter β} {h : set β → set γ}
(hg : monotone g) : (f.lift g).lift' h = f.lift (λs, (g s).lift' h) :=
lift_assoc hg
lemma lift_lift'_same_le_lift' {g : set α → set α → set β} :
f.lift (λs, f.lift' (g s)) ≤ f.lift' (λs, g s s) :=
lift_lift_same_le_lift
lemma lift_lift'_same_eq_lift' {g : set α → set α → set β}
(hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)) :
f.lift (λs, f.lift' (g s)) = f.lift' (λs, g s s) :=
lift_lift_same_eq_lift
(assume s, monotone_principal.comp (hg₁ s))
(assume t, monotone_principal.comp (hg₂ t))
lemma lift'_inf_principal_eq {h : set α → set β} {s : set β} :
f.lift' h ⊓ 𝓟 s = f.lift' (λt, h t ∩ s) :=
by simp only [filter.lift', filter.lift, (∘), ← inf_principal, infi_subtype', ← infi_inf]
lemma lift'_ne_bot_iff (hh : monotone h) : (ne_bot (f.lift' h)) ↔ (∀s∈f, (h s).nonempty) :=
calc (ne_bot (f.lift' h)) ↔ (∀s∈f, ne_bot (𝓟 (h s))) :
lift_ne_bot_iff (monotone_principal.comp hh)
... ↔ (∀s∈f, (h s).nonempty) : by simp only [principal_ne_bot_iff]
@[simp] lemma lift'_id {f : filter α} : f.lift' id = f :=
lift_principal2
lemma le_lift' {f : filter α} {h : set α → set β} {g : filter β}
(h_le : ∀s∈f, h s ∈ g) : g ≤ f.lift' h :=
le_infi $ assume s, le_infi $ assume hs, by simp only [h_le, le_principal_iff, function.comp_app]; exact h_le s hs
lemma lift_infi' {f : ι → filter α} {g : set α → filter β}
[nonempty ι] (hf : directed (≥) f) (hg : monotone g) : (infi f).lift g = (⨅i, (f i).lift g) :=
le_antisymm
(le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _))
(assume s,
begin
rw mem_lift_sets hg,
simp only [exists_imp_distrib, mem_infi hf],
exact assume t i ht hs, mem_infi_sets i $ mem_lift ht hs
end)
lemma lift'_infi {f : ι → filter α} {g : set α → set β}
[nonempty ι] (hg : ∀{s t}, g s ∩ g t = g (s ∩ t)) : (infi f).lift' g = (⨅i, (f i).lift' g) :=
lift_infi $ λ s t, by simp only [principal_eq_iff_eq, inf_principal, (∘), hg]
lemma lift'_inf (f g : filter α) {s : set α → set β} (hs : ∀ {t₁ t₂}, s t₁ ∩ s t₂ = s (t₁ ∩ t₂)) :
(f ⊓ g).lift' s = f.lift' s ⊓ g.lift' s :=
have (⨅ b : bool, cond b f g).lift' s = ⨅ b : bool, (cond b f g).lift' s :=
lift'_infi @hs,
by simpa only [infi_bool_eq]
theorem comap_eq_lift' {f : filter β} {m : α → β} :
comap m f = f.lift' (preimage m) :=
filter.ext $ λ s, (mem_lift'_sets monotone_preimage).symm
lemma lift'_inf_powerset (f g : filter α) :
(f ⊓ g).lift' powerset = f.lift' powerset ⊓ g.lift' powerset :=
lift'_inf f g $ λ _ _, (powerset_inter _ _).symm
lemma eventually_lift'_powerset {f : filter α} {p : set α → Prop} :
(∀ᶠ s in f.lift' powerset, p s) ↔ ∃ s ∈ f, ∀ t ⊆ s, p t :=
eventually_lift'_iff monotone_powerset
lemma eventually_lift'_powerset' {f : filter α} {p : set α → Prop}
(hp : ∀ ⦃s t⦄, s ⊆ t → p t → p s) :
(∀ᶠ s in f.lift' powerset, p s) ↔ ∃ s ∈ f, p s :=
eventually_lift'_powerset.trans $ exists_congr $ λ s, exists_congr $
λ hsf, ⟨λ H, H s (subset.refl s), λ hs t ht, hp ht hs⟩
instance lift'_powerset_ne_bot (f : filter α) : ne_bot (f.lift' powerset) :=
(lift'_ne_bot_iff monotone_powerset).2 $ λ _ _, powerset_nonempty
lemma tendsto_lift'_powerset_mono {la : filter α} {lb : filter β} {s t : α → set β}
(ht : tendsto t la (lb.lift' powerset)) (hst : ∀ᶠ x in la, s x ⊆ t x) :
tendsto s la (lb.lift' powerset) :=
begin
simp only [filter.lift', filter.lift, (∘), tendsto_infi, tendsto_principal] at ht ⊢,
exact λ u hu, (ht u hu).mp (hst.mono $ λ a hst ht, subset.trans hst ht)
end
@[simp] lemma eventually_lift'_powerset_forall {f : filter α} {p : α → Prop} :
(∀ᶠ s in f.lift' powerset, ∀ x ∈ s, p x) ↔ ∀ᶠ x in f, p x :=
iff.trans (eventually_lift'_powerset' $ λ s t hst ht x hx, ht x (hst hx))
exists_sets_subset_iff
alias eventually_lift'_powerset_forall ↔
filter.eventually.of_lift'_powerset filter.eventually.lift'_powerset
@[simp] lemma eventually_lift'_powerset_eventually {f g : filter α} {p : α → Prop} :
(∀ᶠ s in f.lift' powerset, ∀ᶠ x in g, x ∈ s → p x) ↔ ∀ᶠ x in f ⊓ g, p x :=
calc _ ↔ ∃ s ∈ f, ∀ᶠ x in g, x ∈ s → p x :
eventually_lift'_powerset' $ λ s t hst ht, ht.mono $ λ x hx hs, hx (hst hs)
... ↔ ∃ (s ∈ f) (t ∈ g), ∀ x, x ∈ t → x ∈ s → p x :
by simp only [eventually_iff_exists_mem]
... ↔ ∀ᶠ x in f ⊓ g, p x :
by simp only [filter.eventually, mem_inf_sets, subset_def, mem_inter_iff,
← and_imp, and_comm, mem_set_of_eq]
end lift'
section prod
variables {f : filter α}
lemma prod_def {f : filter α} {g : filter β} : f ×ᶠ g = (f.lift $ λs, g.lift' $ set.prod s) :=
have ∀(s:set α) (t : set β),
𝓟 (set.prod s t) = (𝓟 s).comap prod.fst ⊓ (𝓟 t).comap prod.snd,
by simp only [principal_eq_iff_eq, comap_principal, inf_principal]; intros; refl,
begin
simp only [filter.lift', function.comp, this, lift_inf, lift_const, lift_inf],
rw [← comap_lift_eq monotone_principal, ← comap_lift_eq monotone_principal],
simp only [filter.prod, lift_principal2, eq_self_iff_true]
end
lemma prod_same_eq : f ×ᶠ f = f.lift' (λt, set.prod t t) :=
by rw [prod_def];
from lift_lift'_same_eq_lift'
(assume s, set.monotone_prod monotone_const monotone_id)
(assume t, set.monotone_prod monotone_id monotone_const)
lemma mem_prod_same_iff {s : set (α×α)} :
s ∈ f ×ᶠ f ↔ (∃t∈f, set.prod t t ⊆ s) :=
by rw [prod_same_eq, mem_lift'_sets]; exact set.monotone_prod monotone_id monotone_id
lemma tendsto_prod_self_iff {f : α × α → β} {x : filter α} {y : filter β} :
filter.tendsto f (x ×ᶠ x) y ↔
∀ W ∈ y, ∃ U ∈ x, ∀ (x x' : α), x ∈ U → x' ∈ U → f (x, x') ∈ W :=
by simp only [tendsto_def, mem_prod_same_iff, prod_sub_preimage_iff, exists_prop, iff_self]
variables {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*}
lemma prod_lift_lift
{f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → filter β₁} {g₂ : set α₂ → filter β₂}
(hg₁ : monotone g₁) (hg₂ : monotone g₂) :
(f₁.lift g₁) ×ᶠ (f₂.lift g₂) = f₁.lift (λs, f₂.lift (λt, g₁ s ×ᶠ g₂ t)) :=
begin
simp only [prod_def],
rw [lift_assoc],
apply congr_arg, funext x,
rw [lift_comm],
apply congr_arg, funext y,
rw [lift'_lift_assoc],
exact hg₂,
exact hg₁
end
lemma prod_lift'_lift'
{f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → set β₁} {g₂ : set α₂ → set β₂}
(hg₁ : monotone g₁) (hg₂ : monotone g₂) :
f₁.lift' g₁ ×ᶠ f₂.lift' g₂ = f₁.lift (λs, f₂.lift' (λt, (g₁ s).prod (g₂ t))) :=
begin
rw [prod_def, lift_lift'_assoc],
apply congr_arg, funext x,
rw [lift'_lift'_assoc],
exact hg₂,
exact set.monotone_prod monotone_const monotone_id,
exact hg₁,
exact (monotone_lift' monotone_const $ monotone_lam $
assume x, set.monotone_prod monotone_id monotone_const)
end
end prod
end filter
|
ca95f24fca5d0a92c17fe242243d3afb6645dd45 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/measure_theory/function/simple_func.lean | 7e69629179f8c97c83caaac2c70f9d69843d71d8 | [
"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,052 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Johannes Hölzl
-/
import measure_theory.constructions.borel_space.basic
import algebra.indicator_function
import algebra.support
/-!
# Simple functions
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
A function `f` from a measurable space to any type is called *simple*, if every preimage `f ⁻¹' {x}`
is measurable, and the range is finite. In this file, we define simple functions and establish their
basic properties; and we construct a sequence of simple functions approximating an arbitrary Borel
measurable function `f : α → ℝ≥0∞`.
The theorem `measurable.ennreal_induction` shows that in order to prove something for an arbitrary
measurable function into `ℝ≥0∞`, it is sufficient to show that the property holds for (multiples of)
characteristic functions and is closed under addition and supremum of increasing sequences of
functions.
-/
noncomputable theory
open set (hiding restrict restrict_apply) filter ennreal function (support)
open_locale classical topology big_operators nnreal ennreal measure_theory
namespace measure_theory
variables {α β γ δ : Type*}
/-- A function `f` from a measurable space to any type is called *simple*,
if every preimage `f ⁻¹' {x}` is measurable, and the range is finite. This structure bundles
a function with these properties. -/
structure {u v} simple_func (α : Type u) [measurable_space α] (β : Type v) :=
(to_fun : α → β)
(measurable_set_fiber' : ∀ x, measurable_set (to_fun ⁻¹' {x}))
(finite_range' : (set.range to_fun).finite)
local infixr ` →ₛ `:25 := simple_func
namespace simple_func
section measurable
variables [measurable_space α]
instance has_coe_to_fun : has_coe_to_fun (α →ₛ β) (λ _, α → β) := ⟨to_fun⟩
lemma coe_injective ⦃f g : α →ₛ β⦄ (H : (f : α → β) = g) : f = g :=
by cases f; cases g; congr; exact H
@[ext] theorem ext {f g : α →ₛ β} (H : ∀ a, f a = g a) : f = g :=
coe_injective $ funext H
lemma finite_range (f : α →ₛ β) : (set.range f).finite := f.finite_range'
lemma measurable_set_fiber (f : α →ₛ β) (x : β) : measurable_set (f ⁻¹' {x}) :=
f.measurable_set_fiber' x
@[simp] lemma apply_mk (f : α → β) (h h') (x : α) : simple_func.mk f h h' x = f x := rfl
/-- Simple function defined on the empty type. -/
def of_is_empty [is_empty α] : α →ₛ β :=
{ to_fun := is_empty_elim,
measurable_set_fiber' := λ x, subsingleton.measurable_set,
finite_range' := by simp [range_eq_empty] }
/-- Range of a simple function `α →ₛ β` as a `finset β`. -/
protected def range (f : α →ₛ β) : finset β := f.finite_range.to_finset
@[simp] theorem mem_range {f : α →ₛ β} {b} : b ∈ f.range ↔ b ∈ range f :=
finite.mem_to_finset _
theorem mem_range_self (f : α →ₛ β) (x : α) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩
@[simp] lemma coe_range (f : α →ₛ β) : (↑f.range : set β) = set.range f :=
f.finite_range.coe_to_finset
theorem mem_range_of_measure_ne_zero {f : α →ₛ β} {x : β} {μ : measure α} (H : μ (f ⁻¹' {x}) ≠ 0) :
x ∈ f.range :=
let ⟨a, ha⟩ := nonempty_of_measure_ne_zero H in
mem_range.2 ⟨a, ha⟩
lemma forall_range_iff {f : α →ₛ β} {p : β → Prop} :
(∀ y ∈ f.range, p y) ↔ ∀ x, p (f x) :=
by simp only [mem_range, set.forall_range_iff]
lemma exists_range_iff {f : α →ₛ β} {p : β → Prop} :
(∃ y ∈ f.range, p y) ↔ ∃ x, p (f x) :=
by simpa only [mem_range, exists_prop] using set.exists_range_iff
lemma preimage_eq_empty_iff (f : α →ₛ β) (b : β) : f ⁻¹' {b} = ∅ ↔ b ∉ f.range :=
preimage_singleton_eq_empty.trans $ not_congr mem_range.symm
lemma exists_forall_le [nonempty β] [preorder β] [is_directed β (≤)] (f : α →ₛ β) :
∃ C, ∀ x, f x ≤ C :=
f.range.exists_le.imp $ λ C, forall_range_iff.1
/-- Constant function as a `simple_func`. -/
def const (α) {β} [measurable_space α] (b : β) : α →ₛ β :=
⟨λ a, b, λ x, measurable_set.const _, finite_range_const⟩
instance [inhabited β] : inhabited (α →ₛ β) := ⟨const _ default⟩
theorem const_apply (a : α) (b : β) : (const α b) a = b := rfl
@[simp] theorem coe_const (b : β) : ⇑(const α b) = function.const α b := rfl
@[simp] lemma range_const (α) [measurable_space α] [nonempty α] (b : β) :
(const α b).range = {b} :=
finset.coe_injective $ by simp
lemma range_const_subset (α) [measurable_space α] (b : β) :
(const α b).range ⊆ {b} :=
finset.coe_subset.1 $ by simp
lemma simple_func_bot {α} (f : @simple_func α ⊥ β) [nonempty β] : ∃ c, ∀ x, f x = c :=
begin
have hf_meas := @simple_func.measurable_set_fiber α _ ⊥ f,
simp_rw measurable_space.measurable_set_bot_iff at hf_meas,
casesI is_empty_or_nonempty α,
{ simp only [is_empty.forall_iff, exists_const], },
{ specialize hf_meas (f h.some),
cases hf_meas,
{ exfalso,
refine set.not_mem_empty h.some _,
rw [← hf_meas, set.mem_preimage],
exact set.mem_singleton _, },
{ refine ⟨f h.some, λ x, _⟩,
have : x ∈ f ⁻¹' {f h.some},
{ rw hf_meas, exact set.mem_univ x, },
rwa [set.mem_preimage, set.mem_singleton_iff] at this, }, },
end
lemma simple_func_bot' {α} [nonempty β] (f : @simple_func α ⊥ β) :
∃ c, f = @simple_func.const α _ ⊥ c :=
begin
obtain ⟨c, h_eq⟩ := simple_func_bot f,
refine ⟨c, _⟩,
ext1 x,
rw [h_eq x, simple_func.coe_const],
end
lemma measurable_set_cut (r : α → β → Prop) (f : α →ₛ β)
(h : ∀b, measurable_set {a | r a b}) : measurable_set {a | r a (f a)} :=
begin
have : {a | r a (f a)} = ⋃ b ∈ range f, {a | r a b} ∩ f ⁻¹' {b},
{ ext a,
suffices : r a (f a) ↔ ∃ i, r a (f i) ∧ f a = f i, by simpa,
exact ⟨λ h, ⟨a, ⟨h, rfl⟩⟩, λ ⟨a', ⟨h', e⟩⟩, e.symm ▸ h'⟩ },
rw this,
exact measurable_set.bUnion f.finite_range.countable
(λ b _, measurable_set.inter (h b) (f.measurable_set_fiber _))
end
@[measurability]
theorem measurable_set_preimage (f : α →ₛ β) (s) : measurable_set (f ⁻¹' s) :=
measurable_set_cut (λ _ b, b ∈ s) f (λ b, measurable_set.const (b ∈ s))
/-- A simple function is measurable -/
@[measurability]
protected theorem measurable [measurable_space β] (f : α →ₛ β) : measurable f :=
λ s _, measurable_set_preimage f s
@[measurability]
protected theorem ae_measurable [measurable_space β] {μ : measure α} (f : α →ₛ β) :
ae_measurable f μ :=
f.measurable.ae_measurable
protected lemma sum_measure_preimage_singleton (f : α →ₛ β) {μ : measure α} (s : finset β) :
∑ y in s, μ (f ⁻¹' {y}) = μ (f ⁻¹' ↑s) :=
sum_measure_preimage_singleton _ (λ _ _, f.measurable_set_fiber _)
lemma sum_range_measure_preimage_singleton (f : α →ₛ β) (μ : measure α) :
∑ y in f.range, μ (f ⁻¹' {y}) = μ univ :=
by rw [f.sum_measure_preimage_singleton, coe_range, preimage_range]
/-- If-then-else as a `simple_func`. -/
def piecewise (s : set α) (hs : measurable_set s) (f g : α →ₛ β) : α →ₛ β :=
⟨s.piecewise f g,
λ x, by letI : measurable_space β := ⊤; exact
f.measurable.piecewise hs g.measurable trivial,
(f.finite_range.union g.finite_range).subset range_ite_subset⟩
@[simp] theorem coe_piecewise {s : set α} (hs : measurable_set s) (f g : α →ₛ β) :
⇑(piecewise s hs f g) = s.piecewise f g :=
rfl
theorem piecewise_apply {s : set α} (hs : measurable_set s) (f g : α →ₛ β) (a) :
piecewise s hs f g a = if a ∈ s then f a else g a :=
rfl
@[simp] lemma piecewise_compl {s : set α} (hs : measurable_set sᶜ) (f g : α →ₛ β) :
piecewise sᶜ hs f g = piecewise s hs.of_compl g f :=
coe_injective $ by simp [hs]
@[simp] lemma piecewise_univ (f g : α →ₛ β) : piecewise univ measurable_set.univ f g = f :=
coe_injective $ by simp
@[simp] lemma piecewise_empty (f g : α →ₛ β) : piecewise ∅ measurable_set.empty f g = g :=
coe_injective $ by simp
lemma support_indicator [has_zero β] {s : set α} (hs : measurable_set s) (f : α →ₛ β) :
function.support (f.piecewise s hs (simple_func.const α 0)) = s ∩ function.support f :=
set.support_indicator
lemma range_indicator {s : set α} (hs : measurable_set s)
(hs_nonempty : s.nonempty) (hs_ne_univ : s ≠ univ) (x y : β) :
(piecewise s hs (const α x) (const α y)).range = {x, y} :=
by simp only [← finset.coe_inj, coe_range, coe_piecewise, range_piecewise, coe_const,
finset.coe_insert, finset.coe_singleton, hs_nonempty.image_const,
(nonempty_compl.2 hs_ne_univ).image_const, singleton_union]
lemma measurable_bind [measurable_space γ] (f : α →ₛ β) (g : β → α → γ)
(hg : ∀ b, measurable (g b)) : measurable (λ a, g (f a) a) :=
λ s hs, f.measurable_set_cut (λ a b, g b a ∈ s) $ λ b, hg b hs
/-- If `f : α →ₛ β` is a simple function and `g : β → α →ₛ γ` is a family of simple functions,
then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/
def bind (f : α →ₛ β) (g : β → α →ₛ γ) : α →ₛ γ :=
⟨λa, g (f a) a,
λ c, f.measurable_set_cut (λ a b, g b a = c) $ λ b, (g b).measurable_set_preimage {c},
(f.finite_range.bUnion (λ b _, (g b).finite_range)).subset $
by rintro _ ⟨a, rfl⟩; simp; exact ⟨a, a, rfl⟩⟩
@[simp] theorem bind_apply (f : α →ₛ β) (g : β → α →ₛ γ) (a) :
f.bind g a = g (f a) a := rfl
/-- Given a function `g : β → γ` and a simple function `f : α →ₛ β`, `f.map g` return the simple
function `g ∘ f : α →ₛ γ` -/
def map (g : β → γ) (f : α →ₛ β) : α →ₛ γ := bind f (const α ∘ g)
theorem map_apply (g : β → γ) (f : α →ₛ β) (a) : f.map g a = g (f a) := rfl
theorem map_map (g : β → γ) (h: γ → δ) (f : α →ₛ β) : (f.map g).map h = f.map (h ∘ g) := rfl
@[simp] theorem coe_map (g : β → γ) (f : α →ₛ β) : (f.map g : α → γ) = g ∘ f := rfl
@[simp] theorem range_map [decidable_eq γ] (g : β → γ) (f : α →ₛ β) :
(f.map g).range = f.range.image g :=
finset.coe_injective $ by simp only [coe_range, coe_map, finset.coe_image, range_comp]
@[simp] theorem map_const (g : β → γ) (b : β) : (const α b).map g = const α (g b) := rfl
lemma map_preimage (f : α →ₛ β) (g : β → γ) (s : set γ) :
(f.map g) ⁻¹' s = f ⁻¹' ↑(f.range.filter (λb, g b ∈ s)) :=
by { simp only [coe_range, sep_mem_eq, set.mem_range, function.comp_app, coe_map, finset.coe_filter,
← mem_preimage, inter_comm, preimage_inter_range], apply preimage_comp }
lemma map_preimage_singleton (f : α →ₛ β) (g : β → γ) (c : γ) :
(f.map g) ⁻¹' {c} = f ⁻¹' ↑(f.range.filter (λ b, g b = c)) :=
map_preimage _ _ _
/-- Composition of a `simple_fun` and a measurable function is a `simple_func`. -/
def comp [measurable_space β] (f : β →ₛ γ) (g : α → β) (hgm : measurable g) : α →ₛ γ :=
{ to_fun := f ∘ g,
finite_range' := f.finite_range.subset $ set.range_comp_subset_range _ _,
measurable_set_fiber' := λ z, hgm (f.measurable_set_fiber z) }
@[simp] lemma coe_comp [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) :
⇑(f.comp g hgm) = f ∘ g :=
rfl
lemma range_comp_subset_range [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) :
(f.comp g hgm).range ⊆ f.range :=
finset.coe_subset.1 $ by simp only [coe_range, coe_comp, set.range_comp_subset_range]
/-- Extend a `simple_func` along a measurable embedding: `f₁.extend g hg f₂` is the function
`F : β →ₛ γ` such that `F ∘ g = f₁` and `F y = f₂ y` whenever `y ∉ range g`. -/
def extend [measurable_space β] (f₁ : α →ₛ γ) (g : α → β)
(hg : measurable_embedding g) (f₂ : β →ₛ γ) : β →ₛ γ :=
{ to_fun := function.extend g f₁ f₂,
finite_range' := (f₁.finite_range.union $ f₂.finite_range.subset
(image_subset_range _ _)).subset (range_extend_subset _ _ _),
measurable_set_fiber' :=
begin
letI : measurable_space γ := ⊤, haveI : measurable_singleton_class γ := ⟨λ _, trivial⟩,
exact λ x, hg.measurable_extend f₁.measurable f₂.measurable (measurable_set_singleton _)
end }
@[simp] lemma extend_apply [measurable_space β] (f₁ : α →ₛ γ) {g : α → β}
(hg : measurable_embedding g) (f₂ : β →ₛ γ) (x : α) : (f₁.extend g hg f₂) (g x) = f₁ x :=
hg.injective.extend_apply _ _ _
@[simp] lemma extend_apply' [measurable_space β] (f₁ : α →ₛ γ) {g : α → β}
(hg : measurable_embedding g) (f₂ : β →ₛ γ) {y : β} (h : ¬∃ x, g x = y) :
(f₁.extend g hg f₂) y = f₂ y :=
function.extend_apply' _ _ _ h
@[simp] lemma extend_comp_eq' [measurable_space β] (f₁ : α →ₛ γ) {g : α → β}
(hg : measurable_embedding g) (f₂ : β →ₛ γ) : (f₁.extend g hg f₂) ∘ g = f₁ :=
funext $ λ x, extend_apply _ _ _ _
@[simp] lemma extend_comp_eq [measurable_space β] (f₁ : α →ₛ γ) {g : α → β}
(hg : measurable_embedding g) (f₂ : β →ₛ γ) : (f₁.extend g hg f₂).comp g hg.measurable = f₁ :=
coe_injective $ extend_comp_eq' _ _ _
/-- If `f` is a simple function taking values in `β → γ` and `g` is another simple function
with the same domain and codomain `β`, then `f.seq g = f a (g a)`. -/
def seq (f : α →ₛ (β → γ)) (g : α →ₛ β) : α →ₛ γ := f.bind (λf, g.map f)
@[simp] lemma seq_apply (f : α →ₛ (β → γ)) (g : α →ₛ β) (a : α) : f.seq g a = f a (g a) := rfl
/-- Combine two simple functions `f : α →ₛ β` and `g : α →ₛ β`
into `λ a, (f a, g a)`. -/
def pair (f : α →ₛ β) (g : α →ₛ γ) : α →ₛ (β × γ) := (f.map prod.mk).seq g
@[simp] lemma pair_apply (f : α →ₛ β) (g : α →ₛ γ) (a) : pair f g a = (f a, g a) := rfl
lemma pair_preimage (f : α →ₛ β) (g : α →ₛ γ) (s : set β) (t : set γ) :
pair f g ⁻¹' s ×ˢ t = (f ⁻¹' s) ∩ (g ⁻¹' t) := rfl
/- A special form of `pair_preimage` -/
lemma pair_preimage_singleton (f : α →ₛ β) (g : α →ₛ γ) (b : β) (c : γ) :
(pair f g) ⁻¹' {(b, c)} = (f ⁻¹' {b}) ∩ (g ⁻¹' {c}) :=
by { rw ← singleton_prod_singleton, exact pair_preimage _ _ _ _ }
theorem bind_const (f : α →ₛ β) : f.bind (const α) = f := by ext; simp
@[to_additive] instance [has_one β] : has_one (α →ₛ β) := ⟨const α 1⟩
@[to_additive] instance [has_mul β] : has_mul (α →ₛ β) := ⟨λf g, (f.map (*)).seq g⟩
@[to_additive] instance [has_div β] : has_div (α →ₛ β) := ⟨λf g, (f.map (/)).seq g⟩
@[to_additive] instance [has_inv β] : has_inv (α →ₛ β) := ⟨λf, f.map (has_inv.inv)⟩
instance [has_sup β] : has_sup (α →ₛ β) := ⟨λf g, (f.map (⊔)).seq g⟩
instance [has_inf β] : has_inf (α →ₛ β) := ⟨λf g, (f.map (⊓)).seq g⟩
instance [has_le β] : has_le (α →ₛ β) := ⟨λf g, ∀a, f a ≤ g a⟩
@[simp, to_additive] lemma const_one [has_one β] : const α (1 : β) = 1 := rfl
@[simp, norm_cast, to_additive] lemma coe_one [has_one β] : ⇑(1 : α →ₛ β) = 1 := rfl
@[simp, norm_cast, to_additive] lemma coe_mul [has_mul β] (f g : α →ₛ β) : ⇑(f * g) = f * g := rfl
@[simp, norm_cast, to_additive] lemma coe_inv [has_inv β] (f : α →ₛ β) : ⇑(f⁻¹) = f⁻¹ := rfl
@[simp, norm_cast, to_additive] lemma coe_div [has_div β] (f g : α →ₛ β) : ⇑(f / g) = f / g := rfl
@[simp, norm_cast] lemma coe_le [preorder β] {f g : α →ₛ β} : (f : α → β) ≤ g ↔ f ≤ g := iff.rfl
@[simp, norm_cast] lemma coe_sup [has_sup β] (f g : α →ₛ β) : ⇑(f ⊔ g) = f ⊔ g := rfl
@[simp, norm_cast] lemma coe_inf [has_inf β] (f g : α →ₛ β) : ⇑(f ⊓ g) = f ⊓ g := rfl
@[to_additive] lemma mul_apply [has_mul β] (f g : α →ₛ β) (a : α) : (f * g) a = f a * g a := rfl
@[to_additive] lemma div_apply [has_div β] (f g : α →ₛ β) (x : α) : (f / g) x = f x / g x := rfl
@[to_additive] lemma inv_apply [has_inv β] (f : α →ₛ β) (x : α) : f⁻¹ x = (f x)⁻¹ := rfl
lemma sup_apply [has_sup β] (f g : α →ₛ β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl
lemma inf_apply [has_inf β] (f g : α →ₛ β) (a : α) : (f ⊓ g) a = f a ⊓ g a := rfl
@[simp, to_additive] lemma range_one [nonempty α] [has_one β] : (1 : α →ₛ β).range = {1} :=
finset.ext $ λ x, by simp [eq_comm]
@[simp] lemma range_eq_empty_of_is_empty {β} [hα : is_empty α] (f : α →ₛ β) :
f.range = ∅ :=
begin
rw ← finset.not_nonempty_iff_eq_empty,
by_contra,
obtain ⟨y, hy_mem⟩ := h,
rw [simple_func.mem_range, set.mem_range] at hy_mem,
obtain ⟨x, hxy⟩ := hy_mem,
rw is_empty_iff at hα,
exact hα x,
end
lemma eq_zero_of_mem_range_zero [has_zero β] : ∀ {y : β}, y ∈ (0 : α →ₛ β).range → y = 0 :=
forall_range_iff.2 $ λ x, rfl
@[to_additive]
lemma mul_eq_map₂ [has_mul β] (f g : α →ₛ β) : f * g = (pair f g).map (λp:β×β, p.1 * p.2) := rfl
lemma sup_eq_map₂ [has_sup β] (f g : α →ₛ β) : f ⊔ g = (pair f g).map (λp:β×β, p.1 ⊔ p.2) := rfl
@[to_additive]
lemma const_mul_eq_map [has_mul β] (f : α →ₛ β) (b : β) : const α b * f = f.map (λa, b * a) := rfl
@[to_additive]
theorem map_mul [has_mul β] [has_mul γ] {g : β → γ}
(hg : ∀ x y, g (x * y) = g x * g y) (f₁ f₂ : α →ₛ β) : (f₁ * f₂).map g = f₁.map g * f₂.map g :=
ext $ λ x, hg _ _
variables {K : Type*}
instance [has_smul K β] : has_smul K (α →ₛ β) := ⟨λ k f, f.map ((•) k)⟩
@[simp] lemma coe_smul [has_smul K β] (c : K) (f : α →ₛ β) : ⇑(c • f) = c • f := rfl
lemma smul_apply [has_smul K β] (k : K) (f : α →ₛ β) (a : α) : (k • f) a = k • f a := rfl
instance has_nat_pow [monoid β] : has_pow (α →ₛ β) ℕ := ⟨λ f n, f.map (^ n)⟩
@[simp] lemma coe_pow [monoid β] (f : α →ₛ β) (n : ℕ) : ⇑(f ^ n) = f ^ n := rfl
lemma pow_apply [monoid β] (n : ℕ) (f : α →ₛ β) (a : α) : (f ^ n) a = f a ^ n := rfl
instance has_int_pow [div_inv_monoid β] : has_pow (α →ₛ β) ℤ := ⟨λ f n, f.map (^ n)⟩
@[simp] lemma coe_zpow [div_inv_monoid β] (f : α →ₛ β) (z : ℤ) : ⇑(f ^ z) = f ^ z := rfl
lemma zpow_apply [div_inv_monoid β] (z : ℤ) (f : α →ₛ β) (a : α) : (f ^ z) a = f a ^ z := rfl
-- TODO: work out how to generate these instances with `to_additive`, which gets confused by the
-- argument order swap between `coe_smul` and `coe_pow`.
section additive
instance [add_monoid β] : add_monoid (α →ₛ β) :=
function.injective.add_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add
(λ _ _, coe_smul _ _)
instance [add_comm_monoid β] : add_comm_monoid (α →ₛ β) :=
function.injective.add_comm_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add
(λ _ _, coe_smul _ _)
instance [add_group β] : add_group (α →ₛ β) :=
function.injective.add_group (λ f, show α → β, from f) coe_injective
coe_zero coe_add coe_neg coe_sub (λ _ _, coe_smul _ _) (λ _ _, coe_smul _ _)
instance [add_comm_group β] : add_comm_group (α →ₛ β) :=
function.injective.add_comm_group (λ f, show α → β, from f) coe_injective
coe_zero coe_add coe_neg coe_sub (λ _ _, coe_smul _ _) (λ _ _, coe_smul _ _)
end additive
@[to_additive] instance [monoid β] : monoid (α →ₛ β) :=
function.injective.monoid (λ f, show α → β, from f) coe_injective coe_one coe_mul coe_pow
@[to_additive] instance [comm_monoid β] : comm_monoid (α →ₛ β) :=
function.injective.comm_monoid (λ f, show α → β, from f) coe_injective coe_one coe_mul coe_pow
@[to_additive] instance [group β] : group (α →ₛ β) :=
function.injective.group (λ f, show α → β, from f) coe_injective
coe_one coe_mul coe_inv coe_div coe_pow coe_zpow
@[to_additive] instance [comm_group β] : comm_group (α →ₛ β) :=
function.injective.comm_group (λ f, show α → β, from f) coe_injective
coe_one coe_mul coe_inv coe_div coe_pow coe_zpow
instance [semiring K] [add_comm_monoid β] [module K β] : module K (α →ₛ β) :=
function.injective.module K ⟨λ f, show α → β, from f, coe_zero, coe_add⟩
coe_injective coe_smul
lemma smul_eq_map [has_smul K β] (k : K) (f : α →ₛ β) : k • f = f.map ((•) k) := rfl
instance [preorder β] : preorder (α →ₛ β) :=
{ le_refl := λf a, le_rfl,
le_trans := λf g h hfg hgh a, le_trans (hfg _) (hgh a),
.. simple_func.has_le }
instance [partial_order β] : partial_order (α →ₛ β) :=
{ le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a),
.. simple_func.preorder }
instance [has_le β] [order_bot β] : order_bot (α →ₛ β) :=
{ bot := const α ⊥, bot_le := λf a, bot_le }
instance [has_le β] [order_top β] : order_top (α →ₛ β) :=
{ top := const α ⊤, le_top := λf a, le_top }
instance [semilattice_inf β] : semilattice_inf (α →ₛ β) :=
{ inf := (⊓),
inf_le_left := assume f g a, inf_le_left,
inf_le_right := assume f g a, inf_le_right,
le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a),
.. simple_func.partial_order }
instance [semilattice_sup β] : semilattice_sup (α →ₛ β) :=
{ sup := (⊔),
le_sup_left := assume f g a, le_sup_left,
le_sup_right := assume f g a, le_sup_right,
sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a),
.. simple_func.partial_order }
instance [lattice β] : lattice (α →ₛ β) :=
{ .. simple_func.semilattice_sup,.. simple_func.semilattice_inf }
instance [has_le β] [bounded_order β] : bounded_order (α →ₛ β) :=
{ .. simple_func.order_bot, .. simple_func.order_top }
lemma finset_sup_apply [semilattice_sup β] [order_bot β] {f : γ → α →ₛ β} (s : finset γ) (a : α) :
s.sup f a = s.sup (λc, f c a) :=
begin
refine finset.induction_on s rfl _,
assume a s hs ih,
rw [finset.sup_insert, finset.sup_insert, sup_apply, ih]
end
section restrict
variables [has_zero β]
/-- Restrict a simple function `f : α →ₛ β` to a set `s`. If `s` is measurable,
then `f.restrict s a = if a ∈ s then f a else 0`, otherwise `f.restrict s = const α 0`. -/
def restrict (f : α →ₛ β) (s : set α) : α →ₛ β :=
if hs : measurable_set s then piecewise s hs f 0 else 0
theorem restrict_of_not_measurable {f : α →ₛ β} {s : set α}
(hs : ¬measurable_set s) :
restrict f s = 0 :=
dif_neg hs
@[simp] theorem coe_restrict (f : α →ₛ β) {s : set α} (hs : measurable_set s) :
⇑(restrict f s) = indicator s f :=
by { rw [restrict, dif_pos hs], refl }
@[simp] theorem restrict_univ (f : α →ₛ β) : restrict f univ = f :=
by simp [restrict]
@[simp] theorem restrict_empty (f : α →ₛ β) : restrict f ∅ = 0 :=
by simp [restrict]
theorem map_restrict_of_zero [has_zero γ] {g : β → γ} (hg : g 0 = 0) (f : α →ₛ β) (s : set α) :
(f.restrict s).map g = (f.map g).restrict s :=
ext $ λ x,
if hs : measurable_set s then by simp [hs, set.indicator_comp_of_zero hg]
else by simp [restrict_of_not_measurable hs, hg]
theorem map_coe_ennreal_restrict (f : α →ₛ ℝ≥0) (s : set α) :
(f.restrict s).map (coe : ℝ≥0 → ℝ≥0∞) = (f.map coe).restrict s :=
map_restrict_of_zero ennreal.coe_zero _ _
theorem map_coe_nnreal_restrict (f : α →ₛ ℝ≥0) (s : set α) :
(f.restrict s).map (coe : ℝ≥0 → ℝ) = (f.map coe).restrict s :=
map_restrict_of_zero nnreal.coe_zero _ _
theorem restrict_apply (f : α →ₛ β) {s : set α} (hs : measurable_set s) (a) :
restrict f s a = indicator s f a :=
by simp only [f.coe_restrict hs]
theorem restrict_preimage (f : α →ₛ β) {s : set α} (hs : measurable_set s)
{t : set β} (ht : (0:β) ∉ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t :=
by simp [hs, indicator_preimage_of_not_mem _ _ ht, inter_comm]
theorem restrict_preimage_singleton (f : α →ₛ β) {s : set α} (hs : measurable_set s)
{r : β} (hr : r ≠ 0) : restrict f s ⁻¹' {r} = s ∩ f ⁻¹' {r} :=
f.restrict_preimage hs hr.symm
lemma mem_restrict_range {r : β} {s : set α} {f : α →ₛ β} (hs : measurable_set s) :
r ∈ (restrict f s).range ↔ (r = 0 ∧ s ≠ univ) ∨ (r ∈ f '' s) :=
by rw [← finset.mem_coe, coe_range, coe_restrict _ hs, mem_range_indicator]
lemma mem_image_of_mem_range_restrict {r : β} {s : set α} {f : α →ₛ β}
(hr : r ∈ (restrict f s).range) (h0 : r ≠ 0) :
r ∈ f '' s :=
if hs : measurable_set s then by simpa [mem_restrict_range hs, h0] using hr
else by { rw [restrict_of_not_measurable hs] at hr,
exact (h0 $ eq_zero_of_mem_range_zero hr).elim }
@[mono] lemma restrict_mono [preorder β] (s : set α) {f g : α →ₛ β} (H : f ≤ g) :
f.restrict s ≤ g.restrict s :=
if hs : measurable_set s then λ x, by simp only [coe_restrict _ hs, indicator_le_indicator (H x)]
else by simp only [restrict_of_not_measurable hs, le_refl]
end restrict
section approx
section
variables [semilattice_sup β] [order_bot β] [has_zero β]
/-- Fix a sequence `i : ℕ → β`. Given a function `α → β`, its `n`-th approximation
by simple functions is defined so that in case `β = ℝ≥0∞` it sends each `a` to the supremum
of the set `{i k | k ≤ n ∧ i k ≤ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/
def approx (i : ℕ → β) (f : α → β) (n : ℕ) : α →ₛ β :=
(finset.range n).sup (λk, restrict (const α (i k)) {a:α | i k ≤ f a})
lemma approx_apply [topological_space β] [order_closed_topology β] [measurable_space β]
[opens_measurable_space β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : measurable f) :
(approx i f n : α →ₛ β) a = (finset.range n).sup (λk, if i k ≤ f a then i k else 0) :=
begin
dsimp only [approx],
rw [finset_sup_apply],
congr,
funext k,
rw [restrict_apply],
refl,
exact (hf measurable_set_Ici)
end
lemma monotone_approx (i : ℕ → β) (f : α → β) : monotone (approx i f) :=
assume n m h, finset.sup_mono $ finset.range_subset.2 h
lemma approx_comp [topological_space β] [order_closed_topology β] [measurable_space β]
[opens_measurable_space β] [measurable_space γ]
{i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α)
(hf : measurable f) (hg : measurable g) :
(approx i (f ∘ g) n : α →ₛ β) a = (approx i f n : γ →ₛ β) (g a) :=
by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)]
end
lemma supr_approx_apply [topological_space β] [complete_lattice β] [order_closed_topology β]
[has_zero β] [measurable_space β] [opens_measurable_space β]
(i : ℕ → β) (f : α → β) (a : α) (hf : measurable f) (h_zero : (0 : β) = ⊥) :
(⨆n, (approx i f n : α →ₛ β) a) = (⨆k (h : i k ≤ f a), i k) :=
begin
refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _),
{ rw [approx_apply a hf, h_zero],
refine finset.sup_le (assume k hk, _),
split_ifs,
exact le_supr_of_le k (le_supr _ h),
exact bot_le },
{ refine le_supr_of_le (k+1) _,
rw [approx_apply a hf],
have : k ∈ finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _),
refine le_trans (le_of_eq _) (finset.le_sup this),
rw [if_pos hk] }
end
end approx
section eapprox
/-- A sequence of `ℝ≥0∞`s such that its range is the set of non-negative rational numbers. -/
def ennreal_rat_embed (n : ℕ) : ℝ≥0∞ :=
ennreal.of_real ((encodable.decode ℚ n).get_or_else (0 : ℚ))
lemma ennreal_rat_embed_encode (q : ℚ) :
ennreal_rat_embed (encodable.encode q) = real.to_nnreal q :=
by rw [ennreal_rat_embed, encodable.encodek]; refl
/-- Approximate a function `α → ℝ≥0∞` by a sequence of simple functions. -/
def eapprox : (α → ℝ≥0∞) → ℕ → α →ₛ ℝ≥0∞ :=
approx ennreal_rat_embed
lemma eapprox_lt_top (f : α → ℝ≥0∞) (n : ℕ) (a : α) : eapprox f n a < ∞ :=
begin
simp only [eapprox, approx, finset_sup_apply, finset.sup_lt_iff, with_top.zero_lt_top,
finset.mem_range, ennreal.bot_eq_zero, restrict],
assume b hb,
split_ifs,
{ simp only [coe_zero, coe_piecewise, piecewise_eq_indicator, coe_const],
calc {a : α | ennreal_rat_embed b ≤ f a}.indicator (λ x, ennreal_rat_embed b) a
≤ ennreal_rat_embed b : indicator_le_self _ _ a
... < ⊤ : ennreal.coe_lt_top },
{ exact with_top.zero_lt_top },
end
@[mono] lemma monotone_eapprox (f : α → ℝ≥0∞) : monotone (eapprox f) :=
monotone_approx _ f
lemma supr_eapprox_apply (f : α → ℝ≥0∞) (hf : measurable f) (a : α) :
(⨆n, (eapprox f n : α →ₛ ℝ≥0∞) a) = f a :=
begin
rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl],
refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _),
assume h,
rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨q, hq, lt_q, q_lt⟩,
have : (real.to_nnreal q : ℝ≥0∞) ≤
(⨆ (k : ℕ) (h : ennreal_rat_embed k ≤ f a), ennreal_rat_embed k),
{ refine le_supr_of_le (encodable.encode q) _,
rw [ennreal_rat_embed_encode q],
refine le_supr_of_le (le_of_lt q_lt) _,
exact le_rfl },
exact lt_irrefl _ (lt_of_le_of_lt this lt_q)
end
lemma eapprox_comp [measurable_space γ] {f : γ → ℝ≥0∞} {g : α → γ} {n : ℕ}
(hf : measurable f) (hg : measurable g) :
(eapprox (f ∘ g) n : α → ℝ≥0∞) = (eapprox f n : γ →ₛ ℝ≥0∞) ∘ g :=
funext $ assume a, approx_comp a hf hg
/-- Approximate a function `α → ℝ≥0∞` by a series of simple functions taking their values
in `ℝ≥0`. -/
def eapprox_diff (f : α → ℝ≥0∞) : ∀ (n : ℕ), α →ₛ ℝ≥0
| 0 := (eapprox f 0).map ennreal.to_nnreal
| (n+1) := (eapprox f (n+1) - eapprox f n).map ennreal.to_nnreal
lemma sum_eapprox_diff (f : α → ℝ≥0∞) (n : ℕ) (a : α) :
(∑ k in finset.range (n+1), (eapprox_diff f k a : ℝ≥0∞)) = eapprox f n a :=
begin
induction n with n IH,
{ simp only [nat.nat_zero_eq_zero, finset.sum_singleton, finset.range_one], refl },
{ rw [finset.sum_range_succ, nat.succ_eq_add_one, IH, eapprox_diff, coe_map, function.comp_app,
coe_sub, pi.sub_apply, ennreal.coe_to_nnreal,
add_tsub_cancel_of_le (monotone_eapprox f (nat.le_succ _) _)],
apply (lt_of_le_of_lt _ (eapprox_lt_top f (n+1) a)).ne,
rw tsub_le_iff_right,
exact le_self_add },
end
lemma tsum_eapprox_diff (f : α → ℝ≥0∞) (hf : measurable f) (a : α) :
(∑' n, (eapprox_diff f n a : ℝ≥0∞)) = f a :=
by simp_rw [ennreal.tsum_eq_supr_nat' (tendsto_add_at_top_nat 1), sum_eapprox_diff,
supr_eapprox_apply f hf a]
end eapprox
end measurable
section measure
variables {m : measurable_space α} {μ ν : measure α}
/-- Integral of a simple function whose codomain is `ℝ≥0∞`. -/
def lintegral {m : measurable_space α} (f : α →ₛ ℝ≥0∞) (μ : measure α) : ℝ≥0∞ :=
∑ x in f.range, x * μ (f ⁻¹' {x})
lemma lintegral_eq_of_subset (f : α →ₛ ℝ≥0∞) {s : finset ℝ≥0∞}
(hs : ∀ x, f x ≠ 0 → μ (f ⁻¹' {f x}) ≠ 0 → f x ∈ s) :
f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) :=
begin
refine finset.sum_bij_ne_zero (λr _ _, r) _ _ _ _,
{ simpa only [forall_range_iff, mul_ne_zero_iff, and_imp] },
{ intros, assumption },
{ intros b _ hb,
refine ⟨b, _, hb, rfl⟩,
rw [mem_range, ← preimage_singleton_nonempty],
exact nonempty_of_measure_ne_zero (mul_ne_zero_iff.1 hb).2 },
{ intros, refl }
end
lemma lintegral_eq_of_subset' (f : α →ₛ ℝ≥0∞) {s : finset ℝ≥0∞}
(hs : f.range \ {0} ⊆ s) :
f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) :=
f.lintegral_eq_of_subset $ λ x hfx _, hs $
finset.mem_sdiff.2 ⟨f.mem_range_self x, mt finset.mem_singleton.1 hfx⟩
/-- Calculate the integral of `(g ∘ f)`, where `g : β → ℝ≥0∞` and `f : α →ₛ β`. -/
lemma map_lintegral (g : β → ℝ≥0∞) (f : α →ₛ β) :
(f.map g).lintegral μ = ∑ x in f.range, g x * μ (f ⁻¹' {x}) :=
begin
simp only [lintegral, range_map],
refine finset.sum_image' _ (assume b hb, _),
rcases mem_range.1 hb with ⟨a, rfl⟩,
rw [map_preimage_singleton, ← f.sum_measure_preimage_singleton, finset.mul_sum],
refine finset.sum_congr _ _,
{ congr },
{ assume x, simp only [finset.mem_filter], rintro ⟨_, h⟩, rw h },
end
lemma add_lintegral (f g : α →ₛ ℝ≥0∞) : (f + g).lintegral μ = f.lintegral μ + g.lintegral μ :=
calc (f + g).lintegral μ =
∑ x in (pair f g).range, (x.1 * μ (pair f g ⁻¹' {x}) + x.2 * μ (pair f g ⁻¹' {x})) :
by rw [add_eq_map₂, map_lintegral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _)
... = ∑ x in (pair f g).range, x.1 * μ (pair f g ⁻¹' {x}) +
∑ x in (pair f g).range, x.2 * μ (pair f g ⁻¹' {x}) : by rw [finset.sum_add_distrib]
... = ((pair f g).map prod.fst).lintegral μ + ((pair f g).map prod.snd).lintegral μ :
by rw [map_lintegral, map_lintegral]
... = lintegral f μ + lintegral g μ : rfl
lemma const_mul_lintegral (f : α →ₛ ℝ≥0∞) (x : ℝ≥0∞) :
(const α x * f).lintegral μ = x * f.lintegral μ :=
calc (f.map (λa, x * a)).lintegral μ = ∑ r in f.range, x * r * μ (f ⁻¹' {r}) :
map_lintegral _ _
... = ∑ r in f.range, x * (r * μ (f ⁻¹' {r})) :
finset.sum_congr rfl (assume a ha, mul_assoc _ _ _)
... = x * f.lintegral μ :
finset.mul_sum.symm
/-- Integral of a simple function `α →ₛ ℝ≥0∞` as a bilinear map. -/
def lintegralₗ {m : measurable_space α} : (α →ₛ ℝ≥0∞) →ₗ[ℝ≥0∞] measure α →ₗ[ℝ≥0∞] ℝ≥0∞ :=
{ to_fun := λ f,
{ to_fun := lintegral f,
map_add' := by simp [lintegral, mul_add, finset.sum_add_distrib],
map_smul' := λ c μ, by simp [lintegral, mul_left_comm _ c, finset.mul_sum] },
map_add' := λ f g, linear_map.ext (λ μ, add_lintegral f g),
map_smul' := λ c f, linear_map.ext (λ μ, const_mul_lintegral f c) }
@[simp] lemma zero_lintegral : (0 : α →ₛ ℝ≥0∞).lintegral μ = 0 :=
linear_map.ext_iff.1 lintegralₗ.map_zero μ
lemma lintegral_add {ν} (f : α →ₛ ℝ≥0∞) : f.lintegral (μ + ν) = f.lintegral μ + f.lintegral ν :=
(lintegralₗ f).map_add μ ν
lemma lintegral_smul (f : α →ₛ ℝ≥0∞) (c : ℝ≥0∞) :
f.lintegral (c • μ) = c • f.lintegral μ :=
(lintegralₗ f).map_smul c μ
@[simp] lemma lintegral_zero [measurable_space α] (f : α →ₛ ℝ≥0∞) :
f.lintegral 0 = 0 :=
(lintegralₗ f).map_zero
lemma lintegral_sum {m : measurable_space α} {ι} (f : α →ₛ ℝ≥0∞) (μ : ι → measure α) :
f.lintegral (measure.sum μ) = ∑' i, f.lintegral (μ i) :=
begin
simp only [lintegral, measure.sum_apply, f.measurable_set_preimage, ← finset.tsum_subtype,
← ennreal.tsum_mul_left],
apply ennreal.tsum_comm
end
lemma restrict_lintegral (f : α →ₛ ℝ≥0∞) {s : set α} (hs : measurable_set s) :
(restrict f s).lintegral μ = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) :=
calc (restrict f s).lintegral μ = ∑ r in f.range, r * μ (restrict f s ⁻¹' {r}) :
lintegral_eq_of_subset _ $ λ x hx, if hxs : x ∈ s
then λ _, by simp only [f.restrict_apply hs, indicator_of_mem hxs, mem_range_self]
else false.elim $ hx $ by simp [*]
... = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) :
finset.sum_congr rfl $ forall_range_iff.2 $ λ b, if hb : f b = 0 then by simp only [hb, zero_mul]
else by rw [restrict_preimage_singleton _ hs hb, inter_comm]
lemma lintegral_restrict {m : measurable_space α} (f : α →ₛ ℝ≥0∞) (s : set α) (μ : measure α) :
f.lintegral (μ.restrict s) = ∑ y in f.range, y * μ (f ⁻¹' {y} ∩ s) :=
by simp only [lintegral, measure.restrict_apply, f.measurable_set_preimage]
lemma restrict_lintegral_eq_lintegral_restrict (f : α →ₛ ℝ≥0∞) {s : set α}
(hs : measurable_set s) :
(restrict f s).lintegral μ = f.lintegral (μ.restrict s) :=
by rw [f.restrict_lintegral hs, lintegral_restrict]
lemma const_lintegral (c : ℝ≥0∞) : (const α c).lintegral μ = c * μ univ :=
begin
rw [lintegral],
casesI is_empty_or_nonempty α,
{ simp [μ.eq_zero_of_is_empty] },
{ simp [preimage_const_of_mem] },
end
lemma const_lintegral_restrict (c : ℝ≥0∞) (s : set α) :
(const α c).lintegral (μ.restrict s) = c * μ s :=
by rw [const_lintegral, measure.restrict_apply measurable_set.univ, univ_inter]
lemma restrict_const_lintegral (c : ℝ≥0∞) {s : set α} (hs : measurable_set s) :
((const α c).restrict s).lintegral μ = c * μ s :=
by rw [restrict_lintegral_eq_lintegral_restrict _ hs, const_lintegral_restrict]
lemma le_sup_lintegral (f g : α →ₛ ℝ≥0∞) : f.lintegral μ ⊔ g.lintegral μ ≤ (f ⊔ g).lintegral μ :=
calc f.lintegral μ ⊔ g.lintegral μ =
((pair f g).map prod.fst).lintegral μ ⊔ ((pair f g).map prod.snd).lintegral μ : rfl
... ≤ ∑ x in (pair f g).range, (x.1 ⊔ x.2) * μ (pair f g ⁻¹' {x}) :
begin
rw [map_lintegral, map_lintegral],
refine sup_le _ _;
refine finset.sum_le_sum (λ a _, mul_le_mul_right' _ _),
exact le_sup_left,
exact le_sup_right
end
... = (f ⊔ g).lintegral μ : by rw [sup_eq_map₂, map_lintegral]
/-- `simple_func.lintegral` is monotone both in function and in measure. -/
@[mono] lemma lintegral_mono {f g : α →ₛ ℝ≥0∞} (hfg : f ≤ g) (hμν : μ ≤ ν) :
f.lintegral μ ≤ g.lintegral ν :=
calc f.lintegral μ ≤ f.lintegral μ ⊔ g.lintegral μ : le_sup_left
... ≤ (f ⊔ g).lintegral μ : le_sup_lintegral _ _
... = g.lintegral μ : by rw [sup_of_le_right hfg]
... ≤ g.lintegral ν : finset.sum_le_sum $ λ y hy, ennreal.mul_left_mono $
hμν _ (g.measurable_set_preimage _)
/-- `simple_func.lintegral` depends only on the measures of `f ⁻¹' {y}`. -/
lemma lintegral_eq_of_measure_preimage [measurable_space β] {f : α →ₛ ℝ≥0∞} {g : β →ₛ ℝ≥0∞}
{ν : measure β} (H : ∀ y, μ (f ⁻¹' {y}) = ν (g ⁻¹' {y})) :
f.lintegral μ = g.lintegral ν :=
begin
simp only [lintegral, ← H],
apply lintegral_eq_of_subset,
simp only [H],
intros,
exact mem_range_of_measure_ne_zero ‹_›
end
/-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/
lemma lintegral_congr {f g : α →ₛ ℝ≥0∞} (h : f =ᵐ[μ] g) :
f.lintegral μ = g.lintegral μ :=
lintegral_eq_of_measure_preimage $ λ y, measure_congr $
eventually.set_eq $ h.mono $ λ x hx, by simp [hx]
lemma lintegral_map' {β} [measurable_space β] {μ' : measure β} (f : α →ₛ ℝ≥0∞) (g : β →ₛ ℝ≥0∞)
(m' : α → β) (eq : ∀ a, f a = g (m' a)) (h : ∀s, measurable_set s → μ' s = μ (m' ⁻¹' s)) :
f.lintegral μ = g.lintegral μ' :=
lintegral_eq_of_measure_preimage $ λ y,
by { simp only [preimage, eq], exact (h (g ⁻¹' {y}) (g.measurable_set_preimage _)).symm }
lemma lintegral_map {β} [measurable_space β] (g : β →ₛ ℝ≥0∞) {f : α → β} (hf : measurable f) :
g.lintegral (measure.map f μ) = (g.comp f hf).lintegral μ :=
eq.symm $ lintegral_map' _ _ f (λ a, rfl) (λ s hs, measure.map_apply hf hs)
end measure
section fin_meas_supp
open finset function
lemma support_eq [measurable_space α] [has_zero β] (f : α →ₛ β) :
support f = ⋃ y ∈ f.range.filter (λ y, y ≠ 0), f ⁻¹' {y} :=
set.ext $ λ x, by simp only [mem_support, set.mem_preimage, mem_filter, mem_range_self, true_and,
exists_prop, mem_Union, set.mem_range, mem_singleton_iff, exists_eq_right']
variables {m : measurable_space α} [has_zero β] [has_zero γ] {μ : measure α} {f : α →ₛ β}
lemma measurable_set_support [measurable_space α] (f : α →ₛ β) : measurable_set (support f) :=
by { rw f.support_eq, exact finset.measurable_set_bUnion _ (λ y hy, measurable_set_fiber _ _), }
/-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite
measure. -/
protected def fin_meas_supp {m : measurable_space α} (f : α →ₛ β) (μ : measure α) : Prop :=
f =ᶠ[μ.cofinite] 0
lemma fin_meas_supp_iff_support : f.fin_meas_supp μ ↔ μ (support f) < ∞ := iff.rfl
lemma fin_meas_supp_iff : f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ :=
begin
split,
{ refine λ h y hy, lt_of_le_of_lt (measure_mono _) h,
exact λ x hx (H : f x = 0), hy $ H ▸ eq.symm hx },
{ intro H,
rw [fin_meas_supp_iff_support, support_eq],
refine lt_of_le_of_lt (measure_bUnion_finset_le _ _) (sum_lt_top _),
exact λ y hy, (H y (finset.mem_filter.1 hy).2).ne }
end
namespace fin_meas_supp
lemma meas_preimage_singleton_ne_zero (h : f.fin_meas_supp μ) {y : β} (hy : y ≠ 0) :
μ (f ⁻¹' {y}) < ∞ :=
fin_meas_supp_iff.1 h y hy
protected lemma map {g : β → γ} (hf : f.fin_meas_supp μ) (hg : g 0 = 0) :
(f.map g).fin_meas_supp μ :=
flip lt_of_le_of_lt hf (measure_mono $ support_comp_subset hg f)
lemma of_map {g : β → γ} (h : (f.map g).fin_meas_supp μ) (hg : ∀b, g b = 0 → b = 0) :
f.fin_meas_supp μ :=
flip lt_of_le_of_lt h $ measure_mono $ support_subset_comp hg _
lemma map_iff {g : β → γ} (hg : ∀ {b}, g b = 0 ↔ b = 0) :
(f.map g).fin_meas_supp μ ↔ f.fin_meas_supp μ :=
⟨λ h, h.of_map $ λ b, hg.1, λ h, h.map $ hg.2 rfl⟩
protected lemma pair {g : α →ₛ γ} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) :
(pair f g).fin_meas_supp μ :=
calc μ (support $ pair f g) = μ (support f ∪ support g) : congr_arg μ $ support_prod_mk f g
... ≤ μ (support f) + μ (support g) : measure_union_le _ _
... < _ : add_lt_top.2 ⟨hf, hg⟩
protected lemma map₂ [has_zero δ] (hf : f.fin_meas_supp μ)
{g : α →ₛ γ} (hg : g.fin_meas_supp μ) {op : β → γ → δ} (H : op 0 0 = 0) :
((pair f g).map (function.uncurry op)).fin_meas_supp μ :=
(hf.pair hg).map H
protected lemma add {β} [add_monoid β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ)
(hg : g.fin_meas_supp μ) :
(f + g).fin_meas_supp μ :=
by { rw [add_eq_map₂], exact hf.map₂ hg (zero_add 0) }
protected lemma mul {β} [monoid_with_zero β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ)
(hg : g.fin_meas_supp μ) :
(f * g).fin_meas_supp μ :=
by { rw [mul_eq_map₂], exact hf.map₂ hg (zero_mul 0) }
lemma lintegral_lt_top {f : α →ₛ ℝ≥0∞} (hm : f.fin_meas_supp μ) (hf : ∀ᵐ a ∂μ, f a ≠ ∞) :
f.lintegral μ < ∞ :=
begin
refine sum_lt_top (λ a ha, _),
rcases eq_or_ne a ∞ with rfl|ha,
{ simp only [ae_iff, ne.def, not_not] at hf,
simp [set.preimage, hf] },
{ by_cases ha0 : a = 0,
{ subst a, rwa [zero_mul] },
{ exact mul_ne_top ha (fin_meas_supp_iff.1 hm _ ha0).ne } }
end
lemma of_lintegral_ne_top {f : α →ₛ ℝ≥0∞} (h : f.lintegral μ ≠ ∞) : f.fin_meas_supp μ :=
begin
refine fin_meas_supp_iff.2 (λ b hb, _),
rw [f.lintegral_eq_of_subset' (finset.subset_insert b _)] at h,
refine ennreal.lt_top_of_mul_ne_top_right _ hb,
exact (lt_top_of_sum_ne_top h (finset.mem_insert_self _ _)).ne
end
lemma iff_lintegral_lt_top {f : α →ₛ ℝ≥0∞} (hf : ∀ᵐ a ∂μ, f a ≠ ∞) :
f.fin_meas_supp μ ↔ f.lintegral μ < ∞ :=
⟨λ h, h.lintegral_lt_top hf, λ h, of_lintegral_ne_top h.ne⟩
end fin_meas_supp
end fin_meas_supp
/-- To prove something for an arbitrary simple function, it suffices to show
that the property holds for (multiples of) characteristic functions and is closed under
addition (of functions with disjoint support).
It is possible to make the hypotheses in `h_add` a bit stronger, and such conditions can be added
once we need them (for example it is only necessary to consider the case where `g` is a multiple
of a characteristic function, and that this multiple doesn't appear in the image of `f`) -/
@[elab_as_eliminator]
protected lemma induction {α γ} [measurable_space α] [add_monoid γ] {P : simple_func α γ → Prop}
(h_ind : ∀ c {s} (hs : measurable_set s),
P (simple_func.piecewise s hs (simple_func.const _ c) (simple_func.const _ 0)))
(h_add : ∀ ⦃f g : simple_func α γ⦄, disjoint (support f) (support g) → P f → P g → P (f + g))
(f : simple_func α γ) : P f :=
begin
generalize' h : f.range \ {0} = s,
rw [← finset.coe_inj, finset.coe_sdiff, finset.coe_singleton, simple_func.coe_range] at h,
revert s f h, refine finset.induction _ _,
{ intros f hf, rw [finset.coe_empty, diff_eq_empty, range_subset_singleton] at hf,
convert h_ind 0 measurable_set.univ, ext x, simp [hf] },
{ intros x s hxs ih f hf,
have mx := f.measurable_set_preimage {x},
let g := simple_func.piecewise (f ⁻¹' {x}) mx 0 f,
have Pg : P g,
{ apply ih, simp only [g, simple_func.coe_piecewise, range_piecewise],
rw [image_compl_preimage, union_diff_distrib, diff_diff_comm, hf, finset.coe_insert,
insert_diff_self_of_not_mem, diff_eq_empty.mpr, set.empty_union],
{ rw [set.image_subset_iff], convert set.subset_univ _,
exact preimage_const_of_mem (mem_singleton _) },
{ rwa [finset.mem_coe] }},
convert h_add _ Pg (h_ind x mx),
{ ext1 y, by_cases hy : y ∈ f ⁻¹' {x}; [simpa [hy], simp [hy]] },
rw disjoint_iff_inf_le,
rintro y, by_cases hy : y ∈ f ⁻¹' {x}; simp [hy] }
end
end simple_func
end measure_theory
open measure_theory measure_theory.simple_func
/-- To prove something for an arbitrary measurable function into `ℝ≥0∞`, it suffices to show
that the property holds for (multiples of) characteristic functions and is closed under addition
and supremum of increasing sequences of functions.
It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions
can be added once we need them (for example in `h_add` it is only necessary to consider the sum of
a simple function with a multiple of a characteristic function and that the intersection
of their images is a subset of `{0}`. -/
@[elab_as_eliminator]
theorem measurable.ennreal_induction {α} [measurable_space α] {P : (α → ℝ≥0∞) → Prop}
(h_ind : ∀ (c : ℝ≥0∞) ⦃s⦄, measurable_set s → P (indicator s (λ _, c)))
(h_add : ∀ ⦃f g : α → ℝ≥0∞⦄, disjoint (support f) (support g) → measurable f → measurable g →
P f → P g → P (f + g))
(h_supr : ∀ ⦃f : ℕ → α → ℝ≥0∞⦄ (hf : ∀n, measurable (f n)) (h_mono : monotone f)
(hP : ∀ n, P (f n)), P (λ x, ⨆ n, f n x))
⦃f : α → ℝ≥0∞⦄ (hf : measurable f) : P f :=
begin
convert h_supr (λ n, (eapprox f n).measurable) (monotone_eapprox f) _,
{ ext1 x, rw [supr_eapprox_apply f hf] },
{ exact λ n, simple_func.induction (λ c s hs, h_ind c hs)
(λ f g hfg hf hg, h_add hfg f.measurable g.measurable hf hg) (eapprox f n) }
end
|
9c1af03661d9a0b8e196e53a3a8dcb051504b243 | bdb33f8b7ea65f7705fc342a178508e2722eb851 | /data/nat/modeq.lean | c942faa21cf5577f60f1c8852939f9d049305c27 | [
"Apache-2.0"
] | permissive | rwbarton/mathlib | 939ae09bf8d6eb1331fc2f7e067d39567e10e33d | c13c5ea701bb1eec057e0a242d9f480a079105e9 | refs/heads/master | 1,584,015,335,862 | 1,524,142,167,000 | 1,524,142,167,000 | 130,614,171 | 0 | 0 | Apache-2.0 | 1,548,902,667,000 | 1,524,437,371,000 | Lean | UTF-8 | Lean | false | false | 4,130 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
Modular equality relation.
-/
import data.int.basic data.nat.gcd
namespace nat
/-- Modular equality. `modeq n a b`, or `a ≡ b [MOD n]`, means
that `a - b` is a multiple of `n`. -/
def modeq (n a b : ℕ) := a % n = b % n
notation a ` ≡ `:50 b ` [MOD `:50 n `]`:0 := modeq n a b
namespace modeq
variables {n m a b c d : ℕ}
@[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := @rfl _ _
@[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := eq.symm
@[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := eq.trans
instance : decidable (a ≡ b [MOD n]) := by unfold modeq; apply_instance
theorem modeq_zero_iff : a ≡ 0 [MOD n] ↔ n ∣ a :=
by rw [modeq, zero_mod, dvd_iff_mod_eq_zero]
theorem modeq_iff_dvd : a ≡ b [MOD n] ↔ (n:ℤ) ∣ b - a :=
by rw [modeq, eq_comm, ← int.coe_nat_inj'];
simp [int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero]
theorem modeq_of_dvd : (n:ℤ) ∣ b - a → a ≡ b [MOD n] := modeq_iff_dvd.2
theorem dvd_of_modeq : a ≡ b [MOD n] → (n:ℤ) ∣ b - a := modeq_iff_dvd.1
theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] :=
modeq_of_dvd $ dvd_trans (int.coe_nat_dvd.2 d) (dvd_of_modeq h)
theorem modeq_mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD (c * n)] :=
by unfold modeq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h]
theorem modeq_mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] :=
modeq_of_dvd_of_modeq (dvd_mul_left _ _) $ modeq_mul_left' _ h
theorem modeq_mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD (n * c)] :=
by rw [mul_comm a, mul_comm b, mul_comm n]; exact modeq_mul_left' c h
theorem modeq_mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] :=
by rw [mul_comm a, mul_comm b]; exact modeq_mul_left c h
theorem modeq_mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] :=
(modeq_mul_left _ h₂).trans (modeq_mul_right _ h₁)
theorem modeq_add (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a + c ≡ b + d [MOD n] :=
modeq_of_dvd $ by simpa using dvd_add (dvd_of_modeq h₁) (dvd_of_modeq h₂)
theorem modeq_add_cancel_left (h₁ : a ≡ b [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : c ≡ d [MOD n] :=
have (n:ℤ) ∣ a + (-a + (d + -c)),
by simpa using _root_.dvd_sub (dvd_of_modeq h₂) (dvd_of_modeq h₁),
modeq_of_dvd $ by rwa add_neg_cancel_left at this
theorem modeq_add_cancel_right (h₁ : c ≡ d [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : a ≡ b [MOD n] :=
by rw [add_comm a, add_comm b] at h₂; exact modeq_add_cancel_left h₁ h₂
theorem chinese_remainder (co : coprime n m) (a b : ℕ) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} :=
⟨let (c, d) := xgcd n m in int.to_nat ((b * c * n + a * d * m) % (n * m)), begin
rw xgcd_val, dsimp,
rw [modeq_iff_dvd, modeq_iff_dvd],
rw [int.to_nat_of_nonneg], swap,
{ by_cases h₁ : n = 0, {simp [coprime, h₁] at co, substs m n, simp},
by_cases h₂ : m = 0, {simp [coprime, h₂] at co, substs m n, simp},
exact int.mod_nonneg _
(mul_ne_zero (int.coe_nat_ne_zero.2 h₁) (int.coe_nat_ne_zero.2 h₂)) },
have := gcd_eq_gcd_ab n m, simp [co.gcd_eq_one, mul_comm] at this,
rw [int.mod_def, ← sub_add, ← sub_add]; split,
{ refine dvd_add _ (dvd_trans (dvd_mul_right _ _) (dvd_mul_right _ _)),
rw [add_comm, ← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _),
have := congr_arg ((*) ↑a) this,
exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm,
← sub_eq_iff_eq_add] at this⟩ },
{ refine dvd_add _ (dvd_trans (dvd_mul_left _ _) (dvd_mul_right _ _)),
rw [← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _),
have := congr_arg ((*) ↑b) this,
exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm _ ↑m,
← sub_eq_iff_eq_add'] at this⟩ }
end⟩
end modeq
end nat
|
bae7d068c1728836a6d9fd80669303f9e2968c9f | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/algebra/category/Algebra/limits.lean | e2540fbf085e309c61590db4d82d534fd5cf0d5c | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 5,248 | 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 algebra.category.Algebra.basic
import algebra.category.Module.limits
import algebra.category.CommRing.limits
/-!
# The category of R-algebras has all limits
Further, these limits are preserved by the forgetful functor --- that is,
the underlying types are just the limits in the category of types.
-/
open category_theory
open category_theory.limits
universes v u
namespace Algebra
variables {R : Type u} [comm_ring R]
variables {J : Type v} [small_category J]
instance semiring_obj (F : J ⥤ Algebra R) (j) :
semiring ((F ⋙ forget (Algebra R)).obj j) :=
by { change semiring (F.obj j), apply_instance }
instance algebra_obj (F : J ⥤ Algebra R) (j) :
algebra R ((F ⋙ forget (Algebra R)).obj j) :=
by { change algebra R (F.obj j), apply_instance }
/--
The flat sections of a functor into `Algebra R` form a submodule of all sections.
-/
def sections_subalgebra (F : J ⥤ Algebra R) :
subalgebra R (Π j, F.obj j) :=
{ carrier := SemiRing.sections_subsemiring (F ⋙ forget₂ (Algebra R) Ring ⋙ forget₂ Ring SemiRing),
algebra_map_mem' := λ r j j' f, (F.map f).commutes r, }
instance limit_semiring (F : J ⥤ Algebra R) :
ring (types.limit_cone (F ⋙ forget (Algebra.{v} R))).X :=
begin
change ring (sections_subalgebra F),
apply_instance,
end
instance limit_algebra (F : J ⥤ Algebra R) :
algebra R (types.limit_cone (F ⋙ forget (Algebra.{v} R))).X :=
begin
change algebra R (sections_subalgebra F),
apply_instance,
end
/-- `limit.π (F ⋙ forget (Algebra R)) j` as a `alg_hom`. -/
def limit_π_alg_hom (F : J ⥤ Algebra.{v} R) (j) :
(types.limit_cone (F ⋙ forget (Algebra R))).X →ₐ[R] (F ⋙ forget (Algebra.{v} R)).obj j :=
{ commutes' := λ r, rfl,
..SemiRing.limit_π_ring_hom (F ⋙ forget₂ (Algebra R) Ring.{v} ⋙ forget₂ Ring SemiRing.{v}) j }
namespace has_limits
-- The next two definitions are used in the construction of `has_limits (Algebra R)`.
-- After that, the limits should be constructed using the generic limits API,
-- e.g. `limit F`, `limit.cone F`, and `limit.is_limit F`.
/--
Construction of a limit cone in `Algebra R`.
(Internal use only; use the limits API.)
-/
def limit_cone (F : J ⥤ Algebra R) : cone F :=
{ X := Algebra.of R (types.limit_cone (F ⋙ forget _)).X,
π :=
{ app := limit_π_alg_hom F,
naturality' := λ j j' f,
alg_hom.coe_fn_inj ((types.limit_cone (F ⋙ forget _)).π.naturality f) } }
/--
Witness that the limit cone in `Algebra R` is a limit cone.
(Internal use only; use the limits API.)
-/
def limit_cone_is_limit (F : J ⥤ Algebra R) : is_limit (limit_cone F) :=
begin
refine is_limit.of_faithful
(forget (Algebra R)) (types.limit_cone_is_limit _)
(λ s, { .. }) (λ s, rfl),
{ simp only [forget_map_eq_coe, alg_hom.map_one, functor.map_cone_π], refl, },
{ intros x y, simp only [forget_map_eq_coe, alg_hom.map_mul, functor.map_cone_π], refl, },
{ simp only [forget_map_eq_coe, alg_hom.map_zero, functor.map_cone_π], refl, },
{ intros x y, simp only [forget_map_eq_coe, alg_hom.map_add, functor.map_cone_π], refl, },
{ intros r, ext j, dsimp, erw (s.π.app j).commutes r, refl, },
end
end has_limits
open has_limits
/-- The category of R-algebras has all limits. -/
@[irreducible]
instance has_limits : has_limits (Algebra R) :=
{ has_limits_of_shape := λ J 𝒥, by exactI
{ has_limit := λ F,
{ cone := limit_cone F,
is_limit := limit_cone_is_limit F } } }
/--
An auxiliary declaration to speed up typechecking.
-/
def forget₂_Ring_preserves_limits_aux (F : J ⥤ Algebra R) :
is_limit ((forget₂ (Algebra R) Ring).map_cone (limit_cone F)) :=
Ring.limit_cone_is_limit (F ⋙ forget₂ (Algebra R) Ring)
/--
The forgetful functor from R-algebras to rings preserves all limits.
-/
instance forget₂_Ring_preserves_limits : preserves_limits (forget₂ (Algebra R) Ring.{v}) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (forget₂_Ring_preserves_limits_aux F) } }
/--
An auxiliary declaration to speed up typechecking.
-/
def forget₂_Module_preserves_limits_aux (F : J ⥤ Algebra R) :
is_limit ((forget₂ (Algebra R) (Module R)).map_cone (limit_cone F)) :=
Module.has_limits.limit_cone_is_limit (F ⋙ forget₂ (Algebra R) (Module R))
/--
The forgetful functor from R-algebras to R-modules preserves all limits.
-/
instance forget₂_Module_preserves_limits : preserves_limits (forget₂ (Algebra R) (Module.{v} R)) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (forget₂_Module_preserves_limits_aux F) } }
/--
The forgetful functor from R-algebras to types preserves all limits.
-/
instance forget_preserves_limits : preserves_limits (forget (Algebra R)) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget _)) } }
end Algebra
|
daa4b1c827fc841b68434cfa9b31d4848648fa31 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/list/default.lean | 9523af124b170bca64a267eaa1d9dfdf9459e674 | [
"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 | 718 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
-/
import data.list.alist
import data.list.bag_inter
import data.list.basic
import data.list.chain
import data.list.defs
import data.list.erase_dup
import data.list.forall2
import data.list.func
import data.list.intervals
import data.list.min_max
import data.list.indexes
import data.list.nat_antidiagonal
import data.list.nodup
import data.list.of_fn
import data.list.pairwise
import data.list.perm
import data.list.range
import data.list.rotate
import data.list.sections
import data.list.sigma
import data.list.sort
import data.list.tfae
import data.list.zip
|
6cb68544889ef20d6f7f34282718d5aaf6300a4b | 3ef5255cebe505e5ab251615d9fbf31a132f461d | /lean/old/my_opaque_pairs.lean | b5b210f1fc01bc5e057ae7069b685f00af1487c1 | [] | no_license | avigad/scratch | 42441a2ea94918049391e44d7adab304d3adea51 | 3fb9cef15bc5581c9602561427a7f295917990a2 | refs/heads/master | 1,608,917,412,424 | 1,473,078,921,000 | 1,473,078,921,000 | 17,224,172 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,630 | lean | import macros tactic
universe U ≥ 2 -- needed to state Sigma_inj below
-- the "right" equality axioms
axiom subst' {A : (Type U)} {a b : A} {P : A → (Type 1)} (H1 : P a) (H2 : a = b) : P b
-- not needed here
-- axiom subst_id {A : (Type U)} {a : A} {P : A → (Type 1)} (H1 : P a) (e : a = a) : subst' H1 e = H1
-- definition cast' {A B : Type} (e : A = B) (a : A) : B
-- := subst' a e
-- the current definition
-- definition cast' {A B : Type} (e : A = B) : A → B
-- := cast (to_heq e)
-- this will easily be provable with the right definition of ==
axiom subst'_heq {A : (Type U)} {a b : A} {P : A → (Type 1)} (H1 : P a) (H2 : a = b) :
subst' H1 H2 == H1
--
-- Sigma types
--
variable Sigma {A : (Type 1)} (B : A → (Type 1)) : (Type 1)
variable Pair {A : (Type 1)} {B : A → (Type 1)} (a : A) (b : B a) : Sigma B
axiom sigma_rec {A : (Type 1)} {B : A → (Type 1)} {P : Sigma B → (Type 1)} :
(∀ a : A, ∀ b : B a, P (@Pair A B a b)) → ∀ p : Sigma B, P p
axiom sigma_comp {A : (Type 1)} {B : A → (Type 1)} {P : Sigma B → (Type 1)}
(f : (∀ a : A, ∀ b : B a, P (@Pair A B a b))) (a : A) (b : B a) :
@sigma_rec A B P f (Pair a b) = f a b
definition Fst {A : (Type 1)} {B : A → (Type 1)} : ∀ p : Sigma B, A :=
@sigma_rec A B (λ p, A) (λ a : A, λ b : B a, a)
theorem FstAx {A : (Type 1)} {B : A → (Type 1)} (a : A) (b : B a) : @Fst A B (Pair a b) = a
:= sigma_comp _ _ _
definition Snd {A : (Type 1)} {B : A → (Type 1)} : ∀ p : Sigma B, B (Fst p)
:=
let P := λ p, B (Fst p) in
let f := λ a : A, λ b : B a, subst' b (symm (@FstAx A B a b)) in
@sigma_rec A B P f
theorem SndAx_subst {A : (Type 1)} {B : A → (Type 1)} (a : A) (b : B a) :
@Snd A B (Pair a b) = subst' b (symm (@FstAx A B a b))
:= sigma_comp _ _ _
theorem SndAx {A : (Type 1)} {B : A → (Type 1)} (a : A) (b : B a) : @Snd A B (Pair a b) == b
:= htrans (to_heq (@SndAx_subst A B a b)) (subst'_heq _ _)
set_opaque Fst true
set_opaque Snd true
axiom Sigma_inj {A : (Type 1)} {B : A → (Type 1)} {B' : A → (Type 1)} : Sigma B = Sigma B' → B = B'
--
-- Properties of Sigma types
--
theorem Fst_heq {A : (Type 1)} {B : A → (Type 1)} {B' : A → (Type 1)} {p : Sigma B} {p' : Sigma B'} :
p == p' → Fst p == Fst p'
:=
assume e : p == p',
have H : B = B', from Sigma_inj (to_eq (type_eq e)),
let P := λ T : A → (Type 1), ∀ q : Sigma T, p == q → Fst p == Fst q in
have H1 : P B, from
take q : Sigma B, assume e : p == q, (subst (hrefl (Fst p)) (to_eq e)),
substp P H1 H p' e
theorem Fst_heq' {A : (Type 1)} {A' : (Type 1)} (e : A = A') {B : A → (Type 1)}
{B' : A' → (Type 1)} {p : Sigma B} {p' : Sigma B'} :
p == p' → Fst p == Fst p'
:=
take he,
let H1 := substp (λ X, ∀ B : A → (Type 1), ∀ B' : X → (Type 1),
∀ p : Sigma B, ∀ p' : Sigma B', p == p' → Fst p == Fst p') (@Fst_heq A) e in
H1 B B' p p' he
theorem Snd_heq {A : (Type 1)} {B : A → (Type 1)} {B' : A → (Type 1)} {p : Sigma B} {p' : Sigma B'} :
p == p' → Snd p == Snd p'
:=
assume e : p == p',
have H : B = B', from Sigma_inj (to_eq (type_eq e)),
let P := λ T : A → (Type 1), ∀ q : Sigma T, p == q → Snd p == Snd q in
have H1 : P B, from
take q : Sigma B, assume e : p == q, (subst (hrefl (Snd p)) (to_eq e)),
substp P H1 H p' e
theorem Snd_heq' {A : (Type 1)} {A' : (Type 1)} (e : A = A') {B : A → (Type 1)}
{B' : A' → (Type 1)} {p : Sigma B} {p' : Sigma B'} :
p == p' → Snd p == Snd p'
:=
take he,
let H1 := substp (λ X, ∀ B : A → (Type 1), ∀ B' : X → (Type 1),
∀ p : Sigma B, ∀ p' : Sigma B', p == p' → Snd p == Snd p') (@Snd_heq A) e in
H1 B B' p p' he
-- interesting: after the proof was done, I found I could remove most of the implicit arguments
theorem pair_eta {A : (Type 1)} (B : A → (Type 1)) (p : Sigma B) : p = Pair (Fst p) (Snd p)
:=
let P := λ p, p = Pair (Fst p) (Snd p) in
have H : ∀ a : A, ∀ b : B a, P (Pair a b), from
take a : A,
take b : B a,
have H1 : Pair a == Pair (Fst (Pair a b)),
from hcongr (hrefl _) (to_heq (symm (@FstAx A B a b))),
have H2 : Pair a b == Pair (Fst (Pair a b)) (@Snd A B (Pair a b)),
from hcongr H1 (hsymm (SndAx a b)),
show Pair a b = Pair (Fst (Pair a b)) (Snd (Pair a b)), from to_eq H2,
sigma_rec H p
check @pair_eta
-- Conjectures
-- At least they all type check...
--
-- theorem eq_pair {A : (Type 1)} {B : A → (Type 1)} (p : Sigma B) (a : A) (b : B a)
-- (e1 : Fst p = a) (e2 : Snd p == b) : p = Pair a b
-- :=
-- _
-- theorem pair_inj2 {A : (Type 1)} {B : A → (Type 1)} (a : A) (b : B a) (b' : B a)
-- (H : Pair a b = Pair a b') : b == b'
-- :=
-- _
-- theorem eq_pair_iff {A : (Type 1)} {B : A → (Type 1)} {A' : (Type 1)} {B' : A' → (Type 1)} (p : Sigma B)
-- (p' : Sigma B') : (p == p') ↔ (Fst p == Fst p' ∧ Snd p == Snd p')
-- :=
-- _
--
-- Triples
--
definition Sigma2 {A1 : (Type 1)} {A2 : A1 → (Type 1)} (A3 : ∀ a1 : A1, A2 a1 → (Type 1)) : (Type 1)
:= @Sigma A1 (λ a1 : A1, @Sigma (A2 a1) (A3 a1))
definition Triple {A1 : (Type 1)} {A2 : A1 → (Type 1)} (A3 : ∀ a1 : A1, A2 a1 → (Type 1))
(a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2) : @Sigma2 A1 A2 A3
:= @Pair A1 (λ x, @Sigma (A2 x) (A3 x)) a1 (@Pair (A2 a1) (A3 a1) a2 a3)
definition pr31 {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(p : @Sigma2 A1 A2 A3) : A1
:= Fst p
definition pr32 {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(p : @Sigma2 A1 A2 A3) : A2 (pr31 p)
:= Fst (Snd p)
definition pr33 {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(p : @Sigma2 A1 A2 A3) : A3 (pr31 p) (pr32 p)
:= Snd (Snd p)
theorem pr31_Triple_eq {A1 : (Type 1)} {A2 : A1 → (Type 1)} (A3 : ∀ a1 : A1, A2 a1 → (Type 1))
(a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2) :
pr31 (@Triple A1 A2 A3 a1 a2 a3) = a1
:= FstAx _ _
theorem pr32_Triple_eq {A1 : (Type 1)} {A2 : A1 → (Type 1)} (A3 : ∀ a1 : A1, A2 a1 → (Type 1))
(a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2) :
pr32 (@Triple A1 A2 A3 a1 a2 a3) == a2
:=
have H : Snd (Triple A3 a1 a2 a3) == Pair a2 a3, from SndAx _ _,
have H1 : A2 (Fst (Triple A3 a1 a2 a3)) = A2 a1, from congr2 _ (pr31_Triple_eq _ _ _ _),
have H2 : pr32 (Triple A3 a1 a2 a3) == Fst (Pair a2 a3), from Fst_heq' H1 H,
have H3 : Fst (Pair a2 a3) == a2, from to_heq (FstAx _ _),
htrans H2 H3
theorem pr33_Triple_eq {A1 : (Type 1)} {A2 : A1 → (Type 1)} (A3 : ∀ a1 : A1, A2 a1 → (Type 1))
(a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2) :
pr33 (@Triple A1 A2 A3 a1 a2 a3) == a3
:=
have H : Snd (Triple A3 a1 a2 a3) == Pair a2 a3, from SndAx _ _,
have H1 : A2 (Fst (Triple A3 a1 a2 a3)) = A2 a1, from congr2 _ (pr31_Triple_eq _ _ _ _),
have H2 : pr33 (Triple A3 a1 a2 a3) == Snd (Pair a2 a3), from Snd_heq' H1 H,
have H3 : Snd (Pair a2 a3) == a3, from SndAx _ _,
htrans H2 H3
theorem pr31_heq {A1 A1' : (Type 1)} {A2 : A1 → (Type 1)} (A3 : ∀ a1 : A1, A2 a1 → (Type 1))
{A2' : A1' → (Type 1)} (A3' : ∀ a1 : A1', A2' a1 → (Type 1))
{p : @Sigma2 A1 A2 A3} {p' : @Sigma2 A1' A2' A3'} (e : A1 = A1')
: p == p' → pr31 p == pr31 p'
:= Fst_heq' e
theorem pr32_heq {A1 A1' : (Type 1)} {A2 : A1 → (Type 1)} (A3 : ∀ a1 : A1, A2 a1 → (Type 1))
{A2' : A1' → (Type 1)} (A3' : ∀ a1 : A1', A2' a1 → (Type 1))
{p : @Sigma2 A1 A2 A3} {p' : @Sigma2 A1' A2' A3'} (e1 : A1 = A1') (e2 : A2 == A2')
: p == p' → pr32 p == pr32 p'
:=
assume e : p == p',
have H1 : Fst p == Fst p', from Fst_heq' e1 e,
have H2 : Snd p == Snd p', from Snd_heq' e1 e,
have H3 : A2 (Fst p) == A2' (Fst p'), from hcongr e2 H1,
let e3 := to_eq H3 in
show pr32 p == pr32 p', from Fst_heq' e3 H2
theorem pr33_heq {A1 A1' : (Type 1)} {A2 : A1 → (Type 1)} (A3 : ∀ a1 : A1, A2 a1 → (Type 1))
{A2' : A1' → (Type 1)} (A3' : ∀ a1 : A1', A2' a1 → (Type 1))
{p : @Sigma2 A1 A2 A3} {p' : @Sigma2 A1' A2' A3'} (e1 : A1 = A1') (e2 : A2 == A2')
: p == p' → pr33 p == pr33 p'
:=
assume e : p == p',
have H1 : Fst p == Fst p', from Fst_heq' e1 e,
have H2 : Snd p == Snd p', from Snd_heq' e1 e,
have H3 : A2 (Fst p) == A2' (Fst p'), from hcongr e2 H1,
let e3 := to_eq H3 in
show pr33 p == pr33 p', from Snd_heq' e3 H2
set_opaque Sigma2 true
set_opaque Triple true
set_opaque pr31 true
set_opaque pr32 true
set_opaque pr33 true
--
-- Quadruples
--
definition Sigma3 {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1)) : (Type 1)
:= @Sigma A1 (λ a1 : A1, @Sigma2 (A2 a1) (A3 a1) (A4 a1))
definition Quadruple {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1)) (a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2)
(a4 : A4 a1 a2 a3)
:= @Pair A1 (λ x : A1, @Sigma2 (A2 x) (A3 x) (A4 x)) a1 (@Triple (A2 a1) (A3 a1) (A4 a1) a2 a3 a4)
definition pr41 {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
{A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1)}
(p : @Sigma3 A1 A2 A3 A4) := Fst p
definition pr42 {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
{A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1)}
(p : @Sigma3 A1 A2 A3 A4)
:= @pr31 (A2 (Fst p)) (A3 (Fst p)) (A4 (Fst p)) (Snd p)
definition pr43 {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
{A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1)}
(p : @Sigma3 A1 A2 A3 A4)
:= @pr32 (A2 (Fst p)) (A3 (Fst p)) (A4 (Fst p)) (Snd p)
definition pr44 {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
{A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1)}
(p : @Sigma3 A1 A2 A3 A4)
:= @pr33 (A2 (Fst p)) (A3 (Fst p)) (A4 (Fst p)) (Snd p)
theorem pr41_Quadruple_eq {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1))
(a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2) (a4 : A4 a1 a2 a3) :
pr41 (@Quadruple A1 A2 A3 A4 a1 a2 a3 a4) = a1
:= FstAx _ _
theorem pr42_Quadruple_eq {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1))
(a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2) (a4 : A4 a1 a2 a3) :
pr42 (@Quadruple A1 A2 A3 A4 a1 a2 a3 a4) == a2
:=
let q1234 := @Quadruple A1 A2 A3 A4 a1 a2 a3 a4 in
let t234 := @Triple (A2 a1) (A3 a1) (A4 a1) a2 a3 a4 in
have H1 : Fst q1234 = a1, from FstAx _ _,
have H2 : Snd q1234 == t234, from SndAx _ _,
have H3 : A2 (Fst q1234) = A2 a1, from congr2 A2 H1,
have H4 : pr42 q1234 == pr31 t234,
from pr31_heq (A4 (Fst q1234)) (A4 a1) H3 H2,
have H5 : pr31 t234 == a2, from to_heq (pr31_Triple_eq _ _ _ _),
htrans H4 H5
theorem pr43_Quadruple_eq {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1))
(a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2) (a4 : A4 a1 a2 a3) :
pr43 (@Quadruple A1 A2 A3 A4 a1 a2 a3 a4) == a3
:=
let q1234 := @Quadruple A1 A2 A3 A4 a1 a2 a3 a4 in
let t234 := @Triple (A2 a1) (A3 a1) (A4 a1) a2 a3 a4 in
have H1 : Fst q1234 = a1, from FstAx _ _,
have H2 : Snd q1234 == t234, from SndAx _ _,
have H3 : A2 (Fst q1234) = A2 a1, from congr2 A2 H1,
have H3' : A3 (Fst q1234) == A3 a1, from hcongr (hrefl A3) (to_heq H1),
have H4 : pr43 q1234 == pr32 t234,
from pr32_heq (A4 (Fst q1234)) (A4 a1) H3 H3' H2,
have H5 : pr32 t234 == a3, from pr32_Triple_eq _ _ _ _,
htrans H4 H5
theorem pr44_Quadruple_eq {A1 : (Type 1)} {A2 : A1 → (Type 1)} {A3 : ∀ a1 : A1, A2 a1 → (Type 1)}
(A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1))
(a1 : A1) (a2 : A2 a1) (a3 : A3 a1 a2) (a4 : A4 a1 a2 a3) :
pr44 (@Quadruple A1 A2 A3 A4 a1 a2 a3 a4) == a4
:=
let q1234 := @Quadruple A1 A2 A3 A4 a1 a2 a3 a4 in
let t234 := @Triple (A2 a1) (A3 a1) (A4 a1) a2 a3 a4 in
have H1 : Fst q1234 = a1, from FstAx _ _,
have H2 : Snd q1234 == t234, from SndAx _ _,
have H3 : A2 (Fst q1234) = A2 a1, from congr2 A2 H1,
have H3' : A3 (Fst q1234) == A3 a1, from hcongr (hrefl A3) (to_heq H1),
have H4 : pr44 q1234 == pr33 t234,
from pr33_heq (A4 (Fst q1234)) (A4 a1) H3 H3' H2,
have H5 : @pr33 (A2 a1) (A3 a1) (A4 a1) t234 == a4,
from @pr33_Triple_eq (A2 a1) (A3 a1) (A4 a1) a2 a3 a4,
htrans H4 H5
set_opaque Sigma3 true
set_opaque Quadruple true
set_opaque pr41 true
set_opaque pr42 true
set_opaque pr43 true
set_opaque pr44 true
--
-- Experiment with Pair a1 (Pair a2 (Pair a3 a4))
--
scope
-- Remark: A1 a1 A2 a2 A3 a3 A3 a4 are parameters for the definitions and theorems in this scope.
variable A1 : (Type 1)
variable a1 : A1
variable A2 : A1 → (Type 1)
variable a2 : A2 a1
variable A3 : ∀ a1 : A1, A2 a1 → (Type 1)
variable a3 : A3 a1 a2
variable A4 : ∀ (a1 : A1) (a2 : A2 a1), A3 a1 a2 → (Type 1)
variable a4 : A4 a1 a2 a3
-- Pair type parameterized by a1 and a2
definition A1A2_tuple_ty (a1 : A1) (a2 : A2 a1) : (Type 1)
:= @Sigma (A3 a1 a2) (A4 a1 a2)
-- Pair a3 a4
definition tuple_34 : A1A2_tuple_ty a1 a2
:= @Pair (A3 a1 a2) (A4 a1 a2) a3 a4
-- Triple type parameterized by a1 a2 a3
definition A1_tuple_ty (a1 : A1) : (Type 1)
:= @Sigma (A2 a1) (A1A2_tuple_ty a1)
-- Triple a2 a3 a4
definition tuple_234 : A1_tuple_ty a1
:= @Pair (A2 a1) (A1A2_tuple_ty a1) a2 tuple_34
-- Quadruple type
definition tuple_ty : (Type 1)
:= @Sigma A1 A1_tuple_ty
-- Quadruple a1 a2 a3 a4
definition tuple_1234 : tuple_ty
:= @Pair A1 A1_tuple_ty a1 tuple_234
-- First element of the quadruple
definition f_1234 : A1
:= @Fst A1 A1_tuple_ty tuple_1234
-- Rest of the quadruple (i.e., a triple)
definition s_1234 : A1_tuple_ty f_1234
:= @Snd A1 A1_tuple_ty tuple_1234
theorem H_eq_a1 : f_1234 = a1
:= @FstAx A1 A1_tuple_ty a1 tuple_234
theorem H_eq_triple : s_1234 == tuple_234
:= @SndAx A1 A1_tuple_ty a1 tuple_234
-- Second element of the quadruple
definition fs_1234 : A2 f_1234
:= @Fst (A2 f_1234) (A1A2_tuple_ty f_1234) s_1234
-- Rest of the triple (i.e., a pair)
definition ss_1234 : A1A2_tuple_ty f_1234 fs_1234
:= @Snd (A2 f_1234) (A1A2_tuple_ty f_1234) s_1234
theorem H_eq_a2 : fs_1234 == a2
:= have H1 : @Fst (A2 f_1234) (A1A2_tuple_ty f_1234) s_1234 == @Fst (A2 a1) (A1A2_tuple_ty a1) tuple_234,
from hcongr (hcongr (hrefl (λ x, @Fst (A2 x) (A1A2_tuple_ty x))) (to_heq H_eq_a1)) H_eq_triple,
have H2 : @Fst (A2 a1) (A1A2_tuple_ty a1) tuple_234 = a2,
from FstAx _ _,
htrans H1 (to_heq H2)
theorem H_eq_pair : ss_1234 == tuple_34
:= have H1 : @Snd (A2 f_1234) (A1A2_tuple_ty f_1234) s_1234 == @Snd (A2 a1) (A1A2_tuple_ty a1) tuple_234,
from hcongr (hcongr (hrefl (λ x, @Snd (A2 x) (A1A2_tuple_ty x))) (to_heq H_eq_a1)) H_eq_triple,
have H2 : @Snd (A2 a1) (A1A2_tuple_ty a1) tuple_234 == tuple_34,
from SndAx _ _,
htrans H1 H2
-- Third element of the quadruple
definition fss_1234 : A3 f_1234 fs_1234
:= @Fst (A3 f_1234 fs_1234) (A4 f_1234 fs_1234) ss_1234
-- Fourth element of the quadruple
definition sss_1234 : A4 f_1234 fs_1234 fss_1234
:= @Snd (A3 f_1234 fs_1234) (A4 f_1234 fs_1234) ss_1234
theorem H_eq_a3 : fss_1234 == a3
:= have H1 : @Fst (A3 f_1234 fs_1234) (A4 f_1234 fs_1234) ss_1234 == @Fst (A3 a1 a2) (A4 a1 a2) tuple_34,
from hcongr (hcongr (hcongr (hrefl (λ x y z, @Fst (A3 x y) (A4 x y) z)) (to_heq H_eq_a1)) H_eq_a2) H_eq_pair,
have H2 : @Fst (A3 a1 a2) (A4 a1 a2) tuple_34 = a3,
from FstAx _ _,
htrans H1 (to_heq H2)
theorem H_eq_a4 : sss_1234 == a4
:= have H1 : @Snd (A3 f_1234 fs_1234) (A4 f_1234 fs_1234) ss_1234 == @Snd (A3 a1 a2) (A4 a1 a2) tuple_34,
from hcongr (hcongr (hcongr (hrefl (λ x y z, @Snd (A3 x y) (A4 x y) z)) (to_heq H_eq_a1)) H_eq_a2) H_eq_pair,
have H2 : @Snd (A3 a1 a2) (A4 a1 a2) tuple_34 == a4,
from SndAx _ _,
htrans H1 H2
eval tuple_1234
eval f_1234
eval fs_1234
eval fss_1234
eval sss_1234
check H_eq_a1
check H_eq_a2
check H_eq_a3
check H_eq_a4
theorem f_quadruple : Fst (Pair a1 (Pair a2 (Pair a3 a4))) = a1
:= H_eq_a1
theorem fs_quadruple : Fst (Snd (Pair a1 (Pair a2 (Pair a3 a4)))) == a2
:= H_eq_a2
theorem fss_quadruple : Fst (Snd (Snd (Pair a1 (Pair a2 (Pair a3 a4))))) == a3
:= H_eq_a3
theorem sss_quadruple : Snd (Snd (Snd (Pair a1 (Pair a2 (Pair a3 a4))))) == a4
:= H_eq_a4
end
-- Show the theorems with their parameters
check f_quadruple
check fs_quadruple
check fss_quadruple
check sss_quadruple
exit
|
f77a4a4ffd40072b7ff4d1a60ef1ba37092c8951 | 3cb7d30bb276fa1e8fe9ca7b979f53cbfafb2dbb | /leanpkg/leanpkg/manifest.lean | a222ae04428b0c88ab57808ce5888eb5ad487fc2 | [
"Apache-2.0"
] | permissive | andrejtokarcik/lean | e4148f69d020385c4fc07662b77792681b26c4d9 | ceacfa7445953cbc8860ddabc55407430a9ca5c3 | refs/heads/master | 1,584,800,445,031 | 1,529,598,225,000 | 1,529,598,469,000 | 138,652,902 | 0 | 0 | Apache-2.0 | 1,529,962,767,000 | 1,529,962,767,000 | null | UTF-8 | Lean | false | false | 3,646 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Gabriel Ebner
-/
import leanpkg.toml leanpkg.lean_version system.io
namespace leanpkg
inductive source
| path (dir_name : string) : source
| git (url rev : string) : source
namespace source
def from_toml (v : toml.value) : option source :=
(do toml.value.str dir_name ← v.lookup "path" | none,
return $ path dir_name) <|>
(do toml.value.str url ← v.lookup "git" | none,
toml.value.str rev ← v.lookup "rev" | none,
return $ git url rev)
def to_toml : ∀ (s : source), toml.value
| (path dir_name) := toml.value.table [("path", toml.value.str dir_name)]
| (git url rev) :=
toml.value.table [("git", toml.value.str url), ("rev", toml.value.str rev)]
/- TODO(Leo): has_to_string -/
instance : has_repr source :=
⟨λ s, repr s.to_toml⟩
end source
structure dependency :=
(name : string) (src : source)
namespace dependency
/- TODO(Leo): has_to_string -/
instance : has_repr dependency :=
⟨λ d, d.name ++ " = " ++ repr d.src⟩
end dependency
structure manifest :=
(name : string) (version : string)
(lean_version : string := lean_version_string)
(timeout : option nat := none)
(path : option string := none)
(dependencies : list dependency := [])
namespace manifest
def effective_path (m : manifest) : list string :=
[match m.path with some p := p | none := "." end]
def from_toml (t : toml.value) : option manifest := do
pkg ← t.lookup "package",
toml.value.str n ← pkg.lookup "name" | none,
toml.value.str ver ← pkg.lookup "version" | none,
lean_ver ←
match pkg.lookup "lean_version" with
| some (toml.value.str lean_ver) := some lean_ver
| none := some lean_version_string
| _ := none
end,
tm ← match pkg.lookup "timeout" with
| some (toml.value.nat timeout) := some (some timeout)
| none := some none
| _ := none
end,
path ← match pkg.lookup "path" with
| some (toml.value.str path) := some (some path)
| none := some none
| _ := none
end,
toml.value.table deps ← t.lookup "dependencies" <|> some (toml.value.table []) | none,
deps ← deps.mmap (λ ⟨n, src⟩, do src ← source.from_toml src,
return $ dependency.mk n src),
return { name := n, version := ver, lean_version := lean_ver,
path := path, dependencies := deps, timeout := tm }
def to_toml (d : manifest) : toml.value :=
let pkg := [("name", toml.value.str d.name), ("version", toml.value.str d.version),
("lean_version", toml.value.str d.lean_version)],
pkg := match d.path with some p := pkg ++ [("path", toml.value.str p)] | none := pkg end,
pkg := match d.timeout with some t := pkg ++ [("timeout", toml.value.nat t)] | none := pkg end,
deps := toml.value.table $ d.dependencies.map $ λ dep, (dep.name, dep.src.to_toml) in
toml.value.table [("package", toml.value.table pkg), ("dependencies", deps)]
instance : has_repr manifest :=
⟨λ d, repr d.to_toml⟩
def from_string (s : string) : option manifest :=
match parser.run_string toml.File s with
| sum.inr toml := from_toml toml
| sum.inl _ := none
end
def from_file (fn : string) : io manifest := do
cnts ← io.fs.read_file fn,
toml ←
(match parser.run toml.File cnts with
| sum.inl err :=
io.fail $ "toml parse error in " ++ fn ++ "\n\n" ++ err
| sum.inr res := return res
end),
some manifest ← return (from_toml toml)
| io.fail ("cannot read manifest from " ++ fn ++ "\n\n" ++ repr toml),
return manifest
end manifest
def leanpkg_toml_fn := "leanpkg.toml"
end leanpkg
|
bf4dd8f5e9b8a5146e9cbd942fc6da07ba7ff9f2 | a4673261e60b025e2c8c825dfa4ab9108246c32e | /stage0/src/Lean/Elab/App.lean | 825cf8f3b343dca802d2393cc53fda2a32a63f3d | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 41,860 | 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.FindMVar
import Lean.Elab.Term
import Lean.Elab.Binders
import Lean.Elab.SyntheticMVars
namespace Lean.Elab.Term
open Meta
/--
Auxiliary inductive datatype for combining unelaborated syntax
and already elaborated expressions. It is used to elaborate applications. -/
inductive Arg :=
| stx (val : Syntax)
| expr (val : Expr)
instance : Inhabited Arg := ⟨Arg.stx arbitrary⟩
instance : ToString Arg := ⟨fun
| Arg.stx val => toString val
| Arg.expr val => toString val⟩
/-- Named arguments created using the notation `(x := val)` -/
structure NamedArg :=
(ref : Syntax := Syntax.missing)
(name : Name)
(val : Arg)
instance : ToString NamedArg :=
⟨fun s => "(" ++ toString s.name ++ " := " ++ toString s.val ++ ")"⟩
instance : Inhabited NamedArg := ⟨{ name := arbitrary, val := arbitrary }⟩
def throwInvalidNamedArg {α} (namedArg : NamedArg) (fn? : Option Name) : TermElabM α :=
withRef namedArg.ref $ match fn? with
| some fn => throwError! "invalid argument name '{namedArg.name}' for function '{fn}'"
| none => throwError! "invalid argument name '{namedArg.name}' for function"
/--
Add a new named argument to `namedArgs`, and throw an error if it already contains a named argument
with the same name. -/
def addNamedArg (namedArgs : Array NamedArg) (namedArg : NamedArg) : TermElabM (Array NamedArg) := do
if namedArgs.any (namedArg.name == ·.name) then
throwError! "argument '{namedArg.name}' was already set"
pure $ namedArgs.push namedArg
private def ensureArgType (f : Expr) (arg : Expr) (expectedType : Expr) : TermElabM Expr := do
let argType ← inferType arg
ensureHasTypeAux expectedType argType arg f
/-
Relevant definitions:
```
class CoeFun (α : Sort u) (γ : α → outParam (Sort v))
abbrev coeFun {α : Sort u} {γ : α → Sort v} (a : α) [CoeFun α γ] : γ a
``` -/
private def tryCoeFun? (α : Expr) (a : Expr) : TermElabM (Option Expr) := do
let v ← mkFreshLevelMVar
let type ← mkArrow α (mkSort v)
let γ ← mkFreshExprMVar type
let u ← getLevel α
let coeFunInstType := mkAppN (Lean.mkConst `CoeFun [u, v]) #[α, γ]
let mvar ← mkFreshExprMVar coeFunInstType MetavarKind.synthetic
let mvarId := mvar.mvarId!
try
if (← synthesizeInstMVarCore mvarId) then
pure $ some $ mkAppN (Lean.mkConst `coeFun [u, v]) #[α, γ, a, mvar]
else
pure none
catch _ =>
pure none
def synthesizeAppInstMVars (instMVars : Array MVarId) : TermElabM Unit :=
for mvarId in instMVars do
unless (← synthesizeInstMVarCore mvarId) do
registerSyntheticMVarWithCurrRef mvarId SyntheticMVarKind.typeClass
namespace ElabAppArgs
/- Auxiliary structure for elaborating the application `f args namedArgs`. -/
structure State :=
(explicit : Bool) -- true if `@` modifier was used
(f : Expr)
(fType : Expr)
(args : List Arg) -- remaining regular arguments
(namedArgs : List NamedArg) -- remaining named arguments to be processed
(ellipsis : Bool := false)
(expectedType? : Option Expr)
(etaArgs : Array Expr := #[])
(toSetErrorCtx : Array MVarId := #[]) -- metavariables that we need the set the error context using the application being built
(instMVars : Array MVarId := #[]) -- metavariables for the instance implicit arguments that have already been processed
-- The following two fields are used to implement the `propagateExpectedType` heuristic.
(typeMVars : Array MVarId := #[]) -- metavariables for implicit arguments of the form `{α : Sort u}` that have already been processed
(alreadyPropagated : Bool := false) -- true when expectedType has already been propagated
abbrev M := StateRefT State TermElabM
/- Add the given metavariable to the collection of metavariables associated with instance-implicit arguments. -/
private def addInstMVar (mvarId : MVarId) : M Unit :=
modify fun s => { s with instMVars := s.instMVars.push mvarId }
/-
Try to synthesize metavariables are `instMVars` using type class resolution.
The ones that cannot be synthesized yet are registered.
Remark: we use this method before trying to apply coercions to function. -/
def synthesizeAppInstMVars : M Unit := do
let s ← get
let instMVars := s.instMVars
modify fun s => { s with instMVars := #[] }
Lean.Elab.Term.synthesizeAppInstMVars instMVars
/- fType may become a forallE after we synthesize pending metavariables. -/
private def synthesizePendingAndNormalizeFunType : M Unit := do
synthesizeAppInstMVars
synthesizeSyntheticMVars
let s ← get
let fType ← whnfForall s.fType
match fType with
| Expr.forallE _ _ _ _ => modify fun s => { s with fType := fType }
| _ =>
match (← tryCoeFun? fType s.f) with
| some f =>
let fType ← inferType f
modify fun s => { s with f := f, fType := fType }
| none =>
for namedArg in s.namedArgs do
match s.f.getAppFn with
| Expr.const fn .. => throwInvalidNamedArg namedArg fn
| _ => throwInvalidNamedArg namedArg none
throwError! "function expected at{indentExpr s.f}\nterm has type{indentExpr fType}"
/- Normalize and return the function type. -/
private def normalizeFunType : M Expr := do
let s ← get
let fType ← whnfForall s.fType
modify fun s => { s with fType := fType }
pure fType
/- Return the binder name at `fType`. This method assumes `fType` is a function type. -/
private def getBindingName : M Name := return (← get).fType.bindingName!
/- Return the next argument expected type. This method assumes `fType` is a function type. -/
private def getArgExpectedType : M Expr := return (← get).fType.bindingDomain!
def eraseNamedArgCore (namedArgs : List NamedArg) (binderName : Name) : List NamedArg :=
namedArgs.filter (·.name != binderName)
/- Remove named argument with name `binderName` from `namedArgs`. -/
def eraseNamedArg (binderName : Name) : M Unit :=
modify fun s => { s with namedArgs := eraseNamedArgCore s.namedArgs binderName }
/- Return true if the next argument at `args` is of the form `_` -/
private def isNextArgHole : M Bool := do
match (← get).args with
| Arg.stx (Syntax.node `Lean.Parser.Term.hole _) :: _ => pure true
| _ => pure false
/-
Add a new argument to the result. That is, `f := f arg`, update `fType`.
This method assumes `fType` is a function type. -/
private def addNewArg (arg : Expr) : M Unit :=
modify fun s => { s with f := mkApp s.f arg, fType := s.fType.bindingBody!.instantiate1 arg }
/-
Elaborate the given `Arg` and add it to the result. See `addNewArg`.
Recall that, `Arg` may be wrapping an already elaborated `Expr`. -/
private def elabAndAddNewArg (arg : Arg) : M Unit := do
let s ← get
let expectedType ← getArgExpectedType
match arg with
| Arg.expr val =>
let arg ← ensureArgType s.f val expectedType
addNewArg arg
| Arg.stx val =>
let val ← elabTerm val expectedType
let arg ← ensureArgType s.f val expectedType
addNewArg arg
/- Return true if the given type contains `OptParam` or `AutoParams` -/
private def hasOptAutoParams (type : Expr) : M Bool := do
forallTelescopeReducing type fun xs type =>
xs.anyM fun x => do
let xType ← inferType x
pure $ xType.getOptParamDefault?.isSome || xType.getAutoParamTactic?.isSome
/- Return true if `fType` contains `OptParam` or `AutoParams` -/
private def fTypeHasOptAutoParams : M Bool := do
hasOptAutoParams (← get).fType
/- Auxiliary function for retrieving the resulting type of a function application.
See `propagateExpectedType`. -/
private partial def getForallBody : Nat → List NamedArg → Expr → Option Expr
| i, namedArgs, type@(Expr.forallE n d b c) =>
match namedArgs.find? fun (namedArg : NamedArg) => namedArg.name == n with
| some _ => getForallBody i (eraseNamedArgCore namedArgs n) b
| none =>
if !c.binderInfo.isExplicit then
getForallBody i namedArgs b
else if i > 0 then
getForallBody (i-1) namedArgs b
else if d.isAutoParam || d.isOptParam then
getForallBody i namedArgs b
else
some type
| 0, [], type => some type
| _, _, _ => none
/-
Auxiliary method for propagating the expected type. We call it as soon as we find the first explict
argument. The goal is to propagate the expected type in applications of functions such as
```lean
Add.add {α : Type u} : α → α → α
List.cons {α : Type u} : α → List α → List α
```
This is particularly useful when there applicable coercions. For example,
assume we have a coercion from `Nat` to `Int`, and we have
`(x : Nat)` and the expected type is `List Int`. Then, if we don't use this function,
the elaborator will fail to elaborate
```
List.cons x []
```
First, the elaborator creates a new metavariable `?α` for the implicit argument `{α : Type u}`.
Then, when it processes `x`, it assigns `?α := Nat`, and then obtain the
resultant type `List Nat` which is **not** definitionally equal to `List Int`.
We solve the problem by executing this method before we elaborate the first explicit argument (`x` in this example).
This method infers that the resultant type is `List ?α` and unifies it with `List Int`.
Then, when we elaborate `x`, the elaborate realizes the coercion from `Nat` to `Int` must be used, and the
term
```
@List.cons Int (coe x) (@List.nil Int)
```
is produced.
The method will do nothing if
1- The resultant type depends on the remaining arguments (i.e., `!eTypeBody.hasLooseBVars`)
2- The resultant type does not contain any type metavariable.
3- The resultant type contains a nontype metavariable.
We added conditions 2&3 to be able to restrict this method to simple functions that are "morally" in
the Hindley&Milner fragment.
For example, consider the following definitions
```
def foo {n m : Nat} (a : bv n) (b : bv m) : bv (n - m)
```
Now, consider
```
def test (x1 : bv 32) (x2 : bv 31) (y1 : bv 64) (y2 : bv 63) : bv 1 :=
foo x1 x2 = foo y1 y2
```
When the elaborator reaches the term `foo y1 y2`, the expected type is `bv (32-31)`.
If we apply this method, we would solve the unification problem `bv (?n - ?m) =?= bv (32 - 31)`,
by assigning `?n := 32` and `?m := 31`. Then, the elaborator fails elaborating `y1` since
`bv 64` is **not** definitionally equal to `bv 32`.
-/
private def propagateExpectedType : M Unit := do
let s ← get
-- TODO: handle s.etaArgs.size > 0
unless s.explicit || !s.etaArgs.isEmpty || s.alreadyPropagated || s.typeMVars.isEmpty do
match s.expectedType? with
| none => pure ()
| some expectedType =>
/- We don't propagate `Prop` because we often use `Prop` as a more general "Bool" (e.g., `if-then-else`).
If we propagate `expectedType == Prop` in the following examples, the elaborator would fail
```
def f1 (s : Nat × Bool) : Bool := if s.2 then false else true
def f2 (s : List Bool) : Bool := if s.head! then false else true
def f3 (s : List Bool) : Bool := if List.head! (s.map not) then false else true
```
They would all fail for the same reason. So, let's focus on the first one.
We would elaborate `s.2` with `expectedType == Prop`.
Before we elaborate `s`, this method would be invoked, and `s.fType` is `?α × ?β → ?β` and after
propagation we would have `?α × Prop → Prop`. Then, when we would try to elaborate `s`, and
get a type error because `?α × Prop` cannot be unified with `Nat × Bool`
Most users would have a hard time trying to understand why these examples failed.
Here is a possible alternative workarounds. We give up the idea of using `Prop` at `if-then-else`.
Drawback: users use `if-then-else` with conditions that are not Decidable.
So, users would have to embrace `propDecidable` and `choice`.
This may not be that bad since the developers and users don't seem to care about constructivism.
We currently use a different workaround, we just don't propagate the expected type when it is `Prop`. -/
if expectedType.isProp then
modify fun s => { s with alreadyPropagated := true }
else
let numRemainingArgs := s.args.length
trace[Elab.app.propagateExpectedType]! "etaArgs.size: {s.etaArgs.size}, numRemainingArgs: {numRemainingArgs}, fType: {s.fType}"
match getForallBody numRemainingArgs s.namedArgs s.fType with
| none => pure ()
| some fTypeBody =>
unless fTypeBody.hasLooseBVars do
let hasTypeMVar := (fTypeBody.findMVar? fun mvarId => s.typeMVars.contains mvarId).isSome
let hasOnlyTypeMVar := (fTypeBody.findMVar? fun mvarId => !s.typeMVars.contains mvarId).isNone
if hasTypeMVar && hasOnlyTypeMVar then
unless (← hasOptAutoParams fTypeBody) do
trace[Elab.app.propagateExpectedType]! "{expectedType} =?= {fTypeBody}"
if (← isDefEq expectedType fTypeBody) then
/- Note that we only set `alreadyPropagated := true` when propagation has succeeded. -/
modify fun s => { s with alreadyPropagated := true }
/-
Create a fresh local variable with the current binder name and argument type, add it to `etaArgs` and `f`,
and then execute the continuation `k`.-/
private def addEtaArg (k : M Expr) : M Expr := do
let n ← getBindingName
let type ← getArgExpectedType
withLocalDeclD n type fun x => do
modify fun s => { s with etaArgs := s.etaArgs.push x }
addNewArg x
k
/- This method execute after all application arguments have been processed. -/
private def finalize : M Expr := do
let s ← get
let mut e := s.f
let mut eType := s.fType
-- all user explicit arguments have been consumed
trace[Elab.app.finalize]! e
let ref ← getRef
-- Register the error context of implicits
for mvarId in s.toSetErrorCtx do
registerMVarErrorImplicitArgInfo mvarId ref e
if !s.etaArgs.isEmpty then
e ← mkLambdaFVars s.etaArgs e
eType ← inferType e
trace[Elab.app.finalize]! "after etaArgs, {e} : {eType}"
match s.expectedType? with
| none => pure ()
| some expectedType =>
trace[Elab.app.finalize]! "expected type: {expectedType}"
-- Try to propagate expected type. Ignore if types are not definitionally equal, caller must handle it.
isDefEq expectedType eType
pure ()
synthesizeAppInstMVars
pure e
private def addImplicitArg (k : M Expr) : M Expr := do
let argType ← getArgExpectedType
let arg ← mkFreshExprMVar argType
if (← isTypeFormer arg) then
modify fun s => { s with typeMVars := s.typeMVars.push arg.mvarId!, toSetErrorCtx := s.toSetErrorCtx.push arg.mvarId! }
addNewArg arg
k
/-
Process a `fType` of the form `(x : A) → B x`.
This method assume `fType` is a function type -/
private def processExplictArg (k : M Expr) : M Expr := do
let s ← get
match s.args with
| arg::args =>
propagateExpectedType
modify fun s => { s with args := args }
elabAndAddNewArg arg
k
| _ =>
let argType ← getArgExpectedType
match s.explicit, argType.getOptParamDefault?, argType.getAutoParamTactic? with
| false, some defVal, _ => addNewArg defVal; k
| false, _, some (Expr.const tacticDecl _ _) =>
let env ← getEnv
let opts ← getOptions
match evalSyntaxConstant env opts tacticDecl with
| Except.error err => throwError err
| Except.ok tacticSyntax =>
let tacticBlock ← `(by { $(tacticSyntax.getArgs)* })
-- tacticBlock does not have any position information.
-- So, we use the current ref
let ref ← getRef
let tacticBlock := tacticBlock.copyInfo ref
let argType := argType.getArg! 0 -- `autoParam type := by tactic` ==> `type`
propagateExpectedType
elabAndAddNewArg (Arg.stx tacticBlock)
k
| false, _, some _ =>
throwError "invalid autoParam, argument must be a constant"
| _, _, _ =>
if !s.namedArgs.isEmpty then
addEtaArg k
else if !s.explicit then
if (← fTypeHasOptAutoParams) then
addEtaArg k
else if (← get).ellipsis then
addImplicitArg k
else
finalize
else
finalize
/-
Process a `fType` of the form `{x : A} → B x`.
This method assume `fType` is a function type -/
private def processImplicitArg (k : M Expr) : M Expr := do
if (← get).explicit then
processExplictArg k
else
addImplicitArg k
/-
Process a `fType` of the form `[x : A] → B x`.
This method assume `fType` is a function type -/
private def processInstImplicitArg (k : M Expr) : M Expr := do
if (← get).explicit then
let isHole ← isNextArgHole
if isHole then
/- Recall that if '@' has been used, and the argument is '_', then we still use type class resolution -/
let arg ← mkFreshExprMVar (← getArgExpectedType) MetavarKind.synthetic
modify fun s => { s with args := s.args.tail! }
addInstMVar arg.mvarId!
addNewArg arg
k
else
processExplictArg k
else
let arg ← mkFreshExprMVar (← getArgExpectedType) MetavarKind.synthetic
addInstMVar arg.mvarId!
addNewArg arg
k
/- Return true if there are regular or named arguments to be processed. -/
private def hasArgsToProcess : M Bool := do
let s ← get
pure $ !s.args.isEmpty || !s.namedArgs.isEmpty
/- Elaborate function application arguments. -/
partial def main : M Expr := do
let s ← get
let fType ← normalizeFunType
match fType with
| Expr.forallE binderName _ _ c =>
let s ← get
match s.namedArgs.find? fun (namedArg : NamedArg) => namedArg.name == binderName with
| some namedArg =>
propagateExpectedType
eraseNamedArg binderName
elabAndAddNewArg namedArg.val
main
| none =>
match c.binderInfo with
| BinderInfo.implicit => processImplicitArg main
| BinderInfo.instImplicit => processInstImplicitArg main
| _ => processExplictArg main
| _ =>
if (← hasArgsToProcess) then
synthesizePendingAndNormalizeFunType
main
else
finalize
end ElabAppArgs
private def elabAppArgs (f : Expr) (namedArgs : Array NamedArg) (args : Array Arg)
(expectedType? : Option Expr) (explicit ellipsis : Bool) : TermElabM Expr := do
let fType ← inferType f
let fType ← instantiateMVars fType
trace[Elab.app.args]! "explicit: {explicit}, {f} : {fType}"
unless namedArgs.isEmpty && args.isEmpty do
tryPostponeIfMVar fType
ElabAppArgs.main.run' {
args := args.toList,
expectedType? := expectedType?,
explicit := explicit,
ellipsis := ellipsis,
namedArgs := namedArgs.toList,
f := f,
fType := fType
}
/-- Auxiliary inductive datatype that represents the resolution of an `LVal`. -/
inductive LValResolution :=
| projFn (baseStructName : Name) (structName : Name) (fieldName : Name)
| projIdx (structName : Name) (idx : Nat)
| const (baseStructName : Name) (structName : Name) (constName : Name)
| localRec (baseName : Name) (fullName : Name) (fvar : Expr)
| getOp (fullName : Name) (idx : Syntax)
private def throwLValError {α} (e : Expr) (eType : Expr) (msg : MessageData) : TermElabM α :=
throwError! "{msg}{indentExpr e}\nhas type{indentExpr eType}"
/-- `findMethod? env S fName`.
1- If `env` contains `S ++ fName`, return `(S, S++fName)`
2- Otherwise if `env` contains private name `prv` for `S ++ fName`, return `(S, prv)`, o
3- Otherwise for each parent structure `S'` of `S`, we try `findMethod? env S' fname` -/
private partial def findMethod? (env : Environment) (structName fieldName : Name) : Option (Name × Name) :=
let fullName := structName ++ fieldName
match env.find? fullName with
| some _ => some (structName, fullName)
| none =>
let fullNamePrv := mkPrivateName env fullName
match env.find? fullNamePrv with
| some _ => some (structName, fullNamePrv)
| none =>
if isStructureLike env structName then
(getParentStructures env structName).findSome? fun parentStructName => findMethod? env parentStructName fieldName
else
none
private def resolveLValAux (e : Expr) (eType : Expr) (lval : LVal) : TermElabM LValResolution := do
match eType.getAppFn, lval with
| Expr.const structName _ _, LVal.fieldIdx idx =>
if idx == 0 then
throwError "invalid projection, index must be greater than 0"
let env ← getEnv
unless isStructureLike env structName do
throwLValError e eType "invalid projection, structure expected"
let fieldNames := getStructureFields env structName
if h : idx - 1 < fieldNames.size then
if isStructure env structName then
pure $ LValResolution.projFn structName structName (fieldNames.get ⟨idx - 1, h⟩)
else
/- `structName` was declared using `inductive` command.
So, we don't projection functions for it. Thus, we use `Expr.proj` -/
pure $ LValResolution.projIdx structName (idx - 1)
else
throwLValError e eType m!"invalid projection, structure has only {fieldNames.size} field(s)"
| Expr.const structName _ _, LVal.fieldName fieldName =>
let env ← getEnv
let searchEnv : Unit → TermElabM LValResolution := fun _ => do
match findMethod? env structName (Name.mkSimple fieldName) with
| some (baseStructName, fullName) => pure $ LValResolution.const baseStructName structName fullName
| none =>
throwLValError e eType
m!"invalid field notation, '{fieldName}' is not a valid \"field\" because environment does not contain '{Name.mkStr structName fieldName}'"
-- search local context first, then environment
let searchCtx : Unit → TermElabM LValResolution := fun _ => do
let fullName := Name.mkStr structName fieldName
let currNamespace ← getCurrNamespace
let localName := fullName.replacePrefix currNamespace Name.anonymous
let lctx ← getLCtx
match lctx.findFromUserName? localName with
| some localDecl =>
if localDecl.binderInfo == BinderInfo.auxDecl then
/- LVal notation is being used to make a "local" recursive call. -/
pure $ LValResolution.localRec structName fullName localDecl.toExpr
else
searchEnv ()
| none => searchEnv ()
if isStructure env structName then
match findField? env structName (Name.mkSimple fieldName) with
| some baseStructName => pure $ LValResolution.projFn baseStructName structName (Name.mkSimple fieldName)
| none => searchCtx ()
else
searchCtx ()
| Expr.const structName _ _, LVal.getOp idx =>
let env ← getEnv
let fullName := Name.mkStr structName "getOp"
match env.find? fullName with
| some _ => pure $ LValResolution.getOp fullName idx
| none => throwLValError e eType m!"invalid [..] notation because environment does not contain '{fullName}'"
| _, LVal.getOp idx =>
throwLValError e eType "invalid [..] notation, type is not of the form (C ...) where C is a constant"
| _, _ =>
throwLValError e eType "invalid field notation, type is not of the form (C ...) where C is a constant"
/- whnfCore + implicit consumption.
Example: given `e` with `eType := {α : Type} → (fun β => List β) α `, it produces `(e ?m, List ?m)` where `?m` is fresh metavariable. -/
private partial def consumeImplicits (e eType : Expr) : TermElabM (Expr × Expr) := do
let eType ← whnfCore eType
match eType with
| Expr.forallE n d b c =>
if !c.binderInfo.isExplicit then
let mvar ← mkFreshExprMVar d
consumeImplicits (mkApp e mvar) (b.instantiate1 mvar)
else match d.getOptParamDefault? with
| some defVal => consumeImplicits (mkApp e defVal) (b.instantiate1 defVal)
-- TODO: we do not handle autoParams here.
| _ => pure (e, eType)
| _ => pure (e, eType)
private partial def resolveLValLoop (lval : LVal) (e eType : Expr) (previousExceptions : Array Exception) : TermElabM (Expr × LValResolution) := do
let (e, eType) ← consumeImplicits e eType
tryPostponeIfMVar eType
try
let lvalRes ← resolveLValAux e eType lval
pure (e, lvalRes)
catch
| ex@(Exception.error _ _) =>
let eType? ← unfoldDefinition? eType
match eType? with
| some eType => resolveLValLoop lval e eType (previousExceptions.push ex)
| none =>
previousExceptions.forM fun ex => logException ex
throw ex
| ex@(Exception.internal _ _) => throw ex
private def resolveLVal (e : Expr) (lval : LVal) : TermElabM (Expr × LValResolution) := do
let eType ← inferType e
resolveLValLoop lval e eType #[]
private partial def mkBaseProjections (baseStructName : Name) (structName : Name) (e : Expr) : TermElabM Expr := do
let env ← getEnv
match getPathToBaseStructure? env baseStructName structName with
| none => throwError "failed to access field in parent structure"
| some path =>
let mut e := e
for projFunName in path do
let projFn ← mkConst projFunName
e ← elabAppArgs projFn #[{ name := `self, val := Arg.expr e }] (args := #[]) (expectedType? := none) (explicit := false) (ellipsis := false)
return e
/- Auxiliary method for field notation. It tries to add `e` as a new argument to `args` or `namedArgs`.
This method first finds the parameter with a type of the form `(baseName ...)`.
When the parameter is found, if it an explicit one and `args` is big enough, we add `e` to `args`.
Otherwise, if there isn't another parameter with the same name, we add `e` to `namedArgs`.
Remark: `fullName` is the name of the resolved "field" access function. It is used for reporting errors -/
private def addLValArg (baseName : Name) (fullName : Name) (e : Expr) (args : Array Arg) (namedArgs : Array NamedArg) (fType : Expr)
: TermElabM (Array Arg × Array NamedArg) :=
forallTelescopeReducing fType fun xs _ => do
let mut argIdx := 0 -- position of the next explicit argument
let mut remainingNamedArgs := namedArgs
for i in [:xs.size] do
let x := xs[i]
let xDecl ← getLocalDecl x.fvarId!
/- If there is named argument with name `xDecl.userName`, then we skip it. -/
match remainingNamedArgs.findIdx? (fun namedArg => namedArg.name == xDecl.userName) with
| some idx =>
remainingNamedArgs := remainingNamedArgs.eraseIdx idx
| none =>
let mut foundIt := false
let type := xDecl.type
if type.consumeMData.isAppOf baseName then
foundIt := true
if !foundIt then
/- Normalize type and try again -/
let type ← withReducible $ whnf type
if type.consumeMData.isAppOf baseName then
foundIt := true
if foundIt then
/- We found a type of the form (baseName ...).
First, we check if the current argument is an explicit one,
and the current explicit position "fits" at `args` (i.e., it must be ≤ arg.size) -/
if argIdx ≤ args.size && xDecl.binderInfo.isExplicit then
/- We insert `e` as an explicit argument -/
return (args.insertAt argIdx (Arg.expr e), namedArgs)
/- If we can't add `e` to `args`, we try to add it using a named argument, but this is only possible
if there isn't an argument with the same name occurring before it. -/
for j in [:i] do
let prev := xs[j]
let prevDecl ← getLocalDecl prev.fvarId!
if prevDecl.userName == xDecl.userName then
throwError! "invalid field notation, function '{fullName}' has argument with the expected type{indentExpr type}\nbut it cannot be used"
return (args, namedArgs.push { name := xDecl.userName, val := Arg.expr e })
if xDecl.binderInfo.isExplicit then
-- advance explicit argument position
argIdx := argIdx + 1
throwError! "invalid field notation, function '{fullName}' does not have argument with type ({baseName} ...) that can be used, it must be explicit or implicit with an unique name"
private def elabAppLValsAux (namedArgs : Array NamedArg) (args : Array Arg) (expectedType? : Option Expr) (explicit ellipsis : Bool)
(f : Expr) (lvals : List LVal) : TermElabM Expr :=
let rec loop : Expr → List LVal → TermElabM Expr
| f, [] => elabAppArgs f namedArgs args expectedType? explicit ellipsis
| f, lval::lvals => do
let (f, lvalRes) ← resolveLVal f lval
match lvalRes with
| LValResolution.projIdx structName idx =>
let f := mkProj structName idx f
loop f lvals
| LValResolution.projFn baseStructName structName fieldName =>
let f ← mkBaseProjections baseStructName structName f
let projFn ← mkConst (baseStructName ++ fieldName)
if lvals.isEmpty then
let namedArgs ← addNamedArg namedArgs { name := `self, val := Arg.expr f }
elabAppArgs projFn namedArgs args expectedType? explicit ellipsis
else
let f ← elabAppArgs projFn #[{ name := `self, val := Arg.expr f }] #[] (expectedType? := none) (explicit := false) (ellipsis := false)
loop f lvals
| LValResolution.const baseStructName structName constName =>
let f ← if baseStructName != structName then mkBaseProjections baseStructName structName f else pure f
let projFn ← mkConst constName
if lvals.isEmpty then
let projFnType ← inferType projFn
let (args, namedArgs) ← addLValArg baseStructName constName f args namedArgs projFnType
elabAppArgs projFn namedArgs args expectedType? explicit ellipsis
else
let f ← elabAppArgs projFn #[] #[Arg.expr f] (expectedType? := none) (explicit := false) (ellipsis := false)
loop f lvals
| LValResolution.localRec baseName fullName fvar =>
if lvals.isEmpty then
let fvarType ← inferType fvar
let (args, namedArgs) ← addLValArg baseName fullName f args namedArgs fvarType
elabAppArgs fvar namedArgs args expectedType? explicit ellipsis
else
let f ← elabAppArgs fvar #[] #[Arg.expr f] (expectedType? := none) (explicit := false) (ellipsis := false)
loop f lvals
| LValResolution.getOp fullName idx =>
let getOpFn ← mkConst fullName
if lvals.isEmpty then
let namedArgs ← addNamedArg namedArgs { name := `self, val := Arg.expr f }
let namedArgs ← addNamedArg namedArgs { name := `idx, val := Arg.stx idx }
elabAppArgs getOpFn namedArgs args expectedType? explicit ellipsis
else
let f ← elabAppArgs getOpFn #[{ name := `self, val := Arg.expr f }, { name := `idx, val := Arg.stx idx }]
#[] (expectedType? := none) (explicit := false) (ellipsis := false)
loop f lvals
loop f lvals
private def elabAppLVals (f : Expr) (lvals : List LVal) (namedArgs : Array NamedArg) (args : Array Arg)
(expectedType? : Option Expr) (explicit ellipsis : Bool) : TermElabM Expr := do
if !lvals.isEmpty && explicit then
throwError "invalid use of field notation with `@` modifier"
elabAppLValsAux namedArgs args expectedType? explicit ellipsis f lvals
def elabExplicitUnivs (lvls : Array Syntax) : TermElabM (List Level) := do
lvls.foldrM (fun stx lvls => do pure ((← elabLevel stx)::lvls)) []
/-
Interaction between `errToSorry` and `observing`.
- The method `elabTerm` catches exceptions, log them, and returns a synthetic sorry (IF `ctx.errToSorry` == true).
- When we elaborate choice nodes (and overloaded identifiers), we track multiple results using the `observing x` combinator.
The `observing x` executes `x` and returns a `TermElabResult`.
`observing `x does not check for synthetic sorry's, just an exception. Thus, it may think `x` worked when it didn't
if a synthetic sorry was introduced. We decided that checking for synthetic sorrys at `observing` is not a good solution
because it would not be clear to decide what the "main" error message for the alternative is. When the result contains
a synthetic `sorry`, it is not clear which error message corresponds to the `sorry`. Moreover, while executing `x`, many
error messages may have been logged. Recall that we need an error per alternative at `mergeFailures`.
Thus, we decided to set `errToSorry` to `false` whenever processing choice nodes and overloaded symbols.
Important: we rely on the property that after `errToSorry` is set to
false, no elaboration function executed by `x` will reset it to
`true`.
-/
private partial def elabAppFnId (fIdent : Syntax) (fExplicitUnivs : List Level) (lvals : List LVal)
(namedArgs : Array NamedArg) (args : Array Arg) (expectedType? : Option Expr) (explicit ellipsis overloaded : Bool) (acc : Array TermElabResult)
: TermElabM (Array TermElabResult) := do
match fIdent with
| Syntax.ident _ _ n preresolved =>
let funLVals ← withRef fIdent $ resolveName n preresolved fExplicitUnivs
let overloaded := overloaded || funLVals.length > 1
-- Set `errToSorry` to `false` if `funLVals` > 1. See comment above about the interaction between `errToSorry` and `observing`.
withReader (fun ctx => { ctx with errToSorry := funLVals.length == 1 && ctx.errToSorry }) do
funLVals.foldlM (init := acc) fun acc ⟨f, fields⟩ => do
let lvals' := fields.map LVal.fieldName
let s ← observing do
let e ← elabAppLVals f (lvals' ++ lvals) namedArgs args expectedType? explicit ellipsis
if overloaded then ensureHasType expectedType? e else pure e
pure $ acc.push s
| _ => throwUnsupportedSyntax
private partial def elabAppFn (f : Syntax) (lvals : List LVal) (namedArgs : Array NamedArg) (args : Array Arg)
(expectedType? : Option Expr) (explicit ellipsis overloaded : Bool) (acc : Array TermElabResult) : TermElabM (Array TermElabResult) :=
if f.getKind == choiceKind then
-- Set `errToSorry` to `false` when processing choice nodes. See comment above about the interaction between `errToSorry` and `observing`.
withReader (fun ctx => { ctx with errToSorry := false }) do
f.getArgs.foldlM (fun acc f => elabAppFn f lvals namedArgs args expectedType? explicit ellipsis true acc) acc
else match_syntax f with
| `($(e).$idx:fieldIdx) =>
let idx := idx.isFieldIdx?.get!
elabAppFn e (LVal.fieldIdx idx :: lvals) namedArgs args expectedType? explicit ellipsis overloaded acc
| `($e |>. $field) => do
let f ← `($(e).$field)
elabAppFn f lvals namedArgs args expectedType? explicit ellipsis overloaded acc
| `($(e).$field:ident) =>
let newLVals := field.getId.eraseMacroScopes.components.map (fun n => LVal.fieldName (toString n))
elabAppFn e (newLVals ++ lvals) namedArgs args expectedType? explicit ellipsis overloaded acc
| `($e[$idx]) =>
elabAppFn e (LVal.getOp idx :: lvals) namedArgs args expectedType? explicit ellipsis overloaded acc
| `($id:ident@$t:term) =>
throwError "unexpected occurrence of named pattern"
| `($id:ident) => do
elabAppFnId id [] lvals namedArgs args expectedType? explicit ellipsis overloaded acc
| `($id:ident.{$us*}) => do
let us ← elabExplicitUnivs us.getSepElems
elabAppFnId id us lvals namedArgs args expectedType? explicit ellipsis overloaded acc
| `(@$id:ident) =>
elabAppFn id lvals namedArgs args expectedType? (explicit := true) ellipsis overloaded acc
| `(@$id:ident.{$us*}) =>
elabAppFn (f.getArg 1) lvals namedArgs args expectedType? (explicit := true) ellipsis overloaded acc
| `(@$t) => throwUnsupportedSyntax -- invalid occurrence of `@`
| `(_) => throwError "placeholders '_' cannot be used where a function is expected"
| _ => do
let catchPostpone := !overloaded
/- If we are processing a choice node, then we should use `catchPostpone == false` when elaborating terms.
Recall that `observing` does not catch `postponeExceptionId`. -/
if lvals.isEmpty && namedArgs.isEmpty && args.isEmpty then
/- Recall that elabAppFn is used for elaborating atomics terms **and** choice nodes that may contain
arbitrary terms. If they are not being used as a function, we should elaborate using the expectedType. -/
let s ←
if overloaded then
observing $ elabTermEnsuringType f expectedType? catchPostpone
else
observing $ elabTerm f expectedType?
pure $ acc.push s
else
let s ← observing do
let f ← elabTerm f none catchPostpone
let e ← elabAppLVals f lvals namedArgs args expectedType? explicit ellipsis
if overloaded then ensureHasType expectedType? e else pure e
pure $ acc.push s
private def isSuccess (candidate : TermElabResult) : Bool :=
match candidate with
| EStateM.Result.ok _ _ => true
| _ => false
private def getSuccess (candidates : Array TermElabResult) : Array TermElabResult :=
candidates.filter isSuccess
private def toMessageData (ex : Exception) : TermElabM MessageData := do
let pos ← getRefPos
match ex.getRef.getPos with
| none => pure ex.toMessageData
| some exPos =>
if pos == exPos then
pure ex.toMessageData
else
let fileMap ← MonadLog.getFileMap -- Remove `MonadLog.` it is a workaround for old frontend
let exPosition := fileMap.toPosition exPos
pure m!"{exPosition.line}:{exPosition.column} {ex.toMessageData}"
private def toMessageList (msgs : Array MessageData) : MessageData :=
indentD (MessageData.joinSep msgs.toList (Format.line ++ Format.line))
private def mergeFailures {α} (failures : Array TermElabResult) : TermElabM α := do
let msgs ← failures.mapM fun failure =>
match failure with
| EStateM.Result.ok _ _ => unreachable!
| EStateM.Result.error ex _ => toMessageData ex
throwError! "overloaded, errors {toMessageList msgs}"
private def elabAppAux (f : Syntax) (namedArgs : Array NamedArg) (args : Array Arg) (ellipsis : Bool) (expectedType? : Option Expr) : TermElabM Expr := do
let candidates ← elabAppFn f [] namedArgs args expectedType? (explicit := false) (ellipsis := ellipsis) (overloaded := false) #[]
if candidates.size == 1 then
applyResult candidates[0]
else
let successes := getSuccess candidates
if successes.size == 1 then
let e ← applyResult successes[0]
pure e
else if successes.size > 1 then
let lctx ← getLCtx
let opts ← getOptions
let msgs : Array MessageData := successes.map fun success => match success with
| EStateM.Result.ok e s => MessageData.withContext { env := s.core.env, mctx := s.meta.mctx, lctx := lctx, opts := opts } e
| _ => unreachable!
throwErrorAt! f "ambiguous, possible interpretations {toMessageList msgs}"
else
withRef f $ mergeFailures candidates
partial def expandApp (stx : Syntax) (pattern := false) : TermElabM (Syntax × Array NamedArg × Array Arg × Bool) := do
let f := stx[0]
let args := stx[1].getArgs
let (args, ellipsis) :=
if args.back.isOfKind `Lean.Parser.Term.ellipsis then
(args.pop, true)
else
(args, false)
let (namedArgs, args) ← args.foldlM (init := (#[], #[])) fun (namedArgs, args) stx => do
if stx.getKind == `Lean.Parser.Term.namedArgument then
-- tparser! try ("(" >> ident >> " := ") >> termParser >> ")"
let name := stx[1].getId.eraseMacroScopes
let val := stx[3]
let namedArgs ← addNamedArg namedArgs { ref := stx, name := name, val := Arg.stx val }
pure (namedArgs, args)
else if stx.getKind == `Lean.Parser.Term.ellipsis then
throwErrorAt stx "unexpected '..'"
else
pure (namedArgs, args.push $ Arg.stx stx)
pure (f, namedArgs, args, ellipsis)
@[builtinTermElab app] def elabApp : TermElab := fun stx expectedType? =>
withoutPostponingUniverseConstraints do
let (f, namedArgs, args, ellipsis) ← expandApp stx
elabAppAux f namedArgs args (ellipsis := ellipsis) expectedType?
private def elabAtom : TermElab := fun stx expectedType? =>
elabAppAux stx #[] #[] (ellipsis := false) expectedType?
@[builtinTermElab ident] def elabIdent : TermElab := elabAtom
@[builtinTermElab namedPattern] def elabNamedPattern : TermElab := elabAtom
@[builtinTermElab explicitUniv] def elabExplicitUniv : TermElab := elabAtom
@[builtinTermElab pipeProj] def expandPipeProj : TermElab := elabAtom
@[builtinTermElab explicit] def elabExplicit : TermElab := fun stx expectedType? =>
match_syntax stx with
| `(@$id:ident) => elabAtom stx expectedType? -- Recall that `elabApp` also has support for `@`
| `(@$id:ident.{$us*}) => elabAtom stx expectedType?
| `(@($t)) => elabTermWithoutImplicitLambdas t expectedType? -- `@` is being used just to disable implicit lambdas
| `(@$t) => elabTermWithoutImplicitLambdas t expectedType? -- `@` is being used just to disable implicit lambdas
| _ => throwUnsupportedSyntax
@[builtinTermElab choice] def elabChoice : TermElab := elabAtom
@[builtinTermElab proj] def elabProj : TermElab := elabAtom
@[builtinTermElab arrayRef] def elabArrayRef : TermElab := elabAtom
builtin_initialize
registerTraceClass `Elab.app
end Lean.Elab.Term
|
13a0dc0d2838dbe643d1262c2506206f9f31c012 | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/number_theory/bernoulli.lean | 9cf7b72ad0cf30a618ee4823f06acfca48ff67d4 | [
"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 | 4,507 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Kevin Buzzard
-/
import data.rat
import data.fintype.card
/-!
# Bernoulli numbers
The Bernoulli numbers are a sequence of rational numbers that frequently show up in
number theory.
## Mathematical overview
The Bernoulli numbers $(B_0, B_1, B_2, \ldots)=(1, 1/2, 1/6, 0, -1/30, \ldots)$ are
a sequence of rational numbers. They show up in the formula for the sums of $k$th
powers. They are related to the Taylor series expansions of $x/\tan(x)$ and
of $\coth(x)$, and also show up in the values that the Riemann Zeta function
takes both at both negative and positive integers (and hence in the
theory of modular forms). For example, if $1 \leq n$ is even then
$$\zeta(2n)=\sum_{t\geq1}t^{-2n}=(-1)^{n+1}\frac{(2\pi)^{2n}B_{2n}}{2(2n)!}.$$
Note however that this result is not yet formalised in Lean.
The Bernoulli numbers can be formally defined using the power series
$$\sum B_n\frac{t^n}{n!}=\frac{t}{1-e^{-t}}$$
although that happens to not be the definition in mathlib (this is an *implementation
detail* though, and need not concern the mathematician).
Note that $B_1=+1/2$, meaning that we are using the $B_n^+$ of
[from Wikipedia](https://en.wikipedia.org/wiki/Bernoulli_number).
To get the "minus" convention, just use `(-1)^n * bernoulli n`.
There is no particular reason that the `+` convention was used.
In some sense it's like choosing whether you want to sum over `fin n`
(so `j < n`) or sum over `j ≤ n` (or `nat.antidiagonal n`). Indeed
$$(t+1)\sum_{j\lt n}j^t=\sum_{k\leq t}\binom{t+1}{k}B_k^{-}n^{t+1-k}$$
and
$$(t+1)\sum_{j\leq n}j^t=\sum_{k\leq t}\binom{t+1}{k}B_k^{+}n^{t+1-k}.$$
## Implementation detail
The Bernoulli numbers are defined using well-founded induction, by the formula
$$B_n=1-\sum_{k\lt n}\frac{\binom{n}{k}}{n-k+1}B_k.$$
This formula is true for all $n$ and in particular $B_0=1$.
## Main theorems
`sum_bernoulli : ∑ k in finset.range n, (n.choose k : ℚ) * bernoulli k = n`
## Todo
* `∑ k : fin n, n.binomial k * (-1)^k * bernoulli k = if n = 1 then 1 else 0`
* Bernoulli polynomials
* `∑ k : fin n, k ^ t =` the Bernoulli polynomial B_t evaluated at n
* `∑ k : fin n.succ, n.succ.choose k bernoulli_poly k X = n.succ * X ^ n` as polynomials
-/
open_locale big_operators
/-!
### Definitions
-/
/-- The Bernoulli numbers:
the $n$-th Bernoulli number $B_n$ is defined recursively via
$$B_n = 1 - \sum_{k < n} \binom{n}{k}\frac{B_k}{n+1-k}$$ -/
def bernoulli : ℕ → ℚ :=
well_founded.fix nat.lt_wf
(λ n bernoulli, 1 - ∑ k : fin n, n.choose k / (n - k + 1) * bernoulli k k.2)
lemma bernoulli_def' (n : ℕ) :
bernoulli n = 1 - ∑ k : fin n, (n.choose k) / (n - k + 1) * bernoulli k :=
well_founded.fix_eq _ _ _
lemma bernoulli_def (n : ℕ) :
bernoulli n = 1 - ∑ k in finset.range n, (n.choose k) / (n - k + 1) * bernoulli k :=
by { rw [bernoulli_def', ← fin.sum_univ_eq_sum_range], refl }
/-!
### Examples
-/
section examples
open finset
@[simp] lemma bernoulli_zero : bernoulli 0 = 1 := rfl
@[simp] lemma bernoulli_one : bernoulli 1 = 1/2 :=
begin
rw [bernoulli_def, sum_range_one], norm_num
end
@[simp] lemma bernoulli_two : bernoulli 2 = 1/6 :=
begin
rw [bernoulli_def, sum_range_succ, sum_range_one], norm_num
end
@[simp] lemma bernoulli_three : bernoulli 3 = 0 :=
begin
rw [bernoulli_def, sum_range_succ, sum_range_succ, sum_range_one], norm_num
end
@[simp] lemma bernoulli_four : bernoulli 4 = -1/30 :=
begin
rw [bernoulli_def, sum_range_succ, sum_range_succ, sum_range_succ, sum_range_one],
rw (show nat.choose 4 2 = 6, from dec_trivial), -- shrug
norm_num,
end
end examples
open nat finset
@[simp] lemma sum_bernoulli (n : ℕ) :
∑ k in finset.range n, (n.choose k : ℚ) * bernoulli k = n :=
begin
cases n with n, { simp },
rw [sum_range_succ, bernoulli_def],
suffices : (n + 1 : ℚ) * ∑ k in range n, (n.choose k : ℚ) / (n - k + 1) * bernoulli k =
∑ x in range n, (n.succ.choose x : ℚ) * bernoulli x,
{ rw [← this, choose_succ_self_right], norm_cast, ring},
simp_rw [mul_sum, ← mul_assoc],
apply sum_congr rfl,
intros k hk, replace hk := le_of_lt (mem_range.1 hk),
rw ← cast_sub hk,
congr',
field_simp [show ((n - k : ℕ) : ℚ) + 1 ≠ 0, by {norm_cast, simp}],
-- down to nat
norm_cast,
rw [mul_comm, nat.sub_add_eq_add_sub hk],
exact choose_mul_succ_eq n k,
end
|
51783a63376482e7e2c434d4036878c870f8eac0 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/data/polynomial/basic.lean | d7d2c1932d08971dc1c3d2aebf6fb5650d7fae9b | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 24,530 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import tactic.ring_exp
import tactic.chain
import algebra.monoid_algebra
import data.finset.sort
import tactic.ring
/-!
# Theory of univariate polynomials
This file defines `polynomial R`, the type of univariate polynomials over the semiring `R`, builds
a semiring structure on it, and gives basic definitions that are expanded in other files in this
directory.
## Main definitions
* `monomial n a` is the polynomial `a X^n`. Note that `monomial n` is defined as an `R`-linear map.
* `C a` is the constant polynomial `a`. Note that `C` is defined as a ring homomorphism.
* `X` is the polynomial `X`, i.e., `monomial 1 1`.
* `p.sum f` is `∑ n in p.support, f n (p.coeff n)`, i.e., one sums the values of functions applied
to coefficients of the polynomial `p`.
* `p.erase n` is the polynomial `p` in which one removes the `c X^n` term.
There are often two natural variants of lemmas involving sums, depending on whether one acts on the
polynomials, or on the function. The naming convention is that one adds `index` when acting on
the polynomials. For instance,
* `sum_add_index` states that `(p + q).sum f = p.sum f + q.sum f`;
* `sum_add` states that `p.sum (λ n x, f n x + g n x) = p.sum f + p.sum g`.
## Implementation
Polynomials are defined using `add_monoid_algebra R ℕ`, where `R` is a commutative semiring, but
through a structure to make them irreducible from the point of view of the kernel. Most operations
are irreducible since Lean can not compute anyway with `add_monoid_algebra`. There are two
exceptions that we make semireducible:
* The zero polynomial, so that its coefficients are definitionally equal to `0`.
* The scalar action, to permit typeclass search to unfold it to resolve potential instance
diamonds.
The raw implementation of the equivalence between `polynomial R` and `add_monoid_algebra R ℕ` is
done through `of_finsupp` and `to_finsupp` (or, equivalently, `rcases p` when `p` is a polynomial
gives an element `q` of `add_monoid_algebra R ℕ`, and conversely `⟨q⟩` gives back `p`). The
equivalence is also registered as a ring equiv in `polynomial.to_finsupp_iso`. These should
in general not be used once the basic API for polynomials is constructed.
-/
noncomputable theory
/-- `polynomial R` is the type of univariate polynomials over `R`.
Polynomials should be seen as (semi-)rings with the additional constructor `X`.
The embedding from `R` is called `C`. -/
structure polynomial (R : Type*) [semiring R] := of_finsupp ::
(to_finsupp : add_monoid_algebra R ℕ)
open finsupp add_monoid_algebra
open_locale big_operators
namespace polynomial
universes u
variables {R : Type u} {a b : R} {m n : ℕ}
section semiring
variables [semiring R] {p q : polynomial R}
lemma forall_iff_forall_finsupp (P : polynomial R → Prop) :
(∀ p, P p) ↔ ∀ (q : add_monoid_algebra R ℕ), P ⟨q⟩ :=
⟨λ h q, h ⟨q⟩, λ h ⟨p⟩, h p⟩
lemma exists_iff_exists_finsupp (P : polynomial R → Prop) :
(∃ p, P p) ↔ ∃ (q : add_monoid_algebra R ℕ), P ⟨q⟩ :=
⟨λ ⟨⟨p⟩, hp⟩, ⟨p, hp⟩, λ ⟨q, hq⟩, ⟨⟨q⟩, hq⟩ ⟩
/-- The function version of `monomial`. Use `monomial` instead of this one. -/
@[irreducible] def monomial_fun (n : ℕ) (a : R) : polynomial R := ⟨finsupp.single n a⟩
@[irreducible] private def add : polynomial R → polynomial R → polynomial R
| ⟨a⟩ ⟨b⟩ := ⟨a + b⟩
@[irreducible] private def neg {R : Type u} [ring R] : polynomial R → polynomial R
| ⟨a⟩ := ⟨-a⟩
@[irreducible] private def mul : polynomial R → polynomial R → polynomial R
| ⟨a⟩ ⟨b⟩ := ⟨a * b⟩
instance : has_zero (polynomial R) := ⟨⟨0⟩⟩
instance : has_one (polynomial R) := ⟨monomial_fun 0 (1 : R)⟩
instance : has_add (polynomial R) := ⟨add⟩
instance {R : Type u} [ring R] : has_neg (polynomial R) := ⟨neg⟩
instance : has_mul (polynomial R) := ⟨mul⟩
instance {S : Type*} [monoid S] [distrib_mul_action S R] : has_scalar S (polynomial R) :=
⟨λ r p, ⟨r • p.to_finsupp⟩⟩
lemma zero_to_finsupp : (⟨0⟩ : polynomial R) = 0 :=
rfl
lemma one_to_finsupp : (⟨1⟩ : polynomial R) = 1 :=
begin
change (⟨1⟩ : polynomial R) = monomial_fun 0 (1 : R),
rw [monomial_fun],
refl
end
lemma add_to_finsupp {a b} : (⟨a⟩ + ⟨b⟩ : polynomial R) = ⟨a + b⟩ := show add _ _ = _, by rw add
lemma neg_to_finsupp {R : Type u} [ring R] {a} : (-⟨a⟩ : polynomial R) = ⟨-a⟩ :=
show neg _ = _, by rw neg
lemma mul_to_finsupp {a b} : (⟨a⟩ * ⟨b⟩ : polynomial R) = ⟨a * b⟩ := show mul _ _ = _, by rw mul
lemma smul_to_finsupp {S : Type*} [monoid S] [distrib_mul_action S R] {a : S} {b} :
(a • ⟨b⟩ : polynomial R) = ⟨a • b⟩ := rfl
instance : inhabited (polynomial R) := ⟨0⟩
instance : semiring (polynomial R) :=
by refine_struct
{ zero := (0 : polynomial R),
one := 1,
mul := (*),
add := (+),
nsmul := (•),
npow := npow_rec,
npow_zero' := λ x, rfl,
npow_succ' := λ n x, rfl };
{ repeat { rintro ⟨_⟩, };
simp [← zero_to_finsupp, ← one_to_finsupp, add_to_finsupp, mul_to_finsupp, mul_assoc, mul_add,
add_mul, smul_to_finsupp, nat.succ_eq_one_add]; abel }
instance {S} [monoid S] [distrib_mul_action S R] : distrib_mul_action S (polynomial R) :=
{ smul := (•),
one_smul := by { rintros ⟨⟩, simp [smul_to_finsupp] },
mul_smul := by { rintros _ _ ⟨⟩, simp [smul_to_finsupp, mul_smul], },
smul_add := by { rintros _ ⟨⟩ ⟨⟩, simp [smul_to_finsupp, add_to_finsupp] },
smul_zero := by { rintros _, simp [← zero_to_finsupp, smul_to_finsupp] } }
instance {S} [semiring S] [module S R] : module S (polynomial R) :=
{ smul := (•),
add_smul := by { rintros _ _ ⟨⟩, simp [smul_to_finsupp, add_to_finsupp, add_smul] },
zero_smul := by { rintros ⟨⟩, simp [smul_to_finsupp, ← zero_to_finsupp] },
..polynomial.distrib_mul_action }
instance {S₁ S₂} [monoid S₁] [monoid S₂] [distrib_mul_action S₁ R] [distrib_mul_action S₂ R]
[smul_comm_class S₁ S₂ R] : smul_comm_class S₁ S₂ (polynomial R) :=
⟨by { rintros _ _ ⟨⟩, simp [smul_to_finsupp, smul_comm] }⟩
instance {S₁ S₂} [has_scalar S₁ S₂] [monoid S₁] [monoid S₂] [distrib_mul_action S₁ R]
[distrib_mul_action S₂ R] [is_scalar_tower S₁ S₂ R] : is_scalar_tower S₁ S₂ (polynomial R) :=
⟨by { rintros _ _ ⟨⟩, simp [smul_to_finsupp] }⟩
instance [subsingleton R] : unique (polynomial R) :=
{ uniq := by { rintros ⟨x⟩, change (⟨x⟩ : polynomial R) = 0, rw [← zero_to_finsupp], simp },
.. polynomial.inhabited }
variable (R)
/-- Ring isomorphism between `polynomial R` and `add_monoid_algebra R ℕ`. This is just an
implementation detail, but it can be useful to transfer results from `finsupp` to polynomials. -/
@[simps]
def to_finsupp_iso : polynomial R ≃+* add_monoid_algebra R ℕ :=
{ to_fun := λ p, p.to_finsupp,
inv_fun := λ p, ⟨p⟩,
left_inv := λ ⟨p⟩, rfl,
right_inv := λ p, rfl,
map_mul' := by { rintros ⟨⟩ ⟨⟩, simp [mul_to_finsupp] },
map_add' := by { rintros ⟨⟩ ⟨⟩, simp [add_to_finsupp] } }
variable {R}
lemma sum_to_finsupp {ι : Type*} (s : finset ι) (f : ι → add_monoid_algebra R ℕ) :
∑ i in s, (⟨f i⟩ : polynomial R) = ⟨∑ i in s, f i⟩ :=
((to_finsupp_iso R).symm.to_add_monoid_hom.map_sum f s).symm
/--
The set of all `n` such that `X^n` has a non-zero coefficient.
-/
def support : polynomial R → finset ℕ
| ⟨p⟩ := p.support
@[simp] lemma support_zero : (0 : polynomial R).support = ∅ :=
rfl
@[simp] lemma support_eq_empty : p.support = ∅ ↔ p = 0 :=
by { rcases p, simp [support, ← zero_to_finsupp] }
lemma card_support_eq_zero : p.support.card = 0 ↔ p = 0 :=
by simp
/-- `monomial s a` is the monomial `a * X^s` -/
def monomial (n : ℕ) : R →ₗ[R] polynomial R :=
{ to_fun := monomial_fun n,
map_add' := by simp [monomial_fun, add_to_finsupp],
map_smul' := by simp [monomial_fun, smul_to_finsupp] }
@[simp]
lemma monomial_zero_right (n : ℕ) :
monomial n (0 : R) = 0 :=
by simp [monomial, monomial_fun]
-- This is not a `simp` lemma as `monomial_zero_left` is more general.
lemma monomial_zero_one : monomial 0 (1 : R) = 1 := rfl
lemma monomial_add (n : ℕ) (r s : R) :
monomial n (r + s) = monomial n r + monomial n s :=
by simp [monomial, monomial_fun]
lemma monomial_mul_monomial (n m : ℕ) (r s : R) :
monomial n r * monomial m s = monomial (n + m) (r * s) :=
by simp only [monomial, monomial_fun, linear_map.coe_mk, mul_to_finsupp,
add_monoid_algebra.single_mul_single]
@[simp]
lemma monomial_pow (n : ℕ) (r : R) (k : ℕ) :
(monomial n r)^k = monomial (n*k) (r^k) :=
begin
induction k with k ih,
{ simp [pow_zero, monomial_zero_one], },
{ simp [pow_succ, ih, monomial_mul_monomial, nat.succ_eq_add_one, mul_add, add_comm] },
end
lemma smul_monomial {S} [monoid S] [distrib_mul_action S R] (a : S) (n : ℕ) (b : R) :
a • monomial n b = monomial n (a • b) :=
by simp [monomial, monomial_fun, smul_to_finsupp]
@[simp] lemma to_finsupp_iso_monomial : (to_finsupp_iso R) (monomial n a) = single n a :=
by simp [to_finsupp_iso, monomial, monomial_fun]
@[simp] lemma to_finsupp_iso_symm_single : (to_finsupp_iso R).symm (single n a) = monomial n a :=
by simp [to_finsupp_iso, monomial, monomial_fun]
lemma support_add : (p + q).support ⊆ p.support ∪ q.support :=
begin
rcases p, rcases q,
simp only [add_to_finsupp, support],
exact support_add
end
/--
`C a` is the constant polynomial `a`.
`C` is provided as a ring homomorphism.
-/
def C : R →+* polynomial R :=
{ map_one' := by simp [monomial_zero_one],
map_mul' := by simp [monomial_mul_monomial],
map_zero' := by simp,
.. monomial 0 }
@[simp] lemma monomial_zero_left (a : R) : monomial 0 a = C a := rfl
lemma C_0 : C (0 : R) = 0 := by simp
lemma C_1 : C (1 : R) = 1 := rfl
lemma C_mul : C (a * b) = C a * C b := C.map_mul a b
lemma C_add : C (a + b) = C a + C b := C.map_add a b
@[simp] lemma smul_C {S} [monoid S] [distrib_mul_action S R] (s : S) (r : R) :
s • C r = C (s • r) :=
smul_monomial _ _ r
@[simp] lemma C_bit0 : C (bit0 a) = bit0 (C a) := C_add
@[simp] lemma C_bit1 : C (bit1 a) = bit1 (C a) := by simp [bit1, C_bit0]
lemma C_pow : C (a ^ n) = C a ^ n := C.map_pow a n
@[simp]
lemma C_eq_nat_cast (n : ℕ) : C (n : R) = (n : polynomial R) :=
C.map_nat_cast n
@[simp] lemma C_mul_monomial : C a * monomial n b = monomial n (a * b) :=
by simp only [←monomial_zero_left, monomial_mul_monomial, zero_add]
@[simp] lemma monomial_mul_C : monomial n a * C b = monomial n (a * b) :=
by simp only [←monomial_zero_left, monomial_mul_monomial, add_zero]
/-- `X` is the polynomial variable (aka indeterminate). -/
def X : polynomial R := monomial 1 1
lemma monomial_one_one_eq_X : monomial 1 (1 : R) = X := rfl
lemma monomial_one_right_eq_X_pow (n : ℕ) : monomial n (1 : R) = X^n :=
begin
induction n with n ih,
{ simp [monomial_zero_one], },
{ rw [pow_succ, ←ih, ←monomial_one_one_eq_X, monomial_mul_monomial, add_comm, one_mul], }
end
/-- `X` commutes with everything, even when the coefficients are noncommutative. -/
lemma X_mul : X * p = p * X :=
begin
rcases p,
simp only [X, monomial, monomial_fun, mul_to_finsupp, linear_map.coe_mk],
ext,
simp [add_monoid_algebra.mul_apply, sum_single_index, add_comm],
end
lemma X_pow_mul {n : ℕ} : X^n * p = p * X^n :=
begin
induction n with n ih,
{ simp, },
{ conv_lhs { rw pow_succ', },
rw [mul_assoc, X_mul, ←mul_assoc, ih, mul_assoc, ←pow_succ'], }
end
lemma X_pow_mul_assoc {n : ℕ} : (p * X^n) * q = (p * q) * X^n :=
by rw [mul_assoc, X_pow_mul, ←mul_assoc]
lemma commute_X (p : polynomial R) : commute X p := X_mul
@[simp]
lemma monomial_mul_X (n : ℕ) (r : R) : monomial n r * X = monomial (n+1) r :=
by erw [monomial_mul_monomial, mul_one]
@[simp]
lemma monomial_mul_X_pow (n : ℕ) (r : R) (k : ℕ) : monomial n r * X^k = monomial (n+k) r :=
begin
induction k with k ih,
{ simp, },
{ simp [ih, pow_succ', ←mul_assoc, add_assoc], },
end
@[simp]
lemma X_mul_monomial (n : ℕ) (r : R) : X * monomial n r = monomial (n+1) r :=
by rw [X_mul, monomial_mul_X]
@[simp]
lemma X_pow_mul_monomial (k n : ℕ) (r : R) : X^k * monomial n r = monomial (n+k) r :=
by rw [X_pow_mul, monomial_mul_X_pow]
/-- `coeff p n` (often denoted `p.coeff n`) is the coefficient of `X^n` in `p`. -/
def coeff : polynomial R → ℕ → R
| ⟨p⟩ n := p n
lemma coeff_monomial : coeff (monomial n a) m = if n = m then a else 0 :=
by { simp only [monomial, monomial_fun, coeff, linear_map.coe_mk], rw finsupp.single_apply }
@[simp] lemma coeff_zero (n : ℕ) : coeff (0 : polynomial R) n = 0 := rfl
@[simp] lemma coeff_one_zero : coeff (1 : polynomial R) 0 = 1 :=
by { rw [← monomial_zero_one, coeff_monomial], simp }
@[simp] lemma coeff_X_one : coeff (X : polynomial R) 1 = 1 := coeff_monomial
@[simp] lemma coeff_X_zero : coeff (X : polynomial R) 0 = 0 := coeff_monomial
@[simp] lemma coeff_monomial_succ : coeff (monomial (n+1) a) 0 = 0 :=
by simp [coeff_monomial]
lemma coeff_X : coeff (X : polynomial R) n = if 1 = n then 1 else 0 := coeff_monomial
lemma coeff_X_of_ne_one {n : ℕ} (hn : n ≠ 1) : coeff (X : polynomial R) n = 0 :=
by rw [coeff_X, if_neg hn.symm]
@[simp] lemma mem_support_iff : n ∈ p.support ↔ p.coeff n ≠ 0 :=
by { rcases p, simp [support, coeff] }
lemma not_mem_support_iff : n ∉ p.support ↔ p.coeff n = 0 :=
by simp
lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 :=
by { convert coeff_monomial using 2, simp [eq_comm], }
@[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_monomial
lemma coeff_C_ne_zero (h : n ≠ 0) : (C a).coeff n = 0 :=
by rw [coeff_C, if_neg h]
theorem nontrivial.of_polynomial_ne (h : p ≠ q) : nontrivial R :=
⟨⟨0, 1, λ h01 : 0 = 1, h $
by rw [← mul_one p, ← mul_one q, ← C_1, ← h01, C_0, mul_zero, mul_zero] ⟩⟩
lemma monomial_eq_C_mul_X : ∀{n}, monomial n a = C a * X^n
| 0 := (mul_one _).symm
| (n+1) :=
calc monomial (n + 1) a = monomial n a * X : by { rw [X, monomial_mul_monomial, mul_one], }
... = (C a * X^n) * X : by rw [monomial_eq_C_mul_X]
... = C a * X^(n+1) : by simp only [pow_add, mul_assoc, pow_one]
@[simp] lemma C_inj : C a = C b ↔ a = b :=
⟨λ h, coeff_C_zero.symm.trans (h.symm ▸ coeff_C_zero), congr_arg C⟩
@[simp] lemma C_eq_zero : C a = 0 ↔ a = 0 :=
calc C a = 0 ↔ C a = C 0 : by rw C_0
... ↔ a = 0 : C_inj
theorem ext_iff {p q : polynomial R} : p = q ↔ ∀ n, coeff p n = coeff q n :=
by { rcases p, rcases q, simp [coeff, finsupp.ext_iff] }
@[ext] lemma ext {p q : polynomial R} : (∀ n, coeff p n = coeff q n) → p = q :=
ext_iff.2
lemma add_hom_ext {M : Type*} [add_monoid M] {f g : polynomial R →+ M}
(h : ∀ n a, f (monomial n a) = g (monomial n a)) :
f = g :=
begin
set f' : add_monoid_algebra R ℕ →+ M := f.comp (to_finsupp_iso R).symm with hf',
set g' : add_monoid_algebra R ℕ →+ M := g.comp (to_finsupp_iso R).symm with hg',
have : ∀ n a, f' (single n a) = g' (single n a) := λ n, by simp [hf', hg', h n],
have A : f' = g' := finsupp.add_hom_ext this,
have B : f = f'.comp (to_finsupp_iso R), by { rw [hf', add_monoid_hom.comp_assoc], ext x,
simp only [ring_equiv.symm_apply_apply, add_monoid_hom.coe_comp, function.comp_app,
ring_hom.coe_add_monoid_hom, ring_equiv.coe_to_ring_hom, coe_coe]},
have C : g = g'.comp (to_finsupp_iso R), by { rw [hg', add_monoid_hom.comp_assoc], ext x,
simp only [ring_equiv.symm_apply_apply, add_monoid_hom.coe_comp, function.comp_app,
ring_hom.coe_add_monoid_hom, ring_equiv.coe_to_ring_hom, coe_coe]},
rw [B, C, A],
end
@[ext] lemma add_hom_ext' {M : Type*} [add_monoid M] {f g : polynomial R →+ M}
(h : ∀ n, f.comp (monomial n).to_add_monoid_hom = g.comp (monomial n).to_add_monoid_hom) :
f = g :=
add_hom_ext (λ n, add_monoid_hom.congr_fun (h n))
@[ext] lemma lhom_ext' {M : Type*} [add_comm_monoid M] [module R M] {f g : polynomial R →ₗ[R] M}
(h : ∀ n, f.comp (monomial n) = g.comp (monomial n)) :
f = g :=
linear_map.to_add_monoid_hom_injective $ add_hom_ext $ λ n, linear_map.congr_fun (h n)
-- this has the same content as the subsingleton
lemma eq_zero_of_eq_zero (h : (0 : R) = (1 : R)) (p : polynomial R) : p = 0 :=
by rw [←one_smul R p, ←h, zero_smul]
lemma support_monomial (n) (a : R) (H : a ≠ 0) : (monomial n a).support = singleton n :=
by simp [monomial, monomial_fun, support, finsupp.support_single_ne_zero H]
lemma support_monomial' (n) (a : R) : (monomial n a).support ⊆ singleton n :=
by simp [monomial, monomial_fun, support, finsupp.support_single_subset]
lemma X_pow_eq_monomial (n) : X ^ n = monomial n (1:R) :=
begin
induction n with n hn,
{ rw [pow_zero, monomial_zero_one] },
{ rw [pow_succ', hn, X, monomial_mul_monomial, one_mul] },
end
lemma monomial_eq_smul_X {n} : monomial n (a : R) = a • X^n :=
calc monomial n a = monomial n (a * 1) : by simp
... = a • monomial n 1 : by simp [monomial, monomial_fun, smul_to_finsupp]
... = a • X^n : by rw X_pow_eq_monomial
lemma support_X_pow (H : ¬ (1:R) = 0) (n : ℕ) : (X^n : polynomial R).support = singleton n :=
begin
convert support_monomial n 1 H,
exact X_pow_eq_monomial n,
end
lemma support_X_empty (H : (1:R)=0) : (X : polynomial R).support = ∅ :=
begin
rw [X, H, monomial_zero_right, support_zero],
end
lemma support_X (H : ¬ (1 : R) = 0) : (X : polynomial R).support = singleton 1 :=
begin
rw [← pow_one X, support_X_pow H 1],
end
lemma monomial_left_inj {R : Type*} [semiring R] {a : R} (ha : a ≠ 0) {i j : ℕ} :
(monomial i a) = (monomial j a) ↔ i = j :=
by simp [monomial, monomial_fun, finsupp.single_left_inj ha]
lemma nat_cast_mul {R : Type*} [semiring R] (n : ℕ) (p : polynomial R) :
(n : polynomial R) * p = n • p :=
(nsmul_eq_mul _ _).symm
/-- Summing the values of a function applied to the coefficients of a polynomial -/
def sum {S : Type*} [add_comm_monoid S] (p : polynomial R) (f : ℕ → R → S) : S :=
∑ n in p.support, f n (p.coeff n)
lemma sum_def {S : Type*} [add_comm_monoid S] (p : polynomial R) (f : ℕ → R → S) :
p.sum f = ∑ n in p.support, f n (p.coeff n) := rfl
lemma sum_eq_of_subset {S : Type*} [add_comm_monoid S] (p : polynomial R)
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (s : finset ℕ) (hs : p.support ⊆ s) :
p.sum f = ∑ n in s, f n (p.coeff n) :=
begin
apply finset.sum_subset hs (λ n hn h'n, _),
rw not_mem_support_iff at h'n,
simp [h'n, hf]
end
/-- Expressing the product of two polynomials as a double sum. -/
lemma mul_eq_sum_sum :
p * q = ∑ i in p.support, q.sum (λ j a, (monomial (i + j)) (p.coeff i * a)) :=
begin
rcases p, rcases q,
simp [mul_to_finsupp, support, monomial, sum, monomial_fun, coeff, sum_to_finsupp],
refl
end
@[simp] lemma sum_zero_index {S : Type*} [add_comm_monoid S] (f : ℕ → R → S) :
(0 : polynomial R).sum f = 0 :=
by simp [sum]
@[simp] lemma sum_monomial_index {S : Type*} [add_comm_monoid S]
(n : ℕ) (a : R) (f : ℕ → R → S) (hf : f n 0 = 0) :
(monomial n a : polynomial R).sum f = f n a :=
begin
by_cases h : a = 0,
{ simp [h, hf] },
{ simp [sum, support_monomial, h, coeff_monomial] }
end
@[simp] lemma sum_C_index {a} {β} [add_comm_monoid β] {f : ℕ → R → β} (h : f 0 0 = 0) :
(C a).sum f = f 0 a :=
sum_monomial_index 0 a f h
-- the assumption `hf` is only necessary when the ring is trivial
@[simp] lemma sum_X_index {S : Type*} [add_comm_monoid S] {f : ℕ → R → S} (hf : f 1 0 = 0) :
(X : polynomial R).sum f = f 1 1 :=
sum_monomial_index 1 1 f hf
lemma sum_add_index {S : Type*} [add_comm_monoid S] (p q : polynomial R)
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (h_add : ∀a b₁ b₂, f a (b₁ + b₂) = f a b₁ + f a b₂) :
(p + q).sum f = p.sum f + q.sum f :=
begin
rcases p, rcases q,
simp only [add_to_finsupp, sum, support, coeff, pi.add_apply, coe_add],
exact finsupp.sum_add_index hf h_add,
end
lemma sum_add' {S : Type*} [add_comm_monoid S] (p : polynomial R) (f g : ℕ → R → S) :
p.sum (f + g) = p.sum f + p.sum g :=
by simp [sum_def, finset.sum_add_distrib]
lemma sum_add {S : Type*} [add_comm_monoid S] (p : polynomial R) (f g : ℕ → R → S) :
p.sum (λ n x, f n x + g n x) = p.sum f + p.sum g :=
sum_add' _ _ _
lemma sum_smul_index {S : Type*} [add_comm_monoid S] (p : polynomial R) (b : R)
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) : (b • p).sum f = p.sum (λ n a, f n (b * a)) :=
begin
rcases p,
simp [smul_to_finsupp, sum, support, coeff],
exact finsupp.sum_smul_index hf,
end
/-- `erase p n` is the polynomial `p` in which the `X^n` term has been erased. -/
@[irreducible] definition erase (n : ℕ) : polynomial R → polynomial R
| ⟨p⟩ := ⟨p.erase n⟩
@[simp] lemma support_erase (p : polynomial R) (n : ℕ) :
support (p.erase n) = (support p).erase n :=
by { rcases p, simp only [support, erase, support_erase], congr }
lemma monomial_add_erase (p : polynomial R) (n : ℕ) : monomial n (coeff p n) + p.erase n = p :=
begin
rcases p,
simp [add_to_finsupp, monomial, monomial_fun, coeff, erase],
exact finsupp.single_add_erase _ _
end
lemma coeff_erase (p : polynomial R) (n i : ℕ) :
(p.erase n).coeff i = if i = n then 0 else p.coeff i :=
begin
rcases p,
simp only [erase, coeff],
convert rfl
end
@[simp] lemma erase_zero (n : ℕ) : (0 : polynomial R).erase n = 0 :=
by simp [← zero_to_finsupp, erase]
@[simp] lemma erase_monomial {n : ℕ} {a : R} : erase n (monomial n a) = 0 :=
by simp [monomial, monomial_fun, erase, ← zero_to_finsupp]
@[simp] lemma erase_same (p : polynomial R) (n : ℕ) : coeff (p.erase n) n = 0 :=
by simp [coeff_erase]
@[simp] lemma erase_ne (p : polynomial R) (n i : ℕ) (h : i ≠ n) :
coeff (p.erase n) i = coeff p i :=
by simp [coeff_erase, h]
end semiring
section comm_semiring
variables [comm_semiring R]
instance : comm_semiring (polynomial R) :=
{ mul_comm := by { rintros ⟨⟩ ⟨⟩, simp [mul_to_finsupp, mul_comm] }, .. polynomial.semiring }
end comm_semiring
section ring
variables [ring R]
instance : ring (polynomial R) :=
{ neg := has_neg.neg,
add_left_neg := by { rintros ⟨⟩, simp [neg_to_finsupp, add_to_finsupp, ← zero_to_finsupp] },
gsmul := (•),
gsmul_zero' := by { rintro ⟨⟩, simp [smul_to_finsupp, ← zero_to_finsupp] },
gsmul_succ' := by { rintros n ⟨⟩, simp [smul_to_finsupp, add_to_finsupp, add_smul, add_comm] },
gsmul_neg' := by { rintros n ⟨⟩,
simp only [smul_to_finsupp, neg_to_finsupp], simp [add_smul, add_mul] },
.. polynomial.semiring }
@[simp] lemma coeff_neg (p : polynomial R) (n : ℕ) : coeff (-p) n = -coeff p n :=
by { rcases p, simp [coeff, neg_to_finsupp] }
@[simp]
lemma coeff_sub (p q : polynomial R) (n : ℕ) : coeff (p - q) n = coeff p n - coeff q n :=
by { rcases p, rcases q, simp [coeff, sub_eq_add_neg, add_to_finsupp, neg_to_finsupp] }
@[simp] lemma monomial_neg (n : ℕ) (a : R) : monomial n (-a) = -(monomial n a) :=
by rw [eq_neg_iff_add_eq_zero, ←monomial_add, neg_add_self, monomial_zero_right]
@[simp] lemma support_neg {p : polynomial R} : (-p).support = p.support :=
by { rcases p, simp [support, neg_to_finsupp] }
end ring
instance [comm_ring R] : comm_ring (polynomial R) :=
{ .. polynomial.comm_semiring, .. polynomial.ring }
section nonzero_semiring
variables [semiring R] [nontrivial R]
instance : nontrivial (polynomial R) :=
begin
have h : nontrivial (add_monoid_algebra R ℕ) := by apply_instance,
rcases h.exists_pair_ne with ⟨x, y, hxy⟩,
refine ⟨⟨⟨x⟩, ⟨y⟩, _⟩⟩,
simp [hxy],
end
lemma X_ne_zero : (X : polynomial R) ≠ 0 :=
mt (congr_arg (λ p, coeff p 1)) (by simp)
end nonzero_semiring
section repr
variables [semiring R]
open_locale classical
instance [has_repr R] : has_repr (polynomial R) :=
⟨λ p, if p = 0 then "0"
else (p.support.sort (≤)).foldr
(λ n a, a ++ (if a = "" then "" else " + ") ++
if n = 0
then "C (" ++ repr (coeff p n) ++ ")"
else if n = 1
then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X"
else if (coeff p n) = 1 then "X ^ " ++ repr n
else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""⟩
end repr
end polynomial
|
b9ae4a2e5e564c7ca4171c46efbf33934da4ad0a | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/topology/algebra/valued_field.lean | a1477dd844ac0a3426eb5898bfdcb7ad5982d189 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 14,907 | lean | /-
Copyright (c) 2021 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
-/
import topology.algebra.valuation
import topology.algebra.with_zero_topology
import topology.algebra.uniform_field
/-!
# Valued fields and their completions
In this file we study the topology of a field `K` endowed with a valuation (in our application
to adic spaces, `K` will be the valuation field associated to some valuation on a ring, defined in
valuation.basic).
We already know from valuation.topology that one can build a topology on `K` which
makes it a topological ring.
The first goal is to show `K` is a topological *field*, ie inversion is continuous
at every non-zero element.
The next goal is to prove `K` is a *completable* topological field. This gives us
a completion `hat K` which is a topological field. We also prove that `K` is automatically
separated, so the map from `K` to `hat K` is injective.
Then we extend the valuation given on `K` to a valuation on `hat K`.
-/
open filter set
open_locale topological_space
section division_ring
variables {K : Type*} [division_ring K] {Γ₀ : Type*} [linear_ordered_comm_group_with_zero Γ₀]
section valuation_topological_division_ring
section inversion_estimate
variables (v : valuation K Γ₀)
-- The following is the main technical lemma ensuring that inversion is continuous
-- in the topology induced by a valuation on a division ring (ie the next instance)
-- and the fact that a valued field is completable
-- [BouAC, VI.5.1 Lemme 1]
lemma valuation.inversion_estimate {x y : K} {γ : Γ₀ˣ} (y_ne : y ≠ 0)
(h : v (x - y) < min (γ * ((v y) * (v y))) (v y)) :
v (x⁻¹ - y⁻¹) < γ :=
begin
have hyp1 : v (x - y) < γ * ((v y) * (v y)),
from lt_of_lt_of_le h (min_le_left _ _),
have hyp1' : v (x - y) * ((v y) * (v y))⁻¹ < γ,
from mul_inv_lt_of_lt_mul₀ hyp1,
have hyp2 : v (x - y) < v y,
from lt_of_lt_of_le h (min_le_right _ _),
have key : v x = v y, from valuation.map_eq_of_sub_lt v hyp2,
have x_ne : x ≠ 0,
{ intro h,
apply y_ne,
rw [h, v.map_zero] at key,
exact v.zero_iff.1 key.symm },
have decomp : x⁻¹ - y⁻¹ = x⁻¹ * (y - x) * y⁻¹,
by rw [mul_sub_left_distrib, sub_mul, mul_assoc,
show y * y⁻¹ = 1, from mul_inv_cancel y_ne,
show x⁻¹ * x = 1, from inv_mul_cancel x_ne, mul_one, one_mul],
calc
v (x⁻¹ - y⁻¹) = v (x⁻¹ * (y - x) * y⁻¹) : by rw decomp
... = (v x⁻¹) * (v $ y - x) * (v y⁻¹) : by repeat { rw valuation.map_mul }
... = (v x)⁻¹ * (v $ y - x) * (v y)⁻¹ : by rw [map_inv₀, map_inv₀]
... = (v $ y - x) * ((v y) * (v y))⁻¹ : by
{ rw [mul_assoc, mul_comm, key, mul_assoc, mul_inv_rev] }
... = (v $ y - x) * ((v y) * (v y))⁻¹ : rfl
... = (v $ x - y) * ((v y) * (v y))⁻¹ : by rw valuation.map_sub_swap
... < γ : hyp1',
end
end inversion_estimate
open valued
/-- The topology coming from a valuation on a division ring makes it a topological division ring
[BouAC, VI.5.1 middle of Proposition 1] -/
@[priority 100]
instance valued.topological_division_ring [valued K Γ₀] : topological_division_ring K :=
{ continuous_at_inv₀ :=
begin
intros x x_ne s s_in,
cases valued.mem_nhds.mp s_in with γ hs, clear s_in,
rw [mem_map, valued.mem_nhds],
change ∃ (γ : Γ₀ˣ), {y : K | (v (y - x) : Γ₀) < γ} ⊆ {x : K | x⁻¹ ∈ s},
have vx_ne := (valuation.ne_zero_iff $ v).mpr x_ne,
let γ' := units.mk0 _ vx_ne,
use min (γ * (γ'*γ')) γ',
intros y y_in,
apply hs,
simp only [mem_set_of_eq] at y_in,
rw [units.min_coe, units.coe_mul, units.coe_mul] at y_in,
exact valuation.inversion_estimate _ x_ne y_in
end,
..(by apply_instance : topological_ring K) }
/-- A valued division ring is separated. -/
@[priority 100]
instance valued_ring.separated [valued K Γ₀] : separated_space K :=
begin
rw separated_iff_t2,
apply topological_add_group.t2_space_of_zero_sep,
intros x x_ne,
refine ⟨{k | v k < v x}, _, λ h, lt_irrefl _ h⟩,
rw valued.mem_nhds,
have vx_ne := (valuation.ne_zero_iff $ v).mpr x_ne,
let γ' := units.mk0 _ vx_ne,
exact ⟨γ', λ y hy, by simpa using hy⟩,
end
section
local attribute [instance] linear_ordered_comm_group_with_zero.topological_space
open valued
lemma valued.continuous_valuation [valued K Γ₀] : continuous (v : K → Γ₀) :=
begin
rw continuous_iff_continuous_at,
intro x,
classical,
by_cases h : x = 0,
{ rw h,
change tendsto _ _ (𝓝 (v (0 : K))),
erw valuation.map_zero,
rw linear_ordered_comm_group_with_zero.tendsto_zero,
intro γ,
rw valued.mem_nhds_zero,
use [γ, set.subset.refl _] },
{ change tendsto _ _ _,
have v_ne : (v x : Γ₀) ≠ 0, from (valuation.ne_zero_iff _).mpr h,
rw linear_ordered_comm_group_with_zero.tendsto_of_ne_zero v_ne,
apply valued.loc_const v_ne },
end
end
end valuation_topological_division_ring
end division_ring
namespace valued
open uniform_space
variables {K : Type*} [field K] {Γ₀ : Type*} [linear_ordered_comm_group_with_zero Γ₀]
[hv: valued K Γ₀]
include hv
local notation `hat ` := completion
/-- A valued field is completable. -/
@[priority 100]
instance completable : completable_top_field K :=
{ nice := begin
rintros F hF h0,
have : ∃ (γ₀ : Γ₀ˣ) (M ∈ F), ∀ x ∈ M, (γ₀ : Γ₀) ≤ v x,
{ rcases filter.inf_eq_bot_iff.mp h0 with ⟨U, U_in, M, M_in, H⟩,
rcases valued.mem_nhds_zero.mp U_in with ⟨γ₀, hU⟩,
existsi [γ₀, M, M_in],
intros x xM,
apply le_of_not_lt _,
intro hyp,
have : x ∈ U ∩ M := ⟨hU hyp, xM⟩,
rwa H at this },
rcases this with ⟨γ₀, M₀, M₀_in, H₀⟩,
rw valued.cauchy_iff at hF ⊢,
refine ⟨hF.1.map _, _⟩,
replace hF := hF.2,
intros γ,
rcases hF (min (γ * γ₀ * γ₀) γ₀) with ⟨M₁, M₁_in, H₁⟩, clear hF,
use (λ x : K, x⁻¹) '' (M₀ ∩ M₁),
split,
{ rw mem_map,
apply mem_of_superset (filter.inter_mem M₀_in M₁_in),
exact subset_preimage_image _ _ },
{ rintros _ ⟨x, ⟨x_in₀, x_in₁⟩, rfl⟩ _ ⟨y, ⟨y_in₀, y_in₁⟩, rfl⟩,
simp only [mem_set_of_eq],
specialize H₁ x x_in₁ y y_in₁,
replace x_in₀ := H₀ x x_in₀,
replace y_in₀ := H₀ y y_in₀, clear H₀,
apply valuation.inversion_estimate,
{ have : (v x : Γ₀) ≠ 0,
{ intro h, rw h at x_in₀, simpa using x_in₀, },
exact (valuation.ne_zero_iff _).mp this },
{ refine lt_of_lt_of_le H₁ _,
rw units.min_coe,
apply min_le_min _ x_in₀,
rw mul_assoc,
have : ((γ₀ * γ₀ : Γ₀ˣ) : Γ₀) ≤ v x * v x,
from calc ↑γ₀ * ↑γ₀ ≤ ↑γ₀ * v x : mul_le_mul_left' x_in₀ ↑γ₀
... ≤ _ : mul_le_mul_right' x_in₀ (v x),
rw units.coe_mul,
exact mul_le_mul_left' this γ } }
end,
..valued_ring.separated }
local attribute [instance] linear_ordered_comm_group_with_zero.topological_space
/-- The extension of the valuation of a valued field to the completion of the field. -/
noncomputable def extension : hat K → Γ₀ :=
completion.dense_inducing_coe.extend (v : K → Γ₀)
lemma continuous_extension : continuous (valued.extension : hat K → Γ₀) :=
begin
refine completion.dense_inducing_coe.continuous_extend _,
intro x₀,
by_cases h : x₀ = coe 0,
{ refine ⟨0, _⟩,
erw [h, ← completion.dense_inducing_coe.to_inducing.nhds_eq_comap]; try { apply_instance },
rw linear_ordered_comm_group_with_zero.tendsto_zero,
intro γ₀,
rw valued.mem_nhds,
exact ⟨γ₀, by simp⟩ },
{ have preimage_one : v ⁻¹' {(1 : Γ₀)} ∈ 𝓝 (1 : K),
{ have : (v (1 : K) : Γ₀) ≠ 0, { rw valuation.map_one, exact zero_ne_one.symm },
convert valued.loc_const this,
ext x,
rw [valuation.map_one, mem_preimage, mem_singleton_iff, mem_set_of_eq] },
obtain ⟨V, V_in, hV⟩ : ∃ V ∈ 𝓝 (1 : hat K), ∀ x : K, (x : hat K) ∈ V → (v x : Γ₀) = 1,
{ rwa [completion.dense_inducing_coe.nhds_eq_comap, mem_comap] at preimage_one },
have : ∃ V' ∈ (𝓝 (1 : hat K)), (0 : hat K) ∉ V' ∧ ∀ x y ∈ V', x*y⁻¹ ∈ V,
{ have : tendsto (λ p : hat K × hat K, p.1*p.2⁻¹) ((𝓝 1).prod (𝓝 1)) (𝓝 1),
{ rw ← nhds_prod_eq,
conv {congr, skip, skip, rw ← (one_mul (1 : hat K))},
refine tendsto.mul continuous_fst.continuous_at
(tendsto.comp _ continuous_snd.continuous_at),
convert continuous_at_inv₀ (zero_ne_one.symm : 1 ≠ (0 : hat K)),
exact inv_one.symm },
rcases tendsto_prod_self_iff.mp this V V_in with ⟨U, U_in, hU⟩,
let hatKstar := ({0}ᶜ : set $ hat K),
have : hatKstar ∈ 𝓝 (1 : hat K),
from compl_singleton_mem_nhds zero_ne_one.symm,
use [U ∩ hatKstar, filter.inter_mem U_in this],
split,
{ rintro ⟨h, h'⟩,
rw mem_compl_singleton_iff at h',
exact h' rfl },
{ rintros x ⟨hx, _⟩ y ⟨hy, _⟩,
apply hU ; assumption } },
rcases this with ⟨V', V'_in, zeroV', hV'⟩,
have nhds_right : (λ x, x*x₀) '' V' ∈ 𝓝 x₀,
{ have l : function.left_inverse (λ x : hat K, x * x₀⁻¹) (λ x : hat K, x * x₀),
{ intro x,
simp only [mul_assoc, mul_inv_cancel h, mul_one] },
have r: function.right_inverse (λ x : hat K, x * x₀⁻¹) (λ x : hat K, x * x₀),
{ intro x,
simp only [mul_assoc, inv_mul_cancel h, mul_one] },
have c : continuous (λ x : hat K, x * x₀⁻¹),
from continuous_id.mul continuous_const,
rw image_eq_preimage_of_inverse l r,
rw ← mul_inv_cancel h at V'_in,
exact c.continuous_at V'_in },
have : ∃ (z₀ : K) (y₀ ∈ V'), coe z₀ = y₀*x₀ ∧ z₀ ≠ 0,
{ rcases completion.dense_range_coe.mem_nhds nhds_right with ⟨z₀, y₀, y₀_in, H : y₀ * x₀ = z₀⟩,
refine ⟨z₀, y₀, y₀_in, ⟨H.symm, _⟩⟩,
rintro rfl,
exact mul_ne_zero (ne_of_mem_of_not_mem y₀_in zeroV') h H },
rcases this with ⟨z₀, y₀, y₀_in, hz₀, z₀_ne⟩,
have vz₀_ne: (v z₀ : Γ₀) ≠ 0 := by rwa valuation.ne_zero_iff,
refine ⟨v z₀, _⟩,
rw [linear_ordered_comm_group_with_zero.tendsto_of_ne_zero vz₀_ne, mem_comap],
use [(λ x, x*x₀) '' V', nhds_right],
intros x x_in,
rcases mem_preimage.1 x_in with ⟨y, y_in, hy⟩, clear x_in,
change y*x₀ = coe x at hy,
have : (v (x*z₀⁻¹) : Γ₀) = 1,
{ apply hV,
have : ((z₀⁻¹ : K) : hat K) = z₀⁻¹,
from map_inv₀ (completion.coe_ring_hom : K →+* hat K) z₀,
rw [completion.coe_mul, this, ← hy, hz₀, mul_inv, mul_comm y₀⁻¹, ← mul_assoc, mul_assoc y,
mul_inv_cancel h, mul_one],
solve_by_elim },
calc v x = v (x*z₀⁻¹*z₀) : by rw [mul_assoc, inv_mul_cancel z₀_ne, mul_one]
... = v (x*z₀⁻¹)*v z₀ : valuation.map_mul _ _ _
... = v z₀ : by rw [this, one_mul] },
end
@[simp, norm_cast]
lemma extension_extends (x : K) : extension (x : hat K) = v x :=
begin
haveI : t2_space Γ₀ := t3_space.t2_space _,
refine completion.dense_inducing_coe.extend_eq_of_tendsto _,
rw ← completion.dense_inducing_coe.nhds_eq_comap,
exact valued.continuous_valuation.continuous_at,
end
/-- the extension of a valuation on a division ring to its completion. -/
noncomputable def extension_valuation :
valuation (hat K) Γ₀ :=
{ to_fun := valued.extension,
map_zero' := by { rw [← v.map_zero, ← valued.extension_extends (0 : K)], refl, },
map_one' := by { rw [← completion.coe_one, valued.extension_extends (1 : K)],
exact valuation.map_one _ },
map_mul' := λ x y, begin
apply completion.induction_on₂ x y,
{ have c1 : continuous (λ (x : hat K × hat K), valued.extension (x.1 * x.2)),
from valued.continuous_extension.comp (continuous_fst.mul continuous_snd),
have c2 : continuous (λ (x : hat K × hat K), valued.extension x.1 * valued.extension x.2),
from (valued.continuous_extension.comp continuous_fst).mul
(valued.continuous_extension.comp continuous_snd),
exact is_closed_eq c1 c2 },
{ intros x y,
norm_cast,
exact valuation.map_mul _ _ _ },
end,
map_add_le_max' := λ x y, begin
rw le_max_iff,
apply completion.induction_on₂ x y,
{ have cont : continuous (valued.extension : hat K → Γ₀) := valued.continuous_extension,
exact (is_closed_le (cont.comp continuous_add) $ cont.comp continuous_fst).union
(is_closed_le (cont.comp continuous_add) $ cont.comp continuous_snd) },
{ intros x y,
dsimp,
norm_cast,
rw ← le_max_iff,
exact v.map_add x y, },
end }
-- Bourbaki CA VI §5 no.3 Proposition 5 (d)
lemma closure_coe_completion_v_lt {γ : Γ₀ˣ} :
closure (coe '' { x : K | v x < (γ : Γ₀) }) = { x : hat K | extension_valuation x < (γ : Γ₀) } :=
begin
ext x,
let γ₀ := extension_valuation x,
suffices : γ₀ ≠ 0 → (x ∈ closure (coe '' { x : K | v x < (γ : Γ₀) }) ↔ γ₀ < (γ : Γ₀)),
{ cases eq_or_ne γ₀ 0,
{ simp only [h, (valuation.zero_iff _).mp h, mem_set_of_eq, valuation.map_zero, units.zero_lt,
iff_true],
apply subset_closure,
exact ⟨0, by simpa only [mem_set_of_eq, valuation.map_zero, units.zero_lt, true_and]⟩, },
{ exact this h, }, },
intros h,
have hγ₀ : extension ⁻¹' {γ₀} ∈ 𝓝 x := continuous_extension.continuous_at.preimage_mem_nhds
(linear_ordered_comm_group_with_zero.singleton_mem_nhds_of_ne_zero h),
rw mem_closure_iff_nhds',
refine ⟨λ hx, _, λ hx s hs, _⟩,
{ obtain ⟨⟨-, y, hy₁ : v y < (γ : Γ₀), rfl⟩, hy₂⟩ := hx _ hγ₀,
replace hy₂ : v y = γ₀, { simpa using hy₂, },
rwa ← hy₂, },
{ obtain ⟨y, hy₁, hy₂ : ↑y ∈ s⟩ := completion.dense_range_coe.mem_nhds (inter_mem hγ₀ hs),
replace hy₁ : v y = γ₀, { simpa using hy₁, },
rw ← hy₁ at hx,
exact ⟨⟨y, ⟨y, hx, rfl⟩⟩, hy₂⟩, },
end
noncomputable instance valued_completion : valued (hat K) Γ₀ :=
{ v := extension_valuation,
is_topological_valuation := λ s,
begin
suffices : has_basis (𝓝 (0 : hat K)) (λ _, true) (λ γ : Γ₀ˣ, { x | extension_valuation x < γ }),
{ rw this.mem_iff,
exact exists_congr (λ γ, by simp), },
simp_rw ← closure_coe_completion_v_lt,
exact (has_basis_nhds_zero K Γ₀).has_basis_of_dense_inducing completion.dense_inducing_coe,
end }
end valued
|
d1cdd0dc38fdb2d70dec3cbfc7fdf73ba4b32241 | bb31430994044506fa42fd667e2d556327e18dfe | /src/measure_theory/pi_system.lean | eeb339d764f3346fff78777b7f4f82db25ffb994 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 31,219 | lean | /-
Copyright (c) 2021 Martin Zinkevich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Martin Zinkevich, Rémy Degenne
-/
import logic.encodable.lattice
import measure_theory.measurable_space_def
/-!
# Induction principles for measurable sets, related to π-systems and λ-systems.
## Main statements
* The main theorem of this file is Dynkin's π-λ theorem, which appears
here as an induction principle `induction_on_inter`. Suppose `s` is a
collection of subsets of `α` such that the intersection of two members
of `s` belongs to `s` whenever it is nonempty. Let `m` be the σ-algebra
generated by `s`. In order to check that a predicate `C` holds on every
member of `m`, it suffices to check that `C` holds on the members of `s` and
that `C` is preserved by complementation and *disjoint* countable
unions.
* The proof of this theorem relies on the notion of `is_pi_system`, i.e., a collection of sets
which is closed under binary non-empty intersections. Note that this is a small variation around
the usual notion in the literature, which often requires that a π-system is non-empty, and closed
also under disjoint intersections. This variation turns out to be convenient for the
formalization.
* The proof of Dynkin's π-λ theorem also requires the notion of `dynkin_system`, i.e., a collection
of sets which contains the empty set, is closed under complementation and under countable union
of pairwise disjoint sets. The disjointness condition is the only difference with `σ`-algebras.
* `generate_pi_system g` gives the minimal π-system containing `g`.
This can be considered a Galois insertion into both measurable spaces and sets.
* `generate_from_generate_pi_system_eq` proves that if you start from a collection of sets `g`,
take the generated π-system, and then the generated σ-algebra, you get the same result as
the σ-algebra generated from `g`. This is useful because there are connections between
independent sets that are π-systems and the generated independent spaces.
* `mem_generate_pi_system_Union_elim` and `mem_generate_pi_system_Union_elim'` show that any
element of the π-system generated from the union of a set of π-systems can be
represented as the intersection of a finite number of elements from these sets.
* `pi_Union_Inter` defines a new π-system from a family of π-systems `π : ι → set (set α)` and a
set of indices `S : set ι`. `pi_Union_Inter π S` is the set of sets that can be written
as `⋂ x ∈ t, f x` for some finset `t ∈ S` and sets `f x ∈ π x`.
## Implementation details
* `is_pi_system` is a predicate, not a type. Thus, we don't explicitly define the galois
insertion, nor do we define a complete lattice. In theory, we could define a complete
lattice and galois insertion on the subtype corresponding to `is_pi_system`.
-/
open measurable_space set
open_locale classical measure_theory
/-- A π-system is a collection of subsets of `α` that is closed under binary intersection of
non-disjoint sets. Usually it is also required that the collection is nonempty, but we don't do
that here. -/
def is_pi_system {α} (C : set (set α)) : Prop :=
∀ s t ∈ C, (s ∩ t : set α).nonempty → s ∩ t ∈ C
namespace measurable_space
lemma is_pi_system_measurable_set {α:Type*} [measurable_space α] :
is_pi_system {s : set α | measurable_set s} :=
λ s hs t ht _, hs.inter ht
end measurable_space
lemma is_pi_system.singleton {α} (S : set α) : is_pi_system ({S} : set (set α)) :=
begin
intros s h_s t h_t h_ne,
rw [set.mem_singleton_iff.1 h_s, set.mem_singleton_iff.1 h_t, set.inter_self,
set.mem_singleton_iff],
end
lemma is_pi_system.insert_empty {α} {S : set (set α)} (h_pi : is_pi_system S) :
is_pi_system (insert ∅ S) :=
begin
intros s hs t ht hst,
cases hs,
{ simp [hs], },
{ cases ht,
{ simp [ht], },
{ exact set.mem_insert_of_mem _ (h_pi s hs t ht hst), }, },
end
lemma is_pi_system.insert_univ {α} {S : set (set α)} (h_pi : is_pi_system S) :
is_pi_system (insert set.univ S) :=
begin
intros s hs t ht hst,
cases hs,
{ cases ht; simp [hs, ht], },
{ cases ht,
{ simp [hs, ht], },
{ exact set.mem_insert_of_mem _ (h_pi s hs t ht hst), }, },
end
lemma is_pi_system.comap {α β} {S : set (set β)} (h_pi : is_pi_system S) (f : α → β) :
is_pi_system {s : set α | ∃ t ∈ S, f ⁻¹' t = s} :=
begin
rintros _ ⟨s, hs_mem, rfl⟩ _ ⟨t, ht_mem, rfl⟩ hst,
rw ← set.preimage_inter at hst ⊢,
refine ⟨s ∩ t, h_pi s hs_mem t ht_mem _, rfl⟩,
by_contra,
rw set.not_nonempty_iff_eq_empty at h,
rw h at hst,
simpa using hst,
end
lemma is_pi_system_Union_of_directed_le {α ι} (p : ι → set (set α))
(hp_pi : ∀ n, is_pi_system (p n)) (hp_directed : directed (≤) p) :
is_pi_system (⋃ n, p n) :=
begin
intros t1 ht1 t2 ht2 h,
rw set.mem_Union at ht1 ht2 ⊢,
cases ht1 with n ht1,
cases ht2 with m ht2,
obtain ⟨k, hpnk, hpmk⟩ : ∃ k, p n ≤ p k ∧ p m ≤ p k := hp_directed n m,
exact ⟨k, hp_pi k t1 (hpnk ht1) t2 (hpmk ht2) h⟩,
end
lemma is_pi_system_Union_of_monotone {α ι} [semilattice_sup ι] (p : ι → set (set α))
(hp_pi : ∀ n, is_pi_system (p n)) (hp_mono : monotone p) :
is_pi_system (⋃ n, p n) :=
is_pi_system_Union_of_directed_le p hp_pi (monotone.directed_le hp_mono)
section order
variables {α : Type*} {ι ι' : Sort*} [linear_order α]
lemma is_pi_system_image_Iio (s : set α) : is_pi_system (Iio '' s) :=
begin
rintro _ ⟨a, ha, rfl⟩ _ ⟨b, hb, rfl⟩ -,
exact ⟨a ⊓ b, inf_ind a b ha hb, Iio_inter_Iio.symm⟩
end
lemma is_pi_system_Iio : is_pi_system (range Iio : set (set α)) :=
@image_univ α _ Iio ▸ is_pi_system_image_Iio univ
lemma is_pi_system_image_Ioi (s : set α) : is_pi_system (Ioi '' s) :=
@is_pi_system_image_Iio αᵒᵈ _ s
lemma is_pi_system_Ioi : is_pi_system (range Ioi : set (set α)) :=
@image_univ α _ Ioi ▸ is_pi_system_image_Ioi univ
lemma is_pi_system_Ixx_mem {Ixx : α → α → set α} {p : α → α → Prop}
(Hne : ∀ {a b}, (Ixx a b).nonempty → p a b)
(Hi : ∀ {a₁ b₁ a₂ b₂}, Ixx a₁ b₁ ∩ Ixx a₂ b₂ = Ixx (max a₁ a₂) (min b₁ b₂)) (s t : set α) :
is_pi_system {S | ∃ (l ∈ s) (u ∈ t) (hlu : p l u), Ixx l u = S} :=
begin
rintro _ ⟨l₁, hls₁, u₁, hut₁, hlu₁, rfl⟩ _ ⟨l₂, hls₂, u₂, hut₂, hlu₂, rfl⟩,
simp only [Hi, ← sup_eq_max, ← inf_eq_min],
exact λ H, ⟨l₁ ⊔ l₂, sup_ind l₁ l₂ hls₁ hls₂, u₁ ⊓ u₂, inf_ind u₁ u₂ hut₁ hut₂, Hne H, rfl⟩
end
lemma is_pi_system_Ixx {Ixx : α → α → set α} {p : α → α → Prop}
(Hne : ∀ {a b}, (Ixx a b).nonempty → p a b)
(Hi : ∀ {a₁ b₁ a₂ b₂}, Ixx a₁ b₁ ∩ Ixx a₂ b₂ = Ixx (max a₁ a₂) (min b₁ b₂))
(f : ι → α) (g : ι' → α) :
@is_pi_system α ({S | ∃ i j (h : p (f i) (g j)), Ixx (f i) (g j) = S}) :=
by simpa only [exists_range_iff] using is_pi_system_Ixx_mem @Hne @Hi (range f) (range g)
lemma is_pi_system_Ioo_mem (s t : set α) :
is_pi_system {S | ∃ (l ∈ s) (u ∈ t) (h : l < u), Ioo l u = S} :=
is_pi_system_Ixx_mem (λ a b ⟨x, hax, hxb⟩, hax.trans hxb) (λ _ _ _ _, Ioo_inter_Ioo) s t
lemma is_pi_system_Ioo (f : ι → α) (g : ι' → α) :
@is_pi_system α {S | ∃ l u (h : f l < g u), Ioo (f l) (g u) = S} :=
is_pi_system_Ixx (λ a b ⟨x, hax, hxb⟩, hax.trans hxb) (λ _ _ _ _, Ioo_inter_Ioo) f g
lemma is_pi_system_Ioc_mem (s t : set α) :
is_pi_system {S | ∃ (l ∈ s) (u ∈ t) (h : l < u), Ioc l u = S} :=
is_pi_system_Ixx_mem (λ a b ⟨x, hax, hxb⟩, hax.trans_le hxb) (λ _ _ _ _, Ioc_inter_Ioc) s t
lemma is_pi_system_Ioc (f : ι → α) (g : ι' → α) :
@is_pi_system α {S | ∃ i j (h : f i < g j), Ioc (f i) (g j) = S} :=
is_pi_system_Ixx (λ a b ⟨x, hax, hxb⟩, hax.trans_le hxb) (λ _ _ _ _, Ioc_inter_Ioc) f g
lemma is_pi_system_Ico_mem (s t : set α) :
is_pi_system {S | ∃ (l ∈ s) (u ∈ t) (h : l < u), Ico l u = S} :=
is_pi_system_Ixx_mem (λ a b ⟨x, hax, hxb⟩, hax.trans_lt hxb) (λ _ _ _ _, Ico_inter_Ico) s t
lemma is_pi_system_Ico (f : ι → α) (g : ι' → α) :
@is_pi_system α {S | ∃ i j (h : f i < g j), Ico (f i) (g j) = S} :=
is_pi_system_Ixx (λ a b ⟨x, hax, hxb⟩, hax.trans_lt hxb) (λ _ _ _ _, Ico_inter_Ico) f g
lemma is_pi_system_Icc_mem (s t : set α) :
is_pi_system {S | ∃ (l ∈ s) (u ∈ t) (h : l ≤ u), Icc l u = S} :=
is_pi_system_Ixx_mem (λ a b, nonempty_Icc.1) (λ _ _ _ _, Icc_inter_Icc) s t
lemma is_pi_system_Icc (f : ι → α) (g : ι' → α) :
@is_pi_system α {S | ∃ i j (h : f i ≤ g j), Icc (f i) (g j) = S} :=
is_pi_system_Ixx (λ a b, nonempty_Icc.1) (λ _ _ _ _, Icc_inter_Icc) f g
end order
/-- Given a collection `S` of subsets of `α`, then `generate_pi_system S` is the smallest
π-system containing `S`. -/
inductive generate_pi_system {α} (S : set (set α)) : set (set α)
| base {s : set α} (h_s : s ∈ S) : generate_pi_system s
| inter {s t : set α} (h_s : generate_pi_system s) (h_t : generate_pi_system t)
(h_nonempty : (s ∩ t).nonempty) : generate_pi_system (s ∩ t)
lemma is_pi_system_generate_pi_system {α} (S : set (set α)) :
is_pi_system (generate_pi_system S) :=
λ s h_s t h_t h_nonempty, generate_pi_system.inter h_s h_t h_nonempty
lemma subset_generate_pi_system_self {α} (S : set (set α)) : S ⊆ generate_pi_system S :=
λ s, generate_pi_system.base
lemma generate_pi_system_subset_self {α} {S : set (set α)} (h_S : is_pi_system S) :
generate_pi_system S ⊆ S :=
begin
intros x h,
induction h with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u,
{ exact h_s, },
{ exact h_S _ h_s _ h_u h_nonempty, },
end
lemma generate_pi_system_eq {α} {S : set (set α)} (h_pi : is_pi_system S) :
generate_pi_system S = S :=
set.subset.antisymm (generate_pi_system_subset_self h_pi) (subset_generate_pi_system_self S)
lemma generate_pi_system_mono {α} {S T : set (set α)} (hST : S ⊆ T) :
generate_pi_system S ⊆ generate_pi_system T :=
begin
intros t ht,
induction ht with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u,
{ exact generate_pi_system.base (set.mem_of_subset_of_mem hST h_s),},
{ exact is_pi_system_generate_pi_system T _ h_s _ h_u h_nonempty, },
end
lemma generate_pi_system_measurable_set {α} [M : measurable_space α] {S : set (set α)}
(h_meas_S : ∀ s ∈ S, measurable_set s) (t : set α)
(h_in_pi : t ∈ generate_pi_system S) : measurable_set t :=
begin
induction h_in_pi with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u,
{ apply h_meas_S _ h_s, },
{ apply measurable_set.inter h_s h_u, },
end
lemma generate_from_measurable_set_of_generate_pi_system {α} {g : set (set α)} (t : set α)
(ht : t ∈ generate_pi_system g) :
measurable_set[generate_from g] t :=
@generate_pi_system_measurable_set α (generate_from g) g
(λ s h_s_in_g, measurable_set_generate_from h_s_in_g) t ht
lemma generate_from_generate_pi_system_eq {α} {g : set (set α)} :
generate_from (generate_pi_system g) = generate_from g :=
begin
apply le_antisymm; apply generate_from_le,
{ exact λ t h_t, generate_from_measurable_set_of_generate_pi_system t h_t, },
{ exact λ t h_t, measurable_set_generate_from (generate_pi_system.base h_t), },
end
/- Every element of the π-system generated by the union of a family of π-systems
is a finite intersection of elements from the π-systems.
For an indexed union version, see `mem_generate_pi_system_Union_elim'`. -/
lemma mem_generate_pi_system_Union_elim {α β} {g : β → set (set α)}
(h_pi : ∀ b, is_pi_system (g b)) (t : set α) (h_t : t ∈ generate_pi_system (⋃ b, g b)) :
∃ (T : finset β) (f : β → set α), (t = ⋂ b ∈ T, f b) ∧ (∀ b ∈ T, f b ∈ g b) :=
begin
induction h_t with s h_s s t' h_gen_s h_gen_t' h_nonempty h_s h_t',
{ rcases h_s with ⟨t', ⟨⟨b, rfl⟩, h_s_in_t'⟩⟩,
refine ⟨{b}, (λ _, s), _⟩,
simpa using h_s_in_t', },
{ rcases h_t' with ⟨T_t', ⟨f_t', ⟨rfl, h_t'⟩⟩⟩,
rcases h_s with ⟨T_s, ⟨f_s, ⟨rfl, h_s⟩ ⟩ ⟩,
use [(T_s ∪ T_t'), (λ (b:β),
if (b ∈ T_s) then (if (b ∈ T_t') then (f_s b ∩ (f_t' b)) else (f_s b))
else (if (b ∈ T_t') then (f_t' b) else (∅ : set α)))],
split,
{ ext a,
simp_rw [set.mem_inter_iff, set.mem_Inter, finset.mem_union, or_imp_distrib],
rw ← forall_and_distrib,
split; intros h1 b; by_cases hbs : b ∈ T_s; by_cases hbt : b ∈ T_t'; specialize h1 b;
simp only [hbs, hbt, if_true, if_false, true_implies_iff, and_self, false_implies_iff,
and_true, true_and] at h1 ⊢,
all_goals { exact h1, }, },
intros b h_b,
split_ifs with hbs hbt hbt,
{ refine h_pi b (f_s b) (h_s b hbs) (f_t' b) (h_t' b hbt) (set.nonempty.mono _ h_nonempty),
exact set.inter_subset_inter (set.bInter_subset_of_mem hbs) (set.bInter_subset_of_mem hbt), },
{ exact h_s b hbs, },
{ exact h_t' b hbt, },
{ rw finset.mem_union at h_b,
apply false.elim (h_b.elim hbs hbt), }, },
end
/- Every element of the π-system generated by an indexed union of a family of π-systems
is a finite intersection of elements from the π-systems.
For a total union version, see `mem_generate_pi_system_Union_elim`. -/
lemma mem_generate_pi_system_Union_elim' {α β} {g : β → set (set α)} {s : set β}
(h_pi : ∀ b ∈ s, is_pi_system (g b)) (t : set α) (h_t : t ∈ generate_pi_system (⋃ b ∈ s, g b)) :
∃ (T : finset β) (f : β → set α), (↑T ⊆ s) ∧ (t = ⋂ b ∈ T, f b) ∧ (∀ b ∈ T, f b ∈ g b) :=
begin
have : t ∈ generate_pi_system (⋃ (b : subtype s), (g ∘ subtype.val) b),
{ suffices h1 : (⋃ (b : subtype s), (g ∘ subtype.val) b) = (⋃ b ∈ s, g b), by rwa h1,
ext x,
simp only [exists_prop, set.mem_Union, function.comp_app, subtype.exists, subtype.coe_mk],
refl },
rcases @mem_generate_pi_system_Union_elim α (subtype s) (g ∘ subtype.val)
(λ b, h_pi b.val b.property) t this with ⟨T, ⟨f, ⟨rfl, h_t'⟩⟩⟩,
refine ⟨T.image subtype.val, function.extend subtype.val f (λ b : β, (∅ : set α)), by simp, _, _⟩,
{ ext a, split;
{ simp only [set.mem_Inter, subtype.forall, finset.set_bInter_finset_image],
intros h1 b h_b h_b_in_T,
have h2 := h1 b h_b h_b_in_T,
revert h2,
rw subtype.val_injective.extend_apply,
apply id } },
{ intros b h_b,
simp_rw [finset.mem_image, exists_prop, subtype.exists,
exists_and_distrib_right, exists_eq_right] at h_b,
cases h_b,
have h_b_alt : b = (subtype.mk b h_b_w).val := rfl,
rw [h_b_alt, subtype.val_injective.extend_apply],
apply h_t',
apply h_b_h },
end
section Union_Inter
variables {α ι : Type*}
/-! ### π-system generated by finite intersections of sets of a π-system family -/
/-- From a set of indices `S : set ι` and a family of sets of sets `π : ι → set (set α)`,
define the set of sets that can be written as `⋂ x ∈ t, f x` for some finset `t ⊆ S` and sets
`f x ∈ π x`. If `π` is a family of π-systems, then it is a π-system. -/
def pi_Union_Inter (π : ι → set (set α)) (S : set ι) : set (set α) :=
{s : set α | ∃ (t : finset ι) (htS : ↑t ⊆ S) (f : ι → set α) (hf : ∀ x, x ∈ t → f x ∈ π x),
s = ⋂ x ∈ t, f x}
lemma pi_Union_Inter_singleton (π : ι → set (set α)) (i : ι) :
pi_Union_Inter π {i} = π i ∪ {univ} :=
begin
ext1 s,
simp only [pi_Union_Inter, exists_prop, mem_union],
refine ⟨_, λ h, _⟩,
{ rintros ⟨t, hti, f, hfπ, rfl⟩,
simp only [subset_singleton_iff, finset.mem_coe] at hti,
by_cases hi : i ∈ t,
{ have ht_eq_i : t = {i},
{ ext1 x, rw finset.mem_singleton, exact ⟨λ h, hti x h, λ h, h.symm ▸ hi⟩, },
simp only [ht_eq_i, finset.mem_singleton, Inter_Inter_eq_left],
exact or.inl (hfπ i hi), },
{ have ht_empty : t = ∅,
{ ext1 x,
simp only [finset.not_mem_empty, iff_false],
exact λ hx, hi (hti x hx ▸ hx), },
simp only [ht_empty, Inter_false, Inter_univ, set.mem_singleton univ, or_true], }, },
{ cases h with hs hs,
{ refine ⟨{i}, _, λ _, s, ⟨λ x hx, _, _⟩⟩,
{ rw finset.coe_singleton, },
{ rw finset.mem_singleton at hx,
rwa hx, },
{ simp only [finset.mem_singleton, Inter_Inter_eq_left], }, },
{ refine ⟨∅, _⟩,
simpa only [finset.coe_empty, subset_singleton_iff, mem_empty_iff_false, is_empty.forall_iff,
implies_true_iff, finset.not_mem_empty, Inter_false, Inter_univ, true_and, exists_const]
using hs, }, },
end
lemma pi_Union_Inter_singleton_left (s : ι → set α) (S : set ι) :
pi_Union_Inter (λ i, ({s i} : set (set α))) S
= {s' : set α | ∃ (t : finset ι) (htS : ↑t ⊆ S), s' = ⋂ i ∈ t, s i} :=
begin
ext1 s',
simp_rw [pi_Union_Inter, set.mem_singleton_iff, exists_prop, set.mem_set_of_eq],
refine ⟨λ h, _, λ ⟨t, htS, h_eq⟩, ⟨t, htS, s, λ _ _, rfl, h_eq⟩⟩,
obtain ⟨t, htS, f, hft_eq, rfl⟩ := h,
refine ⟨t, htS, _⟩,
congr' with i x,
simp_rw set.mem_Inter,
exact ⟨λ h hit, by { rw ← hft_eq i hit, exact h hit, },
λ h hit, by { rw hft_eq i hit, exact h hit, }⟩,
end
lemma generate_from_pi_Union_Inter_singleton_left (s : ι → set α) (S : set ι) :
generate_from (pi_Union_Inter (λ k, {s k}) S) = generate_from {t | ∃ k ∈ S, s k = t} :=
begin
refine le_antisymm (generate_from_le _) (generate_from_mono _),
{ rintro _ ⟨I, hI, f, hf, rfl⟩,
refine finset.measurable_set_bInter _ (λ m hm, measurable_set_generate_from _),
exact ⟨m, hI hm, (hf m hm).symm⟩, },
{ rintro _ ⟨k, hk, rfl⟩,
refine ⟨{k}, λ m hm, _, s, λ i hi, _, _⟩,
{ rw [finset.mem_coe, finset.mem_singleton] at hm,
rwa hm, },
{ exact set.mem_singleton _, },
{ simp only [finset.mem_singleton, set.Inter_Inter_eq_left], }, },
end
/-- If `π` is a family of π-systems, then `pi_Union_Inter π S` is a π-system. -/
lemma is_pi_system_pi_Union_Inter (π : ι → set (set α))
(hpi : ∀ x, is_pi_system (π x)) (S : set ι) :
is_pi_system (pi_Union_Inter π S) :=
begin
rintros t1 ⟨p1, hp1S, f1, hf1m, ht1_eq⟩ t2 ⟨p2, hp2S, f2, hf2m, ht2_eq⟩ h_nonempty,
simp_rw [pi_Union_Inter, set.mem_set_of_eq],
let g := λ n, (ite (n ∈ p1) (f1 n) set.univ) ∩ (ite (n ∈ p2) (f2 n) set.univ),
have hp_union_ss : ↑(p1 ∪ p2) ⊆ S,
{ simp only [hp1S, hp2S, finset.coe_union, union_subset_iff, and_self], },
use [p1 ∪ p2, hp_union_ss, g],
have h_inter_eq : t1 ∩ t2 = ⋂ i ∈ p1 ∪ p2, g i,
{ rw [ht1_eq, ht2_eq],
simp_rw [← set.inf_eq_inter, g],
ext1 x,
simp only [inf_eq_inter, mem_inter_iff, mem_Inter, finset.mem_union],
refine ⟨λ h i hi_mem_union, _, λ h, ⟨λ i hi1, _, λ i hi2, _⟩⟩,
{ split_ifs,
exacts [⟨h.1 i h_1, h.2 i h_2⟩, ⟨h.1 i h_1, set.mem_univ _⟩,
⟨set.mem_univ _, h.2 i h_2⟩, ⟨set.mem_univ _, set.mem_univ _⟩], },
{ specialize h i (or.inl hi1),
rw if_pos hi1 at h,
exact h.1, },
{ specialize h i (or.inr hi2),
rw if_pos hi2 at h,
exact h.2, }, },
refine ⟨λ n hn, _, h_inter_eq⟩,
simp_rw g,
split_ifs with hn1 hn2,
{ refine hpi n (f1 n) (hf1m n hn1) (f2 n) (hf2m n hn2) (set.nonempty_iff_ne_empty.2 (λ h, _)),
rw h_inter_eq at h_nonempty,
suffices h_empty : (⋂ i ∈ p1 ∪ p2, g i) = ∅,
from (set.not_nonempty_iff_eq_empty.mpr h_empty) h_nonempty,
refine le_antisymm (set.Inter_subset_of_subset n _) (set.empty_subset _),
refine set.Inter_subset_of_subset hn _,
simp_rw [g, if_pos hn1, if_pos hn2],
exact h.subset, },
{ simp [hf1m n hn1], },
{ simp [hf2m n h], },
{ exact absurd hn (by simp [hn1, h]), },
end
lemma pi_Union_Inter_mono_left {π π' : ι → set (set α)} (h_le : ∀ i, π i ⊆ π' i) (S : set ι) :
pi_Union_Inter π S ⊆ pi_Union_Inter π' S :=
λ s ⟨t, ht_mem, ft, hft_mem_pi, h_eq⟩, ⟨t, ht_mem, ft, λ x hxt, h_le x (hft_mem_pi x hxt), h_eq⟩
lemma pi_Union_Inter_mono_right {π : ι → set (set α)} {S T : set ι} (hST : S ⊆ T) :
pi_Union_Inter π S ⊆ pi_Union_Inter π T :=
λ s ⟨t, ht_mem, ft, hft_mem_pi, h_eq⟩, ⟨t, ht_mem.trans hST, ft, hft_mem_pi, h_eq⟩
lemma generate_from_pi_Union_Inter_le {m : measurable_space α}
(π : ι → set (set α)) (h : ∀ n, generate_from (π n) ≤ m) (S : set ι) :
generate_from (pi_Union_Inter π S) ≤ m :=
begin
refine generate_from_le _,
rintros t ⟨ht_p, ht_p_mem, ft, hft_mem_pi, rfl⟩,
refine finset.measurable_set_bInter _ (λ x hx_mem, (h x) _ _),
exact measurable_set_generate_from (hft_mem_pi x hx_mem),
end
lemma subset_pi_Union_Inter {π : ι → set (set α)} {S : set ι} {i : ι} (his : i ∈ S) :
π i ⊆ pi_Union_Inter π S :=
begin
have h_ss : {i} ⊆ S,
{ intros j hj, rw mem_singleton_iff at hj, rwa hj, },
refine subset.trans _ (pi_Union_Inter_mono_right h_ss),
rw pi_Union_Inter_singleton,
exact subset_union_left _ _,
end
lemma mem_pi_Union_Inter_of_measurable_set (m : ι → measurable_space α)
{S : set ι} {i : ι} (hiS : i ∈ S) (s : set α)
(hs : measurable_set[m i] s) :
s ∈ pi_Union_Inter (λ n, {s | measurable_set[m n] s}) S :=
subset_pi_Union_Inter hiS hs
lemma le_generate_from_pi_Union_Inter {π : ι → set (set α)} (S : set ι) {x : ι} (hxS : x ∈ S) :
generate_from (π x) ≤ generate_from (pi_Union_Inter π S) :=
generate_from_mono (subset_pi_Union_Inter hxS)
lemma measurable_set_supr_of_mem_pi_Union_Inter (m : ι → measurable_space α)
(S : set ι) (t : set α) (ht : t ∈ pi_Union_Inter (λ n, {s | measurable_set[m n] s}) S) :
measurable_set[⨆ i ∈ S, m i] t :=
begin
rcases ht with ⟨pt, hpt, ft, ht_m, rfl⟩,
refine pt.measurable_set_bInter (λ i hi, _),
suffices h_le : m i ≤ (⨆ i ∈ S, m i), from h_le (ft i) (ht_m i hi),
have hi' : i ∈ S := hpt hi,
exact le_supr₂ i hi',
end
lemma generate_from_pi_Union_Inter_measurable_set (m : ι → measurable_space α) (S : set ι) :
generate_from (pi_Union_Inter (λ n, {s | measurable_set[m n] s}) S) = ⨆ i ∈ S, m i :=
begin
refine le_antisymm _ _,
{ rw ← @generate_from_measurable_set α (⨆ i ∈ S, m i),
exact generate_from_mono (measurable_set_supr_of_mem_pi_Union_Inter m S), },
{ refine supr₂_le (λ i hi, _),
rw ← @generate_from_measurable_set α (m i),
exact generate_from_mono (mem_pi_Union_Inter_of_measurable_set m hi), },
end
end Union_Inter
namespace measurable_space
variable {α : Type*}
/-! ## Dynkin systems and Π-λ theorem -/
/-- A Dynkin system is a collection of subsets of a type `α` that contains the empty set,
is closed under complementation and under countable union of pairwise disjoint sets.
The disjointness condition is the only difference with `σ`-algebras.
The main purpose of Dynkin systems is to provide a powerful induction rule for σ-algebras
generated by a collection of sets which is stable under intersection.
A Dynkin system is also known as a "λ-system" or a "d-system".
-/
structure dynkin_system (α : Type*) :=
(has : set α → Prop)
(has_empty : has ∅)
(has_compl : ∀ {a}, has a → has aᶜ)
(has_Union_nat : ∀ {f : ℕ → set α}, pairwise (disjoint on f) → (∀ i, has (f i)) → has (⋃ i, f i))
namespace dynkin_system
@[ext] lemma ext : ∀ {d₁ d₂ : dynkin_system α}, (∀ s : set α, d₁.has s ↔ d₂.has s) → d₁ = d₂
| ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h := have s₁ = s₂, from funext $ assume x, propext $ h x,
by subst this
variable (d : dynkin_system α)
lemma has_compl_iff {a} : d.has aᶜ ↔ d.has a :=
⟨λ h, by simpa using d.has_compl h, λ h, d.has_compl h⟩
lemma has_univ : d.has univ :=
by simpa using d.has_compl d.has_empty
lemma has_Union {β} [countable β] {f : β → set α} (hd : pairwise (disjoint on f))
(h : ∀ i, d.has (f i)) : d.has (⋃ i, f i) :=
by { casesI nonempty_encodable β, rw ← encodable.Union_decode₂, exact
d.has_Union_nat (encodable.Union_decode₂_disjoint_on hd)
(λ n, encodable.Union_decode₂_cases d.has_empty h) }
theorem has_union {s₁ s₂ : set α}
(h₁ : d.has s₁) (h₂ : d.has s₂) (h : disjoint s₁ s₂) : d.has (s₁ ∪ s₂) :=
by { rw union_eq_Union, exact
d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 ⟨h₂, h₁⟩) }
lemma has_diff {s₁ s₂ : set α} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₂ ⊆ s₁) : d.has (s₁ \ s₂) :=
begin
apply d.has_compl_iff.1,
simp [diff_eq, compl_inter],
exact d.has_union (d.has_compl h₁) h₂ (disjoint_compl_left.mono_right h),
end
instance : has_le (dynkin_system α) :=
{ le := λ m₁ m₂, m₁.has ≤ m₂.has }
lemma le_def {α} {a b : dynkin_system α} : a ≤ b ↔ a.has ≤ b.has := iff.rfl
instance : partial_order (dynkin_system α) :=
{ le_refl := assume a b, le_rfl,
le_trans := assume a b c hab hbc, le_def.mpr (le_trans hab hbc),
le_antisymm := assume a b h₁ h₂, ext $ assume s, ⟨h₁ s, h₂ s⟩,
..dynkin_system.has_le }
/-- Every measurable space (σ-algebra) forms a Dynkin system -/
def of_measurable_space (m : measurable_space α) : dynkin_system α :=
{ has := m.measurable_set',
has_empty := m.measurable_set_empty,
has_compl := m.measurable_set_compl,
has_Union_nat := assume f _ hf, m.measurable_set_Union f hf }
lemma of_measurable_space_le_of_measurable_space_iff {m₁ m₂ : measurable_space α} :
of_measurable_space m₁ ≤ of_measurable_space m₂ ↔ m₁ ≤ m₂ :=
iff.rfl
/-- The least Dynkin system containing a collection of basic sets.
This inductive type gives the underlying collection of sets. -/
inductive generate_has (s : set (set α)) : set α → Prop
| basic : ∀ t ∈ s, generate_has t
| empty : generate_has ∅
| compl : ∀ {a}, generate_has a → generate_has aᶜ
| Union : ∀ {f : ℕ → set α}, pairwise (disjoint on f) →
(∀ i, generate_has (f i)) → generate_has (⋃ i, f i)
lemma generate_has_compl {C : set (set α)} {s : set α} : generate_has C sᶜ ↔ generate_has C s :=
by { refine ⟨_, generate_has.compl⟩, intro h, convert generate_has.compl h, simp }
/-- The least Dynkin system containing a collection of basic sets. -/
def generate (s : set (set α)) : dynkin_system α :=
{ has := generate_has s,
has_empty := generate_has.empty,
has_compl := assume a, generate_has.compl,
has_Union_nat := assume f, generate_has.Union }
lemma generate_has_def {C : set (set α)} : (generate C).has = generate_has C := rfl
instance : inhabited (dynkin_system α) := ⟨generate univ⟩
/-- If a Dynkin system is closed under binary intersection, then it forms a `σ`-algebra. -/
def to_measurable_space (h_inter : ∀ s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) :=
{ measurable_space .
measurable_set' := d.has,
measurable_set_empty := d.has_empty,
measurable_set_compl := assume s h, d.has_compl h,
measurable_set_Union := λ f hf,
begin
rw ←Union_disjointed,
exact d.has_Union (disjoint_disjointed _)
(λ n, disjointed_rec (λ t i h, h_inter _ _ h $ d.has_compl $ hf i) (hf n)),
end }
lemma of_measurable_space_to_measurable_space
(h_inter : ∀ s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) :
of_measurable_space (d.to_measurable_space h_inter) = d :=
ext $ assume s, iff.rfl
/-- If `s` is in a Dynkin system `d`, we can form the new Dynkin system `{s ∩ t | t ∈ d}`. -/
def restrict_on {s : set α} (h : d.has s) : dynkin_system α :=
{ has := λ t, d.has (t ∩ s),
has_empty := by simp [d.has_empty],
has_compl := assume t hts,
have tᶜ ∩ s = ((t ∩ s)ᶜ) \ sᶜ,
from set.ext $ assume x, by { by_cases x ∈ s; simp [h] },
by { rw [this], exact d.has_diff (d.has_compl hts) (d.has_compl h)
(compl_subset_compl.mpr $ inter_subset_right _ _) },
has_Union_nat := assume f hd hf,
begin
rw [Union_inter],
refine d.has_Union_nat _ hf,
exact hd.mono (λ i j,
disjoint.mono (inter_subset_left _ _) (inter_subset_left _ _)),
end }
lemma generate_le {s : set (set α)} (h : ∀ t ∈ s, d.has t) : generate s ≤ d :=
λ t ht, ht.rec_on h d.has_empty
(assume a _ h, d.has_compl h)
(assume f hd _ hf, d.has_Union hd hf)
lemma generate_has_subset_generate_measurable {C : set (set α)} {s : set α}
(hs : (generate C).has s) : measurable_set[generate_from C] s :=
generate_le (of_measurable_space (generate_from C)) (λ t, measurable_set_generate_from) s hs
lemma generate_inter {s : set (set α)}
(hs : is_pi_system s) {t₁ t₂ : set α}
(ht₁ : (generate s).has t₁) (ht₂ : (generate s).has t₂) : (generate s).has (t₁ ∩ t₂) :=
have generate s ≤ (generate s).restrict_on ht₂,
from generate_le _ $ assume s₁ hs₁,
have (generate s).has s₁, from generate_has.basic s₁ hs₁,
have generate s ≤ (generate s).restrict_on this,
from generate_le _ $ assume s₂ hs₂,
show (generate s).has (s₂ ∩ s₁), from
(s₂ ∩ s₁).eq_empty_or_nonempty.elim
(λ h, h.symm ▸ generate_has.empty)
(λ h, generate_has.basic _ $ hs _ hs₂ _ hs₁ h),
have (generate s).has (t₂ ∩ s₁), from this _ ht₂,
show (generate s).has (s₁ ∩ t₂), by rwa [inter_comm],
this _ ht₁
/--
**Dynkin's π-λ theorem**:
Given a collection of sets closed under binary intersections, then the Dynkin system it
generates is equal to the σ-algebra it generates.
This result is known as the π-λ theorem.
A collection of sets closed under binary intersection is called a π-system (often requiring
additionnally that is is non-empty, but we drop this condition in the formalization).
-/
lemma generate_from_eq {s : set (set α)} (hs : is_pi_system s) :
generate_from s = (generate s).to_measurable_space (λ t₁ t₂, generate_inter hs) :=
le_antisymm
(generate_from_le $ assume t ht, generate_has.basic t ht)
(of_measurable_space_le_of_measurable_space_iff.mp $
by { rw [of_measurable_space_to_measurable_space],
exact (generate_le _ $ assume t ht, measurable_set_generate_from ht) })
end dynkin_system
theorem induction_on_inter {C : set α → Prop} {s : set (set α)} [m : measurable_space α]
(h_eq : m = generate_from s) (h_inter : is_pi_system s)
(h_empty : C ∅) (h_basic : ∀ t ∈ s, C t) (h_compl : ∀ t, measurable_set t → C t → C tᶜ)
(h_union : ∀ f : ℕ → set α, pairwise (disjoint on f) →
(∀ i, measurable_set (f i)) → (∀ i, C (f i)) → C (⋃ i, f i)) :
∀ ⦃t⦄, measurable_set t → C t :=
have eq : measurable_set = dynkin_system.generate_has s,
by { rw [h_eq, dynkin_system.generate_from_eq h_inter], refl },
assume t ht,
have dynkin_system.generate_has s t, by rwa [eq] at ht,
this.rec_on h_basic h_empty
(assume t ht, h_compl t $ by { rw [eq], exact ht })
(assume f hf ht, h_union f hf $ assume i, by { rw [eq], exact ht _ })
end measurable_space
|
14be6b6aaa496d9f7fda98636b8160bf1aa7ab9e | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/algebra/linear_ordered_comm_group_with_zero.lean | 1a3a2a19f91ee656bb7ad91ef5a44bf4d6a83ce3 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,633 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Johan Commelin, Patrick Massot
-/
import algebra.ordered_group
import algebra.group_with_zero
import algebra.group_with_zero.power
import tactic.abel
/-!
# Linearly ordered commutative groups and monoids with a zero element adjoined
This file sets up a special class of linearly ordered commutative monoids
that show up as the target of so-called “valuations” in algebraic number theory.
Usually, in the informal literature, these objects are constructed
by taking a linearly ordered commutative group Γ and formally adjoining a zero element: Γ ∪ {0}.
The disadvantage is that a type such as `nnreal` is not of that form,
whereas it is a very common target for valuations.
The solutions is to use a typeclass, and that is exactly what we do in this file.
Note that to avoid issues with import cycles, `linear_ordered_comm_monoid_with_zero` is defined
in another file. However, the lemmas about it are stated here.
-/
set_option old_structure_cmd true
/-- A linearly ordered commutative group with a zero element. -/
class linear_ordered_comm_group_with_zero (α : Type*)
extends linear_ordered_comm_monoid_with_zero α, comm_group_with_zero α
variables {α : Type*}
variables {a b c d x y z : α}
instance [linear_ordered_add_comm_monoid_with_top α] :
linear_ordered_comm_monoid_with_zero (multiplicative (order_dual α)) :=
{ zero := multiplicative.of_add (⊤ : α),
zero_mul := top_add,
mul_zero := add_top,
zero_le_one := (le_top : (0 : α) ≤ ⊤),
..multiplicative.ordered_comm_monoid,
..multiplicative.linear_order }
instance [linear_ordered_add_comm_group_with_top α] :
linear_ordered_comm_group_with_zero (multiplicative (order_dual α)) :=
{ inv_zero := linear_ordered_add_comm_group_with_top.neg_top,
mul_inv_cancel := linear_ordered_add_comm_group_with_top.add_neg_cancel,
..multiplicative.div_inv_monoid,
..multiplicative.linear_ordered_comm_monoid_with_zero,
..multiplicative.nontrivial }
section linear_ordered_comm_monoid
variables [linear_ordered_comm_monoid_with_zero α]
/-
The following facts are true more generally in a (linearly) ordered commutative monoid.
-/
/-- Pullback a `linear_ordered_comm_monoid_with_zero` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.linear_ordered_comm_monoid_with_zero {β : Type*}
[has_zero β] [has_one β] [has_mul β]
(f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) :
linear_ordered_comm_monoid_with_zero β :=
{ zero_le_one := show f 0 ≤ f 1, by simp only [zero, one,
linear_ordered_comm_monoid_with_zero.zero_le_one],
..linear_order.lift f hf,
..hf.ordered_comm_monoid f one mul,
..hf.comm_monoid_with_zero f zero one mul }
lemma zero_le_one' : (0 : α) ≤ 1 :=
linear_ordered_comm_monoid_with_zero.zero_le_one
@[simp] lemma zero_le' : 0 ≤ a :=
by simpa only [mul_zero, mul_one] using mul_le_mul_left' (@zero_le_one' α _) a
@[simp] lemma not_lt_zero' : ¬a < 0 :=
not_lt_of_le zero_le'
@[simp] lemma le_zero_iff : a ≤ 0 ↔ a = 0 :=
⟨λ h, le_antisymm h zero_le', λ h, h ▸ le_refl _⟩
lemma zero_lt_iff : 0 < a ↔ a ≠ 0 :=
⟨ne_of_gt, λ h, lt_of_le_of_ne zero_le' h.symm⟩
lemma ne_zero_of_lt (h : b < a) : a ≠ 0 :=
λ h1, not_lt_zero' $ show b < 0, from h1 ▸ h
lemma pow_pos_iff [no_zero_divisors α] {n : ℕ} (hn : 0 < n) : 0 < a ^ n ↔ 0 < a :=
by simp_rw [zero_lt_iff, pow_ne_zero_iff hn]
instance : linear_ordered_add_comm_monoid_with_top (additive (order_dual α)) :=
{ top := (0 : α),
top_add' := λ a, (zero_mul a : (0 : α) * a = 0),
le_top := λ _, zero_le',
..additive.ordered_add_comm_monoid,
..additive.linear_order }
end linear_ordered_comm_monoid
variables [linear_ordered_comm_group_with_zero α]
lemma zero_lt_one'' : (0 : α) < 1 :=
lt_of_le_of_ne zero_le_one' zero_ne_one
lemma le_of_le_mul_right (h : c ≠ 0) (hab : a * c ≤ b * c) : a ≤ b :=
by simpa only [mul_inv_cancel_right' h] using (mul_le_mul_right' hab c⁻¹)
lemma le_mul_inv_of_mul_le (h : c ≠ 0) (hab : a * c ≤ b) : a ≤ b * c⁻¹ :=
le_of_le_mul_right h (by simpa [h] using hab)
lemma mul_inv_le_of_le_mul (h : c ≠ 0) (hab : a ≤ b * c) : a * c⁻¹ ≤ b :=
le_of_le_mul_right h (by simpa [h] using hab)
lemma div_le_div' (a b c d : α) (hb : b ≠ 0) (hd : d ≠ 0) :
a * b⁻¹ ≤ c * d⁻¹ ↔ a * d ≤ c * b :=
if ha : a = 0 then by simp [ha] else
if hc : c = 0 then by simp [inv_ne_zero hb, hc, hd] else
show (units.mk0 a ha) * (units.mk0 b hb)⁻¹ ≤ (units.mk0 c hc) * (units.mk0 d hd)⁻¹ ↔
(units.mk0 a ha) * (units.mk0 d hd) ≤ (units.mk0 c hc) * (units.mk0 b hb),
from mul_inv_le_mul_inv_iff'
@[simp] lemma units.zero_lt (u : units α) : (0 : α) < u :=
zero_lt_iff.2 $ u.ne_zero
lemma mul_lt_mul'''' (hab : a < b) (hcd : c < d) : a * c < b * d :=
have hb : b ≠ 0 := ne_zero_of_lt hab,
have hd : d ≠ 0 := ne_zero_of_lt hcd,
if ha : a = 0 then by { rw [ha, zero_mul, zero_lt_iff], exact mul_ne_zero hb hd } else
if hc : c = 0 then by { rw [hc, mul_zero, zero_lt_iff], exact mul_ne_zero hb hd } else
show (units.mk0 a ha) * (units.mk0 c hc) < (units.mk0 b hb) * (units.mk0 d hd),
from mul_lt_mul''' hab hcd
lemma mul_inv_lt_of_lt_mul' (h : x < y * z) : x * z⁻¹ < y :=
have hz : z ≠ 0 := (mul_ne_zero_iff.1 $ ne_zero_of_lt h).2,
by { contrapose! h, simpa only [inv_inv'] using mul_inv_le_of_le_mul (inv_ne_zero hz) h }
lemma mul_lt_right' (c : α) (h : a < b) (hc : c ≠ 0) : a * c < b * c :=
by { contrapose! h, exact le_of_le_mul_right hc h }
lemma pow_lt_pow_succ {x : α} {n : ℕ} (hx : 1 < x) : x ^ n < x ^ n.succ :=
by { rw [← one_mul (x ^ n), pow_succ],
exact mul_lt_right' _ hx (pow_ne_zero _ $ ne_of_gt (lt_trans zero_lt_one'' hx)) }
lemma pow_lt_pow' {x : α} {m n : ℕ} (hx : 1 < x) (hmn : m < n) : x ^ m < x ^ n :=
by { induction hmn with n hmn ih, exacts [pow_lt_pow_succ hx, lt_trans ih (pow_lt_pow_succ hx)] }
lemma inv_lt_inv'' (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ < b⁻¹ ↔ b < a :=
show (units.mk0 a ha)⁻¹ < (units.mk0 b hb)⁻¹ ↔ (units.mk0 b hb) < (units.mk0 a ha),
from inv_lt_inv_iff
lemma inv_le_inv'' (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a :=
show (units.mk0 a ha)⁻¹ ≤ (units.mk0 b hb)⁻¹ ↔ (units.mk0 b hb) ≤ (units.mk0 a ha),
from inv_le_inv_iff
instance : linear_ordered_add_comm_group_with_top (additive (order_dual α)) :=
{ neg_top := inv_zero,
add_neg_cancel := λ a ha, mul_inv_cancel ha,
..additive.sub_neg_monoid,
..additive.linear_ordered_add_comm_monoid_with_top,
..additive.nontrivial }
namespace monoid_hom
variables {R : Type*} [ring R] (f : R →* α)
theorem map_neg_one : f (-1) = 1 :=
(pow_eq_one_iff (nat.succ_ne_zero 1)).1 $
calc f (-1) ^ 2 = f (-1) * f(-1) : sq _
... = f ((-1) * - 1) : (f.map_mul _ _).symm
... = f ( - - 1) : congr_arg _ (neg_one_mul _)
... = f 1 : congr_arg _ (neg_neg _)
... = 1 : map_one f
@[simp] lemma map_neg (x : R) : f (-x) = f x :=
calc f (-x) = f (-1 * x) : congr_arg _ (neg_one_mul _).symm
... = f (-1) * f x : map_mul _ _ _
... = 1 * f x : _root_.congr_arg (λ g, g * (f x)) (map_neg_one f)
... = f x : one_mul _
lemma map_sub_swap (x y : R) : f (x - y) = f (y - x) :=
calc f (x - y) = f (-(y - x)) : congr_arg _ (neg_sub _ _).symm
... = _ : map_neg _ _
end monoid_hom
|
c26864783dbdeaab579292931b3a31e41e8e246f | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /tests/lean/run/kernel2.lean | 367c2f280738b6d9b851834b48ab424d2781cad4 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 1,160 | lean | import Lean
open Lean
def checkDefEq (a b : Name) : CoreM Unit := do
let env ← getEnv;
let a := mkConst a;
let b := mkConst b;
let r := Kernel.isDefEq env {} a b;
IO.println (toString a ++ " =?= " ++ toString b ++ " := " ++ toString r)
def whnf (a : Name) : CoreM Unit := do
let env ← getEnv;
let a := mkConst a;
let r := Kernel.whnf env {} a;
IO.println (toString a ++ " ==> " ++ toString r)
partial def fact : Nat → Nat
| 0 => 1
| (n+1) => (n+1)*fact n
def c1 := 30000000000 + 10000000000
def c2 := 40000000000
def c3 := fact 10
def v1 := 3628800
def v2 := 3628801
#eval whnf `c3
#eval checkDefEq `c3 `v1
#eval checkDefEq `c3 `v2
#eval checkDefEq `c1 `c2
def c4 := decide (100000000 < 20000000000)
#eval whnf `c4
#eval checkDefEq `c4 `Bool.true
def c5 := "h".length
def c6 := 1
set_option pp.all true
#eval whnf `c5
#eval checkDefEq `c5 `c6
def c7 := decide ("hello" = "world")
#eval whnf `c7
def c8 := "hello".length
#eval whnf `c8
def c9 : String := "hello" ++ "world"
def c10 : String := "helloworld"
#eval checkDefEq `c9 `c10
def c11 : Bool := decide ('a' = 'b')
#eval whnf `c11
def c12 : Nat := 'a'.toNat
#eval whnf `c12
|
ba6699351406f95743d977d53166e37faaa1a7aa | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/algebra/big_operators/default.lean | 88af55b7f337088e72a557ce91d6ba5bc7a5222a | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 308 | lean | -- Import this file to pull in everything about "big operators".
-- When preparing a contribution to mathlib, it is best to minimize the imports you use.
import algebra.big_operators.order
import algebra.big_operators.intervals
import algebra.big_operators.ring
import algebra.big_operators.nat_antidiagonal
|
2b5e43c267592d241607d2df87de391155b0b04b | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/topology/metric_space/gluing.lean | e767959abaf32bc17e14eaaef8283cf690ceeb48 | [
"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 | 23,984 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Gluing metric spaces
Authors: Sébastien Gouëzel
-/
import topology.metric_space.isometry topology.metric_space.premetric_space
/-!
# Metric space gluing
Gluing two metric spaces along a common subset. Formally, we are given
```
Φ
γ ---> α
|
|Ψ
v
β
```
where `hΦ : isometry Φ` and `hΨ : isometry Ψ`.
We want to complete the square by a space `glue_space hΦ hΨ` and two isometries
`to_glue_l hΦ hΨ` and `to_glue_r hΦ hΨ` that make the square commute.
We start by defining a predistance on the disjoint union `α ⊕ β`, for which
points `Φ p` and `Ψ p` are at distance 0. The (quotient) metric space associated
to this predistance is the desired space.
This is an instance of a more general construction, where `Φ` and `Ψ` do not have to be isometries,
but the distances in the image almost coincide, up to `2ε` say. Then one can almost glue the two
spaces so that the images of a point under `Φ` and `Ψ` are ε-close. If `ε > 0`, this yields a
metric space structure on `α ⊕ β`, without the need to take a quotient. In particular, when
`α` and `β` are inhabited, this gives a natural metric space structure on `α ⊕ β`, where the basepoints
are at distance 1, say, and the distances between other points are obtained by going through the
two basepoints.
We also define the inductive limit of metric spaces. Given
```
f 0 f 1 f 2 f 3
X 0 -----> X 1 -----> X 2 -----> X 3 -----> ...
```
where the `X n` are metric spaces and `f n` isometric embeddings, we define the inductive
limit of the `X n`, also known as the increasing union of the `X n` in this context, if we
identify `X n` and `X (n+1)` through `f n`. This is a metric space in which all `X n` embed
isometrically and in a way compatible with `f n`.
-/
noncomputable theory
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
open function set premetric
namespace metric
section approx_gluing
variables [metric_space α] [metric_space β]
{Φ : γ → α} {Ψ : γ → β} {ε : ℝ}
open sum (inl inr)
/-- Define a predistance on α ⊕ β, for which Φ p and Ψ p are at distance ε -/
def glue_dist (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) : α ⊕ β → α ⊕ β → ℝ
| (inl x) (inl y) := dist x y
| (inr x) (inr y) := dist x y
| (inl x) (inr y) := infi (λp, dist x (Φ p) + dist y (Ψ p)) + ε
| (inr x) (inl y) := infi (λp, dist y (Φ p) + dist x (Ψ p)) + ε
private lemma glue_dist_self (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) : ∀x, glue_dist Φ Ψ ε x x = 0
| (inl x) := dist_self _
| (inr x) := dist_self _
lemma glue_dist_glued_points [nonempty γ] (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (p : γ) :
glue_dist Φ Ψ ε (inl (Φ p)) (inr (Ψ p)) = ε :=
begin
have : infi (λq, dist (Φ p) (Φ q) + dist (Ψ p) (Ψ q)) = 0,
{ have A : ∀q, 0 ≤ dist (Φ p) (Φ q) + dist (Ψ p) (Ψ q) :=
λq, by rw ← add_zero (0 : ℝ); exact add_le_add dist_nonneg dist_nonneg,
refine le_antisymm _ (le_cinfi A),
have : 0 = dist (Φ p) (Φ p) + dist (Ψ p) (Ψ p), by simp,
rw this,
exact cinfi_le ⟨0, forall_range_iff.2 A⟩ },
rw [glue_dist, this, zero_add]
end
private lemma glue_dist_comm (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) :
∀x y, glue_dist Φ Ψ ε x y = glue_dist Φ Ψ ε y x
| (inl x) (inl y) := dist_comm _ _
| (inr x) (inr y) := dist_comm _ _
| (inl x) (inr y) := rfl
| (inr x) (inl y) := rfl
variable [nonempty γ]
private lemma glue_dist_triangle (Φ : γ → α) (Ψ : γ → β) (ε : ℝ)
(H : ∀p q, abs (dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)) ≤ 2 * ε) :
∀x y z, glue_dist Φ Ψ ε x z ≤ glue_dist Φ Ψ ε x y + glue_dist Φ Ψ ε y z
| (inl x) (inl y) (inl z) := dist_triangle _ _ _
| (inr x) (inr y) (inr z) := dist_triangle _ _ _
| (inr x) (inl y) (inl z) := begin
have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) :=
λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩,
unfold glue_dist,
have : infi (λp, dist z (Φ p) + dist x (Ψ p)) ≤ infi (λp, dist y (Φ p) + dist x (Ψ p)) + dist y z,
{ have : infi (λp, dist y (Φ p) + dist x (Ψ p)) + dist y z =
infi ((λt, t + dist y z) ∘ (λp, dist y (Φ p) + dist x (Ψ p))),
{ refine cinfi_of_cinfi_of_monotone_of_continuous (_ : continuous (λt, t + dist y z)) _ (B _ _),
exact continuous_id.add continuous_const,
exact λx y hx, by simpa },
rw [this, comp],
refine cinfi_le_cinfi (B _ _) (λp, _),
calc
dist z (Φ p) + dist x (Ψ p) ≤ (dist y z + dist y (Φ p)) + dist x (Ψ p) :
add_le_add (dist_triangle_left _ _ _) (le_refl _)
... = dist y (Φ p) + dist x (Ψ p) + dist y z : by ring },
linarith
end
| (inr x) (inr y) (inl z) := begin
have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) :=
λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩,
unfold glue_dist,
have : infi (λp, dist z (Φ p) + dist x (Ψ p)) ≤ dist x y + infi (λp, dist z (Φ p) + dist y (Ψ p)),
{ have : dist x y + infi (λp, dist z (Φ p) + dist y (Ψ p)) =
infi ((λt, dist x y + t) ∘ (λp, dist z (Φ p) + dist y (Ψ p))),
{ refine cinfi_of_cinfi_of_monotone_of_continuous (_ : continuous (λt, dist x y + t)) _ (B _ _),
exact continuous_const.add continuous_id,
exact λx y hx, by simpa },
rw [this, comp],
refine cinfi_le_cinfi (B _ _) (λp, _),
calc
dist z (Φ p) + dist x (Ψ p) ≤ dist z (Φ p) + (dist x y + dist y (Ψ p)) :
add_le_add (le_refl _) (dist_triangle _ _ _)
... = dist x y + (dist z (Φ p) + dist y (Ψ p)) : by ring },
linarith
end
| (inl x) (inl y) (inr z) := begin
have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) :=
λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩,
unfold glue_dist,
have : infi (λp, dist x (Φ p) + dist z (Ψ p)) ≤ dist x y + infi (λp, dist y (Φ p) + dist z (Ψ p)),
{ have : dist x y + infi (λp, dist y (Φ p) + dist z (Ψ p)) =
infi ((λt, dist x y + t) ∘ (λp, dist y (Φ p) + dist z (Ψ p))),
{ refine cinfi_of_cinfi_of_monotone_of_continuous ( _ : continuous (λt, dist x y + t)) _ (B _ _),
exact continuous_const.add continuous_id,
exact λx y hx, by simpa },
rw [this, comp],
refine cinfi_le_cinfi (B _ _) (λp, _),
calc
dist x (Φ p) + dist z (Ψ p) ≤ (dist x y + dist y (Φ p)) + dist z (Ψ p) :
add_le_add (dist_triangle _ _ _) (le_refl _)
... = dist x y + (dist y (Φ p) + dist z (Ψ p)) : by ring },
linarith
end
| (inl x) (inr y) (inr z) := begin
have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) :=
λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩,
unfold glue_dist,
have : infi (λp, dist x (Φ p) + dist z (Ψ p)) ≤ infi (λp, dist x (Φ p) + dist y (Ψ p)) + dist y z,
{ have : infi (λp, dist x (Φ p) + dist y (Ψ p)) + dist y z =
infi ((λt, t + dist y z) ∘ (λp, dist x (Φ p) + dist y (Ψ p))),
{ refine cinfi_of_cinfi_of_monotone_of_continuous (_ : continuous (λt, t + dist y z)) _ (B _ _),
exact continuous_id.add continuous_const,
exact λx y hx, by simpa },
rw [this, comp],
refine cinfi_le_cinfi (B _ _) (λp, _),
calc
dist x (Φ p) + dist z (Ψ p) ≤ dist x (Φ p) + (dist y z + dist y (Ψ p)) :
add_le_add (le_refl _) (dist_triangle_left _ _ _)
... = dist x (Φ p) + dist y (Ψ p) + dist y z : by ring },
linarith
end
| (inl x) (inr y) (inl z) := real.le_of_forall_epsilon_le $ λδ δpos, begin
have : ∃a ∈ range (λp, dist x (Φ p) + dist y (Ψ p)), a < infi (λp, dist x (Φ p) + dist y (Ψ p)) + δ/2 :=
exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith),
rcases this with ⟨a, arange, ha⟩,
rcases mem_range.1 arange with ⟨p, pa⟩,
rw ← pa at ha,
have : ∃b ∈ range (λp, dist z (Φ p) + dist y (Ψ p)), b < infi (λp, dist z (Φ p) + dist y (Ψ p)) + δ/2 :=
exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith),
rcases this with ⟨b, brange, hb⟩,
rcases mem_range.1 brange with ⟨q, qb⟩,
rw ← qb at hb,
have : dist (Φ p) (Φ q) ≤ dist (Ψ p) (Ψ q) + 2 * ε,
{ have := le_trans (le_abs_self _) (H p q), by linarith },
calc dist x z ≤ dist x (Φ p) + dist (Φ p) (Φ q) + dist (Φ q) z : dist_triangle4 _ _ _ _
... ≤ dist x (Φ p) + dist (Ψ p) (Ψ q) + dist z (Φ q) + 2 * ε : by rw [dist_comm z]; linarith
... ≤ dist x (Φ p) + (dist y (Ψ p) + dist y (Ψ q)) + dist z (Φ q) + 2 * ε :
add_le_add (add_le_add (add_le_add (le_refl _) (dist_triangle_left _ _ _)) (le_refl _)) (le_refl _)
... ≤ (infi (λp, dist x (Φ p) + dist y (Ψ p)) + ε) + (infi (λp, dist z (Φ p) + dist y (Ψ p)) + ε) + δ :
by linarith
end
| (inr x) (inl y) (inr z) := real.le_of_forall_epsilon_le $ λδ δpos, begin
have : ∃a ∈ range (λp, dist y (Φ p) + dist x (Ψ p)), a < infi (λp, dist y (Φ p) + dist x (Ψ p)) + δ/2 :=
exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith),
rcases this with ⟨a, arange, ha⟩,
rcases mem_range.1 arange with ⟨p, pa⟩,
rw ← pa at ha,
have : ∃b ∈ range (λp, dist y (Φ p) + dist z (Ψ p)), b < infi (λp, dist y (Φ p) + dist z (Ψ p)) + δ/2 :=
exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith),
rcases this with ⟨b, brange, hb⟩,
rcases mem_range.1 brange with ⟨q, qb⟩,
rw ← qb at hb,
have : dist (Ψ p) (Ψ q) ≤ dist (Φ p) (Φ q) + 2 * ε,
{ have := le_trans (neg_le_abs_self _) (H p q), by linarith },
calc dist x z ≤ dist x (Ψ p) + dist (Ψ p) (Ψ q) + dist (Ψ q) z : dist_triangle4 _ _ _ _
... ≤ dist x (Ψ p) + dist (Φ p) (Φ q) + dist z (Ψ q) + 2 * ε : by rw [dist_comm z]; linarith
... ≤ dist x (Ψ p) + (dist y (Φ p) + dist y (Φ q)) + dist z (Ψ q) + 2 * ε :
add_le_add (add_le_add (add_le_add (le_refl _) (dist_triangle_left _ _ _)) (le_refl _)) (le_refl _)
... ≤ (infi (λp, dist y (Φ p) + dist x (Ψ p)) + ε) + (infi (λp, dist y (Φ p) + dist z (Ψ p)) + ε) + δ :
by linarith
end
private lemma glue_eq_of_dist_eq_zero (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (ε0 : 0 < ε) :
∀p q : α ⊕ β, glue_dist Φ Ψ ε p q = 0 → p = q
| (inl x) (inl y) h := by rw eq_of_dist_eq_zero h
| (inl x) (inr y) h := begin
have : 0 ≤ infi (λp, dist x (Φ p) + dist y (Ψ p)) :=
le_cinfi (λp, by simpa using add_le_add (@dist_nonneg _ _ x _) (@dist_nonneg _ _ y _)),
have : 0 + ε ≤ glue_dist Φ Ψ ε (inl x) (inr y) := add_le_add this (le_refl ε),
exfalso,
linarith
end
| (inr x) (inl y) h := begin
have : 0 ≤ infi (λp, dist y (Φ p) + dist x (Ψ p)) :=
le_cinfi (λp, by simpa [add_comm]
using add_le_add (@dist_nonneg _ _ x _) (@dist_nonneg _ _ y _)),
have : 0 + ε ≤ glue_dist Φ Ψ ε (inr x) (inl y) := add_le_add this (le_refl ε),
exfalso,
linarith
end
| (inr x) (inr y) h := by rw eq_of_dist_eq_zero h
/-- Given two maps Φ and Ψ intro metric spaces α and β such that the distances between Φ p and Φ q,
and between Ψ p and Ψ q, coincide up to 2 ε where ε > 0, one can almost glue the two spaces α
and β along the images of Φ and Ψ, so that Φ p and Ψ p are at distance ε. -/
def glue_metric_approx (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (ε0 : 0 < ε)
(H : ∀p q, abs (dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)) ≤ 2 * ε) : metric_space (α ⊕ β) :=
{ dist := glue_dist Φ Ψ ε,
dist_self := glue_dist_self Φ Ψ ε,
dist_comm := glue_dist_comm Φ Ψ ε,
dist_triangle := glue_dist_triangle Φ Ψ ε H,
eq_of_dist_eq_zero := glue_eq_of_dist_eq_zero Φ Ψ ε ε0 }
end approx_gluing
section sum
/- A particular case of the previous construction is when one uses basepoints in α and β and one
glues only along the basepoints, putting them at distance 1. We give a direct definition of
the distance, without infi, as it is easier to use in applications, and show that it is equal to
the gluing distance defined above to take advantage of the lemmas we have already proved. -/
variables [metric_space α] [metric_space β] [inhabited α] [inhabited β]
open sum (inl inr)
/- Distance on a disjoint union. There are many (noncanonical) ways to put a distance compatible
with each factor.
If the two spaces are bounded, one can say for instance that each point in the first is at distance
`diam α + diam β + 1` of each point in the second.
Instead, we choose a construction that works for unbounded spaces, but requires basepoints.
We embed isometrically each factor, set the basepoints at distance 1,
arbitrarily, and say that the distance from `a` to `b` is the sum of the distances of `a` and `b` to
their respective basepoints, plus the distance 1 between the basepoints.
Since there is an arbitrary choice in this construction, it is not an instance by default. -/
def sum.dist : α ⊕ β → α ⊕ β → ℝ
| (inl a) (inl a') := dist a a'
| (inr b) (inr b') := dist b b'
| (inl a) (inr b) := dist a (default α) + 1 + dist (default β) b
| (inr b) (inl a) := dist b (default β) + 1 + dist (default α) a
lemma sum.dist_eq_glue_dist {p q : α ⊕ β} :
sum.dist p q = glue_dist (λ_ : unit, default α) (λ_ : unit, default β) 1 p q :=
by cases p; cases q; refl <|> simp [sum.dist, glue_dist, dist_comm, add_comm, add_left_comm]
private lemma sum.dist_comm (x y : α ⊕ β) : sum.dist x y = sum.dist y x :=
by cases x; cases y; simp only [sum.dist, dist_comm, add_comm, add_left_comm]
lemma sum.one_dist_le {x : α} {y : β} : 1 ≤ sum.dist (inl x) (inr y) :=
le_trans (le_add_of_nonneg_right dist_nonneg) $
add_le_add_right (le_add_of_nonneg_left dist_nonneg) _
lemma sum.one_dist_le' {x : α} {y : β} : 1 ≤ sum.dist (inr y) (inl x) :=
by rw sum.dist_comm; exact sum.one_dist_le
private lemma sum.mem_uniformity (s : set ((α ⊕ β) × (α ⊕ β))) :
s ∈ (@uniformity (α ⊕ β) _).sets ↔ ∃ ε > 0, ∀ a b, sum.dist a b < ε → (a, b) ∈ s :=
begin
split,
{ rintro ⟨hsα, hsβ⟩,
rcases mem_uniformity_dist.1 hsα with ⟨εα, εα0, hα⟩,
rcases mem_uniformity_dist.1 hsβ with ⟨εβ, εβ0, hβ⟩,
refine ⟨min (min εα εβ) 1, lt_min (lt_min εα0 εβ0) zero_lt_one, _⟩,
rintro (a|a) (b|b) h,
{ exact hα (lt_of_lt_of_le h (le_trans (min_le_left _ _) (min_le_left _ _))) },
{ cases not_le_of_lt (lt_of_lt_of_le h (min_le_right _ _)) sum.one_dist_le },
{ cases not_le_of_lt (lt_of_lt_of_le h (min_le_right _ _)) sum.one_dist_le' },
{ exact hβ (lt_of_lt_of_le h (le_trans (min_le_left _ _) (min_le_right _ _))) } },
{ rintro ⟨ε, ε0, H⟩,
split; rw [filter.mem_map, mem_uniformity_dist];
exact ⟨ε, ε0, λ x y h, H _ _ (by exact h)⟩ }
end
/-- The distance on the disjoint union indeed defines a metric space. All the distance properties follow from our
choice of the distance. The harder work is to show that the uniform structure defined by the distance coincides
with the disjoint union uniform structure. -/
def metric_space_sum : metric_space (α ⊕ β) :=
{ dist := sum.dist,
dist_self := λx, by cases x; simp only [sum.dist, dist_self],
dist_comm := sum.dist_comm,
dist_triangle := λp q r,
by simp only [dist, sum.dist_eq_glue_dist]; exact glue_dist_triangle _ _ _ (by simp; norm_num) _ _ _,
eq_of_dist_eq_zero := λp q,
by simp only [dist, sum.dist_eq_glue_dist]; exact glue_eq_of_dist_eq_zero _ _ _ zero_lt_one _ _,
to_uniform_space := sum.uniform_space,
uniformity_dist := uniformity_dist_of_mem_uniformity _ _ sum.mem_uniformity }
local attribute [instance] metric_space_sum
lemma sum.dist_eq {x y : α ⊕ β} : dist x y = sum.dist x y := rfl
/-- The left injection of a space in a disjoint union in an isometry -/
lemma isometry_on_inl : isometry (sum.inl : α → (α ⊕ β)) :=
isometry_emetric_iff_metric.2 $ λx y, rfl
/-- The right injection of a space in a disjoint union in an isometry -/
lemma isometry_on_inr : isometry (sum.inr : β → (α ⊕ β)) :=
isometry_emetric_iff_metric.2 $ λx y, rfl
end sum
section gluing
/- Exact gluing of two metric spaces along isometric subsets. -/
variables [nonempty γ] [metric_space γ] [metric_space α] [metric_space β]
{Φ : γ → α} {Ψ : γ → β} {ε : ℝ}
open sum (inl inr)
local attribute [instance] premetric.dist_setoid
def glue_premetric (hΦ : isometry Φ) (hΨ : isometry Ψ) : premetric_space (α ⊕ β) :=
{ dist := glue_dist Φ Ψ 0,
dist_self := glue_dist_self Φ Ψ 0,
dist_comm := glue_dist_comm Φ Ψ 0,
dist_triangle := glue_dist_triangle Φ Ψ 0 $ λp q, by rw [hΦ.dist_eq, hΨ.dist_eq]; simp }
def glue_space (hΦ : isometry Φ) (hΨ : isometry Ψ) : Type* :=
@metric_quot _ (glue_premetric hΦ hΨ)
instance metric_space_glue_space (hΦ : isometry Φ) (hΨ : isometry Ψ) :
metric_space (glue_space hΦ hΨ) :=
@premetric.metric_space_quot _ (glue_premetric hΦ hΨ)
def to_glue_l (hΦ : isometry Φ) (hΨ : isometry Ψ) (x : α) : glue_space hΦ hΨ :=
by letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ; exact ⟦inl x⟧
def to_glue_r (hΦ : isometry Φ) (hΨ : isometry Ψ) (y : β) : glue_space hΦ hΨ :=
by letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ; exact ⟦inr y⟧
instance inhabited_left (hΦ : isometry Φ) (hΨ : isometry Ψ) [inhabited α] :
inhabited (glue_space hΦ hΨ) :=
⟨to_glue_l _ _ (default _)⟩
instance inhabited_right (hΦ : isometry Φ) (hΨ : isometry Ψ) [inhabited β] :
inhabited (glue_space hΦ hΨ) :=
⟨to_glue_r _ _ (default _)⟩
lemma to_glue_commute (hΦ : isometry Φ) (hΨ : isometry Ψ) :
(to_glue_l hΦ hΨ) ∘ Φ = (to_glue_r hΦ hΨ) ∘ Ψ :=
begin
letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ,
funext,
simp only [comp, to_glue_l, to_glue_r, quotient.eq],
exact glue_dist_glued_points Φ Ψ 0 x
end
lemma to_glue_l_isometry (hΦ : isometry Φ) (hΨ : isometry Ψ) : isometry (to_glue_l hΦ hΨ) :=
isometry_emetric_iff_metric.2 $ λ_ _, rfl
lemma to_glue_r_isometry (hΦ : isometry Φ) (hΨ : isometry Ψ) : isometry (to_glue_r hΦ hΨ) :=
isometry_emetric_iff_metric.2 $ λ_ _, rfl
end gluing --section
section inductive_limit
/- In this section, we define the inductive limit of
f 0 f 1 f 2 f 3
X 0 -----> X 1 -----> X 2 -----> X 3 -----> ...
where the X n are metric spaces and f n isometric embeddings. We do it by defining a premetric
space structure on Σn, X n, where the predistance dist x y is obtained by pushing x and y in a
common X k using composition by the f n, and taking the distance there. This does not depend on
the choice of k as the f n are isometries. The metric space associated to this premetric space
is the desired inductive limit.-/
open nat
variables {X : ℕ → Type u} [∀n, metric_space (X n)] {f : Πn, X n → X (n+1)}
/-- Predistance on the disjoint union Σn, X n. -/
def inductive_limit_dist (f : Πn, X n → X (n+1)) (x y : Σn, X n) : ℝ :=
dist (le_rec_on (le_max_left x.1 y.1) f x.2 : X (max x.1 y.1))
(le_rec_on (le_max_right x.1 y.1) f y.2 : X (max x.1 y.1))
/-- The predistance on the disjoint union Σn, X n can be computed in any X k for large enough k.-/
lemma inductive_limit_dist_eq_dist (I : ∀n, isometry (f n))
(x y : Σn, X n) (m : ℕ) : ∀hx : x.1 ≤ m, ∀hy : y.1 ≤ m,
inductive_limit_dist f x y = dist (le_rec_on hx f x.2 : X m) (le_rec_on hy f y.2 : X m) :=
begin
induction m with m hm,
{ assume hx hy,
have A : max x.1 y.1 = 0, { rw [le_zero_iff_eq.1 hx, le_zero_iff_eq.1 hy], simp },
unfold inductive_limit_dist,
congr; simp only [A] },
{ assume hx hy,
by_cases h : max x.1 y.1 = m.succ,
{ unfold inductive_limit_dist,
congr; simp only [h] },
{ have : max x.1 y.1 ≤ succ m := by simp [hx, hy],
have : max x.1 y.1 ≤ m := by simpa [h] using of_le_succ this,
have xm : x.1 ≤ m := le_trans (le_max_left _ _) this,
have ym : y.1 ≤ m := le_trans (le_max_right _ _) this,
rw [le_rec_on_succ xm, le_rec_on_succ ym, (I m).dist_eq],
exact hm xm ym }}
end
/-- Premetric space structure on Σn, X n.-/
def inductive_premetric (I : ∀n, isometry (f n)) :
premetric_space (Σn, X n) :=
{ dist := inductive_limit_dist f,
dist_self := λx, by simp [dist, inductive_limit_dist],
dist_comm := λx y, begin
let m := max x.1 y.1,
have hx : x.1 ≤ m := le_max_left _ _,
have hy : y.1 ≤ m := le_max_right _ _,
unfold dist,
rw [inductive_limit_dist_eq_dist I x y m hx hy, inductive_limit_dist_eq_dist I y x m hy hx,
dist_comm]
end,
dist_triangle := λx y z, begin
let m := max (max x.1 y.1) z.1,
have hx : x.1 ≤ m := le_trans (le_max_left _ _) (le_max_left _ _),
have hy : y.1 ≤ m := le_trans (le_max_right _ _) (le_max_left _ _),
have hz : z.1 ≤ m := le_max_right _ _,
calc inductive_limit_dist f x z
= dist (le_rec_on hx f x.2 : X m) (le_rec_on hz f z.2 : X m) :
inductive_limit_dist_eq_dist I x z m hx hz
... ≤ dist (le_rec_on hx f x.2 : X m) (le_rec_on hy f y.2 : X m)
+ dist (le_rec_on hy f y.2 : X m) (le_rec_on hz f z.2 : X m) :
dist_triangle _ _ _
... = inductive_limit_dist f x y + inductive_limit_dist f y z :
by rw [inductive_limit_dist_eq_dist I x y m hx hy,
inductive_limit_dist_eq_dist I y z m hy hz]
end }
local attribute [instance] inductive_premetric premetric.dist_setoid
/-- The type giving the inductive limit in a metric space context. -/
def inductive_limit (I : ∀n, isometry (f n)) : Type* :=
@metric_quot _ (inductive_premetric I)
/-- Metric space structure on the inductive limit. -/
instance metric_space_inductive_limit (I : ∀n, isometry (f n)) :
metric_space (inductive_limit I) :=
@premetric.metric_space_quot _ (inductive_premetric I)
/-- Mapping each `X n` to the inductive limit. -/
def to_inductive_limit (I : ∀n, isometry (f n)) (n : ℕ) (x : X n) : metric.inductive_limit I :=
by letI : premetric_space (Σn, X n) := inductive_premetric I; exact ⟦sigma.mk n x⟧
instance (I : ∀ n, isometry (f n)) [inhabited (X 0)] : inhabited (inductive_limit I) :=
⟨to_inductive_limit _ 0 (default _)⟩
/-- The map `to_inductive_limit n` mapping `X n` to the inductive limit is an isometry. -/
lemma to_inductive_limit_isometry (I : ∀n, isometry (f n)) (n : ℕ) :
isometry (to_inductive_limit I n) := isometry_emetric_iff_metric.2 $ λx y,
begin
change inductive_limit_dist f ⟨n, x⟩ ⟨n, y⟩ = dist x y,
rw [inductive_limit_dist_eq_dist I ⟨n, x⟩ ⟨n, y⟩ n (le_refl n) (le_refl n),
le_rec_on_self, le_rec_on_self]
end
/-- The maps `to_inductive_limit n` are compatible with the maps `f n`. -/
lemma to_inductive_limit_commute (I : ∀n, isometry (f n)) (n : ℕ) :
(to_inductive_limit I n.succ) ∘ (f n) = to_inductive_limit I n :=
begin
funext,
simp only [comp, to_inductive_limit, quotient.eq],
show inductive_limit_dist f ⟨n.succ, f n x⟩ ⟨n, x⟩ = 0,
{ rw [inductive_limit_dist_eq_dist I ⟨n.succ, f n x⟩ ⟨n, x⟩ n.succ,
le_rec_on_self, le_rec_on_succ, le_rec_on_self, dist_self],
exact le_refl _,
exact le_refl _,
exact le_succ _ }
end
end inductive_limit --section
end metric --namespace
|
58c7234514236ea98084d3056881381f29eaf61f | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/monoidal/free/coherence.lean | 8aab1c0a2e6f7fdf64b9a71ef28ad2864705c22b | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 12,206 | lean | /-
Copyright (c) 2021 Markus Himmel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Markus Himmel
-/
import category_theory.monoidal.free.basic
import category_theory.groupoid
import category_theory.discrete_category
/-!
# The monoidal coherence theorem
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file, we prove the monoidal coherence theorem, stated in the following form: the free
monoidal category over any type `C` is thin.
We follow a proof described by Ilya Beylin and Peter Dybjer, which has been previously formalized
in the proof assistant ALF. The idea is to declare a normal form (with regard to association and
adding units) on objects of the free monoidal category and consider the discrete subcategory of
objects that are in normal form. A normalization procedure is then just a functor
`full_normalize : free_monoidal_category C ⥤ discrete (normal_monoidal_object C)`, where
functoriality says that two objects which are related by associators and unitors have the
same normal form. Another desirable property of a normalization procedure is that an object is
isomorphic (i.e., related via associators and unitors) to its normal form. In the case of the
specific normalization procedure we use we not only get these isomorphismns, but also that they
assemble into a natural isomorphism `𝟭 (free_monoidal_category C) ≅ full_normalize ⋙ inclusion`.
But this means that any two parallel morphisms in the free monoidal category factor through a
discrete category in the same way, so they must be equal, and hence the free monoidal category
is thin.
## References
* [Ilya Beylin and Peter Dybjer, Extracting a proof of coherence for monoidal categories from a
proof of normalization for monoids][beylin1996]
-/
universe u
namespace category_theory
open monoidal_category
namespace free_monoidal_category
variables {C : Type u}
section
variables (C)
/-- We say an object in the free monoidal category is in normal form if it is of the form
`(((𝟙_ C) ⊗ X₁) ⊗ X₂) ⊗ ⋯`. -/
@[nolint has_nonempty_instance]
inductive normal_monoidal_object : Type u
| unit : normal_monoidal_object
| tensor : normal_monoidal_object → C → normal_monoidal_object
end
local notation `F` := free_monoidal_category
local notation `N` := discrete ∘ normal_monoidal_object
local infixr ` ⟶ᵐ `:10 := hom
/-- Auxiliary definition for `inclusion`. -/
@[simp] def inclusion_obj : normal_monoidal_object C → F C
| normal_monoidal_object.unit := unit
| (normal_monoidal_object.tensor n a) := tensor (inclusion_obj n) (of a)
/-- The discrete subcategory of objects in normal form includes into the free monoidal category. -/
@[simp] def inclusion : N C ⥤ F C :=
discrete.functor inclusion_obj
/-- Auxiliary definition for `normalize`. -/
@[simp] def normalize_obj : F C → normal_monoidal_object C → N C
| unit n := ⟨n⟩
| (of X) n := ⟨normal_monoidal_object.tensor n X⟩
| (tensor X Y) n := normalize_obj Y (normalize_obj X n).as
@[simp] lemma normalize_obj_unitor (n : normal_monoidal_object C) :
normalize_obj (𝟙_ (F C)) n = ⟨n⟩ :=
rfl
@[simp] lemma normalize_obj_tensor (X Y : F C) (n : normal_monoidal_object C) :
normalize_obj (X ⊗ Y) n = normalize_obj Y (normalize_obj X n).as :=
rfl
section
open hom
local attribute [tidy] tactic.discrete_cases
/-- Auxiliary definition for `normalize`. Here we prove that objects that are related by
associators and unitors map to the same normal form. -/
@[simp] def normalize_map_aux : Π {X Y : F C},
(X ⟶ᵐ Y) →
((discrete.functor (normalize_obj X) : _ ⥤ N C) ⟶ discrete.functor (normalize_obj Y))
| _ _ (id _) := 𝟙 _
| _ _ (α_hom _ _ _) := ⟨λ X, 𝟙 _, by { rintros ⟨X⟩ ⟨Y⟩ f, simp }⟩
| _ _ (α_inv _ _ _) := ⟨λ X, 𝟙 _, by { rintros ⟨X⟩ ⟨Y⟩ f, simp }⟩
| _ _ (l_hom _) := ⟨λ X, 𝟙 _, by { rintros ⟨X⟩ ⟨Y⟩ f, simp }⟩
| _ _ (l_inv _) := ⟨λ X, 𝟙 _, by { rintros ⟨X⟩ ⟨Y⟩ f, simp }⟩
| _ _ (ρ_hom _) := ⟨λ ⟨X⟩, ⟨⟨by simp⟩⟩, by { rintros ⟨X⟩ ⟨Y⟩ f, simp }⟩
| _ _ (ρ_inv _) := ⟨λ ⟨X⟩, ⟨⟨by simp⟩⟩, by { rintros ⟨X⟩ ⟨Y⟩ f, simp }⟩
| X Y (@comp _ U V W f g) := normalize_map_aux f ≫ normalize_map_aux g
| X Y (@hom.tensor _ T U V W f g) :=
⟨λ X, (normalize_map_aux g).app (normalize_obj T X.as) ≫
(discrete.functor (normalize_obj W) : _ ⥤ N C).map ((normalize_map_aux f).app X), by tidy⟩
end
section
variables (C)
/-- Our normalization procedure works by first defining a functor `F C ⥤ (N C ⥤ N C)` (which turns
out to be very easy), and then obtain a functor `F C ⥤ N C` by plugging in the normal object
`𝟙_ C`. -/
@[simp] def normalize : F C ⥤ N C ⥤ N C :=
{ obj := λ X, discrete.functor (normalize_obj X),
map := λ X Y, quotient.lift normalize_map_aux (by tidy) }
/-- A variant of the normalization functor where we consider the result as an object in the free
monoidal category (rather than an object of the discrete subcategory of objects in normal
form). -/
@[simp] def normalize' : F C ⥤ N C ⥤ F C :=
normalize C ⋙ (whiskering_right _ _ _).obj inclusion
/-- The normalization functor for the free monoidal category over `C`. -/
def full_normalize : F C ⥤ N C :=
{ obj := λ X, ((normalize C).obj X).obj ⟨normal_monoidal_object.unit⟩,
map := λ X Y f, ((normalize C).map f).app ⟨normal_monoidal_object.unit⟩ }
/-- Given an object `X` of the free monoidal category and an object `n` in normal form, taking
the tensor product `n ⊗ X` in the free monoidal category is functorial in both `X` and `n`. -/
@[simp] def tensor_func : F C ⥤ N C ⥤ F C :=
{ obj := λ X, discrete.functor (λ n, (inclusion.obj ⟨n⟩) ⊗ X),
map := λ X Y f, ⟨λ n, 𝟙 _ ⊗ f, by { rintro ⟨X⟩ ⟨Y⟩, tidy }⟩ }
lemma tensor_func_map_app {X Y : F C} (f : X ⟶ Y) (n) : ((tensor_func C).map f).app n =
𝟙 _ ⊗ f :=
rfl
lemma tensor_func_obj_map (Z : F C) {n n' : N C} (f : n ⟶ n') :
((tensor_func C).obj Z).map f = inclusion.map f ⊗ 𝟙 Z :=
by { cases n, cases n', tidy }
/-- Auxiliary definition for `normalize_iso`. Here we construct the isomorphism between
`n ⊗ X` and `normalize X n`. -/
@[simp] def normalize_iso_app :
Π (X : F C) (n : N C), ((tensor_func C).obj X).obj n ≅ ((normalize' C).obj X).obj n
| (of X) n := iso.refl _
| unit n := ρ_ _
| (tensor X Y) n :=
(α_ _ _ _).symm ≪≫ tensor_iso (normalize_iso_app X n) (iso.refl _) ≪≫ normalize_iso_app _ _
@[simp] lemma normalize_iso_app_tensor (X Y : F C) (n : N C) :
normalize_iso_app C (X ⊗ Y) n =
(α_ _ _ _).symm ≪≫ tensor_iso (normalize_iso_app C X n) (iso.refl _) ≪≫
normalize_iso_app _ _ _ :=
rfl
@[simp] lemma normalize_iso_app_unitor (n : N C) : normalize_iso_app C (𝟙_ (F C)) n = ρ_ _ :=
rfl
/-- Auxiliary definition for `normalize_iso`. -/
@[simp] def normalize_iso_aux (X : F C) : (tensor_func C).obj X ≅ (normalize' C).obj X :=
nat_iso.of_components (normalize_iso_app C X) (by { rintros ⟨X⟩ ⟨Y⟩, tidy })
section
variables {D : Type u} [category.{u} D] {I : Type u} (f : I → D) (X : discrete I)
-- TODO: move to discrete_category.lean, decide whether this should be a global simp lemma
@[simp] lemma discrete_functor_obj_eq_as : (discrete.functor f).obj X = f X.as :=
rfl
-- TODO: move to discrete_category.lean, decide whether this should be a global simp lemma
@[simp] lemma discrete_functor_map_eq_id (g : X ⟶ X) : (discrete.functor f).map g = 𝟙 _ :=
by tidy
end
/-- The isomorphism between `n ⊗ X` and `normalize X n` is natural (in both `X` and `n`, but
naturality in `n` is trivial and was "proved" in `normalize_iso_aux`). This is the real heart
of our proof of the coherence theorem. -/
def normalize_iso : tensor_func C ≅ normalize' C :=
nat_iso.of_components (normalize_iso_aux C)
begin
rintros X Y f,
apply quotient.induction_on f,
intro f,
ext n,
induction f generalizing n,
{ simp only [mk_id, functor.map_id, category.id_comp, category.comp_id] },
{ dsimp,
simp only [id_tensor_associator_inv_naturality_assoc, ←pentagon_inv_assoc,
tensor_hom_inv_id_assoc, tensor_id, category.id_comp, discrete.functor_map_id, comp_tensor_id,
iso.cancel_iso_inv_left, category.assoc],
dsimp, simp only [category.comp_id], },
{ dsimp,
simp only [discrete.functor_map_id, comp_tensor_id, category.assoc, pentagon_inv_assoc,
←associator_inv_naturality_assoc, tensor_id, iso.cancel_iso_inv_left],
dsimp, simp only [category.comp_id],},
{ dsimp,
rw triangle_assoc_comp_right_assoc,
simp only [discrete.functor_map_id, category.assoc],
cases n,
dsimp, simp only [category.comp_id] },
{ dsimp,
simp only [triangle_assoc_comp_left_inv_assoc, inv_hom_id_tensor_assoc, tensor_id,
category.id_comp, discrete.functor_map_id],
dsimp, simp only [category.comp_id],
cases n, simp },
{ dsimp,
rw [←(iso.inv_comp_eq _).2 (right_unitor_tensor _ _), category.assoc, ←right_unitor_naturality],
simp only [iso.cancel_iso_inv_left, category.assoc],
congr' 1,
convert (category.comp_id _).symm,
convert discrete_functor_map_eq_id inclusion_obj _ _,
ext,
refl },
{ dsimp,
simp only [←(iso.eq_comp_inv _).1 (right_unitor_tensor_inv _ _), right_unitor_conjugation,
category.assoc, iso.hom_inv_id, iso.hom_inv_id_assoc, iso.inv_hom_id, iso.inv_hom_id_assoc],
congr,
convert (discrete_functor_map_eq_id inclusion_obj _ _).symm,
ext, refl, },
{ dsimp at *,
rw [id_tensor_comp, category.assoc, f_ih_g ⟦f_g⟧, ←category.assoc, f_ih_f ⟦f_f⟧, category.assoc,
←functor.map_comp],
congr' 2 },
{ dsimp at *,
rw associator_inv_naturality_assoc,
slice_lhs 2 3 { rw [←tensor_comp, f_ih_f ⟦f_f⟧] },
conv_lhs { rw [←@category.id_comp (F C) _ _ _ ⟦f_g⟧] },
simp only [category.comp_id, tensor_comp, category.assoc],
congr' 2,
rw [←mk_tensor, quotient.lift_mk],
dsimp,
rw [functor.map_comp, ←category.assoc, ←f_ih_g ⟦f_g⟧, ←@category.comp_id (F C) _ _ _ ⟦f_g⟧,
←category.id_comp ((discrete.functor inclusion_obj).map _), tensor_comp],
dsimp,
simp only [category.assoc, category.comp_id],
congr' 1,
convert (normalize_iso_aux C f_Z).hom.naturality ((normalize_map_aux f_f).app n),
exact (tensor_func_obj_map _ _ _).symm }
end
/-- The isomorphism between an object and its normal form is natural. -/
def full_normalize_iso : 𝟭 (F C) ≅ full_normalize C ⋙ inclusion :=
nat_iso.of_components
(λ X, (λ_ X).symm ≪≫ ((normalize_iso C).app X).app ⟨normal_monoidal_object.unit⟩)
begin
intros X Y f,
dsimp,
rw [left_unitor_inv_naturality_assoc, category.assoc, iso.cancel_iso_inv_left],
exact congr_arg (λ f, nat_trans.app f (discrete.mk normal_monoidal_object.unit))
((normalize_iso.{u} C).hom.naturality f)
end
end
/-- The monoidal coherence theorem. -/
instance subsingleton_hom : quiver.is_thin (F C) :=
λ _ _,
⟨λ f g, have (full_normalize C).map f = (full_normalize C).map g, from subsingleton.elim _ _,
begin
rw [←functor.id_map f, ←functor.id_map g],
simp [←nat_iso.naturality_2 (full_normalize_iso.{u} C), this]
end⟩
section groupoid
section
open hom
/-- Auxiliary construction for showing that the free monoidal category is a groupoid. Do not use
this, use `is_iso.inv` instead. -/
def inverse_aux : Π {X Y : F C}, (X ⟶ᵐ Y) → (Y ⟶ᵐ X)
| _ _ (id X) := id X
| _ _ (α_hom _ _ _) := α_inv _ _ _
| _ _ (α_inv _ _ _) := α_hom _ _ _
| _ _ (ρ_hom _) := ρ_inv _
| _ _ (ρ_inv _) := ρ_hom _
| _ _ (l_hom _) := l_inv _
| _ _ (l_inv _) := l_hom _
| _ _ (comp f g) := (inverse_aux g).comp (inverse_aux f)
| _ _ (hom.tensor f g) := (inverse_aux f).tensor (inverse_aux g)
end
instance : groupoid.{u} (F C) :=
{ inv := λ X Y, quotient.lift (λ f, ⟦inverse_aux f⟧) (by tidy),
..(infer_instance : category (F C)) }
end groupoid
end free_monoidal_category
end category_theory
|
2c3d6dbba9d191556e8ef09d686bb0ac07dc0f95 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /archive/100-theorems-list/73_ascending_descending_sequences.lean | e4736cc43075c813a933ce7d7cf0c57c424199c5 | [
"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 | 8,116 | 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 tactic.basic
import data.fintype.basic
/-!
# Erdős–Szekeres theorem
This file proves Theorem 73 from the [100 Theorems List](https://www.cs.ru.nl/~freek/100/), also
known as the Erdős–Szekeres theorem: given a sequence of more than `r * s` distinct
values, there is an increasing sequence of length longer than `r` or a decreasing sequence of length
longer than `s`.
We use the proof outlined at
https://en.wikipedia.org/wiki/Erdos-Szekeres_theorem#Pigeonhole_principle.
## Tags
sequences, increasing, decreasing, Ramsey, Erdos-Szekeres, Erdős–Szekeres, Erdős-Szekeres
-/
variables {α : Type*} [linear_order α] {β : Type*}
open function finset
open_locale classical
/--
**Erdős–Szekeres Theorem**: Given a sequence of more than `r * s` distinct values, there is an
increasing sequence of length longer than `r` or a decreasing sequence of length longer than `s`.
Proof idea:
We label each value in the sequence with two numbers specifying the longest increasing
subsequence ending there, and the longest decreasing subsequence ending there.
We then show the pair of labels must be unique. Now if there is no increasing sequence longer than
`r` and no decreasing sequence longer than `s`, then there are at most `r * s` possible labels,
which is a contradiction if there are more than `r * s` elements.
-/
theorem erdos_szekeres {r s n : ℕ} {f : fin n → α} (hn : r * s < n) (hf : injective f) :
(∃ (t : finset (fin n)), r < t.card ∧ strict_mono_on f ↑t) ∨
(∃ (t : finset (fin n)), s < t.card ∧ strict_anti_on f ↑t) :=
begin
-- Given an index `i`, produce the set of increasing (resp., decreasing) subsequences which ends
-- at `i`.
let inc_sequences_ending_in : fin n → finset (finset (fin n)) :=
λ i, univ.powerset.filter (λ t, finset.max t = some i ∧ strict_mono_on f ↑t),
let dec_sequences_ending_in : fin n → finset (finset (fin n)) :=
λ i, univ.powerset.filter (λ t, finset.max t = some i ∧ strict_anti_on f ↑t),
-- The singleton sequence is in both of the above collections.
-- (This is useful to show that the maximum length subsequence is at least 1, and that the set
-- of subsequences is nonempty.)
have inc_i : ∀ i, {i} ∈ inc_sequences_ending_in i := λ i, by simp [strict_mono_on],
have dec_i : ∀ i, {i} ∈ dec_sequences_ending_in i := λ i, by simp [strict_anti_on],
-- Define the pair of labels: at index `i`, the pair is the maximum length of an increasing
-- subsequence ending at `i`, paired with the maximum length of a decreasing subsequence ending
-- at `i`.
-- We call these labels `(a_i, b_i)`.
let ab : fin n → ℕ × ℕ,
{ intro i,
apply (max' ((inc_sequences_ending_in i).image card) (nonempty.image ⟨{i}, inc_i i⟩ _),
max' ((dec_sequences_ending_in i).image card) (nonempty.image ⟨{i}, dec_i i⟩ _)) },
-- It now suffices to show that one of the labels is 'big' somewhere. In particular, if the
-- first in the pair is more than `r` somewhere, then we have an increasing subsequence in our
-- set, and if the second is more than `s` somewhere, then we have a decreasing subsequence.
suffices : ∃ i, r < (ab i).1 ∨ s < (ab i).2,
{ obtain ⟨i, hi⟩ := this,
apply or.imp _ _ hi,
work_on_goal 0 { have : (ab i).1 ∈ _ := max'_mem _ _ },
work_on_goal 1 { have : (ab i).2 ∈ _ := max'_mem _ _ },
all_goals
{ intro hi,
rw mem_image at this,
obtain ⟨t, ht₁, ht₂⟩ := this,
refine ⟨t, by rwa ht₂, _⟩,
rw mem_filter at ht₁,
apply ht₁.2.2 } },
-- Show first that the pair of labels is unique.
have : injective ab,
{ apply injective_of_lt_imp_ne,
intros i j k q,
injection q with q₁ q₂,
-- We have two cases: `f i < f j` or `f j < f i`.
-- In the former we'll show `a_i < a_j`, and in the latter we'll show `b_i < b_j`.
cases lt_or_gt_of_ne (λ _, ne_of_lt ‹i < j› (hf ‹f i = f j›)),
work_on_goal 0 { apply ne_of_lt _ q₁, have : (ab i).1 ∈ _ := max'_mem _ _ },
work_on_goal 1 { apply ne_of_lt _ q₂, have : (ab i).2 ∈ _ := max'_mem _ _ },
all_goals
{ -- Reduce to showing there is a subsequence of length `a_i + 1` which ends at `j`.
rw nat.lt_iff_add_one_le,
apply le_max',
rw mem_image at this ⊢,
-- In particular we take the subsequence `t` of length `a_i` which ends at `i`, by definition
-- of `a_i`
rcases this with ⟨t, ht₁, ht₂⟩,
rw mem_filter at ht₁,
-- Ensure `t` ends at `i`.
have : i ∈ t.max,
simp [ht₁.2.1],
-- Now our new subsequence is given by adding `j` at the end of `t`.
refine ⟨insert j t, _, _⟩,
-- First make sure it's valid, i.e., that this subsequence ends at `j` and is increasing
{ rw mem_filter,
refine ⟨_, _, _⟩,
{ rw mem_powerset, apply subset_univ },
-- It ends at `j` since `i < j`.
{ convert max_insert,
rw [ht₁.2.1, option.lift_or_get_some_some, max_eq_left, with_top.some_eq_coe],
apply le_of_lt ‹i < j› },
-- To show it's increasing (i.e., `f` is monotone increasing on `t.insert j`), we do cases
-- on what the possibilities could be - either in `t` or equals `j`.
simp only [strict_mono_on, strict_anti_on, coe_insert, set.mem_insert_iff,
mem_coe],
-- Most of the cases are just bashes.
rintros x ⟨rfl | _⟩ y ⟨rfl | _⟩ _,
{ apply (irrefl _ ‹j < j›).elim },
{ exfalso,
apply not_le_of_lt (trans ‹i < j› ‹j < y›) (le_max_of_mem ‹y ∈ t› ‹i ∈ t.max›) },
{ apply lt_of_le_of_lt _ ‹f i < f j› <|> apply lt_of_lt_of_le ‹f j < f i› _,
rcases lt_or_eq_of_le (le_max_of_mem ‹x ∈ t› ‹i ∈ t.max›) with _ | rfl,
{ apply le_of_lt (ht₁.2.2 ‹x ∈ t› (mem_of_max ‹i ∈ t.max›) ‹x < i›) },
{ refl } },
{ apply ht₁.2.2 ‹x ∈ t› ‹y ∈ t› ‹x < y› } },
-- Finally show that this new subsequence is one longer than the old one.
{ rw [card_insert_of_not_mem, ht₂],
intro _,
apply not_le_of_lt ‹i < j› (le_max_of_mem ‹j ∈ t› ‹i ∈ t.max›) } } },
-- Finished both goals!
-- Now that we have uniqueness of each label, it remains to do some counting to finish off.
-- Suppose all the labels are small.
by_contra q,
push_neg at q,
-- Then the labels `(a_i, b_i)` all fit in the following set: `{ (x,y) | 1 ≤ x ≤ r, 1 ≤ y ≤ s }`
let ran : finset (ℕ × ℕ) := ((range r).image nat.succ).product ((range s).image nat.succ),
-- which we prove here.
have : image ab univ ⊆ ran,
-- First some logical shuffling
{ rintro ⟨x₁, x₂⟩,
simp only [mem_image, exists_prop, mem_range, mem_univ, mem_product, true_and, prod.mk.inj_iff],
rintros ⟨i, rfl, rfl⟩,
specialize q i,
-- Show `1 ≤ a_i` and `1 ≤ b_i`, which is easy from the fact that `{i}` is a increasing and
-- decreasing subsequence which we did right near the top.
have z : 1 ≤ (ab i).1 ∧ 1 ≤ (ab i).2,
{ split;
{ apply le_max',
rw mem_image,
refine ⟨{i}, by solve_by_elim, card_singleton i⟩ } },
refine ⟨_, _⟩,
-- Need to get `a_i ≤ r`, here phrased as: there is some `a < r` with `a+1 = a_i`.
{ refine ⟨(ab i).1 - 1, _, nat.succ_pred_eq_of_pos z.1⟩,
rw tsub_lt_iff_right z.1,
apply nat.lt_succ_of_le q.1 },
{ refine ⟨(ab i).2 - 1, _, nat.succ_pred_eq_of_pos z.2⟩,
rw tsub_lt_iff_right z.2,
apply nat.lt_succ_of_le q.2 } },
-- To get our contradiction, it suffices to prove `n ≤ r * s`
apply not_le_of_lt hn,
-- Which follows from considering the cardinalities of the subset above, since `ab` is injective.
simpa [nat.succ_injective, card_image_of_injective, ‹injective ab›] using card_le_of_subset this,
end
|
3e69474fe205ea7e03699cb9bd35cbe5cf984654 | 02fbe05a45fda5abde7583464416db4366eedfbf | /library/init/algebra/functions.lean | 95f73074074a4a659023cdebe11fd04416d5a84c | [
"Apache-2.0"
] | permissive | jasonrute/lean | cc12807e11f9ac6b01b8951a8bfb9c2eb35a0154 | 4be962c167ca442a0ec5e84472d7ff9f5302788f | refs/heads/master | 1,672,036,664,637 | 1,601,642,826,000 | 1,601,642,826,000 | 260,777,966 | 0 | 0 | Apache-2.0 | 1,588,454,819,000 | 1,588,454,818,000 | null | UTF-8 | Lean | false | false | 4,456 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
prelude
import init.algebra.order init.meta
universe u
definition min {α : Type u} [decidable_linear_order α] (a b : α) : α := if a ≤ b then a else b
definition max {α : Type u} [decidable_linear_order α] (a b : α) : α := if b ≤ a then a else b
section
open decidable tactic
variables {α : Type u} [decidable_linear_order α]
private meta def min_tac_step : tactic unit :=
solve1 $ intros
>> `[unfold min max]
>> try `[simp [*, if_pos, if_neg]]
>> try `[apply le_refl]
>> try `[apply le_of_not_le, assumption]
meta def tactic.interactive.min_tac (a b : interactive.parse lean.parser.pexpr) : tactic unit :=
interactive.by_cases (none, ``(%%a ≤ %%b)); min_tac_step
lemma min_le_left (a b : α) : min a b ≤ a :=
by min_tac a b
lemma min_le_right (a b : α) : min a b ≤ b :=
by min_tac a b
lemma le_min {a b c : α} (h₁ : c ≤ a) (h₂ : c ≤ b) : c ≤ min a b :=
by min_tac a b
lemma le_max_left (a b : α) : a ≤ max a b :=
by min_tac b a
lemma le_max_right (a b : α) : b ≤ max a b :=
by min_tac b a
lemma max_le {a b c : α} (h₁ : a ≤ c) (h₂ : b ≤ c) : max a b ≤ c :=
by min_tac b a
lemma eq_min {a b c : α} (h₁ : c ≤ a) (h₂ : c ≤ b) (h₃ : ∀{d}, d ≤ a → d ≤ b → d ≤ c) :
c = min a b :=
le_antisymm (le_min h₁ h₂) (h₃ (min_le_left a b) (min_le_right a b))
lemma min_comm (a b : α) : min a b = min b a :=
eq_min (min_le_right a b) (min_le_left a b) (λ c h₁ h₂, le_min h₂ h₁)
lemma min_assoc (a b c : α) : min (min a b) c = min a (min b c) :=
begin
apply eq_min,
{ apply le_trans, apply min_le_left, apply min_le_left },
{ apply le_min, apply le_trans, apply min_le_left, apply min_le_right, apply min_le_right },
{ intros d h₁ h₂, apply le_min, apply le_min h₁, apply le_trans h₂, apply min_le_left,
apply le_trans h₂, apply min_le_right }
end
lemma min_left_comm : ∀ (a b c : α), min a (min b c) = min b (min a c) :=
left_comm (@min α _) (@min_comm α _) (@min_assoc α _)
@[simp]
lemma min_self (a : α) : min a a = a :=
by min_tac a a
@[ematch]
lemma min_eq_left {a b : α} (h : a ≤ b) : min a b = a :=
begin apply eq.symm, apply eq_min (le_refl _) h, intros, assumption end
@[ematch]
lemma min_eq_right {a b : α} (h : b ≤ a) : min a b = b :=
eq.subst (min_comm b a) (min_eq_left h)
lemma eq_max {a b c : α} (h₁ : a ≤ c) (h₂ : b ≤ c) (h₃ : ∀{d}, a ≤ d → b ≤ d → c ≤ d) : c = max a b :=
le_antisymm (h₃ (le_max_left a b) (le_max_right a b)) (max_le h₁ h₂)
lemma max_comm (a b : α) : max a b = max b a :=
eq_max (le_max_right a b) (le_max_left a b) (λ c h₁ h₂, max_le h₂ h₁)
lemma max_assoc (a b c : α) : max (max a b) c = max a (max b c) :=
begin
apply eq_max,
{ apply le_trans, apply le_max_left a b, apply le_max_left },
{ apply max_le, apply le_trans, apply le_max_right a b, apply le_max_left, apply le_max_right },
{ intros d h₁ h₂, apply max_le, apply max_le h₁, apply le_trans (le_max_left _ _) h₂,
apply le_trans (le_max_right _ _) h₂}
end
lemma max_left_comm : ∀ (a b c : α), max a (max b c) = max b (max a c) :=
left_comm (@max α _) (@max_comm α _) (@max_assoc α _)
@[simp]
lemma max_self (a : α) : max a a = a :=
by min_tac a a
lemma max_eq_left {a b : α} (h : b ≤ a) : max a b = a :=
begin apply eq.symm, apply eq_max (le_refl _) h, intros, assumption end
lemma max_eq_right {a b : α} (h : a ≤ b) : max a b = b :=
eq.subst (max_comm b a) (max_eq_left h)
/- these rely on lt_of_lt -/
lemma min_eq_left_of_lt {a b : α} (h : a < b) : min a b = a :=
min_eq_left (le_of_lt h)
lemma min_eq_right_of_lt {a b : α} (h : b < a) : min a b = b :=
min_eq_right (le_of_lt h)
lemma max_eq_left_of_lt {a b : α} (h : b < a) : max a b = a :=
max_eq_left (le_of_lt h)
lemma max_eq_right_of_lt {a b : α} (h : a < b) : max a b = b :=
max_eq_right (le_of_lt h)
/- these use the fact that it is a linear ordering -/
lemma lt_min {a b c : α} (h₁ : a < b) (h₂ : a < c) : a < min b c :=
or.elim (le_or_gt b c)
(assume h : b ≤ c, by min_tac b c)
(assume h : b > c, by min_tac b c)
lemma max_lt {a b c : α} (h₁ : a < c) (h₂ : b < c) : max a b < c :=
or.elim (le_or_gt a b)
(assume h : a ≤ b, by min_tac b a)
(assume h : a > b, by min_tac b a)
end
|
f921d3d06d8ede2a0a3e523c40763224bf82860f | 07c76fbd96ea1786cc6392fa834be62643cea420 | /hott/types/nat/basic.hlean | 210c0c38488e98beefb08deb03147d527ec0e854 | [
"Apache-2.0"
] | permissive | fpvandoorn/lean2 | 5a430a153b570bf70dc8526d06f18fc000a60ad9 | 0889cf65b7b3cebfb8831b8731d89c2453dd1e9f | refs/heads/master | 1,592,036,508,364 | 1,545,093,958,000 | 1,545,093,958,000 | 75,436,854 | 0 | 0 | null | 1,480,718,780,000 | 1,480,718,780,000 | null | UTF-8 | Lean | false | false | 10,021 | hlean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
(Ported from standard library)
Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad
Basic operations on the natural numbers.
-/
import ..num algebra.ring
open prod binary eq algebra lift is_trunc
namespace nat
/- a variant of add, defined by recursion on the first argument -/
definition addl (x y : ℕ) : ℕ :=
nat.rec y (λ n r, succ r) x
infix ` ⊕ `:65 := addl
theorem addl_succ_right (n m : ℕ) : n ⊕ succ m = succ (n ⊕ m) :=
nat.rec_on n
rfl
(λ n₁ ih, calc
succ n₁ ⊕ succ m = succ (n₁ ⊕ succ m) : rfl
... = succ (succ (n₁ ⊕ m)) : ih
... = succ (succ n₁ ⊕ m) : rfl)
theorem add_eq_addl (x : ℕ) : Πy, x + y = x ⊕ y :=
nat.rec_on x
(λ y, nat.rec_on y
rfl
(λ y₁ ih, calc
0 + succ y₁ = succ (0 + y₁) : rfl
... = succ (0 ⊕ y₁) : {ih}
... = 0 ⊕ (succ y₁) : rfl))
(λ x₁ ih₁ y, nat.rec_on y
(calc
succ x₁ + 0 = succ (x₁ + 0) : rfl
... = succ (x₁ ⊕ 0) : {ih₁ 0}
... = succ x₁ ⊕ 0 : rfl)
(λ y₁ ih₂, calc
succ x₁ + succ y₁ = succ (succ x₁ + y₁) : rfl
... = succ (succ x₁ ⊕ y₁) : {ih₂}
... = succ x₁ ⊕ succ y₁ : addl_succ_right))
/- successor and predecessor -/
theorem succ_ne_zero (n : ℕ) : succ n ≠ 0 :=
by contradiction
-- add_rewrite succ_ne_zero
theorem pred_zero [simp] : pred 0 = 0 :=
rfl
theorem pred_succ [simp] (n : ℕ) : pred (succ n) = n :=
rfl
theorem eq_zero_sum_eq_succ_pred (n : ℕ) : n = 0 ⊎ n = succ (pred n) :=
nat.rec_on n
(sum.inl rfl)
(take m IH, sum.inr
(show succ m = succ (pred (succ m)), from ap succ !pred_succ⁻¹))
theorem exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : Σk : ℕ, n = succ k :=
sigma.mk _ (sum_resolve_right !eq_zero_sum_eq_succ_pred H)
theorem succ.inj {n m : ℕ} (H : succ n = succ m) : n = m :=
down (nat.no_confusion H imp.id)
abbreviation eq_of_succ_eq_succ := @succ.inj
theorem succ_ne_self {n : ℕ} : succ n ≠ n :=
nat.rec_on n
(take H : 1 = 0,
have ne : 1 ≠ 0, from !succ_ne_zero,
absurd H ne)
(take k IH H, IH (succ.inj H))
theorem discriminate {B : Type} {n : ℕ} (H1: n = 0 → B) (H2 : Πm, n = succ m → B) : B :=
have H : n = n → B, from nat.cases_on n H1 H2,
H rfl
theorem two_step_rec_on {P : ℕ → Type} (a : ℕ) (H1 : P 0) (H2 : P 1)
(H3 : Π (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : P a :=
have stronger : P a × P (succ a), from
nat.rec_on a
(pair H1 H2)
(take k IH,
have IH1 : P k, from prod.pr1 IH,
have IH2 : P (succ k), from prod.pr2 IH,
pair IH2 (H3 k IH1 IH2)),
prod.pr1 stronger
theorem sub_induction {P : ℕ → ℕ → Type} (n m : ℕ) (H1 : Πm, P 0 m)
(H2 : Πn, P (succ n) 0) (H3 : Πn m, P n m → P (succ n) (succ m)) : P n m :=
have general : Πm, P n m, from nat.rec_on n H1
(take k : ℕ,
assume IH : Πm, P k m,
take m : ℕ,
nat.cases_on m (H2 k) (take l, (H3 k l (IH l)))),
general m
/- addition -/
protected definition add_zero [simp] (n : ℕ) : n + 0 = n :=
rfl
definition add_succ [simp] (n m : ℕ) : n + succ m = succ (n + m) :=
rfl
protected definition zero_add [simp] (n : ℕ) : 0 + n = n :=
begin
induction n with n IH,
reflexivity,
exact ap succ IH
end
definition succ_add [simp] (n m : ℕ) : (succ n) + m = succ (n + m) :=
begin
induction m with m IH,
reflexivity,
exact ap succ IH
end
protected definition add_comm [simp] (n m : ℕ) : n + m = m + n :=
begin
induction n with n IH,
{ apply nat.zero_add},
{ exact !succ_add ⬝ ap succ IH}
end
protected definition add_add (n l k : ℕ) : n + l + k = n + (k + l) :=
begin
induction l with l IH,
reflexivity,
exact succ_add (n + l) k ⬝ ap succ IH
end
definition succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m :=
!succ_add
protected definition add_assoc [simp] (n m k : ℕ) : (n + m) + k = n + (m + k) :=
begin
induction k with k IH,
reflexivity,
exact ap succ IH
end
protected theorem add_left_comm : Π (n m k : ℕ), n + (m + k) = m + (n + k) :=
left_comm nat.add_comm nat.add_assoc
protected theorem add_right_comm : Π (n m k : ℕ), n + m + k = n + k + m :=
right_comm nat.add_comm nat.add_assoc
protected theorem add_left_cancel {n m k : ℕ} : n + m = n + k → m = k :=
nat.rec_on n
(take H : 0 + m = 0 + k,
!nat.zero_add⁻¹ ⬝ H ⬝ !nat.zero_add)
(take (n : ℕ) (IH : n + m = n + k → m = k) (H : succ n + m = succ n + k),
have succ (n + m) = succ (n + k),
from calc
succ (n + m) = succ n + m : succ_add
... = succ n + k : H
... = succ (n + k) : succ_add,
have n + m = n + k, from succ.inj this,
IH this)
protected theorem add_right_cancel {n m k : ℕ} (H : n + m = k + m) : n = k :=
have H2 : m + n = m + k, from !nat.add_comm ⬝ H ⬝ !nat.add_comm,
nat.add_left_cancel H2
theorem eq_zero_of_add_eq_zero_right {n m : ℕ} : n + m = 0 → n = 0 :=
nat.rec_on n
(take (H : 0 + m = 0), rfl)
(take k IH,
assume H : succ k + m = 0,
absurd
(show succ (k + m) = 0, from calc
succ (k + m) = succ k + m : succ_add
... = 0 : H)
!succ_ne_zero)
theorem eq_zero_of_add_eq_zero_left {n m : ℕ} (H : n + m = 0) : m = 0 :=
eq_zero_of_add_eq_zero_right (!nat.add_comm ⬝ H)
theorem eq_zero_prod_eq_zero_of_add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 × m = 0 :=
pair (eq_zero_of_add_eq_zero_right H) (eq_zero_of_add_eq_zero_left H)
theorem add_one [simp] (n : ℕ) : n + 1 = succ n := rfl
theorem one_add (n : ℕ) : 1 + n = succ n :=
!nat.zero_add ▸ !succ_add
/- multiplication -/
protected theorem mul_zero [simp] (n : ℕ) : n * 0 = 0 :=
rfl
theorem mul_succ [simp] (n m : ℕ) : n * succ m = n * m + n :=
rfl
-- commutativity, distributivity, associativity, identity
protected theorem zero_mul [simp] (n : ℕ) : 0 * n = 0 :=
nat.rec_on n
!nat.mul_zero
(take m IH, !mul_succ ⬝ !nat.add_zero ⬝ IH)
theorem succ_mul [simp] (n m : ℕ) : (succ n) * m = (n * m) + m :=
nat.rec_on m
(by rewrite nat.mul_zero)
(take k IH, calc
succ n * succ k = succ n * k + succ n : mul_succ
... = n * k + k + succ n : IH
... = n * k + (k + succ n) : nat.add_assoc
... = n * k + (succ n + k) : nat.add_comm
... = n * k + (n + succ k) : succ_add_eq_succ_add
... = n * k + n + succ k : nat.add_assoc
... = n * succ k + succ k : mul_succ)
protected theorem mul_comm [simp] (n m : ℕ) : n * m = m * n :=
nat.rec_on m
(!nat.mul_zero ⬝ !nat.zero_mul⁻¹)
(take k IH, calc
n * succ k = n * k + n : mul_succ
... = k * n + n : IH
... = (succ k) * n : succ_mul)
protected theorem right_distrib (n m k : ℕ) : (n + m) * k = n * k + m * k :=
nat.rec_on k
(calc
(n + m) * 0 = 0 : nat.mul_zero
... = 0 + 0 : nat.add_zero
... = n * 0 + 0 : nat.mul_zero
... = n * 0 + m * 0 : nat.mul_zero)
(take l IH, calc
(n + m) * succ l = (n + m) * l + (n + m) : mul_succ
... = n * l + m * l + (n + m) : IH
... = n * l + m * l + n + m : nat.add_assoc
... = n * l + n + m * l + m : nat.add_right_comm
... = n * l + n + (m * l + m) : nat.add_assoc
... = n * succ l + (m * l + m) : mul_succ
... = n * succ l + m * succ l : mul_succ)
protected theorem left_distrib (n m k : ℕ) : n * (m + k) = n * m + n * k :=
calc
n * (m + k) = (m + k) * n : nat.mul_comm
... = m * n + k * n : nat.right_distrib
... = n * m + k * n : nat.mul_comm
... = n * m + n * k : nat.mul_comm
protected theorem mul_assoc [simp] (n m k : ℕ) : (n * m) * k = n * (m * k) :=
nat.rec_on k
(calc
(n * m) * 0 = n * (m * 0) : nat.mul_zero)
(take l IH,
calc
(n * m) * succ l = (n * m) * l + n * m : mul_succ
... = n * (m * l) + n * m : IH
... = n * (m * l + m) : nat.left_distrib
... = n * (m * succ l) : mul_succ)
protected theorem mul_one [simp] (n : ℕ) : n * 1 = n :=
calc
n * 1 = n * 0 + n : mul_succ
... = 0 + n : nat.mul_zero
... = n : nat.zero_add
protected theorem one_mul [simp] (n : ℕ) : 1 * n = n :=
calc
1 * n = n * 1 : nat.mul_comm
... = n : nat.mul_one
theorem eq_zero_sum_eq_zero_of_mul_eq_zero {n m : ℕ} : n * m = 0 → n = 0 ⊎ m = 0 :=
nat.cases_on n
(assume H, sum.inl rfl)
(take n',
nat.cases_on m
(assume H, sum.inr rfl)
(take m',
assume H : succ n' * succ m' = 0,
absurd
(calc
0 = succ n' * succ m' : H
... = succ n' * m' + succ n' : mul_succ
... = succ (succ n' * m' + n') : add_succ)⁻¹
!succ_ne_zero))
protected definition comm_semiring [trans_instance] : comm_semiring nat :=
⦃comm_semiring,
add := nat.add,
add_assoc := nat.add_assoc,
zero := nat.zero,
zero_add := nat.zero_add,
add_zero := nat.add_zero,
add_comm := nat.add_comm,
mul := nat.mul,
mul_assoc := nat.mul_assoc,
one := nat.succ nat.zero,
one_mul := nat.one_mul,
mul_one := nat.mul_one,
left_distrib := nat.left_distrib,
right_distrib := nat.right_distrib,
zero_mul := nat.zero_mul,
mul_zero := nat.mul_zero,
mul_comm := nat.mul_comm,
is_set_carrier:= _⦄
end nat
section
open nat
definition iterate {A : Type} (op : A → A) : ℕ → A → A
| 0 := λ a, a
| (succ k) := λ a, op (iterate k a)
notation f `^[`:80 n:0 `]`:0 := iterate f n
end
|
28896d4043408a654e0169e1b826807c8411fe85 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/linear_algebra/free_module/pid.lean | 73df194265e5949cf6a262f02271f0d78806a23f | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 25,503 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import linear_algebra.dimension
import ring_theory.principal_ideal_domain
import ring_theory.finiteness
/-! # Free modules over PID
A free `R`-module `M` is a module with a basis over `R`,
equivalently it is an `R`-module linearly equivalent to `ι →₀ R` for some `ι`.
This file proves a submodule of a free `R`-module of finite rank is also
a free `R`-module of finite rank, if `R` is a principal ideal domain (PID),
i.e. we have instances `[is_domain R] [is_principal_ideal_ring R]`.
We express "free `R`-module of finite rank" as a module `M` which has a basis
`b : ι → R`, where `ι` is a `fintype`.
We call the cardinality of `ι` the rank of `M` in this file;
it would be equal to `finrank R M` if `R` is a field and `M` is a vector space.
## Main results
In this section, `M` is a free and finitely generated `R`-module, and
`N` is a submodule of `M`.
- `submodule.induction_on_rank`: if `P` holds for `⊥ : submodule R M` and if
`P N` follows from `P N'` for all `N'` that are of lower rank, then `P` holds
on all submodules
- `submodule.exists_basis_of_pid`: if `R` is a PID, then `N : submodule R M` is
free and finitely generated. This is the first part of the structure theorem
for modules.
- `submodule.smith_normal_form`: if `R` is a PID, then `M` has a basis
`bM` and `N` has a basis `bN` such that `bN i = a i • bM i`.
Equivalently, a linear map `f : M →ₗ M` with `range f = N` can be written as
a matrix in Smith normal form, a diagonal matrix with the coefficients `a i`
along the diagonal.
## Tags
free module, finitely generated module, rank, structure theorem
-/
open_locale big_operators
universes u v
section ring
variables {R : Type u} {M : Type v} [ring R] [add_comm_group M] [module R M]
variables {ι : Type*} (b : basis ι R M)
open submodule.is_principal submodule
lemma eq_bot_of_generator_maximal_map_eq_zero (b : basis ι R M) {N : submodule R M}
{ϕ : M →ₗ[R] R} (hϕ : ∀ (ψ : M →ₗ[R] R), N.map ϕ ≤ N.map ψ → N.map ψ = N.map ϕ)
[(N.map ϕ).is_principal] (hgen : generator (N.map ϕ) = 0) : N = ⊥ :=
begin
rw submodule.eq_bot_iff,
intros x hx,
refine b.ext_elem (λ i, _),
rw (eq_bot_iff_generator_eq_zero _).mpr hgen at hϕ,
rw [linear_equiv.map_zero, finsupp.zero_apply],
exact (submodule.eq_bot_iff _).mp (hϕ ((finsupp.lapply i) ∘ₗ ↑b.repr) bot_le) _ ⟨x, hx, rfl⟩
end
lemma eq_bot_of_generator_maximal_submodule_image_eq_zero {N O : submodule R M} (b : basis ι R O)
(hNO : N ≤ O)
{ϕ : O →ₗ[R] R} (hϕ : ∀ (ψ : O →ₗ[R] R), ϕ.submodule_image N ≤ ψ.submodule_image N →
ψ.submodule_image N = ϕ.submodule_image N)
[(ϕ.submodule_image N).is_principal] (hgen : generator (ϕ.submodule_image N) = 0) :
N = ⊥ :=
begin
rw submodule.eq_bot_iff,
intros x hx,
refine congr_arg coe (show (⟨x, hNO hx⟩ : O) = 0, from b.ext_elem (λ i, _)),
rw (eq_bot_iff_generator_eq_zero _).mpr hgen at hϕ,
rw [linear_equiv.map_zero, finsupp.zero_apply],
refine (submodule.eq_bot_iff _).mp (hϕ ((finsupp.lapply i) ∘ₗ ↑b.repr) bot_le) _ _,
exact (linear_map.mem_submodule_image_of_le hNO).mpr ⟨x, hx, rfl⟩
end
end ring
section is_domain
variables {ι : Type*} {R : Type*} [comm_ring R] [is_domain R]
variables {M : Type*} [add_comm_group M] [module R M] {b : ι → M}
open submodule.is_principal set submodule
lemma dvd_generator_iff {I : ideal R} [I.is_principal] {x : R} (hx : x ∈ I) :
x ∣ generator I ↔ I = ideal.span {x} :=
begin
conv_rhs { rw [← span_singleton_generator I] },
erw [ideal.span_singleton_eq_span_singleton, ← dvd_dvd_iff_associated, ← mem_iff_generator_dvd],
exact ⟨λ h, ⟨hx, h⟩, λ h, h.2⟩
end
end is_domain
section principal_ideal_domain
open submodule.is_principal set submodule
variables {ι : Type*} {R : Type*} [comm_ring R] [is_domain R] [is_principal_ideal_ring R]
variables {M : Type*} [add_comm_group M] [module R M] {b : ι → M}
open submodule.is_principal
lemma generator_maximal_submodule_image_dvd {N O : submodule R M} (hNO : N ≤ O)
{ϕ : O →ₗ[R] R} (hϕ : ∀ (ψ : O →ₗ[R] R), ϕ.submodule_image N ≤ ψ.submodule_image N →
ψ.submodule_image N = ϕ.submodule_image N)
[(ϕ.submodule_image N).is_principal]
(y : M) (yN : y ∈ N) (ϕy_eq : ϕ ⟨y, hNO yN⟩ = generator (ϕ.submodule_image N))
(ψ : O →ₗ[R] R) : generator (ϕ.submodule_image N) ∣ ψ ⟨y, hNO yN⟩ :=
begin
let a : R := generator (ϕ.submodule_image N),
let d : R := is_principal.generator (submodule.span R {a, ψ ⟨y, hNO yN⟩}),
have d_dvd_left : d ∣ a := (mem_iff_generator_dvd _).mp
(subset_span (mem_insert _ _)),
have d_dvd_right : d ∣ ψ ⟨y, hNO yN⟩ := (mem_iff_generator_dvd _).mp
(subset_span (mem_insert_of_mem _ (mem_singleton _))),
refine dvd_trans _ d_dvd_right,
rw [dvd_generator_iff, ideal.span,
← span_singleton_generator (submodule.span R {a, ψ ⟨y, hNO yN⟩})],
obtain ⟨r₁, r₂, d_eq⟩ : ∃ r₁ r₂ : R, d = r₁ * a + r₂ * ψ ⟨y, hNO yN⟩,
{ obtain ⟨r₁, r₂', hr₂', hr₁⟩ := mem_span_insert.mp (is_principal.generator_mem
(submodule.span R {a, ψ ⟨y, hNO yN⟩})),
obtain ⟨r₂, rfl⟩ := mem_span_singleton.mp hr₂',
exact ⟨r₁, r₂, hr₁⟩ },
let ψ' : O →ₗ[R] R := r₁ • ϕ + r₂ • ψ,
have : span R {d} ≤ ψ'.submodule_image N,
{ rw [span_le, singleton_subset_iff, set_like.mem_coe, linear_map.mem_submodule_image_of_le hNO],
refine ⟨y, yN, _⟩,
change r₁ * ϕ ⟨y, hNO yN⟩ + r₂ * ψ ⟨y, hNO yN⟩ = d,
rw [d_eq, ϕy_eq] },
refine le_antisymm (this.trans (le_of_eq _))
(ideal.span_singleton_le_span_singleton.mpr d_dvd_left),
rw span_singleton_generator,
refine hϕ ψ' (le_trans _ this),
rw [← span_singleton_generator (ϕ.submodule_image N)],
exact ideal.span_singleton_le_span_singleton.mpr d_dvd_left,
{ exact subset_span (mem_insert _ _) }
end
/-- The induction hypothesis of `submodule.basis_of_pid` and `submodule.smith_normal_form`.
Basically, it says: let `N ≤ M` be a pair of submodules, then we can find a pair of
submodules `N' ≤ M'` of strictly smaller rank, whose basis we can extend to get a basis
of `N` and `M`. Moreover, if the basis for `M'` is up to scalars a basis for `N'`,
then the basis we find for `M` is up to scalars a basis for `N`.
For `basis_of_pid` we only need the first half and can fix `M = ⊤`,
for `smith_normal_form` we need the full statement,
but must also feed in a basis for `M` using `basis_of_pid` to keep the induction going.
-/
lemma submodule.basis_of_pid_aux [fintype ι] {O : Type*} [add_comm_group O] [module R O]
(M N : submodule R O) (b'M : basis ι R M) (N_bot : N ≠ ⊥) (N_le_M : N ≤ M) :
∃ (y ∈ M) (a : R) (hay : a • y ∈ N) (M' ≤ M) (N' ≤ N) (N'_le_M' : N' ≤ M')
(y_ortho_M' : ∀ (c : R) (z : O), z ∈ M' → c • y + z = 0 → c = 0)
(ay_ortho_N' : ∀ (c : R) (z : O), z ∈ N' → c • a • y + z = 0 → c = 0),
∀ (n') (bN' : basis (fin n') R N'), ∃ (bN : basis (fin (n' + 1)) R N),
∀ (m') (hn'm' : n' ≤ m') (bM' : basis (fin m') R M'),
∃ (hnm : (n' + 1) ≤ (m' + 1)) (bM : basis (fin (m' + 1)) R M),
∀ (as : fin n' → R) (h : ∀ (i : fin n'), (bN' i : O) = as i • (bM' (fin.cast_le hn'm' i) : O)),
∃ (as' : fin (n' + 1) → R),
∀ (i : fin (n' + 1)), (bN i : O) = as' i • (bM (fin.cast_le hnm i) : O) :=
begin
-- Let `ϕ` be a maximal projection of `M` onto `R`, in the sense that there is
-- no `ψ` whose image of `N` is larger than `ϕ`'s image of `N`.
have : ∃ ϕ : M →ₗ[R] R, ∀ (ψ : M →ₗ[R] R),
ϕ.submodule_image N ≤ ψ.submodule_image N → ψ.submodule_image N = ϕ.submodule_image N,
{ obtain ⟨P, P_eq, P_max⟩ := set_has_maximal_iff_noetherian.mpr
(infer_instance : is_noetherian R R) _
(show (set.range (λ ψ : M →ₗ[R] R, ψ.submodule_image N)).nonempty,
from ⟨_, set.mem_range.mpr ⟨0, rfl⟩⟩),
obtain ⟨ϕ, rfl⟩ := set.mem_range.mp P_eq,
exact ⟨ϕ, λ ψ hψ, P_max _ ⟨_, rfl⟩ hψ⟩ },
let ϕ := this.some,
have ϕ_max := this.some_spec,
-- Since `ϕ(N)` is a `R`-submodule of the PID `R`,
-- it is principal and generated by some `a`.
let a := generator (ϕ.submodule_image N),
have a_mem : a ∈ ϕ.submodule_image N := generator_mem _,
-- If `a` is zero, then the submodule is trivial. So let's assume `a ≠ 0`, `N ≠ ⊥`.
by_cases a_zero : a = 0,
{ have := eq_bot_of_generator_maximal_submodule_image_eq_zero b'M N_le_M ϕ_max a_zero,
contradiction },
-- We claim that `ϕ⁻¹ a = y` can be taken as basis element of `N`.
obtain ⟨y, yN, ϕy_eq⟩ := (linear_map.mem_submodule_image_of_le N_le_M).mp a_mem,
have ϕy_ne_zero : ϕ ⟨y, N_le_M yN⟩ ≠ 0 := λ h, a_zero (ϕy_eq.symm.trans h),
-- Write `y` as `a • y'` for some `y'`.
have hdvd : ∀ i, a ∣ b'M.coord i ⟨y, N_le_M yN⟩ :=
λ i, generator_maximal_submodule_image_dvd N_le_M ϕ_max y yN ϕy_eq (b'M.coord i),
choose c hc using hdvd,
let y' : O := ∑ i, c i • b'M i,
have y'M : y' ∈ M := M.sum_mem (λ i _, M.smul_mem (c i) (b'M i).2),
have mk_y' : (⟨y', y'M⟩ : M) = ∑ i, c i • b'M i :=
subtype.ext (show y' = M.subtype _,
by { simp only [linear_map.map_sum, linear_map.map_smul], refl }),
have a_smul_y' : a • y' = y,
{ refine congr_arg coe (show (a • ⟨y', y'M⟩ : M) = ⟨y, N_le_M yN⟩, from _),
rw [← b'M.sum_repr ⟨y, N_le_M yN⟩, mk_y', finset.smul_sum],
refine finset.sum_congr rfl (λ i _, _),
rw [← mul_smul, ← hc], refl },
-- We found an `y` and an `a`!
refine ⟨y', y'M, a, a_smul_y'.symm ▸ yN, _⟩,
have ϕy'_eq : ϕ ⟨y', y'M⟩ = 1 := mul_left_cancel₀ a_zero
(calc a • ϕ ⟨y', y'M⟩ = ϕ ⟨a • y', _⟩ : (ϕ.map_smul a ⟨y', y'M⟩).symm
... = ϕ ⟨y, N_le_M yN⟩ : by simp only [a_smul_y']
... = a : ϕy_eq
... = a * 1 : (mul_one a).symm),
have ϕy'_ne_zero : ϕ ⟨y', y'M⟩ ≠ 0 := by simpa only [ϕy'_eq] using one_ne_zero,
-- `M' := ker (ϕ : M → R)` is smaller than `M` and `N' := ker (ϕ : N → R)` is smaller than `N`.
let M' : submodule R O := ϕ.ker.map M.subtype,
let N' : submodule R O := (ϕ.comp (of_le N_le_M)).ker.map N.subtype,
have M'_le_M : M' ≤ M := M.map_subtype_le ϕ.ker,
have N'_le_M' : N' ≤ M',
{ intros x hx,
simp only [mem_map, linear_map.mem_ker] at hx ⊢,
obtain ⟨⟨x, xN⟩, hx, rfl⟩ := hx,
exact ⟨⟨x, N_le_M xN⟩, hx, rfl⟩ },
have N'_le_N : N' ≤ N := N.map_subtype_le (ϕ.comp (of_le N_le_M)).ker,
-- So fill in those results as well.
refine ⟨M', M'_le_M, N', N'_le_N, N'_le_M', _⟩,
-- Note that `y'` is orthogonal to `M'`.
have y'_ortho_M' : ∀ (c : R) z ∈ M', c • y' + z = 0 → c = 0,
{ intros c x xM' hc,
obtain ⟨⟨x, xM⟩, hx', rfl⟩ := submodule.mem_map.mp xM',
rw linear_map.mem_ker at hx',
have hc' : (c • ⟨y', y'M⟩ + ⟨x, xM⟩ : M) = 0 := subtype.coe_injective hc,
simpa only [linear_map.map_add, linear_map.map_zero, linear_map.map_smul, smul_eq_mul, add_zero,
mul_eq_zero, ϕy'_ne_zero, hx', or_false] using congr_arg ϕ hc' },
-- And `a • y'` is orthogonal to `N'`.
have ay'_ortho_N' : ∀ (c : R) z ∈ N', c • a • y' + z = 0 → c = 0,
{ intros c z zN' hc,
refine (mul_eq_zero.mp (y'_ortho_M' (a * c) z (N'_le_M' zN') _)).resolve_left a_zero,
rw [mul_comm, mul_smul, hc] },
-- So we can extend a basis for `N'` with `y`
refine ⟨y'_ortho_M', ay'_ortho_N', λ n' bN', ⟨_, _⟩⟩,
{ refine basis.mk_fin_cons_of_le y yN bN' N'_le_N _ _,
{ intros c z zN' hc,
refine ay'_ortho_N' c z zN' _,
rwa ← a_smul_y' at hc },
{ intros z zN,
obtain ⟨b, hb⟩ : _ ∣ ϕ ⟨z, N_le_M zN⟩ := generator_submodule_image_dvd_of_mem N_le_M ϕ zN,
refine ⟨-b, submodule.mem_map.mpr ⟨⟨_, N.sub_mem zN (N.smul_mem b yN)⟩, _, _⟩⟩,
{ refine linear_map.mem_ker.mpr (show ϕ (⟨z, N_le_M zN⟩ - b • ⟨y, N_le_M yN⟩) = 0, from _),
rw [linear_map.map_sub, linear_map.map_smul, hb, ϕy_eq, smul_eq_mul,
mul_comm, sub_self] },
{ simp only [sub_eq_add_neg, neg_smul], refl } } },
-- And extend a basis for `M'` with `y'`
intros m' hn'm' bM',
refine ⟨nat.succ_le_succ hn'm', _, _⟩,
{ refine basis.mk_fin_cons_of_le y' y'M bM' M'_le_M y'_ortho_M' _,
intros z zM,
refine ⟨-ϕ ⟨z, zM⟩, ⟨⟨z, zM⟩ - (ϕ ⟨z, zM⟩) • ⟨y', y'M⟩, linear_map.mem_ker.mpr _, _⟩⟩,
{ rw [linear_map.map_sub, linear_map.map_smul, ϕy'_eq, smul_eq_mul, mul_one, sub_self] },
{ rw [linear_map.map_sub, linear_map.map_smul, sub_eq_add_neg, neg_smul], refl } },
-- It remains to show the extended bases are compatible with each other.
intros as h,
refine ⟨fin.cons a as, _⟩,
intro i,
rw [basis.coe_mk_fin_cons_of_le, basis.coe_mk_fin_cons_of_le],
refine fin.cases _ (λ i, _) i,
{ simp only [fin.cons_zero, fin.cast_le_zero],
exact a_smul_y'.symm },
{ rw fin.cast_le_succ, simp only [fin.cons_succ, coe_of_le, h i] }
end
/-- A submodule of a free `R`-module of finite rank is also a free `R`-module of finite rank,
if `R` is a principal ideal domain.
This is a `lemma` to make the induction a bit easier. To actually access the basis,
see `submodule.basis_of_pid`.
See also the stronger version `submodule.smith_normal_form`.
-/
lemma submodule.nonempty_basis_of_pid {ι : Type*} [fintype ι]
(b : basis ι R M) (N : submodule R M) :
∃ (n : ℕ), nonempty (basis (fin n) R N) :=
begin
haveI := classical.dec_eq M,
refine N.induction_on_rank b _ _,
intros N ih,
let b' := (b.reindex (fintype.equiv_fin ι)).map (linear_equiv.of_top _ rfl).symm,
by_cases N_bot : N = ⊥,
{ subst N_bot, exact ⟨0, ⟨basis.empty _⟩⟩ },
obtain ⟨y, -, a, hay, M', -, N', N'_le_N, -, -, ay_ortho, h'⟩ :=
submodule.basis_of_pid_aux ⊤ N b' N_bot le_top,
obtain ⟨n', ⟨bN'⟩⟩ := ih N' N'_le_N _ hay ay_ortho,
obtain ⟨bN, hbN⟩ := h' n' bN',
exact ⟨n' + 1, ⟨bN⟩⟩
end
/-- A submodule of a free `R`-module of finite rank is also a free `R`-module of finite rank,
if `R` is a principal ideal domain.
See also the stronger version `submodule.smith_normal_form`.
-/
noncomputable def submodule.basis_of_pid {ι : Type*} [fintype ι]
(b : basis ι R M) (N : submodule R M) :
Σ (n : ℕ), (basis (fin n) R N) :=
⟨_, (N.nonempty_basis_of_pid b).some_spec.some⟩
lemma submodule.basis_of_pid_bot {ι : Type*} [fintype ι] (b : basis ι R M) :
submodule.basis_of_pid b ⊥ = ⟨0, basis.empty _⟩ :=
begin
obtain ⟨n, b'⟩ := submodule.basis_of_pid b ⊥,
let e : fin n ≃ fin 0 := b'.index_equiv (basis.empty _ : basis (fin 0) R (⊥ : submodule R M)),
obtain rfl : n = 0 := by simpa using fintype.card_eq.mpr ⟨e⟩,
exact sigma.eq rfl (basis.eq_of_apply_eq $ fin_zero_elim)
end
/-- A submodule inside a free `R`-submodule of finite rank is also a free `R`-module of finite rank,
if `R` is a principal ideal domain.
See also the stronger version `submodule.smith_normal_form_of_le`.
-/
noncomputable def submodule.basis_of_pid_of_le {ι : Type*} [fintype ι]
{N O : submodule R M} (hNO : N ≤ O) (b : basis ι R O) :
Σ (n : ℕ), basis (fin n) R N :=
let ⟨n, bN'⟩ := submodule.basis_of_pid b (N.comap O.subtype)
in ⟨n, bN'.map (submodule.comap_subtype_equiv_of_le hNO)⟩
/-- A submodule inside the span of a linear independent family is a free `R`-module of finite rank,
if `R` is a principal ideal domain. -/
noncomputable def submodule.basis_of_pid_of_le_span
{ι : Type*} [fintype ι] {b : ι → M} (hb : linear_independent R b)
{N : submodule R M} (le : N ≤ submodule.span R (set.range b)) :
Σ (n : ℕ), basis (fin n) R N :=
submodule.basis_of_pid_of_le le (basis.span hb)
variable {M}
/-- A finite type torsion free module over a PID is free. -/
noncomputable def module.free_of_finite_type_torsion_free [fintype ι] {s : ι → M}
(hs : span R (range s) = ⊤) [no_zero_smul_divisors R M] :
Σ (n : ℕ), basis (fin n) R M :=
begin
classical,
-- We define `N` as the submodule spanned by a maximal linear independent subfamily of `s`
have := exists_maximal_independent R s,
let I : set ι := this.some,
obtain ⟨indepI : linear_independent R (s ∘ coe : I → M),
hI : ∀ i ∉ I, ∃ a : R, a ≠ 0 ∧ a • s i ∈ span R (s '' I)⟩ := this.some_spec,
let N := span R (range $ (s ∘ coe : I → M)), -- same as `span R (s '' I)` but more convenient
let sI : I → N := λ i, ⟨s i.1, subset_span (mem_range_self i)⟩, -- `s` restricted to `I`
let sI_basis : basis I R N, -- `s` restricted to `I` is a basis of `N`
from basis.span indepI,
-- Our first goal is to build `A ≠ 0` such that `A • M ⊆ N`
have exists_a : ∀ i : ι, ∃ a : R, a ≠ 0 ∧ a • s i ∈ N,
{ intro i,
by_cases hi : i ∈ I,
{ use [1, zero_ne_one.symm],
rw one_smul,
exact subset_span (mem_range_self (⟨i, hi⟩ : I)) },
{ simpa [image_eq_range s I] using hI i hi } },
choose a ha ha' using exists_a,
let A := ∏ i, a i,
have hA : A ≠ 0,
{ rw finset.prod_ne_zero_iff,
simpa using ha },
-- `M ≃ A • M` because `M` is torsion free and `A ≠ 0`
let φ : M →ₗ[R] M := linear_map.lsmul R M A,
have : φ.ker = ⊥,
from linear_map.ker_lsmul hA,
let ψ : M ≃ₗ[R] φ.range := linear_equiv.of_injective φ (linear_map.ker_eq_bot.mp this),
have : φ.range ≤ N, -- as announced, `A • M ⊆ N`
{ suffices : ∀ i, φ (s i) ∈ N,
{ rw [linear_map.range_eq_map, ← hs, φ.map_span_le],
rintros _ ⟨i, rfl⟩, apply this },
intro i,
calc (∏ j, a j) • s i = (∏ j in {i}ᶜ, a j) • a i • s i :
by rw [fintype.prod_eq_prod_compl_mul i, mul_smul]
... ∈ N : N.smul_mem _ (ha' i) },
-- Since a submodule of a free `R`-module is free, we get that `A • M` is free
obtain ⟨n, b : basis (fin n) R φ.range⟩ := submodule.basis_of_pid_of_le this sI_basis,
-- hence `M` is free.
exact ⟨n, b.map ψ.symm⟩
end
/-- A finite type torsion free module over a PID is free. -/
noncomputable def module.free_of_finite_type_torsion_free' [module.finite R M]
[no_zero_smul_divisors R M] :
Σ (n : ℕ), basis (fin n) R M :=
module.free_of_finite_type_torsion_free module.finite.exists_fin.some_spec.some_spec
section smith_normal
/-- A Smith normal form basis for a submodule `N` of a module `M` consists of
bases for `M` and `N` such that the inclusion map `N → M` can be written as a
(rectangular) matrix with `a` along the diagonal: in Smith normal form. -/
@[nolint has_nonempty_instance]
structure basis.smith_normal_form (N : submodule R M) (ι : Type*) (n : ℕ) :=
(bM : basis ι R M)
(bN : basis (fin n) R N)
(f : fin n ↪ ι)
(a : fin n → R)
(snf : ∀ i, (bN i : M) = a i • bM (f i))
/-- If `M` is finite free over a PID `R`, then any submodule `N` is free
and we can find a basis for `M` and `N` such that the inclusion map is a diagonal matrix
in Smith normal form.
See `submodule.smith_normal_form_of_le` for a version of this theorem that returns
a `basis.smith_normal_form`.
This is a strengthening of `submodule.basis_of_pid_of_le`.
-/
theorem submodule.exists_smith_normal_form_of_le [fintype ι]
(b : basis ι R M) (N O : submodule R M) (N_le_O : N ≤ O) :
∃ (n o : ℕ) (hno : n ≤ o) (bO : basis (fin o) R O) (bN : basis (fin n) R N) (a : fin n → R),
∀ i, (bN i : M) = a i • bO (fin.cast_le hno i) :=
begin
revert N,
refine induction_on_rank b _ _ O,
intros M ih N N_le_M,
obtain ⟨m, b'M⟩ := M.basis_of_pid b,
by_cases N_bot : N = ⊥,
{ subst N_bot,
exact ⟨0, m, nat.zero_le _, b'M, basis.empty _, fin_zero_elim, fin_zero_elim⟩ },
obtain ⟨y, hy, a, hay, M', M'_le_M, N', N'_le_N, N'_le_M', y_ortho, ay_ortho, h⟩ :=
submodule.basis_of_pid_aux M N b'M N_bot N_le_M,
obtain ⟨n', m', hn'm', bM', bN', as', has'⟩ := ih M' M'_le_M y hy y_ortho N' N'_le_M',
obtain ⟨bN, h'⟩ := h n' bN',
obtain ⟨hmn, bM, h''⟩ := h' m' hn'm' bM',
obtain ⟨as, has⟩ := h'' as' has',
exact ⟨_, _, hmn, bM, bN, as, has⟩
end
/-- If `M` is finite free over a PID `R`, then any submodule `N` is free
and we can find a basis for `M` and `N` such that the inclusion map is a diagonal matrix
in Smith normal form.
See `submodule.exists_smith_normal_form_of_le` for a version of this theorem that doesn't
need to map `N` into a submodule of `O`.
This is a strengthening of `submodule.basis_of_pid_of_le`.
-/
noncomputable def submodule.smith_normal_form_of_le [fintype ι]
(b : basis ι R M) (N O : submodule R M) (N_le_O : N ≤ O) :
Σ (o n : ℕ), basis.smith_normal_form (N.comap O.subtype) (fin o) n :=
begin
choose n o hno bO bN a snf using N.exists_smith_normal_form_of_le b O N_le_O,
refine ⟨o, n, bO, bN.map (comap_subtype_equiv_of_le N_le_O).symm, (fin.cast_le hno).to_embedding,
a, λ i, _⟩,
ext,
simp only [snf, basis.map_apply, submodule.comap_subtype_equiv_of_le_symm_apply_coe_coe,
submodule.coe_smul_of_tower, rel_embedding.coe_fn_to_embedding]
end
/-- If `M` is finite free over a PID `R`, then any submodule `N` is free
and we can find a basis for `M` and `N` such that the inclusion map is a diagonal matrix
in Smith normal form.
This is a strengthening of `submodule.basis_of_pid`.
See also `ideal.smith_normal_form`, which moreover proves that the dimension of
an ideal is the same as the dimension of the whole ring.
-/
noncomputable def submodule.smith_normal_form [fintype ι] (b : basis ι R M) (N : submodule R M) :
Σ (n : ℕ), basis.smith_normal_form N ι n :=
let ⟨m, n, bM, bN, f, a, snf⟩ := N.smith_normal_form_of_le b ⊤ le_top,
bM' := bM.map (linear_equiv.of_top _ rfl),
e := bM'.index_equiv b in
⟨n, bM'.reindex e, bN.map (comap_subtype_equiv_of_le le_top), f.trans e.to_embedding, a,
λ i, by simp only [snf, basis.map_apply, linear_equiv.of_top_apply, submodule.coe_smul_of_tower,
submodule.comap_subtype_equiv_of_le_apply_coe, coe_coe, basis.reindex_apply,
equiv.to_embedding_apply, function.embedding.trans_apply,
equiv.symm_apply_apply]⟩
/-- If `S` a finite-dimensional ring extension of a PID `R` which is free as an `R`-module,
then any nonzero `S`-ideal `I` is free as an `R`-submodule of `S`, and we can
find a basis for `S` and `I` such that the inclusion map is a square diagonal
matrix.
See `ideal.exists_smith_normal_form` for a version of this theorem that doesn't
need to map `I` into a submodule of `R`.
This is a strengthening of `submodule.basis_of_pid`.
-/
noncomputable def ideal.smith_normal_form
[fintype ι] {S : Type*} [comm_ring S] [is_domain S] [algebra R S]
(b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) :
basis.smith_normal_form (I.restrict_scalars R) ι (fintype.card ι) :=
let ⟨n, bS, bI, f, a, snf⟩ := (I.restrict_scalars R).smith_normal_form b in
have eq : _ := ideal.rank_eq bS hI (bI.map ((restrict_scalars_equiv R S S I).restrict_scalars _)),
let e : fin n ≃ fin (fintype.card ι) := fintype.equiv_of_card_eq (by rw [eq, fintype.card_fin]) in
⟨bS, bI.reindex e, e.symm.to_embedding.trans f, a ∘ e.symm, λ i,
by simp only [snf, basis.coe_reindex, function.embedding.trans_apply, equiv.to_embedding_apply]⟩
/-- If `S` a finite-dimensional ring extension of a PID `R` which is free as an `R`-module,
then any nonzero `S`-ideal `I` is free as an `R`-submodule of `S`, and we can
find a basis for `S` and `I` such that the inclusion map is a square diagonal
matrix.
See also `ideal.smith_normal_form` for a version of this theorem that returns
a `basis.smith_normal_form`.
-/
theorem ideal.exists_smith_normal_form
[fintype ι] {S : Type*} [comm_ring S] [is_domain S] [algebra R S]
(b : basis ι R S) (I : ideal S) (hI : I ≠ ⊥) :
∃ (b' : basis ι R S) (a : ι → R) (ab' : basis ι R I),
∀ i, (ab' i : S) = a i • b' i :=
let ⟨bS, bI, f, a, snf⟩ := I.smith_normal_form b hI,
e : fin (fintype.card ι) ≃ ι := equiv.of_bijective f
((fintype.bijective_iff_injective_and_card f).mpr ⟨f.injective, fintype.card_fin _⟩) in
have fe : ∀ i, f (e.symm i) = i := e.apply_symm_apply,
⟨bS, a ∘ e.symm, (bI.reindex e).map ((restrict_scalars_equiv _ _ _ _).restrict_scalars R), λ i,
by simp only [snf, fe, basis.map_apply, linear_equiv.restrict_scalars_apply,
submodule.restrict_scalars_equiv_apply, basis.coe_reindex]⟩
end smith_normal
end principal_ideal_domain
/-- A set of linearly independent vectors in a module `M` over a semiring `S` is also linearly
independent over a subring `R` of `K`. -/
lemma linear_independent.restrict_scalars_algebras {R S M ι : Type*} [comm_semiring R] [semiring S]
[add_comm_monoid M] [algebra R S] [module R M] [module S M] [is_scalar_tower R S M]
(hinj : function.injective (algebra_map R S)) {v : ι → M} (li : linear_independent S v) :
linear_independent R v :=
linear_independent.restrict_scalars (by rwa algebra.algebra_map_eq_smul_one' at hinj) li
|
5c55cf46bc1d0386b913f64e3bff9dced3a33a81 | f4bff2062c030df03d65e8b69c88f79b63a359d8 | /src/game/sup_inf/GLBprop_if_LUBprop.lean | c296e88383ec5478015c6512882b7d221726ae27 | [
"Apache-2.0"
] | permissive | adastra7470/real-number-game | 776606961f52db0eb824555ed2f8e16f92216ea3 | f9dcb7d9255a79b57e62038228a23346c2dc301b | refs/heads/master | 1,669,221,575,893 | 1,594,669,800,000 | 1,594,669,800,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,401 | lean | import game.sup_inf.rat_complete
namespace xena --hide
/-
# Chapter 3 : Sup and Inf
## Level 14
-/
/-
The Least Upper Bound property implies
Greatest Lower bound Property
-/
-- begin hide
--NOTE: We have a form of the completeness axiom at Sup/Inf World
--level 13.
--Here I'll assume LUB property as an axiom, and prove it implies
--GLB property. But perhaps `axiom` should be avoided. -- GT
-- end hide
def is_bdd_above (S : set ℝ) := ∃ x : ℝ, is_upper_bound S x
def is_lower_bound (S : set ℝ) (x : ℝ) := ∀ s ∈ S, x ≤ s
def is_bdd_below (S : set ℝ) := ∃ x : ℝ, is_lower_bound S x
def is_glb (S : set ℝ) (x : ℝ) := is_lower_bound S x ∧
∀ y : ℝ, is_lower_bound S y → y ≤ x
def has_glb (S : set ℝ) := ∃ x : ℝ, is_glb S x
/-
Completeness Axiom
-/
axiom lub_property_reals (S : set ℝ) :
(S.nonempty ∧ is_bdd_above S) → (has_lub S)
/- Lemma
LUB property implies GLB property
-/
theorem glb_property_reals (S: set ℝ) :
(S.nonempty ∧ is_bdd_below S) → has_glb S :=
begin
intro hyp,
--define set L of lower bounds of S
let L := { x : ℝ | is_lower_bound S x},
--anything in S is an upper bound of L
have fact1: ∀ x ∈ S, is_upper_bound L x,
intros x hypx b hypb,
exact hypb x hypx, -- `suggest` provided this line
--hyp.left is the claim that S is nonempty. use this to show
--that L is bounded above
cases hyp.left with y hypy,
have fact2: is_bdd_above L, use y, exact fact1 y hypy,
-- can now show that L has supremum `a`. Note direct use of hyp.right
-- (S is bounded below) as proof that L is nonempty
have fact3:= lub_property_reals L (and.intro hyp.right fact2),
cases fact3 with a hypa,
-- we now show that a is the infimum of S
use a,
split,
-- first prove that a is a lower bound for S,
{
assume z hypz,
-- given z ∈ S, we prove by contradiction that a ≤ z,
by_contradiction claim,
push_neg at claim,
unfold is_lub at hypa,
-- hypa.right says that for any upper bound y of L, a ≤ y
let for_contra := hypa.right z (fact1 z hypz),
-- linarith solves our goal - claim and for_contra are contradictory.
linarith,
},
-- now prove that for any lower bound x of S, x ≤ a
{
intros x hypx,
have fact: x ∈ L, exact hypx,
-- hypa.left says that a is an upper bound of L
exact hypa.left x hypx,
}
end
end xena -- hide
|
330e1b87e58ae14b3ceba733400cc0755216c924 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/int/order/units.lean | d9970503fa589d3bbb24b11aff2862aa4b656ad7 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,938 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
-/
import data.int.order.basic
import data.int.units
import algebra.group_power.order
/-!
# Lemmas about units in `ℤ`, which interact with the order structure.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
namespace int
lemma is_unit_iff_abs_eq {x : ℤ} : is_unit x ↔ abs x = 1 :=
by rw [is_unit_iff_nat_abs_eq, abs_eq_nat_abs, ←int.coe_nat_one, coe_nat_inj']
lemma is_unit_sq {a : ℤ} (ha : is_unit a) : a ^ 2 = 1 :=
by rw [sq, is_unit_mul_self ha]
@[simp] lemma units_sq (u : ℤˣ) : u ^ 2 = 1 :=
by rw [units.ext_iff, units.coe_pow, units.coe_one, is_unit_sq u.is_unit]
alias units_sq ← units_pow_two
@[simp] lemma units_mul_self (u : ℤˣ) : u * u = 1 :=
by rw [←sq, units_sq]
@[simp] lemma units_inv_eq_self (u : ℤˣ) : u⁻¹ = u :=
by rw [inv_eq_iff_mul_eq_one, units_mul_self]
-- `units.coe_mul` is a "wrong turn" for the simplifier, this undoes it and simplifies further
@[simp] lemma units_coe_mul_self (u : ℤˣ) : (u * u : ℤ) = 1 :=
by rw [←units.coe_mul, units_mul_self, units.coe_one]
@[simp] lemma neg_one_pow_ne_zero {n : ℕ} : (-1 : ℤ)^n ≠ 0 :=
pow_ne_zero _ (abs_pos.mp (by simp))
lemma sq_eq_one_of_sq_lt_four {x : ℤ} (h1 : x ^ 2 < 4) (h2 : x ≠ 0) : x ^ 2 = 1 :=
sq_eq_one_iff.mpr ((abs_eq (zero_le_one' ℤ)).mp (le_antisymm (lt_add_one_iff.mp
(abs_lt_of_sq_lt_sq h1 zero_le_two)) (sub_one_lt_iff.mp (abs_pos.mpr h2))))
lemma sq_eq_one_of_sq_le_three {x : ℤ} (h1 : x ^ 2 ≤ 3) (h2 : x ≠ 0) : x ^ 2 = 1 :=
sq_eq_one_of_sq_lt_four (lt_of_le_of_lt h1 (lt_add_one 3)) h2
lemma units_pow_eq_pow_mod_two (u : ℤˣ) (n : ℕ) : u ^ n = u ^ (n % 2) :=
by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_sq, one_pow, mul_one]
end int
|
08c214daef066fbb1dca4234834ad7b6dded5ae5 | e898bfefd5cb60a60220830c5eba68cab8d02c79 | /uexp/src/uexp/rules/mergeJoinFilter.lean | 9079895166d74333d3796ce0117120f9cdef16f7 | [
"BSD-2-Clause"
] | permissive | kkpapa/Cosette | 9ed09e2dc4c1ecdef815c30b5501f64a7383a2ce | fda8fdbbf0de6c1be9b4104b87bbb06cede46329 | refs/heads/master | 1,584,573,128,049 | 1,526,370,422,000 | 1,526,370,422,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,581 | lean | import ..sql
import ..tactics
import ..u_semiring
import ..extra_constants
import ..ucongr
import ..TDP
set_option profiler true
open Expr
open Proj
open Pred
open SQL
open tree
notation `int` := datatypes.int
variable integer_10: const datatypes.int
theorem rule:
forall ( Γ scm_dept scm_emp: Schema) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp),
denoteSQL ((SELECT * FROM1 ((SELECT1 (combine (right⋅right⋅dept_deptno) (right⋅left⋅emp_ename)) FROM1 (product (table rel_emp) (table rel_dept)) WHERE (and (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅dept_deptno))) (equal (uvariable (right⋅right⋅dept_deptno)) (constantExpr integer_10))))) WHERE (equal (uvariable (right⋅left)) (constantExpr integer_10))) :SQL Γ _)
=
denoteSQL ((SELECT1 (combine (right⋅right⋅dept_deptno) (right⋅left⋅emp_ename)) FROM1 (product (table rel_emp) ((SELECT * FROM1 (table rel_dept) WHERE (equal (uvariable (right⋅dept_deptno)) (constantExpr integer_10))))) WHERE (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅dept_deptno)))) :SQL Γ _) :=
begin
intros,
unfold_all_denotations,
funext,
simp,
print_size,
TDP' ucongr,
end |
f9d4df07ddd69bc9729ec3c3c6b81c615111a08a | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/badreset.lean | aa4ea33e55e809231d88c8254d315b78c5b279d4 | [
"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 | 706 | lean | @[noinline] def g (x : Nat × Nat) := x
set_option trace.compiler.boxed true
set_option trace.compiler.lambda_pure true
@[noinline] def f (b : Bool) (x : Nat × Nat) : (Nat × Nat) × (Nat × Nat) :=
let done (y : Nat × Nat) := (g (g (g x)), y) in
match b with
| true := match x with | (a, b) := done (a, 0)
| false := match x with | (a, b) := done (0, b)
@[noinline] def h {α : Type} (x : Nat × α) := x.1
def tst2 (p : Nat × (Except Nat Nat)) : Nat × Nat :=
match p with
| (a, b) :=
let done (x : Nat) := (h p + 1, x) in
match b with
| Except.ok v := done v
| Except.error w := done w
def main (xs : List String) : IO Unit :=
IO.println $ f true (xs.head.toNat, xs.tail.head.toNat)
|
3bfb01c1642e84f3653837740529cc7e14e8512d | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /stage0/src/Lean/ParserCompiler.lean | 0076f3c4e8693df3a8f4aac77ca51a15b5d54fcd | [
"Apache-2.0"
] | permissive | dupuisf/lean4 | d082d13b01243e1de29ae680eefb476961221eef | 6a39c65bd28eb0e28c3870188f348c8914502718 | refs/heads/master | 1,676,948,755,391 | 1,610,665,114,000 | 1,610,665,114,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,383 | lean | /-
Copyright (c) 2020 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
/-!
Gadgets for compiling parser declarations into other programs, such as pretty printers.
-/
import Lean.Util.ReplaceExpr
import Lean.Meta.Basic
import Lean.Meta.WHNF
import Lean.ParserCompiler.Attribute
import Lean.Parser.Extension
namespace Lean
namespace ParserCompiler
structure Context (α : Type) where
varName : Name
categoryAttr : KeyedDeclsAttribute α
combinatorAttr : CombinatorAttribute
def Context.tyName {α} (ctx : Context α) : Name := ctx.categoryAttr.defn.valueTypeName
-- replace all references of `Parser` with `tyName`
def replaceParserTy {α} (ctx : Context α) (e : Expr) : Expr :=
e.replace fun e =>
-- strip `optParam`
let e := if e.isOptParam then e.appFn!.appArg! else e
if e.isConstOf `Lean.Parser.Parser then mkConst ctx.tyName else none
section
open Meta
variables {α} (ctx : Context α) (force : Bool := false) in
/--
Translate an expression of type `Parser` into one of type `tyName`, tagging intermediary constants with
`ctx.combinatorAttr`. If `force` is `false`, refuse to do so for imported constants. -/
partial def compileParserExpr (e : Expr) : MetaM Expr := do
let e ← whnfCore e
match e with
| e@(Expr.lam _ _ _ _) => lambdaLetTelescope e fun xs b => compileParserExpr b >>= mkLambdaFVars xs
| e@(Expr.fvar _ _) => pure e
| _ => do
let fn := e.getAppFn
let Expr.const c _ _ ← pure fn
| throwError! "call of unknown parser at '{e}'"
let args := e.getAppArgs
-- call the translated `p` with (a prefix of) the arguments of `e`, recursing for arguments
-- of type `ty` (i.e. formerly `Parser`)
let mkCall (p : Name) := do
let ty ← inferType (mkConst p)
forallTelescope ty fun params _ => do
let mut p := mkConst p
let args := e.getAppArgs
for i in [:Nat.min params.size args.size] do
let param := params[i]
let arg := args[i]
let paramTy ← inferType param
let resultTy ← forallTelescope paramTy fun _ b => pure b
let arg ← if resultTy.isConstOf ctx.tyName then compileParserExpr arg else pure arg
p := mkApp p arg
pure p
let env ← getEnv
match ctx.combinatorAttr.getDeclFor? env c with
| some p => mkCall p
| none =>
let c' := c ++ ctx.varName
let cinfo ← getConstInfo c
let resultTy ← forallTelescope cinfo.type fun _ b => pure b
if resultTy.isConstOf `Lean.Parser.TrailingParser || resultTy.isConstOf `Lean.Parser.Parser then do
-- synthesize a new `[combinatorAttr c]`
let some value ← pure cinfo.value?
| throwError! "don't know how to generate {ctx.varName} for non-definition '{e}'"
unless (env.getModuleIdxFor? c).isNone || force do
throwError! "refusing to generate code for imported parser declaration '{c}'; use `@[runParserAttributeHooks]` on its definition instead."
let value ← compileParserExpr $ replaceParserTy ctx value
let ty ← forallTelescope cinfo.type fun params _ =>
params.foldrM (init := mkConst ctx.tyName) fun param ty => do
let paramTy ← replaceParserTy ctx <$> inferType param
pure $ mkForall `_ BinderInfo.default paramTy ty
let decl := Declaration.defnDecl {
name := c', lparams := [],
type := ty, value := value, hints := ReducibilityHints.opaque, safety := DefinitionSafety.safe }
let env ← getEnv
let env ← match env.addAndCompile {} decl with
| Except.ok env => pure env
| Except.error kex => do throwError (← (kex.toMessageData {}).toString)
setEnv $ ctx.combinatorAttr.setDeclFor env c c'
mkCall c'
else
-- if this is a generic function, e.g. `AndThen.andthen`, it's easier to just unfold it until we are
-- back to parser combinators
let some e' ← unfoldDefinition? e
| throwError! "don't know how to generate {ctx.varName} for non-parser combinator '{e}'"
compileParserExpr e'
end
open Core
/-- Compile the given declaration into a `[(builtin)categoryAttr declName]` -/
def compileCategoryParser {α} (ctx : Context α) (declName : Name) (builtin : Bool) : AttrM Unit := do
-- This will also tag the declaration as a `[combinatorParenthesizer declName]` in case the parser is used by other parsers.
-- Note that simply having `[(builtin)Parenthesizer]` imply `[combinatorParenthesizer]` is not ideal since builtin
-- attributes are active only in the next stage, while `[combinatorParenthesizer]` is active immediately (since we never
-- call them at compile time but only reference them).
let (Expr.const c' _ _) ← (compileParserExpr ctx (mkConst declName) (force := false)).run'
| unreachable!
-- We assume that for tagged parsers, the kind is equal to the declaration name. This is automatically true for parsers
-- using `parser!` or `syntax`.
let kind := declName
let attrName := if builtin then ctx.categoryAttr.defn.builtinName else ctx.categoryAttr.defn.name
-- Create syntax node for a simple attribute of the form
-- `def simple := parser! ident >> optional (ident <|> priorityParser)`
let stx := Syntax.node `Lean.Parser.Attr.simple #[
mkIdent attrName,
mkNullNode #[mkIdent kind]
]
Attribute.add c' attrName stx
variables {α} (ctx : Context α) in
def compileEmbeddedParsers : ParserDescr → MetaM Unit
| ParserDescr.const _ => pure ()
| ParserDescr.unary _ d => compileEmbeddedParsers d
| ParserDescr.binary _ d₁ d₂ => compileEmbeddedParsers d₁ *> compileEmbeddedParsers d₂
| ParserDescr.parser constName => discard $ compileParserExpr ctx (mkConst constName) (force := false)
| ParserDescr.node _ _ d => compileEmbeddedParsers d
| ParserDescr.nodeWithAntiquot _ _ d => compileEmbeddedParsers d
| ParserDescr.sepBy p _ psep _ => compileEmbeddedParsers p *> compileEmbeddedParsers psep
| ParserDescr.sepBy1 p _ psep _ => compileEmbeddedParsers p *> compileEmbeddedParsers psep
| ParserDescr.trailingNode _ _ d => compileEmbeddedParsers d
| ParserDescr.symbol _ => pure ()
| ParserDescr.nonReservedSymbol _ _ => pure ()
| ParserDescr.cat _ _ => pure ()
/-- Precondition: `α` must match `ctx.tyName`. -/
unsafe def registerParserCompiler {α} (ctx : Context α) : IO Unit := do
Parser.registerParserAttributeHook {
postAdd := fun catName constName builtin => do
let info ← getConstInfo constName
if info.type.isConstOf `Lean.ParserDescr || info.type.isConstOf `Lean.TrailingParserDescr then
let d ← evalConstCheck ParserDescr `Lean.ParserDescr constName <|>
evalConstCheck TrailingParserDescr `Lean.TrailingParserDescr constName
compileEmbeddedParsers ctx d |>.run'
else
if catName.isAnonymous then
-- `[runBuiltinParserAttributeHooks]` => force compilation even if imported, do not apply `ctx.categoryAttr`.
discard (compileParserExpr ctx (mkConst constName) (force := true)).run'
else
compileCategoryParser ctx constName builtin
}
end ParserCompiler
end Lean
|
cd7ee612f656bf52b3cf39e8946c45ddb11ce95c | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/typeclass_easy.lean | 30e2690bab99f0d7432e8544f31f9d55f2db7c6d | [
"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 | 168 | lean | new_frontend
#synth HasToString (Nat × (Nat × Bool))
#synth HasAdd Nat
#synth HasCoe Bool Prop
#synth Decidable (True ∧ 1 = 1)
#synth DecidableEq (Nat × Nat)
|
9a2377b7d391870985ec104419f10e35d345ae77 | a88f0086fb3e2025ebb21e0ba2f2725774c6979f | /src/topology/continuous_on.lean | a0f5de2dec56bf7f3aff2a23de0d2c1d1b21bf06 | [
"Apache-2.0"
] | permissive | Kha/stdlib | b5a4456c35def0ca8f1bf2d32dbeebd7639cbc4d | e44b105c72ec77120f43a7a7dd1cd49867a65a41 | refs/heads/master | 1,609,528,111,500 | 1,572,963,395,000 | 1,572,963,395,000 | 98,516,307 | 0 | 1 | null | 1,501,146,352,000 | 1,501,146,352,000 | null | UTF-8 | Lean | false | false | 18,667 | lean | /-
Copyright (c) 2019 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.constructions
/-!
# Neighborhoods and continuity relative to a subset
This file defines relative versions
`nhds_within` of `nhds`
`continuous_on` of `continuous`
`continuous_within_at` of `continuous_at`
and proves their basic properties, including the relationships between
these restricted notions and the corresponding notions for the subtype
equipped with the subspace topology.
-/
open set filter
variables {α : Type*} {β : Type*} {γ : Type*}
variables [topological_space α]
/-- The "neighborhood within" filter. Elements of `nhds_within a s` are sets containing the
intersection of `s` and a neighborhood of `a`. -/
def nhds_within (a : α) (s : set α) : filter α := nhds a ⊓ principal s
theorem nhds_within_eq (a : α) (s : set α) :
nhds_within a s = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (t ∩ s) :=
have set.univ ∈ {s : set α | a ∈ s ∧ is_open s}, from ⟨set.mem_univ _, is_open_univ⟩,
begin
rw [nhds_within, nhds, lattice.binfi_inf]; try { exact this },
simp only [inf_principal]
end
theorem nhds_within_univ (a : α) : nhds_within a set.univ = nhds a :=
by rw [nhds_within, principal_univ, lattice.inf_top_eq]
theorem mem_nhds_within (t : set α) (a : α) (s : set α) :
t ∈ nhds_within a s ↔ ∃ u, is_open u ∧ a ∈ u ∧ u ∩ s ⊆ t :=
begin
rw [nhds_within, mem_inf_principal, mem_nhds_sets_iff], split,
{ rintros ⟨u, hu, openu, au⟩,
exact ⟨u, openu, au, λ x ⟨xu, xs⟩, hu xu xs⟩ },
rintros ⟨u, openu, au, hu⟩,
exact ⟨u, λ x xu xs, hu ⟨xu, xs⟩, openu, au⟩
end
theorem self_mem_nhds_within {a : α} {s : set α} : s ∈ nhds_within a s :=
begin
rw [nhds_within, mem_inf_principal],
simp only [imp_self],
exact univ_mem_sets
end
theorem inter_mem_nhds_within (s : set α) {t : set α} {a : α} (h : t ∈ nhds a) :
s ∩ t ∈ nhds_within a s :=
inter_mem_sets (mem_inf_sets_of_right (mem_principal_self s)) (mem_inf_sets_of_left h)
theorem nhds_within_mono (a : α) {s t : set α} (h : s ⊆ t) : nhds_within a s ≤ nhds_within a t :=
lattice.inf_le_inf (le_refl _) (principal_mono.mpr h)
theorem nhds_within_restrict'' {a : α} (s : set α) {t : set α} (h : t ∈ nhds_within a s) :
nhds_within a s = nhds_within a (s ∩ t) :=
le_antisymm
(lattice.le_inf lattice.inf_le_left (le_principal_iff.mpr (inter_mem_sets self_mem_nhds_within h)))
(lattice.inf_le_inf (le_refl _) (principal_mono.mpr (set.inter_subset_left _ _)))
theorem nhds_within_restrict' {a : α} (s : set α) {t : set α} (h : t ∈ nhds a) :
nhds_within a s = nhds_within a (s ∩ t) :=
nhds_within_restrict'' s $ mem_inf_sets_of_left h
theorem nhds_within_restrict {a : α} (s : set α) {t : set α} (h₀ : a ∈ t) (h₁ : is_open t) :
nhds_within a s = nhds_within a (s ∩ t) :=
nhds_within_restrict' s (mem_nhds_sets h₁ h₀)
theorem nhds_within_le_of_mem {a : α} {s t : set α} (h : s ∈ nhds_within a t) :
nhds_within a t ≤ nhds_within a s :=
begin
rcases (mem_nhds_within _ _ _).1 h with ⟨u, u_open, au, uts⟩,
have : nhds_within a t = nhds_within a (t ∩ u) := nhds_within_restrict _ au u_open,
rw [this, inter_comm],
exact nhds_within_mono _ uts
end
theorem nhds_within_eq_nhds_within {a : α} {s t u : set α}
(h₀ : a ∈ s) (h₁ : is_open s) (h₂ : t ∩ s = u ∩ s) :
nhds_within a t = nhds_within a u :=
by rw [nhds_within_restrict t h₀ h₁, nhds_within_restrict u h₀ h₁, h₂]
theorem nhds_within_eq_of_open {a : α} {s : set α} (h₀ : a ∈ s) (h₁ : is_open s) :
nhds_within a s = nhds a :=
by rw [←nhds_within_univ]; apply nhds_within_eq_nhds_within h₀ h₁;
rw [set.univ_inter, set.inter_self]
@[simp] theorem nhds_within_empty (a : α) : nhds_within a {} = ⊥ :=
by rw [nhds_within, principal_empty, lattice.inf_bot_eq]
theorem nhds_within_union (a : α) (s t : set α) :
nhds_within a (s ∪ t) = nhds_within a s ⊔ nhds_within a t :=
by unfold nhds_within; rw [←lattice.inf_sup_left, sup_principal]
theorem nhds_within_inter (a : α) (s t : set α) :
nhds_within a (s ∩ t) = nhds_within a s ⊓ nhds_within a t :=
by unfold nhds_within; rw [lattice.inf_left_comm, lattice.inf_assoc, inf_principal,
←lattice.inf_assoc, lattice.inf_idem]
theorem nhds_within_inter' (a : α) (s t : set α) :
nhds_within a (s ∩ t) = (nhds_within a s) ⊓ principal t :=
by { unfold nhds_within, rw [←inf_principal, lattice.inf_assoc] }
theorem tendsto_if_nhds_within {f g : α → β} {p : α → Prop} [decidable_pred p]
{a : α} {s : set α} {l : filter β}
(h₀ : tendsto f (nhds_within a (s ∩ p)) l)
(h₁ : tendsto g (nhds_within a (s ∩ {x | ¬ p x})) l) :
tendsto (λ x, if p x then f x else g x) (nhds_within a s) l :=
by apply tendsto_if; rw [←nhds_within_inter']; assumption
lemma map_nhds_within (f : α → β) (a : α) (s : set α) :
map f (nhds_within a s) =
⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (set.image f (t ∩ s)) :=
have h₀ : directed_on ((λ (i : set α), principal (i ∩ s)) ⁻¹'o ge)
{x : set α | x ∈ {t : set α | a ∈ t ∧ is_open t}}, from
assume x ⟨ax, openx⟩ y ⟨ay, openy⟩,
⟨x ∩ y, ⟨⟨ax, ay⟩, is_open_inter openx openy⟩,
le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_left _ _)),
le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_right _ _))⟩,
have h₁ : ∃ (i : set α), i ∈ {t : set α | a ∈ t ∧ is_open t},
from ⟨set.univ, set.mem_univ _, is_open_univ⟩,
by { rw [nhds_within_eq, map_binfi_eq h₀ h₁], simp only [map_principal] }
theorem tendsto_nhds_within_mono_left {f : α → β} {a : α}
{s t : set α} {l : filter β} (hst : s ⊆ t) (h : tendsto f (nhds_within a t) l) :
tendsto f (nhds_within a s) l :=
tendsto_le_left (nhds_within_mono a hst) h
theorem tendsto_nhds_within_mono_right {f : β → α} {l : filter β}
{a : α} {s t : set α} (hst : s ⊆ t) (h : tendsto f l (nhds_within a s)) :
tendsto f l (nhds_within a t) :=
tendsto_le_right (nhds_within_mono a hst) h
theorem tendsto_nhds_within_of_tendsto_nhds {f : α → β} {a : α}
{s : set α} {l : filter β} (h : tendsto f (nhds a) l) :
tendsto f (nhds_within a s) l :=
by rw [←nhds_within_univ] at h; exact tendsto_nhds_within_mono_left (set.subset_univ _) h
theorem principal_subtype {α : Type*} (s : set α) (t : set {x // x ∈ s}) :
principal t = comap subtype.val (principal (subtype.val '' t)) :=
by rw comap_principal; rw set.preimage_image_eq; apply subtype.val_injective
/-
nhds_within and subtypes
-/
theorem mem_nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t u : set {x // x ∈ s}) :
t ∈ nhds_within a u ↔
t ∈ comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' u)) :=
by rw [nhds_within, nhds_subtype, principal_subtype, ←comap_inf, ←nhds_within]
theorem nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) :
nhds_within a t = comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' t)) :=
filter_eq $ by ext u; rw mem_nhds_within_subtype
theorem nhds_within_eq_map_subtype_val {s : set α} {a : α} (h : a ∈ s) :
nhds_within a s = map subtype.val (nhds ⟨a, h⟩) :=
have h₀ : s ∈ nhds_within a s,
by { rw [mem_nhds_within], existsi set.univ, simp [set.diff_eq] },
have h₁ : ∀ y ∈ s, ∃ x, @subtype.val _ s x = y,
from λ y h, ⟨⟨y, h⟩, rfl⟩,
begin
rw [←nhds_within_univ, nhds_within_subtype, subtype.val_image_univ],
exact (map_comap_of_surjective' h₀ h₁).symm,
end
theorem tendsto_nhds_within_iff_subtype {s : set α} {a : α} (h : a ∈ s) (f : α → β) (l : filter β) :
tendsto f (nhds_within a s) l ↔ tendsto (function.restrict f s) (nhds ⟨a, h⟩) l :=
by rw [tendsto, tendsto, function.restrict, nhds_within_eq_map_subtype_val h,
←(@filter.map_map _ _ _ _ subtype.val)]
variables [topological_space β] [topological_space γ]
/-- A function between topological spaces is continuous at a point `x₀` within a subset `s`
if `f x` tends to `f x₀` when `x` tends to `x₀` while staying within `s`. -/
def continuous_within_at (f : α → β) (s : set α) (x : α) : Prop :=
tendsto f (nhds_within x s) (nhds (f x))
/-- A function between topological spaces is continuous on a subset `s`
when it's continuous at every point of `s` within `s`. -/
def continuous_on (f : α → β) (s : set α) : Prop := ∀ x ∈ s, continuous_within_at f s x
theorem continuous_within_at_univ (f : α → β) (x : α) :
continuous_within_at f set.univ x ↔ continuous_at f x :=
by rw [continuous_at, continuous_within_at, nhds_within_univ]
theorem continuous_within_at_iff_continuous_at_restrict (f : α → β) {x : α} {s : set α} (h : x ∈ s) :
continuous_within_at f s x ↔ continuous_at (function.restrict f s) ⟨x, h⟩ :=
tendsto_nhds_within_iff_subtype h f _
theorem continuous_within_at.tendsto_nhds_within_image {f : α → β} {x : α} {s : set α}
(h : continuous_within_at f s x) :
tendsto f (nhds_within x s) (nhds_within (f x) (f '' s)) :=
tendsto_inf.2 ⟨h, tendsto_principal.2 $
mem_inf_sets_of_right $ mem_principal_sets.2 $
λ x, mem_image_of_mem _⟩
theorem continuous_on_iff {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ x ∈ s, ∀ t : set β, is_open t → f x ∈ t → ∃ u, is_open u ∧ x ∈ u ∧
u ∩ s ⊆ f ⁻¹' t :=
by simp only [continuous_on, continuous_within_at, tendsto_nhds, mem_nhds_within]
theorem continuous_on_iff_continuous_restrict {f : α → β} {s : set α} :
continuous_on f s ↔ continuous (function.restrict f s) :=
begin
rw [continuous_on, continuous_iff_continuous_at], split,
{ rintros h ⟨x, xs⟩,
exact (continuous_within_at_iff_continuous_at_restrict f xs).mp (h x xs) },
intros h x xs,
exact (continuous_within_at_iff_continuous_at_restrict f xs).mpr (h ⟨x, xs⟩)
end
theorem continuous_on_iff' {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ t : set β, is_open t → ∃ u, is_open u ∧ f ⁻¹' t ∩ s = u ∩ s :=
have ∀ t, is_open (function.restrict f s ⁻¹' t) ↔ ∃ (u : set α), is_open u ∧ f ⁻¹' t ∩ s = u ∩ s,
begin
intro t,
rw [is_open_induced_iff, function.restrict_eq, set.preimage_comp],
simp only [subtype.preimage_val_eq_preimage_val_iff],
split; { rintros ⟨u, ou, useq⟩, exact ⟨u, ou, useq.symm⟩ }
end,
by rw [continuous_on_iff_continuous_restrict, continuous]; simp only [this]
theorem continuous_on_iff_is_closed {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ t : set β, is_closed t → ∃ u, is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s :=
have ∀ t, is_closed (function.restrict f s ⁻¹' t) ↔ ∃ (u : set α), is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s,
begin
intro t,
rw [is_closed_induced_iff, function.restrict_eq, set.preimage_comp],
simp only [subtype.preimage_val_eq_preimage_val_iff]
end,
by rw [continuous_on_iff_continuous_restrict, continuous_iff_is_closed]; simp only [this]
theorem nhds_within_le_comap {x : α} {s : set α} {f : α → β} (ctsf : continuous_within_at f s x) :
nhds_within x s ≤ comap f (nhds_within (f x) (f '' s)) :=
map_le_iff_le_comap.1 ctsf.tendsto_nhds_within_image
theorem continuous_within_at_iff_ptendsto_res (f : α → β) {x : α} {s : set α} :
continuous_within_at f s x ↔ ptendsto (pfun.res f s) (nhds x) (nhds (f x)) :=
tendsto_iff_ptendsto _ _ _ _
lemma continuous_iff_continuous_on_univ {f : α → β} : continuous f ↔ continuous_on f univ :=
by simp [continuous_iff_continuous_at, continuous_on, continuous_at, continuous_within_at,
nhds_within_univ]
lemma continuous_within_at.mono {f : α → β} {s t : set α} {x : α} (h : continuous_within_at f t x)
(hs : s ⊆ t) : continuous_within_at f s x :=
tendsto_le_left (nhds_within_mono x hs) h
lemma continuous_within_at_inter' {f : α → β} {s t : set α} {x : α} (h : t ∈ nhds_within x s) :
continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x :=
by simp [continuous_within_at, nhds_within_restrict'' s h]
lemma continuous_within_at_inter {f : α → β} {s t : set α} {x : α} (h : t ∈ nhds x) :
continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x :=
by simp [continuous_within_at, nhds_within_restrict' s h]
lemma continuous_on.congr_mono {f g : α → β} {s s₁ : set α} (h : continuous_on f s)
(h' : ∀x ∈ s₁, g x = f x) (h₁ : s₁ ⊆ s) : continuous_on g s₁ :=
begin
assume x hx,
unfold continuous_within_at,
have A := (h x (h₁ hx)).mono h₁,
unfold continuous_within_at at A,
rw ← h' x hx at A,
have : {x : α | g x = f x} ∈ nhds_within x s₁ := mem_inf_sets_of_right h',
apply tendsto.congr' _ A,
convert this,
ext,
finish
end
lemma continuous_at.continuous_within_at {f : α → β} {s : set α} {x : α} (h : continuous_at f x) :
continuous_within_at f s x :=
continuous_within_at.mono ((continuous_within_at_univ f x).2 h) (subset_univ _)
lemma continuous_within_at.continuous_at {f : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (hs : s ∈ nhds x) : continuous_at f x :=
begin
have : s = univ ∩ s, by rw univ_inter,
rwa [this, continuous_within_at_inter hs, continuous_within_at_univ] at h
end
lemma continuous_within_at.comp {g : β → γ} {f : α → β} {s : set α} {t : set β} {x : α}
(hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) (h : f '' s ⊆ t) :
continuous_within_at (g ∘ f) s x :=
begin
have : tendsto f (principal s) (principal t),
by { rw tendsto_principal_principal, exact λx hx, h (mem_image_of_mem _ hx) },
have : tendsto f (nhds_within x s) (principal t) :=
tendsto_le_left lattice.inf_le_right this,
have : tendsto f (nhds_within x s) (nhds_within (f x) t) :=
tendsto_inf.2 ⟨hf, this⟩,
exact tendsto.comp hg this
end
lemma continuous_on.comp {g : β → γ} {f : α → β} {s : set α} {t : set β}
(hg : continuous_on g t) (hf : continuous_on f s) (h : f '' s ⊆ t) :
continuous_on (g ∘ f) s :=
λx hx, continuous_within_at.comp (hg _ (h (mem_image_of_mem _ hx))) (hf x hx) h
lemma continuous_on.mono {f : α → β} {s t : set α} (hf : continuous_on f s) (h : t ⊆ s) :
continuous_on f t :=
λx hx, tendsto_le_left (nhds_within_mono _ h) (hf x (h hx))
lemma continuous.continuous_on {f : α → β} {s : set α} (h : continuous f) :
continuous_on f s :=
begin
rw continuous_iff_continuous_on_univ at h,
exact h.mono (subset_univ _)
end
lemma continuous.comp_continuous_on {g : β → γ} {f : α → β} {s : set α}
(hg : continuous g) (hf : continuous_on f s) :
continuous_on (g ∘ f) s :=
hg.continuous_on.comp hf (subset_univ _)
lemma continuous_within_at.preimage_mem_nhds_within {f : α → β} {x : α} {s : set α} {t : set β}
(h : continuous_within_at f s x) (ht : t ∈ nhds (f x)) : f ⁻¹' t ∈ nhds_within x s :=
h ht
lemma continuous_within_at.congr_of_mem_nhds_within {f f₁ : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (h₁ : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) :
continuous_within_at f₁ s x :=
by rwa [continuous_within_at, filter.tendsto, hx, filter.map_cong h₁]
lemma continuous_on_const {s : set α} {c : β} : continuous_on (λx, c) s :=
continuous_const.continuous_on
lemma continuous_on_open_iff {f : α → β} {s : set α} (hs : is_open s) :
continuous_on f s ↔ (∀t, _root_.is_open t → is_open (s ∩ f⁻¹' t)) :=
begin
rw continuous_on_iff',
split,
{ assume h t ht,
rcases h t ht with ⟨u, u_open, hu⟩,
rw [inter_comm, hu],
apply is_open_inter u_open hs },
{ assume h t ht,
refine ⟨s ∩ f ⁻¹' t, h t ht, _⟩,
rw [@inter_comm _ s (f ⁻¹' t), inter_assoc, inter_self] }
end
lemma continuous_on.preimage_open_of_open {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_open s) (ht : is_open t) : is_open (s ∩ f⁻¹' t) :=
(continuous_on_open_iff hs).1 hf t ht
lemma continuous_on.preimage_closed_of_closed {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_closed s) (ht : is_closed t) : is_closed (s ∩ f⁻¹' t) :=
begin
rcases continuous_on_iff_is_closed.1 hf t ht with ⟨u, hu⟩,
rw [inter_comm, hu.2],
apply is_closed_inter hu.1 hs
end
lemma continuous_on.preimage_interior_subset_interior_preimage {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_open s) : s ∩ f⁻¹' (interior t) ⊆ s ∩ interior (f⁻¹' t) :=
calc s ∩ f ⁻¹' (interior t)
= interior (s ∩ f ⁻¹' (interior t)) :
(interior_eq_of_open (hf.preimage_open_of_open hs is_open_interior)).symm
... ⊆ interior (s ∩ f ⁻¹' t) :
interior_mono (inter_subset_inter (subset.refl _) (preimage_mono interior_subset))
... = s ∩ interior (f ⁻¹' t) :
by rw [interior_inter, interior_eq_of_open hs]
lemma continuous_on_of_locally_continuous_on {f : α → β} {s : set α}
(h : ∀x∈s, ∃t, is_open t ∧ x ∈ t ∧ continuous_on f (s ∩ t)) : continuous_on f s :=
begin
assume x xs,
rcases h x xs with ⟨t, open_t, xt, ct⟩,
have := ct x ⟨xs, xt⟩,
rwa [continuous_within_at, ← nhds_within_restrict _ xt open_t] at this
end
lemma continuous_on_open_of_generate_from {β : Type*} {s : set α} {T : set (set β)} {f : α → β}
(hs : is_open s) (h : ∀t ∈ T, is_open (s ∩ f⁻¹' t)) :
@continuous_on α β _ (topological_space.generate_from T) f s :=
begin
rw continuous_on_open_iff,
assume t ht,
induction ht with u hu u v Tu Tv hu hv U hU hU',
{ exact h u hu },
{ simp only [preimage_univ, inter_univ], exact hs },
{ have : s ∩ f ⁻¹' (u ∩ v) = (s ∩ f ⁻¹' u) ∩ (s ∩ f ⁻¹' v),
by { ext x, simp, split, finish, finish },
rw this,
exact is_open_inter hu hv },
{ rw [preimage_sUnion, inter_bUnion],
exact is_open_bUnion hU' },
{ exact hs }
end
lemma continuous_within_at.prod {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 :=
tendsto_prod_mk_nhds hf hg
lemma continuous_on.prod {f : α → β} {g : α → γ} {s : set α}
(hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, (f x, g x)) s :=
λx hx, continuous_within_at.prod (hf x hx) (hg x hx)
|
075793bc8143593d42a30e18ee462fe0eed4681d | 94e33a31faa76775069b071adea97e86e218a8ee | /src/algebraic_geometry/locally_ringed_space/has_colimits.lean | 77a30cfb666e914f7bb45788f51f90b7fd10a521 | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 12,846 | lean | /-
Copyright (c) 2021 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import algebraic_geometry.locally_ringed_space
import algebra.category.Ring.constructions
import algebraic_geometry.open_immersion
import category_theory.limits.constructions.limits_of_products_and_equalizers
/-!
# Colimits of LocallyRingedSpace
We construct the explicit coproducts and coequalizers of `LocallyRingedSpace`.
It then follows that `LocallyRingedSpace` has all colimits, and
`forget_to_SheafedSpace` preserves them.
-/
namespace algebraic_geometry
universes v u
open category_theory category_theory.limits opposite topological_space
namespace SheafedSpace
variables {C : Type u} [category.{v} C] [has_limits C]
variables {J : Type v} [category.{v} J] (F : J ⥤ SheafedSpace C)
lemma is_colimit_exists_rep {c : cocone F} (hc : is_colimit c) (x : c.X) :
∃ (i : J) (y : F.obj i), (c.ι.app i).base y = x :=
concrete.is_colimit_exists_rep (F ⋙ SheafedSpace.forget _)
(is_colimit_of_preserves (SheafedSpace.forget _) hc) x
lemma colimit_exists_rep (x : colimit F) :
∃ (i : J) (y : F.obj i), (colimit.ι F i).base y = x :=
concrete.is_colimit_exists_rep (F ⋙ SheafedSpace.forget _)
(is_colimit_of_preserves (SheafedSpace.forget _) (colimit.is_colimit F)) x
instance {X Y : SheafedSpace C} (f g : X ⟶ Y) : epi (coequalizer.π f g).base :=
begin
erw ← (show _ = (coequalizer.π f g).base, from
ι_comp_coequalizer_comparison f g (SheafedSpace.forget C)),
rw ← preserves_coequalizer.iso_hom,
apply epi_comp
end
end SheafedSpace
namespace LocallyRingedSpace
section has_coproducts
variables {ι : Type u} (F : discrete ι ⥤ LocallyRingedSpace.{u})
/-- The explicit coproduct for `F : discrete ι ⥤ LocallyRingedSpace`. -/
noncomputable
def coproduct : LocallyRingedSpace :=
{ to_SheafedSpace := colimit (F ⋙ forget_to_SheafedSpace : _),
local_ring := λ x, begin
obtain ⟨i, y, ⟨⟩⟩ := SheafedSpace.colimit_exists_rep (F ⋙ forget_to_SheafedSpace) x,
haveI : _root_.local_ring (((F ⋙ forget_to_SheafedSpace).obj i).to_PresheafedSpace.stalk y) :=
(F.obj i).local_ring _,
exact (as_iso (PresheafedSpace.stalk_map (colimit.ι (F ⋙ forget_to_SheafedSpace) i : _) y)
).symm.CommRing_iso_to_ring_equiv.local_ring
end }
/-- The explicit coproduct cofan for `F : discrete ι ⥤ LocallyRingedSpace`. -/
noncomputable
def coproduct_cofan : cocone F :=
{ X := coproduct F,
ι :=
{ app := λ j, ⟨colimit.ι (F ⋙ forget_to_SheafedSpace) j, infer_instance⟩,
naturality' := λ j j' f, by { cases j, cases j', tidy, }, } }
/-- The explicit coproduct cofan constructed in `coproduct_cofan` is indeed a colimit. -/
noncomputable
def coproduct_cofan_is_colimit : is_colimit (coproduct_cofan F) :=
{ desc := λ s, ⟨colimit.desc (F ⋙ forget_to_SheafedSpace) (forget_to_SheafedSpace.map_cocone s),
begin
intro x,
obtain ⟨i, y, ⟨⟩⟩ := SheafedSpace.colimit_exists_rep (F ⋙ forget_to_SheafedSpace) x,
have := PresheafedSpace.stalk_map.comp (colimit.ι (F ⋙ forget_to_SheafedSpace) i : _)
(colimit.desc (F ⋙ forget_to_SheafedSpace) (forget_to_SheafedSpace.map_cocone s)) y,
rw ← is_iso.comp_inv_eq at this,
erw [← this, PresheafedSpace.stalk_map.congr_hom _ _
(colimit.ι_desc (forget_to_SheafedSpace.map_cocone s) i : _)],
haveI : is_local_ring_hom (PresheafedSpace.stalk_map
((forget_to_SheafedSpace.map_cocone s).ι.app i) y) := (s.ι.app i).2 y,
apply_instance
end⟩,
fac' := λ s j, subtype.eq (colimit.ι_desc _ _),
uniq' := λ s f h, subtype.eq (is_colimit.uniq _ (forget_to_SheafedSpace.map_cocone s) f.1
(λ j, congr_arg subtype.val (h j))) }
instance : has_coproducts.{u} LocallyRingedSpace.{u} :=
λ ι, ⟨λ F, ⟨⟨⟨_, coproduct_cofan_is_colimit F⟩⟩⟩⟩
noncomputable
instance (J : Type*) : preserves_colimits_of_shape (discrete J) forget_to_SheafedSpace :=
⟨λ G, preserves_colimit_of_preserves_colimit_cocone (coproduct_cofan_is_colimit G)
((colimit.is_colimit _).of_iso_colimit (cocones.ext (iso.refl _) (λ j, category.comp_id _)))⟩
end has_coproducts
section has_coequalizer
variables {X Y : LocallyRingedSpace.{v}} (f g : X ⟶ Y)
namespace has_coequalizer
instance coequalizer_π_app_is_local_ring_hom
(U : topological_space.opens ((coequalizer f.val g.val).carrier)) :
is_local_ring_hom ((coequalizer.π f.val g.val : _).c.app (op U)) :=
begin
have := ι_comp_coequalizer_comparison f.1 g.1 SheafedSpace.forget_to_PresheafedSpace,
rw ← preserves_coequalizer.iso_hom at this,
erw SheafedSpace.congr_app this.symm (op U),
rw [PresheafedSpace.comp_c_app,
← PresheafedSpace.colimit_presheaf_obj_iso_componentwise_limit_hom_π],
apply_instance
end
/-!
We roughly follow the construction given in [MR0302656]. Given a pair `f, g : X ⟶ Y` of morphisms
of locally ringed spaces, we want to show that the stalk map of
`π = coequalizer.π f g` (as sheafed space homs) is a local ring hom. It then follows that
`coequalizer f g` is indeed a locally ringed space, and `coequalizer.π f g` is a morphism of
locally ringed space.
Given a germ `⟨U, s⟩` of `x : coequalizer f g` such that `π꙳ x : Y` is invertible, we ought to show
that `⟨U, s⟩` is invertible. That is, there exists an open set `U' ⊆ U` containing `x` such that the
restriction of `s` onto `U'` is invertible. This `U'` is given by `π '' V`, where `V` is the
basic open set of `π⋆x`.
Since `f ⁻¹' V = Y.basic_open (f ≫ π)꙳ x = Y.basic_open (g ≫ π)꙳ x = g ⁻¹' V`, we have
`π ⁻¹' (π '' V) = V` (as the underlying set map is merely the set-theoretic coequalizer).
This shows that `π '' V` is indeed open, and `s` is invertible on `π '' V` as the components of `π꙳`
are local ring homs.
-/
variable (U : opens ((coequalizer f.1 g.1).carrier))
variable (s : (coequalizer f.1 g.1).presheaf.obj (op U))
/-- (Implementation). The basic open set of the section `π꙳ s`. -/
noncomputable
def image_basic_open : opens Y := (Y.to_RingedSpace.basic_open
(show Y.presheaf.obj (op (unop _)), from ((coequalizer.π f.1 g.1).c.app (op U)) s))
lemma image_basic_open_image_preimage :
(coequalizer.π f.1 g.1).base ⁻¹' ((coequalizer.π f.1 g.1).base ''
(image_basic_open f g U s).1) = (image_basic_open f g U s).1 :=
begin
fapply types.coequalizer_preimage_image_eq_of_preimage_eq f.1.base g.1.base,
{ ext,
simp_rw [types_comp_apply, ← Top.comp_app, ← PresheafedSpace.comp_base],
congr' 2,
exact coequalizer.condition f.1 g.1 },
{ apply is_colimit_cofork_map_of_is_colimit (forget Top),
apply is_colimit_cofork_map_of_is_colimit (SheafedSpace.forget _),
exact coequalizer_is_coequalizer f.1 g.1 },
{ suffices : (topological_space.opens.map f.1.base).obj (image_basic_open f g U s) =
(topological_space.opens.map g.1.base).obj (image_basic_open f g U s),
{ injection this },
delta image_basic_open,
rw [preimage_basic_open f, preimage_basic_open g],
dsimp only [functor.op, unop_op],
rw [← comp_apply, ← SheafedSpace.comp_c_app', ← comp_apply, ← SheafedSpace.comp_c_app',
SheafedSpace.congr_app (coequalizer.condition f.1 g.1), comp_apply],
erw X.to_RingedSpace.basic_open_res,
apply inf_eq_right.mpr,
refine (RingedSpace.basic_open_subset _ _).trans _,
rw coequalizer.condition f.1 g.1,
exact λ _ h, h }
end
lemma image_basic_open_image_open :
is_open ((coequalizer.π f.1 g.1).base '' (image_basic_open f g U s).1) :=
begin
rw [← (Top.homeo_of_iso (preserves_coequalizer.iso (SheafedSpace.forget _) f.1 g.1))
.is_open_preimage, Top.coequalizer_is_open_iff, ← set.preimage_comp],
erw ← coe_comp,
rw [preserves_coequalizer.iso_hom, ι_comp_coequalizer_comparison],
dsimp only [SheafedSpace.forget],
rw image_basic_open_image_preimage,
exact (image_basic_open f g U s).2
end
instance coequalizer_π_stalk_is_local_ring_hom (x : Y) :
is_local_ring_hom (PresheafedSpace.stalk_map (coequalizer.π f.val g.val : _) x) :=
begin
constructor,
rintros a ha,
rcases Top.presheaf.germ_exist _ _ a with ⟨U, hU, s, rfl⟩,
erw PresheafedSpace.stalk_map_germ_apply (coequalizer.π f.1 g.1 : _) U ⟨_, hU⟩ at ha,
let V := image_basic_open f g U s,
have hV : (coequalizer.π f.1 g.1).base ⁻¹' ((coequalizer.π f.1 g.1).base '' V.1) = V.1 :=
image_basic_open_image_preimage f g U s,
have hV' : V = ⟨(coequalizer.π f.1 g.1).base ⁻¹'
((coequalizer.π f.1 g.1).base '' V.1), hV.symm ▸ V.2⟩ := subtype.eq hV.symm,
have V_open : is_open (((coequalizer.π f.val g.val).base) '' V.val) :=
image_basic_open_image_open f g U s,
have VleU :
(⟨((coequalizer.π f.val g.val).base) '' V.val, V_open⟩ : topological_space.opens _) ≤ U,
{ exact set.image_subset_iff.mpr (Y.to_RingedSpace.basic_open_subset _) },
have hxV : x ∈ V := ⟨⟨_, hU⟩, ha, rfl⟩,
erw ← (coequalizer f.val g.val).presheaf.germ_res_apply (hom_of_le VleU)
⟨_, @set.mem_image_of_mem _ _ (coequalizer.π f.val g.val).base x V.1 hxV⟩ s,
apply ring_hom.is_unit_map,
rw [← is_unit_map_iff ((coequalizer.π f.val g.val : _).c.app _), ← comp_apply,
nat_trans.naturality, comp_apply, Top.presheaf.pushforward_obj_map,
← is_unit_map_iff (Y.presheaf.map (eq_to_hom hV').op), ← comp_apply, ← functor.map_comp],
convert @RingedSpace.is_unit_res_basic_open Y.to_RingedSpace (unop _)
(((coequalizer.π f.val g.val).c.app (op U)) s),
apply_instance
end
end has_coequalizer
/-- The coequalizer of two locally ringed space in the category of sheafed spaces is a locally
ringed space. -/
noncomputable
def coequalizer : LocallyRingedSpace :=
{ to_SheafedSpace := coequalizer f.1 g.1,
local_ring := λ x,
begin
obtain ⟨y, rfl⟩ :=
(Top.epi_iff_surjective (coequalizer.π f.val g.val).base).mp infer_instance x,
exact (PresheafedSpace.stalk_map (coequalizer.π f.val g.val : _) y).domain_local_ring
end }
/-- The explicit coequalizer cofork of locally ringed spaces. -/
noncomputable
def coequalizer_cofork : cofork f g :=
@cofork.of_π _ _ _ _ f g (coequalizer f g) ⟨coequalizer.π f.1 g.1, infer_instance⟩
(subtype.eq (coequalizer.condition f.1 g.1))
lemma is_local_ring_hom_stalk_map_congr {X Y : RingedSpace} (f g : X ⟶ Y) (H : f = g)
(x) (h : is_local_ring_hom (PresheafedSpace.stalk_map f x)) :
is_local_ring_hom (PresheafedSpace.stalk_map g x) :=
by { rw PresheafedSpace.stalk_map.congr_hom _ _ H.symm x, apply_instance }
/-- The cofork constructed in `coequalizer_cofork` is indeed a colimit cocone. -/
noncomputable
def coequalizer_cofork_is_colimit : is_colimit (coequalizer_cofork f g) :=
begin
apply cofork.is_colimit.mk',
intro s,
have e : f.val ≫ s.π.val = g.val ≫ s.π.val := by injection s.condition,
use coequalizer.desc s.π.1 e,
{ intro x,
rcases (Top.epi_iff_surjective (coequalizer.π f.val g.val).base).mp
infer_instance x with ⟨y, rfl⟩,
apply is_local_ring_hom_of_comp _ (PresheafedSpace.stalk_map (coequalizer_cofork f g).π.1 _),
change is_local_ring_hom (_ ≫ PresheafedSpace.stalk_map (coequalizer_cofork f g).π.val y),
erw ← PresheafedSpace.stalk_map.comp,
apply is_local_ring_hom_stalk_map_congr _ _ (coequalizer.π_desc s.π.1 e).symm y,
apply_instance },
split,
exact subtype.eq (coequalizer.π_desc _ _),
intros m h,
replace h : (coequalizer_cofork f g).π.1 ≫ m.1 = s.π.1 := by { rw ← h, refl },
apply subtype.eq,
apply (colimit.is_colimit (parallel_pair f.1 g.1)).uniq (cofork.of_π s.π.1 e) m.1,
rintro ⟨⟩,
{ rw [← (colimit.cocone (parallel_pair f.val g.val)).w walking_parallel_pair_hom.left,
category.assoc],
change _ ≫ _ ≫ _ = _ ≫ _,
congr,
exact h },
{ exact h }
end
instance : has_coequalizer f g := ⟨⟨⟨_, coequalizer_cofork_is_colimit f g⟩⟩⟩
instance : has_coequalizers LocallyRingedSpace := has_coequalizers_of_has_colimit_parallel_pair _
noncomputable
instance preserves_coequalizer :
preserves_colimits_of_shape walking_parallel_pair forget_to_SheafedSpace.{v} :=
⟨λ F, begin
apply preserves_colimit_of_iso_diagram _ (diagram_iso_parallel_pair F).symm,
apply preserves_colimit_of_preserves_colimit_cocone (coequalizer_cofork_is_colimit _ _),
apply (is_colimit_map_cocone_cofork_equiv _ _).symm _,
dsimp only [forget_to_SheafedSpace],
exact coequalizer_is_coequalizer _ _
end⟩
end has_coequalizer
instance : has_colimits LocallyRingedSpace := colimits_from_coequalizers_and_coproducts
noncomputable
instance : preserves_colimits LocallyRingedSpace.forget_to_SheafedSpace :=
preserves_colimits_of_preserves_coequalizers_and_coproducts _
end LocallyRingedSpace
end algebraic_geometry
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.