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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
9f6b003dc69acde2e8852b448f1d2a6b7f67d0cd | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/algebra/basic.lean | 1b2127603a6e8e6bbd82190ef3018991e790b4c6 | [
"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 | 59,803 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Yury Kudryashov
-/
import algebra.module.basic
import algebra.ring.aut
import linear_algebra.span
import tactic.abel
/-!
# Algebras over commutative semirings
In this file we define associative unital `algebra`s over commutative (semi)rings, algebra
homomorphisms `alg_hom`, and algebra equivalences `alg_equiv`.
`subalgebra`s are defined in `algebra.algebra.subalgebra`.
For the category of `R`-algebras, denoted `Algebra R`, see the file
`algebra/category/Algebra/basic.lean`.
See the implementation notes for remarks about non-associative and non-unital algebras.
## Main definitions:
* `algebra R A`: the algebra typeclass.
* `alg_hom R A B`: the type of `R`-algebra morphisms from `A` to `B`.
* `alg_equiv R A B`: the type of `R`-algebra isomorphisms between `A` to `B`.
* `algebra_map R A : R →+* A`: the canonical map from `R` to `A`, as a `ring_hom`. This is the
preferred spelling of this map.
* `algebra.linear_map R A : R →ₗ[R] A`: the canonical map from `R` to `A`, as a `linear_map`.
* `algebra.of_id R A : R →ₐ[R] A`: the canonical map from `R` to `A`, as n `alg_hom`.
* Instances of `algebra` in this file:
* `algebra.id`
* `pi.algebra`
* `prod.algebra`
* `algebra_nat`
* `algebra_int`
* `algebra_rat`
* `mul_opposite.algebra`
* `module.End.algebra`
## Notations
* `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`.
* `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`.
## Implementation notes
Given a commutative (semi)ring `R`, there are two ways to define an `R`-algebra structure on a
(possibly noncommutative) (semi)ring `A`:
* By endowing `A` with a morphism of rings `R →+* A` denoted `algebra_map R A` which lands in the
center of `A`.
* By requiring `A` be an `R`-module such that the action associates and commutes with multiplication
as `r • (a₁ * a₂) = (r • a₁) * a₂ = a₁ * (r • a₂)`.
We define `algebra R A` in a way that subsumes both definitions, by extending `has_scalar R A` and
requiring that this scalar action `r • x` must agree with left multiplication by the image of the
structure morphism `algebra_map R A r * x`.
As a result, there are two ways to talk about an `R`-algebra `A` when `A` is a semiring:
1. ```lean
variables [comm_semiring R] [semiring A]
variables [algebra R A]
```
2. ```lean
variables [comm_semiring R] [semiring A]
variables [module R A] [smul_comm_class R A A] [is_scalar_tower R A A]
```
The first approach implies the second via typeclass search; so any lemma stated with the second set
of arguments will automatically apply to the first set. Typeclass search does not know that the
second approach implies the first, but this can be shown with:
```lean
example {R A : Type*} [comm_semiring R] [semiring A]
[module R A] [smul_comm_class R A A] [is_scalar_tower R A A] : algebra R A :=
algebra.of_module smul_mul_assoc mul_smul_comm
```
The advantage of the first approach is that `algebra_map R A` is available, and `alg_hom R A B` and
`subalgebra R A` can be used. For concrete `R` and `A`, `algebra_map R A` is often definitionally
convenient.
The advantage of the second approach is that `comm_semiring R`, `semiring A`, and `module R A` can
all be relaxed independently; for instance, this allows us to:
* Replace `semiring A` with `non_unital_non_assoc_semiring A` in order to describe non-unital and/or
non-associative algebras.
* Replace `comm_semiring R` and `module R A` with `comm_group R'` and `distrib_mul_action R' A`,
which when `R' = Rˣ` lets us talk about the "algebra-like" action of `Rˣ` on an
`R`-algebra `A`.
While `alg_hom R A B` cannot be used in the second approach, `non_unital_alg_hom R A B` still can.
You should always use the first approach when working with associative unital algebras, and mimic
the second approach only when you need to weaken a condition on either `R` or `A`.
-/
universes u v w u₁ v₁
open_locale big_operators
section prio
-- We set this priority to 0 later in this file
set_option extends_priority 200 /- control priority of
`instance [algebra R A] : has_scalar R A` -/
/--
An associative unital `R`-algebra is a semiring `A` equipped with a map into its center `R → A`.
See the implementation notes in this file for discussion of the details of this definition.
-/
@[nolint has_inhabited_instance]
class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A]
extends has_scalar R A, R →+* A :=
(commutes' : ∀ r x, to_fun r * x = x * to_fun r)
(smul_def' : ∀ r x, r • x = to_fun r * x)
end prio
/-- Embedding `R →+* A` given by `algebra` structure. -/
def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A :=
algebra.to_ring_hom
/-- Creating an algebra from a morphism to the center of a semiring. -/
def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S)
(h : ∀ c x, i c * x = x * i c) :
algebra R S :=
{ smul := λ c x, i c * x,
commutes' := h,
smul_def' := λ c x, rfl,
to_ring_hom := i}
/-- Creating an algebra from a morphism to a commutative semiring. -/
def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) :
algebra R S :=
i.to_algebra' $ λ _, mul_comm _
lemma ring_hom.algebra_map_to_algebra {R S} [comm_semiring R] [comm_semiring S]
(i : R →+* S) :
@algebra_map R S _ _ i.to_algebra = i :=
rfl
namespace algebra
variables {R : Type u} {S : Type v} {A : Type w} {B : Type*}
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `module R` structure.
If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra`
over `R`.
See note [reducible non-instances]. -/
@[reducible]
def of_module' [comm_semiring R] [semiring A] [module R A]
(h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x)
(h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A :=
{ to_fun := λ r, r • 1,
map_one' := one_smul _ _,
map_mul' := λ r₁ r₂, by rw [h₁, mul_smul],
map_zero' := zero_smul _ _,
map_add' := λ r₁ r₂, add_smul r₁ r₂ 1,
commutes' := λ r x, by simp only [h₁, h₂],
smul_def' := λ r x, by simp only [h₁] }
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `module R` structure.
If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A`
is an `algebra` over `R`.
See note [reducible non-instances]. -/
@[reducible]
def of_module [comm_semiring R] [semiring A] [module R A]
(h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y))
(h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A :=
of_module' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one])
section semiring
variables [comm_semiring R] [comm_semiring S]
variables [semiring A] [algebra R A] [semiring B] [algebra R B]
/-- We keep this lemma private because it picks up the `algebra.to_has_scalar` instance
which we set to priority 0 shortly. See `smul_def` below for the public version. -/
private lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
/--
To prove two algebra structures on a fixed `[comm_semiring R] [semiring A]` agree,
it suffices to check the `algebra_map`s agree.
-/
-- We'll later use this to show `algebra ℤ M` is a subsingleton.
@[ext]
lemma algebra_ext {R : Type*} [comm_semiring R] {A : Type*} [semiring A] (P Q : algebra R A)
(w : ∀ (r : R), by { haveI := P, exact algebra_map R A r } =
by { haveI := Q, exact algebra_map R A r }) :
P = Q :=
begin
unfreezingI { rcases P with ⟨⟨P⟩⟩, rcases Q with ⟨⟨Q⟩⟩ },
congr,
{ funext r a,
replace w := congr_arg (λ s, s * a) (w r),
simp only [←smul_def''] at w,
apply w, },
{ ext r,
exact w r, },
{ apply proof_irrel_heq, },
{ apply proof_irrel_heq, },
end
@[priority 200] -- see Note [lower instance priority]
instance to_module : module R A :=
{ one_smul := by simp [smul_def''],
mul_smul := by simp [smul_def'', mul_assoc],
smul_add := by simp [smul_def'', mul_add],
smul_zero := by simp [smul_def''],
add_smul := by simp [smul_def'', add_mul],
zero_smul := by simp [smul_def''] }
-- From now on, we don't want to use the following instance anymore.
-- Unfortunately, leaving it in place causes deterministic timeouts later in mathlib.
attribute [instance, priority 0] algebra.to_has_scalar
lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
lemma algebra_map_eq_smul_one (r : R) : algebra_map R A r = r • 1 :=
calc algebra_map R A r = algebra_map R A r * 1 : (mul_one _).symm
... = r • 1 : (algebra.smul_def r 1).symm
lemma algebra_map_eq_smul_one' : ⇑(algebra_map R A) = λ r, r • (1 : A) :=
funext algebra_map_eq_smul_one
/-- `mul_comm` for `algebra`s when one element is from the base ring. -/
theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r :=
algebra.commutes' r x
/-- `mul_left_comm` for `algebra`s when one element is from the base ring. -/
theorem left_comm (x : A) (r : R) (y : A) :
x * (algebra_map R A r * y) = algebra_map R A r * (x * y) :=
by rw [← mul_assoc, ← commutes, mul_assoc]
/-- `mul_right_comm` for `algebra`s when one element is from the base ring. -/
theorem right_comm (x : A) (r : R) (y : A) :
(x * algebra_map R A r) * y = (x * y) * algebra_map R A r :=
by rw [mul_assoc, commutes, ←mul_assoc]
instance _root_.is_scalar_tower.right : is_scalar_tower R A A :=
⟨λ x y z, by rw [smul_eq_mul, smul_eq_mul, smul_def, smul_def, mul_assoc]⟩
/-- This is just a special case of the global `mul_smul_comm` lemma that requires less typeclass
search (and was here first). -/
@[simp] protected lemma mul_smul_comm (s : R) (x y : A) :
x * (s • y) = s • (x * y) :=
-- TODO: set up `is_scalar_tower.smul_comm_class` earlier so that we can actually prove this using
-- `mul_smul_comm s x y`.
by rw [smul_def, smul_def, left_comm]
/-- This is just a special case of the global `smul_mul_assoc` lemma that requires less typeclass
search (and was here first). -/
@[simp] protected lemma smul_mul_assoc (r : R) (x y : A) :
(r • x) * y = r • (x * y) :=
smul_mul_assoc r x y
section
variables {r : R} {a : A}
@[simp] lemma bit0_smul_one : bit0 r • (1 : A) = bit0 (r • (1 : A)) :=
by simp [bit0, add_smul]
lemma bit0_smul_one' : bit0 r • (1 : A) = r • 2 :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit0_smul_bit0 : bit0 r • bit0 a = r • (bit0 (bit0 a)) :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit0_smul_bit1 : bit0 r • bit1 a = r • (bit0 (bit1 a)) :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit1_smul_one : bit1 r • (1 : A) = bit1 (r • (1 : A)) :=
by simp [bit1, add_smul]
lemma bit1_smul_one' : bit1 r • (1 : A) = r • 2 + 1 :=
by simp [bit1, bit0, add_smul, smul_add]
@[simp] lemma bit1_smul_bit0 : bit1 r • bit0 a = r • (bit0 (bit0 a)) + bit0 a :=
by simp [bit1, add_smul, smul_add]
@[simp] lemma bit1_smul_bit1 : bit1 r • bit1 a = r • (bit0 (bit1 a)) + bit1 a :=
by { simp only [bit0, bit1, add_smul, smul_add, one_smul], abel }
end
variables (R A)
/--
The canonical ring homomorphism `algebra_map R A : R →* A` for any `R`-algebra `A`,
packaged as an `R`-linear map.
-/
protected def linear_map : R →ₗ[R] A :=
{ map_smul' := λ x y, by simp [algebra.smul_def],
..algebra_map R A }
@[simp]
lemma linear_map_apply (r : R) : algebra.linear_map R A r = algebra_map R A r := rfl
lemma coe_linear_map : ⇑(algebra.linear_map R A) = algebra_map R A := rfl
instance id : algebra R R := (ring_hom.id R).to_algebra
variables {R A}
namespace id
@[simp] lemma map_eq_id : algebra_map R R = ring_hom.id _ := rfl
lemma map_eq_self (x : R) : algebra_map R R x = x := rfl
@[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl
end id
section punit
instance _root_.punit.algebra : algebra R punit :=
{ to_fun := λ x, punit.star,
map_one' := rfl,
map_mul' := λ _ _, rfl,
map_zero' := rfl,
map_add' := λ _ _, rfl,
commutes' := λ _ _, rfl,
smul_def' := λ _ _, rfl }
@[simp] lemma algebra_map_punit (r : R) : algebra_map R punit r = punit.star := rfl
end punit
section prod
variables (R A B)
instance _root_.prod.algebra : algebra R (A × B) :=
{ commutes' := by { rintro r ⟨a, b⟩, dsimp, rw [commutes r a, commutes r b] },
smul_def' := by { rintro r ⟨a, b⟩, dsimp, rw [smul_def r a, smul_def r b] },
.. prod.module,
.. ring_hom.prod (algebra_map R A) (algebra_map R B) }
variables {R A B}
@[simp] lemma algebra_map_prod_apply (r : R) :
algebra_map R (A × B) r = (algebra_map R A r, algebra_map R B r) := rfl
end prod
/-- Algebra over a subsemiring. This builds upon `subsemiring.module`. -/
instance of_subsemiring (S : subsemiring R) : algebra S A :=
{ smul := (•),
commutes' := λ r x, algebra.commutes r x,
smul_def' := λ r x, algebra.smul_def r x,
.. (algebra_map R A).comp S.subtype }
lemma algebra_map_of_subsemiring (S : subsemiring R) :
(algebra_map S R : S →+* R) = subsemiring.subtype S := rfl
lemma coe_algebra_map_of_subsemiring (S : subsemiring R) :
(algebra_map S R : S → R) = subtype.val := rfl
lemma algebra_map_of_subsemiring_apply (S : subsemiring R) (x : S) :
algebra_map S R x = x := rfl
/-- Algebra over a subring. This builds upon `subring.module`. -/
instance of_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A]
(S : subring R) : algebra S A :=
{ smul := (•),
.. algebra.of_subsemiring S.to_subsemiring,
.. (algebra_map R A).comp S.subtype }
lemma algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) :
(algebra_map S R : S →+* R) = subring.subtype S := rfl
lemma coe_algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) :
(algebra_map S R : S → R) = subtype.val := rfl
lemma algebra_map_of_subring_apply {R : Type*} [comm_ring R] (S : subring R) (x : S) :
algebra_map S R x = x := rfl
/-- Explicit characterization of the submonoid map in the case of an algebra.
`S` is made explicit to help with type inference -/
def algebra_map_submonoid (S : Type*) [semiring S] [algebra R S]
(M : submonoid R) : (submonoid S) :=
submonoid.map (algebra_map R S : R →* S) M
lemma mem_algebra_map_submonoid_of_mem {S : Type*} [semiring S] [algebra R S] {M : submonoid R}
(x : M) : (algebra_map R S x) ∈ algebra_map_submonoid S M :=
set.mem_image_of_mem (algebra_map R S) x.2
end semiring
section comm_semiring
variables [comm_semiring R]
lemma mul_sub_algebra_map_commutes [ring A] [algebra R A] (x : A) (r : R) :
x * (x - algebra_map R A r) = (x - algebra_map R A r) * x :=
by rw [mul_sub, ←commutes, sub_mul]
lemma mul_sub_algebra_map_pow_commutes [ring A] [algebra R A] (x : A) (r : R) (n : ℕ) :
x * (x - algebra_map R A r) ^ n = (x - algebra_map R A r) ^ n * x :=
begin
induction n with n ih,
{ simp },
{ rw [pow_succ, ←mul_assoc, mul_sub_algebra_map_commutes, mul_assoc, ih, ←mul_assoc] }
end
end comm_semiring
section ring
variables [comm_ring R]
variables (R)
/-- A `semiring` that is an `algebra` over a commutative ring carries a natural `ring` structure.
See note [reducible non-instances]. -/
@[reducible]
def semiring_to_ring [semiring A] [algebra R A] : ring A :=
{ ..module.add_comm_monoid_to_add_comm_group R,
..(infer_instance : semiring A) }
end ring
end algebra
namespace no_zero_smul_divisors
variables {R A : Type*}
open algebra
section ring
variables [comm_ring R]
/-- If `algebra_map R A` is injective and `A` has no zero divisors,
`R`-multiples in `A` are zero only if one of the factors is zero.
Cannot be an instance because there is no `injective (algebra_map R A)` typeclass.
-/
lemma of_algebra_map_injective
[semiring A] [algebra R A] [no_zero_divisors A]
(h : function.injective (algebra_map R A)) : no_zero_smul_divisors R A :=
⟨λ c x hcx, (mul_eq_zero.mp ((smul_def c x).symm.trans hcx)).imp_left
((injective_iff_map_eq_zero (algebra_map R A)).mp h _)⟩
variables (R A)
lemma algebra_map_injective [ring A] [nontrivial A]
[algebra R A] [no_zero_smul_divisors R A] :
function.injective (algebra_map R A) :=
suffices function.injective (λ (c : R), c • (1 : A)),
by { convert this, ext, rw [algebra.smul_def, mul_one] },
smul_left_injective R one_ne_zero
variables {R A}
lemma iff_algebra_map_injective [ring A] [is_domain A] [algebra R A] :
no_zero_smul_divisors R A ↔ function.injective (algebra_map R A) :=
⟨@@no_zero_smul_divisors.algebra_map_injective R A _ _ _ _,
no_zero_smul_divisors.of_algebra_map_injective⟩
end ring
section field
variables [field R] [semiring A] [algebra R A]
@[priority 100] -- see note [lower instance priority]
instance algebra.no_zero_smul_divisors [nontrivial A] [no_zero_divisors A] :
no_zero_smul_divisors R A :=
no_zero_smul_divisors.of_algebra_map_injective (algebra_map R A).injective
end field
end no_zero_smul_divisors
namespace mul_opposite
variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A]
instance : algebra R Aᵐᵒᵖ :=
{ to_ring_hom := (algebra_map R A).to_opposite $ λ x y, algebra.commutes _ _,
smul_def' := λ c x, unop_injective $
by { dsimp, simp only [op_mul, algebra.smul_def, algebra.commutes, op_unop] },
commutes' := λ r, mul_opposite.rec $ λ x, by dsimp; simp only [← op_mul, algebra.commutes],
.. mul_opposite.has_scalar A R }
@[simp] lemma algebra_map_apply (c : R) : algebra_map R Aᵐᵒᵖ c = op (algebra_map R A c) := rfl
end mul_opposite
namespace module
variables (R : Type u) (M : Type v) [comm_semiring R] [add_comm_monoid M] [module R M]
instance : algebra R (module.End R M) :=
algebra.of_module smul_mul_assoc (λ r f g, (smul_comm r f g).symm)
lemma algebra_map_End_eq_smul_id (a : R) :
(algebra_map R (End R M)) a = a • linear_map.id := rfl
@[simp] lemma algebra_map_End_apply (a : R) (m : M) :
(algebra_map R (End R M)) a m = a • m := rfl
@[simp] lemma ker_algebra_map_End (K : Type u) (V : Type v)
[field K] [add_comm_group V] [module K V] (a : K) (ha : a ≠ 0) :
((algebra_map K (End K V)) a).ker = ⊥ :=
linear_map.ker_smul _ _ ha
end module
set_option old_structure_cmd true
/-- Defining the homomorphism in the category R-Alg. -/
@[nolint has_inhabited_instance]
structure alg_hom (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`"
infixr ` →ₐ `:25 := alg_hom _
notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁}
section semiring
variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D]
variables [algebra R A] [algebra R B] [algebra R C] [algebra R D]
instance : has_coe_to_fun (A →ₐ[R] B) (λ _, A → B) := ⟨alg_hom.to_fun⟩
initialize_simps_projections alg_hom (to_fun → apply)
@[simp] lemma to_fun_eq_coe (f : A →ₐ[R] B) : f.to_fun = f := rfl
instance : ring_hom_class (A →ₐ[R] B) A B :=
{ coe := to_fun,
coe_injective' := λ f g h, by { cases f, cases g, congr' },
map_add := map_add',
map_zero := map_zero',
map_mul := map_mul',
map_one := map_one' }
instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩
instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩
instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩
@[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) :
⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl
-- make the coercion the simp-normal form
@[simp] lemma to_ring_hom_eq_coe (f : A →ₐ[R] B) : f.to_ring_hom = f := rfl
@[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl
@[simp, norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl
@[simp, norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl
variables (φ : A →ₐ[R] B)
theorem coe_fn_injective : @function.injective (A →ₐ[R] B) (A → B) coe_fn := fun_like.coe_injective
theorem coe_fn_inj {φ₁ φ₂ : A →ₐ[R] B} : (φ₁ : A → B) = φ₂ ↔ φ₁ = φ₂ := fun_like.coe_fn_eq
theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) :=
λ φ₁ φ₂ H, coe_fn_injective $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B),
from congr_arg _ H
theorem coe_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →* B)) :=
ring_hom.coe_monoid_hom_injective.comp coe_ring_hom_injective
theorem coe_add_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) :=
ring_hom.coe_add_monoid_hom_injective.comp coe_ring_hom_injective
protected lemma congr_fun {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁ = φ₂) (x : A) : φ₁ x = φ₂ x :=
fun_like.congr_fun H x
protected lemma congr_arg (φ : A →ₐ[R] B) {x y : A} (h : x = y) : φ x = φ y :=
fun_like.congr_arg φ h
@[ext]
theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := fun_like.ext _ _ H
theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x := fun_like.ext_iff
@[simp] theorem mk_coe {f : A →ₐ[R] B} (h₁ h₂ h₃ h₄ h₅) :
(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := ext $ λ _, rfl
@[simp]
theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r
theorem comp_algebra_map : (φ : A →+* B).comp (algebra_map R A) = algebra_map R B :=
ring_hom.ext $ φ.commutes
lemma map_add (r s : A) : φ (r + s) = φ r + φ s := map_add _ _ _
lemma map_zero : φ 0 = 0 := map_zero _
lemma map_mul (x y) : φ (x * y) = φ x * φ y := map_mul _ _ _
lemma map_one : φ 1 = 1 := map_one _
lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n :=
map_pow _ _ _
@[simp] lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x :=
by simp only [algebra.smul_def, map_mul, commutes]
lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∑ x in s, f x) = ∑ x in s, φ (f x) :=
φ.to_ring_hom.map_sum f s
lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) :
φ (f.sum g) = f.sum (λ i a, φ (g i a)) :=
φ.map_sum _ _
lemma map_bit0 (x) : φ (bit0 x) = bit0 (φ x) := map_bit0 _ _
lemma map_bit1 (x) : φ (bit1 x) = bit1 (φ x) := map_bit1 _ _
/-- If a `ring_hom` is `R`-linear, then it is an `alg_hom`. -/
def mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : A →ₐ[R] B :=
{ to_fun := f,
commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, h, f.map_one],
.. f }
@[simp] lemma coe_mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : ⇑(mk' f h) = f := rfl
section
variables (R A)
/-- Identity map as an `alg_hom`. -/
protected def id : A →ₐ[R] A :=
{ commutes' := λ _, rfl,
..ring_hom.id A }
@[simp] lemma coe_id : ⇑(alg_hom.id R A) = id := rfl
@[simp] lemma id_to_ring_hom : (alg_hom.id R A : A →+* A) = ring_hom.id _ := rfl
end
lemma id_apply (p : A) : alg_hom.id R A p = p := rfl
/-- Composition of algebra homeomorphisms. -/
def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C :=
{ commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl,
.. φ₁.to_ring_hom.comp ↑φ₂ }
@[simp] lemma coe_comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : ⇑(φ₁.comp φ₂) = φ₁ ∘ φ₂ := rfl
lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl
lemma comp_to_ring_hom (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) :
⇑(φ₁.comp φ₂ : A →+* C) = (φ₁ : B →+* C).comp ↑φ₂ := rfl
@[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ :=
ext $ λ x, rfl
@[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ :=
ext $ λ x, rfl
theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) :
(φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) :=
ext $ λ x, rfl
/-- R-Alg ⥤ R-Mod -/
def to_linear_map : A →ₗ[R] B :=
{ to_fun := φ,
map_add' := φ.map_add,
map_smul' := φ.map_smul }
@[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl
theorem to_linear_map_injective : function.injective (to_linear_map : _ → (A →ₗ[R] B)) :=
λ φ₁ φ₂ h, ext $ linear_map.congr_fun h
@[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) :
(g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
@[simp] lemma to_linear_map_id : to_linear_map (alg_hom.id R A) = linear_map.id :=
linear_map.ext $ λ _, rfl
/-- Promote a `linear_map` to an `alg_hom` by supplying proofs about the behavior on `1` and `*`. -/
@[simps]
def of_linear_map (f : A →ₗ[R] B) (map_one : f 1 = 1) (map_mul : ∀ x y, f (x * y) = f x * f y) :
A →ₐ[R] B :=
{ to_fun := f,
map_one' := map_one,
map_mul' := map_mul,
commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, f.map_smul, map_one],
.. f.to_add_monoid_hom }
@[simp] lemma of_linear_map_to_linear_map (map_one) (map_mul) :
of_linear_map φ.to_linear_map map_one map_mul = φ :=
by { ext, refl }
@[simp] lemma to_linear_map_of_linear_map (f : A →ₗ[R] B) (map_one) (map_mul) :
to_linear_map (of_linear_map f map_one map_mul) = f :=
by { ext, refl }
@[simp] lemma of_linear_map_id (map_one) (map_mul) :
of_linear_map linear_map.id map_one map_mul = alg_hom.id R A :=
ext $ λ _, rfl
lemma map_smul_of_tower {R'} [has_scalar R' A] [has_scalar R' B]
[linear_map.compatible_smul A B R' R] (r : R') (x : A) : φ (r • x) = r • φ x :=
φ.to_linear_map.map_smul_of_tower r x
lemma map_list_prod (s : list A) :
φ s.prod = (s.map φ).prod :=
φ.to_ring_hom.map_list_prod s
section prod
/-- First projection as `alg_hom`. -/
def fst : A × B →ₐ[R] A :=
{ commutes' := λ r, rfl, .. ring_hom.fst A B}
/-- Second projection as `alg_hom`. -/
def snd : A × B →ₐ[R] B :=
{ commutes' := λ r, rfl, .. ring_hom.snd A B}
end prod
lemma algebra_map_eq_apply (f : A →ₐ[R] B) {y : R} {x : A} (h : algebra_map R A y = x) :
algebra_map R B y = f x :=
h ▸ (f.commutes _).symm
end semiring
section comm_semiring
variables [comm_semiring R] [comm_semiring A] [comm_semiring B]
variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B)
lemma map_multiset_prod (s : multiset A) :
φ s.prod = (s.map φ).prod :=
φ.to_ring_hom.map_multiset_prod s
lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∏ x in s, f x) = ∏ x in s, φ (f x) :=
φ.to_ring_hom.map_prod f s
lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) :
φ (f.prod g) = f.prod (λ i a, φ (g i a)) :=
φ.map_prod _ _
end comm_semiring
section ring
variables [comm_semiring R] [ring A] [ring B]
variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B)
lemma map_neg (x) : φ (-x) = -φ x := map_neg _ _
lemma map_sub (x y) : φ (x - y) = φ x - φ y := map_sub _ _ _
@[simp] lemma map_int_cast (n : ℤ) : φ n = n :=
φ.to_ring_hom.map_int_cast n
end ring
section division_ring
variables [comm_semiring R] [division_ring A] [division_ring B]
variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B)
@[simp] lemma map_inv (x) : φ (x⁻¹) = (φ x)⁻¹ :=
φ.to_ring_hom.map_inv x
@[simp] lemma map_div (x y) : φ (x / y) = φ x / φ y :=
φ.to_ring_hom.map_div x y
end division_ring
end alg_hom
@[simp] lemma rat.smul_one_eq_coe {A : Type*} [division_ring A] [algebra ℚ A] (m : ℚ) :
m • (1 : A) = ↑m :=
by rw [algebra.smul_def, mul_one, ring_hom.eq_rat_cast]
set_option old_structure_cmd true
/-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/
structure alg_equiv (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
attribute [nolint doc_blame] alg_equiv.to_ring_equiv
attribute [nolint doc_blame] alg_equiv.to_equiv
attribute [nolint doc_blame] alg_equiv.to_add_equiv
attribute [nolint doc_blame] alg_equiv.to_mul_equiv
notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A'
namespace alg_equiv
variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁}
section semiring
variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃]
variables [algebra R A₁] [algebra R A₂] [algebra R A₃]
variables (e : A₁ ≃ₐ[R] A₂)
instance : ring_equiv_class (A₁ ≃ₐ[R] A₂) A₁ A₂ :=
{ coe := to_fun,
inv := inv_fun,
coe_injective' := λ f g h₁ h₂, by { cases f, cases g, congr' },
map_add := map_add',
map_mul := map_mul',
left_inv := left_inv,
right_inv := right_inv }
/-- Helper instance for when there's too many metavariables to apply
`fun_like.has_coe_to_fun` directly. -/
instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) (λ _, A₁ → A₂) := ⟨alg_equiv.to_fun⟩
@[ext]
lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h
protected lemma congr_arg {f : A₁ ≃ₐ[R] A₂} {x x' : A₁} : x = x' → f x = f x' :=
fun_like.congr_arg f
protected lemma congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x :=
fun_like.congr_fun h x
protected lemma ext_iff {f g : A₁ ≃ₐ[R] A₂} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff
lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) :=
fun_like.coe_injective
instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩
@[simp] lemma coe_mk {to_fun inv_fun left_inv right_inv map_mul map_add commutes} :
⇑(⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) = to_fun :=
rfl
@[simp] theorem mk_coe (e : A₁ ≃ₐ[R] A₂) (e' h₁ h₂ h₃ h₄ h₅) :
(⟨e, e', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂) = e := ext $ λ _, rfl
@[simp] lemma to_fun_eq_coe (e : A₁ ≃ₐ[R] A₂) : e.to_fun = e := rfl
@[simp] lemma to_equiv_eq_coe : e.to_equiv = e := rfl
@[simp] lemma to_ring_equiv_eq_coe : e.to_ring_equiv = e := rfl
@[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl
lemma coe_ring_equiv' : (e.to_ring_equiv : A₁ → A₂) = e := rfl
lemma coe_ring_equiv_injective : function.injective (coe : (A₁ ≃ₐ[R] A₂) → (A₁ ≃+* A₂)) :=
λ e₁ e₂ h, ext $ ring_equiv.congr_fun h
protected lemma map_add : ∀ x y, e (x + y) = e x + e y := map_add e
protected lemma map_zero : e 0 = 0 := map_zero e
protected lemma map_mul : ∀ x y, e (x * y) = (e x) * (e y) := map_mul e
protected lemma map_one : e 1 = 1 := map_one e
@[simp] lemma commutes : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r :=
e.commutes'
@[simp] lemma map_smul (r : R) (x : A₁) : e (r • x) = r • e x :=
by simp only [algebra.smul_def, map_mul, commutes]
lemma map_sum {ι : Type*} (f : ι → A₁) (s : finset ι) :
e (∑ x in s, f x) = ∑ x in s, e (f x) :=
e.to_add_equiv.map_sum f s
lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) :
e (f.sum g) = f.sum (λ i b, e (g i b)) :=
e.map_sum _ _
/-- Interpret an algebra equivalence as an algebra homomorphism.
This definition is included for symmetry with the other `to_*_hom` projections.
The `simp` normal form is to use the coercion of the `has_coe_to_alg_hom` instance. -/
def to_alg_hom : A₁ →ₐ[R] A₂ :=
{ map_one' := e.map_one, map_zero' := e.map_zero, ..e }
instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) :=
⟨to_alg_hom⟩
@[simp] lemma to_alg_hom_eq_coe : e.to_alg_hom = e := rfl
@[simp, norm_cast] lemma coe_alg_hom : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e :=
rfl
lemma coe_alg_hom_injective : function.injective (coe : (A₁ ≃ₐ[R] A₂) → (A₁ →ₐ[R] A₂)) :=
λ e₁ e₂ h, ext $ alg_hom.congr_fun h
/-- The two paths coercion can take to a `ring_hom` are equivalent -/
lemma coe_ring_hom_commutes : ((e : A₁ →ₐ[R] A₂) : A₁ →+* A₂) = ((e : A₁ ≃+* A₂) : A₁ →+* A₂) :=
rfl
protected lemma map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = (e x) ^ n := e.to_alg_hom.map_pow
protected lemma injective : function.injective e := equiv_like.injective e
protected lemma surjective : function.surjective e := equiv_like.surjective e
protected lemma bijective : function.bijective e := equiv_like.bijective e
/-- Algebra equivalences are reflexive. -/
@[refl] def refl : A₁ ≃ₐ[R] A₁ := {commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}
instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨refl⟩
@[simp] lemma refl_to_alg_hom : ↑(refl : A₁ ≃ₐ[R] A₁) = alg_hom.id R A₁ := rfl
@[simp] lemma coe_refl : ⇑(refl : A₁ ≃ₐ[R] A₁) = id := rfl
/-- Algebra equivalences are symmetric. -/
@[symm]
def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ :=
{ commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr,
change _ = e _, rw e.commutes, },
..e.to_ring_equiv.symm, }
/-- See Note [custom simps projection] -/
def simps.symm_apply (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm
initialize_simps_projections alg_equiv (to_fun → apply, inv_fun → symm_apply)
@[simp] lemma inv_fun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.inv_fun = e.symm := rfl
@[simp] lemma symm_symm (e : A₁ ≃ₐ[R] A₂) : e.symm.symm = e :=
by { ext, refl, }
lemma symm_bijective : function.bijective (symm : (A₁ ≃ₐ[R] A₂) → (A₂ ≃ₐ[R] A₁)) :=
equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩
@[simp] lemma mk_coe' (e : A₁ ≃ₐ[R] A₂) (f h₁ h₂ h₃ h₄ h₅) :
(⟨f, e, h₁, h₂, h₃, h₄, h₅⟩ : A₂ ≃ₐ[R] A₁) = e.symm :=
symm_bijective.injective $ ext $ λ x, rfl
@[simp] theorem symm_mk (f f') (h₁ h₂ h₃ h₄ h₅) :
(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm =
{ to_fun := f', inv_fun := f,
..(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm } := rfl
@[simp]
theorem refl_symm : (alg_equiv.refl : A₁ ≃ₐ[R] A₁).symm = alg_equiv.refl := rfl
/-- Algebra equivalences are transitive. -/
@[trans]
def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ :=
{ commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'],
..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), }
@[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x :=
e.to_equiv.apply_symm_apply
@[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x :=
e.to_equiv.symm_apply_apply
@[simp] lemma symm_trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₃) :
(e₁.trans e₂).symm x = e₁.symm (e₂.symm x) := rfl
@[simp] lemma coe_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) :
⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl
@[simp] lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) :
(e₁.trans e₂) x = e₂ (e₁ x) := rfl
@[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ :=
by { ext, simp }
@[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ :=
by { ext, simp }
theorem left_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.left_inverse e.symm e := e.left_inv
theorem right_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.right_inverse e.symm e := e.right_inv
/-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps
`A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/
def arrow_congr {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂']
(e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') :=
{ to_fun := λ f, (e₂.to_alg_hom.comp f).comp e₁.symm.to_alg_hom,
inv_fun := λ f, (e₂.symm.to_alg_hom.comp f).comp e₁.to_alg_hom,
left_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, symm_comp],
simp only [←alg_hom.comp_assoc, symm_comp, alg_hom.id_comp, alg_hom.comp_id] },
right_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, comp_symm],
simp only [←alg_hom.comp_assoc, comp_symm, alg_hom.id_comp, alg_hom.comp_id] } }
lemma arrow_congr_comp {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃']
[algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂')
(e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) :
arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) :=
by { ext, simp only [arrow_congr, equiv.coe_fn_mk, alg_hom.comp_apply],
congr, exact (e₂.symm_apply_apply _).symm }
@[simp] lemma arrow_congr_refl :
arrow_congr alg_equiv.refl alg_equiv.refl = equiv.refl (A₁ →ₐ[R] A₂) :=
by { ext, refl }
@[simp] lemma arrow_congr_trans {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃']
[algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂')
(e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') :
arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') :=
by { ext, refl }
@[simp] lemma arrow_congr_symm {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂']
[algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') :
(arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm :=
by { ext, refl }
/-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/
def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂)
(h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ :=
{ to_fun := f,
inv_fun := g,
left_inv := alg_hom.ext_iff.1 h₂,
right_inv := alg_hom.ext_iff.1 h₁,
..f }
lemma coe_alg_hom_of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
↑(of_alg_hom f g h₁ h₂) = f := alg_hom.ext $ λ _, rfl
@[simp]
lemma of_alg_hom_coe_alg_hom (f : A₁ ≃ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
of_alg_hom ↑f g h₁ h₂ = f := ext $ λ _, rfl
lemma of_alg_hom_symm (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) :
(of_alg_hom f g h₁ h₂).symm = of_alg_hom g f h₂ h₁ := rfl
/-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/
noncomputable def of_bijective (f : A₁ →ₐ[R] A₂) (hf : function.bijective f) : A₁ ≃ₐ[R] A₂ :=
{ .. ring_equiv.of_bijective (f : A₁ →+* A₂) hf, .. f }
@[simp] lemma coe_of_bijective {f : A₁ →ₐ[R] A₂} {hf : function.bijective f} :
(alg_equiv.of_bijective f hf : A₁ → A₂) = f := rfl
lemma of_bijective_apply {f : A₁ →ₐ[R] A₂} {hf : function.bijective f} (a : A₁) :
(alg_equiv.of_bijective f hf) a = f a := rfl
/-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/
@[simps apply] def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ :=
{ to_fun := e,
map_smul' := e.map_smul,
inv_fun := e.symm,
.. e }
@[simp] lemma to_linear_equiv_refl :
(alg_equiv.refl : A₁ ≃ₐ[R] A₁).to_linear_equiv = linear_equiv.refl R A₁ := rfl
@[simp] lemma to_linear_equiv_symm (e : A₁ ≃ₐ[R] A₂) :
e.to_linear_equiv.symm = e.symm.to_linear_equiv := rfl
@[simp] lemma to_linear_equiv_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) :
(e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv := rfl
theorem to_linear_equiv_injective : function.injective (to_linear_equiv : _ → (A₁ ≃ₗ[R] A₂)) :=
λ e₁ e₂ h, ext $ linear_equiv.congr_fun h
/-- Interpret an algebra equivalence as a linear map. -/
def to_linear_map : A₁ →ₗ[R] A₂ :=
e.to_alg_hom.to_linear_map
@[simp] lemma to_alg_hom_to_linear_map :
(e : A₁ →ₐ[R] A₂).to_linear_map = e.to_linear_map := rfl
@[simp] lemma to_linear_equiv_to_linear_map :
e.to_linear_equiv.to_linear_map = e.to_linear_map := rfl
@[simp] lemma to_linear_map_apply (x : A₁) : e.to_linear_map x = e x := rfl
theorem to_linear_map_injective : function.injective (to_linear_map : _ → (A₁ →ₗ[R] A₂)) :=
λ e₁ e₂ h, ext $ linear_map.congr_fun h
@[simp] lemma trans_to_linear_map (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) :
(f.trans g).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
section of_linear_equiv
variables (l : A₁ ≃ₗ[R] A₂)
(map_mul : ∀ x y : A₁, l (x * y) = l x * l y)
(commutes : ∀ r : R, l (algebra_map R A₁ r) = algebra_map R A₂ r)
/--
Upgrade a linear equivalence to an algebra equivalence,
given that it distributes over multiplication and action of scalars.
-/
@[simps apply]
def of_linear_equiv : A₁ ≃ₐ[R] A₂ :=
{ to_fun := l,
inv_fun := l.symm,
map_mul' := map_mul,
commutes' := commutes,
..l }
@[simp]
lemma of_linear_equiv_symm :
(of_linear_equiv l map_mul commutes).symm = of_linear_equiv l.symm
((of_linear_equiv l map_mul commutes).symm.map_mul)
((of_linear_equiv l map_mul commutes).symm.commutes) :=
rfl
@[simp] lemma of_linear_equiv_to_linear_equiv (map_mul) (commutes) :
of_linear_equiv e.to_linear_equiv map_mul commutes = e :=
by { ext, refl }
@[simp] lemma to_linear_equiv_of_linear_equiv :
to_linear_equiv (of_linear_equiv l map_mul commutes) = l :=
by { ext, refl }
end of_linear_equiv
@[simps mul one {attrs := []}] instance aut : group (A₁ ≃ₐ[R] A₁) :=
{ mul := λ ϕ ψ, ψ.trans ϕ,
mul_assoc := λ ϕ ψ χ, rfl,
one := refl,
one_mul := λ ϕ, ext $ λ x, rfl,
mul_one := λ ϕ, ext $ λ x, rfl,
inv := symm,
mul_left_inv := λ ϕ, ext $ symm_apply_apply ϕ }
@[simp] lemma one_apply (x : A₁) : (1 : A₁ ≃ₐ[R] A₁) x = x := rfl
@[simp] lemma mul_apply (e₁ e₂ : A₁ ≃ₐ[R] A₁) (x : A₁) : (e₁ * e₂) x = e₁ (e₂ x) := rfl
/-- An algebra isomorphism induces a group isomorphism between automorphism groups -/
@[simps apply]
def aut_congr (ϕ : A₁ ≃ₐ[R] A₂) : (A₁ ≃ₐ[R] A₁) ≃* (A₂ ≃ₐ[R] A₂) :=
{ to_fun := λ ψ, ϕ.symm.trans (ψ.trans ϕ),
inv_fun := λ ψ, ϕ.trans (ψ.trans ϕ.symm),
left_inv := λ ψ, by { ext, simp_rw [trans_apply, symm_apply_apply] },
right_inv := λ ψ, by { ext, simp_rw [trans_apply, apply_symm_apply] },
map_mul' := λ ψ χ, by { ext, simp only [mul_apply, trans_apply, symm_apply_apply] } }
@[simp] lemma aut_congr_refl : aut_congr (alg_equiv.refl) = mul_equiv.refl (A₁ ≃ₐ[R] A₁) :=
by { ext, refl }
@[simp] lemma aut_congr_symm (ϕ : A₁ ≃ₐ[R] A₂) : (aut_congr ϕ).symm = aut_congr ϕ.symm := rfl
@[simp] lemma aut_congr_trans (ϕ : A₁ ≃ₐ[R] A₂) (ψ : A₂ ≃ₐ[R] A₃) :
(aut_congr ϕ).trans (aut_congr ψ) = aut_congr (ϕ.trans ψ) := rfl
/-- The tautological action by `A₁ ≃ₐ[R] A₁` on `A₁`.
This generalizes `function.End.apply_mul_action`. -/
instance apply_mul_semiring_action : mul_semiring_action (A₁ ≃ₐ[R] A₁) A₁ :=
{ smul := ($),
smul_zero := alg_equiv.map_zero,
smul_add := alg_equiv.map_add,
smul_one := alg_equiv.map_one,
smul_mul := alg_equiv.map_mul,
one_smul := λ _, rfl,
mul_smul := λ _ _ _, rfl }
@[simp] protected lemma smul_def (f : A₁ ≃ₐ[R] A₁) (a : A₁) : f • a = f a := rfl
instance apply_has_faithful_scalar : has_faithful_scalar (A₁ ≃ₐ[R] A₁) A₁ :=
⟨λ _ _, alg_equiv.ext⟩
instance apply_smul_comm_class : smul_comm_class R (A₁ ≃ₐ[R] A₁) A₁ :=
{ smul_comm := λ r e a, (e.map_smul r a).symm }
instance apply_smul_comm_class' : smul_comm_class (A₁ ≃ₐ[R] A₁) R A₁ :=
{ smul_comm := λ e r a, (e.map_smul r a) }
@[simp] lemma algebra_map_eq_apply (e : A₁ ≃ₐ[R] A₂) {y : R} {x : A₁} :
(algebra_map R A₂ y = e x) ↔ (algebra_map R A₁ y = x) :=
⟨λ h, by simpa using e.symm.to_alg_hom.algebra_map_eq_apply h,
λ h, e.to_alg_hom.algebra_map_eq_apply h⟩
end semiring
section comm_semiring
variables [comm_semiring R] [comm_semiring A₁] [comm_semiring A₂]
variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂)
lemma map_prod {ι : Type*} (f : ι → A₁) (s : finset ι) :
e (∏ x in s, f x) = ∏ x in s, e (f x) :=
e.to_alg_hom.map_prod f s
lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) :
e (f.prod g) = f.prod (λ i a, e (g i a)) :=
e.to_alg_hom.map_finsupp_prod f g
end comm_semiring
section ring
variables [comm_semiring R] [ring A₁] [ring A₂]
variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂)
protected lemma map_neg (x) : e (-x) = -e x := map_neg e x
protected lemma map_sub (x y) : e (x - y) = e x - e y := map_sub e x y
end ring
section division_ring
variables [comm_ring R] [division_ring A₁] [division_ring A₂]
variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂)
@[simp] lemma map_inv (x) : e (x⁻¹) = (e x)⁻¹ :=
e.to_alg_hom.map_inv x
@[simp] lemma map_div (x y) : e (x / y) = e x / e y :=
e.to_alg_hom.map_div x y
end division_ring
end alg_equiv
namespace mul_semiring_action
variables {M G : Type*} (R A : Type*) [comm_semiring R] [semiring A] [algebra R A]
section
variables [monoid M] [mul_semiring_action M A] [smul_comm_class M R A]
/-- Each element of the monoid defines a algebra homomorphism.
This is a stronger version of `mul_semiring_action.to_ring_hom` and
`distrib_mul_action.to_linear_map`. -/
@[simps]
def to_alg_hom (m : M) : A →ₐ[R] A :=
alg_hom.mk' (mul_semiring_action.to_ring_hom _ _ m) (smul_comm _)
theorem to_alg_hom_injective [has_faithful_scalar M A] :
function.injective (mul_semiring_action.to_alg_hom R A : M → A →ₐ[R] A) :=
λ m₁ m₂ h, eq_of_smul_eq_smul $ λ r, alg_hom.ext_iff.1 h r
end
section
variables [group G] [mul_semiring_action G A] [smul_comm_class G R A]
/-- Each element of the group defines a algebra equivalence.
This is a stronger version of `mul_semiring_action.to_ring_equiv` and
`distrib_mul_action.to_linear_equiv`. -/
@[simps]
def to_alg_equiv (g : G) : A ≃ₐ[R] A :=
{ .. mul_semiring_action.to_ring_equiv _ _ g,
.. mul_semiring_action.to_alg_hom R A g }
theorem to_alg_equiv_injective [has_faithful_scalar G A] :
function.injective (mul_semiring_action.to_alg_equiv R A : G → A ≃ₐ[R] A) :=
λ m₁ m₂ h, eq_of_smul_eq_smul $ λ r, alg_equiv.ext_iff.1 h r
end
end mul_semiring_action
section nat
variables {R : Type*} [semiring R]
-- Lower the priority so that `algebra.id` is picked most of the time when working with
-- `ℕ`-algebras. This is only an issue since `algebra.id` and `algebra_nat` are not yet defeq.
-- TODO: fix this by adding an `of_nat` field to semirings.
/-- Semiring ⥤ ℕ-Alg -/
@[priority 99] instance algebra_nat : algebra ℕ R :=
{ commutes' := nat.cast_commute,
smul_def' := λ _ _, nsmul_eq_mul _ _,
to_ring_hom := nat.cast_ring_hom R }
instance nat_algebra_subsingleton : subsingleton (algebra ℕ R) :=
⟨λ P Q, by { ext, simp, }⟩
end nat
namespace ring_hom
variables {R S : Type*}
/-- Reinterpret a `ring_hom` as an `ℕ`-algebra homomorphism. -/
def to_nat_alg_hom [semiring R] [semiring S] (f : R →+* S) :
R →ₐ[ℕ] S :=
{ to_fun := f, commutes' := λ n, by simp, .. f }
/-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/
def to_int_alg_hom [ring R] [ring S] [algebra ℤ R] [algebra ℤ S] (f : R →+* S) :
R →ₐ[ℤ] S :=
{ commutes' := λ n, by simp, .. f }
-- note that `R`, `S` could be `semiring`s but this is useless mathematically speaking -
-- a ℚ-algebra is a ring. furthermore, this change probably slows down elaboration.
@[simp] lemma map_rat_algebra_map [ring R] [ring S] [algebra ℚ R] [algebra ℚ S]
(f : R →+* S) (r : ℚ) : f (algebra_map ℚ R r) = algebra_map ℚ S r :=
ring_hom.ext_iff.1 (subsingleton.elim (f.comp (algebra_map ℚ R)) (algebra_map ℚ S)) r
/-- Reinterpret a `ring_hom` as a `ℚ`-algebra homomorphism. -/
def to_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) :
R →ₐ[ℚ] S :=
{ commutes' := f.map_rat_algebra_map, .. f }
end ring_hom
section rat
instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α :=
(rat.cast_hom α).to_algebra' $ λ r x, r.cast_commute x
@[simp] theorem algebra_map_rat_rat : algebra_map ℚ ℚ = ring_hom.id ℚ :=
subsingleton.elim _ _
-- TODO[gh-6025]: make this an instance once safe to do so
lemma algebra_rat_subsingleton {α} [semiring α] :
subsingleton (algebra ℚ α) :=
⟨λ x y, algebra.algebra_ext x y $ ring_hom.congr_fun $ subsingleton.elim _ _⟩
end rat
namespace algebra
open module
variables (R : Type u) (A : Type v)
variables [comm_semiring R] [semiring A] [algebra R A]
/-- `algebra_map` as an `alg_hom`. -/
def of_id : R →ₐ[R] A :=
{ commutes' := λ _, rfl, .. algebra_map R A }
variables {R}
theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl
end algebra
section int
variables (R : Type*) [ring R]
-- Lower the priority so that `algebra.id` is picked most of the time when working with
-- `ℤ`-algebras. This is only an issue since `algebra.id ℤ` and `algebra_int ℤ` are not yet defeq.
-- TODO: fix this by adding an `of_int` field to rings.
/-- Ring ⥤ ℤ-Alg -/
@[priority 99] instance algebra_int : algebra ℤ R :=
{ commutes' := int.cast_commute,
smul_def' := λ _ _, zsmul_eq_mul _ _,
to_ring_hom := int.cast_ring_hom R }
/-- A special case of `ring_hom.eq_int_cast'` that happens to be true definitionally -/
@[simp] lemma algebra_map_int_eq : algebra_map ℤ R = int.cast_ring_hom R := rfl
variables {R}
instance int_algebra_subsingleton : subsingleton (algebra ℤ R) :=
⟨λ P Q, by { ext, simp, }⟩
end int
/-!
The R-algebra structure on `Π i : I, A i` when each `A i` is an R-algebra.
We couldn't set this up back in `algebra.pi_instances` because this file imports it.
-/
namespace pi
variable {I : Type u} -- The indexing type
variable {R : Type*} -- The scalar type
variable {f : I → Type v} -- The family of types already equipped with instances
variables (x y : Π i, f i) (i : I)
variables (I f)
instance algebra {r : comm_semiring R}
[s : ∀ i, semiring (f i)] [∀ i, algebra R (f i)] :
algebra R (Π i : I, f i) :=
{ commutes' := λ a f, begin ext, simp [algebra.commutes], end,
smul_def' := λ a f, begin ext, simp [algebra.smul_def], end,
..(pi.ring_hom (λ i, algebra_map R (f i)) : R →+* Π i : I, f i) }
@[simp] lemma algebra_map_apply {r : comm_semiring R}
[s : ∀ i, semiring (f i)] [∀ i, algebra R (f i)] (a : R) (i : I) :
algebra_map R (Π i, f i) a i = algebra_map R (f i) a := rfl
-- One could also build a `Π i, R i`-algebra structure on `Π i, A i`,
-- when each `A i` is an `R i`-algebra, although I'm not sure that it's useful.
variables {I} (R) (f)
/-- `function.eval` as an `alg_hom`. The name matches `pi.eval_ring_hom`, `pi.eval_monoid_hom`,
etc. -/
@[simps]
def eval_alg_hom {r : comm_semiring R} [Π i, semiring (f i)] [Π i, algebra R (f i)] (i : I) :
(Π i, f i) →ₐ[R] f i :=
{ to_fun := λ f, f i, commutes' := λ r, rfl, .. pi.eval_ring_hom f i}
variables (A B : Type*) [comm_semiring R] [semiring B] [algebra R B]
/-- `function.const` as an `alg_hom`. The name matches `pi.const_ring_hom`, `pi.const_monoid_hom`,
etc. -/
@[simps]
def const_alg_hom : B →ₐ[R] (A → B) :=
{ to_fun := function.const _,
commutes' := λ r, rfl,
.. pi.const_ring_hom A B}
/-- When `R` is commutative and permits an `algebra_map`, `pi.const_ring_hom` is equal to that
map. -/
@[simp] lemma const_ring_hom_eq_algebra_map : const_ring_hom A R = algebra_map R (A → R) :=
rfl
@[simp] lemma const_alg_hom_eq_algebra_of_id : const_alg_hom R A R = algebra.of_id R (A → R) :=
rfl
end pi
/-- A special case of `pi.algebra` for non-dependent types. Lean struggles to elaborate
definitions elsewhere in the library without this, -/
instance function.algebra {R : Type*} (I : Type*) (A : Type*) [comm_semiring R]
[semiring A] [algebra R A] : algebra R (I → A) :=
pi.algebra _ _
namespace alg_equiv
/-- A family of algebra equivalences `Π j, (A₁ j ≃ₐ A₂ j)` generates a
multiplicative equivalence between `Π j, A₁ j` and `Π j, A₂ j`.
This is the `alg_equiv` version of `equiv.Pi_congr_right`, and the dependent version of
`alg_equiv.arrow_congr`.
-/
@[simps apply]
def Pi_congr_right {R ι : Type*} {A₁ A₂ : ι → Type*} [comm_semiring R]
[Π i, semiring (A₁ i)] [Π i, semiring (A₂ i)] [Π i, algebra R (A₁ i)] [Π i, algebra R (A₂ i)]
(e : Π i, A₁ i ≃ₐ[R] A₂ i) : (Π i, A₁ i) ≃ₐ[R] Π i, A₂ i :=
{ to_fun := λ x j, e j (x j),
inv_fun := λ x j, (e j).symm (x j),
commutes' := λ r, by { ext i, simp },
.. @ring_equiv.Pi_congr_right ι A₁ A₂ _ _ (λ i, (e i).to_ring_equiv) }
@[simp]
lemma Pi_congr_right_refl {R ι : Type*} {A : ι → Type*} [comm_semiring R]
[Π i, semiring (A i)] [Π i, algebra R (A i)] :
Pi_congr_right (λ i, (alg_equiv.refl : A i ≃ₐ[R] A i)) = alg_equiv.refl := rfl
@[simp]
lemma Pi_congr_right_symm {R ι : Type*} {A₁ A₂ : ι → Type*} [comm_semiring R]
[Π i, semiring (A₁ i)] [Π i, semiring (A₂ i)] [Π i, algebra R (A₁ i)] [Π i, algebra R (A₂ i)]
(e : Π i, A₁ i ≃ₐ[R] A₂ i) : (Pi_congr_right e).symm = (Pi_congr_right $ λ i, (e i).symm) := rfl
@[simp]
lemma Pi_congr_right_trans {R ι : Type*} {A₁ A₂ A₃ : ι → Type*} [comm_semiring R]
[Π i, semiring (A₁ i)] [Π i, semiring (A₂ i)] [Π i, semiring (A₃ i)]
[Π i, algebra R (A₁ i)] [Π i, algebra R (A₂ i)] [Π i, algebra R (A₃ i)]
(e₁ : Π i, A₁ i ≃ₐ[R] A₂ i) (e₂ : Π i, A₂ i ≃ₐ[R] A₃ i) :
(Pi_congr_right e₁).trans (Pi_congr_right e₂) = (Pi_congr_right $ λ i, (e₁ i).trans (e₂ i)) :=
rfl
end alg_equiv
section is_scalar_tower
variables {R : Type*} [comm_semiring R]
variables (A : Type*) [semiring A] [algebra R A]
variables {M : Type*} [add_comm_monoid M] [module A M] [module R M] [is_scalar_tower R A M]
variables {N : Type*} [add_comm_monoid N] [module A N] [module R N] [is_scalar_tower R A N]
lemma algebra_compatible_smul (r : R) (m : M) : r • m = ((algebra_map R A) r) • m :=
by rw [←(one_smul A m), ←smul_assoc, algebra.smul_def, mul_one, one_smul]
@[simp] lemma algebra_map_smul (r : R) (m : M) : ((algebra_map R A) r) • m = r • m :=
(algebra_compatible_smul A r m).symm
lemma no_zero_smul_divisors.trans (R A M : Type*) [comm_ring R] [ring A] [is_domain A] [algebra R A]
[add_comm_group M] [module R M] [module A M] [is_scalar_tower R A M] [no_zero_smul_divisors R A]
[no_zero_smul_divisors A M] : no_zero_smul_divisors R M :=
begin
refine ⟨λ r m h, _⟩,
rw [algebra_compatible_smul A r m] at h,
cases smul_eq_zero.1 h with H H,
{ have : function.injective (algebra_map R A) :=
no_zero_smul_divisors.iff_algebra_map_injective.1 infer_instance,
left,
exact (injective_iff_map_eq_zero _).1 this _ H },
{ right,
exact H }
end
variable {A}
@[priority 100] -- see Note [lower instance priority]
instance is_scalar_tower.to_smul_comm_class : smul_comm_class R A M :=
⟨λ r a m, by rw [algebra_compatible_smul A r (a • m), smul_smul, algebra.commutes, mul_smul,
←algebra_compatible_smul]⟩
@[priority 100] -- see Note [lower instance priority]
instance is_scalar_tower.to_smul_comm_class' : smul_comm_class A R M :=
smul_comm_class.symm _ _ _
lemma smul_algebra_smul_comm (r : R) (a : A) (m : M) : a • r • m = r • a • m :=
smul_comm _ _ _
namespace linear_map
instance coe_is_scalar_tower : has_coe (M →ₗ[A] N) (M →ₗ[R] N) :=
⟨restrict_scalars R⟩
variables (R) {A M N}
@[simp, norm_cast squash] lemma coe_restrict_scalars_eq_coe (f : M →ₗ[A] N) :
(f.restrict_scalars R : M → N) = f := rfl
@[simp, norm_cast squash] lemma coe_coe_is_scalar_tower (f : M →ₗ[A] N) :
((f : M →ₗ[R] N) : M → N) = f := rfl
/-- `A`-linearly coerce a `R`-linear map from `M` to `A` to a function, given an algebra `A` over
a commutative semiring `R` and `M` a module over `R`. -/
def lto_fun (R : Type u) (M : Type v) (A : Type w)
[comm_semiring R] [add_comm_monoid M] [module R M] [comm_ring A] [algebra R A] :
(M →ₗ[R] A) →ₗ[A] (M → A) :=
{ to_fun := linear_map.to_fun,
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl }
end linear_map
end is_scalar_tower
/-! TODO: The following lemmas no longer involve `algebra` at all, and could be moved closer
to `algebra/module/submodule.lean`. Currently this is tricky because `ker`, `range`, `⊤`, and `⊥`
are all defined in `linear_algebra/basic.lean`. -/
section module
open module
variables (R S M N : Type*) [semiring R] [semiring S] [has_scalar R S]
variables [add_comm_monoid M] [module R M] [module S M] [is_scalar_tower R S M]
variables [add_comm_monoid N] [module R N] [module S N] [is_scalar_tower R S N]
variables {S M N}
@[simp]
lemma linear_map.ker_restrict_scalars (f : M →ₗ[S] N) :
(f.restrict_scalars R).ker = f.ker.restrict_scalars R :=
rfl
end module
namespace submodule
variables (R A M : Type*)
variables [comm_semiring R] [semiring A] [algebra R A] [add_comm_monoid M]
variables [module R M] [module A M] [is_scalar_tower R A M]
/-- If `A` is an `R`-algebra such that the induced morhpsim `R →+* A` is surjective, then the
`R`-module generated by a set `X` equals the `A`-module generated by `X`. -/
lemma span_eq_restrict_scalars (X : set M) (hsur : function.surjective (algebra_map R A)) :
span R X = restrict_scalars R (span A X) :=
begin
apply (span_le_restrict_scalars R A X).antisymm (λ m hm, _),
refine span_induction hm subset_span (zero_mem _) (λ _ _, add_mem) (λ a m hm, _),
obtain ⟨r, rfl⟩ := hsur a,
simpa [algebra_map_smul] using smul_mem _ r hm
end
end submodule
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {I : Type*}
variables [comm_semiring R] [semiring A] [semiring B]
variables [algebra R A] [algebra R B]
/-- `R`-algebra homomorphism between the function spaces `I → A` and `I → B`, induced by an
`R`-algebra homomorphism `f` between `A` and `B`. -/
@[simps] protected def comp_left (f : A →ₐ[R] B) (I : Type*) : (I → A) →ₐ[R] (I → B) :=
{ to_fun := λ h, f ∘ h,
commutes' := λ c, by { ext, exact f.commutes' c },
.. f.to_ring_hom.comp_left I }
end alg_hom
example {R A} [comm_semiring R] [semiring A]
[module R A] [smul_comm_class R A A] [is_scalar_tower R A A] : algebra R A :=
algebra.of_module smul_mul_assoc mul_smul_comm
|
9de299289d0e5ccee61b4f3f7202f7725d80c188 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/continued_fractions/computation/basic.lean | a8a7cbf956561e29f8560bef4b839e81f299f094 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 7,941 | lean | /-
Copyright (c) 2020 Kevin Kappelmann. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Kappelmann
-/
import algebra.order.floor
import algebra.continued_fractions.basic
/-!
# Computable Continued Fractions
## Summary
We formalise the standard computation of (regular) continued fractions for linear ordered floor
fields. The algorithm is rather simple. Here is an outline of the procedure adapted from Wikipedia:
Take a value `v`. We call `⌊v⌋` the *integer part* of `v` and `v - ⌊v⌋` the *fractional part* of
`v`. A continued fraction representation of `v` can then be given by `[⌊v⌋; b₀, b₁, b₂,...]`, where
`[b₀; b₁, b₂,...]` recursively is the continued fraction representation of `1 / (v - ⌊v⌋)`. This
process stops when the fractional part hits 0.
In other words: to calculate a continued fraction representation of a number `v`, write down the
integer part (i.e. the floor) of `v`. Subtract this integer part from `v`. If the difference is 0,
stop; otherwise find the reciprocal of the difference and repeat. The procedure will terminate if
and only if `v` is rational.
For an example, refer to `int_fract_pair.stream`.
## Main definitions
- `generalized_continued_fraction.int_fract_pair.stream`: computes the stream of integer and
fractional parts of a given value as described in the summary.
- `generalized_continued_fraction.of`: computes the generalised continued fraction of a value `v`.
In fact, it computes a regular continued fraction that terminates if and only if `v` is rational
(those proofs will be added in a future commit).
## Implementation Notes
There is an intermediate definition `generalized_continued_fraction.int_fract_pair.seq1` between
`generalized_continued_fraction.int_fract_pair.stream` and `generalized_continued_fraction.of`
to wire up things. User should not (need to) directly interact with it.
The computation of the integer and fractional pairs of a value can elegantly be
captured by a recursive computation of a stream of option pairs. This is done in
`int_fract_pair.stream`. However, the type then does not guarantee the first pair to always be
`some` value, as expected by a continued fraction.
To separate concerns, we first compute a single head term that always exists in
`generalized_continued_fraction.int_fract_pair.seq1` followed by the remaining stream of option
pairs. This sequence with a head term (`seq1`) is then transformed to a generalized continued
fraction in `generalized_continued_fraction.of` by extracting the wanted integer parts of the
head term and the stream.
## References
- https://en.wikipedia.org/wiki/Continued_fraction
## Tags
numerics, number theory, approximations, fractions
-/
namespace generalized_continued_fraction
-- Fix a carrier `K`.
variable (K : Type*)
/--
We collect an integer part `b = ⌊v⌋` and fractional part `fr = v - ⌊v⌋` of a value `v` in a pair
`⟨b, fr⟩`.
-/
structure int_fract_pair := (b : ℤ) (fr : K)
variable {K}
/-! Interlude: define some expected coercions and instances. -/
namespace int_fract_pair
/-- Make an `int_fract_pair` printable. -/
instance [has_repr K] : has_repr (int_fract_pair K) :=
⟨λ p, "(b : " ++ (repr p.b) ++ ", fract : " ++ (repr p.fr) ++ ")"⟩
instance inhabited [inhabited K] : inhabited (int_fract_pair K) := ⟨⟨0, default⟩⟩
/--
Maps a function `f` on the fractional components of a given pair.
-/
def mapFr {β : Type*} (f : K → β) (gp : int_fract_pair K) : int_fract_pair β :=
⟨gp.b, f gp.fr⟩
section coe
/-! Interlude: define some expected coercions. -/
/- Fix another type `β` which we will convert to. -/
variables {β : Type*} [has_coe K β]
/-- Coerce a pair by coercing the fractional component. -/
instance has_coe_to_int_fract_pair : has_coe (int_fract_pair K) (int_fract_pair β) :=
⟨mapFr coe⟩
@[simp, norm_cast]
lemma coe_to_int_fract_pair {b : ℤ} {fr : K} :
(↑(int_fract_pair.mk b fr) : int_fract_pair β) = int_fract_pair.mk b (↑fr : β) :=
rfl
end coe
-- Note: this could be relaxed to something like `linear_ordered_division_ring` in the
-- future.
/- Fix a discrete linear ordered field with `floor` function. -/
variables [linear_ordered_field K] [floor_ring K]
/-- Creates the integer and fractional part of a value `v`, i.e. `⟨⌊v⌋, v - ⌊v⌋⟩`. -/
protected def of (v : K) : int_fract_pair K := ⟨⌊v⌋, int.fract v⟩
/--
Creates the stream of integer and fractional parts of a value `v` needed to obtain the continued
fraction representation of `v` in `generalized_continued_fraction.of`. More precisely, given a value
`v : K`, it recursively computes a stream of option `ℤ × K` pairs as follows:
- `stream v 0 = some ⟨⌊v⌋, v - ⌊v⌋⟩`
- `stream v (n + 1) = some ⟨⌊frₙ⁻¹⌋, frₙ⁻¹ - ⌊frₙ⁻¹⌋⟩`,
if `stream v n = some ⟨_, frₙ⟩` and `frₙ ≠ 0`
- `stream v (n + 1) = none`, otherwise
For example, let `(v : ℚ) := 3.4`. The process goes as follows:
- `stream v 0 = some ⟨⌊v⌋, v - ⌊v⌋⟩ = some ⟨3, 0.4⟩`
- `stream v 1 = some ⟨⌊0.4⁻¹⌋, 0.4⁻¹ - ⌊0.4⁻¹⌋⟩ = some ⟨⌊2.5⌋, 2.5 - ⌊2.5⌋⟩ = some ⟨2, 0.5⟩`
- `stream v 2 = some ⟨⌊0.5⁻¹⌋, 0.5⁻¹ - ⌊0.5⁻¹⌋⟩ = some ⟨⌊2⌋, 2 - ⌊2⌋⟩ = some ⟨2, 0⟩`
- `stream v n = none`, for `n ≥ 3`
-/
protected def stream (v : K) : stream $ option (int_fract_pair K)
| 0 := some (int_fract_pair.of v)
| (n + 1) := do ap_n ← stream n,
if ap_n.fr = 0 then none else int_fract_pair.of ap_n.fr⁻¹
/--
Shows that `int_fract_pair.stream` has the sequence property, that is once we return `none` at
position `n`, we also return `none` at `n + 1`.
-/
lemma stream_is_seq (v : K) : (int_fract_pair.stream v).is_seq :=
by { assume _ hyp, simp [int_fract_pair.stream, hyp] }
/--
Uses `int_fract_pair.stream` to create a sequence with head (i.e. `seq1`) of integer and fractional
parts of a value `v`. The first value of `int_fract_pair.stream` is never `none`, so we can safely
extract it and put the tail of the stream in the sequence part.
This is just an intermediate representation and users should not (need to) directly interact with
it. The setup of rewriting/simplification lemmas that make the definitions easy to use is done in
`algebra.continued_fractions.computation.translations`.
-/
protected def seq1 (v : K) : seq1 $ int_fract_pair K :=
⟨ int_fract_pair.of v,--the head
seq.tail -- take the tail of `int_fract_pair.stream` since the first element is already in the
-- head create a sequence from `int_fract_pair.stream`
⟨ int_fract_pair.stream v, -- the underlying stream
@stream_is_seq _ _ _ v ⟩ ⟩ -- the proof that the stream is a sequence
end int_fract_pair
/--
Returns the `generalized_continued_fraction` of a value. In fact, the returned gcf is also
a `continued_fraction` that terminates if and only if `v` is rational (those proofs will be
added in a future commit).
The continued fraction representation of `v` is given by `[⌊v⌋; b₀, b₁, b₂,...]`, where
`[b₀; b₁, b₂,...]` recursively is the continued fraction representation of `1 / (v - ⌊v⌋)`. This
process stops when the fractional part `v - ⌊v⌋` hits 0 at some step.
The implementation uses `int_fract_pair.stream` to obtain the partial denominators of the continued
fraction. Refer to said function for more details about the computation process.
-/
protected def of [linear_ordered_field K] [floor_ring K] (v : K) :
generalized_continued_fraction K :=
let ⟨h, s⟩ := int_fract_pair.seq1 v in -- get the sequence of integer and fractional parts.
⟨ h.b, -- the head is just the first integer part
s.map (λ p, ⟨1, p.b⟩) ⟩ -- the sequence consists of the remaining integer parts as the partial
-- denominators; all partial numerators are simply 1
end generalized_continued_fraction
|
7547a9bb0a850bb14b441b057e16c1de93d4facf | d642a6b1261b2cbe691e53561ac777b924751b63 | /src/analysis/normed_space/riesz_lemma.lean | d698b6c01b087f70a7f52c3495e46b183847f34d | [
"Apache-2.0"
] | permissive | cipher1024/mathlib | fee56b9954e969721715e45fea8bcb95f9dc03fe | d077887141000fefa5a264e30fa57520e9f03522 | refs/heads/master | 1,651,806,490,504 | 1,573,508,694,000 | 1,573,508,694,000 | 107,216,176 | 0 | 0 | Apache-2.0 | 1,647,363,136,000 | 1,508,213,014,000 | Lean | UTF-8 | Lean | false | false | 1,898 | lean | /-
Copyright (c) 2019 Jean Lo. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jean Lo
-/
import analysis.normed_space.basic
import topology.metric_space.hausdorff_distance
/-!
# Riesz's lemma
Riesz's lemma, stated for a normed space over a normed field: for any
closed proper subspace F of E, there is a nonzero x such that ∥x - F∥
is at least r * ∥x∥ for any r < 1.
-/
variables {𝕜 : Type*} [normed_field 𝕜]
variables {E : Type*} [normed_group E] [normed_space 𝕜 E]
/-- Riesz's lemma, which usually states that it is possible to find a
vector with norm 1 whose distance to a closed proper subspace is
arbitrarily close to 1. The statement here is in terms of multiples of
norms, since in general the existence of an element of norm exactly 1
is not guaranteed. -/
lemma riesz_lemma {F : subspace 𝕜 E} (hFc : is_closed (F : set E))
(hF : ∃ x : E, x ∉ F) {r : ℝ} (hr : r < 1) :
∃ x₀ : E, ∀ y : F, r * ∥x₀∥ ≤ ∥x₀ - y∥ :=
or.cases_on (le_or_lt r 0)
(λ hle, ⟨0, λ _, by {rw [norm_zero, mul_zero], exact norm_nonneg _}⟩)
(λ hlt,
let ⟨x, hx⟩ := hF in
let d := metric.inf_dist x F in
have hFn : (F : set E) ≠ ∅, from set.ne_empty_of_mem (submodule.zero F),
have hdp : 0 < d,
from lt_of_le_of_ne metric.inf_dist_nonneg $ λ heq, hx
((metric.mem_iff_inf_dist_zero_of_closed hFc hFn).2 heq.symm),
have hdlt : d < d / r,
from lt_div_of_mul_lt hlt ((mul_lt_iff_lt_one_right hdp).2 hr),
let ⟨y₀, hy₀F, hxy₀⟩ := metric.exists_dist_lt_of_inf_dist_lt hdlt hFn in
⟨x - y₀, λ y,
have hy₀y : (y₀ + y) ∈ F, from F.add hy₀F y.property,
le_of_lt $ calc
∥x - y₀ - y∥ = dist x (y₀ + y) : by { rw [sub_sub, dist_eq_norm] }
... ≥ d : metric.inf_dist_le_dist_of_mem hy₀y
... > _ : by { rw ←dist_eq_norm, exact (lt_div_iff' hlt).1 hxy₀ }⟩)
|
b107b8334e6933faeef2efccfadd9ed37ad90744 | 4fa161becb8ce7378a709f5992a594764699e268 | /src/algebra/ring.lean | 6b30d15a6bf11af76c389a27b0fcfdeb6b3287b2 | [
"Apache-2.0"
] | permissive | laughinggas/mathlib | e4aa4565ae34e46e834434284cb26bd9d67bc373 | 86dcd5cda7a5017c8b3c8876c89a510a19d49aad | refs/heads/master | 1,669,496,232,688 | 1,592,831,995,000 | 1,592,831,995,000 | 274,155,979 | 0 | 0 | Apache-2.0 | 1,592,835,190,000 | 1,592,835,189,000 | null | UTF-8 | Lean | false | false | 40,881 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Amelia Livingston, Yury Kudryashov,
Neil Strickland
-/
import algebra.group.hom
import algebra.group.units
import tactic.alias
import tactic.norm_cast
import tactic.split_ifs
/-!
# Properties and homomorphisms of semirings and rings
This file proves simple properties of semirings, rings and domains and their unit groups. It also
defines bundled homomorphisms of semirings and rings. As with monoid and groups, we use the same
structure `ring_hom a β`, a.k.a. `α →+* β`, for both homomorphism types.
The unbundled homomorphisms are defined in `deprecated/ring`. They are deprecated and the plan is to
slowly remove them from mathlib.
## Main definitions
ring_hom, nonzero, domain, integral_domain
## Notations
→+* for bundled ring homs (also use for semiring homs)
## Implementation notes
There's a coercion from bundled homs to fun, and the canonical
notation is to use the bundled hom as a function via this coercion.
There is no `semiring_hom` -- the idea is that `ring_hom` is used.
The constructor for a `ring_hom` between semirings needs a proof of `map_zero`, `map_one` and
`map_add` as well as `map_mul`; a separate constructor `ring_hom.mk'` will construct ring homs
between rings from monoid homs given only a proof that addition is preserved.
Throughout the section on `ring_hom` implicit `{}` brackets are often used instead
of type class `[]` brackets. This is done when the instances can be inferred because they are
implicit arguments to the type `ring_hom`. When they can be inferred from the type it is faster
to use this method than to use type class inference.
## Tags
`ring_hom`, `semiring_hom`, `semiring`, `comm_semiring`, `ring`, `comm_ring`, `domain`,
`integral_domain`, `nonzero`, `units`
-/
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x}
set_option default_priority 100 -- see Note [default priority]
set_option old_structure_cmd true
mk_simp_attribute field_simps "The simpset `field_simps` is used by the tactic `field_simp` to
reduce an expression in a field to an expression of the form `n / d` where `n` and `d` are
division-free."
@[protect_proj, ancestor has_mul has_add]
class distrib (α : Type u) extends has_mul α, has_add α :=
(left_distrib : ∀ a b c : α, a * (b + c) = (a * b) + (a * c))
(right_distrib : ∀ a b c : α, (a + b) * c = (a * c) + (b * c))
lemma left_distrib [distrib α] (a b c : α) : a * (b + c) = a * b + a * c :=
distrib.left_distrib a b c
alias left_distrib ← mul_add
lemma right_distrib [distrib α] (a b c : α) : (a + b) * c = a * c + b * c :=
distrib.right_distrib a b c
alias right_distrib ← add_mul
@[protect_proj, ancestor has_mul has_zero]
class mul_zero_class (α : Type u) extends has_mul α, has_zero α :=
(zero_mul : ∀ a : α, 0 * a = 0)
(mul_zero : ∀ a : α, a * 0 = 0)
@[ematch, simp] lemma zero_mul [mul_zero_class α] (a : α) : 0 * a = 0 :=
mul_zero_class.zero_mul a
@[ematch, simp] lemma mul_zero [mul_zero_class α] (a : α) : a * 0 = 0 :=
mul_zero_class.mul_zero a
/-- Predicate typeclass for expressing that a (semi)ring or similar algebraic structure
is nonzero. -/
@[protect_proj] class nonzero (α : Type u) [has_zero α] [has_one α] : Prop :=
(zero_ne_one : 0 ≠ (1:α))
@[simp]
lemma zero_ne_one [has_zero α] [has_one α] [nonzero α] : 0 ≠ (1:α) :=
nonzero.zero_ne_one
@[simp]
lemma one_ne_zero [has_zero α] [has_one α] [nonzero α] : (1:α) ≠ 0 :=
zero_ne_one.symm
/-!
### Semirings
-/
@[protect_proj, ancestor add_comm_monoid monoid distrib mul_zero_class]
class semiring (α : Type u) extends add_comm_monoid α, monoid α, distrib α, mul_zero_class α
section semiring
variables [semiring α]
lemma one_add_one_eq_two : 1 + 1 = (2 : α) :=
by unfold bit0
theorem two_mul (n : α) : 2 * n = n + n :=
eq.trans (right_distrib 1 1 n) (by simp)
lemma ne_zero_of_mul_ne_zero_right {a b : α} (h : a * b ≠ 0) : a ≠ 0 :=
assume : a = 0,
have a * b = 0, by rw [this, zero_mul],
h this
lemma ne_zero_of_mul_ne_zero_left {a b : α} (h : a * b ≠ 0) : b ≠ 0 :=
assume : b = 0,
have a * b = 0, by rw [this, mul_zero],
h this
lemma distrib_three_right (a b c d : α) : (a + b + c) * d = a * d + b * d + c * d :=
by simp [right_distrib]
theorem mul_two (n : α) : n * 2 = n + n :=
(left_distrib n 1 1).trans (by simp)
theorem bit0_eq_two_mul (n : α) : bit0 n = 2 * n :=
(two_mul _).symm
@[to_additive] lemma mul_ite {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) :
a * (if P then b else c) = if P then a * b else a * c :=
by split_ifs; refl
@[to_additive] lemma ite_mul {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) :
(if P then a else b) * c = if P then a * c else b * c :=
by split_ifs; refl
-- We make `mul_ite` and `ite_mul` simp lemmas,
-- but not `add_ite` or `ite_add`.
-- The problem we're trying to avoid is dealing with
-- summations of the form `∑ x in s, (f x + ite P 1 0)`,
-- in which `add_ite` followed by `sum_ite` would needlessly slice up
-- the `f x` terms according to whether `P` holds at `x`.
-- There doesn't appear to be a corresponding difficulty so far with
-- `mul_ite` and `ite_mul`.
attribute [simp] mul_ite ite_mul
@[simp] lemma mul_boole {α} [semiring α] (P : Prop) [decidable P] (a : α) :
a * (if P then 1 else 0) = if P then a else 0 :=
by simp
@[simp] lemma boole_mul {α} [semiring α] (P : Prop) [decidable P] (a : α) :
(if P then 1 else 0) * a = if P then a else 0 :=
by simp
variable (α)
/-- Either zero and one are nonequal in a semiring, or the semiring is the zero ring. -/
lemma zero_ne_one_or_forall_eq_0 : (0 : α) ≠ 1 ∨ (∀a:α, a = 0) :=
by haveI := classical.dec;
refine not_or_of_imp (λ h a, _); simpa using congr_arg ((*) a) h.symm
/-- If zero equals one in a semiring, the semiring is the zero ring. -/
lemma eq_zero_of_zero_eq_one (h : (0 : α) = 1) : (∀a:α, a = 0) :=
(zero_ne_one_or_forall_eq_0 α).neg_resolve_left h
/-- If zero equals one in a semiring, all elements of that semiring are equal. -/
theorem subsingleton_of_zero_eq_one (h : (0 : α) = 1) : subsingleton α :=
⟨λa b, by rw [eq_zero_of_zero_eq_one α h a, eq_zero_of_zero_eq_one α h b]⟩
end semiring
namespace add_monoid_hom
/-- Left multiplication by an element of a (semi)ring is an `add_monoid_hom` -/
def mul_left {R : Type*} [semiring R] (r : R) : R →+ R :=
{ to_fun := (*) r,
map_zero' := mul_zero r,
map_add' := mul_add r }
@[simp] lemma coe_mul_left {R : Type*} [semiring R] (r : R) : ⇑(mul_left r) = (*) r := rfl
/-- Right multiplication by an element of a (semi)ring is an `add_monoid_hom` -/
def mul_right {R : Type*} [semiring R] (r : R) : R →+ R :=
{ to_fun := λ a, a * r,
map_zero' := zero_mul r,
map_add' := λ _ _, add_mul _ _ r }
@[simp] lemma mul_right_apply {R : Type*} [semiring R] (a r : R) :
(mul_right r : R → R) a = a * r := rfl
end add_monoid_hom
/-- Bundled semiring homomorphisms; use this for bundled ring homomorphisms too. -/
structure ring_hom (α : Type*) (β : Type*) [semiring α] [semiring β]
extends monoid_hom α β, add_monoid_hom α β
infixr ` →+* `:25 := ring_hom
@[priority 1000]
instance {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} : has_coe_to_fun (α →+* β) :=
⟨_, ring_hom.to_fun⟩
@[priority 1000]
instance {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} : has_coe (α →+* β) (α →* β) :=
⟨ring_hom.to_monoid_hom⟩
@[priority 1000]
instance {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} : has_coe (α →+* β) (α →+ β) :=
⟨ring_hom.to_add_monoid_hom⟩
@[simp, norm_cast]
lemma coe_monoid_hom {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} (f : α →+* β) :
⇑(f : α →* β) = f := rfl
@[simp, norm_cast]
lemma coe_add_monoid_hom {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} (f : α →+* β) :
⇑(f : α →+ β) = f := rfl
namespace ring_hom
variables [rα : semiring α] [rβ : semiring β]
section
include rα rβ
@[simp] lemma coe_mk (f : α → β) (h₁ h₂ h₃ h₄) : ⇑(⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) = f := rfl
variables (f : α →+* β) {x y : α} {rα rβ}
theorem coe_inj ⦃f g : α →+* β⦄ (h : (f : α → β) = g) : f = g :=
by cases f; cases g; cases h; refl
@[ext] theorem ext ⦃f g : α →+* β⦄ (h : ∀ x, f x = g x) : f = g :=
coe_inj (funext h)
theorem ext_iff {f g : α →+* β} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
theorem coe_add_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →+ β)) :=
λ f g h, coe_inj $ show ((f : α →+ β) : α → β) = (g : α →+ β), from congr_arg coe_fn h
theorem coe_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →* β)) :=
λ f g h, coe_inj $ show ((f : α →* β) : α → β) = (g : α →* β), from congr_arg coe_fn h
/-- Ring homomorphisms map zero to zero. -/
@[simp] lemma map_zero (f : α →+* β) : f 0 = 0 := f.map_zero'
/-- Ring homomorphisms map one to one. -/
@[simp] lemma map_one (f : α →+* β) : f 1 = 1 := f.map_one'
/-- Ring homomorphisms preserve addition. -/
@[simp] lemma map_add (f : α →+* β) (a b : α) : f (a + b) = f a + f b := f.map_add' a b
/-- Ring homomorphisms preserve multiplication. -/
@[simp] lemma map_mul (f : α →+* β) (a b : α) : f (a * b) = f a * f b := f.map_mul' a b
end
/-- The identity ring homomorphism from a semiring to itself. -/
def id (α : Type*) [semiring α] : α →+* α :=
by refine {to_fun := id, ..}; intros; refl
include rα
@[simp] lemma id_apply (x : α) : ring_hom.id α x = x := rfl
variable {rγ : semiring γ}
include rβ rγ
/-- Composition of ring homomorphisms is a ring homomorphism. -/
def comp (hnp : β →+* γ) (hmn : α →+* β) : α →+* γ :=
{ to_fun := hnp ∘ hmn,
map_zero' := by simp,
map_one' := by simp,
map_add' := λ x y, by simp,
map_mul' := λ x y, by simp}
/-- Composition of semiring homomorphisms is associative. -/
lemma comp_assoc {δ} {rδ: semiring δ} (f : α →+* β) (g : β →+* γ) (h : γ →+* δ) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
@[simp] lemma coe_comp (hnp : β →+* γ) (hmn : α →+* β) : (hnp.comp hmn : α → γ) = hnp ∘ hmn := rfl
lemma comp_apply (hnp : β →+* γ) (hmn : α →+* β) (x : α) : (hnp.comp hmn : α → γ) x =
(hnp (hmn x)) := rfl
omit rγ
@[simp] lemma comp_id (f : α →+* β) : f.comp (id α) = f := ext $ λ x, rfl
@[simp] lemma id_comp (f : α →+* β) : (id β).comp f = f := ext $ λ x, rfl
omit rβ
instance : monoid (α →+* α) :=
{ one := id α,
mul := comp,
mul_one := comp_id,
one_mul := id_comp,
mul_assoc := λ f g h, comp_assoc _ _ _ }
lemma one_def : (1 : α →+* α) = id α := rfl
@[simp] lemma coe_one : ⇑(1 : α →+* α) = _root_.id := rfl
lemma mul_def (f g : α →+* α) : f * g = f.comp g := rfl
@[simp] lemma coe_mul (f g : α →+* α) : ⇑(f * g) = f ∘ g := rfl
include rβ rγ
lemma cancel_right {g₁ g₂ : β →+* γ} {f : α →+* β} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, ring_hom.ext $ (forall_iff_forall_surj hf).1 (ext_iff.1 h), λ h, h ▸ rfl⟩
lemma cancel_left {g : β →+* γ} {f₁ f₂ : α →+* β} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, ring_hom.ext $ λ x, hg $ by rw [← comp_apply, h, comp_apply], λ h, h ▸ rfl⟩
omit rα rβ rγ
end ring_hom
@[protect_proj, ancestor semiring comm_monoid]
class comm_semiring (α : Type u) extends semiring α, comm_monoid α
section comm_semiring
variables [comm_semiring α] [comm_semiring β] {a b c : α}
lemma add_mul_self_eq (a b : α) : (a + b) * (a + b) = a*a + 2*a*b + b*b :=
calc (a + b)*(a + b) = a*a + (1+1)*a*b + b*b : by simp [add_mul, mul_add, mul_comm, add_assoc]
... = a*a + 2*a*b + b*b : by rw one_add_one_eq_two
instance comm_semiring_has_dvd : has_dvd α :=
has_dvd.mk (λ a b, ∃ c, b = a * c)
-- TODO: this used to not have c explicit, but that seems to be important
-- for use with tactics, similar to exist.intro
theorem dvd.intro (c : α) (h : a * c = b) : a ∣ b :=
exists.intro c h^.symm
alias dvd.intro ← dvd_of_mul_right_eq
theorem dvd.intro_left (c : α) (h : c * a = b) : a ∣ b :=
dvd.intro _ (begin rewrite mul_comm at h, apply h end)
alias dvd.intro_left ← dvd_of_mul_left_eq
theorem exists_eq_mul_right_of_dvd (h : a ∣ b) : ∃ c, b = a * c := h
theorem dvd.elim {P : Prop} {a b : α} (H₁ : a ∣ b) (H₂ : ∀ c, b = a * c → P) : P :=
exists.elim H₁ H₂
theorem exists_eq_mul_left_of_dvd (h : a ∣ b) : ∃ c, b = c * a :=
dvd.elim h (assume c, assume H1 : b = a * c, exists.intro c (eq.trans H1 (mul_comm a c)))
theorem dvd.elim_left {P : Prop} (h₁ : a ∣ b) (h₂ : ∀ c, b = c * a → P) : P :=
exists.elim (exists_eq_mul_left_of_dvd h₁) (assume c, assume h₃ : b = c * a, h₂ c h₃)
@[refl, simp] theorem dvd_refl (a : α) : a ∣ a :=
dvd.intro 1 (by simp)
local attribute [simp] mul_assoc mul_comm mul_left_comm
@[trans] theorem dvd_trans (h₁ : a ∣ b) (h₂ : b ∣ c) : a ∣ c :=
match h₁, h₂ with
| ⟨d, (h₃ : b = a * d)⟩, ⟨e, (h₄ : c = b * e)⟩ :=
⟨d * e, show c = a * (d * e), by simp [h₃, h₄]⟩
end
alias dvd_trans ← dvd.trans
theorem eq_zero_of_zero_dvd (h : 0 ∣ a) : a = 0 :=
dvd.elim h (assume c, assume H' : a = 0 * c, eq.trans H' (zero_mul c))
/-- Given an element a of a commutative semiring, there exists another element whose product
with zero equals a iff a equals zero. -/
@[simp] lemma zero_dvd_iff : 0 ∣ a ↔ a = 0 :=
⟨eq_zero_of_zero_dvd, λ h, by rw h⟩
@[simp] theorem dvd_zero (a : α) : a ∣ 0 := dvd.intro 0 (by simp)
@[simp] theorem one_dvd (a : α) : 1 ∣ a := dvd.intro a (by simp)
@[simp] theorem dvd_mul_right (a b : α) : a ∣ a * b := dvd.intro b rfl
@[simp] theorem dvd_mul_left (a b : α) : a ∣ b * a := dvd.intro b (by simp)
theorem dvd_mul_of_dvd_left (h : a ∣ b) (c : α) : a ∣ b * c :=
dvd.elim h (λ d h', begin rw [h', mul_assoc], apply dvd_mul_right end)
theorem dvd_mul_of_dvd_right (h : a ∣ b) (c : α) : a ∣ c * b :=
begin rw mul_comm, exact dvd_mul_of_dvd_left h _ end
theorem mul_dvd_mul : ∀ {a b c d : α}, a ∣ b → c ∣ d → a * c ∣ b * d
| a ._ c ._ ⟨e, rfl⟩ ⟨f, rfl⟩ := ⟨e * f, by simp⟩
theorem mul_dvd_mul_left (a : α) {b c : α} (h : b ∣ c) : a * b ∣ a * c :=
mul_dvd_mul (dvd_refl a) h
theorem mul_dvd_mul_right (h : a ∣ b) (c : α) : a * c ∣ b * c :=
mul_dvd_mul h (dvd_refl c)
theorem dvd_add (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b + c :=
dvd.elim h₁ (λ d hd, dvd.elim h₂ (λ e he, dvd.intro (d + e) (by simp [left_distrib, hd, he])))
theorem dvd_of_mul_right_dvd (h : a * b ∣ c) : a ∣ c :=
dvd.elim h (begin intros d h₁, rw [h₁, mul_assoc], apply dvd_mul_right end)
theorem dvd_of_mul_left_dvd (h : a * b ∣ c) : b ∣ c :=
dvd.elim h (λ d ceq, dvd.intro (a * d) (by simp [ceq]))
lemma ring_hom.map_dvd (f : α →+* β) {a b : α} : a ∣ b → f a ∣ f b :=
λ ⟨z, hz⟩, ⟨f z, by rw [hz, f.map_mul]⟩
end comm_semiring
/-!
### Rings
-/
@[protect_proj, ancestor add_comm_group monoid distrib]
class ring (α : Type u) extends add_comm_group α, monoid α, distrib α
section ring
variables [ring α] {a b c d e : α}
lemma ring.mul_zero (a : α) : a * 0 = 0 :=
have a * 0 + 0 = a * 0 + a * 0, from calc
a * 0 + 0 = a * (0 + 0) : by simp
... = a * 0 + a * 0 : by rw left_distrib,
show a * 0 = 0, from (add_left_cancel this).symm
lemma ring.zero_mul (a : α) : 0 * a = 0 :=
have 0 * a + 0 = 0 * a + 0 * a, from calc
0 * a + 0 = (0 + 0) * a : by simp
... = 0 * a + 0 * a : by rewrite right_distrib,
show 0 * a = 0, from (add_left_cancel this).symm
instance ring.to_semiring : semiring α :=
{ mul_zero := ring.mul_zero, zero_mul := ring.zero_mul, ..‹ring α› }
/- The instance from `ring` to `semiring` happens often in linear algebra, for which all the basic
definitions are given in terms of semirings, but many applications use rings or fields. We increase
a little bit its priority above 100 to try it quickly, but remaining below the default 1000 so that
more specific instances are tried first. -/
attribute [instance, priority 200] ring.to_semiring
lemma neg_mul_eq_neg_mul (a b : α) : -(a * b) = -a * b :=
neg_eq_of_add_eq_zero
begin rw [← right_distrib, add_right_neg, zero_mul] end
lemma neg_mul_eq_mul_neg (a b : α) : -(a * b) = a * -b :=
neg_eq_of_add_eq_zero
begin rw [← left_distrib, add_right_neg, mul_zero] end
@[simp] lemma neg_mul_eq_neg_mul_symm (a b : α) : - a * b = - (a * b) :=
eq.symm (neg_mul_eq_neg_mul a b)
@[simp] lemma mul_neg_eq_neg_mul_symm (a b : α) : a * - b = - (a * b) :=
eq.symm (neg_mul_eq_mul_neg a b)
lemma neg_mul_neg (a b : α) : -a * -b = a * b :=
by simp
lemma neg_mul_comm (a b : α) : -a * b = a * -b :=
by simp
theorem neg_eq_neg_one_mul (a : α) : -a = -1 * a :=
by simp
lemma mul_sub_left_distrib (a b c : α) : a * (b - c) = a * b - a * c :=
calc
a * (b - c) = a * b + a * -c : left_distrib a b (-c)
... = a * b - a * c : by simp [sub_eq_add_neg]
alias mul_sub_left_distrib ← mul_sub
lemma mul_sub_right_distrib (a b c : α) : (a - b) * c = a * c - b * c :=
calc
(a - b) * c = a * c + -b * c : right_distrib a (-b) c
... = a * c - b * c : by simp [sub_eq_add_neg]
alias mul_sub_right_distrib ← sub_mul
/-- An element of a ring multiplied by the additive inverse of one is the element's additive
inverse. -/
lemma mul_neg_one (a : α) : a * -1 = -a := by simp
/-- The additive inverse of one multiplied by an element of a ring is the element's additive
inverse. -/
lemma neg_one_mul (a : α) : -1 * a = -a := by simp
/-- An iff statement following from right distributivity in rings and the definition
of subtraction. -/
theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d :=
calc
a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp [add_comm]
... ↔ a * e + c - b * e = d : iff.intro (λ h, begin rw h, simp end) (λ h,
begin rw ← h, simp end)
... ↔ (a - b) * e + c = d : begin simp [sub_mul, sub_add_eq_add_sub] end
/-- A simplification of one side of an equation exploiting right distributivity in rings
and the definition of subtraction. -/
theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d :=
assume h,
calc
(a - b) * e + c = (a * e + c) - b * e : begin simp [sub_mul, sub_add_eq_add_sub] end
... = d : begin rw h, simp [@add_sub_cancel α] end
/-- If the product of two elements of a ring is nonzero, both elements are nonzero. -/
theorem ne_zero_and_ne_zero_of_mul_ne_zero (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 :=
begin
split,
{ intro ha, apply h, simp [ha] },
{ intro hb, apply h, simp [hb] }
end
end ring
namespace units
variables [ring α] {a b : α}
/-- Each element of the group of units of a ring has an additive inverse. -/
instance : has_neg (units α) := ⟨λu, ⟨-↑u, -↑u⁻¹, by simp, by simp⟩ ⟩
/-- Representing an element of a ring's unit group as an element of the ring commutes with
mapping this element to its additive inverse. -/
@[simp] protected theorem coe_neg (u : units α) : (↑-u : α) = -u := rfl
/-- Mapping an element of a ring's unit group to its inverse commutes with mapping this element
to its additive inverse. -/
@[simp] protected theorem neg_inv (u : units α) : (-u)⁻¹ = -u⁻¹ := rfl
/-- An element of a ring's unit group equals the additive inverse of its additive inverse. -/
@[simp] protected theorem neg_neg (u : units α) : - -u = u :=
units.ext $ neg_neg _
/-- Multiplication of elements of a ring's unit group commutes with mapping the first
argument to its additive inverse. -/
@[simp] protected theorem neg_mul (u₁ u₂ : units α) : -u₁ * u₂ = -(u₁ * u₂) :=
units.ext $ neg_mul_eq_neg_mul_symm _ _
/-- Multiplication of elements of a ring's unit group commutes with mapping the second argument
to its additive inverse. -/
@[simp] protected theorem mul_neg (u₁ u₂ : units α) : u₁ * -u₂ = -(u₁ * u₂) :=
units.ext $ (neg_mul_eq_mul_neg _ _).symm
/-- Multiplication of the additive inverses of two elements of a ring's unit group equals
multiplication of the two original elements. -/
@[simp] protected theorem neg_mul_neg (u₁ u₂ : units α) : -u₁ * -u₂ = u₁ * u₂ := by simp
/-- The additive inverse of an element of a ring's unit group equals the additive inverse of
one times the original element. -/
protected theorem neg_eq_neg_one_mul (u : units α) : -u = -1 * u := by simp
end units
namespace ring_hom
/-- Ring homomorphisms preserve additive inverse. -/
@[simp] theorem map_neg {α β} [ring α] [ring β] (f : α →+* β) (x : α) : f (-x) = -(f x) :=
(f : α →+ β).map_neg x
/-- Ring homomorphisms preserve subtraction. -/
@[simp] theorem map_sub {α β} [ring α] [ring β] (f : α →+* β) (x y : α) :
f (x - y) = (f x) - (f y) := (f : α →+ β).map_sub x y
/-- A ring homomorphism is injective iff its kernel is trivial. -/
theorem injective_iff {α β} [ring α] [ring β] (f : α →+* β) :
function.injective f ↔ (∀ a, f a = 0 → a = 0) :=
(f : α →+ β).injective_iff
/-- Makes a ring homomorphism from a monoid homomorphism of rings which preserves addition. -/
def mk' {γ} [semiring α] [ring γ] (f : α →* γ) (map_add : ∀ a b : α, f (a + b) = f a + f b) :
α →+* γ :=
{ to_fun := f,
.. add_monoid_hom.mk' f map_add, .. f }
end ring_hom
@[protect_proj, ancestor ring comm_semigroup]
class comm_ring (α : Type u) extends ring α, comm_semigroup α
instance comm_ring.to_comm_semiring [s : comm_ring α] : comm_semiring α :=
{ mul_zero := mul_zero, zero_mul := zero_mul, ..s }
section comm_ring
variables [comm_ring α] {a b c : α}
local attribute [simp] add_assoc add_comm add_left_comm mul_comm
lemma mul_self_sub_mul_self_eq (a b : α) : a * a - b * b = (a + b) * (a - b) :=
begin simp [right_distrib, left_distrib, sub_eq_add_neg] end
lemma mul_self_sub_one_eq (a : α) : a * a - 1 = (a + 1) * (a - 1) :=
begin simp [right_distrib, left_distrib, sub_eq_add_neg], rw [add_left_comm, add_comm (-a), add_left_comm a], simp end
theorem dvd_neg_of_dvd (h : a ∣ b) : (a ∣ -b) :=
dvd.elim h
(assume c, assume : b = a * c,
dvd.intro (-c) (by simp [this]))
theorem dvd_of_dvd_neg (h : a ∣ -b) : (a ∣ b) :=
let t := dvd_neg_of_dvd h in by rwa neg_neg at t
theorem dvd_neg_iff_dvd (a b : α) : (a ∣ -b) ↔ (a ∣ b) :=
⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩
theorem neg_dvd_of_dvd (h : a ∣ b) : -a ∣ b :=
dvd.elim h
(assume c, assume : b = a * c,
dvd.intro (-c) (by simp [this]))
theorem dvd_of_neg_dvd (h : -a ∣ b) : a ∣ b :=
let t := neg_dvd_of_dvd h in by rwa neg_neg at t
theorem neg_dvd_iff_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) :=
⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩
theorem dvd_sub (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b - c :=
dvd_add h₁ (dvd_neg_of_dvd h₂)
theorem dvd_add_iff_left (h : a ∣ c) : a ∣ b ↔ a ∣ b + c :=
⟨λh₂, dvd_add h₂ h, λH, by have t := dvd_sub H h; rwa add_sub_cancel at t⟩
theorem dvd_add_iff_right (h : a ∣ b) : a ∣ c ↔ a ∣ b + c :=
by rw add_comm; exact dvd_add_iff_left h
/-- Representation of a difference of two squares in a commutative ring as a product. -/
theorem mul_self_sub_mul_self (a b : α) : a * a - b * b = (a + b) * (a - b) :=
by rw [add_mul, mul_sub, mul_sub, mul_comm a b, sub_add_sub_cancel]
/-- An element a of a commutative ring divides the additive inverse of an element b iff a
divides b. -/
@[simp] lemma dvd_neg (a b : α) : (a ∣ -b) ↔ (a ∣ b) :=
⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩
/-- The additive inverse of an element a of a commutative ring divides another element b iff a
divides b. -/
@[simp] lemma neg_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) :=
⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩
/-- If an element a divides another element c in a commutative ring, a divides the sum of another
element b with c iff a divides b. -/
theorem dvd_add_left (h : a ∣ c) : a ∣ b + c ↔ a ∣ b :=
(dvd_add_iff_left h).symm
/-- If an element a divides another element b in a commutative ring, a divides the sum of b and
another element c iff a divides c. -/
theorem dvd_add_right (h : a ∣ b) : a ∣ b + c ↔ a ∣ c :=
(dvd_add_iff_right h).symm
/-- An element a divides the sum a + b if and only if a divides b.-/
@[simp] lemma dvd_add_self_left {a b : α} : a ∣ a + b ↔ a ∣ b :=
dvd_add_right (dvd_refl a)
/-- An element a divides the sum b + a if and only if a divides b.-/
@[simp] lemma dvd_add_self_right {a b : α} : a ∣ b + a ↔ a ∣ b :=
dvd_add_left (dvd_refl a)
/-- Vieta's formula for a quadratic equation, relating the coefficients of the polynomial with
its roots. This particular version states that if we have a root `x` of a monic quadratic
polynomial, then there is another root `y` such that `x + y` is negative the `a_1` coefficient
and `x * y` is the `a_0` coefficient. -/
lemma Vieta_formula_quadratic {b c x : α} (h : x * x - b * x + c = 0) :
∃ y : α, y * y - b * y + c = 0 ∧ x + y = b ∧ x * y = c :=
begin
have : c = -(x * x - b * x) := (neg_eq_of_add_eq_zero h).symm,
have : c = x * (b - x), by subst this; simp [mul_sub, mul_comm],
refine ⟨b - x, _, by simp, by rw this⟩,
rw [this, sub_add, ← sub_mul, sub_self]
end
lemma dvd_mul_sub_mul {α : Type*} [comm_ring α]
{k a b x y : α} (hab : k ∣ a - b) (hxy : k ∣ x - y) :
k ∣ a * x - b * y :=
begin
convert dvd_add (dvd_mul_of_dvd_right hxy a) (dvd_mul_of_dvd_left hab y),
rw [mul_sub_left_distrib, mul_sub_right_distrib],
simp only [sub_eq_add_neg, add_assoc, neg_add_cancel_left],
end
lemma dvd_iff_dvd_of_dvd_sub {R : Type*} [comm_ring R] {a b c : R}
(h : a ∣ (b - c)) : (a ∣ b ↔ a ∣ c) :=
begin
split,
{ intro h',
convert dvd_sub h' h,
exact eq.symm (sub_sub_self b c) },
{ intro h',
convert dvd_add h h',
exact eq_add_of_sub_eq rfl }
end
end comm_ring
lemma succ_ne_self [ring α] [nonzero α] (a : α) : a + 1 ≠ a :=
λ h, one_ne_zero ((add_right_inj a).mp (by simp [h]))
lemma pred_ne_self [ring α] [nonzero α] (a : α) : a - 1 ≠ a :=
λ h, one_ne_zero (neg_inj ((add_right_inj a).mp (by { convert h, simp })))
/-- An element of the unit group of a nonzero semiring represented as an element
of the semiring is nonzero. -/
lemma units.coe_ne_zero [semiring α] [nonzero α] (u : units α) : (u : α) ≠ 0 :=
λ h : u.1 = 0, by simpa [h, zero_ne_one] using u.3
/-- Proves that a semiring that contains at least two distinct elements is nonzero. -/
theorem nonzero.of_ne [semiring α] {x y : α} (h : x ≠ y) : nonzero α :=
{ zero_ne_one := λ h01, h $ by rw [← one_mul x, ← one_mul y, ← h01, zero_mul, zero_mul] }
@[protect_proj] class no_zero_divisors (α : Type u) [has_mul α] [has_zero α] : Prop :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0)
lemma eq_zero_or_eq_zero_of_mul_eq_zero [has_mul α] [has_zero α] [no_zero_divisors α]
{a b : α} (h : a * b = 0) : a = 0 ∨ b = 0 :=
no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero a b h
lemma eq_zero_of_mul_self_eq_zero [has_mul α] [has_zero α] [no_zero_divisors α]
{a : α} (h : a * a = 0) : a = 0 :=
or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) (assume h', h') (assume h', h')
/-- A domain is a ring with no zero divisors, i.e. satisfying
the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, a domain
is an integral domain without assuming commutativity of multiplication. -/
@[protect_proj] class domain (α : Type u) extends ring α :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0)
(zero_ne_one : (0 : α) ≠ 1)
section domain
variable [domain α]
instance domain.to_no_zero_divisors : no_zero_divisors α :=
⟨domain.eq_zero_or_eq_zero_of_mul_eq_zero⟩
instance domain.to_nonzero : nonzero α :=
⟨domain.zero_ne_one⟩
/-- Simplification theorems for the definition of a domain. -/
@[simp] theorem mul_eq_zero {a b : α} : a * b = 0 ↔ a = 0 ∨ b = 0 :=
⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo,
or.elim o (λh, by rw h; apply zero_mul) (λh, by rw h; apply mul_zero)⟩
@[simp] theorem zero_eq_mul {a b : α} : 0 = a * b ↔ a = 0 ∨ b = 0 :=
by rw [eq_comm, mul_eq_zero]
lemma mul_self_eq_zero {α} [domain α] {x : α} : x * x = 0 ↔ x = 0 := by simp
lemma zero_eq_mul_self {α} [domain α] {x : α} : 0 = x * x ↔ x = 0 := by simp
/-- The product of two nonzero elements of a domain is nonzero. -/
theorem mul_ne_zero' {a b : α} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 :=
λ h, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) h₁ h₂
/-- Right multiplication by a nonzero element in a domain is injective. -/
theorem domain.mul_left_inj {a b c : α} (ha : a ≠ 0) : b * a = c * a ↔ b = c :=
by rw [← sub_eq_zero, ← mul_sub_right_distrib, mul_eq_zero];
simp [ha]; exact sub_eq_zero
/-- Left multiplication by a nonzero element in a domain is injective. -/
theorem domain.mul_right_inj {a b c : α} (ha : a ≠ 0) : a * b = a * c ↔ b = c :=
by rw [← sub_eq_zero, ← mul_sub_left_distrib, mul_eq_zero];
simp [ha]; exact sub_eq_zero
/-- An element of a domain fixed by right multiplication by an element other than one must
be zero. -/
theorem eq_zero_of_mul_eq_self_right' {a b : α} (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 :=
by apply (mul_eq_zero.1 _).resolve_right (sub_ne_zero.2 h₁);
rw [mul_sub_left_distrib, mul_one, sub_eq_zero, h₂]
/-- An element of a domain fixed by left multiplication by an element other than one must
be zero. -/
theorem eq_zero_of_mul_eq_self_left' {a b : α} (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 :=
by apply (mul_eq_zero.1 _).resolve_left (sub_ne_zero.2 h₁);
rw [mul_sub_right_distrib, one_mul, sub_eq_zero, h₂]
/-- For elements `a`, `b` of a domain, if `a*b` is nonzero, so is `b*a`. -/
theorem mul_ne_zero_comm' {a b : α} (h : a * b ≠ 0) : b * a ≠ 0 :=
mul_ne_zero' (ne_zero_of_mul_ne_zero_left h) (ne_zero_of_mul_ne_zero_right h)
end domain
/- integral domains -/
@[protect_proj, ancestor comm_ring domain]
class integral_domain (α : Type u) extends comm_ring α, domain α
section integral_domain
variables [integral_domain α] {a b c d e : α}
lemma mul_eq_zero_iff_eq_zero_or_eq_zero : a * b = 0 ↔ a = 0 ∨ b = 0 :=
⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo,
or.elim o (λh, by rw h; apply zero_mul) (λh, by rw h; apply mul_zero)⟩
lemma mul_ne_zero (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 :=
λ h, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) (assume h₃, h₁ h₃) (assume h₄, h₂ h₄)
lemma eq_of_mul_eq_mul_right (ha : a ≠ 0) (h : b * a = c * a) : b = c :=
have b * a - c * a = 0, from sub_eq_zero_of_eq h,
have (b - c) * a = 0, by rw [mul_sub_right_distrib, this],
have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right ha,
eq_of_sub_eq_zero this
lemma eq_of_mul_eq_mul_left (ha : a ≠ 0) (h : a * b = a * c) : b = c :=
have a * b - a * c = 0, from sub_eq_zero_of_eq h,
have a * (b - c) = 0, by rw [mul_sub_left_distrib, this],
have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_left ha,
eq_of_sub_eq_zero this
lemma eq_zero_of_mul_eq_self_right (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 :=
have hb : b - 1 ≠ 0, from
assume : b - 1 = 0,
have b = 0 + 1, from eq_add_of_sub_eq this,
have b = 1, by rwa zero_add at this,
h₁ this,
have a * b - a = 0, by simp [h₂],
have a * (b - 1) = 0, by rwa [mul_sub_left_distrib, mul_one],
show a = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right hb
lemma eq_zero_of_mul_eq_self_left (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 :=
eq_zero_of_mul_eq_self_right h₁ (by rwa mul_comm at h₂)
lemma mul_self_eq_mul_self_iff (a b : α) : a * a = b * b ↔ a = b ∨ a = -b :=
iff.intro
(assume : a * a = b * b,
have (a - b) * (a + b) = 0,
by rewrite [mul_comm, ← mul_self_sub_mul_self_eq, this, sub_self],
have a - b = 0 ∨ a + b = 0, from eq_zero_or_eq_zero_of_mul_eq_zero this,
or.elim this
(assume : a - b = 0, or.inl (eq_of_sub_eq_zero this))
(assume : a + b = 0, or.inr (eq_neg_of_add_eq_zero this)))
(assume : a = b ∨ a = -b, or.elim this
(assume : a = b, by rewrite this)
(assume : a = -b, by rewrite [this, neg_mul_neg]))
lemma mul_self_eq_one_iff (a : α) : a * a = 1 ↔ a = 1 ∨ a = -1 :=
have a * a = 1 * 1 ↔ a = 1 ∨ a = -1, from mul_self_eq_mul_self_iff a 1,
by rwa mul_one at this
/-- Right multiplcation by a nonzero element of an integral domain is injective. -/
theorem eq_of_mul_eq_mul_right_of_ne_zero (ha : a ≠ 0) (h : b * a = c * a) : b = c :=
have b * a - c * a = 0, by simp [h],
have (b - c) * a = 0, by rw [mul_sub_right_distrib, this],
have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right ha,
eq_of_sub_eq_zero this
/-- Left multiplication by a nonzero element of an integral domain is injective. -/
theorem eq_of_mul_eq_mul_left_of_ne_zero (ha : a ≠ 0) (h : a * b = a * c) : b = c :=
have a * b - a * c = 0, by simp [h],
have a * (b - c) = 0, by rw [mul_sub_left_distrib, this],
have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_left ha,
eq_of_sub_eq_zero this
/-- Given two elements b, c of an integral domain and a nonzero element a, a*b divides a*c iff
b divides c. -/
theorem mul_dvd_mul_iff_left (ha : a ≠ 0) : a * b ∣ a * c ↔ b ∣ c :=
exists_congr $ λ d, by rw [mul_assoc, domain.mul_right_inj ha]
/-- Given two elements a, b of an integral domain and a nonzero element c, a*c divides b*c iff
a divides b. -/
theorem mul_dvd_mul_iff_right (hc : c ≠ 0) : a * c ∣ b * c ↔ a ∣ b :=
exists_congr $ λ d, by rw [mul_right_comm, domain.mul_left_inj hc]
/-- In the unit group of an integral domain, a unit is its own inverse iff the unit is one or
one's additive inverse. -/
lemma units.inv_eq_self_iff (u : units α) : u⁻¹ = u ↔ u = 1 ∨ u = -1 :=
by conv {to_lhs, rw [inv_eq_iff_mul_eq_one, ← mul_one (1 : units α), units.ext_iff, units.coe_mul,
units.coe_mul, mul_self_eq_mul_self_iff, ← units.ext_iff, ← units.coe_neg, ← units.ext_iff] }
end integral_domain
/- units in various rings -/
namespace units
section semiring
variables [semiring α]
@[simp] theorem mul_left_eq_zero_iff_eq_zero
{r : α} (u : units α) : r * u = 0 ↔ r = 0 :=
⟨λ h, (mul_left_inj u).1 $ (zero_mul (u : α)).symm ▸ h,
λ h, h.symm ▸ zero_mul (u : α)⟩
@[simp] theorem mul_right_eq_zero_iff_eq_zero
{r : α} (u : units α) : (u : α) * r = 0 ↔ r = 0 :=
⟨λ h, (mul_right_inj u).1 $ (mul_zero (u : α)).symm ▸ h,
λ h, h.symm ▸ mul_zero (u : α)⟩
end semiring
section comm_semiring
variables [comm_semiring α] (a b : α) (u : units α)
/-- Elements of the unit group of a commutative semiring represented as elements of the semiring
divide any element of the semiring. -/
@[simp] lemma coe_dvd : ↑u ∣ a := ⟨↑u⁻¹ * a, by simp⟩
/-- In a commutative semiring, an element a divides an element b iff a divides all
associates of b. -/
@[simp] lemma dvd_coe_mul : a ∣ b * u ↔ a ∣ b :=
iff.intro
(assume ⟨c, eq⟩, ⟨c * ↑u⁻¹, by rw [← mul_assoc, ← eq, units.mul_inv_cancel_right]⟩)
(assume ⟨c, eq⟩, eq.symm ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _)
/-- An element of a commutative semiring divides a unit iff the element divides one. -/
@[simp] lemma dvd_coe : a ∣ ↑u ↔ a ∣ 1 :=
suffices a ∣ 1 * ↑u ↔ a ∣ 1, by simpa,
dvd_coe_mul _ _ _
/-- In a commutative semiring, an element a divides an element b iff all associates of a divide b.-/
@[simp] lemma coe_mul_dvd : a * u ∣ b ↔ a ∣ b :=
iff.intro
(assume ⟨c, eq⟩, ⟨c * ↑u, eq.symm ▸ by ac_refl⟩)
(assume h, suffices a * ↑u ∣ b * 1, by simpa, mul_dvd_mul h (coe_dvd _ _))
end comm_semiring
end units
namespace is_unit
section semiring
variables [semiring α]
theorem mul_left_eq_zero_iff_eq_zero {r u : α}
(hu : is_unit u) : r * u = 0 ↔ r = 0 :=
by cases hu with u hu; exact hu ▸ units.mul_left_eq_zero_iff_eq_zero u
theorem mul_right_eq_zero_iff_eq_zero {r u : α}
(hu : is_unit u) : u * r = 0 ↔ r = 0 :=
by cases hu with u hu; exact hu ▸ units.mul_right_eq_zero_iff_eq_zero u
end semiring
end is_unit
/-- A predicate to express that a ring is an integral domain.
This is mainly useful because such a predicate does not contain data,
and can therefore be easily transported along ring isomorphisms. -/
structure is_integral_domain (R : Type u) [ring R] : Prop :=
(mul_comm : ∀ (x y : R), x * y = y * x)
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ x y : R, x * y = 0 → x = 0 ∨ y = 0)
(zero_ne_one : (0 : R) ≠ 1)
/-- Every integral domain satisfies the predicate for integral domains. -/
lemma integral_domain.to_is_integral_domain (R : Type u) [integral_domain R] :
is_integral_domain R :=
{ .. (‹_› : integral_domain R) }
/-- If a ring satisfies the predicate for integral domains,
then it can be endowed with an `integral_domain` instance
whose data is definitionally equal to the existing data. -/
def is_integral_domain.to_integral_domain (R : Type u) [ring R] (h : is_integral_domain R) :
integral_domain R :=
{ .. (‹_› : ring R), .. (‹_› : is_integral_domain R) }
namespace semiconj_by
@[simp] lemma add_right [distrib R] {a x y x' y' : R}
(h : semiconj_by a x y) (h' : semiconj_by a x' y') :
semiconj_by a (x + x') (y + y') :=
by simp only [semiconj_by, left_distrib, right_distrib, h.eq, h'.eq]
@[simp] lemma add_left [distrib R] {a b x y : R}
(ha : semiconj_by a x y) (hb : semiconj_by b x y) :
semiconj_by (a + b) x y :=
by simp only [semiconj_by, left_distrib, right_distrib, ha.eq, hb.eq]
@[simp] lemma zero_right [mul_zero_class R] (a : R) :
semiconj_by a 0 0 :=
by simp only [semiconj_by, mul_zero, zero_mul]
@[simp] lemma zero_left [mul_zero_class R] (x y : R) :
semiconj_by 0 x y :=
by simp only [semiconj_by, mul_zero, zero_mul]
variables [ring R] {a b x y x' y' : R}
lemma neg_right (h : semiconj_by a x y) : semiconj_by a (-x) (-y) :=
by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm]
@[simp] lemma neg_right_iff : semiconj_by a (-x) (-y) ↔ semiconj_by a x y :=
⟨λ h, neg_neg x ▸ neg_neg y ▸ h.neg_right, semiconj_by.neg_right⟩
lemma neg_left (h : semiconj_by a x y) : semiconj_by (-a) x y :=
by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm]
@[simp] lemma neg_left_iff : semiconj_by (-a) x y ↔ semiconj_by a x y :=
⟨λ h, neg_neg a ▸ h.neg_left, semiconj_by.neg_left⟩
@[simp] lemma neg_one_right (a : R) : semiconj_by a (-1) (-1) :=
(one_right a).neg_right
@[simp] lemma neg_one_left (x : R) : semiconj_by (-1) x x :=
(semiconj_by.one_left x).neg_left
@[simp] lemma sub_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') :
semiconj_by a (x - x') (y - y') :=
h.add_right h'.neg_right
@[simp] lemma sub_left (ha : semiconj_by a x y) (hb : semiconj_by b x y) :
semiconj_by (a - b) x y :=
ha.add_left hb.neg_left
end semiconj_by
namespace commute
@[simp] theorem add_right [distrib R] {a b c : R} :
commute a b → commute a c → commute a (b + c) :=
semiconj_by.add_right
@[simp] theorem add_left [distrib R] {a b c : R} :
commute a c → commute b c → commute (a + b) c :=
semiconj_by.add_left
@[simp] theorem zero_right [mul_zero_class R] (a : R) :commute a 0 := semiconj_by.zero_right a
@[simp] theorem zero_left [mul_zero_class R] (a : R) : commute 0 a := semiconj_by.zero_left a a
variables [ring R] {a b c : R}
theorem neg_right : commute a b → commute a (- b) := semiconj_by.neg_right
@[simp] theorem neg_right_iff : commute a (-b) ↔ commute a b := semiconj_by.neg_right_iff
theorem neg_left : commute a b → commute (- a) b := semiconj_by.neg_left
@[simp] theorem neg_left_iff : commute (-a) b ↔ commute a b := semiconj_by.neg_left_iff
@[simp] theorem neg_one_right (a : R) : commute a (-1) := semiconj_by.neg_one_right a
@[simp] theorem neg_one_left (a : R): commute (-1) a := semiconj_by.neg_one_left a
@[simp] theorem sub_right : commute a b → commute a c → commute a (b - c) := semiconj_by.sub_right
@[simp] theorem sub_left : commute a c → commute b c → commute (a - b) c := semiconj_by.sub_left
end commute
|
d6628ea3122d469a85a69de45cc8df4d036b55a3 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/convex/basic.lean | 6825f1007898157d2c56661b615592c1352ca7d6 | [
"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 | 21,880 | lean | /-
Copyright (c) 2019 Alexander Bentkamp. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Alexander Bentkamp, Yury Kudriashov, Yaël Dillies
-/
import algebra.order.module
import analysis.convex.star
import linear_algebra.affine_space.affine_subspace
/-!
# Convex sets and functions in vector spaces
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In a 𝕜-vector space, we define the following objects and properties.
* `convex 𝕜 s`: A set `s` is convex if for any two points `x y ∈ s` it includes `segment 𝕜 x y`.
* `std_simplex 𝕜 ι`: The standard simplex in `ι → 𝕜` (currently requires `fintype ι`). It is the
intersection of the positive quadrant with the hyperplane `s.sum = 1`.
We also provide various equivalent versions of the definitions above, prove that some specific sets
are convex.
## TODO
Generalize all this file to affine spaces.
-/
variables {𝕜 E F β : Type*}
open linear_map set
open_locale big_operators classical convex pointwise
/-! ### Convexity of sets -/
section ordered_semiring
variables [ordered_semiring 𝕜]
section add_comm_monoid
variables [add_comm_monoid E] [add_comm_monoid F]
section has_smul
variables (𝕜) [has_smul 𝕜 E] [has_smul 𝕜 F] (s : set E) {x : E}
/-- Convexity of sets. -/
def convex : Prop := ∀ ⦃x : E⦄, x ∈ s → star_convex 𝕜 x s
variables {𝕜 s}
lemma convex.star_convex (hs : convex 𝕜 s) (hx : x ∈ s) : star_convex 𝕜 x s := hs hx
lemma convex_iff_segment_subset : convex 𝕜 s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → [x -[𝕜] y] ⊆ s :=
forall₂_congr $ λ x hx, star_convex_iff_segment_subset
lemma convex.segment_subset (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) :
[x -[𝕜] y] ⊆ s :=
convex_iff_segment_subset.1 h hx hy
lemma convex.open_segment_subset (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) :
open_segment 𝕜 x y ⊆ s :=
(open_segment_subset_segment 𝕜 x y).trans (h.segment_subset hx hy)
/-- Alternative definition of set convexity, in terms of pointwise set operations. -/
lemma convex_iff_pointwise_add_subset :
convex 𝕜 s ↔ ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s :=
iff.intro
begin
rintro hA a b ha hb hab w ⟨au, bv, ⟨u, hu, rfl⟩, ⟨v, hv, rfl⟩, rfl⟩,
exact hA hu hv ha hb hab
end
(λ h x hx y hy a b ha hb hab,
(h ha hb hab) (set.add_mem_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩))
alias convex_iff_pointwise_add_subset ↔ convex.set_combo_subset _
lemma convex_empty : convex 𝕜 (∅ : set E) := λ x, false.elim
lemma convex_univ : convex 𝕜 (set.univ : set E) := λ _ _, star_convex_univ _
lemma convex.inter {t : set E} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s ∩ t) :=
λ x hx, (hs hx.1).inter (ht hx.2)
lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex 𝕜 s) : convex 𝕜 (⋂₀ S) :=
λ x hx, star_convex_sInter $ λ s hs, h _ hs $ hx _ hs
lemma convex_Inter {ι : Sort*} {s : ι → set E} (h : ∀ i, convex 𝕜 (s i)) : convex 𝕜 (⋂ i, s i) :=
(sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h
lemma convex_Inter₂ {ι : Sort*} {κ : ι → Sort*} {s : Π i, κ i → set E}
(h : ∀ i j, convex 𝕜 (s i j)) :
convex 𝕜 (⋂ i j, s i j) :=
convex_Inter $ λ i, convex_Inter $ h i
lemma convex.prod {s : set E} {t : set F} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s ×ˢ t) :=
λ x hx, (hs hx.1).prod (ht hx.2)
lemma convex_pi {ι : Type*} {E : ι → Type*} [Π i, add_comm_monoid (E i)]
[Π i, has_smul 𝕜 (E i)] {s : set ι} {t : Π i, set (E i)} (ht : ∀ ⦃i⦄, i ∈ s → convex 𝕜 (t i)) :
convex 𝕜 (s.pi t) :=
λ x hx, star_convex_pi $ λ i hi, ht hi $ hx _ hi
lemma directed.convex_Union {ι : Sort*} {s : ι → set E} (hdir : directed (⊆) s)
(hc : ∀ ⦃i : ι⦄, convex 𝕜 (s i)) :
convex 𝕜 (⋃ i, s i) :=
begin
rintro x hx y hy a b ha hb hab,
rw mem_Union at ⊢ hx hy,
obtain ⟨i, hx⟩ := hx,
obtain ⟨j, hy⟩ := hy,
obtain ⟨k, hik, hjk⟩ := hdir i j,
exact ⟨k, hc (hik hx) (hjk hy) ha hb hab⟩,
end
lemma directed_on.convex_sUnion {c : set (set E)} (hdir : directed_on (⊆) c)
(hc : ∀ ⦃A : set E⦄, A ∈ c → convex 𝕜 A) :
convex 𝕜 (⋃₀c) :=
begin
rw sUnion_eq_Union,
exact (directed_on_iff_directed.1 hdir).convex_Union (λ A, hc A.2),
end
end has_smul
section module
variables [module 𝕜 E] [module 𝕜 F] {s : set E} {x : E}
lemma convex_iff_open_segment_subset :
convex 𝕜 s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → open_segment 𝕜 x y ⊆ s :=
forall₂_congr $ λ x, star_convex_iff_open_segment_subset
lemma convex_iff_forall_pos :
convex 𝕜 s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1
→ a • x + b • y ∈ s :=
forall₂_congr $ λ x, star_convex_iff_forall_pos
lemma convex_iff_pairwise_pos :
convex 𝕜 s ↔ s.pairwise (λ x y, ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s) :=
begin
refine convex_iff_forall_pos.trans ⟨λ h x hx y hy _, h hx hy, _⟩,
intros h x hx y hy a b ha hb hab,
obtain rfl | hxy := eq_or_ne x y,
{ rwa convex.combo_self hab },
{ exact h hx hy hxy ha hb hab },
end
lemma convex.star_convex_iff (hs : convex 𝕜 s) (h : s.nonempty) : star_convex 𝕜 x s ↔ x ∈ s :=
⟨λ hxs, hxs.mem h, hs.star_convex⟩
protected lemma set.subsingleton.convex {s : set E} (h : s.subsingleton) : convex 𝕜 s :=
convex_iff_pairwise_pos.mpr (h.pairwise _)
lemma convex_singleton (c : E) : convex 𝕜 ({c} : set E) :=
subsingleton_singleton.convex
lemma convex_segment (x y : E) : convex 𝕜 [x -[𝕜] y] :=
begin
rintro p ⟨ap, bp, hap, hbp, habp, rfl⟩ q ⟨aq, bq, haq, hbq, habq, rfl⟩ a b ha hb hab,
refine ⟨a * ap + b * aq, a * bp + b * bq,
add_nonneg (mul_nonneg ha hap) (mul_nonneg hb haq),
add_nonneg (mul_nonneg ha hbp) (mul_nonneg hb hbq), _, _⟩,
{ rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] },
{ simp_rw [add_smul, mul_smul, smul_add],
exact add_add_add_comm _ _ _ _ }
end
lemma convex.linear_image (hs : convex 𝕜 s) (f : E →ₗ[𝕜] F) : convex 𝕜 (f '' s) :=
begin
intros x hx y hy a b ha hb hab,
obtain ⟨x', hx', rfl⟩ := mem_image_iff_bex.1 hx,
obtain ⟨y', hy', rfl⟩ := mem_image_iff_bex.1 hy,
exact ⟨a • x' + b • y', hs hx' hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]⟩,
end
lemma convex.is_linear_image (hs : convex 𝕜 s) {f : E → F} (hf : is_linear_map 𝕜 f) :
convex 𝕜 (f '' s) :=
hs.linear_image $ hf.mk' f
lemma convex.linear_preimage {s : set F} (hs : convex 𝕜 s) (f : E →ₗ[𝕜] F) :
convex 𝕜 (f ⁻¹' s) :=
begin
intros x hx y hy a b ha hb hab,
rw [mem_preimage, f.map_add, f.map_smul, f.map_smul],
exact hs hx hy ha hb hab,
end
lemma convex.is_linear_preimage {s : set F} (hs : convex 𝕜 s) {f : E → F} (hf : is_linear_map 𝕜 f) :
convex 𝕜 (f ⁻¹' s) :=
hs.linear_preimage $ hf.mk' f
lemma convex.add {t : set E} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s + t) :=
by { rw ← add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add }
lemma convex.vadd (hs : convex 𝕜 s) (z : E) : convex 𝕜 (z +ᵥ s) :=
by { simp_rw [←image_vadd, vadd_eq_add, ←singleton_add], exact (convex_singleton _).add hs }
lemma convex.translate (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, z + x) '' s) := hs.vadd _
/-- The translation of a convex set is also convex. -/
lemma convex.translate_preimage_right (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, z + x) ⁻¹' s) :=
begin
intros x hx y hy a b ha hb hab,
have h := hs hx hy ha hb hab,
rwa [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul] at h,
end
/-- The translation of a convex set is also convex. -/
lemma convex.translate_preimage_left (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, x + z) ⁻¹' s) :=
by simpa only [add_comm] using hs.translate_preimage_right z
section ordered_add_comm_monoid
variables [ordered_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β]
lemma convex_Iic (r : β) : convex 𝕜 (Iic r) :=
λ x hx y hy a b ha hb hab,
calc
a • x + b • y
≤ a • r + b • r
: add_le_add (smul_le_smul_of_nonneg hx ha) (smul_le_smul_of_nonneg hy hb)
... = r : convex.combo_self hab _
lemma convex_Ici (r : β) : convex 𝕜 (Ici r) := @convex_Iic 𝕜 βᵒᵈ _ _ _ _ r
lemma convex_Icc (r s : β) : convex 𝕜 (Icc r s) :=
Ici_inter_Iic.subst ((convex_Ici r).inter $ convex_Iic s)
lemma convex_halfspace_le {f : E → β} (h : is_linear_map 𝕜 f) (r : β) :
convex 𝕜 {w | f w ≤ r} :=
(convex_Iic r).is_linear_preimage h
lemma convex_halfspace_ge {f : E → β} (h : is_linear_map 𝕜 f) (r : β) :
convex 𝕜 {w | r ≤ f w} :=
(convex_Ici r).is_linear_preimage h
lemma convex_hyperplane {f : E → β} (h : is_linear_map 𝕜 f) (r : β) :
convex 𝕜 {w | f w = r} :=
begin
simp_rw le_antisymm_iff,
exact (convex_halfspace_le h r).inter (convex_halfspace_ge h r),
end
end ordered_add_comm_monoid
section ordered_cancel_add_comm_monoid
variables [ordered_cancel_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β]
lemma convex_Iio (r : β) : convex 𝕜 (Iio r) :=
begin
intros x hx y hy a b ha hb hab,
obtain rfl | ha' := ha.eq_or_lt,
{ rw zero_add at hab,
rwa [zero_smul, zero_add, hab, one_smul] },
rw mem_Iio at hx hy,
calc
a • x + b • y
< a • r + b • r
: add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hx ha') (smul_le_smul_of_nonneg hy.le hb)
... = r : convex.combo_self hab _
end
lemma convex_Ioi (r : β) : convex 𝕜 (Ioi r) := @convex_Iio 𝕜 βᵒᵈ _ _ _ _ r
lemma convex_Ioo (r s : β) : convex 𝕜 (Ioo r s) :=
Ioi_inter_Iio.subst ((convex_Ioi r).inter $ convex_Iio s)
lemma convex_Ico (r s : β) : convex 𝕜 (Ico r s) :=
Ici_inter_Iio.subst ((convex_Ici r).inter $ convex_Iio s)
lemma convex_Ioc (r s : β) : convex 𝕜 (Ioc r s) :=
Ioi_inter_Iic.subst ((convex_Ioi r).inter $ convex_Iic s)
lemma convex_halfspace_lt {f : E → β} (h : is_linear_map 𝕜 f) (r : β) :
convex 𝕜 {w | f w < r} :=
(convex_Iio r).is_linear_preimage h
lemma convex_halfspace_gt {f : E → β} (h : is_linear_map 𝕜 f) (r : β) :
convex 𝕜 {w | r < f w} :=
(convex_Ioi r).is_linear_preimage h
end ordered_cancel_add_comm_monoid
section linear_ordered_add_comm_monoid
variables [linear_ordered_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β]
lemma convex_uIcc (r s : β) : convex 𝕜 (uIcc r s) := convex_Icc _ _
end linear_ordered_add_comm_monoid
end module
end add_comm_monoid
section linear_ordered_add_comm_monoid
variables [linear_ordered_add_comm_monoid E] [ordered_add_comm_monoid β] [module 𝕜 E]
[ordered_smul 𝕜 E] {s : set E} {f : E → β}
lemma monotone_on.convex_le (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) :
convex 𝕜 {x ∈ s | f x ≤ r} :=
λ x hx y hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab,
(hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans
(max_rec' _ hx.2 hy.2)⟩
lemma monotone_on.convex_lt (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) :
convex 𝕜 {x ∈ s | f x < r} :=
λ x hx y hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab,
(hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans_lt
(max_rec' _ hx.2 hy.2)⟩
lemma monotone_on.convex_ge (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) :
convex 𝕜 {x ∈ s | r ≤ f x} :=
@monotone_on.convex_le 𝕜 Eᵒᵈ βᵒᵈ _ _ _ _ _ _ _ hf.dual hs r
lemma monotone_on.convex_gt (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) :
convex 𝕜 {x ∈ s | r < f x} :=
@monotone_on.convex_lt 𝕜 Eᵒᵈ βᵒᵈ _ _ _ _ _ _ _ hf.dual hs r
lemma antitone_on.convex_le (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) :
convex 𝕜 {x ∈ s | f x ≤ r} :=
@monotone_on.convex_ge 𝕜 E βᵒᵈ _ _ _ _ _ _ _ hf hs r
lemma antitone_on.convex_lt (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) :
convex 𝕜 {x ∈ s | f x < r} :=
@monotone_on.convex_gt 𝕜 E βᵒᵈ _ _ _ _ _ _ _ hf hs r
lemma antitone_on.convex_ge (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) :
convex 𝕜 {x ∈ s | r ≤ f x} :=
@monotone_on.convex_le 𝕜 E βᵒᵈ _ _ _ _ _ _ _ hf hs r
lemma antitone_on.convex_gt (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) :
convex 𝕜 {x ∈ s | r < f x} :=
@monotone_on.convex_lt 𝕜 E βᵒᵈ _ _ _ _ _ _ _ hf hs r
lemma monotone.convex_le (hf : monotone f) (r : β) :
convex 𝕜 {x | f x ≤ r} :=
set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r)
lemma monotone.convex_lt (hf : monotone f) (r : β) :
convex 𝕜 {x | f x ≤ r} :=
set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r)
lemma monotone.convex_ge (hf : monotone f ) (r : β) :
convex 𝕜 {x | r ≤ f x} :=
set.sep_univ.subst ((hf.monotone_on univ).convex_ge convex_univ r)
lemma monotone.convex_gt (hf : monotone f) (r : β) :
convex 𝕜 {x | f x ≤ r} :=
set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r)
lemma antitone.convex_le (hf : antitone f) (r : β) :
convex 𝕜 {x | f x ≤ r} :=
set.sep_univ.subst ((hf.antitone_on univ).convex_le convex_univ r)
lemma antitone.convex_lt (hf : antitone f) (r : β) :
convex 𝕜 {x | f x < r} :=
set.sep_univ.subst ((hf.antitone_on univ).convex_lt convex_univ r)
lemma antitone.convex_ge (hf : antitone f) (r : β) :
convex 𝕜 {x | r ≤ f x} :=
set.sep_univ.subst ((hf.antitone_on univ).convex_ge convex_univ r)
lemma antitone.convex_gt (hf : antitone f) (r : β) :
convex 𝕜 {x | r < f x} :=
set.sep_univ.subst ((hf.antitone_on univ).convex_gt convex_univ r)
end linear_ordered_add_comm_monoid
end ordered_semiring
section ordered_comm_semiring
variables [ordered_comm_semiring 𝕜]
section add_comm_monoid
variables [add_comm_monoid E] [add_comm_monoid F] [module 𝕜 E] [module 𝕜 F] {s : set E}
lemma convex.smul (hs : convex 𝕜 s) (c : 𝕜) : convex 𝕜 (c • s) :=
hs.linear_image (linear_map.lsmul _ _ c)
lemma convex.smul_preimage (hs : convex 𝕜 s) (c : 𝕜) : convex 𝕜 ((λ z, c • z) ⁻¹' s) :=
hs.linear_preimage (linear_map.lsmul _ _ c)
lemma convex.affinity (hs : convex 𝕜 s) (z : E) (c : 𝕜) : convex 𝕜 ((λ x, z + c • x) '' s) :=
by simpa only [←image_smul, ←image_vadd, image_image] using (hs.smul c).vadd z
end add_comm_monoid
end ordered_comm_semiring
section strict_ordered_comm_semiring
variables [strict_ordered_comm_semiring 𝕜] [add_comm_group E] [module 𝕜 E]
lemma convex_open_segment (a b : E) : convex 𝕜 (open_segment 𝕜 a b) :=
begin
rw convex_iff_open_segment_subset,
rintro p ⟨ap, bp, hap, hbp, habp, rfl⟩ q ⟨aq, bq, haq, hbq, habq, rfl⟩ z ⟨a, b, ha, hb, hab, rfl⟩,
refine ⟨a * ap + b * aq, a * bp + b * bq, by positivity, by positivity, _, _⟩,
{ rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] },
{ simp_rw [add_smul, mul_smul, smul_add, add_add_add_comm] }
end
end strict_ordered_comm_semiring
section ordered_ring
variables [ordered_ring 𝕜]
section add_comm_group
variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {s t : set E}
lemma convex.add_smul_mem (hs : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : x + y ∈ s)
{t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : x + t • y ∈ s :=
begin
have h : x + t • y = (1 - t) • x + t • (x + y),
{ rw [smul_add, ←add_assoc, ←add_smul, sub_add_cancel, one_smul] },
rw h,
exact hs hx hy (sub_nonneg_of_le ht.2) ht.1 (sub_add_cancel _ _),
end
lemma convex.smul_mem_of_zero_mem (hs : convex 𝕜 s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s)
{t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : t • x ∈ s :=
by simpa using hs.add_smul_mem zero_mem (by simpa using hx) ht
lemma convex.add_smul_sub_mem (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s)
{t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : x + t • (y - x) ∈ s :=
begin
apply h.segment_subset hx hy,
rw segment_eq_image',
exact mem_image_of_mem _ ht,
end
/-- Affine subspaces are convex. -/
lemma affine_subspace.convex (Q : affine_subspace 𝕜 E) : convex 𝕜 (Q : set E) :=
begin
intros x hx y hy a b ha hb hab,
rw [eq_sub_of_add_eq hab, ← affine_map.line_map_apply_module],
exact affine_map.line_map_mem b hx hy,
end
/-- The preimage of a convex set under an affine map is convex. -/
lemma convex.affine_preimage (f : E →ᵃ[𝕜] F) {s : set F} (hs : convex 𝕜 s) :
convex 𝕜 (f ⁻¹' s) :=
λ x hx, (hs hx).affine_preimage _
/-- The image of a convex set under an affine map is convex. -/
lemma convex.affine_image (f : E →ᵃ[𝕜] F) (hs : convex 𝕜 s) : convex 𝕜 (f '' s) :=
by { rintro _ ⟨x, hx, rfl⟩, exact (hs hx).affine_image _ }
lemma convex.neg (hs : convex 𝕜 s) : convex 𝕜 (-s) :=
hs.is_linear_preimage is_linear_map.is_linear_map_neg
lemma convex.sub (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s - t) :=
by { rw sub_eq_add_neg, exact hs.add ht.neg }
end add_comm_group
end ordered_ring
section linear_ordered_field
variables [linear_ordered_field 𝕜]
section add_comm_group
variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {s : set E}
/-- Alternative definition of set convexity, using division. -/
lemma convex_iff_div :
convex 𝕜 s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄,
0 ≤ a → 0 ≤ b → 0 < a + b → (a / (a + b)) • x + (b / (a + b)) • y ∈ s :=
forall₂_congr $ λ x hx, star_convex_iff_div
lemma convex.mem_smul_of_zero_mem (h : convex 𝕜 s) {x : E} (zero_mem : (0 : E) ∈ s)
(hx : x ∈ s) {t : 𝕜} (ht : 1 ≤ t) :
x ∈ t • s :=
begin
rw mem_smul_set_iff_inv_smul_mem₀ (zero_lt_one.trans_le ht).ne',
exact h.smul_mem_of_zero_mem zero_mem hx ⟨inv_nonneg.2 (zero_le_one.trans ht), inv_le_one ht⟩,
end
lemma convex.add_smul (h_conv : convex 𝕜 s) {p q : 𝕜} (hp : 0 ≤ p) (hq : 0 ≤ q) :
(p + q) • s = p • s + q • s :=
begin
obtain rfl | hs := s.eq_empty_or_nonempty,
{ simp_rw [smul_set_empty, add_empty] },
obtain rfl | hp' := hp.eq_or_lt,
{ rw [zero_add, zero_smul_set hs, zero_add] },
obtain rfl | hq' := hq.eq_or_lt,
{ rw [add_zero, zero_smul_set hs, add_zero] },
ext,
split,
{ rintro ⟨v, hv, rfl⟩,
exact ⟨p • v, q • v, smul_mem_smul_set hv, smul_mem_smul_set hv, (add_smul _ _ _).symm⟩ },
{ rintro ⟨v₁, v₂, ⟨v₁₁, h₁₂, rfl⟩, ⟨v₂₁, h₂₂, rfl⟩, rfl⟩,
have hpq := add_pos hp' hq',
refine mem_smul_set.2 ⟨_, h_conv h₁₂ h₂₂ _ _
(by rw [←div_self hpq.ne', add_div] : p / (p + q) + q / (p + q) = 1),
by simp only [← mul_smul, smul_add, mul_div_cancel' _ hpq.ne']⟩; positivity }
end
end add_comm_group
end linear_ordered_field
/-!
#### Convex sets in an ordered space
Relates `convex` and `ord_connected`.
-/
section
lemma set.ord_connected.convex_of_chain [ordered_semiring 𝕜] [ordered_add_comm_monoid E]
[module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} (hs : s.ord_connected) (h : is_chain (≤) s) :
convex 𝕜 s :=
begin
refine convex_iff_segment_subset.mpr (λ x hx y hy, _),
obtain hxy | hyx := h.total hx hy,
{ exact (segment_subset_Icc hxy).trans (hs.out hx hy) },
{ rw segment_symm,
exact (segment_subset_Icc hyx).trans (hs.out hy hx) }
end
lemma set.ord_connected.convex [ordered_semiring 𝕜] [linear_ordered_add_comm_monoid E] [module 𝕜 E]
[ordered_smul 𝕜 E] {s : set E} (hs : s.ord_connected) :
convex 𝕜 s :=
hs.convex_of_chain $ is_chain_of_trichotomous s
lemma convex_iff_ord_connected [linear_ordered_field 𝕜] {s : set 𝕜} :
convex 𝕜 s ↔ s.ord_connected :=
by simp_rw [convex_iff_segment_subset, segment_eq_uIcc, ord_connected_iff_uIcc_subset]
alias convex_iff_ord_connected ↔ convex.ord_connected _
end
/-! #### Convexity of submodules/subspaces -/
namespace submodule
variables [ordered_semiring 𝕜] [add_comm_monoid E] [module 𝕜 E]
protected lemma convex (K : submodule 𝕜 E) : convex 𝕜 (↑K : set E) :=
by { repeat {intro}, refine add_mem (smul_mem _ _ _) (smul_mem _ _ _); assumption }
protected lemma star_convex (K : submodule 𝕜 E) : star_convex 𝕜 (0 : E) K := K.convex K.zero_mem
end submodule
/-! ### Simplex -/
section simplex
variables (𝕜) (ι : Type*) [ordered_semiring 𝕜] [fintype ι]
/-- The standard simplex in the space of functions `ι → 𝕜` is the set of vectors with non-negative
coordinates with total sum `1`. This is the free object in the category of convex spaces. -/
def std_simplex : set (ι → 𝕜) :=
{f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1}
lemma std_simplex_eq_inter :
std_simplex 𝕜 ι = (⋂ x, {f | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} :=
by { ext f, simp only [std_simplex, set.mem_inter_iff, set.mem_Inter, set.mem_set_of_eq] }
lemma convex_std_simplex : convex 𝕜 (std_simplex 𝕜 ι) :=
begin
refine λ f hf g hg a b ha hb hab, ⟨λ x, _, _⟩,
{ apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] },
{ erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2,
smul_eq_mul, smul_eq_mul, mul_one, mul_one],
exact hab }
end
variable {ι}
lemma ite_eq_mem_std_simplex (i : ι) : (λ j, ite (i = j) (1:𝕜) 0) ∈ std_simplex 𝕜 ι :=
⟨λ j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)]⟩
end simplex
|
6178812d6721d9d5cef8bbc28a3e14b1a2170c6f | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/covariant_and_contravariant.lean | ffb87739958df59cb3122f91c5ad73c48618f477 | [
"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 | 12,014 | lean | /-
Copyright (c) 2021 Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Damiano Testa
-/
import algebra.group.defs
import order.basic
/-!
# Covariants and contravariants
This file contains general lemmas and instances to work with the interactions between a relation and
an action on a Type.
The intended application is the splitting of the ordering from the algebraic assumptions on the
operations in the `ordered_[...]` hierarchy.
The strategy is to introduce two more flexible typeclasses, `covariant_class` and
`contravariant_class`:
* `covariant_class` models the implication `a ≤ b → c * a ≤ c * b` (multiplication is monotone),
* `contravariant_class` models the implication `a * b < a * c → b < c`.
Since `co(ntra)variant_class` takes as input the operation (typically `(+)` or `(*)`) and the order
relation (typically `(≤)` or `(<)`), these are the only two typeclasses that I have used.
The general approach is to formulate the lemma that you are interested in and prove it, with the
`ordered_[...]` typeclass of your liking. After that, you convert the single typeclass,
say `[ordered_cancel_monoid M]`, into three typeclasses, e.g.
`[left_cancel_semigroup M] [partial_order M] [covariant_class M M (function.swap (*)) (≤)]`
and have a go at seeing if the proof still works!
Note that it is possible to combine several co(ntra)variant_class assumptions together.
Indeed, the usual ordered typeclasses arise from assuming the pair
`[covariant_class M M (*) (≤)] [contravariant_class M M (*) (<)]`
on top of order/algebraic assumptions.
A formal remark is that normally `covariant_class` uses the `(≤)`-relation, while
`contravariant_class` uses the `(<)`-relation. This need not be the case in general, but seems to be
the most common usage. In the opposite direction, the implication
```lean
[semigroup α] [partial_order α] [contravariant_class α α (*) (≤)] => left_cancel_semigroup α
```
holds -- note the `co*ntra*` assumption on the `(≤)`-relation.
# Formalization notes
We stick to the convention of using `function.swap (*)` (or `function.swap (+)`), for the
typeclass assumptions, since `function.swap` is slightly better behaved than `flip`.
However, sometimes as a **non-typeclass** assumption, we prefer `flip (*)` (or `flip (+)`),
as it is easier to use. -/
-- TODO: convert `has_exists_mul_of_le`, `has_exists_add_of_le`?
-- TODO: relationship with `con/add_con`
-- TODO: include equivalence of `left_cancel_semigroup` with
-- `semigroup partial_order contravariant_class α α (*) (≤)`?
-- TODO : use ⇒, as per Eric's suggestion? See
-- https://leanprover.zulipchat.com/#narrow/stream/116395-maths/topic/ordered.20stuff/near/236148738
-- for a discussion.
open function
section variants
variables {M N : Type*} (μ : M → N → N) (r : N → N → Prop)
variables (M N)
/-- `covariant` is useful to formulate succintly statements about the interactions between an
action of a Type on another one and a relation on the acted-upon Type.
See the `covariant_class` doc-string for its meaning. -/
def covariant : Prop := ∀ (m) {n₁ n₂}, r n₁ n₂ → r (μ m n₁) (μ m n₂)
/-- `contravariant` is useful to formulate succintly statements about the interactions between an
action of a Type on another one and a relation on the acted-upon Type.
See the `contravariant_class` doc-string for its meaning. -/
def contravariant : Prop := ∀ (m) {n₁ n₂}, r (μ m n₁) (μ m n₂) → r n₁ n₂
/-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the
`covariant_class` says that "the action `μ` preserves the relation `r`.
More precisely, the `covariant_class` is a class taking two Types `M N`, together with an "action"
`μ : M → N → N` and a relation `r : N → N → Prop`. Its unique field `elim` is the assertion that
for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair
`(n₁, n₂)`, then, the relation `r` also holds for the pair `(μ m n₁, μ m n₂)`,
obtained from `(n₁, n₂)` by "acting upon it by `m`".
If `m : M` and `h : r n₁ n₂`, then `covariant_class.elim m h : r (μ m n₁) (μ m n₂)`.
-/
@[protect_proj] class covariant_class : Prop :=
(elim : covariant M N μ r)
/-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the
`contravariant_class` says that "if the result of the action `μ` on a pair satisfies the
relation `r`, then the initial pair satisfied the relation `r`.
More precisely, the `contravariant_class` is a class taking two Types `M N`, together with an
"action" `μ : M → N → N` and a relation `r : N → N → Prop`. Its unique field `elim` is the
assertion that for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the
pair `(μ m n₁, μ m n₂)` obtained from `(n₁, n₂)` by "acting upon it by `m`"", then, the relation
`r` also holds for the pair `(n₁, n₂)`.
If `m : M` and `h : r (μ m n₁) (μ m n₂)`, then `contravariant_class.elim m h : r n₁ n₂`.
-/
@[protect_proj] class contravariant_class : Prop :=
(elim : contravariant M N μ r)
lemma rel_iff_cov [covariant_class M N μ r] [contravariant_class M N μ r] (m : M) {a b : N} :
r (μ m a) (μ m b) ↔ r a b :=
⟨contravariant_class.elim _, covariant_class.elim _⟩
section flip
variables {M N μ r}
lemma covariant.flip (h : covariant M N μ r) : covariant M N μ (flip r) :=
λ a b c hbc, h a hbc
lemma contravariant.flip (h : contravariant M N μ r) : contravariant M N μ (flip r) :=
λ a b c hbc, h a hbc
end flip
section covariant
variables {M N μ r} [covariant_class M N μ r]
lemma act_rel_act_of_rel (m : M) {a b : N} (ab : r a b) :
r (μ m a) (μ m b) :=
covariant_class.elim _ ab
@[to_additive]
lemma group.covariant_iff_contravariant [group N] :
covariant N N (*) r ↔ contravariant N N (*) r :=
begin
refine ⟨λ h a b c bc, _, λ h a b c bc, _⟩,
{ rw [← inv_mul_cancel_left a b, ← inv_mul_cancel_left a c],
exact h a⁻¹ bc },
{ rw [← inv_mul_cancel_left a b, ← inv_mul_cancel_left a c] at bc,
exact h a⁻¹ bc }
end
@[to_additive]
lemma group.covconv [group N] [covariant_class N N (*) r] :
contravariant_class N N (*) r :=
⟨group.covariant_iff_contravariant.mp covariant_class.elim⟩
section is_trans
variables [is_trans N r] (m n : M) {a b c d : N}
/- Lemmas with 3 elements. -/
lemma act_rel_of_rel_of_act_rel (ab : r a b) (rl : r (μ m b) c) :
r (μ m a) c :=
trans (act_rel_act_of_rel m ab) rl
lemma rel_act_of_rel_of_rel_act (ab : r a b) (rr : r c (μ m a)) :
r c (μ m b) :=
trans rr (act_rel_act_of_rel _ ab)
end is_trans
end covariant
/- Lemma with 4 elements. -/
section M_eq_N
variables {M N μ r} {mu : N → N → N} [is_trans N r]
[covariant_class N N mu r] [covariant_class N N (swap mu) r] {a b c d : N}
lemma act_rel_act_of_rel_of_rel (ab : r a b) (cd : r c d) :
r (mu a c) (mu b d) :=
trans (act_rel_act_of_rel c ab : _) (act_rel_act_of_rel b cd)
end M_eq_N
section contravariant
variables {M N μ r} [contravariant_class M N μ r]
lemma rel_of_act_rel_act (m : M) {a b : N} (ab : r (μ m a) (μ m b)) :
r a b :=
contravariant_class.elim _ ab
section is_trans
variables [is_trans N r] (m n : M) {a b c d : N}
/- Lemmas with 3 elements. -/
lemma act_rel_of_act_rel_of_rel_act_rel (ab : r (μ m a) b) (rl : r (μ m b) (μ m c)) :
r (μ m a) c :=
trans ab (rel_of_act_rel_act m rl)
lemma rel_act_of_act_rel_act_of_rel_act (ab : r (μ m a) (μ m b)) (rr : r b (μ m c)) :
r a (μ m c) :=
trans (rel_of_act_rel_act m ab) rr
end is_trans
end contravariant
lemma covariant_le_of_covariant_lt [partial_order N] :
covariant M N μ (<) → covariant M N μ (≤) :=
begin
refine λ h a b c bc, _,
rcases le_iff_eq_or_lt.mp bc with rfl | bc,
{ exact rfl.le },
{ exact (h _ bc).le }
end
lemma contravariant_lt_of_contravariant_le [partial_order N] :
contravariant M N μ (≤) → contravariant M N μ (<) :=
begin
refine λ h a b c bc, lt_iff_le_and_ne.mpr ⟨h a bc.le, _⟩,
rintro rfl,
exact lt_irrefl _ bc,
end
lemma covariant_le_iff_contravariant_lt [linear_order N] :
covariant M N μ (≤) ↔ contravariant M N μ (<) :=
⟨ λ h a b c bc, not_le.mp (λ k, not_le.mpr bc (h _ k)),
λ h a b c bc, not_lt.mp (λ k, not_lt.mpr bc (h _ k))⟩
lemma covariant_lt_iff_contravariant_le [linear_order N] :
covariant M N μ (<) ↔ contravariant M N μ (≤) :=
⟨ λ h a b c bc, not_lt.mp (λ k, not_lt.mpr bc (h _ k)),
λ h a b c bc, not_le.mp (λ k, not_le.mpr bc (h _ k))⟩
@[to_additive]
lemma covariant_flip_mul_iff [comm_semigroup N] :
covariant N N (flip (*)) (r) ↔ covariant N N (*) (r) :=
by rw is_symm_op.flip_eq
@[to_additive]
lemma contravariant_flip_mul_iff [comm_semigroup N] :
contravariant N N (flip (*)) (r) ↔ contravariant N N (*) (r) :=
by rw is_symm_op.flip_eq
@[to_additive]
instance contravariant_mul_lt_of_covariant_mul_le [has_mul N] [linear_order N]
[covariant_class N N (*) (≤)] : contravariant_class N N (*) (<) :=
{ elim := (covariant_le_iff_contravariant_lt N N (*)).mp covariant_class.elim }
@[to_additive]
instance covariant_mul_lt_of_contravariant_mul_le [has_mul N] [linear_order N]
[contravariant_class N N (*) (≤)] : covariant_class N N (*) (<) :=
{ elim := (covariant_lt_iff_contravariant_le N N (*)).mpr contravariant_class.elim }
@[to_additive]
instance covariant_swap_mul_le_of_covariant_mul_le [comm_semigroup N] [has_le N]
[covariant_class N N (*) (≤)] : covariant_class N N (swap (*)) (≤) :=
{ elim := (covariant_flip_mul_iff N (≤)).mpr covariant_class.elim }
@[to_additive]
instance contravariant_swap_mul_le_of_contravariant_mul_le [comm_semigroup N] [has_le N]
[contravariant_class N N (*) (≤)] : contravariant_class N N (swap (*)) (≤) :=
{ elim := (contravariant_flip_mul_iff N (≤)).mpr contravariant_class.elim }
@[to_additive]
instance contravariant_swap_mul_lt_of_contravariant_mul_lt [comm_semigroup N] [has_lt N]
[contravariant_class N N (*) (<)] : contravariant_class N N (swap (*)) (<) :=
{ elim := (contravariant_flip_mul_iff N (<)).mpr contravariant_class.elim }
@[to_additive]
instance covariant_swap_mul_lt_of_covariant_mul_lt [comm_semigroup N] [has_lt N]
[covariant_class N N (*) (<)] : covariant_class N N (swap (*)) (<) :=
{ elim := (covariant_flip_mul_iff N (<)).mpr covariant_class.elim }
@[to_additive]
instance left_cancel_semigroup.covariant_mul_lt_of_covariant_mul_le
[left_cancel_semigroup N] [partial_order N] [covariant_class N N (*) (≤)] :
covariant_class N N (*) (<) :=
{ elim := λ a b c bc, by { cases lt_iff_le_and_ne.mp bc with bc cb,
exact lt_iff_le_and_ne.mpr ⟨covariant_class.elim a bc, (mul_ne_mul_right a).mpr cb⟩ } }
@[to_additive]
instance right_cancel_semigroup.covariant_swap_mul_lt_of_covariant_swap_mul_le
[right_cancel_semigroup N] [partial_order N] [covariant_class N N (swap (*)) (≤)] :
covariant_class N N (swap (*)) (<) :=
{ elim := λ a b c bc, by { cases lt_iff_le_and_ne.mp bc with bc cb,
exact lt_iff_le_and_ne.mpr ⟨covariant_class.elim a bc, (mul_ne_mul_left a).mpr cb⟩ } }
@[to_additive]
instance left_cancel_semigroup.contravariant_mul_le_of_contravariant_mul_lt
[left_cancel_semigroup N] [partial_order N] [contravariant_class N N (*) (<)] :
contravariant_class N N (*) (≤) :=
{ elim := λ a b c bc, by { cases le_iff_eq_or_lt.mp bc with h h,
{ exact ((mul_right_inj a).mp h).le },
{ exact (contravariant_class.elim _ h).le } } }
@[to_additive]
instance right_cancel_semigroup.contravariant_swap_mul_le_of_contravariant_swap_mul_lt
[right_cancel_semigroup N] [partial_order N] [contravariant_class N N (swap (*)) (<)] :
contravariant_class N N (swap (*)) (≤) :=
{ elim := λ a b c bc, by { cases le_iff_eq_or_lt.mp bc with h h,
{ exact ((mul_left_inj a).mp h).le },
{ exact (contravariant_class.elim _ h).le } } }
end variants
|
5db9733860314b337b41feaee2c4ad1939e1bc1c | ac89c256db07448984849346288e0eeffe8b20d0 | /stage0/src/Lean/Meta/Basic.lean | 512e6a5be7455b285bec80cac27e2207a7ef171d | [
"Apache-2.0"
] | permissive | chepinzhang/lean4 | 002cc667f35417a418f0ebc9cb4a44559bb0ccac | 24fe2875c68549b5481f07c57eab4ad4a0ae5305 | refs/heads/master | 1,688,942,838,326 | 1,628,801,942,000 | 1,628,801,995,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 47,875 | 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.Data.LOption
import Lean.Environment
import Lean.Class
import Lean.ReducibilityAttrs
import Lean.Util.Trace
import Lean.Util.RecDepth
import Lean.Util.PPExt
import Lean.Util.OccursCheck
import Lean.Util.MonadBacktrack
import Lean.Compiler.InlineAttrs
import Lean.Meta.TransparencyMode
import Lean.Meta.DiscrTreeTypes
import Lean.Eval
import Lean.CoreM
/-
This module provides four (mutually dependent) goodies that are needed for building the elaborator and tactic frameworks.
1- Weak head normal form computation with support for metavariables and transparency modes.
2- Definitionally equality checking with support for metavariables (aka unification modulo definitional equality).
3- Type inference.
4- Type class resolution.
They are packed into the MetaM monad.
-/
namespace Lean.Meta
builtin_initialize isDefEqStuckExceptionId : InternalExceptionId ← registerInternalExceptionId `isDefEqStuck
structure Config where
foApprox : Bool := false
ctxApprox : Bool := false
quasiPatternApprox : Bool := false
/- When `constApprox` is set to true,
we solve `?m t =?= c` using
`?m := fun _ => c`
when `?m t` is not a higher-order pattern and `c` is not an application as -/
constApprox : Bool := false
/-
When the following flag is set,
`isDefEq` throws the exeption `Exeption.isDefEqStuck`
whenever it encounters a constraint `?m ... =?= t` where
`?m` is read only.
This feature is useful for type class resolution where
we may want to notify the caller that the TC problem may be solveable
later after it assigns `?m`. -/
isDefEqStuckEx : Bool := false
transparency : TransparencyMode := TransparencyMode.default
/- If zetaNonDep == false, then non dependent let-decls are not zeta expanded. -/
zetaNonDep : Bool := true
/- When `trackZeta == true`, we store zetaFVarIds all free variables that have been zeta-expanded. -/
trackZeta : Bool := false
unificationHints : Bool := true
/- Enables proof irrelevance at `isDefEq` -/
proofIrrelevance : Bool := true
/- By default synthetic opaque metavariables are not assigned by `isDefEq`. Motivation: we want to make
sure typing constraints resolved during elaboration should not "fill" holes that are supposed to be filled using tactics.
However, this restriction is too restrictive for tactics such as `exact t`. When elaborating `t`, we dot not fill
named holes when solving typing constraints or TC resolution. But, we ignore the restriction when we try to unify
the type of `t` with the goal target type. We claim this is not a hack and is defensible behavior because
this last unification step is not really part of the term elaboration. -/
assignSyntheticOpaque : Bool := false
structure ParamInfo where
binderInfo : BinderInfo := BinderInfo.default
hasFwdDeps : Bool := false
backDeps : Array Nat := #[]
deriving Inhabited
def ParamInfo.isImplicit (p : ParamInfo) : Bool :=
p.binderInfo == BinderInfo.implicit
def ParamInfo.isInstImplicit (p : ParamInfo) : Bool :=
p.binderInfo == BinderInfo.instImplicit
def ParamInfo.isStrictImplicit (p : ParamInfo) : Bool :=
p.binderInfo == BinderInfo.strictImplicit
def ParamInfo.isExplicit (p : ParamInfo) : Bool :=
p.binderInfo == BinderInfo.default || p.binderInfo == BinderInfo.auxDecl
structure FunInfo where
paramInfo : Array ParamInfo := #[]
resultDeps : Array Nat := #[]
structure InfoCacheKey where
transparency : TransparencyMode
expr : Expr
nargs? : Option Nat
deriving Inhabited, BEq
namespace InfoCacheKey
instance : Hashable InfoCacheKey :=
⟨fun ⟨transparency, expr, nargs⟩ => mixHash (hash transparency) <| mixHash (hash expr) (hash nargs)⟩
end InfoCacheKey
open Std (PersistentArray PersistentHashMap)
abbrev SynthInstanceCache := PersistentHashMap Expr (Option Expr)
abbrev InferTypeCache := PersistentExprStructMap Expr
abbrev FunInfoCache := PersistentHashMap InfoCacheKey FunInfo
abbrev WhnfCache := PersistentExprStructMap Expr
/- A set of pairs. TODO: consider more efficient representations (e.g., a proper set) and caching policies (e.g., imperfect cache).
We should also investigate the impact on memory consumption. -/
abbrev DefEqCache := PersistentHashMap (Expr × Expr) Unit
structure Cache where
inferType : InferTypeCache := {}
funInfo : FunInfoCache := {}
synthInstance : SynthInstanceCache := {}
whnfDefault : WhnfCache := {} -- cache for closed terms and `TransparencyMode.default`
whnfAll : WhnfCache := {} -- cache for closed terms and `TransparencyMode.all`
defEqDefault : DefEqCache := {}
defEqAll : DefEqCache := {}
deriving Inhabited
/--
"Context" for a postponed universe constraint.
`lhs` and `rhs` are the surrounding `isDefEq` call when the postponed constraint was created.
-/
structure DefEqContext where
lhs : Expr
rhs : Expr
lctx : LocalContext
localInstances : LocalInstances
/--
Auxiliary structure for representing postponed universe constraints.
Remark: the fields `ref` and `rootDefEq?` are used for error message generation only.
Remark: we may consider improving the error message generation in the future.
-/
structure PostponedEntry where
ref : Syntax -- We save the `ref` at entry creation time
lhs : Level
rhs : Level
ctx? : Option DefEqContext -- Context for the surrounding `isDefEq` call when entry was created
deriving Inhabited
structure State where
mctx : MetavarContext := {}
cache : Cache := {}
/- When `trackZeta == true`, then any let-decl free variable that is zeta expansion performed by `MetaM` is stored in `zetaFVarIds`. -/
zetaFVarIds : NameSet := {}
postponed : PersistentArray PostponedEntry := {}
deriving Inhabited
structure SavedState where
core : Core.State
meta : State
deriving Inhabited
structure Context where
config : Config := {}
lctx : LocalContext := {}
localInstances : LocalInstances := #[]
/-- Not `none` when inside of an `isDefEq` test. See `PostponedEntry`. -/
defEqCtx? : Option DefEqContext := none
/--
Track the number of nested `synthPending` invocations. Nested invocations can happen
when the type class resolution invokes `synthPending`.
Remark: in the current implementation, `synthPending` fails if `synthPendingDepth > 0`.
We will add a configuration option if necessary. -/
synthPendingDepth : Nat := 0
abbrev MetaM := ReaderT Context $ StateRefT State CoreM
-- Make the compiler generate specialized `pure`/`bind` so we do not have to optimize through the
-- whole monad stack at every use site. May eventually be covered by `deriving`.
instance : Monad MetaM := { inferInstanceAs (Monad MetaM) with }
instance : Inhabited (MetaM α) where
default := fun _ _ => arbitrary
instance : MonadLCtx MetaM where
getLCtx := return (← read).lctx
instance : MonadMCtx MetaM where
getMCtx := return (← get).mctx
modifyMCtx f := modify fun s => { s with mctx := f s.mctx }
instance : AddMessageContext MetaM where
addMessageContext := addMessageContextFull
protected def saveState : MetaM SavedState :=
return { core := (← getThe Core.State), meta := (← get) }
/-- Restore backtrackable parts of the state. -/
def SavedState.restore (b : SavedState) : MetaM Unit := do
Core.restore b.core
modify fun s => { s with mctx := b.meta.mctx, zetaFVarIds := b.meta.zetaFVarIds, postponed := b.meta.postponed }
instance : MonadBacktrack SavedState MetaM where
saveState := Meta.saveState
restoreState s := s.restore
@[inline] def MetaM.run (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM (α × State) :=
x ctx |>.run s
@[inline] def MetaM.run' (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM α :=
Prod.fst <$> x.run ctx s
@[inline] def MetaM.toIO (x : MetaM α) (ctxCore : Core.Context) (sCore : Core.State) (ctx : Context := {}) (s : State := {}) : IO (α × Core.State × State) := do
let ((a, s), sCore) ← (x.run ctx s).toIO ctxCore sCore
pure (a, sCore, s)
instance [MetaEval α] : MetaEval (MetaM α) :=
⟨fun env opts x _ => MetaEval.eval env opts x.run' true⟩
protected def throwIsDefEqStuck : MetaM α :=
throw <| Exception.internal isDefEqStuckExceptionId
builtin_initialize
registerTraceClass `Meta
registerTraceClass `Meta.debug
@[inline] def liftMetaM [MonadLiftT MetaM m] (x : MetaM α) : m α :=
liftM x
@[inline] def mapMetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, MetaM α → MetaM α) {α} (x : m α) : m α :=
controlAt MetaM fun runInBase => f <| runInBase x
@[inline] def map1MetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → MetaM α) → MetaM α) {α} (k : β → m α) : m α :=
controlAt MetaM fun runInBase => f fun b => runInBase <| k b
@[inline] def map2MetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → γ → MetaM α) → MetaM α) {α} (k : β → γ → m α) : m α :=
controlAt MetaM fun runInBase => f fun b c => runInBase <| k b c
section Methods
variable [MonadControlT MetaM n] [Monad n]
@[inline] def modifyCache (f : Cache → Cache) : MetaM Unit :=
modify fun ⟨mctx, cache, zetaFVarIds, postponed⟩ => ⟨mctx, f cache, zetaFVarIds, postponed⟩
@[inline] def modifyInferTypeCache (f : InferTypeCache → InferTypeCache) : MetaM Unit :=
modifyCache fun ⟨ic, c1, c2, c3, c4, c5, c6⟩ => ⟨f ic, c1, c2, c3, c4, c5, c6⟩
def getLocalInstances : MetaM LocalInstances :=
return (← read).localInstances
def getConfig : MetaM Config :=
return (← read).config
def setMCtx (mctx : MetavarContext) : MetaM Unit :=
modify fun s => { s with mctx := mctx }
def resetZetaFVarIds : MetaM Unit :=
modify fun s => { s with zetaFVarIds := {} }
def getZetaFVarIds : MetaM NameSet :=
return (← get).zetaFVarIds
def getPostponed : MetaM (PersistentArray PostponedEntry) :=
return (← get).postponed
def setPostponed (postponed : PersistentArray PostponedEntry) : MetaM Unit :=
modify fun s => { s with postponed := postponed }
@[inline] def modifyPostponed (f : PersistentArray PostponedEntry → PersistentArray PostponedEntry) : MetaM Unit :=
modify fun s => { s with postponed := f s.postponed }
/- WARNING: The following 4 constants are a hack for simulating forward declarations.
They are defined later using the `export` attribute. This is hackish because we
have to hard-code the true arity of these definitions here, and make sure the C names match.
We have used another hack based on `IO.Ref`s in the past, it was safer but less efficient. -/
@[extern 6 "lean_whnf"] constant whnf : Expr → MetaM Expr
@[extern 6 "lean_infer_type"] constant inferType : Expr → MetaM Expr
@[extern 7 "lean_is_expr_def_eq"] constant isExprDefEqAux : Expr → Expr → MetaM Bool
@[extern 6 "lean_synth_pending"] protected constant synthPending : MVarId → MetaM Bool
def whnfForall (e : Expr) : MetaM Expr := do
let e' ← whnf e
if e'.isForall then pure e' else pure e
-- withIncRecDepth for a monad `n` such that `[MonadControlT MetaM n]`
protected def withIncRecDepth (x : n α) : n α :=
mapMetaM (withIncRecDepth (m := MetaM)) x
private def mkFreshExprMVarAtCore
(mvarId : MVarId) (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind) (userName : Name) (numScopeArgs : Nat) : MetaM Expr := do
modifyMCtx fun mctx => mctx.addExprMVarDecl mvarId userName lctx localInsts type kind numScopeArgs;
return mkMVar mvarId
def mkFreshExprMVarAt
(lctx : LocalContext) (localInsts : LocalInstances) (type : Expr)
(kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0)
: MetaM Expr := do
mkFreshExprMVarAtCore (← mkFreshId) lctx localInsts type kind userName numScopeArgs
def mkFreshLevelMVar : MetaM Level := do
let mvarId ← mkFreshId
modifyMCtx fun mctx => mctx.addLevelMVarDecl mvarId;
return mkLevelMVar mvarId
private def mkFreshExprMVarCore (type : Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := do
mkFreshExprMVarAt (← getLCtx) (← getLocalInstances) type kind userName
private def mkFreshExprMVarImpl (type? : Option Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr :=
match type? with
| some type => mkFreshExprMVarCore type kind userName
| none => do
let u ← mkFreshLevelMVar
let type ← mkFreshExprMVarCore (mkSort u) MetavarKind.natural Name.anonymous
mkFreshExprMVarCore type kind userName
def mkFreshExprMVar (type? : Option Expr) (kind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr :=
mkFreshExprMVarImpl type? kind userName
def mkFreshTypeMVar (kind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr := do
let u ← mkFreshLevelMVar
mkFreshExprMVar (mkSort u) kind userName
/- Low-level version of `MkFreshExprMVar` which allows users to create/reserve a `mvarId` using `mkFreshId`, and then later create
the metavar using this method. -/
private def mkFreshExprMVarWithIdCore (mvarId : MVarId) (type : Expr)
(kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0)
: MetaM Expr := do
mkFreshExprMVarAtCore mvarId (← getLCtx) (← getLocalInstances) type kind userName numScopeArgs
def mkFreshExprMVarWithId (mvarId : MVarId) (type? : Option Expr := none) (kind : MetavarKind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr :=
match type? with
| some type => mkFreshExprMVarWithIdCore mvarId type kind userName
| none => do
let u ← mkFreshLevelMVar
let type ← mkFreshExprMVar (mkSort u)
mkFreshExprMVarWithIdCore mvarId type kind userName
def mkFreshLevelMVars (num : Nat) : MetaM (List Level) :=
num.foldM (init := []) fun _ us =>
return (← mkFreshLevelMVar)::us
def mkFreshLevelMVarsFor (info : ConstantInfo) : MetaM (List Level) :=
mkFreshLevelMVars info.numLevelParams
def mkConstWithFreshMVarLevels (declName : Name) : MetaM Expr := do
let info ← getConstInfo declName
return mkConst declName (← mkFreshLevelMVarsFor info)
def getTransparency : MetaM TransparencyMode :=
return (← getConfig).transparency
def shouldReduceAll : MetaM Bool :=
return (← getTransparency) == TransparencyMode.all
def shouldReduceReducibleOnly : MetaM Bool :=
return (← getTransparency) == TransparencyMode.reducible
def getMVarDecl (mvarId : MVarId) : MetaM MetavarDecl := do
match (← getMCtx).findDecl? mvarId with
| some d => pure d
| none => throwError "unknown metavariable '?{mvarId}'"
def setMVarKind (mvarId : MVarId) (kind : MetavarKind) : MetaM Unit :=
modifyMCtx fun mctx => mctx.setMVarKind mvarId kind
/- Update the type of the given metavariable. This function assumes the new type is
definitionally equal to the current one -/
def setMVarType (mvarId : MVarId) (type : Expr) : MetaM Unit := do
modifyMCtx fun mctx => mctx.setMVarType mvarId type
def isReadOnlyExprMVar (mvarId : MVarId) : MetaM Bool := do
return (← getMVarDecl mvarId).depth != (← getMCtx).depth
def isReadOnlyOrSyntheticOpaqueExprMVar (mvarId : MVarId) : MetaM Bool := do
let mvarDecl ← getMVarDecl mvarId
match mvarDecl.kind with
| MetavarKind.syntheticOpaque => return !(← getConfig).assignSyntheticOpaque
| _ => return mvarDecl.depth != (← getMCtx).depth
def isReadOnlyLevelMVar (mvarId : MVarId) : MetaM Bool := do
let mctx ← getMCtx
match mctx.findLevelDepth? mvarId with
| some depth => return depth != mctx.depth
| _ => throwError "unknown universe metavariable '?{mvarId}'"
def renameMVar (mvarId : MVarId) (newUserName : Name) : MetaM Unit :=
modifyMCtx fun mctx => mctx.renameMVar mvarId newUserName
def isExprMVarAssigned (mvarId : MVarId) : MetaM Bool :=
return (← getMCtx).isExprAssigned mvarId
def getExprMVarAssignment? (mvarId : MVarId) : MetaM (Option Expr) :=
return (← getMCtx).getExprAssignment? mvarId
/-- Return true if `e` contains `mvarId` directly or indirectly -/
def occursCheck (mvarId : MVarId) (e : Expr) : MetaM Bool :=
return (← getMCtx).occursCheck mvarId e
def assignExprMVar (mvarId : MVarId) (val : Expr) : MetaM Unit :=
modifyMCtx fun mctx => mctx.assignExpr mvarId val
def isDelayedAssigned (mvarId : MVarId) : MetaM Bool :=
return (← getMCtx).isDelayedAssigned mvarId
def getDelayedAssignment? (mvarId : MVarId) : MetaM (Option DelayedMetavarAssignment) :=
return (← getMCtx).getDelayedAssignment? mvarId
def hasAssignableMVar (e : Expr) : MetaM Bool :=
return (← getMCtx).hasAssignableMVar e
def throwUnknownFVar (fvarId : FVarId) : MetaM α :=
throwError "unknown free variable '{mkFVar fvarId}'"
def findLocalDecl? (fvarId : FVarId) : MetaM (Option LocalDecl) :=
return (← getLCtx).find? fvarId
def getLocalDecl (fvarId : FVarId) : MetaM LocalDecl := do
match (← getLCtx).find? fvarId with
| some d => pure d
| none => throwUnknownFVar fvarId
def getFVarLocalDecl (fvar : Expr) : MetaM LocalDecl :=
getLocalDecl fvar.fvarId!
def getLocalDeclFromUserName (userName : Name) : MetaM LocalDecl := do
match (← getLCtx).findFromUserName? userName with
| some d => pure d
| none => throwError "unknown local declaration '{userName}'"
def instantiateLevelMVars (u : Level) : MetaM Level :=
MetavarContext.instantiateLevelMVars u
def instantiateMVars (e : Expr) : MetaM Expr :=
(MetavarContext.instantiateExprMVars e).run
def instantiateLocalDeclMVars (localDecl : LocalDecl) : MetaM LocalDecl :=
match localDecl with
| LocalDecl.cdecl idx id n type bi =>
return LocalDecl.cdecl idx id n (← instantiateMVars type) bi
| LocalDecl.ldecl idx id n type val nonDep =>
return LocalDecl.ldecl idx id n (← instantiateMVars type) (← instantiateMVars val) nonDep
@[inline] def liftMkBindingM (x : MetavarContext.MkBindingM α) : MetaM α := do
match x (← getLCtx) { mctx := (← getMCtx), ngen := (← getNGen) } with
| EStateM.Result.ok e newS => do
setNGen newS.ngen;
setMCtx newS.mctx;
pure e
| EStateM.Result.error (MetavarContext.MkBinding.Exception.revertFailure mctx lctx toRevert decl) newS => do
setMCtx newS.mctx;
setNGen newS.ngen;
throwError "failed to create binder due to failure when reverting variable dependencies"
def abstractRange (e : Expr) (n : Nat) (xs : Array Expr) : MetaM Expr :=
liftMkBindingM <| MetavarContext.abstractRange e n xs
def abstract (e : Expr) (xs : Array Expr) : MetaM Expr :=
abstractRange e xs.size xs
def mkForallFVars (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MetaM Expr :=
if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.mkForall xs e usedOnly usedLetOnly
def mkLambdaFVars (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MetaM Expr :=
if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.mkLambda xs e usedOnly usedLetOnly
def mkLetFVars (xs : Array Expr) (e : Expr) (usedLetOnly := true) : MetaM Expr :=
mkLambdaFVars xs e (usedLetOnly := usedLetOnly)
def mkArrow (d b : Expr) : MetaM Expr :=
return Lean.mkForall (← mkFreshUserName `x) BinderInfo.default d b
def elimMVarDeps (xs : Array Expr) (e : Expr) (preserveOrder : Bool := false) : MetaM Expr :=
if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.elimMVarDeps xs e preserveOrder
@[inline] def withConfig (f : Config → Config) : n α → n α :=
mapMetaM <| withReader (fun ctx => { ctx with config := f ctx.config })
@[inline] def withTrackingZeta (x : n α) : n α :=
withConfig (fun cfg => { cfg with trackZeta := true }) x
@[inline] def withoutProofIrrelevance (x : n α) : n α :=
withConfig (fun cfg => { cfg with proofIrrelevance := false }) x
@[inline] def withTransparency (mode : TransparencyMode) : n α → n α :=
mapMetaM <| withConfig (fun config => { config with transparency := mode })
@[inline] def withDefault (x : n α) : n α :=
withTransparency TransparencyMode.default x
@[inline] def withReducible (x : n α) : n α :=
withTransparency TransparencyMode.reducible x
@[inline] def withReducibleAndInstances (x : n α) : n α :=
withTransparency TransparencyMode.instances x
@[inline] def withAtLeastTransparency (mode : TransparencyMode) (x : n α) : n α :=
withConfig
(fun config =>
let oldMode := config.transparency
let mode := if oldMode.lt mode then mode else oldMode
{ config with transparency := mode })
x
/-- Execute `x` allowing `isDefEq` to assign synthetic opaque metavariables. -/
@[inline] def withAssignableSyntheticOpaque (x : n α) : n α :=
withConfig (fun config => { config with assignSyntheticOpaque := true }) x
/-- Save cache, execute `x`, restore cache -/
@[inline] private def savingCacheImpl (x : MetaM α) : MetaM α := do
let savedCache := (← get).cache
try x finally modify fun s => { s with cache := savedCache }
@[inline] def savingCache : n α → n α :=
mapMetaM savingCacheImpl
def getTheoremInfo (info : ConstantInfo) : MetaM (Option ConstantInfo) := do
if (← shouldReduceAll) then
return some info
else
return none
private def getDefInfoTemp (info : ConstantInfo) : MetaM (Option ConstantInfo) := do
match (← getTransparency) with
| TransparencyMode.all => return some info
| TransparencyMode.default => return some info
| _ =>
if (← isReducible info.name) then
return some info
else
return none
/- Remark: we later define `getConst?` at `GetConst.lean` after we define `Instances.lean`.
This method is only used to implement `isClassQuickConst?`.
It is very similar to `getConst?`, but it returns none when `TransparencyMode.instances` and
`constName` is an instance. This difference should be irrelevant for `isClassQuickConst?`. -/
private def getConstTemp? (constName : Name) : MetaM (Option ConstantInfo) := do
match (← getEnv).find? constName with
| some (info@(ConstantInfo.thmInfo _)) => getTheoremInfo info
| some (info@(ConstantInfo.defnInfo _)) => getDefInfoTemp info
| some info => pure (some info)
| none => throwUnknownConstant constName
private def isClassQuickConst? (constName : Name) : MetaM (LOption Name) := do
if isClass (← getEnv) constName then
pure (LOption.some constName)
else
match (← getConstTemp? constName) with
| some _ => pure LOption.undef
| none => pure LOption.none
private partial def isClassQuick? : Expr → MetaM (LOption Name)
| Expr.bvar .. => pure LOption.none
| Expr.lit .. => pure LOption.none
| Expr.fvar .. => pure LOption.none
| Expr.sort .. => pure LOption.none
| Expr.lam .. => pure LOption.none
| Expr.letE .. => pure LOption.undef
| Expr.proj .. => pure LOption.undef
| Expr.forallE _ _ b _ => isClassQuick? b
| Expr.mdata _ e _ => isClassQuick? e
| Expr.const n _ _ => isClassQuickConst? n
| Expr.mvar mvarId _ => do
match (← getExprMVarAssignment? mvarId) with
| some val => isClassQuick? val
| none => pure LOption.none
| Expr.app f _ _ =>
match f.getAppFn with
| Expr.const n .. => isClassQuickConst? n
| Expr.lam .. => pure LOption.undef
| _ => pure LOption.none
def saveAndResetSynthInstanceCache : MetaM SynthInstanceCache := do
let savedSythInstance := (← get).cache.synthInstance
modifyCache fun c => { c with synthInstance := {} }
pure savedSythInstance
def restoreSynthInstanceCache (cache : SynthInstanceCache) : MetaM Unit :=
modifyCache fun c => { c with synthInstance := cache }
@[inline] private def resettingSynthInstanceCacheImpl (x : MetaM α) : MetaM α := do
let savedSythInstance ← saveAndResetSynthInstanceCache
try x finally restoreSynthInstanceCache savedSythInstance
/-- Reset `synthInstance` cache, execute `x`, and restore cache -/
@[inline] def resettingSynthInstanceCache : n α → n α :=
mapMetaM resettingSynthInstanceCacheImpl
@[inline] def resettingSynthInstanceCacheWhen (b : Bool) (x : n α) : n α :=
if b then resettingSynthInstanceCache x else x
private def withNewLocalInstanceImp (className : Name) (fvar : Expr) (k : MetaM α) : MetaM α := do
let localDecl ← getFVarLocalDecl fvar
/- Recall that we use `auxDecl` binderInfo when compiling recursive declarations. -/
match localDecl.binderInfo with
| BinderInfo.auxDecl => k
| _ =>
resettingSynthInstanceCache <|
withReader
(fun ctx => { ctx with localInstances := ctx.localInstances.push { className := className, fvar := fvar } })
k
/-- Add entry `{ className := className, fvar := fvar }` to localInstances,
and then execute continuation `k`.
It resets the type class cache using `resettingSynthInstanceCache`. -/
def withNewLocalInstance (className : Name) (fvar : Expr) : n α → n α :=
mapMetaM <| withNewLocalInstanceImp className fvar
private def fvarsSizeLtMaxFVars (fvars : Array Expr) (maxFVars? : Option Nat) : Bool :=
match maxFVars? with
| some maxFVars => fvars.size < maxFVars
| none => true
mutual
/--
`withNewLocalInstances isClassExpensive fvars j k` updates the vector or local instances
using free variables `fvars[j] ... fvars.back`, and execute `k`.
- `isClassExpensive` is defined later.
- The type class chache is reset whenever a new local instance is found.
- `isClassExpensive` uses `whnf` which depends (indirectly) on the set of local instances.
Thus, each new local instance requires a new `resettingSynthInstanceCache`. -/
private partial def withNewLocalInstancesImp
(fvars : Array Expr) (i : Nat) (k : MetaM α) : MetaM α := do
if h : i < fvars.size then
let fvar := fvars.get ⟨i, h⟩
let decl ← getFVarLocalDecl fvar
match (← isClassQuick? decl.type) with
| LOption.none => withNewLocalInstancesImp fvars (i+1) k
| LOption.undef =>
match (← isClassExpensive? decl.type) with
| none => withNewLocalInstancesImp fvars (i+1) k
| some c => withNewLocalInstance c fvar <| withNewLocalInstancesImp fvars (i+1) k
| LOption.some c => withNewLocalInstance c fvar <| withNewLocalInstancesImp fvars (i+1) k
else
k
/--
`forallTelescopeAuxAux lctx fvars j type`
Remarks:
- `lctx` is the `MetaM` local context extended with declarations for `fvars`.
- `type` is the type we are computing the telescope for. It contains only
dangling bound variables in the range `[j, fvars.size)`
- if `reducing? == true` and `type` is not `forallE`, we use `whnf`.
- when `type` is not a `forallE` nor it can't be reduced to one, we
excute the continuation `k`.
Here is an example that demonstrates the `reducing?`.
Suppose we have
```
abbrev StateM s a := s -> Prod a s
```
Now, assume we are trying to build the telescope for
```
forall (x : Nat), StateM Int Bool
```
if `reducing == true`, the function executes `k #[(x : Nat) (s : Int)] Bool`.
if `reducing == false`, the function executes `k #[(x : Nat)] (StateM Int Bool)`
if `maxFVars?` is `some max`, then we interrupt the telescope construction
when `fvars.size == max`
-/
private partial def forallTelescopeReducingAuxAux
(reducing : Bool) (maxFVars? : Option Nat)
(type : Expr)
(k : Array Expr → Expr → MetaM α) : MetaM α := do
let rec process (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (type : Expr) : MetaM α := do
match type with
| Expr.forallE n d b c =>
if fvarsSizeLtMaxFVars fvars maxFVars? then
let d := d.instantiateRevRange j fvars.size fvars
let fvarId ← mkFreshId
let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo
let fvar := mkFVar fvarId
let fvars := fvars.push fvar
process lctx fvars j b
else
let type := type.instantiateRevRange j fvars.size fvars;
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewLocalInstancesImp fvars j do
k fvars type
| _ =>
let type := type.instantiateRevRange j fvars.size fvars;
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewLocalInstancesImp fvars j do
if reducing && fvarsSizeLtMaxFVars fvars maxFVars? then
let newType ← whnf type
if newType.isForall then
process lctx fvars fvars.size newType
else
k fvars type
else
k fvars type
process (← getLCtx) #[] 0 type
private partial def forallTelescopeReducingAux (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α := do
match maxFVars? with
| some 0 => k #[] type
| _ => do
let newType ← whnf type
if newType.isForall then
forallTelescopeReducingAuxAux true maxFVars? newType k
else
k #[] type
private partial def isClassExpensive? : Expr → MetaM (Option Name)
| type => withReducible <| -- when testing whether a type is a type class, we only unfold reducible constants.
forallTelescopeReducingAux type none fun xs type => do
let env ← getEnv
match type.getAppFn with
| Expr.const c _ _ => do
if isClass env c then
return some c
else
-- make sure abbreviations are unfolded
match (← whnf type).getAppFn with
| Expr.const c _ _ => return if isClass env c then some c else none
| _ => return none
| _ => return none
private partial def isClassImp? (type : Expr) : MetaM (Option Name) := do
match (← isClassQuick? type) with
| LOption.none => pure none
| LOption.some c => pure (some c)
| LOption.undef => isClassExpensive? type
end
def isClass? (type : Expr) : MetaM (Option Name) :=
try isClassImp? type catch _ => pure none
private def withNewLocalInstancesImpAux (fvars : Array Expr) (j : Nat) : n α → n α :=
mapMetaM <| withNewLocalInstancesImp fvars j
partial def withNewLocalInstances (fvars : Array Expr) (j : Nat) : n α → n α :=
mapMetaM <| withNewLocalInstancesImpAux fvars j
@[inline] private def forallTelescopeImp (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := do
forallTelescopeReducingAuxAux (reducing := false) (maxFVars? := none) type k
/--
Given `type` of the form `forall xs, A`, execute `k xs A`.
This combinator will declare local declarations, create free variables for them,
execute `k` with updated local context, and make sure the cache is restored after executing `k`. -/
def forallTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => forallTelescopeImp type k) k
private def forallTelescopeReducingImp (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α :=
forallTelescopeReducingAux type (maxFVars? := none) k
/--
Similar to `forallTelescope`, but given `type` of the form `forall xs, A`,
it reduces `A` and continues bulding the telescope if it is a `forall`. -/
def forallTelescopeReducing (type : Expr) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => forallTelescopeReducingImp type k) k
private def forallBoundedTelescopeImp (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α :=
forallTelescopeReducingAux type maxFVars? k
/--
Similar to `forallTelescopeReducing`, stops constructing the telescope when
it reaches size `maxFVars`. -/
def forallBoundedTelescope (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => forallBoundedTelescopeImp type maxFVars? k) k
private partial def lambdaTelescopeImp (e : Expr) (consumeLet : Bool) (k : Array Expr → Expr → MetaM α) : MetaM α := do
process consumeLet (← getLCtx) #[] 0 e
where
process (consumeLet : Bool) (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (e : Expr) : MetaM α := do
match consumeLet, e with
| _, Expr.lam n d b c =>
let d := d.instantiateRevRange j fvars.size fvars
let fvarId ← mkFreshId
let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo
let fvar := mkFVar fvarId
process consumeLet lctx (fvars.push fvar) j b
| true, Expr.letE n t v b _ => do
let t := t.instantiateRevRange j fvars.size fvars
let v := v.instantiateRevRange j fvars.size fvars
let fvarId ← mkFreshId
let lctx := lctx.mkLetDecl fvarId n t v
let fvar := mkFVar fvarId
process true lctx (fvars.push fvar) j b
| _, e =>
let e := e.instantiateRevRange j fvars.size fvars
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewLocalInstancesImp fvars j do
k fvars e
/-- Similar to `forallTelescope` but for lambda and let expressions. -/
def lambdaLetTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => lambdaTelescopeImp type true k) k
/-- Similar to `forallTelescope` but for lambda expressions. -/
def lambdaTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => lambdaTelescopeImp type false k) k
/-- Return the parameter names for the givel global declaration. -/
def getParamNames (declName : Name) : MetaM (Array Name) := do
forallTelescopeReducing (← getConstInfo declName).type fun xs _ => do
xs.mapM fun x => do
let localDecl ← getLocalDecl x.fvarId!
pure localDecl.userName
-- `kind` specifies the metavariable kind for metavariables not corresponding to instance implicit `[ ... ]` arguments.
private partial def forallMetaTelescopeReducingAux
(e : Expr) (reducing : Bool) (maxMVars? : Option Nat) (kind : MetavarKind) : MetaM (Array Expr × Array BinderInfo × Expr) :=
process #[] #[] 0 e
where
process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do
if maxMVars?.isEqSome mvars.size then
let type := type.instantiateRevRange j mvars.size mvars;
return (mvars, bis, type)
else
match type with
| Expr.forallE n d b c =>
let d := d.instantiateRevRange j mvars.size mvars
let k := if c.binderInfo.isInstImplicit then MetavarKind.synthetic else kind
let mvar ← mkFreshExprMVar d k n
let mvars := mvars.push mvar
let bis := bis.push c.binderInfo
process mvars bis j b
| _ =>
let type := type.instantiateRevRange j mvars.size mvars;
if reducing then do
let newType ← whnf type;
if newType.isForall then
process mvars bis mvars.size newType
else
return (mvars, bis, type)
else
return (mvars, bis, type)
/-- Similar to `forallTelescope`, but creates metavariables instead of free variables. -/
def forallMetaTelescope (e : Expr) (kind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) :=
forallMetaTelescopeReducingAux e (reducing := false) (maxMVars? := none) kind
/-- Similar to `forallTelescopeReducing`, but creates metavariables instead of free variables. -/
def forallMetaTelescopeReducing (e : Expr) (maxMVars? : Option Nat := none) (kind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) :=
forallMetaTelescopeReducingAux e (reducing := true) maxMVars? kind
/-- Similar to `forallMetaTelescopeReducing`, stops constructing the telescope when it reaches size `maxMVars`. -/
def forallMetaBoundedTelescope (e : Expr) (maxMVars : Nat) (kind : MetavarKind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) :=
forallMetaTelescopeReducingAux e (reducing := true) (maxMVars? := some maxMVars) (kind := kind)
/-- Similar to `forallMetaTelescopeReducingAux` but for lambda expressions. -/
partial def lambdaMetaTelescope (e : Expr) (maxMVars? : Option Nat := none) : MetaM (Array Expr × Array BinderInfo × Expr) :=
process #[] #[] 0 e
where
process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do
let finalize : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do
let type := type.instantiateRevRange j mvars.size mvars
pure (mvars, bis, type)
if maxMVars?.isEqSome mvars.size then
finalize ()
else
match type with
| Expr.lam n d b c =>
let d := d.instantiateRevRange j mvars.size mvars
let mvar ← mkFreshExprMVar d
let mvars := mvars.push mvar
let bis := bis.push c.binderInfo
process mvars bis j b
| _ => finalize ()
private def withNewFVar (fvar fvarType : Expr) (k : Expr → MetaM α) : MetaM α := do
match (← isClass? fvarType) with
| none => k fvar
| some c => withNewLocalInstance c fvar <| k fvar
private def withLocalDeclImp (n : Name) (bi : BinderInfo) (type : Expr) (k : Expr → MetaM α) : MetaM α := do
let fvarId ← mkFreshId
let ctx ← read
let lctx := ctx.lctx.mkLocalDecl fvarId n type bi
let fvar := mkFVar fvarId
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewFVar fvar type k
def withLocalDecl (name : Name) (bi : BinderInfo) (type : Expr) (k : Expr → n α) : n α :=
map1MetaM (fun k => withLocalDeclImp name bi type k) k
def withLocalDeclD (name : Name) (type : Expr) (k : Expr → n α) : n α :=
withLocalDecl name BinderInfo.default type k
partial def withLocalDecls
[Inhabited α]
(declInfos : Array (Name × BinderInfo × (Array Expr → n Expr)))
(k : (xs : Array Expr) → n α)
: n α :=
loop #[]
where
loop [Inhabited α] (acc : Array Expr) : n α := do
if acc.size < declInfos.size then
let (name, bi, typeCtor) := declInfos[acc.size]
withLocalDecl name bi (←typeCtor acc) fun x => loop (acc.push x)
else
k acc
def withLocalDeclsD [Inhabited α] (declInfos : Array (Name × (Array Expr → n Expr))) (k : (xs : Array Expr) → n α) : n α :=
withLocalDecls
(declInfos.map (fun (name, typeCtor) => (name, BinderInfo.default, typeCtor))) k
private def withNewBinderInfosImp (bs : Array (FVarId × BinderInfo)) (k : MetaM α) : MetaM α := do
let lctx := bs.foldl (init := (← getLCtx)) fun lctx (fvarId, bi) =>
lctx.setBinderInfo fvarId bi
withReader (fun ctx => { ctx with lctx := lctx }) k
def withNewBinderInfos (bs : Array (FVarId × BinderInfo)) (k : n α) : n α :=
mapMetaM (fun k => withNewBinderInfosImp bs k) k
private def withLetDeclImp (n : Name) (type : Expr) (val : Expr) (k : Expr → MetaM α) : MetaM α := do
let fvarId ← mkFreshId
let ctx ← read
let lctx := ctx.lctx.mkLetDecl fvarId n type val
let fvar := mkFVar fvarId
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewFVar fvar type k
def withLetDecl (name : Name) (type : Expr) (val : Expr) (k : Expr → n α) : n α :=
map1MetaM (fun k => withLetDeclImp name type val k) k
private def withExistingLocalDeclsImp (decls : List LocalDecl) (k : MetaM α) : MetaM α := do
let ctx ← read
let numLocalInstances := ctx.localInstances.size
let lctx := decls.foldl (fun (lctx : LocalContext) decl => lctx.addDecl decl) ctx.lctx
withReader (fun ctx => { ctx with lctx := lctx }) do
let newLocalInsts ← decls.foldlM
(fun (newlocalInsts : Array LocalInstance) (decl : LocalDecl) => (do {
match (← isClass? decl.type) with
| none => pure newlocalInsts
| some c => pure <| newlocalInsts.push { className := c, fvar := decl.toExpr } } : MetaM _))
ctx.localInstances;
if newLocalInsts.size == numLocalInstances then
k
else
resettingSynthInstanceCache <| withReader (fun ctx => { ctx with localInstances := newLocalInsts }) k
def withExistingLocalDecls (decls : List LocalDecl) : n α → n α :=
mapMetaM <| withExistingLocalDeclsImp decls
private def withNewMCtxDepthImp (x : MetaM α) : MetaM α := do
let saved ← get
modify fun s => { s with mctx := s.mctx.incDepth, postponed := {} }
try
x
finally
modify fun s => { s with mctx := saved.mctx, postponed := saved.postponed }
/--
Save cache and `MetavarContext`, bump the `MetavarContext` depth, execute `x`,
and restore saved data. -/
def withNewMCtxDepth : n α → n α :=
mapMetaM withNewMCtxDepthImp
private def withLocalContextImp (lctx : LocalContext) (localInsts : LocalInstances) (x : MetaM α) : MetaM α := do
let localInstsCurr ← getLocalInstances
withReader (fun ctx => { ctx with lctx := lctx, localInstances := localInsts }) do
if localInsts == localInstsCurr then
x
else
resettingSynthInstanceCache x
def withLCtx (lctx : LocalContext) (localInsts : LocalInstances) : n α → n α :=
mapMetaM <| withLocalContextImp lctx localInsts
private def withMVarContextImp (mvarId : MVarId) (x : MetaM α) : MetaM α := do
let mvarDecl ← getMVarDecl mvarId
withLocalContextImp mvarDecl.lctx mvarDecl.localInstances x
/--
Execute `x` using the given metavariable `LocalContext` and `LocalInstances`.
The type class resolution cache is flushed when executing `x` if its `LocalInstances` are
different from the current ones. -/
def withMVarContext (mvarId : MVarId) : n α → n α :=
mapMetaM <| withMVarContextImp mvarId
private def withMCtxImp (mctx : MetavarContext) (x : MetaM α) : MetaM α := do
let mctx' ← getMCtx
setMCtx mctx
try x finally setMCtx mctx'
def withMCtx (mctx : MetavarContext) : n α → n α :=
mapMetaM <| withMCtxImp mctx
@[inline] private def approxDefEqImp (x : MetaM α) : MetaM α :=
withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true}) x
/-- Execute `x` using approximate unification: `foApprox`, `ctxApprox` and `quasiPatternApprox`. -/
@[inline] def approxDefEq : n α → n α :=
mapMetaM approxDefEqImp
@[inline] private def fullApproxDefEqImp (x : MetaM α) : MetaM α :=
withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true, constApprox := true }) x
/--
Similar to `approxDefEq`, but uses all available approximations.
We don't use `constApprox` by default at `approxDefEq` because it often produces undesirable solution for monadic code.
For example, suppose we have `pure (x > 0)` which has type `?m Prop`. We also have the goal `[Pure ?m]`.
Now, assume the expected type is `IO Bool`. Then, the unification constraint `?m Prop =?= IO Bool` could be solved
as `?m := fun _ => IO Bool` using `constApprox`, but this spurious solution would generate a failure when we try to
solve `[Pure (fun _ => IO Bool)]` -/
@[inline] def fullApproxDefEq : n α → n α :=
mapMetaM fullApproxDefEqImp
def normalizeLevel (u : Level) : MetaM Level := do
let u ← instantiateLevelMVars u
pure u.normalize
def assignLevelMVar (mvarId : MVarId) (u : Level) : MetaM Unit := do
modifyMCtx fun mctx => mctx.assignLevel mvarId u
def whnfR (e : Expr) : MetaM Expr :=
withTransparency TransparencyMode.reducible <| whnf e
def whnfD (e : Expr) : MetaM Expr :=
withTransparency TransparencyMode.default <| whnf e
def whnfI (e : Expr) : MetaM Expr :=
withTransparency TransparencyMode.instances <| whnf e
def setInlineAttribute (declName : Name) (kind := Compiler.InlineAttributeKind.inline): MetaM Unit := do
let env ← getEnv
match Compiler.setInlineAttribute env declName kind with
| Except.ok env => setEnv env
| Except.error msg => throwError msg
private partial def instantiateForallAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do
if h : i < ps.size then
let p := ps.get ⟨i, h⟩
match (← whnf e) with
| Expr.forallE _ _ b _ => instantiateForallAux ps (i+1) (b.instantiate1 p)
| _ => throwError "invalid instantiateForall, too many parameters"
else
pure e
/- Given `e` of the form `forall (a_1 : A_1) ... (a_n : A_n), B[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `B[p_1, ..., p_n]`. -/
def instantiateForall (e : Expr) (ps : Array Expr) : MetaM Expr :=
instantiateForallAux ps 0 e
private partial def instantiateLambdaAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do
if h : i < ps.size then
let p := ps.get ⟨i, h⟩
match (← whnf e) with
| Expr.lam _ _ b _ => instantiateLambdaAux ps (i+1) (b.instantiate1 p)
| _ => throwError "invalid instantiateLambda, too many parameters"
else
pure e
/- Given `e` of the form `fun (a_1 : A_1) ... (a_n : A_n) => t[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `t[p_1, ..., p_n]`.
It uses `whnf` to reduce `e` if it is not a lambda -/
def instantiateLambda (e : Expr) (ps : Array Expr) : MetaM Expr :=
instantiateLambdaAux ps 0 e
/-- Return true iff `e` depends on the free variable `fvarId` -/
def dependsOn (e : Expr) (fvarId : FVarId) : MetaM Bool :=
return (← getMCtx).exprDependsOn e fvarId
def ppExpr (e : Expr) : MetaM Format := do
let ctxCore ← readThe Core.Context
Lean.ppExpr { env := (← getEnv), mctx := (← getMCtx), lctx := (← getLCtx), opts := (← getOptions), currNamespace := ctxCore.currNamespace, openDecls := ctxCore.openDecls } e
@[inline] protected def orelse (x y : MetaM α) : MetaM α := do
let env ← getEnv
let mctx ← getMCtx
try x catch _ => setEnv env; setMCtx mctx; y
instance : OrElse (MetaM α) := ⟨Meta.orelse⟩
@[inline] private def orelseMergeErrorsImp (x y : MetaM α)
(mergeRef : Syntax → Syntax → Syntax := fun r₁ r₂ => r₁)
(mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ m₂) : MetaM α := do
let env ← getEnv
let mctx ← getMCtx
try
x
catch ex =>
setEnv env
setMCtx mctx
match ex with
| Exception.error ref₁ m₁ =>
try
y
catch
| Exception.error ref₂ m₂ => throw <| Exception.error (mergeRef ref₁ ref₂) (mergeMsg m₁ m₂)
| ex => throw ex
| ex => throw ex
/--
Similar to `orelse`, but merge errors. Note that internal errors are not caught.
The default `mergeRef` uses the `ref` (position information) for the first message.
The default `mergeMsg` combines error messages using `Format.line ++ Format.line` as a separator. -/
@[inline] def orelseMergeErrors [MonadControlT MetaM m] [Monad m] (x y : m α)
(mergeRef : Syntax → Syntax → Syntax := fun r₁ r₂ => r₁)
(mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ Format.line ++ m₂) : m α := do
controlAt MetaM fun runInBase => orelseMergeErrorsImp (runInBase x) (runInBase y) mergeRef mergeMsg
/-- Execute `x`, and apply `f` to the produced error message -/
def mapErrorImp (x : MetaM α) (f : MessageData → MessageData) : MetaM α := do
try
x
catch
| Exception.error ref msg => throw <| Exception.error ref <| f msg
| ex => throw ex
@[inline] def mapError [MonadControlT MetaM m] [Monad m] (x : m α) (f : MessageData → MessageData) : m α :=
controlAt MetaM fun runInBase => mapErrorImp (runInBase x) f
end Methods
end Meta
export Meta (MetaM)
end Lean
|
4b5d1ecadff5218251ac70bf41cf1cc5a413589e | 46125763b4dbf50619e8846a1371029346f4c3db | /src/algebra/direct_limit.lean | b83ee758a0387673fc6c1e84823ffaa59cbb8305 | [
"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 | 24,984 | 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 lattice submodule
variables {R : Type u} [ring R]
variables {ι : Type v} [nonempty ι]
variables [directed_order ι] [decidable_eq ι]
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, is_add_group_hom.to_linear_map $ f i j hij)
⟨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, is_add_group_hom.to_linear_map $ f i j hij) :=
⟨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, is_add_group_hom.to_linear_map $ f i j hij)
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, is_add_group_hom.to_linear_map $ f i j hij) 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, is_add_group_hom.to_linear_map $ f i j hij)
(λ i, is_add_group_hom.to_linear_map $ g i) 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 j hij x, by 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. -/
def lift : direct_limit G f → P :=
ideal.quotient.lift _ (free_comm_ring.lift $ λ x, g x.1 x.2) begin
suffices : ideal.span _ ≤
ideal.comap (free_comm_ring.lift (λ (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 [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
variables {G f}
omit Hg
instance lift.is_ring_hom : is_ring_hom (lift G f P g Hg) :=
⟨free_comm_ring.lift_one _,
λ x y, quotient.induction_on₂' x y $ λ p q, free_comm_ring.lift_mul _ _ _,
λ x y, quotient.induction_on₂' x y $ λ p q, free_comm_ring.lift_add _ _ _⟩
@[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 := is_ring_hom.map_zero _
@[simp] lemma lift_one : lift G f P g Hg 1 = 1 := is_ring_hom.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 := is_ring_hom.map_add _
@[simp] lemma lift_neg (x) : lift G f P g Hg (-x) = -lift G f P g Hg x := is_ring_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_ring_hom.map_sub _
@[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 := is_ring_hom.map_mul _
@[simp] lemma lift_pow (x) (n : ℕ) : lift G f P g Hg (x ^ n) = lift G f P g Hg x ^ n := is_semiring_hom.map_pow _ _ _
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
|
c4a6a5fba336327516e1be1b7a242a83bedb83e5 | 56af0912bd25910f5caae91d6dd0603b0c032989 | /kb_solutions/field.lean | 2fe04cf722a5afa60c17d3eaa7efd30ab4c0581f | [
"Apache-2.0"
] | permissive | isabella232/complex-number-game | ae36e0b1df9761d9df07049ca29c91ae44dbdc2d | 3d767f14041f9002e435bed3a3527fdd297c166d | refs/heads/master | 1,679,305,953,116 | 1,606,397,567,000 | 1,606,397,567,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,298 | lean | /-
Copyright (c) 2020 The Xena project. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard.
Thanks: Imperial College London, leanprover-community
-/
import complex.kb_solutions.Level_04_norm_sq -- solutions to levels 1 to 4
noncomputable theory
namespace complex
-- Define the inverse of a complex number
/-- The inverse of a complex number-/
noncomputable def inv (z : ℂ) : ℂ :=
⟨re(z)/norm_sq(z), -im(z)/norm_sq(z)⟩
instance : has_inv ℂ := ⟨inv⟩
@[simp] lemma inv_re (z : ℂ) :
re(z⁻¹) = re(z)/norm_sq(z) := rfl
@[simp] lemma inv_im (z : ℂ) :
im(z⁻¹) = -im(z)/norm_sq(z) := rfl
/-- The complex numbers are a field -/
instance : field ℂ :=
{ inv := has_inv.inv,
inv_zero := begin ext; simp end,
zero_ne_one := begin
intro h,
rw ext_iff at h,
cases h with hr hi,
change (0 : ℝ) = 1 at hr,
linarith,
end,
mul_inv_cancel := begin
intros z hz,
-- why is everything in such a weird state?
change z ≠ 0 at hz,
change z * z⁻¹ = 1,
-- that's better
rw ←norm_sq_pos at hz,
have h : z.norm_sq ≠ 0,
linarith,
-- finally ready
ext;
simp [norm_sq] at *;
field_simp [h];
ring
end,
..complex.comm_ring }
end complex |
8ba19ced04f0176c04518a4cb717c0f9d1ac1d89 | 957a80ea22c5abb4f4670b250d55534d9db99108 | /tests/lean/run/smt_ematch_alg_issue.lean | e549ca7c4dee51cfd7734ffa0655fb47dfb46f54 | [
"Apache-2.0"
] | permissive | GaloisInc/lean | aa1e64d604051e602fcf4610061314b9a37ab8cd | f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0 | refs/heads/master | 1,592,202,909,807 | 1,504,624,387,000 | 1,504,624,387,000 | 75,319,626 | 2 | 1 | Apache-2.0 | 1,539,290,164,000 | 1,480,616,104,000 | C++ | UTF-8 | Lean | false | false | 294 | lean | lemma {u} ring_add_comm {α : Type u} [ring α] : ∀ (a b : α), (: a + b :) = b + a :=
add_comm
open smt_tactic
meta def no_ac : smt_config :=
{ cc_cfg := { ac := ff }}
lemma ex {α : Type} [field α] (a b : α) : a + b = b + a :=
begin [smt] with no_ac,
ematch_using [ring_add_comm]
end
|
12347257b63f717140647eec7e6249e43dc6a1ca | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/measure_theory/tactic.lean | 55bccc36e39fb6ddaa6d090d6205059f69efaf67 | [
"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 | 6,709 | lean | /-
Copyright (c) 2021 Rémy Degenne. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Rémy Degenne
-/
import measure_theory.measure.measure_space_def
import tactic.auto_cases
import tactic.tidy
import tactic.with_local_reducibility
/-!
# Tactics for measure theory
Currently we have one domain-specific tactic for measure theory: `measurability`.
This tactic is to a large extent a copy of the `continuity` tactic by Reid Barton.
-/
/-!
### `measurability` tactic
Automatically solve goals of the form `measurable f`, `ae_measurable f μ` and `measurable_set s`.
Mark lemmas with `@[measurability]` to add them to the set of lemmas
used by `measurability`. Note: `to_additive` doesn't know yet how to
copy the attribute to the additive version.
-/
/-- User attribute used to mark tactics used by `measurability`. -/
@[user_attribute]
meta def measurability : user_attribute :=
{ name := `measurability,
descr := "lemmas usable to prove (ae)-measurability" }
/- Mark some measurability lemmas already defined in `measure_theory.measurable_space_def` and
`measure_theory.measure_space_def` -/
attribute [measurability]
measurable_id
measurable_id'
ae_measurable_id
ae_measurable_id'
measurable_const
ae_measurable_const
ae_measurable.measurable_mk
measurable_set.empty
measurable_set.univ
measurable_set.compl
subsingleton.measurable_set
measurable_set.Union
measurable_set.Inter
measurable_set.union
measurable_set.inter
measurable_set.diff
measurable_set.symm_diff
measurable_set.ite
measurable_set.cond
measurable_set.disjointed
measurable_set.const
measurable_set.insert
measurable_set_eq
finset.measurable_set
measurable_space.measurable_set_top
namespace tactic
/--
Tactic to apply `measurable.comp` when appropriate.
Applying `measurable.comp` is not always a good idea, so we have some
extra logic here to try to avoid bad cases.
* If the function we're trying to prove measurable is actually
constant, and that constant is a function application `f z`, then
measurable.comp would produce new goals `measurable f`, `measurable
(λ _, z)`, which is silly. We avoid this by failing if we could
apply `measurable_const`.
* measurable.comp will always succeed on `measurable (λ x, f x)` and
produce new goals `measurable (λ x, x)`, `measurable f`. We detect
this by failing if a new goal can be closed by applying
measurable_id.
-/
meta def apply_measurable.comp : tactic unit :=
`[fail_if_success { exact measurable_const };
refine measurable.comp _ _;
fail_if_success { exact measurable_id }]
/--
Tactic to apply `measurable.comp_ae_measurable` when appropriate.
Applying `measurable.comp_ae_measurable` is not always a good idea, so we have some
extra logic here to try to avoid bad cases.
* If the function we're trying to prove measurable is actually
constant, and that constant is a function application `f z`, then
`measurable.comp_ae_measurable` would produce new goals `measurable f`, `ae_measurable
(λ _, z) μ`, which is silly. We avoid this by failing if we could
apply `ae_measurable_const`.
* `measurable.comp_ae_measurable` will always succeed on `ae_measurable (λ x, f x) μ` and
can produce new goals (`measurable (λ x, x)`, `ae_measurable f μ`) or
(`measurable f`, `ae_measurable (λ x, x) μ`). We detect those by failing if a new goal can be
closed by applying `measurable_id` or `ae_measurable_id`.
-/
meta def apply_measurable.comp_ae_measurable : tactic unit :=
`[fail_if_success { exact ae_measurable_const };
refine measurable.comp_ae_measurable _ _;
fail_if_success { exact measurable_id };
fail_if_success { exact ae_measurable_id }]
/--
We don't want the intro1 tactic to apply to a goal of the form `measurable f`, `ae_measurable f μ`
or `measurable_set s`. This tactic tests the target to see if it matches that form.
-/
meta def goal_is_not_measurable : tactic unit :=
do t ← tactic.target,
match t with
| `(measurable %%l) := failed
| `(ae_measurable %%l %%r) := failed
| `(measurable_set %%l) := failed
| _ := skip
end
/-- List of tactics used by `measurability` internally. The option `use_exfalso := ff` is passed to
the tactic `apply_assumption` in order to avoid loops in the presence of negated hypotheses in
the context. -/
meta def measurability_tactics (md : transparency := semireducible) : list (tactic string) :=
[
propositional_goal >> tactic.interactive.apply_assumption none {use_exfalso := ff}
>> pure "apply_assumption {use_exfalso := ff}",
goal_is_not_measurable >> intro1
>>= λ ns, pure ("intro " ++ ns.to_string),
apply_rules [] [``measurability] 50 { md := md }
>> pure "apply_rules with measurability",
apply_measurable.comp >> pure "refine measurable.comp _ _",
apply_measurable.comp_ae_measurable
>> pure "refine measurable.comp_ae_measurable _ _",
`[ refine measurable.ae_measurable _ ]
>> pure "refine measurable.ae_measurable _",
`[ refine measurable.ae_strongly_measurable _ ]
>> pure "refine measurable.ae_strongly_measurable _"
]
namespace interactive
setup_tactic_parser
/--
Solve goals of the form `measurable f`, `ae_measurable f μ`, `ae_strongly_measurable f μ` or
`measurable_set s`. `measurability?` reports back the proof term it found.
-/
meta def measurability
(bang : parse $ optional (tk "!")) (trace : parse $ optional (tk "?")) (cfg : tidy.cfg := {}) :
tactic unit :=
let md := if bang.is_some then semireducible else reducible,
measurability_core := tactic.tidy { tactics := measurability_tactics md, ..cfg },
trace_fn := if trace.is_some then show_term else id in
trace_fn measurability_core
/-- Version of `measurability` for use with auto_param. -/
meta def measurability' : tactic unit := measurability none none {}
/--
`measurability` solves goals of the form `measurable f`, `ae_measurable f μ`,
`ae_strongly_measurable f μ` or `measurable_set s` by applying lemmas tagged with the
`measurability` user attribute.
You can also use `measurability!`, which applies lemmas with `{ md := semireducible }`.
The default behaviour is more conservative, and only unfolds `reducible` definitions
when attempting to match lemmas with the goal.
`measurability?` reports back the proof term it found.
-/
add_tactic_doc
{ name := "measurability / measurability'",
category := doc_category.tactic,
decl_names := [`tactic.interactive.measurability, `tactic.interactive.measurability'],
tags := ["lemma application"] }
end interactive
end tactic
|
1723bd88d949520e9fc4d6a5d1df7ae2692fee06 | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/logic/embedding.lean | e50a21c07f34369b0c21570c98bca1f09941695e | [
"Apache-2.0"
] | permissive | kmill/mathlib | ea5a007b67ae4e9e18dd50d31d8aa60f650425ee | 1a419a9fea7b959317eddd556e1bb9639f4dcc05 | refs/heads/master | 1,668,578,197,719 | 1,593,629,163,000 | 1,593,629,163,000 | 276,482,939 | 0 | 0 | null | 1,593,637,960,000 | 1,593,637,959,000 | null | UTF-8 | Lean | false | false | 9,772 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import data.equiv.basic
/-!
# Injective functions
-/
universes u v w x
namespace function
/-- `α ↪ β` is a bundled injective function. -/
structure embedding (α : Sort*) (β : Sort*) :=
(to_fun : α → β)
(inj' : injective to_fun)
infixr ` ↪ `:25 := embedding
instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩
end function
/-- Convert an `α ≃ β` to `α ↪ β`. -/
protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β :=
⟨f, f.injective⟩
@[simp] theorem equiv.to_embedding_coe_fn {α : Sort u} {β : Sort v} (f : α ≃ β) :
(f.to_embedding : α → β) = f := rfl
namespace function
namespace embedding
@[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g :=
by cases f; cases g; simpa using funext h
lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g :=
⟨ext, λ h _, by rw h⟩
@[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl
@[simp] theorem coe_fn_mk {α β} (f : α → β) (i) :
(@mk _ _ f i : α → β) = f := rfl
theorem injective {α β} (f : α ↪ β) : injective f := f.inj'
@[refl] protected def refl (α : Sort*) : α ↪ α :=
⟨id, injective_id⟩
@[trans] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ :=
⟨g ∘ f, g.injective.comp f.injective⟩
@[simp] theorem refl_apply {α} (x : α) : embedding.refl α x = x := rfl
@[simp] theorem trans_apply {α β γ} (f : α ↪ β) (g : β ↪ γ) (a : α) :
(f.trans g) a = g (f a) := rfl
@[simp]
lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) :
function.embedding.trans (e.to_embedding) (e.symm.to_embedding) = function.embedding.refl _ :=
by { ext, simp, }
@[simp]
lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) :
function.embedding.trans (e.symm.to_embedding) (e.to_embedding) = function.embedding.refl _ :=
by { ext, simp, }
protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x}
(e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) :=
(equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding)
/-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/
protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) :
α ↪ β :=
⟨surj_inv hf, injective_surj_inv _⟩
/-- Convert a surjective `embedding` to an `equiv` -/
protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) :
α ≃ β :=
equiv.of_bijective f ⟨f.injective, hf⟩
protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β :=
⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩
/-- Change the value of an embedding `f` at one point. If the prescribed image
is already occupied by some `f a'`, then swap the values at these two points. -/
def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)]
[∀ a', decidable (f a' = b)] : α ↪ β :=
⟨λ a', if a' = a then b else if f a' = b then f a else f a',
begin
intros x y h,
dsimp at h,
split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc
end⟩
theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)]
[∀ a', decidable (f a' = b)] : set_value f a b a = b :=
by simp [set_value]
/-- Embedding into `option` -/
protected def some {α} : α ↪ option α :=
⟨some, option.some_injective α⟩
/-- Embedding of a `subtype`. -/
def subtype {α} (p : α → Prop) : subtype p ↪ α :=
⟨subtype.val, λ _ _, subtype.ext_val⟩
/-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/
def punit {β : Sort*} (b : β) : punit ↪ β :=
⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩
/-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/
def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β :=
⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩
/-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/
def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β :=
⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩
/-- Restrict the codomain of an embedding. -/
def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p :=
⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩
@[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) :
cod_restrict p f H a = ⟨f a, H a⟩ := rfl
def prod_congr {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ :=
⟨assume ⟨a, b⟩, (e₁ a, e₂ b),
assume ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h,
have a₁ = a₂ ∧ b₁ = b₂, from
(prod.mk.inj h).imp (assume h, e₁.injective h) (assume h, e₂.injective h),
this.left ▸ this.right ▸ rfl⟩
section sum
open sum
def sum_congr {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ :=
⟨assume s, match s with inl a := inl (e₁ a) | inr b := inr (e₂ b) end,
assume s₁ s₂ h, match s₁, s₂, h with
| inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h
| inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h
end⟩
@[simp] theorem sum_congr_apply_inl {α β γ δ}
(e₁ : α ↪ β) (e₂ : γ ↪ δ) (a) : sum_congr e₁ e₂ (inl a) = inl (e₁ a) := rfl
@[simp] theorem sum_congr_apply_inr {α β γ δ}
(e₁ : α ↪ β) (e₂ : γ ↪ δ) (b) : sum_congr e₁ e₂ (inr b) = inr (e₂ b) := rfl
/-- The embedding of `α` into the sum `α ⊕ β`. -/
def inl {α β : Type*} : α ↪ α ⊕ β :=
⟨sum.inl, λ a b, sum.inl.inj⟩
/-- The embedding of `β` into the sum `α ⊕ β`. -/
def inr {α β : Type*} : β ↪ α ⊕ β :=
⟨sum.inr, λ a b, sum.inr.inj⟩
end sum
section sigma
open sigma
def sigma_congr_right {α : Type*} {β γ : α → Type*} (e : ∀ a, β a ↪ γ a) : sigma β ↪ sigma γ :=
⟨λ ⟨a, b⟩, ⟨a, e a b⟩, λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, begin
injection h with h₁ h₂, subst a₂,
congr,
exact (e a₁).2 (eq_of_heq h₂)
end⟩
end sigma
def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) :=
⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩
def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w}
(e : α ↪ β) : (γ → α) ↪ (γ → β) :=
Pi_congr_right (λ _, e)
noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ]
(e : α ↪ β) : (α → γ) ↪ (β → γ) :=
by haveI := classical.prop_decidable; exact
let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in
⟨f', assume f₁ f₂ h, funext $ assume c,
have ∃c', e c' = e c, from ⟨c, rfl⟩,
have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _,
have eq_b : classical.some this = c, from e.injective $ classical.some_spec this,
by simp [f', this, if_pos, eq_b] at eq'; assumption⟩
protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β)
(h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} :=
⟨subtype.map f h, subtype.map_injective h f.2⟩
open set
/-- `set.image` as an embedding `set α ↪ set β`. -/
protected def image {α β} (f : α ↪ β) : set α ↪ set β :=
⟨image f, f.2.image_injective⟩
@[simp] lemma coe_image {α β} (f : α ↪ β) : ⇑f.image = image f := rfl
end embedding
end function
namespace equiv
@[simp]
lemma refl_to_embedding {α : Type*} :
(equiv.refl α).to_embedding = function.embedding.refl α := rfl
@[simp]
lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) :
(e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl
end equiv
namespace set
/-- The injection map is an embedding between subsets. -/
def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t :=
⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by congr; injection h⟩
@[simp] lemma embedding_of_subset_apply_mk {α} {s t : set α} (h : s ⊆ t) (x : α) (hx : x ∈ s) :
embedding_of_subset s t h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl
@[simp] lemma coe_embedding_of_subset_apply {α} {s t : set α} (h : s ⊆ t) (x : s) :
(embedding_of_subset s t h x : α) = x := rfl
end set
/--
The embedding of a left cancellative semigroup into itself
by left multiplication by a fixed element.
-/
@[to_additive
"The embedding of a left cancellative additive semigroup into itself
by left translation by a fixed element."]
def mul_left_embedding {G : Type u} [left_cancel_semigroup G] (g : G) : G ↪ G :=
{ to_fun := λ h, g * h,
inj' := λ h h', (mul_right_inj g).mp, }
@[simp]
lemma mul_left_embedding_apply {G : Type u} [left_cancel_semigroup G] (g h : G) :
mul_left_embedding g h = g * h :=
rfl
/--
The embedding of a right cancellative semigroup into itself
by right multiplication by a fixed element.
-/
@[to_additive
"The embedding of a right cancellative additive semigroup into itself
by right translation by a fixed element."]
def mul_right_embedding {G : Type u} [right_cancel_semigroup G] (g : G) : G ↪ G :=
{ to_fun := λ h, h * g,
inj' := λ h h', (mul_left_inj g).mp, }
@[simp]
lemma mul_right_embedding_apply {G : Type u} [right_cancel_semigroup G] (g h : G) :
mul_right_embedding g h = h * g :=
rfl
|
4ea6f245f76fc2b58deb63f458938cc10a4f93db | 05b503addd423dd68145d68b8cde5cd595d74365 | /src/algebra/category/Mon/basic.lean | 869717a2415fe3183e5abe820c168878f6a62e85 | [
"Apache-2.0"
] | permissive | aestriplex/mathlib | 77513ff2b176d74a3bec114f33b519069788811d | e2fa8b2b1b732d7c25119229e3cdfba8370cb00f | refs/heads/master | 1,621,969,960,692 | 1,586,279,279,000 | 1,586,279,279,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,851 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.concrete_category
import algebra.group.hom
import data.equiv.mul_add
import algebra.punit_instances
/-!
# Category instances for monoid, add_monoid, comm_monoid, and add_comm_monoid.
We introduce the bundled categories:
* `Mon`
* `AddMon`
* `CommMon`
* `AddCommMon`
along with the relevant forgetful functors between them.
## Implementation notes
See the note [locally reducible category instances]
and the note [reducible has_coe_to_sort instances for bundled categories].
-/
/--
We make SemiRing (and the other categories) locally reducible in order
to define its instances. This is because writing, for example,
```
instance : concrete_category SemiRing := by { delta SemiRing, apply_instance }
```
results in an instance of the form `id (bundled_hom.concrete_category _)`
and this `id`, not being [reducible], prevents a later instance search
(once SemiRing is no longer reducible) from seeing that the morphisms of
SemiRing are really semiring morphisms (`→+*`), and therefore have a coercion
to functions, for example. It's especially important that the `has_coe_to_sort`
instance not contain an extra `id` as we want the `semiring ↥R` instance to
also apply to `semiring R.α` (it seems to be impractical to guarantee that
we always access `R.α` through the coercion rather than directly).
TODO: Probably @[derive] should be able to create instances of the
required form (without `id`), and then we could use that instead of
this obscure `local attribute [reducible]` method.
See also note [reducible has_coe_to_sort instances for bundled categories],
explaining why the `has_coe_to_sort` instances themselves must be `[reducible]`.
-/
library_note "locally reducible category instances"
universes u v
open category_theory
/-- The category of monoids and monoid morphisms. -/
@[to_additive AddMon]
def Mon : Type (u+1) := bundled monoid
namespace Mon
/-- Construct a bundled Mon from the underlying type and typeclass. -/
@[to_additive]
def of (M : Type u) [monoid M] : Mon := bundled.of M
@[to_additive]
instance : inhabited Mon :=
-- The default instance for `monoid punit` is derived via `punit.comm_ring`,
-- which breaks to_additive.
⟨@of punit $ @group.to_monoid _ $ @comm_group.to_group _ punit.comm_group⟩
local attribute [reducible] Mon
/--
`has_coe_to_sort` instances for bundled categories must be `[reducible]`,
see note [reducible has_coe_to_sort instances for bundled categories].
-/
@[reducible, to_additive]
instance : has_coe_to_sort Mon := infer_instance -- short-circuit type class inference
@[to_additive add_monoid]
instance (M : Mon) : monoid M := M.str
@[to_additive]
instance bundled_hom : bundled_hom @monoid_hom :=
⟨@monoid_hom.to_fun, @monoid_hom.id, @monoid_hom.comp, @monoid_hom.coe_inj⟩
@[to_additive]
instance : category Mon := infer_instance -- short-circuit type class inference
@[to_additive]
instance : concrete_category Mon := infer_instance -- short-circuit type class inference
end Mon
/-- The category of commutative monoids and monoid morphisms. -/
@[to_additive AddCommMon]
def CommMon : Type (u+1) := induced_category Mon (bundled.map @comm_monoid.to_monoid)
namespace CommMon
/-- Construct a bundled CommMon from the underlying type and typeclass. -/
@[to_additive]
def of (M : Type u) [comm_monoid M] : CommMon := bundled.of M
@[to_additive]
instance : inhabited CommMon :=
-- The default instance for `comm_monoid punit` is derived via `punit.comm_ring`,
-- which breaks to_additive.
⟨@of punit $ @comm_group.to_comm_monoid _ punit.comm_group⟩
local attribute [reducible] CommMon
/--
`has_coe_to_sort` instances for bundled categories must be `[reducible]`,
see note [reducible has_coe_to_sort instances for bundled categories].
-/
@[reducible, to_additive]
instance : has_coe_to_sort CommMon := infer_instance -- short-circuit type class inference
@[to_additive add_comm_monoid]
instance (M : CommMon) : comm_monoid M := M.str
@[to_additive]
instance : category CommMon := infer_instance -- short-circuit type class inference
@[to_additive]
instance : concrete_category CommMon := infer_instance -- short-circuit type class inference
@[to_additive has_forget_to_AddMon]
instance has_forget_to_Mon : has_forget₂ CommMon Mon := infer_instance -- short-circuit type class inference
end CommMon
-- We verify that the coercions of morphisms to functions work correctly:
example {R S : Mon} (f : R ⟶ S) : (R : Type) → (S : Type) := f
example {R S : CommMon} (f : R ⟶ S) : (R : Type) → (S : Type) := f
variables {X Y : Type u}
section
variables [monoid X] [monoid Y]
/-- Build an isomorphism in the category `Mon` from a `mul_equiv` between `monoid`s. -/
@[to_additive add_equiv.to_AddMon_iso "Build an isomorphism in the category `AddMon` from a `add_equiv` between `add_monoid`s."]
def mul_equiv.to_Mon_iso (e : X ≃* Y) : Mon.of X ≅ Mon.of Y :=
{ hom := e.to_monoid_hom,
inv := e.symm.to_monoid_hom }
@[simp, to_additive add_equiv.to_AddMon_iso_hom]
lemma mul_equiv.to_Mon_iso_hom {e : X ≃* Y} : e.to_Mon_iso.hom = e.to_monoid_hom := rfl
@[simp, to_additive add_equiv.to_AddMon_iso_inv]
lemma mul_equiv.to_Mon_iso_inv {e : X ≃* Y} : e.to_Mon_iso.inv = e.symm.to_monoid_hom := rfl
end
section
variables [comm_monoid X] [comm_monoid Y]
/-- Build an isomorphism in the category `CommMon` from a `mul_equiv` between `comm_monoid`s. -/
@[to_additive add_equiv.to_AddCommMon_iso "Build an isomorphism in the category `AddCommMon` from a `add_equiv` between `add_comm_monoid`s."]
def mul_equiv.to_CommMon_iso (e : X ≃* Y) : CommMon.of X ≅ CommMon.of Y :=
{ hom := e.to_monoid_hom,
inv := e.symm.to_monoid_hom }
@[simp, to_additive add_equiv.to_AddCommMon_iso_hom]
lemma mul_equiv.to_CommMon_iso_hom {e : X ≃* Y} : e.to_CommMon_iso.hom = e.to_monoid_hom := rfl
@[simp, to_additive add_equiv.to_AddCommMon_iso_inv]
lemma mul_equiv.to_CommMon_iso_inv {e : X ≃* Y} : e.to_CommMon_iso.inv = e.symm.to_monoid_hom := rfl
end
namespace category_theory.iso
/-- Build a `mul_equiv` from an isomorphism in the category `Mon`. -/
@[to_additive AddMond_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddMon`."]
def Mon_iso_to_mul_equiv {X Y : Mon.{u}} (i : X ≅ Y) : X ≃* Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
map_mul' := by tidy }.
/-- Build a `mul_equiv` from an isomorphism in the category `CommMon`. -/
@[to_additive AddCommMon_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddCommMon`."]
def CommMon_iso_to_mul_equiv {X Y : CommMon.{u}} (i : X ≅ Y) : X ≃* Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
map_mul' := by tidy }.
end category_theory.iso
/-- multiplicative equivalences between `monoid`s are the same as (isomorphic to) isomorphisms in `Mon` -/
@[to_additive add_equiv_iso_AddMon_iso "additive equivalences between `add_monoid`s are the same as (isomorphic to) isomorphisms in `AddMon`"]
def mul_equiv_iso_Mon_iso {X Y : Type u} [monoid X] [monoid Y] :
(X ≃* Y) ≅ (Mon.of X ≅ Mon.of Y) :=
{ hom := λ e, e.to_Mon_iso,
inv := λ i, i.Mon_iso_to_mul_equiv, }
/-- multiplicative equivalences between `comm_monoid`s are the same as (isomorphic to) isomorphisms in `CommMon` -/
@[to_additive add_equiv_iso_AddCommMon_iso "additive equivalences between `add_comm_monoid`s are the same as (isomorphic to) isomorphisms in `AddCommMon`"]
def mul_equiv_iso_CommMon_iso {X Y : Type u} [comm_monoid X] [comm_monoid Y] :
(X ≃* Y) ≅ (CommMon.of X ≅ CommMon.of Y) :=
{ hom := λ e, e.to_CommMon_iso,
inv := λ i, i.CommMon_iso_to_mul_equiv, }
|
d59fcea26c3cab61e4ba414047463ccedc9d25ee | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/bicategory/strict.lean | 692dd0374e06c5f9404d2abfd1d1bc017f66bcb0 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,146 | lean | /-
Copyright (c) 2022 Yuma Mizuno. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yuma Mizuno
-/
import category_theory.eq_to_hom
import category_theory.bicategory.basic
/-!
# Strict bicategories
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
A bicategory is called `strict` if the left unitors, the right unitors, and the associators are
isomorphisms given by equalities.
## Implementation notes
In the literature of category theory, a strict bicategory (usually called a strict 2-category) is
often defined as a bicategory whose left unitors, right unitors, and associators are identities.
We cannot use this definition directly here since the types of 2-morphisms depend on 1-morphisms.
For this reason, we use `eq_to_iso`, which gives isomorphisms from equalities, instead of
identities.
-/
namespace category_theory
open_locale bicategory
universes w v u
variables (B : Type u) [bicategory.{w v} B]
/--
A bicategory is called `strict` if the left unitors, the right unitors, and the associators are
isomorphisms given by equalities.
-/
class bicategory.strict : Prop :=
(id_comp' : ∀ {a b : B} (f : a ⟶ b), 𝟙 a ≫ f = f . obviously)
(comp_id' : ∀ {a b : B} (f : a ⟶ b), f ≫ 𝟙 b = f . obviously)
(assoc' : ∀ {a b c d : B} (f : a ⟶ b) (g : b ⟶ c) (h : c ⟶ d),
(f ≫ g) ≫ h = f ≫ (g ≫ h) . obviously)
(left_unitor_eq_to_iso' : ∀ {a b : B} (f : a ⟶ b),
λ_ f = eq_to_iso (id_comp' f) . obviously)
(right_unitor_eq_to_iso' : ∀ {a b : B} (f : a ⟶ b),
ρ_ f = eq_to_iso (comp_id' f) . obviously)
(associator_eq_to_iso' : ∀ {a b c d : B} (f : a ⟶ b) (g : b ⟶ c) (h : c ⟶ d),
α_ f g h = eq_to_iso (assoc' f g h) . obviously)
restate_axiom bicategory.strict.id_comp'
restate_axiom bicategory.strict.comp_id'
restate_axiom bicategory.strict.assoc'
restate_axiom bicategory.strict.left_unitor_eq_to_iso'
restate_axiom bicategory.strict.right_unitor_eq_to_iso'
restate_axiom bicategory.strict.associator_eq_to_iso'
attribute [simp]
bicategory.strict.id_comp bicategory.strict.left_unitor_eq_to_iso
bicategory.strict.comp_id bicategory.strict.right_unitor_eq_to_iso
bicategory.strict.assoc bicategory.strict.associator_eq_to_iso
/-- Category structure on a strict bicategory -/
@[priority 100] -- see Note [lower instance priority]
instance strict_bicategory.category [bicategory.strict B] : category B :=
{ id_comp' := λ a b, bicategory.strict.id_comp,
comp_id' := λ a b, bicategory.strict.comp_id,
assoc' := λ a b c d, bicategory.strict.assoc }
namespace bicategory
variables {B}
@[simp]
lemma whisker_left_eq_to_hom {a b c : B} (f : a ⟶ b) {g h : b ⟶ c} (η : g = h) :
f ◁ eq_to_hom η = eq_to_hom (congr_arg2 (≫) rfl η) :=
by { cases η, simp only [whisker_left_id, eq_to_hom_refl] }
@[simp]
lemma eq_to_hom_whisker_right {a b c : B} {f g : a ⟶ b} (η : f = g) (h : b ⟶ c) :
eq_to_hom η ▷ h = eq_to_hom (congr_arg2 (≫) η rfl) :=
by { cases η, simp only [id_whisker_right, eq_to_hom_refl] }
end bicategory
end category_theory
|
6d4211a58ee4761edd14b616823d91c17e6d318c | 5c4a0908390c4938ae21bc616ff2a969ce62fd76 | /library/theories/topology/basic.lean | 4a022d044795f58c5f21106de5de7b2a579d5521 | [
"Apache-2.0"
] | permissive | Bpalkmim/lean | 968be8a73a06fa6db19073cd463d2093464dc0f6 | 994815bc7793f5765beb693c82341cf01d20d807 | refs/heads/master | 1,610,964,748,106 | 1,455,564,675,000 | 1,455,564,675,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,842 | lean | /-
Copyright (c) 2015 Jacob Gross. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jacob Gross, Jeremy Avigad
Open and closed sets, seperation axioms and generated topologies.
-/
import data.set data.nat
open algebra eq.ops set nat
structure topology [class] (X : Type) :=
(opens : set (set X))
(univ_mem_opens : univ ∈ opens)
(sUnion_mem_opens : ∀ {S : set (set X)}, S ⊆ opens → ⋃₀ S ∈ opens)
(inter_mem_opens : ∀₀ s ∈ opens, ∀₀ t ∈ opens, s ∩ t ∈ opens)
namespace topology
variables {X : Type} [topology X]
/- open sets -/
definition Open (s : set X) : Prop := s ∈ opens X
theorem Open_empty : Open (∅ : set X) :=
have ∅ ⊆ opens X, from empty_subset _,
have ⋃₀ ∅ ∈ opens X, from sUnion_mem_opens this,
show ∅ ∈ opens X, using this, by rewrite -sUnion_empty; apply this
theorem Open_univ : Open (univ : set X) :=
univ_mem_opens X
theorem Open_sUnion {S : set (set X)} (H : ∀₀ t ∈ S, Open t) : Open (⋃₀ S) :=
sUnion_mem_opens H
theorem Open_Union {I : Type} {s : I → set X} (H : ∀ i, Open (s i)) : Open (⋃ i, s i) :=
have ∀₀ t ∈ s ' univ, Open t,
from take t, suppose t ∈ s ' univ,
obtain i [univi (Hi : s i = t)], from this,
show Open t, by rewrite -Hi; exact H i,
using this, by rewrite Union_eq_sUnion_image; apply Open_sUnion this
theorem Open_union {s t : set X} (Hs : Open s) (Ht : Open t) : Open (s ∪ t) :=
have ∀ i, Open (bin_ext s t i), by intro i; cases i; exact Hs; exact Ht,
show Open (s ∪ t), using this, by rewrite -Union_bin_ext; exact Open_Union this
theorem Open_inter {s t : set X} (Hs : Open s) (Ht : Open t) : Open (s ∩ t) :=
inter_mem_opens X Hs Ht
theorem Open_sInter_of_finite {s : set (set X)} [fins : finite s] (H : ∀₀ t ∈ s, Open t) :
Open (⋂₀ s) :=
begin
induction fins with a s fins anins ih,
{rewrite sInter_empty, exact Open_univ},
rewrite sInter_insert,
apply Open_inter,
show Open a, from H (mem_insert a s),
apply ih, intros t ts,
show Open t, from H (mem_insert_of_mem a ts)
end
/- closed sets -/
definition closed [reducible] (s : set X) : Prop := Open (-s)
theorem closed_iff_Open_comp (s : set X) : closed s ↔ Open (-s) := !iff.refl
theorem Open_iff_closed_comp (s : set X) : Open s ↔ closed (-s) :=
by rewrite [closed_iff_Open_comp, comp_comp]
theorem closed_comp {s : set X} (H : Open s) : closed (-s) :=
by rewrite [-Open_iff_closed_comp]; apply H
theorem closed_empty : closed (∅ : set X) :=
by rewrite [↑closed, comp_empty]; exact Open_univ
theorem closed_univ : closed (univ : set X) :=
by rewrite [↑closed, comp_univ]; exact Open_empty
theorem closed_sInter {S : set (set X)} (H : ∀₀ t ∈ S, closed t) : closed (⋂₀ S) :=
begin
rewrite [↑closed, comp_sInter],
apply Open_sUnion,
intro t,
rewrite [mem_image_complement, Open_iff_closed_comp],
apply H
end
theorem closed_Inter {I : Type} {s : I → set X} (H : ∀ i, closed (s i : set X)) :
closed (⋂ i, s i) :=
by rewrite [↑closed, comp_Inter]; apply Open_Union; apply H
theorem closed_inter {s t : set X} (Hs : closed s) (Ht : closed t) : closed (s ∩ t) :=
by rewrite [↑closed, comp_inter]; apply Open_union; apply Hs; apply Ht
theorem closed_union {s t : set X} (Hs : closed s) (Ht : closed t) : closed (s ∪ t) :=
by rewrite [↑closed, comp_union]; apply Open_inter; apply Hs; apply Ht
theorem closed_sUnion_of_finite {s : set (set X)} [fins : finite s] (H : ∀₀ t ∈ s, closed t) :
closed (⋂₀ s) :=
begin
rewrite [↑closed, comp_sInter],
apply Open_sUnion,
intro t,
rewrite [mem_image_complement, Open_iff_closed_comp],
apply H
end
theorem open_diff {s t : set X} (Hs : Open s) (Ht : closed t) : Open (s \ t) :=
Open_inter Hs Ht
theorem closed_diff {s t : set X} (Hs : closed s) (Ht : Open t) : closed (s \ t) :=
closed_inter Hs (closed_comp Ht)
section
open classical
theorem Open_of_forall_exists_Open_nbhd {s : set X} (H : ∀₀ x ∈ s, ∃ tx : set X, Open tx ∧ x ∈ tx ∧ tx ⊆ s) :
Open s :=
let Hset : X → set X := λ x, if Hxs : x ∈ s then some (H Hxs) else univ in
let sFam := image (λ x, Hset x) s in
have H_union_open : Open (⋃₀ sFam), from Open_sUnion
(take t : set X, suppose t ∈ sFam,
have H_preim : ∃ t', t' ∈ s ∧ Hset t' = t, from this,
obtain t' (Ht' : t' ∈ s) (Ht't : Hset t' = t), from H_preim,
have HHsett : t = some (H Ht'), from Ht't ▸ dif_pos Ht',
show Open t, from and.left (HHsett⁻¹ ▸ some_spec (H Ht'))),
have H_subset_union : s ⊆ ⋃₀ sFam, from
(take x : X, suppose x ∈ s,
have HxHset : x ∈ Hset x, from (dif_pos this)⁻¹ ▸ (and.left (and.right (some_spec (H this)))),
show x ∈ ⋃₀ sFam, from mem_sUnion HxHset (mem_image this rfl)),
have H_union_subset : ⋃₀ sFam ⊆ s, from
(take x : X, suppose x ∈ ⋃₀ sFam,
obtain (t : set X) (Ht : t ∈ sFam) (Hxt : x ∈ t), from this,
have H_preim : ∃ t', t' ∈ s ∧ Hset t' = t, from Ht,
obtain t' (Ht' : t' ∈ s) (Ht't : Hset t' = t), from H_preim,
have HHsett : t = some (H Ht'), from Ht't ▸ dif_pos Ht',
have t ⊆ s, from and.right (and.right (HHsett⁻¹ ▸ some_spec (H Ht'))),
show x ∈ s, from this Hxt),
have H_union_eq : ⋃₀ sFam = s, from eq_of_subset_of_subset H_union_subset H_subset_union,
show Open s, from H_union_eq ▸ H_union_open
end
end topology
/- separation -/
structure T0_space [class] (X : Type) extends topology X :=
(T0 : ∀ {x y}, x ≠ y → ∃ U, U ∈ opens ∧ ¬(x ∈ U ↔ y ∈ U))
namespace topology
variables {X : Type} [T0_space X]
theorem T0 {x y : X} (H : x ≠ y) : ∃ U, Open U ∧ ¬(x ∈ U ↔ y ∈ U) :=
T0_space.T0 H
end topology
structure T1_space [class] (X : Type) extends topology X :=
(T1 : ∀ {x y}, x ≠ y → ∃ U, U ∈ opens ∧ x ∈ U ∧ y ∉ U)
protected definition T0_space.of_T1 [reducible] [trans_instance] {X : Type} [T : T1_space X] :
T0_space X :=
⦃T0_space, T,
T0 := abstract
take x y, assume H,
obtain U [Uopens [xU ynU]], from T1_space.T1 H,
exists.intro U (and.intro Uopens
(show ¬ (x ∈ U ↔ y ∈ U), from assume H, ynU (iff.mp H xU)))
end ⦄
namespace topology
variables {X : Type} [T1_space X]
theorem T1 {x y : X} (H : x ≠ y) : ∃ U, Open U ∧ x ∈ U ∧ y ∉ U :=
T1_space.T1 H
end topology
structure T2_space [class] (X : Type) extends topology X :=
(T2 : ∀ {x y}, x ≠ y → ∃ U V, U ∈ opens ∧ V ∈ opens ∧ x ∈ U ∧ y ∈ V ∧ U ∩ V = ∅)
protected definition T1_space.of_T2 [reducible] [trans_instance] {X : Type} [T : T2_space X] :
T1_space X :=
⦃T1_space, T,
T1 := abstract
take x y, assume H,
obtain U [V [Uopens [Vopens [xU [yV UVempty]]]]], from T2_space.T2 H,
exists.intro U (and.intro Uopens (and.intro xU
(show y ∉ U, from assume yU,
have y ∈ U ∩ V, from and.intro yU yV,
show y ∈ ∅, from UVempty ▸ this)))
end ⦄
namespace topology
variables {X : Type} [T2_space X]
theorem T2 {x y : X} (H : x ≠ y) : ∃ U V, Open U ∧ Open V ∧ x ∈ U ∧ y ∈ V ∧ U ∩ V = ∅ :=
T2_space.T2 H
end topology
structure perfect_space [class] (X : Type) extends topology X :=
(perfect : ∀ x, '{x} ∉ opens)
/- topology generated by a set -/
namespace topology
inductive opens_generated_by {X : Type} (B : set (set X)) : set X → Prop :=
| generators_mem : ∀ ⦃s : set X⦄, s ∈ B → opens_generated_by B s
| univ_mem : opens_generated_by B univ
| inter_mem : ∀ ⦃s t⦄, opens_generated_by B s → opens_generated_by B t →
opens_generated_by B (s ∩ t)
| sUnion_mem : ∀ ⦃S : set (set X)⦄, S ⊆ opens_generated_by B → opens_generated_by B (⋃₀ S)
protected definition generated_by [instance] [reducible] {X : Type} (B : set (set X)) : topology X :=
⦃topology,
opens := opens_generated_by B,
univ_mem_opens := opens_generated_by.univ_mem B,
inter_mem_opens := λ s Hs t Ht, opens_generated_by.inter_mem Hs Ht,
sUnion_mem_opens := opens_generated_by.sUnion_mem
⦄
theorem generators_mem_topology_generated_by {X : Type} (B : set (set X)) :
let T := topology.generated_by B in
∀₀ s ∈ B, @Open _ T s :=
λ s H, opens_generated_by.generators_mem H
theorem opens_generated_by_initial {X : Type} {B : set (set X)} {T : topology X} (H : B ⊆ @opens _ T) :
opens_generated_by B ⊆ @opens _ T :=
begin
intro s Hs,
induction Hs with s sB s t os ot soX toX S SB SOX,
{exact H sB},
{exact univ_mem_opens X},
{exact inter_mem_opens X soX toX},
exact sUnion_mem_opens SOX
end
theorem topology_generated_by_initial {X : Type} {B : set (set X)} {T : topology X}
(H : ∀₀ s ∈ B, @Open _ T s) {s : set X} (H1 : @Open _ (topology.generated_by B) s) :
@Open _ T s :=
opens_generated_by_initial H H1
section continuity
/- continuous mappings -/
/- continuity at a point -/
variables {M N : Type} [Tm : topology M] [Tn : topology N]
include Tm Tn
definition continuous_at (f : M → N) (x : M) :=
∀ U : set N, f x ∈ U → Open U → ∃ V : set M, x ∈ V ∧ Open V ∧ f 'V ⊆ U
definition continuous (f : M → N) :=
∀ x : M, continuous_at f x
end continuity
section boundary
variables {X : Type} [TX : topology X]
include TX
definition on_boundary (x : X) (u : set X) := ∀ v : set X, Open v → x ∈ v → u ∩ v ≠ ∅ ∧ ¬ v ⊆ u
theorem not_open_of_on_boundary {x : X} {u : set X} (Hxu : x ∈ u) (Hob : on_boundary x u) : ¬ Open u :=
begin
intro Hop,
note Hbxu := Hob _ Hop Hxu,
apply and.right Hbxu,
apply subset.refl
end
end boundary
end topology
|
6449fd788dd7e2a0495f993eb78aed46317a3e23 | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /hott/algebra/binary.hlean | 2c9f3d93fdb3cf14f615d208c889e1156249d180 | [
"Apache-2.0"
] | permissive | respu/lean | 6582d19a2f2838a28ecd2b3c6f81c32d07b5341d | 8c76419c60b63d0d9f7bc04ebb0b99812d0ec654 | refs/heads/master | 1,610,882,451,231 | 1,427,747,084,000 | 1,427,747,429,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,251 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: algebra.binary
Authors: Leonardo de Moura, Jeremy Avigad
General properties of binary operations.
-/
open eq
namespace binary
section
variable {A : Type}
variables (op₁ : A → A → A) (inv : A → A) (one : A)
local notation a * b := op₁ a b
local notation a ⁻¹ := inv a
local notation 1 := one
definition commutative := ∀a b, a*b = b*a
definition associative := ∀a b c, (a*b)*c = a*(b*c)
definition left_identity := ∀a, 1 * a = a
definition right_identity := ∀a, a * 1 = a
definition left_inverse := ∀a, a⁻¹ * a = 1
definition right_inverse := ∀a, a * a⁻¹ = 1
definition left_cancelative := ∀a b c, a * b = a * c → b = c
definition right_cancelative := ∀a b c, a * b = c * b → a = c
definition inv_op_cancel_left := ∀a b, a⁻¹ * (a * b) = b
definition op_inv_cancel_left := ∀a b, a * (a⁻¹ * b) = b
definition inv_op_cancel_right := ∀a b, a * b⁻¹ * b = a
definition op_inv_cancel_right := ∀a b, a * b * b⁻¹ = a
variable (op₂ : A → A → A)
local notation a + b := op₂ a b
definition left_distributive := ∀a b c, a * (b + c) = a * b + a * c
definition right_distributive := ∀a b c, (a + b) * c = a * c + b * c
end
context
variable {A : Type}
variable {f : A → A → A}
variable H_comm : commutative f
variable H_assoc : associative f
infixl `*` := f
theorem left_comm : ∀a b c, a*(b*c) = b*(a*c) :=
take a b c, calc
a*(b*c) = (a*b)*c : H_assoc
... = (b*a)*c : H_comm
... = b*(a*c) : H_assoc
theorem right_comm : ∀a b c, (a*b)*c = (a*c)*b :=
take a b c, calc
(a*b)*c = a*(b*c) : H_assoc
... = a*(c*b) : H_comm
... = (a*c)*b : H_assoc
end
context
variable {A : Type}
variable {f : A → A → A}
variable H_assoc : associative f
infixl `*` := f
theorem assoc4helper (a b c d) : (a*b)*(c*d) = a*((b*c)*d) :=
calc
(a*b)*(c*d) = a*(b*(c*d)) : H_assoc
... = a*((b*c)*d) : H_assoc
end
end binary
|
74ca4cd734efe45d723c0efca2b98b9234c64012 | 6b2a480f27775cba4f3ae191b1c1387a29de586e | /group_rep1/basis.lean | 80a61610c5747c77fff03aa21c28b4f730275c20 | [] | no_license | Or7ando/group_representation | a681de2e19d1930a1e1be573d6735a2f0b8356cb | 9b576984f17764ebf26c8caa2a542d248f1b50d2 | refs/heads/master | 1,662,413,107,324 | 1,590,302,389,000 | 1,590,302,389,000 | 258,130,829 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 2,526 | lean | import linear_algebra.basic
import linear_algebra.basis
import algebra.big_operators
universes u v w
variables (X : Type u)(R : Type v)[comm_ring R][decidable_eq X][fintype X]
def ε (x : X) : (X → R) := λ y, if x = y then 1 else 0
variables ( x y : X)
#check ε X R x
lemma test1 (g : X → R)(s : finset X) : (λ (i : X), g i • ε X R i y) = λ (i : X), if y = i then g y else 0 := begin
funext,
split_ifs, unfold ε, split_ifs,rw h, change g i * 1 = _, rw mul_one, have hypo : i = y, rw h, trivial,
unfold ε,split_ifs, change (g i) * 1 = _, rw mul_one, have hyp : y= i, rw h_1, trivial, change (g i) * 0 = 0, rw mul_zero,
end
#check finset.sum_ite_eq
lemma test (g : X → R)(s : finset X)(y ∈ s) : finset.sum s ((λ (i : X), g i • ε X R i) ) y = g y :=
begin
rw finset.sum_apply,
erw test1,
rw finset.sum_ite_eq,split_ifs, exact rfl,
assumption,
end
lemma rtrt (g : X → R)(s : finset X)(y ∈ s) : finset.sum s ((λ (i : X), g i • ε X R i) ) y = finset.sum s ((λ (i : X), g i • ε X R i y) ) :=
begin
rw finset.sum_apply, exact rfl,
end
lemma trr (M : Type w)[add_comm_group M][module R M] (p : submodule R M)(g : X → M) : set.range g ⊆ p → finset.sum finset.univ g ∈ p :=
begin
sorry,
end
lemma gen (g : X → R) : g = finset.sum (finset.univ) (λ (x : X), g x • ε X R x) :=
begin
funext, rw finset.sum_apply,
change g x = finset.sum finset.univ (λ (g_1 : X), (g g_1 • ε X R g_1 x) ),
rw test1,rw finset.sum_ite_eq,split_ifs,exact rfl,
have R : x ∈ finset.univ, exact finset.mem_univ x, trivial,
use finset.univ,
end
theorem classical_basis : is_basis R (ε X R) :=
begin
split,
{
rw linear_independent_iff', intros s, intros g,
intros hyp,
intros y, intro hyp_y,
rw function.funext_iff at hyp,
specialize hyp y,
rw test at hyp, exact hyp, assumption,
},
{ rw eq_top_iff, rw submodule.le_def',
intros g, intros hyp, rw submodule.mem_span,
intros p, intros hyp',
let F := gen X R g, rw F,
let G := trr X R (X → R) p (λ (x : X), g x • ε X R x),
apply G,
rw set.subset_def,
intros g, intros hyp, rw set.mem_range at hyp,
rcases hyp with ⟨y,hyp_rang ⟩,
rw ← hyp_rang,
apply submodule.smul,
rw [set.subset_def] at hyp',
specialize hyp' (ε X R y),
have GGGG : ε X R y ∈ set.range (ε X R),
exact set.mem_range_self y,
exact hyp' GGGG,
},
end
|
975a258dde7d8f37d2e11a14af6098facea2a8e3 | 50b3917f95cf9fe84639812ea0461b38f8f0dbe1 | /canonical_isomorphism/canonical.lean | ae84bbb574f96c37683917a45d79580525b68df3 | [] | no_license | roro47/xena | 6389bcd7dcf395656a2c85cfc90a4366e9b825bb | 237910190de38d6ff43694ffe3a9b68f79363e6c | refs/heads/master | 1,598,570,061,948 | 1,570,052,567,000 | 1,570,052,567,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,682 | lean | import data.equiv.basic
--import analysis.topology.topological_structures
universes u v w
-- Scott's basic class.
class transportable (f : Type u → Type v) :=
(on_equiv : Π {α β : Type u} (e : equiv α β), equiv (f α) (f β))
(on_refl : Π (α : Type u), on_equiv (equiv.refl α) = equiv.refl (f α))
(on_trans : Π {α β γ : Type u} (d : equiv α β) (e : equiv β γ), on_equiv (equiv.trans d e) = equiv.trans (on_equiv d) (on_equiv e))
-- One goal is an automagic proof of the following
theorem group.transportable : transportable group := sorry
-- we know it can be proved by hand, but we're bored of doing it.
-- Kenny even did ring in his github
-- theorem topological_ring.transportable : transportable topological_ring := sorry
-- type mismatch at application
-- But some basic constructions we might need to do by hand.
def Const : Type u → Type v := λ α, punit
lemma Const.transportable : (transportable Const) :=
{ on_equiv := λ α β e, equiv.punit_equiv_punit,
on_refl := λ α, equiv.ext _ _ $ λ ⟨⟩, rfl,
on_trans := λ α β γ e1 e2, equiv.ext _ _ $ λ ⟨⟩, rfl }
def Fun : Type u → Type v → Type (max u v) := λ α β, α → β
lemma Fun.transportable (α : Type u) : (transportable (Fun α)) :=
{ on_equiv := λ β γ e, equiv.arrow_congr (equiv.refl α) e,
on_refl := λ β, equiv.ext _ _ $ λ f, rfl,
on_trans := λ β γ δ e1 e2, equiv.ext _ _ $ λ f, funext $ λ x,
by cases e1; cases e2; refl }
theorem prod.ext' {α β : Type*} {p q : α × β} (H1 : p.1 = q.1) (H2 : p.2 = q.2) : p = q :=
prod.ext.2 ⟨H1, H2⟩
def Prod : Type u → Type v → Type (max u v) := λ α β, α × β
lemma Prod.transportable (α : Type u) : (transportable (Prod α)) :=
{ on_equiv := λ β γ e, equiv.prod_congr (equiv.refl α) e,
on_refl := λ β, equiv.ext _ _ $ λ ⟨x, y⟩, by simp,
on_trans := λ β γ δ e1 e2, equiv.ext _ _ $ λ ⟨x, y⟩, by simp }
def Swap : Type u → Type v → Type (max u v) := λ α β, β × α
lemma Swap.transportable (α : Type u) : (transportable (Swap α)) :=
{ on_equiv := λ β γ e, equiv.prod_congr e (equiv.refl α),
on_refl := λ β, equiv.ext _ _ $ λ ⟨x, y⟩, by simp,
on_trans := λ β γ δ e1 e2, equiv.ext _ _ $ λ ⟨x, y⟩, by simp }
-- And then we can define
def Hom1 (α : Type u) : Type v → Type (max u v) := λ β, α → β
def Hom2 (β : Type v) : Type u → Type (max u v) := λ α, α → β
def Aut : Type u → Type u := λ α, α → α
-- And hopefully automagically derive
lemma Hom1.transportable (α : Type u) : (transportable (Hom1 α)) :=
Fun.transportable α
lemma Hom2.transportable (β : Type v) : (transportable (Hom2 β)) :=
{ on_equiv := λ α γ e, equiv.arrow_congr e (equiv.refl β),
on_refl := λ β, equiv.ext _ _ $ λ f, rfl,
on_trans := λ β γ δ e1 e2, equiv.ext _ _ $ λ f, funext $ λ x,
by cases e1; cases e2; refl }
lemma Aut.transportable : (transportable Aut) :=
{ on_equiv := λ α β e, equiv.arrow_congr e e,
on_refl := λ α, equiv.ext _ _ $ λ f, funext $ λ x, rfl,
on_trans := λ α β γ e1 e2, equiv.ext _ _ $ λ f, funext $ λ x,
by cases e1; cases e2; refl }
-- If we have all these in place...
-- A bit of magic might actually be able to derive `group.transportable` on line 11.
-- After all, a group just is a type plus some functions... and we can now transport functions.
#print prefix prod
--theorem T {α : Type u} {β : Type v} [group α] [group β] :
--group α × β := by apply_instance
definition prod_group (G : Type u) (H : Type v) [HG : group G] [HH : group H] : group (G × H) :=
{ mul := λ ⟨g1,h1⟩ ⟨g2,h2⟩, ⟨g1 * g2,h1 * h2⟩,
mul_assoc := λ ⟨g1,h1⟩ ⟨g2,h2⟩ ⟨g3,h3⟩,prod.ext.2 ⟨mul_assoc _ _ _,mul_assoc _ _ _⟩,
one := ⟨HG.one,HH.one⟩,
one_mul := λ ⟨g,h⟩, prod.ext.2 ⟨one_mul _,one_mul _⟩,
mul_one := λ ⟨g,h⟩, prod.ext.2 ⟨mul_one _,mul_one _⟩,
inv := λ ⟨g,h⟩, ⟨group.inv g,group.inv h⟩,--begin end,--λ ⟨g,h⟩, ⟨HG.inv g,HH.inv h⟩,
mul_left_inv := λ ⟨g,h⟩, prod.ext.2 ⟨mul_left_inv g,mul_left_inv h⟩
}
-- I can prove that if G is can iso to G', and H to H', then the diagram commutes?
-- remember we proved that Prod was transportable
-- free theorem for chnat
def chℕ := Π X : Type, (X → X) → X → X
theorem free_chnat : ∀ (A B : Type), ∀ f : A → B, ∀ r : chℕ,
∀ a : A,
r (A → B) (λ g,f) f a = r (A → B) (λ g,g) f a
:= begin
intros A B f r a,
let Athing1 := r A (λ b, a) a,
let Athing2 := r A (λ b, b) a,
let Bthing1 := r B (λ b,f a) (f a),
let Bthing2 := r B (λ b,b) (f a),
-- these free theorems are hard to prove
admit,
end
|
d43471eac31c756500cf9667d005459058a02c9f | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /data/int/basic.lean | 8c0167012424510a966f9601c09cf322b5f829e8 | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 45,273 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
The integers, with addition, multiplication, and subtraction.
-/
import data.nat.basic algebra.char_zero algebra.order_functions
open nat
namespace int
instance : inhabited ℤ := ⟨0⟩
meta instance : has_to_format ℤ := ⟨λ z, int.rec_on z (λ k, ↑k) (λ k, "-("++↑k++"+1)")⟩
attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ
@[simp] theorem coe_nat_mul_neg_succ (m n : ℕ) : (m : ℤ) * -[1+ n] = -(m * succ n) := rfl
@[simp] theorem neg_succ_mul_coe_nat (m n : ℕ) : -[1+ m] * n = -(succ m * n) := rfl
@[simp] theorem neg_succ_mul_neg_succ (m n : ℕ) : -[1+ m] * -[1+ n] = succ m * succ n := rfl
theorem coe_nat_le {m n : ℕ} : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := coe_nat_le_coe_nat_iff m n
theorem coe_nat_lt {m n : ℕ} : (↑m : ℤ) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n
theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n
@[simp] theorem coe_nat_pos {n : ℕ} : (0 : ℤ) < n ↔ 0 < n :=
by rw [← int.coe_nat_zero, coe_nat_lt]
@[simp] theorem coe_nat_eq_zero {n : ℕ} : (n : ℤ) = 0 ↔ n = 0 :=
by rw [← int.coe_nat_zero, coe_nat_inj']
@[simp] theorem coe_nat_ne_zero {n : ℕ} : (n : ℤ) ≠ 0 ↔ n ≠ 0 :=
not_congr coe_nat_eq_zero
lemma coe_nat_nonneg (n : ℕ) : 0 ≤ (n : ℤ) := coe_nat_le.2 (zero_le _)
lemma coe_nat_ne_zero_iff_pos {n : ℕ} : (n : ℤ) ≠ 0 ↔ 0 < n :=
⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h),
λ h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩
lemma coe_nat_succ_pos (n : ℕ) : 0 < (n.succ : ℤ) := int.coe_nat_pos.2 (succ_pos n)
/- succ and pred -/
/-- Immediate successor of an integer: `succ n = n + 1` -/
def succ (a : ℤ) := a + 1
/-- Immediate predecessor of an integer: `pred n = n - 1` -/
def pred (a : ℤ) := a - 1
theorem nat_succ_eq_int_succ (n : ℕ) : (nat.succ n : ℤ) = int.succ n := rfl
theorem pred_succ (a : ℤ) : pred (succ a) = a := add_sub_cancel _ _
theorem succ_pred (a : ℤ) : succ (pred a) = a := sub_add_cancel _ _
theorem neg_succ (a : ℤ) : -succ a = pred (-a) := neg_add _ _
theorem succ_neg_succ (a : ℤ) : succ (-succ a) = -a :=
by rw [neg_succ, succ_pred]
theorem neg_pred (a : ℤ) : -pred a = succ (-a) :=
by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg]
theorem pred_neg_pred (a : ℤ) : pred (-pred a) = -a :=
by rw [neg_pred, pred_succ]
theorem pred_nat_succ (n : ℕ) : pred (nat.succ n) = n := pred_succ n
theorem neg_nat_succ (n : ℕ) : -(nat.succ n : ℤ) = pred (-n) := neg_succ n
theorem succ_neg_nat_succ (n : ℕ) : succ (-nat.succ n) = -n := succ_neg_succ n
theorem lt_succ_self (a : ℤ) : a < succ a :=
lt_add_of_pos_right _ zero_lt_one
theorem pred_self_lt (a : ℤ) : pred a < a :=
sub_lt_self _ zero_lt_one
theorem add_one_le_iff {a b : ℤ} : a + 1 ≤ b ↔ a < b := iff.rfl
theorem lt_add_one_iff {a b : ℤ} : a < b + 1 ↔ a ≤ b :=
@add_le_add_iff_right _ _ a b 1
theorem sub_one_le_iff {a b : ℤ} : a - 1 < b ↔ a ≤ b :=
sub_lt_iff_lt_add.trans lt_add_one_iff
theorem le_sub_one_iff {a b : ℤ} : a ≤ b - 1 ↔ a < b :=
le_sub_iff_add_le
@[elab_as_eliminator] protected lemma induction_on {p : ℤ → Prop}
(i : ℤ) (hz : p 0) (hp : ∀i, p i → p (i + 1)) (hn : ∀i, p i → p (i - 1)) : p i :=
begin
induction i,
{ induction i,
{ exact hz },
{ exact hp _ i_ih } },
{ have : ∀n:ℕ, p (- n),
{ intro n, induction n,
{ simp [hz] },
{ have := hn _ n_ih, simpa } },
exact this (i + 1) }
end
/- nat abs -/
attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one
theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b :=
begin
have, {
refine (λ a b : ℕ, sub_nat_nat_elim a b.succ
(λ m n i, n = b.succ → nat_abs i ≤ (m + b).succ) _ _ rfl);
intros i n e,
{ subst e, rw [add_comm _ i, add_assoc],
exact nat.le_add_right i (b.succ + b).succ },
{ apply succ_le_succ,
rw [← succ_inj e, ← add_assoc, add_comm],
apply nat.le_add_right } },
cases a; cases b with b b; simp [nat_abs, nat.succ_add];
try {refl}; [skip, rw add_comm a b]; apply this
end
theorem nat_abs_neg_of_nat (n : ℕ) : nat_abs (neg_of_nat n) = n :=
by cases n; refl
theorem nat_abs_mul (a b : ℤ) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) :=
by cases a; cases b; simp [(*), int.mul, nat_abs_neg_of_nat]
theorem neg_succ_of_nat_eq' (m : ℕ) : -[1+ m] = -m - 1 :=
by simp [neg_succ_of_nat_eq]
lemma nat_abs_ne_zero_of_ne_zero {z : ℤ} (hz : z ≠ 0) : z.nat_abs ≠ 0 :=
λ h, hz $ int.eq_zero_of_nat_abs_eq_zero h
/- / -/
@[simp] theorem of_nat_div (m n : ℕ) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl
@[simp] theorem coe_nat_div (m n : ℕ) : ((m / n : ℕ) : ℤ) = m / n := rfl
theorem neg_succ_of_nat_div (m : ℕ) {b : ℤ} (H : b > 0) :
-[1+m] / b = -(m / b + 1) :=
match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end
@[simp] protected theorem div_neg : ∀ (a b : ℤ), a / -b = -(a / b)
| (m : ℕ) 0 := show of_nat (m / 0) = -(m / 0 : ℕ), by rw nat.div_zero; refl
| (m : ℕ) (n+1:ℕ) := rfl
| 0 -[1+ n] := rfl
| (m+1:ℕ) -[1+ n] := (neg_neg _).symm
| -[1+ m] 0 := rfl
| -[1+ m] (n+1:ℕ) := rfl
| -[1+ m] -[1+ n] := rfl
theorem div_of_neg_of_pos {a b : ℤ} (Ha : a < 0) (Hb : b > 0) : a / b = -((-a - 1) / b + 1) :=
match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ :=
by change (- -[1+ m] : ℤ) with (m+1 : ℤ); rw add_sub_cancel; refl
end
protected theorem div_nonneg {a b : ℤ} (Ha : a ≥ 0) (Hb : b ≥ 0) : a / b ≥ 0 :=
match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _
end
protected theorem div_nonpos {a b : ℤ} (Ha : a ≥ 0) (Hb : b ≤ 0) : a / b ≤ 0 :=
nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb)
theorem div_neg' {a b : ℤ} (Ha : a < 0) (Hb : b > 0) : a / b < 0 :=
match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _
end
@[simp] protected theorem zero_div : ∀ (b : ℤ), 0 / b = 0
| 0 := rfl
| (n+1:ℕ) := rfl
| -[1+ n] := rfl
@[simp] protected theorem div_zero : ∀ (a : ℤ), a / 0 = 0
| 0 := rfl
| (n+1:ℕ) := rfl
| -[1+ n] := rfl
@[simp] protected theorem div_one : ∀ (a : ℤ), a / 1 = a
| 0 := rfl
| (n+1:ℕ) := congr_arg of_nat (nat.div_one _)
| -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _)
theorem div_eq_zero_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0 :=
match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 :=
congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2
end
theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < abs b) : a / b = 0 :=
match b, abs b, abs_eq_nat_abs b, H2 with
| (n : ℕ), ._, rfl, H2 := div_eq_zero_of_lt H1 H2
| -[1+ n], ._, rfl, H2 := neg_inj $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2
end
protected theorem add_mul_div_right (a b : ℤ) {c : ℤ} (H : c ≠ 0) :
(a + b * c) / c = a / c + b :=
have ∀ {k n : ℕ} {a : ℤ}, (a + n * k.succ) / k.succ = a / k.succ + n, from
λ k n a, match a with
| (m : ℕ) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos
| -[1+ m] := show ((n * k.succ:ℕ) - m.succ : ℤ) / k.succ =
n - (m / k.succ + 1 : ℕ), begin
cases lt_or_ge m (n*k.succ) with h h,
{ rw [← int.coe_nat_sub h,
← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)],
apply congr_arg of_nat,
rw [mul_comm, nat.mul_sub_div], rwa mul_comm },
{ change (↑(n * nat.succ k) - (m + 1) : ℤ) / ↑(nat.succ k) =
↑n - ((m / nat.succ k : ℕ) + 1),
rw [← sub_sub, ← sub_sub, ← neg_sub (m:ℤ), ← neg_sub _ (n:ℤ),
← int.coe_nat_sub h,
← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h),
← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'],
{ apply congr_arg neg_succ_of_nat,
rw [mul_comm, nat.sub_mul_div], rwa mul_comm } }
end
end,
have ∀ {a b c : ℤ}, c > 0 → (a + b * c) / c = a / c + b, from
λ a b c H, match c, eq_succ_of_zero_lt H, b with
| ._, ⟨k, rfl⟩, (n : ℕ) := this
| ._, ⟨k, rfl⟩, -[1+ n] :=
show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from
eq_sub_of_add_eq $ by rw [← this, sub_add_cancel]
end,
match lt_trichotomy c 0 with
| or.inl hlt := neg_inj $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg];
apply this (neg_pos_of_neg hlt)
| or.inr (or.inl heq) := absurd heq H
| or.inr (or.inr hgt) := this hgt
end
protected theorem add_mul_div_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) :
(a + b * c) / b = a / b + c :=
by rw [mul_comm, int.add_mul_div_right _ _ H]
@[simp] protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a :=
by have := int.add_mul_div_right 0 a H;
rwa [zero_add, int.zero_div, zero_add] at this
@[simp] protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b :=
by rw [mul_comm, int.mul_div_cancel _ H]
@[simp] protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 :=
by have := int.mul_div_cancel 1 H; rwa one_mul at this
/- mod -/
theorem of_nat_mod (m n : nat) : (m % n : ℤ) = of_nat (m % n) := rfl
@[simp] theorem coe_nat_mod (m n : ℕ) : (↑(m % n) : ℤ) = ↑m % ↑n := rfl
theorem neg_succ_of_nat_mod (m : ℕ) {b : ℤ} (bpos : b > 0) :
-[1+m] % b = b - 1 - m % b :=
by rw [sub_sub, add_comm]; exact
match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end
@[simp] theorem mod_neg : ∀ (a b : ℤ), a % -b = a % b
| (m : ℕ) n := @congr_arg ℕ ℤ _ _ (λ i, ↑(m % i)) (nat_abs_neg _)
| -[1+ m] n := @congr_arg ℕ ℤ _ _ (λ i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _)
@[simp] theorem mod_abs (a b : ℤ) : a % (abs b) = a % b :=
abs_by_cases (λ i, a % i = a % b) rfl (mod_neg _ _)
@[simp] theorem zero_mod (b : ℤ) : 0 % b = 0 :=
congr_arg of_nat $ nat.zero_mod _
@[simp] theorem mod_zero : ∀ (a : ℤ), a % 0 = a
| (m : ℕ) := congr_arg of_nat $ nat.mod_zero _
| -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _
@[simp] theorem mod_one : ∀ (a : ℤ), a % 1 = 0
| (m : ℕ) := congr_arg of_nat $ nat.mod_one _
| -[1+ m] := show (1 - (m % 1).succ : ℤ) = 0, by rw nat.mod_one; refl
theorem mod_eq_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a :=
match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 :=
congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2)
end
theorem mod_nonneg : ∀ (a : ℤ) {b : ℤ}, b ≠ 0 → a % b ≥ 0
| (m : ℕ) n H := coe_zero_le _
| -[1+ m] n H :=
sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H)
theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : b > 0) : a % b < b :=
match a, b, eq_succ_of_zero_lt H with
| (m : ℕ), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _))
| -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _)
end
theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < abs b :=
by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos_of_ne_zero H)
theorem mod_add_div_aux (m n : ℕ) : (n - (m % n + 1) - (n * (m / n) + n) : ℤ) = -[1+ m] :=
begin
rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:ℤ)],
apply eq_neg_of_eq_neg,
rw [neg_sub, sub_sub_self, add_right_comm],
exact @congr_arg ℕ ℤ _ _ (λi, (i + 1 : ℤ)) (nat.mod_add_div _ _).symm
end
theorem mod_add_div : ∀ (a b : ℤ), a % b + b * (a / b) = a
| (m : ℕ) 0 := congr_arg of_nat (nat.mod_add_div _ _)
| (m : ℕ) (n+1:ℕ) := congr_arg of_nat (nat.mod_add_div _ _)
| 0 -[1+ n] := rfl
| (m+1:ℕ) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : ℕ) : ℤ) = _,
by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _)
| -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl
| -[1+ m] (n+1:ℕ) := mod_add_div_aux m n.succ
| -[1+ m] -[1+ n] := mod_add_div_aux m n.succ
theorem mod_def (a b : ℤ) : a % b = a - b * (a / b) :=
eq_sub_of_add_eq (mod_add_div _ _)
@[simp] theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c :=
if cz : c = 0 then by rw [cz, mul_zero, add_zero] else
by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz,
mul_add, mul_comm, add_sub_add_right_eq_sub]
@[simp] theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b :=
by rw [mul_comm, add_mul_mod_self]
@[simp] theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b :=
by have := add_mul_mod_self_left a b 1; rwa mul_one at this
@[simp] theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a :=
by rw [add_comm, add_mod_self]
@[simp] theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n :=
by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm;
rwa [add_right_comm, mod_add_div] at this
@[simp] theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k :=
by rw [add_comm, mod_add_mod, add_comm]
theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) :
(m + i) % n = (k + i) % n :=
by rw [← mod_add_mod, ← mod_add_mod k, H]
theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) :
(i + m) % n = (i + k) % n :=
by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm]
theorem mod_add_cancel_right {m n k : ℤ} (i) : (m + i) % n = (k + i) % n ↔
m % n = k % n :=
⟨λ H, by have := add_mod_eq_add_mod_right (-i) H;
rwa [add_neg_cancel_right, add_neg_cancel_right] at this,
add_mod_eq_add_mod_right _⟩
theorem mod_add_cancel_left {m n k i : ℤ} :
(i + m) % n = (i + k) % n ↔ m % n = k % n :=
by rw [add_comm, add_comm i, mod_add_cancel_right]
theorem mod_sub_cancel_right {m n k : ℤ} (i) : (m - i) % n = (k - i) % n ↔
m % n = k % n :=
mod_add_cancel_right _
theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : ℤ} : m % n = k % n ↔ (m - k) % n = 0 :=
(mod_sub_cancel_right k).symm.trans $ by simp
@[simp] theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 :=
by rw [← zero_add (a * b), add_mul_mod_self, zero_mod]
@[simp] theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 :=
by rw [mul_comm, mul_mod_left]
@[simp] theorem mod_self {a : ℤ} : a % a = 0 :=
by have := mul_mod_left 1 a; rwa one_mul at this
@[simp] lemma mod_mod (a b : ℤ) : a % b % b = a % b :=
by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]}
/- properties of / and % -/
@[simp] theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : a > 0) : a * b / (a * c) = b / c :=
suffices ∀ (m k : ℕ) (b : ℤ), (m.succ * b / (m.succ * k) : ℤ) = b / k, from
match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with
| ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _
| ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ :=
by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg];
apply congr_arg has_neg.neg; apply this
end,
λ m k b, match b, k with
| (n : ℕ), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos)
| -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero]
| -[1+ n], k+1 := congr_arg neg_succ_of_nat $
show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin
apply nat.div_eq_of_lt_le,
{ refine le_trans _ (nat.le_add_right _ _),
rw [← nat.mul_div_mul _ _ m.succ_pos],
apply nat.div_mul_le_self },
{ change m.succ * n.succ ≤ _,
rw [mul_left_comm],
apply nat.mul_le_mul_left,
apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1,
apply nat.lt_succ_self }
end
end
@[simp] theorem mul_div_mul_of_pos_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b > 0) :
a * b / (c * b) = a / c :=
by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H]
@[simp] theorem mul_mod_mul_of_pos {a : ℤ} (b c : ℤ) (H : a > 0) : a * b % (a * c) = a * (b % c) :=
by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc]
theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : b > 0) : a < (a / b + 1) * b :=
by rw [add_mul, one_mul, mul_comm]; apply lt_add_of_sub_left_lt;
rw [← mod_def]; apply mod_lt_of_pos _ H
theorem abs_div_le_abs : ∀ (a b : ℤ), abs (a / b) ≤ abs a :=
suffices ∀ (a : ℤ) (n : ℕ), abs (a / n) ≤ abs a, from
λ a b, match b, eq_coe_or_neg b with
| ._, ⟨n, or.inl rfl⟩ := this _ _
| ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this
end,
λ a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact
coe_nat_le_coe_nat_of_le (match a, n with
| (m : ℕ), n := nat.div_le_self _ _
| -[1+ m], 0 := nat.zero_le _
| -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _)
end)
theorem div_le_self {a : ℤ} (b : ℤ) (Ha : a ≥ 0) : a / b ≤ a :=
by have := le_trans (le_abs_self _) (abs_div_le_abs a b);
rwa [abs_of_nonneg Ha] at this
theorem mul_div_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : b * (a / b) = a :=
by have := mod_add_div a b; rwa [H, zero_add] at this
theorem div_mul_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : a / b * b = a :=
by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H]
/- dvd -/
theorem coe_nat_dvd {m n : ℕ} : (↑m : ℤ) ∣ ↑n ↔ m ∣ n :=
⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim
(λm0, by simp [m0] at ae; simp [ae, m0])
(λm0l, by {
cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left ℤ _ m a
(by simp [ae.symm]) (by simpa using m0l)) with k e,
subst a, exact ⟨k, int.coe_nat_inj ae⟩ }),
λ ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩
theorem coe_nat_dvd_left {n : ℕ} {z : ℤ} : (↑n : ℤ) ∣ z ↔ n ∣ z.nat_abs :=
by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd]
theorem coe_nat_dvd_right {n : ℕ} {z : ℤ} : z ∣ (↑n : ℤ) ↔ z.nat_abs ∣ n :=
by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd]
theorem dvd_antisymm {a b : ℤ} (H1 : a ≥ 0) (H2 : b ≥ 0) : a ∣ b → b ∣ a → a = b :=
begin
rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs],
rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'],
apply nat.dvd_antisymm
end
theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b :=
⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩
theorem mod_eq_zero_of_dvd : ∀ {a b : ℤ}, a ∣ b → b % a = 0
| a ._ ⟨c, rfl⟩ := mul_mod_right _ _
theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 :=
⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩
theorem nat_abs_dvd {a b : ℤ} : (a.nat_abs : ℤ) ∣ b ↔ a ∣ b :=
(nat_abs_eq a).elim (λ e, by rw ← e) (λ e, by rw [← neg_dvd_iff_dvd, ← e])
theorem dvd_nat_abs {a b : ℤ} : a ∣ b.nat_abs ↔ a ∣ b :=
(nat_abs_eq b).elim (λ e, by rw ← e) (λ e, by rw [← dvd_neg_iff_dvd, ← e])
instance decidable_dvd : @decidable_rel ℤ (∣) :=
assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm
protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a :=
div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H)
protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b :=
by rw [mul_comm, int.div_mul_cancel H]
protected theorem mul_div_assoc (a : ℤ) : ∀ {b c : ℤ}, c ∣ b → (a * b) / c = a * (b / c)
| ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else
by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz]
theorem div_dvd_div : ∀ {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a
| a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else
by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az];
apply dvd_mul_right
protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) :
a = b * c :=
by rw [← H2, int.mul_div_cancel' H1]
protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) :
a / b = c :=
by rw [H2, int.mul_div_cancel_left _ H1]
protected theorem div_eq_iff_eq_mul_right {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) :
a / b = c ↔ a = b * c :=
⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩
protected theorem div_eq_iff_eq_mul_left {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) :
a / b = c ↔ a = c * b :=
by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H'
protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) :
a = c * b :=
by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2]
protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) :
a / b = c :=
int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2])
theorem neg_div_of_dvd : ∀ {a b : ℤ} (H : b ∣ a), -a / b = -(a / b)
| ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else
by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz]
theorem div_sign : ∀ a b, a / sign b = a * sign b
| a (n+1:ℕ) := by unfold sign; simp
| a 0 := by simp [sign]
| a -[1+ n] := by simp [sign]
@[simp] theorem sign_mul : ∀ a b, sign (a * b) = sign a * sign b
| a 0 := by simp
| 0 b := by simp
| (m+1:ℕ) (n+1:ℕ) := rfl
| (m+1:ℕ) -[1+ n] := rfl
| -[1+ m] (n+1:ℕ) := rfl
| -[1+ m] -[1+ n] := rfl
protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / (abs a) :=
if az : a = 0 then by simp [az] else
(int.div_eq_of_eq_mul_left (mt eq_zero_of_abs_eq_zero az)
(sign_mul_abs _).symm).symm
theorem mul_sign : ∀ (i : ℤ), i * sign i = nat_abs i
| (n+1:ℕ) := mul_one _
| 0 := mul_zero _
| -[1+ n] := mul_neg_one _
theorem le_of_dvd {a b : ℤ} (bpos : b > 0) (H : a ∣ b) : a ≤ b :=
match a, b, eq_succ_of_zero_lt bpos, H with
| (m : ℕ), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $
nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H
| -[1+ m], ._, ⟨n, rfl⟩, _ :=
le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _)
end
theorem eq_one_of_dvd_one {a : ℤ} (H : a ≥ 0) (H' : a ∣ 1) : a = 1 :=
match a, eq_coe_of_zero_le H, H' with
| ._, ⟨n, rfl⟩, H' := congr_arg coe $
nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H'
end
theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : a ≥ 0) (H' : a * b = 1) : a = 1 :=
eq_one_of_dvd_one H ⟨b, H'.symm⟩
theorem eq_one_of_mul_eq_one_left {a b : ℤ} (H : b ≥ 0) (H' : a * b = 1) : b = 1 :=
eq_one_of_mul_eq_one_right H (by rw [mul_comm, H'])
lemma of_nat_dvd_of_dvd_nat_abs {a : ℕ} : ∀ {z : ℤ} (haz : a ∣ z.nat_abs), ↑a ∣ z
| (int.of_nat _) haz := int.coe_nat_dvd.2 haz
| -[1+k] haz :=
begin
change ↑a ∣ -(k+1 : ℤ),
apply dvd_neg_of_dvd,
apply int.coe_nat_dvd.2,
exact haz
end
lemma dvd_nat_abs_of_of_nat_dvd {a : ℕ} : ∀ {z : ℤ} (haz : ↑a ∣ z), a ∣ z.nat_abs
| (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz)
| -[1+k] haz :=
have haz' : (↑a:ℤ) ∣ (↑(k+1):ℤ), from dvd_of_dvd_neg haz,
int.coe_nat_dvd.1 haz'
lemma pow_div_of_le_of_pow_div_int {p m n : ℕ} {k : ℤ} (hmn : m ≤ n) (hdiv : ↑(p ^ n) ∣ k) :
↑(p ^ m) ∣ k :=
begin
induction k,
{ apply int.coe_nat_dvd.2,
apply pow_div_of_le_of_pow_div hmn,
apply int.coe_nat_dvd.1 hdiv },
{ change -[1+k] with -(↑(k+1) : ℤ),
apply dvd_neg_of_dvd,
apply int.coe_nat_dvd.2,
apply pow_div_of_le_of_pow_div hmn,
apply int.coe_nat_dvd.1,
apply dvd_of_dvd_neg,
exact hdiv }
end
/- / and ordering -/
protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a :=
le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H
protected theorem div_le_of_le_mul {a b c : ℤ} (H : c > 0) (H' : a ≤ b * c) : a / c ≤ b :=
le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H
protected theorem mul_lt_of_lt_div {a b c : ℤ} (H : c > 0) (H3 : a < b / c) : a * c < b :=
lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3)
protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : c > 0) (H2 : a ≤ b / c) : a * c ≤ b :=
le_trans (mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1))
protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : c > 0) (H2 : a * c ≤ b) : a ≤ b / c :=
le_of_lt_add_one $ lt_of_mul_lt_mul_right
(lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1)
protected theorem le_div_iff_mul_le {a b c : ℤ} (H : c > 0) : a ≤ b / c ↔ a * c ≤ b :=
⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩
protected theorem div_le_div {a b c : ℤ} (H : c > 0) (H' : a ≤ b) : a / c ≤ b / c :=
int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H')
protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : c > 0) (H' : a < b * c) : a / c < b :=
lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H')
protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : c > 0) (H2 : a / c < b) : a < b * c :=
lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2)
protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : c > 0) : a / c < b ↔ a < b * c :=
⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩
protected theorem le_mul_of_div_le {a b c : ℤ} (H1 : b ≥ 0) (H2 : b ∣ a) (H3 : a / b ≤ c) :
a ≤ c * b :=
by rw [← int.div_mul_cancel H2]; exact mul_le_mul_of_nonneg_right H3 H1
protected theorem lt_div_of_mul_lt {a b c : ℤ} (H1 : b ≥ 0) (H2 : b ∣ c) (H3 : a * b < c) :
a < c / b :=
lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3)
protected theorem lt_div_iff_mul_lt {a b : ℤ} (c : ℤ) (H : c > 0) (H' : c ∣ b) :
a < b / c ↔ a * c < b :=
⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩
theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : a > 0) (H2 : b ≥ 0) (H3 : b ∣ a) : a / b > 0 :=
int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul)
theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (H1 : b ∣ a) (H2 : d ∣ c) (H3 : b ≠ 0)
(H4 : d ≠ 0) (H5 : a * d = b * c) :
a / b = c / d :=
int.div_eq_of_eq_mul_right H3 $
by rw [← int.mul_div_assoc _ H2]; exact
(int.div_eq_of_eq_mul_left H4 H5.symm).symm
theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : ℤ} (hb : b ≠ 0) (hd : d ≠ 0) (hbc : b ∣ c)
(h : b * a = c * d) : a = c / b * d :=
begin
cases hbc with k hk,
subst hk,
rw int.mul_div_cancel_left, rw mul_assoc at h,
apply _root_.eq_of_mul_eq_mul_left _ h,
repeat {assumption}
end
theorem of_nat_add_neg_succ_of_nat_of_lt {m n : ℕ}
(h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] :=
begin
change sub_nat_nat _ _ = _,
have h' : n.succ - m = (n - m).succ,
apply succ_sub,
apply le_of_lt_succ h,
simp [*, sub_nat_nat]
end
theorem of_nat_add_neg_succ_of_nat_of_ge {m n : ℕ}
(h : m ≥ n.succ) : of_nat m + -[1+n] = of_nat (m - n.succ) :=
begin
change sub_nat_nat _ _ = _,
have h' : n.succ - m = 0,
apply sub_eq_zero_of_le h,
simp [*, sub_nat_nat]
end
@[simp] theorem neg_add_neg (m n : ℕ) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl
/- to_nat -/
theorem to_nat_eq_max : ∀ (a : ℤ), (to_nat a : ℤ) = max a 0
| (n : ℕ) := (max_eq_left (coe_zero_le n)).symm
| -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm
@[simp] theorem to_nat_of_nonneg {a : ℤ} (h : 0 ≤ a) : (to_nat a : ℤ) = a :=
by rw [to_nat_eq_max, max_eq_left h]
@[simp] theorem to_nat_coe_nat (n : ℕ) : to_nat ↑n = n := rfl
theorem le_to_nat (a : ℤ) : a ≤ to_nat a :=
by rw [to_nat_eq_max]; apply le_max_left
@[simp] theorem to_nat_le (a : ℤ) (n : ℕ) : to_nat a ≤ n ↔ a ≤ n :=
by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff];
exact and_iff_left (coe_zero_le _)
def to_nat' : ℤ → option ℕ
| (n : ℕ) := some n
| -[1+ n] := none
theorem mem_to_nat' : ∀ (a : ℤ) (n : ℕ), n ∈ to_nat' a ↔ a = n
| (m : ℕ) n := option.some_inj.trans coe_nat_inj'.symm
| -[1+ m] n := by split; intro h; cases h
/- units -/
@[simp] theorem units_nat_abs (u : units ℤ) : nat_abs u = 1 :=
units.ext_iff.1 $ nat.units_eq_one ⟨nat_abs u, nat_abs ↑u⁻¹,
by rw [← nat_abs_mul, units.mul_inv]; refl,
by rw [← nat_abs_mul, units.inv_mul]; refl⟩
theorem units_eq_one_or (u : units ℤ) : u = 1 ∨ u = -1 :=
by simpa [units.ext_iff, units_nat_abs] using nat_abs_eq u
lemma units_inv_eq_self (u : units ℤ) : u⁻¹ = u :=
(units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl)
/- bitwise ops -/
@[simp] lemma bodd_zero : bodd 0 = ff := rfl
@[simp] lemma bodd_one : bodd 1 = tt := rfl
@[simp] lemma bodd_two : bodd 2 = ff := rfl
@[simp] lemma bodd_sub_nat_nat (m n : ℕ) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd :=
by apply sub_nat_nat_elim m n (λ m n i, bodd i = bxor m.bodd n.bodd);
intros i m; simp [bodd]; cases i.bodd; cases m.bodd; refl
@[simp] lemma bodd_neg_of_nat (n : ℕ) : bodd (neg_of_nat n) = n.bodd :=
by cases n; simp; refl
@[simp] lemma bodd_neg (n : ℤ) : bodd (-n) = bodd n :=
by cases n; unfold has_neg.neg; simp [int.coe_nat_eq, int.neg, bodd]
@[simp] lemma bodd_add (m n : ℤ) : bodd (m + n) = bxor (bodd m) (bodd n) :=
by cases m with m m; cases n with n n; unfold has_add.add; simp [int.add, bodd];
cases m.bodd; cases n.bodd; refl
@[simp] lemma bodd_mul (m n : ℤ) : bodd (m * n) = bodd m && bodd n :=
by cases m with m m; cases n with n n; unfold has_mul.mul; simp [int.mul, bodd];
cases m.bodd; cases n.bodd; refl
theorem bodd_add_div2 : ∀ n, cond (bodd n) 1 0 + 2 * div2 n = n
| (n : ℕ) :=
by rw [show (cond (bodd n) 1 0 : ℤ) = (cond (bodd n) 1 0 : ℕ),
by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2
| -[1+ n] := begin
refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2),
dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul],
{ change -[1+ 2 * nat.div2 n] = _, rw zero_add },
{ rw [zero_add, add_comm], refl }
end
theorem div2_val : ∀ n, div2 n = n / 2
| (n : ℕ) := congr_arg of_nat n.div2_val
| -[1+ n] := congr_arg neg_succ_of_nat n.div2_val
lemma bit0_val (n : ℤ) : bit0 n = 2 * n := (two_mul _).symm
lemma bit1_val (n : ℤ) : bit1 n = 2 * n + 1 := congr_arg (+(1:ℤ)) (bit0_val _)
lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 :=
by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val }
lemma bit_decomp (n : ℤ) : bit (bodd n) (div2 n) = n :=
(bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _
def {u} bit_cases_on {C : ℤ → Sort u} (n) (h : ∀ b n, C (bit b n)) : C n :=
by rw [← bit_decomp n]; apply h
@[simp] lemma bit_zero : bit ff 0 = 0 := rfl
@[simp] lemma bit_coe_nat (b) (n : ℕ) : bit b n = nat.bit b n :=
by rw [bit_val, nat.bit_val]; cases b; refl
@[simp] lemma bit_neg_succ (b) (n : ℕ) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] :=
by rw [bit_val, nat.bit_val]; cases b; refl
@[simp] lemma bodd_bit (b n) : bodd (bit b n) = b :=
by rw bit_val; simp; cases b; cases bodd n; refl
@[simp] lemma div2_bit (b n) : div2 (bit b n) = n :=
begin
rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:ℤ) = 0), zero_add],
cases b, all_goals {exact dec_trivial}
end
@[simp] lemma test_bit_zero (b) : ∀ n, test_bit (bit b n) 0 = b
| (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_zero
| -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero];
clear test_bit_zero; cases b; refl
@[simp] lemma test_bit_succ (m b) : ∀ n, test_bit (bit b n) (nat.succ m) = test_bit n m
| (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_succ
| -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ]
private meta def bitwise_tac : tactic unit := `[
funext m,
funext n,
cases m with m m; cases n with n n; try {refl},
all_goals {
apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat,
try {dsimp [nat.land, nat.ldiff, nat.lor]},
try {rw [
show nat.bitwise (λ a b, a && bnot b) n m =
nat.bitwise (λ a b, b && bnot a) m n, from
congr_fun (congr_fun (@nat.bitwise_swap (λ a b, b && bnot a) rfl) n) m]},
apply congr_arg (λ f, nat.bitwise f m n),
funext a,
funext b,
cases a; cases b; refl
},
all_goals {unfold nat.land nat.ldiff nat.lor}
]
theorem bitwise_or : bitwise bor = lor := by bitwise_tac
theorem bitwise_and : bitwise band = land := by bitwise_tac
theorem bitwise_diff : bitwise (λ a b, a && bnot b) = ldiff := by bitwise_tac
theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac
@[simp] lemma bitwise_bit (f : bool → bool → bool) (a m b n) :
bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) :=
begin
cases m with m m; cases n with n n;
repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ };
unfold bitwise nat_bitwise bnot;
[ induction h : f ff ff,
induction h : f ff tt,
induction h : f tt ff,
induction h : f tt tt ],
all_goals {
unfold cond, rw nat.bitwise_bit,
repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } },
all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl }
end
@[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) :=
by rw [← bitwise_or, bitwise_bit]
@[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) :=
by rw [← bitwise_and, bitwise_bit]
@[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) :=
by rw [← bitwise_diff, bitwise_bit]
@[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) :=
by rw [← bitwise_xor, bitwise_bit]
@[simp] lemma lnot_bit (b) : ∀ n, lnot (bit b n) = bit (bnot b) (lnot n)
| (n : ℕ) := by simp [lnot]
| -[1+ n] := by simp [lnot]
@[simp] lemma test_bit_bitwise (f : bool → bool → bool) (m n k) :
test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) :=
begin
induction k with k IH generalizing m n;
apply bit_cases_on m; intros a m';
apply bit_cases_on n; intros b n';
rw bitwise_bit,
{ simp [test_bit_zero] },
{ simp [test_bit_succ, IH] }
end
@[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k :=
by rw [← bitwise_or, test_bit_bitwise]
@[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k :=
by rw [← bitwise_and, test_bit_bitwise]
@[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) :=
by rw [← bitwise_diff, test_bit_bitwise]
@[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) :=
by rw [← bitwise_xor, test_bit_bitwise]
@[simp] lemma test_bit_lnot : ∀ n k, test_bit (lnot n) k = bnot (test_bit n k)
| (n : ℕ) k := by simp [lnot, test_bit]
| -[1+ n] k := by simp [lnot, test_bit]
lemma shiftl_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), shiftl m (n + k) = shiftl (shiftl m n) k
| (m : ℕ) n (k:ℕ) := congr_arg of_nat (nat.shiftl_add _ _ _)
| -[1+ m] n (k:ℕ) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _)
| (m : ℕ) n -[1+k] := sub_nat_nat_elim n k.succ
(λ n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k)
(λ i n, congr_arg coe $
by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right)
(λ i n, congr_arg coe $
by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl)
| -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ
(λ n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k])
(λ i n, congr_arg neg_succ_of_nat $
by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right)
(λ i n, congr_arg neg_succ_of_nat $
by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl)
lemma shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k :=
shiftl_add _ _ _
@[simp] lemma shiftl_neg (m n : ℤ) : shiftl m (-n) = shiftr m n := rfl
@[simp] lemma shiftr_neg (m n : ℤ) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg]
@[simp] lemma shiftl_coe_nat (m n : ℕ) : shiftl m n = nat.shiftl m n := rfl
@[simp] lemma shiftr_coe_nat (m n : ℕ) : shiftr m n = nat.shiftr m n := by cases n; refl
@[simp] lemma shiftl_neg_succ (m n : ℕ) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl
@[simp] lemma shiftr_neg_succ (m n : ℕ) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl
lemma shiftr_add : ∀ (m : ℤ) (n k : ℕ), shiftr m (n + k) = shiftr (shiftr m n) k
| (m : ℕ) n k := by rw [shiftr_coe_nat, shiftr_coe_nat,
← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add]
| -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ,
← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add]
lemma shiftl_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), shiftl m n = m * ↑(2 ^ n)
| (m : ℕ) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _)
| -[1+ m] n := @congr_arg ℕ ℤ _ _ (λi, -i) (nat.shiftl'_tt_eq_mul_pow _ _)
lemma shiftr_eq_div_pow : ∀ (m : ℤ) (n : ℕ), shiftr m n = m / ↑(2 ^ n)
| (m : ℕ) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _)
| -[1+ m] n := begin
rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl,
exact coe_nat_lt_coe_nat_of_lt (nat.pos_pow_of_pos _ dec_trivial)
end
lemma one_shiftl (n : ℕ) : shiftl 1 n = (2 ^ n : ℕ) :=
congr_arg coe (nat.one_shiftl _)
@[simp] lemma zero_shiftl : ∀ n : ℤ, shiftl 0 n = 0
| (n : ℕ) := congr_arg coe (nat.zero_shiftl _)
| -[1+ n] := congr_arg coe (nat.zero_shiftr _)
@[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _
/- Least upper bound property for integers -/
theorem exists_least_of_bdd {P : ℤ → Prop} [HP : decidable_pred P]
(Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → b ≤ z)
(Hinh : ∃ z : ℤ, P z) : ∃ lb : ℤ, P lb ∧ (∀ z : ℤ, P z → lb ≤ z) :=
let ⟨b, Hb⟩ := Hbdd in
have EX : ∃ n : ℕ, P (b + n), from
let ⟨elt, Helt⟩ := Hinh in
match elt, le.dest (Hb _ Helt), Helt with
| ._, ⟨n, rfl⟩, Hn := ⟨n, Hn⟩
end,
⟨b + (nat.find EX : ℤ), nat.find_spec EX, λ z h,
match z, le.dest (Hb _ h), h with
| ._, ⟨n, rfl⟩, h := add_le_add_left
(int.coe_nat_le.2 $ nat.find_min' _ h) _
end⟩
theorem exists_greatest_of_bdd {P : ℤ → Prop} [HP : decidable_pred P]
(Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → z ≤ b)
(Hinh : ∃ z : ℤ, P z) : ∃ ub : ℤ, P ub ∧ (∀ z : ℤ, P z → z ≤ ub) :=
have Hbdd' : ∃ (b : ℤ), ∀ (z : ℤ), P (-z) → b ≤ z, from
let ⟨b, Hb⟩ := Hbdd in ⟨-b, λ z h, neg_le.1 (Hb _ h)⟩,
have Hinh' : ∃ z : ℤ, P (-z), from
let ⟨elt, Helt⟩ := Hinh in ⟨-elt, by rw [neg_neg]; exact Helt⟩,
let ⟨lb, Plb, al⟩ := exists_least_of_bdd Hbdd' Hinh' in
⟨-lb, Plb, λ z h, le_neg.1 $ al _ $ by rwa neg_neg⟩
/- cast (injection into groups with one) -/
@[simp] theorem nat_cast_eq_coe_nat : ∀ n,
@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n =
@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ int.has_coe)) n
| 0 := rfl
| (n+1) := congr_arg (+(1:ℤ)) (nat_cast_eq_coe_nat n)
section cast
variables {α : Type*}
section
variables [has_zero α] [has_one α] [has_add α] [has_neg α]
/-- Canonical homomorphism from the integers to any ring(-like) structure `α` -/
protected def cast : ℤ → α
| (n : ℕ) := n
| -[1+ n] := -(n+1)
@[priority 0] instance cast_coe : has_coe ℤ α := ⟨int.cast⟩
@[simp] theorem cast_zero : ((0 : ℤ) : α) = 0 := rfl
@[simp] theorem cast_of_nat (n : ℕ) : (of_nat n : α) = n := rfl
@[simp] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n := rfl
@[simp] theorem cast_coe_nat' (n : ℕ) :
(@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n : α) = n :=
by simp
@[simp] theorem cast_neg_succ_of_nat (n : ℕ) : (-[1+ n] : α) = -(n + 1) := rfl
end
@[simp] theorem cast_one [add_monoid α] [has_one α] [has_neg α] : ((1 : ℤ) : α) = 1 := nat.cast_one
@[simp] theorem cast_sub_nat_nat [add_group α] [has_one α] (m n) : ((int.sub_nat_nat m n : ℤ) : α) = m - n :=
begin
unfold sub_nat_nat, cases e : n - m,
{ simp [sub_nat_nat, e, nat.le_of_sub_eq_zero e] },
{ rw [sub_nat_nat, cast_neg_succ_of_nat, ← nat.cast_succ, ← e,
nat.cast_sub $ _root_.le_of_lt $ nat.lt_of_sub_eq_succ e, neg_sub] },
end
@[simp] theorem cast_neg_of_nat [add_group α] [has_one α] : ∀ n, ((neg_of_nat n : ℤ) : α) = -n
| 0 := neg_zero.symm
| (n+1) := rfl
@[simp] theorem cast_add [add_group α] [has_one α] : ∀ m n, ((m + n : ℤ) : α) = m + n
| (m : ℕ) (n : ℕ) := nat.cast_add _ _
| (m : ℕ) -[1+ n] := cast_sub_nat_nat _ _
| -[1+ m] (n : ℕ) := (cast_sub_nat_nat _ _).trans $ sub_eq_of_eq_add $
show (n:α) = -(m+1) + n + (m+1),
by rw [add_assoc, ← cast_succ, ← nat.cast_add, add_comm,
nat.cast_add, cast_succ, neg_add_cancel_left]
| -[1+ m] -[1+ n] := show -((m + n + 1 + 1 : ℕ) : α) = -(m + 1) + -(n + 1),
by rw [← neg_add_rev, ← nat.cast_add_one, ← nat.cast_add_one, ← nat.cast_add];
apply congr_arg (λ x:ℕ, -(x:α)); simp
@[simp] theorem cast_neg [add_group α] [has_one α] : ∀ n, ((-n : ℤ) : α) = -n
| (n : ℕ) := cast_neg_of_nat _
| -[1+ n] := (neg_neg _).symm
theorem cast_sub [add_group α] [has_one α] (m n) : ((m - n : ℤ) : α) = m - n :=
by simp
@[simp] theorem cast_eq_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) = 0 ↔ n = 0 :=
⟨λ h, begin cases n,
{ exact congr_arg coe (nat.cast_eq_zero.1 h) },
{ rw [cast_neg_succ_of_nat, neg_eq_zero, ← cast_succ, nat.cast_eq_zero] at h,
contradiction }
end, λ h, by rw [h, cast_zero]⟩
@[simp] theorem cast_inj [add_group α] [has_one α] [char_zero α] {m n : ℤ} : (m : α) = n ↔ m = n :=
by rw [← sub_eq_zero, ← cast_sub, cast_eq_zero, sub_eq_zero]
theorem cast_injective [add_group α] [has_one α] [char_zero α] : function.injective (coe : ℤ → α)
| m n := cast_inj.1
@[simp] theorem cast_ne_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) ≠ 0 ↔ n ≠ 0 :=
not_congr cast_eq_zero
@[simp] theorem cast_mul [ring α] : ∀ m n, ((m * n : ℤ) : α) = m * n
| (m : ℕ) (n : ℕ) := nat.cast_mul _ _
| (m : ℕ) -[1+ n] := (cast_neg_of_nat _).trans $
show (-(m * (n + 1) : ℕ) : α) = m * -(n + 1),
by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_mul_neg]
| -[1+ m] (n : ℕ) := (cast_neg_of_nat _).trans $
show (-((m + 1) * n : ℕ) : α) = -(m + 1) * n,
by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_neg_mul]
| -[1+ m] -[1+ n] := show (((m + 1) * (n + 1) : ℕ) : α) = -(m + 1) * -(n + 1),
by rw [nat.cast_mul, nat.cast_add_one, nat.cast_add_one, neg_mul_neg]
theorem mul_cast_comm [ring α] (a : α) (n : ℤ) : a * n = n * a :=
by cases n; simp [nat.mul_cast_comm, left_distrib, right_distrib, *]
@[simp] theorem cast_bit0 [ring α] (n : ℤ) : ((bit0 n : ℤ) : α) = bit0 n := cast_add _ _
@[simp] theorem cast_bit1 [ring α] (n : ℤ) : ((bit1 n : ℤ) : α) = bit1 n :=
by rw [bit1, cast_add, cast_one, cast_bit0]; refl
theorem cast_nonneg [linear_ordered_ring α] : ∀ {n : ℤ}, (0 : α) ≤ n ↔ 0 ≤ n
| (n : ℕ) := by simp
| -[1+ n] := by simpa [not_le_of_gt (neg_succ_lt_zero n)] using
show -(n:α) < 1, from lt_of_le_of_lt (by simp) zero_lt_one
@[simp] theorem cast_le [linear_ordered_ring α] {m n : ℤ} : (m : α) ≤ n ↔ m ≤ n :=
by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg]
@[simp] theorem cast_lt [linear_ordered_ring α] {m n : ℤ} : (m : α) < n ↔ m < n :=
by simpa [-cast_le] using not_congr (@cast_le α _ n m)
@[simp] theorem cast_nonpos [linear_ordered_ring α] {n : ℤ} : (n : α) ≤ 0 ↔ n ≤ 0 :=
by rw [← cast_zero, cast_le]
@[simp] theorem cast_pos [linear_ordered_ring α] {n : ℤ} : (0 : α) < n ↔ 0 < n :=
by rw [← cast_zero, cast_lt]
@[simp] theorem cast_lt_zero [linear_ordered_ring α] {n : ℤ} : (n : α) < 0 ↔ n < 0 :=
by rw [← cast_zero, cast_lt]
theorem eq_cast [add_group α] [has_one α] (f : ℤ → α)
(H1 : f 1 = 1) (Hadd : ∀ x y, f (x + y) = f x + f y) (n : ℤ) : f n = n :=
begin
have H : ∀ (n : ℕ), f n = n :=
nat.eq_cast' (λ n, f n) H1 (λ x y, Hadd x y),
cases n, {apply H},
apply eq_neg_of_add_eq_zero,
rw [← nat.cast_zero, ← H 0, int.coe_nat_zero,
← show -[1+ n] + (↑n + 1) = 0, from neg_add_self (↑n+1),
Hadd, show f (n+1) = n+1, from H (n+1)]
end
@[simp] theorem cast_id (n : ℤ) : ↑n = n :=
(eq_cast id rfl (λ _ _, rfl) n).symm
@[simp] theorem cast_min [decidable_linear_ordered_comm_ring α] {a b : ℤ} : (↑(min a b) : α) = min a b :=
by by_cases a ≤ b; simp [h, min]
@[simp] theorem cast_max [decidable_linear_ordered_comm_ring α] {a b : ℤ} : (↑(max a b) : α) = max a b :=
by by_cases a ≤ b; simp [h, max]
@[simp] theorem cast_abs [decidable_linear_ordered_comm_ring α] {q : ℤ} : ((abs q : ℤ) : α) = abs q :=
by simp [abs]
end cast
end int
|
8462b2196e29c59ebe6644947a0837eba5d8a88b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/analysis/normed_space/operator_norm.lean | cdc5fddc547ac053cf3bbbd52072510b1cc596fe | [
"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 | 93,789 | lean | /-
Copyright (c) 2019 Jan-David Salchow. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo
-/
import algebra.algebra.tower
import analysis.asymptotics.asymptotics
import analysis.normed_space.linear_isometry
import topology.algebra.module.strong_topology
/-!
# Operator norm on the space of continuous linear maps
Define the operator norm on the space of continuous (semi)linear maps between normed spaces, and
prove its basic properties. In particular, show that this space is itself a normed space.
Since a lot of elementary properties don't require `‖x‖ = 0 → x = 0` we start setting up the
theory for `seminormed_add_comm_group` and we specialize to `normed_add_comm_group` at the end.
Note that most of statements that apply to semilinear maps only hold when the ring homomorphism
is isometric, as expressed by the typeclass `[ring_hom_isometric σ]`.
-/
noncomputable theory
open_locale classical nnreal topological_space
-- the `ₗ` subscript variables are for special cases about linear (as opposed to semilinear) maps
variables {𝕜 𝕜₂ 𝕜₃ E Eₗ F Fₗ G Gₗ 𝓕 : Type*}
section semi_normed
variables [seminormed_add_comm_group E] [seminormed_add_comm_group Eₗ] [seminormed_add_comm_group F]
[seminormed_add_comm_group Fₗ] [seminormed_add_comm_group G] [seminormed_add_comm_group Gₗ]
open metric continuous_linear_map
section normed_field
/-! Most statements in this file require the field to be non-discrete,
as this is necessary to deduce an inequality `‖f x‖ ≤ C ‖x‖` from the continuity of f.
However, the other direction always holds.
In this section, we just assume that `𝕜` is a normed field.
In the remainder of the file, it will be non-discrete. -/
variables [normed_field 𝕜] [normed_field 𝕜₂] [normed_space 𝕜 E] [normed_space 𝕜₂ F]
variables [normed_space 𝕜 G] {σ : 𝕜 →+* 𝕜₂} (f : E →ₛₗ[σ] F)
/-- Construct a continuous linear map from a linear map and a bound on this linear map.
The fact that the norm of the continuous linear map is then controlled is given in
`linear_map.mk_continuous_norm_le`. -/
def linear_map.mk_continuous (C : ℝ) (h : ∀x, ‖f x‖ ≤ C * ‖x‖) : E →SL[σ] F :=
⟨f, add_monoid_hom_class.continuous_of_bound f C h⟩
/-- Reinterpret a linear map `𝕜 →ₗ[𝕜] E` as a continuous linear map. This construction
is generalized to the case of any finite dimensional domain
in `linear_map.to_continuous_linear_map`. -/
def linear_map.to_continuous_linear_map₁ (f : 𝕜 →ₗ[𝕜] E) : 𝕜 →L[𝕜] E :=
f.mk_continuous (‖f 1‖) $ λ x, le_of_eq $
by { conv_lhs { rw ← mul_one x }, rw [← smul_eq_mul, f.map_smul, norm_smul, mul_comm] }
/-- Construct a continuous linear map from a linear map and the existence of a bound on this linear
map. If you have an explicit bound, use `linear_map.mk_continuous` instead, as a norm estimate will
follow automatically in `linear_map.mk_continuous_norm_le`. -/
def linear_map.mk_continuous_of_exists_bound (h : ∃C, ∀x, ‖f x‖ ≤ C * ‖x‖) : E →SL[σ] F :=
⟨f, let ⟨C, hC⟩ := h in add_monoid_hom_class.continuous_of_bound f C hC⟩
lemma continuous_of_linear_of_boundₛₗ {f : E → F} (h_add : ∀ x y, f (x + y) = f x + f y)
(h_smul : ∀ (c : 𝕜) x, f (c • x) = (σ c) • f x) {C : ℝ} (h_bound : ∀ x, ‖f x‖ ≤ C*‖x‖) :
continuous f :=
let φ : E →ₛₗ[σ] F := { to_fun := f, map_add' := h_add, map_smul' := h_smul } in
add_monoid_hom_class.continuous_of_bound φ C h_bound
lemma continuous_of_linear_of_bound {f : E → G} (h_add : ∀ x y, f (x + y) = f x + f y)
(h_smul : ∀ (c : 𝕜) x, f (c • x) = c • f x) {C : ℝ} (h_bound : ∀ x, ‖f x‖ ≤ C*‖x‖) :
continuous f :=
let φ : E →ₗ[𝕜] G := { to_fun := f, map_add' := h_add, map_smul' := h_smul } in
add_monoid_hom_class.continuous_of_bound φ C h_bound
@[simp, norm_cast] lemma linear_map.mk_continuous_coe (C : ℝ) (h : ∀x, ‖f x‖ ≤ C * ‖x‖) :
((f.mk_continuous C h) : E →ₛₗ[σ] F) = f := rfl
@[simp] lemma linear_map.mk_continuous_apply (C : ℝ) (h : ∀x, ‖f x‖ ≤ C * ‖x‖) (x : E) :
f.mk_continuous C h x = f x := rfl
@[simp, norm_cast] lemma linear_map.mk_continuous_of_exists_bound_coe
(h : ∃C, ∀x, ‖f x‖ ≤ C * ‖x‖) :
((f.mk_continuous_of_exists_bound h) : E →ₛₗ[σ] F) = f := rfl
@[simp] lemma linear_map.mk_continuous_of_exists_bound_apply (h : ∃C, ∀x, ‖f x‖ ≤ C * ‖x‖) (x : E) :
f.mk_continuous_of_exists_bound h x = f x := rfl
@[simp] lemma linear_map.to_continuous_linear_map₁_coe (f : 𝕜 →ₗ[𝕜] E) :
(f.to_continuous_linear_map₁ : 𝕜 →ₗ[𝕜] E) = f :=
rfl
@[simp] lemma linear_map.to_continuous_linear_map₁_apply (f : 𝕜 →ₗ[𝕜] E) (x) :
f.to_continuous_linear_map₁ x = f x :=
rfl
end normed_field
variables [nontrivially_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
[nontrivially_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜 Eₗ] [normed_space 𝕜₂ F]
[normed_space 𝕜 Fₗ] [normed_space 𝕜₃ G] [normed_space 𝕜 Gₗ]
{σ₁₂ : 𝕜 →+* 𝕜₂} {σ₂₃ : 𝕜₂ →+* 𝕜₃} {σ₁₃ : 𝕜 →+* 𝕜₃}
[ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃]
/-- If `‖x‖ = 0` and `f` is continuous then `‖f x‖ = 0`. -/
lemma norm_image_of_norm_zero [semilinear_map_class 𝓕 σ₁₂ E F] (f : 𝓕)
(hf : continuous f) {x : E} (hx : ‖x‖ = 0) : ‖f x‖ = 0 :=
begin
refine le_antisymm (le_of_forall_pos_le_add (λ ε hε, _)) (norm_nonneg (f x)),
rcases normed_add_comm_group.tendsto_nhds_nhds.1 (hf.tendsto 0) ε hε with ⟨δ, δ_pos, hδ⟩,
replace hδ := hδ x,
rw [sub_zero, hx] at hδ,
replace hδ := le_of_lt (hδ δ_pos),
rw [map_zero, sub_zero] at hδ,
rwa [zero_add]
end
section
variables [ring_hom_isometric σ₁₂] [ring_hom_isometric σ₂₃]
lemma semilinear_map_class.bound_of_shell_semi_normed [semilinear_map_class 𝓕 σ₁₂ E F]
(f : 𝓕) {ε C : ℝ} (ε_pos : 0 < ε) {c : 𝕜} (hc : 1 < ‖c‖)
(hf : ∀ x, ε / ‖c‖ ≤ ‖x‖ → ‖x‖ < ε → ‖f x‖ ≤ C * ‖x‖) {x : E} (hx : ‖x‖ ≠ 0) :
‖f x‖ ≤ C * ‖x‖ :=
begin
rcases rescale_to_shell_semi_normed hc ε_pos hx with ⟨δ, hδ, δxle, leδx, δinv⟩,
have := hf (δ • x) leδx δxle,
simpa only [map_smulₛₗ, norm_smul, mul_left_comm C, mul_le_mul_left (norm_pos_iff.2 hδ),
ring_hom_isometric.is_iso] using hf (δ • x) leδx δxle
end
/-- A continuous linear map between seminormed spaces is bounded when the field is nontrivially
normed. The continuity ensures boundedness on a ball of some radius `ε`. The nontriviality of the
norm is then used to rescale any element into an element of norm in `[ε/C, ε]`, whose image has a
controlled norm. The norm control for the original element follows by rescaling. -/
lemma semilinear_map_class.bound_of_continuous [semilinear_map_class 𝓕 σ₁₂ E F] (f : 𝓕)
(hf : continuous f) : ∃ C, 0 < C ∧ (∀ x : E, ‖f x‖ ≤ C * ‖x‖) :=
begin
rcases normed_add_comm_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one with ⟨ε, ε_pos, hε⟩,
simp only [sub_zero, map_zero] at hε,
rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩,
have : 0 < ‖c‖ / ε, from div_pos (zero_lt_one.trans hc) ε_pos,
refine ⟨‖c‖ / ε, this, λ x, _⟩,
by_cases hx : ‖x‖ = 0,
{ rw [hx, mul_zero],
exact le_of_eq (norm_image_of_norm_zero f hf hx) },
refine semilinear_map_class.bound_of_shell_semi_normed f ε_pos hc (λ x hle hlt, _) hx,
refine (hε _ hlt).le.trans _,
rwa [← div_le_iff' this, one_div_div]
end
end
namespace continuous_linear_map
theorem bound [ring_hom_isometric σ₁₂] (f : E →SL[σ₁₂] F) :
∃ C, 0 < C ∧ (∀ x : E, ‖f x‖ ≤ C * ‖x‖) :=
semilinear_map_class.bound_of_continuous f f.2
section
open filter
/-- A linear map which is a homothety is a continuous linear map.
Since the field `𝕜` need not have `ℝ` as a subfield, this theorem is not directly deducible from
the corresponding theorem about isometries plus a theorem about scalar multiplication. Likewise
for the other theorems about homotheties in this file.
-/
def of_homothety (f : E →ₛₗ[σ₁₂] F) (a : ℝ) (hf : ∀x, ‖f x‖ = a * ‖x‖) : E →SL[σ₁₂] F :=
f.mk_continuous a (λ x, le_of_eq (hf x))
variable (𝕜)
lemma to_span_singleton_homothety (x : E) (c : 𝕜) :
‖linear_map.to_span_singleton 𝕜 E x c‖ = ‖x‖ * ‖c‖ :=
by {rw mul_comm, exact norm_smul _ _}
/-- Given an element `x` of a normed space `E` over a field `𝕜`, the natural continuous
linear map from `𝕜` to `E` by taking multiples of `x`.-/
def to_span_singleton (x : E) : 𝕜 →L[𝕜] E :=
of_homothety (linear_map.to_span_singleton 𝕜 E x) ‖x‖ (to_span_singleton_homothety 𝕜 x)
lemma to_span_singleton_apply (x : E) (r : 𝕜) : to_span_singleton 𝕜 x r = r • x :=
by simp [to_span_singleton, of_homothety, linear_map.to_span_singleton]
lemma to_span_singleton_add (x y : E) :
to_span_singleton 𝕜 (x + y) = to_span_singleton 𝕜 x + to_span_singleton 𝕜 y :=
by { ext1, simp [to_span_singleton_apply], }
lemma to_span_singleton_smul' (𝕜') [normed_field 𝕜'] [normed_space 𝕜' E]
[smul_comm_class 𝕜 𝕜' E] (c : 𝕜') (x : E) :
to_span_singleton 𝕜 (c • x) = c • to_span_singleton 𝕜 x :=
by { ext1, rw [to_span_singleton_apply, smul_apply, to_span_singleton_apply, smul_comm], }
lemma to_span_singleton_smul (c : 𝕜) (x : E) :
to_span_singleton 𝕜 (c • x) = c • to_span_singleton 𝕜 x :=
to_span_singleton_smul' 𝕜 𝕜 c x
variables (𝕜 E)
/-- Given a unit-length element `x` of a normed space `E` over a field `𝕜`, the natural linear
isometry map from `𝕜` to `E` by taking multiples of `x`.-/
def _root_.linear_isometry.to_span_singleton {v : E} (hv : ‖v‖ = 1) : 𝕜 →ₗᵢ[𝕜] E :=
{ norm_map' := λ x, by simp [norm_smul, hv],
.. linear_map.to_span_singleton 𝕜 E v }
variables {𝕜 E}
@[simp] lemma _root_.linear_isometry.to_span_singleton_apply {v : E} (hv : ‖v‖ = 1) (a : 𝕜) :
linear_isometry.to_span_singleton 𝕜 E hv a = a • v :=
rfl
@[simp] lemma _root_.linear_isometry.coe_to_span_singleton {v : E} (hv : ‖v‖ = 1) :
(linear_isometry.to_span_singleton 𝕜 E hv).to_linear_map = linear_map.to_span_singleton 𝕜 E v :=
rfl
end
section op_norm
open set real
/-- The operator norm of a continuous linear map is the inf of all its bounds. -/
def op_norm (f : E →SL[σ₁₂] F) := Inf {c | 0 ≤ c ∧ ∀ x, ‖f x‖ ≤ c * ‖x‖}
instance has_op_norm : has_norm (E →SL[σ₁₂] F) := ⟨op_norm⟩
lemma norm_def (f : E →SL[σ₁₂] F) : ‖f‖ = Inf {c | 0 ≤ c ∧ ∀ x, ‖f x‖ ≤ c * ‖x‖} := rfl
-- So that invocations of `le_cInf` make sense: we show that the set of
-- bounds is nonempty and bounded below.
lemma bounds_nonempty [ring_hom_isometric σ₁₂] {f : E →SL[σ₁₂] F} :
∃ c, c ∈ { c | 0 ≤ c ∧ ∀ x, ‖f x‖ ≤ c * ‖x‖ } :=
let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩
lemma bounds_bdd_below {f : E →SL[σ₁₂] F} :
bdd_below { c | 0 ≤ c ∧ ∀ x, ‖f x‖ ≤ c * ‖x‖ } :=
⟨0, λ _ ⟨hn, _⟩, hn⟩
/-- If one controls the norm of every `A x`, then one controls the norm of `A`. -/
lemma op_norm_le_bound (f : E →SL[σ₁₂] F) {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ x, ‖f x‖ ≤ M * ‖x‖) :
‖f‖ ≤ M :=
cInf_le bounds_bdd_below ⟨hMp, hM⟩
/-- If one controls the norm of every `A x`, `‖x‖ ≠ 0`, then one controls the norm of `A`. -/
lemma op_norm_le_bound' (f : E →SL[σ₁₂] F) {M : ℝ} (hMp: 0 ≤ M)
(hM : ∀ x, ‖x‖ ≠ 0 → ‖f x‖ ≤ M * ‖x‖) :
‖f‖ ≤ M :=
op_norm_le_bound f hMp $ λ x, (ne_or_eq (‖x‖) 0).elim (hM x) $
λ h, by simp only [h, mul_zero, norm_image_of_norm_zero f f.2 h]
theorem op_norm_le_of_lipschitz {f : E →SL[σ₁₂] F} {K : ℝ≥0} (hf : lipschitz_with K f) :
‖f‖ ≤ K :=
f.op_norm_le_bound K.2 $ λ x, by simpa only [dist_zero_right, f.map_zero] using hf.dist_le_mul x 0
lemma op_norm_eq_of_bounds {φ : E →SL[σ₁₂] F} {M : ℝ} (M_nonneg : 0 ≤ M)
(h_above : ∀ x, ‖φ x‖ ≤ M*‖x‖) (h_below : ∀ N ≥ 0, (∀ x, ‖φ x‖ ≤ N*‖x‖) → M ≤ N) :
‖φ‖ = M :=
le_antisymm (φ.op_norm_le_bound M_nonneg h_above)
((le_cInf_iff continuous_linear_map.bounds_bdd_below ⟨M, M_nonneg, h_above⟩).mpr $
λ N ⟨N_nonneg, hN⟩, h_below N N_nonneg hN)
lemma op_norm_neg (f : E →SL[σ₁₂] F) : ‖-f‖ = ‖f‖ := by simp only [norm_def, neg_apply, norm_neg]
theorem antilipschitz_of_bound (f : E →SL[σ₁₂] F) {K : ℝ≥0} (h : ∀ x, ‖x‖ ≤ K * ‖f x‖) :
antilipschitz_with K f :=
add_monoid_hom_class.antilipschitz_of_bound _ h
lemma bound_of_antilipschitz (f : E →SL[σ₁₂] F) {K : ℝ≥0} (h : antilipschitz_with K f) (x) :
‖x‖ ≤ K * ‖f x‖ :=
add_monoid_hom_class.bound_of_antilipschitz _ h x
section
variables [ring_hom_isometric σ₁₂] [ring_hom_isometric σ₂₃]
(f g : E →SL[σ₁₂] F) (h : F →SL[σ₂₃] G) (x : E)
lemma op_norm_nonneg : 0 ≤ ‖f‖ :=
le_cInf bounds_nonempty (λ _ ⟨hx, _⟩, hx)
/-- The fundamental property of the operator norm: `‖f x‖ ≤ ‖f‖ * ‖x‖`. -/
theorem le_op_norm : ‖f x‖ ≤ ‖f‖ * ‖x‖ :=
begin
obtain ⟨C, Cpos, hC⟩ := f.bound,
replace hC := hC x,
by_cases h : ‖x‖ = 0,
{ rwa [h, mul_zero] at ⊢ hC },
have hlt : 0 < ‖x‖ := lt_of_le_of_ne (norm_nonneg x) (ne.symm h),
exact (div_le_iff hlt).mp (le_cInf bounds_nonempty (λ c ⟨_, hc⟩,
(div_le_iff hlt).mpr $ by { apply hc })),
end
theorem dist_le_op_norm (x y : E) : dist (f x) (f y) ≤ ‖f‖ * dist x y :=
by simp_rw [dist_eq_norm, ← map_sub, f.le_op_norm]
theorem le_op_norm_of_le {c : ℝ} {x} (h : ‖x‖ ≤ c) : ‖f x‖ ≤ ‖f‖ * c :=
le_trans (f.le_op_norm x) (mul_le_mul_of_nonneg_left h f.op_norm_nonneg)
theorem le_of_op_norm_le {c : ℝ} (h : ‖f‖ ≤ c) (x : E) : ‖f x‖ ≤ c * ‖x‖ :=
(f.le_op_norm x).trans (mul_le_mul_of_nonneg_right h (norm_nonneg x))
lemma ratio_le_op_norm : ‖f x‖ / ‖x‖ ≤ ‖f‖ :=
div_le_of_nonneg_of_le_mul (norm_nonneg _) f.op_norm_nonneg (le_op_norm _ _)
/-- The image of the unit ball under a continuous linear map is bounded. -/
lemma unit_le_op_norm : ‖x‖ ≤ 1 → ‖f x‖ ≤ ‖f‖ :=
mul_one ‖f‖ ▸ f.le_op_norm_of_le
lemma op_norm_le_of_shell {f : E →SL[σ₁₂] F} {ε C : ℝ} (ε_pos : 0 < ε) (hC : 0 ≤ C)
{c : 𝕜} (hc : 1 < ‖c‖) (hf : ∀ x, ε / ‖c‖ ≤ ‖x‖ → ‖x‖ < ε → ‖f x‖ ≤ C * ‖x‖) :
‖f‖ ≤ C :=
f.op_norm_le_bound' hC $ λ x hx, semilinear_map_class.bound_of_shell_semi_normed f ε_pos hc hf hx
lemma op_norm_le_of_ball {f : E →SL[σ₁₂] F} {ε : ℝ} {C : ℝ} (ε_pos : 0 < ε) (hC : 0 ≤ C)
(hf : ∀ x ∈ ball (0 : E) ε, ‖f x‖ ≤ C * ‖x‖) : ‖f‖ ≤ C :=
begin
rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩,
refine op_norm_le_of_shell ε_pos hC hc (λ x _ hx, hf x _),
rwa ball_zero_eq
end
lemma op_norm_le_of_nhds_zero {f : E →SL[σ₁₂] F} {C : ℝ} (hC : 0 ≤ C)
(hf : ∀ᶠ x in 𝓝 (0 : E), ‖f x‖ ≤ C * ‖x‖) : ‖f‖ ≤ C :=
let ⟨ε, ε0, hε⟩ := metric.eventually_nhds_iff_ball.1 hf in op_norm_le_of_ball ε0 hC hε
lemma op_norm_le_of_shell' {f : E →SL[σ₁₂] F} {ε C : ℝ} (ε_pos : 0 < ε) (hC : 0 ≤ C)
{c : 𝕜} (hc : ‖c‖ < 1) (hf : ∀ x, ε * ‖c‖ ≤ ‖x‖ → ‖x‖ < ε → ‖f x‖ ≤ C * ‖x‖) :
‖f‖ ≤ C :=
begin
by_cases h0 : c = 0,
{ refine op_norm_le_of_ball ε_pos hC (λ x hx, hf x _ _),
{ simp [h0] },
{ rwa ball_zero_eq at hx } },
{ rw [← inv_inv c, norm_inv,
inv_lt_one_iff_of_pos (norm_pos_iff.2 $ inv_ne_zero h0)] at hc,
refine op_norm_le_of_shell ε_pos hC hc _,
rwa [norm_inv, div_eq_mul_inv, inv_inv] }
end
/-- For a continuous real linear map `f`, if one controls the norm of every `f x`, `‖x‖ = 1`, then
one controls the norm of `f`. -/
lemma op_norm_le_of_unit_norm [normed_space ℝ E] [normed_space ℝ F] {f : E →L[ℝ] F} {C : ℝ}
(hC : 0 ≤ C) (hf : ∀ x, ‖x‖ = 1 → ‖f x‖ ≤ C) : ‖f‖ ≤ C :=
begin
refine op_norm_le_bound' f hC (λ x hx, _),
have H₁ : ‖(‖x‖⁻¹ • x)‖ = 1, by rw [norm_smul, norm_inv, norm_norm, inv_mul_cancel hx],
have H₂ := hf _ H₁,
rwa [map_smul, norm_smul, norm_inv, norm_norm, ← div_eq_inv_mul, div_le_iff] at H₂,
exact (norm_nonneg x).lt_of_ne' hx
end
/-- The operator norm satisfies the triangle inequality. -/
theorem op_norm_add_le : ‖f + g‖ ≤ ‖f‖ + ‖g‖ :=
(f + g).op_norm_le_bound (add_nonneg f.op_norm_nonneg g.op_norm_nonneg) $
λ x, (norm_add_le_of_le (f.le_op_norm x) (g.le_op_norm x)).trans_eq (add_mul _ _ _).symm
/-- The norm of the `0` operator is `0`. -/
theorem op_norm_zero : ‖(0 : E →SL[σ₁₂] F)‖ = 0 :=
le_antisymm (cInf_le bounds_bdd_below
⟨le_rfl, λ _, le_of_eq (by { rw [zero_mul], exact norm_zero })⟩)
(op_norm_nonneg _)
/-- The norm of the identity is at most `1`. It is in fact `1`, except when the space is trivial
where it is `0`. It means that one can not do better than an inequality in general. -/
lemma norm_id_le : ‖id 𝕜 E‖ ≤ 1 :=
op_norm_le_bound _ zero_le_one (λx, by simp)
/-- If there is an element with norm different from `0`, then the norm of the identity equals `1`.
(Since we are working with seminorms supposing that the space is non-trivial is not enough.) -/
lemma norm_id_of_nontrivial_seminorm (h : ∃ (x : E), ‖x‖ ≠ 0) : ‖id 𝕜 E‖ = 1 :=
le_antisymm norm_id_le $ let ⟨x, hx⟩ := h in
have _ := (id 𝕜 E).ratio_le_op_norm x,
by rwa [id_apply, div_self hx] at this
lemma op_norm_smul_le {𝕜' : Type*} [normed_field 𝕜'] [normed_space 𝕜' F]
[smul_comm_class 𝕜₂ 𝕜' F] (c : 𝕜') (f : E →SL[σ₁₂] F) : ‖c • f‖ ≤ ‖c‖ * ‖f‖ :=
((c • f).op_norm_le_bound
(mul_nonneg (norm_nonneg _) (op_norm_nonneg _)) (λ _,
begin
erw [norm_smul, mul_assoc],
exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _)
end))
/-- Continuous linear maps themselves form a seminormed space with respect to
the operator norm. This is only a temporary definition because we want to replace the topology
with `continuous_linear_map.topological_space` to avoid diamond issues.
See Note [forgetful inheritance] -/
protected def tmp_seminormed_add_comm_group : seminormed_add_comm_group (E →SL[σ₁₂] F) :=
add_group_seminorm.to_seminormed_add_comm_group
{ to_fun := norm,
map_zero' := op_norm_zero,
add_le' := op_norm_add_le,
neg' := op_norm_neg }
/-- The `pseudo_metric_space` structure on `E →SL[σ₁₂] F` coming from
`continuous_linear_map.tmp_seminormed_add_comm_group`.
See Note [forgetful inheritance] -/
protected def tmp_pseudo_metric_space : pseudo_metric_space (E →SL[σ₁₂] F) :=
continuous_linear_map.tmp_seminormed_add_comm_group.to_pseudo_metric_space
/-- The `uniform_space` structure on `E →SL[σ₁₂] F` coming from
`continuous_linear_map.tmp_seminormed_add_comm_group`.
See Note [forgetful inheritance] -/
protected def tmp_uniform_space : uniform_space (E →SL[σ₁₂] F) :=
continuous_linear_map.tmp_pseudo_metric_space.to_uniform_space
/-- The `topological_space` structure on `E →SL[σ₁₂] F` coming from
`continuous_linear_map.tmp_seminormed_add_comm_group`.
See Note [forgetful inheritance] -/
protected def tmp_topological_space : topological_space (E →SL[σ₁₂] F) :=
continuous_linear_map.tmp_uniform_space.to_topological_space
section tmp
local attribute [-instance] continuous_linear_map.topological_space
local attribute [-instance] continuous_linear_map.uniform_space
local attribute [instance] continuous_linear_map.tmp_seminormed_add_comm_group
protected lemma tmp_topological_add_group : topological_add_group (E →SL[σ₁₂] F) :=
infer_instance
protected lemma tmp_closed_ball_div_subset {a b : ℝ} (ha : 0 < a) (hb : 0 < b) :
closed_ball (0 : E →SL[σ₁₂] F) (a / b) ⊆
{f | ∀ x ∈ closed_ball (0 : E) b, f x ∈ closed_ball (0 : F) a} :=
begin
intros f hf x hx,
rw mem_closed_ball_zero_iff at ⊢ hf hx,
calc ‖f x‖
≤ ‖f‖ * ‖x‖ : le_op_norm _ _
... ≤ (a/b) * b : mul_le_mul hf hx (norm_nonneg _) (div_pos ha hb).le
... = a : div_mul_cancel a hb.ne.symm
end
end tmp
protected theorem tmp_topology_eq :
(continuous_linear_map.tmp_topological_space : topological_space (E →SL[σ₁₂] F)) =
continuous_linear_map.topological_space :=
begin
refine continuous_linear_map.tmp_topological_add_group.ext infer_instance
((@metric.nhds_basis_closed_ball _ continuous_linear_map.tmp_pseudo_metric_space 0).ext
(continuous_linear_map.has_basis_nhds_zero_of_basis metric.nhds_basis_closed_ball) _ _),
{ rcases normed_field.exists_norm_lt_one 𝕜 with ⟨c, hc₀, hc₁⟩,
refine λ ε hε, ⟨⟨closed_ball 0 (1 / ‖c‖), ε⟩,
⟨normed_space.is_vonN_bounded_closed_ball _ _ _, hε⟩, λ f hf, _⟩,
change ∀ x, _ at hf,
simp_rw mem_closed_ball_zero_iff at hf,
rw @mem_closed_ball_zero_iff _ seminormed_add_comm_group.to_seminormed_add_group,
refine op_norm_le_of_shell' (div_pos one_pos hc₀) hε.le hc₁ (λ x hx₁ hxc, _),
rw div_mul_cancel 1 hc₀.ne.symm at hx₁,
exact (hf x hxc.le).trans (le_mul_of_one_le_right hε.le hx₁) },
{ rintros ⟨S, ε⟩ ⟨hS, hε⟩,
rw [normed_space.is_vonN_bounded_iff, ← bounded_iff_is_bounded] at hS,
rcases hS.subset_ball_lt 0 0 with ⟨δ, hδ, hSδ⟩,
exact ⟨ε/δ, div_pos hε hδ, (continuous_linear_map.tmp_closed_ball_div_subset hε hδ).trans $
λ f hf x hx, hf x $ hSδ hx⟩ }
end
protected theorem tmp_uniform_space_eq :
(continuous_linear_map.tmp_uniform_space : uniform_space (E →SL[σ₁₂] F)) =
continuous_linear_map.uniform_space :=
begin
rw [← @uniform_add_group.to_uniform_space_eq _ continuous_linear_map.tmp_uniform_space,
← @uniform_add_group.to_uniform_space_eq _ continuous_linear_map.uniform_space],
congr' 1,
exact continuous_linear_map.tmp_topology_eq
end
instance to_pseudo_metric_space : pseudo_metric_space (E →SL[σ₁₂] F) :=
continuous_linear_map.tmp_pseudo_metric_space.replace_uniformity
(congr_arg _ continuous_linear_map.tmp_uniform_space_eq.symm)
/-- Continuous linear maps themselves form a seminormed space with respect to
the operator norm. -/
instance to_seminormed_add_comm_group : seminormed_add_comm_group (E →SL[σ₁₂] F) :=
{ dist_eq := continuous_linear_map.tmp_seminormed_add_comm_group.dist_eq }
lemma nnnorm_def (f : E →SL[σ₁₂] F) : ‖f‖₊ = Inf {c | ∀ x, ‖f x‖₊ ≤ c * ‖x‖₊} :=
begin
ext,
rw [nnreal.coe_Inf, coe_nnnorm, norm_def, nnreal.coe_image],
simp_rw [← nnreal.coe_le_coe, nnreal.coe_mul, coe_nnnorm, mem_set_of_eq, subtype.coe_mk,
exists_prop],
end
/-- If one controls the norm of every `A x`, then one controls the norm of `A`. -/
lemma op_nnnorm_le_bound (f : E →SL[σ₁₂] F) (M : ℝ≥0) (hM : ∀ x, ‖f x‖₊ ≤ M * ‖x‖₊) :
‖f‖₊ ≤ M :=
op_norm_le_bound f (zero_le M) hM
/-- If one controls the norm of every `A x`, `‖x‖₊ ≠ 0`, then one controls the norm of `A`. -/
lemma op_nnnorm_le_bound' (f : E →SL[σ₁₂] F) (M : ℝ≥0) (hM : ∀ x, ‖x‖₊ ≠ 0 → ‖f x‖₊ ≤ M * ‖x‖₊) :
‖f‖₊ ≤ M :=
op_norm_le_bound' f (zero_le M) $ λ x hx, hM x $ by rwa [← nnreal.coe_ne_zero]
/-- For a continuous real linear map `f`, if one controls the norm of every `f x`, `‖x‖₊ = 1`, then
one controls the norm of `f`. -/
lemma op_nnnorm_le_of_unit_nnnorm [normed_space ℝ E] [normed_space ℝ F] {f : E →L[ℝ] F} {C : ℝ≥0}
(hf : ∀ x, ‖x‖₊ = 1 → ‖f x‖₊ ≤ C) : ‖f‖₊ ≤ C :=
op_norm_le_of_unit_norm C.coe_nonneg $ λ x hx, hf x $ by rwa [← nnreal.coe_eq_one]
theorem op_nnnorm_le_of_lipschitz {f : E →SL[σ₁₂] F} {K : ℝ≥0} (hf : lipschitz_with K f) :
‖f‖₊ ≤ K :=
op_norm_le_of_lipschitz hf
lemma op_nnnorm_eq_of_bounds {φ : E →SL[σ₁₂] F} (M : ℝ≥0)
(h_above : ∀ x, ‖φ x‖ ≤ M*‖x‖) (h_below : ∀ N, (∀ x, ‖φ x‖₊ ≤ N*‖x‖₊) → M ≤ N) :
‖φ‖₊ = M :=
subtype.ext $ op_norm_eq_of_bounds (zero_le M) h_above $ subtype.forall'.mpr h_below
instance to_normed_space {𝕜' : Type*} [normed_field 𝕜'] [normed_space 𝕜' F]
[smul_comm_class 𝕜₂ 𝕜' F] : normed_space 𝕜' (E →SL[σ₁₂] F) :=
⟨op_norm_smul_le⟩
include σ₁₃
/-- The operator norm is submultiplicative. -/
lemma op_norm_comp_le (f : E →SL[σ₁₂] F) : ‖h.comp f‖ ≤ ‖h‖ * ‖f‖ :=
(cInf_le bounds_bdd_below
⟨mul_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x,
by { rw mul_assoc, exact h.le_op_norm_of_le (f.le_op_norm x) } ⟩)
lemma op_nnnorm_comp_le [ring_hom_isometric σ₁₃] (f : E →SL[σ₁₂] F) : ‖h.comp f‖₊ ≤ ‖h‖₊ * ‖f‖₊ :=
op_norm_comp_le h f
omit σ₁₃
/-- Continuous linear maps form a seminormed ring with respect to the operator norm. -/
instance to_semi_normed_ring : semi_normed_ring (E →L[𝕜] E) :=
{ norm_mul := λ f g, op_norm_comp_le f g,
.. continuous_linear_map.to_seminormed_add_comm_group, .. continuous_linear_map.ring }
/-- For a normed space `E`, continuous linear endomorphisms form a normed algebra with
respect to the operator norm. -/
instance to_normed_algebra : normed_algebra 𝕜 (E →L[𝕜] E) :=
{ .. continuous_linear_map.to_normed_space,
.. continuous_linear_map.algebra }
theorem le_op_nnnorm : ‖f x‖₊ ≤ ‖f‖₊ * ‖x‖₊ := f.le_op_norm x
theorem nndist_le_op_nnnorm (x y : E) : nndist (f x) (f y) ≤ ‖f‖₊ * nndist x y :=
dist_le_op_norm f x y
/-- continuous linear maps are Lipschitz continuous. -/
theorem lipschitz : lipschitz_with ‖f‖₊ f :=
add_monoid_hom_class.lipschitz_of_bound_nnnorm f _ f.le_op_nnnorm
/-- Evaluation of a continuous linear map `f` at a point is Lipschitz continuous in `f`. -/
theorem lipschitz_apply (x : E) : lipschitz_with ‖x‖₊ (λ f : E →SL[σ₁₂] F, f x) :=
lipschitz_with_iff_norm_sub_le.2 $ λ f g, ((f - g).le_op_norm x).trans_eq (mul_comm _ _)
end
section Sup
variables [ring_hom_isometric σ₁₂]
lemma exists_mul_lt_apply_of_lt_op_nnnorm (f : E →SL[σ₁₂] F) {r : ℝ≥0} (hr : r < ‖f‖₊) :
∃ x, r * ‖x‖₊ < ‖f x‖₊ :=
by simpa only [not_forall, not_le, set.mem_set_of] using not_mem_of_lt_cInf
(nnnorm_def f ▸ hr : r < Inf {c : ℝ≥0 | ∀ x, ‖f x‖₊ ≤ c * ‖x‖₊}) (order_bot.bdd_below _)
lemma exists_mul_lt_of_lt_op_norm (f : E →SL[σ₁₂] F) {r : ℝ} (hr₀ : 0 ≤ r) (hr : r < ‖f‖) :
∃ x, r * ‖x‖ < ‖f x‖ :=
by { lift r to ℝ≥0 using hr₀, exact f.exists_mul_lt_apply_of_lt_op_nnnorm hr }
lemma exists_lt_apply_of_lt_op_nnnorm {𝕜 𝕜₂ E F : Type*} [normed_add_comm_group E]
[seminormed_add_comm_group F] [densely_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
{σ₁₂ : 𝕜 →+* 𝕜₂} [normed_space 𝕜 E] [normed_space 𝕜₂ F] [ring_hom_isometric σ₁₂]
(f : E →SL[σ₁₂] F) {r : ℝ≥0} (hr : r < ‖f‖₊) : ∃ x : E, ‖x‖₊ < 1 ∧ r < ‖f x‖₊ :=
begin
obtain ⟨y, hy⟩ := f.exists_mul_lt_apply_of_lt_op_nnnorm hr,
have hy' : ‖y‖₊ ≠ 0 := nnnorm_ne_zero_iff.2
(λ heq, by simpa only [heq, nnnorm_zero, map_zero, not_lt_zero'] using hy),
have hfy : ‖f y‖₊ ≠ 0 := (zero_le'.trans_lt hy).ne',
rw [←inv_inv (‖f y‖₊), nnreal.lt_inv_iff_mul_lt (inv_ne_zero hfy), mul_assoc, mul_comm (‖y‖₊),
←mul_assoc, ←nnreal.lt_inv_iff_mul_lt hy'] at hy,
obtain ⟨k, hk₁, hk₂⟩ := normed_field.exists_lt_nnnorm_lt 𝕜 hy,
refine ⟨k • y, (nnnorm_smul k y).symm ▸ (nnreal.lt_inv_iff_mul_lt hy').1 hk₂, _⟩,
have : ‖σ₁₂ k‖₊ = ‖k‖₊ := subtype.ext ring_hom_isometric.is_iso,
rwa [map_smulₛₗ f, nnnorm_smul, ←nnreal.div_lt_iff hfy, div_eq_mul_inv, this],
end
lemma exists_lt_apply_of_lt_op_norm {𝕜 𝕜₂ E F : Type*} [normed_add_comm_group E]
[seminormed_add_comm_group F] [densely_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
{σ₁₂ : 𝕜 →+* 𝕜₂} [normed_space 𝕜 E] [normed_space 𝕜₂ F] [ring_hom_isometric σ₁₂]
(f : E →SL[σ₁₂] F) {r : ℝ} (hr : r < ‖f‖) : ∃ x : E, ‖x‖ < 1 ∧ r < ‖f x‖ :=
begin
by_cases hr₀ : r < 0,
{ exact ⟨0, by simpa using hr₀⟩, },
{ lift r to ℝ≥0 using not_lt.1 hr₀,
exact f.exists_lt_apply_of_lt_op_nnnorm hr, }
end
lemma Sup_unit_ball_eq_nnnorm {𝕜 𝕜₂ E F : Type*} [normed_add_comm_group E]
[seminormed_add_comm_group F] [densely_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
{σ₁₂ : 𝕜 →+* 𝕜₂} [normed_space 𝕜 E] [normed_space 𝕜₂ F] [ring_hom_isometric σ₁₂]
(f : E →SL[σ₁₂] F) : Sup ((λ x, ‖f x‖₊) '' ball 0 1) = ‖f‖₊ :=
begin
refine cSup_eq_of_forall_le_of_forall_lt_exists_gt ((nonempty_ball.mpr zero_lt_one).image _)
_ (λ ub hub, _),
{ rintro - ⟨x, hx, rfl⟩,
simpa only [mul_one] using f.le_op_norm_of_le (mem_ball_zero_iff.1 hx).le },
{ obtain ⟨x, hx, hxf⟩ := f.exists_lt_apply_of_lt_op_nnnorm hub,
exact ⟨_, ⟨x, mem_ball_zero_iff.2 hx, rfl⟩, hxf⟩ },
end
lemma Sup_unit_ball_eq_norm {𝕜 𝕜₂ E F : Type*} [normed_add_comm_group E]
[seminormed_add_comm_group F] [densely_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
{σ₁₂ : 𝕜 →+* 𝕜₂} [normed_space 𝕜 E] [normed_space 𝕜₂ F] [ring_hom_isometric σ₁₂]
(f : E →SL[σ₁₂] F) : Sup ((λ x, ‖f x‖) '' ball 0 1) = ‖f‖ :=
by simpa only [nnreal.coe_Sup, set.image_image] using nnreal.coe_eq.2 f.Sup_unit_ball_eq_nnnorm
lemma Sup_closed_unit_ball_eq_nnnorm {𝕜 𝕜₂ E F : Type*} [normed_add_comm_group E]
[seminormed_add_comm_group F] [densely_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
{σ₁₂ : 𝕜 →+* 𝕜₂} [normed_space 𝕜 E] [normed_space 𝕜₂ F] [ring_hom_isometric σ₁₂]
(f : E →SL[σ₁₂] F) : Sup ((λ x, ‖f x‖₊) '' closed_ball 0 1) = ‖f‖₊ :=
begin
have hbdd : ∀ y ∈ (λ x, ‖f x‖₊) '' closed_ball 0 1, y ≤ ‖f‖₊,
{ rintro - ⟨x, hx, rfl⟩, exact f.unit_le_op_norm x (mem_closed_ball_zero_iff.1 hx) },
refine le_antisymm (cSup_le ((nonempty_closed_ball.mpr zero_le_one).image _) hbdd) _,
rw ←Sup_unit_ball_eq_nnnorm,
exact cSup_le_cSup ⟨‖f‖₊, hbdd⟩ ((nonempty_ball.2 zero_lt_one).image _)
(set.image_subset _ ball_subset_closed_ball),
end
lemma Sup_closed_unit_ball_eq_norm {𝕜 𝕜₂ E F : Type*} [normed_add_comm_group E]
[seminormed_add_comm_group F] [densely_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
{σ₁₂ : 𝕜 →+* 𝕜₂} [normed_space 𝕜 E] [normed_space 𝕜₂ F] [ring_hom_isometric σ₁₂]
(f : E →SL[σ₁₂] F) : Sup ((λ x, ‖f x‖) '' closed_ball 0 1) = ‖f‖ :=
by simpa only [nnreal.coe_Sup, set.image_image] using nnreal.coe_eq.2
f.Sup_closed_unit_ball_eq_nnnorm
end Sup
section
lemma op_norm_ext [ring_hom_isometric σ₁₃] (f : E →SL[σ₁₂] F) (g : E →SL[σ₁₃] G)
(h : ∀ x, ‖f x‖ = ‖g x‖) : ‖f‖ = ‖g‖ :=
op_norm_eq_of_bounds (norm_nonneg _) (λ x, by { rw h x, exact le_op_norm _ _ })
(λ c hc h₂, op_norm_le_bound _ hc (λ z, by { rw ←h z, exact h₂ z }))
variables [ring_hom_isometric σ₂₃]
theorem op_norm_le_bound₂ (f : E →SL[σ₁₃] F →SL[σ₂₃] G) {C : ℝ} (h0 : 0 ≤ C)
(hC : ∀ x y, ‖f x y‖ ≤ C * ‖x‖ * ‖y‖) :
‖f‖ ≤ C :=
f.op_norm_le_bound h0 $ λ x,
(f x).op_norm_le_bound (mul_nonneg h0 (norm_nonneg _)) $ hC x
theorem le_op_norm₂ [ring_hom_isometric σ₁₃] (f : E →SL[σ₁₃] F →SL[σ₂₃] G) (x : E) (y : F) :
‖f x y‖ ≤ ‖f‖ * ‖x‖ * ‖y‖ :=
(f x).le_of_op_norm_le (f.le_op_norm x) y
end
@[simp] lemma op_norm_prod (f : E →L[𝕜] Fₗ) (g : E →L[𝕜] Gₗ) : ‖f.prod g‖ = ‖(f, g)‖ :=
le_antisymm
(op_norm_le_bound _ (norm_nonneg _) $ λ x,
by simpa only [prod_apply, prod.norm_def, max_mul_of_nonneg, norm_nonneg]
using max_le_max (le_op_norm f x) (le_op_norm g x)) $
max_le
(op_norm_le_bound _ (norm_nonneg _) $ λ x, (le_max_left _ _).trans ((f.prod g).le_op_norm x))
(op_norm_le_bound _ (norm_nonneg _) $ λ x, (le_max_right _ _).trans ((f.prod g).le_op_norm x))
@[simp] lemma op_nnnorm_prod (f : E →L[𝕜] Fₗ) (g : E →L[𝕜] Gₗ) : ‖f.prod g‖₊ = ‖(f, g)‖₊ :=
subtype.ext $ op_norm_prod f g
/-- `continuous_linear_map.prod` as a `linear_isometry_equiv`. -/
def prodₗᵢ (R : Type*) [semiring R] [module R Fₗ] [module R Gₗ]
[has_continuous_const_smul R Fₗ] [has_continuous_const_smul R Gₗ]
[smul_comm_class 𝕜 R Fₗ] [smul_comm_class 𝕜 R Gₗ] :
(E →L[𝕜] Fₗ) × (E →L[𝕜] Gₗ) ≃ₗᵢ[R] (E →L[𝕜] Fₗ × Gₗ) :=
⟨prodₗ R, λ ⟨f, g⟩, op_norm_prod f g⟩
variables [ring_hom_isometric σ₁₂] (f : E →SL[σ₁₂] F)
@[simp, nontriviality] lemma op_norm_subsingleton [subsingleton E] : ‖f‖ = 0 :=
begin
refine le_antisymm _ (norm_nonneg _),
apply op_norm_le_bound _ rfl.ge,
intros x,
simp [subsingleton.elim x 0]
end
end op_norm
section is_O
variables [ring_hom_isometric σ₁₂]
(c : 𝕜) (f g : E →SL[σ₁₂] F) (h : F →SL[σ₂₃] G) (x y z : E)
open asymptotics
theorem is_O_with_id (l : filter E) : is_O_with ‖f‖ l f (λ x, x) :=
is_O_with_of_le' _ f.le_op_norm
theorem is_O_id (l : filter E) : f =O[l] (λ x, x) :=
(f.is_O_with_id l).is_O
theorem is_O_with_comp [ring_hom_isometric σ₂₃] {α : Type*} (g : F →SL[σ₂₃] G) (f : α → F)
(l : filter α) :
is_O_with ‖g‖ l (λ x', g (f x')) f :=
(g.is_O_with_id ⊤).comp_tendsto le_top
theorem is_O_comp [ring_hom_isometric σ₂₃] {α : Type*} (g : F →SL[σ₂₃] G) (f : α → F)
(l : filter α) :
(λ x', g (f x')) =O[l] f :=
(g.is_O_with_comp f l).is_O
theorem is_O_with_sub (f : E →SL[σ₁₂] F) (l : filter E) (x : E) :
is_O_with ‖f‖ l (λ x', f (x' - x)) (λ x', x' - x) :=
f.is_O_with_comp _ l
theorem is_O_sub (f : E →SL[σ₁₂] F) (l : filter E) (x : E) :
(λ x', f (x' - x)) =O[l] (λ x', x' - x) :=
f.is_O_comp _ l
end is_O
end continuous_linear_map
namespace linear_isometry
lemma norm_to_continuous_linear_map_le (f : E →ₛₗᵢ[σ₁₂] F) :
‖f.to_continuous_linear_map‖ ≤ 1 :=
f.to_continuous_linear_map.op_norm_le_bound zero_le_one $ λ x, by simp
end linear_isometry
namespace linear_map
/-- If a continuous linear map is constructed from a linear map via the constructor `mk_continuous`,
then its norm is bounded by the bound given to the constructor if it is nonnegative. -/
lemma mk_continuous_norm_le (f : E →ₛₗ[σ₁₂] F) {C : ℝ} (hC : 0 ≤ C) (h : ∀x, ‖f x‖ ≤ C * ‖x‖) :
‖f.mk_continuous C h‖ ≤ C :=
continuous_linear_map.op_norm_le_bound _ hC h
/-- If a continuous linear map is constructed from a linear map via the constructor `mk_continuous`,
then its norm is bounded by the bound or zero if bound is negative. -/
lemma mk_continuous_norm_le' (f : E →ₛₗ[σ₁₂] F) {C : ℝ} (h : ∀x, ‖f x‖ ≤ C * ‖x‖) :
‖f.mk_continuous C h‖ ≤ max C 0 :=
continuous_linear_map.op_norm_le_bound _ (le_max_right _ _) $ λ x, (h x).trans $
mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg x)
variables [ring_hom_isometric σ₂₃]
/-- Create a bilinear map (represented as a map `E →L[𝕜] F →L[𝕜] G`) from the corresponding linear
map and a bound on the norm of the image. The linear map can be constructed using
`linear_map.mk₂`. -/
def mk_continuous₂ (f : E →ₛₗ[σ₁₃] F →ₛₗ[σ₂₃] G) (C : ℝ)
(hC : ∀ x y, ‖f x y‖ ≤ C * ‖x‖ * ‖y‖) :
E →SL[σ₁₃] F →SL[σ₂₃] G :=
linear_map.mk_continuous
{ to_fun := λ x, (f x).mk_continuous (C * ‖x‖) (hC x),
map_add' := λ x y, by { ext z, simp },
map_smul' := λ c x, by { ext z, simp } }
(max C 0) $ λ x, (mk_continuous_norm_le' _ _).trans_eq $
by rw [max_mul_of_nonneg _ _ (norm_nonneg x), zero_mul]
@[simp] lemma mk_continuous₂_apply (f : E →ₛₗ[σ₁₃] F →ₛₗ[σ₂₃] G) {C : ℝ}
(hC : ∀ x y, ‖f x y‖ ≤ C * ‖x‖ * ‖y‖) (x : E) (y : F) :
f.mk_continuous₂ C hC x y = f x y :=
rfl
lemma mk_continuous₂_norm_le' (f : E →ₛₗ[σ₁₃] F →ₛₗ[σ₂₃] G) {C : ℝ}
(hC : ∀ x y, ‖f x y‖ ≤ C * ‖x‖ * ‖y‖) :
‖f.mk_continuous₂ C hC‖ ≤ max C 0 :=
mk_continuous_norm_le _ (le_max_iff.2 $ or.inr le_rfl) _
lemma mk_continuous₂_norm_le (f : E →ₛₗ[σ₁₃] F →ₛₗ[σ₂₃] G) {C : ℝ} (h0 : 0 ≤ C)
(hC : ∀ x y, ‖f x y‖ ≤ C * ‖x‖ * ‖y‖) :
‖f.mk_continuous₂ C hC‖ ≤ C :=
(f.mk_continuous₂_norm_le' hC).trans_eq $ max_eq_left h0
end linear_map
namespace continuous_linear_map
variables [ring_hom_isometric σ₂₃] [ring_hom_isometric σ₁₃]
/-- Flip the order of arguments of a continuous bilinear map.
For a version bundled as `linear_isometry_equiv`, see
`continuous_linear_map.flipL`. -/
def flip (f : E →SL[σ₁₃] F →SL[σ₂₃] G) : F →SL[σ₂₃] E →SL[σ₁₃] G :=
linear_map.mk_continuous₂
(linear_map.mk₂'ₛₗ σ₂₃ σ₁₃ (λ y x, f x y)
(λ x y z, (f z).map_add x y)
(λ c y x, (f x).map_smulₛₗ c y)
(λ z x y, by rw [f.map_add, add_apply])
(λ c y x, by rw [f.map_smulₛₗ, smul_apply]))
‖f‖
(λ y x, (f.le_op_norm₂ x y).trans_eq $ by rw mul_right_comm)
private lemma le_norm_flip (f : E →SL[σ₁₃] F →SL[σ₂₃] G) : ‖f‖ ≤ ‖flip f‖ :=
f.op_norm_le_bound₂ (norm_nonneg _) $ λ x y,
by { rw mul_right_comm, exact (flip f).le_op_norm₂ y x }
@[simp] lemma flip_apply (f : E →SL[σ₁₃] F →SL[σ₂₃] G) (x : E) (y : F) : f.flip y x = f x y := rfl
@[simp] lemma flip_flip (f : E →SL[σ₁₃] F →SL[σ₂₃] G) :
f.flip.flip = f :=
by { ext, refl }
@[simp] lemma op_norm_flip (f : E →SL[σ₁₃] F →SL[σ₂₃] G) :
‖f.flip‖ = ‖f‖ :=
le_antisymm (by simpa only [flip_flip] using le_norm_flip f.flip) (le_norm_flip f)
@[simp] lemma flip_add (f g : E →SL[σ₁₃] F →SL[σ₂₃] G) :
(f + g).flip = f.flip + g.flip :=
rfl
@[simp] lemma flip_smul (c : 𝕜₃) (f : E →SL[σ₁₃] F →SL[σ₂₃] G) :
(c • f).flip = c • f.flip :=
rfl
variables (E F G σ₁₃ σ₂₃)
/-- Flip the order of arguments of a continuous bilinear map.
This is a version bundled as a `linear_isometry_equiv`.
For an unbundled version see `continuous_linear_map.flip`. -/
def flipₗᵢ' : (E →SL[σ₁₃] F →SL[σ₂₃] G) ≃ₗᵢ[𝕜₃] (F →SL[σ₂₃] E →SL[σ₁₃] G) :=
{ to_fun := flip,
inv_fun := flip,
map_add' := flip_add,
map_smul' := flip_smul,
left_inv := flip_flip,
right_inv := flip_flip,
norm_map' := op_norm_flip }
variables {E F G σ₁₃ σ₂₃}
@[simp] lemma flipₗᵢ'_symm : (flipₗᵢ' E F G σ₂₃ σ₁₃).symm = flipₗᵢ' F E G σ₁₃ σ₂₃ := rfl
@[simp] lemma coe_flipₗᵢ' : ⇑(flipₗᵢ' E F G σ₂₃ σ₁₃) = flip := rfl
variables (𝕜 E Fₗ Gₗ)
/-- Flip the order of arguments of a continuous bilinear map.
This is a version bundled as a `linear_isometry_equiv`.
For an unbundled version see `continuous_linear_map.flip`. -/
def flipₗᵢ : (E →L[𝕜] Fₗ →L[𝕜] Gₗ) ≃ₗᵢ[𝕜] (Fₗ →L[𝕜] E →L[𝕜] Gₗ) :=
{ to_fun := flip,
inv_fun := flip,
map_add' := flip_add,
map_smul' := flip_smul,
left_inv := flip_flip,
right_inv := flip_flip,
norm_map' := op_norm_flip }
variables {𝕜 E Fₗ Gₗ}
@[simp] lemma flipₗᵢ_symm : (flipₗᵢ 𝕜 E Fₗ Gₗ).symm = flipₗᵢ 𝕜 Fₗ E Gₗ := rfl
@[simp] lemma coe_flipₗᵢ : ⇑(flipₗᵢ 𝕜 E Fₗ Gₗ) = flip := rfl
variables (F σ₁₂) [ring_hom_isometric σ₁₂]
/-- The continuous semilinear map obtained by applying a continuous semilinear map at a given
vector.
This is the continuous version of `linear_map.applyₗ`. -/
def apply' : E →SL[σ₁₂] (E →SL[σ₁₂] F) →L[𝕜₂] F := flip (id 𝕜₂ (E →SL[σ₁₂] F))
variables {F σ₁₂}
@[simp] lemma apply_apply' (v : E) (f : E →SL[σ₁₂] F) : apply' F σ₁₂ v f = f v := rfl
variables (𝕜 Fₗ)
/-- The continuous semilinear map obtained by applying a continuous semilinear map at a given
vector.
This is the continuous version of `linear_map.applyₗ`. -/
def apply : E →L[𝕜] (E →L[𝕜] Fₗ) →L[𝕜] Fₗ := flip (id 𝕜 (E →L[𝕜] Fₗ))
variables {𝕜 Fₗ}
@[simp] lemma apply_apply (v : E) (f : E →L[𝕜] Fₗ) : apply 𝕜 Fₗ v f = f v := rfl
variables (σ₁₂ σ₂₃ E F G)
/-- Composition of continuous semilinear maps as a continuous semibilinear map. -/
def compSL : (F →SL[σ₂₃] G) →L[𝕜₃] (E →SL[σ₁₂] F) →SL[σ₂₃] (E →SL[σ₁₃] G) :=
linear_map.mk_continuous₂
(linear_map.mk₂'ₛₗ (ring_hom.id 𝕜₃) σ₂₃ comp add_comp smul_comp comp_add
(λ c f g, by { ext, simp only [continuous_linear_map.map_smulₛₗ, coe_smul', coe_comp',
function.comp_app, pi.smul_apply] }))
1 $ λ f g, by simpa only [one_mul] using op_norm_comp_le f g
variables {𝕜 σ₁₂ σ₂₃ E F G}
include σ₁₃
@[simp] lemma compSL_apply (f : F →SL[σ₂₃] G) (g : E →SL[σ₁₂] F) :
compSL E F G σ₁₂ σ₂₃ f g = f.comp g := rfl
lemma _root_.continuous.const_clm_comp {X} [topological_space X] {f : X → E →SL[σ₁₂] F}
(hf : continuous f) (g : F →SL[σ₂₃] G) : continuous (λ x, g.comp (f x) : X → E →SL[σ₁₃] G) :=
(compSL E F G σ₁₂ σ₂₃ g).continuous.comp hf
-- Giving the implicit argument speeds up elaboration significantly
lemma _root_.continuous.clm_comp_const {X} [topological_space X] {g : X → F →SL[σ₂₃] G}
(hg : continuous g) (f : E →SL[σ₁₂] F) : continuous (λ x, (g x).comp f : X → E →SL[σ₁₃] G) :=
(@continuous_linear_map.flip _ _ _ _ _ (E →SL[σ₁₃] G) _ _ _ _ _ _ _ _ _ _ _ _ _
(compSL E F G σ₁₂ σ₂₃) f).continuous.comp hg
omit σ₁₃
variables (𝕜 σ₁₂ σ₂₃ E Fₗ Gₗ)
/-- Composition of continuous linear maps as a continuous bilinear map. -/
def compL : (Fₗ →L[𝕜] Gₗ) →L[𝕜] (E →L[𝕜] Fₗ) →L[𝕜] (E →L[𝕜] Gₗ) :=
compSL E Fₗ Gₗ (ring_hom.id 𝕜) (ring_hom.id 𝕜)
@[simp] lemma compL_apply (f : Fₗ →L[𝕜] Gₗ) (g : E →L[𝕜] Fₗ) : compL 𝕜 E Fₗ Gₗ f g = f.comp g := rfl
variables (Eₗ) {𝕜 E Fₗ Gₗ}
/-- Apply `L(x,-)` pointwise to bilinear maps, as a continuous bilinear map -/
@[simps apply]
def precompR (L : E →L[𝕜] Fₗ →L[𝕜] Gₗ) : E →L[𝕜] (Eₗ →L[𝕜] Fₗ) →L[𝕜] (Eₗ →L[𝕜] Gₗ) :=
(compL 𝕜 Eₗ Fₗ Gₗ).comp L
/-- Apply `L(-,y)` pointwise to bilinear maps, as a continuous bilinear map -/
def precompL (L : E →L[𝕜] Fₗ →L[𝕜] Gₗ) : (Eₗ →L[𝕜] E) →L[𝕜] Fₗ →L[𝕜] (Eₗ →L[𝕜] Gₗ) :=
(precompR Eₗ (flip L)).flip
section prod
universes u₁ u₂ u₃ u₄
variables (M₁ : Type u₁) [seminormed_add_comm_group M₁] [normed_space 𝕜 M₁]
(M₂ : Type u₂) [seminormed_add_comm_group M₂] [normed_space 𝕜 M₂]
(M₃ : Type u₃) [seminormed_add_comm_group M₃] [normed_space 𝕜 M₃]
(M₄ : Type u₄) [seminormed_add_comm_group M₄] [normed_space 𝕜 M₄]
variables {Eₗ} (𝕜)
/-- `continuous_linear_map.prod_map` as a continuous linear map. -/
def prod_mapL : ((M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄)) →L[𝕜] ((M₁ × M₃) →L[𝕜] (M₂ × M₄)) :=
continuous_linear_map.copy
(have Φ₁ : (M₁ →L[𝕜] M₂) →L[𝕜] (M₁ →L[𝕜] M₂ × M₄), from
continuous_linear_map.compL 𝕜 M₁ M₂ (M₂ × M₄) (continuous_linear_map.inl 𝕜 M₂ M₄),
have Φ₂ : (M₃ →L[𝕜] M₄) →L[𝕜] (M₃ →L[𝕜] M₂ × M₄), from
continuous_linear_map.compL 𝕜 M₃ M₄ (M₂ × M₄) (continuous_linear_map.inr 𝕜 M₂ M₄),
have Φ₁' : _, from (continuous_linear_map.compL 𝕜 (M₁ × M₃) M₁ (M₂ × M₄)).flip
(continuous_linear_map.fst 𝕜 M₁ M₃),
have Φ₂' : _ , from (continuous_linear_map.compL 𝕜 (M₁ × M₃) M₃ (M₂ × M₄)).flip
(continuous_linear_map.snd 𝕜 M₁ M₃),
have Ψ₁ : ((M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄)) →L[𝕜] (M₁ →L[𝕜] M₂), from
continuous_linear_map.fst 𝕜 (M₁ →L[𝕜] M₂) (M₃ →L[𝕜] M₄),
have Ψ₂ : ((M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄)) →L[𝕜] (M₃ →L[𝕜] M₄), from
continuous_linear_map.snd 𝕜 (M₁ →L[𝕜] M₂) (M₃ →L[𝕜] M₄),
Φ₁' ∘L Φ₁ ∘L Ψ₁ + Φ₂' ∘L Φ₂ ∘L Ψ₂)
(λ p : (M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄), p.1.prod_map p.2)
(begin
apply funext,
rintros ⟨φ, ψ⟩,
apply continuous_linear_map.ext (λ x, _),
simp only [add_apply, coe_comp', coe_fst', function.comp_app,
compL_apply, flip_apply, coe_snd', inl_apply, inr_apply, prod.mk_add_mk, add_zero,
zero_add, coe_prod_map', prod_map, prod.mk.inj_iff, eq_self_iff_true, and_self],
refl
end)
variables {M₁ M₂ M₃ M₄}
@[simp] lemma prod_mapL_apply (p : (M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄)) :
continuous_linear_map.prod_mapL 𝕜 M₁ M₂ M₃ M₄ p = p.1.prod_map p.2 :=
rfl
variables {X : Type*} [topological_space X]
lemma _root_.continuous.prod_mapL {f : X → M₁ →L[𝕜] M₂} {g : X → M₃ →L[𝕜] M₄}
(hf : continuous f) (hg : continuous g) : continuous (λ x, (f x).prod_map (g x)) :=
(prod_mapL 𝕜 M₁ M₂ M₃ M₄).continuous.comp (hf.prod_mk hg)
lemma _root_.continuous.prod_map_equivL {f : X → M₁ ≃L[𝕜] M₂} {g : X → M₃ ≃L[𝕜] M₄}
(hf : continuous (λ x, (f x : M₁ →L[𝕜] M₂))) (hg : continuous (λ x, (g x : M₃ →L[𝕜] M₄))) :
continuous (λ x, ((f x).prod (g x) : M₁ × M₃ →L[𝕜] M₂ × M₄)) :=
(prod_mapL 𝕜 M₁ M₂ M₃ M₄).continuous.comp (hf.prod_mk hg)
lemma _root_.continuous_on.prod_mapL {f : X → M₁ →L[𝕜] M₂} {g : X → M₃ →L[𝕜] M₄} {s : set X}
(hf : continuous_on f s) (hg : continuous_on g s) :
continuous_on (λ x, (f x).prod_map (g x)) s :=
((prod_mapL 𝕜 M₁ M₂ M₃ M₄).continuous.comp_continuous_on (hf.prod hg) : _)
lemma _root_.continuous_on.prod_map_equivL {f : X → M₁ ≃L[𝕜] M₂} {g : X → M₃ ≃L[𝕜] M₄} {s : set X}
(hf : continuous_on (λ x, (f x : M₁ →L[𝕜] M₂)) s)
(hg : continuous_on (λ x, (g x : M₃ →L[𝕜] M₄)) s) :
continuous_on (λ x, ((f x).prod (g x) : M₁ × M₃ →L[𝕜] M₂ × M₄)) s :=
(prod_mapL 𝕜 M₁ M₂ M₃ M₄).continuous.comp_continuous_on (hf.prod hg)
end prod
variables {𝕜 E Fₗ Gₗ}
section multiplication_linear
section non_unital
variables (𝕜) (𝕜' : Type*) [non_unital_semi_normed_ring 𝕜'] [normed_space 𝕜 𝕜']
[is_scalar_tower 𝕜 𝕜' 𝕜'] [smul_comm_class 𝕜 𝕜' 𝕜']
/-- Multiplication in a non-unital normed algebra as a continuous bilinear map. -/
def mul : 𝕜' →L[𝕜] 𝕜' →L[𝕜] 𝕜' := (linear_map.mul 𝕜 𝕜').mk_continuous₂ 1 $
λ x y, by simpa using norm_mul_le x y
@[simp] lemma mul_apply' (x y : 𝕜') : mul 𝕜 𝕜' x y = x * y := rfl
@[simp] lemma op_norm_mul_apply_le (x : 𝕜') : ‖mul 𝕜 𝕜' x‖ ≤ ‖x‖ :=
(op_norm_le_bound _ (norm_nonneg x) (norm_mul_le x))
/-- Simultaneous left- and right-multiplication in a non-unital normed algebra, considered as a
continuous trilinear map. This is akin to its non-continuous version `linear_map.mul_left_right`,
but there is a minor difference: `linear_map.mul_left_right` is uncurried. -/
def mul_left_right : 𝕜' →L[𝕜] 𝕜' →L[𝕜] 𝕜' →L[𝕜] 𝕜' :=
((compL 𝕜 𝕜' 𝕜' 𝕜').comp (mul 𝕜 𝕜').flip).flip.comp (mul 𝕜 𝕜')
@[simp] lemma mul_left_right_apply (x y z : 𝕜') :
mul_left_right 𝕜 𝕜' x y z = x * z * y := rfl
lemma op_norm_mul_left_right_apply_apply_le (x y : 𝕜') :
‖mul_left_right 𝕜 𝕜' x y‖ ≤ ‖x‖ * ‖y‖ :=
(op_norm_comp_le _ _).trans $ (mul_comm _ _).trans_le $
mul_le_mul (op_norm_mul_apply_le _ _ _)
(op_norm_le_bound _ (norm_nonneg _) (λ _, (norm_mul_le _ _).trans_eq (mul_comm _ _)))
(norm_nonneg _) (norm_nonneg _)
lemma op_norm_mul_left_right_apply_le (x : 𝕜') :
‖mul_left_right 𝕜 𝕜' x‖ ≤ ‖x‖ :=
op_norm_le_bound _ (norm_nonneg x) (op_norm_mul_left_right_apply_apply_le 𝕜 𝕜' x)
lemma op_norm_mul_left_right_le :
‖mul_left_right 𝕜 𝕜'‖ ≤ 1 :=
op_norm_le_bound _ zero_le_one (λ x, (one_mul ‖x‖).symm ▸ op_norm_mul_left_right_apply_le 𝕜 𝕜' x)
end non_unital
section unital
variables (𝕜) (𝕜' : Type*) [semi_normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] [norm_one_class 𝕜']
/-- Multiplication in a normed algebra as a linear isometry to the space of
continuous linear maps. -/
def mulₗᵢ : 𝕜' →ₗᵢ[𝕜] 𝕜' →L[𝕜] 𝕜' :=
{ to_linear_map := mul 𝕜 𝕜',
norm_map' := λ x, le_antisymm (op_norm_mul_apply_le _ _ _)
(by { convert ratio_le_op_norm _ (1 : 𝕜'), simp [norm_one],
apply_instance }) }
@[simp] lemma coe_mulₗᵢ : ⇑(mulₗᵢ 𝕜 𝕜') = mul 𝕜 𝕜' := rfl
@[simp] lemma op_norm_mul_apply (x : 𝕜') : ‖mul 𝕜 𝕜' x‖ = ‖x‖ :=
(mulₗᵢ 𝕜 𝕜').norm_map x
end unital
end multiplication_linear
section smul_linear
variables (𝕜) (𝕜' : Type*) [normed_field 𝕜'] [normed_algebra 𝕜 𝕜']
[normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E]
/-- Scalar multiplication as a continuous bilinear map. -/
def lsmul : 𝕜' →L[𝕜] E →L[𝕜] E :=
((algebra.lsmul 𝕜 E).to_linear_map : 𝕜' →ₗ[𝕜] E →ₗ[𝕜] E).mk_continuous₂ 1 $
λ c x, by simpa only [one_mul] using (norm_smul c x).le
@[simp] lemma lsmul_apply (c : 𝕜') (x : E) : lsmul 𝕜 𝕜' c x = c • x := rfl
variables {𝕜'}
lemma norm_to_span_singleton (x : E) : ‖to_span_singleton 𝕜 x‖ = ‖x‖ :=
begin
refine op_norm_eq_of_bounds (norm_nonneg _) (λ x, _) (λ N hN_nonneg h, _),
{ rw [to_span_singleton_apply, norm_smul, mul_comm], },
{ specialize h 1,
rw [to_span_singleton_apply, norm_smul, mul_comm] at h,
exact (mul_le_mul_right (by simp)).mp h, },
end
variables {𝕜}
lemma op_norm_lsmul_apply_le (x : 𝕜') : ‖(lsmul 𝕜 𝕜' x : E →L[𝕜] E)‖ ≤ ‖x‖ :=
continuous_linear_map.op_norm_le_bound _ (norm_nonneg x) $ λ y, (norm_smul x y).le
/-- The norm of `lsmul` is at most 1 in any semi-normed group. -/
lemma op_norm_lsmul_le : ‖(lsmul 𝕜 𝕜' : 𝕜' →L[𝕜] E →L[𝕜] E)‖ ≤ 1 :=
begin
refine continuous_linear_map.op_norm_le_bound _ zero_le_one (λ x, _),
simp_rw [one_mul],
exact op_norm_lsmul_apply_le _,
end
end smul_linear
section restrict_scalars
variables {𝕜' : Type*} [nontrivially_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜]
variables [normed_space 𝕜' E] [is_scalar_tower 𝕜' 𝕜 E]
variables [normed_space 𝕜' Fₗ] [is_scalar_tower 𝕜' 𝕜 Fₗ]
@[simp] lemma norm_restrict_scalars (f : E →L[𝕜] Fₗ) : ‖f.restrict_scalars 𝕜'‖ = ‖f‖ :=
le_antisymm (op_norm_le_bound _ (norm_nonneg _) $ λ x, f.le_op_norm x)
(op_norm_le_bound _ (norm_nonneg _) $ λ x, f.le_op_norm x)
variables (𝕜 E Fₗ 𝕜') (𝕜'' : Type*) [ring 𝕜''] [module 𝕜'' Fₗ]
[has_continuous_const_smul 𝕜'' Fₗ] [smul_comm_class 𝕜 𝕜'' Fₗ] [smul_comm_class 𝕜' 𝕜'' Fₗ]
/-- `continuous_linear_map.restrict_scalars` as a `linear_isometry`. -/
def restrict_scalars_isometry : (E →L[𝕜] Fₗ) →ₗᵢ[𝕜''] (E →L[𝕜'] Fₗ) :=
⟨restrict_scalarsₗ 𝕜 E Fₗ 𝕜' 𝕜'', norm_restrict_scalars⟩
variables {𝕜 E Fₗ 𝕜' 𝕜''}
@[simp] lemma coe_restrict_scalars_isometry :
⇑(restrict_scalars_isometry 𝕜 E Fₗ 𝕜' 𝕜'') = restrict_scalars 𝕜' :=
rfl
@[simp] lemma restrict_scalars_isometry_to_linear_map :
(restrict_scalars_isometry 𝕜 E Fₗ 𝕜' 𝕜'').to_linear_map = restrict_scalarsₗ 𝕜 E Fₗ 𝕜' 𝕜'' :=
rfl
variables (𝕜 E Fₗ 𝕜' 𝕜'')
/-- `continuous_linear_map.restrict_scalars` as a `continuous_linear_map`. -/
def restrict_scalarsL : (E →L[𝕜] Fₗ) →L[𝕜''] (E →L[𝕜'] Fₗ) :=
(restrict_scalars_isometry 𝕜 E Fₗ 𝕜' 𝕜'').to_continuous_linear_map
variables {𝕜 E Fₗ 𝕜' 𝕜''}
@[simp] lemma coe_restrict_scalarsL :
(restrict_scalarsL 𝕜 E Fₗ 𝕜' 𝕜'' : (E →L[𝕜] Fₗ) →ₗ[𝕜''] (E →L[𝕜'] Fₗ)) =
restrict_scalarsₗ 𝕜 E Fₗ 𝕜' 𝕜'' :=
rfl
@[simp] lemma coe_restrict_scalarsL' :
⇑(restrict_scalarsL 𝕜 E Fₗ 𝕜' 𝕜'') = restrict_scalars 𝕜' :=
rfl
end restrict_scalars
end continuous_linear_map
namespace submodule
lemma norm_subtypeL_le (K : submodule 𝕜 E) : ‖K.subtypeL‖ ≤ 1 :=
K.subtypeₗᵢ.norm_to_continuous_linear_map_le
end submodule
section has_sum
-- Results in this section hold for continuous additive monoid homomorphisms or equivalences but we
-- don't have bundled continuous additive homomorphisms.
variables {ι R R₂ M M₂ : Type*} [semiring R] [semiring R₂] [add_comm_monoid M] [module R M]
[add_comm_monoid M₂] [module R₂ M₂] [topological_space M] [topological_space M₂]
{σ : R →+* R₂} {σ' : R₂ →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ]
/-- Applying a continuous linear map commutes with taking an (infinite) sum. -/
protected lemma continuous_linear_map.has_sum {f : ι → M} (φ : M →SL[σ] M₂) {x : M}
(hf : has_sum f x) :
has_sum (λ (b:ι), φ (f b)) (φ x) :=
by simpa only using hf.map φ.to_linear_map.to_add_monoid_hom φ.continuous
alias continuous_linear_map.has_sum ← has_sum.mapL
protected lemma continuous_linear_map.summable {f : ι → M} (φ : M →SL[σ] M₂) (hf : summable f) :
summable (λ b:ι, φ (f b)) :=
(hf.has_sum.mapL φ).summable
alias continuous_linear_map.summable ← summable.mapL
protected lemma continuous_linear_map.map_tsum [t2_space M₂] {f : ι → M}
(φ : M →SL[σ] M₂) (hf : summable f) : φ (∑' z, f z) = ∑' z, φ (f z) :=
(hf.has_sum.mapL φ).tsum_eq.symm
include σ'
/-- Applying a continuous linear map commutes with taking an (infinite) sum. -/
protected lemma continuous_linear_equiv.has_sum {f : ι → M} (e : M ≃SL[σ] M₂) {y : M₂} :
has_sum (λ (b:ι), e (f b)) y ↔ has_sum f (e.symm y) :=
⟨λ h, by simpa only [e.symm.coe_coe, e.symm_apply_apply] using h.mapL (e.symm : M₂ →SL[σ'] M),
λ h, by simpa only [e.coe_coe, e.apply_symm_apply] using (e : M →SL[σ] M₂).has_sum h⟩
/-- Applying a continuous linear map commutes with taking an (infinite) sum. -/
protected lemma continuous_linear_equiv.has_sum' {f : ι → M} (e : M ≃SL[σ] M₂) {x : M} :
has_sum (λ (b:ι), e (f b)) (e x) ↔ has_sum f x :=
by rw [e.has_sum, continuous_linear_equiv.symm_apply_apply]
protected lemma continuous_linear_equiv.summable {f : ι → M} (e : M ≃SL[σ] M₂) :
summable (λ b:ι, e (f b)) ↔ summable f :=
⟨λ hf, (e.has_sum.1 hf.has_sum).summable, (e : M →SL[σ] M₂).summable⟩
lemma continuous_linear_equiv.tsum_eq_iff [t2_space M] [t2_space M₂] {f : ι → M}
(e : M ≃SL[σ] M₂) {y : M₂} : ∑' z, e (f z) = y ↔ ∑' z, f z = e.symm y :=
begin
by_cases hf : summable f,
{ exact ⟨λ h, (e.has_sum.mp ((e.summable.mpr hf).has_sum_iff.mpr h)).tsum_eq,
λ h, (e.has_sum.mpr (hf.has_sum_iff.mpr h)).tsum_eq⟩ },
{ have hf' : ¬summable (λ z, e (f z)) := λ h, hf (e.summable.mp h),
rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hf'],
exact ⟨by { rintro rfl, simp }, λ H, by simpa using (congr_arg (λ z, e z) H)⟩ }
end
protected lemma continuous_linear_equiv.map_tsum [t2_space M] [t2_space M₂] {f : ι → M}
(e : M ≃SL[σ] M₂) : e (∑' z, f z) = ∑' z, e (f z) :=
by { refine symm (e.tsum_eq_iff.mpr _), rw e.symm_apply_apply _ }
end has_sum
namespace continuous_linear_equiv
section
variables {σ₂₁ : 𝕜₂ →+* 𝕜} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂]
[ring_hom_isometric σ₁₂]
variables (e : E ≃SL[σ₁₂] F)
include σ₂₁
protected lemma lipschitz : lipschitz_with (‖(e : E →SL[σ₁₂] F)‖₊) e :=
(e : E →SL[σ₁₂] F).lipschitz
theorem is_O_comp {α : Type*} (f : α → E) (l : filter α) : (λ x', e (f x')) =O[l] f :=
(e : E →SL[σ₁₂] F).is_O_comp f l
theorem is_O_sub (l : filter E) (x : E) : (λ x', e (x' - x)) =O[l] (λ x', x' - x) :=
(e : E →SL[σ₁₂] F).is_O_sub l x
end
variables {σ₂₁ : 𝕜₂ →+* 𝕜} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂]
include σ₂₁
lemma homothety_inverse (a : ℝ) (ha : 0 < a) (f : E ≃ₛₗ[σ₁₂] F) :
(∀ (x : E), ‖f x‖ = a * ‖x‖) → (∀ (y : F), ‖f.symm y‖ = a⁻¹ * ‖y‖) :=
begin
intros hf y,
calc ‖(f.symm) y‖ = a⁻¹ * (a * ‖ (f.symm) y‖) : _
... = a⁻¹ * ‖f ((f.symm) y)‖ : by rw hf
... = a⁻¹ * ‖y‖ : by simp,
rw [← mul_assoc, inv_mul_cancel (ne_of_lt ha).symm, one_mul],
end
/-- A linear equivalence which is a homothety is a continuous linear equivalence. -/
def of_homothety (f : E ≃ₛₗ[σ₁₂] F) (a : ℝ) (ha : 0 < a) (hf : ∀x, ‖f x‖ = a * ‖x‖) :
E ≃SL[σ₁₂] F :=
{ to_linear_equiv := f,
continuous_to_fun := add_monoid_hom_class.continuous_of_bound f a (λ x, le_of_eq (hf x)),
continuous_inv_fun := add_monoid_hom_class.continuous_of_bound f.symm a⁻¹
(λ x, le_of_eq (homothety_inverse a ha f hf x)) }
variables [ring_hom_isometric σ₂₁] (e : E ≃SL[σ₁₂] F)
theorem is_O_comp_rev {α : Type*} (f : α → E) (l : filter α) : f =O[l] (λ x', e (f x')) :=
(e.symm.is_O_comp _ l).congr_left $ λ _, e.symm_apply_apply _
theorem is_O_sub_rev (l : filter E) (x : E) : (λ x', x' - x) =O[l] (λ x', e (x' - x)) :=
e.is_O_comp_rev _ _
omit σ₂₁
variable (𝕜)
lemma to_span_nonzero_singleton_homothety (x : E) (h : x ≠ 0) (c : 𝕜) :
‖linear_equiv.to_span_nonzero_singleton 𝕜 E x h c‖ = ‖x‖ * ‖c‖ :=
continuous_linear_map.to_span_singleton_homothety _ _ _
end continuous_linear_equiv
variables {σ₂₁ : 𝕜₂ →+* 𝕜} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂]
include σ₂₁
/-- Construct a continuous linear equivalence from a linear equivalence together with
bounds in both directions. -/
def linear_equiv.to_continuous_linear_equiv_of_bounds (e : E ≃ₛₗ[σ₁₂] F) (C_to C_inv : ℝ)
(h_to : ∀ x, ‖e x‖ ≤ C_to * ‖x‖) (h_inv : ∀ x : F, ‖e.symm x‖ ≤ C_inv * ‖x‖) : E ≃SL[σ₁₂] F :=
{ to_linear_equiv := e,
continuous_to_fun := add_monoid_hom_class.continuous_of_bound e C_to h_to,
continuous_inv_fun := add_monoid_hom_class.continuous_of_bound e.symm C_inv h_inv }
omit σ₂₁
namespace continuous_linear_map
variables {E' F' : Type*} [seminormed_add_comm_group E'] [seminormed_add_comm_group F']
variables {𝕜₁' : Type*} {𝕜₂' : Type*} [nontrivially_normed_field 𝕜₁']
[nontrivially_normed_field 𝕜₂'] [normed_space 𝕜₁' E'] [normed_space 𝕜₂' F']
{σ₁' : 𝕜₁' →+* 𝕜} {σ₁₃' : 𝕜₁' →+* 𝕜₃} {σ₂' : 𝕜₂' →+* 𝕜₂} {σ₂₃' : 𝕜₂' →+* 𝕜₃}
[ring_hom_comp_triple σ₁' σ₁₃ σ₁₃'] [ring_hom_comp_triple σ₂' σ₂₃ σ₂₃']
[ring_hom_isometric σ₂₃] [ring_hom_isometric σ₁₃'] [ring_hom_isometric σ₂₃']
/--
Compose a bilinear map `E →SL[σ₁₃] F →SL[σ₂₃] G` with two linear maps
`E' →SL[σ₁'] E` and `F' →SL[σ₂'] F`. -/
def bilinear_comp (f : E →SL[σ₁₃] F →SL[σ₂₃] G) (gE : E' →SL[σ₁'] E) (gF : F' →SL[σ₂'] F) :
E' →SL[σ₁₃'] F' →SL[σ₂₃'] G :=
((f.comp gE).flip.comp gF).flip
include σ₁₃' σ₂₃'
@[simp] lemma bilinear_comp_apply (f : E →SL[σ₁₃] F →SL[σ₂₃] G) (gE : E' →SL[σ₁'] E)
(gF : F' →SL[σ₂'] F) (x : E') (y : F') : f.bilinear_comp gE gF x y = f (gE x) (gF y) :=
rfl
omit σ₁₃' σ₂₃'
variables [ring_hom_isometric σ₁₃] [ring_hom_isometric σ₁'] [ring_hom_isometric σ₂']
/-- Derivative of a continuous bilinear map `f : E →L[𝕜] F →L[𝕜] G` interpreted as a map `E × F → G`
at point `p : E × F` evaluated at `q : E × F`, as a continuous bilinear map. -/
def deriv₂ (f : E →L[𝕜] Fₗ →L[𝕜] Gₗ) : (E × Fₗ) →L[𝕜] (E × Fₗ) →L[𝕜] Gₗ :=
f.bilinear_comp (fst _ _ _) (snd _ _ _) + f.flip.bilinear_comp (snd _ _ _) (fst _ _ _)
@[simp] lemma coe_deriv₂ (f : E →L[𝕜] Fₗ →L[𝕜] Gₗ) (p : E × Fₗ) :
⇑(f.deriv₂ p) = λ q : E × Fₗ, f p.1 q.2 + f q.1 p.2 := rfl
lemma map_add_add (f : E →L[𝕜] Fₗ →L[𝕜] Gₗ) (x x' : E) (y y' : Fₗ) :
f (x + x') (y + y') = f x y + f.deriv₂ (x, y) (x', y') + f x' y' :=
by simp only [map_add, add_apply, coe_deriv₂, add_assoc]
end continuous_linear_map
end semi_normed
section normed
variables [normed_add_comm_group E] [normed_add_comm_group F] [normed_add_comm_group G]
[normed_add_comm_group Fₗ]
open metric continuous_linear_map
section
variables [nontrivially_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
[nontrivially_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜₂ F] [normed_space 𝕜₃ G]
[normed_space 𝕜 Fₗ] (c : 𝕜)
{σ₁₂ : 𝕜 →+* 𝕜₂} {σ₂₃ : 𝕜₂ →+* 𝕜₃}
(f g : E →SL[σ₁₂] F) (x y z : E)
lemma linear_map.bound_of_shell [ring_hom_isometric σ₁₂] (f : E →ₛₗ[σ₁₂] F) {ε C : ℝ}
(ε_pos : 0 < ε) {c : 𝕜} (hc : 1 < ‖c‖)
(hf : ∀ x, ε / ‖c‖ ≤ ‖x‖ → ‖x‖ < ε → ‖f x‖ ≤ C * ‖x‖) (x : E) :
‖f x‖ ≤ C * ‖x‖ :=
begin
by_cases hx : x = 0, { simp [hx] },
exact semilinear_map_class.bound_of_shell_semi_normed f ε_pos hc hf
(ne_of_lt (norm_pos_iff.2 hx)).symm
end
/--
`linear_map.bound_of_ball_bound'` is a version of this lemma over a field satisfying `is_R_or_C`
that produces a concrete bound.
-/
lemma linear_map.bound_of_ball_bound {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] Fₗ)
(h : ∀ z ∈ metric.ball (0 : E) r, ‖f z‖ ≤ c) :
∃ C, ∀ (z : E), ‖f z‖ ≤ C * ‖z‖ :=
begin
cases @nontrivially_normed_field.non_trivial 𝕜 _ with k hk,
use c * (‖k‖ / r),
intro z,
refine linear_map.bound_of_shell _ r_pos hk (λ x hko hxo, _) _,
calc ‖f x‖ ≤ c : h _ (mem_ball_zero_iff.mpr hxo)
... ≤ c * ((‖x‖ * ‖k‖) / r) : le_mul_of_one_le_right _ _
... = _ : by ring,
{ exact le_trans (norm_nonneg _) (h 0 (by simp [r_pos])) },
{ rw [div_le_iff (zero_lt_one.trans hk)] at hko,
exact (one_le_div r_pos).mpr hko }
end
namespace continuous_linear_map
section op_norm
open set real
/-- An operator is zero iff its norm vanishes. -/
theorem op_norm_zero_iff [ring_hom_isometric σ₁₂] : ‖f‖ = 0 ↔ f = 0 :=
iff.intro
(λ hn, continuous_linear_map.ext (λ x, norm_le_zero_iff.1
(calc _ ≤ ‖f‖ * ‖x‖ : le_op_norm _ _
... = _ : by rw [hn, zero_mul])))
(by { rintro rfl, exact op_norm_zero })
/-- If a normed space is non-trivial, then the norm of the identity equals `1`. -/
@[simp] lemma norm_id [nontrivial E] : ‖id 𝕜 E‖ = 1 :=
begin
refine norm_id_of_nontrivial_seminorm _,
obtain ⟨x, hx⟩ := exists_ne (0 : E),
exact ⟨x, ne_of_gt (norm_pos_iff.2 hx)⟩,
end
instance norm_one_class [nontrivial E] : norm_one_class (E →L[𝕜] E) := ⟨norm_id⟩
/-- Continuous linear maps themselves form a normed space with respect to
the operator norm. -/
instance to_normed_add_comm_group [ring_hom_isometric σ₁₂] : normed_add_comm_group (E →SL[σ₁₂] F) :=
normed_add_comm_group.of_separation (λ f, (op_norm_zero_iff f).mp)
/-- Continuous linear maps form a normed ring with respect to the operator norm. -/
instance to_normed_ring : normed_ring (E →L[𝕜] E) :=
{ .. continuous_linear_map.to_normed_add_comm_group, .. continuous_linear_map.to_semi_normed_ring }
variable {f}
lemma homothety_norm [ring_hom_isometric σ₁₂] [nontrivial E] (f : E →SL[σ₁₂] F) {a : ℝ}
(hf : ∀x, ‖f x‖ = a * ‖x‖) :
‖f‖ = a :=
begin
obtain ⟨x, hx⟩ : ∃ (x : E), x ≠ 0 := exists_ne 0,
rw ← norm_pos_iff at hx,
have ha : 0 ≤ a, by simpa only [hf, hx, zero_le_mul_right] using norm_nonneg (f x),
apply le_antisymm (f.op_norm_le_bound ha (λ y, le_of_eq (hf y))),
simpa only [hf, hx, mul_le_mul_right] using f.le_op_norm x,
end
variable (f)
theorem uniform_embedding_of_bound {K : ℝ≥0} (hf : ∀ x, ‖x‖ ≤ K * ‖f x‖) :
uniform_embedding f :=
(add_monoid_hom_class.antilipschitz_of_bound f hf).uniform_embedding f.uniform_continuous
/-- If a continuous linear map is a uniform embedding, then it is expands the distances
by a positive factor.-/
theorem antilipschitz_of_uniform_embedding (f : E →L[𝕜] Fₗ) (hf : uniform_embedding f) :
∃ K, antilipschitz_with K f :=
begin
obtain ⟨ε, εpos, hε⟩ : ∃ (ε : ℝ) (H : ε > 0), ∀ {x y : E}, dist (f x) (f y) < ε → dist x y < 1,
from (uniform_embedding_iff.1 hf).2.2 1 zero_lt_one,
let δ := ε/2,
have δ_pos : δ > 0 := half_pos εpos,
have H : ∀{x}, ‖f x‖ ≤ δ → ‖x‖ ≤ 1,
{ assume x hx,
have : dist x 0 ≤ 1,
{ refine (hε _).le,
rw [f.map_zero, dist_zero_right],
exact hx.trans_lt (half_lt_self εpos) },
simpa using this },
rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩,
refine ⟨⟨δ⁻¹, _⟩ * ‖c‖₊, add_monoid_hom_class.antilipschitz_of_bound f $ λx, _⟩,
exact inv_nonneg.2 (le_of_lt δ_pos),
by_cases hx : f x = 0,
{ have : f x = f 0, by { simp [hx] },
have : x = 0 := (uniform_embedding_iff.1 hf).1 this,
simp [this] },
{ rcases rescale_to_shell hc δ_pos hx with ⟨d, hd, dxlt, ledx, dinv⟩,
rw [← f.map_smul d] at dxlt,
have : ‖d • x‖ ≤ 1 := H dxlt.le,
calc ‖x‖ = ‖d‖⁻¹ * ‖d • x‖ :
by rwa [← norm_inv, ← norm_smul, ← mul_smul, inv_mul_cancel, one_smul]
... ≤ ‖d‖⁻¹ * 1 :
mul_le_mul_of_nonneg_left this (inv_nonneg.2 (norm_nonneg _))
... ≤ δ⁻¹ * ‖c‖ * ‖f x‖ :
by rwa [mul_one] }
end
section completeness
open_locale topological_space
open filter
variables {E' : Type*} [seminormed_add_comm_group E'] [normed_space 𝕜 E'] [ring_hom_isometric σ₁₂]
/-- Construct a bundled continuous (semi)linear map from a map `f : E → F` and a proof of the fact
that it belongs to the closure of the image of a bounded set `s : set (E →SL[σ₁₂] F)` under coercion
to function. Coercion to function of the result is definitionally equal to `f`. -/
@[simps apply { fully_applied := ff }]
def of_mem_closure_image_coe_bounded (f : E' → F) {s : set (E' →SL[σ₁₂] F)} (hs : bounded s)
(hf : f ∈ closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s)) :
E' →SL[σ₁₂] F :=
begin
-- `f` is a linear map due to `linear_map_of_mem_closure_range_coe`
refine (linear_map_of_mem_closure_range_coe f _).mk_continuous_of_exists_bound _,
{ refine closure_mono (image_subset_iff.2 $ λ g hg, _) hf, exact ⟨g, rfl⟩ },
{ -- We need to show that `f` has bounded norm. Choose `C` such that `‖g‖ ≤ C` for all `g ∈ s`.
rcases bounded_iff_forall_norm_le.1 hs with ⟨C, hC⟩,
-- Then `‖g x‖ ≤ C * ‖x‖` for all `g ∈ s`, `x : E`, hence `‖f x‖ ≤ C * ‖x‖` for all `x`.
have : ∀ x, is_closed {g : E' → F | ‖g x‖ ≤ C * ‖x‖},
from λ x, is_closed_Iic.preimage (@continuous_apply E' (λ _, F) _ x).norm,
refine ⟨C, λ x, (this x).closure_subset_iff.2 (image_subset_iff.2 $ λ g hg, _) hf⟩,
exact g.le_of_op_norm_le (hC _ hg) _ }
end
/-- Let `f : E → F` be a map, let `g : α → E →SL[σ₁₂] F` be a family of continuous (semi)linear maps
that takes values in a bounded set and converges to `f` pointwise along a nontrivial filter. Then
`f` is a continuous (semi)linear map. -/
@[simps apply { fully_applied := ff }]
def of_tendsto_of_bounded_range {α : Type*} {l : filter α} [l.ne_bot] (f : E' → F)
(g : α → E' →SL[σ₁₂] F) (hf : tendsto (λ a x, g a x) l (𝓝 f)) (hg : bounded (set.range g)) :
E' →SL[σ₁₂] F :=
of_mem_closure_image_coe_bounded f hg $ mem_closure_of_tendsto hf $
eventually_of_forall $ λ a, mem_image_of_mem _ $ set.mem_range_self _
/-- If a Cauchy sequence of continuous linear map converges to a continuous linear map pointwise,
then it converges to the same map in norm. This lemma is used to prove that the space of continuous
linear maps is complete provided that the codomain is a complete space. -/
lemma tendsto_of_tendsto_pointwise_of_cauchy_seq {f : ℕ → E' →SL[σ₁₂] F} {g : E' →SL[σ₁₂] F}
(hg : tendsto (λ n x, f n x) at_top (𝓝 g)) (hf : cauchy_seq f) :
tendsto f at_top (𝓝 g) :=
begin
/- Since `f` is a Cauchy sequence, there exists `b → 0` such that `‖f n - f m‖ ≤ b N` for any
`m, n ≥ N`. -/
rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, hb₀, hfb, hb_lim⟩,
-- Since `b → 0`, it suffices to show that `‖f n x - g x‖ ≤ b n * ‖x‖` for all `n` and `x`.
suffices : ∀ n x, ‖f n x - g x‖ ≤ b n * ‖x‖,
from tendsto_iff_norm_tendsto_zero.2 (squeeze_zero (λ n, norm_nonneg _)
(λ n, op_norm_le_bound _ (hb₀ n) (this n)) hb_lim),
intros n x,
-- Note that `f m x → g x`, hence `‖f n x - f m x‖ → ‖f n x - g x‖` as `m → ∞`
have : tendsto (λ m, ‖f n x - f m x‖) at_top (𝓝 (‖f n x - g x‖)),
from (tendsto_const_nhds.sub $ tendsto_pi_nhds.1 hg _).norm,
-- Thus it suffices to verify `‖f n x - f m x‖ ≤ b n * ‖x‖` for `m ≥ n`.
refine le_of_tendsto this (eventually_at_top.2 ⟨n, λ m hm, _⟩),
-- This inequality follows from `‖f n - f m‖ ≤ b n`.
exact (f n - f m).le_of_op_norm_le (hfb _ _ _ le_rfl hm) _
end
/-- If the target space is complete, the space of continuous linear maps with its norm is also
complete. This works also if the source space is seminormed. -/
instance [complete_space F] : complete_space (E' →SL[σ₁₂] F) :=
begin
-- We show that every Cauchy sequence converges.
refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _),
-- The evaluation at any point `v : E` is Cauchy.
have cau : ∀ v, cauchy_seq (λ n, f n v),
from λ v, hf.map (lipschitz_apply v).uniform_continuous,
-- We assemble the limits points of those Cauchy sequences
-- (which exist as `F` is complete)
-- into a function which we call `G`.
choose G hG using λv, cauchy_seq_tendsto_of_complete (cau v),
-- Next, we show that this `G` is a continuous linear map.
-- This is done in `continuous_linear_map.of_tendsto_of_bounded_range`.
set Glin : E' →SL[σ₁₂] F :=
of_tendsto_of_bounded_range _ _ (tendsto_pi_nhds.mpr hG) hf.bounded_range,
-- Finally, `f n` converges to `Glin` in norm because of
-- `continuous_linear_map.tendsto_of_tendsto_pointwise_of_cauchy_seq`
exact ⟨Glin, tendsto_of_tendsto_pointwise_of_cauchy_seq (tendsto_pi_nhds.2 hG) hf⟩
end
/-- Let `s` be a bounded set in the space of continuous (semi)linear maps `E →SL[σ] F` taking values
in a proper space. Then `s` interpreted as a set in the space of maps `E → F` with topology of
pointwise convergence is precompact: its closure is a compact set. -/
lemma is_compact_closure_image_coe_of_bounded [proper_space F] {s : set (E' →SL[σ₁₂] F)}
(hb : bounded s) :
is_compact (closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s)) :=
have ∀ x, is_compact (closure (apply' F σ₁₂ x '' s)),
from λ x, ((apply' F σ₁₂ x).lipschitz.bounded_image hb).is_compact_closure,
is_compact_closure_of_subset_compact (is_compact_pi_infinite this)
(image_subset_iff.2 $ λ g hg x, subset_closure $ mem_image_of_mem _ hg)
/-- Let `s` be a bounded set in the space of continuous (semi)linear maps `E →SL[σ] F` taking values
in a proper space. If `s` interpreted as a set in the space of maps `E → F` with topology of
pointwise convergence is closed, then it is compact.
TODO: reformulate this in terms of a type synonym with the right topology. -/
lemma is_compact_image_coe_of_bounded_of_closed_image [proper_space F] {s : set (E' →SL[σ₁₂] F)}
(hb : bounded s) (hc : is_closed ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s)) :
is_compact ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) :=
hc.closure_eq ▸ is_compact_closure_image_coe_of_bounded hb
/-- If a set `s` of semilinear functions is bounded and is closed in the weak-* topology, then its
image under coercion to functions `E → F` is a closed set. We don't have a name for `E →SL[σ] F`
with weak-* topology in `mathlib`, so we use an equivalent condition (see `is_closed_induced_iff'`).
TODO: reformulate this in terms of a type synonym with the right topology. -/
lemma is_closed_image_coe_of_bounded_of_weak_closed {s : set (E' →SL[σ₁₂] F)} (hb : bounded s)
(hc : ∀ f, (⇑f : E' → F) ∈ closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) → f ∈ s) :
is_closed ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) :=
is_closed_of_closure_subset $ λ f hf,
⟨of_mem_closure_image_coe_bounded f hb hf, hc (of_mem_closure_image_coe_bounded f hb hf) hf, rfl⟩
/-- If a set `s` of semilinear functions is bounded and is closed in the weak-* topology, then its
image under coercion to functions `E → F` is a compact set. We don't have a name for `E →SL[σ] F`
with weak-* topology in `mathlib`, so we use an equivalent condition (see `is_closed_induced_iff'`).
-/
lemma is_compact_image_coe_of_bounded_of_weak_closed [proper_space F] {s : set (E' →SL[σ₁₂] F)}
(hb : bounded s)
(hc : ∀ f, (⇑f : E' → F) ∈ closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) → f ∈ s) :
is_compact ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) :=
is_compact_image_coe_of_bounded_of_closed_image hb $
is_closed_image_coe_of_bounded_of_weak_closed hb hc
/-- A closed ball is closed in the weak-* topology. We don't have a name for `E →SL[σ] F` with
weak-* topology in `mathlib`, so we use an equivalent condition (see `is_closed_induced_iff'`). -/
lemma is_weak_closed_closed_ball (f₀ : E' →SL[σ₁₂] F) (r : ℝ) ⦃f : E' →SL[σ₁₂] F⦄
(hf : ⇑f ∈ closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' (closed_ball f₀ r))) :
f ∈ closed_ball f₀ r :=
begin
have hr : 0 ≤ r,
from nonempty_closed_ball.1 (nonempty_image_iff.1 (closure_nonempty_iff.1 ⟨_, hf⟩)),
refine mem_closed_ball_iff_norm.2 (op_norm_le_bound _ hr $ λ x, _),
have : is_closed {g : E' → F | ‖g x - f₀ x‖ ≤ r * ‖x‖},
from is_closed_Iic.preimage ((@continuous_apply E' (λ _, F) _ x).sub continuous_const).norm,
refine this.closure_subset_iff.2 (image_subset_iff.2 $ λ g hg, _) hf,
exact (g - f₀).le_of_op_norm_le (mem_closed_ball_iff_norm.1 hg) _
end
/-- The set of functions `f : E → F` that represent continuous linear maps `f : E →SL[σ₁₂] F`
at distance `≤ r` from `f₀ : E →SL[σ₁₂] F` is closed in the topology of pointwise convergence.
This is one of the key steps in the proof of the **Banach-Alaoglu** theorem. -/
lemma is_closed_image_coe_closed_ball (f₀ : E →SL[σ₁₂] F) (r : ℝ) :
is_closed ((coe_fn : (E →SL[σ₁₂] F) → E → F) '' closed_ball f₀ r) :=
is_closed_image_coe_of_bounded_of_weak_closed bounded_closed_ball (is_weak_closed_closed_ball f₀ r)
/-- **Banach-Alaoglu** theorem. The set of functions `f : E → F` that represent continuous linear
maps `f : E →SL[σ₁₂] F` at distance `≤ r` from `f₀ : E →SL[σ₁₂] F` is compact in the topology of
pointwise convergence. Other versions of this theorem can be found in
`analysis.normed_space.weak_dual`. -/
lemma is_compact_image_coe_closed_ball [proper_space F] (f₀ : E →SL[σ₁₂] F) (r : ℝ) :
is_compact ((coe_fn : (E →SL[σ₁₂] F) → E → F) '' closed_ball f₀ r) :=
is_compact_image_coe_of_bounded_of_weak_closed bounded_closed_ball $
is_weak_closed_closed_ball f₀ r
end completeness
section uniformly_extend
variables [complete_space F] (e : E →L[𝕜] Fₗ) (h_dense : dense_range e)
section
variables (h_e : uniform_inducing e)
/-- Extension of a continuous linear map `f : E →SL[σ₁₂] F`, with `E` a normed space and `F` a
complete normed space, along a uniform and dense embedding `e : E →L[𝕜] Fₗ`. -/
def extend : Fₗ →SL[σ₁₂] F :=
/- extension of `f` is continuous -/
have cont : _ := (uniform_continuous_uniformly_extend h_e h_dense f.uniform_continuous).continuous,
/- extension of `f` agrees with `f` on the domain of the embedding `e` -/
have eq : _ := uniformly_extend_of_ind h_e h_dense f.uniform_continuous,
{ to_fun := (h_e.dense_inducing h_dense).extend f,
map_add' :=
begin
refine h_dense.induction_on₂ _ _,
{ exact is_closed_eq (cont.comp continuous_add)
((cont.comp continuous_fst).add (cont.comp continuous_snd)) },
{ assume x y, simp only [eq, ← e.map_add], exact f.map_add _ _ },
end,
map_smul' := λk,
begin
refine (λ b, h_dense.induction_on b _ _),
{ exact is_closed_eq (cont.comp (continuous_const_smul _))
((continuous_const_smul _).comp cont) },
{ assume x, rw ← map_smul, simp only [eq], exact continuous_linear_map.map_smulₛₗ _ _ _ },
end,
cont := cont }
@[simp] lemma extend_eq (x : E) : extend f e h_dense h_e (e x) = f x :=
dense_inducing.extend_eq _ f.cont _
lemma extend_unique (g : Fₗ →SL[σ₁₂] F) (H : g.comp e = f) : extend f e h_dense h_e = g :=
continuous_linear_map.coe_fn_injective $
uniformly_extend_unique h_e h_dense (continuous_linear_map.ext_iff.1 H) g.continuous
@[simp] lemma extend_zero : extend (0 : E →SL[σ₁₂] F) e h_dense h_e = 0 :=
extend_unique _ _ _ _ _ (zero_comp _)
end
section
variables {N : ℝ≥0} (h_e : ∀x, ‖x‖ ≤ N * ‖e x‖) [ring_hom_isometric σ₁₂]
local notation `ψ` := f.extend e h_dense (uniform_embedding_of_bound _ h_e).to_uniform_inducing
/-- If a dense embedding `e : E →L[𝕜] G` expands the norm by a constant factor `N⁻¹`, then the
norm of the extension of `f` along `e` is bounded by `N * ‖f‖`. -/
lemma op_norm_extend_le : ‖ψ‖ ≤ N * ‖f‖ :=
begin
have uni : uniform_inducing e := (uniform_embedding_of_bound _ h_e).to_uniform_inducing,
have eq : ∀x, ψ (e x) = f x := uniformly_extend_of_ind uni h_dense f.uniform_continuous,
by_cases N0 : 0 ≤ N,
{ refine op_norm_le_bound ψ _ (is_closed_property h_dense (is_closed_le _ _) _),
{ exact mul_nonneg N0 (norm_nonneg _) },
{ exact continuous_norm.comp (cont ψ) },
{ exact continuous_const.mul continuous_norm },
{ assume x,
rw eq,
calc ‖f x‖ ≤ ‖f‖ * ‖x‖ : le_op_norm _ _
... ≤ ‖f‖ * (N * ‖e x‖) : mul_le_mul_of_nonneg_left (h_e x) (norm_nonneg _)
... ≤ N * ‖f‖ * ‖e x‖ : by rw [mul_comm ↑N ‖f‖, mul_assoc] } },
{ have he : ∀ x : E, x = 0,
{ assume x,
have N0 : N ≤ 0 := le_of_lt (lt_of_not_ge N0),
rw ← norm_le_zero_iff,
exact le_trans (h_e x) (mul_nonpos_of_nonpos_of_nonneg N0 (norm_nonneg _)) },
have hf : f = 0, { ext, simp only [he x, zero_apply, map_zero] },
have hψ : ψ = 0, { rw hf, apply extend_zero },
rw [hψ, hf, norm_zero, norm_zero, mul_zero] }
end
end
end uniformly_extend
end op_norm
end continuous_linear_map
namespace linear_isometry
@[simp] lemma norm_to_continuous_linear_map [nontrivial E] [ring_hom_isometric σ₁₂]
(f : E →ₛₗᵢ[σ₁₂] F) :
‖f.to_continuous_linear_map‖ = 1 :=
f.to_continuous_linear_map.homothety_norm $ by simp
variables {σ₁₃ : 𝕜 →+* 𝕜₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃]
include σ₁₃
/-- Postcomposition of a continuous linear map with a linear isometry preserves
the operator norm. -/
lemma norm_to_continuous_linear_map_comp [ring_hom_isometric σ₁₂] (f : F →ₛₗᵢ[σ₂₃] G)
{g : E →SL[σ₁₂] F} :
‖f.to_continuous_linear_map.comp g‖ = ‖g‖ :=
op_norm_ext (f.to_continuous_linear_map.comp g) g
(λ x, by simp only [norm_map, coe_to_continuous_linear_map, coe_comp'])
omit σ₁₃
end linear_isometry
end
namespace continuous_linear_map
variables [nontrivially_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
[nontrivially_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜₂ F] [normed_space 𝕜₃ G]
[normed_space 𝕜 Fₗ] (c : 𝕜)
{σ₁₂ : 𝕜 →+* 𝕜₂} {σ₂₃ : 𝕜₂ →+* 𝕜₃}
variables {𝕜₂' : Type*} [nontrivially_normed_field 𝕜₂'] {F' : Type*} [normed_add_comm_group F']
[normed_space 𝕜₂' F'] {σ₂' : 𝕜₂' →+* 𝕜₂} {σ₂'' : 𝕜₂ →+* 𝕜₂'}
{σ₂₃' : 𝕜₂' →+* 𝕜₃}
[ring_hom_inv_pair σ₂' σ₂''] [ring_hom_inv_pair σ₂'' σ₂']
[ring_hom_comp_triple σ₂' σ₂₃ σ₂₃'] [ring_hom_comp_triple σ₂'' σ₂₃' σ₂₃]
[ring_hom_isometric σ₂₃]
[ring_hom_isometric σ₂'] [ring_hom_isometric σ₂''] [ring_hom_isometric σ₂₃']
include σ₂'' σ₂₃'
/-- Precomposition with a linear isometry preserves the operator norm. -/
lemma op_norm_comp_linear_isometry_equiv (f : F →SL[σ₂₃] G) (g : F' ≃ₛₗᵢ[σ₂'] F) :
‖f.comp g.to_linear_isometry.to_continuous_linear_map‖ = ‖f‖ :=
begin
casesI subsingleton_or_nontrivial F',
{ haveI := g.symm.to_linear_equiv.to_equiv.subsingleton,
simp },
refine le_antisymm _ _,
{ convert f.op_norm_comp_le g.to_linear_isometry.to_continuous_linear_map,
simp [g.to_linear_isometry.norm_to_continuous_linear_map] },
{ convert (f.comp g.to_linear_isometry.to_continuous_linear_map).op_norm_comp_le
g.symm.to_linear_isometry.to_continuous_linear_map,
{ ext,
simp },
haveI := g.symm.surjective.nontrivial,
simp [g.symm.to_linear_isometry.norm_to_continuous_linear_map] },
end
omit σ₂'' σ₂₃'
/-- The norm of the tensor product of a scalar linear map and of an element of a normed space
is the product of the norms. -/
@[simp] lemma norm_smul_right_apply (c : E →L[𝕜] 𝕜) (f : Fₗ) :
‖smul_right c f‖ = ‖c‖ * ‖f‖ :=
begin
refine le_antisymm _ _,
{ apply op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) (λx, _),
calc
‖(c x) • f‖ = ‖c x‖ * ‖f‖ : norm_smul _ _
... ≤ (‖c‖ * ‖x‖) * ‖f‖ :
mul_le_mul_of_nonneg_right (le_op_norm _ _) (norm_nonneg _)
... = ‖c‖ * ‖f‖ * ‖x‖ : by ring },
{ by_cases h : f = 0,
{ simp [h] },
{ have : 0 < ‖f‖ := norm_pos_iff.2 h,
rw ← le_div_iff this,
apply op_norm_le_bound _ (div_nonneg (norm_nonneg _) (norm_nonneg f)) (λx, _),
rw [div_mul_eq_mul_div, le_div_iff this],
calc ‖c x‖ * ‖f‖ = ‖c x • f‖ : (norm_smul _ _).symm
... = ‖smul_right c f x‖ : rfl
... ≤ ‖smul_right c f‖ * ‖x‖ : le_op_norm _ _ } },
end
/-- The non-negative norm of the tensor product of a scalar linear map and of an element of a normed
space is the product of the non-negative norms. -/
@[simp] lemma nnnorm_smul_right_apply (c : E →L[𝕜] 𝕜) (f : Fₗ) :
‖smul_right c f‖₊ = ‖c‖₊ * ‖f‖₊ :=
nnreal.eq $ c.norm_smul_right_apply f
variables (𝕜 E Fₗ)
/-- `continuous_linear_map.smul_right` as a continuous trilinear map:
`smul_rightL (c : E →L[𝕜] 𝕜) (f : F) (x : E) = c x • f`. -/
def smul_rightL : (E →L[𝕜] 𝕜) →L[𝕜] Fₗ →L[𝕜] E →L[𝕜] Fₗ :=
linear_map.mk_continuous₂
{ to_fun := smul_rightₗ,
map_add' := λ c₁ c₂, by { ext x, simp only [add_smul, coe_smul_rightₗ, add_apply,
smul_right_apply, linear_map.add_apply] },
map_smul' := λ m c, by { ext x, simp only [smul_smul, coe_smul_rightₗ, algebra.id.smul_eq_mul,
coe_smul', smul_right_apply, linear_map.smul_apply,
ring_hom.id_apply, pi.smul_apply] } }
1 $ λ c x, by simp only [coe_smul_rightₗ, one_mul, norm_smul_right_apply, linear_map.coe_mk]
variables {𝕜 E Fₗ}
@[simp] lemma norm_smul_rightL_apply (c : E →L[𝕜] 𝕜) (f : Fₗ) :
‖smul_rightL 𝕜 E Fₗ c f‖ = ‖c‖ * ‖f‖ :=
norm_smul_right_apply c f
@[simp] lemma norm_smul_rightL (c : E →L[𝕜] 𝕜) [nontrivial Fₗ] :
‖smul_rightL 𝕜 E Fₗ c‖ = ‖c‖ :=
continuous_linear_map.homothety_norm _ c.norm_smul_right_apply
variables (𝕜) (𝕜' : Type*)
section
variables [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜']
@[simp] lemma op_norm_mul [norm_one_class 𝕜'] : ‖mul 𝕜 𝕜'‖ = 1 :=
by haveI := norm_one_class.nontrivial 𝕜'; exact (mulₗᵢ 𝕜 𝕜').norm_to_continuous_linear_map
end
/-- The norm of `lsmul` equals 1 in any nontrivial normed group.
This is `continuous_linear_map.op_norm_lsmul_le` as an equality. -/
@[simp] lemma op_norm_lsmul [normed_field 𝕜'] [normed_algebra 𝕜 𝕜']
[normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [nontrivial E] :
‖(lsmul 𝕜 𝕜' : 𝕜' →L[𝕜] E →L[𝕜] E)‖ = 1 :=
begin
refine continuous_linear_map.op_norm_eq_of_bounds zero_le_one (λ x, _) (λ N hN h, _),
{ rw one_mul,
exact op_norm_lsmul_apply_le _, },
obtain ⟨y, hy⟩ := exists_ne (0 : E),
have := le_of_op_norm_le _ (h 1) y,
simp_rw [lsmul_apply, one_smul, norm_one, mul_one] at this,
refine le_of_mul_le_mul_right _ (norm_pos_iff.mpr hy),
simp_rw [one_mul, this]
end
end continuous_linear_map
namespace submodule
variables [nontrivially_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
[nontrivially_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜₂ F] {σ₁₂ : 𝕜 →+* 𝕜₂}
lemma norm_subtypeL (K : submodule 𝕜 E) [nontrivial K] : ‖K.subtypeL‖ = 1 :=
K.subtypeₗᵢ.norm_to_continuous_linear_map
end submodule
namespace continuous_linear_equiv
variables [nontrivially_normed_field 𝕜] [nontrivially_normed_field 𝕜₂]
[nontrivially_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜₂ F]
{σ₁₂ : 𝕜 →+* 𝕜₂} {σ₂₁ : 𝕜₂ →+* 𝕜}
[ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂]
section
variables [ring_hom_isometric σ₂₁]
protected lemma antilipschitz (e : E ≃SL[σ₁₂] F) :
antilipschitz_with ‖(e.symm : F →SL[σ₂₁] E)‖₊ e :=
e.symm.lipschitz.to_right_inverse e.left_inv
lemma one_le_norm_mul_norm_symm [ring_hom_isometric σ₁₂] [nontrivial E] (e : E ≃SL[σ₁₂] F) :
1 ≤ ‖(e : E →SL[σ₁₂] F)‖ * ‖(e.symm : F →SL[σ₂₁] E)‖ :=
begin
rw [mul_comm],
convert (e.symm : F →SL[σ₂₁] E).op_norm_comp_le (e : E →SL[σ₁₂] F),
rw [e.coe_symm_comp_coe, continuous_linear_map.norm_id]
end
include σ₂₁
lemma norm_pos [ring_hom_isometric σ₁₂] [nontrivial E] (e : E ≃SL[σ₁₂] F) :
0 < ‖(e : E →SL[σ₁₂] F)‖ :=
pos_of_mul_pos_left (lt_of_lt_of_le zero_lt_one e.one_le_norm_mul_norm_symm) (norm_nonneg _)
omit σ₂₁
lemma norm_symm_pos [ring_hom_isometric σ₁₂] [nontrivial E] (e : E ≃SL[σ₁₂] F) :
0 < ‖(e.symm : F →SL[σ₂₁] E)‖ :=
pos_of_mul_pos_right (zero_lt_one.trans_le e.one_le_norm_mul_norm_symm) (norm_nonneg _)
lemma nnnorm_symm_pos [ring_hom_isometric σ₁₂] [nontrivial E] (e : E ≃SL[σ₁₂] F) :
0 < ‖(e.symm : F →SL[σ₂₁] E)‖₊ :=
e.norm_symm_pos
lemma subsingleton_or_norm_symm_pos [ring_hom_isometric σ₁₂] (e : E ≃SL[σ₁₂] F) :
subsingleton E ∨ 0 < ‖(e.symm : F →SL[σ₂₁] E)‖ :=
begin
rcases subsingleton_or_nontrivial E with _i|_i; resetI,
{ left, apply_instance },
{ right, exact e.norm_symm_pos }
end
lemma subsingleton_or_nnnorm_symm_pos [ring_hom_isometric σ₁₂] (e : E ≃SL[σ₁₂] F) :
subsingleton E ∨ 0 < ‖(e.symm : F →SL[σ₂₁] E)‖₊ :=
subsingleton_or_norm_symm_pos e
variable (𝕜)
/-- Given a nonzero element `x` of a normed space `E₁` over a field `𝕜`, the natural
continuous linear equivalence from `E₁` to the span of `x`.-/
def to_span_nonzero_singleton (x : E) (h : x ≠ 0) : 𝕜 ≃L[𝕜] (𝕜 ∙ x) :=
of_homothety
(linear_equiv.to_span_nonzero_singleton 𝕜 E x h)
‖x‖
(norm_pos_iff.mpr h)
(to_span_nonzero_singleton_homothety 𝕜 x h)
/-- Given a nonzero element `x` of a normed space `E₁` over a field `𝕜`, the natural continuous
linear map from the span of `x` to `𝕜`.-/
def coord (x : E) (h : x ≠ 0) : (𝕜 ∙ x) →L[𝕜] 𝕜 := (to_span_nonzero_singleton 𝕜 x h).symm
@[simp] lemma coe_to_span_nonzero_singleton_symm {x : E} (h : x ≠ 0) :
⇑(to_span_nonzero_singleton 𝕜 x h).symm = coord 𝕜 x h := rfl
@[simp] lemma coord_to_span_nonzero_singleton {x : E} (h : x ≠ 0) (c : 𝕜) :
coord 𝕜 x h (to_span_nonzero_singleton 𝕜 x h c) = c :=
(to_span_nonzero_singleton 𝕜 x h).symm_apply_apply c
@[simp] lemma to_span_nonzero_singleton_coord {x : E} (h : x ≠ 0) (y : 𝕜 ∙ x) :
to_span_nonzero_singleton 𝕜 x h (coord 𝕜 x h y) = y :=
(to_span_nonzero_singleton 𝕜 x h).apply_symm_apply y
@[simp] lemma coord_norm (x : E) (h : x ≠ 0) : ‖coord 𝕜 x h‖ = ‖x‖⁻¹ :=
begin
have hx : 0 < ‖x‖ := (norm_pos_iff.mpr h),
haveI : nontrivial (𝕜 ∙ x) := submodule.nontrivial_span_singleton h,
exact continuous_linear_map.homothety_norm _
(λ y, homothety_inverse _ hx _ (to_span_nonzero_singleton_homothety 𝕜 x h) _)
end
@[simp] lemma coord_self (x : E) (h : x ≠ 0) :
(coord 𝕜 x h) (⟨x, submodule.mem_span_singleton_self x⟩ : 𝕜 ∙ x) = 1 :=
linear_equiv.coord_self 𝕜 E x h
variables {𝕜} {𝕜₄ : Type*} [nontrivially_normed_field 𝕜₄]
variables {H : Type*} [normed_add_comm_group H] [normed_space 𝕜₄ H] [normed_space 𝕜₃ G]
variables {σ₂₃ : 𝕜₂ →+* 𝕜₃} {σ₁₃ : 𝕜 →+* 𝕜₃}
variables {σ₃₄ : 𝕜₃ →+* 𝕜₄} {σ₄₃ : 𝕜₄ →+* 𝕜₃}
variables {σ₂₄ : 𝕜₂ →+* 𝕜₄} {σ₁₄ : 𝕜 →+* 𝕜₄}
variables [ring_hom_inv_pair σ₃₄ σ₄₃] [ring_hom_inv_pair σ₄₃ σ₃₄]
variables [ring_hom_comp_triple σ₂₁ σ₁₄ σ₂₄] [ring_hom_comp_triple σ₂₄ σ₄₃ σ₂₃]
variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [ring_hom_comp_triple σ₁₃ σ₃₄ σ₁₄]
variables [ring_hom_isometric σ₁₄] [ring_hom_isometric σ₂₃]
variables [ring_hom_isometric σ₄₃] [ring_hom_isometric σ₂₄]
variables [ring_hom_isometric σ₁₃] [ring_hom_isometric σ₁₂]
variables [ring_hom_isometric σ₃₄]
include σ₂₁ σ₃₄ σ₁₃ σ₂₄
/-- A pair of continuous (semi)linear equivalences generates an continuous (semi)linear equivalence
between the spaces of continuous (semi)linear maps. -/
@[simps apply symm_apply]
def arrow_congrSL (e₁₂ : E ≃SL[σ₁₂] F) (e₄₃ : H ≃SL[σ₄₃] G) :
(E →SL[σ₁₄] H) ≃SL[σ₄₃] (F →SL[σ₂₃] G) :=
{ -- given explicitly to help `simps`
to_fun := λ L, (e₄₃ : H →SL[σ₄₃] G).comp (L.comp (e₁₂.symm : F →SL[σ₂₁] E)),
-- given explicitly to help `simps`
inv_fun := λ L, (e₄₃.symm : G →SL[σ₃₄] H).comp (L.comp (e₁₂ : E →SL[σ₁₂] F)),
map_add' := λ f g, by rw [add_comp, comp_add],
map_smul' := λ t f, by rw [smul_comp, comp_smulₛₗ],
continuous_to_fun := (continuous_id.clm_comp_const _).const_clm_comp _,
continuous_inv_fun := (continuous_id.clm_comp_const _).const_clm_comp _,
.. e₁₂.arrow_congr_equiv e₄₃, }
omit σ₂₁ σ₃₄ σ₁₃ σ₂₄
/-- A pair of continuous linear equivalences generates an continuous linear equivalence between
the spaces of continuous linear maps. -/
def arrow_congr {F H : Type*} [normed_add_comm_group F] [normed_add_comm_group H]
[normed_space 𝕜 F] [normed_space 𝕜 G] [normed_space 𝕜 H]
(e₁ : E ≃L[𝕜] F) (e₂ : H ≃L[𝕜] G) :
(E →L[𝕜] H) ≃L[𝕜] (F →L[𝕜] G) :=
arrow_congrSL e₁ e₂
end
end continuous_linear_equiv
end normed
/--
A bounded bilinear form `B` in a real normed space is *coercive*
if there is some positive constant C such that `C * ‖u‖ * ‖u‖ ≤ B u u`.
-/
def is_coercive
[normed_add_comm_group E] [normed_space ℝ E]
(B : E →L[ℝ] E →L[ℝ] ℝ) : Prop :=
∃ C, (0 < C) ∧ ∀ u, C * ‖u‖ * ‖u‖ ≤ B u u
|
cfda1784e6685a8dd0327bbd3f9ea6605705ec2e | 32a2d1642d7519c99693bc1d3b24069e4853dd1f | /algebra/pi_instances.lean | 2fdf211a7ad9be374d0cb03571574a7ef36029db | [
"Apache-2.0"
] | permissive | Cedric0099/mathlib | 7edb81d5d68e280b4d21f6c0377dad1f9b8c0d71 | a97101d2df5d186848075a2d0452f6a04d8a13eb | refs/heads/master | 1,584,201,847,599 | 1,524,979,632,000 | 1,524,979,632,000 | 131,690,350 | 0 | 0 | null | 1,525,162,341,000 | 1,525,162,341,000 | null | UTF-8 | Lean | false | false | 4,043 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Patrick Massot
Pi instances for algebraic structures.
-/
import algebra.module
namespace tactic
open interactive interactive.types lean.parser
open functor has_seq list nat
meta def mk_mvar_list : ℕ → tactic (list expr)
| 0 := pure []
| (succ n) := (::) <$> mk_mvar <*> mk_mvar_list n
/-- `pi_instance [inst1,inst2]` constructs an instance of `my_class (Π i : I, f i)`
where we know `Π i, my_class (f i)` and where all non-propositional fields are
filled in by `inst1` and `inst2`
-/
meta def pi_instance (sources : parse pexpr_list_or_texpr) : tactic unit :=
do t ← target,
e ← get_env,
let struct_n := t.get_app_fn.const_name,
fields ← (e.structure_fields struct_n : tactic (list name))
<|> fail "target class is not a structure",
st ← sources.mmap (λ s,
do t ← to_expr s >>= infer_type,
e.structure_fields t.get_app_fn.const_name),
let st := st.join,
axms ← mfilter (λ f : name,
resolve_name (f.update_prefix struct_n) >>=
to_expr >>=
is_proof)
(fields.diff st),
vals ← mk_mvar_list axms.length,
refine (pexpr.mk_structure_instance
{ struct := some struct_n,
field_names := axms,
field_values := vals.map to_pexpr,
sources := sources }),
set_goals vals,
axms.mmap' (λ h,
solve1 $
do intros,
funext,
applyc $ h.update_prefix struct_n)
run_cmd add_interactive [`pi_instance]
end tactic
namespace pi
universes u v
variable {I : Type u} -- The indexing type
variable {f : I → Type v} -- The family of types already equiped with instances
instance has_mul [∀ i, has_mul $ f i] : has_mul (Π i : I, f i) :=
⟨λ x y, λ i, x i * y i⟩
instance semigroup [∀ i, semigroup $ f i] : semigroup (Π i : I, f i) :=
by pi_instance [pi.has_mul]
instance comm_semigroup [∀ i, comm_semigroup $ f i] : comm_semigroup (Π i : I, f i) :=
by pi_instance [pi.has_mul]
instance has_one [∀ i, has_one $ f i] : has_one (Π i : I, f i) :=
⟨λ i, 1⟩
instance has_inv [∀ i, has_inv $ f i] : has_inv (Π i : I, f i) :=
⟨λ x, λ i, (x i)⁻¹⟩
instance monoid [∀ i, monoid $ f i] : monoid (Π i : I, f i) :=
by pi_instance [pi.has_one, pi.semigroup]
instance comm_monoid [∀ i, comm_monoid $ f i] : comm_monoid (Π i : I, f i) :=
by pi_instance [pi.monoid, pi.comm_semigroup]
instance group [∀ i, group $ f i] : group (Π i : I, f i) :=
by pi_instance [pi.has_inv, pi.monoid]
instance has_add [∀ i, has_add $ f i] : has_add (Π i : I, f i) :=
⟨λ x y, λ i, x i + y i⟩
instance add_semigroup [∀ i, add_semigroup $ f i] : add_semigroup (Π i : I, f i) :=
by pi_instance [pi.has_add]
instance has_zero [∀ i, has_zero $ f i] : has_zero (Π i : I, f i) :=
⟨λ i, 0⟩
instance has_neg [∀ i, has_neg $ f i] : has_neg (Π i : I, f i) :=
⟨λ x, λ i, -(x i)⟩
instance add_group [∀ i, add_group $ f i] : add_group (Π i : I, f i) :=
by pi_instance [pi.has_zero, pi.has_neg, pi.add_semigroup]
instance add_comm_group [∀ i, add_comm_group $ f i] : add_comm_group (Π i : I, f i) :=
by pi_instance [pi.add_group]
instance distrib [∀ i, distrib $ f i] : distrib (Π i : I, f i) :=
by pi_instance [pi.has_add, pi.has_mul]
instance ring [∀ i, ring $ f i] : ring (Π i : I, f i) :=
by pi_instance [pi.distrib, pi.monoid, pi.add_comm_group]
instance comm_ring [∀ i, comm_ring $ f i] : comm_ring (Π i : I, f i) :=
by pi_instance [pi.comm_semigroup, pi.ring]
instance has_scalar {α : Type*} [∀ i, has_scalar α $ f i] : has_scalar α (Π i : I, f i) :=
⟨λ s x, λ i, s • (x i)⟩
instance module {α : Type*} [ring α] [∀ i, module α $ f i] : module α (Π i : I, f i) :=
by pi_instance [pi.has_scalar]
instance vector_space (α : Type*) [field α] [∀ i, vector_space α $ f i] : vector_space α (Π i : I, f i) :=
{ ..pi.module }
end pi |
152316b52e5e01e4881f6c008cdbf8102a3b1697 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/PrettyPrinter/Parenthesizer.lean | b007273b652bcd7243e21fc3b99c587c8499b013 | [
"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 | 28,177 | lean | /-
Copyright (c) 2020 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
import Lean.Parser.Extension
import Lean.Parser.StrInterpolation
import Lean.ParserCompiler.Attribute
import Lean.PrettyPrinter.Basic
/-!
The parenthesizer inserts parentheses into a `Syntax` object where syntactically necessary, usually as an intermediary
step between the delaborator and the formatter. While the delaborator outputs structurally well-formed syntax trees that
can be re-elaborated without post-processing, this tree structure is lost in the formatter and thus needs to be
preserved by proper insertion of parentheses.
# The abstract problem & solution
The Lean 4 grammar is unstructured and extensible with arbitrary new parsers, so in general it is undecidable whether
parentheses are necessary or even allowed at any point in the syntax tree. Parentheses for different categories, e.g.
terms and levels, might not even have the same structure. In this module, we focus on the correct parenthesization of
parsers defined via `Lean.Parser.prattParser`, which includes both aforementioned built-in categories. Custom
parenthesizers can be added for new node kinds, but the data collected in the implementation below might not be
appropriate for other parenthesization strategies.
Usages of a parser defined via `prattParser` in general have the form `p prec`, where `prec` is the minimum precedence
or binding power. Recall that a Pratt parser greedily runs a leading parser with precedence at least `prec` (otherwise
it fails) followed by zero or more trailing parsers with precedence at least `prec`; the precedence of a parser is
encoded in the call to `leadingNode/trailingNode`, respectively. Thus we should parenthesize a syntax node `stx`
supposedly produced by `p prec` if
1. the leading/any trailing parser involved in `stx` has precedence < `prec` (because without parentheses, `p prec`
would not produce all of `stx`), or
2. the trailing parser parsing the input to *the right of* `stx`, if any, has precedence >= `prec` (because without
parentheses, `p prec` would have parsed it as well and made it a part of `stx`). We also check that the two parsers
are from the same syntax category.
Note that in case 2, it is also sufficient to parenthesize a *parent* node as long as the offending parser is still to
the right of that node. For example, imagine the tree structure of `(f fun x => x) y` without parentheses. We need to
insert *some* parentheses between `x` and `y` since the lambda body is parsed with precedence 0, while the identifier
parser for `y` has precedence `maxPrec`. But we need to parenthesize the `$` node anyway since the precedence of its
RHS (0) again is smaller than that of `y`. So it's better to only parenthesize the outer node than ending up with
`(f $ (fun x => x)) y`.
# Implementation
We transform the syntax tree and collect the necessary precedence information for that in a single traversal. The
traversal is right-to-left to cover case 2. More specifically, for every Pratt parser call, we store as monadic state
the precedence of the left-most trailing parser and the minimum precedence of any parser (`contPrec`/`minPrec`) in this
call, if any, and the precedence of the nested trailing Pratt parser call (`trailPrec`), if any. If `stP` is the state
resulting from the traversal of a Pratt parser call `p prec`, and `st` is the state of the surrounding call, we
parenthesize if `prec > stP.minPrec` (case 1) or if `stP.trailPrec <= st.contPrec` (case 2).
The traversal can be customized for each `[*Parser]` parser declaration `c` (more specifically, each `SyntaxNodeKind`
`c`) using the `[parenthesizer c]` attribute. Otherwise, a default parenthesizer will be synthesized from the used
parser combinators by recursively replacing them with declarations tagged as `[combinator_parenthesizer]` for the
respective combinator. If a called function does not have a registered combinator parenthesizer and is not reducible,
the synthesizer fails. This happens mostly at the `Parser.mk` decl, which is irreducible, when some parser primitive has
not been handled yet.
The traversal over the `Syntax` object is complicated by the fact that a parser does not produce exactly one syntax
node, but an arbitrary (but constant, for each parser) amount that it pushes on top of the parser stack. This amount can
even be zero for parsers such as `checkWsBefore`. Thus we cannot simply pass and return a `Syntax` object to and from
`visit`. Instead, we use a `Syntax.Traverser` that allows arbitrary movement and modification inside the syntax tree.
Our traversal invariant is that a parser interpreter should stop at the syntax object to the *left* of all syntax
objects its parser produced, except when it is already at the left-most child. This special case is not an issue in
practice since if there is another parser to the left that produced zero nodes in this case, it should always do so, so
there is no danger of the left-most child being processed multiple times.
Ultimately, most parenthesizers are implemented via three primitives that do all the actual syntax traversal:
`maybeParenthesize mkParen prec x` runs `x` and afterwards transforms it with `mkParen` if the above
condition for `p prec` is fulfilled. `visitToken` advances to the preceding sibling and is used on atoms. `visitArgs x`
executes `x` on the last child of the current node and then advances to the preceding sibling (of the original current
node).
-/
namespace Lean
namespace PrettyPrinter
namespace Parenthesizer
structure Context where
-- We need to store this `categoryParser` argument to deal with the implicit Pratt parser call in `trailingNode.parenthesizer`.
cat : Name := Name.anonymous
structure State where
stxTrav : Syntax.Traverser
--- precedence and category of the current left-most trailing parser, if any; see module doc for details
contPrec : Option Nat := none
contCat : Name := Name.anonymous
-- current minimum precedence in this Pratt parser call, if any; see module doc for details
minPrec : Option Nat := none
-- precedence and category of the trailing Pratt parser call if any; see module doc for details
trailPrec : Option Nat := none
trailCat : Name := Name.anonymous
-- true iff we have already visited a token on this parser level; used for detecting trailing parsers
visitedToken : Bool := false
end Parenthesizer
abbrev ParenthesizerM := ReaderT Parenthesizer.Context $ StateRefT Parenthesizer.State CoreM
abbrev Parenthesizer := ParenthesizerM Unit
@[inline] def ParenthesizerM.orElse (p₁ : ParenthesizerM α) (p₂ : Unit → ParenthesizerM α) : ParenthesizerM α := do
let s ← get
catchInternalId backtrackExceptionId
p₁
(fun _ => do set s; p₂ ())
instance : OrElse (ParenthesizerM α) := ⟨ParenthesizerM.orElse⟩
unsafe def mkParenthesizerAttribute : IO (KeyedDeclsAttribute Parenthesizer) :=
KeyedDeclsAttribute.init {
builtinName := `builtin_parenthesizer,
name := `parenthesizer,
descr := "Register a parenthesizer for a parser.
[parenthesizer k] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `SyntaxNodeKind` `k`.",
valueTypeName := `Lean.PrettyPrinter.Parenthesizer,
evalKey := fun builtin stx => do
let env ← getEnv
let stx ← Attribute.Builtin.getIdent stx
let id := stx.getId
-- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to
-- synthesize a parenthesizer for it immediately, so we just check for a declaration in this case
unless (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id do
throwError "invalid [parenthesizer] argument, unknown syntax kind '{id}'"
if (← getEnv).contains id && (← Elab.getInfoState).enabled then
Elab.addConstInfo stx id none
pure id
} `Lean.PrettyPrinter.parenthesizerAttribute
@[builtin_init mkParenthesizerAttribute] opaque parenthesizerAttribute : KeyedDeclsAttribute Parenthesizer
abbrev CategoryParenthesizer := (prec : Nat) → Parenthesizer
unsafe def mkCategoryParenthesizerAttribute : IO (KeyedDeclsAttribute CategoryParenthesizer) :=
KeyedDeclsAttribute.init {
builtinName := `builtin_category_parenthesizer,
name := `category_parenthesizer,
descr := "Register a parenthesizer for a syntax category.
[category_parenthesizer cat] registers a declaration of type `Lean.PrettyPrinter.CategoryParenthesizer` for the category `cat`,
which is used when parenthesizing calls of `categoryParser cat prec`. Implementations should call `maybeParenthesize`
with the precedence and `cat`. If no category parenthesizer is registered, the category will never be parenthesized,
but still be traversed for parenthesizing nested categories.",
valueTypeName := `Lean.PrettyPrinter.CategoryParenthesizer,
evalKey := fun _ stx => do
let env ← getEnv
let stx ← Attribute.Builtin.getIdent stx
let id := stx.getId
let some cat := (Parser.parserExtension.getState env).categories.find? id
| throwError "invalid [category_parenthesizer] argument, unknown parser category '{toString id}'"
if (← Elab.getInfoState).enabled && (← getEnv).contains cat.declName then
Elab.addConstInfo stx cat.declName none
pure id
} `Lean.PrettyPrinter.categoryParenthesizerAttribute
@[builtin_init mkCategoryParenthesizerAttribute] opaque categoryParenthesizerAttribute : KeyedDeclsAttribute CategoryParenthesizer
unsafe def mkCombinatorParenthesizerAttribute : IO ParserCompiler.CombinatorAttribute :=
ParserCompiler.registerCombinatorAttribute
`combinator_parenthesizer
"Register a parenthesizer for a parser combinator.
[combinator_parenthesizer c] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `Parser` declaration `c`.
Note that, unlike with [parenthesizer], this is not a node kind since combinators usually do not introduce their own node kinds.
The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced
with `Parenthesizer` in the parameter types."
@[builtin_init mkCombinatorParenthesizerAttribute] opaque combinatorParenthesizerAttribute : ParserCompiler.CombinatorAttribute
namespace Parenthesizer
open Lean.Core Parser
open Std.Format
def throwBacktrack {α} : ParenthesizerM α :=
throw $ Exception.internal backtrackExceptionId
instance : Syntax.MonadTraverser ParenthesizerM := ⟨{
get := State.stxTrav <$> get,
set := fun t => modify (fun st => { st with stxTrav := t }),
modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t }))
}⟩
open Syntax.MonadTraverser
def addPrecCheck (prec : Nat) : ParenthesizerM Unit :=
modify fun st => { st with contPrec := Nat.min (st.contPrec.getD prec) prec, minPrec := Nat.min (st.minPrec.getD prec) prec }
/-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/
def visitArgs (x : ParenthesizerM Unit) : ParenthesizerM Unit := do
let stx ← getCur
if stx.getArgs.size > 0 then
goDown (stx.getArgs.size - 1) *> x <* goUp
goLeft
-- Macro scopes in the parenthesizer output are ultimately ignored by the pretty printer,
-- so give a trivial implementation.
instance : MonadQuotation ParenthesizerM := {
getCurrMacroScope := pure default
getMainModule := pure default
withFreshMacroScope := fun x => x
}
/--
Run `x` and parenthesize the result using `mkParen` if necessary.
If `canJuxtapose` is false, we assume the category does not have a token-less juxtaposition syntax a la function application and deactivate rule 2. -/
def maybeParenthesize (cat : Name) (canJuxtapose : Bool) (mkParen : Syntax → Syntax) (prec : Nat) (x : ParenthesizerM Unit) : ParenthesizerM Unit := do
let stx ← getCur
let idx ← getIdx
let st ← get
-- reset precs for the recursive call
set { stxTrav := st.stxTrav : State }
trace[PrettyPrinter.parenthesize] "parenthesizing (cont := {(st.contPrec, st.contCat)}){indentD (format stx)}"
x
let { minPrec := some minPrec, trailPrec := trailPrec, trailCat := trailCat, .. } ← get
| trace[PrettyPrinter.parenthesize] "visited a syntax tree without precedences?!{line ++ format stx}"
trace[PrettyPrinter.parenthesize] (m!"...precedences are {prec} >? {minPrec}" ++ if canJuxtapose then m!", {(trailPrec, trailCat)} <=? {(st.contPrec, st.contCat)}" else "")
-- Should we parenthesize?
if (prec > minPrec || canJuxtapose && match trailPrec, st.contPrec with | some trailPrec, some contPrec => trailCat == st.contCat && trailPrec <= contPrec | _, _ => false) then
-- The recursive `visit` call, by the invariant, has moved to the preceding node. In order to parenthesize
-- the original node, we must first move to the right, except if we already were at the left-most child in the first
-- place.
if idx > 0 then goRight
let mut stx ← getCur
-- Move leading/trailing whitespace of `stx` outside of parentheses
if let SourceInfo.original _ pos trail endPos := stx.getHeadInfo then
stx := stx.setHeadInfo (SourceInfo.original "".toSubstring pos trail endPos)
if let SourceInfo.original lead pos _ endPos := stx.getTailInfo then
stx := stx.setTailInfo (SourceInfo.original lead pos "".toSubstring endPos)
let mut stx' := mkParen stx
if let SourceInfo.original lead pos _ endPos := stx.getHeadInfo then
stx' := stx'.setHeadInfo (SourceInfo.original lead pos "".toSubstring endPos)
if let SourceInfo.original _ pos trail endPos := stx.getTailInfo then
stx' := stx'.setTailInfo (SourceInfo.original "".toSubstring pos trail endPos)
trace[PrettyPrinter.parenthesize] "parenthesized: {stx'.formatStx none}"
setCur stx'
goLeft
-- after parenthesization, there is no more trailing parser
modify (fun st => { st with contPrec := Parser.maxPrec, contCat := cat, trailPrec := none })
let { trailPrec := trailPrec, .. } ← get
-- If we already had a token at this level, keep the trailing parser. Otherwise, use the minimum of
-- `prec` and `trailPrec`.
if st.visitedToken then
modify fun stP => { stP with trailPrec := st.trailPrec, trailCat := st.trailCat }
else
let trailPrec := match trailPrec with
| some trailPrec => Nat.min trailPrec prec
| _ => prec
modify fun stP => { stP with trailPrec := trailPrec, trailCat := cat }
modify fun stP => { stP with minPrec := st.minPrec }
/-- Adjust state and advance. -/
def visitToken : Parenthesizer := do
modify fun st => { st with contPrec := none, contCat := Name.anonymous, visitedToken := true }
goLeft
@[combinator_parenthesizer orelse] partial def orelse.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := do
let stx ← getCur
-- `orelse` may produce `choice` nodes for antiquotations
if stx.getKind == `choice then
visitArgs $ stx.getArgs.size.forM fun _ => do
orelse.parenthesizer p1 p2
else
-- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try
-- them in turn. Uses the syntax traverser non-linearly!
p1 <|> p2
-- `mkAntiquot` is quite complex, so we'd rather have its parenthesizer synthesized below the actual parser definition.
-- Note that there is a mutual recursion
-- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere
-- anyway.
@[extern "lean_mk_antiquot_parenthesizer"]
opaque mkAntiquot.parenthesizer' (name : String) (kind : SyntaxNodeKind) (anonymous := true) (isPseudoKind := false) : Parenthesizer
@[inline] def liftCoreM {α} (x : CoreM α) : ParenthesizerM α :=
liftM x
-- break up big mutual recursion
@[extern "lean_pretty_printer_parenthesizer_interpret_parser_descr"]
opaque interpretParserDescr' : ParserDescr → CoreM Parenthesizer
unsafe def parenthesizerForKindUnsafe (k : SyntaxNodeKind) : Parenthesizer := do
if k == `missing then
pure ()
else
let p ← runForNodeKind parenthesizerAttribute k interpretParserDescr'
p
@[implemented_by parenthesizerForKindUnsafe]
opaque parenthesizerForKind (k : SyntaxNodeKind) : Parenthesizer
@[combinator_parenthesizer withAntiquot]
def withAntiquot.parenthesizer (antiP p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
-- early check as minor optimization that also cleans up the backtrack traces
if stx.isAntiquot || stx.isAntiquotSplice then
orelse.parenthesizer antiP p
else
p
@[combinator_parenthesizer withAntiquotSuffixSplice]
def withAntiquotSuffixSplice.parenthesizer (_ : SyntaxNodeKind) (p suffix : Parenthesizer) : Parenthesizer := do
if (← getCur).isAntiquotSuffixSplice then
visitArgs <| suffix *> p
else
p
@[combinator_parenthesizer tokenWithAntiquot]
def tokenWithAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
if (← getCur).isTokenAntiquot then
visitArgs p
else
p
partial def parenthesizeCategoryCore (cat : Name) (_prec : Nat) : Parenthesizer :=
withReader (fun ctx => { ctx with cat := cat }) do
let stx ← getCur
if stx.getKind == `choice then
visitArgs $ stx.getArgs.size.forM fun _ => do
parenthesizeCategoryCore cat _prec
else
withAntiquot.parenthesizer (mkAntiquot.parenthesizer' cat.toString cat (isPseudoKind := true)) (parenthesizerForKind stx.getKind)
modify fun st => { st with contCat := cat }
@[combinator_parenthesizer categoryParser]
def categoryParser.parenthesizer (cat : Name) (prec : Nat) : Parenthesizer := do
let env ← getEnv
match categoryParenthesizerAttribute.getValues env cat with
| p::_ => p prec
-- Fall back to the generic parenthesizer.
-- In this case this node will never be parenthesized since we don't know which parentheses to use.
| _ => parenthesizeCategoryCore cat prec
@[combinator_parenthesizer parserOfStack]
def parserOfStack.parenthesizer (offset : Nat) (_prec : Nat := 0) : Parenthesizer := do
let st ← get
let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset)
parenthesizerForKind stx.getKind
@[builtin_category_parenthesizer term]
def term.parenthesizer : CategoryParenthesizer | prec => do
maybeParenthesize `term true (fun stx => Unhygienic.run `(($(⟨stx⟩)))) prec $
parenthesizeCategoryCore `term prec
@[builtin_category_parenthesizer tactic]
def tactic.parenthesizer : CategoryParenthesizer | prec => do
maybeParenthesize `tactic false (fun stx => Unhygienic.run `(tactic|($(⟨stx⟩)))) prec $
parenthesizeCategoryCore `tactic prec
@[builtin_category_parenthesizer level]
def level.parenthesizer : CategoryParenthesizer | prec => do
maybeParenthesize `level false (fun stx => Unhygienic.run `(level|($(⟨stx⟩)))) prec $
parenthesizeCategoryCore `level prec
@[builtin_category_parenthesizer rawStx]
def rawStx.parenthesizer : CategoryParenthesizer | _ => do
goLeft
@[combinator_parenthesizer error]
def error.parenthesizer (_msg : String) : Parenthesizer :=
pure ()
@[combinator_parenthesizer errorAtSavedPos]
def errorAtSavedPos.parenthesizer (_msg : String) (_delta : Bool) : Parenthesizer :=
pure ()
@[combinator_parenthesizer atomic]
def atomic.parenthesizer (p : Parenthesizer) : Parenthesizer :=
p
@[combinator_parenthesizer lookahead]
def lookahead.parenthesizer (_ : Parenthesizer) : Parenthesizer :=
pure ()
@[combinator_parenthesizer notFollowedBy]
def notFollowedBy.parenthesizer (_ : Parenthesizer) : Parenthesizer :=
pure ()
@[combinator_parenthesizer andthen]
def andthen.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer :=
p2 *> p1
def checkKind (k : SyntaxNodeKind) : Parenthesizer := do
let stx ← getCur
if k != stx.getKind then
trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'"
-- HACK; see `orelse.parenthesizer`
throwBacktrack
@[combinator_parenthesizer node]
def node.parenthesizer (k : SyntaxNodeKind) (p : Parenthesizer) : Parenthesizer := do
checkKind k
visitArgs p
@[combinator_parenthesizer checkPrec]
def checkPrec.parenthesizer (prec : Nat) : Parenthesizer :=
addPrecCheck prec
@[combinator_parenthesizer withFn]
def withFn.parenthesizer (_ : ParserFn → ParserFn) (p : Parenthesizer) : Parenthesizer := p
@[combinator_parenthesizer leadingNode]
def leadingNode.parenthesizer (k : SyntaxNodeKind) (prec : Nat) (p : Parenthesizer) : Parenthesizer := do
node.parenthesizer k p
addPrecCheck prec
-- Limit `cont` precedence to `maxPrec-1`.
-- This is because `maxPrec-1` is the precedence of function application, which is the only way to turn a leading parser
-- into a trailing one.
modify fun st => { st with contPrec := Nat.min (Parser.maxPrec-1) prec }
@[combinator_parenthesizer trailingNode]
def trailingNode.parenthesizer (k : SyntaxNodeKind) (prec lhsPrec : Nat) (p : Parenthesizer) : Parenthesizer := do
checkKind k
visitArgs do
p
addPrecCheck prec
let ctx ← read
modify fun st => { st with contCat := ctx.cat }
-- After visiting the nodes actually produced by the parser passed to `trailingNode`, we are positioned on the
-- left-most child, which is the term injected by `trailingNode` in place of the recursion. Left recursion is not an
-- issue for the parenthesizer, so we can think of this child being produced by `termParser lhsPrec`, or whichever Pratt
-- parser is calling us.
categoryParser.parenthesizer ctx.cat lhsPrec
@[combinator_parenthesizer rawCh] def rawCh.parenthesizer (_ch : Char) := visitToken
@[combinator_parenthesizer symbolNoAntiquot] def symbolNoAntiquot.parenthesizer (_sym : String) := visitToken
@[combinator_parenthesizer unicodeSymbolNoAntiquot] def unicodeSymbolNoAntiquot.parenthesizer (_sym _asciiSym : String) := visitToken
@[combinator_parenthesizer identNoAntiquot] def identNoAntiquot.parenthesizer := do checkKind identKind; visitToken
@[combinator_parenthesizer rawIdentNoAntiquot] def rawIdentNoAntiquot.parenthesizer := visitToken
@[combinator_parenthesizer identEq] def identEq.parenthesizer (_id : Name) := visitToken
@[combinator_parenthesizer nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.parenthesizer (_sym : String) (_includeIdent : Bool) := visitToken
@[combinator_parenthesizer charLitNoAntiquot] def charLitNoAntiquot.parenthesizer := visitToken
@[combinator_parenthesizer strLitNoAntiquot] def strLitNoAntiquot.parenthesizer := visitToken
@[combinator_parenthesizer nameLitNoAntiquot] def nameLitNoAntiquot.parenthesizer := visitToken
@[combinator_parenthesizer numLitNoAntiquot] def numLitNoAntiquot.parenthesizer := visitToken
@[combinator_parenthesizer scientificLitNoAntiquot] def scientificLitNoAntiquot.parenthesizer := visitToken
@[combinator_parenthesizer fieldIdx] def fieldIdx.parenthesizer := visitToken
@[combinator_parenthesizer manyNoAntiquot]
def manyNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
visitArgs $ stx.getArgs.size.forM fun _ => p
@[combinator_parenthesizer many1NoAntiquot]
def many1NoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
manyNoAntiquot.parenthesizer p
@[combinator_parenthesizer many1Unbox]
def many1Unbox.parenthesizer (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
if stx.getKind == nullKind then
manyNoAntiquot.parenthesizer p
else
p
@[combinator_parenthesizer optionalNoAntiquot]
def optionalNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
visitArgs p
@[combinator_parenthesizer sepByNoAntiquot]
def sepByNoAntiquot.parenthesizer (p pSep : Parenthesizer) : Parenthesizer := do
let stx ← getCur
visitArgs <| (List.range stx.getArgs.size).reverse.forM fun i => if i % 2 == 0 then p else pSep
@[combinator_parenthesizer sepBy1NoAntiquot] def sepBy1NoAntiquot.parenthesizer := sepByNoAntiquot.parenthesizer
@[combinator_parenthesizer withPosition] def withPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := do
-- We assume the formatter will indent syntax sufficiently such that parenthesizing a `withPosition` node is never necessary
modify fun st => { st with contPrec := none }
p
@[combinator_parenthesizer withPositionAfterLinebreak] def withPositionAfterLinebreak.parenthesizer (p : Parenthesizer) : Parenthesizer :=
-- TODO: improve?
withPosition.parenthesizer p
@[combinator_parenthesizer withoutInfo] def withoutInfo.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinator_parenthesizer checkStackTop] def checkStackTop.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkWsBefore] def checkWsBefore.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkNoWsBefore] def checkNoWsBefore.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkLinebreakBefore] def checkLinebreakBefore.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkTailWs] def checkTailWs.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkColEq] def checkColEq.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkColGe] def checkColGe.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkColGt] def checkColGt.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkLineEq] def checkLineEq.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer eoi] def eoi.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer checkNoImmediateColon] def checkNoImmediateColon.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer skip] def skip.parenthesizer : Parenthesizer := pure ()
@[combinator_parenthesizer pushNone] def pushNone.parenthesizer : Parenthesizer := goLeft
@[combinator_parenthesizer hygieneInfoNoAntiquot] def hygieneInfoNoAntiquot.parenthesizer : Parenthesizer := goLeft
@[combinator_parenthesizer interpolatedStr]
def interpolatedStr.parenthesizer (p : Parenthesizer) : Parenthesizer := do
visitArgs $ (← getCur).getArgs.reverse.forM fun chunk =>
if chunk.isOfKind interpolatedStrLitKind then
goLeft
else
p
@[combinator_parenthesizer _root_.ite, macro_inline] def ite {_ : Type} (c : Prop) [Decidable c] (t e : Parenthesizer) : Parenthesizer :=
if c then t else e
open Parser
abbrev ParenthesizerAliasValue := AliasValue Parenthesizer
builtin_initialize parenthesizerAliasesRef : IO.Ref (NameMap ParenthesizerAliasValue) ← IO.mkRef {}
def registerAlias (aliasName : Name) (v : ParenthesizerAliasValue) : IO Unit := do
Parser.registerAliasCore parenthesizerAliasesRef aliasName v
instance : Coe Parenthesizer ParenthesizerAliasValue := { coe := AliasValue.const }
instance : Coe (Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.unary }
instance : Coe (Parenthesizer → Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.binary }
end Parenthesizer
open Parenthesizer
/-- Add necessary parentheses in `stx` parsed by `parser`. -/
def parenthesize (parenthesizer : Parenthesizer) (stx : Syntax) : CoreM Syntax := do
trace[PrettyPrinter.parenthesize.input] "{format stx}"
catchInternalId backtrackExceptionId
(do
let (_, st) ← (parenthesizer {}).run { stxTrav := Syntax.Traverser.fromSyntax stx }
pure st.stxTrav.cur)
(fun _ => throwError "parenthesize: uncaught backtrack exception")
def parenthesizeCategory (cat : Name) := parenthesize <| categoryParser.parenthesizer cat 0
def parenthesizeTerm := parenthesizeCategory `term
def parenthesizeTactic := parenthesizeCategory `tactic
def parenthesizeCommand := parenthesizeCategory `command
builtin_initialize
registerTraceClass `PrettyPrinter.parenthesize
registerTraceClass `PrettyPrinter.parenthesize.backtrack (inherited := true)
registerTraceClass `PrettyPrinter.parenthesize.input (inherited := true)
end PrettyPrinter
end Lean
|
19ed9047ff87f780cd2b2553dadbc103e864f1f1 | 4fa161becb8ce7378a709f5992a594764699e268 | /src/dynamics/fixed_points/topology.lean | 56a1979f1862c9051ab230f5bb795fab8ff11a27 | [
"Apache-2.0"
] | permissive | laughinggas/mathlib | e4aa4565ae34e46e834434284cb26bd9d67bc373 | 86dcd5cda7a5017c8b3c8876c89a510a19d49aad | refs/heads/master | 1,669,496,232,688 | 1,592,831,995,000 | 1,592,831,995,000 | 274,155,979 | 0 | 0 | Apache-2.0 | 1,592,835,190,000 | 1,592,835,189,000 | null | UTF-8 | Lean | false | false | 1,270 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Johannes Hölzl
-/
import dynamics.fixed_points.basic
import topology.separation
/-!
# Topological properties of fixed points
Currently this file contains two lemmas:
- `is_fixed_pt_of_tendsto_iterate`: if `f^n(x) → y` and `f` is continuous at `y`, then `f y = y`;
- `is_closed_fixed_points`: the set of fixed points of a continuous map is a closed set.
## TODO
fixed points, iterates
-/
variables {α : Type*} [topological_space α] [t2_space α] {f : α → α}
open function filter
open_locale topological_space
/-- If the iterates `f^[n] x` converge to `y` and `f` is continuous at `y`,
then `y` is a fixed point for `f`. -/
lemma is_fixed_pt_of_tendsto_iterate {x y : α} (hy : tendsto (λ n, f^[n] x) at_top (𝓝 y))
(hf : continuous_at f y) :
is_fixed_pt f y :=
begin
refine tendsto_nhds_unique at_top_ne_bot ((tendsto_add_at_top_iff_nat 1).1 _) hy,
simp only [iterate_succ' f],
exact hf.tendsto.comp hy
end
/-- The set of fixed points of a continuous map is a closed set. -/
lemma is_closed_fixed_points (hf : continuous f) : is_closed (fixed_points f) :=
is_closed_eq hf continuous_id
|
41edf7ad9331193579876cd1d46ab383a52937cd | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/tactic/converter/interactive.lean | 7c06dfeaccf0c85485f0015c29785acacb51c91f | [
"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 | 4,291 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Lucas Allen, Keeley Hoek, Leonardo de Moura
Converter monad for building simplifiers.
-/
import tactic.core tactic.converter.old_conv
namespace old_conv
meta def save_info (p : pos) : old_conv unit :=
λ r lhs, do
ts ← tactic.read,
-- TODO(Leo): include context
tactic.save_info_thunk p (λ _, ts.format_expr lhs) >>
return ⟨(), lhs, none⟩
meta def step {α : Type} (c : old_conv α) : old_conv unit :=
c >> return ()
meta def istep {α : Type} (line0 col0 line col : nat) (c : old_conv α) : old_conv unit :=
λ r lhs ts, (@scope_trace _ line col (λ _, (c >> return ()) r lhs ts)).clamp_pos line0 line col
meta def execute (c : old_conv unit) : tactic unit :=
conversion c
namespace interactive
open lean.parser
open interactive
open interactive.types
meta def itactic : Type :=
old_conv unit
meta def whnf : old_conv unit :=
old_conv.whnf
meta def dsimp : old_conv unit :=
old_conv.dsimp
meta def trace_state : old_conv unit :=
old_conv.trace_lhs
meta def change (p : parse texpr) : old_conv unit :=
old_conv.change p
meta def find (p : parse lean.parser.pexpr) (c : itactic) : old_conv unit :=
λ r lhs, do
pat ← tactic.pexpr_to_pattern p,
s ← simp_lemmas.mk_default, -- to be able to use congruence lemmas @[congr]
(found, new_lhs, pr) ←
tactic.ext_simplify_core ff {zeta := ff, beta := ff, single_pass := tt, eta := ff, proj := ff} s
(λ u, return u)
(λ found s r p e, do
guard (not found),
matched ← (tactic.match_pattern pat e >> return tt) <|> return ff,
guard matched,
⟨u, new_e, pr⟩ ← c r e,
return (tt, new_e, pr, ff))
(λ a s r p e, tactic.failed)
r lhs,
if not found then tactic.fail "find converter failed, pattern was not found"
else return ⟨(), new_lhs, some pr⟩
end interactive
end old_conv
namespace conv
open tactic
meta def replace_lhs (tac : expr → tactic (expr × expr)) : conv unit :=
do (e, pf) ← lhs >>= tac, update_lhs e pf
-- Attempts to discharge the equality of the current `lhs` using `tac`,
-- moving to the next goal on success.
meta def discharge_eq_lhs (tac : tactic unit) : conv unit :=
do pf ← lock_tactic_state (do m ← lhs >>= mk_meta_var,
set_goals [m],
tac >> done,
instantiate_mvars m),
congr,
the_rhs ← rhs,
update_lhs the_rhs pf,
skip,
skip
namespace interactive
open interactive
open tactic.interactive (rw_rules)
/-- The `conv` tactic provides a `conv` within a `conv`. It allows the user to return to a
previous state of the outer conv block to continue editing an expression without having to
start a new conv block. -/
protected meta def conv (t : conv.interactive.itactic) : conv unit :=
do transitivity,
a :: rest ← get_goals,
set_goals [a],
t,
all_goals reflexivity,
set_goals rest
meta def erw (q : parse rw_rules) (cfg : rewrite_cfg := {md := semireducible}) : conv unit :=
rw q cfg
open interactive.types
/--
`guard_target t` fails if the target of the conv goal is not `t`.
We use this tactic for writing tests.
-/
meta def guard_target (p : parse texpr) : conv unit :=
do `(%%t = _) ← target, tactic.interactive.guard_expr_eq t p
end interactive
end conv
namespace tactic
namespace interactive
open lean
open lean.parser
open interactive
local postfix `?`:9001 := optional
meta def old_conv (c : old_conv.interactive.itactic) : tactic unit :=
do t ← target,
(new_t, pr) ← c.to_tactic `eq t,
replace_target new_t pr
meta def find (p : parse lean.parser.pexpr) (c : old_conv.interactive.itactic) : tactic unit :=
old_conv $ old_conv.interactive.find p c
meta def conv_lhs (loc : parse (tk "at" *> ident)?)
(p : parse (tk "in" *> parser.pexpr)?)
(c : conv.interactive.itactic) : tactic unit :=
conv loc p (conv.interactive.to_lhs >> c)
meta def conv_rhs (loc : parse (tk "at" *> ident)?)
(p : parse (tk "in" *> parser.pexpr)?)
(c : conv.interactive.itactic) : tactic unit :=
conv loc p (conv.interactive.to_rhs >> c)
end interactive
end tactic
|
6f0a4cf062c89ceaba2539e9d8f4948eb1e57a1c | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/ring_theory/polynomial/cyclotomic/basic.lean | 2aba39c26b744d206683b5a18b493ca84bc9c573 | [
"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 | 48,192 | lean | /-
Copyright (c) 2020 Riccardo Brasca. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Riccardo Brasca
-/
import algebra.polynomial.big_operators
import analysis.complex.roots_of_unity
import data.polynomial.lifts
import field_theory.separable
import field_theory.splitting_field
import number_theory.arithmetic_function
import ring_theory.roots_of_unity
import field_theory.ratfunc
import algebra.ne_zero
/-!
# Cyclotomic polynomials.
For `n : ℕ` and an integral domain `R`, we define a modified version of the `n`-th cyclotomic
polynomial with coefficients in `R`, denoted `cyclotomic' n R`, as `∏ (X - μ)`, where `μ` varies
over the primitive `n`th roots of unity. If there is a primitive `n`th root of unity in `R` then
this the standard definition. We then define the standard cyclotomic polynomial `cyclotomic n R`
with coefficients in any ring `R`.
## Main definition
* `cyclotomic n R` : the `n`-th cyclotomic polynomial with coefficients in `R`.
## Main results
* `int_coeff_of_cycl` : If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K`
comes from a polynomial with integer coefficients.
* `deg_of_cyclotomic` : The degree of `cyclotomic n` is `totient n`.
* `prod_cyclotomic_eq_X_pow_sub_one` : `X ^ n - 1 = ∏ (cyclotomic i)`, where `i` divides `n`.
* `cyclotomic_eq_prod_X_pow_sub_one_pow_moebius` : The Möbius inversion formula for
`cyclotomic n R` over an abstract fraction field for `polynomial R`.
* `cyclotomic.irreducible` : `cyclotomic n ℤ` is irreducible.
## Implementation details
Our definition of `cyclotomic' n R` makes sense in any integral domain `R`, but the interesting
results hold if there is a primitive `n`-th root of unity in `R`. In particular, our definition is
not the standard one unless there is a primitive `n`th root of unity in `R`. For example,
`cyclotomic' 3 ℤ = 1`, since there are no primitive cube roots of unity in `ℤ`. The main example is
`R = ℂ`, we decided to work in general since the difficulties are essentially the same.
To get the standard cyclotomic polynomials, we use `int_coeff_of_cycl`, with `R = ℂ`, to get a
polynomial with integer coefficients and then we map it to `polynomial R`, for any ring `R`.
To prove `cyclotomic.irreducible`, the irreducibility of `cyclotomic n ℤ`, we show in
`cyclotomic_eq_minpoly` that `cyclotomic n ℤ` is the minimal polynomial of any `n`-th primitive root
of unity `μ : K`, where `K` is a field of characteristic `0`.
-/
open_locale classical big_operators polynomial
noncomputable theory
universe u
namespace polynomial
section cyclotomic'
section is_domain
variables {R : Type*} [comm_ring R] [is_domain R]
/-- The modified `n`-th cyclotomic polynomial with coefficients in `R`, it is the usual cyclotomic
polynomial if there is a primitive `n`-th root of unity in `R`. -/
def cyclotomic' (n : ℕ) (R : Type*) [comm_ring R] [is_domain R] : R[X] :=
∏ μ in primitive_roots n R, (X - C μ)
/-- The zeroth modified cyclotomic polyomial is `1`. -/
@[simp] lemma cyclotomic'_zero
(R : Type*) [comm_ring R] [is_domain R] : cyclotomic' 0 R = 1 :=
by simp only [cyclotomic', finset.prod_empty, is_primitive_root.primitive_roots_zero]
/-- The first modified cyclotomic polyomial is `X - 1`. -/
@[simp] lemma cyclotomic'_one
(R : Type*) [comm_ring R] [is_domain R] : cyclotomic' 1 R = X - 1 :=
begin
simp only [cyclotomic', finset.prod_singleton, ring_hom.map_one,
is_primitive_root.primitive_roots_one]
end
/-- The second modified cyclotomic polyomial is `X + 1` if the characteristic of `R` is not `2`. -/
@[simp] lemma cyclotomic'_two
(R : Type*) [comm_ring R] [is_domain R] (p : ℕ) [char_p R p] (hp : p ≠ 2) :
cyclotomic' 2 R = X + 1 :=
begin
rw [cyclotomic'],
have prim_root_two : primitive_roots 2 R = {(-1 : R)},
{ apply finset.eq_singleton_iff_unique_mem.2,
split,
{ simp only [is_primitive_root.neg_one p hp, nat.succ_pos', mem_primitive_roots] },
{ intros x hx,
rw [mem_primitive_roots zero_lt_two] at hx,
exact is_primitive_root.eq_neg_one_of_two_right hx } },
simp only [prim_root_two, finset.prod_singleton, ring_hom.map_neg, ring_hom.map_one,
sub_neg_eq_add]
end
/-- `cyclotomic' n R` is monic. -/
lemma cyclotomic'.monic
(n : ℕ) (R : Type*) [comm_ring R] [is_domain R] : (cyclotomic' n R).monic :=
monic_prod_of_monic _ _ $ λ z hz, monic_X_sub_C _
/-- `cyclotomic' n R` is different from `0`. -/
lemma cyclotomic'_ne_zero
(n : ℕ) (R : Type*) [comm_ring R] [is_domain R] : cyclotomic' n R ≠ 0 :=
(cyclotomic'.monic n R).ne_zero
/-- The natural degree of `cyclotomic' n R` is `totient n` if there is a primitive root of
unity in `R`. -/
lemma nat_degree_cyclotomic' {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) :
(cyclotomic' n R).nat_degree = nat.totient n :=
begin
rw [cyclotomic'],
rw nat_degree_prod (primitive_roots n R) (λ (z : R), (X - C z)),
simp only [is_primitive_root.card_primitive_roots h, mul_one,
nat_degree_X_sub_C,
nat.cast_id, finset.sum_const, nsmul_eq_mul],
intros z hz,
exact X_sub_C_ne_zero z
end
/-- The degree of `cyclotomic' n R` is `totient n` if there is a primitive root of unity in `R`. -/
lemma degree_cyclotomic' {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) :
(cyclotomic' n R).degree = nat.totient n :=
by simp only [degree_eq_nat_degree (cyclotomic'_ne_zero n R), nat_degree_cyclotomic' h]
/-- The roots of `cyclotomic' n R` are the primitive `n`-th roots of unity. -/
lemma roots_of_cyclotomic (n : ℕ) (R : Type*) [comm_ring R] [is_domain R] :
(cyclotomic' n R).roots = (primitive_roots n R).val :=
by { rw cyclotomic', exact roots_prod_X_sub_C (primitive_roots n R) }
/-- If there is a primitive `n`th root of unity in `K`, then `X ^ n - 1 = ∏ (X - μ)`, where `μ`
varies over the `n`-th roots of unity. -/
lemma X_pow_sub_one_eq_prod {ζ : R} {n : ℕ} (hpos : 0 < n) (h : is_primitive_root ζ n) :
X ^ n - 1 = ∏ ζ in nth_roots_finset n R, (X - C ζ) :=
begin
rw [nth_roots_finset, ← multiset.to_finset_eq (is_primitive_root.nth_roots_nodup h)],
simp only [finset.prod_mk, ring_hom.map_one],
rw [nth_roots],
have hmonic : (X ^ n - C (1 : R)).monic := monic_X_pow_sub_C (1 : R) (ne_of_lt hpos).symm,
symmetry,
apply prod_multiset_X_sub_C_of_monic_of_roots_card_eq hmonic,
rw [@nat_degree_X_pow_sub_C R _ _ n 1, ← nth_roots],
exact is_primitive_root.card_nth_roots h
end
end is_domain
section field
variables {K : Type*} [field K]
/-- `cyclotomic' n K` splits. -/
lemma cyclotomic'_splits (n : ℕ) : splits (ring_hom.id K) (cyclotomic' n K) :=
begin
apply splits_prod (ring_hom.id K),
intros z hz,
simp only [splits_X_sub_C (ring_hom.id K)]
end
/-- If there is a primitive `n`-th root of unity in `K`, then `X ^ n - 1`splits. -/
lemma X_pow_sub_one_splits {ζ : K} {n : ℕ} (h : is_primitive_root ζ n) :
splits (ring_hom.id K) (X ^ n - C (1 : K)) :=
by rw [splits_iff_card_roots, ← nth_roots, is_primitive_root.card_nth_roots h,
nat_degree_X_pow_sub_C]
/-- If there is a primitive `n`-th root of unity in `K`, then
`∏ i in nat.divisors n, cyclotomic' i K = X ^ n - 1`. -/
lemma prod_cyclotomic'_eq_X_pow_sub_one {K : Type*} [comm_ring K] [is_domain K] {ζ : K} {n : ℕ}
(hpos : 0 < n) (h : is_primitive_root ζ n) : ∏ i in nat.divisors n, cyclotomic' i K = X ^ n - 1 :=
begin
rw [X_pow_sub_one_eq_prod hpos h],
have rwcyc : ∀ i ∈ nat.divisors n, cyclotomic' i K = ∏ μ in primitive_roots i K, (X - C μ),
{ intros i hi,
simp only [cyclotomic'] },
conv_lhs { apply_congr,
skip,
simp [rwcyc, H] },
rw ← finset.prod_bUnion,
{ simp only [is_primitive_root.nth_roots_one_eq_bUnion_primitive_roots h] },
intros x hx y hy hdiff,
exact is_primitive_root.disjoint hdiff,
end
/-- If there is a primitive `n`-th root of unity in `K`, then
`cyclotomic' n K = (X ^ k - 1) /ₘ (∏ i in nat.proper_divisors k, cyclotomic' i K)`. -/
lemma cyclotomic'_eq_X_pow_sub_one_div {K : Type*} [comm_ring K] [is_domain K] {ζ : K} {n : ℕ}
(hpos : 0 < n) (h : is_primitive_root ζ n) :
cyclotomic' n K = (X ^ n - 1) /ₘ (∏ i in nat.proper_divisors n, cyclotomic' i K) :=
begin
rw [←prod_cyclotomic'_eq_X_pow_sub_one hpos h,
nat.divisors_eq_proper_divisors_insert_self_of_pos hpos,
finset.prod_insert nat.proper_divisors.not_self_mem],
have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic' i K).monic,
{ apply monic_prod_of_monic,
intros i hi,
exact cyclotomic'.monic i K },
rw (div_mod_by_monic_unique (cyclotomic' n K) 0 prod_monic _).1,
simp only [degree_zero, zero_add],
refine ⟨by rw mul_comm, _⟩,
rw [bot_lt_iff_ne_bot],
intro h,
exact monic.ne_zero prod_monic (degree_eq_bot.1 h)
end
/-- If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K` comes from a
monic polynomial with integer coefficients. -/
lemma int_coeff_of_cyclotomic' {K : Type*} [comm_ring K] [is_domain K] {ζ : K} {n : ℕ}
(h : is_primitive_root ζ n) :
(∃ (P : ℤ[X]), map (int.cast_ring_hom K) P = cyclotomic' n K ∧
P.degree = (cyclotomic' n K).degree ∧ P.monic) :=
begin
refine lifts_and_degree_eq_and_monic _ (cyclotomic'.monic n K),
induction n using nat.strong_induction_on with k hk generalizing ζ h,
cases nat.eq_zero_or_pos k with hzero hpos,
{ use 1,
simp only [hzero, cyclotomic'_zero, set.mem_univ, subsemiring.coe_top, eq_self_iff_true,
coe_map_ring_hom, polynomial.map_one, and_self] },
let B : K[X] := ∏ i in nat.proper_divisors k, cyclotomic' i K,
have Bmo : B.monic,
{ apply monic_prod_of_monic,
intros i hi,
exact (cyclotomic'.monic i K) },
have Bint : B ∈ lifts (int.cast_ring_hom K),
{ refine subsemiring.prod_mem (lifts (int.cast_ring_hom K)) _,
intros x hx,
have xsmall := (nat.mem_proper_divisors.1 hx).2,
obtain ⟨d, hd⟩ := (nat.mem_proper_divisors.1 hx).1,
rw [mul_comm] at hd,
exact hk x xsmall (is_primitive_root.pow hpos h hd) },
replace Bint := lifts_and_degree_eq_and_monic Bint Bmo,
obtain ⟨B₁, hB₁, hB₁deg, hB₁mo⟩ := Bint,
let Q₁ : ℤ[X] := (X ^ k - 1) /ₘ B₁,
have huniq : 0 + B * cyclotomic' k K = X ^ k - 1 ∧ (0 : K[X]).degree < B.degree,
{ split,
{ rw [zero_add, mul_comm, ←(prod_cyclotomic'_eq_X_pow_sub_one hpos h),
nat.divisors_eq_proper_divisors_insert_self_of_pos hpos],
simp only [true_and, finset.prod_insert, not_lt, nat.mem_proper_divisors, dvd_refl] },
rw [degree_zero, bot_lt_iff_ne_bot],
intro habs,
exact (monic.ne_zero Bmo) (degree_eq_bot.1 habs) },
replace huniq := div_mod_by_monic_unique (cyclotomic' k K) (0 : K[X]) Bmo huniq,
simp only [lifts, ring_hom.mem_srange],
use Q₁,
rw [coe_map_ring_hom, (map_div_by_monic (int.cast_ring_hom K) hB₁mo), hB₁, ← huniq.1],
simp
end
/-- If `K` is of characteristic `0` and there is a primitive `n`-th root of unity in `K`,
then `cyclotomic n K` comes from a unique polynomial with integer coefficients. -/
lemma unique_int_coeff_of_cycl {K : Type*} [comm_ring K] [is_domain K] [char_zero K] {ζ : K}
{n : ℕ+} (h : is_primitive_root ζ n) :
(∃! (P : ℤ[X]), map (int.cast_ring_hom K) P = cyclotomic' n K) :=
begin
obtain ⟨P, hP⟩ := int_coeff_of_cyclotomic' h,
refine ⟨P, hP.1, λ Q hQ, _⟩,
apply (map_injective (int.cast_ring_hom K) int.cast_injective),
rw [hP.1, hQ]
end
end field
end cyclotomic'
section cyclotomic
/-- The `n`-th cyclotomic polynomial with coefficients in `R`. -/
def cyclotomic (n : ℕ) (R : Type*) [ring R] : R[X] :=
if h : n = 0 then 1 else
map (int.cast_ring_hom R) ((int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n h)).some)
lemma int_cyclotomic_rw {n : ℕ} (h : n ≠ 0) :
cyclotomic n ℤ = (int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n h)).some :=
begin
simp only [cyclotomic, h, dif_neg, not_false_iff],
ext i,
simp only [coeff_map, int.cast_id, ring_hom.eq_int_cast]
end
/-- `cyclotomic n R` comes from `cyclotomic n ℤ`. -/
lemma map_cyclotomic_int (n : ℕ) (R : Type*) [ring R] :
map (int.cast_ring_hom R) (cyclotomic n ℤ) = cyclotomic n R :=
begin
by_cases hzero : n = 0,
{ simp only [hzero, cyclotomic, dif_pos, polynomial.map_one] },
simp only [cyclotomic, int_cyclotomic_rw, hzero, ne.def, dif_neg, not_false_iff]
end
lemma int_cyclotomic_spec (n : ℕ) : map (int.cast_ring_hom ℂ) (cyclotomic n ℤ) = cyclotomic' n ℂ ∧
(cyclotomic n ℤ).degree = (cyclotomic' n ℂ).degree ∧ (cyclotomic n ℤ).monic :=
begin
by_cases hzero : n = 0,
{ simp only [hzero, cyclotomic, degree_one, monic_one, cyclotomic'_zero, dif_pos,
eq_self_iff_true, polynomial.map_one, and_self] },
rw int_cyclotomic_rw hzero,
exact (int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n hzero)).some_spec
end
lemma int_cyclotomic_unique {n : ℕ} {P : ℤ[X]} (h : map (int.cast_ring_hom ℂ) P =
cyclotomic' n ℂ) : P = cyclotomic n ℤ :=
begin
apply map_injective (int.cast_ring_hom ℂ) int.cast_injective,
rw [h, (int_cyclotomic_spec n).1]
end
/-- The definition of `cyclotomic n R` commutes with any ring homomorphism. -/
@[simp] lemma map_cyclotomic (n : ℕ) {R S : Type*} [ring R] [ring S] (f : R →+* S) :
map f (cyclotomic n R) = cyclotomic n S :=
begin
rw [←map_cyclotomic_int n R, ←map_cyclotomic_int n S],
ext i,
simp only [coeff_map, ring_hom.eq_int_cast, ring_hom.map_int_cast]
end
lemma cyclotomic.eval_apply {R S : Type*} (q : R) (n : ℕ) [ring R] [ring S] (f : R →+* S) :
eval (f q) (cyclotomic n S) = f (eval q (cyclotomic n R)) :=
by rw [← map_cyclotomic n f, eval_map, eval₂_at_apply]
/-- The zeroth cyclotomic polyomial is `1`. -/
@[simp] lemma cyclotomic_zero (R : Type*) [ring R] : cyclotomic 0 R = 1 :=
by simp only [cyclotomic, dif_pos]
/-- The first cyclotomic polyomial is `X - 1`. -/
@[simp] lemma cyclotomic_one (R : Type*) [ring R] : cyclotomic 1 R = X - 1 :=
begin
have hspec : map (int.cast_ring_hom ℂ) (X - 1) = cyclotomic' 1 ℂ,
{ simp only [cyclotomic'_one, pnat.one_coe, map_X, polynomial.map_one, polynomial.map_sub] },
symmetry,
rw [←map_cyclotomic_int, ←(int_cyclotomic_unique hspec)],
simp only [map_X, polynomial.map_one, polynomial.map_sub]
end
/-- The second cyclotomic polyomial is `X + 1`. -/
@[simp] lemma cyclotomic_two (R : Type*) [ring R] : cyclotomic 2 R = X + 1 :=
begin
have hspec : map (int.cast_ring_hom ℂ) (X + 1) = cyclotomic' 2 ℂ,
{ simp only [cyclotomic'_two ℂ 0 two_ne_zero.symm, polynomial.map_add, map_X,
polynomial.map_one], },
symmetry,
rw [←map_cyclotomic_int, ←(int_cyclotomic_unique hspec)],
simp only [polynomial.map_add, map_X, polynomial.map_one]
end
/-- `cyclotomic n` is monic. -/
lemma cyclotomic.monic (n : ℕ) (R : Type*) [ring R] : (cyclotomic n R).monic :=
begin
rw ←map_cyclotomic_int,
exact (int_cyclotomic_spec n).2.2.map _,
end
/-- `cyclotomic n` is primitive. -/
lemma cyclotomic.is_primitive (n : ℕ) (R : Type*) [comm_ring R] : (cyclotomic n R).is_primitive :=
(cyclotomic.monic n R).is_primitive
/-- `cyclotomic n R` is different from `0`. -/
lemma cyclotomic_ne_zero (n : ℕ) (R : Type*) [ring R] [nontrivial R] : cyclotomic n R ≠ 0 :=
(cyclotomic.monic n R).ne_zero
/-- The degree of `cyclotomic n` is `totient n`. -/
lemma degree_cyclotomic (n : ℕ) (R : Type*) [ring R] [nontrivial R] :
(cyclotomic n R).degree = nat.totient n :=
begin
rw ←map_cyclotomic_int,
rw degree_map_eq_of_leading_coeff_ne_zero (int.cast_ring_hom R) _,
{ cases n with k,
{ simp only [cyclotomic, degree_one, dif_pos, nat.totient_zero, with_top.coe_zero]},
rw [←degree_cyclotomic' (complex.is_primitive_root_exp k.succ (nat.succ_ne_zero k))],
exact (int_cyclotomic_spec k.succ).2.1 },
simp only [(int_cyclotomic_spec n).right.right, ring_hom.eq_int_cast, monic.leading_coeff,
int.cast_one, ne.def, not_false_iff, one_ne_zero]
end
/-- The natural degree of `cyclotomic n` is `totient n`. -/
lemma nat_degree_cyclotomic (n : ℕ) (R : Type*) [ring R] [nontrivial R] :
(cyclotomic n R).nat_degree = nat.totient n :=
begin
have hdeg := degree_cyclotomic n R,
rw degree_eq_nat_degree (cyclotomic_ne_zero n R) at hdeg,
exact_mod_cast hdeg
end
/-- The degree of `cyclotomic n R` is positive. -/
lemma degree_cyclotomic_pos (n : ℕ) (R : Type*) (hpos : 0 < n) [ring R] [nontrivial R] :
0 < (cyclotomic n R).degree := by
{ rw degree_cyclotomic n R, exact_mod_cast (nat.totient_pos hpos) }
/-- `∏ i in nat.divisors n, cyclotomic i R = X ^ n - 1`. -/
lemma prod_cyclotomic_eq_X_pow_sub_one {n : ℕ} (hpos : 0 < n) (R : Type*) [comm_ring R] :
∏ i in nat.divisors n, cyclotomic i R = X ^ n - 1 :=
begin
have integer : ∏ i in nat.divisors n, cyclotomic i ℤ = X ^ n - 1,
{ apply map_injective (int.cast_ring_hom ℂ) int.cast_injective,
rw polynomial.map_prod (int.cast_ring_hom ℂ) (λ i, cyclotomic i ℤ),
simp only [int_cyclotomic_spec, polynomial.map_pow, nat.cast_id, map_X, polynomial.map_one,
polynomial.map_sub],
exact prod_cyclotomic'_eq_X_pow_sub_one hpos
(complex.is_primitive_root_exp n (ne_of_lt hpos).symm) },
have coerc : X ^ n - 1 = map (int.cast_ring_hom R) (X ^ n - 1),
{ simp only [polynomial.map_pow, polynomial.map_X, polynomial.map_one, polynomial.map_sub] },
have h : ∀ i ∈ n.divisors, cyclotomic i R = map (int.cast_ring_hom R) (cyclotomic i ℤ),
{ intros i hi,
exact (map_cyclotomic_int i R).symm },
rw [finset.prod_congr (refl n.divisors) h, coerc,
← polynomial.map_prod (int.cast_ring_hom R) (λ i, cyclotomic i ℤ), integer]
end
lemma cyclotomic.dvd_X_pow_sub_one (n : ℕ) (R : Type*) [comm_ring R] :
(cyclotomic n R) ∣ X ^ n - 1 :=
begin
rcases n.eq_zero_or_pos with rfl | hn,
{ simp },
refine ⟨∏ i in n.proper_divisors, cyclotomic i R, _⟩,
rw [←prod_cyclotomic_eq_X_pow_sub_one hn,
nat.divisors_eq_proper_divisors_insert_self_of_pos hn, finset.prod_insert],
exact nat.proper_divisors.not_self_mem
end
open_locale big_operators
open finset
lemma prod_cyclotomic_eq_geom_sum {n : ℕ} (h : 0 < n) (R) [comm_ring R] [is_domain R] :
∏ i in n.divisors \ {1}, cyclotomic i R = ∑ i in finset.range n, X ^ i :=
begin
apply_fun (* cyclotomic 1 R) using mul_left_injective₀ (cyclotomic_ne_zero 1 R),
have : ∏ i in {1}, cyclotomic i R = cyclotomic 1 R := finset.prod_singleton,
simp_rw [←this, finset.prod_sdiff $ show {1} ⊆ n.divisors, by simp [h.ne'], this, cyclotomic_one,
geom_sum_mul, prod_cyclotomic_eq_X_pow_sub_one h]
end
lemma cyclotomic_dvd_geom_sum_of_dvd (R) [comm_ring R] {d n : ℕ} (hdn : d ∣ n)
(hd : d ≠ 1) : cyclotomic d R ∣ ∑ i in finset.range n, X ^ i :=
begin
suffices : (cyclotomic d ℤ).map (int.cast_ring_hom R) ∣
(∑ i in finset.range n, X ^ i).map (int.cast_ring_hom R),
{ have key := (map_ring_hom (int.cast_ring_hom R)).map_geom_sum X n,
simp only [coe_map_ring_hom, map_X] at key,
rwa [map_cyclotomic, key] at this },
apply map_dvd,
rcases n.eq_zero_or_pos with rfl | hn,
{ simp },
rw ←prod_cyclotomic_eq_geom_sum hn,
apply finset.dvd_prod_of_mem,
simp [hd, hdn, hn.ne']
end
lemma X_pow_sub_one_mul_prod_cyclotomic_eq_X_pow_sub_one_of_dvd (R) [comm_ring R] {d n : ℕ}
(h : d ∈ n.proper_divisors) :
(X ^ d - 1) * ∏ x in n.divisors \ d.divisors, cyclotomic x R = X ^ n - 1 :=
begin
obtain ⟨hd, hdn⟩ := nat.mem_proper_divisors.mp h,
have h0n := pos_of_gt hdn,
rcases d.eq_zero_or_pos with rfl | h0d,
{ exfalso, linarith [eq_zero_of_zero_dvd hd] },
rw [←prod_cyclotomic_eq_X_pow_sub_one h0d, ←prod_cyclotomic_eq_X_pow_sub_one h0n,
mul_comm, finset.prod_sdiff (nat.divisors_subset_of_dvd h0n.ne' hd)]
end
lemma X_pow_sub_one_mul_cyclotomic_dvd_X_pow_sub_one_of_dvd (R) [comm_ring R] {d n : ℕ}
(h : d ∈ n.proper_divisors) : (X ^ d - 1) * cyclotomic n R ∣ X ^ n - 1 :=
begin
have hdn := (nat.mem_proper_divisors.mp h).2,
use ∏ x in n.proper_divisors \ d.divisors, cyclotomic x R,
symmetry,
convert X_pow_sub_one_mul_prod_cyclotomic_eq_X_pow_sub_one_of_dvd R h using 1,
rw mul_assoc,
congr' 1,
rw [nat.divisors_eq_proper_divisors_insert_self_of_pos $ pos_of_gt hdn,
finset.insert_sdiff_of_not_mem, finset.prod_insert],
{ exact finset.not_mem_sdiff_of_not_mem_left nat.proper_divisors.not_self_mem },
{ exact λ hk, hdn.not_le $ nat.divisor_le hk }
end
lemma _root_.is_root_of_unity_iff {n : ℕ} (h : 0 < n) (R : Type*) [comm_ring R] [is_domain R]
{ζ : R} : ζ ^ n = 1 ↔ ∃ i ∈ n.divisors, (cyclotomic i R).is_root ζ :=
by rw [←mem_nth_roots h, nth_roots, mem_roots $ X_pow_sub_C_ne_zero h _,
C_1, ←prod_cyclotomic_eq_X_pow_sub_one h, is_root_prod]; apply_instance
lemma is_root_of_unity_of_root_cyclotomic {n : ℕ} {R} [comm_ring R] {ζ : R} {i : ℕ}
(hi : i ∈ n.divisors) (h : (cyclotomic i R).is_root ζ) : ζ ^ n = 1 :=
begin
rcases n.eq_zero_or_pos with rfl | hn,
{ exact pow_zero _ },
have := congr_arg (eval ζ) (prod_cyclotomic_eq_X_pow_sub_one hn R).symm,
rw [eval_sub, eval_pow, eval_X, eval_one] at this,
convert eq_add_of_sub_eq' this,
convert (add_zero _).symm,
apply eval_eq_zero_of_dvd_of_eval_eq_zero _ h,
exact finset.dvd_prod_of_mem _ hi
end
section arithmetic_function
open nat.arithmetic_function
open_locale arithmetic_function
/-- `cyclotomic n R` can be expressed as a product in a fraction field of `polynomial R`
using Möbius inversion. -/
lemma cyclotomic_eq_prod_X_pow_sub_one_pow_moebius {n : ℕ} (R : Type*) [comm_ring R] [is_domain R] :
algebra_map _ (ratfunc R) (cyclotomic n R) =
∏ i in n.divisors_antidiagonal, (algebra_map R[X] _ (X ^ i.snd - 1)) ^ μ i.fst :=
begin
rcases n.eq_zero_or_pos with rfl | hpos,
{ simp },
have h : ∀ (n : ℕ), 0 < n →
∏ i in nat.divisors n, algebra_map _ (ratfunc R) (cyclotomic i R) = algebra_map _ _ (X ^ n - 1),
{ intros n hn,
rw [← prod_cyclotomic_eq_X_pow_sub_one hn R, ring_hom.map_prod] },
rw (prod_eq_iff_prod_pow_moebius_eq_of_nonzero (λ n hn, _) (λ n hn, _)).1 h n hpos;
rw [ne.def, is_fraction_ring.to_map_eq_zero_iff],
{ apply cyclotomic_ne_zero },
{ apply monic.ne_zero,
apply monic_X_pow_sub_C _ (ne_of_gt hn) }
end
end arithmetic_function
/-- We have
`cyclotomic n R = (X ^ k - 1) /ₘ (∏ i in nat.proper_divisors k, cyclotomic i K)`. -/
lemma cyclotomic_eq_X_pow_sub_one_div {R : Type*} [comm_ring R] {n : ℕ}
(hpos: 0 < n) : cyclotomic n R = (X ^ n - 1) /ₘ (∏ i in nat.proper_divisors n, cyclotomic i R) :=
begin
nontriviality R,
rw [←prod_cyclotomic_eq_X_pow_sub_one hpos,
nat.divisors_eq_proper_divisors_insert_self_of_pos hpos,
finset.prod_insert nat.proper_divisors.not_self_mem],
have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic i R).monic,
{ apply monic_prod_of_monic,
intros i hi,
exact cyclotomic.monic i R },
rw (div_mod_by_monic_unique (cyclotomic n R) 0 prod_monic _).1,
simp only [degree_zero, zero_add],
split,
{ rw mul_comm },
rw [bot_lt_iff_ne_bot],
intro h,
exact monic.ne_zero prod_monic (degree_eq_bot.1 h)
end
/-- If `m` is a proper divisor of `n`, then `X ^ m - 1` divides
`∏ i in nat.proper_divisors n, cyclotomic i R`. -/
lemma X_pow_sub_one_dvd_prod_cyclotomic (R : Type*) [comm_ring R] {n m : ℕ} (hpos : 0 < n)
(hm : m ∣ n) (hdiff : m ≠ n) : X ^ m - 1 ∣ ∏ i in nat.proper_divisors n, cyclotomic i R :=
begin
replace hm := nat.mem_proper_divisors.2 ⟨hm, lt_of_le_of_ne (nat.divisor_le (nat.mem_divisors.2
⟨hm, (ne_of_lt hpos).symm⟩)) hdiff⟩,
rw [← finset.sdiff_union_of_subset (nat.divisors_subset_proper_divisors (ne_of_lt hpos).symm
(nat.mem_proper_divisors.1 hm).1 (ne_of_lt (nat.mem_proper_divisors.1 hm).2)),
finset.prod_union finset.sdiff_disjoint, prod_cyclotomic_eq_X_pow_sub_one
(nat.pos_of_mem_proper_divisors hm)],
exact ⟨(∏ (x : ℕ) in n.proper_divisors \ m.divisors, cyclotomic x R), by rw mul_comm⟩
end
/-- If there is a primitive `n`-th root of unity in `K`, then
`cyclotomic n K = ∏ μ in primitive_roots n R, (X - C μ)`. In particular,
`cyclotomic n K = cyclotomic' n K` -/
lemma cyclotomic_eq_prod_X_sub_primitive_roots {K : Type*} [comm_ring K] [is_domain K] {ζ : K}
{n : ℕ} (hz : is_primitive_root ζ n) :
cyclotomic n K = ∏ μ in primitive_roots n K, (X - C μ) :=
begin
rw ←cyclotomic',
induction n using nat.strong_induction_on with k hk generalizing ζ hz,
obtain hzero | hpos := k.eq_zero_or_pos,
{ simp only [hzero, cyclotomic'_zero, cyclotomic_zero] },
have h : ∀ i ∈ k.proper_divisors, cyclotomic i K = cyclotomic' i K,
{ intros i hi,
obtain ⟨d, hd⟩ := (nat.mem_proper_divisors.1 hi).1,
rw mul_comm at hd,
exact hk i (nat.mem_proper_divisors.1 hi).2 (is_primitive_root.pow hpos hz hd) },
rw [@cyclotomic_eq_X_pow_sub_one_div _ _ _ hpos,
cyclotomic'_eq_X_pow_sub_one_div hpos hz, finset.prod_congr (refl k.proper_divisors) h]
end
section roots
variables {R : Type*} {n : ℕ} [comm_ring R] [is_domain R]
/-- Any `n`-th primitive root of unity is a root of `cyclotomic n K`.-/
lemma _root_.is_primitive_root.is_root_cyclotomic (hpos : 0 < n) {μ : R}
(h : is_primitive_root μ n) : is_root (cyclotomic n R) μ :=
begin
rw [← mem_roots (cyclotomic_ne_zero n R),
cyclotomic_eq_prod_X_sub_primitive_roots h, roots_prod_X_sub_C, ← finset.mem_def],
rwa [← mem_primitive_roots hpos] at h,
end
private lemma is_root_cyclotomic_iff' {n : ℕ} {K : Type*} [field K] {μ : K} [ne_zero (n : K)] :
is_root (cyclotomic n K) μ ↔ is_primitive_root μ n :=
begin
-- in this proof, `o` stands for `order_of μ`
have hnpos : 0 < n := (ne_zero.of_ne_zero_coe K).out.bot_lt,
refine ⟨λ hμ, _, is_primitive_root.is_root_cyclotomic hnpos⟩,
have hμn : μ ^ n = 1,
{ rw is_root_of_unity_iff hnpos,
exact ⟨n, n.mem_divisors_self hnpos.ne', hμ⟩ },
by_contra hnμ,
have ho : 0 < order_of μ,
{ apply order_of_pos',
rw is_of_fin_order_iff_pow_eq_one,
exact ⟨n, hnpos, hμn⟩ },
have := pow_order_of_eq_one μ,
rw is_root_of_unity_iff ho at this,
obtain ⟨i, hio, hiμ⟩ := this,
replace hio := nat.dvd_of_mem_divisors hio,
rw is_primitive_root.not_iff at hnμ,
rw ←order_of_dvd_iff_pow_eq_one at hμn,
have key : i < n := (nat.le_of_dvd ho hio).trans_lt ((nat.le_of_dvd hnpos hμn).lt_of_ne hnμ),
have key' : i ∣ n := hio.trans hμn,
rw ←polynomial.dvd_iff_is_root at hμ hiμ,
have hni : {i, n} ⊆ n.divisors,
{ simpa [finset.insert_subset, key'] using hnpos.ne' },
obtain ⟨k, hk⟩ := hiμ,
obtain ⟨j, hj⟩ := hμ,
have := prod_cyclotomic_eq_X_pow_sub_one hnpos K,
rw [←finset.prod_sdiff hni, finset.prod_pair key.ne, hk, hj] at this,
have hn := (X_pow_sub_one_separable_iff.mpr $ ne_zero.ne' n K).squarefree,
rw [←this, squarefree] at hn,
contrapose! hn,
refine ⟨X - C μ, ⟨(∏ x in n.divisors \ {i, n}, cyclotomic x K) * k * j, by ring⟩, _⟩,
simp [polynomial.is_unit_iff_degree_eq_zero]
end
lemma is_root_cyclotomic_iff [ne_zero (n : R)] {μ : R} :
is_root (cyclotomic n R) μ ↔ is_primitive_root μ n :=
begin
have hf : function.injective _ := is_fraction_ring.injective R (fraction_ring R),
haveI : ne_zero (n : fraction_ring R) := ne_zero.nat_of_injective hf,
rw [←is_root_map_iff hf, ←is_primitive_root.map_iff_of_injective hf, map_cyclotomic,
←is_root_cyclotomic_iff']
end
lemma roots_cyclotomic_nodup [ne_zero (n : R)] : (cyclotomic n R).roots.nodup :=
begin
obtain h | ⟨ζ, hζ⟩ := (cyclotomic n R).roots.empty_or_exists_mem,
{ exact h.symm ▸ multiset.nodup_zero },
rw [mem_roots $ cyclotomic_ne_zero n R, is_root_cyclotomic_iff] at hζ,
refine multiset.nodup_of_le (roots.le_of_dvd (X_pow_sub_C_ne_zero
(ne_zero.pos_of_ne_zero_coe R) 1) $ cyclotomic.dvd_X_pow_sub_one n R) hζ.nth_roots_nodup,
end
lemma cyclotomic.roots_to_finset_eq_primitive_roots [ne_zero (n : R)] :
(⟨(cyclotomic n R).roots, roots_cyclotomic_nodup⟩ : finset _) = primitive_roots n R :=
by { ext, simp [cyclotomic_ne_zero n R, is_root_cyclotomic_iff,
mem_primitive_roots, ne_zero.pos_of_ne_zero_coe R] }
lemma cyclotomic.roots_eq_primitive_roots_val [ne_zero (n : R)] :
(cyclotomic n R).roots = (primitive_roots n R).val :=
by rw ←cyclotomic.roots_to_finset_eq_primitive_roots
end roots
/-- If `R` is of characteristic zero, then `ζ` is a root of `cyclotomic n R` if and only if it is a
primitive `n`-th root of unity. -/
lemma is_root_cyclotomic_iff_char_zero {n : ℕ} {R : Type*} [comm_ring R] [is_domain R]
[char_zero R] {μ : R} (hn : 0 < n) :
(polynomial.cyclotomic n R).is_root μ ↔ is_primitive_root μ n :=
by { letI := ne_zero.of_gt hn, exact is_root_cyclotomic_iff }
/-- Over a ring `R` of characteristic zero, `λ n, cyclotomic n R` is injective. -/
lemma cyclotomic_injective {R : Type*} [comm_ring R] [char_zero R] :
function.injective (λ n, cyclotomic n R) :=
begin
intros n m hnm,
simp only at hnm,
rcases eq_or_ne n 0 with rfl | hzero,
{ rw [cyclotomic_zero] at hnm,
replace hnm := congr_arg nat_degree hnm,
rw [nat_degree_one, nat_degree_cyclotomic] at hnm,
by_contra,
exact (nat.totient_pos (zero_lt_iff.2 (ne.symm h))).ne hnm },
{ haveI := ne_zero.mk hzero,
rw [← map_cyclotomic_int _ R, ← map_cyclotomic_int _ R] at hnm,
replace hnm := map_injective (int.cast_ring_hom R) int.cast_injective hnm,
replace hnm := congr_arg (map (int.cast_ring_hom ℂ)) hnm,
rw [map_cyclotomic_int, map_cyclotomic_int] at hnm,
have hprim := complex.is_primitive_root_exp _ hzero,
have hroot := is_root_cyclotomic_iff.2 hprim,
rw hnm at hroot,
haveI hmzero : ne_zero m := ⟨λ h, by simpa [h] using hroot⟩,
rw is_root_cyclotomic_iff at hroot,
replace hprim := hprim.eq_order_of,
rwa [← is_primitive_root.eq_order_of hroot] at hprim}
end
lemma eq_cyclotomic_iff {R : Type*} [comm_ring R] {n : ℕ} (hpos: 0 < n)
(P : R[X]) :
P = cyclotomic n R ↔ P * (∏ i in nat.proper_divisors n, polynomial.cyclotomic i R) = X ^ n - 1 :=
begin
nontriviality R,
refine ⟨λ hcycl, _, λ hP, _⟩,
{ rw [hcycl, ← finset.prod_insert (@nat.proper_divisors.not_self_mem n),
← nat.divisors_eq_proper_divisors_insert_self_of_pos hpos],
exact prod_cyclotomic_eq_X_pow_sub_one hpos R },
{ have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic i R).monic,
{ apply monic_prod_of_monic,
intros i hi,
exact cyclotomic.monic i R },
rw [@cyclotomic_eq_X_pow_sub_one_div R _ _ hpos,
(div_mod_by_monic_unique P 0 prod_monic _).1],
refine ⟨by rwa [zero_add, mul_comm], _⟩,
rw [degree_zero, bot_lt_iff_ne_bot],
intro h,
exact monic.ne_zero prod_monic (degree_eq_bot.1 h) },
end
/-- If `p` is prime, then `cyclotomic p R = ∑ i in range p, X ^ i`. -/
lemma cyclotomic_eq_geom_sum {R : Type*} [comm_ring R] {p : ℕ}
(hp : nat.prime p) : cyclotomic p R = ∑ i in finset.range p, X ^ i :=
begin
refine ((eq_cyclotomic_iff hp.pos _).mpr _).symm,
simp only [nat.prime.proper_divisors hp, geom_sum_mul, finset.prod_singleton, cyclotomic_one],
end
lemma cyclotomic_prime_mul_X_sub_one (R : Type*) [comm_ring R] (p : ℕ) [hn : fact (nat.prime p)] :
(cyclotomic p R) * (X - 1) = X ^ p - 1 :=
by rw [cyclotomic_eq_geom_sum hn.out, geom_sum_mul]
/-- If `p ^ k` is a prime power, then
`cyclotomic (p ^ (n + 1)) R = ∑ i in range p, (X ^ (p ^ n)) ^ i`. -/
lemma cyclotomic_prime_pow_eq_geom_sum {R : Type*} [comm_ring R] {p n : ℕ} (hp : nat.prime p) :
cyclotomic (p ^ (n + 1)) R = ∑ i in finset.range p, (X ^ (p ^ n)) ^ i :=
begin
have : ∀ m, cyclotomic (p ^ (m + 1)) R = ∑ i in finset.range p, (X ^ (p ^ m)) ^ i ↔
(∑ i in finset.range p, (X ^ (p ^ m)) ^ i) * ∏ (x : ℕ) in finset.range (m + 1),
cyclotomic (p ^ x) R = X ^ p ^ (m + 1) - 1,
{ intro m,
have := eq_cyclotomic_iff (pow_pos hp.pos (m + 1)) _,
rw eq_comm at this,
rw [this, nat.prod_proper_divisors_prime_pow hp], },
induction n with n_n n_ih,
{ simp [cyclotomic_eq_geom_sum hp], },
rw ((eq_cyclotomic_iff (pow_pos hp.pos (n_n.succ + 1)) _).mpr _).symm,
rw [nat.prod_proper_divisors_prime_pow hp, finset.prod_range_succ, n_ih],
rw this at n_ih,
rw [mul_comm _ (∑ i in _, _), n_ih, geom_sum_mul, sub_left_inj, ← pow_mul, pow_add, pow_one],
end
lemma cyclotomic_prime_pow_mul_X_pow_sub_one (R : Type*) [comm_ring R] (p k : ℕ)
[hn : fact (nat.prime p)] :
(cyclotomic (p ^ (k + 1)) R) * (X ^ (p ^ k) - 1) = X ^ (p ^ (k + 1)) - 1 :=
by rw [cyclotomic_prime_pow_eq_geom_sum hn.out, geom_sum_mul, ← pow_mul, pow_succ, mul_comm]
/-- The constant term of `cyclotomic n R` is `1` if `2 ≤ n`. -/
lemma cyclotomic_coeff_zero (R : Type*) [comm_ring R] {n : ℕ} (hn : 2 ≤ n) :
(cyclotomic n R).coeff 0 = 1 :=
begin
induction n using nat.strong_induction_on with n hi,
have hprod : (∏ i in nat.proper_divisors n, (polynomial.cyclotomic i R).coeff 0) = -1,
{ rw [←finset.insert_erase (nat.one_mem_proper_divisors_iff_one_lt.2
(lt_of_lt_of_le one_lt_two hn)), finset.prod_insert (finset.not_mem_erase 1 _),
cyclotomic_one R],
have hleq : ∀ j ∈ n.proper_divisors.erase 1, 2 ≤ j,
{ intros j hj,
apply nat.succ_le_of_lt,
exact (ne.le_iff_lt ((finset.mem_erase.1 hj).1).symm).mp
(nat.succ_le_of_lt (nat.pos_of_mem_proper_divisors (finset.mem_erase.1 hj).2)) },
have hcongr : ∀ j ∈ n.proper_divisors.erase 1, (cyclotomic j R).coeff 0 = 1,
{ intros j hj,
exact hi j (nat.mem_proper_divisors.1 (finset.mem_erase.1 hj).2).2 (hleq j hj) },
have hrw : ∏ (x : ℕ) in n.proper_divisors.erase 1, (cyclotomic x R).coeff 0 = 1,
{ rw finset.prod_congr (refl (n.proper_divisors.erase 1)) hcongr,
simp only [finset.prod_const_one] },
simp only [hrw, mul_one, zero_sub, coeff_one_zero, coeff_X_zero, coeff_sub] },
have heq : (X ^ n - 1).coeff 0 = -(cyclotomic n R).coeff 0,
{ rw [←prod_cyclotomic_eq_X_pow_sub_one (lt_of_lt_of_le zero_lt_two hn),
nat.divisors_eq_proper_divisors_insert_self_of_pos (lt_of_lt_of_le zero_lt_two hn),
finset.prod_insert nat.proper_divisors.not_self_mem, mul_coeff_zero, coeff_zero_prod, hprod,
mul_neg, mul_one] },
have hzero : (X ^ n - 1).coeff 0 = (-1 : R),
{ rw coeff_zero_eq_eval_zero _,
simp only [zero_pow (lt_of_lt_of_le zero_lt_two hn), eval_X, eval_one, zero_sub, eval_pow,
eval_sub] },
rw hzero at heq,
exact neg_inj.mp (eq.symm heq)
end
/-- If `(a : ℕ)` is a root of `cyclotomic n (zmod p)`, where `p` is a prime, then `a` and `p` are
coprime. -/
lemma coprime_of_root_cyclotomic {n : ℕ} (hpos : 0 < n) {p : ℕ} [hprime : fact p.prime] {a : ℕ}
(hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) :
a.coprime p :=
begin
apply nat.coprime.symm,
rw [hprime.1.coprime_iff_not_dvd],
intro h,
replace h := (zmod.nat_coe_zmod_eq_zero_iff_dvd a p).2 h,
rw [is_root.def, eq_nat_cast, h, ← coeff_zero_eq_eval_zero] at hroot,
by_cases hone : n = 1,
{ simp only [hone, cyclotomic_one, zero_sub, coeff_one_zero, coeff_X_zero, neg_eq_zero,
one_ne_zero, coeff_sub] at hroot,
exact hroot },
rw [cyclotomic_coeff_zero (zmod p) (nat.succ_le_of_lt (lt_of_le_of_ne
(nat.succ_le_of_lt hpos) (ne.symm hone)))] at hroot,
exact one_ne_zero hroot
end
end cyclotomic
section order
/-- If `(a : ℕ)` is a root of `cyclotomic n (zmod p)`, then the multiplicative order of `a` modulo
`p` divides `n`. -/
lemma order_of_root_cyclotomic_dvd {n : ℕ} (hpos : 0 < n) {p : ℕ} [fact p.prime]
{a : ℕ} (hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) :
order_of (zmod.unit_of_coprime a (coprime_of_root_cyclotomic hpos hroot)) ∣ n :=
begin
apply order_of_dvd_of_pow_eq_one,
suffices hpow : eval (nat.cast_ring_hom (zmod p) a) (X ^ n - 1 : (zmod p)[X]) = 0,
{ simp only [eval_X, eval_one, eval_pow, eval_sub, eq_nat_cast] at hpow,
apply units.coe_eq_one.1,
simp only [sub_eq_zero.mp hpow, zmod.coe_unit_of_coprime, units.coe_pow] },
rw [is_root.def] at hroot,
rw [← prod_cyclotomic_eq_X_pow_sub_one hpos (zmod p),
nat.divisors_eq_proper_divisors_insert_self_of_pos hpos,
finset.prod_insert nat.proper_divisors.not_self_mem, eval_mul, hroot, zero_mul]
end
end order
section minpoly
open is_primitive_root complex
/-- The minimal polynomial of a primitive `n`-th root of unity `μ` divides `cyclotomic n ℤ`. -/
lemma _root_.is_primitive_root.minpoly_dvd_cyclotomic {n : ℕ} {K : Type*} [field K] {μ : K}
(h : is_primitive_root μ n) (hpos : 0 < n) [char_zero K] :
minpoly ℤ μ ∣ cyclotomic n ℤ :=
begin
apply minpoly.gcd_domain_dvd (is_integral h hpos) (cyclotomic_ne_zero n ℤ),
simpa [aeval_def, eval₂_eq_eval_map, is_root.def] using is_root_cyclotomic hpos h
end
lemma _root_.is_primitive_root.minpoly_eq_cyclotomic_of_irreducible {K : Type*} [field K]
{R : Type*} [comm_ring R] [is_domain R] {μ : R} {n : ℕ} [algebra K R] (hμ : is_primitive_root μ n)
(h : irreducible $ cyclotomic n K) [ne_zero (n : K)] : cyclotomic n K = minpoly K μ :=
begin
haveI := ne_zero.of_no_zero_smul_divisors K R n,
refine minpoly.eq_of_irreducible_of_monic h _ (cyclotomic.monic n K),
rwa [aeval_def, eval₂_eq_eval_map, map_cyclotomic, ←is_root.def, is_root_cyclotomic_iff]
end
/-- `cyclotomic n ℤ` is the minimal polynomial of a primitive `n`-th root of unity `μ`. -/
lemma cyclotomic_eq_minpoly {n : ℕ} {K : Type*} [field K] {μ : K}
(h : is_primitive_root μ n) (hpos : 0 < n) [char_zero K] :
cyclotomic n ℤ = minpoly ℤ μ :=
begin
refine eq_of_monic_of_dvd_of_nat_degree_le (minpoly.monic (is_integral h hpos))
(cyclotomic.monic n ℤ) (h.minpoly_dvd_cyclotomic hpos) _,
simpa [nat_degree_cyclotomic n ℤ] using totient_le_degree_minpoly h
end
/-- `cyclotomic n ℚ` is the minimal polynomial of a primitive `n`-th root of unity `μ`. -/
lemma cyclotomic_eq_minpoly_rat {n : ℕ} {K : Type*} [field K] {μ : K}
(h : is_primitive_root μ n) (hpos : 0 < n) [char_zero K] :
cyclotomic n ℚ = minpoly ℚ μ :=
begin
rw [← map_cyclotomic_int, cyclotomic_eq_minpoly h hpos],
exact (minpoly.gcd_domain_eq_field_fractions' _ (is_integral h hpos)).symm
end
/-- `cyclotomic n ℤ` is irreducible. -/
lemma cyclotomic.irreducible {n : ℕ} (hpos : 0 < n) : irreducible (cyclotomic n ℤ) :=
begin
rw [cyclotomic_eq_minpoly (is_primitive_root_exp n hpos.ne') hpos],
apply minpoly.irreducible,
exact (is_primitive_root_exp n hpos.ne').is_integral hpos,
end
/-- `cyclotomic n ℚ` is irreducible. -/
lemma cyclotomic.irreducible_rat {n : ℕ} (hpos : 0 < n) : irreducible (cyclotomic n ℚ) :=
begin
rw [← map_cyclotomic_int],
exact (is_primitive.int.irreducible_iff_irreducible_map_cast (cyclotomic.is_primitive n ℤ)).1
(cyclotomic.irreducible hpos),
end
/-- If `n ≠ m`, then `(cyclotomic n ℚ)` and `(cyclotomic m ℚ)` are coprime. -/
lemma cyclotomic.is_coprime_rat {n m : ℕ} (h : n ≠ m) :
is_coprime (cyclotomic n ℚ) (cyclotomic m ℚ) :=
begin
rcases n.eq_zero_or_pos with rfl | hnzero,
{ exact is_coprime_one_left },
rcases m.eq_zero_or_pos with rfl | hmzero,
{ exact is_coprime_one_right },
rw (irreducible.coprime_iff_not_dvd $ cyclotomic.irreducible_rat $ hnzero),
exact (λ hdiv, h $ cyclotomic_injective $ eq_of_monic_of_associated (cyclotomic.monic n ℚ)
(cyclotomic.monic m ℚ) $ irreducible.associated_of_dvd (cyclotomic.irreducible_rat
hnzero) (cyclotomic.irreducible_rat hmzero) hdiv),
end
end minpoly
section expand
/-- If `p` is a prime such that `¬ p ∣ n`, then
`expand R p (cyclotomic n R) = (cyclotomic (n * p) R) * (cyclotomic n R)`. -/
@[simp] lemma cyclotomic_expand_eq_cyclotomic_mul {p n : ℕ} (hp : nat.prime p) (hdiv : ¬p ∣ n)
(R : Type*) [comm_ring R] :
expand R p (cyclotomic n R) = (cyclotomic (n * p) R) * (cyclotomic n R) :=
begin
rcases nat.eq_zero_or_pos n with rfl | hnpos,
{ simp },
haveI := ne_zero.of_pos hnpos,
suffices : expand ℤ p (cyclotomic n ℤ) = (cyclotomic (n * p) ℤ) * (cyclotomic n ℤ),
{ rw [← map_cyclotomic_int, ← map_expand, this, polynomial.map_mul, map_cyclotomic_int] },
refine eq_of_monic_of_dvd_of_nat_degree_le ((cyclotomic.monic _ _).mul
(cyclotomic.monic _ _)) ((cyclotomic.monic n ℤ).expand hp.pos) _ _,
{ refine (is_primitive.int.dvd_iff_map_cast_dvd_map_cast _ _ (is_primitive.mul
(cyclotomic.is_primitive (n * p) ℤ) (cyclotomic.is_primitive n ℤ))
((cyclotomic.monic n ℤ).expand hp.pos).is_primitive).2 _,
rw [polynomial.map_mul, map_cyclotomic_int, map_cyclotomic_int, map_expand, map_cyclotomic_int],
refine is_coprime.mul_dvd (cyclotomic.is_coprime_rat (λ h, _)) _ _,
{ replace h : n * p = n * 1 := by simp [h],
exact nat.prime.ne_one hp (nat.eq_of_mul_eq_mul_left hnpos h) },
{ have hpos : 0 < n * p := mul_pos hnpos hp.pos,
have hprim := complex.is_primitive_root_exp _ hpos.ne',
rw [cyclotomic_eq_minpoly_rat hprim hpos],
refine @minpoly.dvd ℚ ℂ _ _ algebra_rat _ _ _,
rw [aeval_def, ← eval_map, map_expand, map_cyclotomic, expand_eval, ← is_root.def,
is_root_cyclotomic_iff],
convert is_primitive_root.pow_of_dvd hprim hp.ne_zero (dvd_mul_left p n),
rw [nat.mul_div_cancel _ (nat.prime.pos hp)] },
{ have hprim := complex.is_primitive_root_exp _ hnpos.ne.symm,
rw [cyclotomic_eq_minpoly_rat hprim hnpos],
refine @minpoly.dvd ℚ ℂ _ _ algebra_rat _ _ _,
rw [aeval_def, ← eval_map, map_expand, expand_eval, ← is_root.def,
← cyclotomic_eq_minpoly_rat hprim hnpos, map_cyclotomic, is_root_cyclotomic_iff],
exact is_primitive_root.pow_of_prime hprim hp hdiv,} },
{ rw [nat_degree_expand, nat_degree_cyclotomic, nat_degree_mul (cyclotomic_ne_zero _ ℤ)
(cyclotomic_ne_zero _ ℤ), nat_degree_cyclotomic, nat_degree_cyclotomic, mul_comm n,
nat.totient_mul ((nat.prime.coprime_iff_not_dvd hp).2 hdiv),
nat.totient_prime hp, mul_comm (p - 1), ← nat.mul_succ, nat.sub_one,
nat.succ_pred_eq_of_pos hp.pos] }
end
/-- If `p` is a prime such that `p ∣ n`, then
`expand R p (cyclotomic n R) = cyclotomic (p * n) R`. -/
@[simp] lemma cyclotomic_expand_eq_cyclotomic {p n : ℕ} (hp : nat.prime p) (hdiv : p ∣ n)
(R : Type*) [comm_ring R] : expand R p (cyclotomic n R) = cyclotomic (n * p) R :=
begin
rcases n.eq_zero_or_pos with rfl | hzero,
{ simp },
haveI := ne_zero.of_pos hzero,
suffices : expand ℤ p (cyclotomic n ℤ) = cyclotomic (n * p) ℤ,
{ rw [← map_cyclotomic_int, ← map_expand, this, map_cyclotomic_int] },
refine eq_of_monic_of_dvd_of_nat_degree_le (cyclotomic.monic _ _)
((cyclotomic.monic n ℤ).expand hp.pos) _ _,
{ have hpos := nat.mul_pos hzero hp.pos,
have hprim := complex.is_primitive_root_exp _ hpos.ne.symm,
rw [cyclotomic_eq_minpoly hprim hpos],
refine minpoly.gcd_domain_dvd (hprim.is_integral hpos)
((cyclotomic.monic n ℤ).expand hp.pos).ne_zero _,
rw [aeval_def, ← eval_map, map_expand, map_cyclotomic, expand_eval,
← is_root.def, is_root_cyclotomic_iff],
{ convert is_primitive_root.pow_of_dvd hprim hp.ne_zero (dvd_mul_left p n),
rw [nat.mul_div_cancel _ hp.pos] } },
{ rw [nat_degree_expand, nat_degree_cyclotomic, nat_degree_cyclotomic, mul_comm n,
nat.totient_mul_of_prime_of_dvd hp hdiv, mul_comm] }
end
/-- If the `p ^ n`th cyclotomic polynomial is irreducible, so is the `p ^ m`th, for `m ≤ n`. -/
lemma cyclotomic_irreducible_pow_of_irreducible_pow {p : ℕ} (hp : nat.prime p)
{R} [comm_ring R] [is_domain R] {n m : ℕ} (hmn : m ≤ n)
(h : irreducible (cyclotomic (p ^ n) R)) : irreducible (cyclotomic (p ^ m) R) :=
begin
unfreezingI
{ rcases m.eq_zero_or_pos with rfl | hm,
{ simpa using irreducible_X_sub_C (1 : R) },
obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le hmn,
induction k with k hk },
{ simpa using h },
have : m + k ≠ 0 := (add_pos_of_pos_of_nonneg hm k.zero_le).ne',
rw [nat.add_succ, pow_succ', ←cyclotomic_expand_eq_cyclotomic hp $ dvd_pow_self p this] at h,
exact hk (by linarith) (of_irreducible_expand hp.ne_zero h)
end
/-- If `irreducible (cyclotomic (p ^ n) R)` then `irreducible (cyclotomic p R).` -/
lemma cyclotomic_irreducible_of_irreducible_pow {p : ℕ} (hp : nat.prime p) {R} [comm_ring R]
[is_domain R] {n : ℕ} (hn : n ≠ 0) (h : irreducible (cyclotomic (p ^ n) R)) :
irreducible (cyclotomic p R) :=
pow_one p ▸ cyclotomic_irreducible_pow_of_irreducible_pow hp hn.bot_lt h
end expand
section char_p
/-- If `R` is of characteristic `p` and `¬p ∣ n`, then
`cyclotomic (n * p) R = (cyclotomic n R) ^ (p - 1)`. -/
lemma cyclotomic_mul_prime_eq_pow_of_not_dvd (R : Type*) {p n : ℕ} [hp : fact (nat.prime p)]
[ring R] [char_p R p] (hn : ¬p ∣ n) : cyclotomic (n * p) R = (cyclotomic n R) ^ (p - 1) :=
begin
suffices : cyclotomic (n * p) (zmod p) = (cyclotomic n (zmod p)) ^ (p - 1),
{ rw [← map_cyclotomic _ (algebra_map (zmod p) R), ← map_cyclotomic _ (algebra_map (zmod p) R),
this, polynomial.map_pow] },
apply mul_right_injective₀ (cyclotomic_ne_zero n $ zmod p),
rw [←pow_succ, tsub_add_cancel_of_le hp.out.one_lt.le, mul_comm, ← zmod.expand_card],
nth_rewrite 2 [← map_cyclotomic_int],
rw [← map_expand, cyclotomic_expand_eq_cyclotomic_mul hp.out hn, polynomial.map_mul,
map_cyclotomic, map_cyclotomic]
end
/-- If `R` is of characteristic `p` and `p ∣ n`, then
`cyclotomic (n * p) R = (cyclotomic n R) ^ p`. -/
lemma cyclotomic_mul_prime_dvd_eq_pow (R : Type*) {p n : ℕ} [hp : fact (nat.prime p)] [ring R]
[char_p R p] (hn : p ∣ n) : cyclotomic (n * p) R = (cyclotomic n R) ^ p :=
begin
suffices : cyclotomic (n * p) (zmod p) = (cyclotomic n (zmod p)) ^ p,
{ rw [← map_cyclotomic _ (algebra_map (zmod p) R), ← map_cyclotomic _ (algebra_map (zmod p) R),
this, polynomial.map_pow] },
rw [← zmod.expand_card, ← map_cyclotomic_int n, ← map_expand, cyclotomic_expand_eq_cyclotomic
hp.out hn, map_cyclotomic, mul_comm]
end
/-- If `R` is of characteristic `p` and `¬p ∣ m`, then
`cyclotomic (p ^ k * m) R = (cyclotomic m R) ^ (p ^ k - p ^ (k - 1))`. -/
lemma cyclotomic_mul_prime_pow_eq (R : Type*) {p m : ℕ} [fact (nat.prime p)]
[ring R] [char_p R p] (hm : ¬p ∣ m) :
∀ {k}, 0 < k → cyclotomic (p ^ k * m) R = (cyclotomic m R) ^ (p ^ k - p ^ (k - 1))
| 1 _ := by rw [pow_one, nat.sub_self, pow_zero, mul_comm,
cyclotomic_mul_prime_eq_pow_of_not_dvd R hm]
| (a + 2) _ :=
begin
have hdiv : p ∣ p ^ a.succ * m := ⟨p ^ a * m, by rw [← mul_assoc, pow_succ]⟩,
rw [pow_succ, mul_assoc, mul_comm, cyclotomic_mul_prime_dvd_eq_pow R hdiv,
cyclotomic_mul_prime_pow_eq a.succ_pos, ← pow_mul],
congr' 1,
simp only [tsub_zero, nat.succ_sub_succ_eq_sub],
rw [nat.mul_sub_right_distrib, mul_comm, pow_succ']
end
/-- If `R` is of characteristic `p` and `¬p ∣ m`, then `ζ` is a root of `cyclotomic (p ^ k * m) R`
if and only if it is a primitive `m`-th root of unity. -/
lemma is_root_cyclotomic_prime_pow_mul_iff_of_char_p {m k p : ℕ} {R : Type*} [comm_ring R]
[is_domain R] [hp : fact (nat.prime p)] [hchar : char_p R p] {μ : R} [ne_zero (m : R)] :
(polynomial.cyclotomic (p ^ k * m) R).is_root μ ↔ is_primitive_root μ m :=
begin
rcases k.eq_zero_or_pos with rfl | hk,
{ rw [pow_zero, one_mul, is_root_cyclotomic_iff] },
refine ⟨λ h, _, λ h, _⟩,
{ rw [is_root.def, cyclotomic_mul_prime_pow_eq R (ne_zero.not_char_dvd R p m) hk, eval_pow] at h,
replace h := pow_eq_zero h,
rwa [← is_root.def, is_root_cyclotomic_iff] at h },
{ rw [← is_root_cyclotomic_iff, is_root.def] at h,
rw [cyclotomic_mul_prime_pow_eq R (ne_zero.not_char_dvd R p m) hk,
is_root.def, eval_pow, h, zero_pow],
simp only [tsub_pos_iff_lt],
apply strict_mono_pow hp.out.one_lt (nat.pred_lt hk.ne') }
end
end char_p
end polynomial
|
d743527bdb336ba2aa1688392aa5ae3dfb3051ad | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/algebra/category/CommRing/basic.lean | 440ff3a019611515c4cf26102fae468c38ec0276 | [
"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 | 6,639 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johannes Hölzl, Yury Kudryashov
-/
import algebra.category.Group
import category_theory.fully_faithful
import algebra.ring
import data.int.basic
import data.equiv.ring
/-!
# Category instances for semiring, ring, comm_semiring, and comm_ring.
We introduce the bundled categories:
* `SemiRing`
* `Ring`
* `CommSemiRing`
* `CommRing`
along with the relevant forgetful functors between them.
## Implementation notes
See the note [locally reducible category instances].
-/
universes u v
open category_theory
/-- The category of semirings. -/
def SemiRing : Type (u+1) := bundled semiring
namespace SemiRing
/-- Construct a bundled SemiRing from the underlying type and typeclass. -/
def of (R : Type u) [semiring R] : SemiRing := bundled.of R
instance : inhabited SemiRing := ⟨of punit⟩
local attribute [reducible] SemiRing
instance : has_coe_to_sort SemiRing := infer_instance -- short-circuit type class inference
instance (R : SemiRing) : semiring R := R.str
instance bundled_hom : bundled_hom @ring_hom :=
⟨@ring_hom.to_fun, @ring_hom.id, @ring_hom.comp, @ring_hom.coe_inj⟩
instance : concrete_category SemiRing := infer_instance -- short-circuit type class inference
instance has_forget_to_Mon : has_forget₂ SemiRing Mon :=
bundled_hom.mk_has_forget₂ @semiring.to_monoid (λ R₁ R₂, ring_hom.to_monoid_hom) (λ _ _ _, rfl)
instance has_forget_to_AddCommMon : has_forget₂ SemiRing AddCommMon :=
-- can't use bundled_hom.mk_has_forget₂, since AddCommMon is an induced category
{ forget₂ :=
{ obj := λ R, AddCommMon.of R,
map := λ R₁ R₂ f, ring_hom.to_add_monoid_hom f } }
end SemiRing
/-- The category of rings. -/
def Ring : Type (u+1) := induced_category SemiRing (bundled.map @ring.to_semiring)
namespace Ring
/-- Construct a bundled Ring from the underlying type and typeclass. -/
def of (R : Type u) [ring R] : Ring := bundled.of R
instance : inhabited Ring := ⟨of punit⟩
local attribute [reducible] Ring
instance : has_coe_to_sort Ring := infer_instance -- short-circuit type class inference
instance (R : Ring) : ring R := R.str
instance : concrete_category Ring := infer_instance -- short-circuit type class inference
instance has_forget_to_SemiRing : has_forget₂ Ring SemiRing := infer_instance -- short-circuit type class inference
instance has_forget_to_AddCommGroup : has_forget₂ Ring AddCommGroup :=
-- can't use bundled_hom.mk_has_forget₂, since AddCommGroup is an induced category
{ forget₂ :=
{ obj := λ R, AddCommGroup.of R,
map := λ R₁ R₂ f, ring_hom.to_add_monoid_hom f } }
end Ring
/-- The category of commutative semirings. -/
def CommSemiRing : Type (u+1) := induced_category SemiRing (bundled.map comm_semiring.to_semiring)
namespace CommSemiRing
/-- Construct a bundled CommSemiRing from the underlying type and typeclass. -/
def of (R : Type u) [comm_semiring R] : CommSemiRing := bundled.of R
instance : inhabited CommSemiRing := ⟨of punit⟩
local attribute [reducible] CommSemiRing
instance : has_coe_to_sort CommSemiRing := infer_instance -- short-circuit type class inference
instance (R : CommSemiRing) : comm_semiring R := R.str
instance : concrete_category CommSemiRing := infer_instance -- short-circuit type class inference
instance has_forget_to_SemiRing : has_forget₂ CommSemiRing SemiRing := infer_instance -- short-circuit type class inference
/-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/
instance has_forget_to_CommMon : has_forget₂ CommSemiRing CommMon :=
has_forget₂.mk'
(λ R : CommSemiRing, CommMon.of R) (λ R, rfl)
(λ R₁ R₂ f, f.to_monoid_hom) (by tidy)
end CommSemiRing
/-- The category of commutative rings. -/
def CommRing : Type (u+1) := induced_category Ring (bundled.map comm_ring.to_ring)
namespace CommRing
/-- Construct a bundled CommRing from the underlying type and typeclass. -/
def of (R : Type u) [comm_ring R] : CommRing := bundled.of R
instance : inhabited CommRing := ⟨of punit⟩
local attribute [reducible] CommRing
instance : has_coe_to_sort CommRing := infer_instance -- short-circuit type class inference
instance (R : CommRing) : comm_ring R := R.str
instance : concrete_category CommRing := infer_instance -- short-circuit type class inference
instance has_forget_to_Ring : has_forget₂ CommRing Ring := infer_instance -- short-circuit type class inference
/-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/
instance has_forget_to_CommSemiRing : has_forget₂ CommRing CommSemiRing :=
has_forget₂.mk' (λ R : CommRing, CommSemiRing.of R) (λ R, rfl) (λ R₁ R₂ f, f) (by tidy)
end CommRing
namespace ring_equiv
variables {X Y : Type u}
/-- Build an isomorphism in the category `Ring` from a `ring_equiv` between `ring`s. -/
@[simps] def to_Ring_iso [ring X] [ring Y] (e : X ≃+* Y) : Ring.of X ≅ Ring.of Y :=
{ hom := e.to_ring_hom,
inv := e.symm.to_ring_hom }
/-- Build an isomorphism in the category `CommRing` from a `ring_equiv` between `comm_ring`s. -/
@[simps] def to_CommRing_iso [comm_ring X] [comm_ring Y] (e : X ≃+* Y) : CommRing.of X ≅ CommRing.of Y :=
{ hom := e.to_ring_hom,
inv := e.symm.to_ring_hom }
end ring_equiv
namespace category_theory.iso
/-- Build a `ring_equiv` from an isomorphism in the category `Ring`. -/
def Ring_iso_to_ring_equiv {X Y : Ring.{u}} (i : X ≅ Y) : X ≃+* Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
map_add' := by tidy,
map_mul' := by tidy }.
/-- Build a `ring_equiv` from an isomorphism in the category `CommRing`. -/
def CommRing_iso_to_ring_equiv {X Y : CommRing.{u}} (i : X ≅ Y) : X ≃+* Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
map_add' := by tidy,
map_mul' := by tidy }.
end category_theory.iso
/-- ring equivalences between `ring`s are the same as (isomorphic to) isomorphisms in `Ring`. -/
def ring_equiv_iso_Ring_iso {X Y : Type u} [ring X] [ring Y] :
(X ≃+* Y) ≅ (Ring.of X ≅ Ring.of Y) :=
{ hom := λ e, e.to_Ring_iso,
inv := λ i, i.Ring_iso_to_ring_equiv, }
/-- ring equivalences between `comm_ring`s are the same as (isomorphic to) isomorphisms in `CommRing`. -/
def ring_equiv_iso_CommRing_iso {X Y : Type u} [comm_ring X] [comm_ring Y] :
(X ≃+* Y) ≅ (CommRing.of X ≅ CommRing.of Y) :=
{ hom := λ e, e.to_CommRing_iso,
inv := λ i, i.CommRing_iso_to_ring_equiv, }
|
974a97dd1966e313f39222450af4a6c2bb105596 | 432d948a4d3d242fdfb44b81c9e1b1baacd58617 | /src/linear_algebra/char_poly/coeff.lean | fd317d6741f49409ec12ad607ffdb1e1ce5037e8 | [
"Apache-2.0"
] | permissive | JLimperg/aesop3 | 306cc6570c556568897ed2e508c8869667252e8a | a4a116f650cc7403428e72bd2e2c4cda300fe03f | refs/heads/master | 1,682,884,916,368 | 1,620,320,033,000 | 1,620,320,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,667 | lean | /-
Copyright (c) 2020 Aaron Anderson, Jalex Stark. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson, Jalex Stark
-/
import algebra.polynomial.big_operators
import data.matrix.char_p
import field_theory.finite.basic
import group_theory.perm.cycles
import linear_algebra.char_poly.basic
import linear_algebra.matrix
import ring_theory.polynomial.basic
import ring_theory.power_basis
/-!
# Characteristic polynomials
We give methods for computing coefficients of the characteristic polynomial.
## Main definitions
- `char_poly_degree_eq_dim` proves that the degree of the characteristic polynomial
over a nonzero ring is the dimension of the matrix
- `det_eq_sign_char_poly_coeff` proves that the determinant is the constant term of the
characteristic polynomial, up to sign.
- `trace_eq_neg_char_poly_coeff` proves that the trace is the negative of the (d-1)th coefficient of
the characteristic polynomial, where d is the dimension of the matrix.
For a nonzero ring, this is the second-highest coefficient.
-/
noncomputable theory
universes u v w z
open polynomial matrix
open_locale big_operators
variables {R : Type u} [comm_ring R]
variables {n G : Type v} [decidable_eq n] [fintype n]
variables {α β : Type v} [decidable_eq α]
open finset
open polynomial
variable {M : matrix n n R}
lemma char_matrix_apply_nat_degree [nontrivial R] (i j : n) :
(char_matrix M i j).nat_degree = ite (i = j) 1 0 :=
by { by_cases i = j; simp [h, ← degree_eq_iff_nat_degree_eq_of_pos (nat.succ_pos 0)], }
lemma char_matrix_apply_nat_degree_le (i j : n) :
(char_matrix M i j).nat_degree ≤ ite (i = j) 1 0 :=
by split_ifs; simp [h, nat_degree_X_sub_C_le]
variable (M)
lemma char_poly_sub_diagonal_degree_lt :
(char_poly M - ∏ (i : n), (X - C (M i i))).degree < ↑(fintype.card n - 1) :=
begin
rw [char_poly, det_apply', ← insert_erase (mem_univ (equiv.refl n)),
sum_insert (not_mem_erase (equiv.refl n) univ), add_comm],
simp only [char_matrix_apply_eq, one_mul, equiv.perm.sign_refl, id.def, int.cast_one,
units.coe_one, add_sub_cancel, equiv.coe_refl],
rw ← mem_degree_lt, apply submodule.sum_mem (degree_lt R (fintype.card n - 1)),
intros c hc, rw [← C_eq_int_cast, C_mul'],
apply submodule.smul_mem (degree_lt R (fintype.card n - 1)) ↑↑(equiv.perm.sign c),
rw mem_degree_lt, apply lt_of_le_of_lt degree_le_nat_degree _, rw with_bot.coe_lt_coe,
apply lt_of_le_of_lt _ (equiv.perm.fixed_point_card_lt_of_ne_one (ne_of_mem_erase hc)),
apply le_trans (polynomial.nat_degree_prod_le univ (λ i : n, (char_matrix M (c i) i))) _,
rw card_eq_sum_ones, rw sum_filter, apply sum_le_sum,
intros, apply char_matrix_apply_nat_degree_le,
end
lemma char_poly_coeff_eq_prod_coeff_of_le {k : ℕ} (h : fintype.card n - 1 ≤ k) :
(char_poly M).coeff k = (∏ i : n, (X - C (M i i))).coeff k :=
begin
apply eq_of_sub_eq_zero, rw ← coeff_sub, apply polynomial.coeff_eq_zero_of_degree_lt,
apply lt_of_lt_of_le (char_poly_sub_diagonal_degree_lt M) _, rw with_bot.coe_le_coe, apply h,
end
lemma det_of_card_zero (h : fintype.card n = 0) (M : matrix n n R) : M.det = 1 :=
by { rw fintype.card_eq_zero_iff at h, suffices : M = 1, { simp [this] }, ext, tauto }
theorem char_poly_degree_eq_dim [nontrivial R] (M : matrix n n R) :
(char_poly M).degree = fintype.card n :=
begin
by_cases fintype.card n = 0,
{ rw h, unfold char_poly, rw det_of_card_zero, {simp}, {assumption} },
rw ← sub_add_cancel (char_poly M) (∏ (i : n), (X - C (M i i))),
have h1 : (∏ (i : n), (X - C (M i i))).degree = fintype.card n,
{ rw degree_eq_iff_nat_degree_eq_of_pos, swap, apply nat.pos_of_ne_zero h,
rw nat_degree_prod', simp_rw nat_degree_X_sub_C, unfold fintype.card, simp,
simp_rw (monic_X_sub_C _).leading_coeff, simp, },
rw degree_add_eq_right_of_degree_lt, exact h1, rw h1,
apply lt_trans (char_poly_sub_diagonal_degree_lt M), rw with_bot.coe_lt_coe,
rw ← nat.pred_eq_sub_one, apply nat.pred_lt, apply h,
end
theorem char_poly_nat_degree_eq_dim [nontrivial R] (M : matrix n n R) :
(char_poly M).nat_degree = fintype.card n :=
nat_degree_eq_of_degree_eq_some (char_poly_degree_eq_dim M)
lemma char_poly_monic (M : matrix n n R) :
monic (char_poly M) :=
begin
nontriviality,
by_cases fintype.card n = 0, {rw [char_poly, det_of_card_zero h], apply monic_one},
have mon : (∏ (i : n), (X - C (M i i))).monic,
{ apply monic_prod_of_monic univ (λ i : n, (X - C (M i i))), simp [monic_X_sub_C], },
rw ← sub_add_cancel (∏ (i : n), (X - C (M i i))) (char_poly M) at mon,
rw monic at *, rw leading_coeff_add_of_degree_lt at mon, rw ← mon,
rw char_poly_degree_eq_dim, rw ← neg_sub, rw degree_neg,
apply lt_trans (char_poly_sub_diagonal_degree_lt M), rw with_bot.coe_lt_coe,
rw ← nat.pred_eq_sub_one, apply nat.pred_lt, apply h,
end
theorem trace_eq_neg_char_poly_coeff [nonempty n] (M : matrix n n R) :
(matrix.trace n R R) M = -(char_poly M).coeff (fintype.card n - 1) :=
begin
nontriviality,
rw char_poly_coeff_eq_prod_coeff_of_le, swap, refl,
rw [fintype.card, prod_X_sub_C_coeff_card_pred univ (λ i : n, M i i)], simp,
rw [← fintype.card, fintype.card_pos_iff], apply_instance,
end
-- I feel like this should use polynomial.alg_hom_eval₂_algebra_map
lemma mat_poly_equiv_eval (M : matrix n n (polynomial R)) (r : R) (i j : n) :
(mat_poly_equiv M).eval ((scalar n) r) i j = (M i j).eval r :=
begin
unfold polynomial.eval, unfold eval₂,
transitivity finsupp.sum (mat_poly_equiv M) (λ (e : ℕ) (a : matrix n n R),
(a * (scalar n) r ^ e) i j),
{ unfold finsupp.sum, rw sum_apply, rw sum_apply, dsimp, refl, },
{ simp_rw ← (scalar n).map_pow, simp_rw ← (matrix.scalar.commute _ _).eq,
simp only [coe_scalar, matrix.one_mul, ring_hom.id_apply,
smul_apply, mul_eq_mul, algebra.smul_mul_assoc],
have h : ∀ x : ℕ, (λ (e : ℕ) (a : R), r ^ e * a) x 0 = 0 := by simp,
symmetry, rw ← finsupp.sum_map_range_index h, swap, refl,
refine congr (congr rfl _) (by {ext, rw mul_comm}), ext, rw finsupp.map_range_apply,
simpa [coeff] using (mat_poly_equiv_coeff_apply M a i j).symm }
end
lemma eval_det (M : matrix n n (polynomial R)) (r : R) :
polynomial.eval r M.det = (polynomial.eval (matrix.scalar n r) (mat_poly_equiv M)).det :=
begin
rw [polynomial.eval, ← coe_eval₂_ring_hom, ring_hom.map_det],
apply congr_arg det, ext, symmetry, convert mat_poly_equiv_eval _ _ _ _,
end
theorem det_eq_sign_char_poly_coeff (M : matrix n n R) :
M.det = (-1)^(fintype.card n) * (char_poly M).coeff 0:=
begin
rw [coeff_zero_eq_eval_zero, char_poly, eval_det, mat_poly_equiv_char_matrix, ← det_smul],
simp
end
variables {p : ℕ} [fact p.prime]
lemma mat_poly_equiv_eq_X_pow_sub_C {K : Type*} (k : ℕ) [field K] (M : matrix n n K) :
mat_poly_equiv
((expand K (k) : polynomial K →+* polynomial K).map_matrix (char_matrix (M ^ k))) =
X ^ k - C (M ^ k) :=
begin
ext m,
rw [coeff_sub, coeff_C, mat_poly_equiv_coeff_apply, ring_hom.map_matrix_apply, matrix.map_apply,
alg_hom.coe_to_ring_hom, dmatrix.sub_apply, coeff_X_pow],
by_cases hij : i = j,
{ rw [hij, char_matrix_apply_eq, alg_hom.map_sub, expand_C, expand_X, coeff_sub, coeff_X_pow,
coeff_C],
split_ifs with mp m0;
simp only [matrix.one_apply_eq, dmatrix.zero_apply] },
{ rw [char_matrix_apply_ne _ _ _ hij, alg_hom.map_neg, expand_C, coeff_neg, coeff_C],
split_ifs with m0 mp;
simp only [hij, zero_sub, dmatrix.zero_apply, sub_zero, neg_zero, matrix.one_apply_ne, ne.def,
not_false_iff] }
end
@[simp] lemma finite_field.char_poly_pow_card {K : Type*} [field K] [fintype K] (M : matrix n n K) :
char_poly (M ^ (fintype.card K)) = char_poly M :=
begin
by_cases hn : nonempty n,
{ haveI := hn,
cases char_p.exists K with p hp, letI := hp,
rcases finite_field.card K p with ⟨⟨k, kpos⟩, ⟨hp, hk⟩⟩,
haveI : fact p.prime := ⟨hp⟩,
dsimp at hk, rw hk at *,
apply (frobenius_inj (polynomial K) p).iterate k,
repeat { rw iterate_frobenius, rw ← hk },
rw ← finite_field.expand_card,
unfold char_poly, rw [alg_hom.map_det, ← is_monoid_hom.map_pow],
apply congr_arg det,
apply mat_poly_equiv.injective, swap, { apply_instance },
rw [← mat_poly_equiv.coe_alg_hom, alg_hom.map_pow, mat_poly_equiv.coe_alg_hom,
mat_poly_equiv_char_matrix, hk, sub_pow_char_pow_of_commute, ← C_pow],
{ exact (id (mat_poly_equiv_eq_X_pow_sub_C (p ^ k) M) : _) },
{ exact (C M).commute_X } },
{ apply congr_arg, apply @subsingleton.elim _ (subsingleton_of_empty_left hn) _ _, },
end
@[simp] lemma zmod.char_poly_pow_card (M : matrix n n (zmod p)) :
char_poly (M ^ p) = char_poly M :=
by { have h := finite_field.char_poly_pow_card M, rwa zmod.card at h, }
lemma finite_field.trace_pow_card {K : Type*} [field K] [fintype K] [nonempty n]
(M : matrix n n K) : trace n K K (M ^ (fintype.card K)) = (trace n K K M) ^ (fintype.card K) :=
by rw [trace_eq_neg_char_poly_coeff, trace_eq_neg_char_poly_coeff,
finite_field.char_poly_pow_card, finite_field.pow_card]
lemma zmod.trace_pow_card {p:ℕ} [fact p.prime] [nonempty n] (M : matrix n n (zmod p)) :
trace n (zmod p) (zmod p) (M ^ p) = (trace n (zmod p) (zmod p) M)^p :=
by { have h := finite_field.trace_pow_card M, rwa zmod.card at h, }
namespace matrix
theorem is_integral : is_integral R M := ⟨char_poly M, ⟨char_poly_monic M, aeval_self_char_poly M⟩⟩
theorem min_poly_dvd_char_poly {K : Type*} [field K] (M : matrix n n K) :
(minpoly K M) ∣ char_poly M :=
minpoly.dvd _ _ (aeval_self_char_poly M)
end matrix
section power_basis
open algebra
/-- The characteristic polynomial of the map `λ x, a * x` is the minimal polynomial of `a`.
In combination with `det_eq_sign_char_poly_coeff` or `trace_eq_neg_char_poly_coeff`
and a bit of rewriting, this will allow us to conclude the
field norm resp. trace of `x` is the product resp. sum of `x`'s conjugates.
-/
lemma char_poly_left_mul_matrix {K S : Type*} [field K] [comm_ring S] [algebra K S]
(h : power_basis K S) :
char_poly (left_mul_matrix h.is_basis h.gen) = minpoly K h.gen :=
begin
apply minpoly.unique,
{ apply char_poly_monic },
{ apply (left_mul_matrix _).injective_iff.mp (left_mul_matrix_injective h.is_basis),
rw [← polynomial.aeval_alg_hom_apply, aeval_self_char_poly] },
{ intros q q_monic root_q,
rw [char_poly_degree_eq_dim, fintype.card_fin, degree_eq_nat_degree q_monic.ne_zero],
apply with_bot.some_le_some.mpr,
exact h.dim_le_nat_degree_of_root q_monic.ne_zero root_q }
end
end power_basis
|
87ac7ebabe76f811f718b7e89e6a346589201ac7 | aa2345b30d710f7e75f13157a35845ee6d48c017 | /category_theory/embedding.lean | 9e69a58c167c0787baa7858344905c0ae3732a14 | [
"Apache-2.0"
] | permissive | CohenCyril/mathlib | 5241b20a3fd0ac0133e48e618a5fb7761ca7dcbe | a12d5a192f5923016752f638d19fc1a51610f163 | refs/heads/master | 1,586,031,957,957 | 1,541,432,824,000 | 1,541,432,824,000 | 156,246,337 | 0 | 0 | Apache-2.0 | 1,541,434,514,000 | 1,541,434,513,000 | null | UTF-8 | Lean | false | false | 2,664 | lean | -- Copyright (c) 2018 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison
import category_theory.isomorphism
universes u₁ v₁ u₂ v₂ u₃ v₃
namespace category_theory
variables {C : Type u₁} [𝒞 : category.{u₁ v₁} C] {D : Type u₂} [𝒟 : category.{u₂ v₂} D]
include 𝒞 𝒟
class full (F : C ⥤ D) :=
(preimage : ∀ {X Y : C} (f : (F X) ⟶ (F Y)), X ⟶ Y)
(witness' : ∀ {X Y : C} (f : (F X) ⟶ (F Y)), F.map (preimage f) = f . obviously)
restate_axiom full.witness'
attribute [simp] full.witness
class faithful (F : C ⥤ D) : Prop :=
(injectivity' : ∀ {X Y : C} {f g : X ⟶ Y} (p : F.map f = F.map g), f = g . obviously)
restate_axiom faithful.injectivity'
namespace functor
def injectivity (F : C ⥤ D) [faithful F] {X Y : C} {f g : X ⟶ Y} (p : F.map f = F.map g) : f = g :=
faithful.injectivity F p
def preimage (F : C ⥤ D) [full F] {X Y : C} (f : F X ⟶ F Y) : X ⟶ Y := full.preimage.{u₁ v₁ u₂ v₂} f
@[simp] lemma image_preimage (F : C ⥤ D) [full F] {X Y : C} (f : F X ⟶ F Y) : F.map (preimage F f) = f := begin unfold preimage, obviously end
end functor
section
variables {F : C ⥤ D} [full F] [faithful F] {X Y : C}
def preimage_iso (f : (F X) ≅ (F Y)) : X ≅ Y :=
{ hom := F.preimage (f : F X ⟶ F Y),
inv := F.preimage (f.symm : F Y ⟶ F X),
hom_inv_id' := begin apply @faithful.injectivity _ _ _ _ F, obviously, end,
inv_hom_id' := begin apply @faithful.injectivity _ _ _ _ F, obviously, end, }
@[simp] lemma preimage_iso_coe (f : (F X) ≅ (F Y)) : ((preimage_iso f) : X ⟶ Y) = F.preimage (f : F X ⟶ F Y) := rfl
@[simp] lemma preimage_iso_symm_coe (f : (F X) ≅ (F Y)) : ((preimage_iso f).symm : Y ⟶ X) = F.preimage (f.symm : F Y ⟶ F X) := rfl
end
class embedding (F : C ⥤ D) extends (full F), (faithful F).
end category_theory
namespace category_theory
variables {C : Type u₁} [𝒞 : category.{u₁ v₁} C]
include 𝒞
instance full.id : full (functor.id C) :=
{ preimage := λ _ _ f, f }
instance : faithful (functor.id C) := by obviously
instance : embedding (functor.id C) := { ((by apply_instance) : full (functor.id C)) with }
variables {D : Type u₂} [𝒟 : category.{u₂ v₂} D] {E : Type u₃} [ℰ : category.{u₃ v₃} E]
include 𝒟 ℰ
variables (F : C ⥤ D) (G : D ⥤ E)
instance faithful.comp [faithful F] [faithful G] : faithful (F ⋙ G) :=
{ injectivity' := λ _ _ _ _ p, F.injectivity (G.injectivity p) }
instance full.comp [full F] [full G] : full (F ⋙ G) :=
{ preimage := λ _ _ f, F.preimage (G.preimage f) }
end category_theory
|
81eadadb28d41cb4a650798bfb0a325f2e576d3f | 6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf | /src/game/world8/level9.lean | 0714b3eacbdf3d0b495858c3a0e4341e0410df88 | [
"Apache-2.0"
] | permissive | arolihas/natural_number_game | 4f0c93feefec93b8824b2b96adff8b702b8b43ce | 8e4f7b4b42888a3b77429f90cce16292bd288138 | refs/heads/master | 1,621,872,426,808 | 1,586,270,467,000 | 1,586,270,467,000 | 253,648,466 | 0 | 0 | null | 1,586,219,694,000 | 1,586,219,694,000 | null | UTF-8 | Lean | false | false | 1,608 | lean | import mynat.definition -- hide
import mynat.add -- hide
import game.world8.level8 -- hide
namespace mynat -- hide
/- Axiom : zero_ne_succ (a : mynat) :
0 ≠ succ(a)
-/
/- Tactic : symmetry
## Summary
`symmetry` turns goals of the form `⊢ A = B` to `⊢ B = A`.
Also works with `≠`. Also works on hypotheses: if `h : a ≠ b`
then `symmetry at h` gives `h : b ≠ a`.
## Details
`symmetry` works on both goals and hypotheses. By default it
works on the goal. It will turn a goal of the form `⊢ A = B`
to `⊢ B = A`. More generally it will work with any symmetric
binary relation (for example `≠`, or more generally any
binary relation whose proof of symmetry has been tagged
with the `symm` attribute).
To get `symmetry` working on a hypothesis, use `symmetry at h`.
## Examples
If the tactic state is
```
h : a = b
⊢ c ≠ d
```
then `symmetry` changes the goal to `⊢ d ≠ c` and
`symmetry at h` changes `h` to `h : b = a`.
-/
/-
# Advanced Addition World
## Level 9: `succ_ne_zero`
Levels 9 to 13 introduce the last axiom of Peano, namely
that $0\not=\operatorname{succ}(a)$. The proof of this is called `zero_ne_succ a`.
`zero_ne_succ (a : mynat) : 0 ≠ succ(a)`
The `symmetry` tactic will turn any goal of the form `R x y` into `R y x`,
if `R` is a symmetric binary relation (for example `=` or `≠`).
In particular, you can prove `succ_ne_zero` below by first using
`symmetry` and then `exact zero_ne_succ a`.
-/
/- Theorem
Zero is not the successor of any natural number.
-/
theorem succ_ne_zero (a : mynat) : succ a ≠ 0 :=
begin [nat_num_game]
end
end mynat
|
439112bb37573d1f5eda335e94c717debf9129ac | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/measure_theory/measure_space.lean | 2942e61b7a979c2151fb0f09a3f1db0b506d0838 | [
"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 | 84,644 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import measure_theory.outer_measure
import order.filter.countable_Inter
import data.set.accumulate
/-!
# Measure spaces
Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the
extended nonnegative reals that satisfies the following conditions:
1. `μ ∅ = 0`;
2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint
sets is equal to the measure of the individual sets.
Every measure can be canonically extended to an outer measure, so that it assigns values to
all subsets, not just the measurable subsets. On the other hand, a measure that is countably
additive on measurable sets can be restricted to measurable sets to obtain a measure.
In this file a measure is defined to be an outer measure that is countably additive on
measurable sets, with the additional assumption that the outer measure is the canonical
extension of the restricted measure.
Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ennreal`.
We introduce the following typeclasses for measures:
* `probability_measure μ`: `μ univ = 1`;
* `finite_measure μ`: `μ univ < ⊤`;
* `sigma_finite μ`: there exists a countable collection of measurable sets that cover `univ`
where `μ` is finite;
* `locally_finite_measure μ` : `∀ x, ∃ s ∈ 𝓝 x, μ s < ⊤`;
* `has_no_atoms μ` : `∀ x, μ {x} = 0`; possibly should be redefined as
`∀ s, 0 < μ s → ∃ t ⊆ s, 0 < μ t ∧ μ t < μ s`.
Given a measure, the null sets are the sets where `μ s = 0`, where `μ` denotes the corresponding
outer measure (so `s` might not be measurable). We can then define the completion of `μ` as the
measure on the least `σ`-algebra that also contains all null sets, by defining the measure to be `0`
on the null sets.
## Main statements
* `completion` is the completion of a measure to all null measurable sets.
* `measure.of_measurable` and `outer_measure.to_measure` are two important ways to define a measure.
## Implementation notes
Given `μ : measure α`, `μ s` is the value of the *outer measure* applied to `s`.
This conveniently allows us to apply the measure to sets without proving that they are measurable.
We get countable subadditivity for all sets, but only countable additivity for measurable sets.
You often don't want to define a measure via its constructor.
Two ways that are sometimes more convenient:
* `measure.of_measurable` is a way to define a measure by only giving its value on measurable sets
and proving the properties (1) and (2) mentioned above.
* `outer_measure.to_measure` is a way of obtaining a measure from an outer measure by showing that
all measurable sets in the measurable space are Carathéodory measurable.
To prove that two measures are equal, there are multiple options:
* `ext`: two measures are equal if they are equal on all measurable sets.
* `ext_of_generate_from_of_Union`: two measures are equal if they are equal on a π-system generating
the measurable sets, if the π-system contains a spanning increasing sequence of sets where the
measures take finite value (in particular the measures are σ-finite). This is a special case of the
more general `ext_of_generate_from_of_cover`
* `ext_of_generate_finite`: two finite measures are equal if they are equal on a π-system
generating the measurable sets. This is a special case of `ext_of_generate_from_of_Union` using
`C ∪ {univ}`, but is easier to work with.
A `measure_space` is a class that is a measurable space with a canonical measure.
The measure is denoted `volume`.
## References
* <https://en.wikipedia.org/wiki/Measure_(mathematics)>
* <https://en.wikipedia.org/wiki/Complete_measure>
* <https://en.wikipedia.org/wiki/Almost_everywhere>
## Tags
measure, almost everywhere, measure space, completion, null set, null measurable set
-/
noncomputable theory
open classical set filter function measurable_space
open_locale classical topological_space big_operators filter
namespace measure_theory
/-- A measure is defined to be an outer measure that is countably additive on
measurable sets, with the additional assumption that the outer measure is the canonical
extension of the restricted measure. -/
structure measure (α : Type*) [measurable_space α] extends outer_measure α :=
(m_Union ⦃f : ℕ → set α⦄ :
(∀i, is_measurable (f i)) → pairwise (disjoint on f) →
measure_of (⋃i, f i) = (∑'i, measure_of (f i)))
(trimmed : to_outer_measure.trim = to_outer_measure)
/-- Measure projections for a measure space.
For measurable sets this returns the measure assigned by the `measure_of` field in `measure`.
But we can extend this to _all_ sets, but using the outer measure. This gives us monotonicity and
subadditivity for all sets.
-/
instance measure.has_coe_to_fun {α} [measurable_space α] : has_coe_to_fun (measure α) :=
⟨λ _, set α → ennreal, λ m, m.to_outer_measure⟩
namespace measure
/-! ### General facts about measures -/
/-- Obtain a measure by giving a countably additive function that sends `∅` to `0`. -/
def of_measurable {α} [measurable_space α]
(m : Π (s : set α), is_measurable s → ennreal)
(m0 : m ∅ is_measurable.empty = 0)
(mU : ∀ {{f : ℕ → set α}} (h : ∀i, is_measurable (f i)),
pairwise (disjoint on f) →
m (⋃i, f i) (is_measurable.Union h) = (∑'i, m (f i) (h i))) :
measure α :=
{ m_Union := λ f hf hd,
show induced_outer_measure m _ m0 (Union f) =
∑' i, induced_outer_measure m _ m0 (f i), begin
rw [induced_outer_measure_eq m0 mU, mU hf hd],
congr, funext n, rw induced_outer_measure_eq m0 mU
end,
trimmed :=
show (induced_outer_measure m _ m0).trim = induced_outer_measure m _ m0, begin
unfold outer_measure.trim,
congr, funext s hs,
exact induced_outer_measure_eq m0 mU hs
end,
..induced_outer_measure m _ m0 }
lemma of_measurable_apply {α} [measurable_space α]
{m : Π (s : set α), is_measurable s → ennreal}
{m0 : m ∅ is_measurable.empty = 0}
{mU : ∀ {{f : ℕ → set α}} (h : ∀i, is_measurable (f i)),
pairwise (disjoint on f) →
m (⋃i, f i) (is_measurable.Union h) = (∑'i, m (f i) (h i))}
(s : set α) (hs : is_measurable s) :
of_measurable m m0 mU s = m s hs :=
induced_outer_measure_eq m0 mU hs
lemma to_outer_measure_injective {α} [measurable_space α] :
injective (to_outer_measure : measure α → outer_measure α) :=
λ ⟨m₁, u₁, h₁⟩ ⟨m₂, u₂, h₂⟩ h, by { congr, exact h }
@[ext] lemma ext {α} [measurable_space α] {μ₁ μ₂ : measure α}
(h : ∀s, is_measurable s → μ₁ s = μ₂ s) :
μ₁ = μ₂ :=
to_outer_measure_injective $ by rw [← trimmed, outer_measure.trim_congr h, trimmed]
lemma ext_iff {α} [measurable_space α] {μ₁ μ₂ : measure α} :
μ₁ = μ₂ ↔ ∀s, is_measurable s → μ₁ s = μ₂ s :=
⟨by { rintro rfl s hs, refl }, measure.ext⟩
end measure
section
variables {α : Type*} {β : Type*} {ι : Type*} [measurable_space α] {μ μ₁ μ₂ : measure α}
{s s₁ s₂ : set α}
@[simp] lemma coe_to_outer_measure : ⇑μ.to_outer_measure = μ := rfl
lemma to_outer_measure_apply (s) : μ.to_outer_measure s = μ s := rfl
lemma measure_eq_trim (s) : μ s = μ.to_outer_measure.trim s :=
by rw μ.trimmed; refl
lemma measure_eq_infi (s) : μ s = ⨅ t (st : s ⊆ t) (ht : is_measurable t), μ t :=
by rw [measure_eq_trim, outer_measure.trim_eq_infi]; refl
lemma measure_eq_induced_outer_measure :
μ s = induced_outer_measure (λ s _, μ s) is_measurable.empty μ.empty s :=
measure_eq_trim _
lemma to_outer_measure_eq_induced_outer_measure :
μ.to_outer_measure = induced_outer_measure (λ s _, μ s) is_measurable.empty μ.empty :=
μ.trimmed.symm
lemma measure_eq_extend (hs : is_measurable s) :
μ s = extend (λ t (ht : is_measurable t), μ t) s :=
by { rw [measure_eq_induced_outer_measure, induced_outer_measure_eq_extend _ _ hs],
exact μ.m_Union }
@[simp] lemma measure_empty : μ ∅ = 0 := μ.empty
lemma nonempty_of_measure_ne_zero (h : μ s ≠ 0) : s.nonempty :=
ne_empty_iff_nonempty.1 $ λ h', h $ h'.symm ▸ measure_empty
lemma measure_mono (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := μ.mono h
lemma measure_mono_null (h : s₁ ⊆ s₂) (h₂ : μ s₂ = 0) : μ s₁ = 0 :=
le_zero_iff_eq.1 $ h₂ ▸ measure_mono h
lemma measure_mono_top (h : s₁ ⊆ s₂) (h₁ : μ s₁ = ⊤) : μ s₂ = ⊤ :=
top_unique $ h₁ ▸ measure_mono h
lemma exists_is_measurable_superset_of_measure_eq_zero {s : set α} (h : μ s = 0) :
∃t, s ⊆ t ∧ is_measurable t ∧ μ t = 0 :=
outer_measure.exists_is_measurable_superset_of_trim_eq_zero (by rw [← measure_eq_trim, h])
lemma exists_is_measurable_superset_iff_measure_eq_zero {s : set α} :
(∃ t, s ⊆ t ∧ is_measurable t ∧ μ t = 0) ↔ μ s = 0 :=
⟨λ ⟨t, hst, _, ht⟩, measure_mono_null hst ht, exists_is_measurable_superset_of_measure_eq_zero⟩
theorem measure_Union_le {β} [encodable β] (s : β → set α) : μ (⋃i, s i) ≤ (∑'i, μ (s i)) :=
μ.to_outer_measure.Union _
lemma measure_bUnion_le {s : set β} (hs : countable s) (f : β → set α) :
μ (⋃b∈s, f b) ≤ ∑'p:s, μ (f p) :=
begin
haveI := hs.to_encodable,
rw [bUnion_eq_Union],
apply measure_Union_le
end
lemma measure_bUnion_finset_le (s : finset β) (f : β → set α) :
μ (⋃b∈s, f b) ≤ ∑ p in s, μ (f p) :=
begin
rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype],
exact measure_bUnion_le s.countable_to_set f
end
lemma measure_bUnion_lt_top {s : set β} {f : β → set α} (hs : finite s)
(hfin : ∀ i ∈ s, μ (f i) < ⊤) : μ (⋃ i ∈ s, f i) < ⊤ :=
begin
convert (measure_bUnion_finset_le hs.to_finset f).trans_lt _,
{ ext, rw [finite.mem_to_finset] },
apply ennreal.sum_lt_top, simpa only [finite.mem_to_finset]
end
lemma measure_Union_null {β} [encodable β] {s : β → set α} :
(∀ i, μ (s i) = 0) → μ (⋃i, s i) = 0 :=
μ.to_outer_measure.Union_null
lemma measure_Union_null_iff {ι} [encodable ι] {s : ι → set α} :
μ (⋃ i, s i) = 0 ↔ ∀ i, μ (s i) = 0 :=
⟨λ h i, measure_mono_null (subset_Union _ _) h, measure_Union_null⟩
theorem measure_union_le (s₁ s₂ : set α) : μ (s₁ ∪ s₂) ≤ μ s₁ + μ s₂ :=
μ.to_outer_measure.union _ _
lemma measure_union_null {s₁ s₂ : set α} : μ s₁ = 0 → μ s₂ = 0 → μ (s₁ ∪ s₂) = 0 :=
μ.to_outer_measure.union_null
lemma measure_union_null_iff {s₁ s₂ : set α} : μ (s₁ ∪ s₂) = 0 ↔ μ s₁ = 0 ∧ μ s₂ = 0:=
⟨λ h, ⟨measure_mono_null (subset_union_left _ _) h, measure_mono_null (subset_union_right _ _) h⟩,
λ h, measure_union_null h.1 h.2⟩
lemma measure_Union {β} [encodable β] {f : β → set α}
(hn : pairwise (disjoint on f)) (h : ∀i, is_measurable (f i)) :
μ (⋃i, f i) = (∑'i, μ (f i)) :=
begin
rw [measure_eq_extend (is_measurable.Union h),
extend_Union is_measurable.empty _ is_measurable.Union _ hn h],
{ simp [measure_eq_extend, h] },
{ exact μ.empty },
{ exact μ.m_Union }
end
lemma measure_union (hd : disjoint s₁ s₂) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) :
μ (s₁ ∪ s₂) = μ s₁ + μ s₂ :=
begin
rw [union_eq_Union, measure_Union, tsum_fintype, fintype.sum_bool, cond, cond],
exacts [pairwise_disjoint_on_bool.2 hd, λ b, bool.cases_on b h₂ h₁]
end
lemma measure_bUnion {s : set β} {f : β → set α} (hs : countable s)
(hd : pairwise_on s (disjoint on f)) (h : ∀b∈s, is_measurable (f b)) :
μ (⋃b∈s, f b) = ∑'p:s, μ (f p) :=
begin
haveI := hs.to_encodable,
rw bUnion_eq_Union,
exact measure_Union (hd.on_injective subtype.coe_injective $ λ x, x.2) (λ x, h x x.2)
end
lemma measure_sUnion {S : set (set α)} (hs : countable S)
(hd : pairwise_on S disjoint) (h : ∀s∈S, is_measurable s) :
μ (⋃₀ S) = ∑' s:S, μ s :=
by rw [sUnion_eq_bUnion, measure_bUnion hs hd h]
lemma measure_bUnion_finset {s : finset ι} {f : ι → set α} (hd : pairwise_on ↑s (disjoint on f))
(hm : ∀b∈s, is_measurable (f b)) :
μ (⋃b∈s, f b) = ∑ p in s, μ (f p) :=
begin
rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype],
exact measure_bUnion s.countable_to_set hd hm
end
/-- If `s` is a countable set, then the measure of its preimage can be found as the sum of measures
of the fibers `f ⁻¹' {y}`. -/
lemma tsum_measure_preimage_singleton {s : set β} (hs : countable s) {f : α → β}
(hf : ∀ y ∈ s, is_measurable (f ⁻¹' {y})) :
(∑' b : s, μ (f ⁻¹' {↑b})) = μ (f ⁻¹' s) :=
by rw [← set.bUnion_preimage_singleton, measure_bUnion hs (pairwise_on_disjoint_fiber _ _) hf]
/-- If `s` is a `finset`, then the measure of its preimage can be found as the sum of measures
of the fibers `f ⁻¹' {y}`. -/
lemma sum_measure_preimage_singleton (s : finset β) {f : α → β}
(hf : ∀ y ∈ s, is_measurable (f ⁻¹' {y})) :
∑ b in s, μ (f ⁻¹' {b}) = μ (f ⁻¹' ↑s) :=
by simp only [← measure_bUnion_finset (pairwise_on_disjoint_fiber _ _) hf,
finset.bUnion_preimage_singleton]
lemma measure_diff {s₁ s₂ : set α} (h : s₂ ⊆ s₁) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂)
(h_fin : μ s₂ < ⊤) :
μ (s₁ \ s₂) = μ s₁ - μ s₂ :=
begin
refine (ennreal.add_sub_self' h_fin).symm.trans _,
rw [← measure_union disjoint_diff h₂ (h₁.diff h₂), union_diff_cancel h]
end
lemma measure_compl {μ : measure α} {s : set α} (h₁ : is_measurable s) (h_fin : μ s < ⊤) :
μ (sᶜ) = μ univ - μ s :=
by { rw compl_eq_univ_diff, exact measure_diff (subset_univ s) is_measurable.univ h₁ h_fin }
lemma sum_measure_le_measure_univ {s : finset ι} {t : ι → set α} (h : ∀ i ∈ s, is_measurable (t i))
(H : pairwise_on ↑s (disjoint on t)) :
∑ i in s, μ (t i) ≤ μ (univ : set α) :=
by { rw ← measure_bUnion_finset H h, exact measure_mono (subset_univ _) }
lemma tsum_measure_le_measure_univ {s : ι → set α} (hs : ∀ i, is_measurable (s i))
(H : pairwise (disjoint on s)) :
(∑' i, μ (s i)) ≤ μ (univ : set α) :=
begin
rw [ennreal.tsum_eq_supr_sum],
exact supr_le (λ s, sum_measure_le_measure_univ (λ i hi, hs i) (λ i hi j hj hij, H i j hij))
end
/-- Pigeonhole principle for measure spaces: if `∑' i, μ (s i) > μ univ`, then
one of the intersections `s i ∩ s j` is not empty. -/
lemma exists_nonempty_inter_of_measure_univ_lt_tsum_measure (μ : measure α) {s : ι → set α}
(hs : ∀ i, is_measurable (s i)) (H : μ (univ : set α) < ∑' i, μ (s i)) :
∃ i j (h : i ≠ j), (s i ∩ s j).nonempty :=
begin
contrapose! H,
apply tsum_measure_le_measure_univ hs,
exact λ i j hij x hx, H i j hij ⟨x, hx⟩
end
/-- Pigeonhole principle for measure spaces: if `s` is a `finset` and
`∑ i in s, μ (t i) > μ univ`, then one of the intersections `t i ∩ t j` is not empty. -/
lemma exists_nonempty_inter_of_measure_univ_lt_sum_measure (μ : measure α) {s : finset ι}
{t : ι → set α} (h : ∀ i ∈ s, is_measurable (t i)) (H : μ (univ : set α) < ∑ i in s, μ (t i)) :
∃ (i ∈ s) (j ∈ s) (h : i ≠ j), (t i ∩ t j).nonempty :=
begin
contrapose! H,
apply sum_measure_le_measure_univ h,
exact λ i hi j hj hij x hx, H i hi j hj hij ⟨x, hx⟩
end
/-- Continuity from below: the measure of the union of a directed sequence of measurable sets
is the supremum of the measures. -/
lemma measure_Union_eq_supr [encodable ι] {s : ι → set α} (h : ∀ i, is_measurable (s i))
(hd : directed (⊆) s) : μ (⋃ i, s i) = ⨆ i, μ (s i) :=
begin
by_cases hι : nonempty ι, swap,
{ simp only [supr_of_empty hι, Union], exact measure_empty },
resetI,
refine le_antisymm _ (supr_le $ λ i, measure_mono $ subset_Union _ _),
have : ∀ n, is_measurable (disjointed (λ n, ⋃ b ∈ encodable.decode2 ι n, s b) n) :=
is_measurable.disjointed (is_measurable.bUnion_decode2 h),
rw [← encodable.Union_decode2, ← Union_disjointed, measure_Union disjoint_disjointed this,
ennreal.tsum_eq_supr_nat],
simp only [← measure_bUnion_finset (disjoint_disjointed.pairwise_on _) (λ n _, this n)],
refine supr_le (λ n, _),
refine le_trans (_ : _ ≤ μ (⋃ (k ∈ finset.range n) (i ∈ encodable.decode2 ι k), s i)) _,
exact measure_mono (bUnion_subset_bUnion_right (λ k hk, disjointed_subset)),
simp only [← finset.bUnion_option_to_finset, ← finset.bUnion_bind],
generalize : (finset.range n).bind (λ k, (encodable.decode2 ι k).to_finset) = t,
rcases hd.finset_le t with ⟨i, hi⟩,
exact le_supr_of_le i (measure_mono $ bUnion_subset hi)
end
lemma measure_bUnion_eq_supr {s : ι → set α} {t : set ι} (ht : countable t)
(h : ∀ i ∈ t, is_measurable (s i)) (hd : directed_on ((⊆) on s) t) :
μ (⋃ i ∈ t, s i) = ⨆ i ∈ t, μ (s i) :=
begin
haveI := ht.to_encodable,
rw [bUnion_eq_Union, measure_Union_eq_supr (set_coe.forall'.1 h) hd.directed_coe,
supr_subtype'],
refl
end
/-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable
sets is the infimum of the measures. -/
lemma measure_Inter_eq_infi [encodable ι] {s : ι → set α}
(h : ∀i, is_measurable (s i)) (hd : directed (⊇) s)
(hfin : ∃i, μ (s i) < ⊤) :
μ (⋂ i, s i) = (⨅ i, μ (s i)) :=
begin
rcases hfin with ⟨k, hk⟩,
rw [← ennreal.sub_sub_cancel (by exact hk) (infi_le _ k), ennreal.sub_infi,
← ennreal.sub_sub_cancel (by exact hk) (measure_mono (Inter_subset _ k)),
← measure_diff (Inter_subset _ k) (h k) (is_measurable.Inter h)
(lt_of_le_of_lt (measure_mono (Inter_subset _ k)) hk),
diff_Inter, measure_Union_eq_supr],
{ congr' 1,
refine le_antisymm (supr_le_supr2 $ λ i, _) (supr_le_supr $ λ i, _),
{ rcases hd i k with ⟨j, hji, hjk⟩,
use j,
rw [← measure_diff hjk (h _) (h _) ((measure_mono hjk).trans_lt hk)],
exact measure_mono (diff_subset_diff_right hji) },
{ rw [ennreal.sub_le_iff_le_add, ← measure_union disjoint_diff.symm ((h k).diff (h i)) (h i),
set.union_comm],
exact measure_mono (diff_subset_iff.1 $ subset.refl _) } },
{ exact λ i, (h k).diff (h i) },
{ exact hd.mono_comp _ (λ _ _, diff_subset_diff_right) }
end
lemma measure_eq_inter_diff {s t : set α} (hs : is_measurable s) (ht : is_measurable t) :
μ s = μ (s ∩ t) + μ (s \ t) :=
have hd : disjoint (s ∩ t) (s \ t) := assume a ⟨⟨_, hs⟩, _, hns⟩, hns hs ,
by rw [← measure_union hd (hs.inter ht) (hs.diff ht), inter_union_diff s t]
lemma measure_union_add_inter {s t : set α} (hs : is_measurable s) (ht : is_measurable t) :
μ (s ∪ t) + μ (s ∩ t) = μ s + μ t :=
by { rw [measure_eq_inter_diff (hs.union ht) ht, set.union_inter_cancel_right,
union_diff_right, measure_eq_inter_diff hs ht], ac_refl }
/-- Continuity from below: the measure of the union of an increasing sequence of measurable sets
is the limit of the measures. -/
lemma tendsto_measure_Union {μ : measure α} {s : ℕ → set α}
(hs : ∀n, is_measurable (s n)) (hm : monotone s) :
tendsto (μ ∘ s) at_top (𝓝 (μ (⋃ n, s n))) :=
begin
rw measure_Union_eq_supr hs (directed_of_sup hm),
exact tendsto_at_top_supr (assume n m hnm, measure_mono $ hm hnm)
end
/-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable
sets is the limit of the measures. -/
lemma tendsto_measure_Inter {μ : measure α} {s : ℕ → set α}
(hs : ∀n, is_measurable (s n)) (hm : ∀ ⦃n m⦄, n ≤ m → s m ⊆ s n) (hf : ∃i, μ (s i) < ⊤) :
tendsto (μ ∘ s) at_top (𝓝 (μ (⋂ n, s n))) :=
begin
rw measure_Inter_eq_infi hs (directed_of_sup hm) hf,
exact tendsto_at_top_infi (assume n m hnm, measure_mono $ hm hnm),
end
/-- One direction of the Borel-Cantelli lemma: if (sᵢ) is a sequence of measurable sets such that
∑ μ sᵢ exists, then the limit superior of the sᵢ is a null set. -/
lemma measure_limsup_eq_zero {s : ℕ → set α} (hs : ∀ i, is_measurable (s i))
(hs' : (∑' i, μ (s i)) ≠ ⊤) : μ (limsup at_top s) = 0 :=
begin
rw limsup_eq_infi_supr_of_nat',
-- We will show that both `μ (⨅ n, ⨆ i, s (i + n))` and `0` are the limit of `μ (⊔ i, s (i + n))`
-- as `n` tends to infinity. For the former, we use continuity from above.
refine tendsto_nhds_unique
(tendsto_measure_Inter (λ i, is_measurable.Union (λ b, hs (b + i))) _
⟨0, lt_of_le_of_lt (measure_Union_le s) (ennreal.lt_top_iff_ne_top.2 hs')⟩) _,
{ intros n m hnm x,
simp only [set.mem_Union],
exact λ ⟨i, hi⟩, ⟨i + (m - n), by simpa only [add_assoc, nat.sub_add_cancel hnm] using hi⟩ },
{ -- For the latter, notice that, `μ (⨆ i, s (i + n)) ≤ ∑' s (i + n)`. Since the right hand side
-- converges to `0` by hypothesis, so does the former and the proof is complete.
exact (tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds
(ennreal.tendsto_sum_nat_add (μ ∘ s) hs')
(eventually_of_forall (by simp only [forall_const, zero_le]))
(eventually_of_forall (λ i, measure_Union_le _))) }
end
lemma measure_if {β} {x : β} {t : set β} {s : set α} {μ : measure α} :
μ (if x ∈ t then s else ∅) = indicator t (λ _, μ s) x :=
by { split_ifs; simp [h] }
end
/-- Obtain a measure by giving an outer measure where all sets in the σ-algebra are
Carathéodory measurable. -/
def outer_measure.to_measure {α} (m : outer_measure α)
[ms : measurable_space α] (h : ms ≤ m.caratheodory) :
measure α :=
measure.of_measurable (λ s _, m s) m.empty
(λ f hf hd, m.Union_eq_of_caratheodory (λ i, h _ (hf i)) hd)
lemma le_to_outer_measure_caratheodory {α} [ms : measurable_space α]
(μ : measure α) : ms ≤ μ.to_outer_measure.caratheodory :=
begin
assume s hs,
rw to_outer_measure_eq_induced_outer_measure,
refine outer_measure.of_function_caratheodory (λ t, le_infi $ λ ht, _),
rw [← measure_eq_extend (ht.inter hs),
← measure_eq_extend (ht.diff hs),
← measure_union _ (ht.inter hs) (ht.diff hs),
inter_union_diff],
exact le_refl _,
exact λ x ⟨⟨_, h₁⟩, _, h₂⟩, h₂ h₁
end
@[simp] lemma to_measure_to_outer_measure {α} (m : outer_measure α)
[ms : measurable_space α] (h : ms ≤ m.caratheodory) :
(m.to_measure h).to_outer_measure = m.trim := rfl
@[simp] lemma to_measure_apply {α} (m : outer_measure α)
[ms : measurable_space α] (h : ms ≤ m.caratheodory)
{s : set α} (hs : is_measurable s) :
m.to_measure h s = m s := m.trim_eq hs
lemma le_to_measure_apply {α} (m : outer_measure α) [ms : measurable_space α]
(h : ms ≤ m.caratheodory) (s : set α) :
m s ≤ m.to_measure h s :=
m.le_trim s
@[simp] lemma to_outer_measure_to_measure {α : Type*} [ms : measurable_space α] {μ : measure α} :
μ.to_outer_measure.to_measure (le_to_outer_measure_caratheodory _) = μ :=
measure.ext $ λ s, μ.to_outer_measure.trim_eq
namespace measure
variables {α : Type*} {β : Type*} {γ : Type*}
variables [measurable_space α] [measurable_space β] [measurable_space γ]
variables {μ μ₁ μ₂ ν ν' : measure α}
/-! ### The `ennreal`-module of measures -/
instance : has_zero (measure α) :=
⟨{ to_outer_measure := 0,
m_Union := λ f hf hd, tsum_zero.symm,
trimmed := outer_measure.trim_zero }⟩
@[simp] theorem zero_to_outer_measure : (0 : measure α).to_outer_measure = 0 := rfl
@[simp, norm_cast] theorem coe_zero : ⇑(0 : measure α) = 0 := rfl
lemma eq_zero_of_not_nonempty (h : ¬nonempty α) (μ : measure α) : μ = 0 :=
ext $ λ s hs, by simp only [eq_empty_of_not_nonempty h s, measure_empty]
instance : inhabited (measure α) := ⟨0⟩
instance : has_add (measure α) :=
⟨λμ₁ μ₂, {
to_outer_measure := μ₁.to_outer_measure + μ₂.to_outer_measure,
m_Union := λs hs hd,
show μ₁ (⋃ i, s i) + μ₂ (⋃ i, s i) = ∑' i, μ₁ (s i) + μ₂ (s i),
by rw [ennreal.tsum_add, measure_Union hd hs, measure_Union hd hs],
trimmed := by rw [outer_measure.trim_add, μ₁.trimmed, μ₂.trimmed] }⟩
@[simp] theorem add_to_outer_measure (μ₁ μ₂ : measure α) :
(μ₁ + μ₂).to_outer_measure = μ₁.to_outer_measure + μ₂.to_outer_measure := rfl
@[simp, norm_cast] theorem coe_add (μ₁ μ₂ : measure α) : ⇑(μ₁ + μ₂) = μ₁ + μ₂ := rfl
theorem add_apply (μ₁ μ₂ : measure α) (s) : (μ₁ + μ₂) s = μ₁ s + μ₂ s := rfl
instance add_comm_monoid : add_comm_monoid (measure α) :=
to_outer_measure_injective.add_comm_monoid to_outer_measure zero_to_outer_measure
add_to_outer_measure
instance : has_scalar ennreal (measure α) :=
⟨λ c μ,
{ to_outer_measure := c • μ.to_outer_measure,
m_Union := λ s hs hd, by simp [measure_Union, *, ennreal.tsum_mul_left],
trimmed := by rw [outer_measure.trim_smul, μ.trimmed] }⟩
@[simp] theorem smul_to_outer_measure (c : ennreal) (μ : measure α) :
(c • μ).to_outer_measure = c • μ.to_outer_measure :=
rfl
@[simp, norm_cast] theorem coe_smul (c : ennreal) (μ : measure α) :
⇑(c • μ) = c • μ :=
rfl
theorem smul_apply (c : ennreal) (μ : measure α) (s : set α) :
(c • μ) s = c * μ s :=
rfl
instance : semimodule ennreal (measure α) :=
injective.semimodule ennreal ⟨to_outer_measure, zero_to_outer_measure, add_to_outer_measure⟩
to_outer_measure_injective smul_to_outer_measure
/-! ### The complete lattice of measures -/
instance : partial_order (measure α) :=
{ le := λm₁ m₂, ∀ s, is_measurable s → m₁ s ≤ m₂ s,
le_refl := assume m s hs, le_refl _,
le_trans := assume m₁ m₂ m₃ h₁ h₂ s hs, le_trans (h₁ s hs) (h₂ s hs),
le_antisymm := assume m₁ m₂ h₁ h₂, ext $
assume s hs, le_antisymm (h₁ s hs) (h₂ s hs) }
theorem le_iff : μ₁ ≤ μ₂ ↔ ∀ s, is_measurable s → μ₁ s ≤ μ₂ s := iff.rfl
theorem to_outer_measure_le : μ₁.to_outer_measure ≤ μ₂.to_outer_measure ↔ μ₁ ≤ μ₂ :=
by rw [← μ₂.trimmed, outer_measure.le_trim_iff]; refl
theorem le_iff' : μ₁ ≤ μ₂ ↔ ∀ s, μ₁ s ≤ μ₂ s :=
to_outer_measure_le.symm
theorem lt_iff : μ < ν ↔ μ ≤ ν ∧ ∃ s, is_measurable s ∧ μ s < ν s :=
lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff, not_forall, not_le, exists_prop]
theorem lt_iff' : μ < ν ↔ μ ≤ ν ∧ ∃ s, μ s < ν s :=
lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff', not_forall, not_le]
-- TODO: add typeclasses for `∀ c, monotone ((*) c)` and `∀ c, monotone ((+) c)`
protected lemma add_le_add_left (ν : measure α) (hμ : μ₁ ≤ μ₂) : ν + μ₁ ≤ ν + μ₂ :=
λ s hs, add_le_add_left (hμ s hs) _
protected lemma add_le_add_right (hμ : μ₁ ≤ μ₂) (ν : measure α) : μ₁ + ν ≤ μ₂ + ν :=
λ s hs, add_le_add_right (hμ s hs) _
protected lemma add_le_add (hμ : μ₁ ≤ μ₂) {ν₁ ν₂ : measure α} (hν : ν₁ ≤ ν₂) :
μ₁ + ν₁ ≤ μ₂ + ν₂ :=
λ s hs, add_le_add (hμ s hs) (hν s hs)
protected lemma le_add_left (h : μ ≤ ν) : μ ≤ ν' + ν :=
λ s hs, le_add_left (h s hs)
protected lemma le_add_right (h : μ ≤ ν) : μ ≤ ν + ν' :=
λ s hs, le_add_right (h s hs)
section Inf
variables {m : set (measure α)}
lemma Inf_caratheodory (s : set α) (hs : is_measurable s) :
(Inf (to_outer_measure '' m)).caratheodory.is_measurable' s :=
begin
rw [outer_measure.Inf_eq_of_function_Inf_gen],
refine outer_measure.of_function_caratheodory (assume t, _),
cases t.eq_empty_or_nonempty with ht ht, by simp [ht],
simp only [outer_measure.Inf_gen_nonempty1 _ _ ht, le_infi_iff, ball_image_iff,
coe_to_outer_measure, measure_eq_infi t],
assume μ hμ u htu hu,
have hm : ∀{s t}, s ⊆ t → outer_measure.Inf_gen (to_outer_measure '' m) s ≤ μ t,
{ assume s t hst,
rw [outer_measure.Inf_gen_nonempty2 _ ⟨_, mem_image_of_mem _ hμ⟩],
refine infi_le_of_le (μ.to_outer_measure) (infi_le_of_le (mem_image_of_mem _ hμ) _),
rw [to_outer_measure_apply],
refine measure_mono hst },
rw [measure_eq_inter_diff hu hs],
refine add_le_add (hm $ inter_subset_inter_left _ htu) (hm $ diff_subset_diff_left htu)
end
instance : has_Inf (measure α) :=
⟨λm, (Inf (to_outer_measure '' m)).to_measure $ Inf_caratheodory⟩
lemma Inf_apply {m : set (measure α)} {s : set α} (hs : is_measurable s) :
Inf m s = Inf (to_outer_measure '' m) s :=
to_measure_apply _ _ hs
private lemma measure_Inf_le (h : μ ∈ m) : Inf m ≤ μ :=
have Inf (to_outer_measure '' m) ≤ μ.to_outer_measure := Inf_le (mem_image_of_mem _ h),
assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s
private lemma measure_le_Inf (h : ∀μ' ∈ m, μ ≤ μ') : μ ≤ Inf m :=
have μ.to_outer_measure ≤ Inf (to_outer_measure '' m) :=
le_Inf $ ball_image_of_ball $ assume μ hμ, to_outer_measure_le.2 $ h _ hμ,
assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s
instance : complete_lattice (measure α) :=
{ bot := 0,
bot_le := assume a s hs, by exact bot_le,
/- Adding an explicit `top` makes `leanchecker` fail, see lean#364, disable for now
top := (⊤ : outer_measure α).to_measure (by rw [outer_measure.top_caratheodory]; exact le_top),
le_top := assume a s hs,
by cases s.eq_empty_or_nonempty with h h;
simp [h, to_measure_apply ⊤ _ hs, outer_measure.top_apply],
-/
.. complete_lattice_of_Inf (measure α) (λ ms, ⟨λ _, measure_Inf_le, λ _, measure_le_Inf⟩) }
end Inf
protected lemma zero_le (μ : measure α) : 0 ≤ μ := bot_le
lemma le_zero_iff_eq' : μ ≤ 0 ↔ μ = 0 :=
μ.zero_le.le_iff_eq
@[simp] lemma measure_univ_eq_zero {μ : measure α} : μ univ = 0 ↔ μ = 0 :=
⟨λ h, bot_unique $ λ s hs, trans_rel_left (≤) (measure_mono (subset_univ s)) h, λ h, h.symm ▸ rfl⟩
/-! ### Pushforward and pullback -/
/-- Lift a linear map between `outer_measure` spaces such that for each measure `μ` every measurable
set is caratheodory-measurable w.r.t. `f μ` to a linear map between `measure` spaces. -/
def lift_linear (f : outer_measure α →ₗ[ennreal] outer_measure β)
(hf : ∀ μ : measure α, ‹_› ≤ (f μ.to_outer_measure).caratheodory) :
measure α →ₗ[ennreal] measure β :=
{ to_fun := λ μ, (f μ.to_outer_measure).to_measure (hf μ),
map_add' := λ μ₁ μ₂, ext $ λ s hs, by simp [hs],
map_smul' := λ c μ, ext $ λ s hs, by simp [hs] }
@[simp] lemma lift_linear_apply {f : outer_measure α →ₗ[ennreal] outer_measure β} (hf)
{μ : measure α} {s : set β} (hs : is_measurable s) :
lift_linear f hf μ s = f μ.to_outer_measure s :=
to_measure_apply _ _ hs
lemma le_lift_linear_apply {f : outer_measure α →ₗ[ennreal] outer_measure β} (hf)
{μ : measure α} (s : set β) :
f μ.to_outer_measure s ≤ lift_linear f hf μ s :=
le_to_measure_apply _ _ s
/-- The pushforward of a measure. It is defined to be `0` if `f` is not a measurable function. -/
def map (f : α → β) : measure α →ₗ[ennreal] measure β :=
if hf : measurable f then
lift_linear (outer_measure.map f) $ λ μ s hs t,
le_to_outer_measure_caratheodory μ _ (hf hs) (f ⁻¹' t)
else 0
@[simp] theorem map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : is_measurable s) :
map f μ s = μ (f ⁻¹' s) :=
by simp [map, dif_pos hf, hs]
@[simp] lemma map_id : map id μ = μ :=
ext $ λ s, map_apply measurable_id
lemma map_map {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) :
map g (map f μ) = map (g ∘ f) μ :=
ext $ λ s hs,
by simp [hf, hg, hs, hg hs, hg.comp hf, ← preimage_comp]
/-- Pullback of a `measure`. If `f` sends each `measurable` set to a `measurable` set, then for each
measurable set `s` we have `comap f μ s = μ (f '' s)`. -/
def comap (f : α → β) : measure β →ₗ[ennreal] measure α :=
if hf : injective f ∧ ∀ s, is_measurable s → is_measurable (f '' s) then
lift_linear (outer_measure.comap f) $ λ μ s hs t,
begin
simp only [coe_to_outer_measure, outer_measure.comap_apply, ← image_inter hf.1,
image_diff hf.1],
apply le_to_outer_measure_caratheodory,
exact hf.2 s hs
end
else 0
lemma comap_apply (f : α → β) (hfi : injective f)
(hf : ∀ s, is_measurable s → is_measurable (f '' s)) (μ : measure β)
{s : set α} (hs : is_measurable s) :
comap f μ s = μ (f '' s) :=
begin
rw [comap, dif_pos, lift_linear_apply _ hs, outer_measure.comap_apply, coe_to_outer_measure],
exact ⟨hfi, hf⟩
end
/-! ### Restricting a measure -/
/-- Restrict a measure `μ` to a set `s` as an `ennreal`-linear map. -/
def restrictₗ (s : set α) : measure α →ₗ[ennreal] measure α :=
lift_linear (outer_measure.restrict s) $ λ μ s' hs' t,
begin
suffices : μ (s ∩ t) = μ (s ∩ t ∩ s') + μ (s ∩ t \ s'),
{ simpa [← set.inter_assoc, set.inter_comm _ s, ← inter_diff_assoc] },
exact le_to_outer_measure_caratheodory _ _ hs' _,
end
/-- Restrict a measure `μ` to a set `s`. -/
def restrict (μ : measure α) (s : set α) : measure α := restrictₗ s μ
@[simp] lemma restrictₗ_apply (s : set α) (μ : measure α) :
restrictₗ s μ = μ.restrict s :=
rfl
@[simp] lemma restrict_apply {s t : set α} (ht : is_measurable t) :
μ.restrict s t = μ (t ∩ s) :=
by simp [← restrictₗ_apply, restrictₗ, ht]
lemma restrict_apply_univ (s : set α) : μ.restrict s univ = μ s :=
by rw [restrict_apply is_measurable.univ, set.univ_inter]
lemma le_restrict_apply (s t : set α) :
μ (t ∩ s) ≤ μ.restrict s t :=
by { rw [restrict, restrictₗ], convert le_lift_linear_apply _ t, simp }
@[simp] lemma restrict_add (μ ν : measure α) (s : set α) :
(μ + ν).restrict s = μ.restrict s + ν.restrict s :=
(restrictₗ s).map_add μ ν
@[simp] lemma restrict_zero (s : set α) : (0 : measure α).restrict s = 0 :=
(restrictₗ s).map_zero
@[simp] lemma restrict_smul (c : ennreal) (μ : measure α) (s : set α) :
(c • μ).restrict s = c • μ.restrict s :=
(restrictₗ s).map_smul c μ
@[simp] lemma restrict_restrict {s t : set α} (hs : is_measurable s) :
(μ.restrict t).restrict s = μ.restrict (s ∩ t) :=
ext $ λ u hu, by simp [*, set.inter_assoc]
lemma restrict_apply_eq_zero {s t : set α} (ht : is_measurable t) :
μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 :=
by rw [restrict_apply ht]
lemma restrict_apply_eq_zero' {s t : set α} (hs : is_measurable s) :
μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 :=
begin
refine ⟨λ h, le_zero_iff_eq.1 (h ▸ le_restrict_apply _ _), λ h, _⟩,
rcases exists_is_measurable_superset_of_measure_eq_zero h with ⟨t', htt', ht', ht'0⟩,
apply measure_mono_null ((inter_subset _ _ _).1 htt'),
rw [restrict_apply (hs.compl.union ht'), union_inter_distrib_right, compl_inter_self,
set.empty_union],
exact measure_mono_null (inter_subset_left _ _) ht'0
end
@[simp] lemma restrict_eq_zero {s} : μ.restrict s = 0 ↔ μ s = 0 :=
by rw [← measure_univ_eq_zero, restrict_apply_univ]
@[simp] lemma restrict_empty : μ.restrict ∅ = 0 := ext $ λ s hs, by simp [hs]
@[simp] lemma restrict_univ : μ.restrict univ = μ := ext $ λ s hs, by simp [hs]
lemma restrict_union_apply {s s' t : set α} (h : disjoint (t ∩ s) (t ∩ s')) (hs : is_measurable s)
(hs' : is_measurable s') (ht : is_measurable t) :
μ.restrict (s ∪ s') t = μ.restrict s t + μ.restrict s' t :=
begin
simp only [restrict_apply, ht, set.inter_union_distrib_left],
exact measure_union h (ht.inter hs) (ht.inter hs'),
end
lemma restrict_union {s t : set α} (h : disjoint s t) (hs : is_measurable s)
(ht : is_measurable t) :
μ.restrict (s ∪ t) = μ.restrict s + μ.restrict t :=
ext $ λ t' ht', restrict_union_apply (h.mono inf_le_right inf_le_right) hs ht ht'
lemma restrict_union_add_inter {s t : set α} (hs : is_measurable s) (ht : is_measurable t) :
μ.restrict (s ∪ t) + μ.restrict (s ∩ t) = μ.restrict s + μ.restrict t :=
begin
ext1 u hu,
simp only [add_apply, restrict_apply hu, inter_union_distrib_left],
convert measure_union_add_inter (hu.inter hs) (hu.inter ht) using 3,
rw [set.inter_left_comm (u ∩ s), set.inter_assoc, ← set.inter_assoc u u, set.inter_self]
end
@[simp] lemma restrict_add_restrict_compl {s : set α} (hs : is_measurable s) :
μ.restrict s + μ.restrict sᶜ = μ :=
by rw [← restrict_union (disjoint_compl_right _) hs hs.compl, union_compl_self, restrict_univ]
@[simp] lemma restrict_compl_add_restrict {s : set α} (hs : is_measurable s) :
μ.restrict sᶜ + μ.restrict s = μ :=
by rw [add_comm, restrict_add_restrict_compl hs]
lemma restrict_union_le (s s' : set α) : μ.restrict (s ∪ s') ≤ μ.restrict s + μ.restrict s' :=
begin
intros t ht,
suffices : μ (t ∩ s ∪ t ∩ s') ≤ μ (t ∩ s) + μ (t ∩ s'),
by simpa [ht, inter_union_distrib_left],
apply measure_union_le
end
lemma restrict_Union_apply {ι} [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s))
(hm : ∀ i, is_measurable (s i)) {t : set α} (ht : is_measurable t) :
μ.restrict (⋃ i, s i) t = ∑' i, μ.restrict (s i) t :=
begin
simp only [restrict_apply, ht, inter_Union],
exact measure_Union (λ i j hij, (hd i j hij).mono inf_le_right inf_le_right)
(λ i, ht.inter (hm i))
end
lemma restrict_Union_apply_eq_supr {ι} [encodable ι] {s : ι → set α}
(hm : ∀ i, is_measurable (s i)) (hd : directed (⊆) s) {t : set α} (ht : is_measurable t) :
μ.restrict (⋃ i, s i) t = ⨆ i, μ.restrict (s i) t :=
begin
simp only [restrict_apply ht, inter_Union],
rw [measure_Union_eq_supr],
exacts [λ i, ht.inter (hm i), hd.mono_comp _ (λ s₁ s₂, inter_subset_inter_right _)]
end
lemma restrict_map {f : α → β} (hf : measurable f) {s : set β} (hs : is_measurable s) :
(map f μ).restrict s = map f (μ.restrict $ f ⁻¹' s) :=
ext $ λ t ht, by simp [*, hf ht]
lemma map_comap_subtype_coe {s : set α} (hs : is_measurable s) :
(map (coe : s → α)).comp (comap coe) = restrictₗ s :=
linear_map.ext $ λ μ, ext $ λ t ht,
by rw [restrictₗ_apply, restrict_apply ht, linear_map.comp_apply,
map_apply measurable_subtype_coe ht,
comap_apply (coe : s → α) subtype.val_injective (λ _, hs.subtype_image) _
(measurable_subtype_coe ht), subtype.image_preimage_coe]
/-- Restriction of a measure to a subset is monotone both in set and in measure. -/
@[mono] lemma restrict_mono ⦃s s' : set α⦄ (hs : s ⊆ s') ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) :
μ.restrict s ≤ ν.restrict s' :=
assume t ht,
calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht
... ≤ μ (t ∩ s') : measure_mono $ inter_subset_inter_right _ hs
... ≤ ν (t ∩ s') : le_iff'.1 hμν (t ∩ s')
... = ν.restrict s' t : (restrict_apply ht).symm
lemma restrict_le_self {s} : μ.restrict s ≤ μ :=
assume t ht,
calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht
... ≤ μ t : measure_mono $ inter_subset_left t s
lemma restrict_congr_meas {s} (hs : is_measurable s) :
μ.restrict s = ν.restrict s ↔ ∀ t ⊆ s, is_measurable t → μ t = ν t :=
⟨λ H t hts ht,
by rw [← inter_eq_self_of_subset_left hts, ← restrict_apply ht, H, restrict_apply ht],
λ H, ext $ λ t ht,
by rw [restrict_apply ht, restrict_apply ht, H _ (inter_subset_right _ _) (ht.inter hs)]⟩
lemma restrict_congr_mono {s t} (hs : s ⊆ t) (hm : is_measurable s)
(h : μ.restrict t = ν.restrict t) :
μ.restrict s = ν.restrict s :=
by rw [← inter_eq_self_of_subset_left hs, ← restrict_restrict hm, h, restrict_restrict hm]
/-- If two measures agree on all measurable subsets of `s` and `t`, then they agree on all
measurable subsets of `s ∪ t`. -/
lemma restrict_union_congr {s t : set α} (hsm : is_measurable s) (htm : is_measurable t) :
μ.restrict (s ∪ t) = ν.restrict (s ∪ t) ↔
μ.restrict s = ν.restrict s ∧ μ.restrict t = ν.restrict t :=
begin
refine ⟨λ h, ⟨restrict_congr_mono (subset_union_left _ _) hsm h,
restrict_congr_mono (subset_union_right _ _) htm h⟩, _⟩,
simp only [restrict_congr_meas, hsm, htm, hsm.union htm],
rintros ⟨hs, ht⟩ u hu hum,
rw [measure_eq_inter_diff hum hsm, measure_eq_inter_diff hum hsm,
hs _ (inter_subset_right _ _) (hum.inter hsm),
ht _ (diff_subset_iff.2 hu) (hum.diff hsm)]
end
variables {ι : Type*}
lemma restrict_finset_bUnion_congr {s : finset ι} {t : ι → set α}
(htm : ∀ i ∈ s, is_measurable (t i)) :
μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔
∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) :=
begin
induction s using finset.induction_on with i s hi hs, { simp },
simp only [finset.mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at htm ⊢,
simp only [finset.bUnion_insert, ← hs htm.2],
exact restrict_union_congr htm.1 (s.is_measurable_bUnion htm.2)
end
lemma restrict_Union_congr [encodable ι] {s : ι → set α} (hm : ∀ i, is_measurable (s i)) :
μ.restrict (⋃ i, s i) = ν.restrict (⋃ i, s i) ↔
∀ i, μ.restrict (s i) = ν.restrict (s i) :=
begin
refine ⟨λ h i, restrict_congr_mono (subset_Union _ _) (hm i) h, λ h, _⟩,
ext1 t ht,
have M : ∀ t : finset ι, is_measurable (⋃ i ∈ t, s i) :=
λ t, t.is_measurable_bUnion (λ i _, hm i),
have D : directed (⊆) (λ t : finset ι, ⋃ i ∈ t, s i) :=
directed_of_sup (λ t₁ t₂ ht, bUnion_subset_bUnion_left ht),
rw [Union_eq_Union_finset],
simp only [restrict_Union_apply_eq_supr M D ht,
(restrict_finset_bUnion_congr (λ i hi, hm i)).2 (λ i hi, h i)],
end
lemma restrict_bUnion_congr {s : set ι} {t : ι → set α} (hc : countable s)
(htm : ∀ i ∈ s, is_measurable (t i)) :
μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔
∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) :=
begin
simp only [bUnion_eq_Union, set_coe.forall'] at htm ⊢,
haveI := hc.to_encodable,
exact restrict_Union_congr htm
end
lemma restrict_sUnion_congr {S : set (set α)} (hc : countable S) (hm : ∀ s ∈ S, is_measurable s) :
μ.restrict (⋃₀ S) = ν.restrict (⋃₀ S) ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s :=
by rw [sUnion_eq_bUnion, restrict_bUnion_congr hc hm]
/-- This lemma shows that `restrict` and `to_outer_measure` commute. Note that the LHS has a
restrict on measures and the RHS has a restrict on outer measures. -/
lemma restrict_to_outer_measure_eq_to_outer_measure_restrict {s : set α} (h : is_measurable s) :
(μ.restrict s).to_outer_measure = outer_measure.restrict s μ.to_outer_measure :=
by simp_rw [restrict, restrictₗ, lift_linear, linear_map.coe_mk, to_measure_to_outer_measure,
outer_measure.restrict_trim h, μ.trimmed]
/-- This lemma shows that `Inf` and `restrict` commute for measures. -/
lemma restrict_Inf_eq_Inf_restrict {m : set (measure α)} {t : set α}
(h_nonempty : m.nonempty) (ht : is_measurable t) :
(Inf m).restrict t = Inf ((λ μ : measure α, μ.restrict t) '' m) :=
begin
ext1 s hs,
simp_rw [Inf_apply hs, restrict_apply hs, Inf_apply (is_measurable.inter hs ht), set.image_image,
restrict_to_outer_measure_eq_to_outer_measure_restrict ht, ← set.image_image _ to_outer_measure,
← outer_measure.restrict_Inf_eq_Inf_restrict _ (h_nonempty.image _),
outer_measure.restrict_apply]
end
/-! ### Extensionality results -/
/-- Two measures are equal if they have equal restrictions on a spanning collection of sets
(formulated using `Union`). -/
lemma ext_iff_of_Union_eq_univ [encodable ι] {s : ι → set α}
(hm : ∀ i, is_measurable (s i)) (hs : (⋃ i, s i) = univ) :
μ = ν ↔ ∀ i, μ.restrict (s i) = ν.restrict (s i) :=
by rw [← restrict_Union_congr hm, hs, restrict_univ, restrict_univ]
alias ext_iff_of_Union_eq_univ ↔ _ measure_theory.measure.ext_of_Union_eq_univ
/-- Two measures are equal if they have equal restrictions on a spanning collection of sets
(formulated using `bUnion`). -/
lemma ext_iff_of_bUnion_eq_univ {S : set ι} {s : ι → set α} (hc : countable S)
(hm : ∀ i ∈ S, is_measurable (s i)) (hs : (⋃ i ∈ S, s i) = univ) :
μ = ν ↔ ∀ i ∈ S, μ.restrict (s i) = ν.restrict (s i) :=
by rw [← restrict_bUnion_congr hc hm, hs, restrict_univ, restrict_univ]
alias ext_iff_of_bUnion_eq_univ ↔ _ measure_theory.measure.ext_of_bUnion_eq_univ
/-- Two measures are equal if they have equal restrictions on a spanning collection of sets
(formulated using `sUnion`). -/
lemma ext_iff_of_sUnion_eq_univ {S : set (set α)} (hc : countable S)
(hm : ∀ s ∈ S, is_measurable s) (hs : (⋃₀ S) = univ) :
μ = ν ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s :=
ext_iff_of_bUnion_eq_univ hc hm $ by rwa ← sUnion_eq_bUnion
alias ext_iff_of_sUnion_eq_univ ↔ _ measure_theory.measure.ext_of_sUnion_eq_univ
lemma ext_of_generate_from_of_cover {S T : set (set α)}
(h_gen : ‹_› = generate_from S) (hc : countable T)
(h_inter : is_pi_system S)
(hm : ∀ t ∈ T, is_measurable t) (hU : ⋃₀ T = univ) (htop : ∀ t ∈ T, μ t < ⊤)
(ST_eq : ∀ (t ∈ T) (s ∈ S), μ (s ∩ t) = ν (s ∩ t)) (T_eq : ∀ t ∈ T, μ t = ν t) :
μ = ν :=
begin
refine ext_of_sUnion_eq_univ hc hm hU (λ t ht, _),
ext1 u hu,
simp only [restrict_apply hu],
refine induction_on_inter h_gen h_inter _ (ST_eq t ht) _ _ hu,
{ simp only [set.empty_inter, measure_empty] },
{ intros v hv hvt,
have := T_eq t ht,
rw [set.inter_comm] at hvt ⊢,
rwa [measure_eq_inter_diff (hm _ ht) hv, measure_eq_inter_diff (hm _ ht) hv, ← hvt,
ennreal.add_right_inj] at this,
exact (measure_mono $ set.inter_subset_left _ _).trans_lt (htop t ht) },
{ intros f hfd hfm h_eq,
have : pairwise (disjoint on λ n, f n ∩ t) :=
λ m n hmn, (hfd m n hmn).mono (inter_subset_left _ _) (inter_subset_left _ _),
simp only [Union_inter, measure_Union this (λ n, is_measurable.inter (hfm n) (hm t ht)), h_eq] }
end
/-- Two measures are equal if they are equal on the π-system generating the σ-algebra,
and they are both finite on a increasing spanning sequence of sets in the π-system.
This lemma is formulated using `sUnion`. -/
lemma ext_of_generate_from_of_cover_subset {S T : set (set α)}
(h_gen : ‹_› = generate_from S)
(h_inter : is_pi_system S)
(h_sub : T ⊆ S) (hc : countable T) (hU : ⋃₀ T = univ) (htop : ∀ s ∈ T, μ s < ⊤)
(h_eq : ∀ s ∈ S, μ s = ν s) :
μ = ν :=
begin
refine ext_of_generate_from_of_cover h_gen hc h_inter _ hU htop _ (λ t ht, h_eq t (h_sub ht)),
{ intros t ht, rw [h_gen], exact generate_measurable.basic _ (h_sub ht) },
{ intros t ht s hs, cases (s ∩ t).eq_empty_or_nonempty with H H,
{ simp only [H, measure_empty] },
{ exact h_eq _ (h_inter _ _ hs (h_sub ht) H) } }
end
/-- Two measures are equal if they are equal on the π-system generating the σ-algebra,
and they are both finite on a increasing spanning sequence of sets in the π-system.
This lemma is formulated using `Union`.
`finite_spanning_sets_in.ext` is a reformulation of this lemma. -/
lemma ext_of_generate_from_of_Union (C : set (set α)) (B : ℕ → set α)
(hA : ‹_› = generate_from C) (hC : is_pi_system C) (h1B : (⋃ i, B i) = univ)
(h2B : ∀ i, B i ∈ C) (hμB : ∀ i, μ (B i) < ⊤) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν :=
begin
refine ext_of_generate_from_of_cover_subset hA hC _ (countable_range B) h1B _ h_eq,
{ rintro _ ⟨i, rfl⟩, apply h2B },
{ rintro _ ⟨i, rfl⟩, apply hμB }
end
/-- The dirac measure. -/
def dirac (a : α) : measure α :=
(outer_measure.dirac a).to_measure (by simp)
lemma dirac_apply' (a : α) {s : set α} (hs : is_measurable s) :
dirac a s = ⨆ h : a ∈ s, 1 :=
to_measure_apply _ _ hs
@[simp] lemma dirac_apply (a : α) {s : set α} (hs : is_measurable s) :
dirac a s = s.indicator 1 a :=
(dirac_apply' a hs).trans $ by { by_cases h : a ∈ s; simp [h] }
lemma dirac_apply_of_mem {a : α} {s : set α} (h : a ∈ s) :
dirac a s = 1 :=
begin
rw [measure_eq_infi, infi_subtype', infi_subtype'],
convert infi_const,
{ ext1 ⟨⟨t, hst⟩, ht⟩,
dsimp only [subtype.coe_mk] at *,
simp only [dirac_apply _ ht, indicator_of_mem (hst h), pi.one_apply] },
{ exact ⟨⟨⟨set.univ, subset_univ _⟩, is_measurable.univ⟩⟩ }
end
/-- Sum of an indexed family of measures. -/
def sum {ι : Type*} (f : ι → measure α) : measure α :=
(outer_measure.sum (λ i, (f i).to_outer_measure)).to_measure $
le_trans
(by exact le_infi (λ i, le_to_outer_measure_caratheodory _))
(outer_measure.le_sum_caratheodory _)
@[simp] lemma sum_apply {ι : Type*} (f : ι → measure α) {s : set α} (hs : is_measurable s) :
sum f s = ∑' i, f i s :=
to_measure_apply _ _ hs
lemma le_sum {ι : Type*} (μ : ι → measure α) (i : ι) : μ i ≤ sum μ :=
λ s hs, by simp only [sum_apply μ hs, ennreal.le_tsum i]
lemma restrict_Union {ι} [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s))
(hm : ∀ i, is_measurable (s i)) :
μ.restrict (⋃ i, s i) = sum (λ i, μ.restrict (s i)) :=
ext $ λ t ht, by simp only [sum_apply _ ht, restrict_Union_apply hd hm ht]
lemma restrict_Union_le {ι} [encodable ι] {s : ι → set α} :
μ.restrict (⋃ i, s i) ≤ sum (λ i, μ.restrict (s i)) :=
begin
intros t ht,
suffices : μ (⋃ i, t ∩ s i) ≤ ∑' i, μ (t ∩ s i), by simpa [ht, inter_Union],
apply measure_Union_le
end
@[simp] lemma sum_bool (f : bool → measure α) : sum f = f tt + f ff :=
ext $ λ s hs, by simp [hs, tsum_fintype]
@[simp] lemma sum_cond (μ ν : measure α) : sum (λ b, cond b μ ν) = μ + ν :=
sum_bool _
@[simp] lemma restrict_sum {ι : Type*} (μ : ι → measure α) {s : set α} (hs : is_measurable s) :
(sum μ).restrict s = sum (λ i, (μ i).restrict s) :=
ext $ λ t ht, by simp only [sum_apply, restrict_apply, ht, ht.inter hs]
/-- Counting measure on any measurable space. -/
def count : measure α := sum dirac
lemma count_apply {s : set α} (hs : is_measurable s) :
count s = ∑' i : s, 1 :=
by simp only [count, sum_apply, hs, dirac_apply, ← tsum_subtype s 1, pi.one_apply]
@[simp] lemma count_apply_finset [measurable_singleton_class α] (s : finset α) :
count (↑s : set α) = s.card :=
calc count (↑s : set α) = ∑' i : (↑s : set α), (1 : α → ennreal) i : count_apply s.is_measurable
... = ∑ i in s, 1 : s.tsum_subtype 1
... = s.card : by simp
lemma count_apply_finite [measurable_singleton_class α] (s : set α) (hs : finite s) :
count s = hs.to_finset.card :=
by rw [← count_apply_finset, finite.coe_to_finset]
/-- `count` measure evaluates to infinity at infinite sets. -/
lemma count_apply_infinite [measurable_singleton_class α] {s : set α} (hs : s.infinite) :
count s = ⊤ :=
begin
by_contra H,
rcases ennreal.exists_nat_gt H with ⟨n, hn⟩,
rcases hs.exists_subset_card_eq n with ⟨t, ht, rfl⟩,
have := lt_of_le_of_lt (measure_mono ht) hn,
simpa [lt_irrefl] using this
end
@[simp] lemma count_apply_eq_top [measurable_singleton_class α] {s : set α} :
count s = ⊤ ↔ s.infinite :=
begin
by_cases hs : s.finite,
{ simp [set.infinite, hs, count_apply_finite] },
{ change s.infinite at hs,
simp [hs, count_apply_infinite] }
end
@[simp] lemma count_apply_lt_top [measurable_singleton_class α] {s : set α} :
count s < ⊤ ↔ s.finite :=
calc count s < ⊤ ↔ count s ≠ ⊤ : lt_top_iff_ne_top
... ↔ ¬s.infinite : not_congr count_apply_eq_top
... ↔ s.finite : not_not
/-! ### The almost everywhere filter -/
/-- The “almost everywhere” filter of co-null sets. -/
def ae (μ : measure α) : filter α :=
{ sets := {s | μ sᶜ = 0},
univ_sets := by simp,
inter_sets := λ s t hs ht, by simp only [compl_inter, mem_set_of_eq];
exact measure_union_null hs ht,
sets_of_superset := λ s t hs hst, measure_mono_null (set.compl_subset_compl.2 hst) hs }
/-- The filter of sets `s` such that `sᶜ` has finite measure. -/
def cofinite (μ : measure α) : filter α :=
{ sets := {s | μ sᶜ < ⊤},
univ_sets := by simp,
inter_sets := λ s t hs ht, by { simp only [compl_inter, mem_set_of_eq],
calc μ (sᶜ ∪ tᶜ) ≤ μ sᶜ + μ tᶜ : measure_union_le _ _
... < ⊤ : ennreal.add_lt_top.2 ⟨hs, ht⟩ },
sets_of_superset := λ s t hs hst, lt_of_le_of_lt (measure_mono $ compl_subset_compl.2 hst) hs }
lemma mem_cofinite {s : set α} : s ∈ μ.cofinite ↔ μ sᶜ < ⊤ := iff.rfl
lemma compl_mem_cofinite {s : set α} : sᶜ ∈ μ.cofinite ↔ μ s < ⊤ :=
by rw [mem_cofinite, compl_compl]
lemma eventually_cofinite {p : α → Prop} : (∀ᶠ x in μ.cofinite, p x) ↔ μ {x | ¬p x} < ⊤ := iff.rfl
end measure
open measure
variables {α : Type*} {β : Type*} [measurable_space α] {μ : measure α}
notation `∀ᵐ` binders ` ∂` μ `, ` r:(scoped P, filter.eventually P (measure.ae μ)) := r
notation f ` =ᵐ[`:50 μ:50 `] `:0 g:50 := f =ᶠ[measure.ae μ] g
notation f ` ≤ᵐ[`:50 μ:50 `] `:0 g:50 := f ≤ᶠ[measure.ae μ] g
lemma mem_ae_iff {s : set α} : s ∈ μ.ae ↔ μ sᶜ = 0 := iff.rfl
lemma ae_iff {p : α → Prop} : (∀ᵐ a ∂ μ, p a) ↔ μ { a | ¬ p a } = 0 := iff.rfl
lemma compl_mem_ae_iff {s : set α} : sᶜ ∈ μ.ae ↔ μ s = 0 := by simp only [mem_ae_iff, compl_compl]
lemma measure_zero_iff_ae_nmem {s : set α} : μ s = 0 ↔ ∀ᵐ a ∂ μ, a ∉ s :=
compl_mem_ae_iff.symm
@[simp] lemma ae_eq_bot : μ.ae = ⊥ ↔ μ = 0 :=
by rw [← empty_in_sets_eq_bot, mem_ae_iff, compl_empty, measure_univ_eq_zero]
lemma ae_of_all {p : α → Prop} (μ : measure α) : (∀a, p a) → ∀ᵐ a ∂ μ, p a :=
eventually_of_forall
@[mono] lemma ae_mono {μ ν : measure α} (h : μ ≤ ν) : μ.ae ≤ ν.ae :=
λ s hs, bot_unique $ trans_rel_left (≤) (measure.le_iff'.1 h _) hs
instance : countable_Inter_filter μ.ae :=
⟨begin
intros S hSc hS,
simp only [mem_ae_iff, compl_sInter, sUnion_image, bUnion_eq_Union] at hS ⊢,
haveI := hSc.to_encodable,
exact measure_Union_null (subtype.forall.2 hS)
end⟩
instance ae_is_measurably_generated : is_measurably_generated μ.ae :=
⟨λ s hs, let ⟨t, hst, htm, htμ⟩ := exists_is_measurable_superset_of_measure_eq_zero hs in
⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩
lemma ae_all_iff {ι : Type*} [encodable ι] {p : α → ι → Prop} :
(∀ᵐ a ∂ μ, ∀i, p a i) ↔ (∀i, ∀ᵐ a ∂ μ, p a i) :=
eventually_countable_forall
lemma ae_ball_iff {ι} {S : set ι} (hS : countable S) {p : Π (x : α) (i ∈ S), Prop} :
(∀ᵐ x ∂ μ, ∀ i ∈ S, p x i ‹_›) ↔ ∀ i ∈ S, ∀ᵐ x ∂ μ, p x i ‹_› :=
eventually_countable_ball hS
lemma ae_eq_refl (f : α → β) : f =ᵐ[μ] f := eventually_eq.refl _ _
lemma ae_eq_symm {f g : α → β} (h : f =ᵐ[μ] g) : g =ᵐ[μ] f :=
h.symm
lemma ae_eq_trans {f g h: α → β} (h₁ : f =ᵐ[μ] g) (h₂ : g =ᵐ[μ] h) :
f =ᵐ[μ] h :=
h₁.trans h₂
lemma ae_eq_empty {s : set α} : s =ᵐ[μ] (∅ : set α) ↔ μ s = 0 :=
eventually_eq_empty.trans $ by simp [ae_iff]
lemma ae_le_set {s t : set α} : s ≤ᵐ[μ] t ↔ μ (s \ t) = 0 :=
calc s ≤ᵐ[μ] t ↔ ∀ᵐ x ∂μ, x ∈ s → x ∈ t : iff.rfl
... ↔ μ (s \ t) = 0 : by simp [ae_iff]; refl
lemma union_ae_eq_right {s t : set α} :
(s ∪ t : set α) =ᵐ[μ] t ↔ μ (s \ t) = 0 :=
by simp [eventually_le_antisymm_iff, ae_le_set, union_diff_right,
diff_eq_empty.2 (set.subset_union_right _ _)]
lemma diff_ae_eq_self {s t : set α} :
(s \ t : set α) =ᵐ[μ] s ↔ μ (s ∩ t) = 0 :=
by simp [eventually_le_antisymm_iff, ae_le_set, diff_diff_right,
diff_diff, diff_eq_empty.2 (set.subset_union_right _ _)]
lemma mem_ae_map_iff [measurable_space β] {f : α → β} (hf : measurable f)
{s : set β} (hs : is_measurable s) :
s ∈ (map f μ).ae ↔ (f ⁻¹' s) ∈ μ.ae :=
by simp only [mem_ae_iff, map_apply hf hs.compl, preimage_compl]
lemma ae_map_iff [measurable_space β] {f : α → β} (hf : measurable f)
{p : β → Prop} (hp : is_measurable {x | p x}) :
(∀ᵐ y ∂ (map f μ), p y) ↔ ∀ᵐ x ∂ μ, p (f x) :=
mem_ae_map_iff hf hp
lemma ae_restrict_iff {s : set α} {p : α → Prop} (hp : is_measurable {x | p x}) :
(∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x :=
begin
simp only [ae_iff, ← compl_set_of, restrict_apply hp.compl],
congr' with x, simp [and_comm]
end
lemma ae_smul_measure {p : α → Prop} (h : ∀ᵐ x ∂μ, p x) (c : ennreal) : ∀ᵐ x ∂(c • μ), p x :=
ae_iff.2 $ by rw [smul_apply, ae_iff.1 h, mul_zero]
lemma ae_add_measure_iff {p : α → Prop} {ν} : (∀ᵐ x ∂μ + ν, p x) ↔ (∀ᵐ x ∂μ, p x) ∧ ∀ᵐ x ∂ν, p x :=
add_eq_zero_iff
@[simp] lemma ae_restrict_eq {s : set α} (hs : is_measurable s):
(μ.restrict s).ae = μ.ae ⊓ 𝓟 s :=
begin
ext t,
simp only [mem_inf_principal, mem_ae_iff, restrict_apply_eq_zero' hs, compl_set_of,
not_imp, and_comm (_ ∈ s)],
refl
end
@[simp] lemma ae_restrict_eq_bot {s} : (μ.restrict s).ae = ⊥ ↔ μ s = 0 :=
ae_eq_bot.trans restrict_eq_zero
@[simp] lemma ae_restrict_ne_bot {s} : (μ.restrict s).ae.ne_bot ↔ 0 < μ s :=
(not_congr ae_restrict_eq_bot).trans zero_lt_iff_ne_zero.symm
/-- A version of the Borel-Cantelli lemma: if sᵢ is a sequence of measurable sets such that
∑ μ sᵢ exists, then for almost all x, x does not belong to almost all sᵢ. -/
lemma ae_eventually_not_mem {s : ℕ → set α} (hs : ∀ i, is_measurable (s i))
(hs' : (∑' i, μ (s i)) ≠ ⊤) : ∀ᵐ x ∂ μ, ∀ᶠ n in at_top, x ∉ s n :=
begin
refine measure_mono_null _ (measure_limsup_eq_zero hs hs'),
rw ←set.le_eq_subset,
refine le_Inf (λ t ht x hx, _),
simp only [le_eq_subset, not_exists, eventually_map, exists_prop, ge_iff_le, mem_set_of_eq,
eventually_at_top, mem_compl_eq, not_forall, not_not_mem] at hx ht,
rcases ht with ⟨i, hi⟩,
rcases hx i with ⟨j, ⟨hj, hj'⟩⟩,
exact hi j hj hj'
end
lemma mem_dirac_ae_iff {a : α} {s : set α} (hs : is_measurable s) :
s ∈ (dirac a).ae ↔ a ∈ s :=
by by_cases a ∈ s; simp [mem_ae_iff, dirac_apply, hs.compl, indicator_apply, *]
lemma eventually_dirac {a : α} {p : α → Prop} (hp : is_measurable {x | p x}) :
(∀ᵐ x ∂(dirac a), p x) ↔ p a :=
mem_dirac_ae_iff hp
lemma eventually_eq_dirac [measurable_space β] [measurable_singleton_class β] {a : α} {f : α → β}
(hf : measurable f) :
f =ᵐ[dirac a] const α (f a) :=
(eventually_dirac $ show is_measurable (f ⁻¹' {f a}), from hf $ is_measurable_singleton _).2 rfl
lemma dirac_ae_eq [measurable_singleton_class α] (a : α) : (dirac a).ae = pure a :=
begin
ext s,
simp only [mem_ae_iff, mem_pure_sets],
by_cases ha : a ∈ s,
{ simp only [ha, iff_true],
rw [← set.singleton_subset_iff, ← compl_subset_compl] at ha,
refine measure_mono_null ha _,
simp [dirac_apply a (is_measurable_singleton a).compl] },
{ simp only [ha, iff_false, dirac_apply_of_mem (mem_compl ha)],
exact one_ne_zero }
end
lemma eventually_eq_dirac' [measurable_singleton_class α] {a : α} (f : α → β) :
f =ᵐ[dirac a] const α (f a) :=
by { rw [dirac_ae_eq], show f a = f a, refl }
lemma measure_diff_of_ae_le {s t : set α} (H : s ≤ᵐ[μ] t) :
μ (s \ t) = 0 :=
flip measure_mono_null H $ λ x hx H, hx.2 (H hx.1)
/-- If `s ⊆ t` modulo a set of measure `0`, then `μ s ≤ μ t`. -/
lemma measure_mono_ae {s t : set α} (H : s ≤ᵐ[μ] t) :
μ s ≤ μ t :=
calc μ s ≤ μ (s ∪ t) : measure_mono $ subset_union_left s t
... = μ (t ∪ s \ t) : by rw [union_diff_self, set.union_comm]
... ≤ μ t + μ (s \ t) : measure_union_le _ _
... = μ t : by rw [measure_diff_of_ae_le H, add_zero]
alias measure_mono_ae ← filter.eventually_le.measure_le
/-- If two sets are equal modulo a set of measure zero, then `μ s = μ t`. -/
lemma measure_congr {s t : set α} (H : s =ᵐ[μ] t) : μ s = μ t :=
le_antisymm H.le.measure_le H.symm.le.measure_le
lemma restrict_mono_ae {s t : set α} (h : s ≤ᵐ[μ] t) : μ.restrict s ≤ μ.restrict t :=
begin
intros u hu,
simp only [restrict_apply hu],
exact measure_mono_ae (h.mono $ λ x hx, and.imp id hx)
end
lemma restrict_congr_set {s t : set α} (H : s =ᵐ[μ] t) : μ.restrict s = μ.restrict t :=
le_antisymm (restrict_mono_ae H.le) (restrict_mono_ae H.symm.le)
/-- A measure `μ` is called a probability measure if `μ univ = 1`. -/
class probability_measure (μ : measure α) : Prop := (measure_univ : μ univ = 1)
instance measure.dirac.probability_measure {x : α} : probability_measure (dirac x) :=
⟨dirac_apply_of_mem $ mem_univ x⟩
/-- A measure `μ` is called finite if `μ univ < ⊤`. -/
class finite_measure (μ : measure α) : Prop := (measure_univ_lt_top : μ univ < ⊤)
instance restrict.finite_measure (μ : measure α) {s : set α} [hs : fact (μ s < ⊤)] :
finite_measure (μ.restrict s) :=
⟨by simp [hs.elim]⟩
/-- Measure `μ` *has no atoms* if the measure of each singleton is zero.
NB: Wikipedia assumes that for any measurable set `s` with positive `μ`-measure,
there exists a measurable `t ⊆ s` such that `0 < μ t < μ s`. While this implies `μ {x} = 0`,
the converse is not true. -/
class has_no_atoms (μ : measure α) : Prop :=
(measure_singleton : ∀ x, μ {x} = 0)
export probability_measure (measure_univ) has_no_atoms (measure_singleton)
attribute [simp] measure_singleton
lemma measure_lt_top (μ : measure α) [finite_measure μ] (s : set α) : μ s < ⊤ :=
(measure_mono (subset_univ s)).trans_lt finite_measure.measure_univ_lt_top
lemma measure_ne_top (μ : measure α) [finite_measure μ] (s : set α) : μ s ≠ ⊤ :=
ne_of_lt (measure_lt_top μ s)
/-- `le_of_add_le_add_left` is normally applicable to `ordered_cancel_add_comm_monoid`,
but it holds for measures with the additional assumption that μ is finite. -/
lemma measure.le_of_add_le_add_left {μ ν₁ ν₂ : measure α} [finite_measure μ]
(A2 : μ + ν₁ ≤ μ + ν₂) : ν₁ ≤ ν₂ :=
λ S B1, ennreal.le_of_add_le_add_left (measure_theory.measure_lt_top μ S) (A2 S B1)
@[priority 100]
instance probability_measure.to_finite_measure (μ : measure α) [probability_measure μ] :
finite_measure μ :=
⟨by simp only [measure_univ, ennreal.one_lt_top]⟩
lemma probability_measure.ne_zero (μ : measure α) [probability_measure μ] : μ ≠ 0 :=
mt measure_univ_eq_zero.2 $ by simp [measure_univ]
section no_atoms
variables [has_no_atoms μ]
lemma measure_countable {s : set α} (h : countable s) : μ s = 0 :=
begin
rw [← bUnion_of_singleton s, ← le_zero_iff_eq],
refine le_trans (measure_bUnion_le h _) _,
simp
end
lemma measure_finite {s : set α} (h : s.finite) : μ s = 0 :=
measure_countable h.countable
lemma measure_finset (s : finset α) : μ ↑s = 0 :=
measure_finite s.finite_to_set
lemma insert_ae_eq_self (a : α) (s : set α) :
(insert a s : set α) =ᵐ[μ] s :=
union_ae_eq_right.2 $ measure_mono_null (diff_subset _ _) (measure_singleton _)
variables [partial_order α] {a b : α}
lemma Iio_ae_eq_Iic : Iio a =ᵐ[μ] Iic a :=
by simp only [← Iic_diff_right, diff_ae_eq_self,
measure_mono_null (set.inter_subset_right _ _) (measure_singleton a)]
lemma Ioi_ae_eq_Ici : Ioi a =ᵐ[μ] Ici a :=
@Iio_ae_eq_Iic (order_dual α) ‹_› ‹_› _ _ _
lemma Ioo_ae_eq_Ioc : Ioo a b =ᵐ[μ] Ioc a b :=
(ae_eq_refl _).inter Iio_ae_eq_Iic
lemma Ioc_ae_eq_Icc : Ioc a b =ᵐ[μ] Icc a b :=
Ioi_ae_eq_Ici.inter (ae_eq_refl _)
lemma Ioo_ae_eq_Ico : Ioo a b =ᵐ[μ] Ico a b :=
Ioi_ae_eq_Ici.inter (ae_eq_refl _)
lemma Ioo_ae_eq_Icc : Ioo a b =ᵐ[μ] Icc a b :=
Ioi_ae_eq_Ici.inter Iio_ae_eq_Iic
lemma Ico_ae_eq_Icc : Ico a b =ᵐ[μ] Icc a b :=
(ae_eq_refl _).inter Iio_ae_eq_Iic
lemma Ico_ae_eq_Ioc : Ico a b =ᵐ[μ] Ioc a b :=
Ioo_ae_eq_Ico.symm.trans Ioo_ae_eq_Ioc
end no_atoms
namespace measure
/-- A measure is called finite at filter `f` if it is finite at some set `s ∈ f`.
Equivalently, it is eventually finite at `s` in `f.lift' powerset`. -/
def finite_at_filter (μ : measure α) (f : filter α) : Prop := ∃ s ∈ f, μ s < ⊤
lemma finite_at_filter_of_finite (μ : measure α) [finite_measure μ] (f : filter α) :
μ.finite_at_filter f :=
⟨univ, univ_mem_sets, measure_lt_top μ univ⟩
lemma finite_at_bot (μ : measure α) : μ.finite_at_filter ⊥ :=
⟨∅, mem_bot_sets, by simp only [measure_empty, with_top.zero_lt_top]⟩
/-- `μ` has finite spanning sets in `C` if there is a countable sequence of sets in `C` that have
finite measures. This structure is a type, which is useful if we want to record extra properties
about the sets, such as that they are monotone.
`sigma_finite` is defined in terms of this: `μ` is σ-finite if there exists a sequence of
finite spanning sets in the collection of all measurable sets. -/
@[protect_proj, nolint has_inhabited_instance]
structure finite_spanning_sets_in (μ : measure α) (C : set (set α)) :=
(set : ℕ → set α)
(set_mem : ∀ i, set i ∈ C)
(finite : ∀ i, μ (set i) < ⊤)
(spanning : (⋃ i, set i) = univ)
end measure
open measure
/-- A measure `μ` is called σ-finite if there is a countable collection of sets
`{ A i | i ∈ ℕ }` such that `μ (A i) < ⊤` and `⋃ i, A i = s`. -/
@[class] def sigma_finite (μ : measure α) : Prop :=
nonempty (μ.finite_spanning_sets_in {s | is_measurable s})
/-- If `μ` is σ-finite it has finite spanning sets in the collection of all measurable sets. -/
def measure.to_finite_spanning_sets_in (μ : measure α) [h : sigma_finite μ] :
μ.finite_spanning_sets_in {s | is_measurable s} :=
classical.choice h
/-- A noncomputable way to get a monotone collection of sets that span `univ` and have finite
measure using `classical.some`. This definition satisfies monotonicity in addition to all other
properties in `sigma_finite`. -/
def spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) : set α :=
accumulate μ.to_finite_spanning_sets_in.set i
lemma monotone_spanning_sets (μ : measure α) [sigma_finite μ] :
monotone (spanning_sets μ) :=
monotone_accumulate
lemma is_measurable_spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) :
is_measurable (spanning_sets μ i) :=
is_measurable.Union $ λ j, is_measurable.Union_Prop $
λ hij, μ.to_finite_spanning_sets_in.set_mem j
lemma measure_spanning_sets_lt_top (μ : measure α) [sigma_finite μ] (i : ℕ) :
μ (spanning_sets μ i) < ⊤ :=
measure_bUnion_lt_top (finite_le_nat i) $ λ j _, μ.to_finite_spanning_sets_in.finite j
lemma Union_spanning_sets (μ : measure α) [sigma_finite μ] :
(⋃ i : ℕ, spanning_sets μ i) = univ :=
by simp_rw [spanning_sets, Union_accumulate, μ.to_finite_spanning_sets_in.spanning]
lemma is_countably_spanning_spanning_sets (μ : measure α) [sigma_finite μ] :
is_countably_spanning (range (spanning_sets μ)) :=
⟨spanning_sets μ, mem_range_self, Union_spanning_sets μ⟩
namespace measure
lemma supr_restrict_spanning_sets {μ : measure α} [sigma_finite μ] {s : set α}
(hs : is_measurable s) : (⨆ i, μ.restrict (spanning_sets μ i) s) = μ s :=
begin
convert (restrict_Union_apply_eq_supr (is_measurable_spanning_sets μ) _ hs).symm,
{ simp [Union_spanning_sets] },
{ exact directed_of_sup (monotone_spanning_sets μ) }
end
namespace finite_spanning_sets_in
variables {C D : set (set α)}
/-- If `μ` has finite spanning sets in `C` and `C ⊆ D` then `μ` has finite spanning sets in `D`. -/
protected def mono (h : μ.finite_spanning_sets_in C) (hC : C ⊆ D) : μ.finite_spanning_sets_in D :=
⟨h.set, λ i, hC (h.set_mem i), h.finite, h.spanning⟩
/-- If `μ` has finite spanning sets in the collection of measurable sets `C`, then `μ` is σ-finite.
-/
protected lemma sigma_finite (h : μ.finite_spanning_sets_in C) (hC : ∀ s ∈ C, is_measurable s) :
sigma_finite μ :=
⟨h.mono hC⟩
/-- An extensionality for measures. It is `ext_of_generate_from_of_Union` formulated in terms of
`finite_spanning_sets_in`. -/
protected lemma ext {ν : measure α} {C : set (set α)} (hA : ‹_› = generate_from C)
(hC : is_pi_system C) (h : μ.finite_spanning_sets_in C) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν :=
ext_of_generate_from_of_Union C _ hA hC h.spanning h.set_mem h.finite h_eq
protected lemma is_countably_spanning (h : μ.finite_spanning_sets_in C) : is_countably_spanning C :=
⟨_, h.set_mem, h.spanning⟩
end finite_spanning_sets_in
end measure
/-- Every finite measure is σ-finite. -/
@[priority 100]
instance finite_measure.to_sigma_finite (μ : measure α) [finite_measure μ] : sigma_finite μ :=
⟨⟨λ _, univ, λ _, is_measurable.univ, λ _, measure_lt_top μ _, Union_const _⟩⟩
instance restrict.sigma_finite (μ : measure α) [sigma_finite μ] (s : set α) :
sigma_finite (μ.restrict s) :=
begin
refine ⟨⟨spanning_sets μ, is_measurable_spanning_sets μ, λ i, _, Union_spanning_sets μ⟩⟩,
rw [restrict_apply (is_measurable_spanning_sets μ i)],
exact (measure_mono $ inter_subset_left _ _).trans_lt (measure_spanning_sets_lt_top μ i)
end
instance sum.sigma_finite {ι} [fintype ι] (μ : ι → measure α) [∀ i, sigma_finite (μ i)] :
sigma_finite (sum μ) :=
begin
haveI : encodable ι := (encodable.trunc_encodable_of_fintype ι).out,
have : ∀ n, is_measurable (⋂ (i : ι), spanning_sets (μ i) n) :=
λ n, is_measurable.Inter (λ i, is_measurable_spanning_sets (μ i) n),
refine ⟨⟨λ n, ⋂ i, spanning_sets (μ i) n, this, λ n, _, _⟩⟩,
{ rw [sum_apply _ (this n), tsum_fintype, ennreal.sum_lt_top_iff],
rintro i -,
exact (measure_mono $ Inter_subset _ i).trans_lt (measure_spanning_sets_lt_top (μ i) n) },
{ rw [Union_Inter_of_monotone], simp_rw [Union_spanning_sets, Inter_univ],
exact λ i, monotone_spanning_sets (μ i), }
end
instance add.sigma_finite (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] :
sigma_finite (μ + ν) :=
by { rw [← sum_cond], refine @sum.sigma_finite _ _ _ _ _ (bool.rec _ _); simpa }
/-- A measure is called locally finite if it is finite in some neighborhood of each point. -/
class locally_finite_measure [topological_space α] (μ : measure α) : Prop :=
(finite_at_nhds : ∀ x, μ.finite_at_filter (𝓝 x))
@[priority 100]
instance finite_measure.to_locally_finite_measure [topological_space α] (μ : measure α)
[finite_measure μ] :
locally_finite_measure μ :=
⟨λ x, finite_at_filter_of_finite _ _⟩
lemma measure.finite_at_nhds [topological_space α] (μ : measure α)
[locally_finite_measure μ] (x : α) :
μ.finite_at_filter (𝓝 x) :=
locally_finite_measure.finite_at_nhds x
/-- Two finite measures are equal if they are equal on the π-system generating the σ-algebra
(and `univ`). -/
lemma ext_of_generate_finite (C : set (set α)) (hA : _inst_1 = generate_from C)
(hC : is_pi_system C) {μ ν : measure α}
[finite_measure μ] [finite_measure ν] (hμν : ∀ s ∈ C, μ s = ν s) (h_univ : μ univ = ν univ) :
μ = ν :=
begin
ext1 s hs,
refine induction_on_inter hA hC (by simp) hμν _ _ hs,
{ rintros t h1t h2t, change is_measurable t at h1t, simp [measure_compl, measure_lt_top, *] },
{ rintros f h1f h2f h3f, simp [measure_Union, is_measurable.Union, *] }
end
namespace measure
namespace finite_at_filter
variables {ν : measure α} {f g : filter α}
lemma filter_mono (h : f ≤ g) : μ.finite_at_filter g → μ.finite_at_filter f :=
λ ⟨s, hs, hμ⟩, ⟨s, h hs, hμ⟩
lemma inf_of_left (h : μ.finite_at_filter f) : μ.finite_at_filter (f ⊓ g) :=
h.filter_mono inf_le_left
lemma inf_of_right (h : μ.finite_at_filter g) : μ.finite_at_filter (f ⊓ g) :=
h.filter_mono inf_le_right
@[simp] lemma inf_ae_iff : μ.finite_at_filter (f ⊓ μ.ae) ↔ μ.finite_at_filter f :=
begin
refine ⟨_, λ h, h.filter_mono inf_le_left⟩,
rintros ⟨s, ⟨t, ht, u, hu, hs⟩, hμ⟩,
suffices : μ t ≤ μ s, from ⟨t, ht, this.trans_lt hμ⟩,
exact measure_mono_ae (mem_sets_of_superset hu (λ x hu ht, hs ⟨ht, hu⟩))
end
alias inf_ae_iff ↔ measure_theory.measure.finite_at_filter.of_inf_ae _
lemma filter_mono_ae (h : f ⊓ μ.ae ≤ g) (hg : μ.finite_at_filter g) : μ.finite_at_filter f :=
inf_ae_iff.1 (hg.filter_mono h)
protected lemma measure_mono (h : μ ≤ ν) : ν.finite_at_filter f → μ.finite_at_filter f :=
λ ⟨s, hs, hν⟩, ⟨s, hs, (measure.le_iff'.1 h s).trans_lt hν⟩
@[mono] protected lemma mono (hf : f ≤ g) (hμ : μ ≤ ν) :
ν.finite_at_filter g → μ.finite_at_filter f :=
λ h, (h.filter_mono hf).measure_mono hμ
protected lemma eventually (h : μ.finite_at_filter f) : ∀ᶠ s in f.lift' powerset, μ s < ⊤ :=
(eventually_lift'_powerset' $ λ s t hst ht, (measure_mono hst).trans_lt ht).2 h
lemma filter_sup : μ.finite_at_filter f → μ.finite_at_filter g → μ.finite_at_filter (f ⊔ g) :=
λ ⟨s, hsf, hsμ⟩ ⟨t, htg, htμ⟩,
⟨s ∪ t, union_mem_sup hsf htg, (measure_union_le s t).trans_lt (ennreal.add_lt_top.2 ⟨hsμ, htμ⟩)⟩
end finite_at_filter
lemma finite_at_nhds_within [topological_space α] (μ : measure α) [locally_finite_measure μ]
(x : α) (s : set α) :
μ.finite_at_filter (𝓝[s] x) :=
(finite_at_nhds μ x).inf_of_left
@[simp] lemma finite_at_principal {s : set α} : μ.finite_at_filter (𝓟 s) ↔ μ s < ⊤ :=
⟨λ ⟨t, ht, hμ⟩, (measure_mono ht).trans_lt hμ, λ h, ⟨s, mem_principal_self s, h⟩⟩
/-! ### Subtraction of measures -/
/-- The measure `μ - ν` is defined to be the least measure `τ` such that `μ ≤ τ + ν`.
It is the equivalent of `(μ - ν) ⊔ 0` if `μ` and `ν` were signed measures.
Compare with `ennreal.has_sub`.
Specifically, note that if you have `α = {1,2}`, and `μ {1} = 2`, `μ {2} = 0`, and
`ν {2} = 2`, `ν {1} = 0`, then `(μ - ν) {1, 2} = 2`. However, if `μ ≤ ν`, and
`ν univ ≠ ⊤`, then `(μ - ν) + ν = μ`. -/
noncomputable instance has_sub {α : Type*} [measurable_space α] : has_sub (measure α) :=
⟨λ μ ν, Inf {τ | μ ≤ τ + ν} ⟩
section measure_sub
variables {ν : measure_theory.measure α}
lemma sub_def : μ - ν = Inf {d | μ ≤ d + ν} := rfl
lemma sub_eq_zero_of_le (h : μ ≤ ν) : μ - ν = 0 :=
begin
rw [← le_zero_iff_eq', measure.sub_def],
apply @Inf_le (measure α) _ _,
simp [h],
end
/-- This application lemma only works in special circumstances. Given knowledge of
when `μ ≤ ν` and `ν ≤ μ`, a more general application lemma can be written. -/
lemma sub_apply {s : set α} [finite_measure ν] (h₁ : is_measurable s) (h₂ : ν ≤ μ) :
(μ - ν) s = μ s - ν s :=
begin
-- We begin by defining `measure_sub`, which will be equal to `(μ - ν)`.
let measure_sub : measure α := @measure_theory.measure.of_measurable α _
(λ (t : set α) (h_t_is_measurable : is_measurable t), (μ t - ν t))
begin
simp
end
begin
intros g h_meas h_disj, simp only, rw ennreal.tsum_sub,
repeat { rw ← measure_theory.measure_Union h_disj h_meas },
apply measure_theory.measure_lt_top, intro i, apply h₂, apply h_meas
end,
-- Now, we demonstrate `μ - ν = measure_sub`, and apply it.
begin
have h_measure_sub_add : (ν + measure_sub = μ),
{ ext t h_t_is_measurable,
simp only [pi.add_apply, coe_add],
rw [measure_theory.measure.of_measurable_apply _ h_t_is_measurable, add_comm,
ennreal.sub_add_cancel_of_le (h₂ t h_t_is_measurable)] },
have h_measure_sub_eq : (μ - ν) = measure_sub,
{ rw measure_theory.measure.sub_def, apply le_antisymm,
{ apply @Inf_le (measure α) (measure.complete_lattice), simp [le_refl, add_comm, h_measure_sub_add] },
apply @le_Inf (measure α) (measure.complete_lattice),
intros d h_d, rw [← h_measure_sub_add, mem_set_of_eq, add_comm d] at h_d,
apply measure.le_of_add_le_add_left h_d },
rw h_measure_sub_eq,
apply measure.of_measurable_apply _ h₁,
end
end
lemma sub_add_cancel_of_le [finite_measure ν] (h₁ : ν ≤ μ) : μ - ν + ν = μ :=
begin
ext s h_s_meas,
rw [add_apply, sub_apply h_s_meas h₁, ennreal.sub_add_cancel_of_le (h₁ s h_s_meas)],
end
end measure_sub
end measure
end measure_theory
open measure_theory measure_theory.measure
section is_complete
/-- A measure is complete if every null set is also measurable.
A null set is a subset of a measurable set with measure `0`.
Since every measure is defined as a special case of an outer measure, we can more simply state
that a set `s` is null if `μ s = 0`. -/
@[class] def measure_theory.measure.is_complete {α} {_:measurable_space α} (μ : measure α) : Prop :=
∀ s, μ s = 0 → is_measurable s
variables {α : Type*} [measurable_space α] (μ : measure α)
/-- A set is null measurable if it is the union of a null set and a measurable set. -/
def is_null_measurable (s : set α) : Prop :=
∃ t z, s = t ∪ z ∧ is_measurable t ∧ μ z = 0
theorem is_null_measurable_iff {μ : measure α} {s : set α} :
is_null_measurable μ s ↔
∃ t, t ⊆ s ∧ is_measurable t ∧ μ (s \ t) = 0 :=
begin
split,
{ rintro ⟨t, z, rfl, ht, hz⟩,
refine ⟨t, set.subset_union_left _ _, ht, measure_mono_null _ hz⟩,
simp [union_diff_left, diff_subset] },
{ rintro ⟨t, st, ht, hz⟩,
exact ⟨t, _, (union_diff_cancel st).symm, ht, hz⟩ }
end
theorem is_null_measurable_measure_eq {μ : measure α} {s t : set α}
(st : t ⊆ s) (hz : μ (s \ t) = 0) : μ s = μ t :=
begin
refine le_antisymm _ (measure_mono st),
have := measure_union_le t (s \ t),
rw [union_diff_cancel st, hz] at this, simpa
end
theorem is_measurable.is_null_measurable
{s : set α} (hs : is_measurable s) : is_null_measurable μ s :=
⟨s, ∅, by simp, hs, μ.empty⟩
theorem is_null_measurable_of_complete [c : μ.is_complete]
{s : set α} : is_null_measurable μ s ↔ is_measurable s :=
⟨by rintro ⟨t, z, rfl, ht, hz⟩; exact
is_measurable.union ht (c _ hz),
λ h, h.is_null_measurable _⟩
variables {μ}
theorem is_null_measurable.union_null {s z : set α}
(hs : is_null_measurable μ s) (hz : μ z = 0) :
is_null_measurable μ (s ∪ z) :=
begin
rcases hs with ⟨t, z', rfl, ht, hz'⟩,
exact ⟨t, z' ∪ z, set.union_assoc _ _ _, ht, le_zero_iff_eq.1
(le_trans (measure_union_le _ _) $ by simp [hz, hz'])⟩
end
theorem null_is_null_measurable {z : set α}
(hz : μ z = 0) : is_null_measurable μ z :=
by simpa using (is_measurable.empty.is_null_measurable _).union_null hz
theorem is_null_measurable.Union_nat {s : ℕ → set α}
(hs : ∀ i, is_null_measurable μ (s i)) :
is_null_measurable μ (Union s) :=
begin
choose t ht using assume i, is_null_measurable_iff.1 (hs i),
simp [forall_and_distrib] at ht,
rcases ht with ⟨st, ht, hz⟩,
refine is_null_measurable_iff.2
⟨Union t, Union_subset_Union st, is_measurable.Union ht,
measure_mono_null _ (measure_Union_null hz)⟩,
rw [diff_subset_iff, ← Union_union_distrib],
exact Union_subset_Union (λ i, by rw ← diff_subset_iff)
end
theorem is_measurable.diff_null {s z : set α}
(hs : is_measurable s) (hz : μ z = 0) :
is_null_measurable μ (s \ z) :=
begin
rw measure_eq_infi at hz,
choose f hf using show ∀ q : {q:ℚ//q>0}, ∃ t:set α,
z ⊆ t ∧ is_measurable t ∧ μ t < (nnreal.of_real q.1 : ennreal),
{ rintro ⟨ε, ε0⟩,
have : 0 < (nnreal.of_real ε : ennreal), { simpa using ε0 },
rw ← hz at this, simpa [infi_lt_iff] },
refine is_null_measurable_iff.2 ⟨s \ Inter f,
diff_subset_diff_right (subset_Inter (λ i, (hf i).1)),
hs.diff (is_measurable.Inter (λ i, (hf i).2.1)),
measure_mono_null _ (le_zero_iff_eq.1 $ le_of_not_lt $ λ h, _)⟩,
{ exact Inter f },
{ rw [diff_subset_iff, diff_union_self],
exact subset.trans (diff_subset _ _) (subset_union_left _ _) },
rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨ε, ε0', ε0, h⟩,
simp at ε0,
apply not_le_of_lt (lt_trans (hf ⟨ε, ε0⟩).2.2 h),
exact measure_mono (Inter_subset _ _)
end
theorem is_null_measurable.diff_null {s z : set α}
(hs : is_null_measurable μ s) (hz : μ z = 0) :
is_null_measurable μ (s \ z) :=
begin
rcases hs with ⟨t, z', rfl, ht, hz'⟩,
rw [set.union_diff_distrib],
exact (ht.diff_null hz).union_null (measure_mono_null (diff_subset _ _) hz')
end
theorem is_null_measurable.compl {s : set α}
(hs : is_null_measurable μ s) :
is_null_measurable μ sᶜ :=
begin
rcases hs with ⟨t, z, rfl, ht, hz⟩,
rw compl_union,
exact ht.compl.diff_null hz
end
/-- The measurable space of all null measurable sets. -/
def null_measurable {α : Type*} [measurable_space α]
(μ : measure α) : measurable_space α :=
{ is_measurable' := is_null_measurable μ,
is_measurable_empty := is_measurable.empty.is_null_measurable _,
is_measurable_compl := λ s hs, hs.compl,
is_measurable_Union := λ f, is_null_measurable.Union_nat }
/-- Given a measure we can complete it to a (complete) measure on all null measurable sets. -/
def completion {α : Type*} [measurable_space α] (μ : measure α) :
@measure_theory.measure α (null_measurable μ) :=
{ to_outer_measure := μ.to_outer_measure,
m_Union := λ s hs hd, show μ (Union s) = ∑' i, μ (s i), begin
choose t ht using assume i, is_null_measurable_iff.1 (hs i),
simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩,
rw is_null_measurable_measure_eq (Union_subset_Union st),
{ rw measure_Union _ ht,
{ congr, funext i,
exact (is_null_measurable_measure_eq (st i) (hz i)).symm },
{ rintro i j ij x ⟨h₁, h₂⟩,
exact hd i j ij ⟨st i h₁, st j h₂⟩ } },
{ refine measure_mono_null _ (measure_Union_null hz),
rw [diff_subset_iff, ← Union_union_distrib],
exact Union_subset_Union (λ i, by rw ← diff_subset_iff) }
end,
trimmed := begin
letI := null_measurable μ,
refine le_antisymm (λ s, _) (outer_measure.le_trim _),
rw outer_measure.trim_eq_infi,
dsimp,
clear _inst,
resetI,
rw measure_eq_infi s,
exact infi_le_infi (λ t, infi_le_infi $ λ st,
infi_le_infi2 $ λ ht, ⟨ht.is_null_measurable _, le_refl _⟩)
end }
instance completion.is_complete {α : Type*} [measurable_space α] (μ : measure α) :
(completion μ).is_complete :=
λ z hz, null_is_null_measurable hz
end is_complete
namespace measure_theory
/-- A measure space is a measurable space equipped with a
measure, referred to as `volume`. -/
class measure_space (α : Type*) extends measurable_space α :=
(volume : measure α)
export measure_space (volume)
/-- `volume` is the canonical measure on `α`. -/
add_decl_doc volume
section measure_space
variables {α : Type*} {ι : Type*} [measure_space α] {s₁ s₂ : set α}
notation `∀ᵐ` binders `, ` r:(scoped P, filter.eventually P (measure.ae volume)) := r
/-- The tactic `exact volume`, to be used in optional (`auto_param`) arguments. -/
meta def volume_tac : tactic unit := `[exact measure_theory.measure_space.volume]
end measure_space
end measure_theory
namespace is_compact
variables {α : Type*} [topological_space α] [measurable_space α] {μ : measure α} {s : set α}
lemma finite_measure_of_nhds_within (hs : is_compact s) :
(∀ a ∈ s, μ.finite_at_filter (𝓝[s] a)) → μ s < ⊤ :=
by simpa only [← measure.compl_mem_cofinite, measure.finite_at_filter]
using hs.compl_mem_sets_of_nhds_within
lemma finite_measure [locally_finite_measure μ] (hs : is_compact s) : μ s < ⊤ :=
hs.finite_measure_of_nhds_within $ λ a ha, μ.finite_at_nhds_within _ _
lemma measure_zero_of_nhds_within (hs : is_compact s) :
(∀ a ∈ s, ∃ t ∈ 𝓝[s] a, μ t = 0) → μ s = 0 :=
by simpa only [← compl_mem_ae_iff] using hs.compl_mem_sets_of_nhds_within
end is_compact
lemma metric.bounded.finite_measure {α : Type*} [metric_space α] [proper_space α]
[measurable_space α] {μ : measure α} [locally_finite_measure μ] {s : set α}
(hs : metric.bounded s) :
μ s < ⊤ :=
(measure_mono subset_closure).trans_lt (metric.compact_iff_closed_bounded.2
⟨is_closed_closure, metric.bounded_closure_of_bounded hs⟩).finite_measure
|
4374d608f9057111b0c210f2cee1363667e1b846 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/topology/uniform_space/compare_reals.lean | 3cd39634023f57baea64ea4f6e07f6746df157b2 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 4,752 | 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.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 lattice 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 : ℚ → ℚ) = 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] 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],
refine lt_trans _ h'',
exact_mod_cast hab }
end
/-- Cauchy reals packaged as a completion of ℚ using the absolute value route. -/
noncomputable
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 uniform_embedding_of_rat.to_uniform_inducing },
dense := dense_embedding_of_rat.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] def Q := ℚ
instance : uniform_space Q := is_absolute_value.uniform_space (abs : ℚ → ℚ)
/-- Real numbers constructed as in Bourbaki. -/
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 equivalence 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
|
e24e80567503387ef12b1491f2101da7125f463a | 4727251e0cd73359b15b664c3170e5d754078599 | /src/analysis/calculus/affine_map.lean | 8aee84800f4ce2415711665e93688492efcbff84 | [
"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 | 894 | 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.continuous_affine_map
import analysis.calculus.cont_diff
/-!
# Smooth affine maps
This file contains results about smoothness of affine maps.
## Main definitions:
* `continuous_affine_map.cont_diff`: a continuous affine map is smooth
-/
namespace continuous_affine_map
variables {𝕜 V W : Type*} [nondiscrete_normed_field 𝕜]
variables [normed_group V] [normed_space 𝕜 V]
variables [normed_group W] [normed_space 𝕜 W]
/-- A continuous affine map between normed vector spaces is smooth. -/
lemma cont_diff {n : with_top ℕ} (f : V →A[𝕜] W) :
cont_diff 𝕜 n f :=
begin
rw f.decomp,
apply f.cont_linear.cont_diff.add,
simp only,
exact cont_diff_const,
end
end continuous_affine_map
|
8d529da59fb76f963387c2b0f5cbc9fa64aeb4ae | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/ring/inj_surj.lean | d52b98aed2d53cbf09cf55ff5288e5296ef6513f | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 22,091 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Yury Kudryashov, Neil Strickland
-/
import algebra.ring.defs
import algebra.opposites
import algebra.group_with_zero.inj_surj
/-!
# Pulling back rings along injective maps, and pushing them forward along surjective maps.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> https://github.com/leanprover-community/mathlib4/pull/734
> Any changes to this file require a corresponding PR to mathlib4.
-/
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x}
open function
/-!
### `distrib` class
-/
/-- Pullback a `distrib` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.distrib {S} [has_mul R] [has_add R] [distrib S]
(f : R → S) (hf : injective f) (add : ∀ x y, f (x + y) = f x + f y)
(mul : ∀ x y, f (x * y) = f x * f y) :
distrib R :=
{ mul := (*),
add := (+),
left_distrib := λ x y z, hf $ by simp only [*, left_distrib],
right_distrib := λ x y z, hf $ by simp only [*, right_distrib] }
/-- Pushforward a `distrib` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.distrib {S} [distrib R] [has_add S] [has_mul S]
(f : R → S) (hf : surjective f) (add : ∀ x y, f (x + y) = f x + f y)
(mul : ∀ x y, f (x * y) = f x * f y) :
distrib S :=
{ mul := (*),
add := (+),
left_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, left_distrib],
right_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, right_distrib] }
section injective_surjective_maps
/-!
### Semirings
-/
variables [has_zero β] [has_add β] [has_mul β] [has_smul ℕ β]
/-- Pullback a `non_unital_non_assoc_semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_unital_non_assoc_semiring
{α : Type u} [non_unital_non_assoc_semiring α]
(f : β → α) (hf : injective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) :
non_unital_non_assoc_semiring β :=
{ .. hf.mul_zero_class f zero mul, .. hf.add_comm_monoid f zero add nsmul, .. hf.distrib f add mul }
/-- Pullback a `non_unital_semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_unital_semiring
{α : Type u} [non_unital_semiring α]
(f : β → α) (hf : injective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) :
non_unital_semiring β :=
{ .. hf.non_unital_non_assoc_semiring f zero add mul nsmul, .. hf.semigroup_with_zero f zero mul }
/-- Pullback a `non_assoc_semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_assoc_semiring
{α : Type u} [non_assoc_semiring α]
{β : Type v} [has_zero β] [has_one β] [has_mul β] [has_add β]
[has_smul ℕ β] [has_nat_cast β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x)
(nat_cast : ∀ n : ℕ, f n = n) :
non_assoc_semiring β :=
{ .. hf.add_monoid_with_one f zero one add nsmul nat_cast,
.. hf.non_unital_non_assoc_semiring f zero add mul nsmul,
.. hf.mul_one_class f one mul }
/-- Pullback a `semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.semiring
{α : Type u} [semiring α]
{β : Type v} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_pow β ℕ]
[has_smul ℕ β] [has_nat_cast β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(nat_cast : ∀ n : ℕ, f n = n) :
semiring β :=
{ .. hf.non_assoc_semiring f zero one add mul nsmul nat_cast,
.. hf.monoid_with_zero f zero one mul npow,
.. hf.distrib f add mul }
/-- Pushforward a `non_unital_non_assoc_semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_unital_non_assoc_semiring
{α : Type u} [non_unital_non_assoc_semiring α]
(f : α → β) (hf : surjective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) :
non_unital_non_assoc_semiring β :=
{ .. hf.mul_zero_class f zero mul, .. hf.add_comm_monoid f zero add nsmul, .. hf.distrib f add mul }
/-- Pushforward a `non_unital_semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_unital_semiring
{α : Type u} [non_unital_semiring α]
(f : α → β) (hf : surjective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) :
non_unital_semiring β :=
{ .. hf.non_unital_non_assoc_semiring f zero add mul nsmul, .. hf.semigroup_with_zero f zero mul }
/-- Pushforward a `non_assoc_semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_assoc_semiring
{α : Type u} [non_assoc_semiring α]
{β : Type v} [has_zero β] [has_one β] [has_add β] [has_mul β]
[has_smul ℕ β] [has_nat_cast β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x)
(nat_cast : ∀ n : ℕ, f n = n) :
non_assoc_semiring β :=
{ .. hf.add_monoid_with_one f zero one add nsmul nat_cast,
.. hf.non_unital_non_assoc_semiring f zero add mul nsmul, .. hf.mul_one_class f one mul }
/-- Pushforward a `semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.semiring
{α : Type u} [semiring α]
{β : Type v} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_pow β ℕ]
[has_smul ℕ β] [has_nat_cast β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(nat_cast : ∀ n : ℕ, f n = n) :
semiring β :=
{ .. hf.non_assoc_semiring f zero one add mul nsmul nat_cast,
.. hf.monoid_with_zero f zero one mul npow, .. hf.add_comm_monoid f zero add nsmul,
.. hf.distrib f add mul }
end injective_surjective_maps
section non_unital_comm_semiring
variables [non_unital_comm_semiring α] [non_unital_comm_semiring β] {a b c : α}
/-- Pullback a `non_unital_semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_unital_comm_semiring [has_zero γ] [has_add γ] [has_mul γ]
[has_smul ℕ γ] (f : γ → α) (hf : injective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) :
non_unital_comm_semiring γ :=
{ .. hf.non_unital_semiring f zero add mul nsmul, .. hf.comm_semigroup f mul }
/-- Pushforward a `non_unital_semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_unital_comm_semiring [has_zero γ] [has_add γ] [has_mul γ]
[has_smul ℕ γ] (f : α → γ) (hf : surjective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) :
non_unital_comm_semiring γ :=
{ .. hf.non_unital_semiring f zero add mul nsmul, .. hf.comm_semigroup f mul }
end non_unital_comm_semiring
section comm_semiring
variables [comm_semiring α] [comm_semiring β] {a b c : α}
/-- Pullback a `semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.comm_semiring
[has_zero γ] [has_one γ] [has_add γ] [has_mul γ] [has_smul ℕ γ] [has_nat_cast γ]
[has_pow γ ℕ] (f : γ → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(nat_cast : ∀ n : ℕ, f n = n) :
comm_semiring γ :=
{ .. hf.semiring f zero one add mul nsmul npow nat_cast, .. hf.comm_semigroup f mul }
/-- Pushforward a `semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.comm_semiring
[has_zero γ] [has_one γ] [has_add γ] [has_mul γ] [has_smul ℕ γ] [has_nat_cast γ]
[has_pow γ ℕ] (f : α → γ) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(nat_cast : ∀ n : ℕ, f n = n) :
comm_semiring γ :=
{ .. hf.semiring f zero one add mul nsmul npow nat_cast, .. hf.comm_semigroup f mul }
end comm_semiring
section has_distrib_neg
section has_mul
variables [has_mul α] [has_distrib_neg α]
/-- A type endowed with `-` and `*` has distributive negation, if it admits an injective map that
preserves `-` and `*` to a type which has distributive negation. -/
@[reducible] -- See note [reducible non-instances]
protected def function.injective.has_distrib_neg [has_neg β] [has_mul β] (f : β → α)
(hf : injective f) (neg : ∀ a, f (-a) = -f a) (mul : ∀ a b, f (a * b) = f a * f b) :
has_distrib_neg β :=
{ neg_mul := λ x y, hf $ by erw [neg, mul, neg, neg_mul, mul],
mul_neg := λ x y, hf $ by erw [neg, mul, neg, mul_neg, mul],
..hf.has_involutive_neg _ neg, ..‹has_mul β› }
/-- A type endowed with `-` and `*` has distributive negation, if it admits a surjective map that
preserves `-` and `*` from a type which has distributive negation. -/
@[reducible] -- See note [reducible non-instances]
protected def function.surjective.has_distrib_neg [has_neg β] [has_mul β] (f : α → β)
(hf : surjective f) (neg : ∀ a, f (-a) = -f a) (mul : ∀ a b, f (a * b) = f a * f b) :
has_distrib_neg β :=
{ neg_mul := hf.forall₂.2 $ λ x y, by { erw [←neg, ← mul, neg_mul, neg, mul], refl },
mul_neg := hf.forall₂.2 $ λ x y, by { erw [←neg, ← mul, mul_neg, neg, mul], refl },
..hf.has_involutive_neg _ neg, ..‹has_mul β› }
namespace add_opposite
instance : has_distrib_neg αᵃᵒᵖ := unop_injective.has_distrib_neg _ unop_neg unop_mul
end add_opposite
end has_mul
end has_distrib_neg
/-!
### Rings
-/
section non_unital_non_assoc_ring
variables [non_unital_non_assoc_ring α]
/-- Pullback a `non_unital_non_assoc_ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_unital_non_assoc_ring
[has_zero β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_smul ℕ β] [has_smul ℤ β]
(f : β → α) (hf : injective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) :
non_unital_non_assoc_ring β :=
{ .. hf.add_comm_group f zero add neg sub nsmul zsmul, ..hf.mul_zero_class f zero mul,
.. hf.distrib f add mul }
/-- Pushforward a `non_unital_non_assoc_ring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_unital_non_assoc_ring
[has_zero β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_smul ℕ β] [has_smul ℤ β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) :
non_unital_non_assoc_ring β :=
{ .. hf.add_comm_group f zero add neg sub nsmul zsmul, .. hf.mul_zero_class f zero mul,
.. hf.distrib f add mul }
end non_unital_non_assoc_ring
section non_unital_ring
variables [non_unital_ring α]
/-- Pullback a `non_unital_ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_unital_ring
[has_zero β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_smul ℕ β] [has_smul ℤ β]
(f : β → α) (hf : injective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (gsmul : ∀ x (n : ℤ), f (n • x) = n • f x) :
non_unital_ring β :=
{ .. hf.add_comm_group f zero add neg sub nsmul gsmul, ..hf.mul_zero_class f zero mul,
.. hf.distrib f add mul, .. hf.semigroup f mul }
/-- Pushforward a `non_unital_ring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_unital_ring
[has_zero β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_smul ℕ β] [has_smul ℤ β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (gsmul : ∀ x (n : ℤ), f (n • x) = n • f x) :
non_unital_ring β :=
{ .. hf.add_comm_group f zero add neg sub nsmul gsmul, .. hf.mul_zero_class f zero mul,
.. hf.distrib f add mul, .. hf.semigroup f mul }
end non_unital_ring
section non_assoc_ring
variables [non_assoc_ring α]
/-- Pullback a `non_assoc_ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_assoc_ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
[has_smul ℕ β] [has_smul ℤ β] [has_nat_cast β] [has_int_cast β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (gsmul : ∀ x (n : ℤ), f (n • x) = n • f x)
(nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) :
non_assoc_ring β :=
{ .. hf.add_comm_group f zero add neg sub nsmul gsmul,
.. hf.add_group_with_one f zero one add neg sub nsmul gsmul nat_cast int_cast,
.. hf.mul_zero_class f zero mul, .. hf.distrib f add mul,
.. hf.mul_one_class f one mul }
/-- Pushforward a `non_unital_ring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_assoc_ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
[has_smul ℕ β] [has_smul ℤ β] [has_nat_cast β] [has_int_cast β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (gsmul : ∀ x (n : ℤ), f (n • x) = n • f x)
(nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) :
non_assoc_ring β :=
{ .. hf.add_comm_group f zero add neg sub nsmul gsmul, .. hf.mul_zero_class f zero mul,
.. hf.add_group_with_one f zero one add neg sub nsmul gsmul nat_cast int_cast,
.. hf.distrib f add mul, .. hf.mul_one_class f one mul }
end non_assoc_ring
section ring
variables [ring α] {a b c d e : α}
/-- Pullback a `ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
[has_smul ℕ β] [has_smul ℤ β] [has_pow β ℕ] [has_nat_cast β] [has_int_cast β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x)
(npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) :
ring β :=
{ .. hf.add_group_with_one f zero one add neg sub nsmul zsmul nat_cast int_cast,
.. hf.add_comm_group f zero add neg sub nsmul zsmul,
.. hf.monoid f one mul npow, .. hf.distrib f add mul }
/-- Pushforward a `ring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
[has_smul ℕ β] [has_smul ℤ β] [has_pow β ℕ] [has_nat_cast β] [has_int_cast β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x)
(npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) :
ring β :=
{ .. hf.add_group_with_one f zero one add neg sub nsmul zsmul nat_cast int_cast,
.. hf.add_comm_group f zero add neg sub nsmul zsmul,
.. hf.monoid f one mul npow, .. hf.distrib f add mul }
end ring
section non_unital_comm_ring
variables [non_unital_comm_ring α] {a b c : α}
/-- Pullback a `comm_ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_unital_comm_ring
[has_zero β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
[has_smul ℕ β] [has_smul ℤ β]
(f : β → α) (hf : injective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) :
non_unital_comm_ring β :=
{ .. hf.non_unital_ring f zero add mul neg sub nsmul zsmul, .. hf.comm_semigroup f mul }
/-- Pushforward a `non_unital_comm_ring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_unital_comm_ring
[has_zero β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
[has_smul ℕ β] [has_smul ℤ β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) :
non_unital_comm_ring β :=
{ .. hf.non_unital_ring f zero add mul neg sub nsmul zsmul, .. hf.comm_semigroup f mul }
end non_unital_comm_ring
section comm_ring
variables [comm_ring α] {a b c : α}
/-- Pullback a `comm_ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.comm_ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
[has_smul ℕ β] [has_smul ℤ β] [has_pow β ℕ] [has_nat_cast β] [has_int_cast β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x)
(npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) :
comm_ring β :=
{ .. hf.ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast,
.. hf.comm_semigroup f mul }
/-- Pushforward a `comm_ring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.comm_ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
[has_smul ℕ β] [has_smul ℤ β] [has_pow β ℕ] [has_nat_cast β] [has_int_cast β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y)
(nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x)
(npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) :
comm_ring β :=
{ .. hf.ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast,
.. hf.comm_semigroup f mul }
end comm_ring
|
0f64f36d4466e9c1ba27aa5ac5f1a70902ddd89f | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/measure_theory/group/action.lean | 380b5ada642ecf50cf39e4f1f0ee773901fc2eba | [
"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,222 | lean | /-
Copyright (c) 2021 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import measure_theory.group.measurable_equiv
import measure_theory.measure.regular
import dynamics.ergodic.measure_preserving
import dynamics.minimal
/-!
# Measures invariant under group actions
A measure `μ : measure α` is said to be *invariant* under an action of a group `G` if scalar
multiplication by `c : G` is a measure preserving map for all `c`. In this file we define a
typeclass for measures invariant under action of an (additive or multiplicative) group and prove
some basic properties of such measures.
-/
open_locale ennreal nnreal pointwise topological_space
open measure_theory measure_theory.measure set function
namespace measure_theory
variables {G M α : Type*}
/-- A measure `μ : measure α` is invariant under an additive action of `M` on `α` if for any
measurable set `s : set α` and `c : M`, the measure of its preimage under `λ x, c +ᵥ x` is equal to
the measure of `s`. -/
class vadd_invariant_measure (M α : Type*) [has_vadd M α] {_ : measurable_space α}
(μ : measure α) : Prop :=
(measure_preimage_vadd [] : ∀ (c : M) ⦃s : set α⦄, measurable_set s → μ ((λ x, c +ᵥ x) ⁻¹' s) = μ s)
/-- A measure `μ : measure α` is invariant under a multiplicative action of `M` on `α` if for any
measurable set `s : set α` and `c : M`, the measure of its preimage under `λ x, c • x` is equal to
the measure of `s`. -/
@[to_additive] class smul_invariant_measure (M α : Type*) [has_scalar M α] {_ : measurable_space α}
(μ : measure α) : Prop :=
(measure_preimage_smul [] : ∀ (c : M) ⦃s : set α⦄, measurable_set s → μ ((λ x, c • x) ⁻¹' s) = μ s)
namespace smul_invariant_measure
@[to_additive] instance zero [measurable_space α] [has_scalar M α] : smul_invariant_measure M α 0 :=
⟨λ _ _ _, rfl⟩
variables [has_scalar M α] {m : measurable_space α} {μ ν : measure α}
@[to_additive] instance add [smul_invariant_measure M α μ] [smul_invariant_measure M α ν] :
smul_invariant_measure M α (μ + ν) :=
⟨λ c s hs, show _ + _ = _ + _,
from congr_arg2 (+) (measure_preimage_smul μ c hs) (measure_preimage_smul ν c hs)⟩
@[to_additive] instance smul [smul_invariant_measure M α μ] (c : ℝ≥0∞) :
smul_invariant_measure M α (c • μ) :=
⟨λ a s hs, show c • _ = c • _, from congr_arg ((•) c) (measure_preimage_smul μ a hs)⟩
@[to_additive] instance smul_nnreal [smul_invariant_measure M α μ] (c : ℝ≥0) :
smul_invariant_measure M α (c • μ) :=
smul_invariant_measure.smul c
end smul_invariant_measure
variables (G) {m : measurable_space α} [group G] [mul_action G α] [measurable_space G]
[has_measurable_smul G α] (c : G) (μ : measure α)
/-- Equivalent definitions of a measure invariant under a multiplicative action of a group.
- 0: `smul_invariant_measure G α μ`;
- 1: for every `c : G` and a measurable set `s`, the measure of the preimage of `s` under scalar
multiplication by `c` is equal to the measure of `s`;
- 2: for every `c : G` and a measurable set `s`, the measure of the image `c • s` of `s` under
scalar multiplication by `c` is equal to the measure of `s`;
- 3, 4: properties 2, 3 for any set, including non-measurable ones;
- 5: for any `c : G`, scalar multiplication by `c` maps `μ` to `μ`;
- 6: for any `c : G`, scalar multiplication by `c` is a measure preserving map. -/
@[to_additive] lemma smul_invariant_measure_tfae :
tfae [smul_invariant_measure G α μ,
∀ (c : G) s, measurable_set s → μ (((•) c) ⁻¹' s) = μ s,
∀ (c : G) s, measurable_set s → μ (c • s) = μ s,
∀ (c : G) s, μ (((•) c) ⁻¹' s) = μ s,
∀ (c : G) s, μ (c • s) = μ s,
∀ c : G, measure.map ((•) c) μ = μ,
∀ c : G, measure_preserving ((•) c) μ μ] :=
begin
tfae_have : 1 ↔ 2, from ⟨λ h, h.1, λ h, ⟨h⟩⟩,
tfae_have : 2 → 6,
from λ H c, ext (λ s hs, by rw [map_apply (measurable_const_smul c) hs, H _ _ hs]),
tfae_have : 6 → 7, from λ H c, ⟨measurable_const_smul c, H c⟩,
tfae_have : 7 → 4, from λ H c, (H c).measure_preimage_emb (measurable_embedding_const_smul c),
tfae_have : 4 → 5, from λ H c s, by { rw [← preimage_smul_inv], apply H },
tfae_have : 5 → 3, from λ H c s hs, H c s,
tfae_have : 3 → 2, { intros H c s hs, rw preimage_smul, exact H c⁻¹ s hs },
tfae_finish
end
/-- Equivalent definitions of a measure invariant under an additive action of a group.
- 0: `vadd_invariant_measure G α μ`;
- 1: for every `c : G` and a measurable set `s`, the measure of the preimage of `s` under
vector addition `(+ᵥ) c` is equal to the measure of `s`;
- 2: for every `c : G` and a measurable set `s`, the measure of the image `c +ᵥ s` of `s` under
vector addition `(+ᵥ) c` is equal to the measure of `s`;
- 3, 4: properties 2, 3 for any set, including non-measurable ones;
- 5: for any `c : G`, vector addition of `c` maps `μ` to `μ`;
- 6: for any `c : G`, vector addition of `c` is a measure preserving map. -/
add_decl_doc vadd_invariant_measure_tfae
variables {G} [smul_invariant_measure G α μ]
@[to_additive] lemma measure_preserving_smul : measure_preserving ((•) c) μ μ :=
((smul_invariant_measure_tfae G μ).out 0 6).mp ‹_› c
@[simp, to_additive] lemma map_smul : map ((•) c) μ = μ :=
(measure_preserving_smul c μ).map_eq
@[simp, to_additive] lemma measure_preimage_smul (s : set α) : μ ((•) c ⁻¹' s) = μ s :=
((smul_invariant_measure_tfae G μ).out 0 3).mp ‹_› c s
@[simp, to_additive] lemma measure_smul_set (s : set α) : μ (c • s) = μ s :=
((smul_invariant_measure_tfae G μ).out 0 4).mp ‹_› c s
section is_minimal
variables (G) {μ} [topological_space G] [topological_space α] [has_continuous_smul G α]
[mul_action.is_minimal G α] {K U : set α}
/-- If measure `μ` is invariant under a group action and is nonzero on a compact set `K`, then it is
positive on any nonempty open set. In case of a regular measure, one can assume `μ ≠ 0` instead of
`μ K ≠ 0`, see `measure_theory.measure_is_open_pos_of_smul_invariant_of_ne_zero`. -/
@[to_additive] lemma measure_is_open_pos_of_smul_invariant_of_compact_ne_zero (hK : is_compact K)
(hμK : μ K ≠ 0) (hU : is_open U) (hne : U.nonempty) : 0 < μ U :=
let ⟨t, ht⟩ := hK.exists_finite_cover_smul G hU hne
in pos_iff_ne_zero.2 $ λ hμU, hμK $ measure_mono_null ht $
(measure_bUnion_null_iff t.countable_to_set).2 $ λ _ _, by rwa measure_smul_set
/-- If measure `μ` is invariant under an additive group action and is nonzero on a compact set `K`,
then it is positive on any nonempty open set. In case of a regular measure, one can assume `μ ≠ 0`
instead of `μ K ≠ 0`, see `measure_theory.measure_is_open_pos_of_vadd_invariant_of_ne_zero`. -/
add_decl_doc measure_is_open_pos_of_vadd_invariant_of_compact_ne_zero
@[to_additive] lemma is_locally_finite_measure_of_smul_invariant (hU : is_open U) (hne : U.nonempty)
(hμU : μ U ≠ ∞) : is_locally_finite_measure μ :=
⟨λ x, let ⟨g, hg⟩ := hU.exists_smul_mem G x hne in
⟨(•) g ⁻¹' U, (hU.preimage (continuous_id.const_smul _)).mem_nhds hg, ne.lt_top $
by rwa [measure_preimage_smul]⟩⟩
variables [measure.regular μ]
@[to_additive] lemma measure_is_open_pos_of_smul_invariant_of_ne_zero (hμ : μ ≠ 0) (hU : is_open U)
(hne : U.nonempty) : 0 < μ U :=
let ⟨K, hK, hμK⟩ := regular.exists_compact_not_null.mpr hμ
in measure_is_open_pos_of_smul_invariant_of_compact_ne_zero G hK hμK hU hne
@[to_additive] lemma measure_pos_iff_nonempty_of_smul_invariant (hμ : μ ≠ 0) (hU : is_open U) :
0 < μ U ↔ U.nonempty :=
⟨λ h, nonempty_of_measure_ne_zero h.ne', measure_is_open_pos_of_smul_invariant_of_ne_zero G hμ hU⟩
include G
@[to_additive] lemma measure_eq_zero_iff_eq_empty_of_smul_invariant (hμ : μ ≠ 0) (hU : is_open U) :
μ U = 0 ↔ U = ∅ :=
by rw [← not_iff_not, ← ne.def, ← pos_iff_ne_zero,
measure_pos_iff_nonempty_of_smul_invariant G hμ hU, ← ne_empty_iff_nonempty]
end is_minimal
end measure_theory
|
cc6d1f9173553e5544d3cc78c19c5e961cdd2bf5 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /library/data/list/basic.lean | 90d6b9ee9db35d20197c939695847714b94e2849 | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 27,905 | lean | /-
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura
Basic properties of lists.
-/
import logic tools.helper_tactics data.nat.order
open eq.ops helper_tactics nat prod function option
inductive list (T : Type) : Type :=
| nil {} : list T
| cons : T → list T → list T
protected definition list.is_inhabited [instance] (A : Type) : inhabited (list A) :=
inhabited.mk list.nil
namespace list
notation h :: t := cons h t
notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l
variable {T : Type}
lemma cons_ne_nil [simp] (a : T) (l : list T) : a::l ≠ [] :=
by contradiction
lemma head_eq_of_cons_eq {A : Type} {h₁ h₂ : A} {t₁ t₂ : list A} :
(h₁::t₁) = (h₂::t₂) → h₁ = h₂ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq)
lemma tail_eq_of_cons_eq {A : Type} {h₁ h₂ : A} {t₁ t₂ : list A} :
(h₁::t₁) = (h₂::t₂) → t₁ = t₂ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq)
lemma cons_inj {A : Type} {a : A} : injective (cons a) :=
take l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe
/- append -/
definition append : list T → list T → list T
| [] l := l
| (h :: s) t := h :: (append s t)
notation l₁ ++ l₂ := append l₁ l₂
theorem append_nil_left [simp] (t : list T) : [] ++ t = t
theorem append_cons [simp] (x : T) (s t : list T) : (x::s) ++ t = x::(s ++ t)
theorem append_nil_right [simp] : ∀ (t : list T), t ++ [] = t
| [] := rfl
| (a :: l) := calc
(a :: l) ++ [] = a :: (l ++ []) : rfl
... = a :: l : append_nil_right l
theorem append.assoc [simp] : ∀ (s t u : list T), s ++ t ++ u = s ++ (t ++ u)
| [] t u := rfl
| (a :: l) t u :=
show a :: (l ++ t ++ u) = (a :: l) ++ (t ++ u),
by rewrite (append.assoc l t u)
/- length -/
definition length : list T → nat
| [] := 0
| (a :: l) := length l + 1
theorem length_nil [simp] : length (@nil T) = 0
theorem length_cons [simp] (x : T) (t : list T) : length (x::t) = length t + 1
theorem length_append [simp] : ∀ (s t : list T), length (s ++ t) = length s + length t
| [] t := calc
length ([] ++ t) = length t : rfl
... = length [] + length t : zero_add
| (a :: s) t := calc
length (a :: s ++ t) = length (s ++ t) + 1 : rfl
... = length s + length t + 1 : length_append
... = (length s + 1) + length t : succ_add
... = length (a :: s) + length t : rfl
theorem eq_nil_of_length_eq_zero : ∀ {l : list T}, length l = 0 → l = []
| [] H := rfl
| (a::s) H := by contradiction
theorem ne_nil_of_length_eq_succ : ∀ {l : list T} {n : nat}, length l = succ n → l ≠ []
| [] n h := by contradiction
| (a::l) n h := by contradiction
-- add_rewrite length_nil length_cons
/- concat -/
definition concat : Π (x : T), list T → list T
| a [] := [a]
| a (b :: l) := b :: concat a l
theorem concat_nil [simp] (x : T) : concat x [] = [x]
theorem concat_cons [simp] (x y : T) (l : list T) : concat x (y::l) = y::(concat x l)
theorem concat_eq_append (a : T) : ∀ (l : list T), concat a l = l ++ [a]
| [] := rfl
| (b :: l) :=
show b :: (concat a l) = (b :: l) ++ (a :: []),
by rewrite concat_eq_append
theorem concat_ne_nil [simp] (a : T) : ∀ (l : list T), concat a l ≠ [] :=
by intro l; induction l; repeat contradiction
theorem length_concat [simp] (a : T) : ∀ (l : list T), length (concat a l) = length l + 1
| [] := rfl
| (x::xs) := by rewrite [concat_cons, *length_cons, length_concat]
/- last -/
definition last : Π l : list T, l ≠ [] → T
| [] h := absurd rfl h
| [a] h := a
| (a₁::a₂::l) h := last (a₂::l) !cons_ne_nil
lemma last_singleton [simp] (a : T) (h : [a] ≠ []) : last [a] h = a :=
rfl
lemma last_cons_cons [simp] (a₁ a₂ : T) (l : list T) (h : a₁::a₂::l ≠ []) : last (a₁::a₂::l) h = last (a₂::l) !cons_ne_nil :=
rfl
theorem last_congr {l₁ l₂ : list T} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : last l₁ h₁ = last l₂ h₂ :=
by subst l₁
theorem last_concat [simp] {x : T} : ∀ {l : list T} (h : concat x l ≠ []), last (concat x l) h = x
| [] h := rfl
| [a] h := rfl
| (a₁::a₂::l) h :=
begin
change last (a₁::a₂::concat x l) !cons_ne_nil = x,
rewrite last_cons_cons,
change last (concat x (a₂::l)) !concat_ne_nil = x,
apply last_concat
end
-- add_rewrite append_nil append_cons
/- reverse -/
definition reverse : list T → list T
| [] := []
| (a :: l) := concat a (reverse l)
theorem reverse_nil [simp] : reverse (@nil T) = []
theorem reverse_cons [simp] (x : T) (l : list T) : reverse (x::l) = concat x (reverse l)
theorem reverse_singleton [simp] (x : T) : reverse [x] = [x]
theorem reverse_append [simp] : ∀ (s t : list T), reverse (s ++ t) = (reverse t) ++ (reverse s)
| [] t2 := calc
reverse ([] ++ t2) = reverse t2 : rfl
... = (reverse t2) ++ [] : append_nil_right
... = (reverse t2) ++ (reverse []) : by rewrite reverse_nil
| (a2 :: s2) t2 := calc
reverse ((a2 :: s2) ++ t2) = concat a2 (reverse (s2 ++ t2)) : rfl
... = concat a2 (reverse t2 ++ reverse s2) : reverse_append
... = (reverse t2 ++ reverse s2) ++ [a2] : concat_eq_append
... = reverse t2 ++ (reverse s2 ++ [a2]) : append.assoc
... = reverse t2 ++ concat a2 (reverse s2) : concat_eq_append
... = reverse t2 ++ reverse (a2 :: s2) : rfl
theorem reverse_reverse [simp] : ∀ (l : list T), reverse (reverse l) = l
| [] := rfl
| (a :: l) := calc
reverse (reverse (a :: l)) = reverse (concat a (reverse l)) : rfl
... = reverse (reverse l ++ [a]) : concat_eq_append
... = reverse [a] ++ reverse (reverse l) : reverse_append
... = reverse [a] ++ l : reverse_reverse
... = a :: l : rfl
theorem concat_eq_reverse_cons (x : T) (l : list T) : concat x l = reverse (x :: reverse l) :=
calc
concat x l = concat x (reverse (reverse l)) : reverse_reverse
... = reverse (x :: reverse l) : rfl
theorem length_reverse : ∀ (l : list T), length (reverse l) = length l
| [] := rfl
| (x::xs) := begin unfold reverse, rewrite [length_concat, length_cons, length_reverse] end
/- head and tail -/
definition head [h : inhabited T] : list T → T
| [] := arbitrary T
| (a :: l) := a
theorem head_cons [simp] [h : inhabited T] (a : T) (l : list T) : head (a::l) = a
theorem head_append [simp] [h : inhabited T] (t : list T) : ∀ {s : list T}, s ≠ [] → head (s ++ t) = head s
| [] H := absurd rfl H
| (a :: s) H :=
show head (a :: (s ++ t)) = head (a :: s),
by rewrite head_cons
definition tail : list T → list T
| [] := []
| (a :: l) := l
theorem tail_nil [simp] : tail (@nil T) = []
theorem tail_cons [simp] (a : T) (l : list T) : tail (a::l) = l
theorem cons_head_tail [h : inhabited T] {l : list T} : l ≠ [] → (head l)::(tail l) = l :=
list.cases_on l
(suppose [] ≠ [], absurd rfl this)
(take x l, suppose x::l ≠ [], rfl)
/- list membership -/
definition mem : T → list T → Prop
| a [] := false
| a (b :: l) := a = b ∨ mem a l
notation e ∈ s := mem e s
notation e ∉ s := ¬ e ∈ s
theorem mem_nil_iff [simp] (x : T) : x ∈ [] ↔ false :=
iff.rfl
theorem not_mem_nil (x : T) : x ∉ [] :=
iff.mp !mem_nil_iff
theorem mem_cons [simp] (x : T) (l : list T) : x ∈ x :: l :=
or.inl rfl
theorem mem_cons_of_mem (y : T) {x : T} {l : list T} : x ∈ l → x ∈ y :: l :=
assume H, or.inr H
theorem mem_cons_iff (x y : T) (l : list T) : x ∈ y::l ↔ (x = y ∨ x ∈ l) :=
iff.rfl
theorem eq_or_mem_of_mem_cons {x y : T} {l : list T} : x ∈ y::l → x = y ∨ x ∈ l :=
assume h, h
theorem mem_singleton {x a : T} : x ∈ [a] → x = a :=
suppose x ∈ [a], or.elim (eq_or_mem_of_mem_cons this)
(suppose x = a, this)
(suppose x ∈ [], absurd this !not_mem_nil)
theorem mem_of_mem_cons_of_mem {a b : T} {l : list T} : a ∈ b::l → b ∈ l → a ∈ l :=
assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl)
(suppose a = b, by substvars; exact binl)
(suppose a ∈ l, this)
theorem mem_or_mem_of_mem_append {x : T} {s t : list T} : x ∈ s ++ t → x ∈ s ∨ x ∈ t :=
list.induction_on s or.inr
(take y s,
assume IH : x ∈ s ++ t → x ∈ s ∨ x ∈ t,
suppose x ∈ y::s ++ t,
have x = y ∨ x ∈ s ++ t, from this,
have x = y ∨ x ∈ s ∨ x ∈ t, from or_of_or_of_imp_right this IH,
iff.elim_right or.assoc this)
theorem mem_append_of_mem_or_mem {x : T} {s t : list T} : x ∈ s ∨ x ∈ t → x ∈ s ++ t :=
list.induction_on s
(take H, or.elim H false.elim (assume H, H))
(take y s,
assume IH : x ∈ s ∨ x ∈ t → x ∈ s ++ t,
suppose x ∈ y::s ∨ x ∈ t,
or.elim this
(suppose x ∈ y::s,
or.elim (eq_or_mem_of_mem_cons this)
(suppose x = y, or.inl this)
(suppose x ∈ s, or.inr (IH (or.inl this))))
(suppose x ∈ t, or.inr (IH (or.inr this))))
theorem mem_append_iff (x : T) (s t : list T) : x ∈ s ++ t ↔ x ∈ s ∨ x ∈ t :=
iff.intro mem_or_mem_of_mem_append mem_append_of_mem_or_mem
theorem not_mem_of_not_mem_append_left {x : T} {s t : list T} : x ∉ s++t → x ∉ s :=
λ nxinst xins, absurd (mem_append_of_mem_or_mem (or.inl xins)) nxinst
theorem not_mem_of_not_mem_append_right {x : T} {s t : list T} : x ∉ s++t → x ∉ t :=
λ nxinst xint, absurd (mem_append_of_mem_or_mem (or.inr xint)) nxinst
theorem not_mem_append {x : T} {s t : list T} : x ∉ s → x ∉ t → x ∉ s++t :=
λ nxins nxint xinst, or.elim (mem_or_mem_of_mem_append xinst)
(λ xins, by contradiction)
(λ xint, by contradiction)
lemma length_pos_of_mem {a : T} : ∀ {l : list T}, a ∈ l → 0 < length l
| [] := assume Pinnil, by contradiction
| (b::l) := assume Pin, !zero_lt_succ
local attribute mem [reducible]
local attribute append [reducible]
theorem mem_split {x : T} {l : list T} : x ∈ l → ∃s t : list T, l = s ++ (x::t) :=
list.induction_on l
(suppose x ∈ [], false.elim (iff.elim_left !mem_nil_iff this))
(take y l,
assume IH : x ∈ l → ∃s t : list T, l = s ++ (x::t),
suppose x ∈ y::l,
or.elim (eq_or_mem_of_mem_cons this)
(suppose x = y,
exists.intro [] (!exists.intro (this ▸ rfl)))
(suppose x ∈ l,
obtain s (H2 : ∃t : list T, l = s ++ (x::t)), from IH this,
obtain t (H3 : l = s ++ (x::t)), from H2,
have y :: l = (y::s) ++ (x::t),
from H3 ▸ rfl,
!exists.intro (!exists.intro this)))
theorem mem_append_left {a : T} {l₁ : list T} (l₂ : list T) : a ∈ l₁ → a ∈ l₁ ++ l₂ :=
assume ainl₁, mem_append_of_mem_or_mem (or.inl ainl₁)
theorem mem_append_right {a : T} (l₁ : list T) {l₂ : list T} : a ∈ l₂ → a ∈ l₁ ++ l₂ :=
assume ainl₂, mem_append_of_mem_or_mem (or.inr ainl₂)
definition decidable_mem [instance] [H : decidable_eq T] (x : T) (l : list T) : decidable (x ∈ l) :=
list.rec_on l
(decidable.inr (not_of_iff_false !mem_nil_iff))
(take (h : T) (l : list T) (iH : decidable (x ∈ l)),
show decidable (x ∈ h::l), from
decidable.rec_on iH
(assume Hp : x ∈ l,
decidable.rec_on (H x h)
(suppose x = h,
decidable.inl (or.inl this))
(suppose x ≠ h,
decidable.inl (or.inr Hp)))
(suppose ¬x ∈ l,
decidable.rec_on (H x h)
(suppose x = h, decidable.inl (or.inl this))
(suppose x ≠ h,
have ¬(x = h ∨ x ∈ l), from
suppose x = h ∨ x ∈ l, or.elim this
(suppose x = h, by contradiction)
(suppose x ∈ l, by contradiction),
have ¬x ∈ h::l, from
iff.elim_right (not_iff_not_of_iff !mem_cons_iff) this,
decidable.inr this)))
theorem mem_of_ne_of_mem {x y : T} {l : list T} (H₁ : x ≠ y) (H₂ : x ∈ y :: l) : x ∈ l :=
or.elim (eq_or_mem_of_mem_cons H₂) (λe, absurd e H₁) (λr, r)
theorem ne_of_not_mem_cons {a b : T} {l : list T} : a ∉ b::l → a ≠ b :=
assume nin aeqb, absurd (or.inl aeqb) nin
theorem not_mem_of_not_mem_cons {a b : T} {l : list T} : a ∉ b::l → a ∉ l :=
assume nin nainl, absurd (or.inr nainl) nin
lemma not_mem_cons_of_ne_of_not_mem {x y : T} {l : list T} : x ≠ y → x ∉ l → x ∉ y::l :=
assume P1 P2, not.intro (assume Pxin, absurd (eq_or_mem_of_mem_cons Pxin) (not_or P1 P2))
lemma ne_and_not_mem_of_not_mem_cons {x y : T} {l : list T} : x ∉ y::l → x ≠ y ∧ x ∉ l :=
assume P, and.intro (ne_of_not_mem_cons P) (not_mem_of_not_mem_cons P)
definition sublist (l₁ l₂ : list T) := ∀ ⦃a : T⦄, a ∈ l₁ → a ∈ l₂
infix ⊆ := sublist
theorem nil_sub [simp] (l : list T) : [] ⊆ l :=
λ b i, false.elim (iff.mp (mem_nil_iff b) i)
theorem sub.refl [simp] (l : list T) : l ⊆ l :=
λ b i, i
theorem sub.trans {l₁ l₂ l₃ : list T} (H₁ : l₁ ⊆ l₂) (H₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ :=
λ b i, H₂ (H₁ i)
theorem sub_cons [simp] (a : T) (l : list T) : l ⊆ a::l :=
λ b i, or.inr i
theorem sub_of_cons_sub {a : T} {l₁ l₂ : list T} : a::l₁ ⊆ l₂ → l₁ ⊆ l₂ :=
λ s b i, s b (mem_cons_of_mem _ i)
theorem cons_sub_cons {l₁ l₂ : list T} (a : T) (s : l₁ ⊆ l₂) : (a::l₁) ⊆ (a::l₂) :=
λ b Hin, or.elim (eq_or_mem_of_mem_cons Hin)
(λ e : b = a, or.inl e)
(λ i : b ∈ l₁, or.inr (s i))
theorem sub_append_left [simp] (l₁ l₂ : list T) : l₁ ⊆ l₁++l₂ :=
λ b i, iff.mpr (mem_append_iff b l₁ l₂) (or.inl i)
theorem sub_append_right [simp] (l₁ l₂ : list T) : l₂ ⊆ l₁++l₂ :=
λ b i, iff.mpr (mem_append_iff b l₁ l₂) (or.inr i)
theorem sub_cons_of_sub (a : T) {l₁ l₂ : list T} : l₁ ⊆ l₂ → l₁ ⊆ (a::l₂) :=
λ (s : l₁ ⊆ l₂) (x : T) (i : x ∈ l₁), or.inr (s i)
theorem sub_app_of_sub_left (l l₁ l₂ : list T) : l ⊆ l₁ → l ⊆ l₁++l₂ :=
λ (s : l ⊆ l₁) (x : T) (xinl : x ∈ l),
have x ∈ l₁, from s xinl,
mem_append_of_mem_or_mem (or.inl this)
theorem sub_app_of_sub_right (l l₁ l₂ : list T) : l ⊆ l₂ → l ⊆ l₁++l₂ :=
λ (s : l ⊆ l₂) (x : T) (xinl : x ∈ l),
have x ∈ l₂, from s xinl,
mem_append_of_mem_or_mem (or.inr this)
theorem cons_sub_of_sub_of_mem {a : T} {l m : list T} : a ∈ m → l ⊆ m → a::l ⊆ m :=
λ (ainm : a ∈ m) (lsubm : l ⊆ m) (x : T) (xinal : x ∈ a::l), or.elim (eq_or_mem_of_mem_cons xinal)
(suppose x = a, by substvars; exact ainm)
(suppose x ∈ l, lsubm this)
theorem app_sub_of_sub_of_sub {l₁ l₂ l : list T} : l₁ ⊆ l → l₂ ⊆ l → l₁++l₂ ⊆ l :=
λ (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) (x : T) (xinl₁l₂ : x ∈ l₁++l₂),
or.elim (mem_or_mem_of_mem_append xinl₁l₂)
(suppose x ∈ l₁, l₁subl this)
(suppose x ∈ l₂, l₂subl this)
/- find -/
section
variable [H : decidable_eq T]
include H
definition find : T → list T → nat
| a [] := 0
| a (b :: l) := if a = b then 0 else succ (find a l)
theorem find_nil [simp] (x : T) : find x [] = 0
theorem find_cons (x y : T) (l : list T) : find x (y::l) = if x = y then 0 else succ (find x l)
theorem find_cons_of_eq {x y : T} (l : list T) : x = y → find x (y::l) = 0 :=
assume e, if_pos e
theorem find_cons_of_ne {x y : T} (l : list T) : x ≠ y → find x (y::l) = succ (find x l) :=
assume n, if_neg n
theorem find_of_not_mem {l : list T} {x : T} : ¬x ∈ l → find x l = length l :=
list.rec_on l
(suppose ¬x ∈ [], _)
(take y l,
assume iH : ¬x ∈ l → find x l = length l,
suppose ¬x ∈ y::l,
have ¬(x = y ∨ x ∈ l), from iff.elim_right (not_iff_not_of_iff !mem_cons_iff) this,
have ¬x = y ∧ ¬x ∈ l, from (iff.elim_left not_or_iff_not_and_not this),
calc
find x (y::l) = if x = y then 0 else succ (find x l) : !find_cons
... = succ (find x l) : if_neg (and.elim_left this)
... = succ (length l) : {iH (and.elim_right this)}
... = length (y::l) : !length_cons⁻¹)
lemma find_le_length : ∀ {a} {l : list T}, find a l ≤ length l
| a [] := !le.refl
| a (b::l) := decidable.rec_on (H a b)
(assume Peq, by rewrite [find_cons_of_eq l Peq]; exact !zero_le)
(assume Pne,
begin
rewrite [find_cons_of_ne l Pne, length_cons],
apply succ_le_succ, apply find_le_length
end)
lemma not_mem_of_find_eq_length : ∀ {a} {l : list T}, find a l = length l → a ∉ l
| a [] := assume Peq, !not_mem_nil
| a (b::l) := decidable.rec_on (H a b)
(assume Peq, by rewrite [find_cons_of_eq l Peq, length_cons]; contradiction)
(assume Pne,
begin
rewrite [find_cons_of_ne l Pne, length_cons, mem_cons_iff],
intro Plen, apply (not_or Pne),
exact not_mem_of_find_eq_length (succ.inj Plen)
end)
lemma find_lt_length {a} {l : list T} (Pin : a ∈ l) : find a l < length l :=
begin
apply nat.lt_of_le_and_ne,
apply find_le_length,
apply not.intro, intro Peq,
exact absurd Pin (not_mem_of_find_eq_length Peq)
end
end
/- nth element -/
section nth
definition nth : list T → nat → option T
| [] n := none
| (a :: l) 0 := some a
| (a :: l) (n+1) := nth l n
theorem nth_zero [simp] (a : T) (l : list T) : nth (a :: l) 0 = some a
theorem nth_succ [simp] (a : T) (l : list T) (n : nat) : nth (a::l) (succ n) = nth l n
theorem nth_eq_some : ∀ {l : list T} {n : nat}, n < length l → Σ a : T, nth l n = some a
| [] n h := absurd h !not_lt_zero
| (a::l) 0 h := ⟨a, rfl⟩
| (a::l) (succ n) h :=
have n < length l, from lt_of_succ_lt_succ h,
obtain (r : T) (req : nth l n = some r), from nth_eq_some this,
⟨r, by rewrite [nth_succ, req]⟩
open decidable
theorem find_nth [h : decidable_eq T] {a : T} : ∀ {l}, a ∈ l → nth l (find a l) = some a
| [] ain := absurd ain !not_mem_nil
| (b::l) ainbl := by_cases
(λ aeqb : a = b, by rewrite [find_cons_of_eq _ aeqb, nth_zero, aeqb])
(λ aneb : a ≠ b, or.elim (eq_or_mem_of_mem_cons ainbl)
(λ aeqb : a = b, absurd aeqb aneb)
(λ ainl : a ∈ l, by rewrite [find_cons_of_ne _ aneb, nth_succ, find_nth ainl]))
definition inth [h : inhabited T] (l : list T) (n : nat) : T :=
match nth l n with
| some a := a
| none := arbitrary T
end
theorem inth_zero [h : inhabited T] (a : T) (l : list T) : inth (a :: l) 0 = a
theorem inth_succ [h : inhabited T] (a : T) (l : list T) (n : nat) : inth (a::l) (n+1) = inth l n
end nth
section ith
definition ith : Π (l : list T) (i : nat), i < length l → T
| nil i h := absurd h !not_lt_zero
| (x::xs) 0 h := x
| (x::xs) (succ i) h := ith xs i (lt_of_succ_lt_succ h)
lemma ith_zero [simp] (a : T) (l : list T) (h : 0 < length (a::l)) : ith (a::l) 0 h = a :=
rfl
lemma ith_succ [simp] (a : T) (l : list T) (i : nat) (h : succ i < length (a::l))
: ith (a::l) (succ i) h = ith l i (lt_of_succ_lt_succ h) :=
rfl
end ith
open decidable
definition has_decidable_eq {A : Type} [H : decidable_eq A] : ∀ l₁ l₂ : list A, decidable (l₁ = l₂)
| [] [] := inl rfl
| [] (b::l₂) := inr (by contradiction)
| (a::l₁) [] := inr (by contradiction)
| (a::l₁) (b::l₂) :=
match H a b with
| inl Hab :=
match has_decidable_eq l₁ l₂ with
| inl He := inl (by congruence; repeat assumption)
| inr Hn := inr (by intro H; injection H; contradiction)
end
| inr Hnab := inr (by intro H; injection H; contradiction)
end
/- quasiequal a l l' means that l' is exactly l, with a added
once somewhere -/
section qeq
variable {A : Type}
inductive qeq (a : A) : list A → list A → Prop :=
| qhead : ∀ l, qeq a l (a::l)
| qcons : ∀ (b : A) {l l' : list A}, qeq a l l' → qeq a (b::l) (b::l')
open qeq
notation l' `≈`:50 a `|` l:50 := qeq a l l'
theorem qeq_app : ∀ (l₁ : list A) (a : A) (l₂ : list A), l₁++(a::l₂) ≈ a|l₁++l₂
| [] a l₂ := qhead a l₂
| (x::xs) a l₂ := qcons x (qeq_app xs a l₂)
theorem mem_head_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → a ∈ l₁ :=
take q, qeq.induction_on q
(λ l, !mem_cons)
(λ b l l' q r, or.inr r)
theorem mem_tail_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → ∀ x, x ∈ l₂ → x ∈ l₁ :=
take q, qeq.induction_on q
(λ l x i, or.inr i)
(λ b l l' q r x xinbl, or.elim (eq_or_mem_of_mem_cons xinbl)
(λ xeqb : x = b, xeqb ▸ mem_cons x l')
(λ xinl : x ∈ l, or.inr (r x xinl)))
theorem mem_cons_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → ∀ x, x ∈ l₁ → x ∈ a::l₂ :=
take q, qeq.induction_on q
(λ l x i, i)
(λ b l l' q r x xinbl', or.elim (eq_or_mem_of_mem_cons xinbl')
(λ xeqb : x = b, xeqb ▸ or.inr (mem_cons x l))
(λ xinl' : x ∈ l', or.elim (eq_or_mem_of_mem_cons (r x xinl'))
(λ xeqa : x = a, xeqa ▸ mem_cons x (b::l))
(λ xinl : x ∈ l, or.inr (or.inr xinl))))
theorem length_eq_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → length l₁ = succ (length l₂) :=
take q, qeq.induction_on q
(λ l, rfl)
(λ b l l' q r, by rewrite [*length_cons, r])
theorem qeq_of_mem {a : A} {l : list A} : a ∈ l → (∃l', l≈a|l') :=
list.induction_on l
(λ h : a ∈ nil, absurd h (not_mem_nil a))
(λ x xs r ainxxs, or.elim (eq_or_mem_of_mem_cons ainxxs)
(λ aeqx : a = x,
assert aux : ∃ l, x::xs≈x|l, from
exists.intro xs (qhead x xs),
by rewrite aeqx; exact aux)
(λ ainxs : a ∈ xs,
have ∃l', xs ≈ a|l', from r ainxs,
obtain (l' : list A) (q : xs ≈ a|l'), from this,
have x::xs ≈ a | x::l', from qcons x q,
exists.intro (x::l') this))
theorem qeq_split {a : A} {l l' : list A} : l'≈a|l → ∃l₁ l₂, l = l₁++l₂ ∧ l' = l₁++(a::l₂) :=
take q, qeq.induction_on q
(λ t,
have t = []++t ∧ a::t = []++(a::t), from and.intro rfl rfl,
exists.intro [] (exists.intro t this))
(λ b t t' q r,
obtain (l₁ l₂ : list A) (h : t = l₁++l₂ ∧ t' = l₁++(a::l₂)), from r,
have b::t = (b::l₁)++l₂ ∧ b::t' = (b::l₁)++(a::l₂),
begin
rewrite [and.elim_right h, and.elim_left h],
constructor, repeat reflexivity
end,
exists.intro (b::l₁) (exists.intro l₂ this))
theorem sub_of_mem_of_sub_of_qeq {a : A} {l : list A} {u v : list A} : a ∉ l → a::l ⊆ v → v≈a|u → l ⊆ u :=
λ (nainl : a ∉ l) (s : a::l ⊆ v) (q : v≈a|u) (x : A) (xinl : x ∈ l),
have x ∈ v, from s (or.inr xinl),
have x ∈ a::u, from mem_cons_of_qeq q x this,
or.elim (eq_or_mem_of_mem_cons this)
(suppose x = a, by substvars; contradiction)
(suppose x ∈ u, this)
end qeq
section firstn
variable {A : Type}
definition firstn : nat → list A → list A
| 0 l := []
| (n+1) [] := []
| (n+1) (a::l) := a :: firstn n l
lemma firstn_zero : ∀ (l : list A), firstn 0 l = [] :=
by intros; reflexivity
lemma firstn_nil : ∀ n, firstn n [] = ([] : list A)
| 0 := rfl
| (n+1) := rfl
lemma firstn_cons : ∀ n (a : A) (l : list A), firstn (succ n) (a::l) = a :: firstn n l :=
by intros; reflexivity
lemma firstn_all : ∀ (l : list A), firstn (length l) l = l
| [] := rfl
| (a::l) := begin unfold [length, firstn], rewrite firstn_all end
lemma firstn_all_of_ge : ∀ {n} {l : list A}, n ≥ length l → firstn n l = l
| 0 [] h := rfl
| 0 (a::l) h := absurd h (not_le_of_gt !succ_pos)
| (n+1) [] h := rfl
| (n+1) (a::l) h := begin unfold firstn, rewrite [firstn_all_of_ge (le_of_succ_le_succ h)] end
lemma firstn_firstn : ∀ (n m) (l : list A), firstn n (firstn m l) = firstn (min n m) l
| n 0 l := by rewrite [min_zero, firstn_zero, firstn_nil]
| 0 m l := by rewrite [zero_min]
| (succ n) (succ m) nil := by rewrite [*firstn_nil]
| (succ n) (succ m) (a::l) := by rewrite [*firstn_cons, firstn_firstn, min_succ_succ]
lemma length_firstn_le : ∀ (n) (l : list A), length (firstn n l) ≤ n
| 0 l := by rewrite [firstn_zero]
| (succ n) (a::l) := by rewrite [firstn_cons, length_cons, add_one]; apply succ_le_succ; apply length_firstn_le
| (succ n) [] := by rewrite [firstn_nil, length_nil]; apply zero_le
lemma length_firstn_eq : ∀ (n) (l : list A), length (firstn n l) = min n (length l)
| 0 l := by rewrite [firstn_zero, zero_min]
| (succ n) (a::l) := by rewrite [firstn_cons, *length_cons, *add_one, min_succ_succ, length_firstn_eq]
| (succ n) [] := by rewrite [firstn_nil]
end firstn
section count
variable {A : Type}
variable [decA : decidable_eq A]
include decA
definition count (a : A) : list A → nat
| [] := 0
| (x::xs) := if a = x then succ (count xs) else count xs
lemma count_nil (a : A) : count a [] = 0 :=
rfl
lemma count_cons (a b : A) (l : list A) : count a (b::l) = if a = b then succ (count a l) else count a l :=
rfl
lemma count_cons_eq (a : A) (l : list A) : count a (a::l) = succ (count a l) :=
if_pos rfl
lemma count_cons_of_ne {a b : A} (h : a ≠ b) (l : list A) : count a (b::l) = count a l :=
if_neg h
lemma count_cons_ge_count (a b : A) (l : list A) : count a (b::l) ≥ count a l :=
by_cases
(suppose a = b, begin subst b, rewrite count_cons_eq, apply le_succ end)
(suppose a ≠ b, begin rewrite (count_cons_of_ne this), apply le.refl end)
lemma count_singleton (a : A) : count a [a] = 1 :=
by rewrite count_cons_eq
lemma count_append (a : A) : ∀ l₁ l₂, count a (l₁++l₂) = count a l₁ + count a l₂
| [] l₂ := by rewrite [append_nil_left, count_nil, zero_add]
| (b::l₁) l₂ := by_cases
(suppose a = b, by rewrite [-this, append_cons, *count_cons_eq, succ_add, count_append])
(suppose a ≠ b, by rewrite [append_cons, *count_cons_of_ne this, count_append])
lemma count_concat (a : A) (l : list A) : count a (concat a l) = succ (count a l) :=
by rewrite [concat_eq_append, count_append, count_singleton]
lemma mem_of_count_gt_zero : ∀ {a : A} {l : list A}, count a l > 0 → a ∈ l
| a [] h := absurd h !lt.irrefl
| a (b::l) h := by_cases
(suppose a = b, begin subst b, apply mem_cons end)
(suppose a ≠ b,
have count a l > 0, by rewrite [count_cons_of_ne this at h]; exact h,
have a ∈ l, from mem_of_count_gt_zero this,
show a ∈ b::l, from mem_cons_of_mem _ this)
lemma count_gt_zero_of_mem : ∀ {a : A} {l : list A}, a ∈ l → count a l > 0
| a [] h := absurd h !not_mem_nil
| a (b::l) h := or.elim h
(suppose a = b, begin subst b, rewrite count_cons_eq, apply zero_lt_succ end)
(suppose a ∈ l, calc
count a (b::l) ≥ count a l : count_cons_ge_count
... > 0 : count_gt_zero_of_mem this)
lemma count_eq_zero_of_not_mem {a : A} {l : list A} (h : a ∉ l) : count a l = 0 :=
match count a l with
| 0 := suppose count a l = 0, this
| (succ n) := suppose count a l = succ n, absurd (mem_of_count_gt_zero (begin rewrite this, exact dec_trivial end)) h
end rfl
end count
end list
attribute list.has_decidable_eq [instance]
attribute list.decidable_mem [instance]
|
e68d8b5a5a5ebdbabae26ec6391fa958428af2bd | 3dd1b66af77106badae6edb1c4dea91a146ead30 | /tests/lean/run/e5.lean | 393a87015492c7f0cae7f0b99cd5178cfbdaeba9 | [
"Apache-2.0"
] | permissive | silky/lean | 79c20c15c93feef47bb659a2cc139b26f3614642 | df8b88dca2f8da1a422cb618cd476ef5be730546 | refs/heads/master | 1,610,737,587,697 | 1,406,574,534,000 | 1,406,574,534,000 | 22,362,176 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,510 | lean | definition Prop [inline] := Type.{0}
definition false : Prop := ∀x : Prop, x
check false
theorem false_elim (C : Prop) (H : false) : C
:= H C
definition eq {A : Type} (a b : A)
:= ∀ P : A → Prop, P a → P b
check eq
infix `=`:50 := eq
theorem refl {A : Type} (a : A) : a = a
:= λ P H, H
definition true : Prop
:= false = false
theorem trivial : true
:= refl false
theorem subst {A : Type} {P : A -> Prop} {a b : A} (H1 : a = b) (H2 : P a) : P b
:= H1 _ H2
theorem symm {A : Type} {a b : A} (H : a = b) : b = a
:= subst H (refl a)
theorem trans {A : Type} {a b c : A} (H1 : a = b) (H2 : b = c) : a = c
:= subst H2 H1
inductive nat : Type :=
| zero : nat
| succ : nat → nat
print "using strict implicit arguments"
abbreviation symmetric {A : Type} (R : A → A → Prop) := ∀ ⦃a b⦄, R a b → R b a
check symmetric
variable p : nat → nat → Prop
check symmetric p
axiom H1 : symmetric p
axiom H2 : p zero (succ zero)
check H1
check H1 H2
print "------------"
print "using implicit arguments"
abbreviation symmetric2 {A : Type} (R : A → A → Prop) := ∀ {a b}, R a b → R b a
check symmetric2
check symmetric2 p
axiom H3 : symmetric2 p
axiom H4 : p zero (succ zero)
check H3
check H3 H4
print "-----------------"
print "using strict implicit arguments (ASCII notation)"
abbreviation symmetric3 {A : Type} (R : A → A → Prop) := ∀ {{a b}}, R a b → R b a
check symmetric3
check symmetric3 p
axiom H5 : symmetric3 p
axiom H6 : p zero (succ zero)
check H5
check H5 H6
|
d11cf26012d9539f3377c8a9d7afe67dc586bd02 | 26ac254ecb57ffcb886ff709cf018390161a9225 | /src/set_theory/cofinality.lean | cfe8356ee9dc56118a75277e96ad3290ee7ac3c9 | [
"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 | 22,018 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import set_theory.ordinal
/-!
# Cofinality on ordinals, regular cardinals
-/
noncomputable theory
open function cardinal set
open_locale classical
universes u v w
variables {α : Type*} {r : α → α → Prop}
namespace order
/-- Cofinality of a reflexive order `≼`. This is the smallest cardinality
of a subset `S : set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/
def cof (r : α → α → Prop) [is_refl α r] : cardinal :=
@cardinal.min {S : set α // ∀ a, ∃ b ∈ S, r a b}
⟨⟨set.univ, λ a, ⟨a, ⟨⟩, refl _⟩⟩⟩
(λ S, mk S)
lemma cof_le (r : α → α → Prop) [is_refl α r] {S : set α} (h : ∀a, ∃(b ∈ S), r a b) :
order.cof r ≤ mk S :=
le_trans (cardinal.min_le _ ⟨S, h⟩) (le_refl _)
lemma le_cof {r : α → α → Prop} [is_refl α r] (c : cardinal) :
c ≤ order.cof r ↔ ∀ {S : set α} (h : ∀a, ∃(b ∈ S), r a b) , c ≤ mk S :=
by { rw [order.cof, cardinal.le_min], exact ⟨λ H S h, H ⟨S, h⟩, λ H ⟨S, h⟩, H h ⟩ }
end order
theorem order_iso.cof.aux {α : Type u} {β : Type v} {r s}
[is_refl α r] [is_refl β s] (f : r ≃o s) :
cardinal.lift.{u (max u v)} (order.cof r) ≤
cardinal.lift.{v (max u v)} (order.cof s) :=
begin
rw [order.cof, order.cof, lift_min, lift_min, cardinal.le_min],
intro S, cases S with S H, simp [(∘)],
refine le_trans (min_le _ _) _,
{ exact ⟨f ⁻¹' S, λ a,
let ⟨b, bS, h⟩ := H (f a) in ⟨f.symm b, by simp [bS, f.ord', h,
-coe_fn_coe_base, -coe_fn_coe_trans, principal_seg.coe_coe_fn', initial_seg.coe_coe_fn]⟩⟩ },
{ exact lift_mk_le.{u v (max u v)}.2
⟨⟨λ ⟨x, h⟩, ⟨f x, h⟩, λ ⟨x, h₁⟩ ⟨y, h₂⟩ h₃,
by congr; injection h₃ with h'; exact f.to_equiv.injective h'⟩⟩ }
end
theorem order_iso.cof {α : Type u} {β : Type v} {r s}
[is_refl α r] [is_refl β s] (f : r ≃o s) :
cardinal.lift.{u (max u v)} (order.cof r) =
cardinal.lift.{v (max u v)} (order.cof s) :=
le_antisymm (order_iso.cof.aux f) (order_iso.cof.aux f.symm)
def strict_order.cof (r : α → α → Prop) [h : is_irrefl α r] : cardinal :=
@order.cof α (λ x y, ¬ r y x) ⟨h.1⟩
namespace ordinal
/-- Cofinality of an ordinal. This is the smallest cardinal of a
subset `S` of the ordinal which is unbounded, in the sense
`∀ a, ∃ b ∈ S, ¬(b > a)`. It is defined for all ordinals, but
`cof 0 = 0` and `cof (succ o) = 1`, so it is only really
interesting on limit ordinals (when it is an infinite cardinal). -/
def cof (o : ordinal.{u}) : cardinal.{u} :=
quot.lift_on o (λ ⟨α, r, _⟩, by exactI strict_order.cof r)
begin
rintros ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨⟨f, hf⟩⟩,
rw ← cardinal.lift_inj,
apply order_iso.cof ⟨f, _⟩,
simp [hf]
end
lemma cof_type (r : α → α → Prop) [is_well_order α r] : (type r).cof = strict_order.cof r := rfl
theorem le_cof_type [is_well_order α r] {c} : c ≤ cof (type r) ↔
∀ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) → c ≤ mk S :=
by dsimp [cof, strict_order.cof, order.cof, type, quotient.mk, quot.lift_on];
rw [cardinal.le_min, subtype.forall]; refl
theorem cof_type_le [is_well_order α r] (S : set α) (h : ∀ a, ∃ b ∈ S, ¬ r b a) :
cof (type r) ≤ mk S :=
le_cof_type.1 (le_refl _) S h
theorem lt_cof_type [is_well_order α r] (S : set α) (hl : mk S < cof (type r)) :
∃ a, ∀ b ∈ S, r b a :=
not_forall_not.1 $ λ h, not_le_of_lt hl $ cof_type_le S (λ a, not_ball.1 (h a))
theorem cof_eq (r : α → α → Prop) [is_well_order α r] :
∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ mk S = cof (type r) :=
begin
have : ∃ i, cof (type r) = _,
{ dsimp [cof, order.cof, type, quotient.mk, quot.lift_on],
apply cardinal.min_eq },
exact let ⟨⟨S, hl⟩, e⟩ := this in ⟨S, hl, e.symm⟩,
end
theorem ord_cof_eq (r : α → α → Prop) [is_well_order α r] :
∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ type (subrel r S) = (cof (type r)).ord :=
let ⟨S, hS, e⟩ := cof_eq r, ⟨s, _, e'⟩ := cardinal.ord_eq S,
T : set α := {a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a} in
begin
resetI, suffices,
{ refine ⟨T, this,
le_antisymm _ (cardinal.ord_le.2 $ cof_type_le T this)⟩,
rw [← e, e'],
refine type_le'.2 ⟨order_embedding.of_monotone
(λ a, ⟨a, let ⟨aS, _⟩ := a.2 in aS⟩) (λ a b h, _)⟩,
rcases a with ⟨a, aS, ha⟩, rcases b with ⟨b, bS, hb⟩,
change s ⟨a, _⟩ ⟨b, _⟩,
refine ((trichotomous_of s _ _).resolve_left (λ hn, _)).resolve_left _,
{ exact asymm h (ha _ hn) },
{ intro e, injection e with e, subst b,
exact irrefl _ h } },
{ intro a,
have : {b : S | ¬ r b a}.nonempty := let ⟨b, bS, ba⟩ := hS a in ⟨⟨b, bS⟩, ba⟩,
let b := (is_well_order.wf).min _ this,
have ba : ¬r b a := (is_well_order.wf).min_mem _ this,
refine ⟨b, ⟨b.2, λ c, not_imp_not.1 $ λ h, _⟩, ba⟩,
rw [show ∀b:S, (⟨b, b.2⟩:S) = b, by intro b; cases b; refl],
exact (is_well_order.wf).not_lt_min _ this
(is_order_connected.neg_trans h ba) }
end
theorem lift_cof (o) : (cof o).lift = cof o.lift :=
induction_on o $ begin introsI α r _,
cases lift_type r with _ e, rw e,
apply le_antisymm,
{ unfreezingI { refine le_cof_type.2 (λ S H, _) },
have : (mk (ulift.up ⁻¹' S)).lift ≤ mk S :=
⟨⟨λ ⟨⟨x, h⟩⟩, ⟨⟨x⟩, h⟩,
λ ⟨⟨x, h₁⟩⟩ ⟨⟨y, h₂⟩⟩ e, by simp at e; congr; injection e⟩⟩,
refine le_trans (cardinal.lift_le.2 $ cof_type_le _ _) this,
exact λ a, let ⟨⟨b⟩, bs, br⟩ := H ⟨a⟩ in ⟨b, bs, br⟩ },
{ rcases cof_eq r with ⟨S, H, e'⟩,
have : mk (ulift.down ⁻¹' S) ≤ (mk S).lift :=
⟨⟨λ ⟨⟨x⟩, h⟩, ⟨⟨x, h⟩⟩,
λ ⟨⟨x⟩, h₁⟩ ⟨⟨y⟩, h₂⟩ e, by simp at e; congr; injections⟩⟩,
rw e' at this,
unfreezingI { refine le_trans (cof_type_le _ _) this },
exact λ ⟨a⟩, let ⟨b, bs, br⟩ := H a in ⟨⟨b⟩, bs, br⟩ }
end
theorem cof_le_card (o) : cof o ≤ card o :=
induction_on o $ λ α r _, begin
resetI,
have : mk (@set.univ α) = card (type r) :=
quotient.sound ⟨equiv.set.univ _⟩,
rw ← this, exact cof_type_le set.univ (λ a, ⟨a, ⟨⟩, irrefl a⟩)
end
theorem cof_ord_le (c : cardinal) : cof c.ord ≤ c :=
by simpa using cof_le_card c.ord
@[simp] theorem cof_zero : cof 0 = 0 :=
le_antisymm (by simpa using cof_le_card 0) (cardinal.zero_le _)
@[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 :=
⟨induction_on o $ λ α r _ z, by exactI
let ⟨S, hl, e⟩ := cof_eq r in type_eq_zero_iff_empty.2 $
λ ⟨a⟩, let ⟨b, h, _⟩ := hl a in
ne_zero_iff_nonempty.2 (by exact ⟨⟨_, h⟩⟩) (e.trans z),
λ e, by simp [e]⟩
@[simp] theorem cof_succ (o) : cof (succ o) = 1 :=
begin
apply le_antisymm,
{ refine induction_on o (λ α r _, _),
change cof (type _) ≤ _,
rw [← (_ : mk _ = 1)], apply cof_type_le,
{ refine λ a, ⟨sum.inr punit.star, set.mem_singleton _, _⟩,
rcases a with a|⟨⟨⟨⟩⟩⟩; simp [empty_relation] },
{ rw [cardinal.fintype_card, set.card_singleton], simp } },
{ rw [← cardinal.succ_zero, cardinal.succ_le],
simpa [lt_iff_le_and_ne, cardinal.zero_le] using
λ h, succ_ne_zero o (cof_eq_zero.1 (eq.symm h)) }
end
@[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a :=
⟨induction_on o $ λ α r _ z, begin
resetI,
rcases cof_eq r with ⟨S, hl, e⟩, rw z at e,
cases ne_zero_iff_nonempty.1 (by rw e; exact one_ne_zero) with a,
refine ⟨typein r a, eq.symm $ quotient.sound
⟨order_iso.of_surjective (order_embedding.of_monotone _
(λ x y, _)) (λ x, _)⟩⟩,
{ apply sum.rec; [exact subtype.val, exact λ _, a] },
{ rcases x with x|⟨⟨⟨⟩⟩⟩; rcases y with y|⟨⟨⟨⟩⟩⟩;
simp [subrel, order.preimage, empty_relation],
exact x.2 },
{ suffices : r x a ∨ ∃ (b : punit), ↑a = x, {simpa},
rcases trichotomous_of r x a with h|h|h,
{ exact or.inl h },
{ exact or.inr ⟨punit.star, h.symm⟩ },
{ rcases hl x with ⟨a', aS, hn⟩,
rw (_ : ↑a = a') at h, {exact absurd h hn},
refine congr_arg subtype.val (_ : a = ⟨a', aS⟩),
haveI := le_one_iff_subsingleton.1 (le_of_eq e),
apply subsingleton.elim } }
end, λ ⟨a, e⟩, by simp [e]⟩
@[simp] theorem cof_add (a b : ordinal) : b ≠ 0 → cof (a + b) = cof b :=
induction_on a $ λ α r _, induction_on b $ λ β s _ b0, begin
resetI,
change cof (type _) = _,
refine eq_of_forall_le_iff (λ c, _),
rw [le_cof_type, le_cof_type],
split; intros H S hS,
{ refine le_trans (H {a | sum.rec_on a (∅:set α) S} (λ a, _)) ⟨⟨_, _⟩⟩,
{ cases a with a b,
{ cases type_ne_zero_iff_nonempty.1 b0 with b,
rcases hS b with ⟨b', bs, _⟩,
exact ⟨sum.inr b', bs, by simp⟩ },
{ rcases hS b with ⟨b', bs, h⟩,
exact ⟨sum.inr b', bs, by simp [h]⟩ } },
{ exact λ a, match a with ⟨sum.inr b, h⟩ := ⟨b, h⟩ end },
{ exact λ a b, match a, b with
⟨sum.inr a, h₁⟩, ⟨sum.inr b, h₂⟩, h := by congr; injection h
end } },
{ refine le_trans (H (sum.inr ⁻¹' S) (λ a, _)) ⟨⟨_, _⟩⟩,
{ rcases hS (sum.inr a) with ⟨a'|b', bs, h⟩; simp at h,
{ cases h }, { exact ⟨b', bs, h⟩ } },
{ exact λ ⟨a, h⟩, ⟨_, h⟩ },
{ exact λ ⟨a, h₁⟩ ⟨b, h₂⟩ h,
by injection h with h; congr; injection h } }
end
@[simp] theorem cof_cof (o : ordinal) : cof (cof o).ord = cof o :=
le_antisymm (le_trans (cof_le_card _) (by simp)) $
induction_on o $ λ α r _, by exactI
let ⟨S, hS, e₁⟩ := ord_cof_eq r,
⟨T, hT, e₂⟩ := cof_eq (subrel r S) in begin
rw e₁ at e₂, rw ← e₂,
refine le_trans (cof_type_le {a | ∃ h, (subtype.mk a h : S) ∈ T} (λ a, _)) ⟨⟨_, _⟩⟩,
{ rcases hS a with ⟨b, bS, br⟩,
rcases hT ⟨b, bS⟩ with ⟨⟨c, cS⟩, cT, cs⟩,
exact ⟨c, ⟨cS, cT⟩, is_order_connected.neg_trans cs br⟩ },
{ exact λ ⟨a, h⟩, ⟨⟨a, h.fst⟩, h.snd⟩ },
{ exact λ ⟨a, ha⟩ ⟨b, hb⟩ h,
by injection h with h; congr; injection h },
end
theorem omega_le_cof {o} : cardinal.omega ≤ cof o ↔ is_limit o :=
begin
rcases zero_or_succ_or_limit o with rfl|⟨o,rfl⟩|l,
{ simp [not_zero_is_limit, cardinal.omega_ne_zero] },
{ simp [not_succ_is_limit, cardinal.one_lt_omega] },
{ simp [l], refine le_of_not_lt (λ h, _),
cases cardinal.lt_omega.1 h with n e,
have := cof_cof o,
rw [e, ord_nat] at this,
cases n,
{ simp at e, simpa [e, not_zero_is_limit] using l },
{ rw [← nat_cast_succ, cof_succ] at this,
rw [← this, cof_eq_one_iff_is_succ] at e,
rcases e with ⟨a, rfl⟩,
exact not_succ_is_limit _ l } }
end
@[simp] theorem cof_omega : cof omega = cardinal.omega :=
le_antisymm
(by rw ← card_omega; apply cof_le_card)
(omega_le_cof.2 omega_is_limit)
theorem cof_eq' (r : α → α → Prop) [is_well_order α r] (h : is_limit (type r)) :
∃ S : set α, (∀ a, ∃ b ∈ S, r a b) ∧ mk S = cof (type r) :=
let ⟨S, H, e⟩ := cof_eq r in
⟨S, λ a,
let a' := enum r _ (h.2 _ (typein_lt_type r a)) in
let ⟨b, h, ab⟩ := H a' in
⟨b, h, (is_order_connected.conn a b a' $ (typein_lt_typein r).1
(by rw typein_enum; apply ordinal.lt_succ_self)).resolve_right ab⟩,
e⟩
theorem cof_sup_le_lift {ι} (f : ι → ordinal) (H : ∀ i, f i < sup f) :
cof (sup f) ≤ (mk ι).lift :=
begin
generalize e : sup f = o,
refine ordinal.induction_on o _ e, introsI α r _ e',
rw e' at H,
refine le_trans (cof_type_le (set.range (λ i, enum r _ (H i))) _)
⟨embedding.of_surjective _ _⟩,
{ intro a, by_contra h,
apply not_le_of_lt (typein_lt_type r a),
rw [← e', sup_le],
intro i,
have h : ∀ (x : ι), r (enum r (f x) _) a, { simpa using h },
simpa only [typein_enum] using le_of_lt ((typein_lt_typein r).2 (h i)) },
{ exact λ i, ⟨_, set.mem_range_self i.1⟩ },
{ intro a, rcases a with ⟨_, i, rfl⟩, exact ⟨⟨i⟩, by simp⟩ }
end
theorem cof_sup_le {ι} (f : ι → ordinal) (H : ∀ i, f i < sup.{u u} f) :
cof (sup.{u u} f) ≤ mk ι :=
by simpa using cof_sup_le_lift.{u u} f H
theorem cof_bsup_le_lift {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup o f) →
cof (bsup o f) ≤ o.card.lift :=
induction_on o $ λ α r _ f H,
by rw bsup_type; refine cof_sup_le_lift _ _;
rw ← bsup_type; intro a; apply H
theorem cof_bsup_le {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup.{u u} o f) →
cof (bsup.{u u} o f) ≤ o.card :=
induction_on o $ λ α r _ f H,
by simpa using cof_bsup_le_lift.{u u} f H
@[simp] theorem cof_univ : cof univ.{u v} = cardinal.univ :=
le_antisymm (cof_le_card _) begin
refine le_of_forall_lt (λ c h, _),
rcases lt_univ'.1 h with ⟨c, rfl⟩,
rcases @cof_eq ordinal.{u} (<) _ with ⟨S, H, Se⟩,
rw [univ, ← lift_cof, ← cardinal.lift_lift, cardinal.lift_lt, ← Se],
refine lt_of_not_ge (λ h, _),
cases cardinal.lift_down h with a e,
refine quotient.induction_on a (λ α e, _) e,
cases quotient.exact e with f,
have f := equiv.ulift.symm.trans f,
let g := λ a, (f a).1,
let o := succ (sup.{u u} g),
rcases H o with ⟨b, h, l⟩,
refine l (lt_succ.2 _),
rw ← show g (f.symm ⟨b, h⟩) = b, by dsimp [g]; simp,
apply le_sup
end
theorem sup_lt_ord {ι} (f : ι → ordinal) {c : ordinal} (H1 : cardinal.mk ι < c.cof)
(H2 : ∀ i, f i < c) : sup.{u u} f < c :=
begin
apply lt_of_le_of_ne,
{ rw [sup_le], exact λ i, le_of_lt (H2 i) },
rintro h, apply not_le_of_lt H1,
simpa [sup_ord, H2, h] using cof_sup_le.{u} f
end
theorem sup_lt {ι} (f : ι → cardinal) {c : cardinal} (H1 : cardinal.mk ι < c.ord.cof)
(H2 : ∀ i, f i < c) : cardinal.sup.{u u} f < c :=
by { rw [←ord_lt_ord, ←sup_ord], apply sup_lt_ord _ H1, intro i, rw ord_lt_ord, apply H2 }
/-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/
theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : is_well_order α r] {s : set (set α)}
(h₁ : unbounded r $ ⋃₀ s) (h₂ : mk s < strict_order.cof r) : ∃(x ∈ s), unbounded r x :=
begin
by_contra h, simp only [not_exists, exists_prop, not_and, not_unbounded_iff] at h,
apply not_le_of_lt h₂,
let f : s → α := λ x : s, wo.wf.sup x (h x.1 x.2),
let t : set α := range f,
have : mk t ≤ mk s, exact mk_range_le, refine le_trans _ this,
have : unbounded r t,
{ intro x, rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩,
refine ⟨f ⟨c, hc⟩, mem_range_self _, _⟩, intro hxz, apply hxy,
refine trans (wo.wf.lt_sup _ hy) hxz },
exact cardinal.min_le _ (subtype.mk t this)
end
/-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/
theorem unbounded_of_unbounded_Union {α β : Type u} (r : α → α → Prop) [wo : is_well_order α r]
(s : β → set α)
(h₁ : unbounded r $ ⋃x, s x) (h₂ : mk β < strict_order.cof r) : ∃x : β, unbounded r (s x) :=
begin
rw [← sUnion_range] at h₁,
have : mk ↥(range (λ (i : β), s i)) < strict_order.cof r := lt_of_le_of_lt mk_range_le h₂,
rcases unbounded_of_unbounded_sUnion r h₁ this with ⟨_, ⟨x, rfl⟩, u⟩, exact ⟨x, u⟩
end
/-- The infinite pigeonhole principle-/
theorem infinite_pigeonhole {β α : Type u} (f : β → α) (h₁ : cardinal.omega ≤ mk β)
(h₂ : mk α < (mk β).ord.cof) : ∃a : α, mk (f ⁻¹' {a}) = mk β :=
begin
have : ¬∀a, mk (f ⁻¹' {a}) < mk β,
{ intro h,
apply not_lt_of_ge (ge_of_eq $ mk_univ),
rw [←@preimage_univ _ _ f, ←Union_of_singleton, preimage_Union],
apply lt_of_le_of_lt mk_Union_le_sum_mk,
apply lt_of_le_of_lt (sum_le_sup _),
apply mul_lt_of_lt h₁ (lt_of_lt_of_le h₂ $ cof_ord_le _),
exact sup_lt _ h₂ h },
rw [not_forall] at this, cases this with x h,
use x, apply le_antisymm _ (le_of_not_gt h),
rw [le_mk_iff_exists_set], exact ⟨_, rfl⟩
end
/-- pigeonhole principle for a cardinality below the cardinality of the domain -/
theorem infinite_pigeonhole_card {β α : Type u} (f : β → α) (θ : cardinal) (hθ : θ ≤ mk β)
(h₁ : cardinal.omega ≤ θ) (h₂ : mk α < θ.ord.cof) : ∃a : α, θ ≤ mk (f ⁻¹' {a}) :=
begin
rcases le_mk_iff_exists_set.1 hθ with ⟨s, rfl⟩,
cases infinite_pigeonhole (f ∘ subtype.val : s → α) h₁ h₂ with a ha,
use a, rw [←ha, @preimage_comp _ _ _ subtype.val f],
apply mk_preimage_of_injective _ _ subtype.val_injective
end
theorem infinite_pigeonhole_set {β α : Type u} {s : set β} (f : s → α) (θ : cardinal)
(hθ : θ ≤ mk s) (h₁ : cardinal.omega ≤ θ) (h₂ : mk α < θ.ord.cof) :
∃(a : α) (t : set β) (h : t ⊆ s), θ ≤ mk t ∧ ∀{{x}} (hx : x ∈ t), f ⟨x, h hx⟩ = a :=
begin
cases infinite_pigeonhole_card f θ hθ h₁ h₂ with a ha,
refine ⟨a, {x | ∃(h : x ∈ s), f ⟨x, h⟩ = a}, _, _, _⟩,
{ rintro x ⟨hx, hx'⟩, exact hx },
{ refine le_trans ha _, apply ge_of_eq, apply quotient.sound, constructor,
refine equiv.trans _ (equiv.subtype_subtype_equiv_subtype_exists _ _).symm,
simp only [set_coe_eq_subtype, mem_singleton_iff, mem_preimage, mem_set_of_eq] },
rintro x ⟨hx, hx'⟩, exact hx'
end
end ordinal
namespace cardinal
open ordinal
local infixr ^ := @pow cardinal.{u} cardinal cardinal.has_pow
/-- A cardinal is a limit if it is not zero or a successor
cardinal. Note that `ω` is a limit cardinal by this definition. -/
def is_limit (c : cardinal) : Prop :=
c ≠ 0 ∧ ∀ x < c, succ x < c
/-- A cardinal is a strong limit if it is not zero and it is
closed under powersets. Note that `ω` is a strong limit by this definition. -/
def is_strong_limit (c : cardinal) : Prop :=
c ≠ 0 ∧ ∀ x < c, 2 ^ x < c
theorem is_strong_limit.is_limit {c} (H : is_strong_limit c) : is_limit c :=
⟨H.1, λ x h, lt_of_le_of_lt (succ_le.2 $ cantor _) (H.2 _ h)⟩
/-- A cardinal is regular if it is infinite and it equals its own cofinality. -/
def is_regular (c : cardinal) : Prop :=
omega ≤ c ∧ c.ord.cof = c
theorem cof_is_regular {o : ordinal} (h : o.is_limit) : is_regular o.cof :=
⟨omega_le_cof.2 h, cof_cof _⟩
theorem omega_is_regular : is_regular omega :=
⟨le_refl _, by simp⟩
theorem succ_is_regular {c : cardinal.{u}} (h : omega ≤ c) : is_regular (succ c) :=
⟨le_trans h (le_of_lt $ lt_succ_self _), begin
refine le_antisymm (cof_ord_le _) (succ_le.2 _),
cases quotient.exists_rep (succ c) with α αe, simp at αe,
rcases ord_eq α with ⟨r, wo, re⟩, resetI,
have := ord_is_limit (le_trans h $ le_of_lt $ lt_succ_self _),
rw [← αe, re] at this ⊢,
rcases cof_eq' r this with ⟨S, H, Se⟩,
rw [← Se],
apply lt_imp_lt_of_le_imp_le (mul_le_mul_right c),
rw [mul_eq_self h, ← succ_le, ← αe, ← sum_const],
refine le_trans _ (sum_le_sum (λ x:S, card (typein r x)) _ _),
{ simp [typein, sum_mk (λ x:S, {a//r a x})],
refine ⟨embedding.of_surjective _ _⟩,
{ exact λ x, x.2.1 },
{ exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } },
{ intro i,
rw [← lt_succ, ← lt_ord, ← αe, re],
apply typein_lt_type }
end⟩
theorem sup_lt_ord_of_is_regular {ι} (f : ι → ordinal)
{c} (hc : is_regular c) (H1 : cardinal.mk ι < c)
(H2 : ∀ i, f i < c.ord) : ordinal.sup.{u u} f < c.ord :=
by { apply sup_lt_ord _ _ H2, rw [hc.2], exact H1 }
theorem sup_lt_of_is_regular {ι} (f : ι → cardinal)
{c} (hc : is_regular c) (H1 : cardinal.mk ι < c)
(H2 : ∀ i, f i < c) : sup.{u u} f < c :=
by { apply sup_lt _ _ H2, rwa [hc.2] }
theorem sum_lt_of_is_regular {ι} (f : ι → cardinal)
{c} (hc : is_regular c) (H1 : cardinal.mk ι < c)
(H2 : ∀ i, f i < c) : sum.{u u} f < c :=
lt_of_le_of_lt (sum_le_sup _) $ mul_lt_of_lt hc.1 H1 $
sup_lt_of_is_regular f hc H1 H2
/-- A cardinal is inaccessible if it is an
uncountable regular strong limit cardinal. -/
def is_inaccessible (c : cardinal) :=
omega < c ∧ is_regular c ∧ is_strong_limit c
theorem is_inaccessible.mk {c}
(h₁ : omega < c) (h₂ : c ≤ c.ord.cof) (h₃ : ∀ x < c, 2 ^ x < c) :
is_inaccessible c :=
⟨h₁, ⟨le_of_lt h₁, le_antisymm (cof_ord_le _) h₂⟩,
ne_of_gt (lt_trans omega_pos h₁), h₃⟩
/- Lean's foundations prove the existence of ω many inaccessible
cardinals -/
theorem univ_inaccessible : is_inaccessible (univ.{u v}) :=
is_inaccessible.mk
(by simpa using lift_lt_univ' omega)
(by simp)
(λ c h, begin
rcases lt_univ'.1 h with ⟨c, rfl⟩,
rw ← lift_two_power.{u (max (u+1) v)},
apply lift_lt_univ'
end)
theorem lt_power_cof {c : cardinal.{u}} : omega ≤ c → c < c ^ cof c.ord :=
quotient.induction_on c $ λ α h, begin
rcases ord_eq α with ⟨r, wo, re⟩, resetI,
have := ord_is_limit h,
rw [mk_def, re] at this ⊢,
rcases cof_eq' r this with ⟨S, H, Se⟩,
have := sum_lt_prod (λ a:S, mk {x // r x a}) (λ _, mk α) (λ i, _),
{ simp [Se.symm] at this ⊢,
refine lt_of_le_of_lt _ this,
refine ⟨embedding.of_surjective _ _⟩,
{ exact λ x, x.2.1 },
{ exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } },
{ have := typein_lt_type r i,
rwa [← re, lt_ord] at this }
end
theorem lt_cof_power {a b : cardinal} (ha : omega ≤ a) (b1 : 1 < b) :
a < cof (b ^ a).ord :=
begin
have b0 : b ≠ 0 := ne_of_gt (lt_trans zero_lt_one b1),
apply lt_imp_lt_of_le_imp_le (power_le_power_left $ power_ne_zero a b0),
rw [power_mul, mul_eq_self ha],
exact lt_power_cof (le_trans ha $ le_of_lt $ cantor' _ b1),
end
end cardinal
|
b2cb98e8b205b9df519181a9d1cb1b6f444530b6 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/run/induniv.lean | 62314313394b57ba2c0193d656d90bc534561fa2 | [
"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,015 | lean | inductive list (A : Type) : Type :=
nil {} : list A,
cons : A → list A → list A
section
variable A : Type
inductive list2 : Type :=
nil2 {} : list2,
cons2 : A → list2 → list2
end
constant num : Type.{1}
namespace Tree
inductive tree (A : Type) : Type :=
node : A → forest A → tree A
with forest : Type :=
nil : forest A,
cons : tree A → forest A → forest A
end Tree
inductive group_struct (A : Type) : Type :=
mk_group_struct : (A → A → A) → A → group_struct A
inductive group : Type :=
mk_group : Π (A : Type), (A → A → A) → A → group
section
variable A : Type
variable B : Type
inductive pair : Type :=
mk_pair : A → B → pair
end
definition Prop := Type.{0}
inductive eq {A : Type} (a : A) : A → Prop :=
refl : eq a a
section
variable {A : Type}
inductive eq2 (a : A) : A → Prop :=
refl2 : eq2 a a
end
section
variable A : Type
variable B : Type
inductive triple (C : Type) : Type :=
mk_triple : A → B → C → triple C
end
|
18488874fa2a539c05bd055a2085224a12930b53 | f4bff2062c030df03d65e8b69c88f79b63a359d8 | /src/game/topology/hausdorff.lean | ac56513847e517545723a0e16970b726aea6fc26 | [
"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,809 | lean | import data.real.basic
--begin hide
namespace xena
def neighborhood1 (a : ℝ) (ε : ℝ) (h : ε > 0) := { x : ℝ | abs(a - x) < ε}
/-
To be technically correct, the definition below should include
the hypothesis that ε > 0, see the one above.
But that does raise some issues which I don't know how to handle
when using it in the main result lemma.
-/
-- end hide
def neighborhood (a : ℝ) (ε : ℝ) := { x : ℝ | abs(a - x) < ε}
local attribute [instance] classical.prop_decidable --hide
/- Lemma
Hausdorff property for the reals.
-/
lemma hausdorff_reals (a b : ℝ) (hne : a ≠ b) :
∃ (ε:ℝ), ε > 0 ∧ (neighborhood a ε ∩ neighborhood b ε = ∅) :=
begin
set d := abs(b-a) with hd,
have h1 : 0 < d,
rw hd, by_contradiction hf, push_neg at hf,
have h11 := abs_nonneg (b-a),
have h12 : abs(b-a) = 0, linarith,
have h13 : (b-a) = 0, exact abs_eq_zero.1 h12,
have h14 : a = b, linarith,
exact hne h14,
set e := d / 3 with he,
use e, split, linarith,
by_contradiction H,
-- the stuff below can probably be made much shorter
have G := set.ne_empty_iff_nonempty.mp H,
cases G with x hab, cases hab with ha hb,
have ha1 : abs(a-x) < e, exact ha, -- linarith below won't work
have hb1 : abs(b-x) < e, exact hb, -- without these
have hb2 : abs(b-x) = abs(x-b), exact abs_sub _ _,
rw hb2 at hb1,
have hab := abs_add (a-x) (x-b),
have hab1 : a - x + (x - b) = a - b, ring,
rw hab1 at hab,
have hab2 : abs(a-b) < e + e, linarith,
have hdd : d = 3 * e, rw he, linarith,
rw hdd at hd,
have hde := eq.symm hd,
have hdf : abs(b-a) > 2 * e,
linarith,
have hdg : e + e = 2 * e, linarith,
rw hdg at hab2,
have hb2 : abs(b-a) = abs(a-b), exact abs_sub _ _,
rw hb2 at hdf, linarith, done
end
end xena -- hide
|
3bd377709538d10fc1aab41f33eef7af9a037147 | bb31430994044506fa42fd667e2d556327e18dfe | /src/order/with_bot.lean | 93d3cd2093e2c86012698d716c195f23142c3fa8 | [
"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 | 37,476 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import order.bounded_order
/-!
# `with_bot`, `with_top`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Adding a `bot` or a `top` to an order.
## Main declarations
* `with_<top/bot> α`: Equips `option α` with the order on `α` plus `none` as the top/bottom element.
-/
variables {α β γ δ : Type*}
/-- Attach `⊥` to a type. -/
def with_bot (α : Type*) := option α
namespace with_bot
variables {a b : α}
meta instance [has_to_format α] : has_to_format (with_bot α) :=
{ to_format := λ x,
match x with
| none := "⊥"
| (some x) := to_fmt x
end }
instance [has_repr α] : has_repr (with_bot α) :=
⟨λ o, match o with | none := "⊥" | (some a) := "↑" ++ repr a end⟩
instance : has_coe_t α (with_bot α) := ⟨some⟩
instance : has_bot (with_bot α) := ⟨none⟩
meta instance {α : Type} [reflected _ α] [has_reflect α] : has_reflect (with_bot α)
| ⊥ := `(⊥)
| (a : α) := `(coe : α → with_bot α).subst `(a)
instance : inhabited (with_bot α) := ⟨⊥⟩
open function
lemma coe_injective : injective (coe : α → with_bot α) := option.some_injective _
@[norm_cast] lemma coe_inj : (a : with_bot α) = b ↔ a = b := option.some_inj
protected lemma «forall» {p : with_bot α → Prop} : (∀ x, p x) ↔ p ⊥ ∧ ∀ x : α, p x := option.forall
protected lemma «exists» {p : with_bot α → Prop} : (∃ x, p x) ↔ p ⊥ ∨ ∃ x : α, p x := option.exists
lemma none_eq_bot : (none : with_bot α) = (⊥ : with_bot α) := rfl
lemma some_eq_coe (a : α) : (some a : with_bot α) = (↑a : with_bot α) := rfl
@[simp] lemma bot_ne_coe : ⊥ ≠ (a : with_bot α) .
@[simp] lemma coe_ne_bot : (a : with_bot α) ≠ ⊥ .
/-- Recursor for `with_bot` using the preferred forms `⊥` and `↑a`. -/
@[elab_as_eliminator]
def rec_bot_coe {C : with_bot α → Sort*} (h₁ : C ⊥) (h₂ : Π (a : α), C a) :
Π (n : with_bot α), C n :=
option.rec h₁ h₂
@[simp] lemma rec_bot_coe_bot {C : with_bot α → Sort*} (d : C ⊥) (f : Π (a : α), C a) :
@rec_bot_coe _ C d f ⊥ = d := rfl
@[simp] lemma rec_bot_coe_coe {C : with_bot α → Sort*} (d : C ⊥) (f : Π (a : α), C a)
(x : α) : @rec_bot_coe _ C d f ↑x = f x := rfl
/-- Specialization of `option.get_or_else` to values in `with_bot α` that respects API boundaries.
-/
def unbot' (d : α) (x : with_bot α) : α := rec_bot_coe d id x
@[simp] lemma unbot'_bot {α} (d : α) : unbot' d ⊥ = d := rfl
@[simp] lemma unbot'_coe {α} (d x : α) : unbot' d x = x := rfl
@[norm_cast] lemma coe_eq_coe : (a : with_bot α) = b ↔ a = b := option.some_inj
/-- Lift a map `f : α → β` to `with_bot α → with_bot β`. Implemented using `option.map`. -/
def map (f : α → β) : with_bot α → with_bot β := option.map f
@[simp] lemma map_bot (f : α → β) : map f ⊥ = ⊥ := rfl
@[simp] lemma map_coe (f : α → β) (a : α) : map f a = f a := rfl
lemma map_comm {f₁ : α → β} {f₂ : α → γ} {g₁ : β → δ} {g₂ : γ → δ} (h : g₁ ∘ f₁ = g₂ ∘ f₂) (a : α) :
map g₁ (map f₁ a) = map g₂ (map f₂ a) :=
option.map_comm h _
lemma ne_bot_iff_exists {x : with_bot α} : x ≠ ⊥ ↔ ∃ (a : α), ↑a = x := option.ne_none_iff_exists
/-- Deconstruct a `x : with_bot α` to the underlying value in `α`, given a proof that `x ≠ ⊥`. -/
def unbot : Π (x : with_bot α), x ≠ ⊥ → α
| ⊥ h := absurd rfl h
| (some x) h := x
@[simp] lemma coe_unbot (x : with_bot α) (h : x ≠ ⊥) : (x.unbot h : with_bot α) = x :=
by { cases x, simpa using h, refl, }
@[simp] lemma unbot_coe (x : α) (h : (x : with_bot α) ≠ ⊥ := coe_ne_bot) :
(x : with_bot α).unbot h = x := rfl
instance can_lift : can_lift (with_bot α) α coe (λ r, r ≠ ⊥) :=
{ prf := λ x h, ⟨x.unbot h, coe_unbot _ _⟩ }
section has_le
variables [has_le α]
@[priority 10]
instance : has_le (with_bot α) := ⟨λ o₁ o₂ : option α, ∀ a ∈ o₁, ∃ b ∈ o₂, a ≤ b⟩
@[simp] lemma some_le_some : @has_le.le (with_bot α) _ (some a) (some b) ↔ a ≤ b := by simp [(≤)]
@[simp, norm_cast] lemma coe_le_coe : (a : with_bot α) ≤ b ↔ a ≤ b := some_le_some
@[simp] lemma none_le {a : with_bot α} : @has_le.le (with_bot α) _ none a :=
λ b h, option.no_confusion h
instance : order_bot (with_bot α) := { bot_le := λ a, none_le, ..with_bot.has_bot }
instance [order_top α] : order_top (with_bot α) :=
{ top := some ⊤,
le_top := λ o a ha, by cases ha; exact ⟨_, rfl, le_top⟩ }
instance [order_top α] : bounded_order (with_bot α) :=
{ ..with_bot.order_top, ..with_bot.order_bot }
lemma not_coe_le_bot (a : α) : ¬ (a : with_bot α) ≤ ⊥ :=
λ h, let ⟨b, hb, _⟩ := h _ rfl in option.not_mem_none _ hb
lemma coe_le : ∀ {o : option α}, b ∈ o → ((a : with_bot α) ≤ o ↔ a ≤ b) | _ rfl := coe_le_coe
lemma coe_le_iff : ∀ {x : with_bot α}, ↑a ≤ x ↔ ∃ b : α, x = b ∧ a ≤ b
| (some a) := by simp [some_eq_coe, coe_eq_coe]
| none := iff_of_false (not_coe_le_bot _) $ by simp [none_eq_bot]
lemma le_coe_iff : ∀ {x : with_bot α}, x ≤ b ↔ ∀ a, x = ↑a → a ≤ b
| (some b) := by simp [some_eq_coe, coe_eq_coe]
| none := by simp [none_eq_bot]
protected lemma _root_.is_max.with_bot (h : is_max a) : is_max (a : with_bot α)
| none _ := bot_le
| (some b) hb := some_le_some.2 $ h $ some_le_some.1 hb
end has_le
section has_lt
variables [has_lt α]
@[priority 10]
instance : has_lt (with_bot α) := ⟨λ o₁ o₂ : option α, ∃ b ∈ o₂, ∀ a ∈ o₁, a < b⟩
@[simp] lemma some_lt_some : @has_lt.lt (with_bot α) _ (some a) (some b) ↔ a < b := by simp [(<)]
@[simp, norm_cast] lemma coe_lt_coe : (a : with_bot α) < b ↔ a < b := some_lt_some
@[simp] lemma none_lt_some (a : α) : @has_lt.lt (with_bot α) _ none (some a) :=
⟨a, rfl, λ b hb, (option.not_mem_none _ hb).elim⟩
lemma bot_lt_coe (a : α) : (⊥ : with_bot α) < a := none_lt_some a
@[simp] lemma not_lt_none (a : with_bot α) : ¬ @has_lt.lt (with_bot α) _ a none :=
λ ⟨_, h, _⟩, option.not_mem_none _ h
lemma lt_iff_exists_coe : ∀ {a b : with_bot α}, a < b ↔ ∃ p : α, b = p ∧ a < p
| a (some b) := by simp [some_eq_coe, coe_eq_coe]
| a none := iff_of_false (not_lt_none _) $ by simp [none_eq_bot]
lemma lt_coe_iff : ∀ {x : with_bot α}, x < b ↔ ∀ a, x = ↑a → a < b
| (some b) := by simp [some_eq_coe, coe_eq_coe, coe_lt_coe]
| none := by simp [none_eq_bot, bot_lt_coe]
end has_lt
instance [preorder α] : preorder (with_bot α) :=
{ le := (≤),
lt := (<),
lt_iff_le_not_le := by { intros, cases a; cases b; simp [lt_iff_le_not_le]; simp [(<), (≤)] },
le_refl := λ o a ha, ⟨a, ha, le_rfl⟩,
le_trans := λ o₁ o₂ o₃ h₁ h₂ a ha,
let ⟨b, hb, ab⟩ := h₁ a ha, ⟨c, hc, bc⟩ := h₂ b hb in
⟨c, hc, le_trans ab bc⟩ }
instance [partial_order α] : partial_order (with_bot α) :=
{ le_antisymm := λ o₁ o₂ h₁ h₂, begin
cases o₁ with a,
{ cases o₂ with b, {refl},
rcases h₂ b rfl with ⟨_, ⟨⟩, _⟩ },
{ rcases h₁ a rfl with ⟨b, ⟨⟩, h₁'⟩,
rcases h₂ b rfl with ⟨_, ⟨⟩, h₂'⟩,
rw le_antisymm h₁' h₂' }
end,
.. with_bot.preorder }
lemma coe_strict_mono [preorder α] : strict_mono (coe : α → with_bot α) := λ a b, some_lt_some.2
lemma coe_mono [preorder α] : monotone (coe : α → with_bot α) := λ a b, coe_le_coe.2
lemma monotone_iff [preorder α] [preorder β] {f : with_bot α → β} :
monotone f ↔ monotone (f ∘ coe : α → β) ∧ ∀ x : α, f ⊥ ≤ f x :=
⟨λ h, ⟨h.comp with_bot.coe_mono, λ x, h bot_le⟩,
λ h, with_bot.forall.2 ⟨with_bot.forall.2 ⟨λ _, le_rfl, λ x _, h.2 x⟩,
λ x, with_bot.forall.2 ⟨λ h, (not_coe_le_bot _ h).elim, λ y hle, h.1 (coe_le_coe.1 hle)⟩⟩⟩
@[simp] lemma monotone_map_iff [preorder α] [preorder β] {f : α → β} :
monotone (with_bot.map f) ↔ monotone f :=
monotone_iff.trans $ by simp [monotone]
alias monotone_map_iff ↔ _ _root_.monotone.with_bot_map
lemma strict_mono_iff [preorder α] [preorder β] {f : with_bot α → β} :
strict_mono f ↔ strict_mono (f ∘ coe : α → β) ∧ ∀ x : α, f ⊥ < f x :=
⟨λ h, ⟨h.comp with_bot.coe_strict_mono, λ x, h (bot_lt_coe _)⟩,
λ h, with_bot.forall.2 ⟨with_bot.forall.2 ⟨flip absurd (lt_irrefl _), λ x _, h.2 x⟩,
λ x, with_bot.forall.2 ⟨λ h, (not_lt_bot h).elim, λ y hle, h.1 (coe_lt_coe.1 hle)⟩⟩⟩
@[simp] lemma strict_mono_map_iff [preorder α] [preorder β] {f : α → β} :
strict_mono (with_bot.map f) ↔ strict_mono f :=
strict_mono_iff.trans $ by simp [strict_mono, bot_lt_coe]
alias strict_mono_map_iff ↔ _ _root_.strict_mono.with_bot_map
lemma map_le_iff [preorder α] [preorder β] (f : α → β) (mono_iff : ∀ {a b}, f a ≤ f b ↔ a ≤ b) :
∀ (a b : with_bot α), a.map f ≤ b.map f ↔ a ≤ b
| ⊥ _ := by simp only [map_bot, bot_le]
| (a : α) ⊥ := by simp only [map_coe, map_bot, coe_ne_bot, not_coe_le_bot _]
| (a : α) (b : α) := by simpa only [map_coe, coe_le_coe] using mono_iff
lemma le_coe_unbot' [preorder α] : ∀ (a : with_bot α) (b : α), a ≤ a.unbot' b
| (a : α) b := le_rfl
| ⊥ b := bot_le
lemma unbot'_bot_le_iff [has_le α] [order_bot α] {a : with_bot α} {b : α} :
a.unbot' ⊥ ≤ b ↔ a ≤ b :=
by cases a; simp [none_eq_bot, some_eq_coe]
lemma unbot'_lt_iff [has_lt α] {a : with_bot α} {b c : α} (ha : a ≠ ⊥) :
a.unbot' b < c ↔ a < c :=
begin
lift a to α using ha,
rw [unbot'_coe, coe_lt_coe]
end
instance [semilattice_sup α] : semilattice_sup (with_bot α) :=
{ sup := option.lift_or_get (⊔),
le_sup_left := λ o₁ o₂ a ha,
by cases ha; cases o₂; simp [option.lift_or_get],
le_sup_right := λ o₁ o₂ a ha,
by cases ha; cases o₁; simp [option.lift_or_get],
sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin
cases o₁ with b; cases o₂ with c; cases ha,
{ exact h₂ a rfl },
{ exact h₁ a rfl },
{ rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩,
simp at h₂,
exact ⟨d, rfl, sup_le h₁' h₂⟩ }
end,
..with_bot.order_bot,
..with_bot.partial_order }
lemma coe_sup [semilattice_sup α] (a b : α) : ((a ⊔ b : α) : with_bot α) = a ⊔ b := rfl
instance [semilattice_inf α] : semilattice_inf (with_bot α) :=
{ inf := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊓ b)),
inf_le_left := λ o₁ o₂ a ha, begin
simp [map] at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩,
exact ⟨_, rfl, inf_le_left⟩
end,
inf_le_right := λ o₁ o₂ a ha, begin
simp [map] at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩,
exact ⟨_, rfl, inf_le_right⟩
end,
le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin
cases ha,
rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩,
rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩,
exact ⟨_, rfl, le_inf ab ac⟩
end,
..with_bot.order_bot,
..with_bot.partial_order }
lemma coe_inf [semilattice_inf α] (a b : α) : ((a ⊓ b : α) : with_bot α) = a ⊓ b := rfl
instance [lattice α] : lattice (with_bot α) :=
{ ..with_bot.semilattice_sup, ..with_bot.semilattice_inf }
instance [distrib_lattice α] : distrib_lattice (with_bot α) :=
{ le_sup_inf := λ o₁ o₂ o₃,
match o₁, o₂, o₃ with
| ⊥, ⊥, ⊥ := le_rfl
| ⊥, ⊥, (a₁ : α) := le_rfl
| ⊥, (a₁ : α), ⊥ := le_rfl
| ⊥, (a₁ : α), (a₃ : α) := le_rfl
| (a₁ : α), ⊥, ⊥ := inf_le_left
| (a₁ : α), ⊥, (a₃ : α) := inf_le_left
| (a₁ : α), (a₂ : α), ⊥ := inf_le_right
| (a₁ : α), (a₂ : α), (a₃ : α) := coe_le_coe.mpr le_sup_inf
end,
..with_bot.lattice }
instance decidable_le [has_le α] [@decidable_rel α (≤)] : @decidable_rel (with_bot α) (≤)
| none x := is_true $ λ a h, option.no_confusion h
| (some x) (some y) :=
if h : x ≤ y
then is_true (some_le_some.2 h)
else is_false $ by simp *
| (some x) none := is_false $ λ h, by rcases h x rfl with ⟨y, ⟨_⟩, _⟩
instance decidable_lt [has_lt α] [@decidable_rel α (<)] : @decidable_rel (with_bot α) (<)
| none (some x) := is_true $ by existsi [x,rfl]; rintros _ ⟨⟩
| (some x) (some y) :=
if h : x < y
then is_true $ by simp *
else is_false $ by simp *
| x none := is_false $ by rintro ⟨a,⟨⟨⟩⟩⟩
instance is_total_le [has_le α] [is_total α (≤)] : is_total (with_bot α) (≤) :=
⟨λ a b, match a, b with
| none , _ := or.inl bot_le
| _ , none := or.inr bot_le
| some x, some y := (total_of (≤) x y).imp some_le_some.2 some_le_some.2
end⟩
instance [linear_order α] : linear_order (with_bot α) := lattice.to_linear_order _
@[norm_cast] -- this is not marked simp because the corresponding with_top lemmas are used
lemma coe_min [linear_order α] (x y : α) : ((min x y : α) : with_bot α) = min x y := rfl
@[norm_cast] -- this is not marked simp because the corresponding with_top lemmas are used
lemma coe_max [linear_order α] (x y : α) : ((max x y : α) : with_bot α) = max x y := rfl
lemma well_founded_lt [preorder α] (h : @well_founded α (<)) : @well_founded (with_bot α) (<) :=
have acc_bot : acc ((<) : with_bot α → with_bot α → Prop) ⊥ :=
acc.intro _ (λ a ha, (not_le_of_gt ha bot_le).elim),
⟨λ a, option.rec_on a acc_bot (λ a, acc.intro _ (λ b, option.rec_on b (λ _, acc_bot)
(λ b, well_founded.induction h b
(show ∀ b : α, (∀ c, c < b → (c : with_bot α) < a →
acc ((<) : with_bot α → with_bot α → Prop) c) → (b : with_bot α) < a →
acc ((<) : with_bot α → with_bot α → Prop) b,
from λ b ih hba, acc.intro _ (λ c, option.rec_on c (λ _, acc_bot)
(λ c hc, ih _ (some_lt_some.1 hc) (lt_trans hc hba)))))))⟩
instance [has_lt α] [densely_ordered α] [no_min_order α] : densely_ordered (with_bot α) :=
⟨ λ a b,
match a, b with
| a, none := λ h : a < ⊥, (not_lt_none _ h).elim
| none, some b := λ h, let ⟨a, ha⟩ := exists_lt b in ⟨a, bot_lt_coe a, coe_lt_coe.2 ha⟩
| some a, some b := λ h, let ⟨a, ha₁, ha₂⟩ := exists_between (coe_lt_coe.1 h) in
⟨a, coe_lt_coe.2 ha₁, coe_lt_coe.2 ha₂⟩
end⟩
lemma lt_iff_exists_coe_btwn [preorder α] [densely_ordered α] [no_min_order α] {a b : with_bot α} :
a < b ↔ ∃ x : α, a < ↑x ∧ ↑x < b :=
⟨λ h, let ⟨y, hy⟩ := exists_between h, ⟨x, hx⟩ := lt_iff_exists_coe.1 hy.1 in ⟨x, hx.1 ▸ hy⟩,
λ ⟨x, hx⟩, lt_trans hx.1 hx.2⟩
instance [has_le α] [no_top_order α] [nonempty α] : no_top_order (with_bot α) :=
⟨begin
apply rec_bot_coe,
{ exact ‹nonempty α›.elim (λ a, ⟨a, not_coe_le_bot a⟩) },
{ intro a,
obtain ⟨b, h⟩ := exists_not_le a,
exact ⟨b, by rwa coe_le_coe⟩ }
end⟩
instance [has_lt α] [no_max_order α] [nonempty α] : no_max_order (with_bot α) :=
⟨begin
apply with_bot.rec_bot_coe,
{ apply ‹nonempty α›.elim,
exact λ a, ⟨a, with_bot.bot_lt_coe a⟩, },
{ intro a,
obtain ⟨b, ha⟩ := exists_gt a,
exact ⟨b, with_bot.coe_lt_coe.mpr ha⟩, }
end⟩
end with_bot
--TODO(Mario): Construct using order dual on with_bot
/-- Attach `⊤` to a type. -/
def with_top (α : Type*) := option α
namespace with_top
variables {a b : α}
meta instance [has_to_format α] : has_to_format (with_top α) :=
{ to_format := λ x,
match x with
| none := "⊤"
| (some x) := to_fmt x
end }
instance [has_repr α] : has_repr (with_top α) :=
⟨λ o, match o with | none := "⊤" | (some a) := "↑" ++ repr a end⟩
instance : has_coe_t α (with_top α) := ⟨some⟩
instance : has_top (with_top α) := ⟨none⟩
meta instance {α : Type} [reflected _ α] [has_reflect α] : has_reflect (with_top α)
| ⊤ := `(⊤)
| (a : α) := `(coe : α → with_top α).subst `(a)
instance : inhabited (with_top α) := ⟨⊤⟩
protected lemma «forall» {p : with_top α → Prop} : (∀ x, p x) ↔ p ⊤ ∧ ∀ x : α, p x := option.forall
protected lemma «exists» {p : with_top α → Prop} : (∃ x, p x) ↔ p ⊤ ∨ ∃ x : α, p x := option.exists
lemma none_eq_top : (none : with_top α) = (⊤ : with_top α) := rfl
lemma some_eq_coe (a : α) : (some a : with_top α) = (↑a : with_top α) := rfl
@[simp] lemma top_ne_coe : ⊤ ≠ (a : with_top α) .
@[simp] lemma coe_ne_top : (a : with_top α) ≠ ⊤ .
/-- Recursor for `with_top` using the preferred forms `⊤` and `↑a`. -/
@[elab_as_eliminator]
def rec_top_coe {C : with_top α → Sort*} (h₁ : C ⊤) (h₂ : Π (a : α), C a) :
Π (n : with_top α), C n :=
option.rec h₁ h₂
@[simp] lemma rec_top_coe_top {C : with_top α → Sort*} (d : C ⊤) (f : Π (a : α), C a) :
@rec_top_coe _ C d f ⊤ = d := rfl
@[simp] lemma rec_top_coe_coe {C : with_top α → Sort*} (d : C ⊤) (f : Π (a : α), C a)
(x : α) : @rec_top_coe _ C d f ↑x = f x := rfl
/-- `with_top.to_dual` is the equivalence sending `⊤` to `⊥` and any `a : α` to `to_dual a : αᵒᵈ`.
See `with_top.to_dual_bot_equiv` for the related order-iso.
-/
protected def to_dual : with_top α ≃ with_bot αᵒᵈ := equiv.refl _
/-- `with_top.of_dual` is the equivalence sending `⊤` to `⊥` and any `a : αᵒᵈ` to `of_dual a : α`.
See `with_top.to_dual_bot_equiv` for the related order-iso.
-/
protected def of_dual : with_top αᵒᵈ ≃ with_bot α := equiv.refl _
/-- `with_bot.to_dual` is the equivalence sending `⊥` to `⊤` and any `a : α` to `to_dual a : αᵒᵈ`.
See `with_bot.to_dual_top_equiv` for the related order-iso.
-/
protected def _root_.with_bot.to_dual : with_bot α ≃ with_top αᵒᵈ := equiv.refl _
/-- `with_bot.of_dual` is the equivalence sending `⊥` to `⊤` and any `a : αᵒᵈ` to `of_dual a : α`.
See `with_bot.to_dual_top_equiv` for the related order-iso.
-/
protected def _root_.with_bot.of_dual : with_bot αᵒᵈ ≃ with_top α := equiv.refl _
@[simp] lemma to_dual_symm_apply (a : with_bot αᵒᵈ) :
with_top.to_dual.symm a = a.of_dual := rfl
@[simp] lemma of_dual_symm_apply (a : with_bot α) :
with_top.of_dual.symm a = a.to_dual := rfl
@[simp] lemma to_dual_apply_top : with_top.to_dual (⊤ : with_top α) = ⊥ := rfl
@[simp] lemma of_dual_apply_top : with_top.of_dual (⊤ : with_top α) = ⊥ := rfl
open order_dual
@[simp] lemma to_dual_apply_coe (a : α) : with_top.to_dual (a : with_top α) = to_dual a := rfl
@[simp] lemma of_dual_apply_coe (a : αᵒᵈ) : with_top.of_dual (a : with_top αᵒᵈ) = of_dual a := rfl
/-- Specialization of `option.get_or_else` to values in `with_top α` that respects API boundaries.
-/
def untop' (d : α) (x : with_top α) : α := rec_top_coe d id x
@[simp] lemma untop'_top {α} (d : α) : untop' d ⊤ = d := rfl
@[simp] lemma untop'_coe {α} (d x : α) : untop' d x = x := rfl
@[norm_cast] lemma coe_eq_coe : (a : with_top α) = b ↔ a = b := option.some_inj
/-- Lift a map `f : α → β` to `with_top α → with_top β`. Implemented using `option.map`. -/
def map (f : α → β) : with_top α → with_top β := option.map f
@[simp] lemma map_top (f : α → β) : map f ⊤ = ⊤ := rfl
@[simp] lemma map_coe (f : α → β) (a : α) : map f a = f a := rfl
lemma map_comm {f₁ : α → β} {f₂ : α → γ} {g₁ : β → δ} {g₂ : γ → δ} (h : g₁ ∘ f₁ = g₂ ∘ f₂) (a : α) :
map g₁ (map f₁ a) = map g₂ (map f₂ a) :=
option.map_comm h _
lemma map_to_dual (f : αᵒᵈ → βᵒᵈ) (a : with_bot α) :
map f (with_bot.to_dual a) = a.map (to_dual ∘ f) := rfl
lemma map_of_dual (f : α → β) (a : with_bot αᵒᵈ) :
map f (with_bot.of_dual a) = a.map (of_dual ∘ f) := rfl
lemma to_dual_map (f : α → β) (a : with_top α) :
with_top.to_dual (map f a) = with_bot.map (to_dual ∘ f ∘ of_dual) a.to_dual := rfl
lemma of_dual_map (f : αᵒᵈ → βᵒᵈ) (a : with_top αᵒᵈ) :
with_top.of_dual (map f a) = with_bot.map (of_dual ∘ f ∘ to_dual) a.of_dual := rfl
lemma ne_top_iff_exists {x : with_top α} : x ≠ ⊤ ↔ ∃ (a : α), ↑a = x := option.ne_none_iff_exists
/-- Deconstruct a `x : with_top α` to the underlying value in `α`, given a proof that `x ≠ ⊤`. -/
def untop : Π (x : with_top α), x ≠ ⊤ → α :=
with_bot.unbot
@[simp] lemma coe_untop (x : with_top α) (h : x ≠ ⊤) : (x.untop h : with_top α) = x :=
with_bot.coe_unbot x h
@[simp] lemma untop_coe (x : α) (h : (x : with_top α) ≠ ⊤ := coe_ne_top) :
(x : with_top α).untop h = x := rfl
instance can_lift : can_lift (with_top α) α coe (λ r, r ≠ ⊤) :=
{ prf := λ x h, ⟨x.untop h, coe_untop _ _⟩ }
section has_le
variables [has_le α]
@[priority 10]
instance : has_le (with_top α) := ⟨λ o₁ o₂ : option α, ∀ a ∈ o₂, ∃ b ∈ o₁, b ≤ a⟩
lemma to_dual_le_iff {a : with_top α} {b : with_bot αᵒᵈ} :
with_top.to_dual a ≤ b ↔ with_bot.of_dual b ≤ a := iff.rfl
lemma le_to_dual_iff {a : with_bot αᵒᵈ} {b : with_top α} :
a ≤ with_top.to_dual b ↔ b ≤ with_bot.of_dual a := iff.rfl
@[simp] lemma to_dual_le_to_dual_iff {a b : with_top α} :
with_top.to_dual a ≤ with_top.to_dual b ↔ b ≤ a := iff.rfl
lemma of_dual_le_iff {a : with_top αᵒᵈ} {b : with_bot α} :
with_top.of_dual a ≤ b ↔ with_bot.to_dual b ≤ a := iff.rfl
lemma le_of_dual_iff {a : with_bot α} {b : with_top αᵒᵈ} :
a ≤ with_top.of_dual b ↔ b ≤ with_bot.to_dual a := iff.rfl
@[simp] lemma of_dual_le_of_dual_iff {a b : with_top αᵒᵈ} :
with_top.of_dual a ≤ with_top.of_dual b ↔ b ≤ a := iff.rfl
@[simp, norm_cast] lemma coe_le_coe : (a : with_top α) ≤ b ↔ a ≤ b :=
by simp only [←to_dual_le_to_dual_iff, to_dual_apply_coe, with_bot.coe_le_coe, to_dual_le_to_dual]
@[simp] lemma some_le_some : @has_le.le (with_top α) _ (some a) (some b) ↔ a ≤ b := coe_le_coe
@[simp] lemma le_none {a : with_top α} : @has_le.le (with_top α) _ a none :=
to_dual_le_to_dual_iff.mp with_bot.none_le
instance : order_top (with_top α) := { le_top := λ a, le_none, .. with_top.has_top }
instance [order_bot α] : order_bot (with_top α) :=
{ bot := some ⊥,
bot_le := λ o a ha, by cases ha; exact ⟨_, rfl, bot_le⟩ }
instance [order_bot α] : bounded_order (with_top α) :=
{ ..with_top.order_top, ..with_top.order_bot }
lemma not_top_le_coe (a : α) : ¬ (⊤ : with_top α) ≤ ↑a := with_bot.not_coe_le_bot (to_dual a)
lemma le_coe : ∀ {o : option α}, a ∈ o → (@has_le.le (with_top α) _ o b ↔ a ≤ b) | _ rfl :=
coe_le_coe
lemma le_coe_iff {x : with_top α} : x ≤ b ↔ ∃ a : α, x = a ∧ a ≤ b :=
by simpa [←to_dual_le_to_dual_iff, with_bot.coe_le_iff]
lemma coe_le_iff {x : with_top α} : ↑a ≤ x ↔ ∀ b, x = ↑b → a ≤ b :=
begin
simp only [←to_dual_le_to_dual_iff, to_dual_apply_coe, with_bot.le_coe_iff, order_dual.forall,
to_dual_le_to_dual],
exact forall₂_congr (λ _ _, iff.rfl)
end
protected lemma _root_.is_min.with_top (h : is_min a) : is_min (a : with_top α) :=
begin
-- defeq to is_max_to_dual_iff.mp (is_max.with_bot _), but that breaks API boundary
intros _ hb,
rw ←to_dual_le_to_dual_iff at hb,
simpa [to_dual_le_iff] using (is_max.with_bot h : is_max (to_dual a : with_bot αᵒᵈ)) hb
end
end has_le
section has_lt
variables [has_lt α]
@[priority 10]
instance : has_lt (with_top α) := ⟨λ o₁ o₂ : option α, ∃ b ∈ o₁, ∀ a ∈ o₂, b < a⟩
lemma to_dual_lt_iff {a : with_top α} {b : with_bot αᵒᵈ} :
with_top.to_dual a < b ↔ with_bot.of_dual b < a := iff.rfl
lemma lt_to_dual_iff {a : with_bot αᵒᵈ} {b : with_top α} :
a < with_top.to_dual b ↔ b < with_bot.of_dual a := iff.rfl
@[simp] lemma to_dual_lt_to_dual_iff {a b : with_top α} :
with_top.to_dual a < with_top.to_dual b ↔ b < a := iff.rfl
lemma of_dual_lt_iff {a : with_top αᵒᵈ} {b : with_bot α} :
with_top.of_dual a < b ↔ with_bot.to_dual b < a := iff.rfl
lemma lt_of_dual_iff {a : with_bot α} {b : with_top αᵒᵈ} :
a < with_top.of_dual b ↔ b < with_bot.to_dual a := iff.rfl
@[simp] lemma of_dual_lt_of_dual_iff {a b : with_top αᵒᵈ} :
with_top.of_dual a < with_top.of_dual b ↔ b < a := iff.rfl
end has_lt
end with_top
namespace with_bot
open order_dual
@[simp] lemma to_dual_symm_apply (a : with_top αᵒᵈ) : with_bot.to_dual.symm a = a.of_dual := rfl
@[simp] lemma of_dual_symm_apply (a : with_top α) : with_bot.of_dual.symm a = a.to_dual := rfl
@[simp] lemma to_dual_apply_bot : with_bot.to_dual (⊥ : with_bot α) = ⊤ := rfl
@[simp] lemma of_dual_apply_bot : with_bot.of_dual (⊥ : with_bot α) = ⊤ := rfl
@[simp] lemma to_dual_apply_coe (a : α) : with_bot.to_dual (a : with_bot α) = to_dual a := rfl
@[simp] lemma of_dual_apply_coe (a : αᵒᵈ) : with_bot.of_dual (a : with_bot αᵒᵈ) = of_dual a := rfl
lemma map_to_dual (f : αᵒᵈ → βᵒᵈ) (a : with_top α) :
with_bot.map f (with_top.to_dual a) = a.map (to_dual ∘ f) := rfl
lemma map_of_dual (f : α → β) (a : with_top αᵒᵈ) :
with_bot.map f (with_top.of_dual a) = a.map (of_dual ∘ f) := rfl
lemma to_dual_map (f : α → β) (a : with_bot α) :
with_bot.to_dual (with_bot.map f a) = map (to_dual ∘ f ∘ of_dual) a.to_dual := rfl
lemma of_dual_map (f : αᵒᵈ → βᵒᵈ) (a : with_bot αᵒᵈ) :
with_bot.of_dual (with_bot.map f a) = map (of_dual ∘ f ∘ to_dual) a.of_dual := rfl
section has_le
variables [has_le α] {a b : α}
lemma to_dual_le_iff {a : with_bot α} {b : with_top αᵒᵈ} :
with_bot.to_dual a ≤ b ↔ with_top.of_dual b ≤ a := iff.rfl
lemma le_to_dual_iff {a : with_top αᵒᵈ} {b : with_bot α} :
a ≤ with_bot.to_dual b ↔ b ≤ with_top.of_dual a := iff.rfl
@[simp] lemma to_dual_le_to_dual_iff {a b : with_bot α} :
with_bot.to_dual a ≤ with_bot.to_dual b ↔ b ≤ a := iff.rfl
lemma of_dual_le_iff {a : with_bot αᵒᵈ} {b : with_top α} :
with_bot.of_dual a ≤ b ↔ with_top.to_dual b ≤ a := iff.rfl
lemma le_of_dual_iff {a : with_top α} {b : with_bot αᵒᵈ} :
a ≤ with_bot.of_dual b ↔ b ≤ with_top.to_dual a := iff.rfl
@[simp] lemma of_dual_le_of_dual_iff {a b : with_bot αᵒᵈ} :
with_bot.of_dual a ≤ with_bot.of_dual b ↔ b ≤ a := iff.rfl
end has_le
section has_lt
variables [has_lt α] {a b : α}
lemma to_dual_lt_iff {a : with_bot α} {b : with_top αᵒᵈ} :
with_bot.to_dual a < b ↔ with_top.of_dual b < a := iff.rfl
lemma lt_to_dual_iff {a : with_top αᵒᵈ} {b : with_bot α} :
a < with_bot.to_dual b ↔ b < with_top.of_dual a := iff.rfl
@[simp] lemma to_dual_lt_to_dual_iff {a b : with_bot α} :
with_bot.to_dual a < with_bot.to_dual b ↔ b < a := iff.rfl
lemma of_dual_lt_iff {a : with_bot αᵒᵈ} {b : with_top α} :
with_bot.of_dual a < b ↔ with_top.to_dual b < a := iff.rfl
lemma lt_of_dual_iff {a : with_top α} {b : with_bot αᵒᵈ} :
a < with_bot.of_dual b ↔ b < with_top.to_dual a := iff.rfl
@[simp] lemma of_dual_lt_of_dual_iff {a b : with_bot αᵒᵈ} :
with_bot.of_dual a < with_bot.of_dual b ↔ b < a := iff.rfl
end has_lt
end with_bot
namespace with_top
section has_lt
variables [has_lt α] {a b : α}
open order_dual
@[simp, norm_cast] lemma coe_lt_coe : (a : with_top α) < b ↔ a < b :=
by simp only [←to_dual_lt_to_dual_iff, to_dual_apply_coe, with_bot.coe_lt_coe, to_dual_lt_to_dual]
@[simp] lemma some_lt_some : @has_lt.lt (with_top α) _ (some a) (some b) ↔ a < b := coe_lt_coe
lemma coe_lt_top (a : α) : (a : with_top α) < ⊤ :=
by simpa [←to_dual_lt_to_dual_iff] using with_bot.bot_lt_coe _
@[simp] lemma some_lt_none (a : α) : @has_lt.lt (with_top α) _ (some a) none := coe_lt_top a
@[simp] lemma not_none_lt (a : with_top α) : ¬ @has_lt.lt (with_top α) _ none a :=
begin
rw [←to_dual_lt_to_dual_iff],
exact with_bot.not_lt_none _
end
lemma lt_iff_exists_coe {a b : with_top α} : a < b ↔ ∃ p : α, a = p ∧ ↑p < b :=
begin
rw [←to_dual_lt_to_dual_iff, with_bot.lt_iff_exists_coe, order_dual.exists],
exact exists_congr (λ _, and_congr_left' iff.rfl)
end
lemma coe_lt_iff {x : with_top α} : ↑a < x ↔ ∀ b, x = ↑b → a < b :=
begin
simp only [←to_dual_lt_to_dual_iff, with_bot.lt_coe_iff, to_dual_apply_coe, order_dual.forall,
to_dual_lt_to_dual],
exact forall₂_congr (λ _ _, iff.rfl)
end
end has_lt
instance [preorder α] : preorder (with_top α) :=
{ le := (≤),
lt := (<),
lt_iff_le_not_le := by simp [←to_dual_lt_to_dual_iff, lt_iff_le_not_le],
le_refl := λ _, to_dual_le_to_dual_iff.mp le_rfl,
le_trans := λ _ _ _, by { simp_rw [←to_dual_le_to_dual_iff], exact function.swap le_trans } }
instance [partial_order α] : partial_order (with_top α) :=
{ le_antisymm := λ _ _, by { simp_rw [←to_dual_le_to_dual_iff], exact function.swap le_antisymm },
.. with_top.preorder }
lemma coe_strict_mono [preorder α] : strict_mono (coe : α → with_top α) := λ a b, some_lt_some.2
lemma coe_mono [preorder α] : monotone (coe : α → with_top α) := λ a b, coe_le_coe.2
lemma monotone_iff [preorder α] [preorder β] {f : with_top α → β} :
monotone f ↔ monotone (f ∘ coe : α → β) ∧ ∀ x : α, f x ≤ f ⊤ :=
⟨λ h, ⟨h.comp with_top.coe_mono, λ x, h le_top⟩,
λ h, with_top.forall.2 ⟨with_top.forall.2 ⟨λ _, le_rfl, λ x h, (not_top_le_coe _ h).elim⟩,
λ x, with_top.forall.2 ⟨λ _, h.2 x, λ y hle, h.1 (coe_le_coe.1 hle)⟩⟩⟩
@[simp] lemma monotone_map_iff [preorder α] [preorder β] {f : α → β} :
monotone (with_top.map f) ↔ monotone f :=
monotone_iff.trans $ by simp [monotone]
alias monotone_map_iff ↔ _ _root_.monotone.with_top_map
lemma strict_mono_iff [preorder α] [preorder β] {f : with_top α → β} :
strict_mono f ↔ strict_mono (f ∘ coe : α → β) ∧ ∀ x : α, f x < f ⊤ :=
⟨λ h, ⟨h.comp with_top.coe_strict_mono, λ x, h (coe_lt_top _)⟩,
λ h, with_top.forall.2 ⟨with_top.forall.2 ⟨flip absurd (lt_irrefl _), λ x h, (not_top_lt h).elim⟩,
λ x, with_top.forall.2 ⟨λ _, h.2 x, λ y hle, h.1 (coe_lt_coe.1 hle)⟩⟩⟩
@[simp] lemma strict_mono_map_iff [preorder α] [preorder β] {f : α → β} :
strict_mono (with_top.map f) ↔ strict_mono f :=
strict_mono_iff.trans $ by simp [strict_mono, coe_lt_top]
alias strict_mono_map_iff ↔ _ _root_.strict_mono.with_top_map
lemma map_le_iff [preorder α] [preorder β] (f : α → β)
(a b : with_top α) (mono_iff : ∀ {a b}, f a ≤ f b ↔ a ≤ b) :
a.map f ≤ b.map f ↔ a ≤ b :=
begin
rw [←to_dual_le_to_dual_iff, to_dual_map, to_dual_map, with_bot.map_le_iff,
to_dual_le_to_dual_iff],
simp [mono_iff]
end
instance [semilattice_inf α] : semilattice_inf (with_top α) :=
{ inf := option.lift_or_get (⊓),
inf_le_left := λ o₁ o₂ a ha,
by cases ha; cases o₂; simp [option.lift_or_get],
inf_le_right := λ o₁ o₂ a ha,
by cases ha; cases o₁; simp [option.lift_or_get],
le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin
cases o₂ with b; cases o₃ with c; cases ha,
{ exact h₂ a rfl },
{ exact h₁ a rfl },
{ rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩,
simp at h₂,
exact ⟨d, rfl, le_inf h₁' h₂⟩ }
end,
..with_top.partial_order }
lemma coe_inf [semilattice_inf α] (a b : α) : ((a ⊓ b : α) : with_top α) = a ⊓ b := rfl
instance [semilattice_sup α] : semilattice_sup (with_top α) :=
{ sup := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊔ b)),
le_sup_left := λ o₁ o₂ a ha, begin
simp [map] at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩,
exact ⟨_, rfl, le_sup_left⟩
end,
le_sup_right := λ o₁ o₂ a ha, begin
simp [map] at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩,
exact ⟨_, rfl, le_sup_right⟩
end,
sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin
cases ha,
rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩,
rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩,
exact ⟨_, rfl, sup_le ab ac⟩
end,
..with_top.partial_order }
lemma coe_sup [semilattice_sup α] (a b : α) : ((a ⊔ b : α) : with_top α) = a ⊔ b := rfl
instance [lattice α] : lattice (with_top α) :=
{ ..with_top.semilattice_sup, ..with_top.semilattice_inf }
instance [distrib_lattice α] : distrib_lattice (with_top α) :=
{ le_sup_inf := λ o₁ o₂ o₃,
match o₁, o₂, o₃ with
| ⊤, o₂, o₃ := le_rfl
| (a₁ : α), ⊤, ⊤ := le_rfl
| (a₁ : α), ⊤, (a₃ : α) := le_rfl
| (a₁ : α), (a₂ : α), ⊤ := le_rfl
| (a₁ : α), (a₂ : α), (a₃ : α) := coe_le_coe.mpr le_sup_inf
end,
..with_top.lattice }
instance decidable_le [has_le α] [@decidable_rel α (≤)] : @decidable_rel (with_top α) (≤) :=
λ _ _, decidable_of_decidable_of_iff (with_bot.decidable_le _ _) (to_dual_le_to_dual_iff)
instance decidable_lt [has_lt α] [@decidable_rel α (<)] : @decidable_rel (with_top α) (<) :=
λ _ _, decidable_of_decidable_of_iff (with_bot.decidable_lt _ _) (to_dual_lt_to_dual_iff)
instance is_total_le [has_le α] [is_total α (≤)] : is_total (with_top α) (≤) :=
⟨λ _ _, by { simp_rw ←to_dual_le_to_dual_iff, exact total_of _ _ _ }⟩
instance [linear_order α] : linear_order (with_top α) := lattice.to_linear_order _
@[simp, norm_cast]
lemma coe_min [linear_order α] (x y : α) : (↑(min x y) : with_top α) = min x y := rfl
@[simp, norm_cast]
lemma coe_max [linear_order α] (x y : α) : (↑(max x y) : with_top α) = max x y := rfl
lemma well_founded_lt [preorder α] (h : @well_founded α (<)) : @well_founded (with_top α) (<) :=
have acc_some : ∀ a : α, acc ((<) : with_top α → with_top α → Prop) (some a) :=
λ a, acc.intro _ (well_founded.induction h a
(show ∀ b, (∀ c, c < b → ∀ d : with_top α, d < some c → acc (<) d) →
∀ y : with_top α, y < some b → acc (<) y,
from λ b ih c, option.rec_on c (λ hc, (not_lt_of_ge le_top hc).elim)
(λ c hc, acc.intro _ (ih _ (some_lt_some.1 hc))))),
⟨λ a, option.rec_on a (acc.intro _ (λ y, option.rec_on y (λ h, (lt_irrefl _ h).elim)
(λ _ _, acc_some _))) acc_some⟩
open order_dual
lemma well_founded_gt [preorder α] (h : @well_founded α (>)) : @well_founded (with_top α) (>) :=
⟨λ a, begin
-- ideally, use rel_hom_class.acc, but that is defined later
have : acc (<) a.to_dual := well_founded.apply (with_bot.well_founded_lt h) _,
revert this,
generalize ha : a.to_dual = b, intro ac,
induction ac with _ H IH generalizing a, subst ha,
exact ⟨_, λ a' h, IH (a'.to_dual) (to_dual_lt_to_dual.mpr h) _ rfl⟩
end⟩
lemma _root_.with_bot.well_founded_gt [preorder α] (h : @well_founded α (>)) :
@well_founded (with_bot α) (>) :=
⟨λ a, begin
-- ideally, use rel_hom_class.acc, but that is defined later
have : acc (<) a.to_dual := well_founded.apply (with_top.well_founded_lt h) _,
revert this,
generalize ha : a.to_dual = b, intro ac,
induction ac with _ H IH generalizing a, subst ha,
exact ⟨_, λ a' h, IH (a'.to_dual) (to_dual_lt_to_dual.mpr h) _ rfl⟩
end⟩
instance trichotomous.lt [preorder α] [is_trichotomous α (<)] : is_trichotomous (with_top α) (<) :=
⟨begin
rintro (a | _) (b | _),
iterate 3 { simp },
simpa [option.some_inj] using @trichotomous _ (<) _ a b
end⟩
instance is_well_order.lt [preorder α] [h : is_well_order α (<)] : is_well_order (with_top α) (<) :=
{ wf := well_founded_lt h.wf }
instance trichotomous.gt [preorder α] [is_trichotomous α (>)] : is_trichotomous (with_top α) (>) :=
⟨begin
rintro (a | _) (b | _),
iterate 3 { simp },
simpa [option.some_inj] using @trichotomous _ (>) _ a b
end⟩
instance is_well_order.gt [preorder α] [h : is_well_order α (>)] : is_well_order (with_top α) (>) :=
{ wf := well_founded_gt h.wf }
instance _root_.with_bot.trichotomous.lt [preorder α] [h : is_trichotomous α (<)] :
is_trichotomous (with_bot α) (<) :=
@with_top.trichotomous.gt αᵒᵈ _ h
instance _root_.with_bot.is_well_order.lt [preorder α] [h : is_well_order α (<)] :
is_well_order (with_bot α) (<) :=
@with_top.is_well_order.gt αᵒᵈ _ h
instance _root_.with_bot.trichotomous.gt [preorder α] [h : is_trichotomous α (>)] :
is_trichotomous (with_bot α) (>) :=
@with_top.trichotomous.lt αᵒᵈ _ h
instance _root_.with_bot.is_well_order.gt [preorder α] [h : is_well_order α (>)] :
is_well_order (with_bot α) (>) :=
@with_top.is_well_order.lt αᵒᵈ _ h
instance [has_lt α] [densely_ordered α] [no_max_order α] : densely_ordered (with_top α) :=
order_dual.densely_ordered (with_bot αᵒᵈ)
lemma lt_iff_exists_coe_btwn [preorder α] [densely_ordered α] [no_max_order α] {a b : with_top α} :
a < b ↔ ∃ x : α, a < ↑x ∧ ↑x < b :=
⟨λ h, let ⟨y, hy⟩ := exists_between h, ⟨x, hx⟩ := lt_iff_exists_coe.1 hy.2 in ⟨x, hx.1 ▸ hy⟩,
λ ⟨x, hx⟩, lt_trans hx.1 hx.2⟩
instance [has_le α] [no_bot_order α] [nonempty α] : no_bot_order (with_top α) :=
order_dual.no_bot_order (with_bot αᵒᵈ)
instance [has_lt α] [no_min_order α] [nonempty α] : no_min_order (with_top α) :=
order_dual.no_min_order (with_bot αᵒᵈ)
end with_top
|
bbd6cc8ee993f0c235772b9c13ebccc567f39659 | c9e78e68dc955b2325401aec3a6d3240cd8b83f4 | /src/property_catalogue/LTL/sat/precedes.lean | 57783cf4b0287d3221020e13b5da416dface9fee | [] | no_license | loganrjmurphy/lean-strategies | 4b8dd54771bb421c929a8bcb93a528ce6c1a70f1 | 832ea28077701b977b4fc59ed9a8ce6911654e59 | refs/heads/master | 1,682,732,168,860 | 1,621,444,295,000 | 1,621,444,295,000 | 278,458,841 | 3 | 0 | null | 1,613,755,728,000 | 1,594,324,763,000 | Lean | UTF-8 | Lean | false | false | 3,643 | lean | import LTS property_catalogue.LTL.patterns tactic
open tactic
variable {M : LTS}
namespace precedes
namespace globally
-- Proof 1 : Because S never happens
lemma vacuous (P S : formula M) (π : path M) :
(sat (absent.globally S) π) → sat (precedes.globally P S) π
:= λ s, or.inl s
-- Proof 2 : P precedes S because S can't happen before P
local notation π `⊨ `P := sat P π
variables (P Q R : formula M) (π : path M)
lemma by_absent_before (P Q : formula M) (π : path M) :
(π ⊨ absent.before Q P) ∧ (π ⊨ exist.globally P ) →
(π ⊨ precedes.globally P Q) :=
begin
rintros ⟨H1,H2⟩,
rw precedes.globally,
rw [absent.before, sat, imp_iff_not_or] at H1,
cases H1,
left, rw always_eventually_dual, contradiction,
right, assumption,
end
-- Proof 3 : P precedes R because P precedes Q and Q precedes R
lemma by_transitive (P Q R : formula M) (π : path M) :
(π ⊨ precedes.globally P R) ∧ (π ⊨ precedes.globally R Q) →
(π ⊨ precedes.globally P Q) :=
begin
rintros ⟨H1, H2⟩,
cases H2, left, assumption,
cases H1,
cases H2 with k H2, replace H1 := (H1 k), replace H2:= H2.1,
have := sat_em R (π.drop k),replace this := this H2,
contradiction,
rcases H1 with ⟨k,Hk1,Hk2⟩,
rcases H2 with ⟨w, Hw1, Hw2⟩,
right, use k,
split, assumption,
intros i Hi, apply Hw2,
have EM : (k < w) ∨ ¬ (k < w), from em (k < w),
cases EM,
apply lt_trans Hi, assumption,
simp at EM,
have EM2 : (k = w) ∨ ¬ (k = w), from em (k = w),
cases EM2,
rw ← EM2, assumption,
have : w < k, by omega,
replace Hk2 := Hk2 w this,
have := sat_em R (π.drop w),replace this := this Hw1,
contradiction,
end
meta def solve_by_transitive (e₁ e₂ e₃ : expr) (s : string): tactic unit :=
do
tactic.interactive.apply ``(by_transitive %%e₁ %%e₂ %%e₃)
-- e₁ ← tactic_format_expr e₁,
-- e₂ ← tactic_format_expr e₂,
-- e₃ ← tactic_format_expr e₃,
-- return $ s.append $
-- "apply precedes.globally.by_transitive " ++
-- e₁.to_string ++ " " ++ e₂.to_string ++ " " ++ e₃.to_string ++ ",\n"
meta def solve_by_absent_before (e₁ e₂ : expr) (s : string) :
tactic unit :=
do
tactic.interactive.apply ``(by_absent_before %%e₁ %%e₂)
-- e₁ ← e₁.log_format,
-- e₂ ← e₂.log_format,
-- s.log $
-- "apply precedes.globally.by_absent_before " ++
-- e₁ ++ " " ++ e₂ ++ ",\n"
meta def solve (e₁ e₂ : expr) (s : string) : list expr → tactic unit
| [] := return ()
| (h::t) :=
do typ ← infer_type h,
match typ with
| `(sat (precedes.globally _ %%new) _) :=
solve_by_transitive e₁ e₂ new s <|> solve t
| `(sat (absent.before _ _) _) :=
solve_by_absent_before e₁ e₂ s <|> solve t
| _ := solve t
end
end globally
namespace before
-- (◆R) ⇒ ((!P) U (S ⅋ R))
lemma vacuous (P R S: formula M) (π : path M) :
sat (absent.globally S) π → sat (precedes.before P R S) π :=
by {intro H, rw [precedes.before,sat,imp_iff_not_or,
← always_eventually_dual], left, assumption}
lemma by_absent_before (P R S: formula M) (π : path M) :
sat (exist.globally (P ⅋ S)) π
∧ sat (absent.before R (P ⅋ S)) π → sat (precedes.before P R S) π
:=
begin
rintros ⟨L,R⟩,
replace R := R L,
rw [precedes.before, sat, imp_iff_not_or],
exact or.inr R,
end
end before
namespace after
-- (◾!Q) ⅋ ◆(Q & ((!P) W S))
end after
namespace between
-- ◾((Q & (!R) & ◆R) ⇒ (!P U (S ⅋ R)))
end between
end precedes |
2643561f9b34303c07a17952e2537c3081a45a19 | 9b9a16fa2cb737daee6b2785474678b6fa91d6d4 | /src/data/zmod/basic.lean | 337750d8d00ad61f9913063918cfec64b44b854a | [
"Apache-2.0"
] | permissive | johoelzl/mathlib | 253f46daa30b644d011e8e119025b01ad69735c4 | 592e3c7a2dfbd5826919b4605559d35d4d75938f | refs/heads/master | 1,625,657,216,488 | 1,551,374,946,000 | 1,551,374,946,000 | 98,915,829 | 0 | 0 | Apache-2.0 | 1,522,917,267,000 | 1,501,524,499,000 | Lean | UTF-8 | Lean | false | false | 14,121 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Chris Hughes
-/
import data.int.modeq data.int.gcd data.fintype data.pnat
open nat nat.modeq int
def zmod (n : ℕ+) := fin n
namespace zmod
instance (n : ℕ+) : has_neg (zmod n) :=
⟨λ a, ⟨nat_mod (-(a.1 : ℤ)) n,
have h : (n : ℤ) ≠ 0 := int.coe_nat_ne_zero_iff_pos.2 n.pos,
have h₁ : ((n : ℕ) : ℤ) = abs n := (abs_of_nonneg (int.coe_nat_nonneg n)).symm,
by rw [← int.coe_nat_lt, nat_mod, to_nat_of_nonneg (int.mod_nonneg _ h), h₁];
exact int.mod_lt _ h⟩⟩
instance (n : ℕ+) : add_comm_semigroup (zmod n) :=
{ add_assoc := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq
(show ((a + b) % n + c) ≡ (a + (b + c) % n) [MOD n],
from calc ((a + b) % n + c) ≡ a + b + c [MOD n] : modeq_add (nat.mod_mod _ _) rfl
... ≡ a + (b + c) [MOD n] : by rw add_assoc
... ≡ (a + (b + c) % n) [MOD n] : modeq_add rfl (nat.mod_mod _ _).symm),
add_comm := λ ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a + b) % n = (b + a) % n, by rw add_comm),
..fin.has_add }
instance (n : ℕ+) : comm_semigroup (zmod n) :=
{ mul_assoc := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq
(calc ((a * b) % n * c) ≡ a * b * c [MOD n] : modeq_mul (nat.mod_mod _ _) rfl
... ≡ a * (b * c) [MOD n] : by rw mul_assoc
... ≡ a * (b * c % n) [MOD n] : modeq_mul rfl (nat.mod_mod _ _).symm),
mul_comm := λ ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a * b) % n = (b * a) % n, by rw mul_comm),
..fin.has_mul }
instance (n : ℕ+) : has_one (zmod n) := ⟨⟨(1 % n), nat.mod_lt _ n.pos⟩⟩
instance (n : ℕ+) : has_zero (zmod n) := ⟨⟨0, n.pos⟩⟩
instance zmod_one.subsingleton : subsingleton (zmod 1) :=
⟨λ a b, fin.eq_of_veq (by rw [eq_zero_of_le_zero (le_of_lt_succ a.2),
eq_zero_of_le_zero (le_of_lt_succ b.2)])⟩
lemma add_val {n : ℕ+} : ∀ a b : zmod n, (a + b).val = (a.val + b.val) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma mul_val {n : ℕ+} : ∀ a b : zmod n, (a * b).val = (a.val * b.val) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma one_val {n : ℕ+} : (1 : zmod n).val = 1 % n := rfl
@[simp] lemma zero_val (n : ℕ+) : (0 : zmod n).val = 0 := rfl
private lemma one_mul_aux (n : ℕ+) (a : zmod n) : (1 : zmod n) * a = a :=
begin
cases n with n hn,
cases n with n,
{ exact (lt_irrefl _ hn).elim },
{ cases n with n,
{ exact @subsingleton.elim (zmod 1) _ _ _ },
{ have h₁ : a.1 % n.succ.succ = a.1 := nat.mod_eq_of_lt a.2,
have h₂ : 1 % n.succ.succ = 1 := nat.mod_eq_of_lt dec_trivial,
refine fin.eq_of_veq _,
simp [mul_val, one_val, h₁, h₂] } }
end
private lemma left_distrib_aux (n : ℕ+) : ∀ a b c : zmod n, a * (b + c) = a * b + a * c :=
λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq
(calc a * ((b + c) % n) ≡ a * (b + c) [MOD n] : modeq_mul rfl (nat.mod_mod _ _)
... ≡ a * b + a * c [MOD n] : by rw mul_add
... ≡ (a * b) % n + (a * c) % n [MOD n] : modeq_add (nat.mod_mod _ _).symm (nat.mod_mod _ _).symm)
instance (n : ℕ+) : comm_ring (zmod n) :=
{ zero_add := λ ⟨a, ha⟩, fin.eq_of_veq (show (0 + a) % n = a, by rw zero_add; exact nat.mod_eq_of_lt ha),
add_zero := λ ⟨a, ha⟩, fin.eq_of_veq (nat.mod_eq_of_lt ha),
add_left_neg :=
λ ⟨a, ha⟩, fin.eq_of_veq (show (((-a : ℤ) % n).to_nat + a) % n = 0,
from int.coe_nat_inj
begin
have hn : (n : ℤ) ≠ 0 := (ne_of_lt (int.coe_nat_lt.2 n.pos)).symm,
rw [int.coe_nat_mod, int.coe_nat_add, to_nat_of_nonneg (int.mod_nonneg _ hn), add_comm],
simp,
end),
one_mul := one_mul_aux n,
mul_one := λ a, by rw mul_comm; exact one_mul_aux n a,
left_distrib := left_distrib_aux n,
right_distrib := λ a b c, by rw [mul_comm, left_distrib_aux, mul_comm _ b, mul_comm]; refl,
..zmod.has_zero n,
..zmod.has_one n,
..zmod.has_neg n,
..zmod.add_comm_semigroup n,
..zmod.comm_semigroup n }
lemma val_cast_nat {n : ℕ+} (a : ℕ) : (a : zmod n).val = a % n :=
begin
induction a with a ih,
{ rw [nat.zero_mod]; refl },
{ rw [succ_eq_add_one, nat.cast_add, add_val, ih],
show (a % n + ((0 + (1 % n)) % n)) % n = (a + 1) % n,
rw [zero_add, nat.mod_mod],
exact nat.modeq.modeq_add (nat.mod_mod a n) (nat.mod_mod 1 n) }
end
lemma mk_eq_cast {n : ℕ+} {a : ℕ} (h : a < n) : (⟨a, h⟩ : zmod n) = (a : zmod n) :=
fin.eq_of_veq (by rw [val_cast_nat, nat.mod_eq_of_lt h])
@[simp] lemma cast_self_eq_zero {n : ℕ+} : ((n : ℕ) : zmod n) = 0 :=
fin.eq_of_veq (show (n : zmod n).val = 0, by simp [val_cast_nat])
lemma val_cast_of_lt {n : ℕ+} {a : ℕ} (h : a < n) : (a : zmod n).val = a :=
by rw [val_cast_nat, nat.mod_eq_of_lt h]
@[simp] lemma cast_mod_nat (n : ℕ+) (a : ℕ) : ((a % n : ℕ) : zmod n) = a :=
by conv {to_rhs, rw ← nat.mod_add_div a n}; simp
@[simp] lemma cast_val {n : ℕ+} (a : zmod n) : (a.val : zmod n) = a :=
by cases a; simp [mk_eq_cast]
@[simp] lemma cast_mod_int (n : ℕ+) (a : ℤ) : ((a % (n : ℕ) : ℤ) : zmod n) = a :=
by conv {to_rhs, rw ← int.mod_add_div a n}; simp
lemma val_cast_int {n : ℕ+} (a : ℤ) : (a : zmod n).val = (a % (n : ℕ)).nat_abs :=
have h : nat_abs (a % (n : ℕ)) < n := int.coe_nat_lt.1 begin
rw [nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos))],
conv {to_rhs, rw ← abs_of_nonneg (int.coe_nat_nonneg n)},
exact int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 n.pos)
end,
int.coe_nat_inj $
by conv {to_lhs, rw [← cast_mod_int n a,
← nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos)),
int.cast_coe_nat, val_cast_of_lt h] }
lemma coe_val_cast_int {n : ℕ+} (a : ℤ) : ((a : zmod n).val : ℤ) = a % (n : ℕ) :=
by rw [val_cast_int, int.nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos))]
lemma eq_iff_modeq_nat {n : ℕ+} {a b : ℕ} : (a : zmod n) = b ↔ a ≡ b [MOD n] :=
⟨λ h, by have := fin.veq_of_eq h;
rwa [val_cast_nat, val_cast_nat] at this,
λ h, fin.eq_of_veq $ by rwa [val_cast_nat, val_cast_nat]⟩
lemma eq_iff_modeq_int {n : ℕ+} {a b : ℤ} : (a : zmod n) = b ↔ a ≡ b [ZMOD (n : ℕ)] :=
⟨λ h, by have := fin.veq_of_eq h;
rwa [val_cast_int, val_cast_int, ← int.coe_nat_eq_coe_nat_iff,
nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos)),
nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos))] at this,
λ h : a % (n : ℕ) = b % (n : ℕ),
by rw [← cast_mod_int n a, ← cast_mod_int n b, h]⟩
lemma eq_zero_iff_dvd_nat {n : ℕ+} {a : ℕ} : (a : zmod n) = 0 ↔ (n : ℕ) ∣ a :=
by rw [← @nat.cast_zero (zmod n), eq_iff_modeq_nat, nat.modeq.modeq_zero_iff]
lemma eq_zero_iff_dvd_int {n : ℕ+} {a : ℤ} : (a : zmod n) = 0 ↔ ((n : ℕ) : ℤ) ∣ a :=
by rw [← @int.cast_zero (zmod n), eq_iff_modeq_int, int.modeq.modeq_zero_iff]
instance (n : ℕ+) : fintype (zmod n) := fin.fintype _
instance decidable_eq (n : ℕ+) : decidable_eq (zmod n) := fin.decidable_eq _
instance (n : ℕ+) : has_repr (zmod n) := fin.has_repr _
lemma card_zmod (n : ℕ+) : fintype.card (zmod n) = n := fintype.card_fin n
lemma le_div_two_iff_lt_neg {n : ℕ+} (hn : (n : ℕ) % 2 = 1)
{x : zmod n} (hx0 : x ≠ 0) : x.1 ≤ (n / 2 : ℕ) ↔ (n / 2 : ℕ) < (-x).1 :=
have hn2 : (n : ℕ) / 2 < n := nat.div_lt_of_lt_mul ((lt_mul_iff_one_lt_left n.pos).2 dec_trivial),
have hn2' : (n : ℕ) - n / 2 = n / 2 + 1,
by conv {to_lhs, congr, rw [← succ_sub_one n, succ_sub n.pos]};
rw [← two_mul_odd_div_two hn, two_mul, ← succ_add, nat.add_sub_cancel],
have hxn : (n : ℕ) - x.val < n,
begin
rw [nat.sub_lt_iff (le_of_lt x.2) (le_refl _), nat.sub_self],
rw ← zmod.cast_val x at hx0,
exact nat.pos_of_ne_zero (λ h, by simpa [h] using hx0)
end,
by conv {to_rhs, rw [← nat.succ_le_iff, succ_eq_add_one, ← hn2', ← zero_add (- x), ← zmod.cast_self_eq_zero,
← sub_eq_add_neg, ← zmod.cast_val x, ← nat.cast_sub (le_of_lt x.2),
zmod.val_cast_nat, mod_eq_of_lt hxn, nat.sub_le_sub_left_iff (le_of_lt x.2)] }
lemma ne_neg_self {n : ℕ+} (hn1 : (n : ℕ) % 2 = 1) {a : zmod n} (ha : a ≠ 0) : a ≠ -a :=
λ h, have a.val ≤ n / 2 ↔ (n : ℕ) / 2 < (-a).val := le_div_two_iff_lt_neg hn1 ha,
by rwa [← h, ← not_lt, not_iff_self] at this
@[simp] lemma cast_mul_right_val_cast {n m : ℕ+} (a : ℕ) :
((a : zmod (m * n)).val : zmod m) = (a : zmod m) :=
zmod.eq_iff_modeq_nat.2 (by rw zmod.val_cast_nat;
exact nat.modeq.modeq_of_modeq_mul_right _ (nat.mod_mod _ _))
@[simp] lemma cast_mul_left_val_cast {n m : ℕ+} (a : ℕ) :
((a : zmod (n * m)).val : zmod m) = (a : zmod m) :=
zmod.eq_iff_modeq_nat.2 (by rw zmod.val_cast_nat;
exact nat.modeq.modeq_of_modeq_mul_left _ (nat.mod_mod _ _))
lemma cast_val_cast_of_dvd {n m : ℕ+} (h : (m : ℕ) ∣ n) (a : ℕ) :
((a : zmod n).val : zmod m) = (a : zmod m) :=
let ⟨k , hk⟩ := h in
zmod.eq_iff_modeq_nat.2 (nat.modeq.modeq_of_modeq_mul_right k
(by rw [← hk, zmod.val_cast_nat]; exact nat.mod_mod _ _))
def units_equiv_coprime {n : ℕ+} : units (zmod n) ≃ {x : zmod n // nat.coprime x.1 n} :=
{ to_fun := λ x, ⟨x, nat.modeq.coprime_of_mul_modeq_one (x⁻¹).1.1 begin
have := units.ext_iff.1 (mul_right_inv x),
rwa [← zmod.cast_val ((1 : units (zmod n)) : zmod n), units.coe_one, zmod.one_val,
← zmod.cast_val ((x * x⁻¹ : units (zmod n)) : zmod n),
units.coe_mul, zmod.mul_val, zmod.cast_mod_nat, zmod.cast_mod_nat,
zmod.eq_iff_modeq_nat] at this
end⟩,
inv_fun := λ x,
have x.val * ↑(gcd_a ((x.val).val) ↑n) = 1,
by rw [← zmod.cast_val x.1, ← int.cast_coe_nat, ← int.cast_one, ← int.cast_mul,
zmod.eq_iff_modeq_int, ← int.coe_nat_one, ← (show nat.gcd _ _ = _, from x.2)];
simpa using int.modeq.gcd_a_modeq x.1.1 n,
⟨x.1, gcd_a x.1.1 n, this, by simpa [mul_comm] using this⟩,
left_inv := λ ⟨_, _, _, _⟩, units.ext rfl,
right_inv := λ ⟨_, _⟩, rfl }
end zmod
def zmodp (p : ℕ) (hp : prime p) : Type := zmod ⟨p, hp.pos⟩
namespace zmodp
variables {p : ℕ} (hp : prime p)
instance : comm_ring (zmodp p hp) := zmod.comm_ring ⟨p, hp.pos⟩
instance {p : ℕ} (hp : prime p) : has_inv (zmodp p hp) :=
⟨λ a, gcd_a a.1 p⟩
lemma add_val : ∀ a b : zmodp p hp, (a + b).val = (a.val + b.val) % p
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma mul_val : ∀ a b : zmodp p hp, (a * b).val = (a.val * b.val) % p
| ⟨_, _⟩ ⟨_, _⟩ := rfl
@[simp] lemma one_val : (1 : zmodp p hp).val = 1 :=
nat.mod_eq_of_lt hp.gt_one
@[simp] lemma zero_val : (0 : zmodp p hp).val = 0 := rfl
lemma val_cast_nat (a : ℕ) : (a : zmodp p hp).val = a % p :=
@zmod.val_cast_nat ⟨p, hp.pos⟩ _
lemma mk_eq_cast {a : ℕ} (h : a < p) : (⟨a, h⟩ : zmodp p hp) = (a : zmodp p hp) :=
@zmod.mk_eq_cast ⟨p, hp.pos⟩ _ _
@[simp] lemma cast_self_eq_zero: (p : zmodp p hp) = 0 :=
fin.eq_of_veq $ by simp [val_cast_nat]
lemma val_cast_of_lt {a : ℕ} (h : a < p) : (a : zmodp p hp).val = a :=
@zmod.val_cast_of_lt ⟨p, hp.pos⟩ _ h
@[simp] lemma cast_mod_nat (a : ℕ) : ((a % p : ℕ) : zmodp p hp) = a :=
@zmod.cast_mod_nat ⟨p, hp.pos⟩ _
@[simp] lemma cast_val (a : zmodp p hp) : (a.val : zmodp p hp) = a :=
@zmod.cast_val ⟨p, hp.pos⟩ _
@[simp] lemma cast_mod_int (a : ℤ) : ((a % p : ℤ) : zmodp p hp) = a :=
@zmod.cast_mod_int ⟨p, hp.pos⟩ _
lemma val_cast_int (a : ℤ) : (a : zmodp p hp).val = (a % p).nat_abs :=
@zmod.val_cast_int ⟨p, hp.pos⟩ _
lemma coe_val_cast_int (a : ℤ) : ((a : zmodp p hp).val : ℤ) = a % (p : ℕ) :=
@zmod.coe_val_cast_int ⟨p, hp.pos⟩ _
lemma eq_iff_modeq_nat {a b : ℕ} : (a : zmodp p hp) = b ↔ a ≡ b [MOD p] :=
@zmod.eq_iff_modeq_nat ⟨p, hp.pos⟩ _ _
lemma eq_iff_modeq_int {a b : ℤ} : (a : zmodp p hp) = b ↔ a ≡ b [ZMOD p] :=
@zmod.eq_iff_modeq_int ⟨p, hp.pos⟩ _ _
lemma eq_zero_iff_dvd_nat (a : ℕ) : (a : zmodp p hp) = 0 ↔ p ∣ a :=
@zmod.eq_zero_iff_dvd_nat ⟨p, hp.pos⟩ _
lemma eq_zero_iff_dvd_int (a : ℤ) : (a : zmodp p hp) = 0 ↔ (p : ℤ) ∣ a :=
@zmod.eq_zero_iff_dvd_int ⟨p, hp.pos⟩ _
instance : fintype (zmodp p hp) := @zmod.fintype ⟨p, hp.pos⟩
instance decidable_eq : decidable_eq (zmodp p hp) := fin.decidable_eq _
instance (n : ℕ+) : has_repr (zmodp p hp) := fin.has_repr _
@[simp] lemma card_zmodp : fintype.card (zmodp p hp) = p :=
@zmod.card_zmod ⟨p, hp.pos⟩
lemma le_div_two_iff_lt_neg {p : ℕ} (hp : prime p) (hp1 : p % 2 = 1)
{x : zmodp p hp} (hx0 : x ≠ 0) : x.1 ≤ (p / 2 : ℕ) ↔ (p / 2 : ℕ) < (-x).1 :=
@zmod.le_div_two_iff_lt_neg ⟨p, hp.pos⟩ hp1 _ hx0
lemma ne_neg_self (hp1 : p % 2 = 1) {a : zmodp p hp} (ha : a ≠ 0) : a ≠ -a :=
@zmod.ne_neg_self ⟨p, hp.pos⟩ hp1 _ ha
lemma prime_ne_zero {q : ℕ} (hq : prime q) (hpq : p ≠ q) : (q : zmodp p hp) ≠ 0 :=
by rwa [← nat.cast_zero, ne.def, zmodp.eq_iff_modeq_nat, nat.modeq.modeq_zero_iff,
← hp.coprime_iff_not_dvd, coprime_primes hp hq]
lemma mul_inv_eq_gcd (a : ℕ) : (a : zmodp p hp) * a⁻¹ = nat.gcd a p :=
by rw [← int.cast_coe_nat (nat.gcd _ _), nat.gcd_comm, nat.gcd_rec, ← (eq_iff_modeq_int _).2 (int.modeq.gcd_a_modeq _ _)];
simp [has_inv.inv, val_cast_nat]
private lemma mul_inv_cancel_aux : ∀ a : zmodp p hp, a ≠ 0 → a * a⁻¹ = 1 :=
λ ⟨a, hap⟩ ha0, begin
rw [mk_eq_cast, ne.def, ← @nat.cast_zero (zmodp p hp), eq_iff_modeq_nat, modeq_zero_iff] at ha0,
have : nat.gcd p a = 1 := (prime.coprime_iff_not_dvd hp).2 ha0,
rw [mk_eq_cast _ hap, mul_inv_eq_gcd, nat.gcd_comm],
simpa [nat.gcd_comm, this]
end
instance : discrete_field (zmodp p hp) :=
{ zero_ne_one := fin.ne_of_vne $ show 0 ≠ 1 % p,
by rw nat.mod_eq_of_lt hp.gt_one;
exact zero_ne_one,
mul_inv_cancel := mul_inv_cancel_aux hp,
inv_mul_cancel := λ a, by rw mul_comm; exact mul_inv_cancel_aux hp _,
has_decidable_eq := by apply_instance,
inv_zero := show (gcd_a 0 p : zmodp p hp) = 0,
by unfold gcd_a xgcd xgcd_aux; refl,
..zmodp.comm_ring hp,
..zmodp.has_inv hp }
end zmodp |
0cb9d9b7a22438ad23f1a35b5cb4dde86bd49ddc | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/general_recursion.lean | a32b2bda52580459fa6955fb077ed8b757df0aa5 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 10,562 | lean | /-
Copyright (c) 2020 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import tactic.norm_num
import tactic.linarith
import tactic.omega
import control.lawful_fix
import order.category.omega_complete_partial_order
import data.nat.basic
universes u_1 u_2
namespace part.examples
open function has_fix omega_complete_partial_order
/-! `easy` is a trivial, non-recursive example -/
def easy.intl (easy : ℕ → ℕ → part ℕ) : ℕ → ℕ → part ℕ
| x y := pure x
def easy :=
fix easy.intl
-- automation coming soon
theorem easy.cont : continuous' easy.intl :=
pi.omega_complete_partial_order.flip₂_continuous' easy.intl
(λ x, pi.omega_complete_partial_order.flip₂_continuous' _ (λ x_1, const_continuous' (pure x)))
-- automation coming soon
theorem easy.equations.eqn_1 (x y : ℕ) : easy x y = pure x :=
by rw [easy, lawful_fix.fix_eq' easy.cont]; refl
/-! division on natural numbers -/
def div.intl (div : ℕ → ℕ → part ℕ) : ℕ → ℕ → part ℕ
| x y :=
if y ≤ x ∧ y > 0
then div (x - y) y
else pure x
def div : ℕ → ℕ → part ℕ :=
fix div.intl
-- automation coming soon
theorem div.cont : continuous' div.intl :=
pi.omega_complete_partial_order.flip₂_continuous' div.intl
(λ (x : ℕ),
pi.omega_complete_partial_order.flip₂_continuous' (λ (g : ℕ → ℕ → part ℕ), div.intl g x)
(λ (x_1 : ℕ),
(continuous_hom.ite_continuous' (λ (x_2 : ℕ → ℕ → part ℕ), x_2 (x - x_1) x_1)
(λ (x_1 : ℕ → ℕ → part ℕ), pure x)
(pi.omega_complete_partial_order.flip₁_continuous'
(λ (v_1 : ℕ) (x_2 : ℕ → ℕ → part ℕ), x_2 (x - x_1) v_1) _ $
pi.omega_complete_partial_order.flip₁_continuous'
(λ (v : ℕ) (g : ℕ → ℕ → part ℕ) (x : ℕ), g v x) _ id_continuous')
(const_continuous' (pure x)))))
-- automation coming soon
theorem div.equations.eqn_1 (x y : ℕ) : div x y = if y ≤ x ∧ y > 0 then div (x - y) y else pure x :=
by conv_lhs { rw [div, lawful_fix.fix_eq' div.cont] }; refl
inductive tree (α : Type*)
| nil {} : tree
| node (x : α) : tree → tree → tree
open part.examples.tree
/-! `map` on a `tree` using monadic notation -/
def tree_map.intl {α β : Type*} (f : α → β) (tree_map : tree α → part (tree β)) :
tree α → part (tree β)
| nil := pure nil
| (node x t₀ t₁) :=
do tt₀ ← tree_map t₀,
tt₁ ← tree_map t₁,
pure $ node (f x) tt₀ tt₁
-- automation coming soon
def tree_map {α : Type u_1} {β : Type u_2} (f : α → β) : tree α → part (tree β) :=
fix (tree_map.intl f)
-- automation coming soon
theorem tree_map.cont :
∀ {α : Type u_1} {β : Type u_2} (f : α → β), continuous' (tree_map.intl f) :=
λ {α : Type u_1} {β : Type u_2} (f : α → β),
pi.omega_complete_partial_order.flip₂_continuous' (tree_map.intl f)
(λ (x : tree α),
tree.cases_on x (id (const_continuous' (pure nil)))
(λ (x_x : α) (x_a x_a_1 : tree α),
(continuous_hom.bind_continuous' (λ (x : tree α → part (tree β)), x x_a)
(λ (x : tree α → part (tree β)) (tt₀ : tree β),
x x_a_1 >>= λ (tt₁ : tree β), pure (node (f x_x) tt₀ tt₁))
(pi.omega_complete_partial_order.flip₁_continuous' (λ (v : tree α) (x : tree α → part (tree β)), x v) x_a id_continuous')
(pi.omega_complete_partial_order.flip₂_continuous'
(λ (x : tree α → part (tree β)) (tt₀ : tree β),
x x_a_1 >>= λ (tt₁ : tree β), pure (node (f x_x) tt₀ tt₁))
(λ (x : tree β),
continuous_hom.bind_continuous' (λ (x : tree α → part (tree β)), x x_a_1)
(λ (x_1 : tree α → part (tree β)) (tt₁ : tree β), pure (node (f x_x) x tt₁))
(pi.omega_complete_partial_order.flip₁_continuous' (λ (v : tree α) (x : tree α → part (tree β)), x v) x_a_1
id_continuous')
(pi.omega_complete_partial_order.flip₂_continuous'
(λ (x_1 : tree α → part (tree β)) (tt₁ : tree β), pure (node (f x_x) x tt₁))
(λ (x_1 : tree β), const_continuous' (pure (node (f x_x) x x_1)))))))))
-- automation coming soon
theorem tree_map.equations.eqn_1 {α : Type u_1} {β : Type u_2} (f : α → β) :
tree_map f nil = pure nil :=
by rw [tree_map,lawful_fix.fix_eq' (tree_map.cont f)]; refl
-- automation coming soon
theorem tree_map.equations.eqn_2 {α : Type u_1} {β : Type u_2} (f : α → β) (x : α)
(t₀ t₁ : tree α) :
tree_map f (node x t₀ t₁) = tree_map f t₀ >>= λ (tt₀ : tree β), tree_map f t₁ >>=
λ (tt₁ : tree β), pure (node (f x) tt₀ tt₁) :=
by conv_lhs { rw [tree_map,lawful_fix.fix_eq' (tree_map.cont f)] }; refl
/-! `map` on a `tree` using applicative notation -/
def tree_map'.intl {α β} (f : α → β) (tree_map : tree α → part (tree β)) :
tree α → part (tree β)
| nil := pure nil
| (node x t₀ t₁) :=
node (f x) <$> tree_map t₀ <*> tree_map t₁
-- automation coming soon
def tree_map' {α : Type u_1} {β : Type u_2} (f : α → β) : tree α → part (tree β) :=
fix (tree_map'.intl f)
-- automation coming soon
theorem tree_map'.cont :
∀ {α : Type u_1} {β : Type u_2} (f : α → β), continuous' (tree_map'.intl f) :=
λ {α : Type u_1} {β : Type u_2} (f : α → β),
pi.omega_complete_partial_order.flip₂_continuous' (tree_map'.intl f)
(λ (x : tree α),
tree.cases_on x (id (const_continuous' (pure nil)))
(λ (x_x : α) (x_a x_a_1 : tree α),
(continuous_hom.seq_continuous' (λ (x : tree α → part (tree β)), node (f x_x) <$> x x_a)
(λ (x : tree α → part (tree β)), x x_a_1)
(continuous_hom.map_continuous' (node (f x_x)) (λ (x : tree α → part (tree β)), x x_a)
(pi.omega_complete_partial_order.flip₁_continuous' (λ (v : tree α) (x : tree α → part (tree β)), x v) x_a id_continuous'))
(pi.omega_complete_partial_order.flip₁_continuous' (λ (v : tree α) (x : tree α → part (tree β)), x v) x_a_1 id_continuous'))))
-- automation coming soon
theorem tree_map'.equations.eqn_1 {α : Type u_1} {β : Type u_2} (f : α → β) :
tree_map' f nil = pure nil :=
by rw [tree_map',lawful_fix.fix_eq' (tree_map'.cont f)]; refl
-- automation coming soon
theorem tree_map'.equations.eqn_2 {α : Type u_1} {β : Type u_2} (f : α → β) (x : α) (t₀ t₁ : tree α) :
tree_map' f (node x t₀ t₁) = node (f x) <$> tree_map' f t₀ <*> tree_map' f t₁ :=
by conv_lhs { rw [tree_map',lawful_fix.fix_eq' (tree_map'.cont f)] }; refl
/-! f91 is a function whose proof of termination cannot rely on the structural
ordering of its arguments and does not use the usual well-founded order
on natural numbers. It is an interesting candidate to show that `fix` lets us disentangle
the issue of termination from the definition of the function. -/
def f91.intl (f91 : ℕ → part ℕ) (n : ℕ) : part ℕ :=
if n > 100
then pure $ n - 10
else f91 (n + 11) >>= f91
-- automation coming soon
def f91 : ℕ → part ℕ := fix f91.intl
-- automation coming soon
lemma f91.cont : continuous' f91.intl :=
pi.omega_complete_partial_order.flip₂_continuous' f91.intl
(λ (x : ℕ),
id
(continuous_hom.ite_continuous' (λ (x_1 : ℕ → part ℕ), pure (x - 10)) (λ (x_1 : ℕ → part ℕ), x_1 (x + 11) >>= x_1)
(const_continuous' (pure (x - 10)))
(continuous_hom.bind_continuous' (λ (x_1 : ℕ → part ℕ), x_1 (x + 11)) (λ (x : ℕ → part ℕ), x)
(pi.omega_complete_partial_order.flip₁_continuous' (λ (v : ℕ) (x : ℕ → part ℕ), x v) (x + 11) id_continuous')
(pi.omega_complete_partial_order.flip₂_continuous' (λ (x : ℕ → part ℕ), x)
(λ (x_1 : ℕ), pi.omega_complete_partial_order.flip₁_continuous' (λ (v : ℕ) (g : ℕ → part ℕ), g v) x_1 id_continuous')))))
.
-- automation coming soon
theorem f91.equations.eqn_1 (n : ℕ) : f91 n = ite (n > 100) (pure (n - 10)) (f91 (n + 11) >>= f91) :=
by conv_lhs { rw [f91, lawful_fix.fix_eq' f91.cont] }; refl
lemma f91_spec (n : ℕ) : (∃ n', n < n' + 11 ∧ n' ∈ f91 n) :=
begin
apply well_founded.induction (measure_wf $ λ n, 101 - n) n,
clear n, dsimp [measure,inv_image], intros n ih,
by_cases h' : n > 100,
{ rw [part.examples.f91.equations.eqn_1,if_pos h'],
existsi n - 10, rw tsub_add_eq_add_tsub, norm_num [pure],
apply le_of_lt, transitivity 100, norm_num, exact h' },
{ rw [part.examples.f91.equations.eqn_1,if_neg h'],
simp, rcases ih (n + 11) _ with ⟨n',hn₀,hn₁⟩,
rcases ih (n') _ with ⟨n'',hn'₀,hn'₁⟩,
refine ⟨n'',_,_,hn₁,hn'₁⟩,
{ clear ih hn₁ hn'₁, omega },
{ clear ih hn₁, omega },
{ clear ih, omega } },
end
lemma f91_dom (n : ℕ) : (f91 n).dom :=
by rw part.dom_iff_mem; apply exists_imp_exists _ (f91_spec n); simp
def f91' (n : ℕ) : ℕ := (f91 n).get (f91_dom n)
run_cmd guard (f91' 109 = 99)
lemma f91_spec' (n : ℕ) : f91' n = if n > 100 then n - 10 else 91 :=
begin
suffices : (∃ n', n' ∈ f91 n ∧ n' = if n > 100 then n - 10 else 91),
{ dsimp [f91'], rw part.get_eq_of_mem,
rcases this with ⟨n,_,_⟩, subst n, assumption },
apply well_founded.induction (measure_wf $ λ n, 101 - n) n,
clear n, dsimp [measure,inv_image], intros n ih,
by_cases h' : n > 100,
{ rw [part.examples.f91.equations.eqn_1,if_pos h',if_pos h'],
simp [pure] },
{ rw [part.examples.f91.equations.eqn_1,if_neg h',if_neg h'],
simp, rcases ih (n + 11) _ with ⟨n',hn'₀,hn'₁⟩,
split_ifs at hn'₁,
{ subst hn'₁, norm_num at hn'₀, refine ⟨_,hn'₀,_⟩,
rcases ih (n+1) _ with ⟨n',hn'₀,hn'₁⟩,
split_ifs at hn'₁,
{ subst n', convert hn'₀, clear hn'₀ hn'₀ ih, omega },
{ subst n', exact hn'₀ },
{ clear ih hn'₀, omega } },
{ refine ⟨_,hn'₀,_⟩, subst n',
rcases ih 91 _ with ⟨n',hn'₀,hn'₁⟩,
rw if_neg at hn'₁, subst n', exact hn'₀,
{ clear ih hn'₀ hn'₀, omega, },
{ clear ih hn'₀, omega, } },
{ clear ih, omega } }
end
end part.examples
|
9bc56e288ee296e13bdc79f540e2c26ddf5bf9f6 | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/linear_algebra/affine_space/affine_map.lean | 4e4179277adf0f259a9cba34daf28fd4e9c5faef | [
"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 | 19,861 | lean | /-
Copyright (c) 2020 Joseph Myers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joseph Myers
-/
import linear_algebra.affine_space.basic
import linear_algebra.tensor_product
import linear_algebra.prod
import linear_algebra.pi
import data.set.intervals.unordered_interval
/-!
# Affine maps
This file defines affine maps.
## Main definitions
* `affine_map` is the type of affine maps between two affine spaces with the same ring `k`. Various
basic examples of affine maps are defined, including `const`, `id`, `line_map` and `homothety`.
## Notations
* `P1 →ᵃ[k] P2` is a notation for `affine_map k P1 P2`;
* `affine_space V P`: a localized notation for `add_torsor V P` defined in
`linear_algebra.affine_space.basic`.
## Implementation notes
`out_param` is used in the definition of `[add_torsor V P]` to make `V` an implicit argument
(deduced from `P`) in most cases; `include V` is needed in many cases for `V`, and type classes
using it, to be added as implicit arguments to individual lemmas. As for modules, `k` is an
explicit argument rather than implied by `P` or `V`.
This file only provides purely algebraic definitions and results. Those depending on analysis or
topology are defined elsewhere; see `analysis.normed_space.add_torsor` and
`topology.algebra.affine`.
## References
* https://en.wikipedia.org/wiki/Affine_space
* https://en.wikipedia.org/wiki/Principal_homogeneous_space
-/
open_locale affine
/-- An `affine_map k P1 P2` (notation: `P1 →ᵃ[k] P2`) is a map from `P1` to `P2` that
induces a corresponding linear map from `V1` to `V2`. -/
structure affine_map (k : Type*) {V1 : Type*} (P1 : Type*) {V2 : Type*} (P2 : Type*)
[ring k]
[add_comm_group V1] [module k V1] [affine_space V1 P1]
[add_comm_group V2] [module k V2] [affine_space V2 P2] :=
(to_fun : P1 → P2)
(linear : V1 →ₗ[k] V2)
(map_vadd' : ∀ (p : P1) (v : V1), to_fun (v +ᵥ p) = linear v +ᵥ to_fun p)
notation P1 ` →ᵃ[`:25 k:25 `] `:0 P2:0 := affine_map k P1 P2
instance (k : Type*) {V1 : Type*} (P1 : Type*) {V2 : Type*} (P2 : Type*)
[ring k]
[add_comm_group V1] [module k V1] [affine_space V1 P1]
[add_comm_group V2] [module k V2] [affine_space V2 P2]:
has_coe_to_fun (P1 →ᵃ[k] P2) := ⟨_, affine_map.to_fun⟩
namespace linear_map
variables {k : Type*} {V₁ : Type*} {V₂ : Type*} [ring k] [add_comm_group V₁] [module k V₁]
[add_comm_group V₂] [module k V₂] (f : V₁ →ₗ[k] V₂)
/-- Reinterpret a linear map as an affine map. -/
def to_affine_map : V₁ →ᵃ[k] V₂ :=
{ to_fun := f,
linear := f,
map_vadd' := λ p v, f.map_add v p }
@[simp] lemma coe_to_affine_map : ⇑f.to_affine_map = f := rfl
@[simp] lemma to_affine_map_linear : f.to_affine_map.linear = f := rfl
end linear_map
namespace affine_map
variables {k : Type*} {V1 : Type*} {P1 : Type*} {V2 : Type*} {P2 : Type*}
{V3 : Type*} {P3 : Type*} {V4 : Type*} {P4 : Type*} [ring k]
[add_comm_group V1] [module k V1] [affine_space V1 P1]
[add_comm_group V2] [module k V2] [affine_space V2 P2]
[add_comm_group V3] [module k V3] [affine_space V3 P3]
[add_comm_group V4] [module k V4] [affine_space V4 P4]
include V1 V2
/-- Constructing an affine map and coercing back to a function
produces the same map. -/
@[simp] lemma coe_mk (f : P1 → P2) (linear add) :
((mk f linear add : P1 →ᵃ[k] P2) : P1 → P2) = f := rfl
/-- `to_fun` is the same as the result of coercing to a function. -/
@[simp] lemma to_fun_eq_coe (f : P1 →ᵃ[k] P2) : f.to_fun = ⇑f := rfl
/-- An affine map on the result of adding a vector to a point produces
the same result as the linear map applied to that vector, added to the
affine map applied to that point. -/
@[simp] lemma map_vadd (f : P1 →ᵃ[k] P2) (p : P1) (v : V1) :
f (v +ᵥ p) = f.linear v +ᵥ f p := f.map_vadd' p v
/-- The linear map on the result of subtracting two points is the
result of subtracting the result of the affine map on those two
points. -/
@[simp] lemma linear_map_vsub (f : P1 →ᵃ[k] P2) (p1 p2 : P1) :
f.linear (p1 -ᵥ p2) = f p1 -ᵥ f p2 :=
by conv_rhs { rw [←vsub_vadd p1 p2, map_vadd, vadd_vsub] }
/-- Two affine maps are equal if they coerce to the same function. -/
@[ext] lemma ext {f g : P1 →ᵃ[k] P2} (h : ∀ p, f p = g p) : f = g :=
begin
rcases f with ⟨f, f_linear, f_add⟩,
rcases g with ⟨g, g_linear, g_add⟩,
have : f = g := funext h,
subst g,
congr' with v,
cases (add_torsor.nonempty : nonempty P1) with p,
apply vadd_right_cancel (f p),
erw [← f_add, ← g_add]
end
lemma ext_iff {f g : P1 →ᵃ[k] P2} : f = g ↔ ∀ p, f p = g p := ⟨λ h p, h ▸ rfl, ext⟩
lemma coe_fn_injective : @function.injective (P1 →ᵃ[k] P2) (P1 → P2) coe_fn :=
λ f g H, ext $ congr_fun H
protected lemma congr_arg (f : P1 →ᵃ[k] P2) {x y : P1} (h : x = y) : f x = f y :=
congr_arg _ h
protected lemma congr_fun {f g : P1 →ᵃ[k] P2} (h : f = g) (x : P1) : f x = g x :=
h ▸ rfl
variables (k P1)
/-- Constant function as an `affine_map`. -/
def const (p : P2) : P1 →ᵃ[k] P2 :=
{ to_fun := function.const P1 p,
linear := 0,
map_vadd' := λ p v, by simp }
@[simp] lemma coe_const (p : P2) : ⇑(const k P1 p) = function.const P1 p := rfl
@[simp] lemma const_linear (p : P2) : (const k P1 p).linear = 0 := rfl
variables {k P1}
instance nonempty : nonempty (P1 →ᵃ[k] P2) :=
(add_torsor.nonempty : nonempty P2).elim $ λ p, ⟨const k P1 p⟩
/-- Construct an affine map by verifying the relation between the map and its linear part at one
base point. Namely, this function takes a map `f : P₁ → P₂`, a linear map `f' : V₁ →ₗ[k] V₂`, and
a point `p` such that for any other point `p'` we have `f p' = f' (p' -ᵥ p) +ᵥ f p`. -/
def mk' (f : P1 → P2) (f' : V1 →ₗ[k] V2) (p : P1) (h : ∀ p' : P1, f p' = f' (p' -ᵥ p) +ᵥ f p) :
P1 →ᵃ[k] P2 :=
{ to_fun := f,
linear := f',
map_vadd' := λ p' v, by rw [h, h p', vadd_vsub_assoc, f'.map_add, vadd_vadd] }
@[simp] lemma coe_mk' (f : P1 → P2) (f' : V1 →ₗ[k] V2) (p h) : ⇑(mk' f f' p h) = f := rfl
@[simp] lemma mk'_linear (f : P1 → P2) (f' : V1 →ₗ[k] V2) (p h) : (mk' f f' p h).linear = f' := rfl
/-- The set of affine maps to a vector space is an additive commutative group. -/
instance : add_comm_group (P1 →ᵃ[k] V2) :=
{ zero := ⟨0, 0, λ p v, (zero_vadd _ _).symm⟩,
add := λ f g, ⟨f + g, f.linear + g.linear, λ p v, by simp [add_add_add_comm]⟩,
neg := λ f, ⟨-f, -f.linear, λ p v, by simp [add_comm]⟩,
add_assoc := λ f₁ f₂ f₃, ext $ λ p, add_assoc _ _ _,
zero_add := λ f, ext $ λ p, zero_add (f p),
add_zero := λ f, ext $ λ p, add_zero (f p),
add_comm := λ f g, ext $ λ p, add_comm (f p) (g p),
add_left_neg := λ f, ext $ λ p, add_left_neg (f p) }
@[simp, norm_cast] lemma coe_zero : ⇑(0 : P1 →ᵃ[k] V2) = 0 := rfl
@[simp] lemma zero_linear : (0 : P1 →ᵃ[k] V2).linear = 0 := rfl
@[simp, norm_cast] lemma coe_add (f g : P1 →ᵃ[k] V2) : ⇑(f + g) = f + g := rfl
@[simp]
lemma add_linear (f g : P1 →ᵃ[k] V2) : (f + g).linear = f.linear + g.linear := rfl
/-- The space of affine maps from `P1` to `P2` is an affine space over the space of affine maps
from `P1` to the vector space `V2` corresponding to `P2`. -/
instance : affine_space (P1 →ᵃ[k] V2) (P1 →ᵃ[k] P2) :=
{ vadd := λ f g, ⟨λ p, f p +ᵥ g p, f.linear + g.linear, λ p v,
by simp [vadd_vadd, add_right_comm]⟩,
zero_vadd := λ f, ext $ λ p, zero_vadd _ (f p),
add_vadd := λ f₁ f₂ f₃, ext $ λ p, add_vadd (f₁ p) (f₂ p) (f₃ p),
vsub := λ f g, ⟨λ p, f p -ᵥ g p, f.linear - g.linear, λ p v,
by simp [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub, sub_add_eq_add_sub]⟩,
vsub_vadd' := λ f g, ext $ λ p, vsub_vadd (f p) (g p),
vadd_vsub' := λ f g, ext $ λ p, vadd_vsub (f p) (g p) }
@[simp] lemma vadd_apply (f : P1 →ᵃ[k] V2) (g : P1 →ᵃ[k] P2) (p : P1) :
(f +ᵥ g) p = f p +ᵥ g p :=
rfl
@[simp] lemma vsub_apply (f g : P1 →ᵃ[k] P2) (p : P1) :
(f -ᵥ g : P1 →ᵃ[k] V2) p = f p -ᵥ g p :=
rfl
/-- `prod.fst` as an `affine_map`. -/
def fst : (P1 × P2) →ᵃ[k] P1 :=
{ to_fun := prod.fst,
linear := linear_map.fst k V1 V2,
map_vadd' := λ _ _, rfl }
@[simp] lemma coe_fst : ⇑(fst : (P1 × P2) →ᵃ[k] P1) = prod.fst := rfl
@[simp] lemma fst_linear : (fst : (P1 × P2) →ᵃ[k] P1).linear = linear_map.fst k V1 V2 := rfl
/-- `prod.snd` as an `affine_map`. -/
def snd : (P1 × P2) →ᵃ[k] P2 :=
{ to_fun := prod.snd,
linear := linear_map.snd k V1 V2,
map_vadd' := λ _ _, rfl }
@[simp] lemma coe_snd : ⇑(snd : (P1 × P2) →ᵃ[k] P2) = prod.snd := rfl
@[simp] lemma snd_linear : (snd : (P1 × P2) →ᵃ[k] P2).linear = linear_map.snd k V1 V2 := rfl
variables (k P1)
omit V2
/-- Identity map as an affine map. -/
def id : P1 →ᵃ[k] P1 :=
{ to_fun := id,
linear := linear_map.id,
map_vadd' := λ p v, rfl }
/-- The identity affine map acts as the identity. -/
@[simp] lemma coe_id : ⇑(id k P1) = _root_.id := rfl
@[simp] lemma id_linear : (id k P1).linear = linear_map.id := rfl
variable {P1}
/-- The identity affine map acts as the identity. -/
lemma id_apply (p : P1) : id k P1 p = p := rfl
variables {k P1}
instance : inhabited (P1 →ᵃ[k] P1) := ⟨id k P1⟩
include V2 V3
/-- Composition of affine maps. -/
def comp (f : P2 →ᵃ[k] P3) (g : P1 →ᵃ[k] P2) : P1 →ᵃ[k] P3 :=
{ to_fun := f ∘ g,
linear := f.linear.comp g.linear,
map_vadd' := begin
intros p v,
rw [function.comp_app, g.map_vadd, f.map_vadd],
refl
end }
/-- Composition of affine maps acts as applying the two functions. -/
@[simp] lemma coe_comp (f : P2 →ᵃ[k] P3) (g : P1 →ᵃ[k] P2) :
⇑(f.comp g) = f ∘ g := rfl
/-- Composition of affine maps acts as applying the two functions. -/
lemma comp_apply (f : P2 →ᵃ[k] P3) (g : P1 →ᵃ[k] P2) (p : P1) :
f.comp g p = f (g p) := rfl
omit V3
@[simp] lemma comp_id (f : P1 →ᵃ[k] P2) : f.comp (id k P1) = f := ext $ λ p, rfl
@[simp] lemma id_comp (f : P1 →ᵃ[k] P2) : (id k P2).comp f = f := ext $ λ p, rfl
include V3 V4
lemma comp_assoc (f₃₄ : P3 →ᵃ[k] P4) (f₂₃ : P2 →ᵃ[k] P3) (f₁₂ : P1 →ᵃ[k] P2) :
(f₃₄.comp f₂₃).comp f₁₂ = f₃₄.comp (f₂₃.comp f₁₂) :=
rfl
omit V2 V3 V4
instance : monoid (P1 →ᵃ[k] P1) :=
{ one := id k P1,
mul := comp,
one_mul := id_comp,
mul_one := comp_id,
mul_assoc := comp_assoc }
@[simp] lemma coe_mul (f g : P1 →ᵃ[k] P1) : ⇑(f * g) = f ∘ g := rfl
@[simp] lemma coe_one : ⇑(1 : P1 →ᵃ[k] P1) = _root_.id := rfl
include V2
@[simp] lemma injective_iff_linear_injective (f : P1 →ᵃ[k] P2) :
function.injective f.linear ↔ function.injective f :=
begin
split; intros hf x y hxy,
{ rw [← @vsub_eq_zero_iff_eq V1, ← @submodule.mem_bot k V1, ← linear_map.ker_eq_bot.mpr hf,
linear_map.mem_ker, affine_map.linear_map_vsub, hxy, vsub_self], },
{ obtain ⟨p⟩ := (by apply_instance : nonempty P1),
have hxy' : (f.linear x) +ᵥ f p = (f.linear y) +ᵥ f p, { rw hxy, },
rw [← f.map_vadd, ← f.map_vadd] at hxy',
exact (vadd_right_cancel_iff _).mp (hf hxy'), },
end
omit V2
/-! ### Definition of `affine_map.line_map` and lemmas about it -/
/-- The affine map from `k` to `P1` sending `0` to `p₀` and `1` to `p₁`. -/
def line_map (p₀ p₁ : P1) : k →ᵃ[k] P1 :=
((linear_map.id : k →ₗ[k] k).smul_right (p₁ -ᵥ p₀)).to_affine_map +ᵥ const k k p₀
lemma coe_line_map (p₀ p₁ : P1) : (line_map p₀ p₁ : k → P1) = λ c, c • (p₁ -ᵥ p₀) +ᵥ p₀ := rfl
lemma line_map_apply (p₀ p₁ : P1) (c : k) : line_map p₀ p₁ c = c • (p₁ -ᵥ p₀) +ᵥ p₀ := rfl
lemma line_map_apply_module' (p₀ p₁ : V1) (c : k) : line_map p₀ p₁ c = c • (p₁ - p₀) + p₀ := rfl
lemma line_map_apply_module (p₀ p₁ : V1) (c : k) : line_map p₀ p₁ c = (1 - c) • p₀ + c • p₁ :=
by simp [line_map_apply_module', smul_sub, sub_smul]; abel
omit V1
lemma line_map_apply_ring' (a b c : k) : line_map a b c = c * (b - a) + a :=
rfl
lemma line_map_apply_ring (a b c : k) : line_map a b c = (1 - c) * a + c * b :=
line_map_apply_module a b c
include V1
lemma line_map_vadd_apply (p : P1) (v : V1) (c : k) :
line_map p (v +ᵥ p) c = c • v +ᵥ p :=
by rw [line_map_apply, vadd_vsub]
@[simp] lemma line_map_linear (p₀ p₁ : P1) :
(line_map p₀ p₁ : k →ᵃ[k] P1).linear = linear_map.id.smul_right (p₁ -ᵥ p₀) :=
add_zero _
lemma line_map_same_apply (p : P1) (c : k) : line_map p p c = p := by simp [line_map_apply]
@[simp] lemma line_map_same (p : P1) : line_map p p = const k k p :=
ext $ line_map_same_apply p
@[simp] lemma line_map_apply_zero (p₀ p₁ : P1) : line_map p₀ p₁ (0:k) = p₀ :=
by simp [line_map_apply]
@[simp] lemma line_map_apply_one (p₀ p₁ : P1) : line_map p₀ p₁ (1:k) = p₁ :=
by simp [line_map_apply]
include V2
@[simp] lemma apply_line_map (f : P1 →ᵃ[k] P2) (p₀ p₁ : P1) (c : k) :
f (line_map p₀ p₁ c) = line_map (f p₀) (f p₁) c :=
by simp [line_map_apply]
@[simp] lemma comp_line_map (f : P1 →ᵃ[k] P2) (p₀ p₁ : P1) :
f.comp (line_map p₀ p₁) = line_map (f p₀) (f p₁) :=
ext $ f.apply_line_map p₀ p₁
@[simp] lemma fst_line_map (p₀ p₁ : P1 × P2) (c : k) :
(line_map p₀ p₁ c).1 = line_map p₀.1 p₁.1 c :=
fst.apply_line_map p₀ p₁ c
@[simp] lemma snd_line_map (p₀ p₁ : P1 × P2) (c : k) :
(line_map p₀ p₁ c).2 = line_map p₀.2 p₁.2 c :=
snd.apply_line_map p₀ p₁ c
omit V2
lemma line_map_symm (p₀ p₁ : P1) :
line_map p₀ p₁ = (line_map p₁ p₀).comp (line_map (1:k) (0:k)) :=
by { rw [comp_line_map], simp }
lemma line_map_apply_one_sub (p₀ p₁ : P1) (c : k) :
line_map p₀ p₁ (1 - c) = line_map p₁ p₀ c :=
by { rw [line_map_symm p₀, comp_apply], congr, simp [line_map_apply] }
@[simp] lemma line_map_vsub_left (p₀ p₁ : P1) (c : k) :
line_map p₀ p₁ c -ᵥ p₀ = c • (p₁ -ᵥ p₀) :=
vadd_vsub _ _
@[simp] lemma left_vsub_line_map (p₀ p₁ : P1) (c : k) :
p₀ -ᵥ line_map p₀ p₁ c = c • (p₀ -ᵥ p₁) :=
by rw [← neg_vsub_eq_vsub_rev, line_map_vsub_left, ← smul_neg, neg_vsub_eq_vsub_rev]
@[simp] lemma line_map_vsub_right (p₀ p₁ : P1) (c : k) :
line_map p₀ p₁ c -ᵥ p₁ = (1 - c) • (p₀ -ᵥ p₁) :=
by rw [← line_map_apply_one_sub, line_map_vsub_left]
@[simp] lemma right_vsub_line_map (p₀ p₁ : P1) (c : k) :
p₁ -ᵥ line_map p₀ p₁ c = (1 - c) • (p₁ -ᵥ p₀) :=
by rw [← line_map_apply_one_sub, left_vsub_line_map]
lemma line_map_vadd_line_map (v₁ v₂ : V1) (p₁ p₂ : P1) (c : k) :
line_map v₁ v₂ c +ᵥ line_map p₁ p₂ c = line_map (v₁ +ᵥ p₁) (v₂ +ᵥ p₂) c :=
((fst : V1 × P1 →ᵃ[k] V1) +ᵥ snd).apply_line_map (v₁, p₁) (v₂, p₂) c
lemma line_map_vsub_line_map (p₁ p₂ p₃ p₄ : P1) (c : k) :
line_map p₁ p₂ c -ᵥ line_map p₃ p₄ c = line_map (p₁ -ᵥ p₃) (p₂ -ᵥ p₄) c :=
-- Why Lean fails to find this instance without a hint?
by letI : affine_space (V1 × V1) (P1 × P1) := prod.add_torsor; exact
((fst : P1 × P1 →ᵃ[k] P1) -ᵥ (snd : P1 × P1 →ᵃ[k] P1)).apply_line_map (_, _) (_, _) c
/-- Decomposition of an affine map in the special case when the point space and vector space
are the same. -/
lemma decomp (f : V1 →ᵃ[k] V2) : (f : V1 → V2) = f.linear + (λ z, f 0) :=
begin
ext x,
calc
f x = f.linear x +ᵥ f 0 : by simp [← f.map_vadd]
... = (f.linear.to_fun + λ (z : V1), f 0) x : by simp
end
/-- Decomposition of an affine map in the special case when the point space and vector space
are the same. -/
lemma decomp' (f : V1 →ᵃ[k] V2) : (f.linear : V1 → V2) = f - (λ z, f 0) :=
by rw decomp ; simp only [linear_map.map_zero, pi.add_apply, add_sub_cancel, zero_add]
omit V1
lemma image_interval {k : Type*} [linear_ordered_field k] (f : k →ᵃ[k] k)
(a b : k) :
f '' set.interval a b = set.interval (f a) (f b) :=
begin
have : ⇑f = (λ x, x + f 0) ∘ λ x, x * (f 1 - f 0),
{ ext x,
change f x = x • (f 1 -ᵥ f 0) +ᵥ f 0,
rw [← f.linear_map_vsub, ← f.linear.map_smul, ← f.map_vadd],
simp only [vsub_eq_sub, add_zero, mul_one, vadd_eq_add, sub_zero, smul_eq_mul] },
rw [this, set.image_comp],
simp only [set.image_add_const_interval, set.image_mul_const_interval]
end
section
variables {ι : Type*} {V : Π i : ι, Type*} {P : Π i : ι, Type*} [Π i, add_comm_group (V i)]
[Π i, module k (V i)] [Π i, add_torsor (V i) (P i)]
include V
/-- Evaluation at a point as an affine map. -/
def proj (i : ι) : (Π i : ι, P i) →ᵃ[k] P i :=
{ to_fun := λ f, f i,
linear := @linear_map.proj k ι _ V _ _ i,
map_vadd' := λ p v, rfl }
@[simp] lemma proj_apply (i : ι) (f : Π i, P i) : @proj k _ ι V P _ _ _ i f = f i := rfl
@[simp] lemma proj_linear (i : ι) :
(@proj k _ ι V P _ _ _ i).linear = @linear_map.proj k ι _ V _ _ i := rfl
lemma pi_line_map_apply (f g : Π i, P i) (c : k) (i : ι) :
line_map f g c i = line_map (f i) (g i) c :=
(proj i : (Π i, P i) →ᵃ[k] P i).apply_line_map f g c
end
end affine_map
namespace affine_map
variables {k : Type*} {V1 : Type*} {P1 : Type*} {V2 : Type*} [comm_ring k]
[add_comm_group V1] [module k V1] [affine_space V1 P1] [add_comm_group V2] [module k V2]
include V1
/-- If `k` is a commutative ring, then the set of affine maps with codomain in a `k`-module
is a `k`-module. -/
instance : module k (P1 →ᵃ[k] V2) :=
{ smul := λ c f, ⟨c • f, c • f.linear, λ p v, by simp [smul_add]⟩,
one_smul := λ f, ext $ λ p, one_smul _ _,
mul_smul := λ c₁ c₂ f, ext $ λ p, mul_smul _ _ _,
smul_add := λ c f g, ext $ λ p, smul_add _ _ _,
smul_zero := λ c, ext $ λ p, smul_zero _,
add_smul := λ c₁ c₂ f, ext $ λ p, add_smul _ _ _,
zero_smul := λ f, ext $ λ p, zero_smul _ _ }
@[simp] lemma coe_smul (c : k) (f : P1 →ᵃ[k] V2) : ⇑(c • f) = c • f := rfl
/-- `homothety c r` is the homothety (also known as dilation) about `c` with scale factor `r`. -/
def homothety (c : P1) (r : k) : P1 →ᵃ[k] P1 :=
r • (id k P1 -ᵥ const k P1 c) +ᵥ const k P1 c
lemma homothety_def (c : P1) (r : k) :
homothety c r = r • (id k P1 -ᵥ const k P1 c) +ᵥ const k P1 c :=
rfl
lemma homothety_apply (c : P1) (r : k) (p : P1) : homothety c r p = r • (p -ᵥ c : V1) +ᵥ c := rfl
lemma homothety_eq_line_map (c : P1) (r : k) (p : P1) : homothety c r p = line_map c p r := rfl
@[simp] lemma homothety_one (c : P1) : homothety c (1:k) = id k P1 :=
by { ext p, simp [homothety_apply] }
lemma homothety_mul (c : P1) (r₁ r₂ : k) :
homothety c (r₁ * r₂) = (homothety c r₁).comp (homothety c r₂) :=
by { ext p, simp [homothety_apply, mul_smul] }
@[simp] lemma homothety_zero (c : P1) : homothety c (0:k) = const k P1 c :=
by { ext p, simp [homothety_apply] }
@[simp] lemma homothety_add (c : P1) (r₁ r₂ : k) :
homothety c (r₁ + r₂) = r₁ • (id k P1 -ᵥ const k P1 c) +ᵥ homothety c r₂ :=
by simp only [homothety_def, add_smul, vadd_vadd]
/-- `homothety` as a multiplicative monoid homomorphism. -/
def homothety_hom (c : P1) : k →* P1 →ᵃ[k] P1 :=
⟨homothety c, homothety_one c, homothety_mul c⟩
@[simp] lemma coe_homothety_hom (c : P1) : ⇑(homothety_hom c : k →* _) = homothety c := rfl
/-- `homothety` as an affine map. -/
def homothety_affine (c : P1) : k →ᵃ[k] (P1 →ᵃ[k] P1) :=
⟨homothety c, (linear_map.lsmul k _).flip (id k P1 -ᵥ const k P1 c),
function.swap (homothety_add c)⟩
@[simp] lemma coe_homothety_affine (c : P1) :
⇑(homothety_affine c : k →ᵃ[k] _) = homothety c :=
rfl
end affine_map
|
1aad99b96c42268f9aab1edf8f88e224b8a91ec7 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/field_theory/galois.lean | 5b42690ea8dc0940a4b11396d11f3629272c54a8 | [
"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 | 18,247 | lean | /-
Copyright (c) 2020 Thomas Browning, Patrick Lutz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Thomas Browning, Patrick Lutz
-/
import field_theory.normal
import field_theory.primitive_element
import field_theory.fixed
import ring_theory.power_basis
/-!
# Galois Extensions
In this file we define Galois extensions as extensions which are both separable and normal.
## Main definitions
- `is_galois F E` where `E` is an extension of `F`
- `fixed_field H` where `H : subgroup (E ≃ₐ[F] E)`
- `fixing_subgroup K` where `K : intermediate_field F E`
- `galois_correspondence` where `E/F` is finite dimensional and Galois
## Main results
- `fixing_subgroup_of_fixed_field` : If `E/F` is finite dimensional (but not necessarily Galois)
then `fixing_subgroup (fixed_field H) = H`
- `fixed_field_of_fixing_subgroup`: If `E/F` is finite dimensional and Galois
then `fixed_field (fixing_subgroup K) = K`
Together, these two result prove the Galois correspondence
- `is_galois.tfae` : Equivalent characterizations of a Galois extension of finite degree
-/
noncomputable theory
open_locale classical
open finite_dimensional alg_equiv
section
variables (F : Type*) [field F] (E : Type*) [field E] [algebra F E]
/-- A field extension E/F is galois if it is both separable and normal -/
class is_galois : Prop :=
[to_is_separable : is_separable F E]
[to_normal : normal F E]
variables {F E}
theorem is_galois_iff : is_galois F E ↔ is_separable F E ∧ normal F E :=
⟨λ h, ⟨h.1, h.2⟩, λ h, { to_is_separable := h.1, to_normal := h.2 }⟩
attribute [instance, priority 100] -- see Note [lower instance priority]
is_galois.to_is_separable is_galois.to_normal
variables (F E)
namespace is_galois
instance self : is_galois F F :=
⟨⟩
variables (F) {E}
lemma integral [is_galois F E] (x : E) : is_integral F x := normal.is_integral' x
lemma separable [is_galois F E] (x : E) : (minpoly F x).separable := is_separable.separable F x
lemma splits [is_galois F E] (x : E) : (minpoly F x).splits (algebra_map F E) := normal.splits' x
variables (F E)
instance of_fixed_field (G : Type*) [group G] [fintype G] [mul_semiring_action G E] :
is_galois (fixed_points.subfield G E) E :=
⟨⟩
lemma intermediate_field.adjoin_simple.card_aut_eq_finrank
[finite_dimensional F E] {α : E} (hα : is_integral F α)
(h_sep : (minpoly F α).separable)
(h_splits : (minpoly F α).splits (algebra_map F F⟮α⟯)) :
fintype.card (F⟮α⟯ ≃ₐ[F] F⟮α⟯) = finrank F F⟮α⟯ :=
begin
letI : fintype (F⟮α⟯ →ₐ[F] F⟮α⟯) := intermediate_field.fintype_of_alg_hom_adjoin_integral F hα,
rw intermediate_field.adjoin.finrank hα,
rw ← intermediate_field.card_alg_hom_adjoin_integral F hα h_sep h_splits,
exact fintype.card_congr (alg_equiv_equiv_alg_hom F F⟮α⟯)
end
lemma card_aut_eq_finrank [finite_dimensional F E] [is_galois F E] :
fintype.card (E ≃ₐ[F] E) = finrank F E :=
begin
cases field.exists_primitive_element F E with α hα,
let iso : F⟮α⟯ ≃ₐ[F] E := {
to_fun := λ e, e.val,
inv_fun := λ e, ⟨e, by { rw hα, exact intermediate_field.mem_top }⟩,
left_inv := λ _, by { ext, refl },
right_inv := λ _, rfl,
map_mul' := λ _ _, rfl,
map_add' := λ _ _, rfl,
commutes' := λ _, rfl },
have H : is_integral F α := is_galois.integral F α,
have h_sep : (minpoly F α).separable := is_galois.separable F α,
have h_splits : (minpoly F α).splits (algebra_map F E) := is_galois.splits F α,
replace h_splits : polynomial.splits (algebra_map F F⟮α⟯) (minpoly F α),
{ have p : iso.symm.to_alg_hom.to_ring_hom.comp (algebra_map F E) = (algebra_map F ↥F⟮α⟯),
{ ext, simp, },
simpa [p] using polynomial.splits_comp_of_splits
(algebra_map F E) iso.symm.to_alg_hom.to_ring_hom h_splits, },
rw ← linear_equiv.finrank_eq iso.to_linear_equiv,
rw ← intermediate_field.adjoin_simple.card_aut_eq_finrank F E H h_sep h_splits,
apply fintype.card_congr,
apply equiv.mk (λ ϕ, iso.trans (trans ϕ iso.symm)) (λ ϕ, iso.symm.trans (trans ϕ iso)),
{ intro ϕ, ext1, simp only [trans_apply, apply_symm_apply] },
{ intro ϕ, ext1, simp only [trans_apply, symm_apply_apply] },
end
end is_galois
end
section is_galois_tower
variables (F K E : Type*) [field F] [field K] [field E] {E' : Type*} [field E'] [algebra F E']
variables [algebra F K] [algebra F E] [algebra K E] [is_scalar_tower F K E]
lemma is_galois.tower_top_of_is_galois [is_galois F E] : is_galois K E :=
{ to_is_separable := is_separable_tower_top_of_is_separable F K E,
to_normal := normal.tower_top_of_normal F K E }
variables {F E}
@[priority 100] -- see Note [lower instance priority]
instance is_galois.tower_top_intermediate_field (K : intermediate_field F E) [h : is_galois F E] :
is_galois K E := is_galois.tower_top_of_is_galois F K E
lemma is_galois_iff_is_galois_bot : is_galois (⊥ : intermediate_field F E) E ↔ is_galois F E :=
begin
split,
{ introI h,
exact is_galois.tower_top_of_is_galois (⊥ : intermediate_field F E) F E },
{ introI h, apply_instance },
end
lemma is_galois.of_alg_equiv [h : is_galois F E] (f : E ≃ₐ[F] E') : is_galois F E' :=
{ to_is_separable := is_separable.of_alg_hom F E f.symm, to_normal := normal.of_alg_equiv f }
lemma alg_equiv.transfer_galois (f : E ≃ₐ[F] E') : is_galois F E ↔ is_galois F E' :=
⟨λ h, by exactI is_galois.of_alg_equiv f, λ h, by exactI is_galois.of_alg_equiv f.symm⟩
lemma is_galois_iff_is_galois_top : is_galois F (⊤ : intermediate_field F E) ↔ is_galois F E :=
(intermediate_field.top_equiv).transfer_galois
instance is_galois_bot : is_galois F (⊥ : intermediate_field F E) :=
(intermediate_field.bot_equiv F E).transfer_galois.mpr (is_galois.self F)
end is_galois_tower
section galois_correspondence
variables {F : Type*} [field F] {E : Type*} [field E] [algebra F E]
variables (H : subgroup (E ≃ₐ[F] E)) (K : intermediate_field F E)
namespace intermediate_field
/-- The intermediate_field fixed by a subgroup -/
def fixed_field : intermediate_field F E :=
{ carrier := mul_action.fixed_points H E,
zero_mem' := λ g, smul_zero g,
add_mem' := λ a b hx hy g, by rw [smul_add g a b, hx, hy],
neg_mem' := λ a hx g, by rw [smul_neg g a, hx],
one_mem' := λ g, smul_one g,
mul_mem' := λ a b hx hy g, by rw [smul_mul' g a b, hx, hy],
inv_mem' := λ a hx g, by rw [smul_inv'' g a, hx],
algebra_map_mem' := λ a g, commutes g a }
lemma finrank_fixed_field_eq_card [finite_dimensional F E] :
finrank (fixed_field H) E = fintype.card H :=
fixed_points.finrank_eq_card H E
/-- The subgroup fixing an intermediate_field -/
def fixing_subgroup : subgroup (E ≃ₐ[F] E) :=
{ carrier := λ ϕ, ∀ x : K, ϕ x = x,
one_mem' := λ _, rfl,
mul_mem' := λ _ _ hx hy _, (congr_arg _ (hy _)).trans (hx _),
inv_mem' := λ _ hx _, (equiv.symm_apply_eq (to_equiv _)).mpr (hx _).symm }
lemma le_iff_le : K ≤ fixed_field H ↔ H ≤ fixing_subgroup K :=
⟨λ h g hg x, h (subtype.mem x) ⟨g, hg⟩, λ h x hx g, h (subtype.mem g) ⟨x, hx⟩⟩
/-- The fixing_subgroup of `K : intermediate_field F E` is isomorphic to `E ≃ₐ[K] E` -/
def fixing_subgroup_equiv : fixing_subgroup K ≃* (E ≃ₐ[K] E) :=
{ to_fun := λ ϕ, of_bijective (alg_hom.mk ϕ (map_one ϕ) (map_mul ϕ)
(map_zero ϕ) (map_add ϕ) (ϕ.mem)) (bijective ϕ),
inv_fun := λ ϕ, ⟨of_bijective (alg_hom.mk ϕ (ϕ.map_one) (ϕ.map_mul)
(ϕ.map_zero) (ϕ.map_add) (λ r, ϕ.commutes (algebra_map F K r)))
(ϕ.bijective), ϕ.commutes⟩,
left_inv := λ _, by { ext, refl },
right_inv := λ _, by { ext, refl },
map_mul' := λ _ _, by { ext, refl } }
theorem fixing_subgroup_fixed_field [finite_dimensional F E] :
fixing_subgroup (fixed_field H) = H :=
begin
have H_le : H ≤ (fixing_subgroup (fixed_field H)) := (le_iff_le _ _).mp (le_refl _),
suffices : fintype.card H = fintype.card (fixing_subgroup (fixed_field H)),
{ exact set_like.coe_injective
(set.eq_of_inclusion_surjective ((fintype.bijective_iff_injective_and_card
(set.inclusion H_le)).mpr ⟨set.inclusion_injective H_le, this⟩).2).symm },
apply fintype.card_congr,
refine (fixed_points.to_alg_hom_equiv H E).trans _,
refine (alg_equiv_equiv_alg_hom (fixed_field H) E).symm.trans _,
exact (fixing_subgroup_equiv (fixed_field H)).to_equiv.symm
end
instance fixed_field.algebra : algebra K (fixed_field (fixing_subgroup K)) :=
{ smul := λ x y, ⟨x*y, λ ϕ, by rw [smul_mul', (show ϕ • ↑x = ↑x, by exact subtype.mem ϕ x),
(show ϕ • ↑y = ↑y, by exact subtype.mem y ϕ)]⟩,
to_fun := λ x, ⟨x, λ ϕ, subtype.mem ϕ x⟩,
map_zero' := rfl,
map_add' := λ _ _, rfl,
map_one' := rfl,
map_mul' := λ _ _, rfl,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ _ _, rfl }
instance fixed_field.is_scalar_tower : is_scalar_tower K (fixed_field (fixing_subgroup K)) E :=
⟨λ _ _ _, mul_assoc _ _ _⟩
end intermediate_field
namespace is_galois
theorem fixed_field_fixing_subgroup [finite_dimensional F E] [h : is_galois F E] :
intermediate_field.fixed_field (intermediate_field.fixing_subgroup K) = K :=
begin
have K_le : K ≤ intermediate_field.fixed_field (intermediate_field.fixing_subgroup K) :=
(intermediate_field.le_iff_le _ _).mpr (le_refl _),
suffices : finrank K E =
finrank (intermediate_field.fixed_field (intermediate_field.fixing_subgroup K)) E,
{ exact (intermediate_field.eq_of_le_of_finrank_eq' K_le this).symm },
rw [intermediate_field.finrank_fixed_field_eq_card,
fintype.card_congr (intermediate_field.fixing_subgroup_equiv K).to_equiv],
exact (card_aut_eq_finrank K E).symm,
end
lemma card_fixing_subgroup_eq_finrank [finite_dimensional F E] [is_galois F E] :
fintype.card (intermediate_field.fixing_subgroup K) = finrank K E :=
by conv { to_rhs, rw [←fixed_field_fixing_subgroup K,
intermediate_field.finrank_fixed_field_eq_card] }
/-- The Galois correspondence from intermediate fields to subgroups -/
def intermediate_field_equiv_subgroup [finite_dimensional F E] [is_galois F E] :
intermediate_field F E ≃o order_dual (subgroup (E ≃ₐ[F] E)) :=
{ to_fun := intermediate_field.fixing_subgroup,
inv_fun := intermediate_field.fixed_field,
left_inv := λ K, fixed_field_fixing_subgroup K,
right_inv := λ H, intermediate_field.fixing_subgroup_fixed_field H,
map_rel_iff' := λ K L, by { rw [←fixed_field_fixing_subgroup L, intermediate_field.le_iff_le,
fixed_field_fixing_subgroup L, ←order_dual.dual_le], refl } }
/-- The Galois correspondence as a galois_insertion -/
def galois_insertion_intermediate_field_subgroup [finite_dimensional F E] :
galois_insertion (order_dual.to_dual ∘
(intermediate_field.fixing_subgroup : intermediate_field F E → subgroup (E ≃ₐ[F] E)))
((intermediate_field.fixed_field : subgroup (E ≃ₐ[F] E) → intermediate_field F E) ∘
order_dual.to_dual) :=
{ choice := λ K _, intermediate_field.fixing_subgroup K,
gc := λ K H, (intermediate_field.le_iff_le H K).symm,
le_l_u := λ H, le_of_eq (intermediate_field.fixing_subgroup_fixed_field H).symm,
choice_eq := λ K _, rfl }
/-- The Galois correspondence as a galois_coinsertion -/
def galois_coinsertion_intermediate_field_subgroup [finite_dimensional F E] [is_galois F E] :
galois_coinsertion (order_dual.to_dual ∘
(intermediate_field.fixing_subgroup : intermediate_field F E → subgroup (E ≃ₐ[F] E)))
((intermediate_field.fixed_field : subgroup (E ≃ₐ[F] E) → intermediate_field F E) ∘
order_dual.to_dual) :=
{ choice := λ H _, intermediate_field.fixed_field H,
gc := λ K H, (intermediate_field.le_iff_le H K).symm,
u_l_le := λ K, le_of_eq (fixed_field_fixing_subgroup K),
choice_eq := λ H _, rfl }
end is_galois
end galois_correspondence
section galois_equivalent_definitions
variables (F : Type*) [field F] (E : Type*) [field E] [algebra F E]
namespace is_galois
lemma is_separable_splitting_field [finite_dimensional F E] [is_galois F E] :
∃ p : polynomial F, p.separable ∧ p.is_splitting_field F E :=
begin
cases field.exists_primitive_element F E with α h1,
use [minpoly F α, separable F α, is_galois.splits F α],
rw [eq_top_iff, ←intermediate_field.top_to_subalgebra, ←h1],
rw intermediate_field.adjoin_simple_to_subalgebra_of_integral F α (integral F α),
apply algebra.adjoin_mono,
rw [set.singleton_subset_iff, finset.mem_coe, multiset.mem_to_finset, polynomial.mem_roots],
{ dsimp only [polynomial.is_root],
rw [polynomial.eval_map, ←polynomial.aeval_def],
exact minpoly.aeval _ _ },
{ exact polynomial.map_ne_zero (minpoly.ne_zero (integral F α)) }
end
lemma of_fixed_field_eq_bot [finite_dimensional F E]
(h : intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E)) = ⊥) : is_galois F E :=
begin
rw [←is_galois_iff_is_galois_bot, ←h],
exact is_galois.of_fixed_field E (⊤ : subgroup (E ≃ₐ[F] E)),
end
lemma of_card_aut_eq_finrank [finite_dimensional F E]
(h : fintype.card (E ≃ₐ[F] E) = finrank F E) : is_galois F E :=
begin
apply of_fixed_field_eq_bot,
have p : 0 < finrank (intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E))) E := finrank_pos,
rw [←intermediate_field.finrank_eq_one_iff, ←mul_left_inj' (ne_of_lt p).symm, finrank_mul_finrank,
←h, one_mul, intermediate_field.finrank_fixed_field_eq_card],
apply fintype.card_congr,
exact { to_fun := λ g, ⟨g, subgroup.mem_top g⟩, inv_fun := coe,
left_inv := λ g, rfl, right_inv := λ _, by { ext, refl } },
end
variables {F} {E} {p : polynomial F}
lemma of_separable_splitting_field_aux [hFE : finite_dimensional F E]
[sp : p.is_splitting_field F E] (hp : p.separable) (K : intermediate_field F E) {x : E}
(hx : x ∈ (p.map (algebra_map F E)).roots) :
fintype.card ((↑K⟮x⟯ : intermediate_field F E) →ₐ[F] E) =
fintype.card (K →ₐ[F] E) * finrank K K⟮x⟯ :=
begin
have h : is_integral K x := is_integral_of_is_scalar_tower x
(is_integral_of_noetherian (is_noetherian.iff_fg.2 hFE) x),
have h1 : p ≠ 0 := λ hp, by rwa [hp, polynomial.map_zero, polynomial.roots_zero] at hx,
have h2 : (minpoly K x) ∣ p.map (algebra_map F K),
{ apply minpoly.dvd,
rw [polynomial.aeval_def, polynomial.eval₂_map, ←polynomial.eval_map],
exact (polynomial.mem_roots (polynomial.map_ne_zero h1)).mp hx },
let key_equiv : ((↑K⟮x⟯ : intermediate_field F E) →ₐ[F] E) ≃ Σ (f : K →ₐ[F] E),
@alg_hom K K⟮x⟯ E _ _ _ _ (ring_hom.to_algebra f) :=
equiv.trans (alg_equiv.arrow_congr (intermediate_field.lift2_alg_equiv K⟮x⟯) (alg_equiv.refl))
alg_hom_equiv_sigma,
haveI : Π (f : K →ₐ[F] E), fintype (@alg_hom K K⟮x⟯ E _ _ _ _ (ring_hom.to_algebra f)) := λ f, by
{ apply fintype.of_injective (sigma.mk f) (λ _ _ H, eq_of_heq ((sigma.mk.inj H).2)),
exact fintype.of_equiv _ key_equiv },
rw [fintype.card_congr key_equiv, fintype.card_sigma, intermediate_field.adjoin.finrank h],
apply finset.sum_const_nat,
intros f hf,
rw ← @intermediate_field.card_alg_hom_adjoin_integral K _ E _ _ x E _ (ring_hom.to_algebra f) h,
{ apply fintype.card_congr, refl },
{ exact polynomial.separable.of_dvd ((polynomial.separable_map (algebra_map F K)).mpr hp) h2 },
{ refine polynomial.splits_of_splits_of_dvd _ (polynomial.map_ne_zero h1) _ h2,
rw [polynomial.splits_map_iff, ←is_scalar_tower.algebra_map_eq],
exact sp.splits },
end
lemma of_separable_splitting_field [sp : p.is_splitting_field F E] (hp : p.separable) :
is_galois F E :=
begin
haveI hFE : finite_dimensional F E := polynomial.is_splitting_field.finite_dimensional E p,
let s := (p.map (algebra_map F E)).roots.to_finset,
have adjoin_root : intermediate_field.adjoin F ↑s = ⊤,
{ apply intermediate_field.to_subalgebra_injective,
rw [intermediate_field.top_to_subalgebra, ←top_le_iff, ←sp.adjoin_roots],
apply intermediate_field.algebra_adjoin_le_adjoin, },
let P : intermediate_field F E → Prop := λ K, fintype.card (K →ₐ[F] E) = finrank F K,
suffices : P (intermediate_field.adjoin F ↑s),
{ rw adjoin_root at this,
apply of_card_aut_eq_finrank,
rw ← eq.trans this (linear_equiv.finrank_eq intermediate_field.top_equiv.to_linear_equiv),
exact fintype.card_congr (equiv.trans (alg_equiv_equiv_alg_hom F E)
(alg_equiv.arrow_congr intermediate_field.top_equiv.symm alg_equiv.refl)) },
apply intermediate_field.induction_on_adjoin_finset s P,
{ have key := intermediate_field.card_alg_hom_adjoin_integral F
(show is_integral F (0 : E), by exact is_integral_zero),
rw [minpoly.zero, polynomial.nat_degree_X] at key,
specialize key polynomial.separable_X (polynomial.splits_X (algebra_map F E)),
rw [←@subalgebra.finrank_bot F E _ _ _, ←intermediate_field.bot_to_subalgebra] at key,
refine eq.trans _ key,
apply fintype.card_congr,
rw intermediate_field.adjoin_zero },
intros K x hx hK,
simp only [P] at *,
rw [of_separable_splitting_field_aux hp K (multiset.mem_to_finset.mp hx),
hK, finrank_mul_finrank],
exact (linear_equiv.finrank_eq (intermediate_field.lift2_alg_equiv K⟮x⟯).to_linear_equiv).symm,
end
/--Equivalent characterizations of a Galois extension of finite degree-/
theorem tfae [finite_dimensional F E] :
tfae [is_galois F E,
intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E)) = ⊥,
fintype.card (E ≃ₐ[F] E) = finrank F E,
∃ p : polynomial F, p.separable ∧ p.is_splitting_field F E] :=
begin
tfae_have : 1 → 2,
{ exact λ h, order_iso.map_bot (@intermediate_field_equiv_subgroup F _ E _ _ _ h).symm },
tfae_have : 1 → 3,
{ introI _, exact card_aut_eq_finrank F E },
tfae_have : 1 → 4,
{ introI _, exact is_separable_splitting_field F E },
tfae_have : 2 → 1,
{ exact of_fixed_field_eq_bot F E },
tfae_have : 3 → 1,
{ exact of_card_aut_eq_finrank F E },
tfae_have : 4 → 1,
{ rintros ⟨h, hp1, _⟩, exactI of_separable_splitting_field hp1 },
tfae_finish,
end
end is_galois
end galois_equivalent_definitions
|
a7d45fe557123dff654dad18eff26808ed6f3cfc | f3a5af2927397cf346ec0e24312bfff077f00425 | /src/game/world10/level18_old.txt | 32f56434d5498bff66ceff77358862b61f9f82a3 | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/natural_number_game | 05c39e1586408cfb563d1a12e1085a90726ab655 | f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd | refs/heads/master | 1,688,570,964,990 | 1,636,908,242,000 | 1,636,908,242,000 | 195,403,790 | 277 | 84 | Apache-2.0 | 1,694,547,955,000 | 1,562,328,792,000 | Lean | UTF-8 | Lean | false | false | 887 | txt | import game.world10.level17 -- hide
namespace mynat -- hide
/-
# Inequality world.
## Level 18: lt_of_add_lt_add_left
Two collectibles for the price of one: after the below lemma
we can deduce both that the naturals are an ordered commutative
monoid and a canonically-ordered monoid ("canonical" here means
that $a\le b$ if and only if there exists $c$ with $b=a+c$, plus
some other axioms).
-/
/- Lemma :
For all naturals $a$ $b$ and $c$,
$$a+b<a+c\implies b<c.$$
-/
lemma lt_of_add_lt_add_left (a b c : mynat) : a + b < a + c → b < c :=
begin [nat_num_game]
rw lt_iff_succ_le,
rw lt_iff_succ_le,
intro h,
sorry,
-- apply le_of_add_le_add_left, -- wtf? Not there?
end
def bot := 0 -- hide
def bot_le := zero_le -- hide
instance : canonically_ordered_monoid mynat := by structure_helper
instance : ordered_comm_monoid mynat := by structure_helper
end mynat -- hide
|
6e821144cb89915bb0a5b5cc9d555a520a611830 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/finset/basic.lean | a9ae63f87c23f1c7b51c78644f6f0b844c2e38b1 | [
"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 | 132,028 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro
-/
import data.int.order.basic
import data.multiset.finset_ops
import algebra.hom.embedding
import tactic.apply
import tactic.nth_rewrite
import tactic.monotonicity
/-!
# Finite sets
Terms of type `finset α` are one way of talking about finite subsets of `α` in mathlib.
Below, `finset α` is defined as a structure with 2 fields:
1. `val` is a `multiset α` of elements;
2. `nodup` is a proof that `val` has no duplicates.
Finsets in Lean are constructive in that they have an underlying `list` that enumerates their
elements. In particular, any function that uses the data of the underlying list cannot depend on its
ordering. This is handled on the `multiset` level by multiset API, so in most cases one needn't
worry about it explicitly.
Finsets give a basic foundation for defining finite sums and products over types:
1. `∑ i in (s : finset α), f i`;
2. `∏ i in (s : finset α), f i`.
Lean refers to these operations as `big_operator`s.
More information can be found in `algebra.big_operators.basic`.
Finsets are directly used to define fintypes in Lean.
A `fintype α` instance for a type `α` consists of
a universal `finset α` containing every term of `α`, called `univ`. See `data.fintype.basic`.
There is also `univ'`, the noncomputable partner to `univ`,
which is defined to be `α` as a finset if `α` is finite,
and the empty finset otherwise. See `data.fintype.basic`.
`finset.card`, the size of a finset is defined in `data.finset.card`. This is then used to define
`fintype.card`, the size of a type.
## Main declarations
### Main definitions
* `finset`: Defines a type for the finite subsets of `α`.
Constructing a `finset` requires two pieces of data: `val`, a `multiset α` of elements,
and `nodup`, a proof that `val` has no duplicates.
* `finset.has_mem`: Defines membership `a ∈ (s : finset α)`.
* `finset.has_coe`: Provides a coercion `s : finset α` to `s : set α`.
* `finset.has_coe_to_sort`: Coerce `s : finset α` to the type of all `x ∈ s`.
* `finset.induction_on`: Induction on finsets. To prove a proposition about an arbitrary `finset α`,
it suffices to prove it for the empty finset, and to show that if it holds for some `finset α`,
then it holds for the finset obtained by inserting a new element.
* `finset.choose`: Given a proof `h` of existence and uniqueness of a certain element
satisfying a predicate, `choose s h` returns the element of `s` satisfying that predicate.
### Finset constructions
* `singleton`: Denoted by `{a}`; the finset consisting of one element.
* `finset.empty`: Denoted by `∅`. The finset associated to any type consisting of no elements.
* `finset.range`: For any `n : ℕ`, `range n` is equal to `{0, 1, ... , n - 1} ⊆ ℕ`.
This convention is consistent with other languages and normalizes `card (range n) = n`.
Beware, `n` is not in `range n`.
* `finset.attach`: Given `s : finset α`, `attach s` forms a finset of elements of the subtype
`{a // a ∈ s}`; in other words, it attaches elements to a proof of membership in the set.
### Finsets from functions
* `finset.image`: Given a function `f : α → β`, `s.image f` is the image finset in `β`.
* `finset.map`: Given an embedding `f : α ↪ β`, `s.map f` is the image finset in `β`.
* `finset.filter`: Given a predicate `p : α → Prop`, `s.filter p` is
the finset consisting of those elements in `s` satisfying the predicate `p`.
### The lattice structure on subsets of finsets
There is a natural lattice structure on the subsets of a set.
In Lean, we use lattice notation to talk about things involving unions and intersections. See
`order.lattice`. For the lattice structure on finsets, `⊥` is called `bot` with `⊥ = ∅` and `⊤` is
called `top` with `⊤ = univ`.
* `finset.has_subset`: Lots of API about lattices, otherwise behaves exactly as one would expect.
* `finset.has_union`: Defines `s ∪ t` (or `s ⊔ t`) as the union of `s` and `t`.
See `finset.sup`/`finset.bUnion` for finite unions.
* `finset.has_inter`: Defines `s ∩ t` (or `s ⊓ t`) as the intersection of `s` and `t`.
See `finset.inf` for finite intersections.
* `finset.disj_union`: Given a hypothesis `h` which states that finsets `s` and `t` are disjoint,
`s.disj_union t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`; this does
not require decidable equality on the type `α`.
### Operations on two or more finsets
* `insert` and `finset.cons`: For any `a : α`, `insert s a` returns `s ∪ {a}`. `cons s a h`
returns the same except that it requires a hypothesis stating that `a` is not already in `s`.
This does not require decidable equality on the type `α`.
* `finset.has_union`: see "The lattice structure on subsets of finsets"
* `finset.has_inter`: see "The lattice structure on subsets of finsets"
* `finset.erase`: For any `a : α`, `erase s a` returns `s` with the element `a` removed.
* `finset.has_sdiff`: Defines the set difference `s \ t` for finsets `s` and `t`.
* `finset.product`: Given finsets of `α` and `β`, defines finsets of `α × β`.
For arbitrary dependent products, see `data.finset.pi`.
* `finset.bUnion`: Finite unions of finsets; given an indexing function `f : α → finset β` and a
`s : finset α`, `s.bUnion f` is the union of all finsets of the form `f a` for `a ∈ s`.
* `finset.bInter`: TODO: Implemement finite intersections.
### Maps constructed using finsets
* `finset.piecewise`: Given two functions `f`, `g`, `s.piecewise f g` is a function which is equal
to `f` on `s` and `g` on the complement.
### Predicates on finsets
* `disjoint`: defined via the lattice structure on finsets; two sets are disjoint if their
intersection is empty.
* `finset.nonempty`: A finset is nonempty if it has elements.
This is equivalent to saying `s ≠ ∅`. TODO: Decide on the simp normal form.
### Equivalences between finsets
* The `data.equiv` files describe a general type of equivalence, so look in there for any lemmas.
There is some API for rewriting sums and products from `s` to `t` given that `s ≃ t`.
TODO: examples
## Tags
finite sets, finset
-/
open multiset subtype nat function
universes u
variables {α : Type*} {β : Type*} {γ : Type*}
/-- `finset α` is the type of finite sets of elements of `α`. It is implemented
as a multiset (a list up to permutation) which has no duplicate elements. -/
structure finset (α : Type*) :=
(val : multiset α)
(nodup : nodup val)
namespace finset
theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t
| ⟨s, _⟩ ⟨t, _⟩ rfl := rfl
theorem val_injective : injective (val : finset α → multiset α) := λ _ _, eq_of_veq
@[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := val_injective.eq_iff
@[simp] theorem dedup_eq_self [decidable_eq α] (s : finset α) : dedup s.1 = s.1 :=
s.2.dedup
instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α)
| s₁ s₂ := decidable_of_iff _ val_inj
/-! ### membership -/
instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩
theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl
@[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl
instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) :=
multiset.decidable_mem _ _
/-! ### set coercion -/
/-- Convert a finset to a set in the natural way. -/
instance : has_coe_t (finset α) (set α) := ⟨λ s, {x | x ∈ s}⟩
@[simp, norm_cast] lemma mem_coe {a : α} {s : finset α} : a ∈ (s : set α) ↔ a ∈ s := iff.rfl
@[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = s := rfl
@[simp] lemma coe_mem {s : finset α} (x : (s : set α)) : ↑x ∈ s := x.2
@[simp] lemma mk_coe {s : finset α} (x : (s : set α)) {h} :
(⟨x, h⟩ : (s : set α)) = x :=
subtype.coe_eta _ _
instance decidable_mem' [decidable_eq α] (a : α) (s : finset α) :
decidable (a ∈ (s : set α)) := s.decidable_mem _
/-! ### extensionality -/
theorem ext_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ :=
val_inj.symm.trans $ s₁.nodup.ext s₂.nodup
@[ext]
theorem ext {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ :=
ext_iff.2
@[simp, norm_cast] theorem coe_inj {s₁ s₂ : finset α} : (s₁ : set α) = s₂ ↔ s₁ = s₂ :=
set.ext_iff.trans ext_iff.symm
lemma coe_injective {α} : injective (coe : finset α → set α) :=
λ s t, coe_inj.1
/-! ### type coercion -/
/-- Coercion from a finset to the corresponding subtype. -/
instance {α : Type u} : has_coe_to_sort (finset α) (Type u) := ⟨λ s, {x // x ∈ s}⟩
@[simp] protected lemma forall_coe {α : Type*} (s : finset α) (p : s → Prop) :
(∀ (x : s), p x) ↔ ∀ (x : α) (h : x ∈ s), p ⟨x, h⟩ := subtype.forall
@[simp] protected lemma exists_coe {α : Type*} (s : finset α) (p : s → Prop) :
(∃ (x : s), p x) ↔ ∃ (x : α) (h : x ∈ s), p ⟨x, h⟩ := subtype.exists
instance pi_finset_coe.can_lift (ι : Type*) (α : Π i : ι, Type*) [ne : Π i, nonempty (α i)]
(s : finset ι) :
can_lift (Π i : s, α i) (Π i, α i) (λ f i, f i) (λ _, true) :=
pi_subtype.can_lift ι α (∈ s)
instance pi_finset_coe.can_lift' (ι α : Type*) [ne : nonempty α] (s : finset ι) :
can_lift (s → α) (ι → α) (λ f i, f i) (λ _, true) :=
pi_finset_coe.can_lift ι (λ _, α) s
instance finset_coe.can_lift (s : finset α) : can_lift α s coe (λ a, a ∈ s) :=
{ prf := λ a ha, ⟨⟨a, ha⟩, rfl⟩ }
@[simp, norm_cast] lemma coe_sort_coe (s : finset α) :
((s : set α) : Sort*) = s := rfl
/-! ### Subset and strict subset relations -/
section subset
variables {s t : finset α}
instance : has_subset (finset α) := ⟨λ s t, ∀ ⦃a⦄, a ∈ s → a ∈ t⟩
instance : has_ssubset (finset α) := ⟨λ s t, s ⊆ t ∧ ¬ t ⊆ s⟩
instance : partial_order (finset α) :=
{ le := (⊆),
lt := (⊂),
le_refl := λ s a, id,
le_trans := λ s t u hst htu a ha, htu $ hst ha,
le_antisymm := λ s t hst hts, ext $ λ a, ⟨@hst _, @hts _⟩ }
instance : is_refl (finset α) (⊆) := has_le.le.is_refl
instance : is_trans (finset α) (⊆) := has_le.le.is_trans
instance : is_antisymm (finset α) (⊆) := has_le.le.is_antisymm
instance : is_irrefl (finset α) (⊂) := has_lt.lt.is_irrefl
instance : is_trans (finset α) (⊂) := has_lt.lt.is_trans
instance : is_asymm (finset α) (⊂) := has_lt.lt.is_asymm
instance : is_nonstrict_strict_order (finset α) (⊆) (⊂) := ⟨λ _ _, iff.rfl⟩
lemma subset_def : s ⊆ t ↔ s.1 ⊆ t.1 := iff.rfl
lemma ssubset_def : s ⊂ t ↔ s ⊆ t ∧ ¬ t ⊆ s := iff.rfl
@[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _
protected lemma subset.rfl {s :finset α} : s ⊆ s := subset.refl _
protected theorem subset_of_eq {s t : finset α} (h : s = t) : s ⊆ t := h ▸ subset.refl _
theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans
theorem superset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊇ s₂ → s₂ ⊇ s₃ → s₁ ⊇ s₃ :=
λ h' h, subset.trans h h'
theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset
lemma not_mem_mono {s t : finset α} (h : s ⊆ t) {a : α} : a ∉ t → a ∉ s := mt $ @h _
theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ :=
ext $ λ a, ⟨@H₁ a, @H₂ a⟩
theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl
@[simp, norm_cast] theorem coe_subset {s₁ s₂ : finset α} :
(s₁ : set α) ⊆ s₂ ↔ s₁ ⊆ s₂ := iff.rfl
@[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2
theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ :=
le_antisymm_iff
theorem not_subset (s t : finset α) : ¬(s ⊆ t) ↔ ∃ x ∈ s, ¬(x ∈ t) :=
by simp only [←finset.coe_subset, set.not_subset, exists_prop, finset.mem_coe]
@[simp] theorem le_eq_subset : ((≤) : finset α → finset α → Prop) = (⊆) := rfl
@[simp] theorem lt_eq_subset : ((<) : finset α → finset α → Prop) = (⊂) := rfl
theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl
theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl
@[simp, norm_cast] lemma coe_ssubset {s₁ s₂ : finset α} : (s₁ : set α) ⊂ s₂ ↔ s₁ ⊂ s₂ :=
show (s₁ : set α) ⊂ s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁,
by simp only [set.ssubset_def, finset.coe_subset]
@[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ :=
and_congr val_le_iff $ not_congr val_le_iff
lemma ssubset_iff_subset_ne {s t : finset α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t :=
@lt_iff_le_and_ne _ _ s t
theorem ssubset_iff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ⊂ s₂ ↔ ∃ x ∈ s₂, x ∉ s₁ :=
set.ssubset_iff_of_subset h
lemma ssubset_of_ssubset_of_subset {s₁ s₂ s₃ : finset α} (hs₁s₂ : s₁ ⊂ s₂) (hs₂s₃ : s₂ ⊆ s₃) :
s₁ ⊂ s₃ :=
set.ssubset_of_ssubset_of_subset hs₁s₂ hs₂s₃
lemma ssubset_of_subset_of_ssubset {s₁ s₂ s₃ : finset α} (hs₁s₂ : s₁ ⊆ s₂) (hs₂s₃ : s₂ ⊂ s₃) :
s₁ ⊂ s₃ :=
set.ssubset_of_subset_of_ssubset hs₁s₂ hs₂s₃
lemma exists_of_ssubset {s₁ s₂ : finset α} (h : s₁ ⊂ s₂) :
∃ x ∈ s₂, x ∉ s₁ :=
set.exists_of_ssubset h
end subset
-- TODO: these should be global attributes, but this will require fixing other files
local attribute [trans] subset.trans superset.trans
/-! ### Order embedding from `finset α` to `set α` -/
/-- Coercion to `set α` as an `order_embedding`. -/
def coe_emb : finset α ↪o set α := ⟨⟨coe, coe_injective⟩, λ s t, coe_subset⟩
@[simp] lemma coe_coe_emb : ⇑(coe_emb : finset α ↪o set α) = coe := rfl
/-! ### Nonempty -/
/-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used
in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks
to the dot notation. -/
protected def nonempty (s : finset α) : Prop := ∃ x : α, x ∈ s
instance decidable_nonempty {s : finset α} : decidable s.nonempty :=
decidable_of_iff (∃ a ∈ s, true) $ by simp_rw [exists_prop, and_true, finset.nonempty]
@[simp, norm_cast] lemma coe_nonempty {s : finset α} : (s : set α).nonempty ↔ s.nonempty := iff.rfl
@[simp] lemma nonempty_coe_sort {s : finset α} : nonempty ↥s ↔ s.nonempty := nonempty_subtype
alias coe_nonempty ↔ _ nonempty.to_set
alias nonempty_coe_sort ↔ _ nonempty.coe_sort
lemma nonempty.bex {s : finset α} (h : s.nonempty) : ∃ x : α, x ∈ s := h
lemma nonempty.mono {s t : finset α} (hst : s ⊆ t) (hs : s.nonempty) : t.nonempty :=
set.nonempty.mono hst hs
lemma nonempty.forall_const {s : finset α} (h : s.nonempty) {p : Prop} : (∀ x ∈ s, p) ↔ p :=
let ⟨x, hx⟩ := h in ⟨λ h, h x hx, λ h x hx, h⟩
/-! ### empty -/
/-- The empty finset -/
protected def empty : finset α := ⟨0, nodup_zero⟩
instance : has_emptyc (finset α) := ⟨finset.empty⟩
instance inhabited_finset : inhabited (finset α) := ⟨∅⟩
@[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl
@[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id
@[simp] theorem not_nonempty_empty : ¬(∅ : finset α).nonempty :=
λ ⟨x, hx⟩, not_mem_empty x hx
@[simp] theorem mk_zero : (⟨0, nodup_zero⟩ : finset α) = ∅ := rfl
theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ :=
λ e, not_mem_empty a $ e ▸ h
theorem nonempty.ne_empty {s : finset α} (h : s.nonempty) : s ≠ ∅ :=
exists.elim h $ λ a, ne_empty_of_mem
@[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _
lemma eq_empty_of_forall_not_mem {s : finset α} (H : ∀ x, x ∉ s) : s = ∅ :=
eq_of_veq (eq_zero_of_forall_not_mem H)
lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s :=
⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩
@[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅
theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero
@[simp] lemma not_ssubset_empty (s : finset α) : ¬s ⊂ ∅ :=
λ h, let ⟨x, he, hs⟩ := exists_of_ssubset h in he
theorem nonempty_of_ne_empty {s : finset α} (h : s ≠ ∅) : s.nonempty :=
exists_mem_of_ne_zero (mt val_eq_zero.1 h)
theorem nonempty_iff_ne_empty {s : finset α} : s.nonempty ↔ s ≠ ∅ :=
⟨nonempty.ne_empty, nonempty_of_ne_empty⟩
@[simp] theorem not_nonempty_iff_eq_empty {s : finset α} : ¬s.nonempty ↔ s = ∅ :=
nonempty_iff_ne_empty.not.trans not_not
theorem eq_empty_or_nonempty (s : finset α) : s = ∅ ∨ s.nonempty :=
classical.by_cases or.inl (λ h, or.inr (nonempty_of_ne_empty h))
@[simp, norm_cast] lemma coe_empty : ((∅ : finset α) : set α) = ∅ := rfl
@[simp, norm_cast] lemma coe_eq_empty {s : finset α} : (s : set α) = ∅ ↔ s = ∅ :=
by rw [← coe_empty, coe_inj]
@[simp] lemma is_empty_coe_sort {s : finset α} : is_empty ↥s ↔ s = ∅ :=
by simpa using @set.is_empty_coe_sort α s
instance : is_empty (∅ : finset α) := is_empty_coe_sort.2 rfl
/-- A `finset` for an empty type is empty. -/
lemma eq_empty_of_is_empty [is_empty α] (s : finset α) : s = ∅ :=
finset.eq_empty_of_forall_not_mem is_empty_elim
instance : order_bot (finset α) :=
{ bot := ∅, bot_le := empty_subset }
@[simp] lemma bot_eq_empty : (⊥ : finset α) = ∅ := rfl
/-! ### singleton -/
/--
`{a} : finset a` is the set `{a}` containing `a` and nothing else.
This differs from `insert a ∅` in that it does not require a `decidable_eq` instance for `α`.
-/
instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩
@[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = {a} := rfl
@[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton
lemma eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : finset α)) : x = y := mem_singleton.1 h
theorem not_mem_singleton {a b : α} : a ∉ ({b} : finset α) ↔ a ≠ b := not_congr mem_singleton
theorem mem_singleton_self (a : α) : a ∈ ({a} : finset α) := or.inl rfl
lemma singleton_injective : injective (singleton : α → finset α) :=
λ a b h, mem_singleton.1 (h ▸ mem_singleton_self _)
theorem singleton_inj {a b : α} : ({a} : finset α) = {b} ↔ a = b :=
singleton_injective.eq_iff
@[simp] theorem singleton_nonempty (a : α) : ({a} : finset α).nonempty := ⟨a, mem_singleton_self a⟩
@[simp] theorem singleton_ne_empty (a : α) : ({a} : finset α) ≠ ∅ := (singleton_nonempty a).ne_empty
@[simp, norm_cast] lemma coe_singleton (a : α) : (({a} : finset α) : set α) = {a} :=
by { ext, simp }
@[simp, norm_cast] lemma coe_eq_singleton {s : finset α} {a : α} : (s : set α) = {a} ↔ s = {a} :=
by rw [←coe_singleton, coe_inj]
lemma eq_singleton_iff_unique_mem {s : finset α} {a : α} :
s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a :=
begin
split; intro t,
rw t,
refine ⟨finset.mem_singleton_self _, λ _, finset.mem_singleton.1⟩,
ext, rw finset.mem_singleton,
refine ⟨t.right _, λ r, r.symm ▸ t.left⟩
end
lemma eq_singleton_iff_nonempty_unique_mem {s : finset α} {a : α} :
s = {a} ↔ s.nonempty ∧ ∀ x ∈ s, x = a :=
begin
split,
{ rintro rfl, simp },
{ rintros ⟨hne, h_uniq⟩, rw eq_singleton_iff_unique_mem, refine ⟨_, h_uniq⟩,
rw ← h_uniq hne.some hne.some_spec, exact hne.some_spec }
end
lemma nonempty_iff_eq_singleton_default [unique α] {s : finset α} :
s.nonempty ↔ s = {default} :=
by simp [eq_singleton_iff_nonempty_unique_mem]
alias nonempty_iff_eq_singleton_default ↔ nonempty.eq_singleton_default _
lemma singleton_iff_unique_mem (s : finset α) : (∃ a, s = {a}) ↔ ∃! a, a ∈ s :=
by simp only [eq_singleton_iff_unique_mem, exists_unique]
lemma singleton_subset_set_iff {s : set α} {a : α} : ↑({a} : finset α) ⊆ s ↔ a ∈ s :=
by rw [coe_singleton, set.singleton_subset_iff]
@[simp] lemma singleton_subset_iff {s : finset α} {a : α} : {a} ⊆ s ↔ a ∈ s :=
singleton_subset_set_iff
@[simp] lemma subset_singleton_iff {s : finset α} {a : α} : s ⊆ {a} ↔ s = ∅ ∨ s = {a} :=
by rw [←coe_subset, coe_singleton, set.subset_singleton_iff_eq, coe_eq_empty, coe_eq_singleton]
protected lemma nonempty.subset_singleton_iff {s : finset α} {a : α} (h : s.nonempty) :
s ⊆ {a} ↔ s = {a} :=
subset_singleton_iff.trans $ or_iff_right h.ne_empty
lemma subset_singleton_iff' {s : finset α} {a : α} : s ⊆ {a} ↔ ∀ b ∈ s, b = a :=
forall₂_congr $ λ _ _, mem_singleton
@[simp] lemma ssubset_singleton_iff {s : finset α} {a : α} :
s ⊂ {a} ↔ s = ∅ :=
by rw [←coe_ssubset, coe_singleton, set.ssubset_singleton_iff, coe_eq_empty]
lemma eq_empty_of_ssubset_singleton {s : finset α} {x : α} (hs : s ⊂ {x}) : s = ∅ :=
ssubset_singleton_iff.1 hs
instance [nonempty α] : nontrivial (finset α) :=
‹nonempty α›.elim $ λ a, ⟨⟨{a}, ∅, singleton_ne_empty _⟩⟩
instance [is_empty α] : unique (finset α) :=
{ default := ∅,
uniq := λ s, eq_empty_of_forall_not_mem is_empty_elim }
/-! ### cons -/
section cons
variables {s t : finset α} {a b : α}
/-- `cons a s h` is the set `{a} ∪ s` containing `a` and the elements of `s`. It is the same as
`insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq α`,
and the union is guaranteed to be disjoint. -/
def cons (a : α) (s : finset α) (h : a ∉ s) : finset α := ⟨a ::ₘ s.1, nodup_cons.2 ⟨h, s.2⟩⟩
@[simp] lemma mem_cons {h} : b ∈ s.cons a h ↔ b = a ∨ b ∈ s := mem_cons
@[simp] lemma mem_cons_self (a : α) (s : finset α) {h} : a ∈ cons a s h := mem_cons_self _ _
@[simp] lemma cons_val (h : a ∉ s) : (cons a s h).1 = a ::ₘ s.1 := rfl
lemma forall_mem_cons (h : a ∉ s) (p : α → Prop) :
(∀ x, x ∈ cons a s h → p x) ↔ p a ∧ ∀ x, x ∈ s → p x :=
by simp only [mem_cons, or_imp_distrib, forall_and_distrib, forall_eq]
@[simp] lemma mk_cons {s : multiset α} (h : (a ::ₘ s).nodup) :
(⟨a ::ₘ s, h⟩ : finset α) = cons a ⟨s, (nodup_cons.1 h).2⟩ (nodup_cons.1 h).1 := rfl
@[simp] lemma nonempty_cons (h : a ∉ s) : (cons a s h).nonempty := ⟨a, mem_cons.2 $ or.inl rfl⟩
@[simp] lemma nonempty_mk {m : multiset α} {hm} : (⟨m, hm⟩ : finset α).nonempty ↔ m ≠ 0 :=
by induction m using multiset.induction_on; simp
@[simp] lemma coe_cons {a s h} : (@cons α a s h : set α) = insert a s := by { ext, simp }
lemma subset_cons (h : a ∉ s) : s ⊆ s.cons a h := subset_cons _ _
lemma ssubset_cons (h : a ∉ s) : s ⊂ s.cons a h := ssubset_cons h
lemma cons_subset {h : a ∉ s} : s.cons a h ⊆ t ↔ a ∈ t ∧ s ⊆ t := cons_subset
@[simp] lemma cons_subset_cons {hs ht} : s.cons a hs ⊆ t.cons a ht ↔ s ⊆ t :=
by rwa [← coe_subset, coe_cons, coe_cons, set.insert_subset_insert_iff, coe_subset]
lemma ssubset_iff_exists_cons_subset : s ⊂ t ↔ ∃ a (h : a ∉ s), s.cons a h ⊆ t :=
begin
refine ⟨λ h, _, λ ⟨a, ha, h⟩, ssubset_of_ssubset_of_subset (ssubset_cons _) h⟩,
obtain ⟨a, hs, ht⟩ := (not_subset _ _).1 h.2,
exact ⟨a, ht, cons_subset.2 ⟨hs, h.subset⟩⟩,
end
end cons
/-! ### disjoint -/
section disjoint
variables {f : α → β} {s t u : finset α} {a b : α}
lemma disjoint_left : disjoint s t ↔ ∀ ⦃a⦄, a ∈ s → a ∉ t :=
⟨λ h a hs ht,
singleton_subset_iff.mp (h (singleton_subset_iff.mpr hs) (singleton_subset_iff.mpr ht)),
λ h x hs ht a ha, h (hs ha) (ht ha)⟩
lemma disjoint_val : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left
lemma disjoint_right : disjoint s t ↔ ∀ ⦃a⦄, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left]
lemma disjoint_iff_ne : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b :=
by simp only [disjoint_left, imp_not_comm, forall_eq']
lemma _root_.disjoint.forall_ne_finset (h : disjoint s t) (ha : a ∈ s) (hb : b ∈ t) : a ≠ b :=
disjoint_iff_ne.1 h _ ha _ hb
lemma not_disjoint_iff : ¬ disjoint s t ↔ ∃ a, a ∈ s ∧ a ∈ t :=
disjoint_left.not.trans $ not_forall.trans $ exists_congr $ λ _, by rw [not_imp, not_not]
lemma disjoint_of_subset_left (h : s ⊆ u) (d : disjoint u t) : disjoint s t :=
disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁))
lemma disjoint_of_subset_right (h : t ⊆ u) (d : disjoint s u) : disjoint s t :=
disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁))
@[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left
@[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right
@[simp] lemma disjoint_singleton_left : disjoint (singleton a) s ↔ a ∉ s :=
by simp only [disjoint_left, mem_singleton, forall_eq]
@[simp] lemma disjoint_singleton_right : disjoint s (singleton a) ↔ a ∉ s :=
disjoint.comm.trans disjoint_singleton_left
@[simp] lemma disjoint_singleton : disjoint ({a} : finset α) {b} ↔ a ≠ b :=
by rw [disjoint_singleton_left, mem_singleton]
lemma disjoint_self_iff_empty (s : finset α) : disjoint s s ↔ s = ∅ := disjoint_self
@[simp, norm_cast] lemma disjoint_coe : disjoint (s : set α) t ↔ disjoint s t :=
by { rw [finset.disjoint_left, set.disjoint_left], refl }
@[simp, norm_cast] lemma pairwise_disjoint_coe {ι : Type*} {s : set ι} {f : ι → finset α} :
s.pairwise_disjoint (λ i, f i : ι → set α) ↔ s.pairwise_disjoint f :=
forall₅_congr $ λ _ _ _ _ _, disjoint_coe
end disjoint
/-! ### disjoint union -/
/-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`.
It is the same as `s ∪ t`, but it does not require decidable equality on the type. The hypothesis
ensures that the sets are disjoint. -/
def disj_union (s t : finset α) (h : disjoint s t) : finset α :=
⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, disjoint_val.1 h⟩⟩
@[simp] theorem mem_disj_union {α s t h a} :
a ∈ @disj_union α s t h ↔ a ∈ s ∨ a ∈ t :=
by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append
lemma disj_union_comm (s t : finset α) (h : disjoint s t) :
disj_union s t h = disj_union t s h.symm :=
eq_of_veq $ add_comm _ _
@[simp] lemma empty_disj_union (t : finset α) (h : disjoint ∅ t := disjoint_bot_left) :
disj_union ∅ t h = t :=
eq_of_veq $ zero_add _
@[simp] lemma disj_union_empty (s : finset α) (h : disjoint s ∅ := disjoint_bot_right) :
disj_union s ∅ h = s :=
eq_of_veq $ add_zero _
lemma singleton_disj_union (a : α) (t : finset α) (h : disjoint {a} t) :
disj_union {a} t h = cons a t (disjoint_singleton_left.mp h) :=
eq_of_veq $ multiset.singleton_add _ _
lemma disj_union_singleton (s : finset α) (a : α) (h : disjoint s {a}) :
disj_union s {a} h = cons a s (disjoint_singleton_right.mp h) :=
by rw [disj_union_comm, singleton_disj_union]
/-! ### insert -/
section insert
variables [decidable_eq α] {s t u v : finset α} {a b : α}
/-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/
instance : has_insert α (finset α) := ⟨λ a s, ⟨_, s.2.ndinsert a⟩⟩
lemma insert_def (a : α) (s : finset α) : insert a s = ⟨_, s.2.ndinsert a⟩ := rfl
@[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl
theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = dedup (a ::ₘ s.1) :=
by rw [dedup_cons, dedup_eq_self]; refl
theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a ::ₘ s.1 :=
by rw [insert_val, ndinsert_of_not_mem h]
@[simp] lemma mem_insert : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert
theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1
lemma mem_insert_of_mem (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h
lemma mem_of_mem_insert_of_ne (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left
lemma eq_of_not_mem_of_mem_insert (ha : b ∈ insert a s) (hb : b ∉ s) : b = a :=
(mem_insert.1 ha).resolve_right hb
@[simp] theorem cons_eq_insert (a s h) : @cons α a s h = insert a s := ext $ λ a, by simp
@[simp, norm_cast] lemma coe_insert (a : α) (s : finset α) :
↑(insert a s) = (insert a s : set α) :=
set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff]
lemma mem_insert_coe {s : finset α} {x y : α} : x ∈ insert y s ↔ x ∈ insert y (s : set α) :=
by simp
instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩
@[simp] lemma insert_eq_of_mem (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h
@[simp] lemma insert_eq_self : insert a s = s ↔ a ∈ s :=
⟨λ h, h ▸ mem_insert_self _ _, insert_eq_of_mem⟩
lemma insert_ne_self : insert a s ≠ s ↔ a ∉ s := insert_eq_self.not
@[simp] theorem pair_eq_singleton (a : α) : ({a, a} : finset α) = {a} :=
insert_eq_of_mem $ mem_singleton_self _
theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) :=
ext $ λ x, by simp only [mem_insert, or.left_comm]
@[simp, norm_cast] lemma coe_pair {a b : α} :
(({a, b} : finset α) : set α) = {a, b} := by { ext, simp }
@[simp, norm_cast] lemma coe_eq_pair {s : finset α} {a b : α} :
(s : set α) = {a, b} ↔ s = {a, b} := by rw [←coe_pair, coe_inj]
theorem pair_comm (a b : α) : ({a, b} : finset α) = {b, a} := insert.comm a b ∅
@[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s :=
ext $ λ x, by simp only [mem_insert, or.assoc.symm, or_self]
@[simp] theorem insert_nonempty (a : α) (s : finset α) : (insert a s).nonempty :=
⟨a, mem_insert_self a s⟩
@[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ :=
(insert_nonempty a s).ne_empty
/-!
The universe annotation is required for the following instance, possibly this is a bug in Lean. See
leanprover.zulipchat.com/#narrow/stream/113488-general/topic/strange.20error.20(universe.20issue.3F)
-/
instance {α : Type u} [decidable_eq α] (i : α) (s : finset α) :
nonempty.{u + 1} ((insert i s : finset α) : set α) :=
(finset.coe_nonempty.mpr (s.insert_nonempty i)).to_subtype
lemma ne_insert_of_not_mem (s t : finset α) {a : α} (h : a ∉ s) : s ≠ insert a t :=
by { contrapose! h, simp [h] }
lemma insert_subset : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t :=
by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib]
lemma subset_insert (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem
theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t :=
insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩
lemma insert_inj (ha : a ∉ s) : insert a s = insert b s ↔ a = b :=
⟨λ h, eq_of_not_mem_of_mem_insert (h.subst $ mem_insert_self _ _) ha, congr_arg _⟩
lemma insert_inj_on (s : finset α) : set.inj_on (λ a, insert a s) sᶜ := λ a h b _, (insert_inj h).1
lemma ssubset_iff : s ⊂ t ↔ ∃ a ∉ s, insert a s ⊆ t :=
by exact_mod_cast @set.ssubset_iff_insert α s t
lemma ssubset_insert (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.rfl⟩
@[elab_as_eliminator]
lemma cons_induction {α : Type*} {p : finset α → Prop}
(h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α} (h : a ∉ s), p s → p (cons a s h)) : ∀ s, p s
| ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin
cases nodup_cons.1 nd with m nd',
rw [← (eq_of_veq _ : cons a (finset.mk s _) m = ⟨a ::ₘ s, nd⟩)],
{ exact h₂ (by exact m) (IH nd') },
{ rw [cons_val] }
end) nd
@[elab_as_eliminator]
lemma cons_induction_on {α : Type*} {p : finset α → Prop} (s : finset α)
(h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α} (h : a ∉ s), p s → p (cons a s h)) : p s :=
cons_induction h₁ h₂ s
@[elab_as_eliminator]
protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α]
(h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s :=
cons_induction h₁ $ λ a s ha, (s.cons_eq_insert a ha).symm ▸ h₂ ha
/--
To prove a proposition about an arbitrary `finset α`,
it suffices to prove it for the empty `finset`,
and to show that if it holds for some `finset α`,
then it holds for the `finset` obtained by inserting a new element.
-/
@[elab_as_eliminator]
protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α]
(s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s :=
finset.induction h₁ h₂ s
/--
To prove a proposition about `S : finset α`,
it suffices to prove it for the empty `finset`,
and to show that if it holds for some `finset α ⊆ S`,
then it holds for the `finset` obtained by inserting a new element of `S`.
-/
@[elab_as_eliminator]
theorem induction_on' {α : Type*} {p : finset α → Prop} [decidable_eq α]
(S : finset α) (h₁ : p ∅) (h₂ : ∀ {a s}, a ∈ S → s ⊆ S → a ∉ s → p s → p (insert a s)) : p S :=
@finset.induction_on α (λ T, T ⊆ S → p T) _ S (λ _, h₁) (λ a s has hqs hs,
let ⟨hS, sS⟩ := finset.insert_subset.1 hs in h₂ hS sS has (hqs sS)) (finset.subset.refl S)
/-- To prove a proposition about a nonempty `s : finset α`, it suffices to show it holds for all
singletons and that if it holds for nonempty `t : finset α`, then it also holds for the `finset`
obtained by inserting an element in `t`. -/
@[elab_as_eliminator]
lemma nonempty.cons_induction {α : Type*} {p : Π s : finset α, s.nonempty → Prop}
(h₀ : ∀ a, p {a} (singleton_nonempty _))
(h₁ : ∀ ⦃a⦄ s (h : a ∉ s) hs, p s hs → p (finset.cons a s h) (nonempty_cons h))
{s : finset α} (hs : s.nonempty) : p s hs :=
begin
induction s using finset.cons_induction with a t ha h,
{ exact (not_nonempty_empty hs).elim },
obtain rfl | ht := t.eq_empty_or_nonempty,
{ exact h₀ a },
{ exact h₁ t ha ht (h ht) }
end
/-- Inserting an element to a finite set is equivalent to the option type. -/
def subtype_insert_equiv_option {t : finset α} {x : α} (h : x ∉ t) :
{i // i ∈ insert x t} ≃ option {i // i ∈ t} :=
begin
refine
{ to_fun := λ y, if h : ↑y = x then none else some ⟨y, (mem_insert.mp y.2).resolve_left h⟩,
inv_fun := λ y, y.elim ⟨x, mem_insert_self _ _⟩ $ λ z, ⟨z, mem_insert_of_mem z.2⟩,
.. },
{ intro y, by_cases h : ↑y = x,
simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk],
simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] },
{ rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk],
have : ↑y ≠ x, { rintro ⟨⟩, exact h y.2 },
simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta,
subtype.coe_mk] },
end
@[simp] lemma disjoint_insert_left : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t :=
by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq]
@[simp] lemma disjoint_insert_right : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t :=
disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm]
end insert
/-! ### Lattice structure -/
section lattice
variables [decidable_eq α] {s t u v : finset α} {a b : α}
/-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/
instance : has_union (finset α) := ⟨λ s t, ⟨_, t.2.ndunion s.1⟩⟩
/-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/
instance : has_inter (finset α) := ⟨λ s t, ⟨_, s.2.ndinter t.1⟩⟩
instance : lattice (finset α) :=
{ sup := (∪),
sup_le := λ s t u hs ht a ha, (mem_ndunion.1 ha).elim (λ h, hs h) (λ h, ht h),
le_sup_left := λ s t a h, mem_ndunion.2 $ or.inl h,
le_sup_right := λ s t a h, mem_ndunion.2 $ or.inr h,
inf := (∩),
le_inf := λ s t u ht hu a h, mem_ndinter.2 ⟨ht h, hu h⟩,
inf_le_left := λ s t a h, (mem_ndinter.1 h).1,
inf_le_right := λ s t a h, (mem_ndinter.1 h).2,
..finset.partial_order }
@[simp] lemma sup_eq_union : ((⊔) : finset α → finset α → finset α) = (∪) := rfl
@[simp] lemma inf_eq_inter : ((⊓) : finset α → finset α → finset α) = (∩) := rfl
lemma disjoint_iff_inter_eq_empty : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff
instance decidable_disjoint (U V : finset α) : decidable (disjoint U V) :=
decidable_of_iff _ disjoint_left.symm
/-! #### union -/
lemma union_val_nd (s t : finset α) : (s ∪ t).1 = ndunion s.1 t.1 := rfl
@[simp] lemma union_val (s t : finset α) : (s ∪ t).1 = s.1 ∪ t.1 := ndunion_eq_union s.2
@[simp] lemma mem_union : a ∈ s ∪ t ↔ a ∈ s ∨ a ∈ t := mem_ndunion
@[simp] lemma disj_union_eq_union (s t h) : @disj_union α s t h = s ∪ t := ext $ λ a, by simp
lemma mem_union_left (t : finset α) (h : a ∈ s) : a ∈ s ∪ t := mem_union.2 $ or.inl h
lemma mem_union_right (s : finset α) (h : a ∈ t) : a ∈ s ∪ t := mem_union.2 $ or.inr h
lemma forall_mem_union {p : α → Prop} : (∀ a ∈ s ∪ t, p a) ↔ (∀ a ∈ s, p a) ∧ ∀ a ∈ t, p a :=
⟨λ h, ⟨λ a, h a ∘ mem_union_left _, λ b, h b ∘ mem_union_right _⟩,
λ h ab hab, (mem_union.mp hab).elim (h.1 _) (h.2 _)⟩
lemma not_mem_union : a ∉ s ∪ t ↔ a ∉ s ∧ a ∉ t := by rw [mem_union, not_or_distrib]
@[simp, norm_cast]
lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (s₁ ∪ s₂ : set α) := set.ext $ λ x, mem_union
lemma union_subset (hs : s ⊆ u) : t ⊆ u → s ∪ t ⊆ u := sup_le $ le_iff_subset.2 hs
theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _
theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _
lemma union_subset_union (hsu : s ⊆ u) (htv : t ⊆ v) : s ∪ t ⊆ u ∪ v :=
sup_le_sup (le_iff_subset.2 hsu) htv
lemma union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := sup_comm
instance : is_commutative (finset α) (∪) := ⟨union_comm⟩
@[simp] lemma union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := sup_assoc
instance : is_associative (finset α) (∪) := ⟨union_assoc⟩
@[simp] lemma union_idempotent (s : finset α) : s ∪ s = s := sup_idem
instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩
lemma union_subset_left (h : s ∪ t ⊆ u) : s ⊆ u := (subset_union_left _ _).trans h
lemma union_subset_right {s t u : finset α} (h : s ∪ t ⊆ u) : t ⊆ u :=
subset.trans (subset_union_right _ _) h
lemma union_left_comm (s t u : finset α) : s ∪ (t ∪ u) = t ∪ (s ∪ u) :=
ext $ λ _, by simp only [mem_union, or.left_comm]
lemma union_right_comm (s t u : finset α) : (s ∪ t) ∪ u = (s ∪ u) ∪ t :=
ext $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ t)]
theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s
@[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s :=
ext $ λ x, mem_union.trans $ or_false _
@[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s :=
ext $ λ x, mem_union.trans $ false_or _
theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl
@[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) :=
by simp only [insert_eq, union_assoc]
@[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) :=
by simp only [insert_eq, union_left_comm]
lemma insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t :=
by simp only [insert_union, union_insert, insert_idem]
@[simp] lemma union_eq_left_iff_subset {s t : finset α} : s ∪ t = s ↔ t ⊆ s := sup_eq_left
@[simp] lemma left_eq_union_iff_subset {s t : finset α} : s = s ∪ t ↔ t ⊆ s :=
by rw [← union_eq_left_iff_subset, eq_comm]
@[simp] lemma union_eq_right_iff_subset {s t : finset α} : s ∪ t = t ↔ s ⊆ t := sup_eq_right
@[simp] lemma right_eq_union_iff_subset {s t : finset α} : s = t ∪ s ↔ t ⊆ s :=
by rw [← union_eq_right_iff_subset, eq_comm]
lemma union_congr_left (ht : t ⊆ s ∪ u) (hu : u ⊆ s ∪ t) : s ∪ t = s ⊔ u := sup_congr_left ht hu
lemma union_congr_right (hs : s ⊆ t ∪ u) (ht : t ⊆ s ∪ u) : s ∪ u = t ∪ u := sup_congr_right hs ht
lemma union_eq_union_iff_left : s ∪ t = s ∪ u ↔ t ⊆ s ∪ u ∧ u ⊆ s ∪ t := sup_eq_sup_iff_left
lemma union_eq_union_iff_right : s ∪ u = t ∪ u ↔ s ⊆ t ∪ u ∧ t ⊆ s ∪ u := sup_eq_sup_iff_right
@[simp] lemma disjoint_union_left : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u :=
by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib]
@[simp] lemma disjoint_union_right : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u :=
by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib]
/--
To prove a relation on pairs of `finset X`, it suffices to show that it is
* symmetric,
* it holds when one of the `finset`s is empty,
* it holds for pairs of singletons,
* if it holds for `[a, c]` and for `[b, c]`, then it holds for `[a ∪ b, c]`.
-/
lemma induction_on_union (P : finset α → finset α → Prop)
(symm : ∀ {a b}, P a b → P b a)
(empty_right : ∀ {a}, P a ∅)
(singletons : ∀ {a b}, P {a} {b})
(union_of : ∀ {a b c}, P a c → P b c → P (a ∪ b) c) :
∀ a b, P a b :=
begin
intros a b,
refine finset.induction_on b empty_right (λ x s xs hi, symm _),
rw finset.insert_eq,
apply union_of _ (symm hi),
refine finset.induction_on a empty_right (λ a t ta hi, symm _),
rw finset.insert_eq,
exact union_of singletons (symm hi),
end
lemma _root_.directed.exists_mem_subset_of_finset_subset_bUnion {α ι : Type*} [hn : nonempty ι]
{f : ι → set α} (h : directed (⊆) f)
{s : finset α} (hs : (s : set α) ⊆ ⋃ i, f i) : ∃ i, (s : set α) ⊆ f i :=
begin
classical,
revert hs,
apply s.induction_on,
{ refine λ _, ⟨hn.some, _⟩,
simp only [coe_empty, set.empty_subset], },
{ intros b t hbt htc hbtc,
obtain ⟨i : ι , hti : (t : set α) ⊆ f i⟩ :=
htc (set.subset.trans (t.subset_insert b) hbtc),
obtain ⟨j, hbj⟩ : ∃ j, b ∈ f j,
by simpa [set.mem_Union₂] using hbtc (t.mem_insert_self b),
rcases h j i with ⟨k, hk, hk'⟩,
use k,
rw [coe_insert, set.insert_subset],
exact ⟨hk hbj, trans hti hk'⟩ }
end
lemma _root_.directed_on.exists_mem_subset_of_finset_subset_bUnion {α ι : Type*}
{f : ι → set α} {c : set ι} (hn : c.nonempty) (hc : directed_on (λ i j, f i ⊆ f j) c)
{s : finset α} (hs : (s : set α) ⊆ ⋃ i ∈ c, f i) : ∃ i ∈ c, (s : set α) ⊆ f i :=
begin
rw set.bUnion_eq_Union at hs,
haveI := hn.coe_sort,
obtain ⟨⟨i, hic⟩, hi⟩ :=
(directed_comp.2 hc.directed_coe).exists_mem_subset_of_finset_subset_bUnion hs,
exact ⟨i, hic, hi⟩
end
/-! #### inter -/
theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl
@[simp] lemma inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2
@[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter
theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) :
a ∈ s₁ := (mem_inter.1 h).1
theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) :
a ∈ s₂ := (mem_inter.1 h).2
theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ :=
and_imp.1 mem_inter.2
theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left
theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right
lemma subset_inter {s₁ s₂ u : finset α} : s₁ ⊆ s₂ → s₁ ⊆ u → s₁ ⊆ s₂ ∩ u :=
by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial
@[simp, norm_cast]
lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (s₁ ∩ s₂ : set α) := set.ext $ λ _, mem_inter
@[simp] theorem union_inter_cancel_left {s t : finset α} : (s ∪ t) ∩ s = s :=
by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left]
@[simp] theorem union_inter_cancel_right {s t : finset α} : (s ∪ t) ∩ t = t :=
by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right]
theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ :=
ext $ λ _, by simp only [mem_inter, and_comm]
@[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) :=
ext $ λ _, by simp only [mem_inter, and_assoc]
theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) :=
ext $ λ _, by simp only [mem_inter, and.left_comm]
theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ :=
ext $ λ _, by simp only [mem_inter, and.right_comm]
@[simp] lemma inter_self (s : finset α) : s ∩ s = s := ext $ λ _, mem_inter.trans $ and_self _
@[simp] lemma inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext $ λ _, mem_inter.trans $ and_false _
@[simp] lemma empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext $ λ _, mem_inter.trans $ false_and _
@[simp] lemma inter_union_self (s t : finset α) : s ∩ (t ∪ s) = s :=
by rw [inter_comm, union_inter_cancel_right]
@[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) :
insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) :=
ext $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h,
by simp only [mem_inter, mem_insert, or_and_distrib_left, this]
@[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) :
s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) :=
by rw [inter_comm, insert_inter_of_mem h, inter_comm]
@[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) :
insert a s₁ ∩ s₂ = s₁ ∩ s₂ :=
ext $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H,
by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or]
@[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) :
s₁ ∩ insert a s₂ = s₁ ∩ s₂ :=
by rw [inter_comm, insert_inter_of_not_mem h, inter_comm]
@[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : {a} ∩ s = {a} :=
show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter]
@[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : {a} ∩ s = ∅ :=
eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h
@[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ {a} = {a} :=
by rw [inter_comm, singleton_inter_of_mem h]
@[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ {a} = ∅ :=
by rw [inter_comm, singleton_inter_of_not_mem h]
@[mono]
lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t :=
begin
intros a a_in,
rw finset.mem_inter at a_in ⊢,
exact ⟨h a_in.1, h' a_in.2⟩
end
lemma inter_subset_inter_left (h : t ⊆ u) : s ∩ t ⊆ s ∩ u := inter_subset_inter subset.rfl h
lemma inter_subset_inter_right (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := inter_subset_inter h subset.rfl
instance : distrib_lattice (finset α) :=
{ le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c,
by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt};
simp only [true_or, imp_true_iff, true_and, or_true],
..finset.lattice }
@[simp] theorem union_left_idem (s t : finset α) : s ∪ (s ∪ t) = s ∪ t := sup_left_idem
@[simp] theorem union_right_idem (s t : finset α) : s ∪ t ∪ t = s ∪ t := sup_right_idem
@[simp] theorem inter_left_idem (s t : finset α) : s ∩ (s ∩ t) = s ∩ t := inf_left_idem
@[simp] theorem inter_right_idem (s t : finset α) : s ∩ t ∩ t = s ∩ t := inf_right_idem
theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left
theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right
theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left
theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right
lemma union_union_distrib_left (s t u : finset α) : s ∪ (t ∪ u) = (s ∪ t) ∪ (s ∪ u) :=
sup_sup_distrib_left _ _ _
lemma union_union_distrib_right (s t u : finset α) : (s ∪ t) ∪ u = (s ∪ u) ∪ (t ∪ u) :=
sup_sup_distrib_right _ _ _
lemma inter_inter_distrib_left (s t u : finset α) : s ∩ (t ∩ u) = (s ∩ t) ∩ (s ∩ u) :=
inf_inf_distrib_left _ _ _
lemma inter_inter_distrib_right (s t u : finset α) : (s ∩ t) ∩ u = (s ∩ u) ∩ (t ∩ u) :=
inf_inf_distrib_right _ _ _
lemma union_union_union_comm (s t u v : finset α) : (s ∪ t) ∪ (u ∪ v) = (s ∪ u) ∪ (t ∪ v) :=
sup_sup_sup_comm _ _ _ _
lemma inter_inter_inter_comm (s t u v : finset α) : (s ∩ t) ∩ (u ∩ v) = (s ∩ u) ∩ (t ∩ v) :=
inf_inf_inf_comm _ _ _ _
lemma union_eq_empty_iff (A B : finset α) : A ∪ B = ∅ ↔ A = ∅ ∧ B = ∅ := sup_eq_bot_iff
lemma union_subset_iff : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := (sup_le_iff : s ⊔ t ≤ u ↔ s ≤ u ∧ t ≤ u)
lemma subset_inter_iff : s ⊆ t ∩ u ↔ s ⊆ t ∧ s ⊆ u := (le_inf_iff : s ≤ t ⊓ u ↔ s ≤ t ∧ s ≤ u)
lemma inter_eq_left_iff_subset (s t : finset α) : s ∩ t = s ↔ s ⊆ t := inf_eq_left
lemma inter_eq_right_iff_subset (s t : finset α) : t ∩ s = s ↔ s ⊆ t := inf_eq_right
lemma inter_congr_left (ht : s ∩ u ⊆ t) (hu : s ∩ t ⊆ u) : s ∩ t = s ∩ u := inf_congr_left ht hu
lemma inter_congr_right (hs : t ∩ u ⊆ s) (ht : s ∩ u ⊆ t) : s ∩ u = t ∩ u := inf_congr_right hs ht
lemma inter_eq_inter_iff_left : s ∩ t = s ∩ u ↔ s ∩ u ⊆ t ∧ s ∩ t ⊆ u := inf_eq_inf_iff_left
lemma inter_eq_inter_iff_right : s ∩ u = t ∩ u ↔ t ∩ u ⊆ s ∧ s ∩ u ⊆ t := inf_eq_inf_iff_right
lemma ite_subset_union (s s' : finset α) (P : Prop) [decidable P] :
ite P s s' ⊆ s ∪ s' := ite_le_sup s s' P
lemma inter_subset_ite (s s' : finset α) (P : Prop) [decidable P] :
s ∩ s' ⊆ ite P s s' := inf_le_ite s s' P
end lattice
/-! ### erase -/
section erase
variables [decidable_eq α] {s t u v : finset α} {a b : α}
/-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are
not equal to `a`. -/
def erase (s : finset α) (a : α) : finset α := ⟨_, s.2.erase a⟩
@[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl
@[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s :=
s.2.mem_erase_iff
lemma not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := s.2.not_mem_erase
-- While this can be solved by `simp`, this lemma is eligible for `dsimp`
@[nolint simp_nf, simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl
@[simp] lemma erase_singleton (a : α) : ({a} : finset α).erase a = ∅ :=
begin
ext x,
rw [mem_erase, mem_singleton, not_and_self],
refl,
end
lemma ne_of_mem_erase : b ∈ erase s a → b ≠ a := λ h, (mem_erase.1 h).1
lemma mem_of_mem_erase : b ∈ erase s a → b ∈ s := mem_of_mem_erase
lemma mem_erase_of_ne_of_mem : a ≠ b → a ∈ s → a ∈ erase s b :=
by simp only [mem_erase]; exact and.intro
/-- An element of `s` that is not an element of `erase s a` must be
`a`. -/
lemma eq_of_mem_of_not_mem_erase (hs : b ∈ s) (hsa : b ∉ s.erase a) : b = a :=
begin
rw [mem_erase, not_and] at hsa,
exact not_imp_not.mp hsa hs
end
@[simp]
theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s :=
eq_of_veq $ erase_of_not_mem h
@[simp] lemma erase_eq_self : s.erase a = s ↔ a ∉ s :=
⟨λ h, h ▸ not_mem_erase _ _, erase_eq_of_not_mem⟩
@[simp] lemma erase_insert_eq_erase (s : finset α) (a : α) :
(insert a s).erase a = s.erase a :=
ext $ λ x, by simp only [mem_erase, mem_insert, and.congr_right_iff, false_or, iff_self,
implies_true_iff] { contextual := tt }
theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s :=
by rw [erase_insert_eq_erase, erase_eq_of_not_mem h]
theorem erase_insert_of_ne {a b : α} {s : finset α} (h : a ≠ b) :
erase (insert a s) b = insert a (erase s b) :=
ext $ λ x, have x ≠ b ∧ x = a ↔ x = a, from and_iff_right_of_imp (λ hx, hx.symm ▸ h),
by simp only [mem_erase, mem_insert, and_or_distrib_left, this]
theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s :=
ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and];
apply or_iff_right_of_imp; rintro rfl; exact h
theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a :=
val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h
theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _
lemma subset_erase {a : α} {s t : finset α} : s ⊆ t.erase a ↔ s ⊆ t ∧ a ∉ s :=
⟨λ h, ⟨h.trans (erase_subset _ _), λ ha, not_mem_erase _ _ (h ha)⟩,
λ h b hb, mem_erase.2 ⟨ne_of_mem_of_not_mem hb h.2, h.1 hb⟩⟩
@[simp, norm_cast] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (s \ {a} : set α) :=
set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl
lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s :=
calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _
... = _ : insert_erase h
lemma ssubset_iff_exists_subset_erase {s t : finset α} : s ⊂ t ↔ ∃ a ∈ t, s ⊆ t.erase a :=
begin
refine ⟨λ h, _, λ ⟨a, ha, h⟩, ssubset_of_subset_of_ssubset h $ erase_ssubset ha⟩,
obtain ⟨a, ht, hs⟩ := (not_subset _ _).1 h.2,
exact ⟨a, ht, subset_erase.2 ⟨h.1, hs⟩⟩,
end
lemma erase_ssubset_insert (s : finset α) (a : α) : s.erase a ⊂ insert a s :=
ssubset_iff_exists_subset_erase.2 ⟨a, mem_insert_self _ _, erase_subset_erase _ $ subset_insert _ _⟩
lemma erase_ne_self : s.erase a ≠ s ↔ a ∈ s := erase_eq_self.not_left
lemma erase_cons {s : finset α} {a : α} (h : a ∉ s) : (s.cons a h).erase a = s :=
by rw [cons_eq_insert, erase_insert_eq_erase, erase_eq_of_not_mem h]
lemma erase_idem {a : α} {s : finset α} : erase (erase s a) a = erase s a :=
by simp
lemma erase_right_comm {a b : α} {s : finset α} : erase (erase s a) b = erase (erase s b) a :=
by { ext x, simp only [mem_erase, ←and_assoc], rw and_comm (x ≠ a) }
theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t :=
by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp];
exact forall_congr (λ x, forall_swap)
theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s :=
subset_insert_iff.1 $ subset.rfl
theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) :=
subset_insert_iff.2 $ subset.rfl
lemma subset_insert_iff_of_not_mem (h : a ∉ s) : s ⊆ insert a t ↔ s ⊆ t :=
by rw [subset_insert_iff, erase_eq_of_not_mem h]
lemma erase_subset_iff_of_mem (h : a ∈ t) : s.erase a ⊆ t ↔ s ⊆ t :=
by rw [←subset_insert_iff, insert_eq_of_mem h]
lemma erase_inj {x y : α} (s : finset α) (hx : x ∈ s) : s.erase x = s.erase y ↔ x = y :=
begin
refine ⟨λ h, _, congr_arg _⟩,
rw eq_of_mem_of_not_mem_erase hx,
rw ←h,
simp,
end
lemma erase_inj_on (s : finset α) : set.inj_on s.erase s := λ _ _ _ _, (erase_inj s ‹_›).mp
lemma erase_inj_on' (a : α) : {s : finset α | a ∈ s}.inj_on (λ s, erase s a) :=
λ s hs t ht (h : s.erase a = _), by rw [←insert_erase hs, ←insert_erase ht, h]
end erase
/-! ### sdiff -/
section sdiff
variables [decidable_eq α] {s t u v : finset α} {a b : α}
/-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/
instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le tsub_le_self s₁.2⟩⟩
@[simp] lemma sdiff_val (s₁ s₂ : finset α) : (s₁ \ s₂).val = s₁.val - s₂.val := rfl
@[simp] theorem mem_sdiff : a ∈ s \ t ↔ a ∈ s ∧ a ∉ t := mem_sub_of_nodup s.2
@[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ :=
eq_empty_of_forall_not_mem $
by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h
instance : generalized_boolean_algebra (finset α) :=
{ sup_inf_sdiff := λ x y, by { simp only [ext_iff, mem_union, mem_sdiff, inf_eq_inter, sup_eq_union,
mem_inter], tauto },
inf_inf_sdiff := λ x y, by { simp only [ext_iff, inter_sdiff_self, inter_empty, inter_assoc,
false_iff, inf_eq_inter, not_mem_empty], tauto },
..finset.has_sdiff,
..finset.distrib_lattice,
..finset.order_bot }
lemma not_mem_sdiff_of_mem_right (h : a ∈ t) : a ∉ s \ t :=
by simp only [mem_sdiff, h, not_true, not_false_iff, and_false]
lemma not_mem_sdiff_of_not_mem_left (h : a ∉ s) : a ∉ s \ t := by simpa
lemma union_sdiff_of_subset (h : s ⊆ t) : s ∪ (t \ s) = t := sup_sdiff_cancel_right h
theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ :=
(union_comm _ _).trans (union_sdiff_of_subset h)
lemma inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] }
@[simp] lemma sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := inf_sdiff_self_left
@[simp] lemma sdiff_self (s₁ : finset α) : s₁ \ s₁ = ∅ := sdiff_self
lemma sdiff_inter_distrib_right (s t u : finset α) : s \ (t ∩ u) = (s \ t) ∪ (s \ u) := sdiff_inf
@[simp] lemma sdiff_inter_self_left (s t : finset α) : s \ (s ∩ t) = s \ t :=
sdiff_inf_self_left _ _
@[simp] lemma sdiff_inter_self_right (s t : finset α) : s \ (t ∩ s) = s \ t :=
sdiff_inf_self_right _ _
@[simp] lemma sdiff_empty : s \ ∅ = s := sdiff_bot
@[mono] lemma sdiff_subset_sdiff (hst : s ⊆ t) (hvu : v ⊆ u) : s \ u ⊆ t \ v :=
sdiff_le_sdiff ‹s ≤ t› ‹v ≤ u›
@[simp, norm_cast] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (s₁ \ s₂ : set α) :=
set.ext $ λ _, mem_sdiff
@[simp] lemma union_sdiff_self_eq_union : s ∪ t \ s = s ∪ t := sup_sdiff_self_right _ _
@[simp] lemma sdiff_union_self_eq_union : s \ t ∪ t = s ∪ t := sup_sdiff_self_left _ _
lemma union_sdiff_left (s t : finset α) : (s ∪ t) \ s = t \ s := sup_sdiff_left_self
lemma union_sdiff_right (s t : finset α) : (s ∪ t) \ t = s \ t := sup_sdiff_right_self
lemma union_sdiff_symm : s ∪ (t \ s) = t ∪ (s \ t) := by simp [union_comm]
lemma sdiff_union_inter (s t : finset α) : (s \ t) ∪ (s ∩ t) = s := sup_sdiff_inf _ _
@[simp] lemma sdiff_idem (s t : finset α) : s \ t \ t = s \ t := sdiff_idem
lemma sdiff_eq_empty_iff_subset : s \ t = ∅ ↔ s ⊆ t := sdiff_eq_bot_iff
lemma sdiff_nonempty : (s \ t).nonempty ↔ ¬ s ⊆ t :=
nonempty_iff_ne_empty.trans sdiff_eq_empty_iff_subset.not
@[simp] lemma empty_sdiff (s : finset α) : ∅ \ s = ∅ := bot_sdiff
lemma insert_sdiff_of_not_mem (s : finset α) {t : finset α} {x : α} (h : x ∉ t) :
(insert x s) \ t = insert x (s \ t) :=
begin
rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert],
exact set.insert_diff_of_not_mem s h
end
lemma insert_sdiff_of_mem (s : finset α) {x : α} (h : x ∈ t) : (insert x s) \ t = s \ t :=
begin
rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert],
exact set.insert_diff_of_mem s h
end
@[simp] lemma insert_sdiff_insert (s t : finset α) (x : α) :
(insert x s) \ (insert x t) = s \ insert x t :=
insert_sdiff_of_mem _ (mem_insert_self _ _)
lemma sdiff_insert_of_not_mem {x : α} (h : x ∉ s) (t : finset α) : s \ (insert x t) = s \ t :=
begin
refine subset.antisymm (sdiff_subset_sdiff (subset.refl _) (subset_insert _ _)) (λ y hy, _),
simp only [mem_sdiff, mem_insert, not_or_distrib] at hy ⊢,
exact ⟨hy.1, λ hxy, h $ hxy ▸ hy.1, hy.2⟩
end
@[simp] lemma sdiff_subset (s t : finset α) : s \ t ⊆ s := show s \ t ≤ s, from sdiff_le
lemma sdiff_ssubset (h : t ⊆ s) (ht : t.nonempty) : s \ t ⊂ s := sdiff_lt ‹t ≤ s› ht.ne_empty
lemma union_sdiff_distrib (s₁ s₂ t : finset α) : (s₁ ∪ s₂) \ t = s₁ \ t ∪ s₂ \ t := sup_sdiff
lemma sdiff_union_distrib (s t₁ t₂ : finset α) : s \ (t₁ ∪ t₂) = (s \ t₁) ∩ (s \ t₂) := sdiff_sup
lemma union_sdiff_self (s t : finset α) : (s ∪ t) \ t = s \ t := sup_sdiff_right_self
lemma sdiff_singleton_eq_erase (a : α) (s : finset α) : s \ singleton a = erase s a :=
by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto }
@[simp] lemma sdiff_singleton_not_mem_eq_self (s : finset α) {a : α} (ha : a ∉ s) : s \ {a} = s :=
by simp only [sdiff_singleton_eq_erase, ha, erase_eq_of_not_mem, not_false_iff]
lemma sdiff_sdiff_left' (s t u : finset α) :
(s \ t) \ u = (s \ t) ∩ (s \ u) := sdiff_sdiff_left'
lemma sdiff_insert (s t : finset α) (x : α) :
s \ insert x t = (s \ t).erase x :=
by simp_rw [← sdiff_singleton_eq_erase, insert_eq,
sdiff_sdiff_left', sdiff_union_distrib, inter_comm]
lemma sdiff_insert_insert_of_mem_of_not_mem {s t : finset α} {x : α} (hxs : x ∈ s) (hxt : x ∉ t) :
insert x (s \ insert x t) = s \ t :=
by rw [sdiff_insert, insert_erase (mem_sdiff.mpr ⟨hxs, hxt⟩)]
lemma sdiff_erase {x : α} (hx : x ∈ s) : s \ s.erase x = {x} :=
begin
rw [← sdiff_singleton_eq_erase, sdiff_sdiff_right_self],
exact inf_eq_right.2 (singleton_subset_iff.2 hx),
end
lemma sdiff_sdiff_self_left (s t : finset α) : s \ (s \ t) = s ∩ t := sdiff_sdiff_right_self
lemma sdiff_sdiff_eq_self (h : t ⊆ s) : s \ (s \ t) = t := sdiff_sdiff_eq_self h
lemma sdiff_eq_sdiff_iff_inter_eq_inter {s t₁ t₂ : finset α} : s \ t₁ = s \ t₂ ↔ s ∩ t₁ = s ∩ t₂ :=
sdiff_eq_sdiff_iff_inf_eq_inf
lemma union_eq_sdiff_union_sdiff_union_inter (s t : finset α) :
s ∪ t = (s \ t) ∪ (t \ s) ∪ (s ∩ t) :=
sup_eq_sdiff_sup_sdiff_sup_inf
lemma erase_eq_empty_iff (s : finset α) (a : α) : s.erase a = ∅ ↔ s = ∅ ∨ s = {a} :=
by rw [←sdiff_singleton_eq_erase, sdiff_eq_empty_iff_subset, subset_singleton_iff]
--TODO@Yaël: Kill lemmas duplicate with `boolean_algebra`
lemma sdiff_disjoint : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2
lemma disjoint_sdiff : disjoint s (t \ s) := sdiff_disjoint.symm
lemma disjoint_sdiff_inter (s t : finset α) : disjoint (s \ t) (s ∩ t) :=
disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint
lemma sdiff_eq_self_iff_disjoint : s \ t = s ↔ disjoint s t := sdiff_eq_self_iff_disjoint'
lemma sdiff_eq_self_of_disjoint (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h
end sdiff
/-! ### Symmetric difference -/
section symm_diff
variables [decidable_eq α] {s t : finset α} {a b : α}
lemma mem_symm_diff : a ∈ s ∆ t ↔ a ∈ s ∧ a ∉ t ∨ a ∈ t ∧ a ∉ s :=
by simp_rw [symm_diff, sup_eq_union, mem_union, mem_sdiff]
@[simp, norm_cast] lemma coe_symm_diff : (↑(s ∆ t) : set α) = s ∆ t := set.ext $ λ _, mem_symm_diff
end symm_diff
/-! ### attach -/
/-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype
`{x // x ∈ s}`. -/
def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩
theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {s : finset α} (hx : x ∈ s) :
sizeof x < sizeof s := by
{ cases s, dsimp [sizeof, has_sizeof.sizeof, finset.sizeof],
apply lt_add_left, exact multiset.sizeof_lt_sizeof_of_mem hx }
@[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl
@[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _
@[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl
@[simp] lemma attach_nonempty_iff (s : finset α) : s.attach.nonempty ↔ s.nonempty :=
by simp [finset.nonempty]
@[simp] lemma attach_eq_empty_iff (s : finset α) : s.attach = ∅ ↔ s = ∅ :=
by simpa [eq_empty_iff_forall_not_mem]
/-! ### piecewise -/
section piecewise
/-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its
complement. -/
def piecewise {α : Type*} {δ : α → Sort*} (s : finset α) (f g : Π i, δ i) [Π j, decidable (j ∈ s)] :
Π i, δ i :=
λi, if i ∈ s then f i else g i
variables {δ : α → Sort*} (s : finset α) (f g : Π i, δ i)
@[simp] lemma piecewise_insert_self [decidable_eq α] {j : α} [∀ i, decidable (i ∈ insert j s)] :
(insert j s).piecewise f g j = f j :=
by simp [piecewise]
@[simp] lemma piecewise_empty [Π i : α, decidable (i ∈ (∅ : finset α))] : piecewise ∅ f g = g :=
by { ext i, simp [piecewise] }
variable [Π j, decidable (j ∈ s)]
-- TODO: fix this in norm_cast
@[norm_cast move] lemma piecewise_coe [∀ j, decidable (j ∈ (s : set α))] :
(s : set α).piecewise f g = s.piecewise f g :=
by { ext, congr }
@[simp, priority 980]
lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi]
@[simp, priority 980]
lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i :=
by simp [piecewise, hi]
lemma piecewise_congr {f f' g g' : Π i, δ i} (hf : ∀ i ∈ s, f i = f' i) (hg : ∀ i ∉ s, g i = g' i) :
s.piecewise f g = s.piecewise f' g' :=
funext $ λ i, if_ctx_congr iff.rfl (hf i) (hg i)
@[simp, priority 990]
lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀ i, decidable (i ∈ insert j s)]
(h : i ≠ j) : (insert j s).piecewise f g i = s.piecewise f g i :=
by simp [piecewise, h]
lemma piecewise_insert [decidable_eq α] (j : α) [∀ i, decidable (i ∈ insert j s)] :
(insert j s).piecewise f g = update (s.piecewise f g) j (f j) :=
by { classical, simp only [← piecewise_coe, coe_insert, ← set.piecewise_insert] }
lemma piecewise_cases {i} (p : δ i → Prop) (hf : p (f i)) (hg : p (g i)) : p (s.piecewise f g i) :=
by by_cases hi : i ∈ s; simpa [hi]
lemma piecewise_mem_set_pi {δ : α → Type*} {t : set α} {t' : Π i, set (δ i)}
{f g} (hf : f ∈ set.pi t t') (hg : g ∈ set.pi t t') : s.piecewise f g ∈ set.pi t t' :=
by { classical, rw ← piecewise_coe, exact set.piecewise_mem_pi ↑s hf hg }
lemma piecewise_singleton [decidable_eq α] (i : α) :
piecewise {i} f g = update g i (f i) :=
by rw [← insert_emptyc_eq, piecewise_insert, piecewise_empty]
lemma piecewise_piecewise_of_subset_left {s t : finset α} [Π i, decidable (i ∈ s)]
[Π i, decidable (i ∈ t)] (h : s ⊆ t) (f₁ f₂ g : Π a, δ a) :
s.piecewise (t.piecewise f₁ f₂) g = s.piecewise f₁ g :=
s.piecewise_congr (λ i hi, piecewise_eq_of_mem _ _ _ (h hi)) (λ _ _, rfl)
@[simp] lemma piecewise_idem_left (f₁ f₂ g : Π a, δ a) :
s.piecewise (s.piecewise f₁ f₂) g = s.piecewise f₁ g :=
piecewise_piecewise_of_subset_left (subset.refl _) _ _ _
lemma piecewise_piecewise_of_subset_right {s t : finset α} [Π i, decidable (i ∈ s)]
[Π i, decidable (i ∈ t)] (h : t ⊆ s) (f g₁ g₂ : Π a, δ a) :
s.piecewise f (t.piecewise g₁ g₂) = s.piecewise f g₂ :=
s.piecewise_congr (λ _ _, rfl) (λ i hi, t.piecewise_eq_of_not_mem _ _ (mt (@h _) hi))
@[simp] lemma piecewise_idem_right (f g₁ g₂ : Π a, δ a) :
s.piecewise f (s.piecewise g₁ g₂) = s.piecewise f g₂ :=
piecewise_piecewise_of_subset_right (subset.refl _) f g₁ g₂
lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) :
update f i v = piecewise (singleton i) (λj, v) f :=
(piecewise_singleton _ _ _).symm
lemma update_piecewise [decidable_eq α] (i : α) (v : δ i) :
update (s.piecewise f g) i v = s.piecewise (update f i v) (update g i v) :=
begin
ext j,
rcases em (j = i) with (rfl|hj); by_cases hs : j ∈ s; simp *
end
lemma update_piecewise_of_mem [decidable_eq α] {i : α} (hi : i ∈ s) (v : δ i) :
update (s.piecewise f g) i v = s.piecewise (update f i v) g :=
begin
rw update_piecewise,
refine s.piecewise_congr (λ _ _, rfl) (λ j hj, update_noteq _ _ _),
exact λ h, hj (h.symm ▸ hi)
end
lemma update_piecewise_of_not_mem [decidable_eq α] {i : α} (hi : i ∉ s) (v : δ i) :
update (s.piecewise f g) i v = s.piecewise f (update g i v) :=
begin
rw update_piecewise,
refine s.piecewise_congr (λ j hj, update_noteq _ _ _) (λ _ _, rfl),
exact λ h, hi (h ▸ hj)
end
lemma piecewise_le_of_le_of_le {δ : α → Type*} [Π i, preorder (δ i)] {f g h : Π i, δ i}
(Hf : f ≤ h) (Hg : g ≤ h) : s.piecewise f g ≤ h :=
λ x, piecewise_cases s f g (≤ h x) (Hf x) (Hg x)
lemma le_piecewise_of_le_of_le {δ : α → Type*} [Π i, preorder (δ i)] {f g h : Π i, δ i}
(Hf : h ≤ f) (Hg : h ≤ g) : h ≤ s.piecewise f g :=
λ x, piecewise_cases s f g (λ y, h x ≤ y) (Hf x) (Hg x)
lemma piecewise_le_piecewise' {δ : α → Type*} [Π i, preorder (δ i)] {f g f' g' : Π i, δ i}
(Hf : ∀ x ∈ s, f x ≤ f' x) (Hg : ∀ x ∉ s, g x ≤ g' x) : s.piecewise f g ≤ s.piecewise f' g' :=
λ x, by { by_cases hx : x ∈ s; simp [hx, *] }
lemma piecewise_le_piecewise {δ : α → Type*} [Π i, preorder (δ i)] {f g f' g' : Π i, δ i}
(Hf : f ≤ f') (Hg : g ≤ g') : s.piecewise f g ≤ s.piecewise f' g' :=
s.piecewise_le_piecewise' (λ x _, Hf x) (λ x _, Hg x)
lemma piecewise_mem_Icc_of_mem_of_mem {δ : α → Type*} [Π i, preorder (δ i)] {f f₁ g g₁ : Π i, δ i}
(hf : f ∈ set.Icc f₁ g₁) (hg : g ∈ set.Icc f₁ g₁) :
s.piecewise f g ∈ set.Icc f₁ g₁ :=
⟨le_piecewise_of_le_of_le _ hf.1 hg.1, piecewise_le_of_le_of_le _ hf.2 hg.2⟩
lemma piecewise_mem_Icc {δ : α → Type*} [Π i, preorder (δ i)] {f g : Π i, δ i} (h : f ≤ g) :
s.piecewise f g ∈ set.Icc f g :=
piecewise_mem_Icc_of_mem_of_mem _ (set.left_mem_Icc.2 h) (set.right_mem_Icc.2 h)
lemma piecewise_mem_Icc' {δ : α → Type*} [Π i, preorder (δ i)] {f g : Π i, δ i} (h : g ≤ f) :
s.piecewise f g ∈ set.Icc g f :=
piecewise_mem_Icc_of_mem_of_mem _ (set.right_mem_Icc.2 h) (set.left_mem_Icc.2 h)
end piecewise
section decidable_pi_exists
variables {s : finset α}
instance decidable_dforall_finset {p : Π a ∈ s, Prop} [hp : ∀ a (h : a ∈ s), decidable (p a h)] :
decidable (∀ a (h : a ∈ s), p a h) :=
multiset.decidable_dforall_multiset
/-- decidable equality for functions whose domain is bounded by finsets -/
instance decidable_eq_pi_finset {β : α → Type*} [h : ∀ a, decidable_eq (β a)] :
decidable_eq (Π a ∈ s, β a) :=
multiset.decidable_eq_pi_multiset
instance decidable_dexists_finset {p : Π a ∈ s, Prop} [hp : ∀ a (h : a ∈ s), decidable (p a h)] :
decidable (∃ a (h : a ∈ s), p a h) :=
multiset.decidable_dexists_multiset
end decidable_pi_exists
/-! ### filter -/
section filter
variables (p q : α → Prop) [decidable_pred p] [decidable_pred q]
/-- `filter p s` is the set of elements of `s` that satisfy `p`. -/
def filter (s : finset α) : finset α := ⟨_, s.2.filter p⟩
@[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl
@[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ _
variable {p}
@[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter
lemma mem_of_mem_filter {s : finset α} (x : α) (h : x ∈ s.filter p) : x ∈ s :=
mem_of_mem_filter h
theorem filter_ssubset {s : finset α} : s.filter p ⊂ s ↔ ∃ x ∈ s, ¬ p x :=
⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (λ hp, mem_filter.2 ⟨hs, hp⟩) hp⟩,
λ ⟨x, hs, hp⟩, ⟨s.filter_subset _, λ h, hp (mem_filter.1 (h hs)).2⟩⟩
variable (p)
theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) :=
ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm]
lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] :
@finset.filter α (λ _, true) h s = s :=
by ext; simp
@[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ :=
ext $ assume a, by simp only [mem_filter, and_false]; refl
variables {p q}
lemma filter_eq_self (s : finset α) :
s.filter p = s ↔ ∀ x ∈ s, p x :=
by simp [finset.ext_iff]
/-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/
@[simp] lemma filter_true_of_mem {s : finset α} (h : ∀ x ∈ s, p x) : s.filter p = s :=
(filter_eq_self s).mpr h
/-- If all elements of a `finset` fail to satisfy the predicate `p`, `s.filter p` is `∅`. -/
lemma filter_false_of_mem {s : finset α} (h : ∀ x ∈ s, ¬ p x) : s.filter p = ∅ :=
eq_empty_of_forall_not_mem (by simpa)
lemma filter_eq_empty_iff (s : finset α) :
(s.filter p = ∅) ↔ ∀ x ∈ s, ¬ p x :=
begin
refine ⟨_, filter_false_of_mem⟩,
intros hs,
injection hs with hs',
rwa filter_eq_nil at hs'
end
lemma filter_nonempty_iff {s : finset α} : (s.filter p).nonempty ↔ ∃ a ∈ s, p a :=
by simp only [nonempty_iff_ne_empty, ne.def, filter_eq_empty_iff, not_not, not_forall]
lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s :=
eq_of_veq $ filter_congr H
variables (p q)
lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ _
lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p :=
assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩
lemma monotone_filter_left : monotone (filter p) :=
λ _ _, filter_subset_filter p
lemma monotone_filter_right (s : finset α) ⦃p q : α → Prop⦄
[decidable_pred p] [decidable_pred q] (h : p ≤ q) :
s.filter p ≤ s.filter q :=
multiset.subset_of_le (multiset.monotone_filter_right s.val h)
@[simp, norm_cast] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) :=
set.ext $ λ _, mem_filter
lemma subset_coe_filter_of_subset_forall (s : finset α) {t : set α}
(h₁ : t ⊆ s) (h₂ : ∀ x ∈ t, p x) : t ⊆ s.filter p :=
λ x hx, (s.coe_filter p).symm ▸ ⟨h₁ hx, h₂ x hx⟩
theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ :=
by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
theorem filter_cons_of_pos (a : α) (s : finset α) (ha : a ∉ s) (hp : p a):
filter p (cons a s ha) = cons a (filter p s) (mem_filter.not.mpr $ mt and.left ha) :=
eq_of_veq $ multiset.filter_cons_of_pos s.val hp
theorem filter_cons_of_neg (a : α) (s : finset α) (ha : a ∉ s) (hp : ¬p a):
filter p (cons a s ha) = filter p s :=
eq_of_veq $ multiset.filter_cons_of_neg s.val hp
lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] :
disjoint (s.filter p) (s.filter q) ↔ ∀ x ∈ s, p x → ¬ q x :=
by split; simp [disjoint_left] {contextual := tt}
lemma disjoint_filter_filter {s t : finset α} {p q : α → Prop}
[decidable_pred p] [decidable_pred q] :
disjoint s t → disjoint (s.filter p) (t.filter q) :=
disjoint.mono (filter_subset _ _) (filter_subset _ _)
lemma disjoint_filter_filter' (s t : finset α) {p q : α → Prop}
[decidable_pred p] [decidable_pred q] (h : disjoint p q) :
disjoint (s.filter p) (t.filter q) :=
begin
simp_rw [disjoint_left, mem_filter],
rintros a ⟨hs, hp⟩ ⟨ht, hq⟩,
exact h.le_bot _ ⟨hp, hq⟩,
end
lemma disjoint_filter_filter_neg (s t : finset α) (p : α → Prop)
[decidable_pred p] [decidable_pred (λ a, ¬ p a)] :
disjoint (s.filter p) (t.filter $ λ a, ¬ p a) :=
disjoint_filter_filter' s t disjoint_compl_right
theorem filter_disj_union (s : finset α) (t : finset α) (h : disjoint s t) :
filter p (disj_union s t h) = (filter p s).disj_union (filter p t)
(disjoint_filter_filter h) :=
eq_of_veq $ multiset.filter_add _ _ _
theorem filter_cons {a : α} (s : finset α) (ha : a ∉ s) :
filter p (cons a s ha) = (if p a then {a} else ∅ : finset α).disj_union (filter p s) (by
{ split_ifs,
{ rw disjoint_singleton_left,
exact (mem_filter.not.mpr $ mt and.left ha) },
{ exact disjoint_empty_left _ } }) :=
begin
split_ifs with h,
{ rw [filter_cons_of_pos _ _ _ ha h, singleton_disj_union] },
{ rw [filter_cons_of_neg _ _ _ ha h, empty_disj_union] },
end
variable [decidable_eq α]
theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p :=
ext $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right]
theorem filter_union_right (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) :=
ext $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm]
lemma filter_mem_eq_inter {s t : finset α} [Π i, decidable (i ∈ t)] :
s.filter (λ i, i ∈ t) = s ∩ t :=
ext $ λ i, by rw [mem_filter, mem_inter]
lemma filter_inter_distrib (s t : finset α) : (s ∩ t).filter p = s.filter p ∩ t.filter p :=
by { ext, simp only [mem_filter, mem_inter], exact and_and_distrib_right _ _ _ }
theorem filter_inter (s t : finset α) : filter p s ∩ t = filter p (s ∩ t) :=
by { ext, simp only [mem_inter, mem_filter, and.right_comm] }
theorem inter_filter (s t : finset α) : s ∩ filter p t = filter p (s ∩ t) :=
by rw [inter_comm, filter_inter, inter_comm]
theorem filter_insert (a : α) (s : finset α) :
filter p (insert a s) = if p a then insert a (filter p s) else filter p s :=
by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
theorem filter_erase (a : α) (s : finset α) : filter p (erase s a) = erase (filter p s) a :=
by { ext x, simp only [and_assoc, mem_filter, iff_self, mem_erase] }
theorem filter_or [decidable_pred (λ a, p a ∨ q a)] (s : finset α) :
s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q :=
ext $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left]
theorem filter_and [decidable_pred (λ a, p a ∧ q a)] (s : finset α) :
s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q :=
ext $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self]
theorem filter_not [decidable_pred (λ a, ¬ p a)] (s : finset α) :
s.filter (λ a, ¬ p a) = s \ s.filter p :=
ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $
λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm
theorem sdiff_eq_filter (s₁ s₂ : finset α) :
s₁ \ s₂ = filter (∉ s₂) s₁ := ext $ λ _, by simp only [mem_sdiff, mem_filter]
lemma sdiff_eq_self (s₁ s₂ : finset α) : s₁ \ s₂ = s₁ ↔ s₁ ∩ s₂ ⊆ ∅ :=
by { simp [subset.antisymm_iff],
split; intro h,
{ transitivity' ((s₁ \ s₂) ∩ s₂), mono, simp },
{ calc s₁ \ s₂
⊇ s₁ \ (s₁ ∩ s₂) : by simp [(⊇)]
... ⊇ s₁ \ ∅ : by mono using [(⊇)]
... ⊇ s₁ : by simp [(⊇)] } }
lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} (h : ↑s ⊆ t₁ ∪ t₂) :
∃ s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ :=
begin
classical,
refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩,
{ simp [filter_union_right, em] },
{ intro x, simp },
{ intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ }
end
/- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/
@[simp] lemma filter_congr_decidable {α} (s : finset α) (p : α → Prop) (h : decidable_pred p)
[decidable_pred p] : @filter α p h s = s.filter p :=
by congr
section classical
open_locale classical
/-- The following instance allows us to write `{x ∈ s | p x}` for `finset.filter p s`.
Since the former notation requires us to define this for all propositions `p`, and `finset.filter`
only works for decidable propositions, the notation `{x ∈ s | p x}` is only compatible with
classical logic because it uses `classical.prop_decidable`.
We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp`
unfolds the notation `{x ∈ s | p x}` to `finset.filter p s`. If `p` happens to be decidable, the
simp-lemma `finset.filter_congr_decidable` will make sure that `finset.filter` uses the right
instance for decidability.
-/
noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩
@[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl
end classical
/--
After filtering out everything that does not equal a given value, at most that value remains.
This is equivalent to `filter_eq'` with the equality the other way.
-/
-- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing
-- on, e.g. `x ∈ s.filter(eq b)`.
lemma filter_eq [decidable_eq β] (s : finset β) (b : β) : s.filter (eq b) = ite (b ∈ s) {b} ∅ :=
begin
split_ifs,
{ ext,
simp only [mem_filter, mem_singleton],
exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩ }⟩ },
{ ext,
simp only [mem_filter, not_and, iff_false, not_mem_empty],
rintro m ⟨e⟩, exact h m }
end
/--
After filtering out everything that does not equal a given value, at most that value remains.
This is equivalent to `filter_eq` with the equality the other way.
-/
lemma filter_eq' [decidable_eq β] (s : finset β) (b : β) :
s.filter (λ a, a = b) = ite (b ∈ s) {b} ∅ :=
trans (filter_congr (λ _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b)
lemma filter_ne [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, b ≠ a) = s.erase b :=
by { ext, simp only [mem_filter, mem_erase, ne.def], tauto }
lemma filter_ne' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a ≠ b) = s.erase b :=
trans (filter_congr (λ _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b)
theorem filter_inter_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s t : finset α) :
s.filter p ∩ t.filter (λa, ¬ p a) = ∅ :=
(disjoint_filter_filter_neg s t p).eq_bot
theorem filter_union_filter_of_codisjoint (s : finset α) (h : codisjoint p q) :
s.filter p ∪ s.filter q = s :=
(filter_or _ _ _).symm.trans $ filter_true_of_mem $ λ x hx, h.top_le x trivial
theorem filter_union_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s : finset α) :
s.filter p ∪ s.filter (λa, ¬ p a) = s :=
filter_union_filter_of_codisjoint _ _ _ codisjoint_hnot_right
end filter
/-! ### range -/
section range
variables {n m l : ℕ}
/-- `range n` is the set of natural numbers less than `n`. -/
def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩
@[simp] theorem range_coe (n : ℕ) : (range n).1 = multiset.range n := rfl
@[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range
@[simp] theorem range_zero : range 0 = ∅ := rfl
@[simp] theorem range_one : range 1 = {0} := rfl
theorem range_succ : range (succ n) = insert n (range n) :=
eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm
lemma range_add_one : range (n + 1) = insert n (range n) := range_succ
@[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self
@[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := multiset.self_mem_range_succ n
@[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset
theorem range_mono : monotone range := λ _ _, range_subset.2
lemma mem_range_succ_iff {a b : ℕ} : a ∈ finset.range b.succ ↔ a ≤ b :=
finset.mem_range.trans nat.lt_succ_iff
lemma mem_range_le {n x : ℕ} (hx : x ∈ range n) : x ≤ n := (mem_range.1 hx).le
lemma mem_range_sub_ne_zero {n x : ℕ} (hx : x ∈ range n) : n - x ≠ 0 :=
ne_of_gt $ tsub_pos_of_lt $ mem_range.1 hx
@[simp] lemma nonempty_range_iff : (range n).nonempty ↔ n ≠ 0 :=
⟨λ ⟨k, hk⟩, ((zero_le k).trans_lt $ mem_range.1 hk).ne',
λ h, ⟨0, mem_range.2 $ pos_iff_ne_zero.2 h⟩⟩
@[simp] lemma range_eq_empty_iff : range n = ∅ ↔ n = 0 :=
by rw [← not_nonempty_iff_eq_empty, nonempty_range_iff, not_not]
lemma nonempty_range_succ : (range $ n + 1).nonempty :=
nonempty_range_iff.2 n.succ_ne_zero
@[simp]
lemma range_filter_eq {n m : ℕ} : (range n).filter (= m) = if m < n then {m} else ∅ :=
begin
convert filter_eq (range n) m,
{ ext, exact comm },
{ simp }
end
end range
/- useful rules for calculations with quantifiers -/
theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false :=
by simp only [not_mem_empty, false_and, exists_false]
lemma exists_mem_insert [decidable_eq α] (a : α) (s : finset α) (p : α → Prop) :
(∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ ∃ x, x ∈ s ∧ p x :=
by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left]
theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true :=
iff_true_intro $ λ _, false.elim
lemma forall_mem_insert [decidable_eq α] (a : α) (s : finset α) (p : α → Prop) :
(∀ x, x ∈ insert a s → p x) ↔ p a ∧ ∀ x, x ∈ s → p x :=
by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq]
end finset
/-- Equivalence between the set of natural numbers which are `≥ k` and `ℕ`, given by `n → n - k`. -/
def not_mem_range_equiv (k : ℕ) : {n // n ∉ range k} ≃ ℕ :=
{ to_fun := λ i, i.1 - k,
inv_fun := λ j, ⟨j + k, by simp⟩,
left_inv := λ j,
begin
rw subtype.ext_iff_val,
apply tsub_add_cancel_of_le,
simpa using j.2
end,
right_inv := λ j, add_tsub_cancel_right _ _ }
@[simp] lemma coe_not_mem_range_equiv (k : ℕ) :
(not_mem_range_equiv k : {n // n ∉ range k} → ℕ) = (λ i, i - k) := rfl
@[simp] lemma coe_not_mem_range_equiv_symm (k : ℕ) :
((not_mem_range_equiv k).symm : ℕ → {n // n ∉ range k}) = λ j, ⟨j + k, by simp⟩ := rfl
/-! ### dedup on list and multiset -/
namespace multiset
variable [decidable_eq α]
/-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/
def to_finset (s : multiset α) : finset α := ⟨_, nodup_dedup s⟩
@[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.dedup := rfl
theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset :=
finset.val_inj.1 n.dedup.symm
lemma nodup.to_finset_inj {l l' : multiset α} (hl : nodup l) (hl' : nodup l')
(h : l.to_finset = l'.to_finset) : l = l' :=
by simpa [←to_finset_eq hl, ←to_finset_eq hl'] using h
@[simp] lemma mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_dedup
@[simp] lemma to_finset_zero : to_finset (0 : multiset α) = ∅ := rfl
@[simp] lemma to_finset_cons (a : α) (s : multiset α) :
to_finset (a ::ₘ s) = insert a (to_finset s) :=
finset.eq_of_veq dedup_cons
@[simp] lemma to_finset_singleton (a : α) : to_finset ({a} : multiset α) = {a} :=
by rw [←cons_zero, to_finset_cons, to_finset_zero, is_lawful_singleton.insert_emptyc_eq]
@[simp] lemma to_finset_add (s t : multiset α) : to_finset (s + t) = to_finset s ∪ to_finset t :=
finset.ext $ by simp
@[simp] lemma to_finset_nsmul (s : multiset α) :
∀ (n : ℕ) (hn : n ≠ 0), (n • s).to_finset = s.to_finset
| 0 h := by contradiction
| (n+1) h :=
begin
by_cases n = 0,
{ rw [h, zero_add, one_nsmul] },
{ rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] }
end
@[simp] lemma to_finset_inter (s t : multiset α) : to_finset (s ∩ t) = to_finset s ∩ to_finset t :=
finset.ext $ by simp
@[simp] lemma to_finset_union (s t : multiset α) : (s ∪ t).to_finset = s.to_finset ∪ t.to_finset :=
by ext; simp
@[simp] theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 :=
finset.val_inj.symm.trans multiset.dedup_eq_zero
@[simp] lemma to_finset_subset (s t : multiset α) : s.to_finset ⊆ t.to_finset ↔ s ⊆ t :=
by simp only [finset.subset_iff, multiset.subset_iff, multiset.mem_to_finset]
@[simp] lemma to_finset_dedup (m : multiset α) :
m.dedup.to_finset = m.to_finset :=
by simp_rw [to_finset, dedup_idempotent]
@[simp] lemma to_finset_bind_dedup [decidable_eq β] (m : multiset α) (f : α → multiset β) :
(m.dedup.bind f).to_finset = (m.bind f).to_finset :=
by simp_rw [to_finset, dedup_bind_dedup]
end multiset
namespace finset
@[simp] lemma val_to_finset [decidable_eq α] (s : finset α) : s.val.to_finset = s :=
by { ext, rw [multiset.mem_to_finset, ←mem_def] }
lemma val_le_iff_val_subset {a : finset α} {b : multiset α} : a.val ≤ b ↔ a.val ⊆ b :=
multiset.le_iff_subset a.nodup
end finset
namespace list
variables [decidable_eq α] {l l' : list α} {a : α}
/-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/
def to_finset (l : list α) : finset α := multiset.to_finset l
@[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.dedup : multiset α) := rfl
@[simp] theorem to_finset_coe (l : list α) : (l : multiset α).to_finset = l.to_finset := rfl
lemma to_finset_eq (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n
@[simp] lemma mem_to_finset : a ∈ l.to_finset ↔ a ∈ l := mem_dedup
@[simp, norm_cast] lemma coe_to_finset (l : list α) : (l.to_finset : set α) = {a | a ∈ l} :=
set.ext $ λ _, list.mem_to_finset
@[simp] lemma to_finset_nil : to_finset (@nil α) = ∅ := rfl
@[simp] lemma to_finset_cons : to_finset (a :: l) = insert a (to_finset l) :=
finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.dedup_cons, h]
lemma to_finset_surj_on : set.surj_on to_finset {l : list α | l.nodup} set.univ :=
by { rintro ⟨⟨l⟩, hl⟩ _, exact ⟨l, hl, (to_finset_eq hl).symm⟩ }
theorem to_finset_surjective : surjective (to_finset : list α → finset α) :=
λ s, let ⟨l, _, hls⟩ := to_finset_surj_on (set.mem_univ s) in ⟨l, hls⟩
lemma to_finset_eq_iff_perm_dedup : l.to_finset = l'.to_finset ↔ l.dedup ~ l'.dedup :=
by simp [finset.ext_iff, perm_ext (nodup_dedup _) (nodup_dedup _)]
lemma to_finset.ext_iff {a b : list α} : a.to_finset = b.to_finset ↔ ∀ x, x ∈ a ↔ x ∈ b :=
by simp only [finset.ext_iff, mem_to_finset]
lemma to_finset.ext : (∀ x, x ∈ l ↔ x ∈ l') → l.to_finset = l'.to_finset := to_finset.ext_iff.mpr
lemma to_finset_eq_of_perm (l l' : list α) (h : l ~ l') : l.to_finset = l'.to_finset :=
to_finset_eq_iff_perm_dedup.mpr h.dedup
lemma perm_of_nodup_nodup_to_finset_eq (hl : nodup l) (hl' : nodup l')
(h : l.to_finset = l'.to_finset) : l ~ l' :=
by { rw ←multiset.coe_eq_coe, exact multiset.nodup.to_finset_inj hl hl' h }
@[simp] lemma to_finset_append : to_finset (l ++ l') = l.to_finset ∪ l'.to_finset :=
begin
induction l with hd tl hl,
{ simp },
{ simp [hl] }
end
@[simp] lemma to_finset_reverse {l : list α} : to_finset l.reverse = l.to_finset :=
to_finset_eq_of_perm _ _ (reverse_perm l)
lemma to_finset_repeat_of_ne_zero {n : ℕ} (hn : n ≠ 0) : (list.repeat a n).to_finset = {a} :=
by { ext x, simp [hn, list.mem_repeat] }
@[simp] lemma to_finset_union (l l' : list α) : (l ∪ l').to_finset = l.to_finset ∪ l'.to_finset :=
by { ext, simp }
@[simp] lemma to_finset_inter (l l' : list α) : (l ∩ l').to_finset = l.to_finset ∩ l'.to_finset :=
by { ext, simp }
@[simp] lemma to_finset_eq_empty_iff (l : list α) : l.to_finset = ∅ ↔ l = nil := by cases l; simp
end list
namespace finset
/-! ### map -/
section map
open function
/-- When `f` is an embedding of `α` in `β` and `s` is a finset in `α`, then `s.map f` is the image
finset in `β`. The embedding condition guarantees that there are no duplicates in the image. -/
def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, s.2.map f.2⟩
@[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl
@[simp] theorem map_empty (f : α ↪ β) : (∅ : finset α).map f = ∅ := rfl
variables {f : α ↪ β} {s : finset α}
@[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b :=
mem_map.trans $ by simp only [exists_prop]; refl
@[simp] lemma mem_map_equiv {f : α ≃ β} {b : β} : b ∈ s.map f.to_embedding ↔ f.symm b ∈ s :=
by { rw mem_map, exact ⟨by { rintro ⟨a, H, rfl⟩, simpa }, λ h, ⟨_, h, by simp⟩⟩ }
lemma mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_injective f.2
lemma mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f := (mem_map' _).2
lemma forall_mem_map {f : α ↪ β} {s : finset α} {p : Π a, a ∈ s.map f → Prop} :
(∀ y ∈ s.map f, p y H) ↔ ∀ x ∈ s, p (f x) (mem_map_of_mem _ H) :=
⟨λ h y hy, h (f y) (mem_map_of_mem _ hy), λ h x hx,
by { obtain ⟨y, hy, rfl⟩ := mem_map.1 hx, exact h _ hy }⟩
lemma apply_coe_mem_map (f : α ↪ β) (s : finset α) (x : s) : f x ∈ s.map f :=
mem_map_of_mem f x.prop
@[simp, norm_cast] theorem coe_map (f : α ↪ β) (s : finset α) : (s.map f : set β) = f '' s :=
set.ext $ λ x, mem_map.trans set.mem_image_iff_bex.symm
theorem coe_map_subset_range (f : α ↪ β) (s : finset α) : (s.map f : set β) ⊆ set.range f :=
calc ↑(s.map f) = f '' s : coe_map f s
... ⊆ set.range f : set.image_subset_range f ↑s
/-- If the only elements outside `s` are those left fixed by `σ`, then mapping by `σ` has no effect.
-/
lemma map_perm {σ : equiv.perm α} (hs : {a | σ a ≠ a} ⊆ s) : s.map (σ : α ↪ α) = s :=
coe_injective $ (coe_map _ _).trans $ set.image_perm hs
theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} :
s.to_finset.map f = (s.map f).to_finset :=
ext $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset]
@[simp] theorem map_refl : s.map (embedding.refl _) = s :=
ext $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right
@[simp] theorem map_cast_heq {α β} (h : α = β) (s : finset α) :
s.map (equiv.cast h).to_embedding == s :=
by { subst h, simp }
theorem map_map (f : α ↪ β) (g : β ↪ γ) (s : finset α) : (s.map f).map g = s.map (f.trans g) :=
eq_of_veq $ by simp only [map_val, multiset.map_map]; refl
lemma map_comm {β'} {f : β ↪ γ} {g : α ↪ β} {f' : α ↪ β'} {g' : β' ↪ γ}
(h_comm : ∀ a, f (g a) = g' (f' a)) :
(s.map g).map f = (s.map f').map g' :=
by simp_rw [map_map, embedding.trans, function.comp, h_comm]
lemma _root_.function.semiconj.finset_map {f : α ↪ β} {ga : α ↪ α} {gb : β ↪ β}
(h : function.semiconj f ga gb) :
function.semiconj (map f) (map ga) (map gb) :=
λ s, map_comm h
lemma _root_.function.commute.finset_map {f g : α ↪ α} (h : function.commute f g) :
function.commute (map f) (map g) :=
h.finset_map
@[simp] theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ :=
⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs,
λ h, by simp [subset_def, map_subset_map h]⟩
/-- Associate to an embedding `f` from `α` to `β` the order embedding that maps a finset to its
image under `f`. -/
def map_embedding (f : α ↪ β) : finset α ↪o finset β :=
order_embedding.of_map_le_iff (map f) (λ _ _, map_subset_map)
@[simp] theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ :=
(map_embedding f).injective.eq_iff
lemma map_injective (f : α ↪ β) : injective (map f) := (map_embedding f).injective
@[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl
lemma filter_map {p : β → Prop} [decidable_pred p] :
(s.map f).filter p = (s.filter (p ∘ f)).map f :=
eq_of_veq (map_filter _ _ _)
lemma map_filter {f : α ≃ β} {p : α → Prop} [decidable_pred p] :
(s.filter p).map f.to_embedding = (s.map f.to_embedding).filter (p ∘ f.symm) :=
by simp only [filter_map, function.comp, equiv.to_embedding_apply, equiv.symm_apply_apply]
@[simp] lemma disjoint_map {s t : finset α} (f : α ↪ β) :
disjoint (s.map f) (t.map f) ↔ disjoint s t :=
begin
simp only [disjoint_iff_ne, mem_map, exists_prop, exists_imp_distrib, and_imp],
refine ⟨λ h a ha b hb hab, h _ _ ha rfl _ _ hb rfl $ congr_arg _ hab, _⟩,
rintro h _ a ha rfl _ b hb rfl,
exact f.injective.ne (h _ ha _ hb),
end
theorem map_disj_union {f : α ↪ β} (s₁ s₂ : finset α) (h) (h' := (disjoint_map _).mpr h) :
(s₁.disj_union s₂ h).map f = (s₁.map f).disj_union (s₂.map f) h' :=
eq_of_veq $ multiset.map_add _ _ _
/-- A version of `finset.map_disj_union` for writing in the other direction. -/
theorem map_disj_union' {f : α ↪ β} (s₁ s₂ : finset α) (h') (h := (disjoint_map _).mp h') :
(s₁.disj_union s₂ h).map f = (s₁.map f).disj_union (s₂.map f) h' :=
map_disj_union _ _ _
theorem map_union [decidable_eq α] [decidable_eq β]
{f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f :=
coe_injective $ by simp only [coe_map, coe_union, set.image_union]
theorem map_inter [decidable_eq α] [decidable_eq β]
{f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f :=
coe_injective $ by simp only [coe_map, coe_inter, set.image_inter f.injective]
@[simp] theorem map_singleton (f : α ↪ β) (a : α) : map f {a} = {f a} :=
coe_injective $ by simp only [coe_map, coe_singleton, set.image_singleton]
@[simp] lemma map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) :
(insert a s).map f = insert (f a) (s.map f) :=
by simp only [insert_eq, map_union, map_singleton]
@[simp] lemma map_cons (f : α ↪ β) (a : α) (s : finset α) (ha : a ∉ s) :
(cons a s ha).map f = cons (f a) (s.map f) (by simpa using ha) :=
eq_of_veq $ multiset.map_cons f a s.val
@[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ :=
⟨λ h, eq_empty_of_forall_not_mem $
λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩
@[simp] lemma map_nonempty : (s.map f).nonempty ↔ s.nonempty :=
by rw [nonempty_iff_ne_empty, nonempty_iff_ne_empty, ne.def, map_eq_empty]
alias map_nonempty ↔ _ nonempty.map
lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s :=
eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _
lemma disjoint_range_add_left_embedding (a b : ℕ) :
disjoint (range a) (map (add_left_embedding a) (range b)) :=
begin
refine disjoint_iff_inf_le.mpr _,
intros k hk,
simp only [exists_prop, mem_range, inf_eq_inter, mem_map, add_left_embedding_apply,
mem_inter] at hk,
obtain ⟨a, haQ, ha⟩ := hk.2,
simpa [← ha] using hk.1,
end
lemma disjoint_range_add_right_embedding (a b : ℕ) :
disjoint (range a) (map (add_right_embedding a) (range b)) :=
begin
refine disjoint_iff_inf_le.mpr _,
intros k hk,
simp only [exists_prop, mem_range, inf_eq_inter, mem_map, add_left_embedding_apply,
mem_inter] at hk,
obtain ⟨a, haQ, ha⟩ := hk.2,
simpa [← ha] using hk.1,
end
end map
lemma range_add_one' (n : ℕ) :
range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ.inj⟩) :=
by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n]
/-! ### image -/
section image
variables [decidable_eq β]
/-- `image f s` is the forward image of `s` under `f`. -/
def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset
@[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).dedup := rfl
@[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl
variables {f g : α → β} {s : finset α} {t : finset β} {a : α} {b c : β}
@[simp] lemma mem_image : b ∈ s.image f ↔ ∃ a ∈ s, f a = b :=
by simp only [mem_def, image_val, mem_dedup, multiset.mem_map, exists_prop]
lemma mem_image_of_mem (f : α → β) {a} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩
@[simp] lemma mem_image_const : c ∈ s.image (const α b) ↔ s.nonempty ∧ b = c :=
by { rw mem_image, simp only [exists_prop, const_apply, exists_and_distrib_right], refl }
lemma mem_image_const_self : b ∈ s.image (const α b) ↔ s.nonempty :=
mem_image_const.trans $ and_iff_left rfl
instance can_lift (c) (p) [can_lift β α c p] :
can_lift (finset β) (finset α) (image c) (λ s, ∀ x ∈ s, p x) :=
{ prf :=
begin
rintro ⟨⟨l⟩, hd : l.nodup⟩ hl,
lift l to list α using hl,
exact ⟨⟨l, hd.of_map _⟩, ext $ λ a, by simp⟩,
end }
lemma image_congr (h : (s : set α).eq_on f g) : finset.image f s = finset.image g s :=
by { ext, simp_rw mem_image, exact bex_congr (λ x hx, by rw h hx) }
lemma _root_.function.injective.mem_finset_image (hf : injective f) : f a ∈ s.image f ↔ a ∈ s :=
begin
refine ⟨λ h, _, finset.mem_image_of_mem f⟩,
obtain ⟨y, hy, heq⟩ := mem_image.1 h,
exact hf heq ▸ hy,
end
lemma filter_mem_image_eq_image (f : α → β) (s : finset α) (t : finset β) (h : ∀ x ∈ s, f x ∈ t) :
t.filter (λ y, y ∈ s.image f) = s.image f :=
by { ext, rw [mem_filter, mem_image],
simp only [and_imp, exists_prop, and_iff_right_iff_imp, exists_imp_distrib],
rintros x xel rfl, exact h _ xel }
lemma fiber_nonempty_iff_mem_image (f : α → β) (s : finset α) (y : β) :
(s.filter (λ x, f x = y)).nonempty ↔ y ∈ s.image f :=
by simp [finset.nonempty]
@[simp, norm_cast] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s :=
set.ext $ λ _, mem_image.trans set.mem_image_iff_bex.symm
protected lemma nonempty.image (h : s.nonempty) (f : α → β) : (s.image f).nonempty :=
let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩
@[simp] lemma nonempty.image_iff (f : α → β) : (s.image f).nonempty ↔ s.nonempty :=
⟨λ ⟨y, hy⟩, let ⟨x, hx, _⟩ := mem_image.mp hy in ⟨x, hx⟩, λ h, h.image f⟩
theorem image_to_finset [decidable_eq α] {s : multiset α} :
s.to_finset.image f = (s.map f).to_finset :=
ext $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map]
lemma image_val_of_inj_on (H : set.inj_on f s) : (image f s).1 = s.1.map f := (s.2.map_on H).dedup
@[simp] lemma image_id [decidable_eq α] : s.image id = s :=
ext $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right]
@[simp] theorem image_id' [decidable_eq α] : s.image (λ x, x) = s := image_id
theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) :=
eq_of_veq $ by simp only [image_val, dedup_map_dedup_eq, multiset.map_map]
lemma image_comm {β'} [decidable_eq β'] [decidable_eq γ] {f : β → γ} {g : α → β}
{f' : α → β'} {g' : β' → γ} (h_comm : ∀ a, f (g a) = g' (f' a)) :
(s.image g).image f = (s.image f').image g' :=
by simp_rw [image_image, comp, h_comm]
lemma _root_.function.semiconj.finset_image [decidable_eq α] {f : α → β} {ga : α → α} {gb : β → β}
(h : function.semiconj f ga gb) :
function.semiconj (image f) (image ga) (image gb) :=
λ s, image_comm h
lemma _root_.function.commute.finset_image [decidable_eq α] {f g : α → α}
(h : function.commute f g) :
function.commute (image f) (image g) :=
h.finset_image
theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f :=
by simp only [subset_def, image_val, subset_dedup', dedup_subset',
multiset.map_subset_map h]
lemma image_subset_iff : s.image f ⊆ t ↔ ∀ x ∈ s, f x ∈ t :=
calc s.image f ⊆ t ↔ f '' ↑s ⊆ ↑t : by norm_cast
... ↔ _ : set.image_subset_iff
theorem image_mono (f : α → β) : monotone (finset.image f) := λ _ _, image_subset_image
lemma image_subset_image_iff {t : finset α} (hf : injective f) : s.image f ⊆ t.image f ↔ s ⊆ t :=
by { simp_rw ←coe_subset, push_cast, exact set.image_subset_image_iff hf }
theorem coe_image_subset_range : ↑(s.image f) ⊆ set.range f :=
calc ↑(s.image f) = f '' ↑s : coe_image
... ⊆ set.range f : set.image_subset_range f ↑s
theorem image_filter {p : β → Prop} [decidable_pred p] :
(s.image f).filter p = (s.filter (p ∘ f)).image f :=
ext $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact
⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩,
by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩
theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) :
(s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f :=
ext $ λ _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right,
exists_or_distrib]
lemma image_inter_subset [decidable_eq α] (f : α → β) (s t : finset α) :
(s ∩ t).image f ⊆ s.image f ∩ t.image f :=
subset_inter (image_subset_image $ inter_subset_left _ _) $
image_subset_image $ inter_subset_right _ _
lemma image_inter_of_inj_on [decidable_eq α] {f : α → β} (s t : finset α)
(hf : set.inj_on f (s ∪ t)) :
(s ∩ t).image f = s.image f ∩ t.image f :=
(image_inter_subset _ _ _).antisymm $ λ x, begin
simp only [mem_inter, mem_image],
rintro ⟨⟨a, ha, rfl⟩, b, hb, h⟩,
exact ⟨a, ⟨ha, by rwa ←hf (or.inr hb) (or.inl ha) h⟩, rfl⟩,
end
lemma image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : injective f) :
(s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f :=
image_inter_of_inj_on _ _ $ hf.inj_on _
@[simp] theorem image_singleton (f : α → β) (a : α) : image f {a} = {f a} :=
ext $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm
@[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) :
(insert a s).image f = insert (f a) (s.image f) :=
by simp only [insert_eq, image_singleton, image_union]
lemma erase_image_subset_image_erase [decidable_eq α] (f : α → β) (s : finset α) (a : α) :
(s.image f).erase (f a) ⊆ (s.erase a).image f :=
begin
simp only [subset_iff, and_imp, exists_prop, mem_image, exists_imp_distrib, mem_erase],
rintro b hb x hx rfl,
exact ⟨_, ⟨ne_of_apply_ne f hb, hx⟩, rfl⟩,
end
@[simp] lemma image_erase [decidable_eq α] {f : α → β} (hf : injective f) (s : finset α) (a : α) :
(s.erase a).image f = (s.image f).erase (f a) :=
begin
refine (erase_image_subset_image_erase _ _ _).antisymm' (λ b, _),
simp only [mem_image, exists_prop, mem_erase],
rintro ⟨a', ⟨haa', ha'⟩, rfl⟩,
exact ⟨hf.ne haa', a', ha', rfl⟩,
end
@[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ :=
⟨λ h, eq_empty_of_forall_not_mem $
λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩
@[simp] lemma _root_.disjoint.of_image_finset
{s t : finset α} {f : α → β} (h : disjoint (s.image f) (t.image f)) :
disjoint s t :=
disjoint_iff_ne.2 $ λ a ha b hb, ne_of_apply_ne f $ h.forall_ne_finset
(mem_image_of_mem _ ha) (mem_image_of_mem _ hb)
lemma mem_range_iff_mem_finset_range_of_mod_eq' [decidable_eq α] {f : ℕ → α} {a : α} {n : ℕ}
(hn : 0 < n) (h : ∀ i, f (i % n) = f i) :
a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) :=
begin
split,
{ rintros ⟨i, hi⟩,
simp only [mem_image, exists_prop, mem_range],
exact ⟨i % n, nat.mod_lt i hn, (rfl.congr hi).mp (h i)⟩ },
{ rintro h,
simp only [mem_image, exists_prop, set.mem_range, mem_range] at *,
rcases h with ⟨i, hi, ha⟩,
exact ⟨i, ha⟩ }
end
lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ}
(hn : 0 < n) (h : ∀ i, f (i % n) = f i) :
a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) :=
suffices (∃ i, f (i % n) = a) ↔ ∃ i, i < n ∧ f ↑i = a, by simpa [h],
have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn,
iff.intro
(assume ⟨i, hi⟩,
have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'),
⟨int.to_nat (i % n),
by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩)
(assume ⟨i, hi, ha⟩,
⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩)
lemma range_add (a b : ℕ) : range (a + b) = range a ∪ (range b).map (add_left_embedding a) :=
by { rw [←val_inj, union_val], exact multiset.range_add_eq_union a b }
@[simp] lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s :=
eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, dedup_eq_self]
@[simp] lemma attach_image_coe [decidable_eq α] {s : finset α} : s.attach.image coe = s :=
finset.attach_image_val
@[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} :
attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s})
((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) :=
ext $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx)
(λ h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h)
(λ h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩),
λ _, finset.mem_attach _ _⟩
theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f :=
eq_of_veq (s.map f).2.dedup.symm
@[simp] lemma disjoint_image
{s t : finset α} {f : α → β} (hf : injective f) :
disjoint (s.image f) (t.image f) ↔ disjoint s t :=
by convert disjoint_map ⟨_, hf⟩; simp [map_eq_image]
lemma image_const {s : finset α} (h : s.nonempty) (b : β) : s.image (λa, b) = singleton b :=
ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right,
h.bex, true_and, mem_singleton, eq_comm]
@[simp] lemma map_erase [decidable_eq α] (f : α ↪ β) (s : finset α) (a : α) :
(s.erase a).map f = (s.map f).erase (f a) :=
by { simp_rw map_eq_image, exact s.image_erase f.2 a }
/-! ### Subtype -/
/-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose
elements belong to `s`. -/
protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) :=
(s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩,
λ x y H, subtype.eq $ subtype.mk.inj H⟩
@[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} :
∀ {a : subtype p}, a ∈ s.subtype p ↔ (a : α) ∈ s
| ⟨a, ha⟩ := by simp [finset.subtype, ha]
lemma subtype_eq_empty {p : α → Prop} [decidable_pred p] {s : finset α} :
s.subtype p = ∅ ↔ ∀ x, p x → x ∉ s :=
by simp [ext_iff, subtype.forall, subtype.coe_mk]; refl
@[mono] lemma subtype_mono {p : α → Prop} [decidable_pred p] : monotone (finset.subtype p) :=
λ s t h x hx, mem_subtype.2 $ h $ mem_subtype.1 hx
/-- `s.subtype p` converts back to `s.filter p` with
`embedding.subtype`. -/
@[simp] lemma subtype_map (p : α → Prop) [decidable_pred p] :
(s.subtype p).map (embedding.subtype _) = s.filter p :=
begin
ext x,
simp [and_comm _ (_ = _), @and.left_comm _ (_ = _), and_comm (p x) (x ∈ s)]
end
/-- If all elements of a `finset` satisfy the predicate `p`,
`s.subtype p` converts back to `s` with `embedding.subtype`. -/
lemma subtype_map_of_mem {p : α → Prop} [decidable_pred p] (h : ∀ x ∈ s, p x) :
(s.subtype p).map (embedding.subtype _) = s :=
by rw [subtype_map, filter_true_of_mem h]
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, all elements of the result have the property of
the subtype. -/
lemma property_of_mem_map_subtype {p : α → Prop} (s : finset {x // p x}) {a : α}
(h : a ∈ s.map (embedding.subtype _)) : p a :=
begin
rcases mem_map.1 h with ⟨x, hx, rfl⟩,
exact x.2
end
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, the result does not contain any value that does
not satisfy the property of the subtype. -/
lemma not_mem_map_subtype_of_not_property {p : α → Prop} (s : finset {x // p x})
{a : α} (h : ¬ p a) : a ∉ (s.map (embedding.subtype _)) :=
mt s.property_of_mem_map_subtype h
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, the result is a subset of the set giving the
subtype. -/
lemma map_subtype_subset {t : set α} (s : finset t) : ↑(s.map (embedding.subtype _)) ⊆ t :=
begin
intros a ha,
rw mem_coe at ha,
convert property_of_mem_map_subtype s ha
end
/-! ### Fin -/
/--
Given a finset `s` of natural numbers and a bound `n`,
`s.fin n` is the finset of all elements of `s` less than `n`.
-/
protected def fin (n : ℕ) (s : finset ℕ) : finset (fin n) :=
(s.subtype _).map fin.equiv_subtype.symm.to_embedding
@[simp] lemma mem_fin {n} {s : finset ℕ} :
∀ a : fin n, a ∈ s.fin n ↔ (a : ℕ) ∈ s
| ⟨a, ha⟩ := by simp [finset.fin]
@[mono] lemma fin_mono {n} : monotone (finset.fin n) :=
λ s t h x, by simpa using @h x
@[simp] lemma fin_map {n} {s : finset ℕ} : (s.fin n).map fin.coe_embedding = s.filter (< n) :=
by simp [finset.fin, finset.map_map]
lemma subset_image_iff {s : set α} : ↑t ⊆ f '' s ↔ ∃ s' : finset α, ↑s' ⊆ s ∧ s'.image f = t :=
begin
split, swap,
{ rintro ⟨t, ht, rfl⟩, rw [coe_image], exact set.image_subset f ht },
intro h,
letI : can_lift β s (f ∘ coe) (λ y, y ∈ f '' s) := ⟨λ y ⟨x, hxt, hy⟩, ⟨⟨x, hxt⟩, hy⟩⟩,
lift t to finset s using h,
refine ⟨t.map (embedding.subtype _), map_subtype_subset _, _⟩,
ext y, simp
end
lemma range_sdiff_zero {n : ℕ} : range (n + 1) \ {0} = (range n).image nat.succ :=
begin
induction n with k hk,
{ simp },
nth_rewrite 1 range_succ,
rw [range_succ, image_insert, ←hk, insert_sdiff_of_not_mem],
simp
end
end image
lemma _root_.multiset.to_finset_map [decidable_eq α] [decidable_eq β] (f : α → β) (m : multiset α) :
(m.map f).to_finset = m.to_finset.image f :=
finset.val_inj.1 (multiset.dedup_map_dedup_eq _ _).symm
section to_list
/-- Produce a list of the elements in the finite set using choice. -/
noncomputable def to_list (s : finset α) : list α := s.1.to_list
lemma nodup_to_list (s : finset α) : s.to_list.nodup :=
by { rw [to_list, ←multiset.coe_nodup, multiset.coe_to_list], exact s.nodup }
@[simp] lemma mem_to_list {a : α} {s : finset α} : a ∈ s.to_list ↔ a ∈ s := mem_to_list
@[simp] lemma to_list_eq_nil {s : finset α} : s.to_list = [] ↔ s = ∅ :=
to_list_eq_nil.trans val_eq_zero
@[simp] lemma empty_to_list {s : finset α} : s.to_list.empty ↔ s = ∅ :=
list.empty_iff_eq_nil.trans to_list_eq_nil
@[simp] lemma to_list_empty : (∅ : finset α).to_list = [] := to_list_eq_nil.mpr rfl
lemma nonempty.to_list_ne_nil {s : finset α} (hs : s.nonempty) : s.to_list ≠ [] :=
mt to_list_eq_nil.mp hs.ne_empty
lemma nonempty.not_empty_to_list {s : finset α} (hs : s.nonempty) : ¬s.to_list.empty :=
mt empty_to_list.mp hs.ne_empty
@[simp, norm_cast]
lemma coe_to_list (s : finset α) : (s.to_list : multiset α) = s.val := s.val.coe_to_list
@[simp] lemma to_list_to_finset [decidable_eq α] (s : finset α) : s.to_list.to_finset = s :=
by { ext, simp }
lemma exists_list_nodup_eq [decidable_eq α] (s : finset α) :
∃ (l : list α), l.nodup ∧ l.to_finset = s :=
⟨s.to_list, s.nodup_to_list, s.to_list_to_finset⟩
lemma to_list_cons {a : α} {s : finset α} (h : a ∉ s) : (cons a s h).to_list ~ a :: s.to_list :=
(list.perm_ext (nodup_to_list _) (by simp [h, nodup_to_list s])).2 $
λ x, by simp only [list.mem_cons_iff, finset.mem_to_list, finset.mem_cons]
lemma to_list_insert [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) :
(insert a s).to_list ~ a :: s.to_list :=
cons_eq_insert _ _ h ▸ to_list_cons _
end to_list
/-!
### disj_Union
This section is about the bounded union of a disjoint indexed family `t : α → finset β` of finite
sets over a finite set `s : finset α`. In most cases `finset.bUnion` should be preferred.
-/
section disj_Union
variables {s s₁ s₂ : finset α} {t t₁ t₂ : α → finset β}
/-- `disj_Union s f h` is the set such that `a ∈ disj_Union s f` iff `a ∈ f i` for some `i ∈ s`.
It is the same as `s.bUnion f`, but it does not require decidable equality on the type. The
hypothesis ensures that the sets are disjoint. -/
def disj_Union (s : finset α) (t : α → finset β)
(hf : (s : set α).pairwise_disjoint t) : finset β :=
⟨(s.val.bind (finset.val ∘ t)), multiset.nodup_bind.mpr
⟨λ a ha, (t a).nodup, s.nodup.pairwise $ λ a ha b hb hab, finset.disjoint_val.1 $ hf ha hb hab⟩⟩
@[simp] theorem disj_Union_val (s : finset α) (t : α → finset β) (h) :
(s.disj_Union t h).1 = (s.1.bind (λ a, (t a).1)) := rfl
@[simp] theorem disj_Union_empty (t : α → finset β) : disj_Union ∅ t (by simp) = ∅ := rfl
@[simp] lemma mem_disj_Union {b : β} {h} :
b ∈ s.disj_Union t h ↔ ∃ a ∈ s, b ∈ t a :=
by simp only [mem_def, disj_Union_val, mem_bind, exists_prop]
@[simp, norm_cast] lemma coe_disj_Union {h} : (s.disj_Union t h : set β) = ⋃ x ∈ (s : set α), t x :=
by simp only [set.ext_iff, mem_disj_Union, set.mem_Union, iff_self, mem_coe, implies_true_iff]
@[simp] theorem disj_Union_cons (a : α) (s : finset α) (ha : a ∉ s) (f : α → finset β) (H) :
disj_Union (cons a s ha) f H = (f a).disj_union
(s.disj_Union f $
λ b hb c hc, H (mem_cons_of_mem hb) (mem_cons_of_mem hc))
(disjoint_left.mpr $ λ b hb h, let ⟨c, hc, h⟩ := mem_disj_Union.mp h in
disjoint_left.mp
(H (mem_cons_self a s) (mem_cons_of_mem hc) (ne_of_mem_of_not_mem hc ha).symm) hb h)
:=
eq_of_veq $ multiset.cons_bind _ _ _
@[simp] lemma singleton_disj_Union (a : α) {h} : finset.disj_Union {a} t h = t a :=
eq_of_veq $ multiset.singleton_bind _ _
theorem map_disj_Union {f : α ↪ β} {s : finset α} {t : β → finset γ} {h} :
(s.map f).disj_Union t h = s.disj_Union (λa, t (f a))
(λ a ha b hb hab, h (mem_map_of_mem _ ha) (mem_map_of_mem _ hb) (f.injective.ne hab)) :=
eq_of_veq $ multiset.bind_map _ _ _
theorem disj_Union_map {s : finset α} {t : α → finset β} {f : β ↪ γ} {h} :
(s.disj_Union t h).map f = s.disj_Union (λa, (t a).map f)
(λ a ha b hb hab, disjoint_left.mpr $ λ x hxa hxb, begin
obtain ⟨xa, hfa, rfl⟩ := mem_map.mp hxa,
obtain ⟨xb, hfb, hfab⟩ := mem_map.mp hxb,
obtain rfl := f.injective hfab,
exact disjoint_left.mp (h ha hb hab) hfa hfb,
end) :=
eq_of_veq $ multiset.map_bind _ _ _
lemma disj_Union_disj_Union (s : finset α) (f : α → finset β) (g : β → finset γ) (h1 h2) :
(s.disj_Union f h1).disj_Union g h2 =
s.attach.disj_Union (λ a, (f a).disj_Union g $
λ b hb c hc, h2 (mem_disj_Union.mpr ⟨_, a.prop, hb⟩) (mem_disj_Union.mpr ⟨_, a.prop, hc⟩))
(λ a ha b hb hab, disjoint_left.mpr $ λ x hxa hxb, begin
obtain ⟨xa, hfa, hga⟩ := mem_disj_Union.mp hxa,
obtain ⟨xb, hfb, hgb⟩ := mem_disj_Union.mp hxb,
refine disjoint_left.mp (h2
(mem_disj_Union.mpr ⟨_, a.prop, hfa⟩) (mem_disj_Union.mpr ⟨_, b.prop, hfb⟩) _) hga hgb,
rintro rfl,
exact disjoint_left.mp (h1 a.prop b.prop $ subtype.coe_injective.ne hab) hfa hfb,
end) :=
eq_of_veq $ multiset.bind_assoc.trans (multiset.attach_bind_coe _ _).symm
end disj_Union
section bUnion
/-!
### bUnion
This section is about the bounded union of an indexed family `t : α → finset β` of finite sets
over a finite set `s : finset α`.
-/
variables [decidable_eq β] {s s₁ s₂ : finset α} {t t₁ t₂ : α → finset β}
/-- `bUnion s t` is the union of `t x` over `x ∈ s`.
(This was formerly `bind` due to the monad structure on types with `decidable_eq`.) -/
protected def bUnion (s : finset α) (t : α → finset β) : finset β :=
(s.1.bind (λ a, (t a).1)).to_finset
@[simp] theorem bUnion_val (s : finset α) (t : α → finset β) :
(s.bUnion t).1 = (s.1.bind (λ a, (t a).1)).dedup := rfl
@[simp] theorem bUnion_empty : finset.bUnion ∅ t = ∅ := rfl
@[simp] lemma mem_bUnion {b : β} : b ∈ s.bUnion t ↔ ∃ a ∈ s, b ∈ t a :=
by simp only [mem_def, bUnion_val, mem_dedup, mem_bind, exists_prop]
@[simp, norm_cast] lemma coe_bUnion : (s.bUnion t : set β) = ⋃ x ∈ (s : set α), t x :=
by simp only [set.ext_iff, mem_bUnion, set.mem_Union, iff_self, mem_coe, implies_true_iff]
@[simp] theorem bUnion_insert [decidable_eq α] {a : α} : (insert a s).bUnion t = t a ∪ s.bUnion t :=
ext $ λ x, by simp only [mem_bUnion, exists_prop, mem_union, mem_insert,
or_and_distrib_right, exists_or_distrib, exists_eq_left]
-- ext $ λ x, by simp [or_and_distrib_right, exists_or_distrib]
lemma bUnion_congr (hs : s₁ = s₂) (ht : ∀ a ∈ s₁, t₁ a = t₂ a) : s₁.bUnion t₁ = s₂.bUnion t₂ :=
ext $ λ x, by simp [hs, ht] { contextual := tt }
@[simp] lemma disj_Union_eq_bUnion (s : finset α) (f : α → finset β) (hf) :
s.disj_Union f hf = s.bUnion f :=
begin
dsimp [disj_Union, finset.bUnion, function.comp],
generalize_proofs h,
exact eq_of_veq h.dedup.symm,
end
theorem bUnion_subset {s' : finset β} : s.bUnion t ⊆ s' ↔ ∀ x ∈ s, t x ⊆ s' :=
by simp only [subset_iff, mem_bUnion]; exact
⟨λ H a ha b hb, H ⟨a, ha, hb⟩, λ H b ⟨a, ha, hb⟩, H a ha hb⟩
@[simp] lemma singleton_bUnion {a : α} : finset.bUnion {a} t = t a :=
by { classical, rw [← insert_emptyc_eq, bUnion_insert, bUnion_empty, union_empty] }
theorem bUnion_inter (s : finset α) (f : α → finset β) (t : finset β) :
s.bUnion f ∩ t = s.bUnion (λ x, f x ∩ t) :=
begin
ext x,
simp only [mem_bUnion, mem_inter],
tauto
end
theorem inter_bUnion (t : finset β) (s : finset α) (f : α → finset β) :
t ∩ s.bUnion f = s.bUnion (λ x, t ∩ f x) :=
by rw [inter_comm, bUnion_inter]; simp [inter_comm]
theorem image_bUnion [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} :
(s.image f).bUnion t = s.bUnion (λa, t (f a)) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s rfl (λ a s has ih,
by simp only [image_insert, bUnion_insert, ih])
theorem bUnion_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} :
(s.bUnion t).image f = s.bUnion (λa, (t a).image f) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s rfl (λ a s has ih,
by simp only [bUnion_insert, image_union, ih])
lemma bUnion_bUnion [decidable_eq γ] (s : finset α) (f : α → finset β) (g : β → finset γ) :
(s.bUnion f).bUnion g = s.bUnion (λ a, (f a).bUnion g) :=
begin
ext,
simp only [finset.mem_bUnion, exists_prop],
simp_rw [←exists_and_distrib_right, ←exists_and_distrib_left, and_assoc],
rw exists_comm,
end
theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) :
(s.bind t).to_finset = s.to_finset.bUnion (λa, (t a).to_finset) :=
ext $ λ x, by simp only [multiset.mem_to_finset, mem_bUnion, multiset.mem_bind, exists_prop]
lemma bUnion_mono (h : ∀ a ∈ s, t₁ a ⊆ t₂ a) : s.bUnion t₁ ⊆ s.bUnion t₂ :=
have ∀ b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a),
from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩,
by simpa only [subset_iff, mem_bUnion, exists_imp_distrib, and_imp, exists_prop]
lemma bUnion_subset_bUnion_of_subset_left (t : α → finset β) (h : s₁ ⊆ s₂) :
s₁.bUnion t ⊆ s₂.bUnion t :=
begin
intro x,
simp only [and_imp, mem_bUnion, exists_prop],
exact Exists.imp (λ a ha, ⟨h ha.1, ha.2⟩)
end
lemma subset_bUnion_of_mem (u : α → finset β) {x : α} (xs : x ∈ s) : u x ⊆ s.bUnion u :=
singleton_bUnion.superset.trans $ bUnion_subset_bUnion_of_subset_left u $ singleton_subset_iff.2 xs
@[simp] lemma bUnion_subset_iff_forall_subset {α β : Type*} [decidable_eq β]
{s : finset α} {t : finset β} {f : α → finset β} : s.bUnion f ⊆ t ↔ ∀ x ∈ s, f x ⊆ t :=
⟨λ h x hx, (subset_bUnion_of_mem f hx).trans h,
λ h x hx, let ⟨a, ha₁, ha₂⟩ := mem_bUnion.mp hx in h _ ha₁ ha₂⟩
lemma bUnion_singleton {f : α → β} : s.bUnion (λa, {f a}) = s.image f :=
ext $ λ x, by simp only [mem_bUnion, mem_image, mem_singleton, eq_comm]
@[simp] lemma bUnion_singleton_eq_self [decidable_eq α] : s.bUnion (singleton : α → finset α) = s :=
by { rw bUnion_singleton, exact image_id }
lemma filter_bUnion (s : finset α) (f : α → finset β) (p : β → Prop) [decidable_pred p] :
(s.bUnion f).filter p = s.bUnion (λ a, (f a).filter p) :=
begin
ext b,
simp only [mem_bUnion, exists_prop, mem_filter],
split,
{ rintro ⟨⟨a, ha, hba⟩, hb⟩,
exact ⟨a, ha, hba, hb⟩ },
{ rintro ⟨a, ha, hba, hb⟩,
exact ⟨⟨a, ha, hba⟩, hb⟩ }
end
lemma bUnion_filter_eq_of_maps_to [decidable_eq α] {s : finset α} {t : finset β} {f : α → β}
(h : ∀ x ∈ s, f x ∈ t) :
t.bUnion (λa, s.filter $ (λc, f c = a)) = s :=
ext $ λ b, by simpa using h b
lemma image_bUnion_filter_eq [decidable_eq α] (s : finset β) (g : β → α) :
(s.image g).bUnion (λa, s.filter $ (λc, g c = a)) = s :=
bUnion_filter_eq_of_maps_to (λ x, mem_image_of_mem g)
lemma erase_bUnion (f : α → finset β) (s : finset α) (b : β) :
(s.bUnion f).erase b = s.bUnion (λ x, (f x).erase b) :=
by { ext, simp only [finset.mem_bUnion, iff_self, exists_and_distrib_left, finset.mem_erase] }
@[simp] lemma bUnion_nonempty : (s.bUnion t).nonempty ↔ ∃ x ∈ s, (t x).nonempty :=
by simp [finset.nonempty, ← exists_and_distrib_left, @exists_swap α]
lemma nonempty.bUnion (hs : s.nonempty) (ht : ∀ x ∈ s, (t x).nonempty) : (s.bUnion t).nonempty :=
bUnion_nonempty.2 $ hs.imp $ λ x hx, ⟨hx, ht x hx⟩
lemma disjoint_bUnion_left (s : finset α) (f : α → finset β) (t : finset β) :
disjoint (s.bUnion f) t ↔ (∀ i ∈ s, disjoint (f i) t) :=
begin
classical,
refine s.induction _ _,
{ simp only [forall_mem_empty_iff, bUnion_empty, disjoint_empty_left] },
{ assume i s his ih,
simp only [disjoint_union_left, bUnion_insert, his, forall_mem_insert, ih] }
end
lemma disjoint_bUnion_right (s : finset β) (t : finset α) (f : α → finset β) :
disjoint s (t.bUnion f) ↔ ∀ i ∈ t, disjoint s (f i) :=
by simpa only [disjoint.comm] using disjoint_bUnion_left t f s
end bUnion
/-! ### choose -/
section choose
variables (p : α → Prop) [decidable_pred p] (l : finset α)
/-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of
`l` satisfying `p` this unique element, as an element of the corresponding subtype. -/
def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } :=
multiset.choose_x p l.val hp
/-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of
`l` satisfying `p` this unique element, as an element of the ambient type. -/
def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp
lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1
lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
section pairwise
variables {s : finset α}
lemma pairwise_subtype_iff_pairwise_finset' (r : β → β → Prop) (f : α → β) :
pairwise (r on λ x : s, f x) ↔ (s : set α).pairwise (r on f) :=
pairwise_subtype_iff_pairwise_set (s : set α) (r on f)
lemma pairwise_subtype_iff_pairwise_finset (r : α → α → Prop) :
pairwise (r on λ x : s, x) ↔ (s : set α).pairwise r :=
pairwise_subtype_iff_pairwise_finset' r id
lemma pairwise_cons' {a : α} (ha : a ∉ s) (r : β → β → Prop) (f : α → β) :
pairwise (r on λ a : s.cons a ha, f a) ↔
pairwise (r on λ a : s, f a) ∧ ∀ b ∈ s, r (f a) (f b) ∧ r (f b) (f a) :=
begin
simp only [pairwise_subtype_iff_pairwise_finset', finset.coe_cons, set.pairwise_insert,
finset.mem_coe, and.congr_right_iff],
exact λ hsr, ⟨λ h b hb, h b hb $ by { rintro rfl, contradiction }, λ h b hb _, h b hb⟩,
end
lemma pairwise_cons {a : α} (ha : a ∉ s) (r : α → α → Prop) :
pairwise (r on λ a : s.cons a ha, a) ↔ pairwise (r on λ a : s, a) ∧ ∀ b ∈ s, r a b ∧ r b a :=
pairwise_cons' ha r id
end pairwise
end finset
namespace equiv
/-- Given an equivalence `α` to `β`, produce an equivalence between `finset α` and `finset β`. -/
protected def finset_congr (e : α ≃ β) : finset α ≃ finset β :=
{ to_fun := λ s, s.map e.to_embedding,
inv_fun := λ s, s.map e.symm.to_embedding,
left_inv := λ s, by simp [finset.map_map],
right_inv := λ s, by simp [finset.map_map] }
@[simp] lemma finset_congr_apply (e : α ≃ β) (s : finset α) :
e.finset_congr s = s.map e.to_embedding :=
rfl
@[simp] lemma finset_congr_refl : (equiv.refl α).finset_congr = equiv.refl _ := by { ext, simp }
@[simp] lemma finset_congr_symm (e : α ≃ β) : e.finset_congr.symm = e.symm.finset_congr := rfl
@[simp] lemma finset_congr_trans (e : α ≃ β) (e' : β ≃ γ) :
e.finset_congr.trans (e'.finset_congr) = (e.trans e').finset_congr :=
by { ext, simp [-finset.mem_map, -equiv.trans_to_embedding] }
lemma finset_congr_to_embedding (e : α ≃ β) :
e.finset_congr.to_embedding = (finset.map_embedding e.to_embedding).to_embedding := rfl
/--
Inhabited types are equivalent to `option β` for some `β` by identifying `default α` with `none`.
-/
def sigma_equiv_option_of_inhabited (α : Type u) [inhabited α] [decidable_eq α] :
Σ (β : Type u), α ≃ option β :=
⟨{x : α // x ≠ default},
{ to_fun := λ (x : α), if h : x = default then none else some ⟨x, h⟩,
inv_fun := option.elim default coe,
left_inv := λ x, by { dsimp only, split_ifs; simp [*] },
right_inv := begin
rintro (_|⟨x,h⟩),
{ simp },
{ dsimp only,
split_ifs with hi,
{ simpa [h] using hi },
{ simp } }
end }⟩
end equiv
namespace multiset
variable [decidable_eq α]
lemma disjoint_to_finset {m1 m2 : multiset α} :
_root_.disjoint m1.to_finset m2.to_finset ↔ m1.disjoint m2 :=
begin
rw finset.disjoint_iff_ne,
refine ⟨λ h a ha1 ha2, _, _⟩,
{ rw ← multiset.mem_to_finset at ha1 ha2,
exact h _ ha1 _ ha2 rfl },
{ rintros h a ha b hb rfl,
rw multiset.mem_to_finset at ha hb,
exact h ha hb }
end
end multiset
namespace list
variables [decidable_eq α] {l l' : list α}
lemma disjoint_to_finset_iff_disjoint : _root_.disjoint l.to_finset l'.to_finset ↔ l.disjoint l' :=
multiset.disjoint_to_finset
end list
|
418f3f1422a8cf1d1e64e800646ed6d0721fe4d0 | e4def7044cdf5942eed78db25d2daa58d80eed64 | /_target/deps/mathlib/src/analysis/normed_space/basic.lean | 53f84c8cfd7161f73e3b71536ab24287f3a9d915 | [
"Apache-2.0"
] | permissive | kevinsullivan/dm.s20.old | 3f8f736b9a792cca8dd44a73a98ade0b534ed084 | a240be0a53961ac25b5f4426fe7bc8d4dbe7013f | refs/heads/master | 1,607,522,498,471 | 1,579,058,084,000 | 1,579,058,084,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 33,788 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Normed spaces.
Authors: Patrick Massot, Johannes Hölzl
-/
import algebra.pi_instances
import linear_algebra.basic
import topology.instances.nnreal topology.instances.complex
import topology.algebra.module
variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*}
noncomputable theory
open filter metric
open_locale topological_space
localized "notation f `→_{`:50 a `}`:0 b := filter.tendsto f (_root_.nhds a) (_root_.nhds b)" in filter
/-- Auxiliary class, endowing a type `α` with a function `norm : α → ℝ`. This class is designed to
be extended in more interesting classes specifying the properties of the norm. -/
class has_norm (α : Type*) := (norm : α → ℝ)
export has_norm (norm)
notation `∥`:1024 e:1 `∥`:1 := norm e
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A normed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥` defines
a metric space structure. -/
class normed_group (α : Type*) extends has_norm α, add_comm_group α, metric_space α :=
(dist_eq : ∀ x y, dist x y = norm (x - y))
end prio
/-- Construct a normed group from a translation invariant distance -/
def normed_group.of_add_dist [has_norm α] [add_comm_group α] [metric_space α]
(H1 : ∀ x:α, ∥x∥ = dist x 0)
(H2 : ∀ x y z : α, dist x y ≤ dist (x + z) (y + z)) : normed_group α :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 },
{ have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this }
end }
/-- Construct a normed group from a translation invariant distance -/
def normed_group.of_add_dist' [has_norm α] [add_comm_group α] [metric_space α]
(H1 : ∀ x:α, ∥x∥ = dist x 0)
(H2 : ∀ x y z : α, dist (x + z) (y + z) ≤ dist x y) : normed_group α :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this },
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 }
end }
/-- A normed group can be built from a norm that satisfies algebraic properties. This is
formalised in this structure. -/
structure normed_group.core (α : Type*) [add_comm_group α] [has_norm α] :=
(norm_eq_zero_iff : ∀ x : α, ∥x∥ = 0 ↔ x = 0)
(triangle : ∀ x y : α, ∥x + y∥ ≤ ∥x∥ + ∥y∥)
(norm_neg : ∀ x : α, ∥-x∥ = ∥x∥)
/-- Constructing a normed group from core properties of a norm, i.e., registering the distance and
the metric space structure from the norm properties. -/
noncomputable def normed_group.of_core (α : Type*) [add_comm_group α] [has_norm α]
(C : normed_group.core α) : normed_group α :=
{ dist := λ x y, ∥x - y∥,
dist_eq := assume x y, by refl,
dist_self := assume x, (C.norm_eq_zero_iff (x - x)).mpr (show x - x = 0, by simp),
eq_of_dist_eq_zero := assume x y h, show (x = y), from sub_eq_zero.mp $ (C.norm_eq_zero_iff (x - y)).mp h,
dist_triangle := assume x y z,
calc ∥x - z∥ = ∥x - y + (y - z)∥ : by simp
... ≤ ∥x - y∥ + ∥y - z∥ : C.triangle _ _,
dist_comm := assume x y,
calc ∥x - y∥ = ∥ -(y - x)∥ : by simp
... = ∥y - x∥ : by { rw [C.norm_neg] } }
section normed_group
variables [normed_group α] [normed_group β]
lemma dist_eq_norm (g h : α) : dist g h = ∥g - h∥ :=
normed_group.dist_eq _ _
@[simp] lemma dist_zero_right (g : α) : dist g 0 = ∥g∥ :=
by rw [dist_eq_norm, sub_zero]
lemma norm_sub_rev (g h : α) : ∥g - h∥ = ∥h - g∥ :=
by simpa only [dist_eq_norm] using dist_comm g h
@[simp] lemma norm_neg (g : α) : ∥-g∥ = ∥g∥ :=
by simpa using norm_sub_rev 0 g
@[simp] lemma dist_add_left (g h₁ h₂ : α) : dist (g + h₁) (g + h₂) = dist h₁ h₂ :=
by simp [dist_eq_norm]
@[simp] lemma dist_add_right (g₁ g₂ h : α) : dist (g₁ + h) (g₂ + h) = dist g₁ g₂ :=
by simp [dist_eq_norm]
@[simp] lemma dist_neg_neg (g h : α) : dist (-g) (-h) = dist g h :=
by simp only [dist_eq_norm, neg_sub_neg, norm_sub_rev]
@[simp] lemma dist_sub_left (g h₁ h₂ : α) : dist (g - h₁) (g - h₂) = dist h₁ h₂ :=
by simp only [sub_eq_add_neg, dist_add_left, dist_neg_neg]
@[simp] lemma dist_sub_right (g₁ g₂ h : α) : dist (g₁ - h) (g₂ - h) = dist g₁ g₂ :=
dist_add_right _ _ _
/-- Triangle inequality for the norm. -/
lemma norm_add_le (g h : α) : ∥g + h∥ ≤ ∥g∥ + ∥h∥ :=
by simpa [dist_eq_norm] using dist_triangle g 0 (-h)
lemma norm_add_le_of_le {g₁ g₂ : α} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) :
∥g₁ + g₂∥ ≤ n₁ + n₂ :=
le_trans (norm_add_le g₁ g₂) (add_le_add H₁ H₂)
lemma dist_add_add_le (g₁ g₂ h₁ h₂ : α) :
dist (g₁ + g₂) (h₁ + h₂) ≤ dist g₁ h₁ + dist g₂ h₂ :=
by simpa only [dist_add_left, dist_add_right] using dist_triangle (g₁ + g₂) (h₁ + g₂) (h₁ + h₂)
lemma dist_add_add_le_of_le {g₁ g₂ h₁ h₂ : α} {d₁ d₂ : ℝ}
(H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) :
dist (g₁ + g₂) (h₁ + h₂) ≤ d₁ + d₂ :=
le_trans (dist_add_add_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂)
lemma dist_sub_sub_le (g₁ g₂ h₁ h₂ : α) :
dist (g₁ - g₂) (h₁ - h₂) ≤ dist g₁ h₁ + dist g₂ h₂ :=
dist_neg_neg g₂ h₂ ▸ dist_add_add_le _ _ _ _
lemma dist_sub_sub_le_of_le {g₁ g₂ h₁ h₂ : α} {d₁ d₂ : ℝ}
(H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) :
dist (g₁ - g₂) (h₁ - h₂) ≤ d₁ + d₂ :=
le_trans (dist_sub_sub_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂)
@[simp] lemma norm_nonneg (g : α) : 0 ≤ ∥g∥ :=
by { rw[←dist_zero_right], exact dist_nonneg }
lemma norm_eq_zero (g : α) : ∥g∥ = 0 ↔ g = 0 :=
by { rw[←dist_zero_right], exact dist_eq_zero }
@[simp] lemma norm_zero : ∥(0:α)∥ = 0 := (norm_eq_zero _).2 rfl
lemma norm_sum_le {β} : ∀(s : finset β) (f : β → α), ∥s.sum f∥ ≤ s.sum (λa, ∥ f a ∥) :=
finset.le_sum_of_subadditive norm norm_zero norm_add_le
lemma norm_sum_le_of_le {β} (s : finset β) {f : β → α} {n : β → ℝ} (h : ∀ b ∈ s, ∥f b∥ ≤ n b) :
∥s.sum f∥ ≤ s.sum n :=
by { haveI := classical.dec_eq β, exact le_trans (norm_sum_le s f) (finset.sum_le_sum h) }
lemma norm_pos_iff (g : α) : 0 < ∥ g ∥ ↔ g ≠ 0 :=
dist_zero_right g ▸ dist_pos
lemma norm_le_zero_iff (g : α) : ∥g∥ ≤ 0 ↔ g = 0 :=
by { rw[←dist_zero_right], exact dist_le_zero }
lemma norm_sub_le (g h : α) : ∥g - h∥ ≤ ∥g∥ + ∥h∥ :=
by simpa [dist_eq_norm] using dist_triangle g 0 h
lemma norm_sub_le_of_le {g₁ g₂ : α} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) :
∥g₁ - g₂∥ ≤ n₁ + n₂ :=
le_trans (norm_sub_le g₁ g₂) (add_le_add H₁ H₂)
lemma dist_le_norm_add_norm (g h : α) : dist g h ≤ ∥g∥ + ∥h∥ :=
by { rw dist_eq_norm, apply norm_sub_le }
lemma abs_norm_sub_norm_le (g h : α) : abs(∥g∥ - ∥h∥) ≤ ∥g - h∥ :=
by simpa [dist_eq_norm] using abs_dist_sub_le g h 0
lemma norm_sub_norm_le (g h : α) : ∥g∥ - ∥h∥ ≤ ∥g - h∥ :=
le_trans (le_abs_self _) (abs_norm_sub_norm_le g h)
lemma dist_norm_norm_le (g h : α) : dist ∥g∥ ∥h∥ ≤ ∥g - h∥ :=
abs_norm_sub_norm_le g h
lemma ball_0_eq (ε : ℝ) : ball (0:α) ε = {x | ∥x∥ < ε} :=
set.ext $ assume a, by simp
theorem normed_group.tendsto_nhds_zero {f : γ → α} {l : filter γ} :
tendsto f l (𝓝 0) ↔ ∀ ε > 0, { x | ∥ f x ∥ < ε } ∈ l :=
metric.tendsto_nhds.trans $ forall_congr $ λ ε, forall_congr $ λ εgt0,
begin
simp only [dist_zero_right],
exact exists_sets_subset_iff
end
section nnnorm
/-- Version of the norm taking values in nonnegative reals. -/
def nnnorm (a : α) : nnreal := ⟨norm a, norm_nonneg a⟩
@[simp] lemma coe_nnnorm (a : α) : (nnnorm a : ℝ) = norm a := rfl
lemma nndist_eq_nnnorm (a b : α) : nndist a b = nnnorm (a - b) := nnreal.eq $ dist_eq_norm _ _
lemma nnnorm_eq_zero (a : α) : nnnorm a = 0 ↔ a = 0 :=
by simp only [nnreal.eq_iff.symm, nnreal.coe_zero, coe_nnnorm, norm_eq_zero]
@[simp] lemma nnnorm_zero : nnnorm (0 : α) = 0 :=
nnreal.eq norm_zero
lemma nnnorm_add_le (g h : α) : nnnorm (g + h) ≤ nnnorm g + nnnorm h :=
nnreal.coe_le.2 $ norm_add_le g h
@[simp] lemma nnnorm_neg (g : α) : nnnorm (-g) = nnnorm g :=
nnreal.eq $ norm_neg g
lemma nndist_nnnorm_nnnorm_le (g h : α) : nndist (nnnorm g) (nnnorm h) ≤ nnnorm (g - h) :=
nnreal.coe_le.2 $ dist_norm_norm_le g h
lemma of_real_norm_eq_coe_nnnorm (x : β) : ennreal.of_real ∥x∥ = (nnnorm x : ennreal) :=
ennreal.of_real_eq_coe_nnreal _
lemma edist_eq_coe_nnnorm (x : β) : edist x 0 = (nnnorm x : ennreal) :=
by { rw [edist_dist, dist_eq_norm, _root_.sub_zero, of_real_norm_eq_coe_nnnorm] }
end nnnorm
/-- A submodule of a normed group is also a normed group, with the restriction of the norm.
As all instances can be inferred from the submodule `s`, they are put as implicit instead of
typeclasses. -/
instance submodule.normed_group {𝕜 : Type*} {_ : ring 𝕜}
{E : Type*} [normed_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : normed_group s :=
{ norm := λx, norm (x : E),
dist_eq := λx y, dist_eq_norm (x : E) (y : E) }
/-- normed group instance on the product of two normed groups, using the sup norm. -/
instance prod.normed_group : normed_group (α × β) :=
{ norm := λx, max ∥x.1∥ ∥x.2∥,
dist_eq := assume (x y : α × β),
show max (dist x.1 y.1) (dist x.2 y.2) = (max ∥(x - y).1∥ ∥(x - y).2∥), by simp [dist_eq_norm] }
lemma norm_fst_le (x : α × β) : ∥x.1∥ ≤ ∥x∥ :=
by simp [norm, le_max_left]
lemma norm_snd_le (x : α × β) : ∥x.2∥ ≤ ∥x∥ :=
by simp [norm, le_max_right]
/-- normed group instance on the product of finitely many normed groups, using the sup norm. -/
instance pi.normed_group {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] :
normed_group (Πb, π b) :=
{ norm := λf, ((finset.sup finset.univ (λ b, nnnorm (f b)) : nnreal) : ℝ),
dist_eq := assume x y,
congr_arg (coe : nnreal → ℝ) $ congr_arg (finset.sup finset.univ) $ funext $ assume a,
show nndist (x a) (y a) = nnnorm (x a - y a), from nndist_eq_nnnorm _ _ }
/-- The norm of an element in a product space is `≤ r` if and only if the norm of each
component is. -/
lemma pi_norm_le_iff {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] {r : ℝ} (hr : 0 ≤ r)
{x : Πb, π b} : ∥x∥ ≤ r ↔ ∀i, ∥x i∥ ≤ r :=
by { simp only [(dist_zero_right _).symm, dist_pi_le_iff hr], refl }
lemma tendsto_iff_norm_tendsto_zero {f : ι → β} {a : filter ι} {b : β} :
tendsto f a (𝓝 b) ↔ tendsto (λ e, ∥ f e - b ∥) a (𝓝 0) :=
by rw tendsto_iff_dist_tendsto_zero ; simp only [(dist_eq_norm _ _).symm]
lemma tendsto_zero_iff_norm_tendsto_zero {f : γ → β} {a : filter γ} :
tendsto f a (𝓝 0) ↔ tendsto (λ e, ∥ f e ∥) a (𝓝 0) :=
have tendsto f a (𝓝 0) ↔ tendsto (λ e, ∥ f e - 0 ∥) a (𝓝 0) :=
tendsto_iff_norm_tendsto_zero,
by simpa
lemma lim_norm (x : α) : (λg:α, ∥g - x∥) →_{x} 0 :=
tendsto_iff_norm_tendsto_zero.1 (continuous_iff_continuous_at.1 continuous_id x)
lemma lim_norm_zero : (λg:α, ∥g∥) →_{0} 0 :=
by simpa using lim_norm (0:α)
lemma continuous_norm : continuous (λg:α, ∥g∥) :=
begin
rw continuous_iff_continuous_at,
intro x,
rw [continuous_at, tendsto_iff_dist_tendsto_zero],
exact squeeze_zero (λ t, abs_nonneg _) (λ t, abs_norm_sub_norm_le _ _) (lim_norm x)
end
lemma continuous_nnnorm : continuous (nnnorm : α → nnreal) :=
continuous_subtype_mk _ continuous_norm
/-- A normed group is a uniform additive group, i.e., addition and subtraction are uniformly
continuous. -/
@[priority 100] -- see Note [lower instance priority]
instance normed_uniform_group : uniform_add_group α :=
begin
refine ⟨metric.uniform_continuous_iff.2 $ assume ε hε, ⟨ε / 2, half_pos hε, assume a b h, _⟩⟩,
rw [prod.dist_eq, max_lt_iff, dist_eq_norm, dist_eq_norm] at h,
calc dist (a.1 - a.2) (b.1 - b.2) = ∥(a.1 - b.1) - (a.2 - b.2)∥ : by simp [dist_eq_norm]
... ≤ ∥a.1 - b.1∥ + ∥a.2 - b.2∥ : norm_sub_le _ _
... < ε / 2 + ε / 2 : add_lt_add h.1 h.2
... = ε : add_halves _
end
@[priority 100] -- see Note [lower instance priority]
instance normed_top_monoid : topological_add_monoid α := by apply_instance -- short-circuit type class inference
@[priority 100] -- see Note [lower instance priority]
instance normed_top_group : topological_add_group α := by apply_instance -- short-circuit type class inference
end normed_group
section normed_ring
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A normed ring is a ring endowed with a norm which satisfies the inequality `∥x y∥ ≤ ∥x∥ ∥y∥`. -/
class normed_ring (α : Type*) extends has_norm α, ring α, metric_space α :=
(dist_eq : ∀ x y, dist x y = norm (x - y))
(norm_mul : ∀ a b, norm (a * b) ≤ norm a * norm b)
end prio
@[priority 100] -- see Note [lower instance priority]
instance normed_ring.to_normed_group [β : normed_ring α] : normed_group α := { ..β }
lemma norm_mul_le {α : Type*} [normed_ring α] (a b : α) : (∥a*b∥) ≤ (∥a∥) * (∥b∥) :=
normed_ring.norm_mul _ _
lemma norm_pow_le {α : Type*} [normed_ring α] (a : α) : ∀ {n : ℕ}, 0 < n → ∥a^n∥ ≤ ∥a∥^n
| 1 h := by simp
| (n+2) h :=
le_trans (norm_mul_le a (a^(n+1)))
(mul_le_mul (le_refl _)
(norm_pow_le (nat.succ_pos _)) (norm_nonneg _) (norm_nonneg _))
/-- Normed ring structure on the product of two normed rings, using the sup norm. -/
instance prod.normed_ring [normed_ring α] [normed_ring β] : normed_ring (α × β) :=
{ norm_mul := assume x y,
calc
∥x * y∥ = ∥(x.1*y.1, x.2*y.2)∥ : rfl
... = (max ∥x.1*y.1∥ ∥x.2*y.2∥) : rfl
... ≤ (max (∥x.1∥*∥y.1∥) (∥x.2∥*∥y.2∥)) :
max_le_max (norm_mul_le (x.1) (y.1)) (norm_mul_le (x.2) (y.2))
... = (max (∥x.1∥*∥y.1∥) (∥y.2∥*∥x.2∥)) : by simp[mul_comm]
... ≤ (max (∥x.1∥) (∥x.2∥)) * (max (∥y.2∥) (∥y.1∥)) : by { apply max_mul_mul_le_max_mul_max; simp [norm_nonneg] }
... = (max (∥x.1∥) (∥x.2∥)) * (max (∥y.1∥) (∥y.2∥)) : by simp[max_comm]
... = (∥x∥*∥y∥) : rfl,
..prod.normed_group }
end normed_ring
@[priority 100] -- see Note [lower instance priority]
instance normed_ring_top_monoid [normed_ring α] : topological_monoid α :=
⟨ continuous_iff_continuous_at.2 $ λ x, tendsto_iff_norm_tendsto_zero.2 $
have ∀ e : α × α, e.fst * e.snd - x.fst * x.snd =
e.fst * e.snd - e.fst * x.snd + (e.fst * x.snd - x.fst * x.snd), by intro; rw sub_add_sub_cancel,
begin
apply squeeze_zero,
{ intro, apply norm_nonneg },
{ simp only [this], intro, apply norm_add_le },
{ rw ←zero_add (0 : ℝ), apply tendsto.add,
{ apply squeeze_zero,
{ intro, apply norm_nonneg },
{ intro t, show ∥t.fst * t.snd - t.fst * x.snd∥ ≤ ∥t.fst∥ * ∥t.snd - x.snd∥,
rw ←mul_sub, apply norm_mul_le },
{ rw ←mul_zero (∥x.fst∥), apply tendsto.mul,
{ apply continuous_iff_continuous_at.1,
apply continuous_norm.comp continuous_fst },
{ apply tendsto_iff_norm_tendsto_zero.1,
apply continuous_iff_continuous_at.1,
apply continuous_snd }}},
{ apply squeeze_zero,
{ intro, apply norm_nonneg },
{ intro t, show ∥t.fst * x.snd - x.fst * x.snd∥ ≤ ∥t.fst - x.fst∥ * ∥x.snd∥,
rw ←sub_mul, apply norm_mul_le },
{ rw ←zero_mul (∥x.snd∥), apply tendsto.mul,
{ apply tendsto_iff_norm_tendsto_zero.1,
apply continuous_iff_continuous_at.1,
apply continuous_fst },
{ apply tendsto_const_nhds }}}}
end ⟩
/-- A normed ring is a topological ring. -/
@[priority 100] -- see Note [lower instance priority]
instance normed_top_ring [normed_ring α] : topological_ring α :=
⟨ continuous_iff_continuous_at.2 $ λ x, tendsto_iff_norm_tendsto_zero.2 $
have ∀ e : α, -e - -x = -(e - x), by intro; simp,
by simp only [this, norm_neg]; apply lim_norm ⟩
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A normed field is a field with a norm satisfying ∥x y∥ = ∥x∥ ∥y∥. -/
class normed_field (α : Type*) extends has_norm α, discrete_field α, metric_space α :=
(dist_eq : ∀ x y, dist x y = norm (x - y))
(norm_mul' : ∀ a b, norm (a * b) = norm a * norm b)
/-- A nondiscrete normed field is a normed field in which there is an element of norm different from
`0` and `1`. This makes it possible to bring any element arbitrarily close to `0` by multiplication
by the powers of any element, and thus to relate algebra and topology. -/
class nondiscrete_normed_field (α : Type*) extends normed_field α :=
(non_trivial : ∃x:α, 1<∥x∥)
end prio
@[priority 100] -- see Note [lower instance priority]
instance normed_field.to_normed_ring [i : normed_field α] : normed_ring α :=
{ norm_mul := by finish [i.norm_mul'], ..i }
namespace normed_field
@[simp] lemma norm_one {α : Type*} [normed_field α] : ∥(1 : α)∥ = 1 :=
have ∥(1 : α)∥ * ∥(1 : α)∥ = ∥(1 : α)∥ * 1, by calc
∥(1 : α)∥ * ∥(1 : α)∥ = ∥(1 : α) * (1 : α)∥ : by rw normed_field.norm_mul'
... = ∥(1 : α)∥ * 1 : by simp,
eq_of_mul_eq_mul_left (ne_of_gt ((norm_pos_iff _).2 (by simp))) this
@[simp] lemma norm_mul [normed_field α] (a b : α) : ∥a * b∥ = ∥a∥ * ∥b∥ :=
normed_field.norm_mul' a b
instance normed_field.is_monoid_hom_norm [normed_field α] : is_monoid_hom (norm : α → ℝ) :=
{ map_one := norm_one, map_mul := norm_mul }
@[simp] lemma norm_pow [normed_field α] (a : α) : ∀ (n : ℕ), ∥a^n∥ = ∥a∥^n :=
is_monoid_hom.map_pow norm a
@[simp] lemma norm_prod {β : Type*} [normed_field α] (s : finset β) (f : β → α) :
∥s.prod f∥ = s.prod (λb, ∥f b∥) :=
eq.symm (finset.prod_hom norm)
@[simp] lemma norm_div {α : Type*} [normed_field α] (a b : α) : ∥a/b∥ = ∥a∥/∥b∥ :=
if hb : b = 0 then by simp [hb] else
begin
apply eq_div_of_mul_eq,
{ apply ne_of_gt, apply (norm_pos_iff _).mpr hb },
{ rw [←normed_field.norm_mul, div_mul_cancel _ hb] }
end
@[simp] lemma norm_inv {α : Type*} [normed_field α] (a : α) : ∥a⁻¹∥ = ∥a∥⁻¹ :=
by simp only [inv_eq_one_div, norm_div, norm_one]
@[simp] lemma norm_fpow {α : Type*} [normed_field α] (a : α) : ∀n : ℤ,
∥a^n∥ = ∥a∥^n
| (n : ℕ) := norm_pow a n
| -[1+ n] := by simp [fpow_neg_succ_of_nat]
lemma exists_one_lt_norm (α : Type*) [i : nondiscrete_normed_field α] : ∃x : α, 1 < ∥x∥ :=
i.non_trivial
lemma exists_norm_lt_one (α : Type*) [nondiscrete_normed_field α] : ∃x : α, 0 < ∥x∥ ∧ ∥x∥ < 1 :=
begin
rcases exists_one_lt_norm α with ⟨y, hy⟩,
refine ⟨y⁻¹, _, _⟩,
{ simp only [inv_eq_zero, ne.def, norm_pos_iff],
assume h,
rw ← norm_eq_zero at h,
rw h at hy,
exact lt_irrefl _ (lt_trans zero_lt_one hy) },
{ simp [inv_lt_one hy] }
end
lemma exists_lt_norm (α : Type*) [nondiscrete_normed_field α]
(r : ℝ) : ∃ x : α, r < ∥x∥ :=
let ⟨w, hw⟩ := exists_one_lt_norm α in
let ⟨n, hn⟩ := pow_unbounded_of_one_lt r hw in
⟨w^n, by rwa norm_pow⟩
lemma exists_norm_lt (α : Type*) [nondiscrete_normed_field α]
{r : ℝ} (hr : 0 < r) : ∃ x : α, 0 < ∥x∥ ∧ ∥x∥ < r :=
let ⟨w, hw⟩ := exists_one_lt_norm α in
let ⟨n, hle, hlt⟩ := exists_int_pow_near' hr hw in
⟨w^n, by { rw norm_fpow; exact fpow_pos_of_pos (lt_trans zero_lt_one hw) _},
by rwa norm_fpow⟩
lemma tendsto_inv [normed_field α] {r : α} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) :=
begin
refine metric.tendsto_nhds.2 (λε εpos, _),
let δ := min (ε/2/2 * ∥r∥^2) (∥r∥/2),
have norm_r_pos : 0 < ∥r∥ := (norm_pos_iff r).mpr r0,
have A : 0 < ε / 2 / 2 * ∥r∥ ^ 2 := mul_pos' (half_pos (half_pos εpos)) (pow_pos norm_r_pos 2),
have δpos : 0 < δ, by simp [half_pos norm_r_pos, A],
refine ⟨ball r δ, ball_mem_nhds r δpos, λx hx, _⟩,
have rx : ∥r∥/2 ≤ ∥x∥ := calc
∥r∥/2 = ∥r∥ - ∥r∥/2 : by ring
... ≤ ∥r∥ - ∥r - x∥ :
begin
apply sub_le_sub (le_refl _),
rw ← dist_eq_norm,
exact le_trans (le_of_lt (mem_ball'.1 hx)) (min_le_right _ _)
end
... ≤ ∥r - (r - x)∥ : norm_sub_norm_le r (r - x)
... = ∥x∥ : by simp,
have norm_x_pos : 0 < ∥x∥ := lt_of_lt_of_le (half_pos norm_r_pos) rx,
have : x⁻¹ - r⁻¹ = (r - x) * x⁻¹ * r⁻¹,
by rw [sub_mul, sub_mul, mul_inv_cancel ((norm_pos_iff x).mp norm_x_pos), one_mul, mul_comm,
← mul_assoc, inv_mul_cancel r0, one_mul],
calc dist x⁻¹ r⁻¹ = ∥x⁻¹ - r⁻¹∥ : dist_eq_norm _ _
... ≤ ∥r-x∥ * ∥x∥⁻¹ * ∥r∥⁻¹ : by rw [this, norm_mul, norm_mul, norm_inv, norm_inv]
... ≤ (ε/2/2 * ∥r∥^2) * (2 * ∥r∥⁻¹) * (∥r∥⁻¹) : begin
apply_rules [mul_le_mul, inv_nonneg.2, le_of_lt A, norm_nonneg, inv_nonneg.2, mul_nonneg,
(inv_le_inv norm_x_pos norm_r_pos).2, le_refl],
show ∥r - x∥ ≤ ε / 2 / 2 * ∥r∥ ^ 2,
by { rw ← dist_eq_norm, exact le_trans (le_of_lt (mem_ball'.1 hx)) (min_le_left _ _) },
show ∥x∥⁻¹ ≤ 2 * ∥r∥⁻¹,
{ convert (inv_le_inv norm_x_pos (half_pos norm_r_pos)).2 rx,
rw [inv_div (ne.symm (ne_of_lt norm_r_pos)), div_eq_inv_mul, mul_comm],
norm_num },
show (0 : ℝ) ≤ 2, by norm_num
end
... = ε/2 * (∥r∥ * ∥r∥⁻¹)^2 : by { generalize : ∥r∥⁻¹ = u, ring }
... = ε/2 : by { rw [mul_inv_cancel (ne.symm (ne_of_lt norm_r_pos))], simp }
... < ε : half_lt_self εpos
end
lemma continuous_on_inv [normed_field α] : continuous_on (λ(x:α), x⁻¹) {x | x ≠ 0} :=
begin
assume x hx,
apply continuous_at.continuous_within_at,
exact (tendsto_inv hx)
end
instance : normed_field ℝ :=
{ norm := λ x, abs x,
dist_eq := assume x y, rfl,
norm_mul' := abs_mul }
instance : nondiscrete_normed_field ℝ :=
{ non_trivial := ⟨2, by { unfold norm, rw abs_of_nonneg; norm_num }⟩ }
end normed_field
/-- If a function converges to a nonzero value, its inverse converges to the inverse of this value.
We use the name `tendsto.inv'` as `tendsto.inv` is already used in multiplicative topological
groups. -/
lemma filter.tendsto.inv' [normed_field α] {l : filter β} {f : β → α} {y : α}
(hy : y ≠ 0) (h : tendsto f l (𝓝 y)) :
tendsto (λx, (f x)⁻¹) l (𝓝 y⁻¹) :=
(normed_field.tendsto_inv hy).comp h
lemma filter.tendsto.div [normed_field α] {l : filter β} {f g : β → α} {x y : α}
(hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) (hy : y ≠ 0) :
tendsto (λa, f a / g a) l (𝓝 (x / y)) :=
hf.mul (hg.inv' hy)
lemma real.norm_eq_abs (r : ℝ) : norm r = abs r := rfl
@[simp] lemma norm_norm [normed_group α] (x : α) : ∥∥x∥∥ = ∥x∥ :=
by rw [real.norm_eq_abs, abs_of_nonneg (norm_nonneg _)]
@[simp] lemma nnnorm_norm [normed_group α] (a : α) : nnnorm ∥a∥ = nnnorm a :=
by simp only [nnnorm, norm_norm]
instance : normed_ring ℤ :=
{ norm := λ n, ∥(n : ℝ)∥,
norm_mul := λ m n, le_of_eq $ by simp only [norm, int.cast_mul, abs_mul],
dist_eq := λ m n, by simp only [int.dist_eq, norm, int.cast_sub] }
@[elim_cast] lemma int.norm_cast_real (m : ℤ) : ∥(m : ℝ)∥ = ∥m∥ := rfl
instance : normed_field ℚ :=
{ norm := λ r, ∥(r : ℝ)∥,
norm_mul' := λ r₁ r₂, by simp only [norm, rat.cast_mul, abs_mul],
dist_eq := λ r₁ r₂, by simp only [rat.dist_eq, norm, rat.cast_sub] }
instance : nondiscrete_normed_field ℚ :=
{ non_trivial := ⟨2, by { unfold norm, rw abs_of_nonneg; norm_num }⟩ }
@[elim_cast, simp] lemma rat.norm_cast_real (r : ℚ) : ∥(r : ℝ)∥ = ∥r∥ := rfl
@[elim_cast, simp] lemma int.norm_cast_rat (m : ℤ) : ∥(m : ℚ)∥ = ∥m∥ :=
by rw [← rat.norm_cast_real, ← int.norm_cast_real]; congr' 1; norm_cast
section normed_space
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A normed space over a normed field is a vector space endowed with a norm which satisfies the
equality `∥c • x∥ = ∥c∥ ∥x∥`. -/
class normed_space (α : Type*) (β : Type*) [normed_field α] [normed_group β]
extends vector_space α β :=
(norm_smul : ∀ (a:α) (b:β), norm (a • b) = has_norm.norm a * norm b)
end prio
variables [normed_field α] [normed_group β]
instance normed_field.to_normed_space : normed_space α α :=
{ norm_smul := normed_field.norm_mul }
set_option class.instance_max_depth 43
lemma norm_smul [normed_space α β] (s : α) (x : β) : ∥s • x∥ = ∥s∥ * ∥x∥ :=
normed_space.norm_smul s x
lemma dist_smul [normed_space α β] (s : α) (x y : β) : dist (s • x) (s • y) = ∥s∥ * dist x y :=
by simp only [dist_eq_norm, (norm_smul _ _).symm, smul_sub]
lemma nnnorm_smul [normed_space α β] (s : α) (x : β) : nnnorm (s • x) = nnnorm s * nnnorm x :=
nnreal.eq $ norm_smul s x
variables {E : Type*} {F : Type*}
[normed_group E] [normed_space α E] [normed_group F] [normed_space α F]
lemma tendsto_smul {f : γ → α} { g : γ → F} {e : filter γ} {s : α} {b : F} :
(tendsto f e (𝓝 s)) → (tendsto g e (𝓝 b)) → tendsto (λ x, (f x) • (g x)) e (𝓝 (s • b)) :=
begin
intros limf limg,
rw tendsto_iff_norm_tendsto_zero,
have ineq := λ x : γ, calc
∥f x • g x - s • b∥ = ∥(f x • g x - s • g x) + (s • g x - s • b)∥ : by simp[add_assoc]
... ≤ ∥f x • g x - s • g x∥ + ∥s • g x - s • b∥ : norm_add_le (f x • g x - s • g x) (s • g x - s • b)
... ≤ ∥f x - s∥*∥g x∥ + ∥s∥*∥g x - b∥ : by { rw [←smul_sub, ←sub_smul, norm_smul, norm_smul] },
apply squeeze_zero,
{ intro t, exact norm_nonneg _ },
{ exact ineq },
{ clear ineq,
have limf': tendsto (λ x, ∥f x - s∥) e (𝓝 0) := tendsto_iff_norm_tendsto_zero.1 limf,
have limg' : tendsto (λ x, ∥g x∥) e (𝓝 ∥b∥) := filter.tendsto.comp (continuous_iff_continuous_at.1 continuous_norm _) limg,
have lim1 := limf'.mul limg',
simp only [zero_mul, sub_eq_add_neg] at lim1,
have limg3 := tendsto_iff_norm_tendsto_zero.1 limg,
have lim2 := (tendsto_const_nhds : tendsto _ _ (𝓝 ∥ s ∥)).mul limg3,
simp only [sub_eq_add_neg, mul_zero] at lim2,
rw [show (0:ℝ) = 0 + 0, by simp],
exact lim1.add lim2 }
end
lemma tendsto_smul_const {g : γ → F} {e : filter γ} (s : α) {b : F} :
(tendsto g e (𝓝 b)) → tendsto (λ x, s • (g x)) e (𝓝 (s • b)) :=
tendsto_smul tendsto_const_nhds
@[priority 100] -- see Note [lower instance priority]
instance normed_space.topological_vector_space : topological_vector_space α E :=
{ continuous_smul := continuous_iff_continuous_at.2 $ λp, tendsto_smul
(continuous_iff_continuous_at.1 continuous_fst _) (continuous_iff_continuous_at.1 continuous_snd _) }
open normed_field
/-- If there is a scalar `c` with `∥c∥>1`, then any element can be moved by scalar multiplication to
any shell of width `∥c∥`. Also recap information on the norm of the rescaling element that shows
up in applications. -/
lemma rescale_to_shell {c : α} (hc : 1 < ∥c∥) {ε : ℝ} (εpos : 0 < ε) {x : E} (hx : x ≠ 0) :
∃d:α, d ≠ 0 ∧ ∥d • x∥ ≤ ε ∧ (ε/∥c∥ ≤ ∥d • x∥) ∧ (∥d∥⁻¹ ≤ ε⁻¹ * ∥c∥ * ∥x∥) :=
begin
have xεpos : 0 < ∥x∥/ε := div_pos_of_pos_of_pos ((norm_pos_iff _).2 hx) εpos,
rcases exists_int_pow_near xεpos hc with ⟨n, hn⟩,
have cpos : 0 < ∥c∥ := lt_trans (zero_lt_one : (0 :ℝ) < 1) hc,
have cnpos : 0 < ∥c^(n+1)∥ := by { rw norm_fpow, exact lt_trans xεpos hn.2 },
refine ⟨(c^(n+1))⁻¹, _, _, _, _⟩,
show (c ^ (n + 1))⁻¹ ≠ 0,
by rwa [ne.def, inv_eq_zero, ← ne.def, ← norm_pos_iff],
show ∥(c ^ (n + 1))⁻¹ • x∥ ≤ ε,
{ rw [norm_smul, norm_inv, ← div_eq_inv_mul, div_le_iff cnpos, mul_comm, norm_fpow],
exact (div_le_iff εpos).1 (le_of_lt (hn.2)) },
show ε / ∥c∥ ≤ ∥(c ^ (n + 1))⁻¹ • x∥,
{ rw [div_le_iff cpos, norm_smul, norm_inv, norm_fpow, fpow_add (ne_of_gt cpos),
fpow_one, mul_inv', mul_comm, ← mul_assoc, ← mul_assoc, mul_inv_cancel (ne_of_gt cpos),
one_mul, ← div_eq_inv_mul, le_div_iff (fpow_pos_of_pos cpos _), mul_comm],
exact (le_div_iff εpos).1 hn.1 },
show ∥(c ^ (n + 1))⁻¹∥⁻¹ ≤ ε⁻¹ * ∥c∥ * ∥x∥,
{ have : ε⁻¹ * ∥c∥ * ∥x∥ = ε⁻¹ * ∥x∥ * ∥c∥, by ring,
rw [norm_inv, inv_inv', norm_fpow, fpow_add (ne_of_gt cpos), fpow_one, this, ← div_eq_inv_mul],
exact mul_le_mul_of_nonneg_right hn.1 (norm_nonneg _) }
end
/-- The product of two normed spaces is a normed space, with the sup norm. -/
instance : normed_space α (E × F) :=
{ norm_smul :=
begin
intros s x,
cases x with x₁ x₂,
change max (∥s • x₁∥) (∥s • x₂∥) = ∥s∥ * max (∥x₁∥) (∥x₂∥),
rw [norm_smul, norm_smul, ← mul_max_of_nonneg _ _ (norm_nonneg _)]
end,
add_smul := λ r x y, prod.ext (add_smul _ _ _) (add_smul _ _ _),
smul_add := λ r x y, prod.ext (smul_add _ _ _) (smul_add _ _ _),
..prod.normed_group,
..prod.vector_space }
/-- The product of finitely many normed spaces is a normed space, with the sup norm. -/
instance pi.normed_space {E : ι → Type*} [fintype ι] [∀i, normed_group (E i)]
[∀i, normed_space α (E i)] : normed_space α (Πi, E i) :=
{ norm_smul := λ a f,
show (↑(finset.sup finset.univ (λ (b : ι), nnnorm (a • f b))) : ℝ) =
nnnorm a * ↑(finset.sup finset.univ (λ (b : ι), nnnorm (f b))),
by simp only [(nnreal.coe_mul _ _).symm, nnreal.mul_finset_sup, nnnorm_smul] }
/-- A subspace of a normed space is also a normed space, with the restriction of the norm. -/
instance submodule.normed_space {𝕜 : Type*} [normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E] (s : submodule 𝕜 E) : normed_space 𝕜 s :=
{ norm_smul := λc x, norm_smul c (x : E) }
end normed_space
section normed_algebra
/-- A normed algebra `𝕜'` over `𝕜` is an algebra endowed with a norm for which the embedding of
`𝕜` in `𝕜'` is an isometry. -/
class normed_algebra (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜']
extends algebra 𝕜 𝕜' :=
(norm_algebra_map_eq : ∀x:𝕜, ∥algebra_map 𝕜' x∥ = ∥x∥)
@[simp] lemma norm_algebra_map_eq {𝕜 : Type*} (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜']
[h : normed_algebra 𝕜 𝕜'] (x : 𝕜) : ∥algebra_map 𝕜' x∥ = ∥x∥ :=
normed_algebra.norm_algebra_map_eq _ _
end normed_algebra
section restrict_scalars
set_option class.instance_max_depth 40
variables (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_field 𝕜'] [normed_algebra 𝕜 𝕜']
{E : Type*} [normed_group E] [normed_space 𝕜' E]
/-- `𝕜`-normed space structure induced by a `𝕜'`-normed space structure when `𝕜'` is a
normed algebra over `𝕜`. Not registered as an instance as `𝕜'` can not be inferred. -/
def normed_space.restrict_scalars : normed_space 𝕜 E :=
{ norm_smul := λc x, begin
change ∥(algebra_map 𝕜' c) • x∥ = ∥c∥ * ∥x∥,
simp [norm_smul]
end,
..module.restrict_scalars 𝕜 𝕜' E }
end restrict_scalars
section summable
open_locale classical
open finset filter
variables [normed_group α] [complete_space α]
lemma summable_iff_vanishing_norm {f : ι → α} :
summable f ↔ ∀ε > 0, ∃s:finset ι, ∀t, disjoint t s → ∥ t.sum f ∥ < ε :=
begin
simp only [summable_iff_vanishing, metric.mem_nhds_iff, exists_imp_distrib],
split,
{ assume h ε hε, refine h {x | ∥x∥ < ε} ε hε _, rw [ball_0_eq ε] },
{ assume h s ε hε hs,
rcases h ε hε with ⟨t, ht⟩,
refine ⟨t, assume u hu, hs _⟩,
rw [ball_0_eq],
exact ht u hu }
end
lemma summable_of_norm_bounded {f : ι → α} (g : ι → ℝ) (hf : summable g) (h : ∀i, ∥f i∥ ≤ g i) :
summable f :=
summable_iff_vanishing_norm.2 $ assume ε hε,
let ⟨s, hs⟩ := summable_iff_vanishing_norm.1 hf ε hε in
⟨s, assume t ht,
have ∥t.sum g∥ < ε := hs t ht,
have nn : 0 ≤ t.sum g := finset.sum_nonneg (assume a _, le_trans (norm_nonneg _) (h a)),
lt_of_le_of_lt (norm_sum_le_of_le t (λ i _, h i)) $
by rwa [real.norm_eq_abs, abs_of_nonneg nn] at this⟩
lemma summable_of_summable_norm {f : ι → α} (hf : summable (λa, ∥f a∥)) : summable f :=
summable_of_norm_bounded _ hf (assume i, le_refl _)
lemma norm_tsum_le_tsum_norm {f : ι → α} (hf : summable (λi, ∥f i∥)) : ∥(∑i, f i)∥ ≤ (∑ i, ∥f i∥) :=
have h₁ : tendsto (λs:finset ι, ∥s.sum f∥) at_top (𝓝 ∥(∑ i, f i)∥) :=
(continuous_norm.tendsto _).comp (has_sum_tsum $ summable_of_summable_norm hf),
have h₂ : tendsto (λs:finset ι, s.sum (λi, ∥f i∥)) at_top (𝓝 (∑ i, ∥f i∥)) :=
has_sum_tsum hf,
le_of_tendsto_of_tendsto at_top_ne_bot h₁ h₂ $ univ_mem_sets' $ assume s, norm_sum_le _ _
end summable
|
beec659a5b2d918eab59b8f1db2f2c7238f35bbd | a46270e2f76a375564f3b3e9c1bf7b635edc1f2c | /5.8.1-3.7.lean | 00d00cd388839f229d739a0e0d02b2d056a71023 | [
"CC0-1.0"
] | permissive | wudcscheme/lean-exercise | 88ea2506714eac343de2a294d1132ee8ee6d3a20 | 5b23b9be3d361fff5e981d5be3a0a1175504b9f6 | refs/heads/master | 1,678,958,930,293 | 1,583,197,205,000 | 1,583,197,205,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,774 | lean | variables p q r : Prop
-- commutativity of ∧ and ∨
example : p ∧ q ↔ q ∧ p := begin
apply iff.intro, all_goals {intro h, exact ⟨h.2, h.1⟩ }
end
example : p ∨ q ↔ q ∨ p := begin
apply iff.intro, all_goals {intro h, cases h, {right, assumption}, {left, assumption} }
end
-- associativity of ∧ and ∨
example : (p ∧ q) ∧ r ↔ p ∧ (q ∧ r) := begin
apply iff.intro, {
intro h, rw [and_assoc] at h, exact h
}, {
intro h,
rw [and_assoc], exact h
}
end
example : (p ∨ q) ∨ r ↔ p ∨ (q ∨ r) := begin
apply iff.intro, {
intro h, rw [or_assoc] at h, exact h
}, {
intro h, rw [or_assoc], exact h
}
end
-- distributivity
example : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := begin
apply iff.intro, {
intro h,
have hp: p := h.1,
cases h.2,
all_goals {
{left, constructor, repeat {assumption}} <|> {right, constructor, repeat {assumption}}
}
}, {
intro h,
cases h,
all_goals { exact ⟨h.1, or.inl h.2⟩ <|> exact ⟨h.1, or.inr h.2⟩}
}
end
example : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (p ∨ r) := begin
apply iff.intro, {
intro h,
cases h with hp hqr,
exact ⟨or.inl hp, or.inl hp⟩,
exact ⟨or.inr hqr.1, or.inr hqr.2⟩,
}, {
intro h,
apply or.elim h.1,
intro, left, assumption,
intro, cases h.2 with hp hr,
left, assumption,
right, constructor, repeat{assumption}
}
end
-- other properties
example : (p → (q → r)) ↔ (p ∧ q → r) := begin
apply iff.intro, {
intros, exact a a_1.1 a_1.2
}, {
intros, exact a ⟨a_1, a_2⟩
}
end
example : ¬(p ∨ q) ↔ ¬p ∧ ¬q := begin
apply iff.intro, {
intro h,
exact ⟨λ hp, h $ or.inl hp, λ hq, h $ or.inr hq⟩
}, {
intro h,
intro hpq, cases hpq,
exact h.1 hpq,
exact h.2 hpq,
}
end
example : ¬p ∨ ¬q → ¬(p ∧ q) := begin
intro h,
intro hpq,
cases h,
exact h hpq.1,
exact h hpq.2,
end
example : ¬(p ∧ ¬p) := begin
intro h, exact h.2 h.1
end
example : p ∧ ¬q → ¬(p → q) := begin
intro h1, intro h2, exact (h1.2 $ h2 h1.1)
end
example : ¬p → (p → q) := begin
intros, contradiction
end
example : (¬p ∨ q) → (p → q) := begin
intros, cases a,
contradiction,
assumption
end
example : p ∨ false ↔ p := begin
apply iff.intro, {
intro h, cases h,
assumption,
contradiction,
}, {
intro h, left, assumption
}
end
example : p ∧ false ↔ false := begin
apply iff.intro, {
intro h, exact h.2
}, {
intro, contradiction
}
end
example : ¬(p ↔ ¬p) := begin
intro hc, simp at hc, assumption
end
example : (p → q) → (¬q → ¬p) := begin
intro h, intro hnq, exact (λ hp, hnq $ h hp)
end
|
a5fe55b0086bad2a6c296a7c83664a8635f96bda | 947b78d97130d56365ae2ec264df196ce769371a | /tests/compiler/float_cases_bug.lean | 14eb1fe19ba6cd7867d11368a30059a039bd2d0f | [
"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 | 914 | lean | inductive Term : Type
| const : Nat -> Term
| app : List Term -> Term
namespace Term
instance : Inhabited Term := ⟨Term.const 0⟩
partial def hasToString : Term -> String | const n => "CONST(" ++ toString n ++ ")" | app ts => "APP"
instance : HasToString Term := ⟨hasToString⟩
end Term
open Term
structure MyState : Type := (ts : List Term)
def emit (t : Term) : StateM MyState Unit := modify (λ ms => ⟨t::ms.ts⟩)
partial def foo : MyState -> Term -> Term -> List Term
| ms₀, t, u =>
let stateT : StateM MyState Unit := do {
match t with
| const _ => pure ()
| app _ => emit (const 1);
match t, u with
| app _, app _ => emit (app [])
| _, _ => pure () ;
match t, u with
| app _, app _ => emit (app [])
| _, _ => emit (const 2)
} ;
(stateT.run ⟨[]⟩).2.ts.reverse
def main : IO Unit := IO.println $ foo ⟨[]⟩ (app []) (app [])
|
a01929c19eb3c3ccaaced6958dd037c2c0877099 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/category_theory/limits/shapes/multiequalizer.lean | 5cc5edaf285dbfbc1b7d38249f8085ed6d5fd3bf | [
"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 | 26,576 | lean | /-
Copyright (c) 2021 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import category_theory.limits.shapes.products
import category_theory.limits.shapes.equalizers
import category_theory.limits.cone_category
import category_theory.adjunction
/-!
# Multi-(co)equalizers
A *multiequalizer* is an equalizer of two morphisms between two products.
Since both products and equalizers are limits, such an object is again a limit.
This file provides the diagram whose limit is indeed such an object.
In fact, it is well-known that any limit can be obtained as a multiequalizer.
The dual construction (multicoequalizers) is also provided.
## Projects
Prove that a multiequalizer can be identified with
an equalizer between products (and analogously for multicoequalizers).
Prove that the limit of any diagram is a multiequalizer (and similarly for colimits).
-/
namespace category_theory.limits
open category_theory
universes w v u
/-- The type underlying the multiequalizer diagram. -/
@[nolint unused_arguments]
inductive walking_multicospan {L R : Type w} (fst snd : R → L) : Type w
| left : L → walking_multicospan
| right : R → walking_multicospan
/-- The type underlying the multiecoqualizer diagram. -/
@[nolint unused_arguments]
inductive walking_multispan {L R : Type w} (fst snd : L → R) : Type w
| left : L → walking_multispan
| right : R → walking_multispan
namespace walking_multicospan
variables {L R : Type w} {fst snd : R → L}
instance [inhabited L] : inhabited (walking_multicospan fst snd) :=
⟨left default⟩
/-- Morphisms for `walking_multicospan`. -/
inductive hom : Π (a b : walking_multicospan fst snd), Type w
| id (A) : hom A A
| fst (b) : hom (left (fst b)) (right b)
| snd (b) : hom (left (snd b)) (right b)
instance {a : walking_multicospan fst snd} : inhabited (hom a a) :=
⟨hom.id _⟩
/-- Composition of morphisms for `walking_multicospan`. -/
def hom.comp : Π {A B C : walking_multicospan fst snd} (f : hom A B) (g : hom B C),
hom A C
| _ _ _ (hom.id X) f := f
| _ _ _ (hom.fst b) (hom.id X) := hom.fst b
| _ _ _ (hom.snd b) (hom.id X) := hom.snd b
instance : small_category (walking_multicospan fst snd) :=
{ hom := hom,
id := hom.id,
comp := λ X Y Z, hom.comp,
id_comp' := by { rintro (_|_) (_|_) (_|_|_), tidy },
comp_id' := by { rintro (_|_) (_|_) (_|_|_), tidy },
assoc' := by { rintro (_|_) (_|_) (_|_) (_|_) (_|_|_) (_|_|_) (_|_|_), tidy } }
end walking_multicospan
namespace walking_multispan
variables {L R : Type v} {fst snd : L → R}
instance [inhabited L] : inhabited (walking_multispan fst snd) :=
⟨left default⟩
/-- Morphisms for `walking_multispan`. -/
inductive hom : Π (a b : walking_multispan fst snd), Type v
| id (A) : hom A A
| fst (a) : hom (left a) (right (fst a))
| snd (a) : hom (left a) (right (snd a))
instance {a : walking_multispan fst snd} : inhabited (hom a a) :=
⟨hom.id _⟩
/-- Composition of morphisms for `walking_multispan`. -/
def hom.comp : Π {A B C : walking_multispan fst snd} (f : hom A B) (g : hom B C),
hom A C
| _ _ _ (hom.id X) f := f
| _ _ _ (hom.fst a) (hom.id X) := hom.fst a
| _ _ _ (hom.snd a) (hom.id X) := hom.snd a
instance : small_category (walking_multispan fst snd) :=
{ hom := hom,
id := hom.id,
comp := λ X Y Z, hom.comp,
id_comp' := by { rintro (_|_) (_|_) (_|_|_), tidy },
comp_id' := by { rintro (_|_) (_|_) (_|_|_), tidy },
assoc' := by { rintro (_|_) (_|_) (_|_) (_|_) (_|_|_) (_|_|_) (_|_|_), tidy } }
end walking_multispan
/-- This is a structure encapsulating the data necessary to define a `multicospan`. -/
@[nolint has_nonempty_instance]
structure multicospan_index (C : Type u) [category.{v} C] :=
(L R : Type w)
(fst_to snd_to : R → L)
(left : L → C)
(right : R → C)
(fst : Π b, left (fst_to b) ⟶ right b)
(snd : Π b, left (snd_to b) ⟶ right b)
/-- This is a structure encapsulating the data necessary to define a `multispan`. -/
@[nolint has_nonempty_instance]
structure multispan_index (C : Type u) [category.{v} C] :=
(L R : Type w)
(fst_from snd_from : L → R)
(left : L → C)
(right : R → C)
(fst : Π a, left a ⟶ right (fst_from a))
(snd : Π a, left a ⟶ right (snd_from a))
namespace multicospan_index
variables {C : Type u} [category.{v} C] (I : multicospan_index C)
/-- The multicospan associated to `I : multicospan_index`. -/
def multicospan : walking_multicospan I.fst_to I.snd_to ⥤ C :=
{ obj := λ x,
match x with
| walking_multicospan.left a := I.left a
| walking_multicospan.right b := I.right b
end,
map := λ x y f,
match x, y, f with
| _, _, walking_multicospan.hom.id x := 𝟙 _
| _, _, walking_multicospan.hom.fst b := I.fst _
| _, _, walking_multicospan.hom.snd b := I.snd _
end,
map_id' := by { rintros (_|_), tidy },
map_comp' := by { rintros (_|_) (_|_) (_|_) (_|_|_) (_|_|_), tidy } }
@[simp] lemma multicospan_obj_left (a) :
I.multicospan.obj (walking_multicospan.left a) = I.left a := rfl
@[simp] lemma multicospan_obj_right (b) :
I.multicospan.obj (walking_multicospan.right b) = I.right b := rfl
@[simp] lemma multicospan_map_fst (b) :
I.multicospan.map (walking_multicospan.hom.fst b) = I.fst b := rfl
@[simp] lemma multicospan_map_snd (b) :
I.multicospan.map (walking_multicospan.hom.snd b) = I.snd b := rfl
variables [has_product I.left] [has_product I.right]
/-- The induced map `∏ I.left ⟶ ∏ I.right` via `I.fst`. -/
noncomputable
def fst_pi_map : ∏ I.left ⟶ ∏ I.right := pi.lift (λ b, pi.π I.left (I.fst_to b) ≫ I.fst b)
/-- The induced map `∏ I.left ⟶ ∏ I.right` via `I.snd`. -/
noncomputable
def snd_pi_map : ∏ I.left ⟶ ∏ I.right := pi.lift (λ b, pi.π I.left (I.snd_to b) ≫ I.snd b)
@[simp, reassoc]
lemma fst_pi_map_π (b) : I.fst_pi_map ≫ pi.π I.right b = pi.π I.left _ ≫ I.fst b :=
by simp [fst_pi_map]
@[simp, reassoc]
lemma snd_pi_map_π (b) : I.snd_pi_map ≫ pi.π I.right b = pi.π I.left _ ≫ I.snd b :=
by simp [snd_pi_map]
/--
Taking the multiequalizer over the multicospan index is equivalent to taking the equalizer over
the two morphsims `∏ I.left ⇉ ∏ I.right`. This is the diagram of the latter.
-/
@[simps] protected noncomputable
def parallel_pair_diagram := parallel_pair I.fst_pi_map I.snd_pi_map
end multicospan_index
namespace multispan_index
variables {C : Type u} [category.{v} C] (I : multispan_index C)
/-- The multispan associated to `I : multispan_index`. -/
def multispan : walking_multispan I.fst_from I.snd_from ⥤ C :=
{ obj := λ x,
match x with
| walking_multispan.left a := I.left a
| walking_multispan.right b := I.right b
end,
map := λ x y f,
match x, y, f with
| _, _, walking_multispan.hom.id x := 𝟙 _
| _, _, walking_multispan.hom.fst b := I.fst _
| _, _, walking_multispan.hom.snd b := I.snd _
end,
map_id' := by { rintros (_|_), tidy },
map_comp' := by { rintros (_|_) (_|_) (_|_) (_|_|_) (_|_|_), tidy } }
@[simp] lemma multispan_obj_left (a) :
I.multispan.obj (walking_multispan.left a) = I.left a := rfl
@[simp] lemma multispan_obj_right (b) :
I.multispan.obj (walking_multispan.right b) = I.right b := rfl
@[simp] lemma multispan_map_fst (a) :
I.multispan.map (walking_multispan.hom.fst a) = I.fst a := rfl
@[simp] lemma multispan_map_snd (a) :
I.multispan.map (walking_multispan.hom.snd a) = I.snd a := rfl
variables [has_coproduct I.left] [has_coproduct I.right]
/-- The induced map `∐ I.left ⟶ ∐ I.right` via `I.fst`. -/
noncomputable
def fst_sigma_map : ∐ I.left ⟶ ∐ I.right := sigma.desc (λ b, I.fst b ≫ sigma.ι _ (I.fst_from b))
/-- The induced map `∐ I.left ⟶ ∐ I.right` via `I.snd`. -/
noncomputable
def snd_sigma_map : ∐ I.left ⟶ ∐ I.right := sigma.desc (λ b, I.snd b ≫ sigma.ι _ (I.snd_from b))
@[simp, reassoc]
lemma ι_fst_sigma_map (b) : sigma.ι I.left b ≫ I.fst_sigma_map = I.fst b ≫ sigma.ι I.right _ :=
by simp [fst_sigma_map]
@[simp, reassoc]
lemma ι_snd_sigma_map (b) : sigma.ι I.left b ≫ I.snd_sigma_map = I.snd b ≫ sigma.ι I.right _ :=
by simp [snd_sigma_map]
/--
Taking the multicoequalizer over the multispan index is equivalent to taking the coequalizer over
the two morphsims `∐ I.left ⇉ ∐ I.right`. This is the diagram of the latter.
-/
protected noncomputable
abbreviation parallel_pair_diagram := parallel_pair I.fst_sigma_map I.snd_sigma_map
end multispan_index
variables {C : Type u} [category.{v} C]
/-- A multifork is a cone over a multicospan. -/
@[nolint has_nonempty_instance]
abbreviation multifork (I : multicospan_index C) := cone I.multicospan
/-- A multicofork is a cocone over a multispan. -/
@[nolint has_nonempty_instance]
abbreviation multicofork (I : multispan_index C) := cocone I.multispan
namespace multifork
variables {I : multicospan_index C} (K : multifork I)
/-- The maps from the cone point of a multifork to the objects on the left. -/
def ι (a : I.L) : K.X ⟶ I.left a := K.π.app (walking_multicospan.left _)
@[simp] lemma app_left_eq_ι (a) : K.π.app (walking_multicospan.left a) = K.ι a := rfl
@[simp] lemma app_right_eq_ι_comp_fst (b) :
K.π.app (walking_multicospan.right b) = K.ι (I.fst_to b) ≫ I.fst b :=
by { rw ← K.w (walking_multicospan.hom.fst b), refl }
@[reassoc] lemma app_right_eq_ι_comp_snd (b) :
K.π.app (walking_multicospan.right b) = K.ι (I.snd_to b) ≫ I.snd b :=
by { rw ← K.w (walking_multicospan.hom.snd b), refl }
@[simp, reassoc] lemma hom_comp_ι (K₁ K₂ : multifork I) (f : K₁ ⟶ K₂) (j : I.L) :
f.hom ≫ K₂.ι j = K₁.ι j := f.w (walking_multicospan.left j)
/-- Construct a multifork using a collection `ι` of morphisms. -/
@[simps]
def of_ι (I : multicospan_index C) (P : C) (ι : Π a, P ⟶ I.left a)
(w : ∀ b, ι (I.fst_to b) ≫ I.fst b = ι (I.snd_to b) ≫ I.snd b) :
multifork I :=
{ X := P,
π :=
{ app := λ x,
match x with
| walking_multicospan.left a := ι _
| walking_multicospan.right b := ι (I.fst_to b) ≫ I.fst b
end,
naturality' := begin
rintros (_|_) (_|_) (_|_|_),
any_goals { symmetry, dsimp, rw category.id_comp, apply category.comp_id },
{ dsimp, rw category.id_comp, refl },
{ dsimp, rw category.id_comp, apply w }
end } }
@[simp, reassoc]
lemma condition (b) :
K.ι (I.fst_to b) ≫ I.fst b = K.ι (I.snd_to b) ≫ I.snd b :=
by rw [←app_right_eq_ι_comp_fst, ←app_right_eq_ι_comp_snd]
/-- This definition provides a convenient way to show that a multifork is a limit. -/
@[simps]
def is_limit.mk
(lift : Π (E : multifork I), E.X ⟶ K.X)
(fac : ∀ (E : multifork I) (i : I.L), lift E ≫ K.ι i = E.ι i)
(uniq : ∀ (E : multifork I) (m : E.X ⟶ K.X),
(∀ i : I.L, m ≫ K.ι i = E.ι i) → m = lift E) : is_limit K :=
{ lift := lift,
fac' := begin
rintros E (a|b),
{ apply fac },
{ rw [← E.w (walking_multicospan.hom.fst b), ← K.w (walking_multicospan.hom.fst b),
← category.assoc],
congr' 1,
apply fac }
end,
uniq' := begin
rintros E m hm,
apply uniq,
intros i,
apply hm,
end }
variables [has_product I.left] [has_product I.right]
@[simp, reassoc]
lemma pi_condition : pi.lift K.ι ≫ I.fst_pi_map = pi.lift K.ι ≫ I.snd_pi_map :=
by { ext, discrete_cases, simp, }
/-- Given a multifork, we may obtain a fork over `∏ I.left ⇉ ∏ I.right`. -/
@[simps X] noncomputable
def to_pi_fork (K : multifork I) : fork I.fst_pi_map I.snd_pi_map :=
{ X := K.X,
π :=
{ app := λ x,
match x with
| walking_parallel_pair.zero := pi.lift K.ι
| walking_parallel_pair.one := pi.lift K.ι ≫ I.fst_pi_map
end,
naturality' :=
begin
rintros (_|_) (_|_) (_|_|_),
any_goals { symmetry, dsimp, rw category.id_comp, apply category.comp_id },
all_goals { change 𝟙 _ ≫ _ ≫ _ = pi.lift _ ≫ _, simp }
end } }
@[simp] lemma to_pi_fork_π_app_zero : K.to_pi_fork.ι = pi.lift K.ι := rfl
@[simp] lemma to_pi_fork_π_app_one :
K.to_pi_fork.π.app walking_parallel_pair.one = pi.lift K.ι ≫ I.fst_pi_map := rfl
variable (I)
/-- Given a fork over `∏ I.left ⇉ ∏ I.right`, we may obtain a multifork. -/
@[simps X] noncomputable
def of_pi_fork (c : fork I.fst_pi_map I.snd_pi_map) : multifork I :=
{ X := c.X,
π :=
{ app := λ x,
match x with
| walking_multicospan.left a := c.ι ≫ pi.π _ _
| walking_multicospan.right b := c.ι ≫ I.fst_pi_map ≫ pi.π _ _
end,
naturality' :=
begin
rintros (_|_) (_|_) (_|_|_),
any_goals { symmetry, dsimp, rw category.id_comp, apply category.comp_id },
{ change 𝟙 _ ≫ _ ≫ _ = (_ ≫ _) ≫ _, simp },
{ change 𝟙 _ ≫ _ ≫ _ = (_ ≫ _) ≫ _, rw c.condition_assoc, simp }
end } }
@[simp] lemma of_pi_fork_π_app_left (c : fork I.fst_pi_map I.snd_pi_map) (a) :
(of_pi_fork I c).ι a = c.ι ≫ pi.π _ _ := rfl
@[simp] lemma of_pi_fork_π_app_right (c : fork I.fst_pi_map I.snd_pi_map) (a) :
(of_pi_fork I c).π.app (walking_multicospan.right a) = c.ι ≫ I.fst_pi_map ≫ pi.π _ _ := rfl
end multifork
namespace multicospan_index
variables (I : multicospan_index C) [has_product I.left] [has_product I.right]
local attribute [tidy] tactic.case_bash
/-- `multifork.to_pi_fork` is functorial. -/
@[simps] noncomputable
def to_pi_fork_functor : multifork I ⥤ fork I.fst_pi_map I.snd_pi_map :=
{ obj := multifork.to_pi_fork,
map := λ K₁ K₂ f,
{ hom := f.hom,
w' := begin
rintro (_|_),
{ ext, dsimp, simp },
{ ext,
simp only [multifork.to_pi_fork_π_app_one, multifork.pi_condition, category.assoc],
dsimp [snd_pi_map],
simp },
end } }
/-- `multifork.of_pi_fork` is functorial. -/
@[simps] noncomputable
def of_pi_fork_functor : fork I.fst_pi_map I.snd_pi_map ⥤ multifork I :=
{ obj := multifork.of_pi_fork I, map := λ K₁ K₂ f, { hom := f.hom, w' := by rintros (_|_); simp } }
/--
The category of multiforks is equivalent to the category of forks over `∏ I.left ⇉ ∏ I.right`.
It then follows from `category_theory.is_limit_of_preserves_cone_terminal` (or `reflects`) that it
preserves and reflects limit cones.
-/
@[simps] noncomputable
def multifork_equiv_pi_fork : multifork I ≌ fork I.fst_pi_map I.snd_pi_map :=
{ functor := to_pi_fork_functor I,
inverse := of_pi_fork_functor I,
unit_iso := nat_iso.of_components (λ K, cones.ext (iso.refl _)
(by { rintros (_|_); dsimp; simp[←fork.app_one_eq_ι_comp_left, -fork.app_one_eq_ι_comp_left] }))
(λ K₁ K₂ f, by { ext, simp }),
counit_iso := nat_iso.of_components (λ K, fork.ext (iso.refl _) (by { ext ⟨j⟩, dsimp, simp }))
(λ K₁ K₂ f, by { ext, simp }) }
end multicospan_index
namespace multicofork
variables {I : multispan_index C} (K : multicofork I)
/-- The maps to the cocone point of a multicofork from the objects on the right. -/
def π (b : I.R) : I.right b ⟶ K.X :=
K.ι.app (walking_multispan.right _)
@[simp] lemma π_eq_app_right (b) : K.ι.app (walking_multispan.right _) = K.π b := rfl
@[simp] lemma fst_app_right (a) :
K.ι.app (walking_multispan.left a) = I.fst a ≫ K.π _ :=
by { rw ← K.w (walking_multispan.hom.fst a), refl }
@[reassoc] lemma snd_app_right (a) :
K.ι.app (walking_multispan.left a) = I.snd a ≫ K.π _ :=
by { rw ← K.w (walking_multispan.hom.snd a), refl }
/-- Construct a multicofork using a collection `π` of morphisms. -/
@[simps]
def of_π (I : multispan_index C) (P : C) (π : Π b, I.right b ⟶ P)
(w : ∀ a, I.fst a ≫ π (I.fst_from a) = I.snd a ≫ π (I.snd_from a)) :
multicofork I :=
{ X := P,
ι :=
{ app := λ x,
match x with
| walking_multispan.left a := I.fst a ≫ π _
| walking_multispan.right b := π _
end,
naturality' := begin
rintros (_|_) (_|_) (_|_|_),
any_goals { dsimp, rw category.comp_id, apply category.id_comp },
{ dsimp, rw category.comp_id, refl },
{ dsimp, rw category.comp_id, apply (w _).symm }
end } }
@[simp, reassoc]
lemma condition (a) : I.fst a ≫ K.π (I.fst_from a) = I.snd a ≫ K.π (I.snd_from a) :=
by rw [←K.snd_app_right, ←K.fst_app_right]
/-- This definition provides a convenient way to show that a multicofork is a colimit. -/
@[simps]
def is_colimit.mk
(desc : Π (E : multicofork I), K.X ⟶ E.X)
(fac : ∀ (E : multicofork I) (i : I.R), K.π i ≫ desc E = E.π i)
(uniq : ∀ (E : multicofork I) (m : K.X ⟶ E.X),
(∀ i : I.R, K.π i ≫ m = E.π i) → m = desc E) : is_colimit K :=
{ desc := desc,
fac' := begin
rintros S (a|b),
{ rw [← K.w (walking_multispan.hom.fst a), ← S.w (walking_multispan.hom.fst a),
category.assoc],
congr' 1,
apply fac },
{ apply fac },
end,
uniq' := begin
intros S m hm,
apply uniq,
intros i,
apply hm
end }
variables [has_coproduct I.left] [has_coproduct I.right]
@[simp, reassoc]
lemma sigma_condition :
I.fst_sigma_map ≫ sigma.desc K.π = I.snd_sigma_map ≫ sigma.desc K.π :=
by { ext, discrete_cases, simp, }
/-- Given a multicofork, we may obtain a cofork over `∐ I.left ⇉ ∐ I.right`. -/
@[simps X] noncomputable
def to_sigma_cofork (K : multicofork I) : cofork I.fst_sigma_map I.snd_sigma_map :=
{ X := K.X,
ι :=
{ app := λ x,
match x with
| walking_parallel_pair.zero := I.fst_sigma_map ≫ sigma.desc K.π
| walking_parallel_pair.one := sigma.desc K.π
end,
naturality' :=
begin
rintros (_|_) (_|_) (_|_|_),
any_goals { dsimp, rw category.comp_id, apply category.id_comp },
all_goals { change _ ≫ sigma.desc _ = (_ ≫ _) ≫ 𝟙 _, simp }
end } }
@[simp] lemma to_sigma_cofork_π : K.to_sigma_cofork.π = sigma.desc K.π := rfl
variable (I)
/-- Given a cofork over `∐ I.left ⇉ ∐ I.right`, we may obtain a multicofork. -/
@[simps X] noncomputable
def of_sigma_cofork (c : cofork I.fst_sigma_map I.snd_sigma_map) : multicofork I :=
{ X := c.X,
ι :=
{ app := λ x,
match x with
| walking_multispan.left a := (sigma.ι I.left a : _) ≫ I.fst_sigma_map ≫ c.π
| walking_multispan.right b := (sigma.ι I.right b : _) ≫ c.π
end,
naturality' :=
begin
rintros (_|_) (_|_) (_|_|_),
any_goals { dsimp, rw category.comp_id, apply category.id_comp },
{ change _ ≫ _ ≫ _ = (_ ≫ _) ≫ _, dsimp,
simp only [cofork.condition, category.comp_id],
rw [←I.ι_fst_sigma_map_assoc, c.condition] },
{ change _ ≫ _ ≫ _ = (_ ≫ _) ≫ 𝟙 _,
rw c.condition, simp }
end } }
@[simp] lemma of_sigma_cofork_ι_app_left (c : cofork I.fst_sigma_map I.snd_sigma_map) (a) :
(of_sigma_cofork I c).ι.app (walking_multispan.left a) =
(sigma.ι I.left a : _) ≫ I.fst_sigma_map ≫ c.π := rfl
@[simp] lemma of_sigma_cofork_ι_app_right (c : cofork I.fst_sigma_map I.snd_sigma_map) (b) :
(of_sigma_cofork I c).ι.app (walking_multispan.right b) = (sigma.ι I.right b : _) ≫ c.π := rfl
end multicofork
namespace multispan_index
variables (I : multispan_index C) [has_coproduct I.left] [has_coproduct I.right]
local attribute [tidy] tactic.case_bash
/-- `multicofork.to_sigma_cofork` is functorial. -/
@[simps] noncomputable
def to_sigma_cofork_functor : multicofork I ⥤ cofork I.fst_sigma_map I.snd_sigma_map :=
{ obj := multicofork.to_sigma_cofork, map := λ K₁ K₂ f, { hom := f.hom } }
/-- `multicofork.of_sigma_cofork` is functorial. -/
@[simps] noncomputable
def of_sigma_cofork_functor : cofork I.fst_sigma_map I.snd_sigma_map ⥤ multicofork I :=
{ obj := multicofork.of_sigma_cofork I,
map := λ K₁ K₂ f, { hom := f.hom, w' := by rintros (_|_); simp } }
/--
The category of multicoforks is equivalent to the category of coforks over `∐ I.left ⇉ ∐ I.right`.
It then follows from `category_theory.is_colimit_of_preserves_cocone_initial` (or `reflects`) that
it preserves and reflects colimit cocones.
-/
@[simps] noncomputable
def multicofork_equiv_sigma_cofork : multicofork I ≌ cofork I.fst_sigma_map I.snd_sigma_map :=
{ functor := to_sigma_cofork_functor I,
inverse := of_sigma_cofork_functor I,
unit_iso := nat_iso.of_components (λ K, cocones.ext (iso.refl _)
(by { rintros (_|_); dsimp; simp }))
(λ K₁ K₂ f, by { ext, simp }),
counit_iso := nat_iso.of_components (λ K, cofork.ext (iso.refl _)
(by { ext ⟨j⟩, dsimp, simp only [category.comp_id, colimit.ι_desc, cofan.mk_ι_app], refl }))
(λ K₁ K₂ f, by { ext, dsimp, simp, }) }
end multispan_index
/-- For `I : multicospan_index C`, we say that it has a multiequalizer if the associated
multicospan has a limit. -/
abbreviation has_multiequalizer (I : multicospan_index C) :=
has_limit I.multicospan
noncomputable theory
/-- The multiequalizer of `I : multicospan_index C`. -/
abbreviation multiequalizer (I : multicospan_index C) [has_multiequalizer I] : C :=
limit I.multicospan
/-- For `I : multispan_index C`, we say that it has a multicoequalizer if
the associated multicospan has a limit. -/
abbreviation has_multicoequalizer (I : multispan_index C) :=
has_colimit I.multispan
/-- The multiecoqualizer of `I : multispan_index C`. -/
abbreviation multicoequalizer (I : multispan_index C) [has_multicoequalizer I] : C :=
colimit I.multispan
namespace multiequalizer
variables (I : multicospan_index C) [has_multiequalizer I]
/-- The canonical map from the multiequalizer to the objects on the left. -/
abbreviation ι (a : I.L) : multiequalizer I ⟶ I.left a :=
limit.π _ (walking_multicospan.left a)
/-- The multifork associated to the multiequalizer. -/
abbreviation multifork : multifork I :=
limit.cone _
@[simp]
lemma multifork_ι (a) :
(multiequalizer.multifork I).ι a = multiequalizer.ι I a := rfl
@[simp]
lemma multifork_π_app_left (a) :
(multiequalizer.multifork I).π.app (walking_multicospan.left a) =
multiequalizer.ι I a := rfl
@[reassoc]
lemma condition (b) :
multiequalizer.ι I (I.fst_to b) ≫ I.fst b =
multiequalizer.ι I (I.snd_to b) ≫ I.snd b :=
multifork.condition _ _
/-- Construct a morphism to the multiequalizer from its universal property. -/
abbreviation lift (W : C) (k : Π a, W ⟶ I.left a)
(h : ∀ b, k (I.fst_to b) ≫ I.fst b = k (I.snd_to b) ≫ I.snd b) :
W ⟶ multiequalizer I :=
limit.lift _ (multifork.of_ι I _ k h)
@[simp, reassoc]
lemma lift_ι (W : C) (k : Π a, W ⟶ I.left a)
(h : ∀ b, k (I.fst_to b) ≫ I.fst b = k (I.snd_to b) ≫ I.snd b) (a) :
multiequalizer.lift I _ k h ≫ multiequalizer.ι I a = k _ :=
limit.lift_π _ _
@[ext]
lemma hom_ext {W : C} (i j : W ⟶ multiequalizer I)
(h : ∀ a, i ≫ multiequalizer.ι I a =
j ≫ multiequalizer.ι I a) :
i = j :=
limit.hom_ext
begin
rintro (a|b),
{ apply h },
simp_rw [← limit.w I.multicospan (walking_multicospan.hom.fst b),
← category.assoc, h],
end
variables [has_product I.left] [has_product I.right]
instance : has_equalizer I.fst_pi_map I.snd_pi_map :=
⟨⟨⟨_,is_limit.of_preserves_cone_terminal
I.multifork_equiv_pi_fork.functor (limit.is_limit _)⟩⟩⟩
/-- The multiequalizer is isomorphic to the equalizer of `∏ I.left ⇉ ∏ I.right`. -/
def iso_equalizer : multiequalizer I ≅ equalizer I.fst_pi_map I.snd_pi_map :=
limit.iso_limit_cone ⟨_, is_limit.of_preserves_cone_terminal
I.multifork_equiv_pi_fork.inverse (limit.is_limit _)⟩
/-- The canonical injection `multiequalizer I ⟶ ∏ I.left`. -/
def ι_pi : multiequalizer I ⟶ ∏ I.left :=
(iso_equalizer I).hom ≫ equalizer.ι I.fst_pi_map I.snd_pi_map
@[simp, reassoc]
lemma ι_pi_π (a) : ι_pi I ≫ pi.π I.left a = ι I a :=
by { rw [ι_pi, category.assoc, ← iso.eq_inv_comp, iso_equalizer], simpa }
instance : mono (ι_pi I) := @@mono_comp _ _ _ _ equalizer.ι_mono
end multiequalizer
namespace multicoequalizer
variables (I : multispan_index C) [has_multicoequalizer I]
/-- The canonical map from the multiequalizer to the objects on the left. -/
abbreviation π (b : I.R) : I.right b ⟶ multicoequalizer I :=
colimit.ι I.multispan (walking_multispan.right _)
/-- The multicofork associated to the multicoequalizer. -/
abbreviation multicofork : multicofork I :=
colimit.cocone _
@[simp]
lemma multicofork_π (b) :
(multicoequalizer.multicofork I).π b = multicoequalizer.π I b := rfl
@[simp]
lemma multicofork_ι_app_right (b) :
(multicoequalizer.multicofork I).ι.app (walking_multispan.right b) =
multicoequalizer.π I b := rfl
@[reassoc]
lemma condition (a) :
I.fst a ≫ multicoequalizer.π I (I.fst_from a) =
I.snd a ≫ multicoequalizer.π I (I.snd_from a) :=
multicofork.condition _ _
/-- Construct a morphism from the multicoequalizer from its universal property. -/
abbreviation desc (W : C) (k : Π b, I.right b ⟶ W)
(h : ∀ a, I.fst a ≫ k (I.fst_from a) = I.snd a ≫ k (I.snd_from a)) :
multicoequalizer I ⟶ W :=
colimit.desc _ (multicofork.of_π I _ k h)
@[simp, reassoc]
lemma π_desc (W : C) (k : Π b, I.right b ⟶ W)
(h : ∀ a, I.fst a ≫ k (I.fst_from a) = I.snd a ≫ k (I.snd_from a)) (b) :
multicoequalizer.π I b ≫ multicoequalizer.desc I _ k h = k _ :=
colimit.ι_desc _ _
@[ext]
lemma hom_ext {W : C} (i j : multicoequalizer I ⟶ W)
(h : ∀ b, multicoequalizer.π I b ≫ i = multicoequalizer.π I b ≫ j) :
i = j :=
colimit.hom_ext
begin
rintro (a|b),
{ simp_rw [← colimit.w I.multispan (walking_multispan.hom.fst a),
category.assoc, h] },
{ apply h },
end
variables [has_coproduct I.left] [has_coproduct I.right]
instance : has_coequalizer I.fst_sigma_map I.snd_sigma_map :=
⟨⟨⟨_,is_colimit.of_preserves_cocone_initial
I.multicofork_equiv_sigma_cofork.functor (colimit.is_colimit _)⟩⟩⟩
/-- The multicoequalizer is isomorphic to the coequalizer of `∐ I.left ⇉ ∐ I.right`. -/
def iso_coequalizer : multicoequalizer I ≅ coequalizer I.fst_sigma_map I.snd_sigma_map :=
colimit.iso_colimit_cocone ⟨_, is_colimit.of_preserves_cocone_initial
I.multicofork_equiv_sigma_cofork.inverse (colimit.is_colimit _)⟩
/-- The canonical projection `∐ I.right ⟶ multicoequalizer I`. -/
def sigma_π : ∐ I.right ⟶ multicoequalizer I :=
coequalizer.π I.fst_sigma_map I.snd_sigma_map ≫ (iso_coequalizer I).inv
@[simp, reassoc]
lemma ι_sigma_π (b) : sigma.ι I.right b ≫ sigma_π I = π I b :=
by { rw [sigma_π, ← category.assoc, iso.comp_inv_eq, iso_coequalizer], simpa }
instance : epi (sigma_π I) := @@epi_comp _ _ coequalizer.π_epi _ _
end multicoequalizer
end category_theory.limits
|
5fd4cbd3c91a836cb51d85dc32d564c927a91dcf | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/tactic/omega/nat/main.lean | f006f1385f30784096cb4f2954840e49ab963662 | [
"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 | 8,978 | 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
open_locale omega.nat
run_cmd mk_simp_attr `sugar_nat
attribute [sugar_nat]
ne not_le not_lt
nat.lt_iff_add_one_le
nat.succ_eq_add_one
or_false false_or
and_true true_and
ge gt mul_add add_mul mul_comm
one_mul mul_one
imp_iff_not_or
iff_iff_not_or_and_or_not
meta def desugar := `[try {simp only with sugar_nat at *}]
lemma univ_close_of_unsat_neg_elim_not (m) (p : preform) :
(neg_elim (¬* p)).unsat → univ_close p (λ _, 0) m :=
begin
intro h1, apply univ_close_of_valid,
apply valid_of_unsat_not, intro h2, apply h1,
apply preform.sat_of_implies_of_sat implies_neg_elim h2,
end
/-- Return expr of proof that argument is free of subtractions -/
meta def preterm.prove_sub_free : preterm → tactic expr
| (& m) := return `(trivial)
| (m ** n) := return `(trivial)
| (t +* s) :=
do x ← preterm.prove_sub_free t,
y ← preterm.prove_sub_free s,
return `(@and.intro (preterm.sub_free %%`(t))
(preterm.sub_free %%`(s)) %%x %%y)
| (_ -* _) := failed
/-- Return expr of proof that argument is free of negations -/
meta def prove_neg_free : preform → tactic expr
| (t =* s) := return `(trivial)
| (t ≤* s) := return `(trivial)
| (p ∨* q) :=
do x ← prove_neg_free p,
y ← prove_neg_free q,
return `(@and.intro (preform.neg_free %%`(p))
(preform.neg_free %%`(q)) %%x %%y)
| (p ∧* q) :=
do x ← prove_neg_free p,
y ← prove_neg_free q,
return `(@and.intro (preform.neg_free %%`(p))
(preform.neg_free %%`(q)) %%x %%y)
| _ := failed
/-- Return expr of proof that argument is free of subtractions -/
meta def prove_sub_free : preform → tactic expr
| (t =* s) :=
do x ← preterm.prove_sub_free t,
y ← preterm.prove_sub_free s,
return `(@and.intro (preterm.sub_free %%`(t))
(preterm.sub_free %%`(s)) %%x %%y)
| (t ≤* s) :=
do x ← preterm.prove_sub_free t,
y ← preterm.prove_sub_free s,
return `(@and.intro (preterm.sub_free %%`(t))
(preterm.sub_free %%`(s)) %%x %%y)
| (¬*p) := prove_sub_free p
| (p ∨* q) :=
do x ← prove_sub_free p,
y ← prove_sub_free q,
return `(@and.intro (preform.sub_free %%`(p))
(preform.sub_free %%`(q)) %%x %%y)
| (p ∧* q) :=
do x ← prove_sub_free p,
y ← prove_sub_free q,
return `(@and.intro (preform.sub_free %%`(p))
(preform.sub_free %%`(q)) %%x %%y)
/-- Given a p : preform, return the expr of a term t : p.unsat, where p is subtraction- and negation-free. -/
meta def prove_unsat_sub_free (p : preform) : tactic expr :=
do x ← prove_neg_free p,
y ← prove_sub_free p,
z ← prove_unsats (dnf p),
return `(unsat_of_unsat_dnf %%`(p) %%x %%y %%z)
/-- Given a p : preform, return the expr of a term t : p.unsat, where p is negation-free. -/
meta def prove_unsat_neg_free : preform → tactic expr | p :=
match p.sub_terms with
| none := prove_unsat_sub_free p
| (some (t,s)) :=
do x ← prove_unsat_neg_free (sub_elim t s p),
return `(unsat_of_unsat_sub_elim %%`(t) %%`(s) %%`(p) %%x)
end
/-- Given a (m : nat) and (p : preform), return the expr of (t : univ_close m p). -/
meta def prove_univ_close (m : nat) (p : preform) : tactic expr :=
do x ← prove_unsat_neg_free (neg_elim (¬*p)),
to_expr ``(univ_close_of_unsat_neg_elim_not %%`(m) %%`(p) %%x)
/-- Reification to imtermediate shadow syntax that retains exprs -/
meta def to_exprterm : expr → tactic exprterm
| `(%%x * %%y) :=
do m ← eval_expr' nat y,
return (exprterm.exp m x)
| `(%%t1x + %%t2x) :=
do t1 ← to_exprterm t1x,
t2 ← to_exprterm t2x,
return (exprterm.add t1 t2)
| `(%%t1x - %%t2x) :=
do t1 ← to_exprterm t1x,
t2 ← to_exprterm t2x,
return (exprterm.sub t1 t2)
| x :=
( do m ← eval_expr' nat x,
return (exprterm.cst m) ) <|>
( return $ exprterm.exp 1 x )
/-- Reification to imtermediate shadow syntax that retains exprs -/
meta def to_exprform : expr → tactic exprform
| `(%%tx1 = %%tx2) :=
do t1 ← to_exprterm tx1,
t2 ← to_exprterm tx2,
return (exprform.eq t1 t2)
| `(%%tx1 ≤ %%tx2) :=
do t1 ← to_exprterm tx1,
t2 ← to_exprterm tx2,
return (exprform.le t1 t2)
| `(¬ %%px) := do p ← to_exprform px, return (exprform.not p)
| `(%%px ∨ %%qx) :=
do p ← to_exprform px,
q ← to_exprform qx,
return (exprform.or p q)
| `(%%px ∧ %%qx) :=
do p ← to_exprform px,
q ← to_exprform qx,
return (exprform.and p q)
| `(_ → %%px) := to_exprform px
| x := trace "Cannot reify expr : " >> trace x >> failed
/-- List of all unreified exprs -/
meta def exprterm.exprs : exprterm → list expr
| (exprterm.cst _) := []
| (exprterm.exp _ x) := [x]
| (exprterm.add t s) := list.union t.exprs s.exprs
| (exprterm.sub t s) := list.union t.exprs s.exprs
/-- List of all unreified exprs -/
meta def exprform.exprs : exprform → list expr
| (exprform.eq t s) := list.union t.exprs s.exprs
| (exprform.le t s) := list.union t.exprs s.exprs
| (exprform.not p) := p.exprs
| (exprform.or p q) := list.union p.exprs q.exprs
| (exprform.and p q) := list.union p.exprs q.exprs
/-- Reification to an intermediate shadow syntax which eliminates exprs,
but still includes non-canonical terms -/
meta def exprterm.to_preterm (xs : list expr) : exprterm → tactic preterm
| (exprterm.cst k) := return & k
| (exprterm.exp k x) :=
let m := xs.index_of x in
if m < xs.length
then return (k ** m)
else failed
| (exprterm.add xa xb) :=
do a ← xa.to_preterm,
b ← xb.to_preterm,
return (a +* b)
| (exprterm.sub xa xb) :=
do a ← xa.to_preterm,
b ← xb.to_preterm,
return (a -* b)
/-- Reification to an intermediate shadow syntax which eliminates exprs,
but still includes non-canonical terms -/
meta def exprform.to_preform (xs : list expr) : exprform → tactic preform
| (exprform.eq xa xb) :=
do a ← xa.to_preterm xs,
b ← xb.to_preterm xs,
return (a =* b)
| (exprform.le xa xb) :=
do a ← xa.to_preterm xs,
b ← xb.to_preterm xs,
return (a ≤* b)
| (exprform.not xp) :=
do p ← xp.to_preform,
return ¬* p
| (exprform.or xp xq) :=
do p ← xp.to_preform,
q ← xq.to_preform,
return (p ∨* q)
| (exprform.and xp xq) :=
do p ← xp.to_preform,
q ← xq.to_preform,
return (p ∧* q)
/-- Reification to an intermediate shadow syntax which eliminates exprs,
but still includes non-canonical terms. -/
meta def to_preform (x : expr) : tactic (preform × nat) :=
do xf ← to_exprform x,
let xs := xf.exprs,
f ← xf.to_preform xs,
return (f, xs.length)
/-- Return expr of proof of current LNA goal -/
meta def prove : tactic expr :=
do (p,m) ← target >>= to_preform,
trace_if_enabled `omega p,
prove_univ_close m p
/-- Succeed iff argument is expr of ℕ -/
meta def eq_nat (x : expr) : tactic unit :=
if x = `(nat) then skip else failed
/-- Check whether argument is expr of a well-formed formula of LNA-/
meta def wff : expr → tactic unit
| `(¬ %%px) := wff px
| `(%%px ∨ %%qx) := wff px >> wff qx
| `(%%px ∧ %%qx) := wff px >> wff qx
| `(%%px ↔ %%qx) := wff px >> wff qx
| `(%%(expr.pi _ _ px qx)) :=
monad.cond
(if expr.has_var px then return tt else is_prop px)
(wff px >> wff qx)
(eq_nat px >> wff qx)
| `(@has_lt.lt %%dx %%h _ _) := eq_nat dx
| `(@has_le.le %%dx %%h _ _) := eq_nat dx
| `(@eq %%dx _ _) := eq_nat dx
| `(@ge %%dx %%h _ _) := eq_nat dx
| `(@gt %%dx %%h _ _) := eq_nat dx
| `(@ne %%dx _ _) := eq_nat dx
| `(true) := skip
| `(false) := skip
| _ := failed
/-- Succeed iff argument is expr of term whose type is wff -/
meta def wfx (x : expr) : tactic unit :=
infer_type x >>= wff
/-- Intro all universal quantifiers over nat -/
meta def intro_nats_core : tactic unit :=
do x ← target,
match x with
| (expr.pi _ _ `(nat) _) := intro_fresh >> intro_nats_core
| _ := skip
end
meta def intro_nats : tactic unit :=
do (expr.pi _ _ `(nat) _) ← target,
intro_nats_core
/-- If the goal has universal quantifiers over natural, introduce all of them.
Otherwise, revert all hypotheses that are formulas of linear natural number arithmetic. -/
meta def preprocess : tactic unit :=
intro_nats <|> (revert_cond_all wfx >> desugar)
end nat
end omega
open omega.nat
/-- The core omega tactic for natural numbers. -/
meta def omega_nat (is_manual : bool) : tactic unit :=
desugar ; (if is_manual then skip else preprocess) ; prove >>= apply >> skip
|
eaf4445f64f823d38ba1f96f366e6670cd0a5d57 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/ideal/idempotent_fg.lean | 61bdcf8506b19748382b54ec20980581fb94094f | [
"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,700 | lean | /-
Copyright (c) 2018 Mario Carneiro, Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Kevin Buzzard
-/
import algebra.ring.idempotents
import ring_theory.finiteness
/-!
## Lemmas on idempotent finitely generated ideals
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
namespace ideal
/-- A finitely generated idempotent ideal is generated by an idempotent element -/
lemma is_idempotent_elem_iff_of_fg {R : Type*} [comm_ring R] (I : ideal R)
(h : I.fg) :
is_idempotent_elem I ↔ ∃ e : R, is_idempotent_elem e ∧ I = R ∙ e :=
begin
split,
{ intro e,
obtain ⟨r, hr, hr'⟩ := submodule.exists_mem_and_smul_eq_self_of_fg_of_le_smul I I h
(by { rw [smul_eq_mul], exact e.ge }),
simp_rw smul_eq_mul at hr',
refine ⟨r, hr' r hr, antisymm _ ((submodule.span_singleton_le_iff_mem _ _).mpr hr)⟩,
intros x hx,
rw ← hr' x hx,
exact ideal.mem_span_singleton'.mpr ⟨_, mul_comm _ _⟩ },
{ rintros ⟨e, he, rfl⟩,
simp [is_idempotent_elem, ideal.span_singleton_mul_span_singleton, he.eq] }
end
lemma is_idempotent_elem_iff_eq_bot_or_top {R : Type*} [comm_ring R] [is_domain R]
(I : ideal R) (h : I.fg) :
is_idempotent_elem I ↔ I = ⊥ ∨ I = ⊤ :=
begin
split,
{ intro H,
obtain ⟨e, he, rfl⟩ := (I.is_idempotent_elem_iff_of_fg h).mp H,
simp only [ideal.submodule_span_eq, ideal.span_singleton_eq_bot],
apply or_of_or_of_imp_of_imp (is_idempotent_elem.iff_eq_zero_or_one.mp he) id,
rintro rfl,
simp },
{ rintro (rfl|rfl); simp [is_idempotent_elem] }
end
end ideal
|
f11aea8d0a4c01a0aad07d8784d4677bb901971f | 6b10c15e653d49d146378acda9f3692e9b5b1950 | /examples/logic/unnamed_505.lean | 93bd5c626fc08e1aad1a66fb736e89a27e0f6f77 | [] | no_license | gebner/mathematics_in_lean | 3cf7f18767208ea6c3307ec3a67c7ac266d8514d | 6d1462bba46d66a9b948fc1aef2714fd265cde0b | refs/heads/master | 1,655,301,945,565 | 1,588,697,505,000 | 1,588,697,505,000 | 261,523,603 | 0 | 0 | null | 1,588,695,611,000 | 1,588,695,610,000 | null | UTF-8 | Lean | false | false | 200 | lean | variables A B : Prop
-- BEGIN
example : false → A :=
by { intro h, cases h }
example : false → A :=
by { intro h, contradiction }
example (h₁ : B) (h₂ : ¬ B) : A :=
by contradiction
-- END |
8c26b920a1a29e5e1d6fe977b2403891a80a4ec9 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /test/differentiable.lean | 0b325a44c1feb36a6b26b0c45101bcfc2a2e153f | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,467 | lean | import analysis.special_functions.trigonometric.basic
import analysis.special_functions.log_deriv
namespace real
example : differentiable ℝ (λ (x : ℝ), exp x) :=
by simp
example : differentiable ℝ (λ (x : ℝ), exp ((sin x)^2) - exp (exp (cos (x - 3)))) :=
by simp
example (x : ℝ) : deriv (λ (x : ℝ), (cos x)^2 + 1 + (sin x)^2) x = 0 :=
by { simp, ring }
example (x : ℝ) : deriv (λ (x : ℝ), (1+x)^3 - x^3 - 3 * x^2 - 3 * x - 4) x = 0 :=
by { simp, ring }
example (x : ℝ) : deriv (λ x, cos (sin x) * exp x) x = (cos(sin(x))-sin(sin(x))*cos(x))*exp(x) :=
by { simp, ring }
example (x : ℝ) : differentiable_at ℝ (λ x, (cos x, x)) x := by simp
example (x : ℝ) (h : 1 + sin x ≠ 0) : deriv (λ x, exp (cos x) / (1 + sin x)) x =
(-(exp (cos x) * sin x * (1 + sin x)) - exp (cos x) * cos x) / (1 + sin x) ^ 2 :=
by simp [h]
example (x : ℝ) : differentiable_at ℝ (λ x, (sin x) / (exp x)) x :=
by simp [exp_ne_zero]
example : differentiable ℝ (λ x, (sin x) / (exp x)) :=
by simp [exp_ne_zero]
example (x : ℝ) (h : x ≠ 0) : deriv (λ x, x * (log x - 1)) x = log x :=
by simp [h]
end real
namespace complex
example : differentiable ℂ (λ (x : ℂ), exp x) :=
by simp
example : differentiable ℂ (λ (x : ℂ), exp ((sin x)^2) - exp (exp (cos (x - 3)))) :=
by simp
example (x : ℂ) : deriv (λ (x : ℂ), (cos x)^2 + I + (sin x)^2) x = 0 :=
by { simp, ring }
example (x : ℂ) : deriv (λ x, cos (sin x) * exp x) x = (cos(sin(x))-sin(sin(x))*cos(x))*exp(x) :=
by { simp, ring }
example (x : ℂ) : differentiable_at ℂ (λ x, (cos x, x)) x := by simp
example (x : ℂ) (h : 1 + sin x ≠ 0) : deriv (λ x, exp (cos x) / (1 + sin x)) x =
(-(exp (cos x) * sin x * (1 + sin x)) - exp (cos x) * cos x) / (1 + sin x) ^ 2 :=
by simp [h]
example (x : ℂ) : differentiable_at ℂ (λ x, (sin x) / (exp x)) x :=
by simp [exp_ne_zero]
example : differentiable ℂ (λ x, (sin x) / (exp x)) :=
by simp [exp_ne_zero]
end complex
namespace polynomial
variables {R : Type*} [comm_semiring R]
example : (2 : polynomial R).derivative = 0 :=
by conv_lhs { simp }
example : (3 + X : polynomial R).derivative = 1 :=
by conv_lhs { simp }
example : (2 * X ^ 2 : polynomial R).derivative = 4 * X :=
by conv_lhs { simp, ring_nf, }
example : (X ^ 2 : polynomial R).derivative = 2 * X :=
by conv_lhs { simp }
example : ((C 2 * X ^ 3).derivative : polynomial R) = 6 * X ^ 2 :=
by conv_lhs { simp, ring_nf, }
end polynomial
|
bdc52f7b7d673e7b4ef90b6d69b8ca14a21cd306 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/measure_theory/integral/set_integral.lean | da19fbe0accbe87b750b50637680a6e9b9bb8ece | [
"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 | 44,871 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou, Yury Kudryashov
-/
import measure_theory.integral.integrable_on
import measure_theory.integral.bochner
import order.filter.indicator_function
/-!
# Set integral
In this file we prove some properties of `∫ x in s, f x ∂μ`. Recall that this notation
is defined as `∫ x, f x ∂(μ.restrict s)`. In `integral_indicator` we prove that for a measurable
function `f` and a measurable set `s` this definition coincides with another natural definition:
`∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ`, where `indicator s f x` is equal to `f x` for `x ∈ s`
and is zero otherwise.
Since `∫ x in s, f x ∂μ` is a notation, one can rewrite or apply any theorem about `∫ x, f x ∂μ`
directly. In this file we prove some theorems about dependence of `∫ x in s, f x ∂μ` on `s`, e.g.
`integral_union`, `integral_empty`, `integral_univ`.
We use the property `integrable_on f s μ := integrable f (μ.restrict s)`, defined in
`measure_theory.integrable_on`. We also defined in that same file a predicate
`integrable_at_filter (f : α → E) (l : filter α) (μ : measure α)` saying that `f` is integrable at
some set `s ∈ l`.
Finally, we prove a version of the
[Fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus)
for set integral, see `filter.tendsto.integral_sub_linear_is_o_ae` and its corollaries.
Namely, consider a measurably generated filter `l`, a measure `μ` finite at this filter, and
a function `f` that has a finite limit `c` at `l ⊓ μ.ae`. Then `∫ x in s, f x ∂μ = μ s • c + o(μ s)`
as `s` tends to `l.lift' powerset`, i.e. for any `ε>0` there exists `t ∈ l` such that
`∥∫ x in s, f x ∂μ - μ s • c∥ ≤ ε * μ s` whenever `s ⊆ t`. We also formulate a version of this
theorem for a locally finite measure `μ` and a function `f` continuous at a point `a`.
## Notation
We provide the following notations for expressing the integral of a function on a set :
* `∫ a in s, f a ∂μ` is `measure_theory.integral (μ.restrict s) f`
* `∫ a in s, f a` is `∫ a in s, f a ∂volume`
Note that the set notations are defined in the file `measure_theory/integral/bochner`,
but we reference them here because all theorems about set integrals are in this file.
-/
noncomputable theory
open set filter topological_space measure_theory function
open_locale classical topological_space interval big_operators filter ennreal nnreal measure_theory
variables {α β E F : Type*} [measurable_space α]
namespace measure_theory
section normed_group
variables [normed_group E] [measurable_space E] {f g : α → E} {s t : set α} {μ ν : measure α}
{l l' : filter α} [borel_space E] [second_countable_topology E]
variables [complete_space E] [normed_space ℝ E]
lemma set_integral_congr_ae (hs : measurable_set s) (h : ∀ᵐ x ∂μ, x ∈ s → f x = g x) :
∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ :=
integral_congr_ae ((ae_restrict_iff' hs).2 h)
lemma set_integral_congr (hs : measurable_set s) (h : eq_on f g s) :
∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ :=
set_integral_congr_ae hs $ eventually_of_forall h
lemma set_integral_congr_set_ae (hst : s =ᵐ[μ] t) :
∫ x in s, f x ∂μ = ∫ x in t, f x ∂μ :=
by rw measure.restrict_congr_set hst
lemma integral_union_ae (hst : ae_disjoint μ s t) (ht : null_measurable_set t μ)
(hfs : integrable_on f s μ) (hft : integrable_on f t μ) :
∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ :=
by simp only [integrable_on, measure.restrict_union₀ hst ht, integral_add_measure hfs hft]
lemma integral_union (hst : disjoint s t) (ht : measurable_set t)
(hfs : integrable_on f s μ) (hft : integrable_on f t μ) :
∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ :=
integral_union_ae hst.ae_disjoint ht.null_measurable_set hfs hft
lemma integral_diff (ht : measurable_set t) (hfs : integrable_on f s μ)
(hft : integrable_on f t μ) (hts : t ⊆ s) :
∫ x in s \ t, f x ∂μ = ∫ x in s, f x ∂μ - ∫ x in t, f x ∂μ :=
begin
rw [eq_sub_iff_add_eq, ← integral_union, diff_union_of_subset hts],
exacts [disjoint_diff.symm, ht, hfs.mono_set (diff_subset _ _), hft]
end
lemma integral_finset_bUnion {ι : Type*} (t : finset ι) {s : ι → set α}
(hs : ∀ i ∈ t, measurable_set (s i)) (h's : set.pairwise ↑t (disjoint on s))
(hf : ∀ i ∈ t, integrable_on f (s i) μ) :
∫ x in (⋃ i ∈ t, s i), f x ∂ μ = ∑ i in t, ∫ x in s i, f x ∂ μ :=
begin
induction t using finset.induction_on with a t hat IH hs h's,
{ simp },
{ simp only [finset.coe_insert, finset.forall_mem_insert, set.pairwise_insert,
finset.set_bUnion_insert] at hs hf h's ⊢,
rw [integral_union _ _ hf.1 (integrable_on_finset_Union.2 hf.2)],
{ rw [finset.sum_insert hat, IH hs.2 h's.1 hf.2] },
{ simp only [disjoint_Union_right],
exact (λ i hi, (h's.2 i hi (ne_of_mem_of_not_mem hi hat).symm).1) },
{ exact finset.measurable_set_bUnion _ hs.2 } }
end
lemma integral_fintype_Union {ι : Type*} [fintype ι] {s : ι → set α}
(hs : ∀ i, measurable_set (s i)) (h's : pairwise (disjoint on s))
(hf : ∀ i, integrable_on f (s i) μ) :
∫ x in (⋃ i, s i), f x ∂ μ = ∑ i, ∫ x in s i, f x ∂ μ :=
begin
convert integral_finset_bUnion finset.univ (λ i hi, hs i) _ (λ i _, hf i),
{ simp },
{ simp [pairwise_univ, h's] }
end
lemma integral_empty : ∫ x in ∅, f x ∂μ = 0 := by rw [measure.restrict_empty, integral_zero_measure]
lemma integral_univ : ∫ x in univ, f x ∂μ = ∫ x, f x ∂μ := by rw [measure.restrict_univ]
lemma integral_add_compl (hs : measurable_set s) (hfi : integrable f μ) :
∫ x in s, f x ∂μ + ∫ x in sᶜ, f x ∂μ = ∫ x, f x ∂μ :=
by rw [← integral_union (@disjoint_compl_right (set α) _ _) hs.compl
hfi.integrable_on hfi.integrable_on, union_compl_self, integral_univ]
/-- For a function `f` and a measurable set `s`, the integral of `indicator s f`
over the whole space is equal to `∫ x in s, f x ∂μ` defined as `∫ x, f x ∂(μ.restrict s)`. -/
lemma integral_indicator (hs : measurable_set s) :
∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ :=
begin
by_cases hfi : integrable_on f s μ, swap,
{ rwa [integral_undef, integral_undef],
rwa integrable_indicator_iff hs },
calc ∫ x, indicator s f x ∂μ = ∫ x in s, indicator s f x ∂μ + ∫ x in sᶜ, indicator s f x ∂μ :
(integral_add_compl hs (hfi.indicator hs)).symm
... = ∫ x in s, f x ∂μ + ∫ x in sᶜ, 0 ∂μ :
congr_arg2 (+) (integral_congr_ae (indicator_ae_eq_restrict hs))
(integral_congr_ae (indicator_ae_eq_restrict_compl hs))
... = ∫ x in s, f x ∂μ : by simp
end
lemma tendsto_set_integral_of_monotone {ι : Type*} [encodable ι] [semilattice_sup ι]
{s : ι → set α} {f : α → E} (hsm : ∀ i, measurable_set (s i))
(h_mono : monotone s) (hfi : integrable_on f (⋃ n, s n) μ) :
tendsto (λ i, ∫ a in s i, f a ∂μ) at_top (𝓝 (∫ a in (⋃ n, s n), f a ∂μ)) :=
begin
have hfi' : ∫⁻ x in ⋃ n, s n, ∥f x∥₊ ∂μ < ∞ := hfi.2,
set S := ⋃ i, s i,
have hSm : measurable_set S := measurable_set.Union hsm,
have hsub : ∀ {i}, s i ⊆ S, from subset_Union s,
rw [← with_density_apply _ hSm] at hfi',
set ν := μ.with_density (λ x, ∥f x∥₊) with hν,
refine metric.nhds_basis_closed_ball.tendsto_right_iff.2 (λ ε ε0, _),
lift ε to ℝ≥0 using ε0.le,
have : ∀ᶠ i in at_top, ν (s i) ∈ Icc (ν S - ε) (ν S + ε),
from tendsto_measure_Union h_mono (ennreal.Icc_mem_nhds hfi'.ne (ennreal.coe_pos.2 ε0).ne'),
refine this.mono (λ i hi, _),
rw [mem_closed_ball_iff_norm', ← integral_diff (hsm i) hfi (hfi.mono_set hsub) hsub,
← coe_nnnorm, nnreal.coe_le_coe, ← ennreal.coe_le_coe],
refine (ennnorm_integral_le_lintegral_ennnorm _).trans _,
rw [← with_density_apply _ (hSm.diff (hsm _)), ← hν, measure_diff hsub (hsm _)],
exacts [tsub_le_iff_tsub_le.mp hi.1,
(hi.2.trans_lt $ ennreal.add_lt_top.2 ⟨hfi', ennreal.coe_lt_top⟩).ne]
end
lemma has_sum_integral_Union {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E}
(hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s))
(hfi : integrable_on f (⋃ i, s i) μ) :
has_sum (λ n, ∫ a in s n, f a ∂ μ) (∫ a in ⋃ n, s n, f a ∂μ) :=
begin
have hfi' : ∀ i, integrable_on f (s i) μ, from λ i, hfi.mono_set (subset_Union _ _),
simp only [has_sum, ← integral_finset_bUnion _ (λ i _, hm i) (hd.set_pairwise _) (λ i _, hfi' i)],
rw Union_eq_Union_finset at hfi ⊢,
exact tendsto_set_integral_of_monotone (λ t, t.measurable_set_bUnion (λ i _, hm i))
(λ t₁ t₂ h, bUnion_subset_bUnion_left h) hfi
end
lemma integral_Union {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E}
(hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s))
(hfi : integrable_on f (⋃ i, s i) μ) :
(∫ a in (⋃ n, s n), f a ∂μ) = ∑' n, ∫ a in s n, f a ∂ μ :=
(has_sum.tsum_eq (has_sum_integral_Union hm hd hfi)).symm
lemma has_sum_integral_Union_of_null_inter {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E}
(hm : ∀ i, null_measurable_set (s i) μ) (hd : pairwise (ae_disjoint μ on s))
(hfi : integrable_on f (⋃ i, s i) μ) :
has_sum (λ n, ∫ a in s n, f a ∂ μ) (∫ a in ⋃ n, s n, f a ∂μ) :=
begin
rcases exists_subordinate_pairwise_disjoint hm hd with ⟨t, ht_sub, ht_eq, htm, htd⟩,
have htU_eq : (⋃ i, s i) =ᵐ[μ] ⋃ i, t i := eventually_eq.countable_Union ht_eq,
simp only [set_integral_congr_set_ae (ht_eq _), set_integral_congr_set_ae htU_eq, htU_eq],
exact has_sum_integral_Union htm htd (hfi.congr_set_ae htU_eq.symm)
end
lemma integral_Union_of_null_inter {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E}
(hm : ∀ i, null_measurable_set (s i) μ) (hd : pairwise (ae_disjoint μ on s))
(hfi : integrable_on f (⋃ i, s i) μ) :
(∫ a in (⋃ n, s n), f a ∂μ) = ∑' n, ∫ a in s n, f a ∂ μ :=
(has_sum.tsum_eq (has_sum_integral_Union_of_null_inter hm hd hfi)).symm
lemma set_integral_eq_zero_of_forall_eq_zero {f : α → E} (hf : measurable f)
(ht_eq : ∀ x ∈ t, f x = 0) :
∫ x in t, f x ∂μ = 0 :=
begin
refine integral_eq_zero_of_ae _,
rw [eventually_eq, ae_restrict_iff (measurable_set_eq_fun hf measurable_zero)],
refine eventually_of_forall (λ x hx, _),
rw pi.zero_apply,
exact ht_eq x hx,
end
lemma set_integral_union_eq_left {f : α → E} (hf : measurable f) (hfi : integrable f μ)
(hs : measurable_set s) (ht_eq : ∀ x ∈ t, f x = 0) :
∫ x in (s ∪ t), f x ∂μ = ∫ x in s, f x ∂μ :=
begin
rw [← set.union_diff_self, union_comm, integral_union,
set_integral_eq_zero_of_forall_eq_zero _ (λ x hx, ht_eq x (diff_subset _ _ hx)), zero_add],
exacts [hf, disjoint_diff.symm, hs, hfi.integrable_on, hfi.integrable_on]
end
lemma set_integral_neg_eq_set_integral_nonpos [linear_order E] [order_closed_topology E]
{f : α → E} (hf : measurable f) (hfi : integrable f μ) :
∫ x in {x | f x < 0}, f x ∂μ = ∫ x in {x | f x ≤ 0}, f x ∂μ :=
begin
have h_union : {x | f x ≤ 0} = {x | f x < 0} ∪ {x | f x = 0},
by { ext, simp_rw [set.mem_union_eq, set.mem_set_of_eq], exact le_iff_lt_or_eq, },
rw h_union,
exact (set_integral_union_eq_left hf hfi (measurable_set_lt hf measurable_const)
(λ x hx, hx)).symm,
end
lemma integral_norm_eq_pos_sub_neg {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ) :
∫ x, ∥f x∥ ∂μ = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | f x ≤ 0}, f x ∂μ :=
have h_meas : measurable_set {x | 0 ≤ f x}, from measurable_set_le measurable_const hf,
calc ∫ x, ∥f x∥ ∂μ = ∫ x in {x | 0 ≤ f x}, ∥f x∥ ∂μ + ∫ x in {x | 0 ≤ f x}ᶜ, ∥f x∥ ∂μ :
by rw ← integral_add_compl h_meas hfi.norm
... = ∫ x in {x | 0 ≤ f x}, f x ∂μ + ∫ x in {x | 0 ≤ f x}ᶜ, ∥f x∥ ∂μ :
begin
congr' 1,
refine set_integral_congr h_meas (λ x hx, _),
dsimp only,
rw [real.norm_eq_abs, abs_eq_self.mpr _],
exact hx,
end
... = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | 0 ≤ f x}ᶜ, f x ∂μ :
begin
congr' 1,
rw ← integral_neg,
refine set_integral_congr h_meas.compl (λ x hx, _),
dsimp only,
rw [real.norm_eq_abs, abs_eq_neg_self.mpr _],
rw [set.mem_compl_iff, set.nmem_set_of_eq] at hx,
linarith,
end
... = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | f x ≤ 0}, f x ∂μ :
by { rw ← set_integral_neg_eq_set_integral_nonpos hf hfi, congr, ext1 x, simp, }
lemma set_integral_const (c : E) : ∫ x in s, c ∂μ = (μ s).to_real • c :=
by rw [integral_const, measure.restrict_apply_univ]
@[simp]
lemma integral_indicator_const (e : E) ⦃s : set α⦄ (s_meas : measurable_set s) :
∫ (a : α), s.indicator (λ (x : α), e) a ∂μ = (μ s).to_real • e :=
by rw [integral_indicator s_meas, ← set_integral_const]
lemma set_integral_indicator_const_Lp {p : ℝ≥0∞} (hs : measurable_set s) (ht : measurable_set t)
(hμt : μ t ≠ ∞) (x : E) :
∫ a in s, indicator_const_Lp p ht hμt x a ∂μ = (μ (t ∩ s)).to_real • x :=
calc ∫ a in s, indicator_const_Lp p ht hμt x a ∂μ
= (∫ a in s, t.indicator (λ _, x) a ∂μ) :
by rw set_integral_congr_ae hs (indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx))
... = (μ (t ∩ s)).to_real • x : by rw [integral_indicator_const _ ht, measure.restrict_apply ht]
lemma integral_indicator_const_Lp {p : ℝ≥0∞} (ht : measurable_set t) (hμt : μ t ≠ ∞) (x : E) :
∫ a, indicator_const_Lp p ht hμt x a ∂μ = (μ t).to_real • x :=
calc ∫ a, indicator_const_Lp p ht hμt x a ∂μ
= ∫ a in univ, indicator_const_Lp p ht hμt x a ∂μ : by rw integral_univ
... = (μ (t ∩ univ)).to_real • x : set_integral_indicator_const_Lp measurable_set.univ ht hμt x
... = (μ t).to_real • x : by rw inter_univ
lemma set_integral_map {β} [measurable_space β] {g : α → β} {f : β → E} {s : set β}
(hs : measurable_set s) (hf : ae_measurable f (measure.map g μ)) (hg : measurable g) :
∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ :=
begin
rw [measure.restrict_map hg hs, integral_map hg (hf.mono_measure _)],
exact measure.map_mono g measure.restrict_le_self
end
lemma _root_.measurable_embedding.set_integral_map {β} {_ : measurable_space β} {f : α → β}
(hf : measurable_embedding f) (g : β → E) (s : set β) :
∫ y in s, g y ∂(measure.map f μ) = ∫ x in f ⁻¹' s, g (f x) ∂μ :=
by rw [hf.restrict_map, hf.integral_map]
lemma _root_.closed_embedding.set_integral_map [topological_space α] [borel_space α]
{β} [measurable_space β] [topological_space β] [borel_space β]
{g : α → β} {f : β → E} (s : set β) (hg : closed_embedding g) :
∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ :=
hg.measurable_embedding.set_integral_map _ _
lemma measure_preserving.set_integral_preimage_emb {β} {_ : measurable_space β} {f : α → β} {ν}
(h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) (s : set β) :
∫ x in f ⁻¹' s, g (f x) ∂μ = ∫ y in s, g y ∂ν :=
(h₁.restrict_preimage_emb h₂ s).integral_comp h₂ _
lemma measure_preserving.set_integral_image_emb {β} {_ : measurable_space β} {f : α → β} {ν}
(h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) (s : set α) :
∫ y in f '' s, g y ∂ν = ∫ x in s, g (f x) ∂μ :=
eq.symm $ (h₁.restrict_image_emb h₂ s).integral_comp h₂ _
lemma set_integral_map_equiv {β} [measurable_space β] (e : α ≃ᵐ β) (f : β → E) (s : set β) :
∫ y in s, f y ∂(measure.map e μ) = ∫ x in e ⁻¹' s, f (e x) ∂μ :=
e.measurable_embedding.set_integral_map f s
lemma norm_set_integral_le_of_norm_le_const_ae {C : ℝ} (hs : μ s < ∞)
(hC : ∀ᵐ x ∂μ.restrict s, ∥f x∥ ≤ C) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
begin
rw ← measure.restrict_apply_univ at *,
haveI : is_finite_measure (μ.restrict s) := ⟨‹_›⟩,
exact norm_integral_le_of_norm_le_const hC
end
lemma norm_set_integral_le_of_norm_le_const_ae' {C : ℝ} (hs : μ s < ∞)
(hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) (hfm : ae_measurable f (μ.restrict s)) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
begin
apply norm_set_integral_le_of_norm_le_const_ae hs,
have A : ∀ᵐ (x : α) ∂μ, x ∈ s → ∥ae_measurable.mk f hfm x∥ ≤ C,
{ filter_upwards [hC, hfm.ae_mem_imp_eq_mk] with _ h1 h2 h3,
rw [← h2 h3],
exact h1 h3 },
have B : measurable_set {x | ∥(hfm.mk f) x∥ ≤ C} := hfm.measurable_mk.norm measurable_set_Iic,
filter_upwards [hfm.ae_eq_mk, (ae_restrict_iff B).2 A] with _ h1 _,
rwa h1,
end
lemma norm_set_integral_le_of_norm_le_const_ae'' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s)
(hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
norm_set_integral_le_of_norm_le_const_ae hs $ by rwa [ae_restrict_eq hsm, eventually_inf_principal]
lemma norm_set_integral_le_of_norm_le_const {C : ℝ} (hs : μ s < ∞)
(hC : ∀ x ∈ s, ∥f x∥ ≤ C) (hfm : ae_measurable f (μ.restrict s)) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
norm_set_integral_le_of_norm_le_const_ae' hs (eventually_of_forall hC) hfm
lemma norm_set_integral_le_of_norm_le_const' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s)
(hC : ∀ x ∈ s, ∥f x∥ ≤ C) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
norm_set_integral_le_of_norm_le_const_ae'' hs hsm $ eventually_of_forall hC
lemma set_integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f)
(hfi : integrable_on f s μ) :
∫ x in s, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict s] 0 :=
integral_eq_zero_iff_of_nonneg_ae hf hfi
lemma set_integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f)
(hfi : integrable_on f s μ) :
0 < ∫ x in s, f x ∂μ ↔ 0 < μ (support f ∩ s) :=
begin
rw [integral_pos_iff_support_of_nonneg_ae hf hfi, measure.restrict_apply₀],
rw support_eq_preimage,
exact hfi.ae_measurable.null_measurable (measurable_set_singleton 0).compl
end
lemma set_integral_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → E}
(hf_meas : @measurable _ _ m _ f) {s : set α} (hs : measurable_set[m] s) :
∫ x in s, f x ∂μ = ∫ x in s, f x ∂(μ.trim hm) :=
by rwa [integral_trim hm hf_meas, restrict_trim hm μ]
lemma integral_Icc_eq_integral_Ioc' [partial_order α] {f : α → E} {a b : α} (ha : μ {a} = 0) :
∫ t in Icc a b, f t ∂μ = ∫ t in Ioc a b, f t ∂μ :=
set_integral_congr_set_ae (Ioc_ae_eq_Icc' ha).symm
lemma integral_Ioc_eq_integral_Ioo' [partial_order α] {f : α → E} {a b : α} (hb : μ {b} = 0) :
∫ t in Ioc a b, f t ∂μ = ∫ t in Ioo a b, f t ∂μ :=
set_integral_congr_set_ae (Ioo_ae_eq_Ioc' hb).symm
lemma integral_Icc_eq_integral_Ioc [partial_order α] {f : α → E} {a b : α} [has_no_atoms μ] :
∫ t in Icc a b, f t ∂μ = ∫ t in Ioc a b, f t ∂μ :=
integral_Icc_eq_integral_Ioc' $ measure_singleton a
lemma integral_Ioc_eq_integral_Ioo [partial_order α] {f : α → E} {a b : α} [has_no_atoms μ] :
∫ t in Ioc a b, f t ∂μ = ∫ t in Ioo a b, f t ∂μ :=
integral_Ioc_eq_integral_Ioo' $ measure_singleton b
end normed_group
section mono
variables {μ : measure α} {f g : α → ℝ} {s t : set α}
(hf : integrable_on f s μ) (hg : integrable_on g s μ)
lemma set_integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict s] g) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
integral_mono_ae hf hg h
lemma set_integral_mono_ae (h : f ≤ᵐ[μ] g) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
set_integral_mono_ae_restrict hf hg (ae_restrict_of_ae h)
lemma set_integral_mono_on (hs : measurable_set s) (h : ∀ x ∈ s, f x ≤ g x) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
set_integral_mono_ae_restrict hf hg
(by simp [hs, eventually_le, eventually_inf_principal, ae_of_all _ h])
include hf hg -- why do I need this include, but we don't need it in other lemmas?
lemma set_integral_mono_on_ae (hs : measurable_set s) (h : ∀ᵐ x ∂μ, x ∈ s → f x ≤ g x) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
by { refine set_integral_mono_ae_restrict hf hg _, rwa [eventually_le, ae_restrict_iff' hs], }
omit hf hg
lemma set_integral_mono (h : f ≤ g) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
integral_mono hf hg h
lemma set_integral_mono_set (hfi : integrable_on f t μ) (hf : 0 ≤ᵐ[μ.restrict t] f)
(hst : s ≤ᵐ[μ] t) :
∫ x in s, f x ∂μ ≤ ∫ x in t, f x ∂μ :=
integral_mono_measure (measure.restrict_mono_ae hst) hf hfi
end mono
section nonneg
variables {μ : measure α} {f : α → ℝ} {s : set α}
lemma set_integral_nonneg_of_ae_restrict (hf : 0 ≤ᵐ[μ.restrict s] f) :
0 ≤ ∫ a in s, f a ∂μ :=
integral_nonneg_of_ae hf
lemma set_integral_nonneg_of_ae (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a in s, f a ∂μ :=
set_integral_nonneg_of_ae_restrict (ae_restrict_of_ae hf)
lemma set_integral_nonneg (hs : measurable_set s) (hf : ∀ a, a ∈ s → 0 ≤ f a) :
0 ≤ ∫ a in s, f a ∂μ :=
set_integral_nonneg_of_ae_restrict ((ae_restrict_iff' hs).mpr (ae_of_all μ hf))
lemma set_integral_nonneg_ae (hs : measurable_set s) (hf : ∀ᵐ a ∂μ, a ∈ s → 0 ≤ f a) :
0 ≤ ∫ a in s, f a ∂μ :=
set_integral_nonneg_of_ae_restrict $ by rwa [eventually_le, ae_restrict_iff' hs]
lemma set_integral_le_nonneg {s : set α} (hs : measurable_set s) (hf : measurable f)
(hfi : integrable f μ) :
∫ x in s, f x ∂μ ≤ ∫ x in {y | 0 ≤ f y}, f x ∂μ :=
begin
rw [← integral_indicator hs, ← integral_indicator (measurable_set_le measurable_const hf)],
exact integral_mono (hfi.indicator hs) (hfi.indicator (measurable_set_le measurable_const hf))
(indicator_le_indicator_nonneg s f),
end
lemma set_integral_nonpos_of_ae_restrict (hf : f ≤ᵐ[μ.restrict s] 0) :
∫ a in s, f a ∂μ ≤ 0 :=
integral_nonpos_of_ae hf
lemma set_integral_nonpos_of_ae (hf : f ≤ᵐ[μ] 0) : ∫ a in s, f a ∂μ ≤ 0 :=
set_integral_nonpos_of_ae_restrict (ae_restrict_of_ae hf)
lemma set_integral_nonpos (hs : measurable_set s) (hf : ∀ a, a ∈ s → f a ≤ 0) :
∫ a in s, f a ∂μ ≤ 0 :=
set_integral_nonpos_of_ae_restrict ((ae_restrict_iff' hs).mpr (ae_of_all μ hf))
lemma set_integral_nonpos_ae (hs : measurable_set s) (hf : ∀ᵐ a ∂μ, a ∈ s → f a ≤ 0) :
∫ a in s, f a ∂μ ≤ 0 :=
set_integral_nonpos_of_ae_restrict $ by rwa [eventually_le, ae_restrict_iff' hs]
lemma set_integral_nonpos_le {s : set α} (hs : measurable_set s) {f : α → ℝ} (hf : measurable f)
(hfi : integrable f μ) :
∫ x in {y | f y ≤ 0}, f x ∂μ ≤ ∫ x in s, f x ∂μ :=
begin
rw [← integral_indicator hs, ← integral_indicator (measurable_set_le hf measurable_const)],
exact integral_mono (hfi.indicator (measurable_set_le hf measurable_const)) (hfi.indicator hs)
(indicator_nonpos_le_indicator s f),
end
end nonneg
section tendsto_mono
variables {μ : measure α}
[measurable_space E] [normed_group E] [borel_space E] [complete_space E] [normed_space ℝ E]
[second_countable_topology E] {s : ℕ → set α} {f : α → E}
lemma _root_.antitone.tendsto_set_integral (hsm : ∀ i, measurable_set (s i))
(h_anti : antitone s) (hfi : integrable_on f (s 0) μ) :
tendsto (λi, ∫ a in s i, f a ∂μ) at_top (𝓝 (∫ a in (⋂ n, s n), f a ∂μ)) :=
begin
let bound : α → ℝ := indicator (s 0) (λ a, ∥f a∥),
have h_int_eq : (λ i, ∫ a in s i, f a ∂μ) = (λ i, ∫ a, (s i).indicator f a ∂μ),
from funext (λ i, (integral_indicator (hsm i)).symm),
rw h_int_eq,
rw ← integral_indicator (measurable_set.Inter hsm),
refine tendsto_integral_of_dominated_convergence bound _ _ _ _,
{ intro n,
rw ae_measurable_indicator_iff (hsm n),
exact (integrable_on.mono_set hfi (h_anti (zero_le n))).1 },
{ rw integrable_indicator_iff (hsm 0),
exact hfi.norm, },
{ simp_rw norm_indicator_eq_indicator_norm,
refine λ n, eventually_of_forall (λ x, _),
exact indicator_le_indicator_of_subset (h_anti (zero_le n)) (λ a, norm_nonneg _) _ },
{ filter_upwards with a using le_trans (h_anti.tendsto_indicator _ _ _) (pure_le_nhds _), },
end
end tendsto_mono
/-! ### Continuity of the set integral
We prove that for any set `s`, the function `λ f : α →₁[μ] E, ∫ x in s, f x ∂μ` is continuous. -/
section continuous_set_integral
variables [normed_group E] [measurable_space E] [second_countable_topology E] [borel_space E]
{𝕜 : Type*} [is_R_or_C 𝕜]
[normed_group F] [measurable_space F] [second_countable_topology F] [borel_space F]
[normed_space 𝕜 F]
{p : ℝ≥0∞} {μ : measure α}
/-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by
`(Lp.mem_ℒp f).restrict s).to_Lp f`. This map is additive. -/
lemma Lp_to_Lp_restrict_add (f g : Lp E p μ) (s : set α) :
((Lp.mem_ℒp (f + g)).restrict s).to_Lp ⇑(f + g)
= ((Lp.mem_ℒp f).restrict s).to_Lp f + ((Lp.mem_ℒp g).restrict s).to_Lp g :=
begin
ext1,
refine (ae_restrict_of_ae (Lp.coe_fn_add f g)).mp _,
refine (Lp.coe_fn_add (mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s))
(mem_ℒp.to_Lp g ((Lp.mem_ℒp g).restrict s))).mp _,
refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)).mp _,
refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp g).restrict s)).mp _,
refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp (f+g)).restrict s)).mono (λ x hx1 hx2 hx3 hx4 hx5, _),
rw [hx4, hx1, pi.add_apply, hx2, hx3, hx5, pi.add_apply],
end
/-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by
`(Lp.mem_ℒp f).restrict s).to_Lp f`. This map commutes with scalar multiplication. -/
lemma Lp_to_Lp_restrict_smul (c : 𝕜) (f : Lp F p μ) (s : set α) :
((Lp.mem_ℒp (c • f)).restrict s).to_Lp ⇑(c • f) = c • (((Lp.mem_ℒp f).restrict s).to_Lp f) :=
begin
ext1,
refine (ae_restrict_of_ae (Lp.coe_fn_smul c f)).mp _,
refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)).mp _,
refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp (c • f)).restrict s)).mp _,
refine (Lp.coe_fn_smul c (mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s))).mono
(λ x hx1 hx2 hx3 hx4, _),
rw [hx2, hx1, pi.smul_apply, hx3, hx4, pi.smul_apply],
end
/-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by
`(Lp.mem_ℒp f).restrict s).to_Lp f`. This map is non-expansive. -/
lemma norm_Lp_to_Lp_restrict_le (s : set α) (f : Lp E p μ) :
∥((Lp.mem_ℒp f).restrict s).to_Lp f∥ ≤ ∥f∥ :=
begin
rw [Lp.norm_def, Lp.norm_def, ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _)],
refine (le_of_eq _).trans (snorm_mono_measure _ measure.restrict_le_self),
{ exact s, },
exact snorm_congr_ae (mem_ℒp.coe_fn_to_Lp _),
end
variables (α F 𝕜)
/-- Continuous linear map sending a function of `Lp F p μ` to the same function in
`Lp F p (μ.restrict s)`. -/
def Lp_to_Lp_restrict_clm (μ : measure α) (p : ℝ≥0∞) [hp : fact (1 ≤ p)] (s : set α) :
Lp F p μ →L[𝕜] Lp F p (μ.restrict s) :=
@linear_map.mk_continuous 𝕜 𝕜 (Lp F p μ) (Lp F p (μ.restrict s)) _ _ _ _ _ _ (ring_hom.id 𝕜)
⟨λ f, mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s), λ f g, Lp_to_Lp_restrict_add f g s,
λ c f, Lp_to_Lp_restrict_smul c f s⟩
1 (by { intro f, rw one_mul, exact norm_Lp_to_Lp_restrict_le s f, })
variables {α F 𝕜}
variables (𝕜)
lemma Lp_to_Lp_restrict_clm_coe_fn [hp : fact (1 ≤ p)] (s : set α) (f : Lp F p μ) :
Lp_to_Lp_restrict_clm α F 𝕜 μ p s f =ᵐ[μ.restrict s] f :=
mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)
variables {𝕜}
@[continuity]
lemma continuous_set_integral [normed_space ℝ E] [complete_space E] (s : set α) :
continuous (λ f : α →₁[μ] E, ∫ x in s, f x ∂μ) :=
begin
haveI : fact ((1 : ℝ≥0∞) ≤ 1) := ⟨le_rfl⟩,
have h_comp : (λ f : α →₁[μ] E, ∫ x in s, f x ∂μ)
= (integral (μ.restrict s)) ∘ (λ f, Lp_to_Lp_restrict_clm α E ℝ μ 1 s f),
{ ext1 f,
rw [function.comp_apply, integral_congr_ae (Lp_to_Lp_restrict_clm_coe_fn ℝ s f)], },
rw h_comp,
exact continuous_integral.comp (Lp_to_Lp_restrict_clm α E ℝ μ 1 s).continuous,
end
end continuous_set_integral
end measure_theory
open measure_theory asymptotics metric
variables {ι : Type*} [measurable_space E] [normed_group E]
/-- Fundamental theorem of calculus for set integrals: if `μ` is a measure that is finite at a
filter `l` and `f` is a measurable function that has a finite limit `b` at `l ⊓ μ.ae`, then `∫ x in
s i, f x ∂μ = μ (s i) • b + o(μ (s i))` at a filter `li` provided that `s i` tends to `l.lift'
powerset` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the
actual statement.
Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional
argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these
arguments, `m i = (μ (s i)).to_real` is used in the output. -/
lemma filter.tendsto.integral_sub_linear_is_o_ae
[normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E]
{μ : measure α} {l : filter α} [l.is_measurably_generated]
{f : α → E} {b : E} (h : tendsto f (l ⊓ μ.ae) (𝓝 b))
(hfm : measurable_at_filter f l μ) (hμ : μ.finite_at_filter l)
{s : ι → set α} {li : filter ι} (hs : tendsto s li (l.lift' powerset))
(m : ι → ℝ := λ i, (μ (s i)).to_real)
(hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) :
is_o (λ i, ∫ x in s i, f x ∂μ - m i • b) m li :=
begin
suffices : is_o (λ s, ∫ x in s, f x ∂μ - (μ s).to_real • b) (λ s, (μ s).to_real)
(l.lift' powerset),
from (this.comp_tendsto hs).congr' (hsμ.mono $ λ a ha, ha ▸ rfl) hsμ,
refine is_o_iff.2 (λ ε ε₀, _),
have : ∀ᶠ s in l.lift' powerset, ∀ᶠ x in μ.ae, x ∈ s → f x ∈ closed_ball b ε :=
eventually_lift'_powerset_eventually.2 (h.eventually $ closed_ball_mem_nhds _ ε₀),
filter_upwards [hμ.eventually, (hμ.integrable_at_filter_of_tendsto_ae hfm h).eventually,
hfm.eventually, this],
simp only [mem_closed_ball, dist_eq_norm],
intros s hμs h_integrable hfm h_norm,
rw [← set_integral_const, ← integral_sub h_integrable (integrable_on_const.2 $ or.inr hμs),
real.norm_eq_abs, abs_of_nonneg ennreal.to_real_nonneg],
exact norm_set_integral_le_of_norm_le_const_ae' hμs h_norm (hfm.sub ae_measurable_const)
end
/-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally
finite measure and `f` is an almost everywhere measurable function that is continuous at a point `a`
within a measurable set `t`, then `∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at a filter `li`
provided that `s i` tends to `(𝓝[t] a).lift' powerset` along `li`. Since `μ (s i)` is an `ℝ≥0∞`
number, we use `(μ (s i)).to_real` in the actual statement.
Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional
argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these
arguments, `m i = (μ (s i)).to_real` is used in the output. -/
lemma continuous_within_at.integral_sub_linear_is_o_ae
[topological_space α] [opens_measurable_space α]
[normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E]
{μ : measure α} [is_locally_finite_measure μ] {a : α} {t : set α}
{f : α → E} (ha : continuous_within_at f t a) (ht : measurable_set t)
(hfm : measurable_at_filter f (𝓝[t] a) μ)
{s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝[t] a).lift' powerset))
(m : ι → ℝ := λ i, (μ (s i)).to_real)
(hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) :
is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li :=
by haveI : (𝓝[t] a).is_measurably_generated := ht.nhds_within_is_measurably_generated _;
exact (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae
hfm (μ.finite_at_nhds_within a t) hs m hsμ
/-- Fundamental theorem of calculus for set integrals, `nhds` version: if `μ` is a locally finite
measure and `f` is an almost everywhere measurable function that is continuous at a point `a`, then
`∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at `li` provided that `s` tends to `(𝓝 a).lift'
powerset` along `li. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the
actual statement.
Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional
argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these
arguments, `m i = (μ (s i)).to_real` is used in the output. -/
lemma continuous_at.integral_sub_linear_is_o_ae
[topological_space α] [opens_measurable_space α]
[normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E]
{μ : measure α} [is_locally_finite_measure μ] {a : α}
{f : α → E} (ha : continuous_at f a) (hfm : measurable_at_filter f (𝓝 a) μ)
{s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝 a).lift' powerset))
(m : ι → ℝ := λ i, (μ (s i)).to_real)
(hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) :
is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li :=
(ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds a) hs m hsμ
/-- If a function is continuous on an open set `s`, then it is measurable at the filter `𝓝 x` for
all `x ∈ s`. -/
lemma continuous_on.measurable_at_filter
[topological_space α] [opens_measurable_space α] [measurable_space β] [topological_space β]
[borel_space β]
{f : α → β} {s : set α} {μ : measure α} (hs : is_open s) (hf : continuous_on f s) :
∀ x ∈ s, measurable_at_filter f (𝓝 x) μ :=
λ x hx, ⟨s, is_open.mem_nhds hs hx, hf.ae_measurable hs.measurable_set⟩
lemma continuous_at.measurable_at_filter
[topological_space α] [opens_measurable_space α] [borel_space E]
{f : α → E} {s : set α} {μ : measure α} (hs : is_open s) (hf : ∀ x ∈ s, continuous_at f x) :
∀ x ∈ s, measurable_at_filter f (𝓝 x) μ :=
continuous_on.measurable_at_filter hs $ continuous_at.continuous_on hf
lemma continuous.measurable_at_filter [topological_space α] [opens_measurable_space α]
[measurable_space β] [topological_space β] [borel_space β] {f : α → β} (hf : continuous f)
(μ : measure α) (l : filter α) :
measurable_at_filter f l μ :=
hf.measurable.measurable_at_filter
/-- If a function is continuous on a measurable set `s`, then it is measurable at the filter
`𝓝[s] x` for all `x`. -/
lemma continuous_on.measurable_at_filter_nhds_within {α β : Type*} [measurable_space α]
[topological_space α] [opens_measurable_space α] [measurable_space β] [topological_space β]
[borel_space β] {f : α → β} {s : set α} {μ : measure α}
(hf : continuous_on f s) (hs : measurable_set s) (x : α) :
measurable_at_filter f (𝓝[s] x) μ :=
⟨s, self_mem_nhds_within, hf.ae_measurable hs⟩
/-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally
finite measure, `f` is continuous on a measurable set `t`, and `a ∈ t`, then `∫ x in (s i), f x ∂μ =
μ (s i) • f a + o(μ (s i))` at `li` provided that `s i` tends to `(𝓝[t] a).lift' powerset` along
`li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement.
Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional
argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these
arguments, `m i = (μ (s i)).to_real` is used in the output. -/
lemma continuous_on.integral_sub_linear_is_o_ae
[topological_space α] [opens_measurable_space α]
[normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E]
{μ : measure α} [is_locally_finite_measure μ] {a : α} {t : set α}
{f : α → E} (hft : continuous_on f t) (ha : a ∈ t) (ht : measurable_set t)
{s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝[t] a).lift' powerset))
(m : ι → ℝ := λ i, (μ (s i)).to_real)
(hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) :
is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li :=
(hft a ha).integral_sub_linear_is_o_ae ht ⟨t, self_mem_nhds_within, hft.ae_measurable ht⟩ hs m hsμ
section
/-! ### Continuous linear maps composed with integration
The goal of this section is to prove that integration commutes with continuous linear maps.
This holds for simple functions. The general result follows from the continuity of all involved
operations on the space `L¹`. Note that composition by a continuous linear map on `L¹` is not just
the composition, as we are dealing with classes of functions, but it has already been defined
as `continuous_linear_map.comp_Lp`. We take advantage of this construction here.
-/
open_locale complex_conjugate
variables {μ : measure α} {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E]
[normed_group F] [normed_space 𝕜 F]
{p : ennreal}
namespace continuous_linear_map
variables [measurable_space F] [borel_space F]
variables [second_countable_topology F] [complete_space F]
[borel_space E] [second_countable_topology E] [normed_space ℝ F]
lemma integral_comp_Lp (L : E →L[𝕜] F) (φ : Lp E p μ) :
∫ a, (L.comp_Lp φ) a ∂μ = ∫ a, L (φ a) ∂μ :=
integral_congr_ae $ coe_fn_comp_Lp _ _
lemma set_integral_comp_Lp (L : E →L[𝕜] F) (φ : Lp E p μ) {s : set α} (hs : measurable_set s) :
∫ a in s, (L.comp_Lp φ) a ∂μ = ∫ a in s, L (φ a) ∂μ :=
set_integral_congr_ae hs ((L.coe_fn_comp_Lp φ).mono (λ x hx hx2, hx))
lemma continuous_integral_comp_L1 (L : E →L[𝕜] F) :
continuous (λ (φ : α →₁[μ] E), ∫ (a : α), L (φ a) ∂μ) :=
by { rw ← funext L.integral_comp_Lp, exact continuous_integral.comp (L.comp_LpL 1 μ).continuous, }
variables [complete_space E] [normed_space ℝ E]
lemma integral_comp_comm (L : E →L[𝕜] F) {φ : α → E} (φ_int : integrable φ μ) :
∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) :=
begin
apply integrable.induction (λ φ, ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ)),
{ intros e s s_meas s_finite,
rw [integral_indicator_const e s_meas, ← @smul_one_smul E ℝ 𝕜 _ _ _ _ _ (μ s).to_real e,
continuous_linear_map.map_smul, @smul_one_smul F ℝ 𝕜 _ _ _ _ _ (μ s).to_real (L e),
← integral_indicator_const (L e) s_meas],
congr' 1 with a,
rw set.indicator_comp_of_zero L.map_zero },
{ intros f g H f_int g_int hf hg,
simp [L.map_add, integral_add f_int g_int,
integral_add (L.integrable_comp f_int) (L.integrable_comp g_int), hf, hg] },
{ exact is_closed_eq L.continuous_integral_comp_L1 (L.continuous.comp continuous_integral) },
{ intros f g hfg f_int hf,
convert hf using 1 ; clear hf,
{ exact integral_congr_ae (hfg.fun_comp L).symm },
{ rw integral_congr_ae hfg.symm } },
all_goals { assumption }
end
lemma integral_apply {H : Type*} [normed_group H] [normed_space 𝕜 H]
[second_countable_topology $ H →L[𝕜] E] {φ : α → H →L[𝕜] E} (φ_int : integrable φ μ) (v : H) :
(∫ a, φ a ∂μ) v = ∫ a, φ a v ∂μ :=
((continuous_linear_map.apply 𝕜 E v).integral_comp_comm φ_int).symm
lemma integral_comp_comm' (L : E →L[𝕜] F) {K} (hL : antilipschitz_with K L) (φ : α → E) :
∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) :=
begin
by_cases h : integrable φ μ,
{ exact integral_comp_comm L h },
have : ¬ (integrable (L ∘ φ) μ),
by rwa lipschitz_with.integrable_comp_iff_of_antilipschitz L.lipschitz hL (L.map_zero),
simp [integral_undef, h, this]
end
lemma integral_comp_L1_comm (L : E →L[𝕜] F) (φ : α →₁[μ] E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) :=
L.integral_comp_comm (L1.integrable_coe_fn φ)
end continuous_linear_map
namespace linear_isometry
variables [measurable_space F] [borel_space F] [second_countable_topology F] [complete_space F]
[normed_space ℝ F]
[borel_space E] [second_countable_topology E] [complete_space E] [normed_space ℝ E]
lemma integral_comp_comm (L : E →ₗᵢ[𝕜] F) (φ : α → E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) :=
L.to_continuous_linear_map.integral_comp_comm' L.antilipschitz _
end linear_isometry
variables [borel_space E] [second_countable_topology E] [complete_space E] [normed_space ℝ E]
[measurable_space F] [borel_space F] [second_countable_topology F] [complete_space F]
[normed_space ℝ F]
@[norm_cast] lemma integral_of_real {f : α → ℝ} : ∫ a, (f a : 𝕜) ∂μ = ↑∫ a, f a ∂μ :=
(@is_R_or_C.of_real_li 𝕜 _).integral_comp_comm f
lemma integral_re {f : α → 𝕜} (hf : integrable f μ) :
∫ a, is_R_or_C.re (f a) ∂μ = is_R_or_C.re ∫ a, f a ∂μ :=
(@is_R_or_C.re_clm 𝕜 _).integral_comp_comm hf
lemma integral_im {f : α → 𝕜} (hf : integrable f μ) :
∫ a, is_R_or_C.im (f a) ∂μ = is_R_or_C.im ∫ a, f a ∂μ :=
(@is_R_or_C.im_clm 𝕜 _).integral_comp_comm hf
lemma integral_conj {f : α → 𝕜} : ∫ a, conj (f a) ∂μ = conj ∫ a, f a ∂μ :=
(@is_R_or_C.conj_lie 𝕜 _).to_linear_isometry.integral_comp_comm f
lemma integral_coe_re_add_coe_im {f : α → 𝕜} (hf : integrable f μ) :
∫ x, (is_R_or_C.re (f x) : 𝕜) ∂μ + ∫ x, is_R_or_C.im (f x) ∂μ * is_R_or_C.I = ∫ x, f x ∂μ :=
begin
rw [mul_comm, ← smul_eq_mul, ← integral_smul, ← integral_add],
{ congr,
ext1 x,
rw [smul_eq_mul, mul_comm, is_R_or_C.re_add_im] },
{ exact hf.re.of_real },
{ exact hf.im.of_real.smul is_R_or_C.I }
end
lemma integral_re_add_im {f : α → 𝕜} (hf : integrable f μ) :
((∫ x, is_R_or_C.re (f x) ∂μ : ℝ) : 𝕜) + (∫ x, is_R_or_C.im (f x) ∂μ : ℝ) * is_R_or_C.I =
∫ x, f x ∂μ :=
by { rw [← integral_of_real, ← integral_of_real, integral_coe_re_add_coe_im hf] }
lemma set_integral_re_add_im {f : α → 𝕜} {i : set α} (hf : integrable_on f i μ) :
((∫ x in i, is_R_or_C.re (f x) ∂μ : ℝ) : 𝕜) +
(∫ x in i, is_R_or_C.im (f x) ∂μ : ℝ) * is_R_or_C.I = ∫ x in i, f x ∂μ :=
integral_re_add_im hf
lemma fst_integral {f : α → E × F} (hf : integrable f μ) :
(∫ x, f x ∂μ).1 = ∫ x, (f x).1 ∂μ :=
((continuous_linear_map.fst ℝ E F).integral_comp_comm hf).symm
lemma snd_integral {f : α → E × F} (hf : integrable f μ) :
(∫ x, f x ∂μ).2 = ∫ x, (f x).2 ∂μ :=
((continuous_linear_map.snd ℝ E F).integral_comp_comm hf).symm
lemma integral_pair {f : α → E} {g : α → F} (hf : integrable f μ) (hg : integrable g μ) :
∫ x, (f x, g x) ∂μ = (∫ x, f x ∂μ, ∫ x, g x ∂μ) :=
have _ := hf.prod_mk hg, prod.ext (fst_integral this) (snd_integral this)
lemma integral_smul_const {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] (f : α → 𝕜) (c : E) :
∫ x, f x • c ∂μ = (∫ x, f x ∂μ) • c :=
begin
by_cases hf : integrable f μ,
{ exact ((1 : 𝕜 →L[𝕜] 𝕜).smul_right c).integral_comp_comm hf },
{ by_cases hc : c = 0,
{ simp only [hc, integral_zero, smul_zero] },
rw [integral_undef hf, integral_undef, zero_smul],
simp_rw [integrable_smul_const hc, hf, not_false_iff] }
end
section inner
variables {E' : Type*} [inner_product_space 𝕜 E'] [measurable_space E'] [borel_space E']
[second_countable_topology E'] [complete_space E'] [normed_space ℝ E']
local notation `⟪`x`, `y`⟫` := @inner 𝕜 E' _ x y
lemma integral_inner {f : α → E'} (hf : integrable f μ) (c : E') :
∫ x, ⟪c, f x⟫ ∂μ = ⟪c, ∫ x, f x ∂μ⟫ :=
((@innerSL 𝕜 E' _ _ c).restrict_scalars ℝ).integral_comp_comm hf
lemma integral_eq_zero_of_forall_integral_inner_eq_zero (f : α → E') (hf : integrable f μ)
(hf_int : ∀ (c : E'), ∫ x, ⟪c, f x⟫ ∂μ = 0) :
∫ x, f x ∂μ = 0 :=
by { specialize hf_int (∫ x, f x ∂μ), rwa [integral_inner hf, inner_self_eq_zero] at hf_int }
end inner
end
|
c109513dc60d97c40bc1ec4e14210279c966418d | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/fun.lean | d265e0baa8832ce0fbcdc58810939c00826b4e48 | [
"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 | 265 | lean | open Function Bool
opaque f : Nat → Bool := default
opaque g : Nat → Nat := default
#check f ∘ g ∘ g
#check (id : Nat → Nat)
opaque h : Nat → Bool → Nat := default
opaque f1 : Nat → Nat → Bool := default
opaque f2 : Bool → Nat := default
|
b2981987e090b97eb019f98e7fe8d2e591f85704 | 9b9a16fa2cb737daee6b2785474678b6fa91d6d4 | /src/computability/halting.lean | fb1dd251662da93b51fd5ba8adafee7435f7b1e9 | [
"Apache-2.0"
] | permissive | johoelzl/mathlib | 253f46daa30b644d011e8e119025b01ad69735c4 | 592e3c7a2dfbd5826919b4605559d35d4d75938f | refs/heads/master | 1,625,657,216,488 | 1,551,374,946,000 | 1,551,374,946,000 | 98,915,829 | 0 | 0 | Apache-2.0 | 1,522,917,267,000 | 1,501,524,499,000 | Lean | UTF-8 | Lean | false | false | 11,716 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
More partial recursive functions using a universal program;
Rice's theorem and the halting problem.
-/
import computability.partrec_code
open encodable denumerable
namespace nat.partrec
open computable roption
theorem merge' {f g}
(hf : nat.partrec f) (hg : nat.partrec g) :
∃ h, nat.partrec h ∧ ∀ a,
(∀ x ∈ h a, x ∈ f a ∨ x ∈ g a) ∧
((h a).dom ↔ (f a).dom ∨ (g a).dom) :=
begin
rcases code.exists_code.1 hf with ⟨cf, rfl⟩,
rcases code.exists_code.1 hg with ⟨cg, rfl⟩,
have : nat.partrec (λ n,
(nat.rfind_opt (λ k, cf.evaln k n <|> cg.evaln k n))) :=
partrec.nat_iff.1 (partrec.rfind_opt $
primrec.option_orelse.to_comp.comp
(code.evaln_prim.to_comp.comp $ (snd.pair (const cf)).pair fst)
(code.evaln_prim.to_comp.comp $ (snd.pair (const cg)).pair fst)),
refine ⟨_, this, λ n, _⟩,
suffices, refine ⟨this,
⟨λ h, (this _ ⟨h, rfl⟩).imp Exists.fst Exists.fst, _⟩⟩,
{ intro h, rw nat.rfind_opt_dom,
simp [dom_iff_mem, code.evaln_complete] at h,
rcases h with ⟨x, k, e⟩ | ⟨x, k, e⟩,
{ refine ⟨k, x, _⟩, simp [e] },
{ refine ⟨k, _⟩,
cases cf.evaln k n with y,
{ exact ⟨x, by simp [e]⟩ },
{ exact ⟨y, by simp⟩ } } },
{ intros x h,
rcases nat.rfind_opt_spec h with ⟨k, e⟩,
revert e,
simp; cases e' : cf.evaln k n with y; simp; intro,
{ exact or.inr (code.evaln_sound e) },
{ subst y,
exact or.inl (code.evaln_sound e') } }
end
end nat.partrec
namespace partrec
variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*}
variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ]
open computable roption nat.partrec (code) nat.partrec.code
theorem merge' {f g : α →. σ}
(hf : partrec f) (hg : partrec g) :
∃ k : α →. σ, partrec k ∧ ∀ a,
(∀ x ∈ k a, x ∈ f a ∨ x ∈ g a) ∧
((k a).dom ↔ (f a).dom ∨ (g a).dom) :=
let ⟨k, hk, H⟩ :=
nat.partrec.merge' (bind_decode2_iff.1 hf) (bind_decode2_iff.1 hg) in
begin
let k' := λ a, (k (encode a)).bind (λ n, decode σ n),
refine ⟨k', ((nat_iff.2 hk).comp computable.encode).bind
(computable.decode.of_option.comp snd).to₂, λ a, _⟩,
suffices, refine ⟨this,
⟨λ h, (this _ ⟨h, rfl⟩).imp Exists.fst Exists.fst, _⟩⟩,
{ intro h, simp [k'],
have hk : (k (encode a)).dom :=
(H _).2.2 (by simpa [encodek2] using h),
existsi hk,
cases (H _).1 _ ⟨hk, rfl⟩ with h h;
{ simp at h,
rcases h with ⟨a', ha', y, hy, e⟩,
simp [e.symm, encodek] } },
{ intros x h', simp [k'] at h',
rcases h' with ⟨n, hn, hx⟩,
have := (H _).1 _ hn,
simp [mem_decode2, encode_injective.eq_iff] at this,
cases this with h h;
{ rcases h with ⟨a', ha, rfl⟩,
simp [encodek] at hx, subst a',
simp [ha] } },
end
theorem merge {f g : α →. σ}
(hf : partrec f) (hg : partrec g)
(H : ∀ a (x ∈ f a) (y ∈ g a), x = y) :
∃ k : α →. σ, partrec k ∧ ∀ a x, x ∈ k a ↔ x ∈ f a ∨ x ∈ g a :=
let ⟨k, hk, K⟩ := merge' hf hg in
⟨k, hk, λ a x, ⟨(K _).1 _, λ h, begin
have : (k a).dom := (K _).2.2 (h.imp Exists.fst Exists.fst),
refine ⟨this, _⟩,
cases h with h h; cases (K _).1 _ ⟨this, rfl⟩ with h' h',
{ exact mem_unique h' h },
{ exact (H _ _ h _ h').symm },
{ exact H _ _ h' _ h },
{ exact mem_unique h' h }
end⟩⟩
theorem cond {c : α → bool} {f : α →. σ} {g : α →. σ}
(hc : computable c) (hf : partrec f) (hg : partrec g) :
partrec (λ a, cond (c a) (f a) (g a)) :=
let ⟨cf, ef⟩ := exists_code.1 hf,
⟨cg, eg⟩ := exists_code.1 hg in
((eval_part.comp
(computable.cond hc (const cf) (const cg)) computable.id).bind
((@computable.decode σ _).comp snd).of_option.to₂).of_eq $
λ a, by cases c a; simp [ef, eg, encodek]
theorem sum_cases
{f : α → β ⊕ γ} {g : α → β →. σ} {h : α → γ →. σ}
(hf : computable f) (hg : partrec₂ g) (hh : partrec₂ h) :
@partrec _ σ _ _ (λ a, sum.cases_on (f a) (g a) (h a)) :=
option_some_iff.1 $ (cond
(sum_cases hf (const tt).to₂ (const ff).to₂)
(sum_cases_left hf (option_some_iff.2 hg).to₂ (const option.none).to₂)
(sum_cases_right hf (const option.none).to₂ (option_some_iff.2 hh).to₂))
.of_eq $ λ a, by cases f a; simp
end partrec
def computable_pred {α} [primcodable α] (p : α → Prop) :=
∃ [D : decidable_pred p],
by exactI computable (λ a, to_bool (p a))
/- recursively enumerable predicate -/
def re_pred {α} [primcodable α] (p : α → Prop) :=
partrec (λ a, roption.assert (p a) (λ _, roption.some ()))
theorem computable_pred.of_eq {α} [primcodable α]
{p q : α → Prop}
(hp : computable_pred p) (H : ∀ a, p a ↔ q a) : computable_pred q :=
(funext (λ a, propext (H a)) : p = q) ▸ hp
namespace computable_pred
variables {α : Type*} {σ : Type*}
variables [primcodable α] [primcodable σ]
open nat.partrec (code) nat.partrec.code computable
theorem rice (C : set (ℕ →. ℕ))
(h : computable_pred (λ c, eval c ∈ C))
{f g} (hf : nat.partrec f) (hg : nat.partrec g)
(fC : f ∈ C) : g ∈ C :=
begin
cases h with _ h, resetI,
rcases fixed_point₂ (partrec.cond (h.comp fst)
((partrec.nat_iff.2 hg).comp snd).to₂
((partrec.nat_iff.2 hf).comp snd).to₂).to₂ with ⟨c, e⟩,
simp at e,
by_cases eval c ∈ C,
{ simp [h] at e, rwa ← e },
{ simp at h, simp [h] at e,
rw e at h, contradiction }
end
theorem rice₂ (C : set code)
(H : ∀ cf cg, eval cf = eval cg → (cf ∈ C ↔ cg ∈ C)) :
computable_pred (λ c, c ∈ C) ↔ C = ∅ ∨ C = set.univ :=
by haveI := classical.dec; exact
have hC : ∀ f, f ∈ C ↔ eval f ∈ eval '' C,
from λ f, ⟨set.mem_image_of_mem _, λ ⟨g, hg, e⟩, (H _ _ e).1 hg⟩,
⟨λ h, or_iff_not_imp_left.2 $ λ C0,
set.eq_univ_of_forall $ λ cg,
let ⟨cf, fC⟩ := set.ne_empty_iff_exists_mem.1 C0 in
(hC _).2 $ rice (eval '' C) (h.of_eq hC)
(partrec.nat_iff.1 $ eval_part.comp (const cf) computable.id)
(partrec.nat_iff.1 $ eval_part.comp (const cg) computable.id)
((hC _).1 fC),
λ h, by rcases h with rfl | rfl; simp [computable_pred];
exact ⟨by apply_instance, computable.const _⟩⟩
theorem halting_problem (n) : ¬ computable_pred (λ c, (eval c n).dom)
| h := rice {f | (f n).dom} h nat.partrec.zero nat.partrec.none trivial
end computable_pred
namespace nat
open vector roption
/-- A simplified basis for `partrec`. -/
inductive partrec' : ∀ {n}, (vector ℕ n →. ℕ) → Prop
| prim {n f} : @primrec' n f → @partrec' n f
| comp {m n f} (g : fin n → vector ℕ m →. ℕ) :
partrec' f → (∀ i, partrec' (g i)) →
partrec' (λ v, m_of_fn (λ i, g i v) >>= f)
| rfind {n} {f : vector ℕ (n+1) → ℕ} : @partrec' (n+1) f →
partrec' (λ v, rfind (λ n, some (f (n :: v) = 0)))
end nat
namespace nat.partrec'
open vector partrec computable nat (partrec') nat.partrec'
theorem to_part {n f} (pf : @partrec' n f) : partrec f :=
begin
induction pf,
case nat.partrec'.prim : n f hf { exact hf.to_prim.to_comp },
case nat.partrec'.comp : m n f g _ _ hf hg {
exact (vector_m_of_fn (λ i, hg i)).bind (hf.comp snd) },
case nat.partrec'.rfind : n f _ hf {
have := ((primrec.eq.comp primrec.id (primrec.const 0)).to_comp.comp
(hf.comp (vector_cons.comp snd fst))).to₂.part,
exact this.rfind },
end
theorem of_eq {n} {f g : vector ℕ n →. ℕ}
(hf : partrec' f) (H : ∀ i, f i = g i) : partrec' g :=
(funext H : f = g) ▸ hf
theorem of_prim {n} {f : vector ℕ n → ℕ} (hf : primrec f) : @partrec' n f :=
prim (nat.primrec'.of_prim hf)
theorem head {n : ℕ} : @partrec' n.succ (@head ℕ n) :=
prim nat.primrec'.head
theorem tail {n f} (hf : @partrec' n f) : @partrec' n.succ (λ v, f v.tail) :=
(hf.comp _ (λ i, @prim _ _ $ nat.primrec'.nth i.succ)).of_eq $
λ v, by simp; rw [← of_fn_nth v.tail]; congr; funext i; simp
protected theorem bind {n f g}
(hf : @partrec' n f) (hg : @partrec' (n+1) g) :
@partrec' n (λ v, (f v).bind (λ a, g (a :: v))) :=
(@comp n (n+1) g
(λ i, fin.cases f (λ i v, some (v.nth i)) i) hg
(λ i, begin
refine fin.cases _ (λ i, _) i; simp *,
exact prim (nat.primrec'.nth _)
end)).of_eq $
λ v, by simp [m_of_fn, roption.bind_assoc, pure]
protected theorem map {n f} {g : vector ℕ (n+1) → ℕ}
(hf : @partrec' n f) (hg : @partrec' (n+1) g) :
@partrec' n (λ v, (f v).map (λ a, g (a :: v))) :=
by simp [(roption.bind_some_eq_map _ _).symm];
exact hf.bind hg
def vec {n m} (f : vector ℕ n → vector ℕ m) :=
∀ i, partrec' (λ v, (f v).nth i)
theorem vec.prim {n m f} (hf : @nat.primrec'.vec n m f) : vec f :=
λ i, prim $ hf i
protected theorem nil {n} : @vec n 0 (λ _, nil) := λ i, i.elim0
protected theorem cons {n m} {f : vector ℕ n → ℕ} {g}
(hf : @partrec' n f) (hg : @vec n m g) :
vec (λ v, f v :: g v) :=
λ i, fin.cases (by simp *) (λ i, by simp [hg i]) i
theorem idv {n} : @vec n n id := vec.prim nat.primrec'.idv
theorem comp' {n m f g} (hf : @partrec' m f) (hg : @vec n m g) :
partrec' (λ v, f (g v)) :=
(hf.comp _ hg).of_eq $ λ v, by simp
theorem comp₁ {n} (f : ℕ →. ℕ) {g : vector ℕ n → ℕ}
(hf : @partrec' 1 (λ v, f v.head)) (hg : @partrec' n g) :
@partrec' n (λ v, f (g v)) :=
by simpa using hf.comp' (partrec'.cons hg partrec'.nil)
theorem rfind_opt {n} {f : vector ℕ (n+1) → ℕ}
(hf : @partrec' (n+1) f) :
@partrec' n (λ v, nat.rfind_opt (λ a, of_nat (option ℕ) (f (a :: v)))) :=
((rfind $ (of_prim (primrec.nat_sub.comp (primrec.const 1) primrec.vector_head))
.comp₁ (λ n, roption.some (1 - n)) hf)
.bind ((prim nat.primrec'.pred).comp₁ nat.pred hf)).of_eq $
λ v, roption.ext $ λ b, begin
simp [nat.rfind_opt, -nat.mem_rfind],
refine exists_congr (λ a,
(and_congr (iff_of_eq _) iff.rfl).trans (and_congr_right (λ h, _))),
{ congr; funext n,
simp, cases f (n :: v); simp [nat.succ_ne_zero]; refl },
{ have := nat.rfind_spec h,
simp at this,
cases f (a :: v) with c, {cases this},
rw [← option.some_inj, eq_comm], refl }
end
open nat.partrec.code
theorem of_part : ∀ {n f}, partrec f → @partrec' n f :=
suffices ∀ f, nat.partrec f → @partrec' 1 (λ v, f v.head), from
λ n f hf, begin
let g, swap,
exact (comp₁ g (this g hf) (prim nat.primrec'.encode)).of_eq
(λ i, by dsimp [g]; simp [encodek, roption.map_id']),
end,
λ f hf, begin
rcases exists_code.1 hf with ⟨c, rfl⟩,
simpa [eval_eq_rfind_opt] using
(rfind_opt $ of_prim $ primrec.encode_iff.2 $ evaln_prim.comp $
(primrec.vector_head.pair (primrec.const c)).pair $
primrec.vector_head.comp primrec.vector_tail)
end
theorem part_iff {n f} : @partrec' n f ↔ partrec f := ⟨to_part, of_part⟩
theorem part_iff₁ {f : ℕ →. ℕ} :
@partrec' 1 (λ v, f v.head) ↔ partrec f :=
part_iff.trans ⟨
λ h, (h.comp $ (primrec.vector_of_fn $
λ i, primrec.id).to_comp).of_eq (λ v, by simp),
λ h, h.comp vector_head⟩
theorem part_iff₂ {f : ℕ → ℕ →. ℕ} :
@partrec' 2 (λ v, f v.head v.tail.head) ↔ partrec₂ f :=
part_iff.trans ⟨
λ h, (h.comp $ vector_cons.comp fst $
vector_cons.comp snd (const nil)).of_eq (λ v, by simp),
λ h, h.comp vector_head (vector_head.comp vector_tail)⟩
theorem vec_iff {m n f} : @vec m n f ↔ computable f :=
⟨λ h, by simpa using vector_of_fn (λ i, to_part (h i)),
λ h i, of_part $ vector_nth.comp h (const i)⟩
end nat.partrec'
|
9fdbd6536355679a2a52a4f5c3998a31f179c78a | a1179fa077c09acc49e4fbc8d67084ba89ac4f4c | /sublattice/odd_submonoid.lean | 537ae9086b59106a1918e2d14859343d3bf6a2a1 | [] | 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 | 5,525 | lean | import tactic
-- monoidy a submonoidy : prvy den experimentov
-- ignoroval som vacsinu toho co uz je implementovane
-- skusil som spravit z konkretneho submonoidu neparnych cisel subtyp
-- monoidu (nat,*,1) a potom som na nom implementoval monoidovu strukturu
-- monoid je mnozina vybavena binarnou operaciou, ktora je
-- asociativna a jednotkovym prvkom ; spravim teraz
-- dvojprvkovy polozvaz S2:=({zero,one},one,⊓) holymi rukami
-- dvojprvkovy induktivny typ
inductive S2: Type
| zero
| one
-- ⊓ | zero | one |
-- --------------------
-- zero | zero | zero |
-- one | zero | one |
#check S2.zero
-- to, ze je toto nazvane S2.mul je konvencia, nie nevyhnutnost
-- mohlo by sa to volat aj hocijako inak
-- takisto S2.monoid dolu
def S2.mul: S2 → S2 → S2
| S2.one a := a
| a S2.one := a
| S2.zero S2.zero := S2.zero
-- teraz poviem, ze (S2,S2.one,S2.mul) je monoid
-- mechanizmus, ktorym sa to robi je ten,
-- ze dokazem, ze typ S2 je instanciou typeclass monoid
@[instance]
def S2.monoid : monoid S2 :=
{
mul:= S2.mul,
one:= S2.one,
-- tu musim dokazat, ze ta operacia je asociativna
-- asi by to islo lahsie tak, ze najprv dokazem
-- jednotkove zakony a potom ich pouzijem
mul_assoc:=
begin
intros a b c,
cases a,
cases b,
cases c,
refl,
refl,
cases c,
refl,
refl,
cases b,
cases c,
refl,
refl,
cases c,
refl,
refl,
end,
-- jednotkovy zakon zlava a sprava
one_mul:=
begin
intro a,
cases a,
refl,
refl,
end,
mul_one :=
begin
intro a,
cases a,
refl,
refl,
end
}
-- V tomto momente je S2 pouzitelny ako monoid
-- medziinym mame aj nasobenie prvkov cez *
-- kvoli tomu, ze typeclass monoid rozsiruje typeclass has_mul
-- has_mul je typeclass, ktory zavadza binarnu operaciu
-- mul a notaciu _ * _
#check S2.zero*S2.one
#reduce S2.zero*S2.one
-- takisto mame konstantu 1, (typeclass has_one) ktora sa nam prepisuje
-- na spravny prvok typu S2
#check (1:S2)
#reduce (1:S2)
-- takisto mozeme pouzivat asociativny zakon
#check monoid.mul_assoc
#check monoid.mul_assoc S2.zero S2.one S2.zero
-- Teraz idem vyskusat, ako funguju submonoidy
-- ked sa referencuje na ℕ ako monoid, mysli sa nasobenie
-- idem vyrobit submonoid monoidu (ℕ,*,1)
-- pozostavajuci z neparnych cisel
-- submonoid je trojica
-- carrier : podmnozina monoidu
-- one_mem' : dokaz, ze jednotka monoidu patri do monoidu
-- mul_mem' : dokaz, ze submonoid je uzavrety na nasobenie
def odd_nat_submonoid: submonoid ℕ :=
{
carrier := {n:ℕ | odd n}, -- mnozina
one_mem' := -- dokaz, ze jednotka patri do tej mnoziny
begin
simp,
existsi 0,
simp,
end,
mul_mem' := -- dokaz, ze ta mnozina je uzavreta na nasobenie
begin
intros a b,
intro a_odd,
intro b_odd,
simp at a_odd,
simp at b_odd,
simp,
cases a_odd with k,
cases b_odd with l,
existsi 2*k*l+k+l, -- som musel toto zratat na papieri
rw a_odd_h,
rw b_odd_h,
ring, -- taktika pre okruhy (nasobenie+scitanie), usetri robotu
end
}
-- v mathlib je implementovana o submonoidoch cela kopa veci:
-- submonoidy monoidu tvoria poset vzhladom na inkluziu
-- prienik sumbonoidov je submonoid,
-- atd ...
--- ALE!
-- submonoid nie je monoid, nie v leane
-- teraz idem z toho submonoidu neparnych cisel vytvorit monoid
-- vytvorime subtyp (vid subtype v TPiL)
def odd_nat_subtype := {n:ℕ // n ∈ odd_nat_submonoid.carrier}
-- ten subtyp je typ struktur { val:= n:ℕ, prop:= dokaz, ze n je neparne }
-- nie je pravda, ze 3:odd_nat_subtype
-- ideme teraz pre odd_nat_subtype definovat jednotku a nasobenie
-- toto bude jednotka
@[simp]
def one_odd : odd_nat_subtype := {
val := 1,
property := begin -- musim dokazat, ze 1 je neparna ale to som uz robil
exact odd_nat_submonoid.one_mem',
end
}
-- jednotlive zlozky dvojice
#reduce one_odd.val
#reduce one_odd.property -- hmm
-- chcem este nejaky iny prvok
-- ⟨ ... ⟩ je alternativna (nepomenovana) notacia pre {...} s pomenovanymi
-- zlozkami
def three_odd : odd_nat_subtype :=
⟨ 3,
begin
change 3∈ { n:ℕ | odd(n)}, -- toto je strasne neobratne, ako to ide lepsie?
simp,
existsi 1,
ring,
end
⟩
-- teraz skusme zadefinovat nasobenie neparnych cisel aj
-- s transformaciou dokazu neparnosti
@[simp] -- zapojime do simp mechanizmu, to sa zide neskor
def odd_mul (a b:odd_nat_subtype) : odd_nat_subtype :=
⟨ a.val*b.val,
begin
have a_odd := a.property,
have b_odd := b.property,
apply odd_nat_submonoid.mul_mem',
exact a_odd,
exact b_odd,
end
⟩
lemma one_mul_odd {a :odd_nat_subtype} : odd_mul one_odd a = a :=
by simp
lemma mul_one_odd {a :odd_nat_subtype} : odd_mul a one_odd = a :=
by simp
#reduce odd_mul one_odd one_odd
#reduce odd_mul three_odd three_odd
def odd_nine := odd_mul three_odd three_odd
#reduce odd_nine.val -- 9
#reduce odd_nine.property -- tato hodnota je extremne zaujimava ?!ODKIAL?!
-- skusme teraz vyvorit instanciu monoidu z odd_nat_subtype
@[instance] def odd_nat_monoid : monoid odd_nat_subtype :=
{
mul:=odd_mul,
one:=one_odd,
mul_assoc := begin
finish, -- toto ide kvoli tomu @[simp] atributu vyssie
end,
one_mul:= begin
intro,
have ha:= @one_mul_odd a,
exact ha,
end,
mul_one:= begin
intro,
have ha:= @mul_one_odd a,
exact ha,
end
}
#reduce (odd_nine*odd_nine).val -- 81, hura!
|
81dbd990dcb631cef875d1111c6309dd15482a00 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/control/equiv_functor/instances.lean | 5f3324e9789d43ced26ca9f7e459ad9e44477ad6 | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 794 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Scott Morrison
-/
import data.finset.basic
import control.equiv_functor
/-!
# `equiv_functor` instances
We derive some `equiv_functor` instances, to enable `equiv_rw` to rewrite under these functions.
-/
open equiv
instance equiv_functor_unique : equiv_functor unique :=
{ map := λ α β e, equiv.unique_congr e, }
instance equiv_functor_perm : equiv_functor perm :=
{ map := λ α β e p, (e.symm.trans p).trans e }
-- There is a classical instance of `is_lawful_functor finset` available,
-- but we provide this computable alternative separately.
instance equiv_functor_finset : equiv_functor finset :=
{ map := λ α β e s, s.map e.to_embedding, }
|
f7afccc930895466d61fc2068482a1d86ca047a0 | b70447c014d9e71cf619ebc9f539b262c19c2e0b | /hott/algebra/inf_group.hlean | e2ccb70a0764c0197bd548fd7ea57e7bea21feaf | [
"Apache-2.0"
] | permissive | ia0/lean2 | c20d8da69657f94b1d161f9590a4c635f8dc87f3 | d86284da630acb78fa5dc3b0b106153c50ffccd0 | refs/heads/master | 1,611,399,322,751 | 1,495,751,007,000 | 1,495,751,007,000 | 93,104,167 | 0 | 0 | null | 1,496,355,488,000 | 1,496,355,487,000 | null | UTF-8 | Lean | false | false | 26,588 | hlean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
import algebra.binary algebra.priority
open eq eq.ops -- note: ⁻¹ will be overloaded
open binary algebra is_trunc
set_option class.force_new true
variable {A : Type}
/- inf_semigroup -/
namespace algebra
structure inf_semigroup [class] (A : Type) extends has_mul A :=
(mul_assoc : Πa b c, mul (mul a b) c = mul a (mul b c))
definition mul.assoc [s : inf_semigroup A] (a b c : A) : a * b * c = a * (b * c) :=
!inf_semigroup.mul_assoc
structure comm_inf_semigroup [class] (A : Type) extends inf_semigroup A :=
(mul_comm : Πa b, mul a b = mul b a)
definition mul.comm [s : comm_inf_semigroup A] (a b : A) : a * b = b * a :=
!comm_inf_semigroup.mul_comm
theorem mul.left_comm [s : comm_inf_semigroup A] (a b c : A) : a * (b * c) = b * (a * c) :=
binary.left_comm (@mul.comm A _) (@mul.assoc A _) a b c
theorem mul.right_comm [s : comm_inf_semigroup A] (a b c : A) : (a * b) * c = (a * c) * b :=
binary.right_comm (@mul.comm A _) (@mul.assoc A _) a b c
structure left_cancel_inf_semigroup [class] (A : Type) extends inf_semigroup A :=
(mul_left_cancel : Πa b c, mul a b = mul a c → b = c)
theorem mul.left_cancel [s : left_cancel_inf_semigroup A] {a b c : A} :
a * b = a * c → b = c :=
!left_cancel_inf_semigroup.mul_left_cancel
abbreviation eq_of_mul_eq_mul_left' := @mul.left_cancel
structure right_cancel_inf_semigroup [class] (A : Type) extends inf_semigroup A :=
(mul_right_cancel : Πa b c, mul a b = mul c b → a = c)
definition mul.right_cancel [s : right_cancel_inf_semigroup A] {a b c : A} :
a * b = c * b → a = c :=
!right_cancel_inf_semigroup.mul_right_cancel
abbreviation eq_of_mul_eq_mul_right' := @mul.right_cancel
/- additive inf_semigroup -/
definition add_inf_semigroup [class] : Type → Type := inf_semigroup
definition has_add_of_add_inf_semigroup [reducible] [trans_instance] (A : Type) [H : add_inf_semigroup A] :
has_add A :=
has_add.mk (@inf_semigroup.mul A H)
definition add.assoc [s : add_inf_semigroup A] (a b c : A) : a + b + c = a + (b + c) :=
@mul.assoc A s a b c
definition add_comm_inf_semigroup [class] : Type → Type := comm_inf_semigroup
definition add_inf_semigroup_of_add_comm_inf_semigroup [reducible] [trans_instance] (A : Type)
[H : add_comm_inf_semigroup A] : add_inf_semigroup A :=
@comm_inf_semigroup.to_inf_semigroup A H
definition add.comm [s : add_comm_inf_semigroup A] (a b : A) : a + b = b + a :=
@mul.comm A s a b
theorem add.left_comm [s : add_comm_inf_semigroup A] (a b c : A) :
a + (b + c) = b + (a + c) :=
binary.left_comm (@add.comm A _) (@add.assoc A _) a b c
theorem add.right_comm [s : add_comm_inf_semigroup A] (a b c : A) : (a + b) + c = (a + c) + b :=
binary.right_comm (@add.comm A _) (@add.assoc A _) a b c
definition add_left_cancel_inf_semigroup [class] : Type → Type := left_cancel_inf_semigroup
definition add_inf_semigroup_of_add_left_cancel_inf_semigroup [reducible] [trans_instance] (A : Type)
[H : add_left_cancel_inf_semigroup A] : add_inf_semigroup A :=
@left_cancel_inf_semigroup.to_inf_semigroup A H
definition add.left_cancel [s : add_left_cancel_inf_semigroup A] {a b c : A} :
a + b = a + c → b = c :=
@mul.left_cancel A s a b c
abbreviation eq_of_add_eq_add_left := @add.left_cancel
definition add_right_cancel_inf_semigroup [class] : Type → Type := right_cancel_inf_semigroup
definition add_inf_semigroup_of_add_right_cancel_inf_semigroup [reducible] [trans_instance] (A : Type)
[H : add_right_cancel_inf_semigroup A] : add_inf_semigroup A :=
@right_cancel_inf_semigroup.to_inf_semigroup A H
definition add.right_cancel [s : add_right_cancel_inf_semigroup A] {a b c : A} :
a + b = c + b → a = c :=
@mul.right_cancel A s a b c
abbreviation eq_of_add_eq_add_right := @add.right_cancel
/- inf_monoid -/
structure inf_monoid [class] (A : Type) extends inf_semigroup A, has_one A :=
(one_mul : Πa, mul one a = a) (mul_one : Πa, mul a one = a)
definition one_mul [s : inf_monoid A] (a : A) : 1 * a = a := !inf_monoid.one_mul
definition mul_one [s : inf_monoid A] (a : A) : a * 1 = a := !inf_monoid.mul_one
structure comm_inf_monoid [class] (A : Type) extends inf_monoid A, comm_inf_semigroup A
/- additive inf_monoid -/
definition add_inf_monoid [class] : Type → Type := inf_monoid
definition add_inf_semigroup_of_add_inf_monoid [reducible] [trans_instance] (A : Type)
[H : add_inf_monoid A] : add_inf_semigroup A :=
@inf_monoid.to_inf_semigroup A H
definition has_zero_of_add_inf_monoid [reducible] [trans_instance] (A : Type)
[H : add_inf_monoid A] : has_zero A :=
has_zero.mk (@inf_monoid.one A H)
definition zero_add [s : add_inf_monoid A] (a : A) : 0 + a = a := @inf_monoid.one_mul A s a
definition add_zero [s : add_inf_monoid A] (a : A) : a + 0 = a := @inf_monoid.mul_one A s a
definition add_comm_inf_monoid [class] : Type → Type := comm_inf_monoid
definition add_inf_monoid_of_add_comm_inf_monoid [reducible] [trans_instance] (A : Type)
[H : add_comm_inf_monoid A] : add_inf_monoid A :=
@comm_inf_monoid.to_inf_monoid A H
definition add_comm_inf_semigroup_of_add_comm_inf_monoid [reducible] [trans_instance] (A : Type)
[H : add_comm_inf_monoid A] : add_comm_inf_semigroup A :=
@comm_inf_monoid.to_comm_inf_semigroup A H
section add_comm_inf_monoid
variables [s : add_comm_inf_monoid A]
include s
theorem add_comm_three (a b c : A) : a + b + c = c + b + a :=
by rewrite [{a + _}add.comm, {_ + c}add.comm, -*add.assoc]
theorem add.comm4 : Π (n m k l : A), n + m + (k + l) = n + k + (m + l) :=
comm4 add.comm add.assoc
end add_comm_inf_monoid
/- group -/
structure inf_group [class] (A : Type) extends inf_monoid A, has_inv A :=
(mul_left_inv : Πa, mul (inv a) a = one)
-- Note: with more work, we could derive the axiom one_mul
section inf_group
variable [s : inf_group A]
include s
definition mul.left_inv (a : A) : a⁻¹ * a = 1 := !inf_group.mul_left_inv
theorem inv_mul_cancel_left (a b : A) : a⁻¹ * (a * b) = b :=
by rewrite [-mul.assoc, mul.left_inv, one_mul]
theorem inv_mul_cancel_right (a b : A) : a * b⁻¹ * b = a :=
by rewrite [mul.assoc, mul.left_inv, mul_one]
theorem inv_eq_of_mul_eq_one {a b : A} (H : a * b = 1) : a⁻¹ = b :=
by rewrite [-mul_one a⁻¹, -H, inv_mul_cancel_left]
theorem one_inv : 1⁻¹ = (1 : A) := inv_eq_of_mul_eq_one (one_mul 1)
theorem inv_inv (a : A) : (a⁻¹)⁻¹ = a := inv_eq_of_mul_eq_one (mul.left_inv a)
theorem inv.inj {a b : A} (H : a⁻¹ = b⁻¹) : a = b :=
by rewrite [-inv_inv a, H, inv_inv b]
theorem inv_eq_inv_iff_eq (a b : A) : a⁻¹ = b⁻¹ ↔ a = b :=
iff.intro (assume H, inv.inj H) (assume H, ap _ H)
theorem inv_eq_one_iff_eq_one (a : A) : a⁻¹ = 1 ↔ a = 1 :=
one_inv ▸ inv_eq_inv_iff_eq a 1
theorem inv_eq_one {a : A} (H : a = 1) : a⁻¹ = 1 :=
iff.mpr (inv_eq_one_iff_eq_one a) H
theorem eq_one_of_inv_eq_one (a : A) : a⁻¹ = 1 → a = 1 :=
iff.mp !inv_eq_one_iff_eq_one
theorem eq_inv_of_eq_inv {a b : A} (H : a = b⁻¹) : b = a⁻¹ :=
by rewrite [H, inv_inv]
theorem eq_inv_iff_eq_inv (a b : A) : a = b⁻¹ ↔ b = a⁻¹ :=
iff.intro !eq_inv_of_eq_inv !eq_inv_of_eq_inv
theorem eq_inv_of_mul_eq_one {a b : A} (H : a * b = 1) : a = b⁻¹ :=
begin apply eq_inv_of_eq_inv, symmetry, exact inv_eq_of_mul_eq_one H end
theorem mul.right_inv (a : A) : a * a⁻¹ = 1 :=
calc
a * a⁻¹ = (a⁻¹)⁻¹ * a⁻¹ : inv_inv
... = 1 : mul.left_inv
theorem mul_inv_cancel_left (a b : A) : a * (a⁻¹ * b) = b :=
calc
a * (a⁻¹ * b) = a * a⁻¹ * b : by rewrite mul.assoc
... = 1 * b : mul.right_inv
... = b : one_mul
theorem mul_inv_cancel_right (a b : A) : a * b * b⁻¹ = a :=
calc
a * b * b⁻¹ = a * (b * b⁻¹) : mul.assoc
... = a * 1 : mul.right_inv
... = a : mul_one
theorem mul_inv (a b : A) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
inv_eq_of_mul_eq_one
(calc
a * b * (b⁻¹ * a⁻¹) = a * (b * (b⁻¹ * a⁻¹)) : mul.assoc
... = a * a⁻¹ : mul_inv_cancel_left
... = 1 : mul.right_inv)
theorem eq_of_mul_inv_eq_one {a b : A} (H : a * b⁻¹ = 1) : a = b :=
calc
a = a * b⁻¹ * b : by rewrite inv_mul_cancel_right
... = 1 * b : H
... = b : one_mul
theorem eq_mul_inv_of_mul_eq {a b c : A} (H : a * c = b) : a = b * c⁻¹ :=
by rewrite [-H, mul_inv_cancel_right]
theorem eq_inv_mul_of_mul_eq {a b c : A} (H : b * a = c) : a = b⁻¹ * c :=
by rewrite [-H, inv_mul_cancel_left]
theorem inv_mul_eq_of_eq_mul {a b c : A} (H : b = a * c) : a⁻¹ * b = c :=
by rewrite [H, inv_mul_cancel_left]
theorem mul_inv_eq_of_eq_mul {a b c : A} (H : a = c * b) : a * b⁻¹ = c :=
by rewrite [H, mul_inv_cancel_right]
theorem eq_mul_of_mul_inv_eq {a b c : A} (H : a * c⁻¹ = b) : a = b * c :=
!inv_inv ▸ (eq_mul_inv_of_mul_eq H)
theorem eq_mul_of_inv_mul_eq {a b c : A} (H : b⁻¹ * a = c) : a = b * c :=
!inv_inv ▸ (eq_inv_mul_of_mul_eq H)
theorem mul_eq_of_eq_inv_mul {a b c : A} (H : b = a⁻¹ * c) : a * b = c :=
!inv_inv ▸ (inv_mul_eq_of_eq_mul H)
theorem mul_eq_of_eq_mul_inv {a b c : A} (H : a = c * b⁻¹) : a * b = c :=
!inv_inv ▸ (mul_inv_eq_of_eq_mul H)
theorem mul_eq_iff_eq_inv_mul (a b c : A) : a * b = c ↔ b = a⁻¹ * c :=
iff.intro eq_inv_mul_of_mul_eq mul_eq_of_eq_inv_mul
theorem mul_eq_iff_eq_mul_inv (a b c : A) : a * b = c ↔ a = c * b⁻¹ :=
iff.intro eq_mul_inv_of_mul_eq mul_eq_of_eq_mul_inv
theorem mul_left_cancel {a b c : A} (H : a * b = a * c) : b = c :=
by rewrite [-inv_mul_cancel_left a b, H, inv_mul_cancel_left]
theorem mul_right_cancel {a b c : A} (H : a * b = c * b) : a = c :=
by rewrite [-mul_inv_cancel_right a b, H, mul_inv_cancel_right]
theorem mul_eq_one_of_mul_eq_one {a b : A} (H : b * a = 1) : a * b = 1 :=
by rewrite [-inv_eq_of_mul_eq_one H, mul.left_inv]
theorem mul_eq_one_iff_mul_eq_one (a b : A) : a * b = 1 ↔ b * a = 1 :=
iff.intro !mul_eq_one_of_mul_eq_one !mul_eq_one_of_mul_eq_one
definition conj_by (g a : A) := g * a * g⁻¹
definition is_conjugate (a b : A) := Σ x, conj_by x b = a
local infixl ` ~ ` := is_conjugate
local infixr ` ∘c `:55 := conj_by
lemma conj_compose (f g a : A) : f ∘c g ∘c a = f*g ∘c a :=
calc f ∘c g ∘c a = f * (g * a * g⁻¹) * f⁻¹ : rfl
... = f * (g * a) * g⁻¹ * f⁻¹ : mul.assoc
... = f * g * a * g⁻¹ * f⁻¹ : mul.assoc
... = f * g * a * (g⁻¹ * f⁻¹) : mul.assoc
... = f * g * a * (f * g)⁻¹ : mul_inv
lemma conj_id (a : A) : 1 ∘c a = a :=
calc 1 * a * 1⁻¹ = a * 1⁻¹ : one_mul
... = a * 1 : one_inv
... = a : mul_one
lemma conj_one (g : A) : g ∘c 1 = 1 :=
calc g * 1 * g⁻¹ = g * g⁻¹ : mul_one
... = 1 : mul.right_inv
lemma conj_inv_cancel (g : A) : Π a, g⁻¹ ∘c g ∘c a = a :=
assume a, calc
g⁻¹ ∘c g ∘c a = g⁻¹*g ∘c a : conj_compose
... = 1 ∘c a : mul.left_inv
... = a : conj_id
lemma conj_inv (g : A) : Π a, (g ∘c a)⁻¹ = g ∘c a⁻¹ :=
take a, calc
(g * a * g⁻¹)⁻¹ = g⁻¹⁻¹ * (g * a)⁻¹ : mul_inv
... = g⁻¹⁻¹ * (a⁻¹ * g⁻¹) : mul_inv
... = g⁻¹⁻¹ * a⁻¹ * g⁻¹ : mul.assoc
... = g * a⁻¹ * g⁻¹ : inv_inv
lemma is_conj.refl (a : A) : a ~ a := sigma.mk 1 (conj_id a)
lemma is_conj.symm (a b : A) : a ~ b → b ~ a :=
assume Pab, obtain x (Pconj : x ∘c b = a), from Pab,
have Pxinv : x⁻¹ ∘c x ∘c b = x⁻¹ ∘c a, begin congruence, assumption end,
sigma.mk x⁻¹ (inverse (conj_inv_cancel x b ▸ Pxinv))
lemma is_conj.trans (a b c : A) : a ~ b → b ~ c → a ~ c :=
assume Pab, assume Pbc,
obtain x (Px : x ∘c b = a), from Pab,
obtain y (Py : y ∘c c = b), from Pbc,
sigma.mk (x*y) (calc
x*y ∘c c = x ∘c y ∘c c : conj_compose
... = x ∘c b : Py
... = a : Px)
definition inf_group.to_left_cancel_inf_semigroup [trans_instance] : left_cancel_inf_semigroup A :=
⦃ left_cancel_inf_semigroup, s,
mul_left_cancel := @mul_left_cancel A s ⦄
definition inf_group.to_right_cancel_inf_semigroup [trans_instance] : right_cancel_inf_semigroup A :=
⦃ right_cancel_inf_semigroup, s,
mul_right_cancel := @mul_right_cancel A s ⦄
end inf_group
structure ab_inf_group [class] (A : Type) extends inf_group A, comm_inf_monoid A
/- additive inf_group -/
definition add_inf_group [class] : Type → Type := inf_group
definition add_inf_semigroup_of_add_inf_group [reducible] [trans_instance] (A : Type)
[H : add_inf_group A] : add_inf_monoid A :=
@inf_group.to_inf_monoid A H
definition has_neg_of_add_inf_group [reducible] [trans_instance] (A : Type)
[H : add_inf_group A] : has_neg A :=
has_neg.mk (@inf_group.inv A H)
section add_inf_group
variables [s : add_inf_group A]
include s
theorem add.left_inv (a : A) : -a + a = 0 := @inf_group.mul_left_inv A s a
theorem neg_add_cancel_left (a b : A) : -a + (a + b) = b :=
by rewrite [-add.assoc, add.left_inv, zero_add]
theorem neg_add_cancel_right (a b : A) : a + -b + b = a :=
by rewrite [add.assoc, add.left_inv, add_zero]
theorem neg_eq_of_add_eq_zero {a b : A} (H : a + b = 0) : -a = b :=
by rewrite [-add_zero (-a), -H, neg_add_cancel_left]
theorem neg_zero : -0 = (0 : A) := neg_eq_of_add_eq_zero (zero_add 0)
theorem neg_neg (a : A) : -(-a) = a := neg_eq_of_add_eq_zero (add.left_inv a)
theorem eq_neg_of_add_eq_zero {a b : A} (H : a + b = 0) : a = -b :=
by rewrite [-neg_eq_of_add_eq_zero H, neg_neg]
theorem neg.inj {a b : A} (H : -a = -b) : a = b :=
calc
a = -(-a) : neg_neg
... = b : neg_eq_of_add_eq_zero (H⁻¹ ▸ (add.left_inv _))
theorem neg_eq_neg_iff_eq (a b : A) : -a = -b ↔ a = b :=
iff.intro (assume H, neg.inj H) (assume H, ap _ H)
theorem eq_of_neg_eq_neg {a b : A} : -a = -b → a = b :=
iff.mp !neg_eq_neg_iff_eq
theorem neg_eq_zero_iff_eq_zero (a : A) : -a = 0 ↔ a = 0 :=
neg_zero ▸ !neg_eq_neg_iff_eq
theorem eq_zero_of_neg_eq_zero {a : A} : -a = 0 → a = 0 :=
iff.mp !neg_eq_zero_iff_eq_zero
theorem eq_neg_of_eq_neg {a b : A} (H : a = -b) : b = -a :=
H⁻¹ ▸ (neg_neg b)⁻¹
theorem eq_neg_iff_eq_neg (a b : A) : a = -b ↔ b = -a :=
iff.intro !eq_neg_of_eq_neg !eq_neg_of_eq_neg
theorem add.right_inv (a : A) : a + -a = 0 :=
calc
a + -a = -(-a) + -a : neg_neg
... = 0 : add.left_inv
theorem add_neg_cancel_left (a b : A) : a + (-a + b) = b :=
by rewrite [-add.assoc, add.right_inv, zero_add]
theorem add_neg_cancel_right (a b : A) : a + b + -b = a :=
by rewrite [add.assoc, add.right_inv, add_zero]
theorem neg_add_rev (a b : A) : -(a + b) = -b + -a :=
neg_eq_of_add_eq_zero
begin
rewrite [add.assoc, add_neg_cancel_left, add.right_inv]
end
-- TODO: delete these in favor of sub rules?
theorem eq_add_neg_of_add_eq {a b c : A} (H : a + c = b) : a = b + -c :=
H ▸ !add_neg_cancel_right⁻¹
theorem eq_neg_add_of_add_eq {a b c : A} (H : b + a = c) : a = -b + c :=
H ▸ !neg_add_cancel_left⁻¹
theorem neg_add_eq_of_eq_add {a b c : A} (H : b = a + c) : -a + b = c :=
H⁻¹ ▸ !neg_add_cancel_left
theorem add_neg_eq_of_eq_add {a b c : A} (H : a = c + b) : a + -b = c :=
H⁻¹ ▸ !add_neg_cancel_right
theorem eq_add_of_add_neg_eq {a b c : A} (H : a + -c = b) : a = b + c :=
!neg_neg ▸ (eq_add_neg_of_add_eq H)
theorem eq_add_of_neg_add_eq {a b c : A} (H : -b + a = c) : a = b + c :=
!neg_neg ▸ (eq_neg_add_of_add_eq H)
theorem add_eq_of_eq_neg_add {a b c : A} (H : b = -a + c) : a + b = c :=
!neg_neg ▸ (neg_add_eq_of_eq_add H)
theorem add_eq_of_eq_add_neg {a b c : A} (H : a = c + -b) : a + b = c :=
!neg_neg ▸ (add_neg_eq_of_eq_add H)
theorem add_eq_iff_eq_neg_add (a b c : A) : a + b = c ↔ b = -a + c :=
iff.intro eq_neg_add_of_add_eq add_eq_of_eq_neg_add
theorem add_eq_iff_eq_add_neg (a b c : A) : a + b = c ↔ a = c + -b :=
iff.intro eq_add_neg_of_add_eq add_eq_of_eq_add_neg
theorem add_left_cancel {a b c : A} (H : a + b = a + c) : b = c :=
calc b = -a + (a + b) : !neg_add_cancel_left⁻¹
... = -a + (a + c) : H
... = c : neg_add_cancel_left
theorem add_right_cancel {a b c : A} (H : a + b = c + b) : a = c :=
calc a = (a + b) + -b : !add_neg_cancel_right⁻¹
... = (c + b) + -b : H
... = c : add_neg_cancel_right
definition add_inf_group.to_add_left_cancel_inf_semigroup [reducible] [trans_instance] :
add_left_cancel_inf_semigroup A :=
@inf_group.to_left_cancel_inf_semigroup A s
definition add_inf_group.to_add_right_cancel_inf_semigroup [reducible] [trans_instance] :
add_right_cancel_inf_semigroup A :=
@inf_group.to_right_cancel_inf_semigroup A s
theorem add_neg_eq_neg_add_rev {a b : A} : a + -b = -(b + -a) :=
by rewrite [neg_add_rev, neg_neg]
/- sub -/
-- TODO: derive corresponding facts for div in a field
protected definition algebra.sub [reducible] (a b : A) : A := a + -b
definition add_inf_group_has_sub [instance] : has_sub A :=
has_sub.mk algebra.sub
theorem sub_eq_add_neg (a b : A) : a - b = a + -b := rfl
theorem sub_self (a : A) : a - a = 0 := !add.right_inv
theorem sub_add_cancel (a b : A) : a - b + b = a := !neg_add_cancel_right
theorem add_sub_cancel (a b : A) : a + b - b = a := !add_neg_cancel_right
theorem eq_of_sub_eq_zero {a b : A} (H : a - b = 0) : a = b :=
calc
a = (a - b) + b : !sub_add_cancel⁻¹
... = 0 + b : H
... = b : zero_add
theorem eq_iff_sub_eq_zero (a b : A) : a = b ↔ a - b = 0 :=
iff.intro (assume H, H ▸ !sub_self) (assume H, eq_of_sub_eq_zero H)
theorem zero_sub (a : A) : 0 - a = -a := !zero_add
theorem sub_zero (a : A) : a - 0 = a :=
by rewrite [sub_eq_add_neg, neg_zero, add_zero]
theorem sub_neg_eq_add (a b : A) : a - (-b) = a + b :=
by change a + -(-b) = a + b; rewrite neg_neg
theorem neg_sub (a b : A) : -(a - b) = b - a :=
neg_eq_of_add_eq_zero
(calc
a - b + (b - a) = a - b + b - a : by krewrite -add.assoc
... = a - a : sub_add_cancel
... = 0 : sub_self)
theorem add_sub (a b c : A) : a + (b - c) = a + b - c := !add.assoc⁻¹
theorem sub_add_eq_sub_sub_swap (a b c : A) : a - (b + c) = a - c - b :=
calc
a - (b + c) = a + (-c - b) : by rewrite [sub_eq_add_neg, neg_add_rev]
... = a - c - b : by krewrite -add.assoc
theorem sub_eq_iff_eq_add (a b c : A) : a - b = c ↔ a = c + b :=
iff.intro (assume H, eq_add_of_add_neg_eq H) (assume H, add_neg_eq_of_eq_add H)
theorem eq_sub_iff_add_eq (a b c : A) : a = b - c ↔ a + c = b :=
iff.intro (assume H, add_eq_of_eq_add_neg H) (assume H, eq_add_neg_of_add_eq H)
theorem eq_iff_eq_of_sub_eq_sub {a b c d : A} (H : a - b = c - d) : a = b ↔ c = d :=
calc
a = b ↔ a - b = 0 : eq_iff_sub_eq_zero
... = (c - d = 0) : H
... ↔ c = d : iff.symm (eq_iff_sub_eq_zero c d)
theorem eq_sub_of_add_eq {a b c : A} (H : a + c = b) : a = b - c :=
!eq_add_neg_of_add_eq H
theorem sub_eq_of_eq_add {a b c : A} (H : a = c + b) : a - b = c :=
!add_neg_eq_of_eq_add H
theorem eq_add_of_sub_eq {a b c : A} (H : a - c = b) : a = b + c :=
eq_add_of_add_neg_eq H
theorem add_eq_of_eq_sub {a b c : A} (H : a = c - b) : a + b = c :=
add_eq_of_eq_add_neg H
end add_inf_group
definition add_ab_inf_group [class] : Type → Type := ab_inf_group
definition add_inf_group_of_add_ab_inf_group [reducible] [trans_instance] (A : Type)
[H : add_ab_inf_group A] : add_inf_group A :=
@ab_inf_group.to_inf_group A H
definition add_comm_inf_monoid_of_add_ab_inf_group [reducible] [trans_instance] (A : Type)
[H : add_ab_inf_group A] : add_comm_inf_monoid A :=
@ab_inf_group.to_comm_inf_monoid A H
section add_ab_inf_group
variable [s : add_ab_inf_group A]
include s
theorem sub_add_eq_sub_sub (a b c : A) : a - (b + c) = a - b - c :=
!add.comm ▸ !sub_add_eq_sub_sub_swap
theorem neg_add_eq_sub (a b : A) : -a + b = b - a := !add.comm
theorem neg_add (a b : A) : -(a + b) = -a + -b := add.comm (-b) (-a) ▸ neg_add_rev a b
theorem sub_add_eq_add_sub (a b c : A) : a - b + c = a + c - b := !add.right_comm
theorem sub_sub (a b c : A) : a - b - c = a - (b + c) :=
by rewrite [▸ a + -b + -c = _, add.assoc, -neg_add]
theorem add_sub_add_left_eq_sub (a b c : A) : (c + a) - (c + b) = a - b :=
by rewrite [sub_add_eq_sub_sub, (add.comm c a), add_sub_cancel]
theorem eq_sub_of_add_eq' {a b c : A} (H : c + a = b) : a = b - c :=
!eq_sub_of_add_eq (!add.comm ▸ H)
theorem sub_eq_of_eq_add' {a b c : A} (H : a = b + c) : a - b = c :=
!sub_eq_of_eq_add (!add.comm ▸ H)
theorem eq_add_of_sub_eq' {a b c : A} (H : a - b = c) : a = b + c :=
!add.comm ▸ eq_add_of_sub_eq H
theorem add_eq_of_eq_sub' {a b c : A} (H : b = c - a) : a + b = c :=
!add.comm ▸ add_eq_of_eq_sub H
theorem sub_sub_self (a b : A) : a - (a - b) = b :=
by rewrite [sub_eq_add_neg, neg_sub, add.comm, sub_add_cancel]
theorem add_sub_comm (a b c d : A) : a + b - (c + d) = (a - c) + (b - d) :=
by rewrite [sub_add_eq_sub_sub, -sub_add_eq_add_sub a c b, add_sub]
theorem sub_eq_sub_add_sub (a b c : A) : a - b = c - b + (a - c) :=
by rewrite [add_sub, sub_add_cancel] ⬝ !add.comm
theorem neg_neg_sub_neg (a b : A) : - (-a - -b) = a - b :=
by rewrite [neg_sub, sub_neg_eq_add, neg_add_eq_sub]
end add_ab_inf_group
definition inf_group_of_add_inf_group (A : Type) [G : add_inf_group A] : inf_group A :=
⦃inf_group,
mul := has_add.add,
mul_assoc := add.assoc,
one := !has_zero.zero,
one_mul := zero_add,
mul_one := add_zero,
inv := has_neg.neg,
mul_left_inv := add.left_inv ⦄
namespace norm_num
definition add1 [s : has_add A] [s' : has_one A] (a : A) : A := add a one
theorem add_comm_four [s : add_comm_inf_semigroup A] (a b : A) : a + a + (b + b) = (a + b) + (a + b) :=
by rewrite [-add.assoc at {1}, add.comm, {a + b}add.comm at {1}, *add.assoc]
theorem add_comm_middle [s : add_comm_inf_semigroup A] (a b c : A) : a + b + c = a + c + b :=
by rewrite [add.assoc, add.comm b, -add.assoc]
theorem bit0_add_bit0 [s : add_comm_inf_semigroup A] (a b : A) : bit0 a + bit0 b = bit0 (a + b) :=
!add_comm_four
theorem bit0_add_bit0_helper [s : add_comm_inf_semigroup A] (a b t : A) (H : a + b = t) :
bit0 a + bit0 b = bit0 t :=
by rewrite -H; apply bit0_add_bit0
theorem bit1_add_bit0 [s : add_comm_inf_semigroup A] [s' : has_one A] (a b : A) :
bit1 a + bit0 b = bit1 (a + b) :=
begin
rewrite [↑bit0, ↑bit1, add_comm_middle], congruence, apply add_comm_four
end
theorem bit1_add_bit0_helper [s : add_comm_inf_semigroup A] [s' : has_one A] (a b t : A)
(H : a + b = t) : bit1 a + bit0 b = bit1 t :=
by rewrite -H; apply bit1_add_bit0
theorem bit0_add_bit1 [s : add_comm_inf_semigroup A] [s' : has_one A] (a b : A) :
bit0 a + bit1 b = bit1 (a + b) :=
by rewrite [{bit0 a + bit1 b}add.comm,{a + b}add.comm]; exact bit1_add_bit0 b a
theorem bit0_add_bit1_helper [s : add_comm_inf_semigroup A] [s' : has_one A] (a b t : A)
(H : a + b = t) : bit0 a + bit1 b = bit1 t :=
by rewrite -H; apply bit0_add_bit1
theorem bit1_add_bit1 [s : add_comm_inf_semigroup A] [s' : has_one A] (a b : A) :
bit1 a + bit1 b = bit0 (add1 (a + b)) :=
begin
rewrite ↑[bit0, bit1, add1, add.assoc],
rewrite [*add.assoc, {_ + (b + 1)}add.comm, {_ + (b + 1 + _)}add.comm,
{_ + (b + 1 + _ + _)}add.comm, *add.assoc, {1 + a}add.comm, -{b + (a + 1)}add.assoc,
{b + a}add.comm, *add.assoc]
end
theorem bit1_add_bit1_helper [s : add_comm_inf_semigroup A] [s' : has_one A] (a b t s: A)
(H : (a + b) = t) (H2 : add1 t = s) : bit1 a + bit1 b = bit0 s :=
begin rewrite [-H2, -H], apply bit1_add_bit1 end
theorem bin_add_zero [s : add_inf_monoid A] (a : A) : a + zero = a := !add_zero
theorem bin_zero_add [s : add_inf_monoid A] (a : A) : zero + a = a := !zero_add
theorem one_add_bit0 [s : add_comm_inf_semigroup A] [s' : has_one A] (a : A) : one + bit0 a = bit1 a :=
begin rewrite ↑[bit0, bit1], rewrite add.comm end
theorem bit0_add_one [s : has_add A] [s' : has_one A] (a : A) : bit0 a + one = bit1 a :=
rfl
theorem bit1_add_one [s : has_add A] [s' : has_one A] (a : A) : bit1 a + one = add1 (bit1 a) :=
rfl
theorem bit1_add_one_helper [s : has_add A] [s' : has_one A] (a t : A) (H : add1 (bit1 a) = t) :
bit1 a + one = t :=
by rewrite -H
theorem one_add_bit1 [s : add_comm_inf_semigroup A] [s' : has_one A] (a : A) :
one + bit1 a = add1 (bit1 a) := !add.comm
theorem one_add_bit1_helper [s : add_comm_inf_semigroup A] [s' : has_one A] (a t : A)
(H : add1 (bit1 a) = t) : one + bit1 a = t :=
by rewrite -H; apply one_add_bit1
theorem add1_bit0 [s : has_add A] [s' : has_one A] (a : A) : add1 (bit0 a) = bit1 a :=
rfl
theorem add1_bit1 [s : add_comm_inf_semigroup A] [s' : has_one A] (a : A) :
add1 (bit1 a) = bit0 (add1 a) :=
begin
rewrite ↑[add1, bit1, bit0],
rewrite [add.assoc, add_comm_four]
end
theorem add1_bit1_helper [s : add_comm_inf_semigroup A] [s' : has_one A] (a t : A) (H : add1 a = t) :
add1 (bit1 a) = bit0 t :=
by rewrite -H; apply add1_bit1
theorem add1_one [s : has_add A] [s' : has_one A] : add1 (one : A) = bit0 one :=
rfl
theorem add1_zero [s : add_inf_monoid A] [s' : has_one A] : add1 (zero : A) = one :=
begin
rewrite [↑add1, zero_add]
end
theorem one_add_one [s : has_add A] [s' : has_one A] : (one : A) + one = bit0 one :=
rfl
theorem subst_into_sum [s : has_add A] (l r tl tr t : A) (prl : l = tl) (prr : r = tr)
(prt : tl + tr = t) : l + r = t :=
by rewrite [prl, prr, prt]
theorem neg_zero_helper [s : add_inf_group A] (a : A) (H : a = 0) : - a = 0 :=
by rewrite [H, neg_zero]
end norm_num
end algebra
open algebra
attribute [simp]
zero_add add_zero one_mul mul_one
at simplifier.unit
attribute [simp]
neg_neg sub_eq_add_neg
at simplifier.neg
attribute [simp]
add.assoc add.comm add.left_comm
mul.left_comm mul.comm mul.assoc
at simplifier.ac
|
083ba078fd3a68b17cb184c7d987626efb254e0a | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/data/finset/basic.lean | ca66bb09004a4de6d73674ba1769c9b4c8961ee6 | [
"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 | 130,608 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro
-/
import data.multiset.finset_ops
import tactic.apply
import tactic.monotonicity
import tactic.nth_rewrite
/-!
# Finite sets
Terms of type `finset α` are one way of talking about finite subsets of `α` in mathlib.
Below, `finset α` is defined as a structure with 2 fields:
1. `val` is a `multiset α` of elements;
2. `nodup` is a proof that `val` has no duplicates.
Finsets in Lean are constructive in that they have an underlying `list` that enumerates their
elements. In particular, any function that uses the data of the underlying list cannot depend on its
ordering. This is handled on the `multiset` level by multiset API, so in most cases one needn't
worry about it explicitly.
Finsets give a basic foundation for defining finite sums and products over types:
1. `∑ i in (s : finset α), f i`;
2. `∏ i in (s : finset α), f i`.
Lean refers to these operations as `big_operator`s.
More information can be found in `algebra.big_operators.basic`.
Finsets are directly used to define fintypes in Lean.
A `fintype α` instance for a type `α` consists of
a universal `finset α` containing every term of `α`, called `univ`. See `data.fintype.basic`.
There is also `univ'`, the noncomputable partner to `univ`,
which is defined to be `α` as a finset if `α` is finite,
and the empty finset otherwise. See `data.fintype.basic`.
## Main declarations
### Main definitions
* `finset`: Defines a type for the finite subsets of `α`.
Constructing a `finset` requires two pieces of data: `val`, a `multiset α` of elements,
and `nodup`, a proof that `val` has no duplicates.
* `finset.has_mem`: Defines membership `a ∈ (s : finset α)`.
* `finset.has_coe`: Provides a coercion `s : finset α` to `s : set α`.
* `finset.has_coe_to_sort`: Coerce `s : finset α` to the type of all `x ∈ s`.
* `finset.induction_on`: Induction on finsets. To prove a proposition about an arbitrary `finset α`,
it suffices to prove it for the empty finset, and to show that if it holds for some `finset α`,
then it holds for the finset obtained by inserting a new element.
* `finset.choose`: Given a proof `h` of existence and uniqueness of a certain element
satisfying a predicate, `choose s h` returns the element of `s` satisfying that predicate.
* `finset.card`: `card s : ℕ` returns the cardinalilty of `s : finset α`.
The API for `card`'s interaction with operations on finsets is extensive.
TODO: The noncomputable sister `fincard` is about to be added into mathlib.
### Finset constructions
* `singleton`: Denoted by `{a}`; the finset consisting of one element.
* `finset.empty`: Denoted by `∅`. The finset associated to any type consisting of no elements.
* `finset.range`: For any `n : ℕ`, `range n` is equal to `{0, 1, ... , n - 1} ⊆ ℕ`.
This convention is consistent with other languages and normalizes `card (range n) = n`.
Beware, `n` is not in `range n`.
* `finset.diag`: Given `s`, `diag s` is the set of pairs `(a, a)` with `a ∈ s`. See also
`finset.off_diag`: Given a finite set `s`, the off-diagonal,
`s.off_diag` is the set of pairs `(a, b)` with `a ≠ b` for `a, b ∈ s`.
* `finset.attach`: Given `s : finset α`, `attach s` forms a finset of elements of the subtype
`{a // a ∈ s}`; in other words, it attaches elements to a proof of membership in the set.
### Finsets from functions
* `finset.image`: Given a function `f : α → β`, `s.image f` is the image finset in `β`.
* `finset.map`: Given an embedding `f : α ↪ β`, `s.map f` is the image finset in `β`.
* `finset.filter`: Given a predicate `p : α → Prop`, `s.filter p` is
the finset consisting of those elements in `s` satisfying the predicate `p`.
### The lattice structure on subsets of finsets
There is a natural lattice structure on the subsets of a set.
In Lean, we use lattice notation to talk about things involving unions and intersections. See
`order.lattice`. For the lattice structure on finsets, `⊥` is called `bot` with `⊥ = ∅` and `⊤` is
called `top` with `⊤ = univ`.
* `finset.subset`: Lots of API about lattices, otherwise behaves exactly as one would expect.
* `finset.union`: Defines `s ∪ t` (or `s ⊔ t`) as the union of `s` and `t`.
See `finset.bUnion` for finite unions.
* `finset.inter`: Defines `s ∩ t` (or `s ⊓ t`) as the intersection of `s` and `t`.
TODO: `finset.bInter` for finite intersections.
* `finset.disj_union`: Given a hypothesis `h` which states that finsets `s` and `t` are disjoint,
`s.disj_union t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`; this does
not require decidable equality on the type `α`.
### Operations on two or more finsets
* `finset.insert` and `finset.cons`: For any `a : α`, `insert s a` returns `s ∪ {a}`. `cons s a h`
returns the same except that it requires a hypothesis stating that `a` is not already in `s`.
This does not require decidable equality on the type `α`.
* `finset.union`: see "The lattice structure on subsets of finsets"
* `finset.inter`: see "The lattice structure on subsets of finsets"
* `finset.erase`: For any `a : α`, `erase s a` returns `s` with the element `a` removed.
* `finset.sdiff`: Defines the set difference `s \ t` for finsets `s` and `t`.
* `finset.prod`: Given finsets of `α` and `β`, defines finsets of `α × β`.
For arbitrary dependent products, see `data.finset.pi`.
* `finset.sigma`: Given finsets of `α` and `β`, defines finsets of the dependent sum type `Σ α, β`
* `finset.bUnion`: Finite unions of finsets; given an indexing function `f : α → finset β` and a
`s : finset α`, `s.bUnion f` is the union of all finsets of the form `f a` for `a ∈ s`.
* `finset.bInter`: TODO: Implemement finite intersections.
### Maps constructed using finsets
* `finset.piecewise`: Given two functions `f`, `g`, `s.piecewise f g` is a function which is equal
to `f` on `s` and `g` on the complement.
### Predicates on finsets
* `disjoint`: defined via the lattice structure on finsets; two sets are disjoint if their
intersection is empty.
* `finset.nonempty`: A finset is nonempty if it has elements.
This is equivalent to saying `s ≠ ∅`. TODO: Decide on the simp normal form.
### Equivalences between finsets
* The `data.equiv` files describe a general type of equivalence, so look in there for any lemmas.
There is some API for rewriting sums and products from `s` to `t` given that `s ≃ t`.
TODO: examples
## Tags
finite sets, finset
-/
open multiset subtype nat function
variables {α : Type*} {β : Type*} {γ : Type*}
/-- `finset α` is the type of finite sets of elements of `α`. It is implemented
as a multiset (a list up to permutation) which has no duplicate elements. -/
structure finset (α : Type*) :=
(val : multiset α)
(nodup : nodup val)
namespace finset
theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t
| ⟨s, _⟩ ⟨t, _⟩ rfl := rfl
@[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t :=
⟨eq_of_veq, congr_arg _⟩
@[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 :=
erase_dup_eq_self.2 s.2
instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α)
| s₁ s₂ := decidable_of_iff _ val_inj
/-! ### membership -/
instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩
theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl
@[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl
instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) :=
multiset.decidable_mem _ _
/-! ### set coercion -/
/-- Convert a finset to a set in the natural way. -/
instance : has_coe_t (finset α) (set α) := ⟨λ s, {x | x ∈ s}⟩
@[simp, norm_cast] lemma mem_coe {a : α} {s : finset α} : a ∈ (s : set α) ↔ a ∈ s := iff.rfl
@[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = s := rfl
@[simp] lemma coe_mem {s : finset α} (x : (s : set α)) : ↑x ∈ s := x.2
@[simp] lemma mk_coe {s : finset α} (x : (s : set α)) {h} :
(⟨x, h⟩ : (s : set α)) = x :=
subtype.coe_eta _ _
instance decidable_mem' [decidable_eq α] (a : α) (s : finset α) :
decidable (a ∈ (s : set α)) := s.decidable_mem _
/-! ### extensionality -/
theorem ext_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ :=
val_inj.symm.trans $ nodup_ext s₁.2 s₂.2
@[ext]
theorem ext {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ :=
ext_iff.2
@[simp, norm_cast] theorem coe_inj {s₁ s₂ : finset α} : (s₁ : set α) = s₂ ↔ s₁ = s₂ :=
set.ext_iff.trans ext_iff.symm
lemma coe_injective {α} : injective (coe : finset α → set α) :=
λ s t, coe_inj.1
/-! ### type coercion -/
/-- Coercion from a finset to the corresponding subtype. -/
instance {α : Type*} : has_coe_to_sort (finset α) := ⟨_, λ s, {x // x ∈ s}⟩
instance pi_finset_coe.can_lift (ι : Type*) (α : Π i : ι, Type*) [ne : Π i, nonempty (α i)]
(s : finset ι) :
can_lift (Π i : s, α i) (Π i, α i) :=
{ coe := λ f i, f i,
.. pi_subtype.can_lift ι α (∈ s) }
instance pi_finset_coe.can_lift' (ι α : Type*) [ne : nonempty α] (s : finset ι) :
can_lift (s → α) (ι → α) :=
pi_finset_coe.can_lift ι (λ _, α) s
instance finset_coe.can_lift (s : finset α) : can_lift α s :=
{ coe := coe,
cond := λ a, a ∈ s,
prf := λ a ha, ⟨⟨a, ha⟩, rfl⟩ }
@[simp, norm_cast] lemma coe_sort_coe (s : finset α) :
((s : set α) : Sort*) = s := rfl
/-! ### subset -/
instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩
theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl
@[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _
theorem subset_of_eq {s t : finset α} (h : s = t) : s ⊆ t := h ▸ subset.refl _
theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans
theorem superset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊇ s₂ → s₂ ⊇ s₃ → s₁ ⊇ s₃ :=
λ h' h, subset.trans h h'
-- TODO: these should be global attributes, but this will require fixing other files
local attribute [trans] subset.trans superset.trans
theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset
theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ :=
ext $ λ a, ⟨@H₁ a, @H₂ a⟩
theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl
@[simp, norm_cast] theorem coe_subset {s₁ s₂ : finset α} :
(s₁ : set α) ⊆ s₂ ↔ s₁ ⊆ s₂ := iff.rfl
@[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2
instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩
instance : partial_order (finset α) :=
{ le := (⊆),
lt := (⊂),
le_refl := subset.refl,
le_trans := @subset.trans _,
le_antisymm := @subset.antisymm _ }
/-- Coercion to `set α` as an `order_embedding`. -/
def coe_emb : finset α ↪o set α := ⟨⟨coe, coe_injective⟩, λ s t, coe_subset⟩
@[simp] lemma coe_coe_emb : ⇑(coe_emb : finset α ↪o set α) = coe := rfl
theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ :=
le_antisymm_iff
theorem not_subset (s t : finset α) : ¬(s ⊆ t) ↔ ∃ x ∈ s, ¬(x ∈ t) :=
by simp only [←finset.coe_subset, set.not_subset, exists_prop, finset.mem_coe]
@[simp] theorem le_eq_subset : ((≤) : finset α → finset α → Prop) = (⊆) := rfl
@[simp] theorem lt_eq_subset : ((<) : finset α → finset α → Prop) = (⊂) := rfl
theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl
theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl
@[simp, norm_cast] lemma coe_ssubset {s₁ s₂ : finset α} : (s₁ : set α) ⊂ s₂ ↔ s₁ ⊂ s₂ :=
show (s₁ : set α) ⊂ s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁,
by simp only [set.ssubset_def, finset.coe_subset]
@[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ :=
and_congr val_le_iff $ not_congr val_le_iff
lemma ssubset_iff_subset_ne {s t : finset α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t :=
@lt_iff_le_and_ne _ _ s t
theorem ssubset_iff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ⊂ s₂ ↔ ∃ x ∈ s₂, x ∉ s₁ :=
set.ssubset_iff_of_subset h
lemma ssubset_of_ssubset_of_subset {s₁ s₂ s₃ : finset α} (hs₁s₂ : s₁ ⊂ s₂) (hs₂s₃ : s₂ ⊆ s₃) :
s₁ ⊂ s₃ :=
set.ssubset_of_ssubset_of_subset hs₁s₂ hs₂s₃
lemma ssubset_of_subset_of_ssubset {s₁ s₂ s₃ : finset α} (hs₁s₂ : s₁ ⊆ s₂) (hs₂s₃ : s₂ ⊂ s₃) :
s₁ ⊂ s₃ :=
set.ssubset_of_subset_of_ssubset hs₁s₂ hs₂s₃
lemma exists_of_ssubset {s₁ s₂ : finset α} (h : s₁ ⊂ s₂) :
∃ x ∈ s₂, x ∉ s₁ :=
set.exists_of_ssubset h
/-! ### Nonempty -/
/-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used
in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks
to the dot notation. -/
protected def nonempty (s : finset α) : Prop := ∃ x:α, x ∈ s
@[simp, norm_cast] lemma coe_nonempty {s : finset α} : (s:set α).nonempty ↔ s.nonempty := iff.rfl
@[simp] lemma nonempty_coe_sort (s : finset α) : nonempty ↥s ↔ s.nonempty := nonempty_subtype
alias coe_nonempty ↔ _ finset.nonempty.to_set
lemma nonempty.bex {s : finset α} (h : s.nonempty) : ∃ x:α, x ∈ s := h
lemma nonempty.mono {s t : finset α} (hst : s ⊆ t) (hs : s.nonempty) : t.nonempty :=
set.nonempty.mono hst hs
lemma nonempty.forall_const {s : finset α} (h : s.nonempty) {p : Prop} : (∀ x ∈ s, p) ↔ p :=
let ⟨x, hx⟩ := h in ⟨λ h, h x hx, λ h x hx, h⟩
/-! ### empty -/
/-- The empty finset -/
protected def empty : finset α := ⟨0, nodup_zero⟩
instance : has_emptyc (finset α) := ⟨finset.empty⟩
instance inhabited_finset : inhabited (finset α) := ⟨∅⟩
@[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl
@[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id
@[simp] theorem not_nonempty_empty : ¬(∅ : finset α).nonempty :=
λ ⟨x, hx⟩, not_mem_empty x hx
@[simp] theorem mk_zero : (⟨0, nodup_zero⟩ : finset α) = ∅ := rfl
theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ :=
λ e, not_mem_empty a $ e ▸ h
theorem nonempty.ne_empty {s : finset α} (h : s.nonempty) : s ≠ ∅ :=
exists.elim h $ λ a, ne_empty_of_mem
@[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _
theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ :=
eq_of_veq (eq_zero_of_forall_not_mem H)
lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s :=
⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩
@[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅
theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero
@[simp] lemma not_ssubset_empty (s : finset α) : ¬s ⊂ ∅ :=
λ h, let ⟨x, he, hs⟩ := exists_of_ssubset h in he
theorem nonempty_of_ne_empty {s : finset α} (h : s ≠ ∅) : s.nonempty :=
exists_mem_of_ne_zero (mt val_eq_zero.1 h)
theorem nonempty_iff_ne_empty {s : finset α} : s.nonempty ↔ s ≠ ∅ :=
⟨nonempty.ne_empty, nonempty_of_ne_empty⟩
@[simp] theorem not_nonempty_iff_eq_empty {s : finset α} : ¬s.nonempty ↔ s = ∅ :=
by { rw nonempty_iff_ne_empty, exact not_not, }
theorem eq_empty_or_nonempty (s : finset α) : s = ∅ ∨ s.nonempty :=
classical.by_cases or.inl (λ h, or.inr (nonempty_of_ne_empty h))
@[simp, norm_cast] lemma coe_empty : ((∅ : finset α) : set α) = ∅ := rfl
@[simp, norm_cast] lemma coe_eq_empty {s : finset α} :
(s : set α) = ∅ ↔ s = ∅ :=
by rw [← coe_empty, coe_inj]
/-- A `finset` for an empty type is empty. -/
lemma eq_empty_of_is_empty [is_empty α] (s : finset α) : s = ∅ :=
finset.eq_empty_of_forall_not_mem is_empty_elim
/-! ### singleton -/
/--
`{a} : finset a` is the set `{a}` containing `a` and nothing else.
This differs from `insert a ∅` in that it does not require a `decidable_eq` instance for `α`.
-/
instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩
@[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = {a} := rfl
@[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton
theorem not_mem_singleton {a b : α} : a ∉ ({b} : finset α) ↔ a ≠ b := not_congr mem_singleton
theorem mem_singleton_self (a : α) : a ∈ ({a} : finset α) := or.inl rfl
theorem singleton_inj {a b : α} : ({a} : finset α) = {b} ↔ a = b :=
⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩
@[simp] theorem singleton_nonempty (a : α) : ({a} : finset α).nonempty := ⟨a, mem_singleton_self a⟩
@[simp] theorem singleton_ne_empty (a : α) : ({a} : finset α) ≠ ∅ := (singleton_nonempty a).ne_empty
@[simp, norm_cast] lemma coe_singleton (a : α) : (({a} : finset α) : set α) = {a} :=
by { ext, simp }
@[simp, norm_cast] lemma coe_eq_singleton {α : Type*} {s : finset α} {a : α} :
(s : set α) = {a} ↔ s = {a} :=
by rw [←finset.coe_singleton, finset.coe_inj]
lemma eq_singleton_iff_unique_mem {s : finset α} {a : α} :
s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a :=
begin
split; intro t,
rw t,
refine ⟨finset.mem_singleton_self _, λ _, finset.mem_singleton.1⟩,
ext, rw finset.mem_singleton,
refine ⟨t.right _, λ r, r.symm ▸ t.left⟩
end
lemma eq_singleton_iff_nonempty_unique_mem {s : finset α} {a : α} :
s = {a} ↔ s.nonempty ∧ ∀ x ∈ s, x = a :=
begin
split,
{ intros h, subst h, simp, },
{ rintros ⟨hne, h_uniq⟩, rw eq_singleton_iff_unique_mem, refine ⟨_, h_uniq⟩,
rw ← h_uniq hne.some hne.some_spec, apply hne.some_spec, },
end
lemma singleton_iff_unique_mem (s : finset α) : (∃ a, s = {a}) ↔ ∃! a, a ∈ s :=
by simp only [eq_singleton_iff_unique_mem, exists_unique]
lemma singleton_subset_set_iff {s : set α} {a : α} :
↑({a} : finset α) ⊆ s ↔ a ∈ s :=
by rw [coe_singleton, set.singleton_subset_iff]
@[simp] lemma singleton_subset_iff {s : finset α} {a : α} :
{a} ⊆ s ↔ a ∈ s :=
singleton_subset_set_iff
@[simp] lemma subset_singleton_iff {s : finset α} {a : α} : s ⊆ {a} ↔ s = ∅ ∨ s = {a} :=
begin
split,
{ intro hs,
apply or.imp_right _ s.eq_empty_or_nonempty,
rintro ⟨t, ht⟩,
apply subset.antisymm hs,
rwa [singleton_subset_iff, ←mem_singleton.1 (hs ht)] },
rintro (rfl | rfl),
{ exact empty_subset _ },
exact subset.refl _,
end
@[simp] lemma ssubset_singleton_iff {s : finset α} {a : α} :
s ⊂ {a} ↔ s = ∅ :=
by rw [←coe_ssubset, coe_singleton, set.ssubset_singleton_iff, coe_eq_empty]
lemma eq_empty_of_ssubset_singleton {s : finset α} {x : α} (hs : s ⊂ {x}) : s = ∅ :=
ssubset_singleton_iff.1 hs
/-! ### cons -/
/-- `cons a s h` is the set `{a} ∪ s` containing `a` and the elements of `s`. It is the same as
`insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq α`,
and the union is guaranteed to be disjoint. -/
def cons {α} (a : α) (s : finset α) (h : a ∉ s) : finset α :=
⟨a ::ₘ s.1, multiset.nodup_cons.2 ⟨h, s.2⟩⟩
@[simp] theorem mem_cons {α a s h b} : b ∈ @cons α a s h ↔ b = a ∨ b ∈ s :=
by rcases s with ⟨⟨s⟩⟩; apply list.mem_cons_iff
@[simp] theorem cons_val {a : α} {s : finset α} (h : a ∉ s) : (cons a s h).1 = a ::ₘ s.1 := rfl
@[simp] theorem mk_cons {a : α} {s : multiset α} (h : (a ::ₘ s).nodup) :
(⟨a ::ₘ s, h⟩ : finset α) = cons a ⟨s, (multiset.nodup_cons.1 h).2⟩ (multiset.nodup_cons.1 h).1 :=
rfl
@[simp] theorem nonempty_cons {a : α} {s : finset α} (h : a ∉ s) : (cons a s h).nonempty :=
⟨a, mem_cons.2 (or.inl rfl)⟩
@[simp] lemma nonempty_mk_coe : ∀ {l : list α} {hl}, (⟨↑l, hl⟩ : finset α).nonempty ↔ l ≠ []
| [] hl := by simp
| (a::l) hl := by simp [← multiset.cons_coe]
/-! ### disjoint union -/
/-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`.
It is the same as `s ∪ t`, but it does not require decidable equality on the type. The hypothesis
ensures that the sets are disjoint. -/
def disj_union {α} (s t : finset α) (h : ∀ a ∈ s, a ∉ t) : finset α :=
⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, h⟩⟩
@[simp] theorem mem_disj_union {α s t h a} :
a ∈ @disj_union α s t h ↔ a ∈ s ∨ a ∈ t :=
by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append
/-! ### insert -/
section decidable_eq
variables [decidable_eq α]
/-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/
instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩
theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl
@[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl
theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a ::ₘ s.1) :=
by rw [erase_dup_cons, erase_dup_eq_self]; refl
theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a ::ₘ s.1 :=
by rw [insert_val, ndinsert_of_not_mem h]
@[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert
theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1
theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s :=
mem_ndinsert_of_mem h
theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s :=
(mem_insert.1 h).resolve_left
@[simp] theorem cons_eq_insert {α} [decidable_eq α] (a s h) : @cons α a s h = insert a s :=
ext $ λ a, by simp
@[simp, norm_cast] lemma coe_insert (a : α) (s : finset α) :
↑(insert a s) = (insert a s : set α) :=
set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff]
lemma mem_insert_coe {s : finset α} {x y : α} : x ∈ insert y s ↔ x ∈ insert y (s : set α) :=
by simp
instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩
@[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s :=
eq_of_veq $ ndinsert_of_mem h
@[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = {a} :=
insert_eq_of_mem $ mem_singleton_self _
theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) :=
ext $ λ x, by simp only [mem_insert, or.left_comm]
theorem insert_singleton_comm (a b : α) : ({a, b} : finset α) = {b, a} :=
begin
ext,
simp [or.comm]
end
@[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s :=
ext $ λ x, by simp only [mem_insert, or.assoc.symm, or_self]
@[simp] theorem insert_nonempty (a : α) (s : finset α) : (insert a s).nonempty :=
⟨a, mem_insert_self a s⟩
@[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ :=
(insert_nonempty a s).ne_empty
section
universe u
/-!
The universe annotation is required for the following instance, possibly this is a bug in Lean. See
leanprover.zulipchat.com/#narrow/stream/113488-general/topic/strange.20error.20(universe.20issue.3F)
-/
instance {α : Type u} [decidable_eq α] (i : α) (s : finset α) :
nonempty.{u + 1} ((insert i s : finset α) : set α) :=
(finset.coe_nonempty.mpr (s.insert_nonempty i)).to_subtype
end
lemma ne_insert_of_not_mem (s t : finset α) {a : α} (h : a ∉ s) :
s ≠ insert a t :=
by { contrapose! h, simp [h] }
theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t :=
by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib]
theorem subset_insert (a : α) (s : finset α) : s ⊆ insert a s :=
λ b, mem_insert_of_mem
theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t :=
insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩
lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a ∉ s, insert a s ⊆ t) :=
by exact_mod_cast @set.ssubset_iff_insert α s t
lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s :=
ssubset_iff.mpr ⟨a, h, subset.refl _⟩
@[elab_as_eliminator]
lemma cons_induction {α : Type*} {p : finset α → Prop}
(h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α} (h : a ∉ s), p s → p (cons a s h)) : ∀ s, p s
| ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin
cases nodup_cons.1 nd with m nd',
rw [← (eq_of_veq _ : cons a (finset.mk s _) m = ⟨a ::ₘ s, nd⟩)],
{ exact h₂ (by exact m) (IH nd') },
{ rw [cons_val] }
end) nd
@[elab_as_eliminator]
lemma cons_induction_on {α : Type*} {p : finset α → Prop} (s : finset α)
(h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α} (h : a ∉ s), p s → p (cons a s h)) : p s :=
cons_induction h₁ h₂ s
@[elab_as_eliminator]
protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α]
(h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s :=
cons_induction h₁ $ λ a s ha, (s.cons_eq_insert a ha).symm ▸ h₂ ha
/--
To prove a proposition about an arbitrary `finset α`,
it suffices to prove it for the empty `finset`,
and to show that if it holds for some `finset α`,
then it holds for the `finset` obtained by inserting a new element.
-/
@[elab_as_eliminator]
protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α]
(s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s :=
finset.induction h₁ h₂ s
/--
To prove a proposition about `S : finset α`,
it suffices to prove it for the empty `finset`,
and to show that if it holds for some `finset α ⊆ S`,
then it holds for the `finset` obtained by inserting a new element of `S`.
-/
@[elab_as_eliminator]
theorem induction_on' {α : Type*} {p : finset α → Prop} [decidable_eq α]
(S : finset α) (h₁ : p ∅) (h₂ : ∀ {a s}, a ∈ S → s ⊆ S → a ∉ s → p s → p (insert a s)) : p S :=
@finset.induction_on α (λ T, T ⊆ S → p T) _ S (λ _, h₁) (λ a s has hqs hs,
let ⟨hS, sS⟩ := finset.insert_subset.1 hs in h₂ hS sS has (hqs sS)) (finset.subset.refl S)
/-- Inserting an element to a finite set is equivalent to the option type. -/
def subtype_insert_equiv_option {t : finset α} {x : α} (h : x ∉ t) :
{i // i ∈ insert x t} ≃ option {i // i ∈ t} :=
begin
refine
{ to_fun := λ y, if h : ↑y = x then none else some ⟨y, (mem_insert.mp y.2).resolve_left h⟩,
inv_fun := λ y, y.elim ⟨x, mem_insert_self _ _⟩ $ λ z, ⟨z, mem_insert_of_mem z.2⟩,
.. },
{ intro y, by_cases h : ↑y = x,
simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk],
simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] },
{ rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk],
have : ↑y ≠ x, { rintro ⟨⟩, exact h y.2 },
simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta,
subtype.coe_mk] },
end
/-! ### union -/
/-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/
instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩
theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl
@[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 :=
ndunion_eq_union s₁.2
@[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion
@[simp] theorem disj_union_eq_union {α} [decidable_eq α] (s t h) : @disj_union α s t h = s ∪ t :=
ext $ λ a, by simp
theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ :=
mem_union.2 $ or.inl h
theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ :=
mem_union.2 $ or.inr h
theorem forall_mem_union {s₁ s₂ : finset α} {p : α → Prop} :
(∀ ab ∈ (s₁ ∪ s₂), p ab) ↔ (∀ a ∈ s₁, p a) ∧ (∀ b ∈ s₂, p b) :=
⟨λ h, ⟨λ a, h a ∘ mem_union_left _, λ b, h b ∘ mem_union_right _⟩,
λ h ab hab, (mem_union.mp hab).elim (h.1 _) (h.2 _)⟩
theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ :=
by rw [mem_union, not_or_distrib]
@[simp, norm_cast]
lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (s₁ ∪ s₂ : set α) := set.ext $ λ x, mem_union
theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ :=
val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩)
theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _
theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _
lemma union_subset_union {s₁ t₁ s₂ t₂ : finset α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) :
s₁ ∪ s₂ ⊆ t₁ ∪ t₂ :=
by { intros x hx, rw finset.mem_union at hx ⊢, tauto }
theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ :=
ext $ λ x, by simp only [mem_union, or_comm]
instance : is_commutative (finset α) (∪) := ⟨union_comm⟩
@[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) :=
ext $ λ x, by simp only [mem_union, or_assoc]
instance : is_associative (finset α) (∪) := ⟨union_assoc⟩
@[simp] theorem union_idempotent (s : finset α) : s ∪ s = s :=
ext $ λ _, mem_union.trans $ or_self _
instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩
theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) :=
ext $ λ _, by simp only [mem_union, or.left_comm]
theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ :=
ext $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ s₂)]
theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s
@[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s :=
ext $ λ x, mem_union.trans $ or_false _
@[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s :=
ext $ λ x, mem_union.trans $ false_or _
theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl
@[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) :=
by simp only [insert_eq, union_assoc]
@[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) :=
by simp only [insert_eq, union_left_comm]
theorem insert_union_distrib (a : α) (s t : finset α) :
insert a (s ∪ t) = insert a s ∪ insert a t :=
by simp only [insert_union, union_insert, insert_idem]
@[simp] lemma union_eq_left_iff_subset {s t : finset α} :
s ∪ t = s ↔ t ⊆ s :=
begin
split,
{ assume h,
have : t ⊆ s ∪ t := subset_union_right _ _,
rwa h at this },
{ assume h,
exact subset.antisymm (union_subset (subset.refl _) h) (subset_union_left _ _) }
end
@[simp] lemma left_eq_union_iff_subset {s t : finset α} :
s = s ∪ t ↔ t ⊆ s :=
by rw [← union_eq_left_iff_subset, eq_comm]
@[simp] lemma union_eq_right_iff_subset {s t : finset α} :
t ∪ s = s ↔ t ⊆ s :=
by rw [union_comm, union_eq_left_iff_subset]
@[simp] lemma right_eq_union_iff_subset {s t : finset α} :
s = t ∪ s ↔ t ⊆ s :=
by rw [← union_eq_right_iff_subset, eq_comm]
/--
To prove a relation on pairs of `finset X`, it suffices to show that it is
* symmetric,
* it holds when one of the `finset`s is empty,
* it holds for pairs of singletons,
* if it holds for `[a, c]` and for `[b, c]`, then it holds for `[a ∪ b, c]`.
-/
lemma induction_on_union (P : finset α → finset α → Prop)
(symm : ∀ {a b}, P a b → P b a)
(empty_right : ∀ {a}, P a ∅)
(singletons : ∀ {a b}, P {a} {b})
(union_of : ∀ {a b c}, P a c → P b c → P (a ∪ b) c) :
∀ a b, P a b :=
begin
intros a b,
refine finset.induction_on b empty_right (λ x s xs hi, symm _),
rw finset.insert_eq,
apply union_of _ (symm hi),
refine finset.induction_on a empty_right (λ a t ta hi, symm _),
rw finset.insert_eq,
exact union_of singletons (symm hi),
end
lemma exists_mem_subset_of_subset_bUnion_of_directed_on {α ι : Type*}
{f : ι → set α} {c : set ι} {a : ι} (hac : a ∈ c) (hc : directed_on (λ i j, f i ⊆ f j) c)
{s : finset α} (hs : (s : set α) ⊆ ⋃ i ∈ c, f i) : ∃ i ∈ c, (s : set α) ⊆ f i :=
begin
classical,
revert hs,
apply s.induction_on,
{ intros,
use [a, hac],
simp },
{ intros b t hbt htc hbtc,
obtain ⟨i : ι , hic : i ∈ c, hti : (t : set α) ⊆ f i⟩ :=
htc (set.subset.trans (t.subset_insert b) hbtc),
obtain ⟨j, hjc, hbj⟩ : ∃ j ∈ c, b ∈ f j,
by simpa [set.mem_bUnion_iff] using hbtc (t.mem_insert_self b),
rcases hc j hjc i hic with ⟨k, hkc, hk, hk'⟩,
use [k, hkc],
rw [coe_insert, set.insert_subset],
exact ⟨hk hbj, trans hti hk'⟩ }
end
/-! ### inter -/
/-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/
instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩
-- TODO: some of these results may have simpler proofs, once there are enough results
-- to obtain the `lattice` instance.
theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl
@[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 :=
ndinter_eq_inter s₁.2
@[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter
theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) :
a ∈ s₁ := (mem_inter.1 h).1
theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) :
a ∈ s₂ := (mem_inter.1 h).2
theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ :=
and_imp.1 mem_inter.2
theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left
theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right
theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ :=
by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial
@[simp, norm_cast]
lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (s₁ ∩ s₂ : set α) := set.ext $ λ _, mem_inter
@[simp] theorem union_inter_cancel_left {s t : finset α} : (s ∪ t) ∩ s = s :=
by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left]
@[simp] theorem union_inter_cancel_right {s t : finset α} : (s ∪ t) ∩ t = t :=
by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right]
theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ :=
ext $ λ _, by simp only [mem_inter, and_comm]
@[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) :=
ext $ λ _, by simp only [mem_inter, and_assoc]
theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) :=
ext $ λ _, by simp only [mem_inter, and.left_comm]
theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ :=
ext $ λ _, by simp only [mem_inter, and.right_comm]
@[simp] theorem inter_self (s : finset α) : s ∩ s = s :=
ext $ λ _, mem_inter.trans $ and_self _
@[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ :=
ext $ λ _, mem_inter.trans $ and_false _
@[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ :=
ext $ λ _, mem_inter.trans $ false_and _
@[simp] lemma inter_union_self (s t : finset α) : s ∩ (t ∪ s) = s :=
by rw [inter_comm, union_inter_cancel_right]
@[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) :
insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) :=
ext $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h,
by simp only [mem_inter, mem_insert, or_and_distrib_left, this]
@[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) :
s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) :=
by rw [inter_comm, insert_inter_of_mem h, inter_comm]
@[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) :
insert a s₁ ∩ s₂ = s₁ ∩ s₂ :=
ext $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H,
by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or]
@[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) :
s₁ ∩ insert a s₂ = s₁ ∩ s₂ :=
by rw [inter_comm, insert_inter_of_not_mem h, inter_comm]
@[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : {a} ∩ s = {a} :=
show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter]
@[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : {a} ∩ s = ∅ :=
eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h
@[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ {a} = {a} :=
by rw [inter_comm, singleton_inter_of_mem h]
@[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ {a} = ∅ :=
by rw [inter_comm, singleton_inter_of_not_mem h]
@[mono]
lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t :=
begin
intros a a_in,
rw finset.mem_inter at a_in ⊢,
exact ⟨h a_in.1, h' a_in.2⟩
end
lemma inter_subset_inter_right {x y s : finset α} (h : x ⊆ y) : x ∩ s ⊆ y ∩ s :=
finset.inter_subset_inter h (finset.subset.refl _)
lemma inter_subset_inter_left {x y s : finset α} (h : x ⊆ y) : s ∩ x ⊆ s ∩ y :=
finset.inter_subset_inter (finset.subset.refl _) h
/-! ### lattice laws -/
instance : lattice (finset α) :=
{ sup := (∪),
sup_le := assume a b c, union_subset,
le_sup_left := subset_union_left,
le_sup_right := subset_union_right,
inf := (∩),
le_inf := assume a b c, subset_inter,
inf_le_left := inter_subset_left,
inf_le_right := inter_subset_right,
..finset.partial_order }
@[simp] theorem sup_eq_union : ((⊔) : finset α → finset α → finset α) = (∪) := rfl
@[simp] theorem inf_eq_inter : ((⊓) : finset α → finset α → finset α) = (∩) := rfl
instance : semilattice_inf_bot (finset α) :=
{ bot := ∅, bot_le := empty_subset, ..finset.lattice }
@[simp] lemma bot_eq_empty : (⊥ : finset α) = ∅ := rfl
instance {α : Type*} [decidable_eq α] : semilattice_sup_bot (finset α) :=
{ ..finset.semilattice_inf_bot, ..finset.lattice }
instance : distrib_lattice (finset α) :=
{ le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c,
by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt};
simp only [true_or, imp_true_iff, true_and, or_true],
..finset.lattice }
theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left
theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right
theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left
theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right
lemma union_eq_empty_iff (A B : finset α) : A ∪ B = ∅ ↔ A = ∅ ∧ B = ∅ := sup_eq_bot_iff
lemma union_subset_iff {s₁ s₂ s₃ : finset α} :
s₁ ∪ s₂ ⊆ s₃ ↔ s₁ ⊆ s₃ ∧ s₂ ⊆ s₃ :=
(sup_le_iff : s₁ ⊔ s₂ ≤ s₃ ↔ s₁ ≤ s₃ ∧ s₂ ≤ s₃)
lemma subset_inter_iff {s₁ s₂ s₃ : finset α} :
s₁ ⊆ s₂ ∩ s₃ ↔ s₁ ⊆ s₂ ∧ s₁ ⊆ s₃ :=
(le_inf_iff : s₁ ≤ s₂ ⊓ s₃ ↔ s₁ ≤ s₂ ∧ s₁ ≤ s₃)
theorem inter_eq_left_iff_subset (s t : finset α) :
s ∩ t = s ↔ s ⊆ t :=
(inf_eq_left : s ⊓ t = s ↔ s ≤ t)
theorem inter_eq_right_iff_subset (s t : finset α) :
t ∩ s = s ↔ s ⊆ t :=
(inf_eq_right : t ⊓ s = s ↔ s ≤ t)
/-! ### erase -/
/-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are
not equal to `a`. -/
def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩
@[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl
@[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s :=
mem_erase_iff_of_nodup s.2
theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := mem_erase_of_nodup s.2
-- While this can be solved by `simp`, this lemma is eligible for `dsimp`
@[nolint simp_nf, simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl
theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a :=
by simp only [mem_erase]; exact and.left
theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase
theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b :=
by simp only [mem_erase]; exact and.intro
/-- An element of `s` that is not an element of `erase s a` must be
`a`. -/
lemma eq_of_mem_of_not_mem_erase {a b : α} {s : finset α} (hs : b ∈ s)
(hsa : b ∉ s.erase a) : b = a :=
begin
rw [mem_erase, not_and] at hsa,
exact not_imp_not.mp hsa hs
end
theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s :=
ext $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or];
apply and_iff_right_of_imp; rintro H rfl; exact h H
theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s :=
ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and];
apply or_iff_right_of_imp; rintro rfl; exact h
theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a :=
val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h
theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _
@[simp, norm_cast] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (s \ {a} : set α) :=
set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl
lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s :=
calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _
... = _ : insert_erase h
@[simp]
theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s :=
eq_of_veq $ erase_of_not_mem h
theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t :=
by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp];
exact forall_congr (λ x, forall_swap)
theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s :=
subset_insert_iff.1 $ subset.refl _
theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) :=
subset_insert_iff.2 $ subset.refl _
lemma erase_inj {x y : α} (s : finset α) (hx : x ∈ s) :
s.erase x = s.erase y ↔ x = y :=
begin
refine ⟨λ h, _, congr_arg _⟩,
rw eq_of_mem_of_not_mem_erase hx,
rw ←h,
simp,
end
lemma erase_inj_on (s : finset α) : set.inj_on s.erase s :=
λ _ _ _ _, (erase_inj s ‹_›).mp
/-! ### sdiff -/
/-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/
instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩
@[simp] lemma sdiff_val (s₁ s₂ : finset α) : (s₁ \ s₂).val = s₁.val - s₂.val := rfl
@[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} :
a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2
@[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ :=
eq_empty_of_forall_not_mem $
by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h
instance : generalized_boolean_algebra (finset α) :=
{ sup_inf_sdiff := λ x y, by { simp only [ext_iff, mem_union, mem_sdiff, inf_eq_inter, sup_eq_union,
mem_inter], tauto },
inf_inf_sdiff := λ x y, by { simp only [ext_iff, inter_sdiff_self, inter_empty, inter_assoc,
false_iff, inf_eq_inter, not_mem_empty], tauto },
..finset.has_sdiff,
..finset.distrib_lattice,
..finset.semilattice_inf_bot }
lemma not_mem_sdiff_of_mem_right {a : α} {s t : finset α} (h : a ∈ t) : a ∉ s \ t :=
by simp only [mem_sdiff, h, not_true, not_false_iff, and_false]
theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ :=
sup_sdiff_of_le h
theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ :=
(union_comm _ _).trans (union_sdiff_of_subset h)
theorem inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u :=
by { ext x, simp [and_assoc] }
@[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ :=
inf_sdiff_self_left
@[simp] theorem sdiff_self (s₁ : finset α) : s₁ \ s₁ = ∅ :=
sdiff_self
theorem sdiff_inter_distrib_right (s₁ s₂ s₃ : finset α) : s₁ \ (s₂ ∩ s₃) = (s₁ \ s₂) ∪ (s₁ \ s₃) :=
sdiff_inf
@[simp] theorem sdiff_inter_self_left (s₁ s₂ : finset α) : s₁ \ (s₁ ∩ s₂) = s₁ \ s₂ :=
sdiff_inf_self_left
@[simp] theorem sdiff_inter_self_right (s₁ s₂ : finset α) : s₁ \ (s₂ ∩ s₁) = s₁ \ s₂ :=
sdiff_inf_self_right
@[simp] theorem sdiff_empty {s₁ : finset α} : s₁ \ ∅ = s₁ :=
sdiff_bot
@[mono]
theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) :
t₁ \ s₁ ⊆ t₂ \ s₂ :=
sdiff_le_sdiff ‹t₁ ≤ t₂› ‹s₂ ≤ s₁›
@[simp, norm_cast] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (s₁ \ s₂ : set α) :=
set.ext $ λ _, mem_sdiff
@[simp] theorem union_sdiff_self_eq_union {s t : finset α} : s ∪ (t \ s) = s ∪ t :=
sup_sdiff_self_right
@[simp] theorem sdiff_union_self_eq_union {s t : finset α} : (s \ t) ∪ t = s ∪ t :=
sup_sdiff_self_left
lemma union_sdiff_symm {s t : finset α} : s ∪ (t \ s) = t ∪ (s \ t) :=
sup_sdiff_symm
lemma sdiff_union_inter (s t : finset α) : (s \ t) ∪ (s ∩ t) = s :=
by { rw union_comm, exact sup_inf_sdiff _ _ }
@[simp] lemma sdiff_idem (s t : finset α) : s \ t \ t = s \ t :=
sdiff_idem
lemma sdiff_eq_empty_iff_subset {s t : finset α} : s \ t = ∅ ↔ s ⊆ t :=
sdiff_eq_bot_iff
@[simp] lemma empty_sdiff (s : finset α) : ∅ \ s = ∅ :=
bot_sdiff
lemma insert_sdiff_of_not_mem (s : finset α) {t : finset α} {x : α} (h : x ∉ t) :
(insert x s) \ t = insert x (s \ t) :=
begin
rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert],
exact set.insert_diff_of_not_mem s h
end
lemma insert_sdiff_of_mem (s : finset α) {t : finset α} {x : α} (h : x ∈ t) :
(insert x s) \ t = s \ t :=
begin
rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert],
exact set.insert_diff_of_mem s h
end
@[simp] lemma insert_sdiff_insert (s t : finset α) (x : α) :
(insert x s) \ (insert x t) = s \ insert x t :=
insert_sdiff_of_mem _ (mem_insert_self _ _)
lemma sdiff_insert_of_not_mem {s : finset α} {x : α} (h : x ∉ s) (t : finset α) :
s \ (insert x t) = s \ t :=
begin
refine subset.antisymm (sdiff_subset_sdiff (subset.refl _) (subset_insert _ _)) (λ y hy, _),
simp only [mem_sdiff, mem_insert, not_or_distrib] at hy ⊢,
exact ⟨hy.1, λ hxy, h $ hxy ▸ hy.1, hy.2⟩
end
@[simp] lemma sdiff_subset (s t : finset α) : s \ t ⊆ s :=
show s \ t ≤ s, from sdiff_le
lemma sdiff_ssubset {s t : finset α} (h : t ⊆ s) (ht : t.nonempty) :
s \ t ⊂ s :=
sdiff_lt (le_iff_subset.2 h) ht.ne_empty
lemma union_sdiff_distrib (s₁ s₂ t : finset α) : (s₁ ∪ s₂) \ t = s₁ \ t ∪ s₂ \ t :=
sup_sdiff
lemma sdiff_union_distrib (s t₁ t₂ : finset α) : s \ (t₁ ∪ t₂) = (s \ t₁) ∩ (s \ t₂) :=
sdiff_sup
lemma union_sdiff_self (s t : finset α) : (s ∪ t) \ t = s \ t :=
sup_sdiff_right_self
lemma sdiff_singleton_eq_erase (a : α) (s : finset α) : s \ singleton a = erase s a :=
by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto }
@[simp] lemma sdiff_singleton_not_mem_eq_self (s : finset α) {a : α} (ha : a ∉ s) : s \ {a} = s :=
by simp only [sdiff_singleton_eq_erase, ha, erase_eq_of_not_mem, not_false_iff]
lemma sdiff_sdiff_self_left (s t : finset α) : s \ (s \ t) = s ∩ t :=
sdiff_sdiff_right_self
lemma sdiff_eq_sdiff_iff_inter_eq_inter {s t₁ t₂ : finset α} : s \ t₁ = s \ t₂ ↔ s ∩ t₁ = s ∩ t₂ :=
sdiff_eq_sdiff_iff_inf_eq_inf
lemma union_eq_sdiff_union_sdiff_union_inter (s t : finset α) :
s ∪ t = (s \ t) ∪ (t \ s) ∪ (s ∩ t) :=
sup_eq_sdiff_sup_sdiff_sup_inf
end decidable_eq
/-! ### attach -/
/-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype
`{x // x ∈ s}`. -/
def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩
theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {s : finset α} (hx : x ∈ s) :
sizeof x < sizeof s := by
{ cases s, dsimp [sizeof, has_sizeof.sizeof, finset.sizeof],
apply lt_add_left, exact multiset.sizeof_lt_sizeof_of_mem hx }
@[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl
@[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _
@[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl
@[simp] lemma attach_nonempty_iff (s : finset α) : s.attach.nonempty ↔ s.nonempty :=
by simp [finset.nonempty]
@[simp] lemma attach_eq_empty_iff (s : finset α) : s.attach = ∅ ↔ s = ∅ :=
by simpa [eq_empty_iff_forall_not_mem]
/-! ### piecewise -/
section piecewise
/-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its
complement. -/
def piecewise {α : Type*} {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) [∀j, decidable (j ∈ s)] :
Πi, δ i :=
λi, if i ∈ s then f i else g i
variables {δ : α → Sort*} (s : finset α) (f g : Πi, δ i)
@[simp] lemma piecewise_insert_self [decidable_eq α] {j : α} [∀i, decidable (i ∈ insert j s)] :
(insert j s).piecewise f g j = f j :=
by simp [piecewise]
@[simp] lemma piecewise_empty [∀i : α, decidable (i ∈ (∅ : finset α))] : piecewise ∅ f g = g :=
by { ext i, simp [piecewise] }
variable [∀j, decidable (j ∈ s)]
@[norm_cast] lemma piecewise_coe [∀j, decidable (j ∈ (s : set α))] :
(s : set α).piecewise f g = s.piecewise f g :=
by { ext, congr }
@[simp, priority 980]
lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i :=
by simp [piecewise, hi]
@[simp, priority 980]
lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i :=
by simp [piecewise, hi]
lemma piecewise_congr {f f' g g' : Π i, δ i} (hf : ∀ i ∈ s, f i = f' i) (hg : ∀ i ∉ s, g i = g' i) :
s.piecewise f g = s.piecewise f' g' :=
funext $ λ i, if_ctx_congr iff.rfl (hf i) (hg i)
@[simp, priority 990]
lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀i, decidable (i ∈ insert j s)]
(h : i ≠ j) : (insert j s).piecewise f g i = s.piecewise f g i :=
by simp [piecewise, h]
lemma piecewise_insert [decidable_eq α] (j : α) [∀i, decidable (i ∈ insert j s)] :
(insert j s).piecewise f g = update (s.piecewise f g) j (f j) :=
begin
classical,
rw [← piecewise_coe, ← piecewise_coe, ← set.piecewise_insert, ← coe_insert j s],
congr
end
lemma piecewise_cases {i} (p : δ i → Prop) (hf : p (f i)) (hg : p (g i)) : p (s.piecewise f g i) :=
by by_cases hi : i ∈ s; simpa [hi]
lemma piecewise_mem_set_pi {δ : α → Type*} {t : set α} {t' : Π i, set (δ i)}
{f g} (hf : f ∈ set.pi t t') (hg : g ∈ set.pi t t') : s.piecewise f g ∈ set.pi t t' :=
by { classical, rw ← piecewise_coe, exact set.piecewise_mem_pi ↑s hf hg }
lemma piecewise_singleton [decidable_eq α] (i : α) :
piecewise {i} f g = update g i (f i) :=
by rw [← insert_emptyc_eq, piecewise_insert, piecewise_empty]
lemma piecewise_piecewise_of_subset_left {s t : finset α} [Π i, decidable (i ∈ s)]
[Π i, decidable (i ∈ t)] (h : s ⊆ t) (f₁ f₂ g : Π a, δ a) :
s.piecewise (t.piecewise f₁ f₂) g = s.piecewise f₁ g :=
s.piecewise_congr (λ i hi, piecewise_eq_of_mem _ _ _ (h hi)) (λ _ _, rfl)
@[simp] lemma piecewise_idem_left (f₁ f₂ g : Π a, δ a) :
s.piecewise (s.piecewise f₁ f₂) g = s.piecewise f₁ g :=
piecewise_piecewise_of_subset_left (subset.refl _) _ _ _
lemma piecewise_piecewise_of_subset_right {s t : finset α} [Π i, decidable (i ∈ s)]
[Π i, decidable (i ∈ t)] (h : t ⊆ s) (f g₁ g₂ : Π a, δ a) :
s.piecewise f (t.piecewise g₁ g₂) = s.piecewise f g₂ :=
s.piecewise_congr (λ _ _, rfl) (λ i hi, t.piecewise_eq_of_not_mem _ _ (mt (@h _) hi))
@[simp] lemma piecewise_idem_right (f g₁ g₂ : Π a, δ a) :
s.piecewise f (s.piecewise g₁ g₂) = s.piecewise f g₂ :=
piecewise_piecewise_of_subset_right (subset.refl _) f g₁ g₂
lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) :
update f i v = piecewise (singleton i) (λj, v) f :=
(piecewise_singleton _ _ _).symm
lemma update_piecewise [decidable_eq α] (i : α) (v : δ i) :
update (s.piecewise f g) i v = s.piecewise (update f i v) (update g i v) :=
begin
ext j,
rcases em (j = i) with (rfl|hj); by_cases hs : j ∈ s; simp *
end
lemma update_piecewise_of_mem [decidable_eq α] {i : α} (hi : i ∈ s) (v : δ i) :
update (s.piecewise f g) i v = s.piecewise (update f i v) g :=
begin
rw update_piecewise,
refine s.piecewise_congr (λ _ _, rfl) (λ j hj, update_noteq _ _ _),
exact λ h, hj (h.symm ▸ hi)
end
lemma update_piecewise_of_not_mem [decidable_eq α] {i : α} (hi : i ∉ s) (v : δ i) :
update (s.piecewise f g) i v = s.piecewise f (update g i v) :=
begin
rw update_piecewise,
refine s.piecewise_congr (λ j hj, update_noteq _ _ _) (λ _ _, rfl),
exact λ h, hi (h ▸ hj)
end
lemma piecewise_le_of_le_of_le {δ : α → Type*} [Π i, preorder (δ i)] {f g h : Π i, δ i}
(Hf : f ≤ h) (Hg : g ≤ h) : s.piecewise f g ≤ h :=
λ x, piecewise_cases s f g (≤ h x) (Hf x) (Hg x)
lemma le_piecewise_of_le_of_le {δ : α → Type*} [Π i, preorder (δ i)] {f g h : Π i, δ i}
(Hf : h ≤ f) (Hg : h ≤ g) : h ≤ s.piecewise f g :=
λ x, piecewise_cases s f g (λ y, h x ≤ y) (Hf x) (Hg x)
lemma piecewise_le_piecewise' {δ : α → Type*} [Π i, preorder (δ i)] {f g f' g' : Π i, δ i}
(Hf : ∀ x ∈ s, f x ≤ f' x) (Hg : ∀ x ∉ s, g x ≤ g' x) : s.piecewise f g ≤ s.piecewise f' g' :=
λ x, by { by_cases hx : x ∈ s; simp [hx, *] }
lemma piecewise_le_piecewise {δ : α → Type*} [Π i, preorder (δ i)] {f g f' g' : Π i, δ i}
(Hf : f ≤ f') (Hg : g ≤ g') : s.piecewise f g ≤ s.piecewise f' g' :=
s.piecewise_le_piecewise' (λ x _, Hf x) (λ x _, Hg x)
lemma piecewise_mem_Icc_of_mem_of_mem {δ : α → Type*} [Π i, preorder (δ i)] {f f₁ g g₁ : Π i, δ i}
(hf : f ∈ set.Icc f₁ g₁) (hg : g ∈ set.Icc f₁ g₁) :
s.piecewise f g ∈ set.Icc f₁ g₁ :=
⟨le_piecewise_of_le_of_le _ hf.1 hg.1, piecewise_le_of_le_of_le _ hf.2 hg.2⟩
lemma piecewise_mem_Icc {δ : α → Type*} [Π i, preorder (δ i)] {f g : Π i, δ i} (h : f ≤ g) :
s.piecewise f g ∈ set.Icc f g :=
piecewise_mem_Icc_of_mem_of_mem _ (set.left_mem_Icc.2 h) (set.right_mem_Icc.2 h)
lemma piecewise_mem_Icc' {δ : α → Type*} [Π i, preorder (δ i)] {f g : Π i, δ i} (h : g ≤ f) :
s.piecewise f g ∈ set.Icc g f :=
piecewise_mem_Icc_of_mem_of_mem _ (set.right_mem_Icc.2 h) (set.left_mem_Icc.2 h)
end piecewise
section decidable_pi_exists
variables {s : finset α}
instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] :
decidable (∀a (h : a ∈ s), p a h) :=
multiset.decidable_dforall_multiset
/-- decidable equality for functions whose domain is bounded by finsets -/
instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] :
decidable_eq (Πa∈s, β a) :=
multiset.decidable_eq_pi_multiset
instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] :
decidable (∃a (h : a ∈ s), p a h) :=
multiset.decidable_dexists_multiset
end decidable_pi_exists
/-! ### filter -/
section filter
variables (p q : α → Prop) [decidable_pred p] [decidable_pred q]
/-- `filter p s` is the set of elements of `s` that satisfy `p`. -/
def filter (s : finset α) : finset α :=
⟨_, nodup_filter p s.2⟩
@[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl
@[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ _
variable {p}
@[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter
theorem filter_ssubset {s : finset α} : s.filter p ⊂ s ↔ ∃ x ∈ s, ¬ p x :=
⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (λ hp, mem_filter.2 ⟨hs, hp⟩) hp⟩,
λ ⟨x, hs, hp⟩, ⟨s.filter_subset _, λ h, hp (mem_filter.1 (h hs)).2⟩⟩
variable (p)
theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) :=
ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm]
lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] :
@finset.filter α (λ _, true) h s = s :=
by ext; simp
@[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ :=
ext $ assume a, by simp only [mem_filter, and_false]; refl
variables {p q}
/-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/
@[simp] lemma filter_true_of_mem {s : finset α} (h : ∀ x ∈ s, p x) : s.filter p = s :=
ext $ λ x, ⟨λ h, (mem_filter.1 h).1, λ hx, mem_filter.2 ⟨hx, h x hx⟩⟩
/-- If all elements of a `finset` fail to satisfy the predicate `p`, `s.filter p` is `∅`. -/
lemma filter_false_of_mem {s : finset α} (h : ∀ x ∈ s, ¬ p x) : s.filter p = ∅ :=
eq_empty_of_forall_not_mem (by simpa)
lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s :=
eq_of_veq $ filter_congr H
variables (p q)
lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ _
lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p :=
assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩
lemma monotone_filter_left (p : α → Prop) [decidable_pred p] :
monotone (filter p) :=
λ _ _, filter_subset_filter p
lemma monotone_filter_right (s : finset α) ⦃p q : α → Prop⦄
[decidable_pred p] [decidable_pred q] (h : p ≤ q) :
s.filter p ≤ s.filter q :=
multiset.subset_of_le (multiset.monotone_filter_right s.val h)
@[simp, norm_cast] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) :=
set.ext $ λ _, mem_filter
theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ :=
by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
variable [decidable_eq α]
theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p :=
ext $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right]
theorem filter_union_right (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) :=
ext $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm]
lemma filter_mem_eq_inter {s t : finset α} [Π i, decidable (i ∈ t)] :
s.filter (λ i, i ∈ t) = s ∩ t :=
ext $ λ i, by rw [mem_filter, mem_inter]
theorem filter_inter (s t : finset α) : filter p s ∩ t = filter p (s ∩ t) :=
by { ext, simp only [mem_inter, mem_filter, and.right_comm] }
theorem inter_filter (s t : finset α) : s ∩ filter p t = filter p (s ∩ t) :=
by rw [inter_comm, filter_inter, inter_comm]
theorem filter_insert (a : α) (s : finset α) :
filter p (insert a s) = if p a then insert a (filter p s) else filter p s :=
by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
theorem filter_or [decidable_pred (λ a, p a ∨ q a)] (s : finset α) :
s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q :=
ext $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left]
theorem filter_and [decidable_pred (λ a, p a ∧ q a)] (s : finset α) :
s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q :=
ext $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self]
theorem filter_not [decidable_pred (λ a, ¬ p a)] (s : finset α) :
s.filter (λ a, ¬ p a) = s \ s.filter p :=
ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $
λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm
theorem sdiff_eq_filter (s₁ s₂ : finset α) :
s₁ \ s₂ = filter (∉ s₂) s₁ := ext $ λ _, by simp only [mem_sdiff, mem_filter]
theorem sdiff_eq_self (s₁ s₂ : finset α) :
s₁ \ s₂ = s₁ ↔ s₁ ∩ s₂ ⊆ ∅ :=
by { simp [subset.antisymm_iff],
split; intro h,
{ transitivity' ((s₁ \ s₂) ∩ s₂), mono, simp },
{ calc s₁ \ s₂
⊇ s₁ \ (s₁ ∩ s₂) : by simp [(⊇)]
... ⊇ s₁ \ ∅ : by mono using [(⊇)]
... ⊇ s₁ : by simp [(⊇)] } }
theorem filter_union_filter_neg_eq [decidable_pred (λ a, ¬ p a)]
(s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s :=
by simp only [filter_not, union_sdiff_of_subset (filter_subset p s)]
theorem filter_inter_filter_neg_eq [decidable_pred (λ a, ¬ p a)]
(s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ :=
by simp only [filter_not, inter_sdiff_self]
lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} (h : ↑s ⊆ t₁ ∪ t₂) :
∃s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ :=
begin
classical,
refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩,
{ simp [filter_union_right, em] },
{ intro x, simp },
{ intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ }
end
/- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/
@[simp] lemma filter_congr_decidable {α} (s : finset α) (p : α → Prop) (h : decidable_pred p)
[decidable_pred p] : @filter α p h s = s.filter p :=
by congr
section classical
open_locale classical
/-- The following instance allows us to write `{x ∈ s | p x}` for `finset.filter p s`.
Since the former notation requires us to define this for all propositions `p`, and `finset.filter`
only works for decidable propositions, the notation `{x ∈ s | p x}` is only compatible with
classical logic because it uses `classical.prop_decidable`.
We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp`
unfolds the notation `{x ∈ s | p x}` to `finset.filter p s`. If `p` happens to be decidable, the
simp-lemma `finset.filter_congr_decidable` will make sure that `finset.filter` uses the right
instance for decidability.
-/
noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩
@[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl
end classical
/--
After filtering out everything that does not equal a given value, at most that value remains.
This is equivalent to `filter_eq'` with the equality the other way.
-/
-- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing
-- on, e.g. `x ∈ s.filter(eq b)`.
lemma filter_eq [decidable_eq β] (s : finset β) (b : β) :
s.filter (eq b) = ite (b ∈ s) {b} ∅ :=
begin
split_ifs,
{ ext,
simp only [mem_filter, mem_singleton],
exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩, }⟩ },
{ ext,
simp only [mem_filter, not_and, iff_false, not_mem_empty],
rintros m ⟨e⟩, exact h m, }
end
/--
After filtering out everything that does not equal a given value, at most that value remains.
This is equivalent to `filter_eq` with the equality the other way.
-/
lemma filter_eq' [decidable_eq β] (s : finset β) (b : β) :
s.filter (λ a, a = b) = ite (b ∈ s) {b} ∅ :=
trans (filter_congr (λ _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b)
lemma filter_ne [decidable_eq β] (s : finset β) (b : β) :
s.filter (λ a, b ≠ a) = s.erase b :=
by { ext, simp only [mem_filter, mem_erase, ne.def], tauto, }
lemma filter_ne' [decidable_eq β] (s : finset β) (b : β) :
s.filter (λ a, a ≠ b) = s.erase b :=
trans (filter_congr (λ _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b)
end filter
/-! ### range -/
section range
variables {n m l : ℕ}
/-- `range n` is the set of natural numbers less than `n`. -/
def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩
@[simp] theorem range_coe (n : ℕ) : (range n).1 = multiset.range n := rfl
@[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range
@[simp] theorem range_zero : range 0 = ∅ := rfl
@[simp] theorem range_one : range 1 = {0} := rfl
theorem range_succ : range (succ n) = insert n (range n) :=
eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm
theorem range_add_one : range (n + 1) = insert n (range n) :=
range_succ
@[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self
@[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := multiset.self_mem_range_succ n
@[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset
theorem range_mono : monotone range := λ _ _, range_subset.2
lemma mem_range_succ_iff {a b : ℕ} : a ∈ finset.range b.succ ↔ a ≤ b :=
finset.mem_range.trans nat.lt_succ_iff
lemma mem_range_le {n x : ℕ} (hx : x ∈ range n) : x ≤ n :=
(mem_range.1 hx).le
lemma mem_range_sub_ne_zero {n x : ℕ} (hx : x ∈ range n) : n - x ≠ 0 :=
ne_of_gt $ nat.sub_pos_of_lt $ mem_range.1 hx
@[simp] lemma nonempty_range_iff : (range n).nonempty ↔ n ≠ 0 :=
⟨λ ⟨k, hk⟩, ((zero_le k).trans_lt $ mem_range.1 hk).ne',
λ h, ⟨0, mem_range.2 $ pos_iff_ne_zero.2 h⟩⟩
@[simp] lemma range_eq_empty_iff : range n = ∅ ↔ n = 0 :=
by rw [← not_nonempty_iff_eq_empty, nonempty_range_iff, not_not]
lemma nonempty_range_succ : (range $ n + 1).nonempty :=
nonempty_range_iff.2 n.succ_ne_zero
end range
/- useful rules for calculations with quantifiers -/
theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false :=
by simp only [not_mem_empty, false_and, exists_false]
theorem exists_mem_insert [d : decidable_eq α]
(a : α) (s : finset α) (p : α → Prop) :
(∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) :=
by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left]
theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true :=
iff_true_intro $ λ _, false.elim
theorem forall_mem_insert [d : decidable_eq α]
(a : α) (s : finset α) (p : α → Prop) :
(∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) :=
by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq]
end finset
/-- Equivalence between the set of natural numbers which are `≥ k` and `ℕ`, given by `n → n - k`. -/
def not_mem_range_equiv (k : ℕ) : {n // n ∉ range k} ≃ ℕ :=
{ to_fun := λ i, i.1 - k,
inv_fun := λ j, ⟨j + k, by simp⟩,
left_inv :=
begin
assume j,
rw subtype.ext_iff_val,
apply nat.sub_add_cancel,
simpa using j.2
end,
right_inv := λ j, nat.add_sub_cancel _ _ }
@[simp] lemma coe_not_mem_range_equiv (k : ℕ) :
(not_mem_range_equiv k : {n // n ∉ range k} → ℕ) = (λ i, i - k) := rfl
@[simp] lemma coe_not_mem_range_equiv_symm (k : ℕ) :
((not_mem_range_equiv k).symm : ℕ → {n // n ∉ range k}) = λ j, ⟨j + k, by simp⟩ := rfl
namespace option
/-- Construct an empty or singleton finset from an `option` -/
def to_finset : option α → finset α
| none := ∅
| (some a) := {a}
@[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl
@[simp] theorem to_finset_some {a : α} : (some a).to_finset = {a} := rfl
@[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o :=
by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl
end option
/-! ### erase_dup on list and multiset -/
namespace multiset
variable [decidable_eq α]
/-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/
def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩
@[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl
theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset :=
finset.val_inj.1 (erase_dup_eq_self.2 n).symm
lemma nodup.to_finset_inj {l l' : multiset α} (hl : nodup l) (hl' : nodup l')
(h : l.to_finset = l'.to_finset) : l = l' :=
by simpa [←to_finset_eq hl, ←to_finset_eq hl'] using h
@[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s :=
mem_erase_dup
@[simp] lemma to_finset_zero :
to_finset (0 : multiset α) = ∅ :=
rfl
@[simp] lemma to_finset_cons (a : α) (s : multiset α) :
to_finset (a ::ₘ s) = insert a (to_finset s) :=
finset.eq_of_veq erase_dup_cons
@[simp] lemma to_finset_singleton (a : α) :
to_finset ({a} : multiset α) = {a} :=
by rw [singleton_eq_cons, to_finset_cons, to_finset_zero, is_lawful_singleton.insert_emptyc_eq]
@[simp] lemma to_finset_add (s t : multiset α) :
to_finset (s + t) = to_finset s ∪ to_finset t :=
finset.ext $ by simp
@[simp] lemma to_finset_nsmul (s : multiset α) :
∀(n : ℕ) (hn : n ≠ 0), (n • s).to_finset = s.to_finset
| 0 h := by contradiction
| (n+1) h :=
begin
by_cases n = 0,
{ rw [h, zero_add, one_nsmul] },
{ rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] }
end
@[simp] lemma to_finset_inter (s t : multiset α) :
to_finset (s ∩ t) = to_finset s ∩ to_finset t :=
finset.ext $ by simp
@[simp] lemma to_finset_union (s t : multiset α) :
(s ∪ t).to_finset = s.to_finset ∪ t.to_finset :=
by ext; simp
theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 :=
finset.val_inj.symm.trans multiset.erase_dup_eq_zero
@[simp] lemma to_finset_subset (m1 m2 : multiset α) :
m1.to_finset ⊆ m2.to_finset ↔ m1 ⊆ m2 :=
by simp only [finset.subset_iff, multiset.subset_iff, multiset.mem_to_finset]
end multiset
namespace finset
@[simp] lemma val_to_finset [decidable_eq α] (s : finset α) : s.val.to_finset = s :=
by { ext, rw [multiset.mem_to_finset, ←mem_def] }
end finset
namespace list
variable [decidable_eq α]
/-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/
def to_finset (l : list α) : finset α := multiset.to_finset l
@[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl
theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset :=
multiset.to_finset_eq n
@[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l :=
mem_erase_dup
@[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ :=
rfl
@[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) :=
finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h]
lemma to_finset_surj_on : set.surj_on to_finset {l : list α | l.nodup} set.univ :=
begin
rintro s -,
cases s with t hl, induction t using quot.ind with l,
refine ⟨l, hl, (to_finset_eq hl).symm⟩
end
theorem to_finset_surjective : surjective (to_finset : list α → finset α) :=
by { intro s, rcases to_finset_surj_on (set.mem_univ s) with ⟨l, -, hls⟩, exact ⟨l, hls⟩ }
lemma to_finset_eq_iff_perm_erase_dup {l l' : list α} :
l.to_finset = l'.to_finset ↔ l.erase_dup ~ l'.erase_dup :=
by simp [finset.ext_iff, perm_ext (nodup_erase_dup _) (nodup_erase_dup _)]
lemma to_finset_eq_of_perm (l l' : list α) (h : l ~ l') :
l.to_finset = l'.to_finset :=
to_finset_eq_iff_perm_erase_dup.mpr h.erase_dup
lemma perm_of_nodup_nodup_to_finset_eq {l l' : list α} (hl : nodup l) (hl' : nodup l')
(h : l.to_finset = l'.to_finset) : l ~ l' :=
begin
rw ←multiset.coe_eq_coe,
exact multiset.nodup.to_finset_inj hl hl' h
end
@[simp] lemma to_finset_append {l l' : list α} :
to_finset (l ++ l') = l.to_finset ∪ l'.to_finset :=
begin
induction l with hd tl hl,
{ simp },
{ simp [hl] }
end
@[simp] lemma to_finset_reverse {l : list α} :
to_finset l.reverse = l.to_finset :=
to_finset_eq_of_perm _ _ (reverse_perm l)
end list
namespace finset
/-! ### map -/
section map
open function
/-- When `f` is an embedding of `α` in `β` and `s` is a finset in `α`, then `s.map f` is the image
finset in `β`. The embedding condition guarantees that there are no duplicates in the image. -/
def map (f : α ↪ β) (s : finset α) : finset β :=
⟨s.1.map f, nodup_map f.2 s.2⟩
@[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl
@[simp] theorem map_empty (f : α ↪ β) : (∅ : finset α).map f = ∅ := rfl
variables {f : α ↪ β} {s : finset α}
@[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b :=
mem_map.trans $ by simp only [exists_prop]; refl
@[simp] theorem mem_map_equiv {f : α ≃ β} {b : β} :
b ∈ s.map f.to_embedding ↔ f.symm b ∈ s :=
by { rw mem_map, exact ⟨by { rintro ⟨a, H, rfl⟩, simpa }, λ h, ⟨_, h, by simp⟩⟩ }
theorem mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s :=
mem_map_of_injective f.2
theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f :=
(mem_map' _).2
lemma apply_coe_mem_map (f : α ↪ β) (s : finset α) (x : s) : f x ∈ s.map f :=
mem_map_of_mem f x.prop
@[simp, norm_cast] theorem coe_map (f : α ↪ β) (s : finset α) : (s.map f : set β) = f '' s :=
set.ext $ λ x, mem_map.trans set.mem_image_iff_bex.symm
theorem coe_map_subset_range (f : α ↪ β) (s : finset α) : (s.map f : set β) ⊆ set.range f :=
calc ↑(s.map f) = f '' s : coe_map f s
... ⊆ set.range f : set.image_subset_range f ↑s
theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} :
s.to_finset.map f = (s.map f).to_finset :=
ext $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset]
@[simp] theorem map_refl : s.map (embedding.refl _) = s :=
ext $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right
@[simp] theorem map_cast_heq {α β} (h : α = β) (s : finset α) :
s.map (equiv.cast h).to_embedding == s :=
by { subst h, simp }
theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) :=
eq_of_veq $ by simp only [map_val, multiset.map_map]; refl
theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ :=
⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs,
λ h, by simp [subset_def, map_subset_map h]⟩
theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ :=
by simp only [subset.antisymm_iff, map_subset_map]
/-- Associate to an embedding `f` from `α` to `β` the embedding that maps a finset to its image
under `f`. -/
def map_embedding (f : α ↪ β) : finset α ↪ finset β := ⟨map f, λ s₁ s₂, map_inj.1⟩
@[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl
theorem map_filter {p : β → Prop} [decidable_pred p] :
(s.map f).filter p = (s.filter (p ∘ f)).map f :=
eq_of_veq (map_filter _ _ _)
theorem map_union [decidable_eq α] [decidable_eq β]
{f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f :=
ext $ λ _, by simp only [mem_map, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib]
theorem map_inter [decidable_eq α] [decidable_eq β]
{f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f :=
ext $ λ b, by simp only [mem_map, mem_inter, exists_prop]; exact
⟨by rintro ⟨a, ⟨m₁, m₂⟩, rfl⟩; exact ⟨⟨a, m₁, rfl⟩, ⟨a, m₂, rfl⟩⟩,
by rintro ⟨⟨a, m₁, e⟩, ⟨a', m₂, rfl⟩⟩; cases f.2 e; exact ⟨_, ⟨m₁, m₂⟩, rfl⟩⟩
@[simp] theorem map_singleton (f : α ↪ β) (a : α) : map f {a} = {f a} :=
ext $ λ _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact eq_comm
@[simp] theorem map_insert [decidable_eq α] [decidable_eq β]
(f : α ↪ β) (a : α) (s : finset α) :
(insert a s).map f = insert (f a) (s.map f) :=
by simp only [insert_eq, map_union, map_singleton]
@[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ :=
⟨λ h, eq_empty_of_forall_not_mem $
λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩
lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s :=
eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _
lemma nonempty.map (h : s.nonempty) (f : α ↪ β) : (s.map f).nonempty :=
let ⟨a, ha⟩ := h in ⟨f a, (mem_map' f).mpr ha⟩
end map
lemma range_add_one' (n : ℕ) :
range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ.inj⟩) :=
by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n]
/-! ### image -/
section image
variables [decidable_eq β]
/-- `image f s` is the forward image of `s` under `f`. -/
def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset
@[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl
@[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl
variables {f : α → β} {s : finset α}
@[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b :=
by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop]
theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f :=
mem_image.2 ⟨_, h, rfl⟩
instance [can_lift β α] : can_lift (finset β) (finset α) :=
{ cond := λ s, ∀ x ∈ s, can_lift.cond α x,
coe := image can_lift.coe,
prf :=
begin
rintro ⟨⟨l⟩, hd : l.nodup⟩ hl,
lift l to list α using hl,
refine ⟨⟨l, list.nodup_of_nodup_map _ hd⟩, ext $ λ a, _⟩,
simp
end }
lemma _root_.function.injective.mem_finset_image {f : α → β} (hf : function.injective f)
{s : finset α} {x : α} :
f x ∈ s.image f ↔ x ∈ s :=
begin
refine ⟨λ h, _, finset.mem_image_of_mem f⟩,
obtain ⟨y, hy, heq⟩ := mem_image.1 h,
exact hf heq ▸ hy,
end
lemma filter_mem_image_eq_image (f : α → β) (s : finset α) (t : finset β) (h : ∀ x ∈ s, f x ∈ t) :
t.filter (λ y, y ∈ s.image f) = s.image f :=
by { ext, rw [mem_filter, mem_image],
simp only [and_imp, exists_prop, and_iff_right_iff_imp, exists_imp_distrib],
rintros x xel rfl, exact h _ xel }
lemma fiber_nonempty_iff_mem_image (f : α → β) (s : finset α) (y : β) :
(s.filter (λ x, f x = y)).nonempty ↔ y ∈ s.image f :=
by simp [finset.nonempty]
@[simp, norm_cast] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s :=
set.ext $ λ _, mem_image.trans set.mem_image_iff_bex.symm
lemma nonempty.image (h : s.nonempty) (f : α → β) : (s.image f).nonempty :=
let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩
@[simp]
lemma nonempty.image_iff (f : α → β) : (s.image f).nonempty ↔ s.nonempty :=
⟨λ ⟨y, hy⟩, let ⟨x, hx, _⟩ := mem_image.mp hy in ⟨x, hx⟩, λ h, h.image f⟩
theorem image_to_finset [decidable_eq α] {s : multiset α} :
s.to_finset.image f = (s.map f).to_finset :=
ext $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map]
theorem image_val_of_inj_on (H : set.inj_on f s) : (image f s).1 = s.1.map f :=
multiset.erase_dup_eq_self.2 (nodup_map_on H s.2)
@[simp]
theorem image_id [decidable_eq α] : s.image id = s :=
ext $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right]
@[simp] theorem image_id' [decidable_eq α] : s.image (λ x, x) = s := image_id
theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) :=
eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map]
theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f :=
by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset',
multiset.map_subset_map h]
theorem image_subset_iff {s : finset α} {t : finset β} {f : α → β} :
s.image f ⊆ t ↔ ∀ x ∈ s, f x ∈ t :=
calc s.image f ⊆ t ↔ f '' ↑s ⊆ ↑t : by norm_cast
... ↔ _ : set.image_subset_iff
theorem image_mono (f : α → β) : monotone (finset.image f) := λ _ _, image_subset_image
theorem coe_image_subset_range : ↑(s.image f) ⊆ set.range f :=
calc ↑(s.image f) = f '' ↑s : coe_image
... ⊆ set.range f : set.image_subset_range f ↑s
theorem image_filter {p : β → Prop} [decidable_pred p] :
(s.image f).filter p = (s.filter (p ∘ f)).image f :=
ext $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact
⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩,
by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩
theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) :
(s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f :=
ext $ λ _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right,
exists_or_distrib]
theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) :
(s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f :=
ext $ by simp only [mem_image, exists_prop, mem_inter]; exact λ b,
⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩,
λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩.
@[simp] theorem image_singleton (f : α → β) (a : α) : image f {a} = {f a} :=
ext $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm
@[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) :
(insert a s).image f = insert (f a) (s.image f) :=
by simp only [insert_eq, image_singleton, image_union]
@[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ :=
⟨λ h, eq_empty_of_forall_not_mem $
λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩
lemma mem_range_iff_mem_finset_range_of_mod_eq' [decidable_eq α] {f : ℕ → α} {a : α} {n : ℕ}
(hn : 0 < n) (h : ∀i, f (i % n) = f i) :
a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) :=
begin
split,
{ rintros ⟨i, hi⟩,
simp only [mem_image, exists_prop, mem_range],
exact ⟨i % n, nat.mod_lt i hn, (rfl.congr hi).mp (h i)⟩ },
{ rintro h,
simp only [mem_image, exists_prop, set.mem_range, mem_range] at *,
rcases h with ⟨i, hi, ha⟩,
use ⟨i, ha⟩ },
end
lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ}
(hn : 0 < n) (h : ∀i, f (i % n) = f i) :
a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) :=
suffices (∃i, f (i % n) = a) ↔ ∃i, i < n ∧ f ↑i = a, by simpa [h],
have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn,
iff.intro
(assume ⟨i, hi⟩,
have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'),
⟨int.to_nat (i % n),
by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩)
(assume ⟨i, hi, ha⟩,
⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩)
@[simp] lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s :=
eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self]
@[simp] lemma attach_image_coe [decidable_eq α] {s : finset α} : s.attach.image coe = s :=
finset.attach_image_val
@[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} :
attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s})
((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) :=
ext $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx)
(λ h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h)
(λ h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩),
λ _, finset.mem_attach _ _⟩
theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f :=
eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm
lemma image_const {s : finset α} (h : s.nonempty) (b : β) : s.image (λa, b) = singleton b :=
ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right,
h.bex, true_and, mem_singleton, eq_comm]
/--
Because `finset.image` requires a `decidable_eq` instances for the target type,
we can only construct a `functor finset` when working classically.
-/
instance [Π P, decidable P] : functor finset :=
{ map := λ α β f s, s.image f, }
instance [Π P, decidable P] : is_lawful_functor finset :=
{ id_map := λ α x, image_id,
comp_map := λ α β γ f g s, image_image.symm, }
/-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose
elements belong to `s`. -/
protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) :=
(s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩,
λ x y H, subtype.eq $ subtype.mk.inj H⟩
@[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} :
∀{a : subtype p}, a ∈ s.subtype p ↔ (a : α) ∈ s
| ⟨a, ha⟩ := by simp [finset.subtype, ha]
lemma subtype_eq_empty {p : α → Prop} [decidable_pred p] {s : finset α} :
s.subtype p = ∅ ↔ ∀ x, p x → x ∉ s :=
by simp [ext_iff, subtype.forall, subtype.coe_mk]; refl
/-- `s.subtype p` converts back to `s.filter p` with
`embedding.subtype`. -/
@[simp] lemma subtype_map (p : α → Prop) [decidable_pred p] :
(s.subtype p).map (embedding.subtype _) = s.filter p :=
begin
ext x,
rw mem_map,
change (∃ a : {x // p x}, ∃ H, (a : α) = x) ↔ _,
split,
{ rintros ⟨y, hy, hyval⟩,
rw [mem_subtype, hyval] at hy,
rw mem_filter,
use hy,
rw ← hyval,
use y.property },
{ intro hx,
rw mem_filter at hx,
use ⟨⟨x, hx.2⟩, mem_subtype.2 hx.1, rfl⟩ }
end
/-- If all elements of a `finset` satisfy the predicate `p`,
`s.subtype p` converts back to `s` with `embedding.subtype`. -/
lemma subtype_map_of_mem {p : α → Prop} [decidable_pred p] (h : ∀ x ∈ s, p x) :
(s.subtype p).map (embedding.subtype _) = s :=
by rw [subtype_map, filter_true_of_mem h]
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, all elements of the result have the property of
the subtype. -/
lemma property_of_mem_map_subtype {p : α → Prop} (s : finset {x // p x}) {a : α}
(h : a ∈ s.map (embedding.subtype _)) : p a :=
begin
rcases mem_map.1 h with ⟨x, hx, rfl⟩,
exact x.2
end
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, the result does not contain any value that does
not satisfy the property of the subtype. -/
lemma not_mem_map_subtype_of_not_property {p : α → Prop} (s : finset {x // p x})
{a : α} (h : ¬ p a) : a ∉ (s.map (embedding.subtype _)) :=
mt s.property_of_mem_map_subtype h
/-- If a `finset` of a subtype is converted to the main type with
`embedding.subtype`, the result is a subset of the set giving the
subtype. -/
lemma map_subtype_subset {t : set α} (s : finset t) :
↑(s.map (embedding.subtype _)) ⊆ t :=
begin
intros a ha,
rw mem_coe at ha,
convert property_of_mem_map_subtype s ha
end
lemma subset_image_iff {f : α → β}
{s : finset β} {t : set α} : ↑s ⊆ f '' t ↔ ∃s' : finset α, ↑s' ⊆ t ∧ s'.image f = s :=
begin
classical,
split, swap,
{ rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs },
intro h, induction s using finset.induction with a s has ih h,
{ refine ⟨∅, set.empty_subset _, _⟩,
convert finset.image_empty _ },
rw [finset.coe_insert, set.insert_subset] at h,
rcases ih h.2 with ⟨s', hst, hsi⟩,
rcases h.1 with ⟨x, hxt, rfl⟩,
refine ⟨insert x s', _, _⟩,
{ rw [finset.coe_insert, set.insert_subset], exact ⟨hxt, hst⟩ },
rw [finset.image_insert, hsi],
congr
end
end image
end finset
theorem multiset.to_finset_map [decidable_eq α] [decidable_eq β] (f : α → β) (m : multiset α) :
(m.map f).to_finset = m.to_finset.image f :=
finset.val_inj.1 (multiset.erase_dup_map_erase_dup_eq _ _).symm
namespace finset
/-! ### card -/
section card
/-- `card s` is the cardinality (number of elements) of `s`. -/
def card (s : finset α) : nat := s.1.card
theorem card_def (s : finset α) : s.card = s.1.card := rfl
@[simp] lemma card_mk {m nodup} : (⟨m, nodup⟩ : finset α).card = m.card := rfl
@[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl
theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t :=
multiset.card_le_of_le ∘ val_le_iff.mpr
@[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ :=
card_eq_zero.trans val_eq_zero
theorem card_pos {s : finset α} : 0 < card s ↔ s.nonempty :=
pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm
theorem card_ne_zero_of_mem {s : finset α} {a : α} (h : a ∈ s) : card s ≠ 0 :=
(not_congr card_eq_zero).2 (ne_empty_of_mem h)
theorem card_eq_one {s : finset α} : s.card = 1 ↔ ∃ a, s = {a} :=
by cases s; simp only [multiset.card_eq_one, finset.card, ← val_inj, singleton_val]
theorem card_le_one {s : finset α} : s.card ≤ 1 ↔ ∀ (a ∈ s) (b ∈ s), a = b :=
begin
rcases s.eq_empty_or_nonempty with rfl|⟨x, hx⟩, { simp },
refine (nat.succ_le_of_lt (card_pos.2 ⟨x, hx⟩)).le_iff_eq.trans (card_eq_one.trans ⟨_, _⟩),
{ rintro ⟨y, rfl⟩, simp },
{ exact λ h, ⟨x, eq_singleton_iff_unique_mem.2 ⟨hx, λ y hy, h _ hy _ hx⟩⟩ }
end
theorem card_le_one_iff {s : finset α} : s.card ≤ 1 ↔ ∀ {a b}, a ∈ s → b ∈ s → a = b :=
by { rw card_le_one, tauto }
lemma card_le_one_iff_subset_singleton [nonempty α] {s : finset α} :
s.card ≤ 1 ↔ ∃ (x : α), s ⊆ {x} :=
begin
split,
{ assume H,
by_cases h : ∃ x, x ∈ s,
{ rcases h with ⟨x, hx⟩,
refine ⟨x, λ y hy, _⟩,
rw [card_le_one.1 H y hy x hx, mem_singleton] },
{ push_neg at h,
inhabit α,
exact ⟨default α, λ y hy, (h y hy).elim⟩ } },
{ rintros ⟨x, hx⟩,
rw ← card_singleton x,
exact card_le_of_subset hx }
end
/-- A `finset` of a subsingleton type has cardinality at most one. -/
lemma card_le_one_of_subsingleton [subsingleton α] (s : finset α) : s.card ≤ 1 :=
finset.card_le_one_iff.2 $ λ _ _ _ _, subsingleton.elim _ _
theorem one_lt_card {s : finset α} : 1 < s.card ↔ ∃ (a ∈ s) (b ∈ s), a ≠ b :=
by { rw ← not_iff_not, push_neg, exact card_le_one }
lemma exists_ne_of_one_lt_card {s : finset α} (hs : 1 < s.card) (a : α) :
∃ b : α, b ∈ s ∧ b ≠ a :=
begin
obtain ⟨x, hx, y, hy, hxy⟩ := finset.one_lt_card.mp hs,
by_cases ha : y = a,
{ exact ⟨x, hx, ne_of_ne_of_eq hxy ha⟩ },
{ exact ⟨y, hy, ha⟩ },
end
lemma one_lt_card_iff {s : finset α} :
1 < s.card ↔ ∃ x y, (x ∈ s) ∧ (y ∈ s) ∧ x ≠ y :=
by { rw one_lt_card, simp only [exists_prop, exists_and_distrib_left] }
@[simp] theorem card_insert_of_not_mem [decidable_eq α]
{a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 :=
by simpa only [card_cons, card, insert_val] using
congr_arg multiset.card (ndinsert_of_not_mem h)
theorem card_insert_of_mem [decidable_eq α] {a : α} {s : finset α}
(h : a ∈ s) : card (insert a s) = card s := by rw insert_eq_of_mem h
theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 :=
by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right},
rw [card_insert_of_not_mem h]]
@[simp] theorem card_singleton (a : α) : card ({a} : finset α) = 1 := card_singleton _
lemma card_singleton_inter [decidable_eq α] {x : α} {s : finset α} : ({x} ∩ s).card ≤ 1 :=
begin
cases (finset.decidable_mem x s),
{ simp [finset.singleton_inter_of_not_mem h] },
{ simp [finset.singleton_inter_of_mem h] },
end
@[simp]
theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} :
a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem
theorem card_erase_lt_of_mem [decidable_eq α] {a : α} {s : finset α} :
a ∈ s → card (erase s a) < card s := card_erase_lt_of_mem
theorem card_erase_le [decidable_eq α] {a : α} {s : finset α} :
card (erase s a) ≤ card s := card_erase_le
theorem pred_card_le_card_erase [decidable_eq α] {a : α} {s : finset α} :
card s - 1 ≤ card (erase s a) :=
begin
by_cases h : a ∈ s,
{ rw [card_erase_of_mem h], refl },
{ rw [erase_eq_of_not_mem h], apply nat.sub_le }
end
/-- If `a ∈ s` is known, see also `finset.card_erase_of_mem` and `finset.erase_eq_of_not_mem`. -/
theorem card_erase_eq_ite [decidable_eq α] {a : α} {s : finset α} :
card (erase s a) = if a ∈ s then pred (card s) else card s := card_erase_eq_ite
@[simp] theorem card_range (n : ℕ) : card (range n) = n := card_range n
@[simp] theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach
end card
end finset
theorem multiset.to_finset_card_le [decidable_eq α] (m : multiset α) : m.to_finset.card ≤ m.card :=
card_le_of_le (erase_dup_le _)
lemma list.card_to_finset [decidable_eq α] (l : list α) :
finset.card l.to_finset = l.erase_dup.length := rfl
theorem list.to_finset_card_le [decidable_eq α] (l : list α) : l.to_finset.card ≤ l.length :=
multiset.to_finset_card_le ⟦l⟧
namespace finset
section card
theorem card_image_le [decidable_eq β] {f : α → β} {s : finset α} : card (image f s) ≤ card s :=
by simpa only [card_map] using (s.1.map f).to_finset_card_le
theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α}
(H : set.inj_on f s) : card (image f s) = card s :=
by simp only [card, image_val_of_inj_on H, card_map]
theorem inj_on_of_card_image_eq [decidable_eq β] {f : α → β} {s : finset α}
(H : card (image f s) = card s) :
set.inj_on f s :=
begin
change (s.1.map f).erase_dup.card = s.1.card at H,
have : (s.1.map f).erase_dup = s.1.map f,
{ apply multiset.eq_of_le_of_card_le,
{ apply multiset.erase_dup_le },
rw H,
simp only [multiset.card_map] },
rw multiset.erase_dup_eq_self at this,
apply inj_on_of_nodup_map this,
end
theorem card_image_eq_iff_inj_on [decidable_eq β] {f : α → β} {s : finset α} :
(s.image f).card = s.card ↔ set.inj_on f s :=
⟨inj_on_of_card_image_eq, card_image_of_inj_on⟩
theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α)
(H : injective f) : card (image f s) = card s :=
card_image_of_inj_on $ λ x _ y _ h, H h
lemma fiber_card_ne_zero_iff_mem_image (s : finset α) (f : α → β) [decidable_eq β] (y : β) :
(s.filter (λ x, f x = y)).card ≠ 0 ↔ y ∈ s.image f :=
by { rw [←pos_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image] }
@[simp] lemma card_map {α β} (f : α ↪ β) {s : finset α} : (s.map f).card = s.card :=
multiset.card_map _ _
@[simp] lemma card_subtype (p : α → Prop) [decidable_pred p] (s : finset α) :
(s.subtype p).card = (s.filter p).card :=
by simp [finset.subtype]
lemma card_eq_of_bijective {s : finset α} {n : ℕ}
(f : ∀i, i < n → α)
(hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s)
(f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) :
card s = n :=
begin
classical,
have : ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a,
from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩,
assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩,
have : s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)),
by simpa only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and],
calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) :
by rw [this]
... = card ((range n).attach) :
card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq,
subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq
... = card (range n) : card_attach
... = n : card_range n
end
lemma card_eq_succ [decidable_eq α] {s : finset α} {n : ℕ} :
s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) :=
iff.intro
(assume eq,
have 0 < card s, from eq.symm ▸ nat.zero_lt_succ _,
let ⟨a, has⟩ := card_pos.mp this in
⟨a, s.erase a, s.not_mem_erase a, insert_erase has,
by simp only [eq, card_erase_of_mem has, pred_succ]⟩)
(assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat)
theorem card_filter_le (s : finset α) (p : α → Prop) [decidable_pred p] :
card (s.filter p) ≤ card s :=
card_le_of_subset $ filter_subset _ _
theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t :=
eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂
lemma filter_card_eq {s : finset α} {p : α → Prop} [decidable_pred p]
(h : (s.filter p).card = s.card) (x : α) (hx : x ∈ s) : p x :=
begin
rw [←eq_of_subset_of_card_le (s.filter_subset p) h.ge, mem_filter] at hx,
exact hx.2,
end
lemma card_lt_card {s t : finset α} (h : s ⊂ t) : s.card < t.card :=
card_lt_of_lt (val_lt_iff.2 h)
lemma card_le_card_of_inj_on {s : finset α} {t : finset β}
(f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) :
card s ≤ card t :=
begin
classical,
calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj]
... ≤ card t : card_le_of_subset $ image_subset_iff.2 hf
end
/--
If there are more pigeons than pigeonholes, then there are two pigeons
in the same pigeonhole.
-/
lemma exists_ne_map_eq_of_card_lt_of_maps_to {s : finset α} {t : finset β} (hc : t.card < s.card)
{f : α → β} (hf : ∀ a ∈ s, f a ∈ t) :
∃ (x ∈ s) (y ∈ s), x ≠ y ∧ f x = f y :=
begin
classical, by_contra hz, push_neg at hz,
refine hc.not_le (card_le_card_of_inj_on f hf _),
intros x hx y hy, contrapose, exact hz x hx y hy,
end
lemma le_card_of_inj_on_range {n} {s : finset α}
(f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀ (i<n) (j<n), f i = f j → i = j) : n ≤ card s :=
calc n = card (range n) : (card_range n).symm
... ≤ card s : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simpa only [mem_range])
/-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to
define an object on `s`. Then one can inductively define an object on all finsets, starting from
the empty set and iterating. This can be used either to define data, or to prove properties. -/
def strong_induction {p : finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) :
∀ (s : finset α), p s
| s := H s (λ t h, have card t < card s, from card_lt_card h, strong_induction t)
using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf card⟩]}
lemma strong_induction_eq {p : finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) (s : finset α) :
strong_induction H s = H s (λ t h, strong_induction H t) :=
by rw strong_induction
/-- Analogue of `strong_induction` with order of arguments swapped. -/
@[elab_as_eliminator] def strong_induction_on {p : finset α → Sort*} :
∀ (s : finset α), (∀s, (∀ t ⊂ s, p t) → p s) → p s :=
λ s H, strong_induction H s
lemma strong_induction_on_eq {p : finset α → Sort*} (s : finset α) (H : ∀ s, (∀ t ⊂ s, p t) → p s) :
s.strong_induction_on H = H s (λ t h, t.strong_induction_on H) :=
by { dunfold strong_induction_on, rw strong_induction }
@[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop}
(s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀ t ⊆ s, p t) → p (insert a s)) : p s :=
finset.strong_induction_on s $ λ s,
finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $
λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _)
/-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than
`n`, one knows how to define `p s`. Then one can inductively define `p s` for all finsets `s` of
cardinality less than `n`, starting from finsets of card `n` and iterating. This
can be used either to define data, or to prove properties. -/
def strong_downward_induction {p : finset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : finset α},
t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) :
∀ (s : finset α), s.card ≤ n → p s
| s := H s (λ t ht h, have n - card t < n - card s,
from (nat.sub_lt_sub_left_iff ht).2 (finset.card_lt_card h),
strong_downward_induction t ht)
using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ (t : finset α), n - t.card)⟩]}
lemma strong_downward_induction_eq {p : finset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : finset α},
t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) (s : finset α) :
strong_downward_induction H s = H s (λ t ht hst, strong_downward_induction H t ht) :=
by rw strong_downward_induction
/-- Analogue of `strong_downward_induction` with order of arguments swapped. -/
@[elab_as_eliminator] def strong_downward_induction_on {p : finset α → Sort*} {n : ℕ} :
∀ (s : finset α), (∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁)
→ s.card ≤ n → p s :=
λ s H, strong_downward_induction H s
lemma strong_downward_induction_on_eq {p : finset α → Sort*} (s : finset α) {n : ℕ} (H : ∀ t₁,
(∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) :
s.strong_downward_induction_on H = H s (λ t ht h, t.strong_downward_induction_on H ht) :=
by { dunfold strong_downward_induction_on, rw strong_downward_induction }
lemma card_congr {s : finset α} {t : finset β} (f : Π a ∈ s, β)
(h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b)
(h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.card = t.card :=
by haveI := classical.prop_decidable; exact
calc s.card = s.attach.card : card_attach.symm
... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card :
eq.symm (card_image_of_injective _ (λ a b h, subtype.eq (h₂ _ _ _ _ h)))
... = t.card : congr_arg card (finset.ext $ λ b,
⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _,
λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩)
lemma card_union_add_card_inter [decidable_eq α] (s t : finset α) :
(s ∪ t).card + (s ∩ t).card = s.card + t.card :=
finset.induction_on t (by simp) $ λ a r har, by by_cases a ∈ s; simp *; cc
lemma card_union_le [decidable_eq α] (s t : finset α) :
(s ∪ t).card ≤ s.card + t.card :=
card_union_add_card_inter s t ▸ nat.le_add_right _ _
lemma card_union_eq [decidable_eq α] {s t : finset α} (h : disjoint s t) :
(s ∪ t).card = s.card + t.card :=
begin
rw [← card_union_add_card_inter],
convert (add_zero _).symm, rw [card_eq_zero], rwa [disjoint_iff] at h
end
lemma surj_on_of_inj_on_of_card_le {s : finset α} {t : finset β}
(f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t)
(hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂)
(hst : card t ≤ card s) :
(∀ b ∈ t, ∃ a ha, b = f a ha) :=
by haveI := classical.dec_eq β; exact
λ b hb,
have h : card (image (λ (a : {a // a ∈ s}), f a a.prop) (attach s)) = card s,
from @card_attach _ s ▸ card_image_of_injective _
(λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h),
have h₁ : image (λ a : {a // a ∈ s}, f a a.prop) s.attach = t :=
eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in
ha₂ ▸ hf _ _) (by simp [hst, h]),
begin
rw ← h₁ at hb,
rcases mem_image.1 hb with ⟨a, ha₁, ha₂⟩,
exact ⟨a, a.2, ha₂.symm⟩,
end
open function
lemma inj_on_of_surj_on_of_card_le {s : finset α} {t : finset β}
(f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t)
(hsurj : ∀ b ∈ t, ∃ a ha, b = f a ha)
(hst : card s ≤ card t)
⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s) (ha₂ : a₂ ∈ s)
(ha₁a₂: f a₁ ha₁ = f a₂ ha₂) : a₁ = a₂ :=
by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, ha₁⟩⟩; exact
let f' : {x // x ∈ s} → {x // x ∈ t} := λ x, ⟨f x.1 x.2, hf x.1 x.2⟩ in
let g : {x // x ∈ t} → {x // x ∈ s} :=
@surj_inv _ _ f'
(λ x, let ⟨y, hy₁, hy₂⟩ := hsurj x.1 x.2 in ⟨⟨y, hy₁⟩, subtype.eq hy₂.symm⟩) in
have hg : injective g, from injective_surj_inv _,
have hsg : surjective g, from λ x,
let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (λ (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x)
(λ x _, show (g x) ∈ s.attach, from mem_attach _ _)
(λ x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in
⟨y, hy.snd.symm⟩,
have hif : injective f',
from (left_inverse_of_surjective_of_right_inverse hsg
(right_inverse_surj_inv _)).injective,
subtype.ext_iff_val.1 (@hif ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ (subtype.eq ha₁a₂))
end card
section to_list
/-- Produce a list of the elements in the finite set using choice. -/
@[reducible] noncomputable def to_list (s : finset α) : list α := s.1.to_list
lemma nodup_to_list (s : finset α) : s.to_list.nodup :=
by { rw [to_list, ←multiset.coe_nodup, multiset.coe_to_list], exact s.nodup }
@[simp] lemma mem_to_list {a : α} (s : finset α) : a ∈ s.to_list ↔ a ∈ s :=
by { rw [to_list, ←multiset.mem_coe, multiset.coe_to_list], exact iff.rfl }
@[simp] lemma length_to_list (s : finset α) : s.to_list.length = s.card :=
by { rw [to_list, ←multiset.coe_card, multiset.coe_to_list], refl }
@[simp] lemma to_list_empty : (∅ : finset α).to_list = [] :=
by simp [to_list]
@[simp, norm_cast]
lemma coe_to_list (s : finset α) : (s.to_list : multiset α) = s.val :=
by { classical, ext, simp }
@[simp] lemma to_list_to_finset [decidable_eq α] (s : finset α) : s.to_list.to_finset = s :=
by { ext, simp }
lemma exists_list_nodup_eq [decidable_eq α] (s : finset α) :
∃ (l : list α), l.nodup ∧ l.to_finset = s :=
⟨s.to_list, s.nodup_to_list, s.to_list_to_finset⟩
end to_list
section bUnion
/-!
### bUnion
This section is about the bounded union of an indexed family `t : α → finset β` of finite sets
over a finite set `s : finset α`.
-/
variables [decidable_eq β] {s : finset α} {t : α → finset β}
/-- `bUnion s t` is the union of `t x` over `x ∈ s`.
(This was formerly `bind` due to the monad structure on types with `decidable_eq`.) -/
protected def bUnion (s : finset α) (t : α → finset β) : finset β :=
(s.1.bind (λ a, (t a).1)).to_finset
@[simp] theorem bUnion_val (s : finset α) (t : α → finset β) :
(s.bUnion t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl
@[simp] theorem bUnion_empty : finset.bUnion ∅ t = ∅ := rfl
@[simp] theorem mem_bUnion {b : β} : b ∈ s.bUnion t ↔ ∃a∈s, b ∈ t a :=
by simp only [mem_def, bUnion_val, mem_erase_dup, mem_bind, exists_prop]
@[simp] theorem bUnion_insert [decidable_eq α] {a : α} : (insert a s).bUnion t = t a ∪ s.bUnion t :=
ext $ λ x, by simp only [mem_bUnion, exists_prop, mem_union, mem_insert,
or_and_distrib_right, exists_or_distrib, exists_eq_left]
-- ext $ λ x, by simp [or_and_distrib_right, exists_or_distrib]
@[simp] lemma singleton_bUnion {a : α} : finset.bUnion {a} t = t a :=
begin
classical,
rw [← insert_emptyc_eq, bUnion_insert, bUnion_empty, union_empty]
end
theorem bUnion_inter (s : finset α) (f : α → finset β) (t : finset β) :
s.bUnion f ∩ t = s.bUnion (λ x, f x ∩ t) :=
begin
ext x,
simp only [mem_bUnion, mem_inter],
tauto
end
theorem inter_bUnion (t : finset β) (s : finset α) (f : α → finset β) :
t ∩ s.bUnion f = s.bUnion (λ x, t ∩ f x) :=
by rw [inter_comm, bUnion_inter]; simp [inter_comm]
theorem image_bUnion [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} :
(s.image f).bUnion t = s.bUnion (λa, t (f a)) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s rfl (λ a s has ih,
by simp only [image_insert, bUnion_insert, ih])
theorem bUnion_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} :
(s.bUnion t).image f = s.bUnion (λa, (t a).image f) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s rfl (λ a s has ih,
by simp only [bUnion_insert, image_union, ih])
lemma bUnion_bUnion [decidable_eq γ] (s : finset α) (f : α → finset β) (g : β → finset γ) :
(s.bUnion f).bUnion g = s.bUnion (λ a, (f a).bUnion g) :=
begin
ext,
simp only [finset.mem_bUnion, exists_prop],
simp_rw [←exists_and_distrib_right, ←exists_and_distrib_left, and_assoc],
rw exists_comm,
end
theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) :
(s.bind t).to_finset = s.to_finset.bUnion (λa, (t a).to_finset) :=
ext $ λ x, by simp only [multiset.mem_to_finset, mem_bUnion, multiset.mem_bind, exists_prop]
lemma bUnion_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bUnion t₁ ⊆ s.bUnion t₂ :=
have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a),
from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩,
by simpa only [subset_iff, mem_bUnion, exists_imp_distrib, and_imp, exists_prop]
lemma bUnion_subset_bUnion_of_subset_left {α : Type*} {s₁ s₂ : finset α}
(t : α → finset β) (h : s₁ ⊆ s₂) : s₁.bUnion t ⊆ s₂.bUnion t :=
begin
intro x,
simp only [and_imp, mem_bUnion, exists_prop],
exact Exists.imp (λ a ha, ⟨h ha.1, ha.2⟩)
end
lemma subset_bUnion_of_mem {s : finset α}
(u : α → finset β) {x : α} (xs : x ∈ s) :
u x ⊆ s.bUnion u :=
begin
apply subset.trans _ (bUnion_subset_bUnion_of_subset_left u (singleton_subset_iff.2 xs)),
exact subset_of_eq singleton_bUnion.symm,
end
@[simp] lemma bUnion_subset_iff_forall_subset {α β : Type*} [decidable_eq β]
{s : finset α} {t : finset β} {f : α → finset β} : s.bUnion f ⊆ t ↔ ∀ x ∈ s, f x ⊆ t :=
⟨λ h x hx, (subset_bUnion_of_mem f hx).trans h,
λ h x hx, let ⟨a, ha₁, ha₂⟩ := mem_bUnion.mp hx in h _ ha₁ ha₂⟩
lemma bUnion_singleton {f : α → β} : s.bUnion (λa, {f a}) = s.image f :=
ext $ λ x, by simp only [mem_bUnion, mem_image, mem_singleton, eq_comm]
@[simp] lemma bUnion_singleton_eq_self [decidable_eq α] :
s.bUnion (singleton : α → finset α) = s :=
by { rw bUnion_singleton, exact image_id }
lemma bUnion_filter_eq_of_maps_to [decidable_eq α] {s : finset α} {t : finset β} {f : α → β}
(h : ∀ x ∈ s, f x ∈ t) :
t.bUnion (λa, s.filter $ (λc, f c = a)) = s :=
ext $ λ b, by simpa using h b
lemma image_bUnion_filter_eq [decidable_eq α] (s : finset β) (g : β → α) :
(s.image g).bUnion (λa, s.filter $ (λc, g c = a)) = s :=
bUnion_filter_eq_of_maps_to (λ x, mem_image_of_mem g)
lemma erase_bUnion (f : α → finset β) (s : finset α) (b : β) :
(s.bUnion f).erase b = s.bUnion (λ x, (f x).erase b) :=
by { ext, simp only [finset.mem_bUnion, iff_self, exists_and_distrib_left, finset.mem_erase] }
@[simp] lemma bUnion_nonempty : (s.bUnion t).nonempty ↔ ∃ x ∈ s, (t x).nonempty :=
by simp [finset.nonempty, ← exists_and_distrib_left, @exists_swap α]
lemma nonempty.bUnion (hs : s.nonempty) (ht : ∀ x ∈ s, (t x).nonempty) :
(s.bUnion t).nonempty :=
bUnion_nonempty.2 $ hs.imp $ λ x hx, ⟨hx, ht x hx⟩
end bUnion
/-! ### prod -/
section prod
variables {s : finset α} {t : finset β}
/-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/
protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩
@[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl
@[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product
theorem subset_product [decidable_eq α] [decidable_eq β] {s : finset (α × β)} :
s ⊆ (s.image prod.fst).product (s.image prod.snd) :=
λ p hp, mem_product.2 ⟨mem_image_of_mem _ hp, mem_image_of_mem _ hp⟩
theorem product_eq_bUnion [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) :
s.product t = s.bUnion (λa, t.image $ λb, (a, b)) :=
ext $ λ ⟨x, y⟩, by simp only [mem_product, mem_bUnion, mem_image, exists_prop, prod.mk.inj_iff,
and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left]
@[simp] lemma product_bUnion {β γ : Type*} [decidable_eq γ]
(s : finset α) (t : finset β) (f : α × β → finset γ) :
(s.product t).bUnion f = s.bUnion (λ a, t.bUnion (λ b, f (a, b))) :=
by { classical, simp_rw [product_eq_bUnion, bUnion_bUnion, image_bUnion] }
@[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t :=
multiset.card_product _ _
theorem filter_product (p : α → Prop) (q : β → Prop) [decidable_pred p] [decidable_pred q] :
(s.product t).filter (λ (x : α × β), p x.1 ∧ q x.2) = (s.filter p).product (t.filter q) :=
by { ext ⟨a, b⟩, simp only [mem_filter, mem_product], finish, }
lemma filter_product_card (s : finset α) (t : finset β)
(p : α → Prop) (q : β → Prop) [decidable_pred p] [decidable_pred q] :
((s.product t).filter (λ (x : α × β), p x.1 ↔ q x.2)).card =
(s.filter p).card * (t.filter q).card + (s.filter (not ∘ p)).card * (t.filter (not ∘ q)).card :=
begin
classical,
rw [← card_product, ← card_product, ← filter_product, ← filter_product, ← card_union_eq],
{ apply congr_arg, ext ⟨a, b⟩, simp only [filter_union_right, mem_filter, mem_product],
split; intros; finish, },
{ rw disjoint_iff, change _ ∩ _ = ∅, ext ⟨a, b⟩, rw mem_inter, finish, },
end
lemma empty_product (t : finset β) :
(∅ : finset α).product t = ∅ :=
rfl
lemma product_empty (s : finset α) :
s.product (∅ : finset β) = ∅ :=
eq_empty_of_forall_not_mem (λ x h, (finset.mem_product.1 h).2)
end prod
/-! ### sigma -/
section sigma
variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)}
/-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/
protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) :=
⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩
@[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma
@[simp] theorem sigma_nonempty : (s.sigma t).nonempty ↔ ∃ x ∈ s, (t x).nonempty :=
by simp [finset.nonempty]
@[simp] theorem sigma_eq_empty : s.sigma t = ∅ ↔ ∀ x ∈ s, t x = ∅ :=
by simp only [← not_nonempty_iff_eq_empty, sigma_nonempty, not_exists]
@[mono] theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)}
(H1 : s₁ ⊆ s₂) (H2 : ∀a, t₁ a ⊆ t₂ a) : s₁.sigma t₁ ⊆ s₂.sigma t₂ :=
λ ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩
theorem sigma_eq_bUnion [decidable_eq (Σ a, σ a)] (s : finset α)
(t : Πa, finset (σ a)) :
s.sigma t = s.bUnion (λa, (t a).map $ embedding.sigma_mk a) :=
by { ext ⟨x, y⟩, simp [and.left_comm] }
end sigma
/-! ### disjoint -/
section disjoint
variable [decidable_eq α]
theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t :=
by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and,
and_imp]; refl
theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 :=
disjoint_left
theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ :=
disjoint_iff
instance decidable_disjoint (U V : finset α) : decidable (disjoint U V) :=
decidable_of_decidable_of_iff (by apply_instance) eq_bot_iff
theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s :=
by rw [disjoint.comm, disjoint_left]
theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b :=
by simp only [disjoint_left, imp_not_comm, forall_eq']
theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t :=
disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁))
theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t :=
disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁))
@[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left
@[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right
@[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s :=
by simp only [disjoint_left, mem_singleton, forall_eq]
@[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s :=
disjoint.comm.trans singleton_disjoint
@[simp] theorem disjoint_insert_left {a : α} {s t : finset α} :
disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t :=
by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq]
@[simp] theorem disjoint_insert_right {a : α} {s t : finset α} :
disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t :=
disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm]
@[simp] theorem disjoint_union_left {s t u : finset α} :
disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u :=
by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_union_right {s t u : finset α} :
disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u :=
by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib]
lemma sdiff_disjoint {s t : finset α} : disjoint (t \ s) s :=
disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2
lemma disjoint_sdiff {s t : finset α} : disjoint s (t \ s) :=
sdiff_disjoint.symm
lemma disjoint_sdiff_inter (s t : finset α) : disjoint (s \ t) (s ∩ t) :=
disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint
lemma sdiff_eq_self_iff_disjoint {s t : finset α} : s \ t = s ↔ disjoint s t :=
by rw [sdiff_eq_self, subset_empty, disjoint_iff_inter_eq_empty]
lemma sdiff_eq_self_of_disjoint {s t : finset α} (h : disjoint s t) : s \ t = s :=
sdiff_eq_self_iff_disjoint.2 h
lemma disjoint_self_iff_empty (s : finset α) : disjoint s s ↔ s = ∅ :=
disjoint_self
lemma disjoint_bUnion_left {ι : Type*}
(s : finset ι) (f : ι → finset α) (t : finset α) :
disjoint (s.bUnion f) t ↔ (∀i∈s, disjoint (f i) t) :=
begin
classical,
refine s.induction _ _,
{ simp only [forall_mem_empty_iff, bUnion_empty, disjoint_empty_left] },
{ assume i s his ih,
simp only [disjoint_union_left, bUnion_insert, his, forall_mem_insert, ih] }
end
lemma disjoint_bUnion_right {ι : Type*}
(s : finset α) (t : finset ι) (f : ι → finset α) :
disjoint s (t.bUnion f) ↔ (∀i∈t, disjoint s (f i)) :=
by simpa only [disjoint.comm] using disjoint_bUnion_left t f s
@[simp] theorem card_disjoint_union {s t : finset α} (h : disjoint s t) :
card (s ∪ t) = card s + card t :=
by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero]
theorem card_sdiff {s t : finset α} (h : s ⊆ t) : card (t \ s) = card t - card s :=
suffices card (t \ s) = card ((t \ s) ∪ s) - card s, by rwa sdiff_union_of_subset h at this,
by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel]
lemma card_sdiff_add_card {s t : finset α} : (s \ t).card + t.card = (s ∪ t).card :=
by rw [← card_disjoint_union sdiff_disjoint, sdiff_union_self_eq_union]
lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] :
disjoint (s.filter p) (s.filter q) ↔ (∀ x ∈ s, p x → ¬ q x) :=
by split; simp [disjoint_left] {contextual := tt}
lemma disjoint_filter_filter {s t : finset α} {p q : α → Prop} [decidable_pred p]
[decidable_pred q] :
(disjoint s t) → disjoint (s.filter p) (t.filter q) :=
disjoint.mono (filter_subset _ _) (filter_subset _ _)
lemma disjoint_iff_disjoint_coe {α : Type*} {a b : finset α} [decidable_eq α] :
disjoint a b ↔ disjoint (↑a : set α) (↑b : set α) :=
by { rw [finset.disjoint_left, set.disjoint_left], refl }
lemma filter_card_add_filter_neg_card_eq_card {α : Type*} {s : finset α} (p : α → Prop)
[decidable_pred p] :
(s.filter p).card + (s.filter (not ∘ p)).card = s.card :=
by { classical, simp [← card_union_eq, filter_union_filter_neg_eq, disjoint_filter], }
end disjoint
section self_prod
variables (s : finset α) [decidable_eq α]
/-- Given a finite set `s`, the diagonal, `s.diag` is the set of pairs of the form `(a, a)` for
`a ∈ s`. -/
def diag := (s.product s).filter (λ (a : α × α), a.fst = a.snd)
/-- Given a finite set `s`, the off-diagonal, `s.off_diag` is the set of pairs `(a, b)` with `a ≠ b`
for `a, b ∈ s`. -/
def off_diag := (s.product s).filter (λ (a : α × α), a.fst ≠ a.snd)
@[simp] lemma mem_diag (x : α × α) : x ∈ s.diag ↔ x.1 ∈ s ∧ x.1 = x.2 :=
by { simp only [diag, mem_filter, mem_product], split; intros; finish, }
@[simp] lemma mem_off_diag (x : α × α) : x ∈ s.off_diag ↔ x.1 ∈ s ∧ x.2 ∈ s ∧ x.1 ≠ x.2 :=
by { simp only [off_diag, mem_filter, mem_product], split; intros; finish, }
@[simp] lemma diag_card : (diag s).card = s.card :=
begin
suffices : diag s = s.image (λ a, (a, a)), { rw this, apply card_image_of_inj_on, finish, },
ext ⟨a₁, a₂⟩, rw mem_diag, split; intros; finish,
end
@[simp] lemma off_diag_card : (off_diag s).card = s.card * s.card - s.card :=
begin
suffices : (diag s).card + (off_diag s).card = s.card * s.card,
{ nth_rewrite 2 ← s.diag_card, finish, },
rw ← card_product,
apply filter_card_add_filter_neg_card_eq_card,
end
end self_prod
/--
Given a set A and a set B inside it, we can shrink A to any appropriate size, and keep B
inside it.
-/
lemma exists_intermediate_set {A B : finset α} (i : ℕ)
(h₁ : i + card B ≤ card A) (h₂ : B ⊆ A) :
∃ (C : finset α), B ⊆ C ∧ C ⊆ A ∧ card C = i + card B :=
begin
classical,
rcases nat.le.dest h₁ with ⟨k, _⟩,
clear h₁,
induction k with k ih generalizing A,
{ exact ⟨A, h₂, subset.refl _, h.symm⟩ },
{ have : (A \ B).nonempty,
{ rw [← card_pos, card_sdiff h₂, ← h, nat.add_right_comm,
nat.add_sub_cancel, nat.add_succ],
apply nat.succ_pos },
rcases this with ⟨a, ha⟩,
have z : i + card B + k = card (erase A a),
{ rw [card_erase_of_mem, ← h, nat.add_succ, nat.pred_succ],
rw mem_sdiff at ha,
exact ha.1 },
rcases ih _ z with ⟨B', hB', B'subA', cards⟩,
{ exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ },
{ rintros t th,
apply mem_erase_of_ne_of_mem _ (h₂ th),
rintro rfl,
exact not_mem_sdiff_of_mem_right th ha } }
end
/-- We can shrink A to any smaller size. -/
lemma exists_smaller_set (A : finset α) (i : ℕ) (h₁ : i ≤ card A) :
∃ (B : finset α), B ⊆ A ∧ card B = i :=
let ⟨B, _, x₁, x₂⟩ := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, x₂⟩
/-- `finset.fin_range k` is the finset `{0, 1, ..., k-1}`, as a `finset (fin k)`. -/
def fin_range (k : ℕ) : finset (fin k) :=
⟨list.fin_range k, list.nodup_fin_range k⟩
@[simp]
lemma fin_range_card {k : ℕ} : (fin_range k).card = k :=
by simp [fin_range]
@[simp]
lemma mem_fin_range {k : ℕ} (m : fin k) : m ∈ fin_range k :=
list.mem_fin_range m
@[simp] lemma coe_fin_range (k : ℕ) : (fin_range k : set (fin k)) = set.univ :=
set.eq_univ_of_forall mem_fin_range
/-- Given a finset `s` of `ℕ` contained in `{0,..., n-1}`, the corresponding finset in `fin n`
is `s.attach_fin h` where `h` is a proof that all elements of `s` are less than `n`. -/
def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) :=
⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.veq_of_eq) s.2⟩
@[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} :
a ∈ s.attach_fin h ↔ (a : ℕ) ∈ s :=
⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁,
λ h, multiset.mem_pmap.2 ⟨a, h, fin.eta _ _⟩⟩
@[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) :
(s.attach_fin h).card = s.card := multiset.card_pmap _ _ _
/-! ### choose -/
section choose
variables (p : α → Prop) [decidable_pred p] (l : finset α)
/-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of
`l` satisfying `p` this unique element, as an element of the corresponding subtype. -/
def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } :=
multiset.choose_x p l.val hp
/-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of
`l` satisfying `p` this unique element, as an element of the ambient type. -/
def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp
lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1
lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
theorem lt_wf {α} : well_founded (@has_lt.lt (finset α) _) :=
have H : subrelation (@has_lt.lt (finset α) _)
(inv_image (<) card),
from λ x y hxy, card_lt_card hxy,
subrelation.wf H $ inv_image.wf _ $ nat.lt_wf
end finset
namespace equiv
/-- Given an equivalence `α` to `β`, produce an equivalence between `finset α` and `finset β`. -/
protected def finset_congr (e : α ≃ β) : finset α ≃ finset β :=
{ to_fun := λ s, s.map e.to_embedding,
inv_fun := λ s, s.map e.symm.to_embedding,
left_inv := λ s, by simp [finset.map_map],
right_inv := λ s, by simp [finset.map_map] }
@[simp] lemma finset_congr_apply (e : α ≃ β) (s : finset α) :
e.finset_congr s = s.map e.to_embedding :=
rfl
@[simp] lemma finset_congr_refl :
(equiv.refl α).finset_congr = equiv.refl _ :=
by { ext, simp }
@[simp] lemma finset_congr_symm (e : α ≃ β) :
e.finset_congr.symm = e.symm.finset_congr :=
rfl
@[simp] lemma finset_congr_trans (e : α ≃ β) (e' : β ≃ γ) :
e.finset_congr.trans (e'.finset_congr) = (e.trans e').finset_congr :=
by { ext, simp [-finset.mem_map, -equiv.trans_to_embedding] }
end equiv
namespace multiset
variable [decidable_eq α]
theorem to_finset_card_of_nodup {l : multiset α} (h : l.nodup) : l.to_finset.card = l.card :=
congr_arg card $ multiset.erase_dup_eq_self.mpr h
lemma disjoint_to_finset {m1 m2 : multiset α} :
_root_.disjoint m1.to_finset m2.to_finset ↔ m1.disjoint m2 :=
begin
rw finset.disjoint_iff_ne,
split,
{ intro h,
intros a ha1 ha2,
rw ← multiset.mem_to_finset at ha1 ha2,
exact h _ ha1 _ ha2 rfl },
{ rintros h a ha b hb rfl,
rw multiset.mem_to_finset at ha hb,
exact h ha hb }
end
end multiset
namespace list
variable [decidable_eq α]
theorem to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length :=
multiset.to_finset_card_of_nodup h
lemma disjoint_to_finset_iff_disjoint {l l' : list α} :
_root_.disjoint l.to_finset l'.to_finset ↔ l.disjoint l' :=
multiset.disjoint_to_finset
end list
|
1bd30d0f3e0264deac9e946240d0aafd15e3a9aa | d1a52c3f208fa42c41df8278c3d280f075eb020c | /tests/lean/run/mutwf2.lean | 1ceb7ce867b9e1c3fb2a56837c616214cf2c8d41 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 347 | lean | mutual
def isEven : Nat → Bool
| 0 => true
| n+1 => isOdd n
def isOdd : Nat → Bool
| 0 => false
| n+1 => isEven n
end
termination_by measure fun
| Sum.inl n => n
| Sum.inr n => n
decreasing_by
simp [measure, invImage, InvImage, Nat.lt_wfRel]
apply Nat.lt_succ_self
#print isEven
#print isOdd
#print isEven._mutual
|
45d7977e3f25982d0d65e82401289625be96e6db | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /tests/lean/modBug.lean | 5e664c22c7dd3bd431d008edda8b3209f2501195 | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 63 | lean | theorem proofOfFalse : False := Nat.zeroNeOne (Nat.mod_zero 1)
|
f6706e939a4460a82a4d12d4f222f8a73b871b2c | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/ring_theory/mv_polynomial/symmetric.lean | d3bf7caf12f6b414d0c7374e49a005d3946dc712 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 7,836 | lean | /-
Copyright (c) 2020 Hanting Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Hanting Zhang, Johan Commelin
-/
import data.mv_polynomial.rename
import data.mv_polynomial.comm_ring
import algebra.algebra.subalgebra.basic
/-!
# Symmetric Polynomials and Elementary Symmetric Polynomials
This file defines symmetric `mv_polynomial`s and elementary symmetric `mv_polynomial`s.
We also prove some basic facts about them.
## Main declarations
* `mv_polynomial.is_symmetric`
* `mv_polynomial.symmetric_subalgebra`
* `mv_polynomial.esymm`
## Notation
+ `esymm σ R n`, is the `n`th elementary symmetric polynomial in `mv_polynomial σ R`.
As in other polynomial files, we typically use the notation:
+ `σ τ : Type*` (indexing the variables)
+ `R S : Type*` `[comm_semiring R]` `[comm_semiring S]` (the coefficients)
+ `r : R` elements of the coefficient ring
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `φ ψ : mv_polynomial σ R`
-/
open equiv (perm)
open_locale big_operators
noncomputable theory
namespace multiset
variables {R : Type*} [comm_semiring R]
/-- The `n`th elementary symmetric function evaluated at the elements of `s` -/
def esymm (s : multiset R) (n : ℕ) : R := ((s.powerset_len n).map multiset.prod).sum
lemma _root_.finset.esymm_map_val {σ} (f : σ → R) (s : finset σ) (n : ℕ) :
(s.val.map f).esymm n = (s.powerset_len n).sum (λ t, t.prod f) :=
by simpa only [esymm, powerset_len_map, ← finset.map_val_val_powerset_len, map_map]
end multiset
namespace mv_polynomial
variables {σ : Type*} {R : Type*}
variables {τ : Type*} {S : Type*}
/-- A `mv_polynomial φ` is symmetric if it is invariant under
permutations of its variables by the `rename` operation -/
def is_symmetric [comm_semiring R] (φ : mv_polynomial σ R) : Prop :=
∀ e : perm σ, rename e φ = φ
variables (σ R)
/-- The subalgebra of symmetric `mv_polynomial`s. -/
def symmetric_subalgebra [comm_semiring R] : subalgebra R (mv_polynomial σ R) :=
{ carrier := set_of is_symmetric,
algebra_map_mem' := λ r e, rename_C e r,
mul_mem' := λ a b ha hb e, by rw [alg_hom.map_mul, ha, hb],
add_mem' := λ a b ha hb e, by rw [alg_hom.map_add, ha, hb] }
variables {σ R}
@[simp] lemma mem_symmetric_subalgebra [comm_semiring R] (p : mv_polynomial σ R) :
p ∈ symmetric_subalgebra σ R ↔ p.is_symmetric := iff.rfl
namespace is_symmetric
section comm_semiring
variables [comm_semiring R] [comm_semiring S] {φ ψ : mv_polynomial σ R}
@[simp]
lemma C (r : R) : is_symmetric (C r : mv_polynomial σ R) :=
(symmetric_subalgebra σ R).algebra_map_mem r
@[simp]
lemma zero : is_symmetric (0 : mv_polynomial σ R) :=
(symmetric_subalgebra σ R).zero_mem
@[simp]
lemma one : is_symmetric (1 : mv_polynomial σ R) :=
(symmetric_subalgebra σ R).one_mem
lemma add (hφ : is_symmetric φ) (hψ : is_symmetric ψ) : is_symmetric (φ + ψ) :=
(symmetric_subalgebra σ R).add_mem hφ hψ
lemma mul (hφ : is_symmetric φ) (hψ : is_symmetric ψ) : is_symmetric (φ * ψ) :=
(symmetric_subalgebra σ R).mul_mem hφ hψ
lemma smul (r : R) (hφ : is_symmetric φ) : is_symmetric (r • φ) :=
(symmetric_subalgebra σ R).smul_mem hφ r
@[simp]
lemma map (hφ : is_symmetric φ) (f : R →+* S) : is_symmetric (map f φ) :=
λ e, by rw [← map_rename, hφ]
end comm_semiring
section comm_ring
variables [comm_ring R] {φ ψ : mv_polynomial σ R}
lemma neg (hφ : is_symmetric φ) : is_symmetric (-φ) :=
(symmetric_subalgebra σ R).neg_mem hφ
lemma sub (hφ : is_symmetric φ) (hψ : is_symmetric ψ) : is_symmetric (φ - ψ) :=
(symmetric_subalgebra σ R).sub_mem hφ hψ
end comm_ring
end is_symmetric
section elementary_symmetric
open finset
variables (σ R) [comm_semiring R] [comm_semiring S] [fintype σ] [fintype τ]
/-- The `n`th elementary symmetric `mv_polynomial σ R`. -/
def esymm (n : ℕ) : mv_polynomial σ R :=
∑ t in powerset_len n univ, ∏ i in t, X i
/-- The `n`th elementary symmetric `mv_polynomial σ R` is obtained by evaluating the
`n`th elementary symmetric at the `multiset` of the monomials -/
lemma esymm_eq_multiset_esymm : esymm σ R = (finset.univ.val.map X).esymm :=
funext $ λ n, (finset.univ.esymm_map_val X n).symm
lemma aeval_esymm_eq_multiset_esymm [algebra R S] (f : σ → S) (n : ℕ) :
aeval f (esymm σ R n) = (finset.univ.val.map f).esymm n :=
by simp_rw [esymm, aeval_sum, aeval_prod, aeval_X, esymm_map_val]
/-- We can define `esymm σ R n` by summing over a subtype instead of over `powerset_len`. -/
lemma esymm_eq_sum_subtype (n : ℕ) : esymm σ R n =
∑ t : {s : finset σ // s.card = n}, ∏ i in (t : finset σ), X i :=
sum_subtype _ (λ _, mem_powerset_len_univ_iff) _
/-- We can define `esymm σ R n` as a sum over explicit monomials -/
lemma esymm_eq_sum_monomial (n : ℕ) : esymm σ R n =
∑ t in powerset_len n univ, monomial (∑ i in t, finsupp.single i 1) 1 :=
begin
simp_rw monomial_sum_one,
refl,
end
@[simp] lemma esymm_zero : esymm σ R 0 = 1 :=
by simp only [esymm, powerset_len_zero, sum_singleton, prod_empty]
lemma map_esymm (n : ℕ) (f : R →+* S) : map f (esymm σ R n) = esymm σ S n :=
by simp_rw [esymm, map_sum, map_prod, map_X]
lemma rename_esymm (n : ℕ) (e : σ ≃ τ) : rename e (esymm σ R n) = esymm τ R n :=
calc rename e (esymm σ R n)
= ∑ x in powerset_len n univ, ∏ i in x, X (e i)
: by simp_rw [esymm, map_sum, map_prod, rename_X]
... = ∑ t in powerset_len n (univ.map e.to_embedding), ∏ i in t, X i
: by simp [finset.powerset_len_map, -finset.map_univ_equiv]
... = ∑ t in powerset_len n univ, ∏ i in t, X i : by rw finset.map_univ_equiv
lemma esymm_is_symmetric (n : ℕ) : is_symmetric (esymm σ R n) :=
by { intro, rw rename_esymm }
lemma support_esymm'' (n : ℕ) [decidable_eq σ] [nontrivial R] :
(esymm σ R n).support = (powerset_len n (univ : finset σ)).bUnion
(λ t, (finsupp.single (∑ (i : σ) in t, finsupp.single i 1) (1:R)).support) :=
begin
rw esymm_eq_sum_monomial,
simp only [← single_eq_monomial],
convert finsupp.support_sum_eq_bUnion (powerset_len n (univ : finset σ)) _,
intros s t hst,
rw finset.disjoint_left,
simp only [finsupp.support_single_ne_zero _ one_ne_zero, mem_singleton],
rintro a h rfl,
have := congr_arg finsupp.support h,
rw [finsupp.support_sum_eq_bUnion, finsupp.support_sum_eq_bUnion] at this,
{ simp only [finsupp.support_single_ne_zero _ one_ne_zero, bUnion_singleton_eq_self] at this,
exact absurd this hst.symm },
all_goals { intros x y, simp [finsupp.support_single_disjoint] }
end
lemma support_esymm' (n : ℕ) [decidable_eq σ] [nontrivial R] :
(esymm σ R n).support =
(powerset_len n (univ : finset σ)).bUnion (λ t, {∑ (i : σ) in t, finsupp.single i 1}) :=
begin
rw support_esymm'',
congr,
funext,
exact finsupp.support_single_ne_zero _ one_ne_zero
end
lemma support_esymm (n : ℕ) [decidable_eq σ] [nontrivial R] :
(esymm σ R n).support =
(powerset_len n (univ : finset σ)).image (λ t, ∑ (i : σ) in t, finsupp.single i 1) :=
by { rw support_esymm', exact bUnion_singleton }
lemma degrees_esymm [nontrivial R]
(n : ℕ) (hpos : 0 < n) (hn : n ≤ fintype.card σ) :
(esymm σ R n).degrees = (univ : finset σ).val :=
begin
classical,
have : (finsupp.to_multiset ∘ λ (t : finset σ), ∑ (i : σ) in t, finsupp.single i 1) = finset.val,
{ funext, simp [finsupp.to_multiset_sum_single] },
rw [degrees, support_esymm, sup_finset_image, this, ←comp_sup_eq_sup_comp],
{ obtain ⟨k, rfl⟩ := nat.exists_eq_succ_of_ne_zero hpos.ne',
simpa using powerset_len_sup _ _ (nat.lt_of_succ_le hn) },
{ intros,
simp only [union_val, sup_eq_union],
congr },
{ refl }
end
end elementary_symmetric
end mv_polynomial
|
67044aaa9d910f69115ebdbae6518ea3fd1d7586 | d8820d2c92be8052d13f9c8f8c483a6e15c5f566 | /src/M40002/metric_spaces.lean | 2f7b3e4d4e39915eec9b936bf9222913ee322673 | [] | no_license | JasonKYi/M4000x_LEAN_formalisation | 4a19b84f6d0fe2e214485b8532e21cd34996c4b1 | 6e99793f2fcbe88596e27644f430e46aa2a464df | refs/heads/master | 1,599,755,414,708 | 1,589,494,604,000 | 1,589,494,604,000 | 221,759,483 | 8 | 1 | null | 1,589,494,605,000 | 1,573,755,201,000 | Lean | UTF-8 | Lean | false | false | 1,040 | lean | import data.real.basic
import M40002.M40002_C5
/-
Metric spaces with reference to Chap. 2.15 of Rudin
-/
/-
class has_group_notation (G : Type) extends has_mul G, has_one G, has_inv G
-- definition of the group structure
class group (G : Type) extends has_group_notation G :=
(mul_assoc : ∀ (a b c : G), a * b * c = a * (b * c))
(one_mul : ∀ (a : G), 1 * a = a)
(mul_left_inv : ∀ (a : G), a⁻¹ * a = 1)
class comm_group (G : Type) extends group G :=
(mul_comm : ∀ a b : G, a * b = b * a)
-/
class has_metric_space_notation (M : Type) extends has_mul M
class metric_space (M : Type) :=
(distance : M → M → ℝ)
(self_distance : ∀ p : M, distance p p = 0)
(pos_distance : ∀ p q : M, p ≠ q → 0 < distance p q)
(distance_assoc : ∀ p q : M, distance p q = distance q p)
(triangle_ineq : ∀ p q r : M, distance p q ≤ distance p r + distance r q)
variable {M : Type}
-- def neighborhood (p : metric_space M) (r : ℝ) (h : 0 < r) : set (metric_space M) := {q : metric_space M | (metric_space M).distance p q < r} |
e2ba8a4c171f870e153cb8f008b0a6a3587b77fd | 957a80ea22c5abb4f4670b250d55534d9db99108 | /tests/lean/run/format.lean | b1c501ee49d8cb4fb1d0182014014a0ee9092d12 | [
"Apache-2.0"
] | permissive | GaloisInc/lean | aa1e64d604051e602fcf4610061314b9a37ab8cd | f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0 | refs/heads/master | 1,592,202,909,807 | 1,504,624,387,000 | 1,504,624,387,000 | 75,319,626 | 2 | 1 | Apache-2.0 | 1,539,290,164,000 | 1,480,616,104,000 | C++ | UTF-8 | Lean | false | false | 312 | lean | import system.io
open list
variable [io.interface]
#eval pp ([["aaa", "bbb", "ccc", "dddd", "eeeeee", "ffffff"], ["aaa", "bbb", "ccc", "dddd", "eeeeee", "ffffff"],
["aaa", "bbb", "ccc", "dddd", "eeeeee", "ffffff"], ["aaa", "bbb", "ccc", "dddd", "eeeeee", "ffffff"]],
[(10:nat), 20, 30])
|
3792a44d823b1b61bbb21564103a64bc7bb646ff | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Meta/UnificationHint.lean | 19e7247f02288a1a411cd3d4e79732b562ce4182 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 5,382 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.ScopedEnvExtension
import Lean.Util.Recognizers
import Lean.Meta.DiscrTree
import Lean.Meta.SynthInstance
namespace Lean.Meta
abbrev UnificationHintKey := DiscrTree.Key (simpleReduce := true)
structure UnificationHintEntry where
keys : Array UnificationHintKey
val : Name
deriving Inhabited
abbrev UnificationHintTree := DiscrTree Name (simpleReduce := true)
structure UnificationHints where
discrTree : UnificationHintTree := DiscrTree.empty
deriving Inhabited
instance : ToFormat UnificationHints where
format h := format h.discrTree
def UnificationHints.add (hints : UnificationHints) (e : UnificationHintEntry) : UnificationHints :=
{ hints with discrTree := hints.discrTree.insertCore e.keys e.val }
builtin_initialize unificationHintExtension : SimpleScopedEnvExtension UnificationHintEntry UnificationHints ←
registerSimpleScopedEnvExtension {
addEntry := UnificationHints.add
initial := {}
}
structure UnificationConstraint where
lhs : Expr
rhs : Expr
structure UnificationHint where
pattern : UnificationConstraint
constraints : List UnificationConstraint
private partial def decodeUnificationHint (e : Expr) : ExceptT MessageData Id UnificationHint := do
decode e #[]
where
decodeConstraint (e : Expr) : ExceptT MessageData Id UnificationConstraint :=
match e.eq? with
| some (_, lhs, rhs) => return UnificationConstraint.mk lhs rhs
| none => throw m!"invalid unification hint constraint, unexpected term{indentExpr e}"
decode (e : Expr) (cs : Array UnificationConstraint) : ExceptT MessageData Id UnificationHint := do
match e with
| Expr.forallE _ d b _ => do
let c ← decodeConstraint d
if b.hasLooseBVars then
throw m!"invalid unification hint constraint, unexpected dependency{indentExpr e}"
decode b (cs.push c)
| _ => do
let p ← decodeConstraint e
return { pattern := p, constraints := cs.toList }
private partial def validateHint (hint : UnificationHint) : MetaM Unit := do
hint.constraints.forM fun c => do
unless (← isDefEq c.lhs c.rhs) do
throwError "invalid unification hint, failed to unify constraint left-hand-side{indentExpr c.lhs}\nwith right-hand-side{indentExpr c.rhs}"
unless (← isDefEq hint.pattern.lhs hint.pattern.rhs) do
throwError "invalid unification hint, failed to unify pattern left-hand-side{indentExpr hint.pattern.lhs}\nwith right-hand-side{indentExpr hint.pattern.rhs}"
def addUnificationHint (declName : Name) (kind : AttributeKind) : MetaM Unit :=
withNewMCtxDepth do
let info ← getConstInfo declName
match info.value? with
| none => throwError "invalid unification hint, it must be a definition"
| some val =>
let (_, _, body) ← lambdaMetaTelescope val
match decodeUnificationHint body with
| Except.error msg => throwError msg
| Except.ok hint =>
let keys ← DiscrTree.mkPath hint.pattern.lhs
validateHint hint
unificationHintExtension.add { keys := keys, val := declName } kind
builtin_initialize
registerBuiltinAttribute {
name := `unification_hint
descr := "unification hint"
add := fun declName stx kind => do
Attribute.Builtin.ensureNoArgs stx
discard <| addUnificationHint declName kind |>.run
}
def tryUnificationHints (t s : Expr) : MetaM Bool := do
trace[Meta.isDefEq.hint] "{t} =?= {s}"
unless (← read).config.unificationHints do
return false
if t.isMVar then
return false
let hints := unificationHintExtension.getState (← getEnv)
let candidates ← hints.discrTree.getMatch t
for candidate in candidates do
if (← tryCandidate candidate) then
return true
return false
where
isDefEqPattern p e :=
withReducible <| Meta.isExprDefEqAux p e
tryCandidate candidate : MetaM Bool :=
withTraceNode `Meta.isDefEq.hint
(return m!"{exceptBoolEmoji ·} hint {candidate} at {t} =?= {s}") do
checkpointDefEq do
let cinfo ← getConstInfo candidate
let us ← cinfo.levelParams.mapM fun _ => mkFreshLevelMVar
let val ← instantiateValueLevelParams cinfo us
let (xs, bis, body) ← lambdaMetaTelescope val
let hint? ← withConfig (fun cfg => { cfg with unificationHints := false }) do
match decodeUnificationHint body with
| Except.error _ => return none
| Except.ok hint =>
if (← isDefEqPattern hint.pattern.lhs t <&&> isDefEqPattern hint.pattern.rhs s) then
return some hint
else
return none
match hint? with
| none => return false
| some hint =>
trace[Meta.isDefEq.hint] "{candidate} succeeded, applying constraints"
for c in hint.constraints do
unless (← Meta.isExprDefEqAux c.lhs c.rhs) do
return false
for x in xs, bi in bis do
if bi == BinderInfo.instImplicit then
match (← trySynthInstance (← inferType x)) with
| LOption.some val => unless (← isDefEq x val) do return false
| _ => return false
return true
builtin_initialize
registerTraceClass `Meta.isDefEq.hint
end Lean.Meta
|
f7ac2795b791e4a72fdd2dedb1773f9e4cb57314 | 97f752b44fd85ec3f635078a2dd125ddae7a82b6 | /library/init/reserved_notation.lean | a23ca4e2b741e09547e004f5b3ccc8ad39061ca6 | [
"Apache-2.0"
] | permissive | tectronics/lean | ab977ba6be0fcd46047ddbb3c8e16e7c26710701 | f38af35e0616f89c6e9d7e3eb1d48e47ee666efe | refs/heads/master | 1,532,358,526,384 | 1,456,276,623,000 | 1,456,276,623,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,517 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn
-/
prelude
import init.datatypes
notation `assume` binders `,` r:(scoped f, f) := r
notation `take` binders `,` r:(scoped f, f) := r
structure has_zero [class] (A : Type) := (zero : A)
structure has_one [class] (A : Type) := (one : A)
structure has_add [class] (A : Type) := (add : A → A → A)
structure has_mul [class] (A : Type) := (mul : A → A → A)
structure has_inv [class] (A : Type) := (inv : A → A)
structure has_neg [class] (A : Type) := (neg : A → A)
structure has_sub [class] (A : Type) := (sub : A → A → A)
structure has_div [class] (A : Type) := (div : A → A → A)
structure has_dvd [class] (A : Type) := (dvd : A → A → Prop)
structure has_mod [class] (A : Type) := (mod : A → A → A)
structure has_le [class] (A : Type) := (le : A → A → Prop)
structure has_lt [class] (A : Type) := (lt : A → A → Prop)
definition zero {A : Type} [s : has_zero A] : A := has_zero.zero A
definition one {A : Type} [s : has_one A] : A := has_one.one A
definition add {A : Type} [s : has_add A] : A → A → A := has_add.add
definition mul {A : Type} [s : has_mul A] : A → A → A := has_mul.mul
definition sub {A : Type} [s : has_sub A] : A → A → A := has_sub.sub
definition div {A : Type} [s : has_div A] : A → A → A := has_div.div
definition dvd {A : Type} [s : has_dvd A] : A → A → Prop := has_dvd.dvd
definition mod {A : Type} [s : has_mod A] : A → A → A := has_mod.mod
definition neg {A : Type} [s : has_neg A] : A → A := has_neg.neg
definition inv {A : Type} [s : has_inv A] : A → A := has_inv.inv
definition le {A : Type} [s : has_le A] : A → A → Prop := has_le.le
definition lt {A : Type} [s : has_lt A] : A → A → Prop := has_lt.lt
definition ge [reducible] {A : Type} [s : has_le A] (a b : A) : Prop := le b a
definition gt [reducible] {A : Type} [s : has_lt A] (a b : A) : Prop := lt b a
definition bit0 {A : Type} [s : has_add A] (a : A) : A := add a a
definition bit1 {A : Type} [s₁ : has_one A] [s₂ : has_add A] (a : A) : A := add (bit0 a) one
definition num_has_zero [reducible] [instance] : has_zero num :=
has_zero.mk num.zero
definition num_has_one [reducible] [instance] : has_one num :=
has_one.mk (num.pos pos_num.one)
definition pos_num_has_one [reducible] [instance] : has_one pos_num :=
has_one.mk (pos_num.one)
namespace pos_num
open bool
definition is_one (a : pos_num) : bool :=
pos_num.rec_on a tt (λn r, ff) (λn r, ff)
definition pred (a : pos_num) : pos_num :=
pos_num.rec_on a one (λn r, bit0 n) (λn r, bool.rec_on (is_one n) (bit1 r) one)
definition size (a : pos_num) : pos_num :=
pos_num.rec_on a one (λn r, succ r) (λn r, succ r)
definition add (a b : pos_num) : pos_num :=
pos_num.rec_on a
succ
(λn f b, pos_num.rec_on b
(succ (bit1 n))
(λm r, succ (bit1 (f m)))
(λm r, bit1 (f m)))
(λn f b, pos_num.rec_on b
(bit1 n)
(λm r, bit1 (f m))
(λm r, bit0 (f m)))
b
end pos_num
definition pos_num_has_add [reducible] [instance] : has_add pos_num :=
has_add.mk pos_num.add
namespace num
open pos_num
definition add (a b : num) : num :=
num.rec_on a b (λpa, num.rec_on b (pos pa) (λpb, pos (pos_num.add pa pb)))
end num
definition num_has_add [reducible] [instance] : has_add num :=
has_add.mk num.add
definition std.priority.default : num := 1000
definition std.priority.max : num := 4294967295
namespace nat
protected definition prio := num.add std.priority.default 100
protected definition add (a b : nat) : nat :=
nat.rec a (λ b₁ r, succ r) b
definition of_num (n : num) : nat :=
num.rec zero
(λ n, pos_num.rec (succ zero) (λ n r, nat.add (nat.add r r) (succ zero)) (λ n r, nat.add r r) n) n
end nat
attribute pos_num_has_add pos_num_has_one num_has_zero num_has_one num_has_add
[instance] [priority nat.prio]
definition nat_has_zero [reducible] [instance] [priority nat.prio] : has_zero nat :=
has_zero.mk nat.zero
definition nat_has_one [reducible] [instance] [priority nat.prio] : has_one nat :=
has_one.mk (nat.succ (nat.zero))
definition nat_has_add [reducible] [instance] [priority nat.prio] : has_add nat :=
has_add.mk nat.add
/-
Global declarations of right binding strength
If a module reassigns these, it will be incompatible with other modules that adhere to these
conventions.
When hovering over a symbol, use "C-c C-k" to see how to input it.
-/
definition std.prec.max : num := 1024 -- the strength of application, identifiers, (, [, etc.
definition std.prec.arrow : num := 25
/-
The next definition is "max + 10". It can be used e.g. for postfix operations that should
be stronger than application.
-/
definition std.prec.max_plus :=
num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ
(num.succ std.prec.max)))))))))
/- Logical operations and relations -/
reserve prefix `¬`:40
reserve prefix `~`:40
reserve infixr ` ∧ `:35
reserve infixr ` /\ `:35
reserve infixr ` \/ `:30
reserve infixr ` ∨ `:30
reserve infix ` <-> `:20
reserve infix ` ↔ `:20
reserve infix ` = `:50
reserve infix ` ≠ `:50
reserve infix ` ≈ `:50
reserve infix ` ~ `:50
reserve infix ` ≡ `:50
reserve infixr ` ∘ `:60 -- input with \comp
reserve postfix `⁻¹`:std.prec.max_plus -- input with \sy or \-1 or \inv
reserve infixl ` ⬝ `:75
reserve infixr ` ▸ `:75
reserve infixr ` ▹ `:75
/- types and type constructors -/
reserve infixl ` ⊎ `:25
reserve infixl ` × `:30
/- arithmetic operations -/
reserve infixl ` + `:65
reserve infixl ` - `:65
reserve infixl ` * `:70
reserve infixl ` / `:70
reserve infixl ` % `:70
reserve prefix `-`:100
reserve infix ` ^ `:80
reserve infix ` <= `:50
reserve infix ` ≤ `:50
reserve infix ` < `:50
reserve infix ` >= `:50
reserve infix ` ≥ `:50
reserve infix ` > `:50
/- boolean operations -/
reserve infixl ` && `:70
reserve infixl ` || `:65
/- set operations -/
reserve infix ` ∈ `:50
reserve infix ` ∉ `:50
reserve infixl ` ∩ `:70
reserve infixl ` ∪ `:65
reserve infix ` ⊆ `:50
reserve infix ` ⊇ `:50
reserve infix ` ' `:75 -- for the image of a set under a function
reserve infix ` '- `:75 -- for the preimage of a set under a function
/- other symbols -/
reserve infix ` ∣ `:50
reserve infixl ` ++ `:65
reserve infixr ` :: `:67
infix + := add
infix * := mul
infix - := sub
infix / := div
infix ∣ := dvd
infix % := mod
prefix - := neg
postfix ⁻¹ := inv
infix ≤ := le
infix ≥ := ge
infix < := lt
infix > := gt
notation [parsing_only] x ` +[`:65 A:0 `] `:0 y:65 := @add A _ x y
notation [parsing_only] x ` -[`:65 A:0 `] `:0 y:65 := @sub A _ x y
notation [parsing_only] x ` *[`:70 A:0 `] `:0 y:70 := @mul A _ x y
notation [parsing_only] x ` /[`:70 A:0 `] `:0 y:70 := @div A _ x y
notation [parsing_only] x ` ∣[`:70 A:0 `] `:0 y:70 := @dvd A _ x y
notation [parsing_only] x ` %[`:70 A:0 `] `:0 y:70 := @mod A _ x y
notation [parsing_only] x ` ≤[`:50 A:0 `] `:0 y:50 := @le A _ x y
notation [parsing_only] x ` ≥[`:50 A:0 `] `:0 y:50 := @ge A _ x y
notation [parsing_only] x ` <[`:50 A:0 `] `:0 y:50 := @lt A _ x y
notation [parsing_only] x ` >[`:50 A:0 `] `:0 y:50 := @gt A _ x y
|
70648bd39001c4ba58e5db26503947a42f95fc26 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/ring_theory/finiteness_auto.lean | f2ad7aa2e669c1e03dbb1846a03d8ff4f9f3a7e5 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,955 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.ring_theory.noetherian
import Mathlib.ring_theory.ideal.operations
import Mathlib.ring_theory.algebra_tower
import Mathlib.PostPort
universes u_1 u_4 u_2 u_5 u_3
namespace Mathlib
/-!
# Finiteness conditions in commutative algebra
In this file we define several notions of finiteness that are common in commutative algebra.
## Main declarations
- `module.finite`, `algebra.finite`, `ring_hom.finite`, `alg_hom.finite`
all of these express that some object is finitely generated *as module* over some base ring.
- `algebra.finite_type`, `ring_hom.finite_type`, `alg_hom.finite_type`
all of these express that some object is finitely generated *as algebra* over some base ring.
-/
/-- A module over a commutative ring is `finite` if it is finitely generated as a module. -/
def module.finite (R : Type u_1) (M : Type u_4) [comm_ring R] [add_comm_group M] [module R M] :=
submodule.fg ⊤
/-- An algebra over a commutative ring is of `finite_type` if it is finitely generated
over the base ring as algebra. -/
def algebra.finite_type (R : Type u_1) (A : Type u_2) [comm_ring R] [comm_ring A] [algebra R A] :=
subalgebra.fg ⊤
/-- An algebra over a commutative ring is `finitely_presented` if it is the quotient of a
polynomial ring in `n` variables by a finitely generated ideal. -/
def algebra.finitely_presented (R : Type u_1) (A : Type u_2) [comm_ring R] [comm_ring A]
[algebra R A] :=
∃ (n : ℕ),
∃ (f : alg_hom R (mv_polynomial (fin n) R) A),
function.surjective ⇑f ∧ submodule.fg (ring_hom.ker (alg_hom.to_ring_hom f))
namespace module
theorem finite_def {R : Type u_1} {M : Type u_4} [comm_ring R] [add_comm_group M] [module R M] :
finite R M ↔ submodule.fg ⊤ :=
iff.rfl
protected instance is_noetherian.finite (R : Type u_1) (M : Type u_4) [comm_ring R]
[add_comm_group M] [module R M] [is_noetherian R M] : finite R M :=
is_noetherian.noetherian ⊤
namespace finite
theorem of_surjective {R : Type u_1} {M : Type u_4} {N : Type u_5} [comm_ring R] [add_comm_group M]
[module R M] [add_comm_group N] [module R N] [hM : finite R M] (f : linear_map R M N)
(hf : function.surjective ⇑f) : finite R N :=
sorry
theorem of_injective {R : Type u_1} {M : Type u_4} {N : Type u_5} [comm_ring R] [add_comm_group M]
[module R M] [add_comm_group N] [module R N] [is_noetherian R N] (f : linear_map R M N)
(hf : function.injective ⇑f) : finite R M :=
fg_of_injective f (iff.mpr linear_map.ker_eq_bot hf)
protected instance self (R : Type u_1) [comm_ring R] : finite R R :=
Exists.intro (singleton 1)
(eq.mpr
(id
((fun (a a_1 : submodule R R) (e_1 : a = a_1) (ᾰ ᾰ_1 : submodule R R) (e_2 : ᾰ = ᾰ_1) =>
congr (congr_arg Eq e_1) e_2)
(submodule.span R ↑(singleton 1)) (submodule.span R (singleton 1))
((fun (s s_1 : set R) (e_2 : s = s_1) => congr_arg (submodule.span R) e_2)
(↑(singleton 1)) (singleton 1) (finset.coe_singleton 1))
⊤ ⊤ (Eq.refl ⊤)))
(eq.mp (Eq.refl (ideal.span 1 = ⊤)) ideal.span_singleton_one))
protected instance prod {R : Type u_1} {M : Type u_4} {N : Type u_5} [comm_ring R]
[add_comm_group M] [module R M] [add_comm_group N] [module R N] [hM : finite R M]
[hN : finite R N] : finite R (M × N) :=
eq.mpr (id (Eq._oldrec (Eq.refl (finite R (M × N))) (equations._eqn_1 R (M × N))))
(eq.mpr (id (Eq._oldrec (Eq.refl (submodule.fg ⊤)) (Eq.symm submodule.prod_top)))
(submodule.fg_prod hM hN))
theorem equiv {R : Type u_1} {M : Type u_4} {N : Type u_5} [comm_ring R] [add_comm_group M]
[module R M] [add_comm_group N] [module R N] [hM : finite R M] (e : linear_equiv R M N) :
finite R N :=
of_surjective (↑e) (linear_equiv.surjective e)
theorem trans {R : Type u_1} (A : Type u_2) (B : Type u_3) [comm_ring R] [comm_ring A] [algebra R A]
[comm_ring B] [algebra R B] [algebra A B] [is_scalar_tower R A B] [hRA : finite R A]
[hAB : finite A B] : finite R B :=
sorry
protected instance finite_type {R : Type u_1} (A : Type u_2) [comm_ring R] [comm_ring A]
[algebra R A] [hRA : finite R A] : algebra.finite_type R A :=
subalgebra.fg_of_submodule_fg hRA
end finite
end module
namespace algebra
namespace finite_type
theorem self (R : Type u_1) [comm_ring R] : finite_type R R :=
Exists.intro (singleton 1) (subsingleton.elim (adjoin R ↑(singleton 1)) ⊤)
protected theorem mv_polynomial (R : Type u_1) [comm_ring R] (ι : Type u_2) [fintype ι] :
finite_type R (mv_polynomial ι R) :=
sorry
theorem of_surjective {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A]
[algebra R A] [comm_ring B] [algebra R B] (hRA : finite_type R A) (f : alg_hom R A B)
(hf : function.surjective ⇑f) : finite_type R B :=
sorry
theorem equiv {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [algebra R A]
[comm_ring B] [algebra R B] (hRA : finite_type R A) (e : alg_equiv R A B) : finite_type R B :=
of_surjective hRA (↑e) (alg_equiv.surjective e)
theorem trans {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [algebra R A]
[comm_ring B] [algebra R B] [algebra A B] [is_scalar_tower R A B] (hRA : finite_type R A)
(hAB : finite_type A B) : finite_type R B :=
fg_trans' hRA hAB
/-- An algebra is finitely generated if and only if it is a quotient
of a polynomial ring whose variables are indexed by a finset. -/
theorem iff_quotient_mv_polynomial {R : Type u_1} {A : Type u_2} [comm_ring R] [comm_ring A]
[algebra R A] :
finite_type R A ↔
∃ (s : finset A),
∃ (f : alg_hom R (mv_polynomial (Subtype fun (x : A) => x ∈ s) R) A),
function.surjective ⇑f :=
sorry
/-- An algebra is finitely generated if and only if it is a quotient
of a polynomial ring whose variables are indexed by a fintype. -/
theorem iff_quotient_mv_polynomial' {R : Type u_1} {A : Type u_2} [comm_ring R] [comm_ring A]
[algebra R A] :
finite_type R A ↔
∃ (ι : Type u_2),
Exists (∃ (f : alg_hom R (mv_polynomial ι R) A), function.surjective ⇑f) :=
sorry
/-- An algebra is finitely generated if and only if it is a quotient of a polynomial ring in `n`
variables. -/
theorem iff_quotient_mv_polynomial'' {R : Type u_1} {A : Type u_2} [comm_ring R] [comm_ring A]
[algebra R A] :
finite_type R A ↔
∃ (n : ℕ), ∃ (f : alg_hom R (mv_polynomial (fin n) R) A), function.surjective ⇑f :=
sorry
/-- A finitely presented algebra is of finite type. -/
theorem of_finitely_presented {R : Type u_1} {A : Type u_2} [comm_ring R] [comm_ring A]
[algebra R A] : finitely_presented R A → finite_type R A :=
sorry
end finite_type
namespace finitely_presented
/-- If `e : A ≃ₐ[R] B` and `A` is finitely presented, then so is `B`. -/
theorem equiv (R : Type u_1) (A : Type u_2) (B : Type u_3) [comm_ring R] [comm_ring A] [algebra R A]
[comm_ring B] [algebra R B] (hfp : finitely_presented R A) (e : alg_equiv R A B) :
finitely_presented R B :=
sorry
/-- The ring of polynomials in finitely many variables is finitely presented. -/
theorem mv_polynomial (R : Type u_1) [comm_ring R] (ι : Type u_2) [fintype ι] :
finitely_presented R (mv_polynomial ι R) :=
sorry
/-- `R` is finitely presented as `R`-algebra. -/
theorem self (R : Type u_1) [comm_ring R] : finitely_presented R R :=
let hempty : finitely_presented R (mv_polynomial pempty R) := mv_polynomial R pempty;
equiv R (mv_polynomial pempty R) R hempty (mv_polynomial.pempty_alg_equiv R)
end finitely_presented
end algebra
namespace ring_hom
/-- A ring morphism `A →+* B` is `finite` if `B` is finitely generated as `A`-module. -/
def finite {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] (f : A →+* B) :=
let _inst : algebra A B := to_algebra f;
module.finite A B
/-- A ring morphism `A →+* B` is of `finite_type` if `B` is finitely generated as `A`-algebra. -/
def finite_type {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] (f : A →+* B) :=
algebra.finite_type A B
namespace finite
theorem id (A : Type u_1) [comm_ring A] : finite (id A) := module.finite.self A
theorem of_surjective {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] (f : A →+* B)
(hf : function.surjective ⇑f) : finite f :=
let _inst : algebra A B := to_algebra f;
module.finite.of_surjective (alg_hom.to_linear_map (algebra.of_id A B)) hf
theorem comp {A : Type u_1} {B : Type u_2} {C : Type u_3} [comm_ring A] [comm_ring B] [comm_ring C]
{g : B →+* C} {f : A →+* B} (hg : finite g) (hf : finite f) : finite (comp g f) :=
module.finite.trans B C
theorem finite_type {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] {f : A →+* B}
(hf : finite f) : finite_type f :=
module.finite.finite_type B
end finite
namespace finite_type
theorem id (A : Type u_1) [comm_ring A] : finite_type (id A) := algebra.finite_type.self A
theorem comp_surjective {A : Type u_1} {B : Type u_2} {C : Type u_3} [comm_ring A] [comm_ring B]
[comm_ring C] {f : A →+* B} {g : B →+* C} (hf : finite_type f) (hg : function.surjective ⇑g) :
finite_type (comp g f) :=
algebra.finite_type.of_surjective hf
(alg_hom.mk (⇑g) (map_one' g) (map_mul' g) (map_zero' g) (map_add' g) fun (a : A) => rfl) hg
theorem of_surjective {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] (f : A →+* B)
(hf : function.surjective ⇑f) : finite_type f :=
eq.mpr (id (Eq._oldrec (Eq.refl (finite_type f)) (Eq.symm (comp_id f))))
(comp_surjective (id A) hf)
theorem comp {A : Type u_1} {B : Type u_2} {C : Type u_3} [comm_ring A] [comm_ring B] [comm_ring C]
{g : B →+* C} {f : A →+* B} (hg : finite_type g) (hf : finite_type f) :
finite_type (comp g f) :=
algebra.finite_type.trans hf hg
end finite_type
end ring_hom
namespace alg_hom
/-- An algebra morphism `A →ₐ[R] B` is finite if it is finite as ring morphism.
In other words, if `B` is finitely generated as `A`-module. -/
def finite {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [comm_ring B]
[algebra R A] [algebra R B] (f : alg_hom R A B) :=
ring_hom.finite (to_ring_hom f)
/-- An algebra morphism `A →ₐ[R] B` is of `finite_type` if it is of finite type as ring morphism.
In other words, if `B` is finitely generated as `A`-algebra. -/
def finite_type {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A]
[comm_ring B] [algebra R A] [algebra R B] (f : alg_hom R A B) :=
ring_hom.finite_type (to_ring_hom f)
namespace finite
theorem id (R : Type u_1) (A : Type u_2) [comm_ring R] [comm_ring A] [algebra R A] :
finite (alg_hom.id R A) :=
ring_hom.finite.id A
theorem comp {R : Type u_1} {A : Type u_2} {B : Type u_3} {C : Type u_4} [comm_ring R] [comm_ring A]
[comm_ring B] [comm_ring C] [algebra R A] [algebra R B] [algebra R C] {g : alg_hom R B C}
{f : alg_hom R A B} (hg : finite g) (hf : finite f) : finite (comp g f) :=
ring_hom.finite.comp hg hf
theorem of_surjective {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A]
[comm_ring B] [algebra R A] [algebra R B] (f : alg_hom R A B) (hf : function.surjective ⇑f) :
finite f :=
ring_hom.finite.of_surjective (↑f) hf
theorem finite_type {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A]
[comm_ring B] [algebra R A] [algebra R B] {f : alg_hom R A B} (hf : finite f) : finite_type f :=
ring_hom.finite.finite_type hf
end finite
namespace finite_type
theorem id (R : Type u_1) (A : Type u_2) [comm_ring R] [comm_ring A] [algebra R A] :
finite_type (alg_hom.id R A) :=
ring_hom.finite_type.id A
theorem comp {R : Type u_1} {A : Type u_2} {B : Type u_3} {C : Type u_4} [comm_ring R] [comm_ring A]
[comm_ring B] [comm_ring C] [algebra R A] [algebra R B] [algebra R C] {g : alg_hom R B C}
{f : alg_hom R A B} (hg : finite_type g) (hf : finite_type f) : finite_type (comp g f) :=
ring_hom.finite_type.comp hg hf
theorem comp_surjective {R : Type u_1} {A : Type u_2} {B : Type u_3} {C : Type u_4} [comm_ring R]
[comm_ring A] [comm_ring B] [comm_ring C] [algebra R A] [algebra R B] [algebra R C]
{f : alg_hom R A B} {g : alg_hom R B C} (hf : finite_type f) (hg : function.surjective ⇑g) :
finite_type (comp g f) :=
ring_hom.finite_type.comp_surjective hf hg
theorem of_surjective {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A]
[comm_ring B] [algebra R A] [algebra R B] (f : alg_hom R A B) (hf : function.surjective ⇑f) :
finite_type f :=
ring_hom.finite_type.of_surjective (↑f) hf
end Mathlib |
7d6ea38303719ae322c92b49940e291a9016fe46 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/lake/Lake/Util/StoreInsts.lean | 03adfbf95587cefe529ba69eaa635790c42f47bb | [
"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 | 915 | lean | /-
Copyright (c) 2022 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
import Lake.Util.DRBMap
import Lake.Util.Family
import Lake.Util.Store
open Lean
namespace Lake
instance [Monad m] [EqOfCmpWrt κ β cmp] : MonadDStore κ β (StateT (DRBMap κ β cmp) m) where
fetch? k := return (← get).find? k
store k a := modify (·.insert k a)
instance [Monad m] : MonadStore κ α (StateT (RBMap κ α cmp) m) where
fetch? k := return (← get).find? k
store k a := modify (·.insert k a)
instance [Monad m] : MonadStore Name α (StateT (NameMap α) m) :=
inferInstanceAs (MonadStore _ _ (StateT (RBMap ..) _))
@[inline] instance [MonadDStore κ β m] [t : FamilyOut β k α] : MonadStore1 k α m where
fetch? := cast (by rw [t.family_key_eq_type]) <| fetch? (m := m) k
store a := store k <| cast t.family_key_eq_type.symm a
|
1de137cff8a5b4267316e2bb6d8cbe2ff3e3504e | 1a61aba1b67cddccce19532a9596efe44be4285f | /hott/types/arrow.hlean | 0f1743e1490ba281b069b395b6faa7df536a24bb | [
"Apache-2.0"
] | permissive | eigengrau/lean | 07986a0f2548688c13ba36231f6cdbee82abf4c6 | f8a773be1112015e2d232661ce616d23f12874d0 | refs/heads/master | 1,610,939,198,566 | 1,441,352,386,000 | 1,441,352,494,000 | 41,903,576 | 0 | 0 | null | 1,441,352,210,000 | 1,441,352,210,000 | null | UTF-8 | Lean | false | false | 2,873 | hlean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Floris van Doorn
Ported from Coq HoTT
Theorems about arrow types (function spaces)
-/
import types.pi
open eq equiv is_equiv funext pi equiv.ops
namespace pi
variables {A A' : Type} {B B' : Type} {C : A → B → Type}
{a a' a'' : A} {b b' b'' : B} {f g : A → B}
-- all lemmas here are special cases of the ones for pi-types
/- Functorial action -/
variables (f0 : A' → A) (f1 : B → B')
definition arrow_functor : (A → B) → (A' → B') := pi_functor f0 (λa, f1)
/- Equivalences -/
definition is_equiv_arrow_functor
[H0 : is_equiv f0] [H1 : is_equiv f1] : is_equiv (arrow_functor f0 f1) :=
is_equiv_pi_functor f0 (λa, f1)
definition arrow_equiv_arrow_rev (f0 : A' ≃ A) (f1 : B ≃ B') : (A → B) ≃ (A' → B') :=
equiv.mk _ (is_equiv_arrow_functor f0 f1)
definition arrow_equiv_arrow (f0 : A ≃ A') (f1 : B ≃ B') : (A → B) ≃ (A' → B') :=
arrow_equiv_arrow_rev (equiv.symm f0) f1
definition arrow_equiv_arrow_right (f1 : B ≃ B') : (A → B) ≃ (A → B') :=
arrow_equiv_arrow_rev equiv.refl f1
definition arrow_equiv_arrow_left_rev (f0 : A' ≃ A) : (A → B) ≃ (A' → B) :=
arrow_equiv_arrow_rev f0 equiv.refl
definition arrow_equiv_arrow_left (f0 : A ≃ A') : (A → B) ≃ (A' → B) :=
arrow_equiv_arrow f0 equiv.refl
definition arrow_equiv_arrow_right' (f1 : A → (B ≃ B')) : (A → B) ≃ (A → B') :=
pi_equiv_pi_id f1
/- Transport -/
definition arrow_transport {B C : A → Type} (p : a = a') (f : B a → C a)
: (transport (λa, B a → C a) p f) ~ (λb, p ▸ f (p⁻¹ ▸ b)) :=
eq.rec_on p (λx, idp)
/- Pathovers -/
definition arrow_pathover {B C : A → Type} {f : B a → C a} {g : B a' → C a'} {p : a = a'}
(r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[p] g b') : f =[p] g :=
begin
cases p, apply pathover_idp_of_eq,
apply eq_of_homotopy, intro b,
exact eq_of_pathover_idp (r b b idpo),
end
definition arrow_pathover_left {B C : A → Type} {f : B a → C a} {g : B a' → C a'} {p : a = a'}
(r : Π(b : B a), f b =[p] g (p ▸ b)) : f =[p] g :=
begin
cases p, apply pathover_idp_of_eq,
apply eq_of_homotopy, intro b,
exact eq_of_pathover_idp (r b),
end
definition arrow_pathover_right {B C : A → Type} {f : B a → C a} {g : B a' → C a'} {p : a = a'}
(r : Π(b' : B a'), f (p⁻¹ ▸ b') =[p] g b') : f =[p] g :=
begin
cases p, apply pathover_idp_of_eq,
apply eq_of_homotopy, intro b,
exact eq_of_pathover_idp (r b),
end
definition arrow_pathover_constant {B : Type} {C : A → Type} {f : B → C a} {g : B → C a'}
{p : a = a'} (r : Π(b : B), f b =[p] g b) : f =[p] g :=
pi_pathover_constant r
end pi
|
735f8a2e7936fea9cd4e65e7244b4e9ae898b59d | 94e33a31faa76775069b071adea97e86e218a8ee | /src/data/matrix/block.lean | f466dfb81ab24b78f2417c245d39e4d8ec39fa38 | [
"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 | 26,020 | lean | /-
Copyright (c) 2018 Ellen Arlt. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin
-/
import data.matrix.basic
/-!
# Block Matrices
## Main definitions
* `matrix.from_blocks`: build a block matrix out of 4 blocks
* `matrix.to_blocks₁₁`, `matrix.to_blocks₁₂`, `matrix.to_blocks₂₁`, `matrix.to_blocks₂₂`:
extract each of the four blocks from `matrix.from_blocks`.
* `matrix.block_diagonal`: block diagonal of equally sized blocks. On square blocks, this is a
ring homomorphisms, `matrix.block_diagonal_ring_hom`.
* `matrix.block_diag`: extract the blocks from the diagonal of a block diagonal matrix.
* `matrix.block_diagonal'`: block diagonal of unequally sized blocks. On square blocks, this is a
ring homomorphisms, `matrix.block_diagonal'_ring_hom`.
* `matrix.block_diag'`: extract the blocks from the diagonal of a block diagonal matrix.
-/
variables {l m n o p q : Type*} {m' n' p' : o → Type*}
variables {R : Type*} {S : Type*} {α : Type*} {β : Type*}
open_locale matrix
namespace matrix
section block_matrices
/-- We can form a single large matrix by flattening smaller 'block' matrices of compatible
dimensions. -/
@[pp_nodot]
def from_blocks (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
matrix (n ⊕ o) (l ⊕ m) α :=
of $ sum.elim (λ i, sum.elim (A i) (B i))
(λ i, sum.elim (C i) (D i))
@[simp] lemma from_blocks_apply₁₁
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : n) (j : l) :
from_blocks A B C D (sum.inl i) (sum.inl j) = A i j :=
rfl
@[simp] lemma from_blocks_apply₁₂
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : n) (j : m) :
from_blocks A B C D (sum.inl i) (sum.inr j) = B i j :=
rfl
@[simp] lemma from_blocks_apply₂₁
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : o) (j : l) :
from_blocks A B C D (sum.inr i) (sum.inl j) = C i j :=
rfl
@[simp] lemma from_blocks_apply₂₂
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : o) (j : m) :
from_blocks A B C D (sum.inr i) (sum.inr j) = D i j :=
rfl
/-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding
"top left" submatrix. -/
def to_blocks₁₁ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix n l α :=
of $ λ i j, M (sum.inl i) (sum.inl j)
/-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding
"top right" submatrix. -/
def to_blocks₁₂ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix n m α :=
of $ λ i j, M (sum.inl i) (sum.inr j)
/-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding
"bottom left" submatrix. -/
def to_blocks₂₁ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix o l α :=
of $ λ i j, M (sum.inr i) (sum.inl j)
/-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding
"bottom right" submatrix. -/
def to_blocks₂₂ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix o m α :=
of $ λ i j, M (sum.inr i) (sum.inr j)
lemma from_blocks_to_blocks (M : matrix (n ⊕ o) (l ⊕ m) α) :
from_blocks M.to_blocks₁₁ M.to_blocks₁₂ M.to_blocks₂₁ M.to_blocks₂₂ = M :=
begin
ext i j, rcases i; rcases j; refl,
end
@[simp] lemma to_blocks_from_blocks₁₁
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
(from_blocks A B C D).to_blocks₁₁ = A :=
rfl
@[simp] lemma to_blocks_from_blocks₁₂
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
(from_blocks A B C D).to_blocks₁₂ = B :=
rfl
@[simp] lemma to_blocks_from_blocks₂₁
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
(from_blocks A B C D).to_blocks₂₁ = C :=
rfl
@[simp] lemma to_blocks_from_blocks₂₂
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
(from_blocks A B C D).to_blocks₂₂ = D :=
rfl
lemma from_blocks_map
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (f : α → β) :
(from_blocks A B C D).map f = from_blocks (A.map f) (B.map f) (C.map f) (D.map f) :=
begin
ext i j, rcases i; rcases j; simp [from_blocks],
end
lemma from_blocks_transpose
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
(from_blocks A B C D)ᵀ = from_blocks Aᵀ Cᵀ Bᵀ Dᵀ :=
begin
ext i j, rcases i; rcases j; simp [from_blocks],
end
lemma from_blocks_conj_transpose [has_star α]
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
(from_blocks A B C D)ᴴ = from_blocks Aᴴ Cᴴ Bᴴ Dᴴ :=
begin
simp only [conj_transpose, from_blocks_transpose, from_blocks_map]
end
@[simp] lemma from_blocks_minor_sum_swap_left
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (f : p → l ⊕ m) :
(from_blocks A B C D).minor sum.swap f = (from_blocks C D A B).minor id f :=
by { ext i j, cases i; dsimp; cases f j; refl }
@[simp] lemma from_blocks_minor_sum_swap_right
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (f : p → n ⊕ o) :
(from_blocks A B C D).minor f sum.swap = (from_blocks B A D C).minor f id :=
by { ext i j, cases j; dsimp; cases f i; refl }
lemma from_blocks_minor_sum_swap_sum_swap {l m n o α : Type*}
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
(from_blocks A B C D).minor sum.swap sum.swap = from_blocks D C B A :=
by simp
/-- A 2x2 block matrix is block diagonal if the blocks outside of the diagonal vanish -/
def is_two_block_diagonal [has_zero α] (A : matrix (n ⊕ o) (l ⊕ m) α) : Prop :=
to_blocks₁₂ A = 0 ∧ to_blocks₂₁ A = 0
/-- Let `p` pick out certain rows and `q` pick out certain columns of a matrix `M`. Then
`to_block M p q` is the corresponding block matrix. -/
def to_block (M : matrix m n α) (p : m → Prop) (q : n → Prop) :
matrix {a // p a} {a // q a} α := M.minor coe coe
@[simp] lemma to_block_apply (M : matrix m n α) (p : m → Prop) (q : n → Prop)
(i : {a // p a}) (j : {a // q a}) : to_block M p q i j = M ↑i ↑j := rfl
/-- Let `b` map rows and columns of a square matrix `M` to blocks. Then
`to_square_block M b k` is the block `k` matrix. -/
def to_square_block (M : matrix m m α) {n : nat} (b : m → fin n) (k : fin n) :
matrix {a // b a = k} {a // b a = k} α := M.minor coe coe
@[simp] lemma to_square_block_def (M : matrix m m α) {n : nat} (b : m → fin n) (k : fin n) :
to_square_block M b k = λ i j, M ↑i ↑j := rfl
/-- Alternate version with `b : m → nat`. Let `b` map rows and columns of a square matrix `M` to
blocks. Then `to_square_block' M b k` is the block `k` matrix. -/
def to_square_block' (M : matrix m m α) (b : m → nat) (k : nat) :
matrix {a // b a = k} {a // b a = k} α := M.minor coe coe
@[simp] lemma to_square_block_def' (M : matrix m m α) (b : m → nat) (k : nat) :
to_square_block' M b k = λ i j, M ↑i ↑j := rfl
/-- Let `p` pick out certain rows and columns of a square matrix `M`. Then
`to_square_block_prop M p` is the corresponding block matrix. -/
def to_square_block_prop (M : matrix m m α) (p : m → Prop) :
matrix {a // p a} {a // p a} α := M.minor coe coe
@[simp] lemma to_square_block_prop_def (M : matrix m m α) (p : m → Prop) :
to_square_block_prop M p = λ i j, M ↑i ↑j := rfl
lemma from_blocks_smul [has_smul R α]
(x : R) (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) :
x • (from_blocks A B C D) = from_blocks (x • A) (x • B) (x • C) (x • D) :=
begin
ext i j, rcases i; rcases j; simp [from_blocks],
end
lemma from_blocks_add [has_add α]
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α)
(A' : matrix n l α) (B' : matrix n m α) (C' : matrix o l α) (D' : matrix o m α) :
(from_blocks A B C D) + (from_blocks A' B' C' D') =
from_blocks (A + A') (B + B')
(C + C') (D + D') :=
begin
ext i j, rcases i; rcases j; refl,
end
lemma from_blocks_multiply [fintype l] [fintype m] [non_unital_non_assoc_semiring α]
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α)
(A' : matrix l p α) (B' : matrix l q α) (C' : matrix m p α) (D' : matrix m q α) :
(from_blocks A B C D) ⬝ (from_blocks A' B' C' D') =
from_blocks (A ⬝ A' + B ⬝ C') (A ⬝ B' + B ⬝ D')
(C ⬝ A' + D ⬝ C') (C ⬝ B' + D ⬝ D') :=
begin
ext i j, rcases i; rcases j;
simp only [from_blocks, mul_apply, fintype.sum_sum_type, sum.elim_inl, sum.elim_inr,
pi.add_apply, of_apply],
end
lemma from_blocks_mul_vec [fintype l] [fintype m] [non_unital_non_assoc_semiring α]
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (x : l ⊕ m → α) :
mul_vec (from_blocks A B C D) x =
sum.elim (mul_vec A (x ∘ sum.inl) + mul_vec B (x ∘ sum.inr))
(mul_vec C (x ∘ sum.inl) + mul_vec D (x ∘ sum.inr)) :=
by { ext i, cases i; simp [mul_vec, dot_product] }
lemma vec_mul_from_blocks [fintype n] [fintype o] [non_unital_non_assoc_semiring α]
(A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (x : n ⊕ o → α) :
vec_mul x (from_blocks A B C D) =
sum.elim (vec_mul (x ∘ sum.inl) A + vec_mul (x ∘ sum.inr) C)
(vec_mul (x ∘ sum.inl) B + vec_mul (x ∘ sum.inr) D) :=
by { ext i, cases i; simp [vec_mul, dot_product] }
variables [decidable_eq l] [decidable_eq m]
@[simp] lemma from_blocks_diagonal [has_zero α] (d₁ : l → α) (d₂ : m → α) :
from_blocks (diagonal d₁) 0 0 (diagonal d₂) = diagonal (sum.elim d₁ d₂) :=
begin
ext i j, rcases i; rcases j; simp [diagonal],
end
@[simp] lemma from_blocks_one [has_zero α] [has_one α] :
from_blocks (1 : matrix l l α) 0 0 (1 : matrix m m α) = 1 :=
by { ext i j, rcases i; rcases j; simp [one_apply] }
end block_matrices
section block_diagonal
variables [decidable_eq o]
section has_zero
variables [has_zero α] [has_zero β]
/-- `matrix.block_diagonal M` turns a homogenously-indexed collection of matrices
`M : o → matrix m n α'` into a `m × o`-by-`n × o` block matrix which has the entries of `M` along
the diagonal and zero elsewhere.
See also `matrix.block_diagonal'` if the matrices may not have the same size everywhere.
-/
def block_diagonal (M : o → matrix m n α) : matrix (m × o) (n × o) α
| ⟨i, k⟩ ⟨j, k'⟩ := if k = k' then M k i j else 0
lemma block_diagonal_apply (M : o → matrix m n α) (ik jk) :
block_diagonal M ik jk = if ik.2 = jk.2 then M ik.2 ik.1 jk.1 else 0 :=
by { cases ik, cases jk, refl }
@[simp]
lemma block_diagonal_apply_eq (M : o → matrix m n α) (i j k) :
block_diagonal M (i, k) (j, k) = M k i j :=
if_pos rfl
lemma block_diagonal_apply_ne (M : o → matrix m n α) (i j) {k k'} (h : k ≠ k') :
block_diagonal M (i, k) (j, k') = 0 :=
if_neg h
lemma block_diagonal_map (M : o → matrix m n α) (f : α → β) (hf : f 0 = 0) :
(block_diagonal M).map f = block_diagonal (λ k, (M k).map f) :=
begin
ext,
simp only [map_apply, block_diagonal_apply, eq_comm],
rw [apply_ite f, hf],
end
@[simp] lemma block_diagonal_transpose (M : o → matrix m n α) :
(block_diagonal M)ᵀ = block_diagonal (λ k, (M k)ᵀ) :=
begin
ext,
simp only [transpose_apply, block_diagonal_apply, eq_comm],
split_ifs with h,
{ rw h },
{ refl }
end
@[simp] lemma block_diagonal_conj_transpose
{α : Type*} [add_monoid α] [star_add_monoid α] (M : o → matrix m n α) :
(block_diagonal M)ᴴ = block_diagonal (λ k, (M k)ᴴ) :=
begin
simp only [conj_transpose, block_diagonal_transpose],
rw block_diagonal_map _ star (star_zero α),
end
@[simp] lemma block_diagonal_zero :
block_diagonal (0 : o → matrix m n α) = 0 :=
by { ext, simp [block_diagonal_apply] }
@[simp] lemma block_diagonal_diagonal [decidable_eq m] (d : o → m → α) :
block_diagonal (λ k, diagonal (d k)) = diagonal (λ ik, d ik.2 ik.1) :=
begin
ext ⟨i, k⟩ ⟨j, k'⟩,
simp only [block_diagonal_apply, diagonal, prod.mk.inj_iff, ← ite_and],
congr' 1,
rw and_comm,
end
@[simp] lemma block_diagonal_one [decidable_eq m] [has_one α] :
block_diagonal (1 : o → matrix m m α) = 1 :=
show block_diagonal (λ (_ : o), diagonal (λ (_ : m), (1 : α))) = diagonal (λ _, 1),
by rw [block_diagonal_diagonal]
end has_zero
@[simp] lemma block_diagonal_add [add_zero_class α] (M N : o → matrix m n α) :
block_diagonal (M + N) = block_diagonal M + block_diagonal N :=
begin
ext,
simp only [block_diagonal_apply, pi.add_apply],
split_ifs; simp
end
section
variables (o m n α)
/-- `matrix.block_diagonal` as an `add_monoid_hom`. -/
@[simps] def block_diagonal_add_monoid_hom [add_zero_class α] :
(o → matrix m n α) →+ matrix (m × o) (n × o) α :=
{ to_fun := block_diagonal,
map_zero' := block_diagonal_zero,
map_add' := block_diagonal_add }
end
@[simp] lemma block_diagonal_neg [add_group α] (M : o → matrix m n α) :
block_diagonal (-M) = - block_diagonal M :=
map_neg (block_diagonal_add_monoid_hom m n o α) M
@[simp] lemma block_diagonal_sub [add_group α] (M N : o → matrix m n α) :
block_diagonal (M - N) = block_diagonal M - block_diagonal N :=
map_sub (block_diagonal_add_monoid_hom m n o α) M N
@[simp] lemma block_diagonal_mul [fintype n] [fintype o] [non_unital_non_assoc_semiring α]
(M : o → matrix m n α) (N : o → matrix n p α) :
block_diagonal (λ k, M k ⬝ N k) = block_diagonal M ⬝ block_diagonal N :=
begin
ext ⟨i, k⟩ ⟨j, k'⟩,
simp only [block_diagonal_apply, mul_apply, ← finset.univ_product_univ, finset.sum_product],
split_ifs with h; simp [h]
end
section
variables (α m o)
/-- `matrix.block_diagonal` as a `ring_hom`. -/
@[simps]
def block_diagonal_ring_hom [decidable_eq m] [fintype o] [fintype m] [non_assoc_semiring α] :
(o → matrix m m α) →+* matrix (m × o) (m × o) α :=
{ to_fun := block_diagonal,
map_one' := block_diagonal_one,
map_mul' := block_diagonal_mul,
..block_diagonal_add_monoid_hom m m o α }
end
@[simp] lemma block_diagonal_pow [decidable_eq m] [fintype o] [fintype m] [semiring α]
(M : o → matrix m m α) (n : ℕ) :
block_diagonal (M ^ n) = block_diagonal M ^ n :=
map_pow (block_diagonal_ring_hom m o α) M n
@[simp] lemma block_diagonal_smul {R : Type*} [monoid R] [add_monoid α] [distrib_mul_action R α]
(x : R) (M : o → matrix m n α) : block_diagonal (x • M) = x • block_diagonal M :=
by { ext, simp only [block_diagonal_apply, pi.smul_apply], split_ifs; simp }
end block_diagonal
section block_diag
/-- Extract a block from the diagonal of a block diagonal matrix.
This is the block form of `matrix.diag`, and the left-inverse of `matrix.block_diagonal`. -/
def block_diag (M : matrix (m × o) (n × o) α) (k : o) : matrix m n α
| i j := M (i, k) (j, k)
lemma block_diag_map (M : matrix (m × o) (n × o) α) (f : α → β) :
block_diag (M.map f) = λ k, (block_diag M k).map f :=
rfl
@[simp] lemma block_diag_transpose (M : matrix (m × o) (n × o) α) (k : o) :
block_diag Mᵀ k = (block_diag M k)ᵀ :=
ext $ λ i j, rfl
@[simp] lemma block_diag_conj_transpose
{α : Type*} [add_monoid α] [star_add_monoid α] (M : matrix (m × o) (n × o) α) (k : o) :
block_diag Mᴴ k = (block_diag M k)ᴴ :=
ext $ λ i j, rfl
section has_zero
variables [has_zero α] [has_zero β]
@[simp] lemma block_diag_zero :
block_diag (0 : matrix (m × o) (n × o) α) = 0 :=
rfl
@[simp] lemma block_diag_diagonal [decidable_eq o] [decidable_eq m] (d : (m × o) → α) (k : o) :
block_diag (diagonal d) k = diagonal (λ i, d (i, k)) :=
ext $ λ i j, begin
obtain rfl | hij := decidable.eq_or_ne i j,
{ rw [block_diag, diagonal_apply_eq, diagonal_apply_eq] },
{ rw [block_diag, diagonal_apply_ne _ hij, diagonal_apply_ne _ (mt _ hij)],
exact prod.fst_eq_iff.mpr },
end
@[simp] lemma block_diag_block_diagonal [decidable_eq o] (M : o → matrix m n α) :
block_diag (block_diagonal M) = M :=
funext $ λ k, ext $ λ i j, block_diagonal_apply_eq _ _ _ _
@[simp] lemma block_diag_one [decidable_eq o] [decidable_eq m] [has_one α] :
block_diag (1 : matrix (m × o) (m × o) α) = 1 :=
funext $ block_diag_diagonal _
end has_zero
@[simp] lemma block_diag_add [add_zero_class α] (M N : matrix (m × o) (n × o) α) :
block_diag (M + N) = block_diag M + block_diag N :=
rfl
section
variables (o m n α)
/-- `matrix.block_diag` as an `add_monoid_hom`. -/
@[simps] def block_diag_add_monoid_hom [add_zero_class α] :
matrix (m × o) (n × o) α →+ (o → matrix m n α) :=
{ to_fun := block_diag,
map_zero' := block_diag_zero,
map_add' := block_diag_add }
end
@[simp] lemma block_diag_neg [add_group α] (M : matrix (m × o) (n × o) α) :
block_diag (-M) = - block_diag M :=
map_neg (block_diag_add_monoid_hom m n o α) M
@[simp] lemma block_diag_sub [add_group α] (M N : matrix (m × o) (n × o) α) :
block_diag (M - N) = block_diag M - block_diag N :=
map_sub (block_diag_add_monoid_hom m n o α) M N
@[simp] lemma block_diag_smul {R : Type*} [monoid R] [add_monoid α] [distrib_mul_action R α]
(x : R) (M : matrix (m × o) (n × o) α) : block_diag (x • M) = x • block_diag M :=
rfl
end block_diag
section block_diagonal'
variables [decidable_eq o]
section has_zero
variables [has_zero α] [has_zero β]
/-- `matrix.block_diagonal' M` turns `M : Π i, matrix (m i) (n i) α` into a
`Σ i, m i`-by-`Σ i, n i` block matrix which has the entries of `M` along the diagonal
and zero elsewhere.
This is the dependently-typed version of `matrix.block_diagonal`. -/
def block_diagonal' (M : Π i, matrix (m' i) (n' i) α) : matrix (Σ i, m' i) (Σ i, n' i) α
| ⟨k, i⟩ ⟨k', j⟩ := if h : k = k' then M k i (cast (congr_arg n' h.symm) j) else 0
lemma block_diagonal'_eq_block_diagonal (M : o → matrix m n α) {k k'} (i j) :
block_diagonal M (i, k) (j, k') = block_diagonal' M ⟨k, i⟩ ⟨k', j⟩ :=
rfl
lemma block_diagonal'_minor_eq_block_diagonal (M : o → matrix m n α) :
(block_diagonal' M).minor (prod.to_sigma ∘ prod.swap) (prod.to_sigma ∘ prod.swap) =
block_diagonal M :=
matrix.ext $ λ ⟨k, i⟩ ⟨k', j⟩, rfl
lemma block_diagonal'_apply (M : Π i, matrix (m' i) (n' i) α) (ik jk) :
block_diagonal' M ik jk = if h : ik.1 = jk.1 then
M ik.1 ik.2 (cast (congr_arg n' h.symm) jk.2) else 0 :=
by { cases ik, cases jk, refl }
@[simp]
lemma block_diagonal'_apply_eq (M : Π i, matrix (m' i) (n' i) α) (k i j) :
block_diagonal' M ⟨k, i⟩ ⟨k, j⟩ = M k i j :=
dif_pos rfl
lemma block_diagonal'_apply_ne (M : Π i, matrix (m' i) (n' i) α) {k k'} (i j) (h : k ≠ k') :
block_diagonal' M ⟨k, i⟩ ⟨k', j⟩ = 0 :=
dif_neg h
lemma block_diagonal'_map (M : Π i, matrix (m' i) (n' i) α) (f : α → β) (hf : f 0 = 0) :
(block_diagonal' M).map f = block_diagonal' (λ k, (M k).map f) :=
begin
ext,
simp only [map_apply, block_diagonal'_apply, eq_comm],
rw [apply_dite f, hf],
end
@[simp] lemma block_diagonal'_transpose (M : Π i, matrix (m' i) (n' i) α) :
(block_diagonal' M)ᵀ = block_diagonal' (λ k, (M k)ᵀ) :=
begin
ext ⟨ii, ix⟩ ⟨ji, jx⟩,
simp only [transpose_apply, block_diagonal'_apply],
split_ifs; cc
end
@[simp] lemma block_diagonal'_conj_transpose {α} [add_monoid α] [star_add_monoid α]
(M : Π i, matrix (m' i) (n' i) α) :
(block_diagonal' M)ᴴ = block_diagonal' (λ k, (M k)ᴴ) :=
begin
simp only [conj_transpose, block_diagonal'_transpose],
exact block_diagonal'_map _ star (star_zero α),
end
@[simp] lemma block_diagonal'_zero :
block_diagonal' (0 : Π i, matrix (m' i) (n' i) α) = 0 :=
by { ext, simp [block_diagonal'_apply] }
@[simp] lemma block_diagonal'_diagonal [Π i, decidable_eq (m' i)] (d : Π i, m' i → α) :
block_diagonal' (λ k, diagonal (d k)) = diagonal (λ ik, d ik.1 ik.2) :=
begin
ext ⟨i, k⟩ ⟨j, k'⟩,
simp only [block_diagonal'_apply, diagonal],
obtain rfl | hij := decidable.eq_or_ne i j,
{ simp, },
{ simp [hij] },
end
@[simp] lemma block_diagonal'_one [∀ i, decidable_eq (m' i)] [has_one α] :
block_diagonal' (1 : Π i, matrix (m' i) (m' i) α) = 1 :=
show block_diagonal' (λ (i : o), diagonal (λ (_ : m' i), (1 : α))) = diagonal (λ _, 1),
by rw [block_diagonal'_diagonal]
end has_zero
@[simp] lemma block_diagonal'_add [add_zero_class α] (M N : Π i, matrix (m' i) (n' i) α) :
block_diagonal' (M + N) = block_diagonal' M + block_diagonal' N :=
begin
ext,
simp only [block_diagonal'_apply, pi.add_apply],
split_ifs; simp
end
section
variables (m' n' α)
/-- `matrix.block_diagonal'` as an `add_monoid_hom`. -/
@[simps] def block_diagonal'_add_monoid_hom [add_zero_class α] :
(Π i, matrix (m' i) (n' i) α) →+ matrix (Σ i, m' i) (Σ i, n' i) α :=
{ to_fun := block_diagonal',
map_zero' := block_diagonal'_zero,
map_add' := block_diagonal'_add }
end
@[simp] lemma block_diagonal'_neg [add_group α] (M : Π i, matrix (m' i) (n' i) α) :
block_diagonal' (-M) = - block_diagonal' M :=
map_neg (block_diagonal'_add_monoid_hom m' n' α) M
@[simp] lemma block_diagonal'_sub [add_group α] (M N : Π i, matrix (m' i) (n' i) α) :
block_diagonal' (M - N) = block_diagonal' M - block_diagonal' N :=
map_sub (block_diagonal'_add_monoid_hom m' n' α) M N
@[simp] lemma block_diagonal'_mul [non_unital_non_assoc_semiring α]
[Π i, fintype (n' i)] [fintype o]
(M : Π i, matrix (m' i) (n' i) α) (N : Π i, matrix (n' i) (p' i) α) :
block_diagonal' (λ k, M k ⬝ N k) = block_diagonal' M ⬝ block_diagonal' N :=
begin
ext ⟨k, i⟩ ⟨k', j⟩,
simp only [block_diagonal'_apply, mul_apply, ← finset.univ_sigma_univ, finset.sum_sigma],
rw fintype.sum_eq_single k,
{ split_ifs; simp },
{ intros j' hj', exact finset.sum_eq_zero (λ _ _, by rw [dif_neg hj'.symm, zero_mul]) },
end
section
variables (α m')
/-- `matrix.block_diagonal'` as a `ring_hom`. -/
@[simps]
def block_diagonal'_ring_hom [Π i, decidable_eq (m' i)] [fintype o] [Π i, fintype (m' i)]
[non_assoc_semiring α] :
(Π i, matrix (m' i) (m' i) α) →+* matrix (Σ i, m' i) (Σ i, m' i) α :=
{ to_fun := block_diagonal',
map_one' := block_diagonal'_one,
map_mul' := block_diagonal'_mul,
..block_diagonal'_add_monoid_hom m' m' α }
end
@[simp] lemma block_diagonal'_pow [Π i, decidable_eq (m' i)] [fintype o] [Π i, fintype (m' i)]
[semiring α] (M : Π i, matrix (m' i) (m' i) α) (n : ℕ) :
block_diagonal' (M ^ n) = block_diagonal' M ^ n :=
map_pow (block_diagonal'_ring_hom m' α) M n
@[simp] lemma block_diagonal'_smul {R : Type*} [semiring R] [add_comm_monoid α] [module R α]
(x : R) (M : Π i, matrix (m' i) (n' i) α) : block_diagonal' (x • M) = x • block_diagonal' M :=
by { ext, simp only [block_diagonal'_apply, pi.smul_apply], split_ifs; simp }
end block_diagonal'
section block_diag'
/-- Extract a block from the diagonal of a block diagonal matrix.
This is the block form of `matrix.diag`, and the left-inverse of `matrix.block_diagonal'`. -/
def block_diag' (M : matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) : matrix (m' k) (n' k) α
| i j := M ⟨k, i⟩ ⟨k, j⟩
lemma block_diag'_map (M : matrix (Σ i, m' i) (Σ i, n' i) α) (f : α → β) :
block_diag' (M.map f) = λ k, (block_diag' M k).map f :=
rfl
@[simp] lemma block_diag'_transpose (M : matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) :
block_diag' Mᵀ k = (block_diag' M k)ᵀ :=
ext $ λ i j, rfl
@[simp] lemma block_diag'_conj_transpose
{α : Type*} [add_monoid α] [star_add_monoid α] (M : matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) :
block_diag' Mᴴ k = (block_diag' M k)ᴴ :=
ext $ λ i j, rfl
section has_zero
variables [has_zero α] [has_zero β]
@[simp] lemma block_diag'_zero :
block_diag' (0 : matrix (Σ i, m' i) (Σ i, n' i) α) = 0 :=
rfl
@[simp] lemma block_diag'_diagonal [decidable_eq o] [Π i, decidable_eq (m' i)]
(d : (Σ i, m' i) → α) (k : o) :
block_diag' (diagonal d) k = diagonal (λ i, d ⟨k, i⟩) :=
ext $ λ i j, begin
obtain rfl | hij := decidable.eq_or_ne i j,
{ rw [block_diag', diagonal_apply_eq, diagonal_apply_eq] },
{ rw [block_diag', diagonal_apply_ne _ hij, diagonal_apply_ne _ (mt (λ h, _) hij)],
cases h, refl },
end
@[simp] lemma block_diag'_block_diagonal' [decidable_eq o] (M : Π i, matrix (m' i) (n' i) α) :
block_diag' (block_diagonal' M) = M :=
funext $ λ k, ext $ λ i j, block_diagonal'_apply_eq _ _ _ _
@[simp] lemma block_diag'_one [decidable_eq o] [Π i, decidable_eq (m' i)] [has_one α] :
block_diag' (1 : matrix (Σ i, m' i) (Σ i, m' i) α) = 1 :=
funext $ block_diag'_diagonal _
end has_zero
@[simp] lemma block_diag'_add [add_zero_class α] (M N : matrix (Σ i, m' i) (Σ i, n' i) α) :
block_diag' (M + N) = block_diag' M + block_diag' N :=
rfl
section
variables (m' n' α)
/-- `matrix.block_diag'` as an `add_monoid_hom`. -/
@[simps] def block_diag'_add_monoid_hom [add_zero_class α] :
matrix (Σ i, m' i) (Σ i, n' i) α →+ Π i, matrix (m' i) (n' i) α :=
{ to_fun := block_diag',
map_zero' := block_diag'_zero,
map_add' := block_diag'_add }
end
@[simp] lemma block_diag'_neg [add_group α] (M : matrix (Σ i, m' i) (Σ i, n' i) α) :
block_diag' (-M) = - block_diag' M :=
map_neg (block_diag'_add_monoid_hom m' n' α) M
@[simp] lemma block_diag'_sub [add_group α] (M N : matrix (Σ i, m' i) (Σ i, n' i) α) :
block_diag' (M - N) = block_diag' M - block_diag' N :=
map_sub (block_diag'_add_monoid_hom m' n' α) M N
@[simp] lemma block_diag'_smul {R : Type*} [monoid R] [add_monoid α] [distrib_mul_action R α]
(x : R) (M : matrix (Σ i, m' i) (Σ i, n' i) α) : block_diag' (x • M) = x • block_diag' M :=
rfl
end block_diag'
end matrix
|
cf24889a6a45552e4afc2dff556cf7e170681943 | a3416b394f900e6d43a462113bf00eecea016923 | /src/ack.lean | 0bc038ee1bafad50c279c3af727c6345475849ff | [] | no_license | Nolrai/non-standard | efe17e1e97db75ec1a26aed2623e578ec8881c51 | 2a128217005a0c9eef53e7c24c6637d0edcf3151 | refs/heads/master | 1,682,477,161,430 | 1,604,790,441,000 | 1,604,790,441,000 | 359,366,919 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 128 | lean | import computability.partrec
import computability.partrec_code
import computability.primrec
import computability.tm_to_partrec
|
737b5ef0b54161ae9f31c47711cd6665d7e77464 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/logic/nontrivial.lean | 3a1ac50b4933c978d46ee08568638c95b1c12076 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 10,422 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import data.prod
import data.subtype
import logic.function.basic
import logic.unique
/-!
# Nontrivial types
A type is *nontrivial* if it contains at least two elements. This is useful in particular for rings
(where it is equivalent to the fact that zero is different from one) and for vector spaces
(where it is equivalent to the fact that the dimension is positive).
We introduce a typeclass `nontrivial` formalizing this property.
-/
variables {α : Type*} {β : Type*}
open_locale classical
/-- Predicate typeclass for expressing that a type is not reduced to a single element. In rings,
this is equivalent to `0 ≠ 1`. In vector spaces, this is equivalent to positive dimension. -/
class nontrivial (α : Type*) : Prop :=
(exists_pair_ne : ∃ (x y : α), x ≠ y)
lemma nontrivial_iff : nontrivial α ↔ ∃ (x y : α), x ≠ y :=
⟨λ h, h.exists_pair_ne, λ h, ⟨h⟩⟩
lemma exists_pair_ne (α : Type*) [nontrivial α] : ∃ (x y : α), x ≠ y :=
nontrivial.exists_pair_ne
-- See Note [decidable namespace]
protected lemma decidable.exists_ne [nontrivial α] [decidable_eq α] (x : α) : ∃ y, y ≠ x :=
begin
rcases exists_pair_ne α with ⟨y, y', h⟩,
by_cases hx : x = y,
{ rw ← hx at h,
exact ⟨y', h.symm⟩ },
{ exact ⟨y, ne.symm hx⟩ }
end
lemma exists_ne [nontrivial α] (x : α) : ∃ y, y ≠ x :=
by classical; exact decidable.exists_ne x
-- `x` and `y` are explicit here, as they are often needed to guide typechecking of `h`.
lemma nontrivial_of_ne (x y : α) (h : x ≠ y) : nontrivial α :=
⟨⟨x, y, h⟩⟩
-- `x` and `y` are explicit here, as they are often needed to guide typechecking of `h`.
lemma nontrivial_of_lt [preorder α] (x y : α) (h : x < y) : nontrivial α :=
⟨⟨x, y, ne_of_lt h⟩⟩
lemma nontrivial_iff_exists_ne (x : α) : nontrivial α ↔ ∃ y, y ≠ x :=
⟨λ h, @exists_ne α h x, λ ⟨y, hy⟩, nontrivial_of_ne _ _ hy⟩
lemma subtype.nontrivial_iff_exists_ne (p : α → Prop) (x : subtype p) :
nontrivial (subtype p) ↔ ∃ (y : α) (hy : p y), y ≠ x :=
by simp only [nontrivial_iff_exists_ne x, subtype.exists, ne.def, subtype.ext_iff, subtype.coe_mk]
instance : nontrivial Prop := ⟨⟨true, false, true_ne_false⟩⟩
/--
See Note [lower instance priority]
Note that since this and `nonempty_of_inhabited` are the most "obvious" way to find a nonempty
instance if no direct instance can be found, we give this a higher priority than the usual `100`.
-/
@[priority 500]
instance nontrivial.to_nonempty [nontrivial α] : nonempty α :=
let ⟨x, _⟩ := exists_pair_ne α in ⟨x⟩
attribute [instance, priority 500] nonempty_of_inhabited
/-- An inhabited type is either nontrivial, or has a unique element. -/
noncomputable def nontrivial_psum_unique (α : Type*) [inhabited α] :
psum (nontrivial α) (unique α) :=
if h : nontrivial α then psum.inl h else psum.inr
{ default := default,
uniq := λ (x : α),
begin
change x = default,
contrapose! h,
use [x, default]
end }
lemma subsingleton_iff : subsingleton α ↔ ∀ (x y : α), x = y :=
⟨by { introsI h, exact subsingleton.elim }, λ h, ⟨h⟩⟩
lemma not_nontrivial_iff_subsingleton : ¬(nontrivial α) ↔ subsingleton α :=
by { rw [nontrivial_iff, subsingleton_iff], push_neg, refl }
lemma not_subsingleton (α) [h : nontrivial α] : ¬subsingleton α :=
let ⟨⟨x, y, hxy⟩⟩ := h in λ ⟨h'⟩, hxy $ h' x y
/-- A type is either a subsingleton or nontrivial. -/
lemma subsingleton_or_nontrivial (α : Type*) : subsingleton α ∨ nontrivial α :=
by { rw [← not_nontrivial_iff_subsingleton, or_comm], exact classical.em _ }
lemma false_of_nontrivial_of_subsingleton (α : Type*) [nontrivial α] [subsingleton α] : false :=
let ⟨x, y, h⟩ := exists_pair_ne α in h $ subsingleton.elim x y
instance option.nontrivial [nonempty α] : nontrivial (option α) :=
by { inhabit α, use [none, some default] }
/-- Pushforward a `nontrivial` instance along an injective function. -/
protected lemma function.injective.nontrivial [nontrivial α]
{f : α → β} (hf : function.injective f) : nontrivial β :=
let ⟨x, y, h⟩ := exists_pair_ne α in ⟨⟨f x, f y, hf.ne h⟩⟩
/-- Pullback a `nontrivial` instance along a surjective function. -/
protected lemma function.surjective.nontrivial [nontrivial β]
{f : α → β} (hf : function.surjective f) : nontrivial α :=
begin
rcases exists_pair_ne β with ⟨x, y, h⟩,
rcases hf x with ⟨x', hx'⟩,
rcases hf y with ⟨y', hy'⟩,
have : x' ≠ y', by { contrapose! h, rw [← hx', ← hy', h] },
exact ⟨⟨x', y', this⟩⟩
end
/-- An injective function from a nontrivial type has an argument at
which it does not take a given value. -/
protected lemma function.injective.exists_ne [nontrivial α] {f : α → β}
(hf : function.injective f) (y : β) : ∃ x, f x ≠ y :=
begin
rcases exists_pair_ne α with ⟨x₁, x₂, hx⟩,
by_cases h : f x₂ = y,
{ exact ⟨x₁, (hf.ne_iff' h).2 hx⟩ },
{ exact ⟨x₂, h⟩ }
end
instance nontrivial_prod_right [nonempty α] [nontrivial β] : nontrivial (α × β) :=
prod.snd_surjective.nontrivial
instance nontrivial_prod_left [nontrivial α] [nonempty β] : nontrivial (α × β) :=
prod.fst_surjective.nontrivial
namespace pi
variables {I : Type*} {f : I → Type*}
/-- A pi type is nontrivial if it's nonempty everywhere and nontrivial somewhere. -/
lemma nontrivial_at (i' : I) [inst : Π i, nonempty (f i)] [nontrivial (f i')] :
nontrivial (Π i : I, f i) :=
by classical; exact
(function.update_injective (λ i, classical.choice (inst i)) i').nontrivial
/--
As a convenience, provide an instance automatically if `(f default)` is nontrivial.
If a different index has the non-trivial type, then use `haveI := nontrivial_at that_index`.
-/
instance nontrivial [inhabited I] [inst : Π i, nonempty (f i)] [nontrivial (f default)] :
nontrivial (Π i : I, f i) := nontrivial_at default
end pi
instance function.nontrivial [h : nonempty α] [nontrivial β] : nontrivial (α → β) :=
h.elim $ λ a, pi.nontrivial_at a
mk_simp_attribute nontriviality "Simp lemmas for `nontriviality` tactic"
protected lemma subsingleton.le [preorder α] [subsingleton α] (x y : α) : x ≤ y :=
le_of_eq (subsingleton.elim x y)
attribute [nontriviality] eq_iff_true_of_subsingleton subsingleton.le
namespace tactic
/--
Tries to generate a `nontrivial α` instance by performing case analysis on
`subsingleton_or_nontrivial α`,
attempting to discharge the subsingleton branch using lemmas with `@[nontriviality]` attribute,
including `subsingleton.le` and `eq_iff_true_of_subsingleton`.
-/
meta def nontriviality_by_elim (α : expr) (lems : interactive.parse simp_arg_list) : tactic unit :=
do
alternative ← to_expr ``(subsingleton_or_nontrivial %%α),
n ← get_unused_name "_inst",
tactic.cases alternative [n, n],
(solve1 $ do
reset_instance_cache,
apply_instance <|>
interactive.simp none none ff lems [`nontriviality] (interactive.loc.ns [none])) <|>
fail format!"Could not prove goal assuming `subsingleton {α}`",
reset_instance_cache
/--
Tries to generate a `nontrivial α` instance using `nontrivial_of_ne` or `nontrivial_of_lt`
and local hypotheses.
-/
meta def nontriviality_by_assumption (α : expr) : tactic unit :=
do
n ← get_unused_name "_inst",
to_expr ``(nontrivial %%α) >>= assert n,
apply_instance <|> `[solve_by_elim [nontrivial_of_ne, nontrivial_of_lt]],
reset_instance_cache
end tactic
namespace tactic.interactive
open tactic
setup_tactic_parser
/--
Attempts to generate a `nontrivial α` hypothesis.
The tactic first looks for an instance using `apply_instance`.
If the goal is an (in)equality, the type `α` is inferred from the goal.
Otherwise, the type needs to be specified in the tactic invocation, as `nontriviality α`.
The `nontriviality` tactic will first look for strict inequalities amongst the hypotheses,
and use these to derive the `nontrivial` instance directly.
Otherwise, it will perform a case split on `subsingleton α ∨ nontrivial α`, and attempt to discharge
the `subsingleton` goal using `simp [lemmas] with nontriviality`, where `[lemmas]` is a list of
additional `simp` lemmas that can be passed to `nontriviality` using the syntax
`nontriviality α using [lemmas]`.
```
example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : 0 < a :=
begin
nontriviality, -- There is now a `nontrivial R` hypothesis available.
assumption,
end
```
```
example {R : Type} [comm_ring R] {r s : R} : r * s = s * r :=
begin
nontriviality, -- There is now a `nontrivial R` hypothesis available.
apply mul_comm,
end
```
```
example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : (2 : ℕ) ∣ 4 :=
begin
nontriviality R, -- there is now a `nontrivial R` hypothesis available.
dec_trivial
end
```
```
def myeq {α : Type} (a b : α) : Prop := a = b
example {α : Type} (a b : α) (h : a = b) : myeq a b :=
begin
success_if_fail { nontriviality α }, -- Fails
nontriviality α using [myeq], -- There is now a `nontrivial α` hypothesis available
assumption
end
```
-/
meta def nontriviality (t : parse texpr?)
(lems : parse (tk "using" *> simp_arg_list <|> pure [])) :
tactic unit :=
do
α ← match t with
| some α := to_expr α
| none :=
(do t ← mk_mvar, e ← to_expr ``(@eq %%t _ _), target >>= unify e, return t) <|>
(do t ← mk_mvar, e ← to_expr ``(@has_le.le %%t _ _ _), target >>= unify e, return t) <|>
(do t ← mk_mvar, e ← to_expr ``(@ne %%t _ _), target >>= unify e, return t) <|>
(do t ← mk_mvar, e ← to_expr ``(@has_lt.lt %%t _ _ _), target >>= unify e, return t) <|>
fail "The goal is not an (in)equality, so you'll need to specify the desired `nontrivial α`
instance by invoking `nontriviality α`."
end,
nontriviality_by_assumption α <|> nontriviality_by_elim α lems
add_tactic_doc
{ name := "nontriviality",
category := doc_category.tactic,
decl_names := [`tactic.interactive.nontriviality],
tags := ["logic", "type class"] }
end tactic.interactive
namespace bool
instance : nontrivial bool := ⟨⟨tt,ff, tt_eq_ff_eq_false⟩⟩
end bool
|
f843f526003472016d149c0fbd8c56e792d137e4 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /06_Inductive_Types.org.48.lean | e38dec0d8375335020ee8196915dc7dc9a832de2 | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 352 | lean | /- page 93 -/
import standard
namespace hide
inductive eq {A : Type} (a : A) : A → Prop :=
refl : eq a a
inductive heq {A : Type} (a : A) : Π {B : Type}, B → Prop :=
refl : heq a a
-- BEGIN
theorem hcongr {A : Type} {B : A → Type} {a b : A} (f : Π x : A, B x)
(H : eq a b) : heq (f a) (f b) :=
eq.rec_on H (heq.refl (f a))
-- END
end hide
|
1916ba5e15724fc7b9d88774f8659026aff8a5f9 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/geometry/euclidean/circumcenter.lean | 41736c1778e66dcf559a4293e3088df0e2d61eea | [
"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 | 44,268 | lean | /-
Copyright (c) 2020 Joseph Myers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joseph Myers
-/
import geometry.euclidean.sphere.basic
import linear_algebra.affine_space.finite_dimensional
import tactic.derive_fintype
/-!
# Circumcenter and circumradius
This file proves some lemmas on points equidistant from a set of
points, and defines the circumradius and circumcenter of a simplex.
There are also some definitions for use in calculations where it is
convenient to work with affine combinations of vertices together with
the circumcenter.
## Main definitions
* `circumcenter` and `circumradius` are the circumcenter and
circumradius of a simplex.
## References
* https://en.wikipedia.org/wiki/Circumscribed_circle
-/
noncomputable theory
open_locale big_operators
open_locale classical
open_locale real_inner_product_space
namespace euclidean_geometry
variables {V : Type*} {P : Type*}
[normed_add_comm_group V] [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P]
include V
open affine_subspace
/-- `p` is equidistant from two points in `s` if and only if its
`orthogonal_projection` is. -/
lemma dist_eq_iff_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p1 p2 : P} (p3 : P) (hp1 : p1 ∈ s) (hp2 : p2 ∈ s) :
dist p1 p3 = dist p2 p3 ↔
dist p1 (orthogonal_projection s p3) = dist p2 (orthogonal_projection s p3) :=
begin
rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg,
←mul_self_inj_of_nonneg dist_nonneg dist_nonneg,
dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq
p3 hp1,
dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq
p3 hp2],
simp
end
/-- `p` is equidistant from a set of points in `s` if and only if its
`orthogonal_projection` is. -/
lemma dist_set_eq_iff_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {ps : set P} (hps : ps ⊆ s) (p : P) :
set.pairwise ps (λ p1 p2, dist p1 p = dist p2 p) ↔
(set.pairwise ps (λ p1 p2, dist p1 (orthogonal_projection s p) =
dist p2 (orthogonal_projection s p))) :=
⟨λ h p1 hp1 p2 hp2 hne,
(dist_eq_iff_dist_orthogonal_projection_eq p (hps hp1) (hps hp2)).1 (h hp1 hp2 hne),
λ h p1 hp1 p2 hp2 hne,
(dist_eq_iff_dist_orthogonal_projection_eq p (hps hp1) (hps hp2)).2 (h hp1 hp2 hne)⟩
/-- There exists `r` such that `p` has distance `r` from all the
points of a set of points in `s` if and only if there exists (possibly
different) `r` such that its `orthogonal_projection` has that distance
from all the points in that set. -/
lemma exists_dist_eq_iff_exists_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {ps : set P} (hps : ps ⊆ s) (p : P) :
(∃ r, ∀ p1 ∈ ps, dist p1 p = r) ↔
∃ r, ∀ p1 ∈ ps, dist p1 ↑(orthogonal_projection s p) = r :=
begin
have h := dist_set_eq_iff_dist_orthogonal_projection_eq hps p,
simp_rw set.pairwise_eq_iff_exists_eq at h,
exact h
end
/-- The induction step for the existence and uniqueness of the
circumcenter. Given a nonempty set of points in a nonempty affine
subspace whose direction is complete, such that there is a unique
(circumcenter, circumradius) pair for those points in that subspace,
and a point `p` not in that subspace, there is a unique (circumcenter,
circumradius) pair for the set with `p` added, in the span of the
subspace with `p` added. -/
lemma exists_unique_dist_eq_of_insert {s : affine_subspace ℝ P}
[complete_space s.direction] {ps : set P} (hnps : ps.nonempty) {p : P}
(hps : ps ⊆ s) (hp : p ∉ s)
(hu : ∃! cs : sphere P, cs.center ∈ s ∧ ps ⊆ (cs : set P)) :
∃! cs₂ : sphere P, cs₂.center ∈ affine_span ℝ (insert p (s : set P)) ∧
(insert p ps) ⊆ (cs₂ : set P) :=
begin
haveI : nonempty s := set.nonempty.to_subtype (hnps.mono hps),
rcases hu with ⟨⟨cc, cr⟩, ⟨hcc, hcr⟩, hcccru⟩,
simp only at hcc hcr hcccru,
let x := dist cc (orthogonal_projection s p),
let y := dist p (orthogonal_projection s p),
have hy0 : y ≠ 0 := dist_orthogonal_projection_ne_zero_of_not_mem hp,
let ycc₂ := (x * x + y * y - cr * cr) / (2 * y),
let cc₂ := (ycc₂ / y) • (p -ᵥ orthogonal_projection s p : V) +ᵥ cc,
let cr₂ := real.sqrt (cr * cr + ycc₂ * ycc₂),
use ⟨cc₂, cr₂⟩,
simp only,
have hpo : p = (1 : ℝ) • (p -ᵥ orthogonal_projection s p : V) +ᵥ orthogonal_projection s p,
{ simp },
split,
{ split,
{ refine vadd_mem_of_mem_direction _ (mem_affine_span ℝ (set.mem_insert_of_mem _ hcc)),
rw direction_affine_span,
exact submodule.smul_mem _ _
(vsub_mem_vector_span ℝ (set.mem_insert _ _)
(set.mem_insert_of_mem _ (orthogonal_projection_mem _))) },
{ intros p1 hp1,
rw [sphere.mem_coe, mem_sphere, ←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _),
real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _))],
cases hp1,
{ rw hp1,
rw [hpo,
dist_sq_smul_orthogonal_vadd_smul_orthogonal_vadd
(orthogonal_projection_mem p) hcc _ _
(vsub_orthogonal_projection_mem_direction_orthogonal s p),
←dist_eq_norm_vsub V p, dist_comm _ cc],
field_simp [hy0],
ring },
{ rw [dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq
_ (hps hp1),
orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc, subtype.coe_mk,
dist_of_mem_subset_mk_sphere hp1 hcr,
dist_eq_norm_vsub V cc₂ cc, vadd_vsub, norm_smul, ←dist_eq_norm_vsub V,
real.norm_eq_abs, abs_div, abs_of_nonneg dist_nonneg, div_mul_cancel _ hy0,
abs_mul_abs_self] } } },
{ rintros ⟨cc₃, cr₃⟩ ⟨hcc₃, hcr₃⟩,
simp only at hcc₃ hcr₃,
obtain ⟨t₃, cc₃', hcc₃', hcc₃''⟩ :
∃ (r : ℝ) (p0 : P) (hp0 : p0 ∈ s), cc₃ = r • (p -ᵥ ↑((orthogonal_projection s) p)) +ᵥ p0,
{ rwa mem_affine_span_insert_iff (orthogonal_projection_mem p) at hcc₃ },
have hcr₃' : ∃ r, ∀ p1 ∈ ps, dist p1 cc₃ = r :=
⟨cr₃, λ p1 hp1, dist_of_mem_subset_mk_sphere (set.mem_insert_of_mem _ hp1) hcr₃⟩,
rw [exists_dist_eq_iff_exists_dist_orthogonal_projection_eq hps cc₃, hcc₃'',
orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc₃'] at hcr₃',
cases hcr₃' with cr₃' hcr₃',
have hu := hcccru ⟨cc₃', cr₃'⟩,
simp only at hu,
replace hu := hu ⟨hcc₃', hcr₃'⟩,
cases hu with hucc hucr,
substs hucc hucr,
have hcr₃val : cr₃ = real.sqrt (cr₃' * cr₃' + (t₃ * y) * (t₃ * y)),
{ cases hnps with p0 hp0,
have h' : ↑(⟨cc₃', hcc₃'⟩ : s) = cc₃' := rfl,
rw [←dist_of_mem_subset_mk_sphere (set.mem_insert_of_mem _ hp0) hcr₃, hcc₃'',
←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _),
real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _)),
dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq
_ (hps hp0),
orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc₃', h',
dist_of_mem_subset_mk_sphere hp0 hcr,
dist_eq_norm_vsub V _ cc₃', vadd_vsub, norm_smul, ←dist_eq_norm_vsub V p,
real.norm_eq_abs, ←mul_assoc, mul_comm _ (|t₃|), ←mul_assoc, abs_mul_abs_self],
ring },
replace hcr₃ := dist_of_mem_subset_mk_sphere (set.mem_insert _ _) hcr₃,
rw [hpo, hcc₃'', hcr₃val, ←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _),
dist_sq_smul_orthogonal_vadd_smul_orthogonal_vadd
(orthogonal_projection_mem p) hcc₃' _ _
(vsub_orthogonal_projection_mem_direction_orthogonal s p),
dist_comm, ←dist_eq_norm_vsub V p,
real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _))] at hcr₃,
change x * x + _ * (y * y) = _ at hcr₃,
rw [(show x * x + (1 - t₃) * (1 - t₃) * (y * y) =
x * x + y * y - 2 * y * (t₃ * y) + t₃ * y * (t₃ * y), by ring), add_left_inj] at hcr₃,
have ht₃ : t₃ = ycc₂ / y,
{ field_simp [←hcr₃, hy0],
ring },
subst ht₃,
change cc₃ = cc₂ at hcc₃'',
congr',
rw hcr₃val,
congr' 2,
field_simp [hy0],
ring }
end
/-- Given a finite nonempty affinely independent family of points,
there is a unique (circumcenter, circumradius) pair for those points
in the affine subspace they span. -/
lemma _root_.affine_independent.exists_unique_dist_eq {ι : Type*} [hne : nonempty ι] [finite ι]
{p : ι → P} (ha : affine_independent ℝ p) :
∃! cs : sphere P, cs.center ∈ affine_span ℝ (set.range p) ∧ set.range p ⊆ (cs : set P) :=
begin
casesI nonempty_fintype ι,
unfreezingI { induction hn : fintype.card ι with m hm generalizing ι },
{ exfalso,
have h := fintype.card_pos_iff.2 hne,
rw hn at h,
exact lt_irrefl 0 h },
{ cases m,
{ rw fintype.card_eq_one_iff at hn,
cases hn with i hi,
haveI : unique ι := ⟨⟨i⟩, hi⟩,
use ⟨p i, 0⟩,
simp only [set.range_unique, affine_subspace.mem_affine_span_singleton],
split,
{ simp_rw [hi default, set.singleton_subset_iff, sphere.mem_coe, mem_sphere, dist_self],
exact ⟨rfl, rfl⟩ },
{ rintros ⟨cc, cr⟩,
simp only,
rintros ⟨rfl, hdist⟩,
simp_rw [set.singleton_subset_iff, sphere.mem_coe, mem_sphere, dist_self] at hdist,
rw [hi default, hdist],
exact ⟨rfl, rfl⟩ } },
{ have i := hne.some,
let ι2 := {x // x ≠ i},
have hc : fintype.card ι2 = m + 1,
{ rw fintype.card_of_subtype (finset.univ.filter (λ x, x ≠ i)),
{ rw finset.filter_not,
simp_rw eq_comm,
rw [finset.filter_eq, if_pos (finset.mem_univ _),
finset.card_sdiff (finset.subset_univ _), finset.card_singleton, finset.card_univ,
hn],
simp },
{ simp } },
haveI : nonempty ι2 := fintype.card_pos_iff.1 (hc.symm ▸ nat.zero_lt_succ _),
have ha2 : affine_independent ℝ (λ i2 : ι2, p i2) := ha.subtype _,
replace hm := hm ha2 _ hc,
have hr : set.range p = insert (p i) (set.range (λ i2 : ι2, p i2)),
{ change _ = insert _ (set.range (λ i2 : {x | x ≠ i}, p i2)),
rw [←set.image_eq_range, ←set.image_univ, ←set.image_insert_eq],
congr' with j,
simp [classical.em] },
rw [hr, ←affine_span_insert_affine_span],
refine exists_unique_dist_eq_of_insert
(set.range_nonempty _)
(subset_span_points ℝ _)
_
hm,
convert ha.not_mem_affine_span_diff i set.univ,
change set.range (λ i2 : {x | x ≠ i}, p i2) = _,
rw ←set.image_eq_range,
congr' with j, simp, refl } }
end
end euclidean_geometry
namespace affine
namespace simplex
open finset affine_subspace euclidean_geometry
variables {V : Type*} {P : Type*}
[normed_add_comm_group V] [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P]
include V
/-- The circumsphere of a simplex. -/
def circumsphere {n : ℕ} (s : simplex ℝ P n) : sphere P :=
s.independent.exists_unique_dist_eq.some
/-- The property satisfied by the circumsphere. -/
lemma circumsphere_unique_dist_eq {n : ℕ} (s : simplex ℝ P n) :
(s.circumsphere.center ∈ affine_span ℝ (set.range s.points) ∧
set.range s.points ⊆ s.circumsphere) ∧
(∀ cs : sphere P, (cs.center ∈ affine_span ℝ (set.range s.points) ∧
set.range s.points ⊆ cs → cs = s.circumsphere)) :=
s.independent.exists_unique_dist_eq.some_spec
/-- The circumcenter of a simplex. -/
def circumcenter {n : ℕ} (s : simplex ℝ P n) : P :=
s.circumsphere.center
/-- The circumradius of a simplex. -/
def circumradius {n : ℕ} (s : simplex ℝ P n) : ℝ :=
s.circumsphere.radius
/-- The center of the circumsphere is the circumcenter. -/
@[simp] lemma circumsphere_center {n : ℕ} (s : simplex ℝ P n) :
s.circumsphere.center = s.circumcenter :=
rfl
/-- The radius of the circumsphere is the circumradius. -/
@[simp] lemma circumsphere_radius {n : ℕ} (s : simplex ℝ P n) :
s.circumsphere.radius = s.circumradius :=
rfl
/-- The circumcenter lies in the affine span. -/
lemma circumcenter_mem_affine_span {n : ℕ} (s : simplex ℝ P n) :
s.circumcenter ∈ affine_span ℝ (set.range s.points) :=
s.circumsphere_unique_dist_eq.1.1
/-- All points have distance from the circumcenter equal to the
circumradius. -/
@[simp] lemma dist_circumcenter_eq_circumradius {n : ℕ} (s : simplex ℝ P n) (i : fin (n + 1)) :
dist (s.points i) s.circumcenter = s.circumradius :=
dist_of_mem_subset_sphere (set.mem_range_self _) s.circumsphere_unique_dist_eq.1.2
/-- All points lie in the circumsphere. -/
lemma mem_circumsphere {n : ℕ} (s : simplex ℝ P n) (i : fin (n + 1)) :
s.points i ∈ s.circumsphere :=
s.dist_circumcenter_eq_circumradius i
/-- All points have distance to the circumcenter equal to the
circumradius. -/
@[simp] lemma dist_circumcenter_eq_circumradius' {n : ℕ} (s : simplex ℝ P n) :
∀ i, dist s.circumcenter (s.points i) = s.circumradius :=
begin
intro i,
rw dist_comm,
exact dist_circumcenter_eq_circumradius _ _
end
/-- Given a point in the affine span from which all the points are
equidistant, that point is the circumcenter. -/
lemma eq_circumcenter_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P}
(hp : p ∈ affine_span ℝ (set.range s.points)) {r : ℝ} (hr : ∀ i, dist (s.points i) p = r) :
p = s.circumcenter :=
begin
have h := s.circumsphere_unique_dist_eq.2 ⟨p, r⟩,
simp only [hp, hr, forall_const, eq_self_iff_true, subset_sphere, sphere.ext_iff,
set.forall_range_iff, mem_sphere, true_and] at h,
exact h.1
end
/-- Given a point in the affine span from which all the points are
equidistant, that distance is the circumradius. -/
lemma eq_circumradius_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P}
(hp : p ∈ affine_span ℝ (set.range s.points)) {r : ℝ} (hr : ∀ i, dist (s.points i) p = r) :
r = s.circumradius :=
begin
have h := s.circumsphere_unique_dist_eq.2 ⟨p, r⟩,
simp only [hp, hr, forall_const, eq_self_iff_true, subset_sphere, sphere.ext_iff,
set.forall_range_iff, mem_sphere, true_and] at h,
exact h.2
end
/-- The circumradius is non-negative. -/
lemma circumradius_nonneg {n : ℕ} (s : simplex ℝ P n) : 0 ≤ s.circumradius :=
s.dist_circumcenter_eq_circumradius 0 ▸ dist_nonneg
/-- The circumradius of a simplex with at least two points is
positive. -/
lemma circumradius_pos {n : ℕ} (s : simplex ℝ P (n + 1)) : 0 < s.circumradius :=
begin
refine lt_of_le_of_ne s.circumradius_nonneg _,
intro h,
have hr := s.dist_circumcenter_eq_circumradius,
simp_rw [←h, dist_eq_zero] at hr,
have h01 := s.independent.injective.ne (dec_trivial : (0 : fin (n + 2)) ≠ 1),
simpa [hr] using h01
end
/-- The circumcenter of a 0-simplex equals its unique point. -/
lemma circumcenter_eq_point (s : simplex ℝ P 0) (i : fin 1) :
s.circumcenter = s.points i :=
begin
have h := s.circumcenter_mem_affine_span,
rw [set.range_unique, mem_affine_span_singleton] at h,
rw h,
congr
end
/-- The circumcenter of a 1-simplex equals its centroid. -/
lemma circumcenter_eq_centroid (s : simplex ℝ P 1) :
s.circumcenter = finset.univ.centroid ℝ s.points :=
begin
have hr : set.pairwise set.univ
(λ i j : fin 2, dist (s.points i) (finset.univ.centroid ℝ s.points) =
dist (s.points j) (finset.univ.centroid ℝ s.points)),
{ intros i hi j hj hij,
rw [finset.centroid_pair_fin, dist_eq_norm_vsub V (s.points i),
dist_eq_norm_vsub V (s.points j), vsub_vadd_eq_vsub_sub, vsub_vadd_eq_vsub_sub,
←one_smul ℝ (s.points i -ᵥ s.points 0), ←one_smul ℝ (s.points j -ᵥ s.points 0)],
fin_cases i; fin_cases j; simp [-one_smul, ←sub_smul]; norm_num },
rw set.pairwise_eq_iff_exists_eq at hr,
cases hr with r hr,
exact (s.eq_circumcenter_of_dist_eq
(centroid_mem_affine_span_of_card_eq_add_one ℝ _ (finset.card_fin 2))
(λ i, hr i (set.mem_univ _))).symm
end
/-- Reindexing a simplex along an `equiv` of index types does not change the circumsphere. -/
@[simp] lemma circumsphere_reindex {m n : ℕ} (s : simplex ℝ P m) (e : fin (m + 1) ≃ fin (n + 1)) :
(s.reindex e).circumsphere = s.circumsphere :=
begin
refine s.circumsphere_unique_dist_eq.2 _ ⟨_, _⟩; rw ←s.reindex_range_points e,
{ exact (s.reindex e).circumsphere_unique_dist_eq.1.1 },
{ exact (s.reindex e).circumsphere_unique_dist_eq.1.2 }
end
/-- Reindexing a simplex along an `equiv` of index types does not change the circumcenter. -/
@[simp] lemma circumcenter_reindex {m n : ℕ} (s : simplex ℝ P m) (e : fin (m + 1) ≃ fin (n + 1)) :
(s.reindex e).circumcenter = s.circumcenter :=
by simp_rw [←circumcenter, circumsphere_reindex]
/-- Reindexing a simplex along an `equiv` of index types does not change the circumradius. -/
@[simp] lemma circumradius_reindex {m n : ℕ} (s : simplex ℝ P m) (e : fin (m + 1) ≃ fin (n + 1)) :
(s.reindex e).circumradius = s.circumradius :=
by simp_rw [←circumradius, circumsphere_reindex]
local attribute [instance] affine_subspace.to_add_torsor
/-- The orthogonal projection of a point `p` onto the hyperplane spanned by the simplex's points. -/
def orthogonal_projection_span {n : ℕ} (s : simplex ℝ P n) :
P →ᵃ[ℝ] affine_span ℝ (set.range s.points) :=
orthogonal_projection (affine_span ℝ (set.range s.points))
/-- Adding a vector to a point in the given subspace, then taking the
orthogonal projection, produces the original point if the vector is a
multiple of the result of subtracting a point's orthogonal projection
from that point. -/
lemma orthogonal_projection_vadd_smul_vsub_orthogonal_projection
{n : ℕ} (s : simplex ℝ P n) {p1 : P} (p2 : P) (r : ℝ)
(hp : p1 ∈ affine_span ℝ (set.range s.points)) :
s.orthogonal_projection_span (r • (p2 -ᵥ s.orthogonal_projection_span p2 : V) +ᵥ p1) =
⟨p1, hp⟩ :=
orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ _
lemma coe_orthogonal_projection_vadd_smul_vsub_orthogonal_projection {n : ℕ} {r₁ : ℝ}
(s : simplex ℝ P n) {p p₁o : P} (hp₁o : p₁o ∈ affine_span ℝ (set.range s.points)) :
↑(s.orthogonal_projection_span (r₁ • (p -ᵥ ↑(s.orthogonal_projection_span p)) +ᵥ p₁o)) = p₁o :=
congr_arg coe (orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ _ hp₁o)
lemma dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq {n : ℕ}
(s : simplex ℝ P n) {p1 : P}
(p2 : P) (hp1 : p1 ∈ affine_span ℝ (set.range s.points)) :
dist p1 p2 * dist p1 p2 =
dist p1 (s.orthogonal_projection_span p2) * dist p1 (s.orthogonal_projection_span p2) +
dist p2 (s.orthogonal_projection_span p2) * dist p2 (s.orthogonal_projection_span p2) :=
begin
rw [pseudo_metric_space.dist_comm p2 _, dist_eq_norm_vsub V p1 _, dist_eq_norm_vsub V p1 _,
dist_eq_norm_vsub V _ p2, ← vsub_add_vsub_cancel p1 (s.orthogonal_projection_span p2) p2,
norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero],
exact submodule.inner_right_of_mem_orthogonal
(vsub_orthogonal_projection_mem_direction p2 hp1)
(orthogonal_projection_vsub_mem_direction_orthogonal _ p2),
end
lemma dist_circumcenter_sq_eq_sq_sub_circumradius {n : ℕ} {r : ℝ} (s : simplex ℝ P n)
{p₁ : P}
(h₁ : ∀ (i : fin (n + 1)), dist (s.points i) p₁ = r)
(h₁' : ↑((s.orthogonal_projection_span) p₁) = s.circumcenter)
(h : s.points 0 ∈ affine_span ℝ (set.range s.points)) :
dist p₁ s.circumcenter * dist p₁ s.circumcenter = r * r - s.circumradius * s.circumradius :=
begin
rw [dist_comm, ←h₁ 0,
s.dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq p₁ h],
simp only [h₁', dist_comm p₁, add_sub_cancel', simplex.dist_circumcenter_eq_circumradius],
end
/-- If there exists a distance that a point has from all vertices of a
simplex, the orthogonal projection of that point onto the subspace
spanned by that simplex is its circumcenter. -/
lemma orthogonal_projection_eq_circumcenter_of_exists_dist_eq {n : ℕ} (s : simplex ℝ P n)
{p : P} (hr : ∃ r, ∀ i, dist (s.points i) p = r) :
↑(s.orthogonal_projection_span p) = s.circumcenter :=
begin
change ∃ r : ℝ, ∀ i, (λ x, dist x p = r) (s.points i) at hr,
conv at hr { congr, funext, rw ←set.forall_range_iff },
rw exists_dist_eq_iff_exists_dist_orthogonal_projection_eq (subset_affine_span ℝ _) p at hr,
cases hr with r hr,
exact s.eq_circumcenter_of_dist_eq
(orthogonal_projection_mem p) (λ i, hr _ (set.mem_range_self i)),
end
/-- If a point has the same distance from all vertices of a simplex,
the orthogonal projection of that point onto the subspace spanned by
that simplex is its circumcenter. -/
lemma orthogonal_projection_eq_circumcenter_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P}
{r : ℝ} (hr : ∀ i, dist (s.points i) p = r) :
↑(s.orthogonal_projection_span p) = s.circumcenter :=
s.orthogonal_projection_eq_circumcenter_of_exists_dist_eq ⟨r, hr⟩
/-- The orthogonal projection of the circumcenter onto a face is the
circumcenter of that face. -/
lemma orthogonal_projection_circumcenter {n : ℕ} (s : simplex ℝ P n) {fs : finset (fin (n + 1))}
{m : ℕ} (h : fs.card = m + 1) :
↑((s.face h).orthogonal_projection_span s.circumcenter) = (s.face h).circumcenter :=
begin
have hr : ∃ r, ∀ i, dist ((s.face h).points i) s.circumcenter = r,
{ use s.circumradius,
simp [face_points] },
exact orthogonal_projection_eq_circumcenter_of_exists_dist_eq _ hr
end
/-- Two simplices with the same points have the same circumcenter. -/
lemma circumcenter_eq_of_range_eq {n : ℕ} {s₁ s₂ : simplex ℝ P n}
(h : set.range s₁.points = set.range s₂.points) : s₁.circumcenter = s₂.circumcenter :=
begin
have hs : s₁.circumcenter ∈ affine_span ℝ (set.range s₂.points) :=
h ▸ s₁.circumcenter_mem_affine_span,
have hr : ∀ i, dist (s₂.points i) s₁.circumcenter = s₁.circumradius,
{ intro i,
have hi : s₂.points i ∈ set.range s₂.points := set.mem_range_self _,
rw [←h, set.mem_range] at hi,
rcases hi with ⟨j, hj⟩,
rw [←hj, s₁.dist_circumcenter_eq_circumradius j] },
exact s₂.eq_circumcenter_of_dist_eq hs hr
end
omit V
/-- An index type for the vertices of a simplex plus its circumcenter.
This is for use in calculations where it is convenient to work with
affine combinations of vertices together with the circumcenter. (An
equivalent form sometimes used in the literature is placing the
circumcenter at the origin and working with vectors for the vertices.) -/
@[derive fintype]
inductive points_with_circumcenter_index (n : ℕ)
| point_index : fin (n + 1) → points_with_circumcenter_index
| circumcenter_index : points_with_circumcenter_index
open points_with_circumcenter_index
instance points_with_circumcenter_index_inhabited (n : ℕ) :
inhabited (points_with_circumcenter_index n) :=
⟨circumcenter_index⟩
/-- `point_index` as an embedding. -/
def point_index_embedding (n : ℕ) : fin (n + 1) ↪ points_with_circumcenter_index n :=
⟨λ i, point_index i, λ _ _ h, by injection h⟩
/-- The sum of a function over `points_with_circumcenter_index`. -/
lemma sum_points_with_circumcenter {α : Type*} [add_comm_monoid α] {n : ℕ}
(f : points_with_circumcenter_index n → α) :
∑ i, f i = (∑ (i : fin (n + 1)), f (point_index i)) + f circumcenter_index :=
begin
have h : univ = insert circumcenter_index (univ.map (point_index_embedding n)),
{ ext x,
refine ⟨λ h, _, λ _, mem_univ _⟩,
cases x with i,
{ exact mem_insert_of_mem (mem_map_of_mem _ (mem_univ i)) },
{ exact mem_insert_self _ _ } },
change _ = ∑ i, f (point_index_embedding n i) + _,
rw [add_comm, h, ←sum_map, sum_insert],
simp_rw [finset.mem_map, not_exists],
intros x hx h,
injection h
end
include V
/-- The vertices of a simplex plus its circumcenter. -/
def points_with_circumcenter {n : ℕ} (s : simplex ℝ P n) : points_with_circumcenter_index n → P
| (point_index i) := s.points i
| circumcenter_index := s.circumcenter
/-- `points_with_circumcenter`, applied to a `point_index` value,
equals `points` applied to that value. -/
@[simp] lemma points_with_circumcenter_point {n : ℕ} (s : simplex ℝ P n) (i : fin (n + 1)) :
s.points_with_circumcenter (point_index i) = s.points i :=
rfl
/-- `points_with_circumcenter`, applied to `circumcenter_index`, equals the
circumcenter. -/
@[simp] lemma points_with_circumcenter_eq_circumcenter {n : ℕ} (s : simplex ℝ P n) :
s.points_with_circumcenter circumcenter_index = s.circumcenter :=
rfl
omit V
/-- The weights for a single vertex of a simplex, in terms of
`points_with_circumcenter`. -/
def point_weights_with_circumcenter {n : ℕ} (i : fin (n + 1)) : points_with_circumcenter_index n → ℝ
| (point_index j) := if j = i then 1 else 0
| circumcenter_index := 0
/-- `point_weights_with_circumcenter` sums to 1. -/
@[simp] lemma sum_point_weights_with_circumcenter {n : ℕ} (i : fin (n + 1)) :
∑ j, point_weights_with_circumcenter i j = 1 :=
begin
convert sum_ite_eq' univ (point_index i) (function.const _ (1 : ℝ)),
{ ext j,
cases j ; simp [point_weights_with_circumcenter] },
{ simp }
end
include V
/-- A single vertex, in terms of `points_with_circumcenter`. -/
lemma point_eq_affine_combination_of_points_with_circumcenter {n : ℕ} (s : simplex ℝ P n)
(i : fin (n + 1)) :
s.points i =
(univ : finset (points_with_circumcenter_index n)).affine_combination ℝ
s.points_with_circumcenter (point_weights_with_circumcenter i) :=
begin
rw ←points_with_circumcenter_point,
symmetry,
refine affine_combination_of_eq_one_of_eq_zero _ _ _
(mem_univ _)
(by simp [point_weights_with_circumcenter])
_,
intros i hi hn,
cases i,
{ have h : i_1 ≠ i := λ h, hn (h ▸ rfl),
simp [point_weights_with_circumcenter, h] },
{ refl }
end
omit V
/-- The weights for the centroid of some vertices of a simplex, in
terms of `points_with_circumcenter`. -/
def centroid_weights_with_circumcenter {n : ℕ} (fs : finset (fin (n + 1)))
: points_with_circumcenter_index n → ℝ
| (point_index i) := if i ∈ fs then ((card fs : ℝ) ⁻¹) else 0
| circumcenter_index := 0
/-- `centroid_weights_with_circumcenter` sums to 1, if the `finset` is
nonempty. -/
@[simp] lemma sum_centroid_weights_with_circumcenter {n : ℕ} {fs : finset (fin (n + 1))}
(h : fs.nonempty) :
∑ i, centroid_weights_with_circumcenter fs i = 1 :=
begin
simp_rw [sum_points_with_circumcenter, centroid_weights_with_circumcenter, add_zero,
←fs.sum_centroid_weights_eq_one_of_nonempty ℝ h,
set.sum_indicator_subset _ fs.subset_univ],
rcongr
end
include V
/-- The centroid of some vertices of a simplex, in terms of
`points_with_circumcenter`. -/
lemma centroid_eq_affine_combination_of_points_with_circumcenter {n : ℕ} (s : simplex ℝ P n)
(fs : finset (fin (n + 1))) :
fs.centroid ℝ s.points =
(univ : finset (points_with_circumcenter_index n)).affine_combination ℝ
s.points_with_circumcenter (centroid_weights_with_circumcenter fs) :=
begin
simp_rw [centroid_def, affine_combination_apply,
weighted_vsub_of_point_apply, sum_points_with_circumcenter,
centroid_weights_with_circumcenter, points_with_circumcenter_point, zero_smul,
add_zero, centroid_weights,
set.sum_indicator_subset_of_eq_zero
(function.const (fin (n + 1)) ((card fs : ℝ)⁻¹))
(λ i wi, wi • (s.points i -ᵥ classical.choice add_torsor.nonempty))
fs.subset_univ
(λ i, zero_smul ℝ _),
set.indicator_apply],
congr,
end
omit V
/-- The weights for the circumcenter of a simplex, in terms of
`points_with_circumcenter`. -/
def circumcenter_weights_with_circumcenter (n : ℕ) : points_with_circumcenter_index n → ℝ
| (point_index i) := 0
| circumcenter_index := 1
/-- `circumcenter_weights_with_circumcenter` sums to 1. -/
@[simp] lemma sum_circumcenter_weights_with_circumcenter (n : ℕ) :
∑ i, circumcenter_weights_with_circumcenter n i = 1 :=
begin
convert sum_ite_eq' univ circumcenter_index (function.const _ (1 : ℝ)),
{ ext ⟨j⟩ ; simp [circumcenter_weights_with_circumcenter] },
{ simp }
end
include V
/-- The circumcenter of a simplex, in terms of
`points_with_circumcenter`. -/
lemma circumcenter_eq_affine_combination_of_points_with_circumcenter {n : ℕ}
(s : simplex ℝ P n) :
s.circumcenter = (univ : finset (points_with_circumcenter_index n)).affine_combination ℝ
s.points_with_circumcenter (circumcenter_weights_with_circumcenter n) :=
begin
rw ←points_with_circumcenter_eq_circumcenter,
symmetry,
refine affine_combination_of_eq_one_of_eq_zero _ _ _ (mem_univ _) rfl _,
rintros ⟨i⟩ hi hn ; tauto
end
omit V
/-- The weights for the reflection of the circumcenter in an edge of a
simplex. This definition is only valid with `i₁ ≠ i₂`. -/
def reflection_circumcenter_weights_with_circumcenter {n : ℕ} (i₁ i₂ : fin (n + 1)) :
points_with_circumcenter_index n → ℝ
| (point_index i) := if i = i₁ ∨ i = i₂ then 1 else 0
| circumcenter_index := -1
/-- `reflection_circumcenter_weights_with_circumcenter` sums to 1. -/
@[simp] lemma sum_reflection_circumcenter_weights_with_circumcenter {n : ℕ} {i₁ i₂ : fin (n + 1)}
(h : i₁ ≠ i₂) : ∑ i, reflection_circumcenter_weights_with_circumcenter i₁ i₂ i = 1 :=
begin
simp_rw [sum_points_with_circumcenter, reflection_circumcenter_weights_with_circumcenter,
sum_ite, sum_const, filter_or, filter_eq'],
rw card_union_eq,
{ simp },
{ simpa only [if_true, mem_univ, disjoint_singleton] using h }
end
include V
/-- The reflection of the circumcenter of a simplex in an edge, in
terms of `points_with_circumcenter`. -/
lemma reflection_circumcenter_eq_affine_combination_of_points_with_circumcenter {n : ℕ}
(s : simplex ℝ P n) {i₁ i₂ : fin (n + 1)} (h : i₁ ≠ i₂) :
reflection (affine_span ℝ (s.points '' {i₁, i₂})) s.circumcenter =
(univ : finset (points_with_circumcenter_index n)).affine_combination ℝ
s.points_with_circumcenter (reflection_circumcenter_weights_with_circumcenter i₁ i₂) :=
begin
have hc : card ({i₁, i₂} : finset (fin (n + 1))) = 2,
{ simp [h] },
-- Making the next line a separate definition helps the elaborator:
set W : affine_subspace ℝ P := affine_span ℝ (s.points '' {i₁, i₂}) with W_def,
have h_faces : ↑(orthogonal_projection W s.circumcenter)
= ↑((s.face hc).orthogonal_projection_span s.circumcenter),
{ apply eq_orthogonal_projection_of_eq_subspace,
simp },
rw [euclidean_geometry.reflection_apply, h_faces, s.orthogonal_projection_circumcenter hc,
circumcenter_eq_centroid, s.face_centroid_eq_centroid hc,
centroid_eq_affine_combination_of_points_with_circumcenter,
circumcenter_eq_affine_combination_of_points_with_circumcenter, ←@vsub_eq_zero_iff_eq V,
affine_combination_vsub, weighted_vsub_vadd_affine_combination, affine_combination_vsub,
weighted_vsub_apply, sum_points_with_circumcenter],
simp_rw [pi.sub_apply, pi.add_apply, pi.sub_apply, sub_smul, add_smul, sub_smul,
centroid_weights_with_circumcenter, circumcenter_weights_with_circumcenter,
reflection_circumcenter_weights_with_circumcenter, ite_smul, zero_smul, sub_zero,
apply_ite2 (+), add_zero, ←add_smul, hc, zero_sub, neg_smul, sub_self, add_zero],
convert sum_const_zero,
norm_num
end
end simplex
end affine
namespace euclidean_geometry
open affine affine_subspace finite_dimensional
variables {V : Type*} {P : Type*}
[normed_add_comm_group V] [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P]
include V
/-- Given a nonempty affine subspace, whose direction is complete,
that contains a set of points, those points are cospherical if and
only if they are equidistant from some point in that subspace. -/
lemma cospherical_iff_exists_mem_of_complete {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s)
[nonempty s] [complete_space s.direction] :
cospherical ps ↔ ∃ (center ∈ s) (radius : ℝ), ∀ p ∈ ps, dist p center = radius :=
begin
split,
{ rintro ⟨c, hcr⟩,
rw exists_dist_eq_iff_exists_dist_orthogonal_projection_eq h c at hcr,
exact ⟨orthogonal_projection s c, orthogonal_projection_mem _, hcr⟩ },
{ exact λ ⟨c, hc, hd⟩, ⟨c, hd⟩ }
end
/-- Given a nonempty affine subspace, whose direction is
finite-dimensional, that contains a set of points, those points are
cospherical if and only if they are equidistant from some point in
that subspace. -/
lemma cospherical_iff_exists_mem_of_finite_dimensional {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] [finite_dimensional ℝ s.direction] :
cospherical ps ↔ ∃ (center ∈ s) (radius : ℝ), ∀ p ∈ ps, dist p center = radius :=
cospherical_iff_exists_mem_of_complete h
/-- All n-simplices among cospherical points in an n-dimensional
subspace have the same circumradius. -/
lemma exists_circumradius_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) :
∃ r : ℝ, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumradius = r :=
begin
rw cospherical_iff_exists_mem_of_finite_dimensional h at hc,
rcases hc with ⟨c, hc, r, hcr⟩,
use r,
intros sx hsxps,
have hsx : affine_span ℝ (set.range sx.points) = s,
{ refine sx.independent.affine_span_eq_of_le_of_card_eq_finrank_add_one
(span_points_subset_coe_of_subset_coe (hsxps.trans h)) _,
simp [hd] },
have hc : c ∈ affine_span ℝ (set.range sx.points) := hsx.symm ▸ hc,
exact (sx.eq_circumradius_of_dist_eq
hc
(λ i, hcr (sx.points i) (hsxps (set.mem_range_self i)))).symm
end
/-- Two n-simplices among cospherical points in an n-dimensional
subspace have the same circumradius. -/
lemma circumradius_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumradius = sx₂.circumradius :=
begin
rcases exists_circumradius_eq_of_cospherical_subset h hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- All n-simplices among cospherical points in n-space have the same
circumradius. -/
lemma exists_circumradius_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) :
∃ r : ℝ, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumradius = r :=
begin
haveI : nonempty (⊤ : affine_subspace ℝ P) := set.univ.nonempty,
rw [←finrank_top, ←direction_top ℝ V P] at hd,
refine exists_circumradius_eq_of_cospherical_subset _ hd hc,
exact set.subset_univ _
end
/-- Two n-simplices among cospherical points in n-space have the same
circumradius. -/
lemma circumradius_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumradius = sx₂.circumradius :=
begin
rcases exists_circumradius_eq_of_cospherical hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- All n-simplices among cospherical points in an n-dimensional
subspace have the same circumcenter. -/
lemma exists_circumcenter_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) :
∃ c : P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumcenter = c :=
begin
rw cospherical_iff_exists_mem_of_finite_dimensional h at hc,
rcases hc with ⟨c, hc, r, hcr⟩,
use c,
intros sx hsxps,
have hsx : affine_span ℝ (set.range sx.points) = s,
{ refine sx.independent.affine_span_eq_of_le_of_card_eq_finrank_add_one
(span_points_subset_coe_of_subset_coe (hsxps.trans h)) _,
simp [hd] },
have hc : c ∈ affine_span ℝ (set.range sx.points) := hsx.symm ▸ hc,
exact (sx.eq_circumcenter_of_dist_eq
hc
(λ i, hcr (sx.points i) (hsxps (set.mem_range_self i)))).symm
end
/-- Two n-simplices among cospherical points in an n-dimensional
subspace have the same circumcenter. -/
lemma circumcenter_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumcenter = sx₂.circumcenter :=
begin
rcases exists_circumcenter_eq_of_cospherical_subset h hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- All n-simplices among cospherical points in n-space have the same
circumcenter. -/
lemma exists_circumcenter_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) :
∃ c : P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumcenter = c :=
begin
haveI : nonempty (⊤ : affine_subspace ℝ P) := set.univ.nonempty,
rw [←finrank_top, ←direction_top ℝ V P] at hd,
refine exists_circumcenter_eq_of_cospherical_subset _ hd hc,
exact set.subset_univ _
end
/-- Two n-simplices among cospherical points in n-space have the same
circumcenter. -/
lemma circumcenter_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumcenter = sx₂.circumcenter :=
begin
rcases exists_circumcenter_eq_of_cospherical hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- All n-simplices among cospherical points in an n-dimensional
subspace have the same circumsphere. -/
lemma exists_circumsphere_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) :
∃ c : sphere P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumsphere = c :=
begin
obtain ⟨r, hr⟩ := exists_circumradius_eq_of_cospherical_subset h hd hc,
obtain ⟨c, hc⟩ := exists_circumcenter_eq_of_cospherical_subset h hd hc,
exact ⟨⟨c, r⟩, λ sx hsx, sphere.ext _ _ (hc sx hsx) (hr sx hsx)⟩
end
/-- Two n-simplices among cospherical points in an n-dimensional
subspace have the same circumsphere. -/
lemma circumsphere_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumsphere = sx₂.circumsphere :=
begin
rcases exists_circumsphere_eq_of_cospherical_subset h hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- All n-simplices among cospherical points in n-space have the same
circumsphere. -/
lemma exists_circumsphere_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) :
∃ c : sphere P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumsphere = c :=
begin
haveI : nonempty (⊤ : affine_subspace ℝ P) := set.univ.nonempty,
rw [←finrank_top, ←direction_top ℝ V P] at hd,
refine exists_circumsphere_eq_of_cospherical_subset _ hd hc,
exact set.subset_univ _
end
/-- Two n-simplices among cospherical points in n-space have the same
circumsphere. -/
lemma circumsphere_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumsphere = sx₂.circumsphere :=
begin
rcases exists_circumsphere_eq_of_cospherical hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- Suppose all distances from `p₁` and `p₂` to the points of a
simplex are equal, and that `p₁` and `p₂` lie in the affine span of
`p` with the vertices of that simplex. Then `p₁` and `p₂` are equal
or reflections of each other in the affine span of the vertices of the
simplex. -/
lemma eq_or_eq_reflection_of_dist_eq {n : ℕ} {s : simplex ℝ P n} {p p₁ p₂ : P} {r : ℝ}
(hp₁ : p₁ ∈ affine_span ℝ (insert p (set.range s.points)))
(hp₂ : p₂ ∈ affine_span ℝ (insert p (set.range s.points)))
(h₁ : ∀ i, dist (s.points i) p₁ = r) (h₂ : ∀ i, dist (s.points i) p₂ = r) :
p₁ = p₂ ∨ p₁ = reflection (affine_span ℝ (set.range s.points)) p₂ :=
begin
let span_s := affine_span ℝ (set.range s.points),
have h₁' := s.orthogonal_projection_eq_circumcenter_of_dist_eq h₁,
have h₂' := s.orthogonal_projection_eq_circumcenter_of_dist_eq h₂,
rw [←affine_span_insert_affine_span,
mem_affine_span_insert_iff (orthogonal_projection_mem p)] at hp₁ hp₂,
obtain ⟨r₁, p₁o, hp₁o, hp₁⟩ := hp₁,
obtain ⟨r₂, p₂o, hp₂o, hp₂⟩ := hp₂,
obtain rfl : ↑(s.orthogonal_projection_span p₁) = p₁o,
{ subst hp₁, exact s.coe_orthogonal_projection_vadd_smul_vsub_orthogonal_projection hp₁o },
rw h₁' at hp₁,
obtain rfl : ↑(s.orthogonal_projection_span p₂) = p₂o,
{ subst hp₂, exact s.coe_orthogonal_projection_vadd_smul_vsub_orthogonal_projection hp₂o },
rw h₂' at hp₂,
have h : s.points 0 ∈ span_s := mem_affine_span ℝ (set.mem_range_self _),
have hd₁ : dist p₁ s.circumcenter * dist p₁ s.circumcenter =
r * r - s.circumradius * s.circumradius :=
s.dist_circumcenter_sq_eq_sq_sub_circumradius h₁ h₁' h,
have hd₂ : dist p₂ s.circumcenter * dist p₂ s.circumcenter =
r * r - s.circumradius * s.circumradius :=
s.dist_circumcenter_sq_eq_sq_sub_circumradius h₂ h₂' h,
rw [←hd₂, hp₁, hp₂, dist_eq_norm_vsub V _ s.circumcenter,
dist_eq_norm_vsub V _ s.circumcenter, vadd_vsub, vadd_vsub, ←real_inner_self_eq_norm_mul_norm,
←real_inner_self_eq_norm_mul_norm, real_inner_smul_left, real_inner_smul_left,
real_inner_smul_right, real_inner_smul_right, ←mul_assoc, ←mul_assoc] at hd₁,
by_cases hp : p = s.orthogonal_projection_span p,
{ rw simplex.orthogonal_projection_span at hp,
rw [hp₁, hp₂, ←hp],
simp only [true_or, eq_self_iff_true, smul_zero, vsub_self] },
{ have hz : ⟪p -ᵥ orthogonal_projection span_s p, p -ᵥ orthogonal_projection span_s p⟫ ≠ 0,
by simpa only [ne.def, vsub_eq_zero_iff_eq, inner_self_eq_zero] using hp,
rw [mul_left_inj' hz, mul_self_eq_mul_self_iff] at hd₁,
rw [hp₁, hp₂],
cases hd₁,
{ left,
rw hd₁ },
{ right,
rw [hd₁,
reflection_vadd_smul_vsub_orthogonal_projection p r₂ s.circumcenter_mem_affine_span,
neg_smul] } }
end
end euclidean_geometry
|
6971a86999a23d467b4ba35a1aaadb66b4fedc8f | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /test/integration.lean | 1bcdadde7aed59376ac7e41a4a969fa44aac6018 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 5,068 | lean | /-
Copyright (c) 2021 Benjamin Davidson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Benjamin Davidson
-/
import analysis.special_functions.integrals
open interval_integral real
open_locale real
/-! ### Simple functions -/
/- constants -/
example : ∫ x : ℝ in 8..11, (1 : ℝ) = 3 := by norm_num
example : ∫ x : ℝ in 5..19, (12 : ℝ) = 168 := by norm_num
/- the identity function -/
example : ∫ x : ℝ in (-1)..4, x = 15 / 2 := by norm_num
example : ∫ x : ℝ in 4..5, x * 2 = 9 := by norm_num
/- inverse -/
example : ∫ x : ℝ in 2..3, x⁻¹ = log (3 / 2) := by norm_num
/- natural powers -/
example : ∫ x : ℝ in 2..4, x ^ (3 : ℕ) = 60 := by norm_num
/- trigonometric functions -/
example : ∫ x in 0..π, sin x = 2 := by norm_num
example : ∫ x in 0..π/4, cos x = sqrt 2 / 2 := by simp
example : ∫ x in 0..π, 2 * sin x = 4 := by norm_num
example : ∫ x in 0..π/2, cos x / 2 = 1 / 2 := by simp
example : ∫ x : ℝ in 0..1, 1 / (1 + x ^ 2) = π / 4 := by simp
example : ∫ x in 0..2*π, sin x ^ 2 = π := by simp [mul_div_cancel_left]
example : ∫ x in 0..π/2, cos x ^ 2 / 2 = π / 8 := by norm_num [div_div_eq_div_mul]
example : ∫ x in 0..π, cos x ^ 2 - sin x ^ 2 = 0 := by simp [integral_cos_sq_sub_sin_sq]
example : ∫ x in 0..π/2, sin x ^ 3 = 2 / 3 := by norm_num
example : ∫ x in 0..π/2, cos x ^ 3 = 2 / 3 := by norm_num
example : ∫ x in 0..π, sin x * cos x = 0 := by simp
example : ∫ x in 0..π, sin x ^ 2 * cos x ^ 2 = π / 8 := by simpa using sin_nat_mul_pi 4
/- the exponential function -/
example : ∫ x in 0..2, -exp x = 1 - exp 2 := by simp
/- the logarithmic function -/
example : ∫ x in 1..2, log x = 2 * log 2 - 1 := by { norm_num, ring }
/- linear combinations (e.g. polynomials) -/
example : ∫ x : ℝ in 0..2, 6*x^5 + 3*x^4 + x^3 - 2*x^2 + x - 7 = 1048 / 15 := by norm_num
example : ∫ x : ℝ in 0..1, exp x + 9 * x^8 + x^3 - x/2 + (1 + x^2)⁻¹ = exp 1 + π / 4 := by norm_num
/-! ### Functions composed with multiplication by and/or addition of a constant -/
/- many examples are computable by `norm_num` -/
example : ∫ x in 0..2, -exp (-x) = exp (-2) - 1 := by norm_num
example : ∫ x in 1..2, exp (5*x - 5) = 1/5 * (exp 5 - 1) := by norm_num
example : ∫ x in 0..π, cos (x/2) = 2 := by norm_num
example : ∫ x in 0..π/4, sin (2*x) = 1/2 := by norm_num [mul_div_comm, mul_one_div]
example (ω φ : ℝ) : ω * ∫ θ in 0..π, sin (ω*θ + φ) = cos φ - cos (ω*π + φ) := by simp
/- some examples may require a bit of algebraic massaging -/
example {L : ℝ} (h : L ≠ 0) : ∫ x in 0..2/L*π, sin (L/2 * x) = 4 / L :=
begin
norm_num [div_ne_zero h, ← mul_assoc],
field_simp [h, mul_div_cancel],
norm_num,
end
/- you may need to provide `norm_num` with the composition lemma you are invoking if it has a
difficult time recognizing the function you are trying to integrate -/
example : ∫ x : ℝ in 0..2, 3 * (x + 1) ^ 2 = 26 :=
by norm_num [integral_comp_add_right (λ x, x ^ 2)]
example : ∫ x : ℝ in -1..0, (1 + (x + 1) ^ 2)⁻¹ = π / 4 :=
by simp [integral_comp_add_right (λ x, (1 + x ^ 2)⁻¹)]
/-! ### Compositions of functions (aka "change of variables" or "integration by substitution") -/
/- `interval_integral.integral_comp_mul_deriv` can be used to simplify integrals of the form
`∫ x in a..b, (g ∘ f) x * f' x`, where `f'` is the derivative of `f`, to `∫ x in f a..f b, g x` -/
example {a b : ℝ} : ∫ x in a..b, exp (exp x) * exp x = ∫ x in exp a..exp b, exp x :=
integral_comp_mul_deriv (λ x hx, has_deriv_at_exp x) continuous_on_exp continuous_exp
/- if it is known (to mathlib), the integral of `g` can then be evaluated using `simp`/`norm_num` -/
example : ∫ x in 0..1, exp (exp x) * exp x = exp (exp 1) - exp 1 :=
by rw integral_comp_mul_deriv (λ x hx, has_deriv_at_exp x) continuous_on_exp continuous_exp; simp
/- a more detailed example -/
example : ∫ x in 0..2, exp (x ^ 2) * (2 * x) = exp 4 - 1 :=
begin -- let g := exp x, f := x ^ 2, f' := 2 * x
rw integral_comp_mul_deriv (λ x hx, _), -- simplify to ∫ x in f 0..f 2, g x
{ norm_num }, -- compute the integral
{ exact continuous_on_const.mul continuous_on_id }, -- show that f' is continuous on [0, 2]
{ exact continuous_exp }, -- show that g is continuous
{ simpa using has_deriv_at_pow 2 x }, -- show that f' = derivative of f on [0, 2]
end
/- alternatively, `interval_integral.integral_deriv_comp_mul_deriv` can be used to compute integrals
of this same form, provided that you also know that `g` is the derivative of some function -/
example : ∫ x : ℝ in 0..1, exp (x ^ 2) * (2 * x) = exp 1 - 1 :=
begin
rw integral_deriv_comp_mul_deriv (λ x hx, _) (λ x hx, has_deriv_at_exp (x^2)) _ continuous_exp,
{ simp },
{ simpa using has_deriv_at_pow 2 x },
{ exact continuous_on_const.mul continuous_on_id },
end
|
98ff4da5793b5ba117b4811141b91628ba6f987b | 94e33a31faa76775069b071adea97e86e218a8ee | /src/linear_algebra/sesquilinear_form.lean | b2e7c5f5bdde05a9595624b317f7f618e7f57153 | [
"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 | 27,291 | lean | /-
Copyright (c) 2018 Andreas Swerdlow. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andreas Swerdlow
-/
import algebra.module.linear_map
import linear_algebra.bilinear_map
import linear_algebra.matrix.basis
import linear_algebra.linear_pmap
/-!
# Sesquilinear form
This files provides properties about sesquilinear forms. The maps considered are of the form
`M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R`, where `I₁ : R₁ →+* R` and `I₂ : R₂ →+* R` are ring homomorphisms and
`M₁` is a module over `R₁` and `M₂` is a module over `R₂`.
Sesquilinear forms are the special case that `M₁ = M₂`, `R₁ = R₂ = R`, and `I₁ = ring_hom.id R`.
Taking additionally `I₂ = ring_hom.id R`, then one obtains bilinear forms.
These forms are a special case of the bilinear maps defined in `bilinear_map.lean` and all basic
lemmas about construction and elementary calculations are found there.
## Main declarations
* `is_ortho`: states that two vectors are orthogonal with respect to a sesquilinear form
* `is_symm`, `is_alt`: states that a sesquilinear form is symmetric and alternating, respectively
* `orthogonal_bilin`: provides the orthogonal complement with respect to sesquilinear form
## References
* <https://en.wikipedia.org/wiki/Sesquilinear_form#Over_arbitrary_rings>
## Tags
Sesquilinear form,
-/
open_locale big_operators
variables {R R₁ R₂ R₃ M M₁ M₂ K K₁ K₂ V V₁ V₂ n: Type*}
namespace linear_map
/-! ### Orthogonal vectors -/
section comm_ring
-- the `ₗ` subscript variables are for special cases about linear (as opposed to semilinear) maps
variables [comm_semiring R] [comm_semiring R₁] [add_comm_monoid M₁] [module R₁ M₁]
[comm_semiring R₂] [add_comm_monoid M₂] [module R₂ M₂]
{I₁ : R₁ →+* R} {I₂ : R₂ →+* R} {I₁' : R₁ →+* R}
/-- The proposition that two elements of a sesquilinear form space are orthogonal -/
def is_ortho (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) (x y) : Prop := B x y = 0
lemma is_ortho_def {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} {x y} : B.is_ortho x y ↔ B x y = 0 := iff.rfl
lemma is_ortho_zero_left (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) (x) : is_ortho B (0 : M₁) x :=
by { dunfold is_ortho, rw [ map_zero B, zero_apply] }
lemma is_ortho_zero_right (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) (x) : is_ortho B x (0 : M₂) :=
map_zero (B x)
lemma is_ortho_flip {B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₁'] R} {x y} :
B.is_ortho x y ↔ B.flip.is_ortho y x :=
by simp_rw [is_ortho_def, flip_apply]
/-- A set of vectors `v` is orthogonal with respect to some bilinear form `B` if and only
if for all `i ≠ j`, `B (v i) (v j) = 0`. For orthogonality between two elements, use
`bilin_form.is_ortho` -/
def is_Ortho (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₁'] R) (v : n → M₁) : Prop :=
pairwise (B.is_ortho on v)
lemma is_Ortho_def {B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₁'] R} {v : n → M₁} :
B.is_Ortho v ↔ ∀ i j : n, i ≠ j → B (v i) (v j) = 0 := iff.rfl
lemma is_Ortho_flip (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₁'] R) {v : n → M₁} :
B.is_Ortho v ↔ B.flip.is_Ortho v :=
begin
simp_rw is_Ortho_def,
split; intros h i j hij,
{ rw flip_apply,
exact h j i (ne.symm hij) },
simp_rw flip_apply at h,
exact h j i (ne.symm hij),
end
end comm_ring
section field
variables [field K] [field K₁] [add_comm_group V₁] [module K₁ V₁]
[field K₂] [add_comm_group V₂] [module K₂ V₂]
{I₁ : K₁ →+* K} {I₂ : K₂ →+* K} {I₁' : K₁ →+* K}
{J₁ : K →+* K} {J₂ : K →+* K}
-- todo: this also holds for [comm_ring R] [is_domain R] when J₁ is invertible
lemma ortho_smul_left {B : V₁ →ₛₗ[I₁] V₂ →ₛₗ[I₂] K} {x y} {a : K₁} (ha : a ≠ 0) :
(is_ortho B x y) ↔ (is_ortho B (a • x) y) :=
begin
dunfold is_ortho,
split; intro H,
{ rw [map_smulₛₗ₂, H, smul_zero]},
{ rw [map_smulₛₗ₂, smul_eq_zero] at H,
cases H,
{ rw I₁.map_eq_zero at H, trivial },
{ exact H }}
end
-- todo: this also holds for [comm_ring R] [is_domain R] when J₂ is invertible
lemma ortho_smul_right {B : V₁ →ₛₗ[I₁] V₂ →ₛₗ[I₂] K} {x y} {a : K₂} {ha : a ≠ 0} :
(is_ortho B x y) ↔ (is_ortho B x (a • y)) :=
begin
dunfold is_ortho,
split; intro H,
{ rw [map_smulₛₗ, H, smul_zero] },
{ rw [map_smulₛₗ, smul_eq_zero] at H,
cases H,
{ simp at H,
exfalso,
exact ha H },
{ exact H }}
end
/-- A set of orthogonal vectors `v` with respect to some sesquilinear form `B` is linearly
independent if for all `i`, `B (v i) (v i) ≠ 0`. -/
lemma linear_independent_of_is_Ortho {B : V₁ →ₛₗ[I₁] V₁ →ₛₗ[I₁'] K} {v : n → V₁}
(hv₁ : B.is_Ortho v) (hv₂ : ∀ i, ¬ B.is_ortho (v i) (v i)) : linear_independent K₁ v :=
begin
classical,
rw linear_independent_iff',
intros s w hs i hi,
have : B (s.sum $ λ (i : n), w i • v i) (v i) = 0,
{ rw [hs, map_zero, zero_apply] },
have hsum : s.sum (λ (j : n), I₁(w j) * B (v j) (v i)) = I₁(w i) * B (v i) (v i),
{ apply finset.sum_eq_single_of_mem i hi,
intros j hj hij,
rw [is_Ortho_def.1 hv₁ _ _ hij, mul_zero], },
simp_rw [B.map_sum₂, map_smulₛₗ₂, smul_eq_mul, hsum] at this,
apply I₁.map_eq_zero.mp,
exact eq_zero_of_ne_zero_of_mul_right_eq_zero (hv₂ i) this,
end
end field
/-! ### Reflexive bilinear forms -/
section reflexive
variables [comm_semiring R] [comm_semiring R₁] [add_comm_monoid M₁] [module R₁ M₁]
{I₁ : R₁ →+* R} {I₂ : R₁ →+* R}
{B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R}
/-- The proposition that a sesquilinear form is reflexive -/
def is_refl (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R) : Prop :=
∀ (x y), B x y = 0 → B y x = 0
namespace is_refl
variable (H : B.is_refl)
lemma eq_zero : ∀ {x y}, B x y = 0 → B y x = 0 := λ x y, H x y
lemma ortho_comm {x y} : is_ortho B x y ↔ is_ortho B y x := ⟨eq_zero H, eq_zero H⟩
lemma dom_restrict_refl (H : B.is_refl) (p : submodule R₁ M₁) : (B.dom_restrict₁₂ p p).is_refl :=
λ _ _, by { simp_rw dom_restrict₁₂_apply, exact H _ _}
@[simp] lemma flip_is_refl_iff : B.flip.is_refl ↔ B.is_refl :=
⟨λ h x y H, h y x ((B.flip_apply _ _).trans H), λ h x y, h y x⟩
lemma ker_flip_eq_bot (H : B.is_refl) (h : B.ker = ⊥) : B.flip.ker = ⊥ :=
begin
refine ker_eq_bot'.mpr (λ _ hx, ker_eq_bot'.mp h _ _),
ext,
exact H _ _ (linear_map.congr_fun hx _),
end
lemma ker_eq_bot_iff_ker_flip_eq_bot (H : B.is_refl) : B.ker = ⊥ ↔ B.flip.ker = ⊥ :=
begin
refine ⟨ker_flip_eq_bot H, λ h, _⟩,
exact (congr_arg _ B.flip_flip.symm).trans (ker_flip_eq_bot (flip_is_refl_iff.mpr H) h),
end
end is_refl
end reflexive
/-! ### Symmetric bilinear forms -/
section symmetric
variables [comm_semiring R] [add_comm_monoid M] [module R M]
{I : R →+* R} {B : M →ₛₗ[I] M →ₗ[R] R}
/-- The proposition that a sesquilinear form is symmetric -/
def is_symm (B : M →ₛₗ[I] M →ₗ[R] R) : Prop :=
∀ (x y), I (B x y) = B y x
namespace is_symm
protected lemma eq (H : B.is_symm) (x y) : I (B x y) = B y x := H x y
lemma is_refl (H : B.is_symm) : B.is_refl := λ x y H1, by { rw ←H.eq, simp [H1] }
lemma ortho_comm (H : B.is_symm) {x y} : is_ortho B x y ↔ is_ortho B y x := H.is_refl.ortho_comm
lemma dom_restrict_symm (H : B.is_symm) (p : submodule R M) : (B.dom_restrict₁₂ p p).is_symm :=
λ _ _, by { simp_rw dom_restrict₁₂_apply, exact H _ _}
end is_symm
lemma is_symm_iff_eq_flip {B : M →ₗ[R] M →ₗ[R] R} : B.is_symm ↔ B = B.flip :=
begin
split; intro h,
{ ext,
rw [←h, flip_apply, ring_hom.id_apply] },
intros x y,
conv_lhs { rw h },
rw [flip_apply, ring_hom.id_apply],
end
end symmetric
/-! ### Alternating bilinear forms -/
section alternating
variables [comm_ring R] [comm_semiring R₁] [add_comm_monoid M₁] [module R₁ M₁]
{I₁ : R₁ →+* R} {I₂ : R₁ →+* R} {I : R₁ →+* R} {B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R}
/-- The proposition that a sesquilinear form is alternating -/
def is_alt (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R) : Prop := ∀ x, B x x = 0
namespace is_alt
variable (H : B.is_alt)
include H
lemma self_eq_zero (x) : B x x = 0 := H x
lemma neg (x y) : - B x y = B y x :=
begin
have H1 : B (y + x) (y + x) = 0,
{ exact self_eq_zero H (y + x) },
simp [map_add, self_eq_zero H] at H1,
rw [add_eq_zero_iff_neg_eq] at H1,
exact H1,
end
lemma is_refl : B.is_refl :=
begin
intros x y h,
rw [←neg H, h, neg_zero],
end
lemma ortho_comm {x y} : is_ortho B x y ↔ is_ortho B y x := H.is_refl.ortho_comm
end is_alt
lemma is_alt_iff_eq_neg_flip [no_zero_divisors R] [char_zero R] {B : M₁ →ₛₗ[I] M₁ →ₛₗ[I] R} :
B.is_alt ↔ B = -B.flip :=
begin
split; intro h,
{ ext,
simp_rw [neg_apply, flip_apply],
exact (h.neg _ _).symm },
intros x,
let h' := congr_fun₂ h x x,
simp only [neg_apply, flip_apply, ←add_eq_zero_iff_eq_neg] at h',
exact add_self_eq_zero.mp h',
end
end alternating
end linear_map
namespace submodule
/-! ### The orthogonal complement -/
variables [comm_ring R] [comm_ring R₁] [add_comm_group M₁] [module R₁ M₁]
{I₁ : R₁ →+* R} {I₂ : R₁ →+* R}
{B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R}
/-- The orthogonal complement of a submodule `N` with respect to some bilinear form is the set of
elements `x` which are orthogonal to all elements of `N`; i.e., for all `y` in `N`, `B x y = 0`.
Note that for general (neither symmetric nor antisymmetric) bilinear forms this definition has a
chirality; in addition to this "left" orthogonal complement one could define a "right" orthogonal
complement for which, for all `y` in `N`, `B y x = 0`. This variant definition is not currently
provided in mathlib. -/
def orthogonal_bilin (N : submodule R₁ M₁) (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R) : submodule R₁ M₁ :=
{ carrier := { m | ∀ n ∈ N, B.is_ortho n m },
zero_mem' := λ x _, B.is_ortho_zero_right x,
add_mem' := λ x y hx hy n hn,
by rw [linear_map.is_ortho, map_add, show B n x = 0, by exact hx n hn,
show B n y = 0, by exact hy n hn, zero_add],
smul_mem' := λ c x hx n hn,
by rw [linear_map.is_ortho, linear_map.map_smulₛₗ, show B n x = 0, by exact hx n hn,
smul_zero] }
variables {N L : submodule R₁ M₁}
@[simp] lemma mem_orthogonal_bilin_iff {m : M₁} :
m ∈ N.orthogonal_bilin B ↔ ∀ n ∈ N, B.is_ortho n m := iff.rfl
lemma orthogonal_bilin_le (h : N ≤ L) : L.orthogonal_bilin B ≤ N.orthogonal_bilin B :=
λ _ hn l hl, hn l (h hl)
lemma le_orthogonal_bilin_orthogonal_bilin (b : B.is_refl) :
N ≤ (N.orthogonal_bilin B).orthogonal_bilin B :=
λ n hn m hm, b _ _ (hm n hn)
end submodule
namespace linear_map
section orthogonal
variables [field K] [add_comm_group V] [module K V]
[field K₁] [add_comm_group V₁] [module K₁ V₁]
{J : K →+* K} {J₁ : K₁ →+* K} {J₁' : K₁ →+* K}
-- ↓ This lemma only applies in fields as we require `a * b = 0 → a = 0 ∨ b = 0`
lemma span_singleton_inf_orthogonal_eq_bot
(B : V₁ →ₛₗ[J₁] V₁ →ₛₗ[J₁'] K) (x : V₁) (hx : ¬ B.is_ortho x x) :
(K₁ ∙ x) ⊓ submodule.orthogonal_bilin (K₁ ∙ x) B = ⊥ :=
begin
rw ← finset.coe_singleton,
refine eq_bot_iff.2 (λ y h, _),
rcases mem_span_finset.1 h.1 with ⟨μ, rfl⟩,
have := h.2 x _,
{ rw finset.sum_singleton at this ⊢,
suffices hμzero : μ x = 0,
{ rw [hμzero, zero_smul, submodule.mem_bot] },
change B x (μ x • x) = 0 at this, rw [map_smulₛₗ, smul_eq_mul] at this,
exact or.elim (zero_eq_mul.mp this.symm)
(λ y, by { simp at y, exact y })
(λ hfalse, false.elim $ hx hfalse) },
{ rw submodule.mem_span; exact λ _ hp, hp $ finset.mem_singleton_self _ }
end
-- ↓ This lemma only applies in fields since we use the `mul_eq_zero`
lemma orthogonal_span_singleton_eq_to_lin_ker {B : V →ₗ[K] V →ₛₗ[J] K} (x : V) :
submodule.orthogonal_bilin (K ∙ x) B = (B x).ker :=
begin
ext y,
simp_rw [submodule.mem_orthogonal_bilin_iff, linear_map.mem_ker,
submodule.mem_span_singleton ],
split,
{ exact λ h, h x ⟨1, one_smul _ _⟩ },
{ rintro h _ ⟨z, rfl⟩,
rw [is_ortho, map_smulₛₗ₂, smul_eq_zero],
exact or.intro_right _ h }
end
-- todo: Generalize this to sesquilinear maps
lemma span_singleton_sup_orthogonal_eq_top {B : V →ₗ[K] V →ₗ[K] K}
{x : V} (hx : ¬ B.is_ortho x x) :
(K ∙ x) ⊔ submodule.orthogonal_bilin (K ∙ x) B = ⊤ :=
begin
rw orthogonal_span_singleton_eq_to_lin_ker,
exact (B x).span_singleton_sup_ker_eq_top hx,
end
-- todo: Generalize this to sesquilinear maps
/-- Given a bilinear form `B` and some `x` such that `B x x ≠ 0`, the span of the singleton of `x`
is complement to its orthogonal complement. -/
lemma is_compl_span_singleton_orthogonal {B : V →ₗ[K] V →ₗ[K] K}
{x : V} (hx : ¬ B.is_ortho x x) : is_compl (K ∙ x) (submodule.orthogonal_bilin (K ∙ x) B) :=
{ disjoint := eq_bot_iff.1 $ span_singleton_inf_orthogonal_eq_bot B x hx,
codisjoint := eq_top_iff.1 $ span_singleton_sup_orthogonal_eq_top hx }
end orthogonal
/-! ### Adjoint pairs -/
section adjoint_pair
section add_comm_monoid
variables [comm_semiring R]
variables [add_comm_monoid M] [module R M]
variables [add_comm_monoid M₁] [module R M₁]
variables [add_comm_monoid M₂] [module R M₂]
variables {B F : M →ₗ[R] M →ₗ[R] R} {B' : M₁ →ₗ[R] M₁ →ₗ[R] R} {B'' : M₂ →ₗ[R] M₂ →ₗ[R] R}
variables {f f' : M →ₗ[R] M₁} {g g' : M₁ →ₗ[R] M}
variables (B B' f g)
/-- Given a pair of modules equipped with bilinear forms, this is the condition for a pair of
maps between them to be mutually adjoint. -/
def is_adjoint_pair := ∀ x y, B' (f x) y = B x (g y)
variables {B B' f g}
lemma is_adjoint_pair_iff_comp_eq_compl₂ :
is_adjoint_pair B B' f g ↔ B'.comp f = B.compl₂ g :=
begin
split; intros h,
{ ext x y, rw [comp_apply, compl₂_apply], exact h x y },
{ intros _ _, rw [←compl₂_apply, ←comp_apply, h] },
end
lemma is_adjoint_pair_zero : is_adjoint_pair B B' 0 0 :=
λ _ _, by simp only [zero_apply, map_zero]
lemma is_adjoint_pair_id : is_adjoint_pair B B 1 1 := λ x y, rfl
lemma is_adjoint_pair.add (h : is_adjoint_pair B B' f g) (h' : is_adjoint_pair B B' f' g') :
is_adjoint_pair B B' (f + f') (g + g') :=
λ x _, by rw [f.add_apply, g.add_apply, B'.map_add₂, (B x).map_add, h, h']
lemma is_adjoint_pair.comp {f' : M₁ →ₗ[R] M₂} {g' : M₂ →ₗ[R] M₁}
(h : is_adjoint_pair B B' f g) (h' : is_adjoint_pair B' B'' f' g') :
is_adjoint_pair B B'' (f'.comp f) (g.comp g') :=
λ _ _, by rw [linear_map.comp_apply, linear_map.comp_apply, h', h]
lemma is_adjoint_pair.mul
{f g f' g' : module.End R M} (h : is_adjoint_pair B B f g) (h' : is_adjoint_pair B B f' g') :
is_adjoint_pair B B (f * f') (g' * g) :=
h'.comp h
end add_comm_monoid
section add_comm_group
variables [comm_ring R]
variables [add_comm_group M] [module R M]
variables [add_comm_group M₁] [module R M₁]
variables {B F : M →ₗ[R] M →ₗ[R] R} {B' : M₁ →ₗ[R] M₁ →ₗ[R] R}
variables {f f' : M →ₗ[R] M₁} {g g' : M₁ →ₗ[R] M}
lemma is_adjoint_pair.sub (h : is_adjoint_pair B B' f g) (h' : is_adjoint_pair B B' f' g') :
is_adjoint_pair B B' (f - f') (g - g') :=
λ x _, by rw [f.sub_apply, g.sub_apply, B'.map_sub₂, (B x).map_sub, h, h']
lemma is_adjoint_pair.smul (c : R) (h : is_adjoint_pair B B' f g) :
is_adjoint_pair B B' (c • f) (c • g) :=
λ _ _, by simp only [smul_apply, map_smul, smul_eq_mul, h _ _]
end add_comm_group
end adjoint_pair
/-! ### Self-adjoint pairs-/
section selfadjoint_pair
section add_comm_monoid
variables [comm_semiring R]
variables [add_comm_monoid M] [module R M]
variables (B F : M →ₗ[R] M →ₗ[R] R)
/-- The condition for an endomorphism to be "self-adjoint" with respect to a pair of bilinear forms
on the underlying module. In the case that these two forms are identical, this is the usual concept
of self adjointness. In the case that one of the forms is the negation of the other, this is the
usual concept of skew adjointness. -/
def is_pair_self_adjoint (f : module.End R M) := is_adjoint_pair B F f f
/-- An endomorphism of a module is self-adjoint with respect to a bilinear form if it serves as an
adjoint for itself. -/
protected def is_self_adjoint (f : module.End R M) := is_adjoint_pair B B f f
end add_comm_monoid
section add_comm_group
variables [comm_ring R]
variables [add_comm_group M] [module R M]
variables [add_comm_group M₁] [module R M₁]
(B F : M →ₗ[R] M →ₗ[R] R)
/-- The set of pair-self-adjoint endomorphisms are a submodule of the type of all endomorphisms. -/
def is_pair_self_adjoint_submodule : submodule R (module.End R M) :=
{ carrier := { f | is_pair_self_adjoint B F f },
zero_mem' := is_adjoint_pair_zero,
add_mem' := λ f g hf hg, hf.add hg,
smul_mem' := λ c f h, h.smul c, }
/-- An endomorphism of a module is skew-adjoint with respect to a bilinear form if its negation
serves as an adjoint. -/
def is_skew_adjoint (f : module.End R M) := is_adjoint_pair B B f (-f)
/-- The set of self-adjoint endomorphisms of a module with bilinear form is a submodule. (In fact
it is a Jordan subalgebra.) -/
def self_adjoint_submodule := is_pair_self_adjoint_submodule B B
/-- The set of skew-adjoint endomorphisms of a module with bilinear form is a submodule. (In fact
it is a Lie subalgebra.) -/
def skew_adjoint_submodule := is_pair_self_adjoint_submodule (-B) B
variables {B F}
@[simp] lemma mem_is_pair_self_adjoint_submodule (f : module.End R M) :
f ∈ is_pair_self_adjoint_submodule B F ↔ is_pair_self_adjoint B F f :=
iff.rfl
lemma is_pair_self_adjoint_equiv (e : M₁ ≃ₗ[R] M) (f : module.End R M) :
is_pair_self_adjoint B F f ↔
is_pair_self_adjoint (B.compl₁₂ ↑e ↑e) (F.compl₁₂ ↑e ↑e) (e.symm.conj f) :=
begin
have hₗ : (F.compl₁₂ (↑e : M₁ →ₗ[R] M) (↑e : M₁ →ₗ[R] M)).comp (e.symm.conj f) =
(F.comp f).compl₁₂ (↑e : M₁ →ₗ[R] M) (↑e : M₁ →ₗ[R] M) :=
by { ext, simp only [linear_equiv.symm_conj_apply, coe_comp, linear_equiv.coe_coe, compl₁₂_apply,
linear_equiv.apply_symm_apply], },
have hᵣ : (B.compl₁₂ (↑e : M₁ →ₗ[R] M) (↑e : M₁ →ₗ[R] M)).compl₂ (e.symm.conj f) =
(B.compl₂ f).compl₁₂ (↑e : M₁ →ₗ[R] M) (↑e : M₁ →ₗ[R] M) :=
by { ext, simp only [linear_equiv.symm_conj_apply, compl₂_apply, coe_comp, linear_equiv.coe_coe,
compl₁₂_apply, linear_equiv.apply_symm_apply] },
have he : function.surjective (⇑(↑e : M₁ →ₗ[R] M) : M₁ → M) := e.surjective,
simp_rw [is_pair_self_adjoint, is_adjoint_pair_iff_comp_eq_compl₂, hₗ, hᵣ,
compl₁₂_inj he he],
end
lemma is_skew_adjoint_iff_neg_self_adjoint (f : module.End R M) :
B.is_skew_adjoint f ↔ is_adjoint_pair (-B) B f f :=
show (∀ x y, B (f x) y = B x ((-f) y)) ↔ ∀ x y, B (f x) y = (-B) x (f y),
by simp
@[simp] lemma mem_self_adjoint_submodule (f : module.End R M) :
f ∈ B.self_adjoint_submodule ↔ B.is_self_adjoint f := iff.rfl
@[simp] lemma mem_skew_adjoint_submodule (f : module.End R M) :
f ∈ B.skew_adjoint_submodule ↔ B.is_skew_adjoint f :=
by { rw is_skew_adjoint_iff_neg_self_adjoint, exact iff.rfl }
end add_comm_group
end selfadjoint_pair
/-! ### Nondegenerate bilinear forms -/
section nondegenerate
section comm_semiring
variables [comm_semiring R] [comm_semiring R₁] [add_comm_monoid M₁] [module R₁ M₁]
[comm_semiring R₂] [add_comm_monoid M₂] [module R₂ M₂]
{I₁ : R₁ →+* R} {I₂ : R₂ →+* R} {I₁' : R₁ →+* R}
/-- A bilinear form is called left-separating if
the only element that is left-orthogonal to every other element is `0`; i.e.,
for every nonzero `x` in `M₁`, there exists `y` in `M₂` with `B x y ≠ 0`.-/
def separating_left (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) : Prop :=
∀ x : M₁, (∀ y : M₂, B x y = 0) → x = 0
/-- A bilinear form is called right-separating if
the only element that is right-orthogonal to every other element is `0`; i.e.,
for every nonzero `y` in `M₂`, there exists `x` in `M₁` with `B x y ≠ 0`.-/
def separating_right (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) : Prop :=
∀ y : M₂, (∀ x : M₁, B x y = 0) → y = 0
/-- A bilinear form is called non-degenerate if it is left-separating and right-separating. -/
def nondegenerate (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) : Prop := separating_left B ∧ separating_right B
@[simp] lemma flip_separating_right {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} :
B.flip.separating_right ↔ B.separating_left := ⟨λ hB x hy, hB x hy, λ hB x hy, hB x hy⟩
@[simp] lemma flip_separating_left {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} :
B.flip.separating_left ↔ separating_right B := by rw [←flip_separating_right, flip_flip]
@[simp] lemma flip_nondegenerate {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} :
B.flip.nondegenerate ↔ B.nondegenerate :=
iff.trans and.comm (and_congr flip_separating_right flip_separating_left)
lemma separating_left_iff_linear_nontrivial {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} :
B.separating_left ↔ ∀ x : M₁, B x = 0 → x = 0 :=
begin
split; intros h x hB,
{ let h' := h x,
simp only [hB, zero_apply, eq_self_iff_true, forall_const] at h',
exact h' },
have h' : B x = 0 := by { ext, rw [zero_apply], exact hB _ },
exact h x h',
end
lemma separating_right_iff_linear_flip_nontrivial {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} :
B.separating_right ↔ ∀ y : M₂, B.flip y = 0 → y = 0 :=
by rw [←flip_separating_left, separating_left_iff_linear_nontrivial]
/-- A bilinear form is left-separating if and only if it has a trivial kernel. -/
theorem separating_left_iff_ker_eq_bot {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} :
B.separating_left ↔ B.ker = ⊥ :=
iff.trans separating_left_iff_linear_nontrivial linear_map.ker_eq_bot'.symm
/-- A bilinear form is right-separating if and only if its flip has a trivial kernel. -/
theorem separating_right_iff_flip_ker_eq_bot {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} :
B.separating_right ↔ B.flip.ker = ⊥ :=
by rw [←flip_separating_left, separating_left_iff_ker_eq_bot]
end comm_semiring
section comm_ring
variables [comm_ring R] [add_comm_group M] [module R M]
{I I' : R →+* R}
lemma is_refl.nondegenerate_of_separating_left {B : M →ₗ[R] M →ₗ[R] R}
(hB : B.is_refl) (hB' : B.separating_left) : B.nondegenerate :=
begin
refine ⟨hB', _⟩,
rw [separating_right_iff_flip_ker_eq_bot, hB.ker_eq_bot_iff_ker_flip_eq_bot.mp],
rwa ←separating_left_iff_ker_eq_bot,
end
lemma is_refl.nondegenerate_of_separating_right {B : M →ₗ[R] M →ₗ[R] R}
(hB : B.is_refl) (hB' : B.separating_right) : B.nondegenerate :=
begin
refine ⟨_, hB'⟩,
rw [separating_left_iff_ker_eq_bot, hB.ker_eq_bot_iff_ker_flip_eq_bot.mpr],
rwa ←separating_right_iff_flip_ker_eq_bot,
end
/-- The restriction of a reflexive bilinear form `B` onto a submodule `W` is
nondegenerate if `W` has trivial intersection with its orthogonal complement,
that is `disjoint W (W.orthogonal_bilin B)`. -/
lemma nondegenerate_restrict_of_disjoint_orthogonal
{B : M →ₗ[R] M →ₗ[R] R} (hB : B.is_refl)
{W : submodule R M} (hW : disjoint W (W.orthogonal_bilin B)) :
(B.dom_restrict₁₂ W W).nondegenerate :=
begin
refine (hB.dom_restrict_refl W).nondegenerate_of_separating_left _,
rintro ⟨x, hx⟩ b₁,
rw [submodule.mk_eq_zero, ← submodule.mem_bot R],
refine hW ⟨hx, λ y hy, _⟩,
specialize b₁ ⟨y, hy⟩,
simp_rw [dom_restrict₁₂_apply, submodule.coe_mk] at b₁,
rw hB.ortho_comm,
exact b₁,
end
/-- An orthogonal basis with respect to a left-separating bilinear form has no self-orthogonal
elements. -/
lemma is_Ortho.not_is_ortho_basis_self_of_separating_left [nontrivial R]
{B : M →ₛₗ[I] M →ₛₗ[I'] R} {v : basis n R M} (h : B.is_Ortho v) (hB : B.separating_left)
(i : n) : ¬B.is_ortho (v i) (v i) :=
begin
intro ho,
refine v.ne_zero i (hB (v i) $ λ m, _),
obtain ⟨vi, rfl⟩ := v.repr.symm.surjective m,
rw [basis.repr_symm_apply, finsupp.total_apply, finsupp.sum, map_sum],
apply finset.sum_eq_zero,
rintros j -,
rw map_smulₛₗ,
convert mul_zero _ using 2,
obtain rfl | hij := eq_or_ne i j,
{ exact ho },
{ exact h i j hij },
end
/-- An orthogonal basis with respect to a right-separating bilinear form has no self-orthogonal
elements. -/
lemma is_Ortho.not_is_ortho_basis_self_of_separating_right [nontrivial R]
{B : M →ₛₗ[I] M →ₛₗ[I'] R} {v : basis n R M} (h : B.is_Ortho v) (hB : B.separating_right)
(i : n) : ¬B.is_ortho (v i) (v i) :=
begin
rw is_Ortho_flip at h,
rw is_ortho_flip,
exact h.not_is_ortho_basis_self_of_separating_left (flip_separating_left.mpr hB) i,
end
/-- Given an orthogonal basis with respect to a bilinear form, the bilinear form is left-separating
if the basis has no elements which are self-orthogonal. -/
lemma is_Ortho.separating_left_of_not_is_ortho_basis_self [no_zero_divisors R]
{B : M →ₗ[R] M →ₗ[R] R} (v : basis n R M) (hO : B.is_Ortho v) (h : ∀ i, ¬B.is_ortho (v i) (v i)) :
B.separating_left :=
begin
intros m hB,
obtain ⟨vi, rfl⟩ := v.repr.symm.surjective m,
rw linear_equiv.map_eq_zero_iff,
ext i,
rw [finsupp.zero_apply],
specialize hB (v i),
simp_rw [basis.repr_symm_apply, finsupp.total_apply, finsupp.sum, map_sum₂, map_smulₛₗ₂,
smul_eq_mul] at hB,
rw finset.sum_eq_single i at hB,
{ exact eq_zero_of_ne_zero_of_mul_right_eq_zero (h i) hB, },
{ intros j hj hij, convert mul_zero _ using 2, exact hO j i hij, },
{ intros hi, convert zero_mul _ using 2, exact finsupp.not_mem_support_iff.mp hi }
end
/-- Given an orthogonal basis with respect to a bilinear form, the bilinear form is right-separating
if the basis has no elements which are self-orthogonal. -/
lemma is_Ortho.separating_right_iff_not_is_ortho_basis_self [no_zero_divisors R]
{B : M →ₗ[R] M →ₗ[R] R} (v : basis n R M) (hO : B.is_Ortho v) (h : ∀ i, ¬B.is_ortho (v i) (v i)) :
B.separating_right :=
begin
rw is_Ortho_flip at hO,
rw [←flip_separating_left],
refine is_Ortho.separating_left_of_not_is_ortho_basis_self v hO (λ i, _),
rw is_ortho_flip,
exact h i,
end
/-- Given an orthogonal basis with respect to a bilinear form, the bilinear form is nondegenerate
if the basis has no elements which are self-orthogonal. -/
lemma is_Ortho.nondegenerate_of_not_is_ortho_basis_self [no_zero_divisors R]
{B : M →ₗ[R] M →ₗ[R] R} (v : basis n R M) (hO : B.is_Ortho v) (h : ∀ i, ¬B.is_ortho (v i) (v i)) :
B.nondegenerate :=
⟨is_Ortho.separating_left_of_not_is_ortho_basis_self v hO h,
is_Ortho.separating_right_iff_not_is_ortho_basis_self v hO h⟩
end comm_ring
end nondegenerate
end linear_map
|
62e388d385c969dda5d9913644626e67d28bb0d8 | 958488bc7f3c2044206e0358e56d7690b6ae696c | /lean/while/Env.lean | f898e4a733c428832f3723d39be9232a3949fe08 | [] | no_license | possientis/Prog | a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4 | d4b3debc37610a88e0dac3ac5914903604fd1d1f | refs/heads/master | 1,692,263,717,723 | 1,691,757,179,000 | 1,691,757,179,000 | 40,361,602 | 3 | 0 | null | 1,679,896,438,000 | 1,438,953,859,000 | Coq | UTF-8 | Lean | false | false | 154 | lean | def Env : Type := string → ℕ
def env : list (string × ℕ) → Env
| [] := λ s, 0
| ((v,n) :: xs) := λ s, if s = v then n else env xs s
|
24341e3bb0b7b8978ec59517c76bc169be30d347 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/data/sum.lean | 6d4abf963fbc082a4cc7fd7ce1f5a274f3bd1a57 | [
"Apache-2.0"
] | permissive | anthony2698/mathlib | 03cd69fe5c280b0916f6df2d07c614c8e1efe890 | 407615e05814e98b24b2ff322b14e8e3eb5e5d67 | refs/heads/master | 1,678,792,774,873 | 1,614,371,563,000 | 1,614,371,563,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,459 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Yury G. Kudryashov
-/
import logic.function.basic
/-!
# More theorems about the sum type
-/
universes u v w x
variables {α : Type u} {α' : Type w} {β : Type v} {β' : Type x}
open sum
/-- Check if a sum is `inl` and if so, retrieve its contents. -/
@[simp] def sum.get_left {α β} : α ⊕ β → option α
| (inl a) := some a
| (inr _) := none
/-- Check if a sum is `inr` and if so, retrieve its contents. -/
@[simp] def sum.get_right {α β} : α ⊕ β → option β
| (inr b) := some b
| (inl _) := none
/-- Check if a sum is `inl`. -/
@[simp] def sum.is_left {α β} : α ⊕ β → bool
| (inl _) := tt
| (inr _) := ff
/-- Check if a sum is `inr`. -/
@[simp] def sum.is_right {α β} : α ⊕ β → bool
| (inl _) := ff
| (inr _) := tt
attribute [derive decidable_eq] sum
@[simp] theorem sum.forall {p : α ⊕ β → Prop} : (∀ x, p x) ↔ (∀ a, p (inl a)) ∧ (∀ b, p (inr b)) :=
⟨λ h, ⟨λ a, h _, λ b, h _⟩, λ ⟨h₁, h₂⟩, sum.rec h₁ h₂⟩
@[simp] theorem sum.exists {p : α ⊕ β → Prop} : (∃ x, p x) ↔ (∃ a, p (inl a)) ∨ ∃ b, p (inr b) :=
⟨λ h, match h with
| ⟨inl a, h⟩ := or.inl ⟨a, h⟩
| ⟨inr b, h⟩ := or.inr ⟨b, h⟩
end, λ h, match h with
| or.inl ⟨a, h⟩ := ⟨inl a, h⟩
| or.inr ⟨b, h⟩ := ⟨inr b, h⟩
end⟩
namespace sum
lemma injective_inl : function.injective (sum.inl : α → α ⊕ β) :=
λ x y, sum.inl.inj
lemma injective_inr : function.injective (sum.inr : β → α ⊕ β) :=
λ x y, sum.inr.inj
/-- Map `α ⊕ β` to `α' ⊕ β'` sending `α` to `α'` and `β` to `β'`. -/
protected def map (f : α → α') (g : β → β') : α ⊕ β → α' ⊕ β'
| (sum.inl x) := sum.inl (f x)
| (sum.inr x) := sum.inr (g x)
@[simp] lemma map_inl (f : α → α') (g : β → β') (x : α) : (inl x).map f g = inl (f x) := rfl
@[simp] lemma map_inr (f : α → α') (g : β → β') (x : β) : (inr x).map f g = inr (g x) := rfl
@[simp] lemma map_map {α'' β''} (f' : α' → α'') (g' : β' → β'') (f : α → α') (g : β → β') :
∀ x : α ⊕ β, (x.map f g).map f' g' = x.map (f' ∘ f) (g' ∘ g)
| (inl a) := rfl
| (inr b) := rfl
@[simp] lemma map_comp_map {α'' β''} (f' : α' → α'') (g' : β' → β'') (f : α → α') (g : β → β') :
(sum.map f' g') ∘ (sum.map f g) = sum.map (f' ∘ f) (g' ∘ g) :=
funext $ map_map f' g' f g
@[simp] lemma map_id_id (α β) : sum.map (@id α) (@id β) = id :=
funext $ λ x, sum.rec_on x (λ _, rfl) (λ _, rfl)
theorem inl.inj_iff {a b} : (inl a : α ⊕ β) = inl b ↔ a = b :=
⟨inl.inj, congr_arg _⟩
theorem inr.inj_iff {a b} : (inr a : α ⊕ β) = inr b ↔ a = b :=
⟨inr.inj, congr_arg _⟩
theorem inl_ne_inr {a : α} {b : β} : inl a ≠ inr b.
theorem inr_ne_inl {a : α} {b : β} : inr b ≠ inl a.
/-- Define a function on `α ⊕ β` by giving separate definitions on `α` and `β`. -/
protected def elim {α β γ : Sort*} (f : α → γ) (g : β → γ) : α ⊕ β → γ := λ x, sum.rec_on x f g
@[simp] lemma elim_inl {α β γ : Sort*} (f : α → γ) (g : β → γ) (x : α) :
sum.elim f g (inl x) = f x := rfl
@[simp] lemma elim_inr {α β γ : Sort*} (f : α → γ) (g : β → γ) (x : β) :
sum.elim f g (inr x) = g x := rfl
@[simp] lemma elim_comp_inl {α β γ : Sort*} (f : α → γ) (g : β → γ) :
sum.elim f g ∘ inl = f := rfl
@[simp] lemma elim_comp_inr {α β γ : Sort*} (f : α → γ) (g : β → γ) :
sum.elim f g ∘ inr = g := rfl
@[simp] lemma elim_inl_inr {α β : Sort*} :
@sum.elim α β _ inl inr = id :=
funext $ λ x, sum.cases_on x (λ _, rfl) (λ _, rfl)
lemma comp_elim {α β γ δ : Sort*} (f : γ → δ) (g : α → γ) (h : β → γ):
f ∘ sum.elim g h = sum.elim (f ∘ g) (f ∘ h) :=
funext $ λ x, sum.cases_on x (λ _, rfl) (λ _, rfl)
@[simp] lemma elim_comp_inl_inr {α β γ : Sort*} (f : α ⊕ β → γ) :
sum.elim (f ∘ inl) (f ∘ inr) = f :=
funext $ λ x, sum.cases_on x (λ _, rfl) (λ _, rfl)
open function (update update_eq_iff update_comp_eq_of_injective update_comp_eq_of_forall_ne)
@[simp] lemma update_elim_inl {α β γ} [decidable_eq α] [decidable_eq (α ⊕ β)]
{f : α → γ} {g : β → γ} {i : α} {x : γ} :
update (sum.elim f g) (inl i) x = sum.elim (update f i x) g :=
update_eq_iff.2 ⟨by simp, by simp { contextual := tt }⟩
@[simp] lemma update_elim_inr {α β γ} [decidable_eq β] [decidable_eq (α ⊕ β)]
{f : α → γ} {g : β → γ} {i : β} {x : γ} :
update (sum.elim f g) (inr i) x = sum.elim f (update g i x) :=
update_eq_iff.2 ⟨by simp, by simp { contextual := tt }⟩
@[simp] lemma update_inl_comp_inl {α β γ} [decidable_eq α] [decidable_eq (α ⊕ β)]
{f : α ⊕ β → γ} {i : α} {x : γ} :
update f (inl i) x ∘ inl = update (f ∘ inl) i x :=
update_comp_eq_of_injective _ injective_inl _ _
@[simp] lemma update_inl_apply_inl {α β γ} [decidable_eq α] [decidable_eq (α ⊕ β)]
{f : α ⊕ β → γ} {i j : α} {x : γ} :
update f (inl i) x (inl j) = update (f ∘ inl) i x j :=
by rw ← update_inl_comp_inl
@[simp] lemma update_inl_comp_inr {α β γ} [decidable_eq (α ⊕ β)]
{f : α ⊕ β → γ} {i : α} {x : γ} :
update f (inl i) x ∘ inr = f ∘ inr :=
update_comp_eq_of_forall_ne _ _ $ λ _, inr_ne_inl
@[simp] lemma update_inl_apply_inr {α β γ} [decidable_eq (α ⊕ β)]
{f : α ⊕ β → γ} {i : α} {j : β} {x : γ} :
update f (inl i) x (inr j) = f (inr j) :=
function.update_noteq inr_ne_inl _ _
@[simp] lemma update_inr_comp_inl {α β γ} [decidable_eq (α ⊕ β)]
{f : α ⊕ β → γ} {i : β} {x : γ} :
update f (inr i) x ∘ inl = f ∘ inl :=
update_comp_eq_of_forall_ne _ _ $ λ _, inl_ne_inr
@[simp] lemma update_inr_apply_inl {α β γ} [decidable_eq (α ⊕ β)]
{f : α ⊕ β → γ} {i : α} {j : β} {x : γ} :
update f (inr j) x (inl i) = f (inl i) :=
function.update_noteq inl_ne_inr _ _
@[simp] lemma update_inr_comp_inr {α β γ} [decidable_eq β] [decidable_eq (α ⊕ β)]
{f : α ⊕ β → γ} {i : β} {x : γ} :
update f (inr i) x ∘ inr = update (f ∘ inr) i x :=
update_comp_eq_of_injective _ injective_inr _ _
@[simp] lemma update_inr_apply_inr {α β γ} [decidable_eq β] [decidable_eq (α ⊕ β)]
{f : α ⊕ β → γ} {i j : β} {x : γ} :
update f (inr i) x (inr j) = update (f ∘ inr) i x j :=
by rw ← update_inr_comp_inr
section
variables (ra : α → α → Prop) (rb : β → β → Prop)
/-- Lexicographic order for sum. Sort all the `inl a` before the `inr b`,
otherwise use the respective order on `α` or `β`. -/
inductive lex : α ⊕ β → α ⊕ β → Prop
| inl {a₁ a₂} (h : ra a₁ a₂) : lex (inl a₁) (inl a₂)
| inr {b₁ b₂} (h : rb b₁ b₂) : lex (inr b₁) (inr b₂)
| sep (a b) : lex (inl a) (inr b)
variables {ra rb}
@[simp] theorem lex_inl_inl {a₁ a₂} : lex ra rb (inl a₁) (inl a₂) ↔ ra a₁ a₂ :=
⟨λ h, by cases h; assumption, lex.inl⟩
@[simp] theorem lex_inr_inr {b₁ b₂} : lex ra rb (inr b₁) (inr b₂) ↔ rb b₁ b₂ :=
⟨λ h, by cases h; assumption, lex.inr⟩
@[simp] theorem lex_inr_inl {b a} : ¬ lex ra rb (inr b) (inl a) :=
λ h, by cases h
attribute [simp] lex.sep
theorem lex_acc_inl {a} (aca : acc ra a) : acc (lex ra rb) (inl a) :=
begin
induction aca with a H IH,
constructor, intros y h,
cases h with a' _ h',
exact IH _ h'
end
theorem lex_acc_inr (aca : ∀ a, acc (lex ra rb) (inl a)) {b} (acb : acc rb b) : acc (lex ra rb) (inr b) :=
begin
induction acb with b H IH,
constructor, intros y h,
cases h with _ _ _ b' _ h' a,
{ exact IH _ h' },
{ exact aca _ }
end
theorem lex_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (lex ra rb) :=
have aca : ∀ a, acc (lex ra rb) (inl a), from λ a, lex_acc_inl (ha.apply a),
⟨λ x, sum.rec_on x aca (λ b, lex_acc_inr aca (hb.apply b))⟩
end
/-- Swap the factors of a sum type -/
@[simp] def swap : α ⊕ β → β ⊕ α
| (inl a) := inr a
| (inr b) := inl b
@[simp] lemma swap_swap (x : α ⊕ β) : swap (swap x) = x :=
by cases x; refl
@[simp] lemma swap_swap_eq : swap ∘ swap = @id (α ⊕ β) :=
funext $ swap_swap
@[simp] lemma swap_left_inverse : function.left_inverse (@swap α β) swap :=
swap_swap
@[simp] lemma swap_right_inverse : function.right_inverse (@swap α β) swap :=
swap_swap
end sum
namespace function
open sum
lemma injective.sum_elim {γ} {f : α → γ} {g : β → γ}
(hf : injective f) (hg : injective g) (hfg : ∀ a b, f a ≠ g b) :
injective (sum.elim f g)
| (inl x) (inl y) h := congr_arg inl $ hf h
| (inl x) (inr y) h := (hfg x y h).elim
| (inr x) (inl y) h := (hfg y x h.symm).elim
| (inr x) (inr y) h := congr_arg inr $ hg h
lemma injective.sum_map {f : α → β} {g : α' → β'} (hf : injective f) (hg : injective g) :
injective (sum.map f g)
| (inl x) (inl y) h := congr_arg inl $ hf $ inl.inj h
| (inr x) (inr y) h := congr_arg inr $ hg $ inr.inj h
lemma surjective.sum_map {f : α → β} {g : α' → β'} (hf : surjective f) (hg : surjective g) :
surjective (sum.map f g)
| (inl y) := let ⟨x, hx⟩ := hf y in ⟨inl x, congr_arg inl hx⟩
| (inr y) := let ⟨x, hx⟩ := hg y in ⟨inr x, congr_arg inr hx⟩
end function
|
7e2ab04715235cc1a2c17ba084dfa83d21384571 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/ring_theory/witt_vector/mul_coeff.lean | 5a2bed5e2b75c4ce47ca59603e31b1cb4840be98 | [
"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 | 12,748 | lean | /-
Copyright (c) 2022 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis, Heather Macbeth
-/
import ring_theory.witt_vector.truncated
import data.mv_polynomial.supported
/-!
# Leading terms of Witt vector multiplication
The goal of this file is to study the leading terms of the formula for the `n+1`st coefficient
of a product of Witt vectors `x` and `y` over a ring of characteristic `p`.
We aim to isolate the `n+1`st coefficients of `x` and `y`, and express the rest of the product
in terms of a function of the lower coefficients.
For most of this file we work with terms of type `mv_polynomial (fin 2 × ℕ) ℤ`.
We will eventually evaluate them in `k`, but first we must take care of a calculation
that needs to happen in characteristic 0.
## Main declarations
* `witt_vector.nth_mul_coeff`: expresses the coefficient of a product of Witt vectors
in terms of the previous coefficients of the multiplicands.
-/
noncomputable theory
namespace witt_vector
variables (p : ℕ) [hp : fact p.prime]
variables {k : Type*} [comm_ring k]
local notation `𝕎` := witt_vector p
open finset mv_polynomial
open_locale big_operators
/--
```
(∑ i in range n, (y.coeff i)^(p^(n-i)) * p^i.val) *
(∑ i in range n, (y.coeff i)^(p^(n-i)) * p^i.val)
```
-/
def witt_poly_prod (n : ℕ) : mv_polynomial (fin 2 × ℕ) ℤ :=
rename (prod.mk (0 : fin 2)) (witt_polynomial p ℤ n) *
rename (prod.mk (1 : fin 2)) (witt_polynomial p ℤ n)
include hp
lemma witt_poly_prod_vars (n : ℕ) : (witt_poly_prod p n).vars ⊆ univ ×ˢ range (n + 1) :=
begin
rw [witt_poly_prod],
apply subset.trans (vars_mul _ _),
apply union_subset;
{ apply subset.trans (vars_rename _ _),
simp [witt_polynomial_vars,image_subset_iff] }
end
/-- The "remainder term" of `witt_vector.witt_poly_prod`. See `mul_poly_of_interest_aux2`. -/
def witt_poly_prod_remainder (n : ℕ) : mv_polynomial (fin 2 × ℕ) ℤ :=
∑ i in range n, p^i * (witt_mul p i)^(p^(n-i))
lemma witt_poly_prod_remainder_vars (n : ℕ) :
(witt_poly_prod_remainder p n).vars ⊆ univ ×ˢ range n :=
begin
rw [witt_poly_prod_remainder],
apply subset.trans (vars_sum_subset _ _),
rw bUnion_subset,
intros x hx,
apply subset.trans (vars_mul _ _),
apply union_subset,
{ apply subset.trans (vars_pow _ _),
have : (p : mv_polynomial (fin 2 × ℕ) ℤ) = (C (p : ℤ)),
{ simp only [int.cast_coe_nat, ring_hom.eq_int_cast] },
rw [this, vars_C],
apply empty_subset },
{ apply subset.trans (vars_pow _ _),
apply subset.trans (witt_mul_vars _ _),
apply product_subset_product (subset.refl _),
simp only [mem_range, range_subset] at hx ⊢,
exact hx }
end
omit hp
/--
`remainder p n` represents the remainder term from `mul_poly_of_interest_aux3`.
`witt_poly_prod p (n+1)` will have variables up to `n+1`,
but `remainder` will only have variables up to `n`.
-/
def remainder (n : ℕ) : mv_polynomial (fin 2 × ℕ) ℤ :=
(∑ (x : ℕ) in
range (n + 1),
(rename (prod.mk 0)) ((monomial (finsupp.single x (p ^ (n + 1 - x)))) (↑p ^ x))) *
∑ (x : ℕ) in
range (n + 1),
(rename (prod.mk 1)) ((monomial (finsupp.single x (p ^ (n + 1 - x)))) (↑p ^ x))
include hp
lemma remainder_vars (n : ℕ) : (remainder p n).vars ⊆ univ ×ˢ range (n + 1) :=
begin
rw [remainder],
apply subset.trans (vars_mul _ _),
apply union_subset;
{ apply subset.trans (vars_sum_subset _ _),
rw bUnion_subset,
intros x hx,
rw [rename_monomial, vars_monomial, finsupp.map_domain_single],
{ apply subset.trans (finsupp.support_single_subset),
simp [hx], },
{ apply pow_ne_zero,
exact_mod_cast hp.out.ne_zero } }
end
/-- This is the polynomial whose degree we want to get a handle on. -/
def poly_of_interest (n : ℕ) : mv_polynomial (fin 2 × ℕ) ℤ :=
witt_mul p (n + 1) + p^(n+1) * X (0, n+1) * X (1, n+1) -
(X (0, n+1)) * rename (prod.mk (1 : fin 2)) (witt_polynomial p ℤ (n + 1)) -
(X (1, n+1)) * rename (prod.mk (0 : fin 2)) (witt_polynomial p ℤ (n + 1))
lemma mul_poly_of_interest_aux1 (n : ℕ) :
(∑ i in range (n+1), p^i * (witt_mul p i)^(p^(n-i)) : mv_polynomial (fin 2 × ℕ) ℤ) =
witt_poly_prod p n :=
begin
simp only [witt_poly_prod],
convert witt_structure_int_prop p (X (0 : fin 2) * X 1) n using 1,
{ simp only [witt_polynomial, witt_mul],
rw alg_hom.map_sum,
congr' 1 with i,
congr' 1,
have hsupp : (finsupp.single i (p ^ (n - i))).support = {i},
{ rw finsupp.support_eq_singleton,
simp only [and_true, finsupp.single_eq_same, eq_self_iff_true, ne.def],
exact pow_ne_zero _ hp.out.ne_zero, },
simp only [bind₁_monomial, hsupp, int.cast_coe_nat, prod_singleton, ring_hom.eq_int_cast,
finsupp.single_eq_same, C_pow, mul_eq_mul_left_iff, true_or, eq_self_iff_true], },
{ simp only [map_mul, bind₁_X_right] }
end
lemma mul_poly_of_interest_aux2 (n : ℕ) :
(p ^ n * witt_mul p n : mv_polynomial (fin 2 × ℕ) ℤ) + witt_poly_prod_remainder p n =
witt_poly_prod p n :=
begin
convert mul_poly_of_interest_aux1 p n,
rw [sum_range_succ, add_comm, nat.sub_self, pow_zero, pow_one],
refl
end
omit hp
lemma mul_poly_of_interest_aux3 (n : ℕ) :
witt_poly_prod p (n+1) =
- (p^(n+1) * X (0, n+1)) * (p^(n+1) * X (1, n+1)) +
(p^(n+1) * X (0, n+1)) * rename (prod.mk (1 : fin 2)) (witt_polynomial p ℤ (n + 1)) +
(p^(n+1) * X (1, n+1)) * rename (prod.mk (0 : fin 2)) (witt_polynomial p ℤ (n + 1)) +
remainder p n :=
begin
-- a useful auxiliary fact
have mvpz : (p ^ (n + 1) : mv_polynomial (fin 2 × ℕ) ℤ) = mv_polynomial.C (↑p ^ (n + 1)),
{ simp only [int.cast_coe_nat, ring_hom.eq_int_cast, C_pow, eq_self_iff_true] },
-- unfold definitions and peel off the last entries of the sums.
rw [witt_poly_prod, witt_polynomial, alg_hom.map_sum, alg_hom.map_sum,
sum_range_succ],
-- these are sums up to `n+2`, so be careful to only unfold to `n+1`.
conv_lhs {congr, skip, rw [sum_range_succ] },
simp only [add_mul, mul_add, tsub_self, pow_zero, alg_hom.map_sum],
-- rearrange so that the first summand on rhs and lhs is `remainder`, and peel off
conv_rhs { rw add_comm },
simp only [add_assoc],
apply congr_arg (has_add.add _),
conv_rhs { rw sum_range_succ },
-- the rest is equal with proper unfolding and `ring`
simp only [rename_monomial, monomial_eq_C_mul_X, map_mul, rename_C, pow_one, rename_X, mvpz],
simp only [int.cast_coe_nat, map_pow, ring_hom.eq_int_cast, rename_X, pow_one, tsub_self,
pow_zero],
ring,
end
include hp
lemma mul_poly_of_interest_aux4 (n : ℕ) :
(p ^ (n + 1) * witt_mul p (n + 1) : mv_polynomial (fin 2 × ℕ) ℤ) =
- (p^(n+1) * X (0, n+1)) * (p^(n+1) * X (1, n+1)) +
(p^(n+1) * X (0, n+1)) * rename (prod.mk (1 : fin 2)) (witt_polynomial p ℤ (n + 1)) +
(p^(n+1) * X (1, n+1)) * rename (prod.mk (0 : fin 2)) (witt_polynomial p ℤ (n + 1)) +
(remainder p n - witt_poly_prod_remainder p (n + 1)) :=
begin
rw [← add_sub_assoc, eq_sub_iff_add_eq, mul_poly_of_interest_aux2],
exact mul_poly_of_interest_aux3 _ _
end
lemma mul_poly_of_interest_aux5 (n : ℕ) :
(p ^ (n + 1) : mv_polynomial (fin 2 × ℕ) ℤ) *
poly_of_interest p n =
(remainder p n - witt_poly_prod_remainder p (n + 1)) :=
begin
simp only [poly_of_interest, mul_sub, mul_add, sub_eq_iff_eq_add'],
rw mul_poly_of_interest_aux4 p n,
ring,
end
lemma mul_poly_of_interest_vars (n : ℕ) :
((p ^ (n + 1) : mv_polynomial (fin 2 × ℕ) ℤ) * poly_of_interest p n).vars ⊆
univ ×ˢ range (n + 1) :=
begin
rw mul_poly_of_interest_aux5,
apply subset.trans (vars_sub_subset _ _),
apply union_subset,
{ apply remainder_vars },
{ apply witt_poly_prod_remainder_vars }
end
lemma poly_of_interest_vars_eq (n : ℕ) :
(poly_of_interest p n).vars =
((p ^ (n + 1) : mv_polynomial (fin 2 × ℕ) ℤ) * (witt_mul p (n + 1) +
p^(n+1) * X (0, n+1) * X (1, n+1) -
(X (0, n+1)) * rename (prod.mk (1 : fin 2)) (witt_polynomial p ℤ (n + 1)) -
(X (1, n+1)) * rename (prod.mk (0 : fin 2)) (witt_polynomial p ℤ (n + 1)))).vars :=
begin
have : (p ^ (n + 1) : mv_polynomial (fin 2 × ℕ) ℤ) = C (p ^ (n + 1) : ℤ),
{ simp only [int.cast_coe_nat, ring_hom.eq_int_cast, C_pow, eq_self_iff_true] },
rw [poly_of_interest, this, vars_C_mul],
apply pow_ne_zero,
exact_mod_cast hp.out.ne_zero
end
lemma poly_of_interest_vars (n : ℕ) : (poly_of_interest p n).vars ⊆ univ ×ˢ (range (n+1)) :=
by rw poly_of_interest_vars_eq; apply mul_poly_of_interest_vars
lemma peval_poly_of_interest (n : ℕ) (x y : 𝕎 k) :
peval (poly_of_interest p n) ![λ i, x.coeff i, λ i, y.coeff i] =
(x * y).coeff (n + 1) + p^(n+1) * x.coeff (n+1) * y.coeff (n+1)
- y.coeff (n+1) * ∑ i in range (n+1+1), p^i * x.coeff i ^ (p^(n+1-i))
- x.coeff (n+1) * ∑ i in range (n+1+1), p^i * y.coeff i ^ (p^(n+1-i)) :=
begin
simp only [poly_of_interest, peval, map_nat_cast, matrix.head_cons, map_pow,
function.uncurry_apply_pair, aeval_X,
matrix.cons_val_one, map_mul, matrix.cons_val_zero, map_sub],
rw [sub_sub, add_comm (_ * _), ← sub_sub],
have mvpz : (p : mv_polynomial ℕ ℤ) = mv_polynomial.C ↑p,
{ rw [ring_hom.eq_int_cast, int.cast_coe_nat] },
have : ∀ (f : ℤ →+* k) (g : ℕ → k), eval₂ f g p = f p,
{ intros, rw [mvpz, mv_polynomial.eval₂_C] },
simp [witt_polynomial_eq_sum_C_mul_X_pow, aeval, eval₂_rename, this, mul_coeff, peval,
map_nat_cast, map_add, map_pow, map_mul]
end
variable [char_p k p]
/-- The characteristic `p` version of `peval_poly_of_interest` -/
lemma peval_poly_of_interest' (n : ℕ) (x y : 𝕎 k) :
peval (poly_of_interest p n) ![λ i, x.coeff i, λ i, y.coeff i] =
(x * y).coeff (n + 1) - y.coeff (n+1) * x.coeff 0 ^ (p^(n+1))
- x.coeff (n+1) * y.coeff 0 ^ (p^(n+1)) :=
begin
rw peval_poly_of_interest,
have : (p : k) = 0 := char_p.cast_eq_zero (k) p,
simp only [this, add_zero, zero_mul, nat.succ_ne_zero, ne.def, not_false_iff, zero_pow'],
have sum_zero_pow_mul_pow_p : ∀ y : 𝕎 k,
∑ (x : ℕ) in range (n + 1 + 1), 0 ^ x * y.coeff x ^ p ^ (n + 1 - x) = y.coeff 0 ^ p ^ (n + 1),
{ intro y,
rw finset.sum_eq_single_of_mem 0,
{ simp },
{ simp },
{ intros j _ hj,
simp [zero_pow (zero_lt_iff.mpr hj)] } },
congr; apply sum_zero_pow_mul_pow_p,
end
variable (k)
lemma nth_mul_coeff' (n : ℕ) :
∃ f : truncated_witt_vector p (n+1) k → truncated_witt_vector p (n+1) k → k, ∀ (x y : 𝕎 k),
f (truncate_fun (n+1) x) (truncate_fun (n+1) y)
= (x * y).coeff (n+1) - y.coeff (n+1) * x.coeff 0 ^ (p^(n+1))
- x.coeff (n+1) * y.coeff 0 ^ (p^(n+1)) :=
begin
simp only [←peval_poly_of_interest'],
obtain ⟨f₀, hf₀⟩ := exists_restrict_to_vars k (poly_of_interest_vars p n),
let f : truncated_witt_vector p (n+1) k → truncated_witt_vector p (n+1) k → k,
{ intros x y,
apply f₀,
rintros ⟨a, ha⟩,
apply function.uncurry (![x, y]),
simp only [true_and, multiset.mem_cons, range_coe, product_val, multiset.mem_range,
multiset.mem_product, multiset.range_succ, mem_univ_val] at ha,
refine ⟨a.fst, ⟨a.snd, _⟩⟩,
cases ha with ha ha; linarith only [ha] },
use f,
intros x y,
dsimp [peval],
rw ← hf₀,
simp only [f, function.uncurry_apply_pair],
congr,
ext a,
cases a with a ha,
cases a with i m,
simp only [true_and, multiset.mem_cons, range_coe, product_val, multiset.mem_range,
multiset.mem_product, multiset.range_succ, mem_univ_val] at ha,
have ha' : m < n + 1 := by cases ha with ha ha; linarith only [ha],
fin_cases i; -- surely this case split is not necessary
{ simpa only using x.coeff_truncate_fun ⟨m, ha'⟩ }
end
lemma nth_mul_coeff (n : ℕ) :
∃ f : truncated_witt_vector p (n+1) k → truncated_witt_vector p (n+1) k → k, ∀ (x y : 𝕎 k),
(x * y).coeff (n+1) =
x.coeff (n+1) * y.coeff 0 ^ (p^(n+1)) + y.coeff (n+1) * x.coeff 0 ^ (p^(n+1)) +
f (truncate_fun (n+1) x) (truncate_fun (n+1) y) :=
begin
obtain ⟨f, hf⟩ := nth_mul_coeff' p k n,
use f,
intros x y,
rw hf x y,
ring,
end
variable {k}
/--
Produces the "remainder function" of the `n+1`st coefficient, which does not depend on the `n+1`st
coefficients of the inputs. -/
def nth_remainder (n : ℕ) : (fin (n+1) → k) → (fin (n+1) → k) → k :=
classical.some (nth_mul_coeff p k n)
lemma nth_remainder_spec (n : ℕ) (x y : 𝕎 k) :
(x * y).coeff (n+1) =
x.coeff (n+1) * y.coeff 0 ^ (p^(n+1)) + y.coeff (n+1) * x.coeff 0 ^ (p^(n+1)) +
nth_remainder p n (truncate_fun (n+1) x) (truncate_fun (n+1) y) :=
classical.some_spec (nth_mul_coeff p k n) _ _
end witt_vector
|
bddff2eebd80ea8e954065a4a792b251db6bd3b7 | ec62863c729b7eedee77b86d974f2c529fa79d25 | /10/a.lean | a9f2747a27a610250784bbb1753f5fdcb23b1a51 | [] | 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 | 403 | lean | def main : IO Unit := do
let input ← IO.FS.lines "a.in"
let mut vals := (input.map String.toInt!).qsort (· < ·)
vals := vals.push (vals.get! (vals.size - 1) + 3)
let mut diffs : Array Int := Array.mkArray 4 0
let mut prev := 0
for v in vals do
let d : Nat := (v - prev).toNat
diffs := diffs.set! d (diffs.get! d + 1)
prev := v
IO.print s!"{diffs.get! 1 * diffs.get! 3}\n"
|
a93ec3e00ded9d2457d5881fd71482bcbf2773a1 | 32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7 | /src/Std/Data/PersistentHashMap.lean | bb880e040f1d976e9f7b1276baea62916540e432 | [
"Apache-2.0"
] | permissive | walterhu1015/lean4 | b2c71b688975177402758924eaa513475ed6ce72 | 2214d81e84646a905d0b20b032c89caf89c737ad | refs/heads/master | 1,671,342,096,906 | 1,599,695,985,000 | 1,599,695,985,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,400 | 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
-/
namespace Std
universes u v w w'
namespace PersistentHashMap
inductive Entry (α : Type u) (β : Type v) (σ : Type w)
| entry (key : α) (val : β) : Entry
| ref (node : σ) : Entry
| null : Entry
instance Entry.inhabited {α β σ} : Inhabited (Entry α β σ) := ⟨Entry.null⟩
inductive Node (α : Type u) (β : Type v) : Type (max u v)
| entries (es : Array (Entry α β Node)) : Node
| collision (ks : Array α) (vs : Array β) (h : ks.size = vs.size) : Node
instance Node.inhabited {α β} : Inhabited (Node α β) := ⟨Node.entries #[]⟩
abbrev shift : USize := 5
abbrev branching : USize := USize.ofNat (2 ^ shift.toNat)
abbrev maxDepth : USize := 7
abbrev maxCollisions : Nat := 4
def mkEmptyEntriesArray {α β} : Array (Entry α β (Node α β)) :=
(Array.mkArray PersistentHashMap.branching.toNat PersistentHashMap.Entry.null)
end PersistentHashMap
structure PersistentHashMap (α : Type u) (β : Type v) [HasBeq α] [Hashable α] :=
(root : PersistentHashMap.Node α β := PersistentHashMap.Node.entries PersistentHashMap.mkEmptyEntriesArray)
(size : Nat := 0)
abbrev PHashMap (α : Type u) (β : Type v) [HasBeq α] [Hashable α] := PersistentHashMap α β
namespace PersistentHashMap
variables {α : Type u} {β : Type v}
def empty [HasBeq α] [Hashable α] : PersistentHashMap α β := {}
instance [HasBeq α] [Hashable α] : HasEmptyc (PersistentHashMap α β) := ⟨empty⟩
def isEmpty [HasBeq α] [Hashable α] (m : PersistentHashMap α β) : Bool :=
m.size == 0
instance [HasBeq α] [Hashable α] : Inhabited (PersistentHashMap α β) := ⟨{}⟩
def mkEmptyEntries {α β} : Node α β :=
Node.entries mkEmptyEntriesArray
abbrev mul2Shift (i : USize) (shift : USize) : USize := i.shiftLeft shift
abbrev div2Shift (i : USize) (shift : USize) : USize := i.shiftRight shift
abbrev mod2Shift (i : USize) (shift : USize) : USize := USize.land i ((USize.shiftLeft 1 shift) - 1)
inductive IsCollisionNode : Node α β → Prop
| mk (keys : Array α) (vals : Array β) (h : keys.size = vals.size) : IsCollisionNode (Node.collision keys vals h)
abbrev CollisionNode (α β) := { n : Node α β // IsCollisionNode n }
inductive IsEntriesNode : Node α β → Prop
| mk (entries : Array (Entry α β (Node α β))) : IsEntriesNode (Node.entries entries)
abbrev EntriesNode (α β) := { n : Node α β // IsEntriesNode n }
private theorem setSizeEq {ks : Array α} {vs : Array β} (h : ks.size = vs.size) (i : Fin ks.size) (j : Fin vs.size) (k : α) (v : β)
: (ks.set i k).size = (vs.set j v).size :=
have h₁ : (ks.set i k).size = ks.size from Array.szFSetEq _ _ _;
have h₂ : (vs.set j v).size = vs.size from Array.szFSetEq _ _ _;
(h₁.trans h).trans h₂.symm
private theorem pushSizeEq {ks : Array α} {vs : Array β} (h : ks.size = vs.size) (k : α) (v : β) : (ks.push k).size = (vs.push v).size :=
have h₁ : (ks.push k).size = ks.size + 1 from Array.szPushEq _ _;
have h₂ : (vs.push v).size = vs.size + 1 from Array.szPushEq _ _;
have h₃ : ks.size + 1 = vs.size + 1 from h ▸ rfl;
(h₁.trans h₃).trans h₂.symm
partial def insertAtCollisionNodeAux [HasBeq α] : CollisionNode α β → Nat → α → β → CollisionNode α β
| n@⟨Node.collision keys vals heq, _⟩, i, k, v =>
if h : i < keys.size then
let idx : Fin keys.size := ⟨i, h⟩;
let k' := keys.get idx;
if k == k' then
let j : Fin vals.size := ⟨i, heq ▸ h⟩;
⟨Node.collision (keys.set idx k) (vals.set j v) (setSizeEq heq idx j k v), IsCollisionNode.mk _ _ _⟩
else insertAtCollisionNodeAux n (i+1) k v
else
⟨Node.collision (keys.push k) (vals.push v) (pushSizeEq heq k v), IsCollisionNode.mk _ _ _⟩
| ⟨Node.entries _, h⟩, _, _, _ => False.elim (nomatch h)
def insertAtCollisionNode [HasBeq α] : CollisionNode α β → α → β → CollisionNode α β :=
fun n k v => insertAtCollisionNodeAux n 0 k v
def getCollisionNodeSize : CollisionNode α β → Nat
| ⟨Node.collision keys _ _, _⟩ => keys.size
| ⟨Node.entries _, h⟩ => False.elim (nomatch h)
def mkCollisionNode (k₁ : α) (v₁ : β) (k₂ : α) (v₂ : β) : Node α β :=
let ks : Array α := Array.mkEmpty maxCollisions;
let ks := (ks.push k₁).push k₂;
let vs : Array β := Array.mkEmpty maxCollisions;
let vs := (vs.push v₁).push v₂;
Node.collision ks vs rfl
partial def insertAux [HasBeq α] [Hashable α] : Node α β → USize → USize → α → β → Node α β
| Node.collision keys vals heq, _, depth, k, v =>
let newNode := insertAtCollisionNode ⟨Node.collision keys vals heq, IsCollisionNode.mk _ _ _⟩ k v;
if depth >= maxDepth || getCollisionNodeSize newNode < maxCollisions then newNode.val
else match newNode with
| ⟨Node.entries _, h⟩ => False.elim (nomatch h)
| ⟨Node.collision keys vals heq, _⟩ =>
let entries : Node α β := mkEmptyEntries;
keys.iterate entries $ fun i k entries =>
let v := vals.get ⟨i.val, heq ▸ i.isLt⟩;
let h := hash k;
-- dbgTrace ("toCollision " ++ toString i ++ ", h: " ++ toString h ++ ", depth: " ++ toString depth ++ ", h': " ++
-- toString (div2Shift h (shift * (depth - 1)))) $ fun _ =>
let h := div2Shift h (shift * (depth - 1));
insertAux entries h depth k v
| Node.entries entries, h, depth, k, v =>
let j := (mod2Shift h shift).toNat;
Node.entries $ entries.modify j $ fun entry =>
match entry with
| Entry.null => Entry.entry k v
| Entry.ref node => Entry.ref $ insertAux node (div2Shift h shift) (depth+1) k v
| Entry.entry k' v' =>
if k == k' then Entry.entry k v
else Entry.ref $ mkCollisionNode k' v' k v
def insert [HasBeq α] [Hashable α] : PersistentHashMap α β → α → β → PersistentHashMap α β
| { root := n, size := sz }, k, v => { root := insertAux n (hash k) 1 k v, size := sz + 1 }
partial def findAtAux [HasBeq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) : Nat → α → Option β
| i, k =>
if h : i < keys.size then
let k' := keys.get ⟨i, h⟩;
if k == k' then some (vals.get ⟨i, heq ▸ h⟩)
else findAtAux (i+1) k
else none
partial def findAux [HasBeq α] : Node α β → USize → α → Option β
| Node.entries entries, h, k =>
let j := (mod2Shift h shift).toNat;
match entries.get! j with
| Entry.null => none
| Entry.ref node => findAux node (div2Shift h shift) k
| Entry.entry k' v => if k == k' then some v else none
| Node.collision keys vals heq, _, k => findAtAux keys vals heq 0 k
def find? [HasBeq α] [Hashable α] : PersistentHashMap α β → α → Option β
| { root := n, .. }, k => findAux n (hash k) k
@[inline] def getOp [HasBeq α] [Hashable α] (self : PersistentHashMap α β) (idx : α) : Option β :=
self.find? idx
@[inline] def findD [HasBeq α] [Hashable α] (m : PersistentHashMap α β) (a : α) (b₀ : β) : β :=
(m.find? a).getD b₀
@[inline] def find! [HasBeq α] [Hashable α] [Inhabited β] (m : PersistentHashMap α β) (a : α) : β :=
match m.find? a with
| some b => b
| none => panic! "key is not in the map"
partial def findEntryAtAux [HasBeq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) : Nat → α → Option (α × β)
| i, k =>
if h : i < keys.size then
let k' := keys.get ⟨i, h⟩;
if k == k' then some (k', vals.get ⟨i, heq ▸ h⟩)
else findEntryAtAux (i+1) k
else none
partial def findEntryAux [HasBeq α] : Node α β → USize → α → Option (α × β)
| Node.entries entries, h, k =>
let j := (mod2Shift h shift).toNat;
match entries.get! j with
| Entry.null => none
| Entry.ref node => findEntryAux node (div2Shift h shift) k
| Entry.entry k' v => if k == k' then some (k', v) else none
| Node.collision keys vals heq, _, k => findEntryAtAux keys vals heq 0 k
def findEntry? [HasBeq α] [Hashable α] : PersistentHashMap α β → α → Option (α × β)
| { root := n, .. }, k => findEntryAux n (hash k) k
partial def containsAtAux [HasBeq α] (keys : Array α) (vals : Array β) (heq : keys.size = vals.size) : Nat → α → Bool
| i, k =>
if h : i < keys.size then
let k' := keys.get ⟨i, h⟩;
if k == k' then true
else containsAtAux (i+1) k
else false
partial def containsAux [HasBeq α] : Node α β → USize → α → Bool
| Node.entries entries, h, k =>
let j := (mod2Shift h shift).toNat;
match entries.get! j with
| Entry.null => false
| Entry.ref node => containsAux node (div2Shift h shift) k
| Entry.entry k' v => k == k'
| Node.collision keys vals heq, _, k => containsAtAux keys vals heq 0 k
def contains [HasBeq α] [Hashable α] : PersistentHashMap α β → α → Bool
| { root := n, .. }, k => containsAux n (hash k) k
partial def isUnaryEntries (a : Array (Entry α β (Node α β))) : Nat → Option (α × β) → Option (α × β)
| i, acc =>
if h : i < a.size then
match a.get ⟨i, h⟩ with
| Entry.null => isUnaryEntries (i+1) acc
| Entry.ref _ => none
| Entry.entry k v =>
match acc with
| none => isUnaryEntries (i+1) (some (k, v))
| some _ => none
else acc
def isUnaryNode : Node α β → Option (α × β)
| Node.entries entries => isUnaryEntries entries 0 none
| Node.collision keys vals heq =>
if h : 1 = keys.size then
have 0 < keys.size from h ▸ (Nat.zeroLtSucc _);
some (keys.get ⟨0, this⟩, vals.get ⟨0, heq ▸ this⟩)
else
none
partial def eraseAux [HasBeq α] : Node α β → USize → α → Node α β × Bool
| n@(Node.collision keys vals heq), _, k =>
match keys.indexOf k with
| some idx =>
let ⟨keys', keq⟩ := keys.eraseIdx' idx;
let ⟨vals', veq⟩ := vals.eraseIdx' (Eq.rec idx heq);
have keys.size - 1 = vals.size - 1 from heq ▸ rfl;
(Node.collision keys' vals' (keq.trans (this.trans veq.symm)), true)
| none => (n, false)
| n@(Node.entries entries), h, k =>
let j := (mod2Shift h shift).toNat;
let entry := entries.get! j;
match entry with
| Entry.null => (n, false)
| Entry.entry k' v =>
if k == k' then (Node.entries (entries.set! j Entry.null), true) else (n, false)
| Entry.ref node =>
let entries := entries.set! j Entry.null;
let (newNode, deleted) := eraseAux node (div2Shift h shift) k;
if !deleted then (n, false)
else match isUnaryNode newNode with
| none => (Node.entries (entries.set! j (Entry.ref newNode)), true)
| some (k, v) => (Node.entries (entries.set! j (Entry.entry k v)), true)
def erase [HasBeq α] [Hashable α] : PersistentHashMap α β → α → PersistentHashMap α β
| { root := n, size := sz }, k =>
let h := hash k;
let (n, del) := eraseAux n h k;
{ root := n, size := if del then sz - 1 else sz }
section
variables {m : Type w → Type w'} [Monad m]
variables {σ : Type w}
@[specialize] partial def foldlMAux (f : σ → α → β → m σ) : Node α β → σ → m σ
| Node.collision keys vals heq, acc => keys.iterateM acc $ fun i k acc => f acc k (vals.get ⟨i.val, heq ▸ i.isLt⟩)
| Node.entries entries, acc => entries.foldlM (fun acc entry =>
match entry with
| Entry.null => pure acc
| Entry.entry k v => f acc k v
| Entry.ref node => foldlMAux node acc)
acc
@[specialize] def foldlM [HasBeq α] [Hashable α] (map : PersistentHashMap α β) (f : σ → α → β → m σ) (acc : σ) : m σ :=
foldlMAux f map.root acc
@[specialize] def forM [HasBeq α] [Hashable α] (map : PersistentHashMap α β) (f : α → β → m PUnit) : m PUnit :=
map.foldlM (fun _ => f) ⟨⟩
@[specialize] def foldl [HasBeq α] [Hashable α] (map : PersistentHashMap α β) (f : σ → α → β → σ) (acc : σ) : σ :=
Id.run $ map.foldlM f acc
end
def toList [HasBeq α] [Hashable α] (m : PersistentHashMap α β) : List (α × β) :=
m.foldl (fun ps k v => (k, v) :: ps) []
structure Stats :=
(numNodes : Nat := 0)
(numNull : Nat := 0)
(numCollisions : Nat := 0)
(maxDepth : Nat := 0)
partial def collectStats : Node α β → Stats → Nat → Stats
| Node.collision keys _ _, stats, depth =>
{ stats with
numNodes := stats.numNodes + 1,
numCollisions := stats.numCollisions + keys.size - 1,
maxDepth := Nat.max stats.maxDepth depth }
| Node.entries entries, stats, depth =>
let stats :=
{ stats with
numNodes := stats.numNodes + 1,
maxDepth := Nat.max stats.maxDepth depth };
entries.foldl (fun stats entry =>
match entry with
| Entry.null => { stats with numNull := stats.numNull + 1 }
| Entry.ref node => collectStats node stats (depth + 1)
| Entry.entry _ _ => stats)
stats
def stats [HasBeq α] [Hashable α] (m : PersistentHashMap α β) : Stats :=
collectStats m.root {} 1
def Stats.toString (s : Stats) : String :=
"{ nodes := " ++ toString s.numNodes ++ ", null := " ++ toString s.numNull ++
", collisions := " ++ toString s.numCollisions ++ ", depth := " ++ toString s.maxDepth ++ "}"
instance : HasToString Stats := ⟨Stats.toString⟩
end PersistentHashMap
end Std
|
7c512d9f111d952c8c5cfa28acee165ef8598e39 | cc104245380aa3287223e0e350a8319e583aba37 | /src/h0.lean | 35783f7c535adc54055972c43fdfe048469f2dd0 | [] | no_license | anca797/group-cohomology | 7d10749d28430d30698e7ec0cc33adbac97fe5e7 | f896dfa5057bd70c6fbb09ee6fdc26a7ffab5e5d | refs/heads/master | 1,591,037,800,699 | 1,561,424,571,000 | 1,561,424,571,000 | 190,891,710 | 3 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,108 | lean | import algebra.group -- for is_add_group_hom
import group_theory.subgroup -- for kernels
import algebra.module
class G_module (G : Type*) [group G] (M : Type*) [add_comm_group M]
extends has_scalar G M :=
(id : ∀ m : M, (1 : G) • m = m)
(mul : ∀ g h : G, ∀ m : M, g • (h • m) = (g * h) • m)
(linear : ∀ g : G, ∀ m n : M, g • (m + n) = g • m + g • n)
attribute [simp] G_module.linear
definition H0 (G : Type*) [group G] (M : Type*) [add_comm_group M]
[G_module G M]
:= {m : M // ∀ g : G, g • m = m}
variables (G : Type*) [group G] (M : Type*) [add_comm_group M]
[G_module G M]
variables {G} {M}
lemma g_zero (g : G) : g • (0 : M) = 0 :=
begin
have h : 0 + g • (0 : M) = g • 0 + g • 0,
calc
0 + g • (0 : M) = g • 0 : zero_add _
... = g • (0 + 0) : by rw [add_zero (0 : M)]
... = g • 0 + g • 0 : G_module.linear g 0 0,
symmetry,
exact add_right_cancel h
end
lemma g_neg (g : G) (m : M) : g • (-m) = -(g• m) :=
begin
apply eq_neg_of_add_eq_zero,
rw ←G_module.linear,
rw neg_add_self,
exact g_zero g
end
lemma G_module.map_sub (g : G) (m n : M) : g • (m - n) = g • m - g • n :=
begin
rw eq_sub_iff_add_eq,
rw ←G_module.linear,
congr',
rw sub_add_cancel
end
theorem H0.add_closed (m n : M)
(hm : ∀ g : G, g • m = m) (hn : ∀ g : G, g • n = n) :
∀ g : G, g • (m + n) = m + n :=
begin
intro g,
rw G_module.linear,
rw hm,
rw hn,
end
instance H0.add_comm_group : add_comm_group (H0 G M) :=
{ add := λ x y, ⟨x.1 + y.1, H0.add_closed x.1 y.1 x.2 y.2⟩,
add_assoc := λ a b c, subtype.eq (add_assoc _ _ _),
/- begin
intros a b c,
apply subtype.eq,
-- show a.val + b.val + c.val = a.val + (b.val + c.val),
exact add_assoc _ _ _
end ,-/
zero := ⟨0,g_zero⟩,
zero_add := λ a, subtype.eq (zero_add _),
add_zero := λ a, subtype.eq (add_zero _),
neg := λ x, ⟨-x.1, λ g, by rw [g_neg g x.1, x.2]⟩,
add_left_neg := λ a, subtype.eq (add_left_neg _),
add_comm := λ a b, subtype.eq (add_comm _ _)}
variables {N : Type*} [add_comm_group N] [G_module G N]
variable (G)
class G_module_hom (f : M → N) : Prop :=
(add : ∀ a b : M, f (a + b) = f a + f b)
(G_hom : ∀ g : G, ∀ m : M, f (g • m) = g • (f m))
instance G_module_hom.is_add_group_hom (f : M → N) [G_module_hom G f] :
is_add_group_hom f :=
{map_add := G_module_hom.add G f}
lemma H0.G_module_hom (f : M → N) [G_module_hom G f] (g : G) (m : M)
(hm : ∀ g : G, g • m = m):
g • f m = f m :=
begin
rw ←G_module_hom.G_hom f,
rw hm g,
apply_instance
end
def H0_f (f : M → N) [G_module_hom G f] : H0 G M → H0 G N :=
λ x, ⟨f x.1, λ g, H0.G_module_hom G f g x.1 x.2⟩
instance (f : M → N) [G_module_hom G f] : is_add_group_hom (H0_f G f)
:= { map_add :=
begin
intro a,
intro b,
cases a,
cases b,
apply subtype.eq,
show f(a_val + b_val) = f a_val + f b_val,
apply G_module_hom.add G f
end }
instance id.G_module_hom : G_module_hom G (id : M → M) :=
{ add :=
begin
intros,
refl
end,
G_hom :=
begin
intros,
refl
end}
open set is_add_group_hom
open function
/-- A->B->C -/
def is_exact {A B C : Type*} [add_comm_group A] [add_comm_group B] [add_comm_group C]
(f : A → B) (g : B → C) [is_add_group_hom f] [is_add_group_hom g] : Prop :=
range f = ker g
/-- 0->A->B->C->0 -/
def short_exact {A B C : Type*} [add_comm_group A] [add_comm_group B] [add_comm_group C]
(f : A → B) (g : B → C) [is_add_group_hom f] [is_add_group_hom g] : Prop :=
function.injective f ∧
function.surjective g ∧
is_exact f g
lemma H0inj_of_inj {A B : Type*} [add_comm_group A] [G_module G A]
[add_comm_group B] [G_module G B] (f : A → B)
(H1 : injective f) [G_module_hom G f] : injective (H0_f G f) :=
begin
intro x,
intro y,
intro H,
unfold H0_f at H,
simp at H,
have H3 : x.val = y.val,
exact H1 H,
exact subtype.eq H3
end
/- H0(G,A) -> H0(G,B) -> H0(G,C) -/
lemma h0_exact {A B C : Type*} [add_comm_group A] [G_module G A]
[add_comm_group B] [G_module G B] [add_comm_group C] [G_module G C]
(f : A → B) (g : B → C) (H1 : injective f) [G_module_hom G f]
[G_module_hom G g] (H2 : is_exact f g) : is_exact (H0_f G f) (H0_f G g) :=
begin
change range f = ker g at H2,
apply subset.antisymm,
{ /- intro x,
cases x with x h,
intro h2,
cases h2 with a ha,
cases a with a propa,
-/
rintros ⟨x,h⟩ ⟨⟨a, _⟩, ha⟩,
rw mem_ker,
apply subtype.eq,
show g x = 0,
rw [←mem_ker g, ←H2],
use a,
injection ha,
},
{ rintros ⟨x,h⟩ hx,
rw mem_ker at hx,
unfold H0_f at hx,
injection hx with h2,
change g x = 0 at h2,
rw ← mem_ker g at h2,
rw ← H2 at h2,
cases h2 with a ha,
have h2a : ∀ g : G, g • a = a,
{ intro g,
apply H1,
rw G_module_hom.G_hom f,
rw ha,
exact h g,
apply_instance,},
use ⟨a, h2a⟩,
apply subtype.eq,
unfold H0_f,
exact ha,
}
end |
7152e79df86355664ea71fdb637092791e6118ef | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/rcases.lean | 600e64a8d4e40062f5e45415e340e3652b5e0f5f | [
"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 | 9,705 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import tactic.rcases
instance {α} : has_inter (set α) := ⟨λ s t, {a | a ∈ s ∧ a ∈ t}⟩
universe u
variables {α β γ : Type u}
example (x : α × β × γ) : true :=
begin
rcases x with ⟨a, b, c⟩,
{ guard_hyp a : α,
guard_hyp b : β,
guard_hyp c : γ,
trivial }
end
example (x : α × β × γ) : true :=
begin
rcases x with ⟨a, ⟨-, c⟩⟩,
{ guard_hyp a : α,
success_if_fail { guard_hyp x_snd_fst : β },
guard_hyp c : γ,
trivial }
end
example (x : (α × β) × γ) : true :=
begin
rcases x with ⟨⟨a:α, b⟩, c⟩,
{ guard_hyp a : α,
guard_hyp b : β,
guard_hyp c : γ,
trivial }
end
example : inhabited α × option β ⊕ γ → true :=
begin
rintro (⟨⟨a⟩, _ | b⟩ | c),
{ guard_hyp a : α, trivial },
{ guard_hyp a : α, guard_hyp b : β, trivial },
{ guard_hyp c : γ, trivial }
end
example : cond ff ℕ ℤ → cond tt ℤ ℕ → (ℕ ⊕ unit) → true :=
begin
rintro (x y : ℤ) (z | u),
{ guard_hyp x : ℤ, guard_hyp y : ℤ, guard_hyp z : ℕ, trivial },
{ guard_hyp x : ℤ, guard_hyp y : ℤ, guard_hyp u : unit, trivial }
end
example (x y : ℕ) (h : x = y) : true :=
begin
rcases x with _|⟨⟩|z,
{ guard_hyp h : nat.zero = y, trivial },
{ guard_hyp h : nat.succ nat.zero = y, trivial },
{ guard_hyp z : ℕ,
guard_hyp h : z.succ.succ = y, trivial },
end
-- from equiv.sum_empty
example (s : α ⊕ empty) : true :=
begin
rcases s with _ | ⟨⟨⟩⟩,
{ guard_hyp s : α, trivial }
end
example : true :=
begin
obtain ⟨n : ℕ, h : n = n, -⟩ : ∃ n : ℕ, n = n ∧ true,
{ existsi 0, simp },
guard_hyp n : ℕ,
guard_hyp h : n = n,
success_if_fail {assumption},
trivial
end
example : true :=
begin
obtain : ∃ n : ℕ, n = n ∧ true,
{ existsi 0, simp },
trivial
end
example : true :=
begin
obtain (h : true) | ⟨⟨⟩⟩ : true ∨ false,
{ left, trivial },
guard_hyp h : true,
trivial
end
example : true :=
begin
obtain h | ⟨⟨⟩⟩ : true ∨ false := or.inl trivial,
guard_hyp h : true,
trivial
end
example : true :=
begin
obtain ⟨h, h2⟩ := and.intro trivial trivial,
guard_hyp h : true,
guard_hyp h2 : true,
trivial
end
example : true :=
begin
success_if_fail {obtain ⟨h, h2⟩},
trivial
end
example (x y : α × β) : true :=
begin
rcases ⟨x, y⟩ with ⟨⟨a, b⟩, c, d⟩,
{ guard_hyp a : α,
guard_hyp b : β,
guard_hyp c : α,
guard_hyp d : β,
trivial }
end
example (x y : α ⊕ β) : true :=
begin
obtain ⟨a|b, c|d⟩ := ⟨x, y⟩,
{ guard_hyp a : α, guard_hyp c : α, trivial },
{ guard_hyp a : α, guard_hyp d : β, trivial },
{ guard_hyp b : β, guard_hyp c : α, trivial },
{ guard_hyp b : β, guard_hyp d : β, trivial },
end
example {i j : ℕ} : (Σ' x, i ≤ x ∧ x ≤ j) → i ≤ j :=
begin
intro h,
rcases h' : h with ⟨x,h₀,h₁⟩,
guard_hyp h' : h = ⟨x,h₀,h₁⟩,
apply le_trans h₀ h₁,
end
protected def set.foo {α β} (s : set α) (t : set β) : set (α × β) := ∅
example {α} (V : set α) (w : true → ∃ p, p ∈ (V.foo V) ∩ (V.foo V)) : true :=
begin
obtain ⟨a, h⟩ : ∃ p, p ∈ (V.foo V) ∩ (V.foo V) := w trivial,
trivial,
end
example (n : ℕ) : true :=
begin
obtain one_lt_n | n_le_one : 1 < n + 1 ∨ n + 1 ≤ 1 := nat.lt_or_ge 1 (n + 1),
trivial, trivial,
end
example (n : ℕ) : true :=
begin
obtain one_lt_n | (n_le_one : n + 1 ≤ 1) := nat.lt_or_ge 1 (n + 1),
trivial, trivial,
end
example (h : ∃ x : ℕ, x = x ∧ 1 = 1) : true :=
begin
rcases h with ⟨-, _⟩,
(do lc ← tactic.local_context, guard lc.empty),
trivial
end
example (h : ∃ x : ℕ, x = x ∧ 1 = 1) : true :=
begin
rcases h with ⟨-, _, h⟩,
(do lc ← tactic.local_context, guard (lc.length = 1)),
guard_hyp h : 1 = 1,
trivial
end
example (h : true ∨ true ∨ true) : true :=
begin
rcases h with -|-|-,
iterate 3 {
(do lc ← tactic.local_context, guard lc.empty),
trivial },
end
example : bool → false → true
| ff := by rintro ⟨⟩
| tt := by rintro ⟨⟩
example : true :=
begin
obtain h : true,
{ trivial },
exact h
end
example {a b} (h : a ∧ b) : a ∧ b :=
begin
rcases h with t,
exact t
end
structure baz {α : Type*} (f : α → α) : Prop := [inst : nonempty α] (h : f ∘ f = id)
example {α} (f : α → α) (h : baz f) : true := by { rcases h with ⟨_⟩; trivial }
example {α} (f : α → α) (h : baz f) : true := by { rcases h with @⟨_, _⟩; trivial }
inductive test : nat → Prop
| a (n) : test (2 + n)
| b {n} : n > 5 → test (n * n)
example {n} (h : test n) : n = n :=
begin
have : true,
{ rcases h with a | b,
{ guard_hyp a : nat, trivial },
{ guard_hyp b : ‹nat› > 5, trivial } },
{ rcases h with a | @⟨n, b⟩,
{ guard_hyp a : nat, trivial },
{ guard_hyp b : n > 5, trivial } },
end
open tactic
meta def test_rcases_hint (s : string) (num_goals : ℕ) (depth := 5) : tactic unit :=
do change `(true),
h ← get_local `h,
pat ← rcases_hint ```(h) depth,
p ← pp pat,
guard (p.to_string = s) <|> fail format!"got '{p.to_string}', expected: '{s}'",
gs ← get_goals,
guard (gs.length = num_goals) <|> fail format!"there are {gs.length} goals remaining",
all_goals triv $> ()
example {α} (h : ∃ x : α, x = x) := by test_rcases_hint "⟨h_w, ⟨⟩⟩" 1
example (h : true ∨ true ∨ true) := by test_rcases_hint "⟨⟨⟩⟩ | ⟨⟨⟩⟩ | ⟨⟨⟩⟩" 3
example (h : ℕ) := by test_rcases_hint "_ | _ | h" 3 2
example {p} (h : (p ∧ p) ∨ (p ∧ p)) :=
by test_rcases_hint "⟨h_left, h_right⟩ | ⟨h_left, h_right⟩" 2
example {p} (h : (p ∧ p) ∨ (p ∧ (p ∨ p))) :=
by test_rcases_hint "⟨h_left, h_right⟩ | ⟨h_left, h_right | h_right⟩" 3
example {p} (h : p ∧ (p ∨ p)) :=
by test_rcases_hint "⟨h_left, h_right | h_right⟩" 2
example (h : 0 < 2) := by test_rcases_hint "_ | ⟨_, _ | ⟨_, ⟨⟩⟩⟩" 1
example (h : 3 < 2) := by test_rcases_hint "_ | ⟨_, _ | ⟨_, ⟨⟩⟩⟩" 0
example (h : 3 < 0) := by test_rcases_hint "⟨⟩" 0
example (h : false) := by test_rcases_hint "⟨⟩" 0
example (h : true) := by test_rcases_hint "⟨⟩" 1
example {α} (h : list α) := by test_rcases_hint "_ | ⟨h_hd, _ | ⟨h_tl_hd, h_tl_tl⟩⟩" 3 2
example {α} (h : (α ⊕ α) × α) := by test_rcases_hint "⟨h_fst | h_fst, h_snd⟩" 2 2
inductive foo (α : Type) : ℕ → Type
| zero : foo 0
| one (m) : α → foo m
example {α} (h : foo α 0) : true := by test_rcases_hint "_ | ⟨_, h_ᾰ⟩" 2
example {α} (h : foo α 1) : true := by test_rcases_hint "_ | ⟨_, h_ᾰ⟩" 1
example {α n} (h : foo α n) : true := by test_rcases_hint "_ | h_ᾰ" 2 1
example {α} (V : set α) (h : ∃ p, p ∈ (V.foo V) ∩ (V.foo V)) :=
by test_rcases_hint "⟨⟨h_w_fst, h_w_snd⟩, ⟨⟩⟩" 0
section rsuffices
/-- These next few are duplicated from `rcases/obtain` tests, with the goal order swapped. -/
example : true :=
begin
rsuffices ⟨n : ℕ, h : n = n, -⟩ : ∃ n : ℕ, n = n ∧ true,
{ guard_hyp n : ℕ,
guard_hyp h : n = n,
success_if_fail {assumption},
trivial },
{ existsi 0, simp },
end
example : true :=
begin
rsuffices : ∃ n : ℕ, n = n ∧ true,
{ trivial },
{ existsi 0, simp },
end
example : true :=
begin
rsuffices (h : true) | ⟨⟨⟩⟩ : true ∨ false,
{ guard_hyp h : true,
trivial },
{ left, trivial },
end
example : true :=
begin
success_if_fail {rsuffices ⟨h, h2⟩},
trivial
end
example (x y : α × β) : true :=
begin
rsuffices ⟨⟨a, b⟩, c, d⟩ : (α × β) × (α × β),
{ guard_hyp a : α,
guard_hyp b : β,
guard_hyp c : α,
guard_hyp d : β,
trivial },
{ exact ⟨x, y⟩ }
end
-- This test demonstrates why `swap` is not used in the implementation of `rsuffices`:
-- it would make the _second_ goal the one requiring ⟨x, y⟩, not the last one.
example (x y : α ⊕ β) : true :=
begin
rsuffices ⟨a|b, c|d⟩ : (α ⊕ β) × (α ⊕ β),
{ guard_hyp a : α, guard_hyp c : α, trivial },
{ guard_hyp a : α, guard_hyp d : β, trivial },
{ guard_hyp b : β, guard_hyp c : α, trivial },
{ guard_hyp b : β, guard_hyp d : β, trivial },
exact ⟨x, y⟩,
end
example {α} (V : set α) (w : true → ∃ p, p ∈ (V.foo V) ∩ (V.foo V)) : true :=
begin
rsuffices ⟨a, h⟩ : ∃ p, p ∈ (V.foo V) ∩ (V.foo V),
{ trivial },
{ exact w trivial },
end
-- Now some tests that ensure that things stay in the correct order.
-- This test demonstrates why `focus1` is required in the definition of `rsuffices`; otherwise
-- the `∃ ...` goal would get put _after_ the `true` goal.
example : nonempty ℕ ∧ true :=
begin
split,
rsuffices ⟨n : ℕ, hn⟩ : ∃ n, _,
{ exact ⟨n⟩ },
{ exact true },
{ exact ⟨0, trivial⟩ },
{ trivial },
end
section instances
example (h : Π {α}, inhabited α) : inhabited (α ⊕ β) :=
begin
rsufficesI (ha | hb) : inhabited α ⊕ inhabited β,
{ exact ⟨sum.inl default⟩ },
{ exact ⟨sum.inr default⟩ },
{ exact sum.inl h }
end
include β
-- this test demonstrates that the `resetI` also applies onto the goal.
example (h : Π {α}, inhabited α) : inhabited α :=
begin
have : inhabited β := h,
rsufficesI t : β,
{ exact h },
{ exact default }
end
example (h : Π {α}, inhabited α) : β :=
begin
rsufficesI ht : inhabited β,
{ guard_hyp ht : inhabited β,
exact default },
{ exact h }
end
end instances
end rsuffices
|
34a20cf86efbc06246f6d15b434d1c3129526f28 | d7189ea2ef694124821b033e533f18905b5e87ef | /galois/subset/fixpoint.lean | 7d9d7e8de8cc0da171c75b42db12b05e39454aec | [
"Apache-2.0"
] | permissive | digama0/lean-protocol-support | eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59 | cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda | refs/heads/master | 1,625,421,450,627 | 1,506,035,462,000 | 1,506,035,462,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,327 | lean | import .subset
universes u v
namespace subset
section
parameters {A : Type u} {B : Type v} (F : subset A → subset B)
def cocontinuous :=
∀ (Ix : Type) (f : Ix → subset A),
F (union_ix f) = union_ix (F ∘ f)
def cocontinuous_inh :=
∀ (Ix : Type) [inhabited Ix] (f : Ix → subset A),
F (union_ix f) = union_ix (F ∘ f)
def continuous :=
∀ (Ix : Type) (f : Ix → subset A),
F (intersection_ix f) = intersection_ix (F ∘ f)
def continuous_inh :=
∀ (Ix : Type) [inhabited Ix] (f : Ix → subset A),
F (intersection_ix f) = intersection_ix (F ∘ f)
end
section
parameter {A : Type u}
/-- Given a function over subsets, the greatest fixpoint
for that function is the union of all sets that might
be produced by applying the fixpoint-/
def greatest_fixpoint (F : subset A → subset A) : subset A
:= union_st (λ P, P ≤ F P)
/-- Given a function over subsets, the greatest fixpoint
for that function is the intersection of all sets that might
be produced by applying the fixpoint-/
def least_fixpoint (F : subset A → subset A) : subset A
:= intersection_st (λ P, F P ≤ P)
section
parameters (F : subset A → subset A) (Fmono : monotone F)
include Fmono
/-- Applying F to a greatest fixpoint of F results in
a set that includes the greatest fixpoint
this should likely be an internal only lemma -/
lemma greatest_fixpoint_postfixed
: greatest_fixpoint F ≤ F (greatest_fixpoint F)
:= begin
intros tr H, induction H, apply Fmono, tactic.swap,
apply a, assumption, clear a_1 tr,
intros x H, constructor; assumption,
end
/-- Applying F to a greatest fixpoint of F results in
the same set --/
lemma greatest_fixpoint_fixed
: greatest_fixpoint F = F (greatest_fixpoint F)
:= begin
apply included_eq,
{ apply greatest_fixpoint_postfixed, assumption },
{ intros x H, constructor, apply Fmono,
apply greatest_fixpoint_postfixed, assumption,
assumption
}
end
/-- Applying a function to the fixpoint is no smaller than the fixpoint -/
lemma least_fixpoint_prefixed
: F (least_fixpoint F) ≤ least_fixpoint F
:= begin
unfold least_fixpoint intersection_st,
intros x H P HP, apply HP, revert x H,
apply Fmono, intros x H, apply H, dsimp, assumption
end
/-- Applying a function to the fixpoint does not change the set -/
lemma least_fixpoint_fixed
: F (least_fixpoint F) = least_fixpoint F
:= begin
apply included_eq,
{ apply least_fixpoint_prefixed, assumption },
{ intros x H,
apply H, dsimp, apply Fmono,
apply least_fixpoint_prefixed, assumption,
}
end
end
lemma greatest_fixpoint_and_le (F G : subset A → subset A)
: greatest_fixpoint (λ X, F X ∩ G X)
≤ greatest_fixpoint F ∩ greatest_fixpoint G
:= begin
unfold greatest_fixpoint,
apply included_trans,
tactic.swap,
apply union_st_bintersection,
apply union_st_mono, intros x H,
constructor; intros z Hz;
specialize (H z Hz); induction H with H1 H2; assumption
end
/-- A function F from subset to subset is finitary if
an arbitrary application can be described as the union of some number of applications to finite arguments -/
def finitary (F : subset A → subset A) : Prop :=
∀ x, F x = union_ix_st (λ xs : list A, from_list xs ≤ x) (λ xs, F (from_list xs))
/-- Finitary functions are monotone -/
lemma finitary_monotone (F : subset A → subset A)
(Ffin : finitary F) : monotone F :=
begin
intros P Q PQ,
repeat { rw Ffin },
intros x H,
induction H,
constructor, apply included_trans; assumption,
assumption,
end
def chain_cont (F : subset A → subset A) :=
∀ (f : ℕ → subset A),
(∀ x y : ℕ, x ≤ y → f x ≤ f y)
→ F (union_ix f) = union_ix (F ∘ f)
def chain_cocont (F : subset A → subset A) :=
∀ (f : ℕ → subset A),
(∀ x y : ℕ, x ≤ y → f y ≤ f x)
→ F (intersection_ix f) = intersection_ix (F ∘ f)
protected def simple_chain (P Q : subset A) : ℕ → subset A
| 0 := P
| (nat.succ n) := Q
private lemma simple_chain_mono
{P Q : subset A} (PQ : P ≤ Q) (x y : ℕ)
(H : x ≤ y) : simple_chain P Q x ≤ simple_chain P Q y
:= begin
induction H, apply included_refl,
apply included_trans, assumption,
clear ih_1 a x y,
cases b; simp [subset.simple_chain],
intros x Px, apply PQ, assumption,
apply included_refl,
end
private lemma simple_chain_anti
{P Q : subset A} (PQ : Q ≤ P) (x y : ℕ)
(H : x ≤ y) : simple_chain P Q y ≤ simple_chain P Q x
:= begin
induction H, apply included_refl,
apply included_trans, tactic.swap, assumption,
clear ih_1 a x y,
cases b; simp [subset.simple_chain],
intros x Px, apply PQ, assumption,
apply included_refl,
end
lemma chain_cont_union {F : subset A → subset A}
(H : chain_cont F) {P Q : subset A}
(PQ : P ≤ Q)
: F (P ∪ Q) = F P ∪ F Q
:= begin
unfold chain_cont at H,
specialize (H (subset.simple_chain P Q) (simple_chain_mono PQ)),
transitivity (F (union_ix (subset.simple_chain P Q))),
f_equal,
apply included_eq, rw imp_or at PQ, rw PQ,
intros x Qx, apply (union_ix_st.mk 1),
trivial, dsimp [subset.simple_chain], assumption,
intros x Hx, induction Hx with n _ Hn,
cases n; dsimp [subset.simple_chain] at Hn,
apply or.inl, assumption, apply or.inr, assumption,
rw H, clear H, apply included_eq,
intros x Hx, induction Hx with n _ Hn,
cases n; dsimp [function.comp, subset.simple_chain] at Hn,
apply or.inl, assumption, apply or.inr, assumption,
intros x Hx, induction Hx with Hx Hx,
apply (union_ix_st.mk 0), trivial,
dsimp [function.comp, subset.simple_chain], assumption,
apply (union_ix_st.mk 1), trivial,
dsimp [function.comp, subset.simple_chain], assumption,
end
lemma chain_cocont_intersection {F : subset A → subset A}
(H : chain_cocont F) {P Q : subset A}
(PQ : P ≤ Q)
: F (P ∩ Q) = F P ∩ F Q
:= begin
unfold chain_cocont at H,
specialize (H (subset.simple_chain Q P) (simple_chain_anti PQ)),
transitivity (F (intersection_ix (subset.simple_chain Q P))),
-- f_equal -- TODO: FIX, the tactic isn't working here
apply congr_arg,
apply included_eq,
intros x PQx n, induction PQx with Px Qx,
cases n; dsimp [subset.simple_chain]; assumption,
rw imp_and at PQ, rw ← PQ,
intros x Hx, apply (Hx 1),
rw H, clear H, apply included_eq,
intros x Hx, constructor, apply (Hx 1),
apply (Hx 0),
intros x FPQx, induction FPQx with FPx FQx,
intros n,
cases n; dsimp [function.comp, subset.simple_chain]; assumption,
end
lemma chain_cont_mono {F : subset A → subset A}
(H : chain_cont F) : monotone F
:= begin
unfold monotone, intros P Q PQ,
rw imp_or, rw ← chain_cont_union,
rw imp_or at PQ, rw PQ, assumption, assumption
end
lemma chain_cocont_mono {F : subset A → subset A}
(H : chain_cocont F) : monotone F
:=
begin
unfold monotone, intros P Q PQ,
rw imp_and, rw ← chain_cocont_intersection,
rw imp_and at PQ, rw ← PQ, assumption, assumption
end
/-- Repeatedly apply a function f starting with x. -/
def iterate {A : Type u} (f : A → A) (x : A) : ℕ → A
| 0 := x
| (nat.succ n) := f (iterate n)
/-- The least fixpoint is described by the union of all sets
indexed by the number of iterations
-/
def least_fixpointn (F : subset A → subset A) : subset A
:= union_ix (iterate F ff)
/-- The greatest fixpoint is described by the intersection of all sets
indexed by the number of iterations
-/
def greatest_fixpointn (F : subset A → subset A) : subset A
:= intersection_ix (iterate F tt)
lemma least_fixpointn_postfixed
{F : subset A → subset A}
(Fmono : monotone F)
: least_fixpointn F ≤ F (least_fixpointn F)
:= begin
intros x H, induction H with n _ Hn,
cases n; simp [iterate] at Hn, exfalso, apply Hn,
apply Fmono, tactic.swap, assumption,
clear Hn x,
intros x H, constructor, constructor, assumption,
end
lemma greatest_fixpointn_prefixed
{F : subset A → subset A}
(Fmono : monotone F)
: F (greatest_fixpointn F) ≤ greatest_fixpointn F
:= begin
intros x H n,
cases n; simp [iterate], apply Fmono,
tactic.swap, assumption, clear H x,
intros x H, apply H,
end
lemma continuous_chain_cocont {F : subset A → subset A}
(H : continuous_inh F) : chain_cocont F :=
begin
intros f fmono, apply H
end
lemma cocontinuous_chain_cont {F : subset A → subset A}
(H : cocontinuous F) : chain_cont F :=
begin
intros f fmono, apply H
end
lemma and_continuous_r (P : subset A)
: continuous_inh (bintersection P)
:= begin
unfold continuous_inh, intros Ix inh f, apply included_eq,
{ intros x H ix, dsimp [function.comp],
induction H with Hl Hr, constructor, assumption,
apply Hr },
{ intros x H, constructor,
specialize (H inh.default),
dsimp [function.comp] at H,
induction H with Hl Hr, assumption,
intros ix, specialize (H ix),
induction H with Hl Hr, assumption, }
end
lemma and_cocontinuous_r (P : subset A)
: cocontinuous (bintersection P)
:= begin
unfold cocontinuous, intros Ix f, apply included_eq,
{ intros x H, dsimp [function.comp],
induction H with Hl Hr, induction Hr,
constructor, trivial, constructor; assumption,
},
{ intros x H, induction H, induction a_1, constructor,
assumption, constructor; assumption
}
end
lemma or_continuous_r (P : subset A)
[decP : decidable_pred P]
: continuous (bunion P)
:= begin
unfold continuous, intros Ix f,
apply included_eq,
{ intros x H ix, dsimp [function.comp],
induction H with H H,
apply or.inl, assumption,
apply or.inr, apply H, },
{ intros x Hx,
have H := decP x, induction H with HP HP,
{ apply or.inr, intros n,
specialize (Hx n), induction Hx with Hl Hr,
contradiction, assumption },
{ apply or.inl, assumption }
}
end
lemma or_cocontinuous_r (P : subset A)
: cocontinuous_inh (bunion P)
:= begin
unfold cocontinuous_inh, intros Ix inh f,
apply included_eq,
{ intros x H, dsimp [function.comp],
induction H with H H,
constructor, trivial, apply or.inl,
assumption, apply inh.default,
induction H, constructor, trivial,
apply or.inr, assumption },
{ intros x Hx,
induction Hx with ix _ H', induction H',
apply or.inl, assumption,
apply or.inr, constructor, trivial, assumption,
}
end
lemma iterate_mono_tt_succ {F : subset A → subset A}
(Fmono : monotone F) (n : ℕ)
: iterate F tt n.succ ≤ iterate F tt n
:= begin
dsimp [iterate], induction n; dsimp [iterate],
apply tt_top, apply Fmono, assumption,
end
lemma iterate_mono_tt_n {F : subset A → subset A}
(Fmono : monotone F) (x y : ℕ)
(H : x ≤ y)
: iterate F tt y ≤ iterate F tt x
:= begin
induction H, apply included_refl,
simp [iterate], apply included_trans,
apply Fmono, assumption,
apply iterate_mono_tt_succ,
apply Fmono,
end
lemma greatest_fixpointn_fixed
{F : subset A → subset A}
(Fcont : chain_cocont F)
: F (greatest_fixpointn F) = greatest_fixpointn F
:= begin
apply included_eq, apply greatest_fixpointn_prefixed,
apply chain_cocont_mono,
assumption,
unfold greatest_fixpointn,
rw Fcont, intros x Hx,
intros n, dsimp [function.comp],
specialize (Hx n.succ), apply Hx,
intros, apply iterate_mono_tt_n,
apply chain_cocont_mono, assumption,
assumption
end
lemma le_greatest_fixpoint {P : subset A}
{F : subset A → subset A}
(H : P ≤ F P)
: P ≤ greatest_fixpoint F
:= begin
intros x H', constructor, apply H, assumption
end
lemma least_fixpoint_le {P : subset A}
{F : subset A → subset A}
(H : F P ≤ P)
: least_fixpoint F ≤ P
:= begin
intros x H', apply H', apply H
end
lemma greatest_fixpoint_le {P : subset A}
{F : subset A → subset A} (Fmono : monotone F)
(H : ∀ Q, Q = F Q → Q ≤ P)
: greatest_fixpoint F ≤ P
:= begin
apply (H _ (greatest_fixpoint_fixed F Fmono))
end
lemma le_least_fixpoint {P : subset A}
{F : subset A → subset A} (Fmono : monotone F)
(H : ∀ Q, F Q = Q → P ≤ Q)
: P ≤ least_fixpoint F
:= begin
apply (H _ (least_fixpoint_fixed F Fmono))
end
lemma iterate_mono_ff_succ {F : subset A → subset A}
(Fmono : monotone F) (n : ℕ)
: iterate F ff n ≤ iterate F ff n.succ
:= begin
dsimp [iterate], induction n; dsimp [iterate],
apply ff_bot, apply Fmono, assumption,
end
lemma iterate_mono_ff_n {F : subset A → subset A}
(Fmono : monotone F) (x y : ℕ)
(H : x ≤ y)
: iterate F ff x ≤ iterate F ff y
:= begin
induction H, apply included_refl,
simp [iterate], apply included_trans,
apply ih_1,
apply iterate_mono_ff_succ,
apply Fmono,
end
lemma iterate_mono {F : subset A → subset A}
(Fmono : monotone F) {P Q : subset A}
(PQ : P ≤ Q) (n : ℕ)
: iterate F P n ≤ iterate F Q n
:= begin
induction n; simp [iterate],
{ assumption },
{ apply Fmono, assumption }
end
lemma iterate_mono2 {F G : subset A → subset A}
{P : subset A}
(Fmono : monotone F)
(FG : ∀ x, F x ≤ G x)
(n : ℕ)
: iterate F P n ≤ iterate G P n
:= begin
induction n; simp [iterate],
{ apply included_refl },
{ apply included_trans, apply Fmono, assumption,
apply FG }
end
lemma least_fixpointn_fixed
{F : subset A → subset A}
(Fcoc : chain_cont F)
: least_fixpointn F = F (least_fixpointn F)
:= begin
apply included_eq, apply least_fixpointn_postfixed,
apply chain_cont_mono, assumption,
unfold least_fixpointn,
rw Fcoc, intros x Hx,
induction Hx with n _ Hn,
dsimp [function.comp] at Hn,
constructor, assumption,
tactic.swap, exact n.succ,
apply Hn, intros,
apply iterate_mono_ff_n,
apply chain_cont_mono, assumption, assumption
end
/-- The fixpoint defined by greatest_fixpointn is actually a greatest fixpoint-/
lemma greatest_fixpointn_same
{F : subset A → subset A}
(Fcoc : chain_cocont F)
: greatest_fixpointn F = greatest_fixpoint F
:= begin
apply included_eq,
apply le_greatest_fixpoint,
rw (greatest_fixpointn_fixed Fcoc),
apply (included_refl (greatest_fixpointn F)),
apply (greatest_fixpoint_le _ _),
apply chain_cocont_mono, assumption,
intros Q HQ, intros x Qx,
have HQx : ∀ n, Q = iterate F Q n,
intros n, induction n; simp [iterate],
rw ← ih_1, assumption,
intros n, apply iterate_mono,
apply chain_cocont_mono, assumption,
tactic.swap, rw (HQx n) at Qx,
assumption, apply tt_top
end
/-- The fixpoint defined by least_fixpointn is actually a least fixpoint-/
lemma least_fixpointn_same
{F : subset A → subset A}
(Fchain_cont : chain_cont F)
: least_fixpointn F = least_fixpoint F
:= begin
apply included_eq, tactic.swap,
apply least_fixpoint_le,
rw ← (least_fixpointn_fixed Fchain_cont),
apply included_refl,
apply (le_least_fixpoint _ _),
apply chain_cont_mono, assumption,
intros Q HQ, intros x Qx,
have HQx : ∀ n, Q = iterate F Q n,
intros n, induction n; simp [iterate],
rw ← ih_1, symmetry, assumption,
unfold least_fixpointn at Qx,
induction Qx with n _ Hn,
rw (HQx n), apply iterate_mono,
apply chain_cont_mono, assumption,
tactic.swap, assumption, apply ff_bot,
end
lemma greatest_fixpoint_mono
{F G : subset A → subset A}
(H : ∀ P, F P ≤ G P)
: greatest_fixpoint F ≤ greatest_fixpoint G
:= begin
intros x Hx, induction Hx,
constructor, tactic.swap, apply a_1,
dsimp, apply included_trans, apply a, apply H
end
lemma greatest_fixpointn_mono
{F G : subset A → subset A}
(Fmono : monotone F)
(H : ∀ P, F P ≤ G P)
: greatest_fixpointn F ≤ greatest_fixpointn G
:= begin
unfold greatest_fixpointn,
intros x H n, specialize (H n),
revert H, apply iterate_mono2,
assumption, assumption
end
lemma and_functional_mono
{F G : subset A → subset A}
(Fmono : monotone F) (Gmono : monotone G)
: monotone (λ X, F X ∩ G X)
:= begin
unfold monotone, intros P Q PQ, apply bintersection_mono,
apply Fmono, assumption, apply Gmono, assumption
end
lemma greatest_fixpointn_and_le (F G : subset A → subset A)
(Fmono : monotone F) (Gmono : monotone G)
: greatest_fixpointn (λ X, F X ∩ G X)
≤ greatest_fixpointn F ∩ greatest_fixpointn G
:= begin
intros x H,
constructor,
{ revert H,
apply greatest_fixpointn_mono, apply and_functional_mono,
apply Fmono, apply Gmono,
intros P x H, induction H with Hl Hr, apply Hl,
},
{ revert H,
apply greatest_fixpointn_mono, apply and_functional_mono,
apply Fmono, apply Gmono,
intros P x H, induction H with Hl Hr, apply Hr,
}
end
lemma tImp_cocontinuous_l {Ix : Type}
(P : Ix → subset A) (Q : subset A)
: (union_ix P => Q) = intersection_ix (λ ix, P ix => Q)
:= begin
apply included_eq; intros x Hx,
{ intros n Pn, apply Hx,
constructor, trivial, assumption },
{ intros H, induction H, apply Hx, assumption }
end
end
end subset |
4d728b2dd8da998cc04f273f778ca5f193a9992b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/preadditive/schur.lean | 2faa96f55b3955af10110d3b03364676e5c19e1c | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 8,310 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Markus Himmel, Scott Morrison
-/
import algebra.group.ext
import category_theory.simple
import category_theory.linear.basic
import category_theory.endomorphism
import algebra.algebra.spectrum
/-!
# Schur's lemma
We first prove the part of Schur's Lemma that holds in any preadditive category with kernels,
that any nonzero morphism between simple objects
is an isomorphism.
Second, we prove Schur's lemma for `𝕜`-linear categories with finite dimensional hom spaces,
over an algebraically closed field `𝕜`:
the hom space `X ⟶ Y` between simple objects `X` and `Y` is at most one dimensional,
and is 1-dimensional iff `X` and `Y` are isomorphic.
-/
namespace category_theory
open category_theory.limits
variables {C : Type*} [category C]
variables [preadditive C]
-- See also `epi_of_nonzero_to_simple`, which does not require `preadditive C`.
lemma mono_of_nonzero_from_simple [has_kernels C] {X Y : C} [simple X] {f : X ⟶ Y} (w : f ≠ 0) :
mono f :=
preadditive.mono_of_kernel_zero (kernel_zero_of_nonzero_from_simple w)
/--
The part of **Schur's lemma** that holds in any preadditive category with kernels:
that a nonzero morphism between simple objects is an isomorphism.
-/
lemma is_iso_of_hom_simple [has_kernels C] {X Y : C} [simple X] [simple Y] {f : X ⟶ Y} (w : f ≠ 0) :
is_iso f :=
begin
haveI := mono_of_nonzero_from_simple w,
exact is_iso_of_mono_of_nonzero w
end
/--
As a corollary of Schur's lemma for preadditive categories,
any morphism between simple objects is (exclusively) either an isomorphism or zero.
-/
lemma is_iso_iff_nonzero [has_kernels C] {X Y : C} [simple X] [simple Y] (f : X ⟶ Y) :
is_iso f ↔ f ≠ 0 :=
⟨λ I,
begin
introI h,
apply id_nonzero X,
simp only [←is_iso.hom_inv_id f, h, zero_comp],
end,
λ w, is_iso_of_hom_simple w⟩
/--
In any preadditive category with kernels,
the endomorphisms of a simple object form a division ring.
-/
noncomputable
instance [has_kernels C] {X : C} [simple X] : division_ring (End X) :=
by classical; exact
{ inv := λ f, if h : f = 0 then 0 else by { haveI := is_iso_of_hom_simple h, exact inv f, },
exists_pair_ne := ⟨𝟙 X, 0, id_nonzero _⟩,
inv_zero := dif_pos rfl,
mul_inv_cancel := λ f h, begin
haveI := is_iso_of_hom_simple h,
convert is_iso.inv_hom_id f,
exact dif_neg h,
end,
..(infer_instance : ring (End X)) }
open finite_dimensional
section
variables (𝕜 : Type*) [division_ring 𝕜]
/--
Part of **Schur's lemma** for `𝕜`-linear categories:
the hom space between two non-isomorphic simple objects is 0-dimensional.
-/
lemma finrank_hom_simple_simple_eq_zero_of_not_iso
[has_kernels C] [linear 𝕜 C] {X Y : C} [simple X] [simple Y]
(h : (X ≅ Y) → false):
finrank 𝕜 (X ⟶ Y) = 0 :=
begin
haveI := subsingleton_of_forall_eq (0 : X ⟶ Y) (λ f, begin
have p := not_congr (is_iso_iff_nonzero f),
simp only [not_not, ne.def] at p,
refine p.mp (λ _, by exactI h (as_iso f)),
end),
exact finrank_zero_of_subsingleton,
end
end
variables (𝕜 : Type*) [field 𝕜]
variables [is_alg_closed 𝕜] [linear 𝕜 C]
-- In the proof below we have some difficulty using `I : finite_dimensional 𝕜 (X ⟶ X)`
-- where we need a `finite_dimensional 𝕜 (End X)`.
-- These are definitionally equal, but without eta reduction Lean can't see this.
-- To get around this, we use `convert I`,
-- then check the various instances agree field-by-field,
/--
An auxiliary lemma for Schur's lemma.
If `X ⟶ X` is finite dimensional, and every nonzero endomorphism is invertible,
then `X ⟶ X` is 1-dimensional.
-/
-- We prove this with the explicit `is_iso_iff_nonzero` assumption,
-- rather than just `[simple X]`, as this form is useful for
-- Müger's formulation of semisimplicity.
lemma finrank_endomorphism_eq_one
{X : C} (is_iso_iff_nonzero : ∀ f : X ⟶ X, is_iso f ↔ f ≠ 0)
[I : finite_dimensional 𝕜 (X ⟶ X)] :
finrank 𝕜 (X ⟶ X) = 1 :=
begin
have id_nonzero := (is_iso_iff_nonzero (𝟙 X)).mp (by apply_instance),
apply finrank_eq_one (𝟙 X),
{ exact id_nonzero, },
{ intro f,
haveI : nontrivial (End X) := nontrivial_of_ne _ _ id_nonzero,
obtain ⟨c, nu⟩ := @spectrum.nonempty_of_is_alg_closed_of_finite_dimensional 𝕜 (End X) _ _ _ _ _
(by { convert I, ext, refl, ext, refl, }) (End.of f),
use c,
rw [spectrum.mem_iff, is_unit.sub_iff, is_unit_iff_is_iso, is_iso_iff_nonzero, ne.def,
not_not, sub_eq_zero, algebra.algebra_map_eq_smul_one] at nu,
exact nu.symm, },
end
variables [has_kernels C]
/--
**Schur's lemma** for endomorphisms in `𝕜`-linear categories.
-/
lemma finrank_endomorphism_simple_eq_one
(X : C) [simple X] [I : finite_dimensional 𝕜 (X ⟶ X)] :
finrank 𝕜 (X ⟶ X) = 1 :=
finrank_endomorphism_eq_one 𝕜 is_iso_iff_nonzero
lemma endomorphism_simple_eq_smul_id
{X : C} [simple X] [I : finite_dimensional 𝕜 (X ⟶ X)] (f : X ⟶ X) :
∃ c : 𝕜, c • 𝟙 X = f :=
(finrank_eq_one_iff_of_nonzero' (𝟙 X) (id_nonzero X)).mp (finrank_endomorphism_simple_eq_one 𝕜 X) f
/--
Endomorphisms of a simple object form a field if they are finite dimensional.
This can't be an instance as `𝕜` would be undetermined.
-/
noncomputable
def field_End_of_finite_dimensional (X : C) [simple X] [I : finite_dimensional 𝕜 (X ⟶ X)] :
field (End X) :=
by classical; exact
{ mul_comm := λ f g, begin
obtain ⟨c, rfl⟩ := endomorphism_simple_eq_smul_id 𝕜 f,
obtain ⟨d, rfl⟩ := endomorphism_simple_eq_smul_id 𝕜 g,
simp [←mul_smul, mul_comm c d],
end,
..(infer_instance : division_ring (End X)) }
/--
**Schur's lemma** for `𝕜`-linear categories:
if hom spaces are finite dimensional, then the hom space between simples is at most 1-dimensional.
See `finrank_hom_simple_simple_eq_one_iff` and `finrank_hom_simple_simple_eq_zero_iff` below
for the refinements when we know whether or not the simples are isomorphic.
-/
-- There is a symmetric argument that uses `[finite_dimensional 𝕜 (Y ⟶ Y)]` instead,
-- but we don't bother proving that here.
lemma finrank_hom_simple_simple_le_one
(X Y : C) [finite_dimensional 𝕜 (X ⟶ X)] [simple X] [simple Y] :
finrank 𝕜 (X ⟶ Y) ≤ 1 :=
begin
cases subsingleton_or_nontrivial (X ⟶ Y) with h,
{ resetI,
rw finrank_zero_of_subsingleton,
exact zero_le_one },
{ obtain ⟨f, nz⟩ := (nontrivial_iff_exists_ne 0).mp h,
haveI fi := (is_iso_iff_nonzero f).mpr nz,
apply finrank_le_one f,
intro g,
obtain ⟨c, w⟩ := endomorphism_simple_eq_smul_id 𝕜 (g ≫ inv f),
exact ⟨c, by simpa using w =≫ f⟩, },
end
lemma finrank_hom_simple_simple_eq_one_iff
(X Y : C) [finite_dimensional 𝕜 (X ⟶ X)] [finite_dimensional 𝕜 (X ⟶ Y)] [simple X] [simple Y] :
finrank 𝕜 (X ⟶ Y) = 1 ↔ nonempty (X ≅ Y) :=
begin
fsplit,
{ intro h,
rw finrank_eq_one_iff' at h,
obtain ⟨f, nz, -⟩ := h,
rw ←is_iso_iff_nonzero at nz,
exactI ⟨as_iso f⟩, },
{ rintro ⟨f⟩,
have le_one := finrank_hom_simple_simple_le_one 𝕜 X Y,
have zero_lt : 0 < finrank 𝕜 (X ⟶ Y) :=
finrank_pos_iff_exists_ne_zero.mpr ⟨f.hom, (is_iso_iff_nonzero f.hom).mp infer_instance⟩,
linarith, }
end
lemma finrank_hom_simple_simple_eq_zero_iff
(X Y : C) [finite_dimensional 𝕜 (X ⟶ X)] [finite_dimensional 𝕜 (X ⟶ Y)] [simple X] [simple Y] :
finrank 𝕜 (X ⟶ Y) = 0 ↔ is_empty (X ≅ Y) :=
begin
rw [← not_nonempty_iff, ← not_congr (finrank_hom_simple_simple_eq_one_iff 𝕜 X Y)],
refine ⟨λ h, by { rw h, simp, }, λ h, _⟩,
have := finrank_hom_simple_simple_le_one 𝕜 X Y,
interval_cases finrank 𝕜 (X ⟶ Y) with h',
{ exact h', },
{ exact false.elim (h h'), },
end
open_locale classical
lemma finrank_hom_simple_simple
(X Y : C) [∀ X Y : C, finite_dimensional 𝕜 (X ⟶ Y)] [simple X] [simple Y] :
finrank 𝕜 (X ⟶ Y) = if nonempty (X ≅ Y) then 1 else 0 :=
begin
split_ifs,
exact (finrank_hom_simple_simple_eq_one_iff 𝕜 X Y).2 h,
exact (finrank_hom_simple_simple_eq_zero_iff 𝕜 X Y).2 (not_nonempty_iff.mp h),
end
end category_theory
|
ad251b66b18487d2f22e3e2ce605c4923b879880 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/algebraic_geometry/function_field.lean | a3a3ed90ca9c53391bf336ba92111369f7cec2a6 | [
"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,164 | lean | /-
Copyright (c) 2022 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import algebraic_geometry.properties
/-!
# Function field of integral schemes
We define the function field of an irreducible scheme as the stalk of the generic point.
This is a field when the scheme is integral.
## Main definition
* `algebraic_geometry.Scheme.function_field`: The function field of an integral scheme.
* `algebraic_geometry.germ_to_function_field`: The canonical map from a component into the function
field. This map is injective.
-/
universes u v
open topological_space opposite category_theory category_theory.limits Top
namespace algebraic_geometry
variable (X : Scheme)
/-- The function field of an irreducible scheme is the local ring at its generic point.
Despite the name, this is a field only when the scheme is integral. -/
noncomputable
abbreviation Scheme.function_field [irreducible_space X.carrier] : CommRing :=
X.presheaf.stalk (generic_point X.carrier)
/-- The restriction map from a component to the function field. -/
noncomputable
abbreviation Scheme.germ_to_function_field [irreducible_space X.carrier] (U : opens X.carrier)
[h : nonempty U] : X.presheaf.obj (op U) ⟶ X.function_field :=
X.presheaf.germ ⟨generic_point X.carrier,
((generic_point_spec X.carrier).mem_open_set_iff U.prop).mpr (by simpa using h)⟩
noncomputable
instance [irreducible_space X.carrier] (U : opens X.carrier) [nonempty U] :
algebra (X.presheaf.obj (op U)) X.function_field :=
(X.germ_to_function_field U).to_algebra
noncomputable
instance [is_integral X] : field X.function_field :=
begin
apply field_of_is_unit_or_eq_zero,
intro a,
obtain ⟨U, m, s, rfl⟩ := Top.presheaf.germ_exist _ _ a,
rw [or_iff_not_imp_right, ← (X.presheaf.germ ⟨_, m⟩).map_zero],
intro ha,
replace ha := ne_of_apply_ne _ ha,
have hs : generic_point X.carrier ∈ RingedSpace.basic_open _ s,
{ rw [← opens.mem_coe, (generic_point_spec X.carrier).mem_open_set_iff, set.top_eq_univ,
set.univ_inter, ← set.ne_empty_iff_nonempty, ne.def, ← opens.coe_bot,
subtype.coe_injective.eq_iff, ← opens.empty_eq],
erw basic_open_eq_bot_iff,
exacts [ha, (RingedSpace.basic_open _ _).prop] },
have := (X.presheaf.germ ⟨_, hs⟩).is_unit_map (RingedSpace.is_unit_res_basic_open _ s),
rwa Top.presheaf.germ_res_apply at this
end
lemma germ_injective_of_is_integral [is_integral X] {U : opens X.carrier} (x : U) :
function.injective (X.presheaf.germ x) :=
begin
rw ring_hom.injective_iff,
intros y hy,
rw ← (X.presheaf.germ x).map_zero at hy,
obtain ⟨W, hW, iU, iV, e⟩ := X.presheaf.germ_eq _ x.prop x.prop _ _ hy,
cases (show iU = iV, from subsingleton.elim _ _),
haveI : nonempty W := ⟨⟨_, hW⟩⟩,
exact map_injective_of_is_integral X iU e
end
lemma Scheme.germ_to_function_field_injective [is_integral X] (U : opens X.carrier)
[nonempty U] : function.injective (X.germ_to_function_field U) :=
germ_injective_of_is_integral _ _
lemma generic_point_eq_of_is_open_immersion {X Y : Scheme} (f : X ⟶ Y) [H : is_open_immersion f]
[hX : irreducible_space X.carrier] [irreducible_space Y.carrier] :
f.1.base (generic_point X.carrier : _) = (generic_point Y.carrier : _) :=
begin
apply ((generic_point_spec _).eq _).symm,
show t0_space Y.carrier, by apply_instance,
convert (generic_point_spec X.carrier).image (show continuous f.1.base, by continuity),
symmetry,
rw [eq_top_iff, set.top_eq_univ, set.top_eq_univ],
convert subset_closure_inter_of_is_preirreducible_of_is_open _ H.base_open.open_range _,
rw [set.univ_inter, set.image_univ],
apply_with preirreducible_space.is_preirreducible_univ { instances := ff },
show preirreducible_space Y.carrier, by apply_instance,
exact ⟨_, trivial, set.mem_range_self hX.2.some⟩,
end
noncomputable
instance stalk_function_field_algebra [irreducible_space X.carrier] (x : X.carrier) :
algebra (X.presheaf.stalk x) X.function_field :=
begin
apply ring_hom.to_algebra,
exact X.presheaf.stalk_specializes ((generic_point_spec X.carrier).specializes trivial)
end
instance function_field_is_scalar_tower [irreducible_space X.carrier] (U : opens X.carrier) (x : U)
[nonempty U] :
is_scalar_tower (X.presheaf.obj $ op U) (X.presheaf.stalk x) X.function_field :=
begin
apply is_scalar_tower.of_algebra_map_eq',
simp_rw [ring_hom.algebra_map_to_algebra],
change _ = X.presheaf.germ x ≫ _,
rw X.presheaf.germ_stalk_specializes,
refl
end
noncomputable
instance (R : CommRing) [is_domain R] : algebra R (Scheme.Spec.obj $ op R).function_field :=
begin
apply ring_hom.to_algebra,
exact structure_sheaf.to_stalk R _,
end
@[simp] lemma generic_point_eq_bot_of_affine (R : CommRing) [is_domain R] :
generic_point (Scheme.Spec.obj $ op R).carrier = (⟨0, ideal.bot_prime⟩ : prime_spectrum R) :=
begin
apply (generic_point_spec (Scheme.Spec.obj $ op R).carrier).eq,
simp [is_generic_point_def, ← prime_spectrum.zero_locus_vanishing_ideal_eq_closure]
end
instance function_field_is_fraction_ring_of_affine (R : CommRing.{u}) [is_domain R] :
is_fraction_ring R (Scheme.Spec.obj $ op R).function_field :=
begin
convert structure_sheaf.is_localization.to_stalk R _,
delta is_fraction_ring is_localization.at_prime,
congr' 1,
rw generic_point_eq_bot_of_affine,
ext,
exact mem_non_zero_divisors_iff_ne_zero
end
instance {X : Scheme} [is_integral X] {U : opens X.carrier} [hU : nonempty U] :
is_integral (X.restrict U.open_embedding) :=
begin
haveI : nonempty (X.restrict U.open_embedding).carrier := hU,
exact is_integral_of_open_immersion (X.of_restrict U.open_embedding)
end
lemma is_affine_open.prime_ideal_of_generic_point {X : Scheme} [is_integral X]
{U : opens X.carrier} (hU : is_affine_open U) [h : nonempty U] :
hU.prime_ideal_of ⟨generic_point X.carrier,
((generic_point_spec X.carrier).mem_open_set_iff U.prop).mpr (by simpa using h)⟩ =
generic_point (Scheme.Spec.obj $ op $ X.presheaf.obj $ op U).carrier :=
begin
haveI : is_affine _ := hU,
have e : U.open_embedding.is_open_map.functor.obj ⊤ = U,
{ ext1, exact set.image_univ.trans subtype.range_coe },
delta is_affine_open.prime_ideal_of,
rw ← Scheme.comp_val_base_apply,
convert (generic_point_eq_of_is_open_immersion ((X.restrict U.open_embedding).iso_Spec.hom ≫
Scheme.Spec.map (X.presheaf.map (eq_to_hom e).op).op)),
ext1,
exact (generic_point_eq_of_is_open_immersion (X.of_restrict U.open_embedding)).symm
end
lemma function_field_is_fraction_ring_of_is_affine_open [is_integral X] (U : opens X.carrier)
(hU : is_affine_open U) [hU' : nonempty U] :
is_fraction_ring (X.presheaf.obj $ op U) X.function_field :=
begin
haveI : is_affine _ := hU,
haveI : nonempty (X.restrict U.open_embedding).carrier := hU',
haveI : is_integral (X.restrict U.open_embedding) := @@is_integral_of_is_affine_is_domain _ _ _
(by { dsimp, rw opens.open_embedding_obj_top, apply_instance }),
have e : U.open_embedding.is_open_map.functor.obj ⊤ = U,
{ ext1, exact set.image_univ.trans subtype.range_coe },
delta is_fraction_ring Scheme.function_field,
convert hU.is_localization_stalk ⟨generic_point X.carrier, _⟩ using 1,
rw [hU.prime_ideal_of_generic_point, generic_point_eq_bot_of_affine],
ext, exact mem_non_zero_divisors_iff_ne_zero
end
instance (x : X.carrier) : is_affine (X.affine_cover.obj x) :=
algebraic_geometry.Spec_is_affine _
instance [h : is_integral X] (x : X.carrier) :
is_fraction_ring (X.presheaf.stalk x) X.function_field :=
begin
let U : opens X.carrier := ⟨set.range (X.affine_cover.map x).1.base,
PresheafedSpace.is_open_immersion.base_open.open_range⟩,
haveI : nonempty U := ⟨⟨_, X.affine_cover.covers x⟩⟩,
have hU : is_affine_open U := range_is_affine_open_of_open_immersion (X.affine_cover.map x),
exact @@is_fraction_ring.is_fraction_ring_of_is_domain_of_is_localization _ _ _ _ _ _ _ _ _ _ _
(hU.is_localization_stalk ⟨x, X.affine_cover.covers x⟩)
(function_field_is_fraction_ring_of_is_affine_open X U hU)
end
end algebraic_geometry
|
e0e52a6b361b296ec6cabf43b79ac533ee7118c4 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/lex.lean | 0bb01d7df3e41f7980a862e1f34f3097b0e42065 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 2,960 | lean | import Init.Control.Except
inductive Tok where
| lpar
| rpar
| plus
| minus
| times
| divide
| num : Nat → Tok
deriving Repr
structure Token where
text : String -- Let's avoid parentheses in structures. This is legacy from Lean 3.
tok : Tok
deriving Repr
inductive LexErr where
| unexpected : Char → LexErr
| notDigit : Char → LexErr
deriving Repr
def Char.digit? (char : Char) : Option Nat :=
if char.isDigit then
some (char.toNat - '0'.toNat)
else
none
mutual
def lex [Monad m] [MonadExceptOf LexErr m] (it : String.Iterator) : m (List Token) := do
if it.atEnd then
return []
else
match it.curr with
| '(' => return { text := "(", tok := Tok.lpar } :: (← lex it.next)
| ')' => return { text := ")", tok := Tok.rpar } :: (← lex it.next)
| '+' => return { text := "+", tok := Tok.plus } :: (← lex it.next)
| other =>
match other.digit? with
| none => throw <| LexErr.unexpected other
| some d => lexnumber d [other] it.next
def lexnumber [Monad m] [MonadExceptOf LexErr m] (soFar : Nat) (text : List Char) (it : String.Iterator) : m (List Token) :=
if it.atEnd then
return [{ text := text.reverse.asString, tok := Tok.num soFar }]
else
let c := it.curr
match c.digit? with
| none => return { text := text.reverse.asString, tok := Tok.num soFar } :: (← lex it)
| some d => lexnumber (soFar * 10 + d) (c :: text) it.next
end
#eval lex (m := Except LexErr) "".iter
#eval lex (m := Except LexErr) "123".iter
#eval lex (m := Except LexErr) "1+23".iter
#eval lex (m := Except LexErr) "1+23()".iter
def Option.toList : Option α -> List α
| none => []
| some x => [x]
namespace NonMutual
def lex [Monad m] [MonadExceptOf LexErr m] (current? : Option (List Char × Nat)) (it : String.Iterator) : m (List Token) := do
let currTok := fun
| (cs, n) => { text := {data := cs.reverse}, tok := Tok.num n }
if it.atEnd then
return current?.toList.map currTok
else
let emit (tok : Token) (xs : List Token) : List Token :=
match current? with
| none => tok :: xs
| some numInfo => currTok numInfo :: tok :: xs;
match it.curr with
| '(' => return emit { text := "(", tok := Tok.lpar } (← lex none it.next)
| ')' => return emit { text := ")", tok := Tok.rpar } (← lex none it.next)
| '+' => return emit { text := "+", tok := Tok.plus } (← lex none it.next)
| other =>
match other.digit? with
| none => throw <| LexErr.unexpected other
| some d => match current? with
| none => lex (some ([other], d)) it.next
| some (tokTxt, soFar) => lex (other :: tokTxt, soFar * 10 + d) it.next
#eval lex (m := Except LexErr) none "".iter
#eval lex (m := Except LexErr) none "123".iter
#eval lex (m := Except LexErr) none "1+23".iter
#eval lex (m := Except LexErr) none "1+23()".iter
|
4569ca04a60ddd405bde059a4a673049d74b4ec0 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/ring_theory/coprime/basic.lean | 81e19a8c3e464c9e36e6e34760189e656c1301d8 | [
"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 | 9,996 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Ken Lee, Chris Hughes
-/
import tactic.ring
import algebra.ring.basic
/-!
# Coprime elements of a ring
## Main definitions
* `is_coprime x y`: that `x` and `y` are coprime, defined to be the existence of `a` and `b` such
that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime,
e.g., the multivariate polynomials `x₁` and `x₂` are not coprime.
See also `ring_theory.coprime.lemmas` for further development of coprime elements.
-/
open_locale classical
universes u v
section comm_semiring
variables {R : Type u} [comm_semiring R] (x y z : R)
/-- The proposition that `x` and `y` are coprime, defined to be the existence of `a` and `b` such
that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime,
e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. -/
@[simp] def is_coprime : Prop :=
∃ a b, a * x + b * y = 1
variables {x y z}
theorem is_coprime.symm (H : is_coprime x y) : is_coprime y x :=
let ⟨a, b, H⟩ := H in ⟨b, a, by rw [add_comm, H]⟩
theorem is_coprime_comm : is_coprime x y ↔ is_coprime y x :=
⟨is_coprime.symm, is_coprime.symm⟩
theorem is_coprime_self : is_coprime x x ↔ is_unit x :=
⟨λ ⟨a, b, h⟩, is_unit_of_mul_eq_one x (a + b) $ by rwa [mul_comm, add_mul],
λ h, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 h in ⟨b, 0, by rwa [zero_mul, add_zero]⟩⟩
theorem is_coprime_zero_left : is_coprime 0 x ↔ is_unit x :=
⟨λ ⟨a, b, H⟩, is_unit_of_mul_eq_one x b $ by rwa [mul_zero, zero_add, mul_comm] at H,
λ H, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 H in ⟨1, b, by rwa [one_mul, zero_add]⟩⟩
theorem is_coprime_zero_right : is_coprime x 0 ↔ is_unit x :=
is_coprime_comm.trans is_coprime_zero_left
lemma not_coprime_zero_zero [nontrivial R] : ¬ is_coprime (0 : R) 0 :=
mt is_coprime_zero_right.mp not_is_unit_zero
theorem is_coprime_one_left : is_coprime 1 x :=
⟨1, 0, by rw [one_mul, zero_mul, add_zero]⟩
theorem is_coprime_one_right : is_coprime x 1 :=
⟨0, 1, by rw [one_mul, zero_mul, zero_add]⟩
theorem is_coprime.dvd_of_dvd_mul_right (H1 : is_coprime x z) (H2 : x ∣ y * z) : x ∣ y :=
let ⟨a, b, H⟩ := H1 in by { rw [← mul_one y, ← H, mul_add, ← mul_assoc, mul_left_comm],
exact dvd_add (dvd_mul_left _ _) (H2.mul_left _) }
theorem is_coprime.dvd_of_dvd_mul_left (H1 : is_coprime x y) (H2 : x ∣ y * z) : x ∣ z :=
let ⟨a, b, H⟩ := H1 in by { rw [← one_mul z, ← H, add_mul, mul_right_comm, mul_assoc b],
exact dvd_add (dvd_mul_left _ _) (H2.mul_left _) }
theorem is_coprime.mul_left (H1 : is_coprime x z) (H2 : is_coprime y z) : is_coprime (x * y) z :=
let ⟨a, b, h1⟩ := H1, ⟨c, d, h2⟩ := H2 in
⟨a * c, a * x * d + b * c * y + b * d * z,
calc a * c * (x * y) + (a * x * d + b * c * y + b * d * z) * z
= (a * x + b * z) * (c * y + d * z) : by ring
... = 1 : by rw [h1, h2, mul_one]⟩
theorem is_coprime.mul_right (H1 : is_coprime x y) (H2 : is_coprime x z) : is_coprime x (y * z) :=
by { rw is_coprime_comm at H1 H2 ⊢, exact H1.mul_left H2 }
theorem is_coprime.mul_dvd (H : is_coprime x y) (H1 : x ∣ z) (H2 : y ∣ z) : x * y ∣ z :=
begin
obtain ⟨a, b, h⟩ := H,
rw [← mul_one z, ← h, mul_add],
apply dvd_add,
{ rw [mul_comm z, mul_assoc],
exact (mul_dvd_mul_left _ H2).mul_left _ },
{ rw [mul_comm b, ← mul_assoc],
exact (mul_dvd_mul_right H1 _).mul_right _ }
end
theorem is_coprime.of_mul_left_left (H : is_coprime (x * y) z) : is_coprime x z :=
let ⟨a, b, h⟩ := H in ⟨a * y, b, by rwa [mul_right_comm, mul_assoc]⟩
theorem is_coprime.of_mul_left_right (H : is_coprime (x * y) z) : is_coprime y z :=
by { rw mul_comm at H, exact H.of_mul_left_left }
theorem is_coprime.of_mul_right_left (H : is_coprime x (y * z)) : is_coprime x y :=
by { rw is_coprime_comm at H ⊢, exact H.of_mul_left_left }
theorem is_coprime.of_mul_right_right (H : is_coprime x (y * z)) : is_coprime x z :=
by { rw mul_comm at H, exact H.of_mul_right_left }
theorem is_coprime.mul_left_iff : is_coprime (x * y) z ↔ is_coprime x z ∧ is_coprime y z :=
⟨λ H, ⟨H.of_mul_left_left, H.of_mul_left_right⟩, λ ⟨H1, H2⟩, H1.mul_left H2⟩
theorem is_coprime.mul_right_iff : is_coprime x (y * z) ↔ is_coprime x y ∧ is_coprime x z :=
by rw [is_coprime_comm, is_coprime.mul_left_iff, is_coprime_comm, @is_coprime_comm _ _ z]
theorem is_coprime.of_coprime_of_dvd_left (h : is_coprime y z) (hdvd : x ∣ y) : is_coprime x z :=
begin
obtain ⟨d, rfl⟩ := hdvd,
exact is_coprime.of_mul_left_left h
end
theorem is_coprime.of_coprime_of_dvd_right (h : is_coprime z y) (hdvd : x ∣ y) : is_coprime z x :=
(h.symm.of_coprime_of_dvd_left hdvd).symm
theorem is_coprime.is_unit_of_dvd (H : is_coprime x y) (d : x ∣ y) : is_unit x :=
let ⟨k, hk⟩ := d in is_coprime_self.1 $ is_coprime.of_mul_right_left $
show is_coprime x (x * k), from hk ▸ H
theorem is_coprime.is_unit_of_dvd' {a b x : R} (h : is_coprime a b) (ha : x ∣ a) (hb : x ∣ b) :
is_unit x :=
(h.of_coprime_of_dvd_left ha).is_unit_of_dvd hb
theorem is_coprime.map (H : is_coprime x y) {S : Type v} [comm_semiring S] (f : R →+* S) :
is_coprime (f x) (f y) :=
let ⟨a, b, h⟩ := H in ⟨f a, f b, by rw [← f.map_mul, ← f.map_mul, ← f.map_add, h, f.map_one]⟩
variables {x y z}
lemma is_coprime.of_add_mul_left_left (h : is_coprime (x + y * z) y) : is_coprime x y :=
let ⟨a, b, H⟩ := h in ⟨a, a * z + b, by simpa only [add_mul, mul_add,
add_assoc, add_comm, add_left_comm, mul_assoc, mul_comm, mul_left_comm] using H⟩
lemma is_coprime.of_add_mul_right_left (h : is_coprime (x + z * y) y) : is_coprime x y :=
by { rw mul_comm at h, exact h.of_add_mul_left_left }
lemma is_coprime.of_add_mul_left_right (h : is_coprime x (y + x * z)) : is_coprime x y :=
by { rw is_coprime_comm at h ⊢, exact h.of_add_mul_left_left }
lemma is_coprime.of_add_mul_right_right (h : is_coprime x (y + z * x)) : is_coprime x y :=
by { rw mul_comm at h, exact h.of_add_mul_left_right }
lemma is_coprime.of_mul_add_left_left (h : is_coprime (y * z + x) y) : is_coprime x y :=
by { rw add_comm at h, exact h.of_add_mul_left_left }
lemma is_coprime.of_mul_add_right_left (h : is_coprime (z * y + x) y) : is_coprime x y :=
by { rw add_comm at h, exact h.of_add_mul_right_left }
lemma is_coprime.of_mul_add_left_right (h : is_coprime x (x * z + y)) : is_coprime x y :=
by { rw add_comm at h, exact h.of_add_mul_left_right }
lemma is_coprime.of_mul_add_right_right (h : is_coprime x (z * x + y)) : is_coprime x y :=
by { rw add_comm at h, exact h.of_add_mul_right_right }
end comm_semiring
namespace is_coprime
section comm_ring
variables {R : Type u} [comm_ring R]
lemma add_mul_left_left {x y : R} (h : is_coprime x y) (z : R) : is_coprime (x + y * z) y :=
@of_add_mul_left_left R _ _ _ (-z) $
by simpa only [mul_neg_eq_neg_mul_symm, add_neg_cancel_right] using h
lemma add_mul_right_left {x y : R} (h : is_coprime x y) (z : R) : is_coprime (x + z * y) y :=
by { rw mul_comm, exact h.add_mul_left_left z }
lemma add_mul_left_right {x y : R} (h : is_coprime x y) (z : R) : is_coprime x (y + x * z) :=
by { rw is_coprime_comm, exact h.symm.add_mul_left_left z }
lemma add_mul_right_right {x y : R} (h : is_coprime x y) (z : R) : is_coprime x (y + z * x) :=
by { rw is_coprime_comm, exact h.symm.add_mul_right_left z }
lemma mul_add_left_left {x y : R} (h : is_coprime x y) (z : R) : is_coprime (y * z + x) y :=
by { rw add_comm, exact h.add_mul_left_left z }
lemma mul_add_right_left {x y : R} (h : is_coprime x y) (z : R) : is_coprime (z * y + x) y :=
by { rw add_comm, exact h.add_mul_right_left z }
lemma mul_add_left_right {x y : R} (h : is_coprime x y) (z : R) : is_coprime x (x * z + y) :=
by { rw add_comm, exact h.add_mul_left_right z }
lemma mul_add_right_right {x y : R} (h : is_coprime x y) (z : R) : is_coprime x (z * x + y) :=
by { rw add_comm, exact h.add_mul_right_right z }
lemma add_mul_left_left_iff {x y z : R} : is_coprime (x + y * z) y ↔ is_coprime x y :=
⟨of_add_mul_left_left, λ h, h.add_mul_left_left z⟩
lemma add_mul_right_left_iff {x y z : R} : is_coprime (x + z * y) y ↔ is_coprime x y :=
⟨of_add_mul_right_left, λ h, h.add_mul_right_left z⟩
lemma add_mul_left_right_iff {x y z : R} : is_coprime x (y + x * z) ↔ is_coprime x y :=
⟨of_add_mul_left_right, λ h, h.add_mul_left_right z⟩
lemma add_mul_right_right_iff {x y z : R} : is_coprime x (y + z * x) ↔ is_coprime x y :=
⟨of_add_mul_right_right, λ h, h.add_mul_right_right z⟩
lemma mul_add_left_left_iff {x y z : R} : is_coprime (y * z + x) y ↔ is_coprime x y :=
⟨of_mul_add_left_left, λ h, h.mul_add_left_left z⟩
lemma mul_add_right_left_iff {x y z : R} : is_coprime (z * y + x) y ↔ is_coprime x y :=
⟨of_mul_add_right_left, λ h, h.mul_add_right_left z⟩
lemma mul_add_left_right_iff {x y z : R} : is_coprime x (x * z + y) ↔ is_coprime x y :=
⟨of_mul_add_left_right, λ h, h.mul_add_left_right z⟩
lemma mul_add_right_right_iff {x y z : R} : is_coprime x (z * x + y) ↔ is_coprime x y :=
⟨of_mul_add_right_right, λ h, h.mul_add_right_right z⟩
lemma neg_left {x y : R} (h : is_coprime x y) : is_coprime (-x) y :=
begin
obtain ⟨a, b, h⟩ := h,
use [-a, b],
rwa neg_mul_neg,
end
lemma neg_left_iff (x y : R) : is_coprime (-x) y ↔ is_coprime x y :=
⟨λ h, neg_neg x ▸ h.neg_left, neg_left⟩
lemma neg_right {x y : R} (h : is_coprime x y) : is_coprime x (-y) :=
h.symm.neg_left.symm
lemma neg_right_iff (x y : R) : is_coprime x (-y) ↔ is_coprime x y :=
⟨λ h, neg_neg y ▸ h.neg_right, neg_right⟩
lemma neg_neg {x y : R} (h : is_coprime x y) : is_coprime (-x) (-y) :=
h.neg_left.neg_right
lemma neg_neg_iff (x y : R) : is_coprime (-x) (-y) ↔ is_coprime x y :=
(neg_left_iff _ _).trans (neg_right_iff _ _)
end comm_ring
end is_coprime
|
091d9b1203b350acd7eb77ca747bf17cd63237bb | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/homology/Module.lean | b2a096d7dfe9ecd8988fb9d7f5576942c0a8f3fc | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,996 | 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 algebra.homology.homotopy
import algebra.category.Module.abelian
import algebra.category.Module.subobject
import category_theory.limits.concrete_category
/-!
# Complexes of modules
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We provide some additional API to work with homological complexes in `Module R`.
-/
universes v u
open_locale classical
noncomputable theory
open category_theory category_theory.limits homological_complex
variables {R : Type v} [ring R]
variables {ι : Type*} {c : complex_shape ι} {C D : homological_complex (Module.{u} R) c}
namespace Module
/--
To prove that two maps out of a homology group are equal,
it suffices to check they are equal on the images of cycles.
-/
lemma homology_ext {L M N K : Module R} {f : L ⟶ M} {g : M ⟶ N} (w : f ≫ g = 0)
{h k : homology f g w ⟶ K}
(w : ∀ (x : linear_map.ker g),
h (cokernel.π (image_to_kernel _ _ w) (to_kernel_subobject x)) =
k (cokernel.π (image_to_kernel _ _ w) (to_kernel_subobject x))) : h = k :=
begin
refine cokernel_funext (λ n, _),
-- Gosh it would be nice if `equiv_rw` could directly use an isomorphism, or an enriched `≃`.
equiv_rw (kernel_subobject_iso g ≪≫ Module.kernel_iso_ker g).to_linear_equiv.to_equiv at n,
convert w n; simp [to_kernel_subobject],
end
/-- Bundle an element `C.X i` such that `C.d_from i x = 0` as a term of `C.cycles i`. -/
abbreviation to_cycles {C : homological_complex (Module.{u} R) c}
{i : ι} (x : linear_map.ker (C.d_from i)) : C.cycles i :=
to_kernel_subobject x
@[ext]
lemma cycles_ext {C : homological_complex (Module.{u} R) c} {i : ι}
{x y : C.cycles i} (w : (C.cycles i).arrow x = (C.cycles i).arrow y) : x = y :=
begin
apply_fun (C.cycles i).arrow using (Module.mono_iff_injective _).mp (cycles C i).arrow_mono,
exact w,
end
local attribute [instance] concrete_category.has_coe_to_sort
@[simp] lemma cycles_map_to_cycles (f : C ⟶ D) {i : ι} (x : linear_map.ker (C.d_from i)) :
(cycles_map f i) (to_cycles x) = to_cycles ⟨f.f i x.1, by simp [x.2]⟩ :=
by { ext, simp, }
/-- Build a term of `C.homology i` from an element `C.X i` such that `C.d_from i x = 0`. -/
abbreviation to_homology
{C : homological_complex (Module.{u} R) c} {i : ι} (x : linear_map.ker (C.d_from i)) :
C.homology i :=
homology.π (C.d_to i) (C.d_from i) _ (to_cycles x)
@[ext]
lemma homology_ext' {M : Module R} (i : ι) {h k : C.homology i ⟶ M}
(w : ∀ (x : linear_map.ker (C.d_from i)), h (to_homology x) = k (to_homology x)) :
h = k :=
homology_ext _ w
/-- We give an alternative proof of `homology_map_eq_of_homotopy`,
specialized to the setting of `V = Module R`,
to demonstrate the use of extensionality lemmas for homology in `Module R`. -/
example (f g : C ⟶ D) (h : homotopy f g) (i : ι) :
(homology_functor (Module.{u} R) c i).map f = (homology_functor (Module.{u} R) c i).map g :=
begin
-- To check that two morphisms out of a homology group agree, it suffices to check on cycles:
ext,
simp only [homology_functor_map, homology.π_map_apply],
-- To check that two elements are equal mod boundaries, it suffices to exhibit a boundary:
ext1,
swap, exact (to_prev i h.hom) x.1,
-- Moreover, to check that two cycles are equal, it suffices to check their underlying elements:
ext1,
simp only [map_add, image_to_kernel_arrow_apply, homological_complex.hom.sq_from_left,
Module.to_kernel_subobject_arrow, category_theory.limits.kernel_subobject_map_arrow_apply,
d_next_eq_d_from_from_next, function.comp_app, zero_add, Module.coe_comp,
linear_map.add_apply, map_zero, subtype.val_eq_coe,
category_theory.limits.image_subobject_arrow_comp_apply, linear_map.map_coe_ker,
prev_d_eq_to_prev_d_to, h.comm i, x.2],
abel
end
end Module
|
f339719e85596fe57751215d220b1111304d6d46 | 38193807b9085b93599c814229d2b0dacb64ba22 | /benchmarks/list-ident/ListIdent.lean | 3ff67e89d63d3cf4a53323541295ab61e07a7664 | [] | no_license | zgrannan/rest-old | d650363e403a9d5322fb44ee892b743aec558e1b | 6a6974641b25259cb8701af4302169db22b33b6b | refs/heads/master | 1,670,816,037,466 | 1,599,571,928,000 | 1,599,571,928,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 90 | lean | variable { α : Type}
theorem listIdent (xs : list α): xs = (xs ++ []) ++ [] := by simp
|
b85e6f8f64d5fb3d8d769b402460c92a2e2db99d | a047a4718edfa935d17231e9e6ecec8c7b701e05 | /src/data/dfinsupp.lean | 581f8767f47fc9e382e4f3f5a738079350cf8cb1 | [
"Apache-2.0"
] | permissive | utensil-contrib/mathlib | bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767 | b91909e77e219098a2f8cc031f89d595fe274bd2 | refs/heads/master | 1,668,048,976,965 | 1,592,442,701,000 | 1,592,442,701,000 | 273,197,855 | 0 | 0 | null | 1,592,472,812,000 | 1,592,472,811,000 | null | UTF-8 | Lean | false | false | 32,973 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Kenny Lau
-/
import algebra.pi_instances
/-!
# Dependent functions with finite support
For a non-dependent version see `data/finsupp.lean`.
-/
universes u u₁ u₂ v v₁ v₂ v₃ w x y l
open_locale big_operators
variables (ι : Type u) (β : ι → Type v)
namespace dfinsupp
variable [Π i, has_zero (β i)]
structure pre : Type (max u v) :=
(to_fun : Π i, β i)
(pre_support : multiset ι)
(zero : ∀ i, i ∈ pre_support ∨ to_fun i = 0)
instance inhabited_pre : inhabited (pre ι β) :=
⟨⟨λ i, 0, ∅, λ i, or.inr rfl⟩⟩
instance : setoid (pre ι β) :=
{ r := λ x y, ∀ i, x.to_fun i = y.to_fun i,
iseqv := ⟨λ f i, rfl, λ f g H i, (H i).symm,
λ f g h H1 H2 i, (H1 i).trans (H2 i)⟩ }
end dfinsupp
variable {ι}
/-- A dependent function `Π i, β i` with finite support. -/
@[reducible]
def dfinsupp [Π i, has_zero (β i)] : Type* :=
quotient (dfinsupp.setoid ι β)
variable {β}
notation `Π₀` binders `, ` r:(scoped f, dfinsupp f) := r
infix ` →ₚ `:25 := dfinsupp
namespace dfinsupp
section basic
variables [Π i, has_zero (β i)]
variables {β₁ : ι → Type v₁} {β₂ : ι → Type v₂}
variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)]
instance : has_coe_to_fun (Π₀ i, β i) :=
⟨λ _, Π i, β i, λ f, quotient.lift_on f pre.to_fun $ λ _ _, funext⟩
instance : has_zero (Π₀ i, β i) := ⟨⟦⟨λ i, 0, ∅, λ i, or.inr rfl⟩⟧⟩
instance : inhabited (Π₀ i, β i) := ⟨0⟩
@[simp] lemma zero_apply {i : ι} : (0 : Π₀ i, β i) i = 0 := rfl
@[ext]
lemma ext {f g : Π₀ i, β i} (H : ∀ i, f i = g i) : f = g :=
quotient.induction_on₂ f g (λ _ _ H, quotient.sound H) H
/-- The composition of `f : β₁ → β₂` and `g : Π₀ i, β₁ i` is
`map_range f hf g : Π₀ i, β₂ i`, well defined when `f 0 = 0`. -/
def map_range (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (g : Π₀ i, β₁ i) : Π₀ i, β₂ i :=
quotient.lift_on g (λ x, ⟦(⟨λ i, f i (x.1 i), x.2,
λ i, or.cases_on (x.3 i) or.inl $ λ H, or.inr $ by rw [H, hf]⟩ : pre ι β₂)⟧) $ λ x y H,
quotient.sound $ λ i, by simp only [H i]
@[simp] lemma map_range_apply
{f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} {i : ι} :
map_range f hf g i = f i (g i) :=
quotient.induction_on g $ λ x, rfl
/-- Let `f i` be a binary operation `β₁ i → β₂ i → β i` such that `f i 0 0 = 0`.
Then `zip_with f hf` is a binary operation `Π₀ i, β₁ i → Π₀ i, β₂ i → Π₀ i, β i`. -/
def zip_with (f : Π i, β₁ i → β₂ i → β i) (hf : ∀ i, f i 0 0 = 0)
(g₁ : Π₀ i, β₁ i) (g₂ : Π₀ i, β₂ i) : (Π₀ i, β i) :=
begin
refine quotient.lift_on₂ g₁ g₂ (λ x y, ⟦(⟨λ i, f i (x.1 i) (y.1 i), x.2 + y.2,
λ i, _⟩ : pre ι β)⟧) _,
{ cases x.3 i with h1 h1,
{ left, rw multiset.mem_add, left, exact h1 },
cases y.3 i with h2 h2,
{ left, rw multiset.mem_add, right, exact h2 },
right, rw [h1, h2, hf] },
exact λ x₁ x₂ y₁ y₂ H1 H2, quotient.sound $ λ i, by simp only [H1 i, H2 i]
end
@[simp] lemma zip_with_apply
{f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} {i : ι} :
zip_with f hf g₁ g₂ i = f i (g₁ i) (g₂ i) :=
quotient.induction_on₂ g₁ g₂ $ λ _ _, rfl
end basic
section algebra
instance [Π i, add_monoid (β i)] : has_add (Π₀ i, β i) :=
⟨zip_with (λ _, (+)) (λ _, add_zero 0)⟩
@[simp] lemma add_apply [Π i, add_monoid (β i)] {g₁ g₂ : Π₀ i, β i} {i : ι} :
(g₁ + g₂) i = g₁ i + g₂ i :=
zip_with_apply
instance [Π i, add_monoid (β i)] : add_monoid (Π₀ i, β i) :=
{ add_monoid .
zero := 0,
add := (+),
add_assoc := λ f g h, ext $ λ i, by simp only [add_apply, add_assoc],
zero_add := λ f, ext $ λ i, by simp only [add_apply, zero_apply, zero_add],
add_zero := λ f, ext $ λ i, by simp only [add_apply, zero_apply, add_zero] }
instance [Π i, add_monoid (β i)] {i : ι} : is_add_monoid_hom (λ g : Π₀ i : ι, β i, g i) :=
{ map_add := λ _ _, add_apply, map_zero := zero_apply }
instance [Π i, add_group (β i)] : has_neg (Π₀ i, β i) :=
⟨λ f, f.map_range (λ _, has_neg.neg) (λ _, neg_zero)⟩
instance [Π i, add_comm_monoid (β i)] : add_comm_monoid (Π₀ i, β i) :=
{ add_comm := λ f g, ext $ λ i, by simp only [add_apply, add_comm],
.. dfinsupp.add_monoid }
@[simp] lemma neg_apply [Π i, add_group (β i)] {g : Π₀ i, β i} {i : ι} : (- g) i = - g i :=
map_range_apply
instance [Π i, add_group (β i)] : add_group (Π₀ i, β i) :=
{ add_left_neg := λ f, ext $ λ i, by simp only [add_apply, neg_apply, zero_apply, add_left_neg],
.. dfinsupp.add_monoid,
.. (infer_instance : has_neg (Π₀ i, β i)) }
@[simp] lemma sub_apply [Π i, add_group (β i)] {g₁ g₂ : Π₀ i, β i} {i : ι} :
(g₁ - g₂) i = g₁ i - g₂ i :=
by rw [sub_eq_add_neg]; simp [sub_eq_add_neg]
instance [Π i, add_comm_group (β i)] : add_comm_group (Π₀ i, β i) :=
{ add_comm := λ f g, ext $ λ i, by simp only [add_apply, add_comm],
..dfinsupp.add_group }
/-- Dependent functions with finite support inherit a semiring action from an action on each
coordinate. -/
def to_has_scalar {γ : Type w} [semiring γ] [Π i, add_comm_group (β i)] [Π i, semimodule γ (β i)] :
has_scalar γ (Π₀ i, β i) :=
⟨λc v, v.map_range (λ _, (•) c) (λ _, smul_zero _)⟩
local attribute [instance] to_has_scalar
@[simp] lemma smul_apply {γ : Type w} [semiring γ] [Π i, add_comm_group (β i)]
[Π i, semimodule γ (β i)] {i : ι} {b : γ} {v : Π₀ i, β i} :
(b • v) i = b • (v i) :=
map_range_apply
/-- Dependent functions with finite support inherit a semimodule structure from such a structure on
each coordinate. -/
def to_semimodule {γ : Type w} [semiring γ] [Π i, add_comm_group (β i)] [Π i, semimodule γ (β i)] :
semimodule γ (Π₀ i, β i) :=
semimodule.of_core {
smul_add := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, smul_add],
add_smul := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, add_smul],
one_smul := λ x, ext $ λ i, by simp only [smul_apply, one_smul],
mul_smul := λ r s x, ext $ λ i, by simp only [smul_apply, smul_smul],
.. (infer_instance : has_scalar γ (Π₀ i, β i)) }
end algebra
section filter_and_subtype_domain
/-- `filter p f` is the function which is `f i` if `p i` is true and 0 otherwise. -/
def filter [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] (f : Π₀ i, β i) : Π₀ i, β i :=
quotient.lift_on f (λ x, ⟦(⟨λ i, if p i then x.1 i else 0, x.2,
λ i, or.cases_on (x.3 i) or.inl $ λ H, or.inr $ by rw [H, if_t_t]⟩ : pre ι β)⟧) $ λ x y H,
quotient.sound $ λ i, by simp only [H i]
@[simp] lemma filter_apply [Π i, has_zero (β i)]
{p : ι → Prop} [decidable_pred p] {i : ι} {f : Π₀ i, β i} :
f.filter p i = if p i then f i else 0 :=
quotient.induction_on f $ λ x, rfl
lemma filter_apply_pos [Π i, has_zero (β i)]
{p : ι → Prop} [decidable_pred p] {f : Π₀ i, β i} {i : ι} (h : p i) :
f.filter p i = f i :=
by simp only [filter_apply, if_pos h]
lemma filter_apply_neg [Π i, has_zero (β i)]
{p : ι → Prop} [decidable_pred p] {f : Π₀ i, β i} {i : ι} (h : ¬ p i) :
f.filter p i = 0 :=
by simp only [filter_apply, if_neg h]
lemma filter_pos_add_filter_neg [Π i, add_monoid (β i)] {f : Π₀ i, β i}
{p : ι → Prop} [decidable_pred p] :
f.filter p + f.filter (λi, ¬ p i) = f :=
ext $ λ i, by simp only [add_apply, filter_apply]; split_ifs; simp only [add_zero, zero_add]
/-- `subtype_domain p f` is the restriction of the finitely supported function
`f` to the subtype `p`. -/
def subtype_domain [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p]
(f : Π₀ i, β i) : Π₀ i : subtype p, β i.1 :=
begin
fapply quotient.lift_on f,
{ intro x,
refine ⟦⟨λ i, x.1 i.1,
(x.2.filter p).attach.map $ λ j, ⟨j.1, (multiset.mem_filter.1 j.2).2⟩, _⟩⟧,
refine λ i, or.cases_on (x.3 i.1) (λ H, _) or.inr,
left, rw multiset.mem_map, refine ⟨⟨i.1, multiset.mem_filter.2 ⟨H, i.2⟩⟩, _, subtype.eta _ _⟩,
apply multiset.mem_attach },
intros x y H,
exact quotient.sound (λ i, H i.1)
end
@[simp] lemma subtype_domain_zero [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] :
subtype_domain p (0 : Π₀ i, β i) = 0 :=
rfl
@[simp] lemma subtype_domain_apply [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p]
{i : subtype p} {v : Π₀ i, β i} :
(subtype_domain p v) i = v (i.val) :=
quotient.induction_on v $ λ x, rfl
@[simp] lemma subtype_domain_add [Π i, add_monoid (β i)] {p : ι → Prop} [decidable_pred p]
{v v' : Π₀ i, β i} :
(v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p :=
ext $ λ i, by simp only [add_apply, subtype_domain_apply]
instance subtype_domain.is_add_monoid_hom [Π i, add_monoid (β i)]
{p : ι → Prop} [decidable_pred p] :
is_add_monoid_hom (subtype_domain p : (Π₀ i : ι, β i) → Π₀ i : subtype p, β i) :=
{ map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero }
@[simp]
lemma subtype_domain_neg [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p] {v : Π₀ i, β i} :
(- v).subtype_domain p = - v.subtype_domain p :=
ext $ λ i, by simp only [neg_apply, subtype_domain_apply]
@[simp] lemma subtype_domain_sub [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p]
{v v' : Π₀ i, β i} :
(v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p :=
ext $ λ i, by simp only [sub_apply, subtype_domain_apply]
end filter_and_subtype_domain
variable [dec : decidable_eq ι]
include dec
section basic
variable [Π i, has_zero (β i)]
omit dec
lemma finite_supp (f : Π₀ i, β i) : set.finite {i | f i ≠ 0} :=
begin
classical,
exact quotient.induction_on f (λ x, set.finite_subset
(finset.finite_to_set x.2.to_finset) (λ i H,
multiset.mem_to_finset.2 ((x.3 i).resolve_right H)))
end
include dec
/-- Create an element of `Π₀ i, β i` from a finset `s` and a function `x`
defined on this `finset`. -/
def mk (s : finset ι) (x : Π i : (↑s : set ι), β i.1) : Π₀ i, β i :=
⟦⟨λ i, if H : i ∈ s then x ⟨i, H⟩ else 0, s.1,
λ i, if H : i ∈ s then or.inl H else or.inr $ dif_neg H⟩⟧
@[simp] lemma mk_apply {s : finset ι} {x : Π i : (↑s : set ι), β i.1} {i : ι} :
(mk s x : Π i, β i) i = if H : i ∈ s then x ⟨i, H⟩ else 0 :=
rfl
theorem mk_inj (s : finset ι) : function.injective (@mk ι β _ _ s) :=
begin
intros x y H,
ext i,
have h1 : (mk s x : Π i, β i) i = (mk s y : Π i, β i) i, {rw H},
cases i with i hi,
change i ∈ s at hi,
dsimp only [mk_apply, subtype.coe_mk] at h1,
simpa only [dif_pos hi] using h1
end
/-- The function `single i b : Π₀ i, β i` sends `i` to `b`
and all other points to `0`. -/
def single (i : ι) (b : β i) : Π₀ i, β i :=
mk {i} $ λ j, eq.rec_on (finset.mem_singleton.1 j.2).symm b
@[simp] lemma single_apply {i i' b} :
(single i b : Π₀ i, β i) i' = (if h : i = i' then eq.rec_on h b else 0) :=
begin
dsimp only [single],
by_cases h : i = i',
{ have h1 : i' ∈ ({i} : finset ι) := finset.mem_singleton.2 h.symm,
simp only [mk_apply, dif_pos h, dif_pos h1] },
{ have h1 : i' ∉ ({i} : finset ι) := finset.not_mem_singleton.2 (ne.symm h),
simp only [mk_apply, dif_neg h, dif_neg h1] }
end
@[simp] lemma single_zero {i} : (single i 0 : Π₀ i, β i) = 0 :=
quotient.sound $ λ j, if H : j ∈ ({i} : finset _)
then by dsimp only; rw [dif_pos H]; cases finset.mem_singleton.1 H; refl
else dif_neg H
@[simp] lemma single_eq_same {i b} : (single i b : Π₀ i, β i) i = b :=
by simp only [single_apply, dif_pos rfl]
lemma single_eq_of_ne {i i' b} (h : i ≠ i') : (single i b : Π₀ i, β i) i' = 0 :=
by simp only [single_apply, dif_neg h]
/-- Redefine `f i` to be `0`. -/
def erase (i : ι) (f : Π₀ i, β i) : Π₀ i, β i :=
quotient.lift_on f (λ x, ⟦(⟨λ j, if j = i then 0 else x.1 j, x.2,
λ j, or.cases_on (x.3 j) or.inl $ λ H, or.inr $ by simp only [H, if_t_t]⟩ : pre ι β)⟧) $ λ x y H,
quotient.sound $ λ j, if h : j = i then by simp only [if_pos h]
else by simp only [if_neg h, H j]
@[simp] lemma erase_apply {i j : ι} {f : Π₀ i, β i} :
(f.erase i) j = if j = i then 0 else f j :=
quotient.induction_on f $ λ x, rfl
@[simp] lemma erase_same {i : ι} {f : Π₀ i, β i} : (f.erase i) i = 0 :=
by simp
lemma erase_ne {i i' : ι} {f : Π₀ i, β i} (h : i' ≠ i) : (f.erase i) i' = f i' :=
by simp [h]
end basic
section add_monoid
variable [Π i, add_monoid (β i)]
@[simp] lemma single_add {i : ι} {b₁ b₂ : β i} : single i (b₁ + b₂) = single i b₁ + single i b₂ :=
ext $ assume i',
begin
by_cases h : i = i',
{ subst h, simp only [add_apply, single_eq_same] },
{ simp only [add_apply, single_eq_of_ne h, zero_add] }
end
lemma single_add_erase {i : ι} {f : Π₀ i, β i} : single i (f i) + f.erase i = f :=
ext $ λ i',
if h : i = i'
then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, add_zero]
else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), zero_add]
lemma erase_add_single {i : ι} {f : Π₀ i, β i} : f.erase i + single i (f i) = f :=
ext $ λ i',
if h : i = i'
then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, zero_add]
else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), add_zero]
protected theorem induction {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i)
(h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (single i b + f)) :
p f :=
begin
refine quotient.induction_on f (λ x, _),
cases x with f s H, revert f H,
apply multiset.induction_on s,
{ intros f H, convert h0, ext i, exact (H i).resolve_left id },
intros i s ih f H,
by_cases H1 : i ∈ s,
{ have H2 : ∀ j, j ∈ s ∨ f j = 0,
{ intro j, cases H j with H2 H2,
{ cases multiset.mem_cons.1 H2 with H3 H3,
{ left, rw H3, exact H1 },
{ left, exact H3 } },
right, exact H2 },
have H3 : (⟦{to_fun := f, pre_support := i :: s, zero := H}⟧ : Π₀ i, β i)
= ⟦{to_fun := f, pre_support := s, zero := H2}⟧,
{ exact quotient.sound (λ i, rfl) },
rw H3, apply ih },
have H2 : p (erase i ⟦{to_fun := f, pre_support := i :: s, zero := H}⟧),
{ dsimp only [erase, quotient.lift_on_beta],
have H2 : ∀ j, j ∈ s ∨ ite (j = i) 0 (f j) = 0,
{ intro j, cases H j with H2 H2,
{ cases multiset.mem_cons.1 H2 with H3 H3,
{ right, exact if_pos H3 },
{ left, exact H3 } },
right, split_ifs; [refl, exact H2] },
have H3 : (⟦{to_fun := λ (j : ι), ite (j = i) 0 (f j),
pre_support := i :: s, zero := _}⟧ : Π₀ i, β i)
= ⟦{to_fun := λ (j : ι), ite (j = i) 0 (f j), pre_support := s, zero := H2}⟧ :=
quotient.sound (λ i, rfl),
rw H3, apply ih },
have H3 : single i _ + _ = (⟦{to_fun := f, pre_support := i :: s, zero := H}⟧ : Π₀ i, β i) :=
single_add_erase,
rw ← H3,
change p (single i (f i) + _),
cases classical.em (f i = 0) with h h,
{ rw [h, single_zero, zero_add], exact H2 },
refine ha _ _ _ _ h H2,
rw erase_same
end
lemma induction₂ {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i)
(h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (f + single i b)) :
p f :=
dfinsupp.induction f h0 $ λ i b f h1 h2 h3,
have h4 : f + single i b = single i b + f,
{ ext j, by_cases H : i = j,
{ subst H, simp [h1] },
{ simp [H] } },
eq.rec_on h4 $ ha i b f h1 h2 h3
end add_monoid
@[simp] lemma mk_add [Π i, add_monoid (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i.1} :
mk s (x + y) = mk s x + mk s y :=
ext $ λ i, by simp only [add_apply, mk_apply]; split_ifs; [refl, rw zero_add]
@[simp] lemma mk_zero [Π i, has_zero (β i)] {s : finset ι} :
mk s (0 : Π i : (↑s : set ι), β i.1) = 0 :=
ext $ λ i, by simp only [mk_apply]; split_ifs; refl
@[simp] lemma mk_neg [Π i, add_group (β i)] {s : finset ι} {x : Π i : (↑s : set ι), β i.1} :
mk s (-x) = -mk s x :=
ext $ λ i, by simp only [neg_apply, mk_apply]; split_ifs; [refl, rw neg_zero]
@[simp] lemma mk_sub [Π i, add_group (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i.1} :
mk s (x - y) = mk s x - mk s y :=
ext $ λ i, by simp only [sub_apply, mk_apply]; split_ifs; [refl, rw sub_zero]
instance [Π i, add_group (β i)] {s : finset ι} : is_add_group_hom (@mk ι β _ _ s) :=
{ map_add := λ _ _, mk_add }
section
local attribute [instance] to_semimodule
variables (γ : Type w) [semiring γ] [Π i, add_comm_group (β i)] [Π i, semimodule γ (β i)]
include γ
@[simp] lemma mk_smul {s : finset ι} {c : γ} (x : Π i : (↑s : set ι), β i.1) :
mk s (c • x) = c • mk s x :=
ext $ λ i, by simp only [smul_apply, mk_apply]; split_ifs; [refl, rw smul_zero]
@[simp] lemma single_smul {i : ι} {c : γ} {x : β i} :
single i (c • x) = c • single i x :=
ext $ λ i, by simp only [smul_apply, single_apply]; split_ifs; [cases h, rw smul_zero]; refl
variable β
/-- `dfinsupp.mk` as a `linear_map`. -/
def lmk (s : finset ι) : (Π i : (↑s : set ι), β i.1) →ₗ[γ] Π₀ i, β i :=
⟨mk s, λ _ _, mk_add, λ c x, by rw [mk_smul γ x]⟩
/-- `dfinsupp.single` as a `linear_map` -/
def lsingle (i) : β i →ₗ[γ] Π₀ i, β i :=
⟨single i, λ _ _, single_add, λ _ _, single_smul _⟩
variable {β}
@[simp] lemma lmk_apply {s : finset ι} {x} : lmk β γ s x = mk s x := rfl
@[simp] lemma lsingle_apply {i : ι} {x : β i} : lsingle β γ i x = single i x := rfl
end
section support_basic
variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
/-- Set `{i | f x ≠ 0}` as a `finset`. -/
def support (f : Π₀ i, β i) : finset ι :=
quotient.lift_on f (λ x, x.2.to_finset.filter $ λ i, x.1 i ≠ 0) $
begin
intros x y Hxy,
ext i, split,
{ intro H,
rcases finset.mem_filter.1 H with ⟨h1, h2⟩,
rw Hxy i at h2,
exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (y.3 i).resolve_right h2, h2⟩ },
{ intro H,
rcases finset.mem_filter.1 H with ⟨h1, h2⟩,
rw ← Hxy i at h2,
exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (x.3 i).resolve_right h2, h2⟩ },
end
@[simp] theorem support_mk_subset {s : finset ι} {x : Π i : (↑s : set ι), β i.1} :
(mk s x).support ⊆ s :=
λ i H, multiset.mem_to_finset.1 (finset.mem_filter.1 H).1
@[simp] theorem mem_support_to_fun (f : Π₀ i, β i) (i) : i ∈ f.support ↔ f i ≠ 0 :=
begin
refine quotient.induction_on f (λ x, _),
dsimp only [support, quotient.lift_on_beta],
rw [finset.mem_filter, multiset.mem_to_finset],
exact and_iff_right_of_imp (x.3 i).resolve_right
end
theorem eq_mk_support (f : Π₀ i, β i) : f = mk f.support (λ i, f i) :=
begin
change f = mk f.support (λ i, f i.1),
ext i,
by_cases h : f i ≠ 0; [skip, rw [classical.not_not] at h];
simp [h]
end
@[simp] lemma support_zero : (0 : Π₀ i, β i).support = ∅ := rfl
lemma mem_support_iff (f : Π₀ i, β i) : ∀i:ι, i ∈ f.support ↔ f i ≠ 0 :=
f.mem_support_to_fun
@[simp] lemma support_eq_empty {f : Π₀ i, β i} : f.support = ∅ ↔ f = 0 :=
⟨λ H, ext $ by simpa [finset.ext_iff] using H, by simp {contextual:=tt}⟩
instance decidable_zero : decidable_pred (eq (0 : Π₀ i, β i)) :=
λ f, decidable_of_iff _ $ support_eq_empty.trans eq_comm
lemma support_subset_iff {s : set ι} {f : Π₀ i, β i} :
↑f.support ⊆ s ↔ (∀i∉s, f i = 0) :=
by simp [set.subset_def];
exact forall_congr (assume i, @not_imp_comm _ _ (classical.dec _) (classical.dec _))
lemma support_single_ne_zero {i : ι} {b : β i} (hb : b ≠ 0) : (single i b).support = {i} :=
begin
ext j, by_cases h : i = j,
{ subst h, simp [hb] },
simp [ne.symm h, h]
end
lemma support_single_subset {i : ι} {b : β i} : (single i b).support ⊆ {i} :=
support_mk_subset
section map_range_and_zip_with
variables {β₁ : ι → Type v₁} {β₂ : ι → Type v₂}
variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)]
lemma map_range_def [Π i (x : β₁ i), decidable (x ≠ 0)]
{f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} :
map_range f hf g = mk g.support (λ i, f i.1 (g i.1)) :=
begin
ext i,
by_cases h : g i ≠ 0; simp at h; simp [h, hf]
end
@[simp] lemma map_range_single {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {i : ι} {b : β₁ i} :
map_range f hf (single i b) = single i (f i b) :=
dfinsupp.ext $ λ i', by by_cases i = i'; [{subst i', simp}, simp [h, hf]]
variables [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)]
lemma support_map_range {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} :
(map_range f hf g).support ⊆ g.support :=
by simp [map_range_def]
lemma zip_with_def {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0}
{g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} :
zip_with f hf g₁ g₂ = mk (g₁.support ∪ g₂.support) (λ i, f i.1 (g₁ i.1) (g₂ i.1)) :=
begin
ext i,
by_cases h1 : g₁ i ≠ 0; by_cases h2 : g₂ i ≠ 0;
simp only [classical.not_not, ne.def] at h1 h2; simp [h1, h2, hf]
end
lemma support_zip_with {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0}
{g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} :
(zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support :=
by simp [zip_with_def]
end map_range_and_zip_with
lemma erase_def (i : ι) (f : Π₀ i, β i) :
f.erase i = mk (f.support.erase i) (λ j, f j.1) :=
by { ext j, by_cases h1 : j = i; by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] }
@[simp] lemma support_erase (i : ι) (f : Π₀ i, β i) :
(f.erase i).support = f.support.erase i :=
by { ext j, by_cases h1 : j = i; by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] }
section filter_and_subtype_domain
variables {p : ι → Prop} [decidable_pred p]
lemma filter_def (f : Π₀ i, β i) :
f.filter p = mk (f.support.filter p) (λ i, f i.1) :=
by ext i; by_cases h1 : p i; by_cases h2 : f i ≠ 0;
simp at h2; simp [h1, h2]
@[simp] lemma support_filter (f : Π₀ i, β i) :
(f.filter p).support = f.support.filter p :=
by ext i; by_cases h : p i; simp [h]
lemma subtype_domain_def (f : Π₀ i, β i) :
f.subtype_domain p = mk (f.support.subtype p) (λ i, f i.1) :=
by ext i; cases i with i hi;
by_cases h1 : p i; by_cases h2 : f i ≠ 0;
try {simp at h2}; dsimp; simp [h1, h2]
@[simp] lemma support_subtype_domain {f : Π₀ i, β i} :
(subtype_domain p f).support = f.support.subtype p :=
by ext i; cases i with i hi;
by_cases h1 : p i; by_cases h2 : f i ≠ 0;
try {simp at h2}; dsimp; simp [h1, h2]
end filter_and_subtype_domain
end support_basic
lemma support_add [Π i, add_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] {g₁ g₂ : Π₀ i, β i} :
(g₁ + g₂).support ⊆ g₁.support ∪ g₂.support :=
support_zip_with
@[simp] lemma support_neg [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)]
{f : Π₀ i, β i} :
support (-f) = support f :=
by ext i; simp
local attribute [instance] dfinsupp.to_semimodule
lemma support_smul {γ : Type w} [ring γ] [Π i, add_comm_group (β i)] [Π i, module γ (β i)]
[Π (i : ι) (x : β i), decidable (x ≠ 0)]
{b : γ} {v : Π₀ i, β i} : (b • v).support ⊆ v.support :=
support_map_range
instance [Π i, has_zero (β i)] [Π i, decidable_eq (β i)] : decidable_eq (Π₀ i, β i) :=
assume f g, decidable_of_iff (f.support = g.support ∧ (∀i∈f.support, f i = g i))
⟨assume ⟨h₁, h₂⟩, ext $ assume i,
if h : i ∈ f.support then h₂ i h else
have hf : f i = 0, by rwa [f.mem_support_iff, not_not] at h,
have hg : g i = 0, by rwa [h₁, g.mem_support_iff, not_not] at h,
by rw [hf, hg],
by intro h; subst h; simp⟩
section prod_and_sum
variables {γ : Type w}
-- [to_additive sum] for dfinsupp.prod doesn't work, the equation lemmas are not generated
/-- `sum f g` is the sum of `g i (f i)` over the support of `f`. -/
def sum [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_monoid γ]
(f : Π₀ i, β i) (g : Π i, β i → γ) : γ :=
∑ i in f.support, g i (f i)
/-- `prod f g` is the product of `g i (f i)` over the support of `f`. -/
@[to_additive]
def prod [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ]
(f : Π₀ i, β i) (g : Π i, β i → γ) : γ :=
∏ i in f.support, g i (f i)
@[to_additive]
lemma prod_map_range_index {β₁ : ι → Type v₁} {β₂ : ι → Type v₂}
[Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)]
[Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)] [comm_monoid γ]
{f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} {h : Π i, β₂ i → γ}
(h0 : ∀i, h i 0 = 1) :
(map_range f hf g).prod h = g.prod (λi b, h i (f i b)) :=
begin
rw [map_range_def],
refine (finset.prod_subset support_mk_subset _).trans _,
{ intros i h1 h2,
dsimp, simp [h1] at h2, dsimp at h2,
simp [h1, h2, h0] },
{ refine finset.prod_congr rfl _,
intros i h1,
simp [h1] }
end
@[to_additive]
lemma prod_zero_index [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {h : Π i, β i → γ} : (0 : Π₀ i, β i).prod h = 1 :=
rfl
@[to_additive]
lemma prod_single_index [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ]
{i : ι} {b : β i} {h : Π i, β i → γ} (h_zero : h i 0 = 1) :
(single i b).prod h = h i b :=
begin
by_cases h : b ≠ 0,
{ simp [dfinsupp.prod, support_single_ne_zero h] },
{ rw [classical.not_not] at h, simp [h, prod_zero_index, h_zero], refl }
end
@[to_additive]
lemma prod_neg_index [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ]
{g : Π₀ i, β i} {h : Π i, β i → γ} (h0 : ∀i, h i 0 = 1) :
(-g).prod h = g.prod (λi b, h i (- b)) :=
prod_map_range_index h0
omit dec
@[simp] lemma sum_apply {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁}
[Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)]
[Π i, add_comm_monoid (β i)]
{f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} {i₂ : ι} :
(f.sum g) i₂ = f.sum (λi₁ b, g i₁ b i₂) :=
(f.support.sum_hom (λf : Π₀ i, β i, f i₂)).symm
include dec
lemma support_sum {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁}
[Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)]
[Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
{f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} :
(f.sum g).support ⊆ f.support.bind (λi, (g i (f i)).support) :=
have ∀i₁ : ι, f.sum (λ (i : ι₁) (b : β₁ i), (g i b) i₁) ≠ 0 →
(∃ (i : ι₁), f i ≠ 0 ∧ ¬ (g i (f i)) i₁ = 0),
from assume i₁ h,
let ⟨i, hi, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in
⟨i, (f.mem_support_iff i).mp hi, ne⟩,
by simpa [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply] using this
@[simp] lemma sum_zero [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[add_comm_monoid γ] {f : Π₀ i, β i} :
f.sum (λi b, (0 : γ)) = 0 :=
finset.sum_const_zero
@[simp] lemma sum_add [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[add_comm_monoid γ] {f : Π₀ i, β i} {h₁ h₂ : Π i, β i → γ} :
f.sum (λi b, h₁ i b + h₂ i b) = f.sum h₁ + f.sum h₂ :=
finset.sum_add_distrib
@[simp] lemma sum_neg [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[add_comm_group γ] {f : Π₀ i, β i} {h : Π i, β i → γ} :
f.sum (λi b, - h i b) = - f.sum h :=
f.support.sum_hom (@has_neg.neg γ _)
@[to_additive]
lemma prod_add_index [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {f g : Π₀ i, β i}
{h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) :
(f + g).prod h = f.prod h * g.prod h :=
have f_eq : ∏ i in f.support ∪ g.support, h i (f i) = f.prod h,
from (finset.prod_subset (finset.subset_union_left _ _) $
by simp [mem_support_iff, h_zero] {contextual := tt}).symm,
have g_eq : ∏ i in f.support ∪ g.support, h i (g i) = g.prod h,
from (finset.prod_subset (finset.subset_union_right _ _) $
by simp [mem_support_iff, h_zero] {contextual := tt}).symm,
calc ∏ i in (f + g).support, h i ((f + g) i) =
∏ i in f.support ∪ g.support, h i ((f + g) i) :
finset.prod_subset support_add $
by simp [mem_support_iff, h_zero] {contextual := tt}
... = (∏ i in f.support ∪ g.support, h i (f i)) *
(∏ i in f.support ∪ g.support, h i (g i)) :
by simp [h_add, finset.prod_mul_distrib]
... = _ : by rw [f_eq, g_eq]
lemma sum_sub_index [Π i, add_comm_group (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[add_comm_group γ] {f g : Π₀ i, β i}
{h : Π i, β i → γ} (h_sub : ∀i b₁ b₂, h i (b₁ - b₂) = h i b₁ - h i b₂) :
(f - g).sum h = f.sum h - g.sum h :=
have h_zero : ∀i, h i 0 = 0,
from assume i,
have h i (0 - 0) = h i 0 - h i 0, from h_sub i 0 0,
by simpa using this,
have h_neg : ∀i b, h i (- b) = - h i b,
from assume i b,
have h i (0 - b) = h i 0 - h i b, from h_sub i 0 b,
by simpa [h_zero] using this,
have h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ + h i b₂,
from assume i b₁ b₂,
have h i (b₁ - (- b₂)) = h i b₁ - h i (- b₂), from h_sub i b₁ (-b₂),
by simpa [h_neg, sub_eq_add_neg] using this,
by simp [sub_eq_add_neg];
simp [@sum_add_index ι β _ γ _ _ _ f (-g) h h_zero h_add];
simp [@sum_neg_index ι β _ γ _ _ _ g h h_zero, h_neg];
simp [@sum_neg ι β _ γ _ _ _ g h]
@[to_additive]
lemma prod_finset_sum_index {γ : Type w} {α : Type x}
[Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ]
{s : finset α} {g : α → Π₀ i, β i}
{h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) :
∏ i in s, (g i).prod h = (∑ i in s, g i).prod h :=
begin
classical,
exact finset.induction_on s
(by simp [prod_zero_index])
(by simp [prod_add_index, h_zero, h_add] {contextual := tt})
end
@[to_additive]
lemma prod_sum_index {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁}
[Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)]
[Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ]
{f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i}
{h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) :
(f.sum g).prod h = f.prod (λi b, (g i b).prod h) :=
(prod_finset_sum_index h_zero h_add).symm
@[simp] lemma sum_single [Π i, add_comm_monoid (β i)]
[Π i (x : β i), decidable (x ≠ 0)] {f : Π₀ i, β i} :
f.sum single = f :=
begin
apply dfinsupp.induction f, {rw [sum_zero_index]},
intros i b f H hb ih,
rw [sum_add_index, ih, sum_single_index],
all_goals { intros, simp }
end
@[to_additive]
lemma prod_subtype_domain_index [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)]
[comm_monoid γ] {v : Π₀ i, β i} {p : ι → Prop} [decidable_pred p]
{h : Π i, β i → γ} (hp : ∀x∈v.support, p x) :
(v.subtype_domain p).prod (λi b, h i.1 b) = v.prod h :=
finset.prod_bij (λp _, p.val)
(by simp)
(by simp)
(assume ⟨a₀, ha₀⟩ ⟨a₁, ha₁⟩, by simp)
(λ i hi, ⟨⟨i, hp i hi⟩, by simpa using hi, rfl⟩)
omit dec
lemma subtype_domain_sum [Π i, add_comm_monoid (β i)]
{s : finset γ} {h : γ → Π₀ i, β i} {p : ι → Prop} [decidable_pred p] :
(∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p :=
eq.symm (s.sum_hom _)
lemma subtype_domain_finsupp_sum {δ : γ → Type x} [decidable_eq γ]
[Π c, has_zero (δ c)] [Π c (x : δ c), decidable (x ≠ 0)]
[Π i, add_comm_monoid (β i)]
{p : ι → Prop} [decidable_pred p]
{s : Π₀ c, δ c} {h : Π c, δ c → Π₀ i, β i} :
(s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) :=
subtype_domain_sum
end prod_and_sum
end dfinsupp
|
99ff62b692d1a7cf397619944090e686aa1c1e31 | 618003631150032a5676f229d13a079ac875ff77 | /src/tactic/omega/main.lean | 353ae30833ca159b11efe5a3f7a8bf359630fcab | [
"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,666 | lean | /- Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Seul Baek
A tactic for discharging linear integer & natural
number arithmetic goals using the Omega test. -/
import tactic.omega.int.main
import tactic.omega.nat.main
namespace omega
open tactic
meta def select_domain (t s : tactic (option bool)) : tactic (option bool) :=
do a ← t, b ← s,
match a, b with
| a, none := return a
| none, b := return b
| (some tt), (some tt) := return (some tt)
| (some ff), (some ff) := return (some ff)
| _, _ := failed
end
meta def type_domain (x : expr) : tactic (option bool) :=
if x = `(int)
then return (some tt)
else if x = `(nat)
then return (some ff)
else failed
/-- Detects domain of a formula from its expr.
* Returns none, if domain can be either ℤ or ℕ
* Returns some tt, if domain is exclusively ℤ
* Returns some ff, if domain is exclusively ℕ
* Fails, if domain is neither ℤ nor ℕ -/
meta def form_domain : expr → tactic (option bool)
| `(¬ %%px) := form_domain px
| `(%%px ∨ %%qx) := select_domain (form_domain px) (form_domain qx)
| `(%%px ∧ %%qx) := select_domain (form_domain px) (form_domain qx)
| `(%%px ↔ %%qx) := select_domain (form_domain px) (form_domain qx)
| `(%%(expr.pi _ _ px qx)) :=
monad.cond
(if expr.has_var px then return tt else is_prop px)
(select_domain (form_domain px) (form_domain qx))
(select_domain (type_domain px) (form_domain qx))
| `(@has_lt.lt %%dx %%h _ _) := type_domain dx
| `(@has_le.le %%dx %%h _ _) := type_domain dx
| `(@eq %%dx _ _) := type_domain dx
| `(@ge %%dx %%h _ _) := type_domain dx
| `(@gt %%dx %%h _ _) := type_domain dx
| `(@ne %%dx _ _) := type_domain dx
| `(true) := return none
| `(false) := return none
| x := failed
meta def goal_domain_aux (x : expr) : tactic bool :=
(omega.int.wff x >> return tt) <|> (omega.nat.wff x >> return ff)
/-- Use the current goal to determine.
Return tt if the domain is ℤ, and return ff if it is ℕ -/
meta def goal_domain : tactic bool :=
do gx ← target,
hxs ← local_context >>= monad.mapm infer_type,
app_first goal_domain_aux (gx::hxs)
/-- Return tt if the domain is ℤ, and return ff if it is ℕ -/
meta def determine_domain (opt : list name) : tactic bool :=
if `int ∈ opt
then return tt
else if `nat ∈ opt
then return ff
else goal_domain
end omega
open lean.parser interactive omega
/-- Attempts to discharge goals in the quantifier-free fragment of
linear integer and natural number arithmetic using the Omega test.
Guesses the correct domain by looking at the goal and hypotheses,
and then reverts all relevant hypotheses and variables.
Use `omega manual` to disable automatic reverts, and `omega int` or
`omega nat` to specify the domain.
-/
meta def tactic.interactive.omega (opt : parse (many ident)) : tactic unit :=
do is_int ← determine_domain opt,
let is_manual : bool := if `manual ∈ opt then tt else ff,
if is_int
then omega_int is_manual
else omega_nat is_manual
add_hint_tactic "omega"
declare_trace omega
/--
`omega` attempts to discharge goals in the quantifier-free fragment of linear integer and natural
number arithmetic using the Omega test. In other words, the core procedure of `omega` works with
goals of the form
```lean
∀ x₁, ... ∀ xₖ, P
```
where `x₁, ... xₖ` are integer (resp. natural number) variables, and `P` is a quantifier-free
formula of linear integer (resp. natural number) arithmetic. For instance:
```lean
example : ∀ (x y : int), (x ≤ 5 ∧ y ≤ 3) → x + y ≤ 8 := by omega
```
By default, `omega` tries to guess the correct domain by looking at the goal and hypotheses, and
then reverts all relevant hypotheses and variables (e.g., all variables of type `nat` and `Prop`s
in linear natural number arithmetic, if the domain was determined to be `nat`) to universally close
the goal before calling the main procedure. Therefore, `omega` will often work even if the goal
is not in the above form:
```lean
example (x y : nat) (h : 2 * x + 1 = 2 * y) : false := by omega
```
But this behaviour is not always optimal, since it may revert irrelevant hypotheses or incorrectly
guess the domain. Use `omega manual` to disable automatic reverts, and `omega int` or `omega nat`
to specify the domain.
```lean
example (x y z w : int) (h1 : 3 * y ≥ x) (h2 : z > 19 * w) : 3 * x ≤ 9 * y :=
by {revert h1 x y, omega manual}
example (i : int) (n : nat) (h1 : i = 0) (h2 : n < n) : false := by omega nat
example (n : nat) (h1 : n < 34) (i : int) (h2 : i * 9 = -72) : i = -8 :=
by {revert h2 i, omega manual int}
```
`omega` handles `nat` subtraction by repeatedly rewriting goals of the form `P[t-s]` into
`P[x] ∧ (t = s + x ∨ (t ≤ s ∧ x = 0))`, where `x` is fresh. This means that each (distinct)
occurrence of subtraction will cause the goal size to double during DNF transformation.
`omega` implements the real shadow step of the Omega test, but not the dark and gray shadows.
Therefore, it should (in principle) succeed whenever the negation of the goal has no real solution,
but it may fail if a real solution exists, even if there is no integer/natural number solution.
You can enable `set_option trace.omega true` to see how `omega` interprets your goal.
-/
add_tactic_doc
{ name := "omega",
category := doc_category.tactic,
decl_names := [`tactic.interactive.omega],
tags := ["finishing", "arithmetic", "decision procedure"] }
|
79f710541c90f979139f44c4a205ff736a138817 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/linear_algebra/std_basis.lean | c3c5560c3960a1c2b7464761256b80dc3ffd1b2e | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 10,778 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import data.matrix.basis
import linear_algebra.basis
import linear_algebra.pi
/-!
# The standard basis
This file defines the standard basis `pi.basis (s : ∀ j, basis (ι j) R (M j))`,
which is the `Σ j, ι j`-indexed basis of Π j, M j`. The basis vectors are given by
`pi.basis s ⟨j, i⟩ j' = linear_map.std_basis R M j' (s j) i = if j = j' then s i else 0`.
The standard basis on `R^η`, i.e. `η → R` is called `pi.basis_fun`.
To give a concrete example, `linear_map.std_basis R (λ (i : fin 3), R) i 1`
gives the `i`th unit basis vector in `R³`, and `pi.basis_fun R (fin 3)` proves
this is a basis over `fin 3 → R`.
## Main definitions
- `linear_map.std_basis R M`: if `x` is a basis vector of `M i`, then
`linear_map.std_basis R M i x` is the `i`th standard basis vector of `Π i, M i`.
- `pi.basis s`: given a basis `s i` for each `M i`, the standard basis on `Π i, M i`
- `pi.basis_fun R η`: the standard basis on `R^η`, i.e. `η → R`, given by
`pi.basis_fun R η i j = if i = j then 1 else 0`.
- `matrix.std_basis R n m`: the standard basis on `matrix n m R`, given by
`matrix.std_basis R n m (i, j) i' j' = if (i, j) = (i', j') then 1 else 0`.
-/
open function submodule
open_locale big_operators
open_locale big_operators
namespace linear_map
variables (R : Type*) {ι : Type*} [semiring R] (φ : ι → Type*)
[Π i, add_comm_monoid (φ i)] [Π i, module R (φ i)] [decidable_eq ι]
/-- The standard basis of the product of `φ`. -/
def std_basis : Π (i : ι), φ i →ₗ[R] (Πi, φ i) := single
lemma std_basis_apply (i : ι) (b : φ i) : std_basis R φ i b = update 0 i b :=
rfl
lemma coe_std_basis (i : ι) : ⇑(std_basis R φ i) = pi.single i :=
funext $ std_basis_apply R φ i
@[simp] lemma std_basis_same (i : ι) (b : φ i) : std_basis R φ i b i = b :=
by rw [std_basis_apply, update_same]
lemma std_basis_ne (i j : ι) (h : j ≠ i) (b : φ i) : std_basis R φ i b j = 0 :=
by rw [std_basis_apply, update_noteq h]; refl
lemma std_basis_eq_pi_diag (i : ι) : std_basis R φ i = pi (diag i) :=
begin
ext x j,
convert (update_apply 0 x i j _).symm,
refl,
end
lemma ker_std_basis (i : ι) : ker (std_basis R φ i) = ⊥ :=
ker_eq_bot_of_injective $ assume f g hfg,
have std_basis R φ i f i = std_basis R φ i g i := hfg ▸ rfl,
by simpa only [std_basis_same]
lemma proj_comp_std_basis (i j : ι) : (proj i).comp (std_basis R φ j) = diag j i :=
by rw [std_basis_eq_pi_diag, proj_pi]
lemma proj_std_basis_same (i : ι) : (proj i).comp (std_basis R φ i) = id :=
by ext b; simp
lemma proj_std_basis_ne (i j : ι) (h : i ≠ j) : (proj i).comp (std_basis R φ j) = 0 :=
by ext b; simp [std_basis_ne R φ _ _ h]
lemma supr_range_std_basis_le_infi_ker_proj (I J : set ι) (h : disjoint I J) :
(⨆i∈I, range (std_basis R φ i)) ≤ (⨅i∈J, ker (proj i)) :=
begin
refine (supr_le $ λ i, supr_le $ λ hi, range_le_iff_comap.2 _),
simp only [(ker_comp _ _).symm, eq_top_iff, set_like.le_def, mem_ker, comap_infi, mem_infi],
rintro b - j hj,
rw [proj_std_basis_ne R φ j i, zero_apply],
rintro rfl,
exact h ⟨hi, hj⟩
end
lemma infi_ker_proj_le_supr_range_std_basis {I : finset ι} {J : set ι} (hu : set.univ ⊆ ↑I ∪ J) :
(⨅ i∈J, ker (proj i)) ≤ (⨆i∈I, range (std_basis R φ i)) :=
set_like.le_def.2
begin
assume b hb,
simp only [mem_infi, mem_ker, proj_apply] at hb,
rw ← show ∑ i in I, std_basis R φ i (b i) = b,
{ ext i,
rw [finset.sum_apply, ← std_basis_same R φ i (b i)],
refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _,
assume hiI,
rw [std_basis_same],
exact hb _ ((hu trivial).resolve_left hiI) },
exact sum_mem _ (assume i hiI, mem_supr_of_mem i $ mem_supr_of_mem hiI $
(std_basis R φ i).mem_range_self (b i))
end
lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ι}
(hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) (hI : set.finite I) :
(⨆i∈I, range (std_basis R φ i)) = (⨅i∈J, ker (proj i)) :=
begin
refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _,
have : set.univ ⊆ ↑hI.to_finset ∪ J, { rwa [hI.coe_to_finset] },
refine le_trans (infi_ker_proj_le_supr_range_std_basis R φ this) (supr_le_supr $ assume i, _),
rw [set.finite.mem_to_finset],
exact le_rfl
end
lemma supr_range_std_basis [fintype ι] : (⨆i:ι, range (std_basis R φ i)) = ⊤ :=
have (set.univ : set ι) ⊆ ↑(finset.univ : finset ι) ∪ ∅ := by rw [finset.coe_univ, set.union_empty],
begin
apply top_unique,
convert (infi_ker_proj_le_supr_range_std_basis R φ this),
exact infi_emptyset.symm,
exact (funext $ λi, (@supr_pos _ _ _ (λh, range (std_basis R φ i)) $ finset.mem_univ i).symm)
end
lemma disjoint_std_basis_std_basis (I J : set ι) (h : disjoint I J) :
disjoint (⨆i∈I, range (std_basis R φ i)) (⨆i∈J, range (std_basis R φ i)) :=
begin
refine disjoint.mono
(supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ disjoint_compl_right)
(supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ disjoint_compl_right) _,
simp only [disjoint, set_like.le_def, mem_infi, mem_inf, mem_ker, mem_bot, proj_apply,
funext_iff],
rintros b ⟨hI, hJ⟩ i,
classical,
by_cases hiI : i ∈ I,
{ by_cases hiJ : i ∈ J,
{ exact (h ⟨hiI, hiJ⟩).elim },
{ exact hJ i hiJ } },
{ exact hI i hiI }
end
lemma std_basis_eq_single {a : R} :
(λ (i : ι), (std_basis R (λ _ : ι, R) i) a) = λ (i : ι), (finsupp.single i a) :=
begin
ext i j,
rw [std_basis_apply, finsupp.single_apply],
split_ifs,
{ rw [h, function.update_same] },
{ rw [function.update_noteq (ne.symm h)], refl },
end
end linear_map
namespace pi
open linear_map
open set
variables {R : Type*}
section module
variables {η : Type*} {ιs : η → Type*} {Ms : η → Type*}
lemma linear_independent_std_basis [ring R] [∀i, add_comm_group (Ms i)] [∀i, module R (Ms i)]
[decidable_eq η] (v : Πj, ιs j → (Ms j)) (hs : ∀i, linear_independent R (v i)) :
linear_independent R (λ (ji : Σ j, ιs j), std_basis R Ms ji.1 (v ji.1 ji.2)) :=
begin
have hs' : ∀j : η, linear_independent R (λ i : ιs j, std_basis R Ms j (v j i)),
{ intro j,
exact (hs j).map' _ (ker_std_basis _ _ _) },
apply linear_independent_Union_finite hs',
{ assume j J _ hiJ,
simp [(set.Union.equations._eqn_1 _).symm, submodule.span_image, submodule.span_Union],
have h₀ : ∀ j, span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))
≤ range (std_basis R Ms j),
{ intro j,
rw [span_le, linear_map.range_coe],
apply range_comp_subset_range },
have h₁ : span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))
≤ ⨆ i ∈ {j}, range (std_basis R Ms i),
{ rw @supr_singleton _ _ _ (λ i, linear_map.range (std_basis R (λ (j : η), Ms j) i)),
apply h₀ },
have h₂ : (⨆ j ∈ J, span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))) ≤
⨆ j ∈ J, range (std_basis R (λ (j : η), Ms j) j) :=
supr_le_supr (λ i, supr_le_supr (λ H, h₀ i)),
have h₃ : disjoint (λ (i : η), i ∈ {j}) J,
{ convert set.disjoint_singleton_left.2 hiJ using 0 },
exact (disjoint_std_basis_std_basis _ _ _ _ h₃).mono h₁ h₂ }
end
variables [semiring R] [∀i, add_comm_monoid (Ms i)] [∀i, module R (Ms i)]
variable [fintype η]
section
open linear_equiv
/-- `pi.basis (s : ∀ j, basis (ιs j) R (Ms j))` is the `Σ j, ιs j`-indexed basis on `Π j, Ms j`
given by `s j` on each component. -/
protected noncomputable def basis (s : ∀ j, basis (ιs j) R (Ms j)) :
basis (Σ j, ιs j) R (Π j, Ms j) :=
-- The `add_comm_monoid (Π j, Ms j)` instance was hard to find.
-- Defining this in tactic mode seems to shake up instance search enough that it works by itself.
by { refine basis.of_repr (_ ≪≫ₗ (finsupp.sigma_finsupp_lequiv_pi_finsupp R).symm),
exact linear_equiv.Pi_congr_right (λ j, (s j).repr) }
@[simp] lemma basis_repr_std_basis [decidable_eq η] (s : ∀ j, basis (ιs j) R (Ms j)) (j i) :
(pi.basis s).repr (std_basis R _ j (s j i)) = finsupp.single ⟨j, i⟩ 1 :=
begin
ext ⟨j', i'⟩,
by_cases hj : j = j',
{ subst hj,
simp only [pi.basis, linear_equiv.trans_apply, basis.repr_self, std_basis_same,
linear_equiv.Pi_congr_right_apply, finsupp.sigma_finsupp_lequiv_pi_finsupp_symm_apply],
symmetry,
exact basis.finsupp.single_apply_left
(λ i i' (h : (⟨j, i⟩ : Σ j, ιs j) = ⟨j, i'⟩), eq_of_heq (sigma.mk.inj h).2) _ _ _ },
simp only [pi.basis, linear_equiv.trans_apply, finsupp.sigma_finsupp_lequiv_pi_finsupp_symm_apply,
linear_equiv.Pi_congr_right_apply],
dsimp,
rw [std_basis_ne _ _ _ _ (ne.symm hj), linear_equiv.map_zero, finsupp.zero_apply,
finsupp.single_eq_of_ne],
rintros ⟨⟩,
contradiction
end
@[simp] lemma basis_apply [decidable_eq η] (s : ∀ j, basis (ιs j) R (Ms j)) (ji) :
pi.basis s ji = std_basis R _ ji.1 (s ji.1 ji.2) :=
basis.apply_eq_iff.mpr (by simp)
@[simp] lemma basis_repr (s : ∀ j, basis (ιs j) R (Ms j)) (x) (ji) :
(pi.basis s).repr x ji = (s ji.1).repr (x ji.1) ji.2 :=
rfl
end
section
variables (R η)
/-- The basis on `η → R` where the `i`th basis vector is `function.update 0 i 1`. -/
noncomputable def basis_fun : basis η R (Π (j : η), R) :=
basis.of_equiv_fun (linear_equiv.refl _ _)
@[simp] lemma basis_fun_apply [decidable_eq η] (i) :
basis_fun R η i = std_basis R (λ (i : η), R) i 1 :=
by { simp only [basis_fun, basis.coe_of_equiv_fun, linear_equiv.refl_symm,
linear_equiv.refl_apply, std_basis_apply],
congr /- Get rid of a `decidable_eq` mismatch. -/ }
@[simp] lemma basis_fun_repr (x : η → R) (i : η) :
(pi.basis_fun R η).repr x i = x i :=
by simp [basis_fun]
end
end module
end pi
namespace matrix
variables (R : Type*) (n : Type*) (m : Type*) [fintype m] [fintype n] [semiring R]
/-- The standard basis of `matrix n m R`. -/
noncomputable def std_basis : basis (n × m) R (matrix n m R) :=
basis.reindex (pi.basis (λ (i : n), pi.basis_fun R m)) (equiv.sigma_equiv_prod _ _)
variables {n m}
lemma std_basis_eq_std_basis_matrix (i : n) (j : m) [decidable_eq n] [decidable_eq m] :
std_basis R n m (i, j) = std_basis_matrix i j (1 : R) :=
begin
ext a b,
by_cases hi : i = a; by_cases hj : j = b,
{ simp [std_basis, hi, hj] },
{ simp [std_basis, hi, hj, ne.symm hj, linear_map.std_basis_ne] },
{ simp [std_basis, hi, hj, ne.symm hi, linear_map.std_basis_ne] },
{ simp [std_basis, hi, hj, ne.symm hj, ne.symm hi, linear_map.std_basis_ne] }
end
end matrix
|
144eea6b3bc98852ba817603b376beacb77a8353 | 92b13ae5cb04d78dd215ae736d93f5353e0e8e87 | /broken/C.lean | e062aaf0722997f7569e4f0476a522b6483dce25 | [] | no_license | jroesch/exp-compiler | f2dec4f17e769e7f3b41429c41ece1f004a3f209 | 6efd4512c80df947361bdada12415bc166db5e7f | refs/heads/master | 1,585,267,520,150 | 1,469,047,597,000 | 1,469,047,597,000 | 63,471,055 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,011 | lean | import data.list
inductive stack_lang :=
| push : nat -> stack_lang
| pop : stack_lang
| add : stack_lang
inductive exp :=
| add : exp -> exp -> exp
| literal : nat -> exp
open list
open stack_lang
definition compile : exp → list stack_lang
| compile (exp.literal i) := [ push i ]
| compile (exp.add e1 e2) :=
compile e1 ++ compile e2 ++ [add]
definition stack_state : Type.{1} := list nat
inductive stack_step : stack_lang → stack_state → stack_state -> Type :=
| step_add : Π st v1 v2, stack_step add (v1 :: v2 :: st) ((v1 + v2) :: st)
| step_pop : Π st n, stack_step pop (n :: st) st
| step_push : Π st n, stack_step (push n) st (n :: st)
inductive exp_step : exp → exp → Type :=
| add_literals : Π n m, exp_step (exp.add (exp.literal n) (exp.literal m)) (exp.literal (m + n))
-- inductive match_states : stack_state -> stack_state :=
inductive stack_star : list stack_lang → stack_state → stack_state → Prop :=
| refl : ∀ st, stack_star [] st st
| step : ∀ st st' st'' s ss,
stack_step s st st' →
stack_star ss st' st'' →
stack_star (s :: ss) st' st''
definition match_states : exp → stack_state → Prop := fun x y, true
lemma compile_always_cons :
forall e, exists x xs, compile e = x :: xs :=
begin
intros,
induction e,
cases v_0,
cases a_3,
cases v_1,
cases a_6,
constructor,
constructor,
unfold compile,
rewrite [a_4, a_7],
reflexivity,
constructor,
constructor,
unfold compile,
reflexivity
end
lemma compile_add_correct :
forall e1 e2, exists x xs y ys, compile (exp.add e1 e2) = (x :: xs) ++ (y :: ys) ++ [add] :=
begin
intros,
constructor,
constructor,
constructor,
constructor,
unfold compile,
end
theorem step_simulation :
∀ e e' st,
match_states e st →
exp_step e e' →
exists st', stack_star (compile e) st st' :=
begin
intros,
induction e,
constructor,
unfold compile,
eapply stack_star.step,
end
|
12eb4c153592ee00b4a6863f84cabf3b85fa92b3 | f5f7e6fae601a5fe3cac7cc3ed353ed781d62419 | /src/tactic/basic.lean | 22f7929c3a884db47874451102245414b8f4b65e | [
"Apache-2.0"
] | permissive | EdAyers/mathlib | 9ecfb2f14bd6caad748b64c9c131befbff0fb4e0 | ca5d4c1f16f9c451cf7170b10105d0051db79e1b | refs/heads/master | 1,626,189,395,845 | 1,555,284,396,000 | 1,555,284,396,000 | 144,004,030 | 0 | 0 | Apache-2.0 | 1,533,727,664,000 | 1,533,727,663,000 | null | UTF-8 | Lean | false | false | 33,993 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Simon Hudon, Scott Morrison, Keeley Hoek
-/
import data.dlist.basic category.basic meta.expr meta.rb_map
namespace expr
open tactic
attribute [derive has_reflect] binder_info
protected meta def of_nat (α : expr) : ℕ → tactic expr :=
nat.binary_rec
(tactic.mk_mapp ``has_zero.zero [some α, none])
(λ b n tac, if n = 0 then mk_mapp ``has_one.one [some α, none] else
do e ← tac, tactic.mk_app (cond b ``bit1 ``bit0) [e])
protected meta def of_int (α : expr) : ℤ → tactic expr
| (n : ℕ) := expr.of_nat α n
| -[1+ n] := do
e ← expr.of_nat α (n+1),
tactic.mk_app ``has_neg.neg [e]
/- only traverses the direct descendents -/
meta def {u} traverse {m : Type → Type u} [applicative m]
{elab elab' : bool} (f : expr elab → m (expr elab')) :
expr elab → m (expr elab')
| (var v) := pure $ var v
| (sort l) := pure $ sort l
| (const n ls) := pure $ const n ls
| (mvar n n' e) := mvar n n' <$> f e
| (local_const n n' bi e) := local_const n n' bi <$> f e
| (app e₀ e₁) := app <$> f e₀ <*> f e₁
| (lam n bi e₀ e₁) := lam n bi <$> f e₀ <*> f e₁
| (pi n bi e₀ e₁) := pi n bi <$> f e₀ <*> f e₁
| (elet n e₀ e₁ e₂) := elet n <$> f e₀ <*> f e₁ <*> f e₂
| (macro mac es) := macro mac <$> list.traverse f es
meta def mfoldl {α : Type} {m} [monad m] (f : α → expr → m α) : α → expr → m α
| x e := prod.snd <$> (state_t.run (e.traverse $ λ e',
(get >>= monad_lift ∘ flip f e' >>= put) $> e') x : m _)
end expr
namespace interaction_monad
open result
meta def get_result {σ α} (tac : interaction_monad σ α) :
interaction_monad σ (interaction_monad.result σ α) | s :=
match tac s with
| r@(success _ s') := success r s'
| r@(exception _ _ s') := success r s'
end
end interaction_monad
namespace lean.parser
open lean interaction_monad.result
meta def of_tactic' {α} (tac : tactic α) : parser α :=
do r ← of_tactic (interaction_monad.get_result tac),
match r with
| (success a _) := return a
| (exception f pos _) := exception f pos
end
-- Override the builtin `lean.parser.of_tactic` coe, which is broken.
-- (See test/tactics.lean for a failure case.)
@[priority 2000]
meta instance has_coe' {α} : has_coe (tactic α) (parser α) :=
⟨of_tactic'⟩
meta def emit_command_here (str : string) : lean.parser string :=
do (_, left) ← with_input command_like str,
return left
-- Emit a source code string at the location being parsed.
meta def emit_code_here : string → lean.parser unit
| str := do left ← emit_command_here str,
if left.length = 0 then return ()
else emit_code_here left
end lean.parser
namespace name
meta def head : name → string
| (mk_string s anonymous) := s
| (mk_string s p) := head p
| (mk_numeral n p) := head p
| anonymous := "[anonymous]"
meta def is_private (n : name) : bool :=
n.head = "_private"
meta def last : name → string
| (mk_string s _) := s
| (mk_numeral n _) := repr n
| anonymous := "[anonymous]"
meta def length : name → ℕ
| (mk_string s anonymous) := s.length
| (mk_string s p) := s.length + 1 + p.length
| (mk_numeral n p) := p.length
| anonymous := "[anonymous]".length
end name
namespace environment
meta def decl_filter_map {α : Type} (e : environment) (f : declaration → option α) : list α :=
e.fold [] $ λ d l, match f d with
| some r := r :: l
| none := l
end
meta def decl_map {α : Type} (e : environment) (f : declaration → α) : list α :=
e.decl_filter_map $ λ d, some (f d)
meta def get_decls (e : environment) : list declaration :=
e.decl_map id
meta def get_trusted_decls (e : environment) : list declaration :=
e.decl_filter_map (λ d, if d.is_trusted then some d else none)
meta def get_decl_names (e : environment) : list name :=
e.decl_map declaration.to_name
end environment
namespace format
meta def intercalate (x : format) : list format → format :=
format.join ∘ list.intersperse x
end format
namespace tactic
meta def eval_expr' (α : Type*) [_inst_1 : reflected α] (e : expr) : tactic α :=
mk_app ``id [e] >>= eval_expr α
-- `mk_fresh_name` returns identifiers starting with underscores,
-- which are not legal when emitted by tactic programs. Turn the
-- useful source of random names provided by `mk_fresh_name` into
-- names which are usable by tactic programs.
--
-- The returned name has four components which are all strings.
meta def mk_user_fresh_name : tactic name :=
do nm ← mk_fresh_name,
return $ `user__ ++ nm.pop_prefix.sanitize_name ++ `user__
meta def is_simp_lemma : name → tactic bool :=
succeeds ∘ tactic.has_attribute `simp
meta def local_decls : tactic (name_map declaration) :=
do e ← tactic.get_env,
let xs := e.fold native.mk_rb_map
(λ d s, if environment.in_current_file' e d.to_name
then s.insert d.to_name d else s),
pure xs
meta def simp_lemmas_from_file : tactic name_set :=
do s ← local_decls,
let s := s.map (expr.list_constant ∘ declaration.value),
xs ← s.to_list.mmap ((<$>) name_set.of_list ∘ mfilter tactic.is_simp_lemma ∘ name_set.to_list ∘ prod.snd),
return $ name_set.filter (λ x, ¬ s.contains x) (xs.foldl name_set.union mk_name_set)
meta def file_simp_attribute_decl (attr : name) : tactic unit :=
do s ← simp_lemmas_from_file,
trace format!"run_cmd mk_simp_attr `{attr}",
let lmms := format.join $ list.intersperse " " $ s.to_list.map to_fmt,
trace format!"local attribute [{attr}] {lmms}"
meta def mk_local (n : name) : expr :=
expr.local_const n n binder_info.default (expr.const n [])
meta def local_def_value (e : expr) : tactic expr := do
do (v,_) ← solve_aux `(true) (do
(expr.elet n t v _) ← (revert e >> target)
| fail format!"{e} is not a local definition",
return v),
return v
meta def check_defn (n : name) (e : pexpr) : tactic unit :=
do (declaration.defn _ _ _ d _ _) ← get_decl n,
e' ← to_expr e,
guard (d =ₐ e') <|> trace d >> failed
-- meta def compile_eqn (n : name) (univ : list name) (args : list expr) (val : expr) (num : ℕ) : tactic unit :=
-- do let lhs := (expr.const n $ univ.map level.param).mk_app args,
-- stmt ← mk_app `eq [lhs,val],
-- let vs := stmt.list_local_const,
-- let stmt := stmt.pis vs,
-- (_,pr) ← solve_aux stmt (tactic.intros >> reflexivity),
-- add_decl $ declaration.thm (n <.> "equations" <.> to_string (format!"_eqn_{num}")) univ stmt (pure pr)
meta def to_implicit : expr → expr
| (expr.local_const uniq n bi t) := expr.local_const uniq n binder_info.implicit t
| e := e
meta def pis : list expr → expr → tactic expr
| (e@(expr.local_const uniq pp info _) :: es) f := do
t ← infer_type e,
f' ← pis es f,
pure $ expr.pi pp info t (expr.abstract_local f' uniq)
| _ f := pure f
meta def lambdas : list expr → expr → tactic expr
| (e@(expr.local_const uniq pp info _) :: es) f := do
t ← infer_type e,
f' ← lambdas es f,
pure $ expr.lam pp info t (expr.abstract_local f' uniq)
| _ f := pure f
meta def extract_def (n : name) (trusted : bool) (elab_def : tactic unit) : tactic unit :=
do cxt ← list.map to_implicit <$> local_context,
t ← target,
(eqns,d) ← solve_aux t elab_def,
d ← instantiate_mvars d,
t' ← pis cxt t,
d' ← lambdas cxt d,
let univ := t'.collect_univ_params,
add_decl $ declaration.defn n univ t' d' (reducibility_hints.regular 1 tt) trusted,
applyc n
meta def exact_dec_trivial : tactic unit := `[exact dec_trivial]
/-- Runs a tactic for a result, reverting the state after completion -/
meta def retrieve {α} (tac : tactic α) : tactic α :=
λ s, result.cases_on (tac s)
(λ a s', result.success a s)
result.exception
/-- Repeat a tactic at least once, calling it recursively on all subgoals,
until it fails. This tactic fails if the first invocation fails. -/
meta def repeat1 (t : tactic unit) : tactic unit := t; repeat t
/-- `iterate_range m n t`: Repeat the given tactic at least `m` times and
at most `n` times or until `t` fails. Fails if `t` does not run at least m times. -/
meta def iterate_range : ℕ → ℕ → tactic unit → tactic unit
| 0 0 t := skip
| 0 (n+1) t := try (t >> iterate_range 0 n t)
| (m+1) n t := t >> iterate_range m (n-1) t
meta def replace_at (tac : expr → tactic (expr × expr)) (hs : list expr) (tgt : bool) : tactic bool :=
do to_remove ← hs.mfilter $ λ h, do {
h_type ← infer_type h,
succeeds $ do
(new_h_type, pr) ← tac h_type,
assert h.local_pp_name new_h_type,
mk_eq_mp pr h >>= tactic.exact },
goal_simplified ← succeeds $ do {
guard tgt,
(new_t, pr) ← target >>= tac,
replace_target new_t pr },
to_remove.mmap' (λ h, try (clear h)),
return (¬ to_remove.empty ∨ goal_simplified)
meta def simp_bottom_up' (post : expr → tactic (expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (expr × expr) :=
prod.snd <$> simplify_bottom_up () (λ _, (<$>) (prod.mk ()) ∘ post) e cfg
meta structure instance_cache :=
(α : expr)
(univ : level)
(inst : name_map expr)
meta def mk_instance_cache (α : expr) : tactic instance_cache :=
do u ← mk_meta_univ,
infer_type α >>= unify (expr.sort (level.succ u)),
u ← get_univ_assignment u,
return ⟨α, u, mk_name_map⟩
namespace instance_cache
meta def get (c : instance_cache) (n : name) : tactic (instance_cache × expr) :=
match c.inst.find n with
| some i := return (c, i)
| none := do e ← mk_app n [c.α] >>= mk_instance,
return (⟨c.α, c.univ, c.inst.insert n e⟩, e)
end
open expr
meta def append_typeclasses : expr → instance_cache → list expr →
tactic (instance_cache × list expr)
| (pi _ binder_info.inst_implicit (app (const n _) (var _)) body) c l :=
do (c, p) ← c.get n, return (c, p :: l)
| _ c l := return (c, l)
meta def mk_app (c : instance_cache) (n : name) (l : list expr) : tactic (instance_cache × expr) :=
do d ← get_decl n,
(c, l) ← append_typeclasses d.type.binding_body c l,
return (c, (expr.const n [c.univ]).mk_app (c.α :: l))
end instance_cache
/-- Reset the instance cache for the main goal. -/
meta def reset_instance_cache : tactic unit := unfreeze_local_instances
meta def match_head (e : expr) : expr → tactic unit
| e' :=
unify e e'
<|> do `(_ → %%e') ← whnf e',
v ← mk_mvar,
match_head (e'.instantiate_var v)
meta def find_matching_head : expr → list expr → tactic (list expr)
| e [] := return []
| e (H :: Hs) :=
do t ← infer_type H,
((::) H <$ match_head e t <|> pure id) <*> find_matching_head e Hs
meta def subst_locals (s : list (expr × expr)) (e : expr) : expr :=
(e.abstract_locals (s.map (expr.local_uniq_name ∘ prod.fst)).reverse).instantiate_vars (s.map prod.snd)
meta def set_binder : expr → list binder_info → expr
| e [] := e
| (expr.pi v _ d b) (bi :: bs) := expr.pi v bi d (set_binder b bs)
| e _ := e
meta def last_explicit_arg : expr → tactic expr
| (expr.app f e) :=
do t ← infer_type f >>= whnf,
if t.binding_info = binder_info.default
then pure e
else last_explicit_arg f
| e := pure e
private meta def get_expl_pi_arity_aux : expr → tactic nat
| (expr.pi n bi d b) :=
do m ← mk_fresh_name,
let l := expr.local_const m n bi d,
new_b ← whnf (expr.instantiate_var b l),
r ← get_expl_pi_arity_aux new_b,
if bi = binder_info.default then
return (r + 1)
else
return r
| e := return 0
/-- Compute the arity of explicit arguments of the given (Pi-)type -/
meta def get_expl_pi_arity (type : expr) : tactic nat :=
whnf type >>= get_expl_pi_arity_aux
/-- Compute the arity of explicit arguments of the given function -/
meta def get_expl_arity (fn : expr) : tactic nat :=
infer_type fn >>= get_expl_pi_arity
/-- variation on `assert` where a (possibly incomplete)
proof of the assertion is provided as a parameter.
``(h,gs) ← local_proof `h p tac`` creates a local `h : p` and
use `tac` to (partially) construct a proof for it. `gs` is the
list of remaining goals in the proof of `h`.
The benefits over assert are:
- unlike with ``h ← assert `h p, tac`` , `h` cannot be used by `tac`;
- when `tac` does not complete the proof of `h`, returning the list
of goals allows one to write a tactic using `h` and with the confidence
that a proof will not boil over to goals left over from the proof of `h`,
unlike what would be the case when using `tactic.swap`.
-/
meta def local_proof (h : name) (p : expr) (tac₀ : tactic unit) :
tactic (expr × list expr) :=
focus1 $
do h' ← assert h p,
[g₀,g₁] ← get_goals,
set_goals [g₀], tac₀,
gs ← get_goals,
set_goals [g₁],
return (h', gs)
meta def var_names : expr → list name
| (expr.pi n _ _ b) := n :: var_names b
| _ := []
meta def drop_binders : expr → tactic expr
| (expr.pi n bi t b) := b.instantiate_var <$> mk_local' n bi t >>= drop_binders
| e := pure e
meta def subobject_names (struct_n : name) : tactic (list name × list name) :=
do env ← get_env,
[c] ← pure $ env.constructors_of struct_n | fail "too many constructors",
vs ← var_names <$> (mk_const c >>= infer_type),
fields ← env.structure_fields struct_n,
return $ fields.partition (λ fn, ↑("_" ++ fn.to_string) ∈ vs)
meta def expanded_field_list' : name → tactic (dlist $ name × name) | struct_n :=
do (so,fs) ← subobject_names struct_n,
ts ← so.mmap (λ n, do
e ← mk_const (n.update_prefix struct_n) >>= infer_type >>= drop_binders,
expanded_field_list' $ e.get_app_fn.const_name),
return $ dlist.join ts ++ dlist.of_list (fs.map $ prod.mk struct_n)
open functor function
meta def expanded_field_list (struct_n : name) : tactic (list $ name × name) :=
dlist.to_list <$> expanded_field_list' struct_n
meta def get_classes (e : expr) : tactic (list name) :=
attribute.get_instances `class >>= list.mfilter (λ n,
succeeds $ mk_app n [e] >>= mk_instance)
open nat
meta def mk_mvar_list : ℕ → tactic (list expr)
| 0 := pure []
| (succ n) := (::) <$> mk_mvar <*> mk_mvar_list n
/-- Returns the only goal, or fails if there isn't just one goal. -/
meta def get_goal : tactic expr :=
do gs ← get_goals,
match gs with
| [a] := return a
| [] := fail "there are no goals"
| _ := fail "there are too many goals"
end
/--`iterate_at_most_on_all_goals n t`: repeat the given tactic at most `n` times on all goals,
or until it fails. Always succeeds. -/
meta def iterate_at_most_on_all_goals : nat → tactic unit → tactic unit
| 0 tac := trace "maximal iterations reached"
| (succ n) tac := tactic.all_goals $ (do tac, iterate_at_most_on_all_goals n tac) <|> skip
/--`iterate_at_most_on_subgoals n t`: repeat the tactic `t` at most `n` times on the first
goal and on all subgoals thus produced, or until it fails. Fails iff `t` fails on
current goal. -/
meta def iterate_at_most_on_subgoals : nat → tactic unit → tactic unit
| 0 tac := trace "maximal iterations reached"
| (succ n) tac := focus1 (do tac, iterate_at_most_on_all_goals n tac)
/--`apply_list l`: try to apply the tactics in the list `l` on the first goal, and
fail if none succeeds -/
meta def apply_list_expr : list expr → tactic unit
| [] := fail "no matching rule"
| (h::t) := do interactive.concat_tags (apply h) <|> apply_list_expr t
/-- constructs a list of expressions given a list of p-expressions, as follows:
- if the p-expression is the name of a theorem, use `i_to_expr_for_apply` on it
- if the p-expression is a user attribute, add all the theorems with this attribute
to the list.-/
meta def build_list_expr_for_apply : list pexpr → tactic (list expr)
| [] := return []
| (h::t) := do
tail ← build_list_expr_for_apply t,
a ← i_to_expr_for_apply h,
(do l ← attribute.get_instances (expr.const_name a),
m ← list.mmap mk_const l,
return (m.append tail))
<|> return (a::tail)
/--`apply_rules hs n`: apply the list of rules `hs` (given as pexpr) and `assumption` on the
first goal and the resulting subgoals, iteratively, at most `n` times -/
meta def apply_rules (hs : list pexpr) (n : nat) : tactic unit :=
do l ← build_list_expr_for_apply hs,
iterate_at_most_on_subgoals n (assumption <|> apply_list_expr l)
meta def replace (h : name) (p : pexpr) : tactic unit :=
do h' ← get_local h,
p ← to_expr p,
note h none p,
clear h'
/-- Auxiliary function for `iff_mp` and `iff_mpr`. Takes a name, which should be either `` `iff.mp``
or `` `iff.mpr``. If the passed expression is an iterated function type eventually producing an
`iff`, returns an expression with the `iff` converted to either the forwards or backwards
implication, as requested. -/
meta def mk_iff_mp_app (iffmp : name) : expr → (nat → expr) → option expr
| (expr.pi n bi e t) f := expr.lam n bi e <$> mk_iff_mp_app t (λ n, f (n+1) (expr.var n))
| `(%%a ↔ %%b) f := some $ @expr.const tt iffmp [] a b (f 0)
| _ f := none
meta def iff_mp_core (e ty: expr) : option expr :=
mk_iff_mp_app `iff.mp ty (λ_, e)
meta def iff_mpr_core (e ty: expr) : option expr :=
mk_iff_mp_app `iff.mpr ty (λ_, e)
/-- Given an expression whose type is (a possibly iterated function producing) an `iff`,
create the expression which is the forward implication. -/
meta def iff_mp (e : expr) : tactic expr :=
do t ← infer_type e,
iff_mp_core e t <|> fail "Target theorem must have the form `Π x y z, a ↔ b`"
/-- Given an expression whose type is (a possibly iterated function producing) an `iff`,
create the expression which is the reverse implication. -/
meta def iff_mpr (e : expr) : tactic expr :=
do t ← infer_type e,
iff_mpr_core e t <|> fail "Target theorem must have the form `Π x y z, a ↔ b`"
/--
Attempts to apply `e`, and if that fails, if `e` is an `iff`,
try applying both directions separately.
-/
meta def apply_iff (e : expr) : tactic (list (name × expr)) :=
let ap e := tactic.apply e {new_goals := new_goals.non_dep_only} in
ap e <|> (iff_mp e >>= ap) <|> (iff_mpr e >>= ap)
meta def symm_apply (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) :=
tactic.apply e cfg <|> (symmetry >> tactic.apply e cfg)
meta def apply_assumption
(asms : tactic (list expr) := local_context)
(tac : tactic unit := skip) : tactic unit :=
do { ctx ← asms,
ctx.any_of (λ H, symm_apply H >> tac) } <|>
do { exfalso,
ctx ← asms,
ctx.any_of (λ H, symm_apply H >> tac) }
<|> fail "assumption tactic failed"
meta def change_core (e : expr) : option expr → tactic unit
| none := tactic.change e
| (some h) :=
do num_reverted : ℕ ← revert h,
expr.pi n bi d b ← target,
tactic.change $ expr.pi n bi e b,
intron num_reverted
/--
assuming olde and newe are defeq when elaborated, replaces occurences of olde with newe at hypothesis h.
-/
meta def change_with_at (olde newe : pexpr) (hyp : name) : tactic unit :=
do h ← get_local hyp,
tp ← infer_type h,
olde ← to_expr olde, newe ← to_expr newe,
let repl_tp := tp.replace (λ a n, if a = olde then some newe else none),
change_core repl_tp (some h)
open nat
meta def solve_by_elim_aux (discharger : tactic unit) (asms : tactic (list expr)) : ℕ → tactic unit
| 0 := done
| (succ n) := discharger <|> (apply_assumption asms $ solve_by_elim_aux n)
meta structure by_elim_opt :=
(all_goals : bool := ff)
(discharger : tactic unit := done)
(assumptions : tactic (list expr) := local_context)
(max_rep : ℕ := 3)
meta def solve_by_elim (opt : by_elim_opt := { }) : tactic unit :=
do
tactic.fail_if_no_goals,
(if opt.all_goals then id else focus1) $
solve_by_elim_aux opt.discharger opt.assumptions opt.max_rep
meta def metavariables : tactic (list expr) :=
do r ← result,
pure (r.list_meta_vars)
/-- Succeeds only if the current goal is a proposition. -/
meta def propositional_goal : tactic unit :=
do goals ← get_goals,
p ← is_proof goals.head,
guard p
meta def triv' : tactic unit := do c ← mk_const `trivial, exact c reducible
variable {α : Type}
private meta def iterate_aux (t : tactic α) : list α → tactic (list α)
| L := (do r ← t, iterate_aux (r :: L)) <|> return L
/-- Apply a tactic as many times as possible, collecting the results in a list. -/
meta def iterate' (t : tactic α) : tactic (list α) :=
list.reverse <$> iterate_aux t []
/-- Like iterate', but fail if the tactic does not succeed at least once. -/
meta def iterate1 (t : tactic α) : tactic (α × list α) :=
do r ← decorate_ex "iterate1 failed: tactic did not succeed" t,
L ← iterate' t,
return (r, L)
meta def intros1 : tactic (list expr) :=
iterate1 intro1 >>= λ p, return (p.1 :: p.2)
/-- `successes` invokes each tactic in turn, returning the list of successful results. -/
meta def successes (tactics : list (tactic α)) : tactic (list α) :=
list.filter_map id <$> monad.sequence (tactics.map (λ t, try_core t))
/-- Return target after instantiating metavars and whnf -/
private meta def target' : tactic expr :=
target >>= instantiate_mvars >>= whnf
/--
Just like `split`, `fsplit` applies the constructor when the type of the target is an inductive data type with one constructor.
However it does not reorder goals or invoke `auto_param` tactics.
-/
-- FIXME check if we can remove `auto_param := ff`
meta def fsplit : tactic unit :=
do [c] ← target' >>= get_constructors_for | tactic.fail "fsplit tactic failed, target is not an inductive datatype with only one constructor",
mk_const c >>= λ e, apply e {new_goals := new_goals.all, auto_param := ff} >> skip
run_cmd add_interactive [`fsplit]
/-- Calls `injection` on each hypothesis, and then, for each hypothesis on which `injection`
succeeds, clears the old hypothesis. -/
meta def injections_and_clear : tactic unit :=
do l ← local_context,
results ← successes $ l.map $ λ e, injection e >> clear e,
when (results.empty) (fail "could not use `injection` then `clear` on any hypothesis")
run_cmd add_interactive [`injections_and_clear]
meta def note_anon (e : expr) : tactic unit :=
do n ← get_unused_name "lh",
note n none e, skip
/-- `find_local t` returns a local constant with type t, or fails if none exists. -/
meta def find_local (t : pexpr) : tactic expr :=
do t' ← to_expr t,
prod.snd <$> solve_aux t' assumption
/-- `dependent_pose_core l`: introduce dependent hypothesis, where the proofs depend on the values
of the previous local constants. `l` is a list of local constants and their values. -/
meta def dependent_pose_core (l : list (expr × expr)) : tactic unit := do
let lc := l.map prod.fst,
let lm := l.map (λ⟨l, v⟩, (l.local_uniq_name, v)),
t ← target,
new_goal ← mk_meta_var (t.pis lc),
old::other_goals ← get_goals,
set_goals (old :: new_goal :: other_goals),
exact ((new_goal.mk_app lc).instantiate_locals lm),
return ()
/-- like `mk_local_pis` but translating into weak head normal form before checking if it is a Π. -/
meta def mk_local_pis_whnf : expr → tactic (list expr × expr) | e := do
(expr.pi n bi d b) ← whnf e | return ([], e),
p ← mk_local' n bi d,
(ps, r) ← mk_local_pis (expr.instantiate_var b p),
return ((p :: ps), r)
/-- Changes `(h : ∀xs, ∃a:α, p a) ⊢ g` to `(d : ∀xs, a) (s : ∀xs, p (d xs) ⊢ g` -/
meta def choose1 (h : expr) (data : name) (spec : name) : tactic expr := do
t ← infer_type h,
(ctxt, t) ← mk_local_pis_whnf t,
`(@Exists %%α %%p) ← whnf t transparency.all | fail "expected a term of the shape ∀xs, ∃a, p xs a",
α_t ← infer_type α,
expr.sort u ← whnf α_t transparency.all,
value ← mk_local_def data (α.pis ctxt),
t' ← head_beta (p.app (value.mk_app ctxt)),
spec ← mk_local_def spec (t'.pis ctxt),
dependent_pose_core [
(value, ((((expr.const `classical.some [u]).app α).app p).app (h.mk_app ctxt)).lambdas ctxt),
(spec, ((((expr.const `classical.some_spec [u]).app α).app p).app (h.mk_app ctxt)).lambdas ctxt)],
try (tactic.clear h),
intro1,
intro1
/-- Changes `(h : ∀xs, ∃as, p as) ⊢ g` to a list of functions `as`, an a final hypothesis on `p as` -/
meta def choose : expr → list name → tactic unit
| h [] := fail "expect list of variables"
| h [n] := do
cnt ← revert h,
intro n,
intron (cnt - 1),
return ()
| h (n::ns) := do
v ← get_unused_name >>= choose1 h n,
choose v ns
/-- This makes sure that the execution of the tactic does not change the tactic state.
This can be helpful while using rewrite, apply, or expr munging.
Remember to instantiate your metavariables before you're done! -/
meta def lock_tactic_state {α} (t : tactic α) : tactic α
| s := match t s with
| result.success a s' := result.success a s
| result.exception msg pos s' := result.exception msg pos s
end
/--
Hole command used to fill in a structure's field when specifying an instance.
In the following:
```
instance : monad id :=
{! !}
```
invoking hole command `Instance Stub` produces:
```
instance : monad id :=
{ map := _,
map_const := _,
pure := _,
seq := _,
seq_left := _,
seq_right := _,
bind := _ }
```
-/
@[hole_command] meta def instance_stub : hole_command :=
{ name := "Instance Stub",
descr := "Generate a skeleton for the structure under construction.",
action := λ _,
do tgt ← target >>= whnf,
let cl := tgt.get_app_fn.const_name,
env ← get_env,
fs ← expanded_field_list cl,
let fs := fs.map prod.snd,
let fs := format.intercalate (",\n " : format) $ fs.map (λ fn, format!"{fn} := _"),
let out := format.to_string format!"{{ {fs} }",
return [(out,"")] }
meta def strip_prefix' (n : name) : list string → name → tactic name
| s name.anonymous := pure $ s.foldl (flip name.mk_string) name.anonymous
| s (name.mk_string a p) :=
do let n' := s.foldl (flip name.mk_string) name.anonymous,
do { n'' ← tactic.resolve_constant n',
if n'' = n
then pure n'
else strip_prefix' (a :: s) p }
<|> strip_prefix' (a :: s) p
| s (name.mk_numeral a p) := interaction_monad.failed
meta def strip_prefix : name → tactic name
| n@(name.mk_string a a_1) := strip_prefix' n [a] a_1
| _ := interaction_monad.failed
meta def is_default_local : expr → bool
| (expr.local_const _ _ binder_info.default _) := tt
| _ := ff
meta def mk_patterns (t : expr) : tactic (list format) :=
do let cl := t.get_app_fn.const_name,
env ← get_env,
let fs := env.constructors_of cl,
fs.mmap $ λ f,
do { (vs,_) ← mk_const f >>= infer_type >>= mk_local_pis,
let vs := vs.filter (λ v, is_default_local v),
vs ← vs.mmap (λ v,
do v' ← get_unused_name v.local_pp_name,
pose v' none `(()),
pure v' ),
vs.mmap' $ λ v, get_local v >>= clear,
let args := list.intersperse (" " : format) $ vs.map to_fmt,
f ← strip_prefix f,
if args.empty
then pure $ format!"| {f} := _\n"
else pure format!"| ({f} {format.join args}) := _\n" }
/--
Hole command used to generate a `match` expression.
In the following:
```
meta def foo (e : expr) : tactic unit :=
{! e !}
```
invoking hole command `Match Stub` produces:
```
meta def foo (e : expr) : tactic unit :=
match e with
| (expr.var a) := _
| (expr.sort a) := _
| (expr.const a a_1) := _
| (expr.mvar a a_1 a_2) := _
| (expr.local_const a a_1 a_2 a_3) := _
| (expr.app a a_1) := _
| (expr.lam a a_1 a_2 a_3) := _
| (expr.pi a a_1 a_2 a_3) := _
| (expr.elet a a_1 a_2 a_3) := _
| (expr.macro a a_1) := _
end
```
-/
@[hole_command] meta def match_stub : hole_command :=
{ name := "Match Stub",
descr := "Generate a list of equations for a `match` expression.",
action := λ es,
do [e] ← pure es | fail "expecting one expression",
e ← to_expr e,
t ← infer_type e >>= whnf,
fs ← mk_patterns t,
e ← pp e,
let out := format.to_string format!"match {e} with\n{format.join fs}end\n",
return [(out,"")] }
/--
Hole command used to generate a `match` expression.
In the following:
```
meta def foo : {! expr → tactic unit !} -- `:=` is omitted
```
invoking hole command `Equations Stub` produces:
```
meta def foo : expr → tactic unit
| (expr.var a) := _
| (expr.sort a) := _
| (expr.const a a_1) := _
| (expr.mvar a a_1 a_2) := _
| (expr.local_const a a_1 a_2 a_3) := _
| (expr.app a a_1) := _
| (expr.lam a a_1 a_2 a_3) := _
| (expr.pi a a_1 a_2 a_3) := _
| (expr.elet a a_1 a_2 a_3) := _
| (expr.macro a a_1) := _
```
A similar result can be obtained by invoking `Equations Stub` on the following:
```
meta def foo : expr → tactic unit := -- do not forget to write `:=`!!
{! !}
```
```
meta def foo : expr → tactic unit := -- don't forget to erase `:=`!!
| (expr.var a) := _
| (expr.sort a) := _
| (expr.const a a_1) := _
| (expr.mvar a a_1 a_2) := _
| (expr.local_const a a_1 a_2 a_3) := _
| (expr.app a a_1) := _
| (expr.lam a a_1 a_2 a_3) := _
| (expr.pi a a_1 a_2 a_3) := _
| (expr.elet a a_1 a_2 a_3) := _
| (expr.macro a a_1) := _
```
-/
@[hole_command] meta def eqn_stub : hole_command :=
{ name := "Equations Stub",
descr := "Generate a list of equations for a recursive definition.",
action := λ es,
do t ← match es with
| [t] := to_expr t
| [] := target
| _ := fail "expecting one type"
end,
e ← whnf t,
(v :: _,_) ← mk_local_pis e | fail "expecting a Pi-type",
t' ← infer_type v,
fs ← mk_patterns t',
t ← pp t,
let out :=
if es.empty then
format.to_string format!"-- do not forget to erase `:=`!!\n{format.join fs}"
else format.to_string format!"{t}\n{format.join fs}",
return [(out,"")] }
/--
This command lists the constructors that can be used to satisfy the expected type.
When used in the following hole:
```
def foo : ℤ ⊕ ℕ :=
{! !}
```
the command will produce:
```
def foo : ℤ ⊕ ℕ :=
{! sum.inl, sum.inr !}
```
and will display:
```
sum.inl : ℤ → ℤ ⊕ ℕ
sum.inr : ℕ → ℤ ⊕ ℕ
```
-/
@[hole_command] meta def list_constructors_hole : hole_command :=
{ name := "List Constructors",
descr := "Show the list of constructors of the expected type.",
action := λ es,
do t ← target >>= whnf,
(_,t) ← mk_local_pis t,
let cl := t.get_app_fn.const_name,
let args := t.get_app_args,
env ← get_env,
let cs := env.constructors_of cl,
ts ← cs.mmap $ λ c,
do { e ← mk_const c,
t ← infer_type (e.mk_app args) >>= pp,
c ← strip_prefix c,
pure format!"\n{c} : {t}\n" },
fs ← format.intercalate ", " <$> cs.mmap (strip_prefix >=> pure ∘ to_fmt),
let out := format.to_string format!"{{! {fs} !}",
trace (format.join ts).to_string,
return [(out,"")] }
meta def classical : tactic unit :=
do h ← get_unused_name `_inst,
mk_const `classical.prop_decidable >>= note h none,
reset_instance_cache
open expr
meta def add_prime : name → name
| (name.mk_string s p) := name.mk_string (s ++ "'") p
| n := (name.mk_string "x'" n)
meta def mk_comp (v : expr) : expr → tactic expr
| (app f e) :=
if e = v then pure f
else do
guard (¬ v.occurs f) <|> fail "bad guard",
e' ← mk_comp e >>= instantiate_mvars,
f ← instantiate_mvars f,
mk_mapp ``function.comp [none,none,none,f,e']
| e :=
do guard (e = v),
t ← infer_type e,
mk_mapp ``id [t]
meta def mk_higher_order_type : expr → tactic expr
| (pi n bi d b@(pi _ _ _ _)) :=
do v ← mk_local_def n d,
let b' := (b.instantiate_var v),
(pi n bi d ∘ flip abstract_local v.local_uniq_name) <$> mk_higher_order_type b'
| (pi n bi d b) :=
do v ← mk_local_def n d,
let b' := (b.instantiate_var v),
(l,r) ← match_eq b' <|> fail format!"not an equality {b'}",
l' ← mk_comp v l,
r' ← mk_comp v r,
mk_app ``eq [l',r']
| e := failed
open lean.parser interactive.types
@[user_attribute]
meta def higher_order_attr : user_attribute unit (option name) :=
{ name := `higher_order,
parser := optional ident,
descr :=
"From a lemma of the shape `f (g x) = h x` derive an auxiliary lemma of the
form `f ∘ g = h` for reasoning about higher-order functions.",
after_set := some $ λ lmm _ _,
do env ← get_env,
decl ← env.get lmm,
let num := decl.univ_params.length,
let lvls := (list.iota num).map (`l).append_after,
let l : expr := expr.const lmm $ lvls.map level.param,
t ← infer_type l >>= instantiate_mvars,
t' ← mk_higher_order_type t,
(_,pr) ← solve_aux t' $ do {
intros, applyc ``_root_.funext, intro1, applyc lmm; assumption },
pr ← instantiate_mvars pr,
lmm' ← higher_order_attr.get_param lmm,
lmm' ← (flip name.update_prefix lmm.get_prefix <$> lmm') <|> pure (add_prime lmm),
add_decl $ declaration.thm lmm' lvls t' (pure pr),
copy_attribute `simp lmm tt lmm',
copy_attribute `functor_norm lmm tt lmm' }
attribute [higher_order map_comp_pure] map_pure
private meta def tactic.use_aux (h : pexpr) : tactic unit :=
(focus1 (refine h >> done)) <|> (fconstructor >> tactic.use_aux)
meta def tactic.use (l : list pexpr) : tactic unit :=
focus1 $ l.mmap' $ λ h, tactic.use_aux h <|> fail format!"failed to instantiate goal with {h}"
meta def clear_aux_decl_aux : list expr → tactic unit
| [] := skip
| (e::l) := do cond e.is_aux_decl (tactic.clear e) skip, clear_aux_decl_aux l
meta def clear_aux_decl : tactic unit :=
local_context >>= clear_aux_decl_aux
end tactic
|
3cd76c80e4112a2d13113732519a5eaa244910cd | bb31430994044506fa42fd667e2d556327e18dfe | /src/linear_algebra/affine_space/matrix.lean | b0109bc3f186c3b99819bd896d3977a4dd3efcbb | [
"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 | 6,817 | 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 linear_algebra.affine_space.basis
import linear_algebra.determinant
/-!
# Matrix results for barycentric co-ordinates
Results about the matrix of barycentric co-ordinates for a family of points in an affine space, with
respect to some affine basis.
-/
open_locale affine big_operators matrix
open set
universes u₁ u₂ u₃ u₄
variables {ι : Type u₁} {k : Type u₂} {V : Type u₃} {P : Type u₄}
variables [add_comm_group V] [affine_space V P]
namespace affine_basis
section ring
variables [ring k] [module k V] (b : affine_basis ι k P)
/-- Given an affine basis `p`, and a family of points `q : ι' → P`, this is the matrix whose
rows are the barycentric coordinates of `q` with respect to `p`.
It is an affine equivalent of `basis.to_matrix`. -/
noncomputable def to_matrix {ι' : Type*} (q : ι' → P) : matrix ι' ι k :=
λ i j, b.coord j (q i)
@[simp] lemma to_matrix_apply {ι' : Type*} (q : ι' → P) (i : ι') (j : ι) :
b.to_matrix q i j = b.coord j (q i) :=
rfl
@[simp] lemma to_matrix_self [decidable_eq ι] :
b.to_matrix b.points = (1 : matrix ι ι k) :=
begin
ext i j,
rw [to_matrix_apply, coord_apply, matrix.one_eq_pi_single, pi.single_apply],
end
variables {ι' : Type*} [fintype ι'] [fintype ι] (b₂ : affine_basis ι k P)
lemma to_matrix_row_sum_one {ι' : Type*} (q : ι' → P) (i : ι') :
∑ j, b.to_matrix q i j = 1 :=
by simp
/-- Given a family of points `p : ι' → P` and an affine basis `b`, if the matrix whose rows are the
coordinates of `p` with respect `b` has a right inverse, then `p` is affine independent. -/
lemma affine_independent_of_to_matrix_right_inv [decidable_eq ι']
(p : ι' → P) {A : matrix ι ι' k} (hA : (b.to_matrix p) ⬝ A = 1) : affine_independent k p :=
begin
rw affine_independent_iff_eq_of_fintype_affine_combination_eq,
intros w₁ w₂ hw₁ hw₂ hweq,
have hweq' : (b.to_matrix p).vec_mul w₁ = (b.to_matrix p).vec_mul w₂,
{ ext j,
change ∑ i, (w₁ i) • (b.coord j (p i)) = ∑ i, (w₂ i) • (b.coord j (p i)),
rw [← finset.univ.affine_combination_eq_linear_combination _ _ hw₁,
← finset.univ.affine_combination_eq_linear_combination _ _ hw₂,
← finset.univ.map_affine_combination p w₁ hw₁,
← finset.univ.map_affine_combination p w₂ hw₂, hweq], },
replace hweq' := congr_arg (λ w, A.vec_mul w) hweq',
simpa only [matrix.vec_mul_vec_mul, ← matrix.mul_eq_mul, hA, matrix.vec_mul_one] using hweq',
end
/-- Given a family of points `p : ι' → P` and an affine basis `b`, if the matrix whose rows are the
coordinates of `p` with respect `b` has a left inverse, then `p` spans the entire space. -/
lemma affine_span_eq_top_of_to_matrix_left_inv [decidable_eq ι] [nontrivial k]
(p : ι' → P) {A : matrix ι ι' k} (hA : A ⬝ b.to_matrix p = 1) : affine_span k (range p) = ⊤ :=
begin
suffices : ∀ i, b.points i ∈ affine_span k (range p),
{ rw [eq_top_iff, ← b.tot, affine_span_le],
rintros q ⟨i, rfl⟩,
exact this i, },
intros i,
have hAi : ∑ j, A i j = 1,
{ calc ∑ j, A i j = ∑ j, (A i j) * ∑ l, b.to_matrix p j l : by simp
... = ∑ j, ∑ l, (A i j) * b.to_matrix p j l : by simp_rw finset.mul_sum
... = ∑ l, ∑ j, (A i j) * b.to_matrix p j l : by rw finset.sum_comm
... = ∑ l, (A ⬝ b.to_matrix p) i l : rfl
... = 1 : by simp [hA, matrix.one_apply, finset.filter_eq], },
have hbi : b.points i = finset.univ.affine_combination p (A i),
{ apply b.ext_elem,
intros j,
rw [b.coord_apply, finset.univ.map_affine_combination _ _ hAi,
finset.univ.affine_combination_eq_linear_combination _ _ hAi],
change _ = (A ⬝ b.to_matrix p) i j,
simp_rw [hA, matrix.one_apply, @eq_comm _ i j] },
rw hbi,
exact affine_combination_mem_affine_span hAi p,
end
/-- A change of basis formula for barycentric coordinates.
See also `affine_basis.to_matrix_inv_mul_affine_basis_to_matrix`. -/
@[simp] lemma to_matrix_vec_mul_coords (x : P) :
(b.to_matrix b₂.points).vec_mul (b₂.coords x) = b.coords x :=
begin
ext j,
change _ = b.coord j x,
conv_rhs { rw ← b₂.affine_combination_coord_eq_self x, },
rw finset.map_affine_combination _ _ _ (b₂.sum_coord_apply_eq_one x),
simp [matrix.vec_mul, matrix.dot_product, to_matrix_apply, coords],
end
variables [decidable_eq ι]
lemma to_matrix_mul_to_matrix :
(b.to_matrix b₂.points) ⬝ (b₂.to_matrix b.points) = 1 :=
begin
ext l m,
change (b₂.to_matrix b.points).vec_mul (b.coords (b₂.points l)) m = _,
rw [to_matrix_vec_mul_coords, coords_apply, ← to_matrix_apply, to_matrix_self],
end
lemma is_unit_to_matrix :
is_unit (b.to_matrix b₂.points) :=
⟨{ val := b.to_matrix b₂.points,
inv := b₂.to_matrix b.points,
val_inv := b.to_matrix_mul_to_matrix b₂,
inv_val := b₂.to_matrix_mul_to_matrix b, }, rfl⟩
lemma is_unit_to_matrix_iff [nontrivial k] (p : ι → P) :
is_unit (b.to_matrix p) ↔ affine_independent k p ∧ affine_span k (range p) = ⊤ :=
begin
split,
{ rintros ⟨⟨B, A, hA, hA'⟩, (rfl : B = b.to_matrix p)⟩,
rw matrix.mul_eq_mul at hA hA',
exact ⟨b.affine_independent_of_to_matrix_right_inv p hA,
b.affine_span_eq_top_of_to_matrix_left_inv p hA'⟩, },
{ rintros ⟨h_tot, h_ind⟩,
let b' : affine_basis ι k P := ⟨p, h_tot, h_ind⟩,
change is_unit (b.to_matrix b'.points),
exact b.is_unit_to_matrix b', },
end
end ring
section comm_ring
variables [comm_ring k] [module k V] [decidable_eq ι] [fintype ι]
variables (b b₂ : affine_basis ι k P)
/-- A change of basis formula for barycentric coordinates.
See also `affine_basis.to_matrix_vec_mul_coords`. -/
@[simp] lemma to_matrix_inv_vec_mul_to_matrix (x : P) :
(b.to_matrix b₂.points)⁻¹.vec_mul (b.coords x) = b₂.coords x :=
begin
have hu := b.is_unit_to_matrix b₂,
rw matrix.is_unit_iff_is_unit_det at hu,
rw [← b.to_matrix_vec_mul_coords b₂, matrix.vec_mul_vec_mul, matrix.mul_nonsing_inv _ hu,
matrix.vec_mul_one],
end
/-- If we fix a background affine basis `b`, then for any other basis `b₂`, we can characterise
the barycentric coordinates provided by `b₂` in terms of determinants relative to `b`. -/
lemma det_smul_coords_eq_cramer_coords (x : P) :
(b.to_matrix b₂.points).det • b₂.coords x = (b.to_matrix b₂.points)ᵀ.cramer (b.coords x) :=
begin
have hu := b.is_unit_to_matrix b₂,
rw matrix.is_unit_iff_is_unit_det at hu,
rw [← b.to_matrix_inv_vec_mul_to_matrix, matrix.det_smul_inv_vec_mul_eq_cramer_transpose _ _ hu],
end
end comm_ring
end affine_basis
|
ace7e196d798e6d70b601f395ee32fa6a3ac03a5 | 432d948a4d3d242fdfb44b81c9e1b1baacd58617 | /src/data/finset/sort.lean | e2d952d077e26f42b6ef5f37484670a10cf77e23 | [
"Apache-2.0"
] | permissive | JLimperg/aesop3 | 306cc6570c556568897ed2e508c8869667252e8a | a4a116f650cc7403428e72bd2e2c4cda300fe03f | refs/heads/master | 1,682,884,916,368 | 1,620,320,033,000 | 1,620,320,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,351 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.finset.lattice
import data.multiset.sort
import data.list.nodup_equiv_fin
/-!
# Construct a sorted list from a finset.
-/
namespace finset
open multiset nat
variables {α β : Type*}
/-! ### sort -/
section sort
variables (r : α → α → Prop) [decidable_rel r]
[is_trans α r] [is_antisymm α r] [is_total α r]
/-- `sort s` constructs a sorted list from the unordered set `s`.
(Uses merge sort algorithm.) -/
def sort (s : finset α) : list α := sort r s.1
@[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) :=
sort_sorted _ _
@[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 :=
sort_eq _ _
@[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup :=
(by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s))
@[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s :=
list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s)
@[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s :=
multiset.mem_sort _
@[simp] theorem length_sort {s : finset α} : (sort r s).length = s.card :=
multiset.length_sort _
end sort
section sort_linear_order
variables [linear_order α]
theorem sort_sorted_lt (s : finset α) : list.sorted (<) (sort (≤) s) :=
(sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _)
lemma sorted_zero_eq_min'_aux (s : finset α) (h : 0 < (s.sort (≤)).length) (H : s.nonempty) :
(s.sort (≤)).nth_le 0 h = s.min' H :=
begin
let l := s.sort (≤),
apply le_antisymm,
{ have : s.min' H ∈ l := (finset.mem_sort (≤)).mpr (s.min'_mem H),
obtain ⟨i, i_lt, hi⟩ : ∃ i (hi : i < l.length), l.nth_le i hi = s.min' H :=
list.mem_iff_nth_le.1 this,
rw ← hi,
exact (s.sort_sorted (≤)).rel_nth_le_of_le _ _ (nat.zero_le i) },
{ have : l.nth_le 0 h ∈ s := (finset.mem_sort (≤)).1 (list.nth_le_mem l 0 h),
exact s.min'_le _ this }
end
lemma sorted_zero_eq_min' {s : finset α} {h : 0 < (s.sort (≤)).length} :
(s.sort (≤)).nth_le 0 h = s.min' (card_pos.1 $ by rwa length_sort at h) :=
sorted_zero_eq_min'_aux _ _ _
lemma min'_eq_sorted_zero {s : finset α} {h : s.nonempty} :
s.min' h = (s.sort (≤)).nth_le 0 (by { rw length_sort, exact card_pos.2 h }) :=
(sorted_zero_eq_min'_aux _ _ _).symm
lemma sorted_last_eq_max'_aux (s : finset α) (h : (s.sort (≤)).length - 1 < (s.sort (≤)).length)
(H : s.nonempty) : (s.sort (≤)).nth_le ((s.sort (≤)).length - 1) h = s.max' H :=
begin
let l := s.sort (≤),
apply le_antisymm,
{ have : l.nth_le ((s.sort (≤)).length - 1) h ∈ s :=
(finset.mem_sort (≤)).1 (list.nth_le_mem l _ h),
exact s.le_max' _ this },
{ have : s.max' H ∈ l := (finset.mem_sort (≤)).mpr (s.max'_mem H),
obtain ⟨i, i_lt, hi⟩ : ∃ i (hi : i < l.length), l.nth_le i hi = s.max' H :=
list.mem_iff_nth_le.1 this,
rw ← hi,
have : i ≤ l.length - 1 := nat.le_pred_of_lt i_lt,
exact (s.sort_sorted (≤)).rel_nth_le_of_le _ _ (nat.le_pred_of_lt i_lt) },
end
lemma sorted_last_eq_max' {s : finset α} {h : (s.sort (≤)).length - 1 < (s.sort (≤)).length} :
(s.sort (≤)).nth_le ((s.sort (≤)).length - 1) h =
s.max' (by { rw length_sort at h, exact card_pos.1 (lt_of_le_of_lt bot_le h) }) :=
sorted_last_eq_max'_aux _ _ _
lemma max'_eq_sorted_last {s : finset α} {h : s.nonempty} :
s.max' h = (s.sort (≤)).nth_le ((s.sort (≤)).length - 1)
(by simpa using sub_lt (card_pos.mpr h) zero_lt_one) :=
(sorted_last_eq_max'_aux _ _ _).symm
/-- Given a finset `s` of cardinality `k` in a linear order `α`, the map `order_iso_of_fin s h`
is the increasing bijection between `fin k` and `s` as an `order_iso`. Here, `h` is a proof that
the cardinality of `s` is `k`. We use this instead of an iso `fin s.card ≃o s` to avoid
casting issues in further uses of this function. -/
def order_iso_of_fin (s : finset α) {k : ℕ} (h : s.card = k) : fin k ≃o (s : set α) :=
order_iso.trans (fin.cast ((length_sort (≤)).trans h).symm) $
(s.sort_sorted_lt.nth_le_iso _).trans $ order_iso.set_congr _ _ $
set.ext $ λ x, mem_sort _
/-- Given a finset `s` of cardinality `k` in a linear order `α`, the map `order_emb_of_fin s h` is
the increasing bijection between `fin k` and `s` as an order embedding into `α`. Here, `h` is a
proof that the cardinality of `s` is `k`. We use this instead of an embedding `fin s.card ↪o α` to
avoid casting issues in further uses of this function. -/
def order_emb_of_fin (s : finset α) {k : ℕ} (h : s.card = k) : fin k ↪o α :=
(order_iso_of_fin s h).to_order_embedding.trans (order_embedding.subtype _)
@[simp] lemma coe_order_iso_of_fin_apply (s : finset α) {k : ℕ} (h : s.card = k) (i : fin k) :
↑(order_iso_of_fin s h i) = order_emb_of_fin s h i :=
rfl
lemma order_iso_of_fin_symm_apply (s : finset α) {k : ℕ} (h : s.card = k) (x : (s : set α)) :
↑((s.order_iso_of_fin h).symm x) = (s.sort (≤)).index_of x :=
rfl
lemma order_emb_of_fin_apply (s : finset α) {k : ℕ} (h : s.card = k) (i : fin k) :
s.order_emb_of_fin h i = (s.sort (≤)).nth_le i (by { rw [length_sort, h], exact i.2 }) :=
rfl
@[simp] lemma order_emb_of_fin_mem (s : finset α) {k : ℕ} (h : s.card = k) (i : fin k) :
s.order_emb_of_fin h i ∈ s :=
(s.order_iso_of_fin h i).2
@[simp] lemma range_order_emb_of_fin (s : finset α) {k : ℕ} (h : s.card = k) :
set.range (s.order_emb_of_fin h) = s :=
by simp [order_emb_of_fin, set.range_comp coe (s.order_iso_of_fin h)]
/-- The bijection `order_emb_of_fin s h` sends `0` to the minimum of `s`. -/
lemma order_emb_of_fin_zero {s : finset α} {k : ℕ} (h : s.card = k) (hz : 0 < k) :
order_emb_of_fin s h ⟨0, hz⟩ = s.min' (card_pos.mp (h.symm ▸ hz)) :=
by simp only [order_emb_of_fin_apply, subtype.coe_mk, sorted_zero_eq_min']
/-- The bijection `order_emb_of_fin s h` sends `k-1` to the maximum of `s`. -/
lemma order_emb_of_fin_last {s : finset α} {k : ℕ} (h : s.card = k) (hz : 0 < k) :
order_emb_of_fin s h ⟨k-1, buffer.lt_aux_2 hz⟩ = s.max' (card_pos.mp (h.symm ▸ hz)) :=
by simp [order_emb_of_fin_apply, max'_eq_sorted_last, h]
/-- `order_emb_of_fin {a} h` sends any argument to `a`. -/
@[simp] lemma order_emb_of_fin_singleton (a : α) (i : fin 1) :
order_emb_of_fin {a} (card_singleton a) i = a :=
by rw [subsingleton.elim i ⟨0, zero_lt_one⟩, order_emb_of_fin_zero _ zero_lt_one, min'_singleton]
/-- Any increasing map `f` from `fin k` to a finset of cardinality `k` has to coincide with
the increasing bijection `order_emb_of_fin s h`. -/
lemma order_emb_of_fin_unique {s : finset α} {k : ℕ} (h : s.card = k) {f : fin k → α}
(hfs : ∀ x, f x ∈ s) (hmono : strict_mono f) : f = s.order_emb_of_fin h :=
begin
apply fin.strict_mono_unique hmono (s.order_emb_of_fin h).strict_mono,
rw [range_order_emb_of_fin, ← set.image_univ, ← coe_fin_range, ← coe_image, coe_inj],
refine eq_of_subset_of_card_le (λ x hx, _) _,
{ rcases mem_image.1 hx with ⟨x, hx, rfl⟩, exact hfs x },
{ rw [h, card_image_of_injective _ hmono.injective, fin_range_card] }
end
/-- An order embedding `f` from `fin k` to a finset of cardinality `k` has to coincide with
the increasing bijection `order_emb_of_fin s h`. -/
lemma order_emb_of_fin_unique' {s : finset α} {k : ℕ} (h : s.card = k) {f : fin k ↪o α}
(hfs : ∀ x, f x ∈ s) : f = s.order_emb_of_fin h :=
rel_embedding.ext $ function.funext_iff.1 $ order_emb_of_fin_unique h hfs f.strict_mono
/-- Two parametrizations `order_emb_of_fin` of the same set take the same value on `i` and `j` if
and only if `i = j`. Since they can be defined on a priori not defeq types `fin k` and `fin l`
(although necessarily `k = l`), the conclusion is rather written `(i : ℕ) = (j : ℕ)`. -/
@[simp] lemma order_emb_of_fin_eq_order_emb_of_fin_iff
{k l : ℕ} {s : finset α} {i : fin k} {j : fin l} {h : s.card = k} {h' : s.card = l} :
s.order_emb_of_fin h i = s.order_emb_of_fin h' j ↔ (i : ℕ) = (j : ℕ) :=
begin
substs k l,
exact (s.order_emb_of_fin rfl).eq_iff_eq.trans (fin.ext_iff _ _)
end
end sort_linear_order
instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩
end finset
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.