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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
729cc15c126ad3be39b556b936c67351d50cca3b | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/data/multiset/functor.lean | 4004dbdd7d1e426b81a03852376b66e41395004a | [
"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 | 4,239 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro, Johannes Hölzl, Simon Hudon, Kenny Lau
-/
import data.multiset.basic
import control.traversable.lemmas
import control.traversable.instances
/-!
# Functoriality of `multiset`.
-/
universes u
namespace multiset
open list
instance : functor multiset :=
{ map := @map }
@[simp] lemma fmap_def {α' β'} {s : multiset α'} (f : α' → β') : f <$> s = s.map f := rfl
instance : is_lawful_functor multiset :=
by refine { .. }; intros; simp
open is_lawful_traversable is_comm_applicative
variables {F : Type u → Type u} [applicative F] [is_comm_applicative F]
variables {α' β' : Type u} (f : α' → F β')
def traverse : multiset α' → F (multiset β') :=
quotient.lift (functor.map coe ∘ traversable.traverse f)
begin
introv p, unfold function.comp,
induction p,
case perm.nil { refl },
case perm.cons {
have : multiset.cons <$> f p_x <*> (coe <$> traverse f p_l₁) =
multiset.cons <$> f p_x <*> (coe <$> traverse f p_l₂),
{ rw [p_ih] },
simpa with functor_norm },
case perm.swap {
have : (λa b (l:list β'), (↑(a :: b :: l) : multiset β')) <$> f p_y <*> f p_x =
(λa b l, ↑(a :: b :: l)) <$> f p_x <*> f p_y,
{ rw [is_comm_applicative.commutative_map],
congr, funext a b l, simpa [flip] using perm.swap b a l },
simp [(∘), this] with functor_norm },
case perm.trans { simp [*] }
end
instance : monad multiset :=
{ pure := λ α x, x::0,
bind := @bind,
.. multiset.functor }
@[simp] lemma pure_def {α} : (pure : α → multiset α) = (λ x, x::0) := rfl
@[simp] lemma bind_def {α β} : (>>=) = @bind α β := rfl
instance : is_lawful_monad multiset :=
{ bind_pure_comp_eq_map := λ α β f s, multiset.induction_on s rfl $ λ a s ih, by simp,
pure_bind := λ α β x f, by simp,
bind_assoc := @bind_assoc }
open functor
open traversable is_lawful_traversable
@[simp]
lemma lift_beta {α β : Type*} (x : list α) (f : list α → β)
(h : ∀ a b : list α, a ≈ b → f a = f b) :
quotient.lift f h (x : multiset α) = f x :=
quotient.lift_beta _ _ _
@[simp]
lemma map_comp_coe {α β} (h : α → β) :
functor.map h ∘ coe = (coe ∘ functor.map h : list α → multiset β) :=
by funext; simp [functor.map]
lemma id_traverse {α : Type*} (x : multiset α) :
traverse id.mk x = x :=
quotient.induction_on x begin intro, simp [traverse], refl end
lemma comp_traverse {G H : Type* → Type*}
[applicative G] [applicative H]
[is_comm_applicative G] [is_comm_applicative H]
{α β γ : Type*}
(g : α → G β) (h : β → H γ) (x : multiset α) :
traverse (comp.mk ∘ functor.map h ∘ g) x =
comp.mk (functor.map (traverse h) (traverse g x)) :=
quotient.induction_on x
(by intro;
simp [traverse,comp_traverse] with functor_norm;
simp [(<$>),(∘)] with functor_norm)
lemma map_traverse {G : Type* → Type*}
[applicative G] [is_comm_applicative G]
{α β γ : Type*}
(g : α → G β) (h : β → γ)
(x : multiset α) :
functor.map (functor.map h) (traverse g x) =
traverse (functor.map h ∘ g) x :=
quotient.induction_on x
(by intro; simp [traverse] with functor_norm;
rw [comp_map,map_traverse])
lemma traverse_map {G : Type* → Type*}
[applicative G] [is_comm_applicative G]
{α β γ : Type*}
(g : α → β) (h : β → G γ)
(x : multiset α) :
traverse h (map g x) =
traverse (h ∘ g) x :=
quotient.induction_on x
(by intro; simp [traverse];
rw [← traversable.traverse_map h g];
[ refl, apply_instance ])
lemma naturality {G H : Type* → Type*}
[applicative G] [applicative H]
[is_comm_applicative G] [is_comm_applicative H]
(eta : applicative_transformation G H)
{α β : Type*} (f : α → G β) (x : multiset α) :
eta (traverse f x) = traverse (@eta _ ∘ f) x :=
quotient.induction_on x
(by intro; simp [traverse,is_lawful_traversable.naturality] with functor_norm)
end multiset
|
86750c83f0287ce8d2366d281223e717b3ce51c8 | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/group_theory/subgroup.lean | 7158096d3f42f28f3ea00d3fbf0a1830d29f9a17 | [
"Apache-2.0"
] | permissive | troyjlee/mathlib | e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5 | 45e7eb8447555247246e3fe91c87066506c14875 | refs/heads/master | 1,689,248,035,046 | 1,629,470,528,000 | 1,629,470,528,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 89,609 | lean | /-
Copyright (c) 2020 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying
-/
import group_theory.submonoid
import algebra.group.conj
import algebra.pointwise
import order.atoms
/-!
# Subgroups
This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled
form (unbundled subgroups are in `deprecated/subgroups.lean`).
We prove subgroups of a group form a complete lattice, and results about images and preimages of
subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms.
There are also theorems about the subgroups generated by an element or a subset of a group,
defined both inductively and as the infimum of the set of subgroups containing a given
element/subset.
Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration.
## Main definitions
Notation used here:
- `G N` are `group`s
- `A` is an `add_group`
- `H K` are `subgroup`s of `G` or `add_subgroup`s of `A`
- `x` is an element of type `G` or type `A`
- `f g : N →* G` are group homomorphisms
- `s k` are sets of elements of type `G`
Definitions in the file:
* `subgroup G` : the type of subgroups of a group `G`
* `add_subgroup A` : the type of subgroups of an additive group `A`
* `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice
* `subgroup.closure k` : the minimal subgroup that includes the set `k`
* `subgroup.subtype` : the natural group homomorphism from a subgroup of group `G` to `G`
* `subgroup.gi` : `closure` forms a Galois insertion with the coercion to set
* `subgroup.comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a
subgroup
* `subgroup.map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a
subgroup
* `subgroup.prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H × K`
is a subgroup of `G × N`
* `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup
* `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G`
such that `f x = 1`
* `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that
`f x = g x` form a subgroup of `G`
* `is_simple_group G` : a class indicating that a group has exactly two normal subgroups
## Implementation notes
Subgroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as
membership of a subgroup's underlying set.
## Tags
subgroup, subgroups
-/
open_locale big_operators pointwise
variables {G : Type*} [group G]
variables {A : Type*} [add_group A]
set_option old_structure_cmd true
/-- A subgroup of a group `G` is a subset containing 1, closed under multiplication
and closed under multiplicative inverse. -/
structure subgroup (G : Type*) [group G] extends submonoid G :=
(inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier)
/-- An additive subgroup of an additive group `G` is a subset containing 0, closed
under addition and additive inverse. -/
structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:=
(neg_mem' {x} : x ∈ carrier → -x ∈ carrier)
attribute [to_additive] subgroup
attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid
/-- Reinterpret a `subgroup` as a `submonoid`. -/
add_decl_doc subgroup.to_submonoid
/-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/
add_decl_doc add_subgroup.to_add_submonoid
namespace subgroup
@[to_additive]
instance : set_like (subgroup G) G :=
⟨subgroup.carrier, λ p q h, by cases p; cases q; congr'⟩
@[simp, to_additive]
lemma mem_carrier {s : subgroup G} {x : G} : x ∈ s.carrier ↔ x ∈ s := iff.rfl
/-- See Note [custom simps projection] -/
@[to_additive "See Note [custom simps projection]"]
def simps.coe (S : subgroup G) : set G := S
initialize_simps_projections subgroup (carrier → coe)
initialize_simps_projections add_subgroup (carrier → coe)
@[simp, to_additive]
lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl
@[simp, to_additive]
lemma mem_to_submonoid (K : subgroup G) (x : G) : x ∈ K.to_submonoid ↔ x ∈ K := iff.rfl
@[to_additive]
instance (K : subgroup G) [d : decidable_pred (∈ K)] [fintype G] : fintype K :=
show fintype {g : G // g ∈ K}, from infer_instance
@[to_additive]
theorem to_submonoid_injective :
function.injective (to_submonoid : subgroup G → submonoid G) :=
λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h)
@[simp, to_additive]
theorem to_submonoid_eq {p q : subgroup G} : p.to_submonoid = q.to_submonoid ↔ p = q :=
to_submonoid_injective.eq_iff
@[mono, to_additive] lemma to_submonoid_strict_mono :
strict_mono (to_submonoid : subgroup G → submonoid G) := λ _ _, id
@[mono, to_additive]
lemma to_submonoid_mono : monotone (to_submonoid : subgroup G → submonoid G) :=
to_submonoid_strict_mono.monotone
@[simp, to_additive]
lemma to_submonoid_le {p q : subgroup G} : p.to_submonoid ≤ q.to_submonoid ↔ p ≤ q :=
iff.rfl
end subgroup
/-!
### Conversion to/from `additive`/`multiplicative`
-/
section mul_add
/-- Supgroups of a group `G` are isomorphic to additive subgroups of `additive G`. -/
@[simps]
def subgroup.to_add_subgroup : subgroup G ≃o add_subgroup (additive G) :=
{ to_fun := λ S,
{ neg_mem' := S.inv_mem',
..S.to_submonoid.to_add_submonoid },
inv_fun := λ S,
{ inv_mem' := S.neg_mem',
..S.to_add_submonoid.to_submonoid' },
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl,
map_rel_iff' := λ a b, iff.rfl, }
/-- Additive subgroup of an additive group `additive G` are isomorphic to subgroup of `G`. -/
abbreviation add_subgroup.to_subgroup' : add_subgroup (additive G) ≃o subgroup G :=
subgroup.to_add_subgroup.symm
/-- Additive supgroups of an additive group `A` are isomorphic to subgroups of `multiplicative A`.
-/
@[simps]
def add_subgroup.to_subgroup : add_subgroup A ≃o subgroup (multiplicative A) :=
{ to_fun := λ S,
{ inv_mem' := S.neg_mem',
..S.to_add_submonoid.to_submonoid },
inv_fun := λ S,
{ neg_mem' := S.inv_mem',
..S.to_submonoid.to_add_submonoid' },
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl,
map_rel_iff' := λ a b, iff.rfl, }
/-- Subgroups of an additive group `multiplicative A` are isomorphic to additive subgroups of `A`.
-/
abbreviation subgroup.to_add_subgroup' : subgroup (multiplicative A) ≃o add_subgroup A :=
add_subgroup.to_subgroup.symm
end mul_add
namespace subgroup
variables (H K : subgroup G)
/-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional
equalities.-/
@[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one.
Useful to fix definitional equalities"]
protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G :=
{ carrier := s,
one_mem' := hs.symm ▸ K.one_mem',
mul_mem' := hs.symm ▸ K.mul_mem',
inv_mem' := hs.symm ▸ K.inv_mem' }
/-- Two subgroups are equal if they have the same elements. -/
@[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."]
theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K := set_like.ext h
attribute [ext] add_subgroup.ext
/-- A subgroup contains the group's 1. -/
@[to_additive "An `add_subgroup` contains the group's 0."]
theorem one_mem : (1 : G) ∈ H := H.one_mem'
/-- A subgroup is closed under multiplication. -/
@[to_additive "An `add_subgroup` is closed under addition."]
theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := λ hx hy, H.mul_mem' hx hy
/-- A subgroup is closed under inverse. -/
@[to_additive "An `add_subgroup` is closed under inverse."]
theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H := λ hx, H.inv_mem' hx
/-- A subgroup is closed under division. -/
@[to_additive "An `add_subgroup` is closed under subtraction."]
theorem div_mem {x y : G} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H :=
by simpa only [div_eq_mul_inv] using H.mul_mem' hx (H.inv_mem' hy)
@[simp, to_additive] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H :=
⟨λ h, inv_inv x ▸ H.inv_mem h, H.inv_mem⟩
@[to_additive] lemma div_mem_comm_iff {a b : G} : a / b ∈ H ↔ b / a ∈ H :=
by rw [← H.inv_mem_iff, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, inv_inv]
@[simp, to_additive]
theorem inv_coe_set : (H : set G)⁻¹ = H :=
by { ext, simp, }
@[simp, to_additive]
lemma exists_inv_mem_iff_exists_mem (K : subgroup G) {P : G → Prop} :
(∃ (x : G), x ∈ K ∧ P x⁻¹) ↔ ∃ x ∈ K, P x :=
by split; { rintros ⟨x, x_in, hx⟩, exact ⟨x⁻¹, inv_mem K x_in, by simp [hx]⟩ }
@[to_additive]
lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H :=
⟨λ hba, by simpa using H.mul_mem hba (H.inv_mem h), λ hb, H.mul_mem hb h⟩
@[to_additive]
lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H :=
⟨λ hab, by simpa using H.mul_mem (H.inv_mem h) hab, H.mul_mem h⟩
/-- Product of a list of elements in a subgroup is in the subgroup. -/
@[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."]
lemma list_prod_mem {l : list G} : (∀ x ∈ l, x ∈ K) → l.prod ∈ K :=
K.to_submonoid.list_prod_mem
/-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/
@[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group`
is in the `add_subgroup`."]
lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) :
(∀ a ∈ g, a ∈ K) → g.prod ∈ K := K.to_submonoid.multiset_prod_mem g
/-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the
subgroup. -/
@[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset`
is in the `add_subgroup`."]
lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G)
{ι : Type*} {t : finset ι} {f : ι → G} (h : ∀ c ∈ t, f c ∈ K) :
∏ c in t, f c ∈ K :=
K.to_submonoid.prod_mem h
lemma pow_mem {x : G} (hx : x ∈ K) : ∀ n : ℕ, x ^ n ∈ K := K.to_submonoid.pow_mem hx
lemma gpow_mem {x : G} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K
| (n : ℕ) := by { rw [gpow_coe_nat], exact pow_mem _ hx n }
| -[1+ n] := by { rw [gpow_neg_succ_of_nat], exact K.inv_mem (K.pow_mem hx n.succ) }
/-- Construct a subgroup from a nonempty set that is closed under division. -/
@[to_additive "Construct a subgroup from a nonempty set that is closed under subtraction"]
def of_div (s : set G) (hsn : s.nonempty) (hs : ∀ x y ∈ s, x * y⁻¹ ∈ s) : subgroup G :=
have one_mem : (1 : G) ∈ s, from let ⟨x, hx⟩ := hsn in by simpa using hs x x hx hx,
have inv_mem : ∀ x, x ∈ s → x⁻¹ ∈ s, from λ x hx, by simpa using hs 1 x one_mem hx,
{ carrier := s,
one_mem' := one_mem,
inv_mem' := inv_mem,
mul_mem' := λ x y hx hy, by simpa using hs x y⁻¹ hx (inv_mem y hy) }
/-- A subgroup of a group inherits a multiplication. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits an addition."]
instance has_mul : has_mul H := H.to_submonoid.has_mul
/-- A subgroup of a group inherits a 1. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits a zero."]
instance has_one : has_one H := H.to_submonoid.has_one
/-- A subgroup of a group inherits an inverse. -/
@[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."]
instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, H.inv_mem a.2⟩⟩
/-- A subgroup of a group inherits a division -/
@[to_additive "An `add_subgroup` of an `add_group` inherits a subtraction."]
instance has_div : has_div H := ⟨λ a b, ⟨a / b, H.div_mem a.2 b.2⟩⟩
@[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (↑(x * y) : G) = ↑x * ↑y := rfl
@[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl
@[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : G) := rfl
@[simp, norm_cast, to_additive] lemma coe_div (x y : H) : (↑(x / y) : G) = ↑x / ↑y := rfl
@[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x ∈ H) : ((⟨x, hx⟩ : H) : G) = x := rfl
attribute [norm_cast] add_subgroup.coe_add add_subgroup.coe_zero
add_subgroup.coe_neg add_subgroup.coe_mk
/-- A subgroup of a group inherits a group structure. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits an `add_group` structure."]
instance to_group {G : Type*} [group G] (H : subgroup G) : group H :=
subtype.coe_injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
/-- A subgroup of a `comm_group` is a `comm_group`. -/
@[to_additive "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."]
instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H :=
subtype.coe_injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
/-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/
@[to_additive "An `add_subgroup` of an `add_ordered_comm_group` is an `add_ordered_comm_group`."]
instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] (H : subgroup G) :
ordered_comm_group H :=
subtype.coe_injective.ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
/-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/
@[to_additive "An `add_subgroup` of a `linear_ordered_add_comm_group` is a
`linear_ordered_add_comm_group`."]
instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G]
(H : subgroup G) : linear_ordered_comm_group H :=
subtype.coe_injective.linear_ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
/-- The natural group hom from a subgroup of group `G` to `G`. -/
@[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."]
def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩
@[simp, to_additive] theorem coe_subtype : ⇑H.subtype = coe := rfl
@[simp, norm_cast] lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n :=
coe_subtype H ▸ monoid_hom.map_pow _ _ _
@[simp, norm_cast] lemma coe_gpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n :=
coe_subtype H ▸ monoid_hom.map_gpow _ _ _
/-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/
@[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."]
def inclusion {H K : subgroup G} (h : H ≤ K) : H →* K :=
monoid_hom.mk' (λ x, ⟨x, h x.prop⟩) (λ ⟨a, ha⟩ ⟨b, hb⟩, rfl)
@[simp, to_additive]
lemma coe_inclusion {H K : subgroup G} {h : H ≤ K} (a : H) : (inclusion h a : G) = a :=
by { cases a, simp only [inclusion, coe_mk, monoid_hom.mk'_apply] }
@[simp, to_additive]
lemma subtype_comp_inclusion {H K : subgroup G} (hH : H ≤ K) :
K.subtype.comp (inclusion hH) = H.subtype :=
by { ext, simp }
/-- The subgroup `G` of the group `G`. -/
@[to_additive "The `add_subgroup G` of the `add_group G`."]
instance : has_top (subgroup G) :=
⟨{ inv_mem' := λ _ _, set.mem_univ _ , .. (⊤ : submonoid G) }⟩
/-- The trivial subgroup `{1}` of an group `G`. -/
@[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."]
instance : has_bot (subgroup G) :=
⟨{ inv_mem' := λ _, by simp *, .. (⊥ : submonoid G) }⟩
@[to_additive]
instance : inhabited (subgroup G) := ⟨⊥⟩
@[simp, to_additive] lemma mem_bot {x : G} : x ∈ (⊥ : subgroup G) ↔ x = 1 := iff.rfl
@[simp, to_additive] lemma mem_top (x : G) : x ∈ (⊤ : subgroup G) := set.mem_univ x
@[simp, to_additive] lemma coe_top : ((⊤ : subgroup G) : set G) = set.univ := rfl
@[simp, to_additive] lemma coe_bot : ((⊥ : subgroup G) : set G) = {1} := rfl
@[to_additive] lemma eq_bot_iff_forall : H = ⊥ ↔ ∀ x ∈ H, x = (1 : G) :=
begin
rw set_like.ext'_iff,
simp only [coe_bot, set.eq_singleton_iff_unique_mem, set_like.mem_coe, H.one_mem, true_and],
end
@[to_additive] lemma eq_bot_of_subsingleton [subsingleton H] : H = ⊥ :=
begin
rw subgroup.eq_bot_iff_forall,
intros y hy,
rw [← subgroup.coe_mk H y hy, subsingleton.elim (⟨y, hy⟩ : H) 1, subgroup.coe_one],
end
@[to_additive] instance fintype_bot : fintype (⊥ : subgroup G) := ⟨{1},
by {rintro ⟨x, ⟨hx⟩⟩, exact finset.mem_singleton_self _}⟩
/- curly brackets `{}` are used here instead of instance brackets `[]` because
the instance in a goal is often not the same as the one inferred by type class inference. -/
@[simp, to_additive] lemma card_bot {_ : fintype ↥(⊥ : subgroup G)} :
fintype.card (⊥ : subgroup G) = 1 :=
fintype.card_eq_one_iff.2
⟨⟨(1 : G), set.mem_singleton 1⟩, λ ⟨y, hy⟩, subtype.eq $ subgroup.mem_bot.1 hy⟩
@[to_additive] lemma eq_top_of_card_eq [fintype H] [fintype G]
(h : fintype.card H = fintype.card G) : H = ⊤ :=
begin
haveI : fintype (H : set G) := ‹fintype H›,
rw [set_like.ext'_iff, coe_top, ← finset.coe_univ, ← (H : set G).coe_to_finset, finset.coe_inj,
← finset.card_eq_iff_eq_univ, ← h, set.to_finset_card],
congr
end
@[to_additive] lemma eq_top_of_le_card [fintype H] [fintype G]
(h : fintype.card G ≤ fintype.card H) : H = ⊤ :=
eq_top_of_card_eq H (le_antisymm (fintype.card_le_of_injective coe subtype.coe_injective) h)
@[to_additive] lemma eq_bot_of_card_le [fintype H] (h : fintype.card H ≤ 1) : H = ⊥ :=
let _ := fintype.card_le_one_iff_subsingleton.mp h in by exactI eq_bot_of_subsingleton H
@[to_additive] lemma eq_bot_of_card_eq [fintype H] (h : fintype.card H = 1) : H = ⊥ :=
H.eq_bot_of_card_le (le_of_eq h)
@[to_additive] lemma nontrivial_iff_exists_ne_one (H : subgroup G) :
nontrivial H ↔ ∃ x ∈ H, x ≠ (1:G) :=
subtype.nontrivial_iff_exists_ne (λ x, x ∈ H) (1 : H)
/-- A subgroup is either the trivial subgroup or nontrivial. -/
@[to_additive] lemma bot_or_nontrivial (H : subgroup G) : H = ⊥ ∨ nontrivial H :=
begin
classical,
by_cases h : ∀ x ∈ H, x = (1 : G),
{ left,
exact H.eq_bot_iff_forall.mpr h },
{ right,
push_neg at h,
simpa [nontrivial_iff_exists_ne_one] using h },
end
/-- A subgroup is either the trivial subgroup or contains a nonzero element. -/
@[to_additive] lemma bot_or_exists_ne_one (H : subgroup G) : H = ⊥ ∨ ∃ x ∈ H, x ≠ (1:G) :=
begin
convert H.bot_or_nontrivial,
rw nontrivial_iff_exists_ne_one
end
@[to_additive] lemma card_le_one_iff_eq_bot [fintype H] : fintype.card H ≤ 1 ↔ H = ⊥ :=
⟨λ h, (eq_bot_iff_forall _).2
(λ x hx, by simpa [subtype.ext_iff] using fintype.card_le_one_iff.1 h ⟨x, hx⟩ 1),
λ h, by simp [h]⟩
/-- The inf of two subgroups is their intersection. -/
@[to_additive "The inf of two `add_subgroups`s is their intersection."]
instance : has_inf (subgroup G) :=
⟨λ H₁ H₂,
{ inv_mem' := λ _ ⟨hx, hx'⟩, ⟨H₁.inv_mem hx, H₂.inv_mem hx'⟩,
.. H₁.to_submonoid ⊓ H₂.to_submonoid }⟩
@[simp, to_additive]
lemma coe_inf (p p' : subgroup G) : ((p ⊓ p' : subgroup G) : set G) = p ∩ p' := rfl
@[simp, to_additive]
lemma mem_inf {p p' : subgroup G} {x : G} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl
@[to_additive]
instance : has_Inf (subgroup G) :=
⟨λ s,
{ inv_mem' := λ x hx, set.mem_bInter $ λ i h, i.inv_mem (by apply set.mem_bInter_iff.1 hx i h),
.. (⨅ S ∈ s, subgroup.to_submonoid S).copy (⋂ S ∈ s, ↑S) (by simp) }⟩
@[simp, to_additive]
lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = ⋂ s ∈ H, ↑s := rfl
attribute [norm_cast] coe_Inf add_subgroup.coe_Inf
@[simp, to_additive]
lemma mem_Inf {S : set (subgroup G)} {x : G} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff
@[to_additive]
lemma mem_infi {ι : Sort*} {S : ι → subgroup G} {x : G} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i :=
by simp only [infi, mem_Inf, set.forall_range_iff]
@[simp, to_additive]
lemma coe_infi {ι : Sort*} {S : ι → subgroup G} : (↑(⨅ i, S i) : set G) = ⋂ i, S i :=
by simp only [infi, coe_Inf, set.bInter_range]
attribute [norm_cast] coe_infi add_subgroup.coe_infi
/-- Subgroups of a group form a complete lattice. -/
@[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."]
instance : complete_lattice (subgroup G) :=
{ bot := (⊥),
bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem,
top := (⊤),
le_top := λ S x hx, mem_top x,
inf := (⊓),
le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩,
inf_le_left := λ a b x, and.left,
inf_le_right := λ a b x, and.right,
.. complete_lattice_of_Inf (subgroup G) $ λ s, is_glb.of_image
(λ H K, show (H : set G) ≤ K ↔ H ≤ K, from set_like.coe_subset_coe) is_glb_binfi }
@[to_additive]
lemma mem_sup_left {S T : subgroup G} : ∀ {x : G}, x ∈ S → x ∈ S ⊔ T :=
show S ≤ S ⊔ T, from le_sup_left
@[to_additive]
lemma mem_sup_right {S T : subgroup G} : ∀ {x : G}, x ∈ T → x ∈ S ⊔ T :=
show T ≤ S ⊔ T, from le_sup_right
@[to_additive]
lemma mem_supr_of_mem {ι : Type*} {S : ι → subgroup G} (i : ι) :
∀ {x : G}, x ∈ S i → x ∈ supr S :=
show S i ≤ supr S, from le_supr _ _
@[to_additive]
lemma mem_Sup_of_mem {S : set (subgroup G)} {s : subgroup G}
(hs : s ∈ S) : ∀ {x : G}, x ∈ s → x ∈ Sup S :=
show s ≤ Sup S, from le_Sup hs
@[simp, to_additive]
lemma subsingleton_iff : subsingleton (subgroup G) ↔ subsingleton G :=
⟨ λ h, by exactI ⟨λ x y,
have ∀ i : G, i = 1 := λ i, mem_bot.mp $ subsingleton.elim (⊤ : subgroup G) ⊥ ▸ mem_top i,
(this x).trans (this y).symm⟩,
λ h, by exactI ⟨λ x y, subgroup.ext $ λ i, subsingleton.elim 1 i ▸ by simp [subgroup.one_mem]⟩⟩
@[simp, to_additive]
lemma nontrivial_iff : nontrivial (subgroup G) ↔ nontrivial G :=
not_iff_not.mp (
(not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans
not_nontrivial_iff_subsingleton.symm)
@[to_additive]
instance [subsingleton G] : unique (subgroup G) :=
⟨⟨⊥⟩, λ a, @subsingleton.elim _ (subsingleton_iff.mpr ‹_›) a _⟩
@[to_additive]
instance [nontrivial G] : nontrivial (subgroup G) := nontrivial_iff.mpr ‹_›
@[to_additive] lemma eq_top_iff' : H = ⊤ ↔ ∀ x : G, x ∈ H :=
eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩
/-- The `subgroup` generated by a set. -/
@[to_additive "The `add_subgroup` generated by a set"]
def closure (k : set G) : subgroup G := Inf {K | k ⊆ K}
variable {k : set G}
@[to_additive]
lemma mem_closure {x : G} : x ∈ closure k ↔ ∀ K : subgroup G, k ⊆ K → x ∈ K :=
mem_Inf
/-- The subgroup generated by a set includes the set. -/
@[simp, to_additive "The `add_subgroup` generated by a set includes the set."]
lemma subset_closure : k ⊆ closure k := λ x hx, mem_closure.2 $ λ K hK, hK hx
open set
/-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/
@[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"]
lemma closure_le : closure k ≤ K ↔ k ⊆ K :=
⟨subset.trans subset_closure, λ h, Inf_le h⟩
@[to_additive]
lemma closure_eq_of_le (h₁ : k ⊆ K) (h₂ : K ≤ closure k) : closure k = K :=
le_antisymm ((closure_le $ K).2 h₁) h₂
/-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and
is preserved under multiplication and inverse, then `p` holds for all elements of the closure
of `k`. -/
@[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all
elements of `k`, and is preserved under addition and inverses, then `p` holds for all elements
of the additive closure of `k`."]
lemma closure_induction {p : G → Prop} {x} (h : x ∈ closure k)
(Hk : ∀ x ∈ k, p x) (H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(Hinv : ∀ x, p x → p x⁻¹) : p x :=
(@closure_le _ _ ⟨p, H1, Hmul, Hinv⟩ _).2 Hk h
attribute [elab_as_eliminator] subgroup.closure_induction add_subgroup.closure_induction
/-- An induction principle on elements of the subtype `subgroup.closure`.
If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse,
then `p` holds for all elements `x : closure k`.
The difference with `subgroup.closure_induction` is that this acts on the subtype.
-/
@[to_additive "An induction principle on elements of the subtype `add_subgroup.closure`.
If `p` holds for `0` and all elements of `k`, and is preserved under addition and negation,
then `p` holds for all elements `x : closure k`.
The difference with `add_subgroup.closure_induction` is that this acts on the subtype."]
lemma closure_induction' (k : set G) {p : closure k → Prop}
(Hk : ∀ x (h : x ∈ k), p ⟨x, subset_closure h⟩)
(H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(Hinv : ∀ x, p x → p x⁻¹)
(x : closure k) :
p x :=
subtype.rec_on x $ λ x hx, begin
refine exists.elim _ (λ (hx : x ∈ closure k) (hc : p ⟨x, hx⟩), hc),
exact closure_induction hx
(λ x hx, ⟨subset_closure hx, Hk x hx⟩)
⟨one_mem _, H1⟩
(λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy,
⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩)
(λ x hx, exists.elim hx $ λ hx' hx, ⟨inv_mem _ hx', Hinv _ hx⟩),
end
attribute [elab_as_eliminator] subgroup.closure_induction' add_subgroup.closure_induction'
variable (G)
/-- `closure` forms a Galois insertion with the coercion to set. -/
@[to_additive "`closure` forms a Galois insertion with the coercion to set."]
protected def gi : galois_insertion (@closure G _) coe :=
{ choice := λ s _, closure s,
gc := λ s t, @closure_le _ _ t s,
le_l_u := λ s, subset_closure,
choice_eq := λ s h, rfl }
variable {G}
/-- Subgroup closure of a set is monotone in its argument: if `h ⊆ k`,
then `closure h ≤ closure k`. -/
@[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h ⊆ k`,
then `closure h ≤ closure k`"]
lemma closure_mono ⦃h k : set G⦄ (h' : h ⊆ k) : closure h ≤ closure k :=
(subgroup.gi G).gc.monotone_l h'
/-- Closure of a subgroup `K` equals `K`. -/
@[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"]
lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K
@[simp, to_additive] lemma closure_empty : closure (∅ : set G) = ⊥ :=
(subgroup.gi G).gc.l_bot
@[simp, to_additive] lemma closure_univ : closure (univ : set G) = ⊤ :=
@coe_top G _ ▸ closure_eq ⊤
@[to_additive]
lemma closure_union (s t : set G) : closure (s ∪ t) = closure s ⊔ closure t :=
(subgroup.gi G).gc.l_sup
@[to_additive]
lemma closure_Union {ι} (s : ι → set G) : closure (⋃ i, s i) = ⨆ i, closure (s i) :=
(subgroup.gi G).gc.l_supr
@[to_additive]
lemma closure_eq_bot_iff (G : Type*) [group G] (S : set G) :
closure S = ⊥ ↔ S ⊆ {1} :=
by { rw [← le_bot_iff], exact closure_le _}
/-- The subgroup generated by an element of a group equals the set of integer number powers of
the element. -/
lemma mem_closure_singleton {x y : G} : y ∈ closure ({x} : set G) ↔ ∃ n : ℤ, x ^ n = y :=
begin
refine ⟨λ hy, closure_induction hy _ _ _ _,
λ ⟨n, hn⟩, hn ▸ gpow_mem _ (subset_closure $ mem_singleton x) n⟩,
{ intros y hy,
rw [eq_of_mem_singleton hy],
exact ⟨1, gpow_one x⟩ },
{ exact ⟨0, gpow_zero x⟩ },
{ rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩,
exact ⟨n + m, gpow_add x n m⟩ },
rintros _ ⟨n, rfl⟩,
exact ⟨-n, gpow_neg x n⟩
end
lemma closure_singleton_one : closure ({1} : set G) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
@[simp, to_additive] lemma inv_subset_closure (S : set G) : S⁻¹ ⊆ closure S :=
begin
intros s hs,
rw [set_like.mem_coe, ←subgroup.inv_mem_iff],
exact subset_closure (mem_inv.mp hs),
end
@[simp, to_additive] lemma closure_inv (S : set G) : closure S⁻¹ = closure S :=
begin
refine le_antisymm ((subgroup.closure_le _).2 _) ((subgroup.closure_le _).2 _),
{ exact inv_subset_closure S },
{ simpa only [set.inv_inv] using inv_subset_closure S⁻¹ },
end
@[to_additive]
lemma closure_to_submonoid (S : set G) :
(closure S).to_submonoid = submonoid.closure (S ∪ S⁻¹) :=
begin
refine le_antisymm _ (submonoid.closure_le.2 _),
{ intros x hx,
refine closure_induction hx (λ x hx, submonoid.closure_mono (subset_union_left S S⁻¹)
(submonoid.subset_closure hx)) (submonoid.one_mem _) (λ x y hx hy, submonoid.mul_mem _ hx hy)
(λ x hx, _),
rwa [←submonoid.mem_closure_inv, set.union_inv, set.inv_inv, set.union_comm] },
{ simp only [true_and, coe_to_submonoid, union_subset_iff, subset_closure, inv_subset_closure] }
end
/-- An induction principle for closure membership. If `p` holds for `1` and all elements of
`k` and their inverse, and is preserved under multiplication, then `p` holds for all elements of
the closure of `k`. -/
@[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all
elements of `k` and their negation, and is preserved under addition, then `p` holds for all
elements of the additive closure of `k`."]
lemma closure_induction'' {p : G → Prop} {x} (h : x ∈ closure k)
(Hk : ∀ x ∈ k, p x) (Hk_inv : ∀ x ∈ k, p x⁻¹) (H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y)) : p x :=
begin
rw [← mem_to_submonoid, closure_to_submonoid k] at h,
refine submonoid.closure_induction h (λ x hx, _) H1 (λ x y hx hy, Hmul x y hx hy),
{ rw [mem_union, mem_inv] at hx,
cases hx with mem invmem,
{ exact Hk x mem },
{ rw [← inv_inv x],
exact Hk_inv _ invmem } },
end
@[to_additive]
lemma mem_supr_of_directed {ι} [hι : nonempty ι] {K : ι → subgroup G} (hK : directed (≤) K)
{x : G} :
x ∈ (supr K : subgroup G) ↔ ∃ i, x ∈ K i :=
begin
refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr K i) hi⟩,
suffices : x ∈ closure (⋃ i, (K i : set G)) → ∃ i, x ∈ K i,
by simpa only [closure_Union, closure_eq (K _)] using this,
refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _ _),
{ exact hι.elim (λ i, ⟨i, (K i).one_mem⟩) },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
rcases hK i j with ⟨k, hki, hkj⟩,
exact ⟨k, (K k).mul_mem (hki hi) (hkj hj)⟩ },
rintros _ ⟨i, hi⟩, exact ⟨i, inv_mem (K i) hi⟩
end
@[to_additive]
lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → subgroup G} (hS : directed (≤) S) :
((⨆ i, S i : subgroup G) : set G) = ⋃ i, ↑(S i) :=
set.ext $ λ x, by simp [mem_supr_of_directed hS]
@[to_additive]
lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty)
(hK : directed_on (≤) K) {x : G} :
x ∈ Sup K ↔ ∃ s ∈ K, x ∈ s :=
begin
haveI : nonempty K := Kne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hK.directed_coe, set_coe.exists, subtype.coe_mk]
end
variables {N : Type*} [group N] {P : Type*} [group P]
/-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/
@[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism
is an `add_subgroup`."]
def comap {N : Type*} [group N] (f : G →* N)
(H : subgroup N) : subgroup G :=
{ carrier := (f ⁻¹' H),
inv_mem' := λ a ha,
show f a⁻¹ ∈ H, by rw f.map_inv; exact H.inv_mem ha,
.. H.to_submonoid.comap f }
@[simp, to_additive]
lemma coe_comap (K : subgroup N) (f : G →* N) : (K.comap f : set G) = f ⁻¹' K := rfl
@[simp, to_additive]
lemma mem_comap {K : subgroup N} {f : G →* N} {x : G} : x ∈ K.comap f ↔ f x ∈ K := iff.rfl
@[to_additive]
lemma comap_mono {f : G →* N} {K K' : subgroup N} : K ≤ K' → comap f K ≤ comap f K' :=
preimage_mono
@[to_additive]
lemma comap_comap (K : subgroup P) (g : N →* P) (f : G →* N) :
(K.comap g).comap f = K.comap (g.comp f) :=
rfl
/-- The image of a subgroup along a monoid homomorphism is a subgroup. -/
@[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism
is an `add_subgroup`."]
def map (f : G →* N) (H : subgroup G) : subgroup N :=
{ carrier := (f '' H),
inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, H.inv_mem hx, f.map_inv x⟩ },
.. H.to_submonoid.map f }
@[simp, to_additive]
lemma coe_map (f : G →* N) (K : subgroup G) :
(K.map f : set N) = f '' K := rfl
@[simp, to_additive]
lemma mem_map {f : G →* N} {K : subgroup G} {y : N} :
y ∈ K.map f ↔ ∃ x ∈ K, f x = y :=
mem_image_iff_bex
@[to_additive]
lemma mem_map_of_mem (f : G →* N) {K : subgroup G} {x : G} (hx : x ∈ K) : f x ∈ K.map f :=
mem_image_of_mem f hx
@[to_additive]
lemma apply_coe_mem_map (f : G →* N) (K : subgroup G) (x : K) : f x ∈ K.map f :=
mem_map_of_mem f x.prop
@[to_additive]
lemma map_mono {f : G →* N} {K K' : subgroup G} : K ≤ K' → map f K ≤ map f K' :=
image_subset _
@[to_additive]
lemma map_map (g : N →* P) (f : G →* N) : (K.map f).map g = K.map (g.comp f) :=
set_like.coe_injective $ image_image _ _ _
@[to_additive]
lemma mem_map_equiv {f : G ≃* N} {K : subgroup G} {x : N} :
x ∈ K.map f.to_monoid_hom ↔ f.symm x ∈ K :=
@set.mem_image_equiv _ _ ↑K f.to_equiv x
@[to_additive]
lemma map_equiv_eq_comap_symm (f : G ≃* N) (K : subgroup G) :
K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom :=
set_like.coe_injective (f.to_equiv.image_eq_preimage K)
@[to_additive]
lemma comap_equiv_eq_map_symm (f : N ≃* G) (K : subgroup G) :
K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom :=
(map_equiv_eq_comap_symm f.symm K).symm
@[to_additive]
lemma map_le_iff_le_comap {f : G →* N} {K : subgroup G} {H : subgroup N} :
K.map f ≤ H ↔ K ≤ H.comap f :=
image_subset_iff
@[to_additive]
lemma gc_map_comap (f : G →* N) : galois_connection (map f) (comap f) :=
λ _ _, map_le_iff_le_comap
@[to_additive]
lemma map_sup (H K : subgroup G) (f : G →* N) : (H ⊔ K).map f = H.map f ⊔ K.map f :=
(gc_map_comap f).l_sup
@[to_additive]
lemma map_supr {ι : Sort*} (f : G →* N) (s : ι → subgroup G) :
(supr s).map f = ⨆ i, (s i).map f :=
(gc_map_comap f).l_supr
@[to_additive] lemma comap_sup_comap_le
(H K : subgroup N) (f : G →* N) : comap f H ⊔ comap f K ≤ comap f (H ⊔ K) :=
monotone.le_map_sup (λ _ _, comap_mono) H K
@[to_additive] lemma supr_comap_le {ι : Sort*} (f : G →* N) (s : ι → subgroup N) :
(⨆ i, (s i).comap f) ≤ (supr s).comap f :=
monotone.le_map_supr (λ _ _, comap_mono)
@[to_additive]
lemma comap_inf (H K : subgroup N) (f : G →* N) : (H ⊓ K).comap f = H.comap f ⊓ K.comap f :=
(gc_map_comap f).u_inf
@[to_additive]
lemma comap_infi {ι : Sort*} (f : G →* N) (s : ι → subgroup N) :
(infi s).comap f = ⨅ i, (s i).comap f :=
(gc_map_comap f).u_infi
@[to_additive] lemma map_inf_le (H K : subgroup G) (f : G →* N) :
map f (H ⊓ K) ≤ map f H ⊓ map f K :=
le_inf (map_mono inf_le_left) (map_mono inf_le_right)
@[to_additive] lemma map_inf_eq (H K : subgroup G) (f : G →* N) (hf : function.injective f) :
map f (H ⊓ K) = map f H ⊓ map f K :=
begin
rw ← set_like.coe_set_eq,
simp [set.image_inter hf],
end
@[simp, to_additive] lemma map_bot (f : G →* N) : (⊥ : subgroup G).map f = ⊥ :=
(gc_map_comap f).l_bot
@[simp, to_additive] lemma comap_top (f : G →* N) : (⊤ : subgroup N).comap f = ⊤ :=
(gc_map_comap f).u_top
@[simp, to_additive]
lemma comap_subtype_inf_left {H K : subgroup G} : comap H.subtype (H ⊓ K) = comap H.subtype K :=
ext $ λ x, and_iff_right_of_imp (λ _, x.prop)
@[simp, to_additive]
lemma comap_subtype_inf_right {H K : subgroup G} : comap K.subtype (H ⊓ K) = comap K.subtype H :=
ext $ λ x, and_iff_left_of_imp (λ _, x.prop)
/-- For any subgroups `H` and `K`, view `H ⊓ K` as a subgroup of `K`. -/
@[to_additive "For any subgroups `H` and `K`, view `H ⊓ K` as a subgroup of `K`."]
def subgroup_of (H K : subgroup G) : subgroup K := H.comap K.subtype
@[to_additive] lemma coe_subgroup_of (H K : subgroup G) :
(H.subgroup_of K : set K) = K.subtype ⁻¹' H := rfl
@[to_additive] lemma mem_subgroup_of {H K : subgroup G} {h : K} :
h ∈ H.subgroup_of K ↔ (h : G) ∈ H :=
iff.rfl
@[to_additive] lemma subgroup_of_map_subtype (H K : subgroup G) :
(H.subgroup_of K).map K.subtype = H ⊓ K := set_like.ext'
begin
convert set.image_preimage_eq_inter_range,
simp only [subtype.range_coe_subtype, coe_subtype, coe_inf],
refl,
end
/-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H × K` as a subgroup of `G × N`. -/
@[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H × K`
as an `add_subgroup` of `A × B`."]
def prod (H : subgroup G) (K : subgroup N) : subgroup (G × N) :=
{ inv_mem' := λ _ hx, ⟨H.inv_mem' hx.1, K.inv_mem' hx.2⟩,
.. submonoid.prod H.to_submonoid K.to_submonoid}
@[to_additive coe_prod]
lemma coe_prod (H : subgroup G) (K : subgroup N) :
(H.prod K : set (G × N)) = (H : set G).prod (K : set N) := rfl
@[to_additive mem_prod]
lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G × N} :
p ∈ H.prod K ↔ p.1 ∈ H ∧ p.2 ∈ K := iff.rfl
@[to_additive prod_mono]
lemma prod_mono : ((≤) ⇒ (≤) ⇒ (≤)) (@prod G _ N _) (@prod G _ N _) :=
λ s s' hs t t' ht, set.prod_mono hs ht
@[to_additive prod_mono_right]
lemma prod_mono_right (K : subgroup G) : monotone (λ t : subgroup N, K.prod t) :=
prod_mono (le_refl K)
@[to_additive prod_mono_left]
lemma prod_mono_left (H : subgroup N) : monotone (λ K : subgroup G, K.prod H) :=
λ s₁ s₂ hs, prod_mono hs (le_refl H)
@[to_additive prod_top]
lemma prod_top (K : subgroup G) :
K.prod (⊤ : subgroup N) = K.comap (monoid_hom.fst G N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst]
@[to_additive top_prod]
lemma top_prod (H : subgroup N) :
(⊤ : subgroup G).prod H = H.comap (monoid_hom.snd G N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd]
@[simp, to_additive top_prod_top]
lemma top_prod_top : (⊤ : subgroup G).prod (⊤ : subgroup N) = ⊤ :=
(top_prod _).trans $ comap_top _
@[to_additive] lemma bot_prod_bot : (⊥ : subgroup G).prod (⊥ : subgroup N) = ⊥ :=
set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk]
/-- Product of subgroups is isomorphic to their product as groups. -/
@[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product
as additive groups"]
def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K ≃* H × K :=
{ map_mul' := λ x y, rfl, .. equiv.set.prod ↑H ↑K }
/-- A subgroup is normal if whenever `n ∈ H`, then `g * n * g⁻¹ ∈ H` for every `g : G` -/
structure normal : Prop :=
(conj_mem : ∀ n, n ∈ H → ∀ g : G, g * n * g⁻¹ ∈ H)
attribute [class] normal
end subgroup
namespace add_subgroup
/-- An add_subgroup is normal if whenever `n ∈ H`, then `g + n - g ∈ H` for every `g : G` -/
structure normal (H : add_subgroup A) : Prop :=
(conj_mem [] : ∀ n, n ∈ H → ∀ g : A, g + n + -g ∈ H)
attribute [to_additive add_subgroup.normal] subgroup.normal
attribute [class] normal
end add_subgroup
namespace subgroup
variables {H K : subgroup G}
@[priority 100, to_additive]
instance normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal :=
⟨by simp [mul_comm, mul_left_comm]⟩
namespace normal
variable (nH : H.normal)
@[to_additive] lemma mem_comm {a b : G} (h : a * b ∈ H) : b * a ∈ H :=
have a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ H, from nH.conj_mem (a * b) h a⁻¹, by simpa
@[to_additive] lemma mem_comm_iff {a b : G} : a * b ∈ H ↔ b * a ∈ H :=
⟨nH.mem_comm, nH.mem_comm⟩
end normal
@[priority 100, to_additive]
instance bot_normal : normal (⊥ : subgroup G) := ⟨by simp⟩
@[priority 100, to_additive]
instance top_normal : normal (⊤ : subgroup G) := ⟨λ _ _, mem_top⟩
variable (G)
/-- The center of a group `G` is the set of elements that commute with everything in `G` -/
@[to_additive "The center of a group `G` is the set of elements that commute with everything in
`G`"]
def center : subgroup G :=
{ carrier := {z | ∀ g, g * z = z * g},
one_mem' := by simp,
mul_mem' := λ a b (ha : ∀ g, g * a = a * g) (hb : ∀ g, g * b = b * g) g,
by assoc_rw [ha, hb g],
inv_mem' := λ a (ha : ∀ g, g * a = a * g) g,
by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] }
variable {G}
@[to_additive] lemma mem_center_iff {z : G} : z ∈ center G ↔ ∀ g, g * z = z * g := iff.rfl
@[priority 100, to_additive]
instance center_normal : (center G).normal :=
⟨begin
assume n hn g h,
assoc_rw [hn (h * g), hn g],
simp
end⟩
variables {G} (H)
/-- The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal. -/
@[to_additive "The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal."]
def normalizer : subgroup G :=
{ carrier := {g : G | ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H},
one_mem' := by simp,
mul_mem' := λ a b (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) (hb : ∀ n, n ∈ H ↔ b * n * b⁻¹ ∈ H) n,
by { rw [hb, ha], simp [mul_assoc] },
inv_mem' := λ a (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) n,
by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } }
-- variant for sets.
-- TODO should this replace `normalizer`?
/-- The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g*S*g⁻¹=S` -/
@[to_additive "The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy
`g+S-g=S`."]
def set_normalizer (S : set G) : subgroup G :=
{ carrier := {g : G | ∀ n, n ∈ S ↔ g * n * g⁻¹ ∈ S},
one_mem' := by simp,
mul_mem' := λ a b (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) (hb : ∀ n, n ∈ S ↔ b * n * b⁻¹ ∈ S) n,
by { rw [hb, ha], simp [mul_assoc] },
inv_mem' := λ a (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) n,
by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } }
lemma mem_normalizer_fintype {S : set G} [fintype S] {x : G}
(h : ∀ n, n ∈ S → x * n * x⁻¹ ∈ S) : x ∈ subgroup.set_normalizer S :=
by haveI := classical.prop_decidable;
haveI := set.fintype_image S (λ n, x * n * x⁻¹); exact
λ n, ⟨h n, λ h₁,
have heq : (λ n, x * n * x⁻¹) '' S = S := set.eq_of_subset_of_card_le
(λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective S conj_injective),
have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' S := heq.symm ▸ h₁,
let ⟨y, hy⟩ := this in conj_injective hy.2 ▸ hy.1⟩
variable {H}
@[to_additive] lemma mem_normalizer_iff {g : G} :
g ∈ normalizer H ↔ ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H := iff.rfl
@[to_additive] lemma le_normalizer : H ≤ normalizer H :=
λ x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH]
@[priority 100, to_additive]
instance normal_in_normalizer : (H.comap H.normalizer.subtype).normal :=
⟨λ x xH g, by simpa using (g.2 x).1 xH⟩
open_locale classical
@[to_additive]
lemma le_normalizer_of_normal [hK : (H.comap K.subtype).normal] (HK : H ≤ K) : K ≤ H.normalizer :=
λ x hx y, ⟨λ yH, hK.conj_mem ⟨y, HK yH⟩ yH ⟨x, hx⟩,
λ yH, by simpa [mem_comap, mul_assoc] using
hK.conj_mem ⟨x * y * x⁻¹, HK yH⟩ yH ⟨x⁻¹, K.inv_mem hx⟩⟩
variables {N : Type*} [group N]
/-- The preimage of the normalizer is contained in the normalizer of the preimage. -/
@[to_additive "The preimage of the normalizer is contained in the normalizer of the preimage."]
lemma le_normalizer_comap (f : N →* G) :
H.normalizer.comap f ≤ (H.comap f).normalizer :=
λ x, begin
simp only [mem_normalizer_iff, mem_comap],
assume h n,
simp [h (f n)]
end
/-- The image of the normalizer is contained in the normalizer of the image. -/
@[to_additive "The image of the normalizer is contained in the normalizer of the image."]
lemma le_normalizer_map (f : G →* N) :
H.normalizer.map f ≤ (H.map f).normalizer :=
λ _, begin
simp only [and_imp, exists_prop, mem_map, exists_imp_distrib, mem_normalizer_iff],
rintros x hx rfl n,
split,
{ rintros ⟨y, hy, rfl⟩,
use [x * y * x⁻¹, (hx y).1 hy],
simp },
{ rintros ⟨y, hyH, hy⟩,
use [x⁻¹ * y * x],
rw [hx],
simp [hy, hyH, mul_assoc] }
end
variable (H)
/-- Commutivity of a subgroup -/
structure is_commutative : Prop :=
(is_comm : _root_.is_commutative H (*))
attribute [class] is_commutative
/-- Commutivity of an additive subgroup -/
structure _root_.add_subgroup.is_commutative (H : add_subgroup A) : Prop :=
(is_comm : _root_.is_commutative H (+))
attribute [to_additive add_subgroup.is_commutative] subgroup.is_commutative
attribute [class] add_subgroup.is_commutative
/-- A commutative subgroup is commutative -/
@[to_additive] instance is_commutative.comm_group [h : H.is_commutative] : comm_group H :=
{ mul_comm := h.is_comm.comm, .. H.to_group }
instance center.is_commutative : (center G).is_commutative :=
⟨⟨λ a b, subtype.ext (b.2 a)⟩⟩
end subgroup
namespace group
variables {s : set G}
/-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of
the elements of `s`. -/
def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates_of a
lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x :=
set.mem_bUnion_iff
theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s :=
λ (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩
theorem conjugates_of_set_mono {s t : set G} (h : s ⊆ t) :
conjugates_of_set s ⊆ conjugates_of_set t :=
set.bUnion_subset_bUnion_left h
lemma conjugates_subset_normal {N : subgroup G} [tn : N.normal] {a : G} (h : a ∈ N) :
conjugates_of a ⊆ N :=
by { rintros a hc, obtain ⟨c, rfl⟩ := is_conj_iff.1 hc, exact tn.conj_mem a h c }
theorem conjugates_of_set_subset {s : set G} {N : subgroup G} [N.normal] (h : s ⊆ N) :
conjugates_of_set s ⊆ N :=
set.bUnion_subset (λ x H, conjugates_subset_normal (h H))
/-- The set of conjugates of `s` is closed under conjugation. -/
lemma conj_mem_conjugates_of_set {x c : G} :
x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) :=
λ H,
begin
rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩,
exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans h₂ (is_conj_iff.2 ⟨c,rfl⟩)⟩,
end
end group
namespace subgroup
open group
variable {s : set G}
/-- The normal closure of a set `s` is the subgroup closure of all the conjugates of
elements of `s`. It is the smallest normal subgroup containing `s`. -/
def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s)
theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s :=
subset_closure
theorem subset_normal_closure : s ⊆ normal_closure s :=
set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure
theorem le_normal_closure {H : subgroup G} : H ≤ normal_closure ↑H :=
λ _ h, subset_normal_closure h
/-- The normal closure of `s` is a normal subgroup. -/
instance normal_closure_normal : (normal_closure s).normal :=
⟨λ n h g,
begin
refine subgroup.closure_induction h (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _),
{ exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) },
{ simpa using (normal_closure s).one_mem },
{ rw ← conj_mul,
exact mul_mem _ ihx ihy },
{ rw ← conj_inv,
exact inv_mem _ ihx }
end⟩
/-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/
theorem normal_closure_le_normal {N : subgroup G} [N.normal]
(h : s ⊆ N) : normal_closure s ≤ N :=
begin
assume a w,
refine closure_induction w (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _),
{ exact (conjugates_of_set_subset h hx) },
{ exact subgroup.one_mem _ },
{ exact subgroup.mul_mem _ ihx ihy },
{ exact subgroup.inv_mem _ ihx }
end
lemma normal_closure_subset_iff {N : subgroup G} [N.normal] : s ⊆ N ↔ normal_closure s ≤ N :=
⟨normal_closure_le_normal, set.subset.trans (subset_normal_closure)⟩
theorem normal_closure_mono {s t : set G} (h : s ⊆ t) : normal_closure s ≤ normal_closure t :=
normal_closure_le_normal (set.subset.trans h subset_normal_closure)
theorem normal_closure_eq_infi : normal_closure s =
⨅ (N : subgroup G) [normal N] (hs : s ⊆ N), N :=
le_antisymm
(le_infi (λ N, le_infi (λ hN, by exactI le_infi (normal_closure_le_normal))))
(infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance)
(infi_le_of_le subset_normal_closure (le_refl _))))
@[simp] theorem normal_closure_eq_self (H : subgroup G) [H.normal] : normal_closure ↑H = H :=
le_antisymm (normal_closure_le_normal rfl.subset) (le_normal_closure)
@[simp] theorem normal_closure_idempotent : normal_closure ↑(normal_closure s) = normal_closure s :=
normal_closure_eq_self _
theorem closure_le_normal_closure {s : set G} : closure s ≤ normal_closure s :=
by simp only [subset_normal_closure, closure_le]
@[simp] theorem normal_closure_closure_eq_normal_closure {s : set G} :
normal_closure ↑(closure s) = normal_closure s :=
le_antisymm (normal_closure_le_normal closure_le_normal_closure)
(normal_closure_mono subset_closure)
end subgroup
namespace add_subgroup
open set
lemma gsmul_mem (H : add_subgroup A) {x : A} (hx : x ∈ H) :
∀ n : ℤ, n • x ∈ H
| (n : ℕ) := by { rw [gsmul_coe_nat], exact add_submonoid.nsmul_mem H.to_add_submonoid hx n }
| -[1+ n] := begin
rw gsmul_neg_succ_of_nat,
apply H.neg_mem',
exact add_submonoid.nsmul_mem H.to_add_submonoid hx n.succ
end
/-- The `add_subgroup` generated by an element of an `add_group` equals the set of
natural number multiples of the element. -/
lemma mem_closure_singleton {x y : A} :
y ∈ closure ({x} : set A) ↔ ∃ n : ℤ, n • x = y :=
begin
refine ⟨λ hy, closure_induction hy _ _ _ _,
λ ⟨n, hn⟩, hn ▸ gsmul_mem _ (subset_closure $ mem_singleton x) n⟩,
{ intros y hy,
rw [eq_of_mem_singleton hy],
exact ⟨1, one_gsmul x⟩ },
{ exact ⟨0, zero_gsmul x⟩ },
{ rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩,
exact ⟨n + m, add_gsmul x n m⟩ },
{ rintros _ ⟨n, rfl⟩,
refine ⟨-n, neg_gsmul x n⟩ }
end
lemma closure_singleton_zero : closure ({0} : set A) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
variable (H : add_subgroup A)
@[simp] lemma coe_smul (x : H) (n : ℕ) : ((n • x : H) : A) = n • x :=
coe_subtype H ▸ add_monoid_hom.map_nsmul _ _ _
@[simp] lemma coe_gsmul (x : H) (n : ℤ) : ((n • x : H) : A) = n • x :=
coe_subtype H ▸ add_monoid_hom.map_gsmul _ _ _
attribute [to_additive add_subgroup.coe_smul] subgroup.coe_pow
attribute [to_additive add_subgroup.coe_gsmul] subgroup.coe_gpow
end add_subgroup
namespace monoid_hom
variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G)
open subgroup
/-- The range of a monoid homomorphism from a group is a subgroup. -/
@[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."]
def range (f : G →* N) : subgroup N :=
subgroup.copy ((⊤ : subgroup G).map f) (set.range f) (by simp [set.ext_iff])
@[to_additive]
instance decidable_mem_range (f : G →* N) [fintype G] [decidable_eq N] :
decidable_pred (∈ f.range) :=
λ x, fintype.decidable_exists_fintype
@[simp, to_additive] lemma coe_range (f : G →* N) :
(f.range : set N) = set.range f := rfl
@[simp, to_additive] lemma mem_range {f : G →* N} {y : N} :
y ∈ f.range ↔ ∃ x, f x = y :=
iff.rfl
@[to_additive] lemma range_eq_map (f : G →* N) : f.range = (⊤ : subgroup G).map f :=
by ext; simp
/-- The canonical surjective group homomorphism `G →* f(G)` induced by a group
homomorphism `G →* N`. -/
@[to_additive "The canonical surjective `add_group` homomorphism `G →+ f(G)` induced by a group
homomorphism `G →+ N`."]
def range_restrict (f : G →* N) : G →* f.range :=
monoid_hom.mk' (λ g, ⟨f g, ⟨g, rfl⟩⟩) $ λ a b, by {ext, exact f.map_mul' _ _}
@[simp, to_additive]
lemma coe_range_restrict (f : G →* N) (g : G) : (f.range_restrict g : N) = f g := rfl
@[to_additive]
lemma map_range (g : N →* P) (f : G →* N) : f.range.map g = (g.comp f).range :=
by rw [range_eq_map, range_eq_map]; exact (⊤ : subgroup G).map_map g f
@[to_additive]
lemma range_top_iff_surjective {N} [group N] {f : G →* N} :
f.range = (⊤ : subgroup N) ↔ function.surjective f :=
set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective
/-- The range of a surjective monoid homomorphism is the whole of the codomain. -/
@[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."]
lemma range_top_of_surjective {N} [group N] (f : G →* N) (hf : function.surjective f) :
f.range = (⊤ : subgroup N) :=
range_top_iff_surjective.2 hf
@[simp, to_additive] lemma _root_.subgroup.subtype_range (H : subgroup G) : H.subtype.range = H :=
by { rw [range_eq_map, ← set_like.coe_set_eq, coe_map, subgroup.coe_subtype], ext, simp }
/-- Restriction of a group hom to a subgroup of the domain. -/
@[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the domain."]
def restrict (f : G →* N) (H : subgroup G) : H →* N :=
f.comp H.subtype
@[simp, to_additive]
lemma restrict_apply {H : subgroup G} (f : G →* N) (x : H) :
f.restrict H x = f (x : G) := rfl
/-- Restriction of a group hom to a subgroup of the codomain. -/
@[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the codomain."]
def cod_restrict (f : G →* N) (S : subgroup N) (h : ∀ x, f x ∈ S) : G →* S :=
{ to_fun := λ n, ⟨f n, h n⟩,
map_one' := subtype.eq f.map_one,
map_mul' := λ x y, subtype.eq (f.map_mul x y) }
@[simp, to_additive]
lemma cod_restrict_apply {G : Type*} [group G] {N : Type*} [group N] (f : G →* N)
(S : subgroup N) (h : ∀ (x : G), f x ∈ S) {x : G} :
f.cod_restrict S h x = ⟨f x, h x⟩ := rfl
@[to_additive] lemma subgroup_of_range_eq_of_le {G₁ G₂ : Type*} [group G₁] [group G₂]
{K : subgroup G₂} (f : G₁ →* G₂) (h : f.range ≤ K) :
f.range.subgroup_of K = (f.cod_restrict K (λ x, h ⟨x, rfl⟩)).range :=
begin
ext k,
refine exists_congr _,
simp [subtype.ext_iff],
end
/-- Computable alternative to `monoid_hom.of_injective`. -/
def of_left_inverse {f : G →* N} {g : N →* G} (h : function.left_inverse g f) : G ≃* f.range :=
{ to_fun := f.range_restrict,
inv_fun := g ∘ f.range.subtype,
left_inv := h,
right_inv := by
{ rintros ⟨x, y, rfl⟩,
apply subtype.ext,
rw [coe_range_restrict, function.comp_apply, subgroup.coe_subtype, subtype.coe_mk, h] },
.. f.range_restrict }
@[simp] lemma of_left_inverse_apply {f : G →* N} {g : N →* G}
(h : function.left_inverse g f) (x : G) :
↑(of_left_inverse h x) = f x := rfl
@[simp] lemma of_left_inverse_symm_apply {f : G →* N} {g : N →* G}
(h : function.left_inverse g f) (x : f.range) :
(of_left_inverse h).symm x = g x := rfl
/-- The range of an injective group homomorphism is isomorphic to its domain. -/
noncomputable def of_injective {f : G →* N} (hf : function.injective f) : G ≃* f.range :=
(mul_equiv.of_bijective (f.cod_restrict f.range (λ x, ⟨x, rfl⟩))
⟨λ x y h, hf (subtype.ext_iff.mp h), by { rintros ⟨x, y, rfl⟩, exact ⟨y, rfl⟩ }⟩)
lemma of_injective_apply {f : G →* N} (hf : function.injective f) {x : G} :
↑(of_injective hf x) = f x := rfl
/-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that
`f x = 1` -/
@[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements
such that `f x = 0`"]
def ker (f : G →* N) := (⊥ : subgroup N).comap f
@[to_additive]
lemma mem_ker (f : G →* N) {x : G} : x ∈ f.ker ↔ f x = 1 := iff.rfl
@[to_additive]
lemma coe_ker (f : G →* N) : (f.ker : set G) = (f : G → N) ⁻¹' {1} := rfl
@[to_additive]
lemma eq_iff (f : G →* N) {x y : G} : f x = f y ↔ y⁻¹ * x ∈ f.ker :=
by rw [f.mem_ker, f.map_mul, f.map_inv, inv_mul_eq_one, eq_comm]
@[to_additive]
instance decidable_mem_ker [decidable_eq N] (f : G →* N) :
decidable_pred (∈ f.ker) :=
λ x, decidable_of_iff (f x = 1) f.mem_ker
@[to_additive]
lemma comap_ker (g : N →* P) (f : G →* N) : g.ker.comap f = (g.comp f).ker := rfl
@[simp, to_additive] lemma comap_bot (f : G →* N) :
(⊥ : subgroup N).comap f = f.ker := rfl
@[to_additive] lemma range_restrict_ker (f : G →* N) : ker (range_restrict f) = ker f :=
begin
ext,
change (⟨f x, _⟩ : range f) = ⟨1, _⟩ ↔ f x = 1,
simp only [],
end
@[simp, to_additive]
lemma ker_one : (1 : G →* N).ker = ⊤ :=
by { ext, simp [mem_ker] }
@[to_additive] lemma ker_eq_bot_iff (f : G →* N) : f.ker = ⊥ ↔ function.injective f :=
begin
split,
{ intros h x y hxy,
rwa [←mul_inv_eq_one, ←map_inv, ←map_mul, ←mem_ker, h, mem_bot, mul_inv_eq_one] at hxy },
{ exact λ h, le_bot_iff.mp (λ x hx, h (hx.trans f.map_one.symm)) },
end
@[to_additive]
lemma prod_map_comap_prod {G' : Type*} {N' : Type*} [group G'] [group N']
(f : G →* N) (g : G' →* N') (S : subgroup N) (S' : subgroup N') :
(S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) :=
set_like.coe_injective $ set.preimage_prod_map_prod f g _ _
@[to_additive]
lemma ker_prod_map {G' : Type*} {N' : Type*} [group G'] [group N'] (f : G →* N) (g : G' →* N') :
(prod_map f g).ker = f.ker.prod g.ker :=
begin
dsimp only [ker],
rw [←prod_map_comap_prod, bot_prod_bot],
end
/-- The subgroup of elements `x : G` such that `f x = g x` -/
@[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"]
def eq_locus (f g : G →* N) : subgroup G :=
{ inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx],
.. eq_mlocus f g}
/-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/
@[to_additive]
lemma eq_on_closure {f g : G →* N} {s : set G} (h : set.eq_on f g s) :
set.eq_on f g (closure s) :=
show closure s ≤ f.eq_locus g, from (closure_le _).2 h
@[to_additive]
lemma eq_of_eq_on_top {f g : G →* N} (h : set.eq_on f g (⊤ : subgroup G)) :
f = g :=
ext $ λ x, h trivial
@[to_additive]
lemma eq_of_eq_on_dense {s : set G} (hs : closure s = ⊤) {f g : G →* N} (h : s.eq_on f g) :
f = g :=
eq_of_eq_on_top $ hs ▸ eq_on_closure h
@[to_additive]
lemma gclosure_preimage_le (f : G →* N) (s : set N) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
(closure_le _).2 $ λ x hx, by rw [set_like.mem_coe, mem_comap]; exact subset_closure hx
/-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup
generated by the image of the set. -/
@[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals
the `add_subgroup` generated by the image of the set."]
lemma map_closure (f : G →* N) (s : set G) :
(closure s).map f = closure (f '' s) :=
le_antisymm
(map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image f s)
(gclosure_preimage_le _ _))
((closure_le _).2 $ set.image_subset _ subset_closure)
-- this instance can't go just after the definition of `mrange` because `fintype` is
-- not imported at that stage
/-- The range of a finite monoid under a monoid homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` in the
presence of `fintype N`. -/
@[to_additive "The range of a finite additive monoid under an additive monoid homomorphism is
finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`."]
instance fintype_mrange {M N : Type*} [monoid M] [monoid N] [fintype M] [decidable_eq N]
(f : M →* N) : fintype (mrange f) :=
set.fintype_range f
/-- The range of a finite group under a group homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`. -/
@[to_additive "The range of a finite additive group under an additive group homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`."]
instance fintype_range [fintype G] [decidable_eq N] (f : G →* N) : fintype (range f) :=
set.fintype_range f
end monoid_hom
namespace subgroup
variables {N : Type*} [group N] (H : subgroup G)
@[to_additive] lemma map_eq_bot_iff {f : G →* N} : H.map f = ⊥ ↔ H ≤ f.ker :=
begin
rw eq_bot_iff,
split,
{ exact λ h x hx, h ⟨x, hx, rfl⟩ },
{ intros h x hx,
obtain ⟨y, hy, rfl⟩ := hx,
exact h hy },
end
@[to_additive]
lemma map_eq_bot_iff_of_injective {f : G →* N} (hf : function.injective f) : H.map f = ⊥ ↔ H = ⊥ :=
by rw [map_eq_bot_iff, f.ker_eq_bot_iff.mpr hf, le_bot_iff]
end subgroup
namespace subgroup
open monoid_hom
variables {N : Type*} [group N] (f : G →* N)
@[to_additive]
lemma map_le_range (H : subgroup G) : map f H ≤ f.range :=
(range_eq_map f).symm ▸ map_mono le_top
@[to_additive]
lemma ker_le_comap (H : subgroup N) : f.ker ≤ comap f H :=
comap_mono bot_le
@[to_additive]
lemma map_comap_le (H : subgroup N) : map f (comap f H) ≤ H :=
(gc_map_comap f).l_u_le _
@[to_additive]
lemma le_comap_map (H : subgroup G) : H ≤ comap f (map f H) :=
(gc_map_comap f).le_u_l _
@[to_additive]
lemma map_comap_eq (H : subgroup N) :
map f (comap f H) = f.range ⊓ H :=
set_like.ext' begin
convert set.image_preimage_eq_inter_range,
simp [set.inter_comm],
end
@[to_additive]
lemma comap_map_eq (H : subgroup G) : comap f (map f H) = H ⊔ f.ker :=
begin
refine le_antisymm _ (sup_le (le_comap_map _ _) (ker_le_comap _ _)),
intros x hx, simp only [exists_prop, mem_map, mem_comap] at hx,
rcases hx with ⟨y, hy, hy'⟩,
have : y⁻¹ * x ∈ f.ker, { rw mem_ker, simp [hy'] },
convert mul_mem _ (mem_sup_left hy) (mem_sup_right this),
simp,
end
@[to_additive]
lemma map_comap_eq_self {f : G →* N} {H : subgroup N} (h : H ≤ f.range) :
map f (comap f H) = H :=
by rwa [map_comap_eq, inf_eq_right]
@[to_additive]
lemma map_comap_eq_self_of_surjective {f : G →* N} (h : function.surjective f) (H : subgroup N) :
map f (comap f H) = H :=
map_comap_eq_self ((range_top_of_surjective _ h).symm ▸ le_top)
@[to_additive]
lemma comap_injective {f : G →* N} (h : function.surjective f) : function.injective (comap f) :=
λ K L hKL, by { apply_fun map f at hKL, simpa [map_comap_eq_self_of_surjective h] using hKL }
@[to_additive]
lemma comap_map_eq_self {f : G →* N} {H : subgroup G} (h : f.ker ≤ H) :
comap f (map f H) = H :=
by rwa [comap_map_eq, sup_eq_left]
@[to_additive]
lemma comap_map_eq_self_of_injective {f : G →* N} (h : function.injective f) (H : subgroup G) :
comap f (map f H) = H :=
comap_map_eq_self (((ker_eq_bot_iff _).mpr h).symm ▸ bot_le)
@[to_additive]
lemma map_injective {f : G →* N} (h : function.injective f) : function.injective (map f) :=
λ K L hKL, by { apply_fun comap f at hKL, simpa [comap_map_eq_self_of_injective h] using hKL }
@[to_additive]
lemma map_eq_comap_of_inverse {f : G →* N} {g : N →* G} (hl : function.left_inverse g f)
(hr : function.right_inverse g f) (H : subgroup G) : map f H = comap g H :=
set_like.ext' $ by rw [coe_map, coe_comap, set.image_eq_preimage_of_inverse hl hr]
/-- Given `f(A) = f(B)`, `ker f ≤ A`, and `ker f ≤ B`, deduce that `A = B` -/
@[to_additive] lemma map_injective_of_ker_le
{H K : subgroup G} (hH : f.ker ≤ H) (hK : f.ker ≤ K) (hf : map f H = map f K) :
H = K :=
begin
apply_fun comap f at hf,
rwa [comap_map_eq, comap_map_eq, sup_of_le_left hH, sup_of_le_left hK] at hf,
end
@[to_additive] lemma comap_sup_eq
(H K : subgroup N) (hf : function.surjective f):
comap f H ⊔ comap f K = comap f (H ⊔ K) :=
begin
have : map f (comap f H ⊔ comap f K) = map f (comap f (H ⊔ K)),
{ simp [subgroup.map_comap_eq, map_sup, f.range_top_of_surjective hf], },
refine map_injective_of_ker_le f _ _ this,
{ calc f.ker ≤ comap f H : ker_le_comap f _
... ≤ comap f H ⊔ comap f K : le_sup_left, },
exact ker_le_comap _ _,
end
/-- A subgroup is isomorphic to its image under an injective function -/
@[to_additive "An additive subgroup is isomorphic to its image under an injective function"]
noncomputable def equiv_map_of_injective (H : subgroup G)
(f : G →* N) (hf : function.injective f) : H ≃* H.map f :=
{ map_mul' := λ _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f H hf }
@[simp, to_additive] lemma coe_equiv_map_of_injective_apply (H : subgroup G)
(f : G →* N) (hf : function.injective f) (h : H) :
(equiv_map_of_injective H f hf h : N) = f h := rfl
/-- The preimage of the normalizer is equal to the normalizer of the preimage of a surjective
function. -/
@[to_additive "The preimage of the normalizer is equal to the normalizer of the preimage of
a surjective function."]
lemma comap_normalizer_eq_of_surjective (H : subgroup G)
{f : N →* G} (hf : function.surjective f) :
H.normalizer.comap f = (H.comap f).normalizer :=
le_antisymm (le_normalizer_comap f)
begin
assume x hx,
simp only [mem_comap, mem_normalizer_iff] at *,
assume n,
rcases hf n with ⟨y, rfl⟩,
simp [hx y]
end
/-- The image of the normalizer is equal to the normalizer of the image of an isomorphism. -/
@[to_additive "The image of the normalizer is equal to the normalizer of the image of an
isomorphism."]
lemma map_equiv_normalizer_eq (H : subgroup G)
(f : G ≃* N) : H.normalizer.map f.to_monoid_hom = (H.map f.to_monoid_hom).normalizer :=
begin
ext x,
simp only [mem_normalizer_iff, mem_map_equiv],
rw [f.to_equiv.forall_congr],
simp
end
/-- The image of the normalizer is equal to the normalizer of the image of a bijective
function. -/
@[to_additive "The image of the normalizer is equal to the normalizer of the image of a bijective
function."]
lemma map_normalizer_eq_of_bijective (H : subgroup G)
{f : G →* N} (hf : function.bijective f) :
H.normalizer.map f = (H.map f).normalizer :=
map_equiv_normalizer_eq H (mul_equiv.of_bijective f hf)
end subgroup
namespace monoid_hom
variables {G₁ G₂ G₃ : Type*} [group G₁] [group G₂] [group G₃]
variables (f : G₁ →* G₂) (f_inv : G₂ → G₁)
/-- Auxiliary definition used to define `lift_of_right_inverse` -/
@[to_additive "Auxiliary definition used to define `lift_of_right_inverse`"]
def lift_of_right_inverse_aux
(hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) :
G₂ →* G₃ :=
{ to_fun := λ b, g (f_inv b),
map_one' := hg (hf 1),
map_mul' :=
begin
intros x y,
rw [← g.map_mul, ← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one, f.map_mul],
simp only [hf _],
end }
@[simp, to_additive]
lemma lift_of_right_inverse_aux_comp_apply
(hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (x : G₁) :
(f.lift_of_right_inverse_aux f_inv hf g hg) (f x) = g x :=
begin
dsimp [lift_of_right_inverse_aux],
rw [← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one],
simp only [hf _],
end
/-- `lift_of_right_inverse f hf g hg` is the unique group homomorphism `φ`
* such that `φ.comp f = g` (`monoid_hom.lift_of_right_inverse_comp`),
* where `f : G₁ →+* G₂` has a right_inverse `f_inv` (`hf`),
* and `g : G₂ →+* G₃` satisfies `hg : f.ker ≤ g.ker`.
See `monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma.
```
G₁.
| \
f | \ g
| \
v \⌟
G₂----> G₃
∃!φ
```
-/
@[to_additive "`lift_of_right_inverse f f_inv hf g hg` is the unique additive group homomorphism `φ`
* such that `φ.comp f = g` (`add_monoid_hom.lift_of_right_inverse_comp`),
* where `f : G₁ →+ G₂` has a right_inverse `f_inv` (`hf`),
* and `g : G₂ →+ G₃` satisfies `hg : f.ker ≤ g.ker`.
See `add_monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma.
```
G₁.
| \\
f | \\ g
| \\
v \\⌟
G₂----> G₃
∃!φ
```"]
def lift_of_right_inverse
(hf : function.right_inverse f_inv f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) :=
{ to_fun := λ g, f.lift_of_right_inverse_aux f_inv hf g.1 g.2,
inv_fun := λ φ, ⟨φ.comp f, λ x hx, (mem_ker _).mpr $ by simp [(mem_ker _).mp hx]⟩,
left_inv := λ g, by {
ext,
simp only [comp_apply, lift_of_right_inverse_aux_comp_apply, subtype.coe_mk,
subtype.val_eq_coe], },
right_inv := λ φ, by {
ext b,
simp [lift_of_right_inverse_aux, hf b], } }
/-- A non-computable version of `monoid_hom.lift_of_right_inverse` for when no computable right
inverse is available, that uses `function.surj_inv`. -/
@[simp, to_additive "A non-computable version of `add_monoid_hom.lift_of_right_inverse` for when no
computable right inverse is available."]
noncomputable abbreviation lift_of_surjective
(hf : function.surjective f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) :=
f.lift_of_right_inverse (function.surj_inv hf) (function.right_inverse_surj_inv hf)
@[simp, to_additive]
lemma lift_of_right_inverse_comp_apply
(hf : function.right_inverse f_inv f) (g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) (x : G₁) :
(f.lift_of_right_inverse f_inv hf g) (f x) = g x :=
f.lift_of_right_inverse_aux_comp_apply f_inv hf g.1 g.2 x
@[simp, to_additive]
lemma lift_of_right_inverse_comp (hf : function.right_inverse f_inv f)
(g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) :
(f.lift_of_right_inverse f_inv hf g).comp f = g :=
monoid_hom.ext $ f.lift_of_right_inverse_comp_apply f_inv hf g
@[to_additive]
lemma eq_lift_of_right_inverse (hf : function.right_inverse f_inv f) (g : G₁ →* G₃)
(hg : f.ker ≤ g.ker) (h : G₂ →* G₃) (hh : h.comp f = g) :
h = (f.lift_of_right_inverse f_inv hf ⟨g, hg⟩) :=
begin
simp_rw ←hh,
exact ((f.lift_of_right_inverse f_inv hf).apply_symm_apply _).symm,
end
end monoid_hom
variables {N : Type*} [group N]
-- Here `H.normal` is an explicit argument so we can use dot notation with `comap`.
@[to_additive]
lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G →* N) :
(H.comap f).normal :=
⟨λ _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}⟩
@[priority 100, to_additive]
instance subgroup.normal_comap {H : subgroup N}
[nH : H.normal] (f : G →* N) : (H.comap f).normal := nH.comap _
@[priority 100, to_additive]
instance monoid_hom.normal_ker (f : G →* N) : f.ker.normal :=
by rw [monoid_hom.ker]; apply_instance
@[priority 100, to_additive]
instance subgroup.normal_inf (H N : subgroup G) [hN : N.normal] :
((H ⊓ N).comap H.subtype).normal :=
⟨λ x hx g, begin
simp only [subgroup.mem_inf, coe_subtype, subgroup.mem_comap] at hx,
simp only [subgroup.coe_mul, subgroup.mem_inf, coe_subtype, subgroup.coe_inv, subgroup.mem_comap],
exact ⟨H.mul_mem (H.mul_mem g.2 hx.1) (H.inv_mem g.2), hN.1 x hx.2 g⟩,
end⟩
namespace subgroup
/-- The subgroup generated by an element. -/
def gpowers (g : G) : subgroup G :=
subgroup.copy (gpowers_hom G g).range (set.range ((^) g : ℤ → G)) rfl
@[simp] lemma mem_gpowers (g : G) : g ∈ gpowers g := ⟨1, gpow_one _⟩
lemma gpowers_eq_closure (g : G) : gpowers g = closure {g} :=
by { ext, exact mem_closure_singleton.symm }
@[simp] lemma range_gpowers_hom (g : G) : (gpowers_hom G g).range = gpowers g := rfl
lemma gpowers_subset {a : G} {K : subgroup G} (h : a ∈ K) : gpowers a ≤ K :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := K.gpow_mem h i end
lemma mem_gpowers_iff {g h : G} :
h ∈ gpowers g ↔ ∃ (k : ℤ), g ^ k = h :=
iff.rfl
@[simp] lemma forall_gpowers {x : G} {p : gpowers x → Prop} :
(∀ g, p g) ↔ ∀ m : ℤ, p ⟨x ^ m, m, rfl⟩ :=
set.forall_subtype_range_iff
@[simp] lemma exists_gpowers {x : G} {p : gpowers x → Prop} :
(∃ g, p g) ↔ ∃ m : ℤ, p ⟨x ^ m, m, rfl⟩ :=
set.exists_subtype_range_iff
lemma forall_mem_gpowers {x : G} {p : G → Prop} :
(∀ g ∈ gpowers x, p g) ↔ ∀ m : ℤ, p (x ^ m) :=
set.forall_range_iff
lemma exists_mem_gpowers {x : G} {p : G → Prop} :
(∃ g ∈ gpowers x, p g) ↔ ∃ m : ℤ, p (x ^ m) :=
set.exists_range_iff
end subgroup
namespace add_subgroup
/-- The subgroup generated by an element. -/
def gmultiples (a : A) : add_subgroup A :=
add_subgroup.copy (gmultiples_hom A a).range (set.range ((• a) : ℤ → A)) rfl
@[simp] lemma range_gmultiples_hom (a : A) : (gmultiples_hom A a).range = gmultiples a := rfl
lemma gmultiples_subset {a : A} {B : add_subgroup A} (h : a ∈ B) : gmultiples a ≤ B :=
@subgroup.gpowers_subset (multiplicative A) _ _ (B.to_subgroup) h
attribute [to_additive add_subgroup.gmultiples] subgroup.gpowers
attribute [to_additive add_subgroup.mem_gmultiples] subgroup.mem_gpowers
attribute [to_additive add_subgroup.gmultiples_eq_closure] subgroup.gpowers_eq_closure
attribute [to_additive add_subgroup.range_gmultiples_hom] subgroup.range_gpowers_hom
attribute [to_additive add_subgroup.gmultiples_subset] subgroup.gpowers_subset
attribute [to_additive add_subgroup.mem_gmultiples_iff] subgroup.mem_gpowers_iff
attribute [to_additive add_subgroup.forall_gmultiples] subgroup.forall_gpowers
attribute [to_additive add_subgroup.forall_mem_gmultiples] subgroup.forall_mem_gpowers
attribute [to_additive add_subgroup.exists_gmultiples] subgroup.exists_gpowers
attribute [to_additive add_subgroup.exists_mem_gmultiples] subgroup.exists_mem_gpowers
end add_subgroup
lemma int.mem_gmultiples_iff {a b : ℤ} :
b ∈ add_subgroup.gmultiples a ↔ a ∣ b :=
exists_congr (λ k, by rw [mul_comm, eq_comm, ← smul_eq_mul])
lemma of_mul_image_gpowers_eq_gmultiples_of_mul { x : G } :
additive.of_mul '' ((subgroup.gpowers x) : set G) = add_subgroup.gmultiples (additive.of_mul x) :=
begin
ext y,
split,
{ rintro ⟨z, ⟨m, hm⟩, hz2⟩,
use m,
simp only,
rwa [← of_mul_gpow, hm] },
{ rintros ⟨n, hn⟩,
refine ⟨x ^ n, ⟨n, rfl⟩, _⟩,
rwa of_mul_gpow }
end
lemma of_add_image_gmultiples_eq_gpowers_of_add {x : A} :
multiplicative.of_add '' ((add_subgroup.gmultiples x) : set A) =
subgroup.gpowers (multiplicative.of_add x) :=
begin
symmetry,
rw equiv.eq_image_iff_symm_image_eq,
exact of_mul_image_gpowers_eq_gmultiples_of_mul,
end
namespace mul_equiv
variables {H K : subgroup G}
/-- Makes the identity isomorphism from a proof two subgroups of a multiplicative
group are equal. -/
@[to_additive "Makes the identity additive isomorphism from a proof
two subgroups of an additive group are equal."]
def subgroup_congr (h : H = K) : H ≃* K :=
{ map_mul' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h }
end mul_equiv
-- TODO : ↥(⊤ : subgroup H) ≃* H ?
namespace subgroup
variables {C : Type*} [comm_group C] {s t : subgroup C} {x : C}
@[to_additive]
lemma mem_sup : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x :=
⟨λ h, begin
rw [← closure_eq s, ← closure_eq t, ← closure_union] at h,
apply closure_induction h,
{ rintro y (h | h),
{ exact ⟨y, h, 1, t.one_mem, by simp⟩ },
{ exact ⟨1, s.one_mem, y, h, by simp⟩ } },
{ exact ⟨1, s.one_mem, 1, ⟨t.one_mem, mul_one 1⟩⟩ },
{ rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩,
exact ⟨_, mul_mem _ hy₁ hy₂, _, mul_mem _ hz₁ hz₂, by simp [mul_assoc]; cc⟩ },
{ rintro _ ⟨y, hy, z, hz, rfl⟩,
exact ⟨_, inv_mem _ hy, _, inv_mem _ hz, mul_comm z y ▸ (mul_inv_rev z y).symm⟩ }
end,
by rintro ⟨y, hy, z, hz, rfl⟩; exact mul_mem _
((le_sup_left : s ≤ s ⊔ t) hy)
((le_sup_right : t ≤ s ⊔ t) hz)⟩
@[to_additive]
lemma mem_sup' : x ∈ s ⊔ t ↔ ∃ (y : s) (z : t), (y:C) * z = x :=
mem_sup.trans $ by simp only [set_like.exists, coe_mk]
@[to_additive]
instance : is_modular_lattice (subgroup C) :=
⟨λ x y z xz a ha, begin
rw [mem_inf, mem_sup] at ha,
rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩,
rw mem_sup,
refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩,
rw ← inv_mul_cancel_left b c,
apply z.mul_mem (z.inv_mem (xz hb)) haz,
end⟩
end subgroup
section
variables (G) (A)
/-- A `group` is simple when it has exactly two normal `subgroup`s. -/
class is_simple_group extends nontrivial G : Prop :=
(eq_bot_or_eq_top_of_normal : ∀ H : subgroup G, H.normal → H = ⊥ ∨ H = ⊤)
/-- An `add_group` is simple when it has exactly two normal `add_subgroup`s. -/
class is_simple_add_group extends nontrivial A : Prop :=
(eq_bot_or_eq_top_of_normal : ∀ H : add_subgroup A, H.normal → H = ⊥ ∨ H = ⊤)
attribute [to_additive] is_simple_group
variables {G} {A}
@[to_additive]
lemma subgroup.normal.eq_bot_or_eq_top [is_simple_group G] {H : subgroup G} (Hn : H.normal) :
H = ⊥ ∨ H = ⊤ :=
is_simple_group.eq_bot_or_eq_top_of_normal H Hn
namespace is_simple_group
@[to_additive]
instance {C : Type*} [comm_group C] [is_simple_group C] :
is_simple_lattice (subgroup C) :=
⟨λ H, H.normal_of_comm.eq_bot_or_eq_top⟩
open subgroup
@[to_additive]
lemma is_simple_group_of_surjective {H : Type*} [group H] [is_simple_group G]
[nontrivial H] (f : G →* H) (hf : function.surjective f) :
is_simple_group H :=
⟨nontrivial.exists_pair_ne, λ H iH, begin
refine ((iH.comap f).eq_bot_or_eq_top).imp (λ h, _) (λ h, _),
{ rw [←map_bot f, ←h, map_comap_eq_self_of_surjective hf] },
{ rw [←comap_top f] at h, exact comap_injective hf h }
end⟩
end is_simple_group
end
namespace subgroup
section pointwise
@[to_additive]
lemma closure_mul_le (S T : set G) : closure (S * T) ≤ closure S ⊔ closure T :=
Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem
(set_like.le_def.mp le_sup_left $ subset_closure hs)
(set_like.le_def.mp le_sup_right $ subset_closure ht)
@[to_additive]
lemma sup_eq_closure (H K : subgroup G) : H ⊔ K = closure (H * K) :=
le_antisymm
(sup_le
(λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩)
(λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩))
(by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le)
@[to_additive]
private def mul_normal_aux (H N : subgroup G) [hN : N.normal] : subgroup G :=
{ carrier := (H : set G) * N,
one_mem' := ⟨1, 1, H.one_mem, N.one_mem, by rw mul_one⟩,
mul_mem' := λ a b ⟨h, n, hh, hn, ha⟩ ⟨h', n', hh', hn', hb⟩,
⟨h * h', h'⁻¹ * n * h' * n',
H.mul_mem hh hh', N.mul_mem (by simpa using hN.conj_mem _ hn h'⁻¹) hn',
by simp [← ha, ← hb, mul_assoc]⟩,
inv_mem' := λ x ⟨h, n, hh, hn, hx⟩,
⟨h⁻¹, h * n⁻¹ * h⁻¹, H.inv_mem hh, hN.conj_mem _ (N.inv_mem hn) h,
by rw [mul_assoc h, inv_mul_cancel_left, ← hx, mul_inv_rev]⟩ }
/-- The carrier of `H ⊔ N` is just `↑H * ↑N` (pointwise set product) when `N` is normal. -/
@[to_additive "The carrier of `H ⊔ N` is just `↑H + ↑N` (pointwise set addition)
when `N` is normal."]
lemma mul_normal (H N : subgroup G) [N.normal] : (↑(H ⊔ N) : set G) = H * N :=
set.subset.antisymm
(show H ⊔ N ≤ mul_normal_aux H N,
by { rw sup_eq_closure, apply Inf_le _, dsimp, refl })
((sup_eq_closure H N).symm ▸ subset_closure)
@[to_additive]
private def normal_mul_aux (N H : subgroup G) [hN : N.normal] : subgroup G :=
{ carrier := (N : set G) * H,
one_mem' := ⟨1, 1, N.one_mem, H.one_mem, by rw mul_one⟩,
mul_mem' := λ a b ⟨n, h, hn, hh, ha⟩ ⟨n', h', hn', hh', hb⟩,
⟨n * (h * n' * h⁻¹), h * h',
N.mul_mem hn (hN.conj_mem _ hn' _), H.mul_mem hh hh',
by simp [← ha, ← hb, mul_assoc]⟩,
inv_mem' := λ x ⟨n, h, hn, hh, hx⟩,
⟨h⁻¹ * n⁻¹ * h, h⁻¹,
by simpa using hN.conj_mem _ (N.inv_mem hn) h⁻¹, H.inv_mem hh,
by rw [mul_inv_cancel_right, ← mul_inv_rev, hx]⟩ }
/-- The carrier of `N ⊔ H` is just `↑N * ↑H` (pointwise set product) when `N` is normal. -/
@[to_additive "The carrier of `N ⊔ H` is just `↑N + ↑H` (pointwise set addition)
when `N` is normal."]
lemma normal_mul (N H : subgroup G) [N.normal] : (↑(N ⊔ H) : set G) = N * H :=
set.subset.antisymm
(show N ⊔ H ≤ normal_mul_aux N H,
by { rw sup_eq_closure, apply Inf_le _, dsimp, refl })
((sup_eq_closure N H).symm ▸ subset_closure)
@[to_additive] lemma mul_inf_assoc (A B C : subgroup G) (h : A ≤ C) :
(A : set G) * ↑(B ⊓ C) = (A * B) ⊓ C :=
begin
ext,
simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff],
split,
{ rintros ⟨y, z, hy, ⟨hzB, hzC⟩, rfl⟩,
refine ⟨_, mul_mem C (h hy) hzC⟩,
exact ⟨y, z, hy, hzB, rfl⟩ },
rintros ⟨⟨y, z, hy, hz, rfl⟩, hyz⟩,
refine ⟨y, z, hy, ⟨hz, _⟩, rfl⟩,
suffices : y⁻¹ * (y * z) ∈ C, { simpa },
exact mul_mem C (inv_mem C (h hy)) hyz
end
@[to_additive] lemma inf_mul_assoc (A B C : subgroup G) (h : C ≤ A) :
((A ⊓ B : subgroup G) : set G) * C = A ⊓ (B * C) :=
begin
ext,
simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff],
split,
{ rintros ⟨y, z, ⟨hyA, hyB⟩, hz, rfl⟩,
refine ⟨mul_mem A hyA (h hz), _⟩,
exact ⟨y, z, hyB, hz, rfl⟩ },
rintros ⟨hyz, y, z, hy, hz, rfl⟩,
refine ⟨y, z, ⟨_, hy⟩, hz, rfl⟩,
suffices : (y * z) * z⁻¹ ∈ A, { simpa },
exact mul_mem A hyz (inv_mem A (h hz))
end
end pointwise
section subgroup_normal
@[to_additive] lemma normal_subgroup_of_iff {H K : subgroup G} (hHK : H ≤ K) :
(H.subgroup_of K).normal ↔ ∀ h k, h ∈ H → k ∈ K → k * h * k⁻¹ ∈ H :=
⟨λ hN h k hH hK, hN.conj_mem ⟨h, hHK hH⟩ hH ⟨k, hK⟩,
λ hN, { conj_mem := λ h hm k, (hN h.1 k.1 hm k.2) }⟩
@[to_additive] instance prod_subgroup_of_prod_normal
{H₁ K₁ : subgroup G} {H₂ K₂ : subgroup N}
[h₁ : (H₁.subgroup_of K₁).normal] [h₂ : (H₂.subgroup_of K₂).normal] :
((H₁.prod H₂).subgroup_of (K₁.prod K₂)).normal :=
{ conj_mem := λ n hgHK g,
⟨h₁.conj_mem ⟨(n : G × N).fst, (mem_prod.mp n.2).1⟩
hgHK.1 ⟨(g : G × N).fst, (mem_prod.mp g.2).1⟩,
h₂.conj_mem ⟨(n : G × N).snd, (mem_prod.mp n.2).2⟩
hgHK.2 ⟨(g : G × N).snd, (mem_prod.mp g.2).2⟩⟩ }
@[to_additive] instance prod_normal
(H : subgroup G) (K : subgroup N) [hH : H.normal] [hK : K.normal] :
(H.prod K).normal :=
{ conj_mem := λ n hg g,
⟨hH.conj_mem n.fst (subgroup.mem_prod.mp hg).1 g.fst,
hK.conj_mem n.snd (subgroup.mem_prod.mp hg).2 g.snd⟩ }
@[to_additive] lemma inf_subgroup_of_inf_normal_of_right
(A B' B : subgroup G) (hB : B' ≤ B) [hN : (B'.subgroup_of B).normal] :
((A ⊓ B').subgroup_of (A ⊓ B)).normal :=
{ conj_mem := λ n hn g,
⟨mul_mem A (mul_mem A (mem_inf.1 g.2).1 (mem_inf.1 n.2).1) (inv_mem A (mem_inf.1 g.2).1),
(normal_subgroup_of_iff hB).mp hN n g hn.2 (mem_inf.mp g.2).2⟩ }
@[to_additive] lemma inf_subgroup_of_inf_normal_of_left
{A' A : subgroup G} (B : subgroup G) (hA : A' ≤ A) [hN : (A'.subgroup_of A).normal] :
((A' ⊓ B).subgroup_of (A ⊓ B)).normal :=
{ conj_mem := λ n hn g,
⟨(normal_subgroup_of_iff hA).mp hN n g hn.1 (mem_inf.mp g.2).1,
mul_mem B (mul_mem B (mem_inf.1 g.2).2 (mem_inf.1 n.2).2) (inv_mem B (mem_inf.1 g.2).2)⟩ }
instance sup_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] : (H ⊔ K).normal :=
{ conj_mem := λ n hmem g,
begin
change n ∈ ↑(H ⊔ K) at hmem,
change g * n * g⁻¹ ∈ ↑(H ⊔ K),
rw [normal_mul, set.mem_mul] at *,
rcases hmem with ⟨h, k, hh, hk, rfl⟩,
refine ⟨g * h * g⁻¹, g * k * g⁻¹, hH.conj_mem h hh g, hK.conj_mem k hk g, _⟩,
simp
end }
@[to_additive] instance normal_inf_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] :
(H ⊓ K).normal :=
{ conj_mem := λ n hmem g,
by { rw mem_inf at *, exact ⟨hH.conj_mem n hmem.1 g, hK.conj_mem n hmem.2 g⟩ } }
@[to_additive] lemma subgroup_of_sup (A A' B : subgroup G) (hA : A ≤ B) (hA' : A' ≤ B) :
(A ⊔ A').subgroup_of B = A.subgroup_of B ⊔ A'.subgroup_of B :=
begin
refine map_injective_of_ker_le B.subtype
(ker_le_comap _ _) (le_trans (ker_le_comap B.subtype _) le_sup_left) _,
{ simp only [subgroup_of, map_comap_eq, map_sup, subtype_range],
rw [inf_of_le_right (sup_le hA hA'), inf_of_le_right hA', inf_of_le_right hA] },
end
@[to_additive] lemma subgroup_normal.mem_comm {H K : subgroup G}
(hK : H ≤ K) [hN : (H.subgroup_of K).normal] {a b : G} (hb : b ∈ K) (h : a * b ∈ H) :
b * a ∈ H :=
begin
have := (normal_subgroup_of_iff hK).mp hN (a * b) b h hb,
rwa [mul_assoc, mul_assoc, mul_right_inv, mul_one] at this,
end
end subgroup_normal
end subgroup
namespace is_conj
open subgroup
lemma normal_closure_eq_top_of {N : subgroup G} [hn : N.normal]
{g g' : G} {hg : g ∈ N} {hg' : g' ∈ N} (hc : is_conj g g')
(ht : normal_closure ({⟨g, hg⟩} : set N) = ⊤) :
normal_closure ({⟨g', hg'⟩} : set N) = ⊤ :=
begin
obtain ⟨c, rfl⟩ := is_conj_iff.1 hc,
have h : ∀ x : N, (mul_aut.conj c) x ∈ N,
{ rintro ⟨x, hx⟩,
exact hn.conj_mem _ hx c },
have hs : function.surjective (((mul_aut.conj c).to_monoid_hom.restrict N).cod_restrict _ h),
{ rintro ⟨x, hx⟩,
refine ⟨⟨c⁻¹ * x * c, _⟩, _⟩,
{ have h := hn.conj_mem _ hx c⁻¹,
rwa [inv_inv] at h },
simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply,
coe_mk, monoid_hom.restrict_apply, subtype.mk_eq_mk, ← mul_assoc, mul_inv_self, one_mul],
rw [mul_assoc, mul_inv_self, mul_one] },
have ht' := map_mono (eq_top_iff.1 ht),
rw [← monoid_hom.range_eq_map, monoid_hom.range_top_of_surjective _ hs] at ht',
refine eq_top_iff.2 (le_trans ht' (map_le_iff_le_comap.2 (normal_closure_le_normal _))),
rw [set.singleton_subset_iff, set_like.mem_coe],
simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply, coe_mk,
monoid_hom.restrict_apply, mem_comap],
exact subset_normal_closure (set.mem_singleton _),
end
end is_conj
/-! ### Actions by `subgroup`s
These are just copies of the definitions about `submonoid` starting from `submonoid.mul_action`.
-/
section actions
namespace subgroup
variables {α β : Type*}
/-- The action by a subgroup is the action by the underlying group. -/
@[to_additive /-"The additive action by an add_subgroup is the action by the underlying
add_group. "-/]
instance [mul_action G α] (S : subgroup G) : mul_action S α :=
S.to_submonoid.mul_action
@[to_additive]
lemma smul_def [mul_action G α] {S : subgroup G} (g : S) (m : α) : g • m = (g : G) • m := rfl
@[to_additive]
instance smul_comm_class_left
[mul_action G β] [has_scalar α β] [smul_comm_class G α β] (S : subgroup G) :
smul_comm_class S α β :=
S.to_submonoid.smul_comm_class_left
@[to_additive]
instance smul_comm_class_right
[has_scalar α β] [mul_action G β] [smul_comm_class α G β] (S : subgroup G) :
smul_comm_class α S β :=
S.to_submonoid.smul_comm_class_right
/-- Note that this provides `is_scalar_tower S G G` which is needed by `smul_mul_assoc`. -/
instance
[has_scalar α β] [mul_action G α] [mul_action G β] [is_scalar_tower G α β] (S : subgroup G) :
is_scalar_tower S α β :=
S.to_submonoid.is_scalar_tower
instance [mul_action G α] [has_faithful_scalar G α] (S : subgroup G) :
has_faithful_scalar S α :=
S.to_submonoid.has_faithful_scalar
/-- The action by a subgroup is the action by the underlying group. -/
instance [add_monoid α] [distrib_mul_action G α] (S : subgroup G) : distrib_mul_action S α :=
S.to_submonoid.distrib_mul_action
end subgroup
end actions
/-! ### Saturated subgroups -/
section saturated
namespace subgroup
/-- A subgroup `H` of `G` is *saturated* if for all `n : ℕ` and `g : G` with `g^n ∈ H`
we have `n = 0` or `g ∈ H`. -/
@[to_additive "An additive subgroup `H` of `G` is *saturated* if
for all `n : ℕ` and `g : G` with `n•g ∈ H` we have `n = 0` or `g ∈ H`."]
def saturated (H : subgroup G) : Prop := ∀ ⦃n g⦄, npow n g ∈ H → n = 0 ∨ g ∈ H
@[to_additive] lemma saturated_iff_npow {H : subgroup G} :
saturated H ↔ (∀ (n : ℕ) (g : G), g^n ∈ H → n = 0 ∨ g ∈ H) := iff.rfl
@[to_additive] lemma saturated_iff_gpow {H : subgroup G} :
saturated H ↔ (∀ (n : ℤ) (g : G), g^n ∈ H → n = 0 ∨ g ∈ H) :=
begin
split,
{ rintros hH ⟨n⟩ g hgn,
{ simp only [int.coe_nat_eq_zero, int.of_nat_eq_coe, gpow_coe_nat] at hgn ⊢,
exact hH hgn },
{ suffices : g ^ (n+1) ∈ H,
{ refine (hH this).imp _ id, simp only [forall_false_left, nat.succ_ne_zero], },
simpa only [inv_mem_iff, gpow_neg_succ_of_nat] using hgn, } },
{ intros h n g hgn,
specialize h n g,
simp only [int.coe_nat_eq_zero, gpow_coe_nat] at h,
apply h hgn }
end
end subgroup
namespace add_subgroup
lemma ker_saturated {A₁ A₂ : Type*} [add_comm_group A₁] [add_comm_group A₂]
[no_zero_smul_divisors ℕ A₂] (f : A₁ →+ A₂) :
(f.ker).saturated :=
begin
intros n g hg,
simpa only [f.mem_ker, nsmul_eq_smul, f.map_nsmul, smul_eq_zero] using hg
end
end add_subgroup
end saturated
|
f0bbd7c66aa57f7fcf659d36672c03efff7292cd | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/measure_theory/measurable_space.lean | d54ca2f0c61353cb6b696cc44f29de3ff9ed8fde | [
"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 | 45,128 | 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.measurable_space_def
import measure_theory.tactic
import data.tprod
/-!
# Measurable spaces and measurable functions
This file provides properties of measurable spaces and the functions and isomorphisms
between them. The definition of a measurable space is in `measure_theory.measurable_space_def`.
A measurable space is a set equipped with a σ-algebra, a collection of
subsets closed under complementation and countable union. A function
between measurable spaces is measurable if the preimage of each
measurable subset is measurable.
σ-algebras on a fixed set `α` form a complete lattice. Here we order
σ-algebras by writing `m₁ ≤ m₂` if every set which is `m₁`-measurable is
also `m₂`-measurable (that is, `m₁` is a subset of `m₂`). In particular, any
collection of subsets of `α` generates a smallest σ-algebra which
contains all of them. A function `f : α → β` induces a Galois connection
between the lattices of σ-algebras on `α` and `β`.
A measurable equivalence between measurable spaces is an equivalence
which respects the σ-algebras, that is, for which both directions of
the equivalence are measurable functions.
We say that a filter `f` is measurably generated if every set `s ∈ f` includes a measurable
set `t ∈ f`. This property is useful, e.g., to extract a measurable witness of `filter.eventually`.
## Notation
* We write `α ≃ᵐ β` for measurable equivalences between the measurable spaces `α` and `β`.
This should not be confused with `≃ₘ` which is used for diffeomorphisms between manifolds.
## Implementation notes
Measurability of a function `f : α → β` between measurable spaces is
defined in terms of the Galois connection induced by f.
## References
* <https://en.wikipedia.org/wiki/Measurable_space>
* <https://en.wikipedia.org/wiki/Sigma-algebra>
* <https://en.wikipedia.org/wiki/Dynkin_system>
## Tags
measurable space, σ-algebra, measurable function, measurable equivalence, dynkin system,
π-λ theorem, π-system
-/
open set encodable function equiv
open_locale classical filter
variables {α β γ δ δ' : Type*} {ι : Sort*} {s t u : set α}
namespace measurable_space
section functors
variables {m m₁ m₂ : measurable_space α} {m' : measurable_space β} {f : α → β} {g : β → α}
/-- The forward image of a measurable space under a function. `map f m` contains the sets
`s : set β` whose preimage under `f` is measurable. -/
protected def map (f : α → β) (m : measurable_space α) : measurable_space β :=
{ measurable_set' := λ s, m.measurable_set' $ f ⁻¹' s,
measurable_set_empty := m.measurable_set_empty,
measurable_set_compl := assume s hs, m.measurable_set_compl _ hs,
measurable_set_Union := assume f hf, by { rw preimage_Union, exact m.measurable_set_Union _ hf }}
@[simp] lemma map_id : m.map id = m :=
measurable_space.ext $ assume s, iff.rfl
@[simp] lemma map_comp {f : α → β} {g : β → γ} : (m.map f).map g = m.map (g ∘ f) :=
measurable_space.ext $ assume s, iff.rfl
/-- The reverse image of a measurable space under a function. `comap f m` contains the sets
`s : set α` such that `s` is the `f`-preimage of a measurable set in `β`. -/
protected def comap (f : α → β) (m : measurable_space β) : measurable_space α :=
{ measurable_set' := λ s, ∃s', m.measurable_set' s' ∧ f ⁻¹' s' = s,
measurable_set_empty := ⟨∅, m.measurable_set_empty, rfl⟩,
measurable_set_compl := assume s ⟨s', h₁, h₂⟩, ⟨s'ᶜ, m.measurable_set_compl _ h₁, h₂ ▸ rfl⟩,
measurable_set_Union := assume s hs,
let ⟨s', hs'⟩ := classical.axiom_of_choice hs in
⟨⋃ i, s' i, m.measurable_set_Union _ (λ i, (hs' i).left), by simp [hs'] ⟩ }
@[simp] lemma comap_id : m.comap id = m :=
measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h ▸ hs', assume h, ⟨s, h, rfl⟩⟩
@[simp] lemma comap_comp {f : β → α} {g : γ → β} : (m.comap f).comap g = m.comap (f ∘ g) :=
measurable_space.ext $ assume s,
⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht ▸ hu ▸ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩
lemma comap_le_iff_le_map {f : α → β} : m'.comap f ≤ m ↔ m' ≤ m.map f :=
⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq ▸ h _ ht⟩
lemma gc_comap_map (f : α → β) :
galois_connection (measurable_space.comap f) (measurable_space.map f) :=
assume f g, comap_le_iff_le_map
lemma map_mono (h : m₁ ≤ m₂) : m₁.map f ≤ m₂.map f := (gc_comap_map f).monotone_u h
lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h
lemma comap_mono (h : m₁ ≤ m₂) : m₁.comap g ≤ m₂.comap g := (gc_comap_map g).monotone_l h
lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h
@[simp] lemma comap_bot : (⊥ : measurable_space α).comap g = ⊥ := (gc_comap_map g).l_bot
@[simp] lemma comap_sup : (m₁ ⊔ m₂).comap g = m₁.comap g ⊔ m₂.comap g := (gc_comap_map g).l_sup
@[simp] lemma comap_supr {m : ι → measurable_space α} : (⨆i, m i).comap g = (⨆i, (m i).comap g) :=
(gc_comap_map g).l_supr
@[simp] lemma map_top : (⊤ : measurable_space α).map f = ⊤ := (gc_comap_map f).u_top
@[simp] lemma map_inf : (m₁ ⊓ m₂).map f = m₁.map f ⊓ m₂.map f := (gc_comap_map f).u_inf
@[simp] lemma map_infi {m : ι → measurable_space α} : (⨅i, m i).map f = (⨅i, (m i).map f) :=
(gc_comap_map f).u_infi
lemma comap_map_le : (m.map f).comap f ≤ m := (gc_comap_map f).l_u_le _
lemma le_map_comap : m ≤ (m.comap g).map g := (gc_comap_map g).le_u_l _
end functors
lemma generate_from_le_generate_from {s t : set (set α)} (h : s ⊆ t) :
generate_from s ≤ generate_from t :=
gi_generate_from.gc.monotone_l h
lemma generate_from_sup_generate_from {s t : set (set α)} :
generate_from s ⊔ generate_from t = generate_from (s ∪ t) :=
(@gi_generate_from α).gc.l_sup.symm
lemma comap_generate_from {f : α → β} {s : set (set β)} :
(generate_from s).comap f = generate_from (preimage f '' s) :=
le_antisymm
(comap_le_iff_le_map.2 $ generate_from_le $ assume t hts,
generate_measurable.basic _ $ mem_image_of_mem _ $ hts)
(generate_from_le $ assume t ⟨u, hu, eq⟩, eq ▸ ⟨u, generate_measurable.basic _ hu, rfl⟩)
end measurable_space
section measurable_functions
open measurable_space
lemma measurable_iff_le_map {m₁ : measurable_space α} {m₂ : measurable_space β} {f : α → β} :
measurable f ↔ m₂ ≤ m₁.map f :=
iff.rfl
alias measurable_iff_le_map ↔ measurable.le_map measurable.of_le_map
lemma measurable_iff_comap_le {m₁ : measurable_space α} {m₂ : measurable_space β} {f : α → β} :
measurable f ↔ m₂.comap f ≤ m₁ :=
comap_le_iff_le_map.symm
alias measurable_iff_comap_le ↔ measurable.comap_le measurable.of_comap_le
lemma measurable.mono {ma ma' : measurable_space α} {mb mb' : measurable_space β} {f : α → β}
(hf : @measurable α β ma mb f) (ha : ma ≤ ma') (hb : mb' ≤ mb) :
@measurable α β ma' mb' f :=
λ t ht, ha _ $ hf $ hb _ ht
@[measurability]
lemma measurable_from_top [measurable_space β] {f : α → β} : @measurable _ _ ⊤ _ f :=
λ s hs, trivial
lemma measurable_generate_from [measurable_space α] {s : set (set β)} {f : α → β}
(h : ∀ t ∈ s, measurable_set (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f :=
measurable.of_le_map $ generate_from_le h
variables [measurable_space α] [measurable_space β] [measurable_space γ]
@[measurability]
lemma measurable_set_preimage {f : α → β} {t : set β} (hf : measurable f) (ht : measurable_set t) :
measurable_set (f ⁻¹' t) :=
hf ht
@[measurability] lemma measurable.iterate {f : α → α} (hf : measurable f) : ∀ n, measurable (f^[n])
| 0 := measurable_id
| (n+1) := (measurable.iterate n).comp hf
@[nontriviality, measurability]
lemma subsingleton.measurable [subsingleton α] {f : α → β} : measurable f :=
λ s hs, @subsingleton.measurable_set α _ _ _
@[measurability]
lemma measurable.piecewise {s : set α} {_ : decidable_pred (∈ s)} {f g : α → β}
(hs : measurable_set s) (hf : measurable f) (hg : measurable g) :
measurable (piecewise s f g) :=
begin
intros t ht,
rw piecewise_preimage,
exact hs.ite (hf ht) (hg ht)
end
/-- this is slightly different from `measurable.piecewise`. It can be used to show
`measurable (ite (x=0) 0 1)` by
`exact measurable.ite (measurable_set_singleton 0) measurable_const measurable_const`,
but replacing `measurable.ite` by `measurable.piecewise` in that example proof does not work. -/
lemma measurable.ite {p : α → Prop} {_ : decidable_pred p} {f g : α → β}
(hp : measurable_set {a : α | p a}) (hf : measurable f) (hg : measurable g) :
measurable (λ x, ite (p x) (f x) (g x)) :=
measurable.piecewise hp hf hg
@[measurability]
lemma measurable.indicator [has_zero β] {s : set α} {f : α → β}
(hf : measurable f) (hs : measurable_set s) : measurable (s.indicator f) :=
hf.piecewise hs measurable_const
@[to_additive]
lemma measurable_one [has_one α] : measurable (1 : β → α) := @measurable_const _ _ _ _ 1
lemma measurable_of_empty [is_empty α] (f : α → β) : measurable f :=
begin
assume s hs,
convert measurable_set.empty,
exact eq_empty_of_is_empty _,
end
lemma measurable_of_empty_codomain [is_empty β] (f : α → β) : measurable f :=
by { haveI := function.is_empty f, exact measurable_of_empty f }
/-- A version of `measurable_const` that assumes `f x = f y` for all `x, y`. This version works
for functions between empty types. -/
lemma measurable_const' {f : β → α} (hf : ∀ x y, f x = f y) : measurable f :=
begin
casesI is_empty_or_nonempty β,
{ exact measurable_of_empty f },
{ convert measurable_const, exact funext (λ x, hf x h.some) }
end
@[to_additive, measurability] lemma measurable_set_mul_support [has_one β]
[measurable_singleton_class β] {f : α → β} (hf : measurable f) :
measurable_set (mul_support f) :=
hf (measurable_set_singleton 1).compl
attribute [measurability] measurable_set_support
/-- If a function coincides with a measurable function outside of a countable set, it is
measurable. -/
lemma measurable.measurable_of_countable_ne [measurable_singleton_class α]
{f g : α → β} (hf : measurable f) (h : countable {x | f x ≠ g x}) : measurable g :=
begin
assume t ht,
have : g ⁻¹' t = (g ⁻¹' t ∩ {x | f x = g x}ᶜ) ∪ (g ⁻¹' t ∩ {x | f x = g x}),
by simp [← inter_union_distrib_left],
rw this,
apply measurable_set.union (h.mono (inter_subset_right _ _)).measurable_set,
have : g ⁻¹' t ∩ {x : α | f x = g x} = f ⁻¹' t ∩ {x : α | f x = g x},
by { ext x, simp {contextual := tt} },
rw this,
exact (hf ht).inter h.measurable_set.of_compl,
end
lemma measurable_of_fintype [fintype α] [measurable_singleton_class α] (f : α → β) :
measurable f :=
λ s hs, (finite.of_fintype (f ⁻¹' s)).measurable_set
end measurable_functions
section constructions
variables [measurable_space α] [measurable_space β] [measurable_space γ]
instance : measurable_space empty := ⊤
instance : measurable_space punit := ⊤ -- this also works for `unit`
instance : measurable_space bool := ⊤
instance : measurable_space ℕ := ⊤
instance : measurable_space ℤ := ⊤
instance : measurable_space ℚ := ⊤
lemma measurable_to_encodable [encodable α] {f : β → α} (h : ∀ y, measurable_set (f ⁻¹' {f y})) :
measurable f :=
begin
assume s hs,
rw [← bUnion_preimage_singleton],
refine measurable_set.Union (λ y, measurable_set.Union_Prop $ λ hy, _),
by_cases hyf : y ∈ range f,
{ rcases hyf with ⟨y, rfl⟩,
apply h },
{ simp only [preimage_singleton_eq_empty.2 hyf, measurable_set.empty] }
end
@[measurability] lemma measurable_unit (f : unit → α) : measurable f :=
measurable_from_top
section nat
@[measurability] lemma measurable_from_nat {f : ℕ → α} : measurable f :=
measurable_from_top
lemma measurable_to_nat {f : α → ℕ} : (∀ y, measurable_set (f ⁻¹' {f y})) → measurable f :=
measurable_to_encodable
lemma measurable_find_greatest' {p : α → ℕ → Prop}
{N} (hN : ∀ k ≤ N, measurable_set {x | nat.find_greatest (p x) N = k}) :
measurable (λ x, nat.find_greatest (p x) N) :=
measurable_to_nat $ λ x, hN _ nat.find_greatest_le
lemma measurable_find_greatest {p : α → ℕ → Prop} {N} (hN : ∀ k ≤ N, measurable_set {x | p x k}) :
measurable (λ x, nat.find_greatest (p x) N) :=
begin
refine measurable_find_greatest' (λ k hk, _),
simp only [nat.find_greatest_eq_iff, set_of_and, set_of_forall, ← compl_set_of],
repeat { apply_rules [measurable_set.inter, measurable_set.const, measurable_set.Inter,
measurable_set.Inter_Prop, measurable_set.compl, hN]; try { intros } }
end
lemma measurable_find {p : α → ℕ → Prop} (hp : ∀ x, ∃ N, p x N)
(hm : ∀ k, measurable_set {x | p x k}) :
measurable (λ x, nat.find (hp x)) :=
begin
refine measurable_to_nat (λ x, _),
simp only [set.preimage, mem_singleton_iff, nat.find_eq_iff, set_of_and, set_of_forall,
← compl_set_of],
repeat { apply_rules [measurable_set.inter, hm, measurable_set.Inter, measurable_set.Inter_Prop,
measurable_set.compl]; try { intros } }
end
end nat
section subtype
instance {α} {p : α → Prop} [m : measurable_space α] : measurable_space (subtype p) :=
m.comap (coe : _ → α)
@[measurability] lemma measurable_subtype_coe {p : α → Prop} : measurable (coe : subtype p → α) :=
measurable_space.le_map_comap
@[measurability] lemma measurable.subtype_coe {p : β → Prop} {f : α → subtype p}
(hf : measurable f) :
measurable (λ a : α, (f a : β)) :=
measurable_subtype_coe.comp hf
@[measurability]
lemma measurable.subtype_mk {p : β → Prop} {f : α → β} (hf : measurable f) {h : ∀ x, p (f x)} :
measurable (λ x, (⟨f x, h x⟩ : subtype p)) :=
λ t ⟨s, hs⟩, hs.2 ▸ by simp only [← preimage_comp, (∘), subtype.coe_mk, hf hs.1]
lemma measurable_set.subtype_image {s : set α} {t : set s}
(hs : measurable_set s) : measurable_set t → measurable_set ((coe : s → α) '' t)
| ⟨u, (hu : measurable_set u), (eq : coe ⁻¹' u = t)⟩ :=
begin
rw [← eq, subtype.image_preimage_coe],
exact hu.inter hs
end
lemma measurable_of_measurable_union_cover
{f : α → β} (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (h : univ ⊆ s ∪ t)
(hc : measurable (λ a : s, f a)) (hd : measurable (λ a : t, f a)) :
measurable f :=
begin
intros u hu,
convert (hs.subtype_image (hc hu)).union (ht.subtype_image (hd hu)),
change f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) ∪ coe '' (coe ⁻¹' (f ⁻¹' u) : set t),
rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, subtype.range_coe,
subtype.range_coe, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ],
end
instance {α} {p : α → Prop} [measurable_space α] [measurable_singleton_class α] :
measurable_singleton_class (subtype p) :=
{ measurable_set_singleton := λ x,
begin
have : measurable_set {(x : α)} := measurable_set_singleton _,
convert @measurable_subtype_coe α _ p _ this,
ext y,
simp [subtype.ext_iff],
end }
lemma measurable_of_measurable_on_compl_finite [measurable_singleton_class α]
{f : α → β} (s : set α) (hs : finite s) (hf : measurable (set.restrict f sᶜ)) :
measurable f :=
begin
letI : fintype s := finite.fintype hs,
exact measurable_of_measurable_union_cover s sᶜ hs.measurable_set hs.measurable_set.compl
(by simp only [union_compl_self]) (measurable_of_fintype _) hf
end
lemma measurable_of_measurable_on_compl_singleton [measurable_singleton_class α]
{f : α → β} (a : α) (hf : measurable (set.restrict f {x | x ≠ a})) :
measurable f :=
measurable_of_measurable_on_compl_finite {a} (finite_singleton a) hf
end subtype
section prod
instance {α β} [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α × β) :=
m₁.comap prod.fst ⊔ m₂.comap prod.snd
@[measurability] lemma measurable_fst : measurable (prod.fst : α × β → α) :=
measurable.of_comap_le le_sup_left
lemma measurable.fst {f : α → β × γ} (hf : measurable f) :
measurable (λ a : α, (f a).1) :=
measurable_fst.comp hf
@[measurability] lemma measurable_snd : measurable (prod.snd : α × β → β) :=
measurable.of_comap_le le_sup_right
lemma measurable.snd {f : α → β × γ} (hf : measurable f) :
measurable (λ a : α, (f a).2) :=
measurable_snd.comp hf
@[measurability] lemma measurable.prod {f : α → β × γ}
(hf₁ : measurable (λ a, (f a).1)) (hf₂ : measurable (λ a, (f a).2)) : measurable f :=
measurable.of_le_map $ sup_le
(by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₁ })
(by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₂ })
lemma measurable_prod {f : α → β × γ} : measurable f ↔
measurable (λ a, (f a).1) ∧ measurable (λ a, (f a).2) :=
⟨λ hf, ⟨measurable_fst.comp hf, measurable_snd.comp hf⟩, λ h, measurable.prod h.1 h.2⟩
lemma measurable.prod_mk {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) :
measurable (λ a : α, (f a, g a)) :=
measurable.prod hf hg
lemma measurable_prod_mk_left {x : α} : measurable (@prod.mk _ β x) :=
measurable_const.prod_mk measurable_id
lemma measurable_prod_mk_right {y : β} : measurable (λ x : α, (x, y)) :=
measurable_id.prod_mk measurable_const
lemma measurable.prod_map [measurable_space δ] {f : α → β} {g : γ → δ} (hf : measurable f)
(hg : measurable g) : measurable (prod.map f g) :=
(hf.comp measurable_fst).prod_mk (hg.comp measurable_snd)
lemma measurable.of_uncurry_left {f : α → β → γ} (hf : measurable (uncurry f)) {x : α} :
measurable (f x) :=
hf.comp measurable_prod_mk_left
lemma measurable.of_uncurry_right {f : α → β → γ} (hf : measurable (uncurry f)) {y : β} :
measurable (λ x, f x y) :=
hf.comp measurable_prod_mk_right
@[measurability] lemma measurable_swap : measurable (prod.swap : α × β → β × α) :=
measurable.prod measurable_snd measurable_fst
lemma measurable_swap_iff {f : α × β → γ} : measurable (f ∘ prod.swap) ↔ measurable f :=
⟨λ hf, by { convert hf.comp measurable_swap, ext ⟨x, y⟩, refl }, λ hf, hf.comp measurable_swap⟩
@[measurability]
lemma measurable_set.prod {s : set α} {t : set β} (hs : measurable_set s) (ht : measurable_set t) :
measurable_set (s.prod t) :=
measurable_set.inter (measurable_fst hs) (measurable_snd ht)
lemma measurable_set_prod_of_nonempty {s : set α} {t : set β} (h : (s.prod t).nonempty) :
measurable_set (s.prod t) ↔ measurable_set s ∧ measurable_set t :=
begin
rcases h with ⟨⟨x, y⟩, hx, hy⟩,
refine ⟨λ hst, _, λ h, h.1.prod h.2⟩,
have : measurable_set ((λ x, (x, y)) ⁻¹' s.prod t) := measurable_id.prod_mk measurable_const hst,
have : measurable_set (prod.mk x ⁻¹' s.prod t) := measurable_const.prod_mk measurable_id hst,
simp * at *
end
lemma measurable_set_prod {s : set α} {t : set β} :
measurable_set (s.prod t) ↔ (measurable_set s ∧ measurable_set t) ∨ s = ∅ ∨ t = ∅ :=
begin
cases (s.prod t).eq_empty_or_nonempty with h h,
{ simp [h, prod_eq_empty_iff.mp h] },
{ simp [←not_nonempty_iff_eq_empty, prod_nonempty_iff.mp h, measurable_set_prod_of_nonempty h] }
end
lemma measurable_set_swap_iff {s : set (α × β)} :
measurable_set (prod.swap ⁻¹' s) ↔ measurable_set s :=
⟨λ hs, by { convert measurable_swap hs, ext ⟨x, y⟩, refl }, λ hs, measurable_swap hs⟩
lemma measurable_from_prod_encodable [encodable β] [measurable_singleton_class β]
{f : α × β → γ} (hf : ∀ y, measurable (λ x, f (x, y))) :
measurable f :=
begin
intros s hs,
have : f ⁻¹' s = ⋃ y, ((λ x, f (x, y)) ⁻¹' s).prod {y},
{ ext1 ⟨x, y⟩,
simp [and_assoc, and.left_comm] },
rw this,
exact measurable_set.Union (λ y, (hf y hs).prod (measurable_set_singleton y))
end
end prod
section pi
variables {π : δ → Type*}
instance measurable_space.pi [m : Π a, measurable_space (π a)] : measurable_space (Π a, π a) :=
⨆ a, (m a).comap (λ b, b a)
variables [Π a, measurable_space (π a)] [measurable_space γ]
lemma measurable_pi_iff {g : α → Π a, π a} :
measurable g ↔ ∀ a, measurable (λ x, g x a) :=
by simp_rw [measurable_iff_comap_le, measurable_space.pi, measurable_space.comap_supr,
measurable_space.comap_comp, function.comp, supr_le_iff]
@[measurability]
lemma measurable_pi_apply (a : δ) : measurable (λ f : Π a, π a, f a) :=
measurable.of_comap_le $ le_supr _ a
@[measurability]
lemma measurable.eval {a : δ} {g : α → Π a, π a}
(hg : measurable g) : measurable (λ x, g x a) :=
(measurable_pi_apply a).comp hg
@[measurability]
lemma measurable_pi_lambda (f : α → Π a, π a) (hf : ∀ a, measurable (λ c, f c a)) :
measurable f :=
measurable_pi_iff.mpr hf
/-- The function `update f a : π a → Π a, π a` is always measurable.
This doesn't require `f` to be measurable.
This should not be confused with the statement that `update f a x` is measurable. -/
@[measurability]
lemma measurable_update (f : Π (a : δ), π a) {a : δ} : measurable (update f a) :=
begin
apply measurable_pi_lambda,
intro x, by_cases hx : x = a,
{ cases hx, convert measurable_id, ext, simp },
simp_rw [update_noteq hx], apply measurable_const,
end
/- Even though we cannot use projection notation, we still keep a dot to be consistent with similar
lemmas, like `measurable_set.prod`. -/
@[measurability]
lemma measurable_set.pi {s : set δ} {t : Π i : δ, set (π i)} (hs : countable s)
(ht : ∀ i ∈ s, measurable_set (t i)) :
measurable_set (s.pi t) :=
by { rw [pi_def], exact measurable_set.bInter hs (λ i hi, measurable_pi_apply _ (ht i hi)) }
lemma measurable_set.univ_pi [encodable δ] {t : Π i : δ, set (π i)}
(ht : ∀ i, measurable_set (t i)) : measurable_set (pi univ t) :=
measurable_set.pi (countable_encodable _) (λ i _, ht i)
lemma measurable_set_pi_of_nonempty {s : set δ} {t : Π i, set (π i)} (hs : countable s)
(h : (pi s t).nonempty) : measurable_set (pi s t) ↔ ∀ i ∈ s, measurable_set (t i) :=
begin
rcases h with ⟨f, hf⟩, refine ⟨λ hst i hi, _, measurable_set.pi hs⟩,
convert measurable_update f hst, rw [update_preimage_pi hi], exact λ j hj _, hf j hj
end
lemma measurable_set_pi {s : set δ} {t : Π i, set (π i)} (hs : countable s) :
measurable_set (pi s t) ↔ (∀ i ∈ s, measurable_set (t i)) ∨ pi s t = ∅ :=
begin
cases (pi s t).eq_empty_or_nonempty with h h,
{ simp [h] },
{ simp [measurable_set_pi_of_nonempty hs, h, ← not_nonempty_iff_eq_empty] }
end
section fintype
local attribute [instance] fintype.encodable
lemma measurable_set.pi_fintype [fintype δ] {s : set δ} {t : Π i, set (π i)}
(ht : ∀ i ∈ s, measurable_set (t i)) : measurable_set (pi s t) :=
measurable_set.pi (countable_encodable _) ht
lemma measurable_set.univ_pi_fintype [fintype δ] {t : Π i, set (π i)}
(ht : ∀ i, measurable_set (t i)) : measurable_set (pi univ t) :=
measurable_set.pi_fintype (λ i _, ht i)
end fintype
end pi
instance tprod.measurable_space (π : δ → Type*) [∀ x, measurable_space (π x)] :
∀ (l : list δ), measurable_space (list.tprod π l)
| [] := punit.measurable_space
| (i :: is) := @prod.measurable_space _ _ _ (tprod.measurable_space is)
section tprod
open list
variables {π : δ → Type*} [∀ x, measurable_space (π x)]
lemma measurable_tprod_mk (l : list δ) : measurable (@tprod.mk δ π l) :=
begin
induction l with i l ih,
{ exact measurable_const },
{ exact (measurable_pi_apply i).prod_mk ih }
end
lemma measurable_tprod_elim : ∀ {l : list δ} {i : δ} (hi : i ∈ l),
measurable (λ (v : tprod π l), v.elim hi)
| (i :: is) j hj := begin
by_cases hji : j = i,
{ subst hji, simp [measurable_fst] },
{ rw [funext $ tprod.elim_of_ne _ hji],
exact (measurable_tprod_elim (hj.resolve_left hji)).comp measurable_snd }
end
lemma measurable_tprod_elim' {l : list δ} (h : ∀ i, i ∈ l) :
measurable (tprod.elim' h : tprod π l → Π i, π i) :=
measurable_pi_lambda _ (λ i, measurable_tprod_elim (h i))
lemma measurable_set.tprod (l : list δ) {s : ∀ i, set (π i)} (hs : ∀ i, measurable_set (s i)) :
measurable_set (set.tprod l s) :=
by { induction l with i l ih, exact measurable_set.univ, exact (hs i).prod ih }
end tprod
instance {α β} [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α ⊕ β) :=
m₁.map sum.inl ⊓ m₂.map sum.inr
section sum
@[measurability] lemma measurable_inl : measurable (@sum.inl α β) :=
measurable.of_le_map inf_le_left
@[measurability] lemma measurable_inr : measurable (@sum.inr α β) :=
measurable.of_le_map inf_le_right
lemma measurable_sum {f : α ⊕ β → γ}
(hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f :=
measurable.of_comap_le $ le_inf
(measurable_space.comap_le_iff_le_map.2 $ hl)
(measurable_space.comap_le_iff_le_map.2 $ hr)
@[measurability]
lemma measurable.sum_elim {f : α → γ} {g : β → γ} (hf : measurable f) (hg : measurable g) :
measurable (sum.elim f g) :=
measurable_sum hf hg
lemma measurable_set.inl_image {s : set α} (hs : measurable_set s) :
measurable_set (sum.inl '' s : set (α ⊕ β)) :=
⟨show measurable_set (sum.inl ⁻¹' _), by { rwa [preimage_image_eq], exact (λ a b, sum.inl.inj) },
have sum.inr ⁻¹' (sum.inl '' s : set (α ⊕ β)) = ∅ :=
eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction,
show measurable_set (sum.inr ⁻¹' _), by { rw [this], exact measurable_set.empty }⟩
lemma measurable_set_range_inl : measurable_set (range sum.inl : set (α ⊕ β)) :=
by { rw [← image_univ], exact measurable_set.univ.inl_image }
lemma measurable_set_inr_image {s : set β} (hs : measurable_set s) :
measurable_set (sum.inr '' s : set (α ⊕ β)) :=
⟨ have sum.inl ⁻¹' (sum.inr '' s : set (α ⊕ β)) = ∅ :=
eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction,
show measurable_set (sum.inl ⁻¹' _), by { rw [this], exact measurable_set.empty },
show measurable_set (sum.inr ⁻¹' _), by { rwa [preimage_image_eq], exact λ a b, sum.inr.inj }⟩
lemma measurable_set_range_inr : measurable_set (range sum.inr : set (α ⊕ β)) :=
by { rw [← image_univ], exact measurable_set_inr_image measurable_set.univ }
end sum
instance {α} {β : α → Type*} [m : Πa, measurable_space (β a)] : measurable_space (sigma β) :=
⨅a, (m a).map (sigma.mk a)
end constructions
/-- Equivalences between measurable spaces. Main application is the simplification of measurability
statements along measurable equivalences. -/
structure measurable_equiv (α β : Type*) [measurable_space α] [measurable_space β] extends α ≃ β :=
(measurable_to_fun : measurable to_fun)
(measurable_inv_fun : measurable inv_fun)
infix ` ≃ᵐ `:25 := measurable_equiv
namespace measurable_equiv
variables (α β) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ]
instance : has_coe_to_fun (α ≃ᵐ β) :=
⟨λ _, α → β, λ e, e.to_equiv⟩
variables {α β}
lemma coe_eq (e : α ≃ᵐ β) : (e : α → β) = e.to_equiv := rfl
@[measurability]
protected lemma measurable (e : α ≃ᵐ β) : measurable (e : α → β) :=
e.measurable_to_fun
@[simp] lemma coe_mk (e : α ≃ β) (h1 : measurable e) (h2 : measurable e.symm) :
((⟨e, h1, h2⟩ : α ≃ᵐ β) : α → β) = e := rfl
/-- Any measurable space is equivalent to itself. -/
def refl (α : Type*) [measurable_space α] : α ≃ᵐ α :=
{ to_equiv := equiv.refl α,
measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id }
instance : inhabited (α ≃ᵐ α) := ⟨refl α⟩
/-- The composition of equivalences between measurable spaces. -/
@[simps] def trans (ab : α ≃ᵐ β) (bc : β ≃ᵐ γ) :
α ≃ᵐ γ :=
{ to_equiv := ab.to_equiv.trans bc.to_equiv,
measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun,
measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun }
/-- The inverse of an equivalence between measurable spaces. -/
@[simps] def symm (ab : α ≃ᵐ β) : β ≃ᵐ α :=
{ to_equiv := ab.to_equiv.symm,
measurable_to_fun := ab.measurable_inv_fun,
measurable_inv_fun := ab.measurable_to_fun }
@[simp] lemma coe_symm_mk (e : α ≃ β) (h1 : measurable e) (h2 : measurable e.symm) :
((⟨e, h1, h2⟩ : α ≃ᵐ β).symm : β → α) = e.symm := rfl
@[simp] theorem symm_comp_self (e : α ≃ᵐ β) : e.symm ∘ e = id := funext e.left_inv
@[simp] theorem self_comp_symm (e : α ≃ᵐ β) : e ∘ e.symm = id := funext e.right_inv
/-- Equal measurable spaces are equivalent. -/
protected def cast {α β} [i₁ : measurable_space α] [i₂ : measurable_space β]
(h : α = β) (hi : i₁ == i₂) : α ≃ᵐ β :=
{ to_equiv := equiv.cast h,
measurable_to_fun := by { substI h, substI hi, exact measurable_id },
measurable_inv_fun := by { substI h, substI hi, exact measurable_id }}
protected lemma measurable_coe_iff {f : β → γ} (e : α ≃ᵐ β) :
measurable (f ∘ e) ↔ measurable f :=
iff.intro
(assume hfe,
have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable,
by rwa [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this)
(λ h, h.comp e.measurable)
/-- Products of equivalent measurable spaces are equivalent. -/
def prod_congr (ab : α ≃ᵐ β) (cd : γ ≃ᵐ δ) : α × γ ≃ᵐ β × δ :=
{ to_equiv := prod_congr ab.to_equiv cd.to_equiv,
measurable_to_fun := (ab.measurable_to_fun.comp measurable_id.fst).prod_mk
(cd.measurable_to_fun.comp measurable_id.snd),
measurable_inv_fun := (ab.measurable_inv_fun.comp measurable_id.fst).prod_mk
(cd.measurable_inv_fun.comp measurable_id.snd) }
/-- Products of measurable spaces are symmetric. -/
def prod_comm : α × β ≃ᵐ β × α :=
{ to_equiv := prod_comm α β,
measurable_to_fun := measurable_id.snd.prod_mk measurable_id.fst,
measurable_inv_fun := measurable_id.snd.prod_mk measurable_id.fst }
/-- Products of measurable spaces are associative. -/
def prod_assoc : (α × β) × γ ≃ᵐ α × (β × γ) :=
{ to_equiv := prod_assoc α β γ,
measurable_to_fun := measurable_fst.fst.prod_mk $ measurable_fst.snd.prod_mk measurable_snd,
measurable_inv_fun := (measurable_fst.prod_mk measurable_snd.fst).prod_mk measurable_snd.snd }
/-- Sums of measurable spaces are symmetric. -/
def sum_congr (ab : α ≃ᵐ β) (cd : γ ≃ᵐ δ) : α ⊕ γ ≃ᵐ β ⊕ δ :=
{ to_equiv := sum_congr ab.to_equiv cd.to_equiv,
measurable_to_fun :=
begin
cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd',
refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm)
end,
measurable_inv_fun :=
begin
cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd',
refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm)
end }
/-- `set.prod s t ≃ (s × t)` as measurable spaces. -/
def set.prod (s : set α) (t : set β) : s.prod t ≃ᵐ s × t :=
{ to_equiv := equiv.set.prod s t,
measurable_to_fun := measurable_id.subtype_coe.fst.subtype_mk.prod_mk
measurable_id.subtype_coe.snd.subtype_mk,
measurable_inv_fun := measurable.subtype_mk $ measurable_id.fst.subtype_coe.prod_mk
measurable_id.snd.subtype_coe }
/-- `univ α ≃ α` as measurable spaces. -/
def set.univ (α : Type*) [measurable_space α] : (univ : set α) ≃ᵐ α :=
{ to_equiv := equiv.set.univ α,
measurable_to_fun := measurable_id.subtype_coe,
measurable_inv_fun := measurable_id.subtype_mk }
/-- `{a} ≃ unit` as measurable spaces. -/
def set.singleton (a : α) : ({a} : set α) ≃ᵐ unit :=
{ to_equiv := equiv.set.singleton a,
measurable_to_fun := measurable_const,
measurable_inv_fun := measurable_const }
/-- A set is equivalent to its image under a function `f` as measurable spaces,
if `f` is an injective measurable function that sends measurable sets to measurable sets. -/
noncomputable def set.image (f : α → β) (s : set α) (hf : injective f)
(hfm : measurable f) (hfi : ∀ s, measurable_set s → measurable_set (f '' s)) : s ≃ᵐ (f '' s) :=
{ to_equiv := equiv.set.image f s hf,
measurable_to_fun := (hfm.comp measurable_id.subtype_coe).subtype_mk,
measurable_inv_fun :=
begin
rintro t ⟨u, hu, rfl⟩, simp [preimage_preimage, set.image_symm_preimage hf],
exact measurable_subtype_coe (hfi u hu)
end }
/-- The domain of `f` is equivalent to its range as measurable spaces,
if `f` is an injective measurable function that sends measurable sets to measurable sets. -/
noncomputable def set.range (f : α → β) (hf : injective f) (hfm : measurable f)
(hfi : ∀ s, measurable_set s → measurable_set (f '' s)) :
α ≃ᵐ (range f) :=
(measurable_equiv.set.univ _).symm.trans $
(measurable_equiv.set.image f univ hf hfm hfi).trans $
measurable_equiv.cast (by rw image_univ) (by rw image_univ)
/-- `α` is equivalent to its image in `α ⊕ β` as measurable spaces. -/
def set.range_inl : (range sum.inl : set (α ⊕ β)) ≃ᵐ α :=
{ to_fun := λ ab, match ab with
| ⟨sum.inl a, _⟩ := a
| ⟨sum.inr b, p⟩ := have false, by { cases p, contradiction }, this.elim
end,
inv_fun := λ a, ⟨sum.inl a, a, rfl⟩,
left_inv := by { rintro ⟨ab, a, rfl⟩, refl },
right_inv := assume a, rfl,
measurable_to_fun := assume s (hs : measurable_set s),
begin
refine ⟨_, hs.inl_image, set.ext _⟩,
rintros ⟨ab, a, rfl⟩,
simp [set.range_inl._match_1]
end,
measurable_inv_fun := measurable.subtype_mk measurable_inl }
/-- `β` is equivalent to its image in `α ⊕ β` as measurable spaces. -/
def set.range_inr : (range sum.inr : set (α ⊕ β)) ≃ᵐ β :=
{ to_fun := λ ab, match ab with
| ⟨sum.inr b, _⟩ := b
| ⟨sum.inl a, p⟩ := have false, by { cases p, contradiction }, this.elim
end,
inv_fun := λ b, ⟨sum.inr b, b, rfl⟩,
left_inv := by { rintro ⟨ab, b, rfl⟩, refl },
right_inv := assume b, rfl,
measurable_to_fun := assume s (hs : measurable_set s),
begin
refine ⟨_, measurable_set_inr_image hs, set.ext _⟩,
rintros ⟨ab, b, rfl⟩,
simp [set.range_inr._match_1]
end,
measurable_inv_fun := measurable.subtype_mk measurable_inr }
/-- Products distribute over sums (on the right) as measurable spaces. -/
def sum_prod_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] :
(α ⊕ β) × γ ≃ᵐ (α × γ) ⊕ (β × γ) :=
{ to_equiv := sum_prod_distrib α β γ,
measurable_to_fun :=
begin
refine measurable_of_measurable_union_cover
((range sum.inl).prod univ)
((range sum.inr).prod univ)
(measurable_set_range_inl.prod measurable_set.univ)
(measurable_set_range_inr.prod measurable_set.univ)
(by { rintro ⟨a|b, c⟩; simp [set.prod_eq] })
_
_,
{ refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _,
refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _,
dsimp [(∘)],
convert measurable_inl,
ext ⟨a, c⟩, refl },
{ refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _,
refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _,
dsimp [(∘)],
convert measurable_inr,
ext ⟨b, c⟩, refl }
end,
measurable_inv_fun :=
measurable_sum
((measurable_inl.comp measurable_fst).prod_mk measurable_snd)
((measurable_inr.comp measurable_fst).prod_mk measurable_snd) }
/-- Products distribute over sums (on the left) as measurable spaces. -/
def prod_sum_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] :
α × (β ⊕ γ) ≃ᵐ (α × β) ⊕ (α × γ) :=
prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm
/-- Products distribute over sums as measurable spaces. -/
def sum_prod_sum (α β γ δ)
[measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] :
(α ⊕ β) × (γ ⊕ δ) ≃ᵐ ((α × γ) ⊕ (α × δ)) ⊕ ((β × γ) ⊕ (β × δ)) :=
(sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _)
variables {π π' : δ' → Type*} [∀ x, measurable_space (π x)] [∀ x, measurable_space (π' x)]
/-- A family of measurable equivalences `Π a, β₁ a ≃ᵐ β₂ a` generates a measurable equivalence
between `Π a, β₁ a` and `Π a, β₂ a`. -/
def Pi_congr_right (e : Π a, π a ≃ᵐ π' a) : (Π a, π a) ≃ᵐ (Π a, π' a) :=
{ to_equiv := Pi_congr_right (λ a, (e a).to_equiv),
measurable_to_fun :=
measurable_pi_lambda _ (λ i, (e i).measurable_to_fun.comp (measurable_pi_apply i)),
measurable_inv_fun :=
measurable_pi_lambda _ (λ i, (e i).measurable_inv_fun.comp (measurable_pi_apply i)) }
/-- Pi-types are measurably equivalent to iterated products. -/
noncomputable def pi_measurable_equiv_tprod {l : list δ'} (hnd : l.nodup) (h : ∀ i, i ∈ l) :
(Π i, π i) ≃ᵐ list.tprod π l :=
{ to_equiv := list.tprod.pi_equiv_tprod hnd h,
measurable_to_fun := measurable_tprod_mk l,
measurable_inv_fun := measurable_tprod_elim' h }
end measurable_equiv
namespace filter
variables [measurable_space α]
/-- A filter `f` is measurably generates if each `s ∈ f` includes a measurable `t ∈ f`. -/
class is_measurably_generated (f : filter α) : Prop :=
(exists_measurable_subset : ∀ ⦃s⦄, s ∈ f → ∃ t ∈ f, measurable_set t ∧ t ⊆ s)
instance is_measurably_generated_bot : is_measurably_generated (⊥ : filter α) :=
⟨λ _ _, ⟨∅, mem_bot, measurable_set.empty, empty_subset _⟩⟩
instance is_measurably_generated_top : is_measurably_generated (⊤ : filter α) :=
⟨λ s hs, ⟨univ, univ_mem, measurable_set.univ, λ x _, hs x⟩⟩
lemma eventually.exists_measurable_mem {f : filter α} [is_measurably_generated f]
{p : α → Prop} (h : ∀ᶠ x in f, p x) :
∃ s ∈ f, measurable_set s ∧ ∀ x ∈ s, p x :=
is_measurably_generated.exists_measurable_subset h
lemma eventually.exists_measurable_mem_of_lift' {f : filter α} [is_measurably_generated f]
{p : set α → Prop} (h : ∀ᶠ s in f.lift' powerset, p s) :
∃ s ∈ f, measurable_set s ∧ p s :=
let ⟨s, hsf, hs⟩ := eventually_lift'_powerset.1 h,
⟨t, htf, htm, hts⟩ := is_measurably_generated.exists_measurable_subset hsf
in ⟨t, htf, htm, hs t hts⟩
instance inf_is_measurably_generated (f g : filter α) [is_measurably_generated f]
[is_measurably_generated g] :
is_measurably_generated (f ⊓ g) :=
begin
refine ⟨_⟩,
rintros t ⟨sf, hsf, sg, hsg, rfl⟩,
rcases is_measurably_generated.exists_measurable_subset hsf with ⟨s'f, hs'f, hmf, hs'sf⟩,
rcases is_measurably_generated.exists_measurable_subset hsg with ⟨s'g, hs'g, hmg, hs'sg⟩,
refine ⟨s'f ∩ s'g, inter_mem_inf hs'f hs'g, hmf.inter hmg, _⟩,
exact inter_subset_inter hs'sf hs'sg
end
lemma principal_is_measurably_generated_iff {s : set α} :
is_measurably_generated (𝓟 s) ↔ measurable_set s :=
begin
refine ⟨_, λ hs, ⟨λ t ht, ⟨s, mem_principal_self s, hs, ht⟩⟩⟩,
rintros ⟨hs⟩,
rcases hs (mem_principal_self s) with ⟨t, ht, htm, hts⟩,
have : t = s := subset.antisymm hts ht,
rwa ← this
end
alias principal_is_measurably_generated_iff ↔
_ measurable_set.principal_is_measurably_generated
instance infi_is_measurably_generated {f : ι → filter α} [∀ i, is_measurably_generated (f i)] :
is_measurably_generated (⨅ i, f i) :=
begin
refine ⟨λ s hs, _⟩,
rw [← equiv.plift.surjective.infi_comp, mem_infi] at hs,
rcases hs with ⟨t, ht, ⟨V, hVf, rfl⟩⟩,
choose U hUf hU using λ i, is_measurably_generated.exists_measurable_subset (hVf i),
refine ⟨⋂ i : t, U i, _, _, _⟩,
{ rw [← equiv.plift.surjective.infi_comp, mem_infi],
refine ⟨t, ht, U, hUf, rfl⟩ },
{ haveI := ht.countable.to_encodable,
refine measurable_set.Inter (λ i, (hU i).1) },
{ exact Inter_subset_Inter (λ i, (hU i).2) }
end
end filter
/-- We say that a collection of sets is countably spanning if a countable subset spans the
whole type. This is a useful condition in various parts of measure theory. For example, it is
a needed condition to show that the product of two collections generate the product sigma algebra,
see `generate_from_prod_eq`. -/
def is_countably_spanning (C : set (set α)) : Prop :=
∃ (s : ℕ → set α), (∀ n, s n ∈ C) ∧ (⋃ n, s n) = univ
lemma is_countably_spanning_measurable_set [measurable_space α] :
is_countably_spanning {s : set α | measurable_set s} :=
⟨λ _, univ, λ _, measurable_set.univ, Union_const _⟩
namespace measurable_set
/-!
### Typeclasses on `subtype measurable_set`
-/
variables [measurable_space α]
instance : has_mem α (subtype (measurable_set : set α → Prop)) :=
⟨λ a s, a ∈ (s : set α)⟩
@[simp] lemma mem_coe (a : α) (s : subtype (measurable_set : set α → Prop)) :
a ∈ (s : set α) ↔ a ∈ s := iff.rfl
instance : has_emptyc (subtype (measurable_set : set α → Prop)) :=
⟨⟨∅, measurable_set.empty⟩⟩
@[simp] lemma coe_empty : ↑(∅ : subtype (measurable_set : set α → Prop)) = (∅ : set α) := rfl
instance [measurable_singleton_class α] : has_insert α (subtype (measurable_set : set α → Prop)) :=
⟨λ a s, ⟨has_insert.insert a s, s.prop.insert a⟩⟩
@[simp] lemma coe_insert [measurable_singleton_class α] (a : α)
(s : subtype (measurable_set : set α → Prop)) :
↑(has_insert.insert a s) = (has_insert.insert a s : set α) := rfl
instance : has_compl (subtype (measurable_set : set α → Prop)) :=
⟨λ x, ⟨xᶜ, x.prop.compl⟩⟩
@[simp] lemma coe_compl (s : subtype (measurable_set : set α → Prop)) : ↑(sᶜ) = (sᶜ : set α) := rfl
instance : has_union (subtype (measurable_set : set α → Prop)) :=
⟨λ x y, ⟨x ∪ y, x.prop.union y.prop⟩⟩
@[simp] lemma coe_union (s t : subtype (measurable_set : set α → Prop)) :
↑(s ∪ t) = (s ∪ t : set α) := rfl
instance : has_inter (subtype (measurable_set : set α → Prop)) :=
⟨λ x y, ⟨x ∩ y, x.prop.inter y.prop⟩⟩
@[simp] lemma coe_inter (s t : subtype (measurable_set : set α → Prop)) :
↑(s ∩ t) = (s ∩ t : set α) := rfl
instance : has_sdiff (subtype (measurable_set : set α → Prop)) :=
⟨λ x y, ⟨x \ y, x.prop.diff y.prop⟩⟩
@[simp] lemma coe_sdiff (s t : subtype (measurable_set : set α → Prop)) :
↑(s \ t) = (s \ t : set α) := rfl
instance : has_bot (subtype (measurable_set : set α → Prop)) :=
⟨⟨⊥, measurable_set.empty⟩⟩
@[simp] lemma coe_bot : ↑(⊥ : subtype (measurable_set : set α → Prop)) = (⊥ : set α) := rfl
instance : has_top (subtype (measurable_set : set α → Prop)) :=
⟨⟨⊤, measurable_set.univ⟩⟩
@[simp] lemma coe_top : ↑(⊤ : subtype (measurable_set : set α → Prop)) = (⊤ : set α) := rfl
instance : partial_order (subtype (measurable_set : set α → Prop)) :=
partial_order.lift _ subtype.coe_injective
instance : bounded_distrib_lattice (subtype (measurable_set : set α → Prop)) :=
{ sup := (∪),
le_sup_left := λ a b, show (a : set α) ≤ a ⊔ b, from le_sup_left,
le_sup_right := λ a b, show (b : set α) ≤ a ⊔ b, from le_sup_right,
sup_le := λ a b c ha hb, show (a ⊔ b : set α) ≤ c, from sup_le ha hb,
inf := (∩),
inf_le_left := λ a b, show (a ⊓ b : set α) ≤ a, from inf_le_left,
inf_le_right := λ a b, show (a ⊓ b : set α) ≤ b, from inf_le_right,
le_inf := λ a b c ha hb, show (a : set α) ≤ b ⊓ c, from le_inf ha hb,
top := ⊤,
le_top := λ a, show (a : set α) ≤ ⊤, from le_top,
bot := ⊥,
bot_le := λ a, show (⊥ : set α) ≤ a, from bot_le,
le_sup_inf := λ x y z, show ((x ⊔ y) ⊓ (x ⊔ z) : set α) ≤ x ⊔ y ⊓ z, from le_sup_inf,
.. measurable_set.subtype.partial_order }
instance : boolean_algebra (subtype (measurable_set : set α → Prop)) :=
{ sdiff := (\),
sup_inf_sdiff := λ a b, subtype.eq $ sup_inf_sdiff a b,
inf_inf_sdiff := λ a b, subtype.eq $ inf_inf_sdiff a b,
compl := has_compl.compl,
inf_compl_le_bot := λ a, boolean_algebra.inf_compl_le_bot (a : set α),
top_le_sup_compl := λ a, boolean_algebra.top_le_sup_compl (a : set α),
sdiff_eq := λ a b, subtype.eq $ sdiff_eq,
.. measurable_set.subtype.bounded_distrib_lattice }
end measurable_set
|
9ee463281fee88ecf04691838a3f4ca9ba589c22 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/arrow.lean | b63a0c942e06b1dcf13e71760a04a574d571f585 | [
"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 | 8,371 | lean | /-
Copyright (c) 2020 Markus Himmel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Markus Himmel
-/
import category_theory.comma
/-!
# The category of arrows
The category of arrows, with morphisms commutative squares.
We set this up as a specialization of the comma category `comma L R`,
where `L` and `R` are both the identity functor.
We also define the typeclass `has_lift`, representing a choice of a lift
of a commutative square (that is, a diagonal morphism making the two triangles commute).
## Tags
comma, arrow
-/
namespace category_theory
universes v u -- morphism levels before object levels. See note [category_theory universes].
variables {T : Type u} [category.{v} T]
section
variables (T)
/-- The arrow category of `T` has as objects all morphisms in `T` and as morphisms commutative
squares in `T`. -/
@[derive category]
def arrow := comma.{v v v} (𝟭 T) (𝟭 T)
-- Satisfying the inhabited linter
instance arrow.inhabited [inhabited T] : inhabited (arrow T) :=
{ default := show comma (𝟭 T) (𝟭 T), from default (comma (𝟭 T) (𝟭 T)) }
end
namespace arrow
@[simp] lemma id_left (f : arrow T) : comma_morphism.left (𝟙 f) = 𝟙 (f.left) := rfl
@[simp] lemma id_right (f : arrow T) : comma_morphism.right (𝟙 f) = 𝟙 (f.right) := rfl
/-- An object in the arrow category is simply a morphism in `T`. -/
@[simps]
def mk {X Y : T} (f : X ⟶ Y) : arrow T :=
{ left := X,
right := Y,
hom := f }
theorem mk_injective (A B : T) :
function.injective (arrow.mk : (A ⟶ B) → arrow T) :=
λ f g h, by { cases h, refl }
theorem mk_inj (A B : T) {f g : A ⟶ B} : arrow.mk f = arrow.mk g ↔ f = g :=
(mk_injective A B).eq_iff
instance {X Y : T} : has_coe (X ⟶ Y) (arrow T) := ⟨mk⟩
/-- A morphism in the arrow category is a commutative square connecting two objects of the arrow
category. -/
@[simps]
def hom_mk {f g : arrow T} {u : f.left ⟶ g.left} {v : f.right ⟶ g.right}
(w : u ≫ g.hom = f.hom ≫ v) : f ⟶ g :=
{ left := u,
right := v,
w' := w }
/-- We can also build a morphism in the arrow category out of any commutative square in `T`. -/
@[simps]
def hom_mk' {X Y : T} {f : X ⟶ Y} {P Q : T} {g : P ⟶ Q} {u : X ⟶ P} {v : Y ⟶ Q}
(w : u ≫ g = f ≫ v) : arrow.mk f ⟶ arrow.mk g :=
{ left := u,
right := v,
w' := w }
@[simp, reassoc] lemma w {f g : arrow T} (sq : f ⟶ g) : sq.left ≫ g.hom = f.hom ≫ sq.right := sq.w
-- `w_mk_left` is not needed, as it is a consequence of `w` and `mk_hom`.
@[simp, reassoc] lemma w_mk_right {f : arrow T} {X Y : T} {g : X ⟶ Y} (sq : f ⟶ mk g) :
sq.left ≫ g = f.hom ≫ sq.right :=
sq.w
instance {f g : arrow T} (ff : f ⟶ g) [is_iso ff.left] [is_iso ff.right] :
is_iso ff :=
{ out := ⟨⟨inv ff.left, inv ff.right⟩,
by { ext; dsimp; simp only [is_iso.hom_inv_id] },
by { ext; dsimp; simp only [is_iso.inv_hom_id] }⟩ }
/-- Create an isomorphism between arrows,
by providing isomorphisms between the domains and codomains,
and a proof that the square commutes. -/
@[simps] def iso_mk {f g : arrow T}
(l : f.left ≅ g.left) (r : f.right ≅ g.right) (h : l.hom ≫ g.hom = f.hom ≫ r.hom) :
f ≅ g :=
comma.iso_mk l r h
/-- Given a square from an arrow `i` to an isomorphism `p`, express the source part of `sq`
in terms of the inverse of `p`. -/
@[simp] lemma square_to_iso_invert (i : arrow T) {X Y : T} (p : X ≅ Y) (sq : i ⟶ arrow.mk p.hom) :
i.hom ≫ sq.right ≫ p.inv = sq.left :=
by simpa only [category.assoc] using (iso.comp_inv_eq p).mpr ((arrow.w_mk_right sq).symm)
/-- Given a square from an isomorphism `i` to an arrow `p`, express the target part of `sq`
in terms of the inverse of `i`. -/
lemma square_from_iso_invert {X Y : T} (i : X ≅ Y) (p : arrow T) (sq : arrow.mk i.hom ⟶ p) :
i.inv ≫ sq.left ≫ p.hom = sq.right :=
by simp only [iso.inv_hom_id_assoc, arrow.w, arrow.mk_hom]
/-- A lift of a commutative square is a diagonal morphism making the two triangles commute. -/
@[ext] structure lift_struct {f g : arrow T} (sq : f ⟶ g) :=
(lift : f.right ⟶ g.left)
(fac_left' : f.hom ≫ lift = sq.left . obviously)
(fac_right' : lift ≫ g.hom = sq.right . obviously)
restate_axiom lift_struct.fac_left'
restate_axiom lift_struct.fac_right'
instance lift_struct_inhabited {X : T} : inhabited (lift_struct (𝟙 (arrow.mk (𝟙 X)))) :=
⟨⟨𝟙 _, category.id_comp _, category.comp_id _⟩⟩
/-- `has_lift sq` says that there is some `lift_struct sq`, i.e., that it is possible to find a
diagonal morphism making the two triangles commute. -/
class has_lift {f g : arrow T} (sq : f ⟶ g) : Prop :=
mk' :: (exists_lift : nonempty (lift_struct sq))
lemma has_lift.mk {f g : arrow T} {sq : f ⟶ g} (s : lift_struct sq) : has_lift sq :=
⟨nonempty.intro s⟩
attribute [simp, reassoc] lift_struct.fac_left lift_struct.fac_right
/-- Given `has_lift sq`, obtain a lift. -/
noncomputable def has_lift.struct {f g : arrow T} (sq : f ⟶ g) [has_lift sq] : lift_struct sq :=
classical.choice has_lift.exists_lift
/-- If there is a lift of a commutative square `sq`, we can access it by saying `lift sq`. -/
noncomputable abbreviation lift {f g : arrow T} (sq : f ⟶ g) [has_lift sq] : f.right ⟶ g.left :=
(has_lift.struct sq).lift
lemma lift.fac_left {f g : arrow T} (sq : f ⟶ g) [has_lift sq] : f.hom ≫ lift sq = sq.left :=
by simp
lemma lift.fac_right {f g : arrow T} (sq : f ⟶ g) [has_lift sq] : lift sq ≫ g.hom = sq.right :=
by simp
@[simp, reassoc]
lemma lift.fac_right_of_to_mk {X Y : T} {f : arrow T} {g : X ⟶ Y} (sq : f ⟶ mk g) [has_lift sq] :
lift sq ≫ g = sq.right :=
by simp only [←mk_hom g, lift.fac_right]
@[simp, reassoc]
lemma lift.fac_left_of_from_mk {X Y : T} {f : X ⟶ Y} {g : arrow T} (sq : mk f ⟶ g) [has_lift sq] :
f ≫ lift sq = sq.left :=
by simp only [←mk_hom f, lift.fac_left]
@[simp, reassoc]
lemma lift_mk'_left {X Y P Q : T} {f : X ⟶ Y} {g : P ⟶ Q} {u : X ⟶ P} {v : Y ⟶ Q}
(h : u ≫ g = f ≫ v) [has_lift $ arrow.hom_mk' h] : f ≫ lift (arrow.hom_mk' h) = u :=
by simp only [←arrow.mk_hom f, lift.fac_left, arrow.hom_mk'_left]
@[simp, reassoc]
lemma lift_mk'_right {X Y P Q : T} {f : X ⟶ Y} {g : P ⟶ Q} {u : X ⟶ P} {v : Y ⟶ Q}
(h : u ≫ g = f ≫ v) [has_lift $ arrow.hom_mk' h] : lift (arrow.hom_mk' h) ≫ g = v :=
by simp only [←arrow.mk_hom g, lift.fac_right, arrow.hom_mk'_right]
section
instance subsingleton_lift_struct_of_epi {f g : arrow T} (sq : f ⟶ g) [epi f.hom] :
subsingleton (lift_struct sq) :=
subsingleton.intro $ λ a b, lift_struct.ext a b $ (cancel_epi f.hom).1 $ by simp
instance subsingleton_lift_struct_of_mono {f g : arrow T} (sq : f ⟶ g) [mono g.hom] :
subsingleton (lift_struct sq) :=
subsingleton.intro $ λ a b, lift_struct.ext a b $ (cancel_mono g.hom).1 $ by simp
end
variables {C : Type u} [category.{v} C]
/-- A helper construction: given a square between `i` and `f ≫ g`, produce a square between
`i` and `g`, whose top leg uses `f`:
A → X
↓f
↓i Y --> A → Y
↓g ↓i ↓g
B → Z B → Z
-/
@[simps] def square_to_snd {X Y Z: C} {i : arrow C} {f : X ⟶ Y} {g : Y ⟶ Z}
(sq : i ⟶ arrow.mk (f ≫ g)) :
i ⟶ arrow.mk g :=
{ left := sq.left ≫ f,
right := sq.right }
/-- The functor sending an arrow to its source. -/
@[simps] def left_func : arrow C ⥤ C := comma.fst _ _
/-- The functor sending an arrow to its target. -/
@[simps] def right_func : arrow C ⥤ C := comma.snd _ _
/-- The natural transformation from `left_func` to `right_func`, given by the arrow itself. -/
@[simps]
def left_to_right : (left_func : arrow C ⥤ C) ⟶ right_func :=
{ app := λ f, f.hom }
end arrow
namespace functor
universes v₁ v₂ u₁ u₂
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D]
/-- A functor `C ⥤ D` induces a functor between the corresponding arrow categories. -/
@[simps]
def map_arrow (F : C ⥤ D) : arrow C ⥤ arrow D :=
{ obj := λ a,
{ left := F.obj a.left,
right := F.obj a.right,
hom := F.map a.hom, },
map := λ a b f,
{ left := F.map f.left,
right := F.map f.right,
w' := by { have w := f.w, simp only [id_map] at w, dsimp, simp only [←F.map_comp, w], } } }
end functor
end category_theory
|
7790e27fb5d05d36b30476d4e8c70fe54895267f | 88fb7558b0636ec6b181f2a548ac11ad3919f8a5 | /tests/lean/run/io_process_echo.lean | 18efafe8c4d662785fd6a3c9c0fbaf39c2ae485b | [
"Apache-2.0"
] | permissive | moritayasuaki/lean | 9f666c323cb6fa1f31ac597d777914aed41e3b7a | ae96ebf6ee953088c235ff7ae0e8c95066ba8001 | refs/heads/master | 1,611,135,440,814 | 1,493,852,869,000 | 1,493,852,869,000 | 90,269,903 | 0 | 0 | null | 1,493,906,291,000 | 1,493,906,291,000 | null | UTF-8 | Lean | false | false | 152 | lean | import system.io
variable [io.interface]
def main : io unit := do
out ← io.cmd "echo" ["Hello World!"],
io.put_str out,
return ()
#eval main
|
cf31d346ac60a55080724ca09becd3da1f5bb5d0 | b32d3853770e6eaf06817a1b8c52064baaed0ef1 | /src/super/clause.lean | 9453366a31ba69cba4b9b4af13946d4a39e7b5d2 | [] | no_license | gebner/super2 | 4d58b7477b6f7d945d5d866502982466db33ab0b | 9bc5256c31750021ab97d6b59b7387773e54b384 | refs/heads/master | 1,635,021,682,021 | 1,634,886,326,000 | 1,634,886,326,000 | 225,600,688 | 4 | 2 | null | 1,598,209,306,000 | 1,575,371,550,000 | Lean | UTF-8 | Lean | false | false | 11,575 | lean | import super.utils super.debug
namespace super
open tactic native
@[derive decidable_eq]
meta inductive literal
| pos (a : expr)
| neg (a : expr)
namespace literal
protected meta def to_string : literal → string
| (pos a) := "pos " ++ a.to_string
| (neg a) := "neg " ++ a.to_string
meta instance : has_to_string literal := ⟨literal.to_string⟩
meta instance : has_repr literal := ⟨literal.to_string⟩
protected meta def to_tactic_fmt : literal → tactic format
| (pos a) := do a ← pp a, pure $ "pos " ++ a
| (neg a) := do a ← pp a, pure $ "neg " ++ a
meta instance : has_to_tactic_format literal := ⟨literal.to_tactic_fmt⟩
meta def is_pos : literal → bool
| (pos _) := tt
| (neg _) := ff
meta def is_neg : literal → bool
| (neg _) := tt
| (pos _) := ff
meta def formula : literal → expr
| (neg f) := f
| (pos f) := f
meta def instantiate_mvars : literal → tactic literal
| (neg f) := neg <$> tactic.instantiate_mvars f
| (pos f) := pos <$> tactic.instantiate_mvars f
end literal
@[derive decidable_eq]
meta inductive clause_type
| ff
| imp (a : expr) (b : clause_type)
| disj (is_or : bool) (a : clause_type) (b : clause_type)
| atom (a : expr)
namespace clause_type
protected meta def to_string : clause_type → string
| ff := "ff"
| (imp a b) := "(imp " ++ a.to_string ++ " " ++ b.to_string ++ ")"
| (disj io a b) := "(disj " ++ repr io ++ " " ++ a.to_string ++ " " ++ b.to_string ++ ")"
| (atom a) := "(atom " ++ a.to_string ++ ")"
meta instance : has_to_string clause_type := ⟨clause_type.to_string⟩
meta instance : has_repr clause_type := ⟨clause_type.to_string⟩
protected meta def to_tactic_fmt : clause_type → tactic format
| ff := pure "ff"
| (imp a b) := do a ← pp a, b ← b.to_tactic_fmt, pure $ format.form ["imp", a.paren, b]
| (disj io a b) := do a ← a.to_tactic_fmt, b ← b.to_tactic_fmt, pure $ format.form ["or", to_fmt io, a, b]
| (atom a) := do a ← pp a, pure $ format.form ["atom", a]
meta instance : has_to_tactic_format clause_type := ⟨clause_type.to_tactic_fmt⟩
meta def of_expr : expr → tactic clause_type
| `(%%a ∨ %%b) := disj tt <$> of_expr a <*> of_expr b
| `(false) := pure ff
| `(¬ %%a) := pure $ imp a ff
| `(psum %%a %%b) := disj bool.ff <$> of_expr a <*> of_expr b
| e@(expr.pi _ _ a b) :=
if b.has_var then
pure $ atom e
else
imp a <$> of_expr b
| e := pure $ atom e
meta def to_expr : clause_type → tactic expr
| ff := pure `(false)
| (disj tt a b) := do a ← a.to_expr, b ← b.to_expr, pure `(%%a ∨ %%b)
| (disj bool.ff a b) := do a ← a.to_expr, b ← b.to_expr, mk_mapp ``psum [a,b]
| (imp a ff) := do
ip ← is_prop a,
pure $ if ip then `(¬ %%a) else a.imp `(false)
| (imp a b) := a.imp <$> b.to_expr
| (atom a) := pure a
meta def to_expr_wrong : clause_type → expr
| ff := `(false)
| (disj tt a b) := `(%%a.to_expr_wrong ∨ %%b.to_expr_wrong)
| (disj bool.ff a b) := `(psum.{1 1} %%a.to_expr_wrong %%b.to_expr_wrong)
| (imp a ff) := `(¬ %%a)
| (imp a b) := `((%%a : Prop) → %%b.to_expr_wrong : Prop)
| (atom a) := a
meta def literals : clause_type → list literal
| ff := []
| (disj _ a b) := a.literals ++ b.literals
| (imp a b) := literal.neg a :: b.literals
| (atom a) := [literal.pos a]
meta def num_literals (ty : clause_type) : ℕ :=
ty.literals.length
meta def num_pos_literals : clause_type → ℕ
| ff := 0
| (disj _ a b) := a.num_pos_literals + b.num_pos_literals
| (imp a b) := b.num_pos_literals
| (atom a) := 1
meta def num_neg_literals : clause_type → ℕ
| ff := 0
| (disj _ a b) := a.num_neg_literals + b.num_neg_literals
| (imp a b) := b.num_neg_literals + 1
| (atom a) := 0
meta def instantiate_mvars : clause_type → tactic clause_type
| ff := pure ff
| (disj io a b) := disj io <$> instantiate_mvars a <*> instantiate_mvars b
| (imp a b) := imp <$> tactic.instantiate_mvars a <*> instantiate_mvars b
| (atom a) := atom <$> tactic.instantiate_mvars a
meta def abstract_mvars (mvars : list name) : clause_type → clause_type
| ff := ff
| (disj io a b) := disj io a.abstract_mvars b.abstract_mvars
| (imp a b) := imp (a.abstract_mvars mvars) b.abstract_mvars
| (atom a) := atom (a.abstract_mvars mvars)
meta def abstract_locals (locals : list name) : clause_type → clause_type
| ff := ff
| (disj io a b) := disj io a.abstract_locals b.abstract_locals
| (imp a b) := imp (a.abstract_locals locals) b.abstract_locals
| (atom a) := atom (a.abstract_locals locals)
meta def instantiate_vars (es : list expr) : clause_type → clause_type
| ff := ff
| (disj io a b) := disj io a.instantiate_vars b.instantiate_vars
| (imp a b) := imp (a.instantiate_vars es) b.instantiate_vars
| (atom a) := atom (a.instantiate_vars es)
meta def instantiate_univ_mvars (subst : rb_map name level) : clause_type → clause_type
| ff := ff
| (disj io a b) := disj io a.instantiate_univ_mvars b.instantiate_univ_mvars
| (imp a b) := imp (a.instantiate_univ_mvars subst) b.instantiate_univ_mvars
| (atom a) := atom (a.instantiate_univ_mvars subst)
meta def instantiate_univ_params (subst : list (name × level)) : clause_type → clause_type
| ff := ff
| (disj io a b) := disj io a.instantiate_univ_params b.instantiate_univ_params
| (imp a b) := imp (a.instantiate_univ_params subst) b.instantiate_univ_params
| (atom a) := atom (a.instantiate_univ_params subst)
meta def pos_lits (ty : clause_type) : list expr :=
do literal.pos l ← ty.literals | [], [l]
meta def neg_lits (ty : clause_type) : list expr :=
do literal.neg l ← ty.literals | [], [l]
meta def is_taut (ty : clause_type) : bool :=
ty.pos_lits ∩ ty.neg_lits ≠ []
end clause_type
@[derive decidable_eq]
meta structure clause :=
(ty : clause_type)
(prf : expr)
namespace clause
meta instance : has_to_string clause := ⟨λ c, c.ty.to_string⟩
meta instance : has_repr clause := ⟨to_string⟩
meta instance : has_to_tactic_format clause := ⟨λ c, pp c.ty.to_expr_wrong⟩
meta def of_type_and_proof : expr → expr → tactic clause
| ty@(expr.pi _ _ a b) prf :=
if b.has_var then do
m ← mk_meta_var a,
of_type_and_proof (b.instantiate_var m) (prf.app' m)
else do
ty ← clause_type.of_expr ty,
pure ⟨ty, prf⟩
| ty prf := do
ty ← clause_type.of_expr ty,
pure ⟨ty, prf⟩
meta def of_proof (prf : expr) : tactic clause := do
ty ← infer_type prf,
ty ← instantiate_mvars ty,
ty ← head_beta ty,
of_type_and_proof ty prf
meta def of_const (n : name) : tactic clause :=
mk_const n >>= of_proof
meta def literals (c : clause) : list literal :=
c.ty.literals
meta def num_literals (c : clause) : ℕ :=
c.ty.num_literals
meta def instantiate_mvars (cls : clause) : tactic clause :=
clause.mk <$> cls.ty.instantiate_mvars <*> tactic.instantiate_mvars cls.prf
meta def instantiate_univ_mvars (cls : clause) (subst : rb_map name level) : clause :=
⟨cls.ty.instantiate_univ_mvars subst, cls.prf.instantiate_univ_mvars subst⟩
meta def instantiate_univ_params (cls : clause) (subst : list (name × level)) : clause :=
⟨cls.ty.instantiate_univ_params subst, cls.prf.instantiate_univ_params subst⟩
meta def abstract_mvars (cls : clause) (mvars : list name) : clause :=
{ ty := cls.ty.abstract_mvars mvars, prf := cls.prf.abstract_mvars mvars }
meta def abstract_mvars' (cls : clause) (mvars : list expr) : clause :=
cls.abstract_mvars (mvars.map expr.meta_uniq_name)
meta def abstract_locals (cls : clause) (locals : list name) : clause :=
⟨cls.ty.abstract_locals locals, cls.prf.abstract_locals locals⟩
meta def instantiate_vars (cls : clause) (es : list expr) : clause :=
⟨cls.ty.instantiate_vars es, cls.prf.instantiate_vars es⟩
meta def is_taut (cls : clause) : bool :=
cls.ty.is_taut
meta def check (cls : clause) : tactic unit :=
on_exception (do trace "\n", trace cls.prf, trace cls.ty, trace_call_stack) $ do
when cls.prf.has_var (fail $ to_fmt "proof has de Bruijn variables"),
ty ← cls.ty.to_expr,
when ty.has_var (fail $ to_fmt "type has de Bruijn variables"),
type_check ty,
type_check cls.prf,
infer_type cls.prf >>= is_def_eq ty
@[inline]
meta def check_if_debug (cls : clause) : tactic unit :=
when_debug cls.check
@[inline]
meta def check_result_if_debug {m} [monad m] [has_monad_lift tactic m] : m clause → m clause :=
check_result_when_debug (monad_lift ∘ check)
@[inline]
meta def check_results_if_debug {m} [monad m] [has_monad_lift tactic m] :
m (list clause) → m (list clause) :=
check_result_when_debug (λ cs, monad_lift $ cs.mmap' check)
end clause
@[derive decidable_eq]
meta structure packed_clause :=
(univ_mvars : list name)
(free_var_tys : list expr)
(cls : clause)
namespace packed_clause
open native
meta instance : has_to_string packed_clause := ⟨to_string ∘ packed_clause.cls⟩
meta instance : has_repr packed_clause := ⟨to_string⟩
meta instance : has_to_tactic_format packed_clause := ⟨tactic.pp ∘ packed_clause.cls⟩
meta def mk_metas (c : packed_clause) : tactic (list expr) :=
mk_metas_core c.free_var_tys
meta def mk_locals (c : packed_clause) : tactic (list expr) :=
mk_locals_core c.free_var_tys
meta def refresh_univ_mvars (c : packed_clause) : tactic packed_clause :=
if c.univ_mvars = [] then pure c else do
ls ← mk_num_meta_univs c.univ_mvars.length,
let ls_subst := rb_map.of_list (c.univ_mvars.zip ls),
pure {
univ_mvars := ls.map level.mvar_name,
free_var_tys := c.free_var_tys.map (λ t, t.instantiate_univ_mvars ls_subst),
cls := c.cls.instantiate_univ_mvars ls_subst
}
meta def unpack (c : packed_clause) : tactic clause := do
c ← c.refresh_univ_mvars,
mvars ← c.mk_metas,
pure $ c.cls.instantiate_vars mvars
meta def unpack_quantified (c : packed_clause) : tactic clause := do
c ← c.refresh_univ_mvars,
ty ← c.cls.ty.to_expr,
let ty' := c.free_var_tys.foldl (λ e x, expr.pi x.hyp_name_hint binder_info.default x e) ty,
let prf' := c.free_var_tys.foldl (λ e x, expr.lam x.hyp_name_hint binder_info.default x e) c.cls.prf,
pure ⟨clause_type.atom ty', prf'⟩
meta def unpack_with_locals (c : packed_clause) : tactic clause := do
c ← c.refresh_univ_mvars,
lcs ← c.mk_locals,
pure $ c.cls.instantiate_vars lcs
end packed_clause
meta def clause.pack (c : clause) : tactic packed_clause := do
c ← c.instantiate_mvars,
mvars ← c.prf.sorted_mvars,
free_var_tys ← abstract_mvar_telescope mvars,
pure {
univ_mvars := c.prf.univ_meta_vars.to_list,
free_var_tys := free_var_tys,
cls := c.abstract_mvars' mvars,
}
meta def clause.clone (c : clause) : tactic clause :=
pure c >>= clause.pack >>= packed_clause.unpack
meta def clause.with_locals (c : clause) : tactic clause :=
pure c >>= clause.pack >>= packed_clause.unpack_with_locals
meta def clause.with_locals_unsafe (c : clause) : tactic clause := do
mvars ← c.prf.sorted_mvars,
lcs ← abstract_mvar_telescope mvars >>= mk_locals_core,
(mvars.zip lcs).mmap' (λ x, unify x.1 x.2),
c.instantiate_mvars
namespace clause
meta def mk_decl (c : clause) (i : ℕ) : tactic expr := do
c ← c.instantiate_mvars,
mvars ← c.prf.sorted_mvars,
free_var_tys ← abstract_mvar_telescope mvars,
c_ty ← c.ty.to_expr,
let ty := free_var_tys.foldl (λ e x, expr.pi `x binder_info.default x e)
(c_ty.abstract_mvars' mvars),
let prf := free_var_tys.foldl (λ e x, expr.lam `x binder_info.default x e)
(c.prf.abstract_mvars' mvars),
ty_univ ← infer_univ ty,
c_ty_univ ← infer_univ c_ty,
new_prf ← add_aux_decl ((`_super.step).mk_numeral (unsigned.of_nat i))
ty prf (ty_univ = level.zero),
let new_prf := new_prf.mk_app mvars.reverse,
clause.check_if_debug ⟨c.ty, new_prf⟩,
pure new_prf
end clause
end super
|
6113497d3bac6ea319c907e4f1c1ad57b61e5e7e | 4727251e0cd73359b15b664c3170e5d754078599 | /src/order/circular.lean | 5255e3f546214e25d1e22c7eacb57126bc0e01c8 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 15,283 | lean | /-
Copyright (c) 2021 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import data.set.basic
/-!
# Circular order hierarchy
This file defines circular preorders, circular partial orders and circular orders.
## Hierarchy
* A ternary "betweenness" relation `btw : α → α → α → Prop` forms a `circular_order` if it is
- reflexive: `btw a a a`
- cyclic: `btw a b c → btw b c a`
- antisymmetric: `btw a b c → btw c b a → a = b ∨ b = c ∨ c = a`
- total: `btw a b c ∨ btw c b a`
along with a strict betweenness relation `sbtw : α → α → α → Prop` which respects
`sbtw a b c ↔ btw a b c ∧ ¬ btw c b a`, analogously to how `<` and `≤` are related, and is
- transitive: `sbtw a b c → sbtw b d c → sbtw a d c`.
* A `circular_partial_order` drops totality.
* A `circular_preorder` further drops antisymmetry.
The intuition is that a circular order is a circle and `btw a b c` means that going around
clockwise from `a` you reach `b` before `c` (`b` is between `a` and `c` is meaningless on an
unoriented circle). A circular partial order is several, potentially intersecting, circles. A
circular preorder is like a circular partial order, but several points can coexist.
Note that the relations between `circular_preorder`, `circular_partial_order` and `circular_order`
are subtler than between `preorder`, `partial_order`, `linear_order`. In particular, one cannot
simply extend the `btw` of a `circular_partial_order` to make it a `circular_order`.
One can translate from usual orders to circular ones by "closing the necklace at infinity". See
`has_le.to_has_btw` and `has_lt.to_has_sbtw`. Going the other way involves "cutting the necklace" or
"rolling the necklace open".
## Examples
Some concrete circular orders one encounters in the wild are `zmod n` for `0 < n`, `circle`,
`real.angle`...
## Main definitions
* `set.cIcc`: Closed-closed circular interval.
* `set.cIoo`: Open-open circular interval.
## Notes
There's an unsolved diamond on `order_dual α` here. The instances `has_le α → has_btw αᵒᵈ` and
`has_lt α → has_sbtw αᵒᵈ` can each be inferred in two ways:
* `has_le α` → `has_btw α` → `has_btw αᵒᵈ` vs
`has_le α` → `has_le αᵒᵈ` → `has_btw αᵒᵈ`
* `has_lt α` → `has_sbtw α` → `has_sbtw αᵒᵈ` vs
`has_lt α` → `has_lt αᵒᵈ` → `has_sbtw αᵒᵈ`
The fields are propeq, but not defeq. It is temporarily fixed by turning the circularizing instances
into definitions.
## TODO
Antisymmetry is quite weak in the sense that there's no way to discriminate which two points are
equal. This prevents defining closed-open intervals `cIco` and `cIoc` in the neat `=`-less way. We
currently haven't defined them at all.
What is the correct generality of "rolling the necklace" open? At least, this works for `α × β` and
`β × α` where `α` is a circular order and `β` is a linear order.
What's next is to define circular groups and provide instances for `zmod n`, the usual circle group
`circle`, `real.angle`, and `roots_of_unity M`. What conditions do we need on `M` for this last one
to work?
We should have circular order homomorphisms. The typical example is
`days_to_month : days_of_the_year →c months_of_the_year` which relates the circular order of days
and the circular order of months. Is `α →c β` a good notation?
## References
* https://en.wikipedia.org/wiki/Cyclic_order
* https://en.wikipedia.org/wiki/Partial_cyclic_order
## Tags
circular order, cyclic order, circularly ordered set, cyclically ordered set
-/
/-- Syntax typeclass for a betweenness relation. -/
class has_btw (α : Type*) :=
(btw : α → α → α → Prop)
export has_btw (btw)
/-- Syntax typeclass for a strict betweenness relation. -/
class has_sbtw (α : Type*) :=
(sbtw : α → α → α → Prop)
export has_sbtw (sbtw)
/-- A circular preorder is the analogue of a preorder where you can loop around. `≤` and `<` are
replaced by ternary relations `btw` and `sbtw`. `btw` is reflexive and cyclic. `sbtw` is transitive.
-/
class circular_preorder (α : Type*) extends has_btw α, has_sbtw α :=
(btw_refl (a : α) : btw a a a)
(btw_cyclic_left {a b c : α} : btw a b c → btw b c a)
(sbtw := λ a b c, btw a b c ∧ ¬btw c b a)
(sbtw_iff_btw_not_btw {a b c : α} : sbtw a b c ↔ (btw a b c ∧ ¬btw c b a) . order_laws_tac)
(sbtw_trans_left {a b c d : α} : sbtw a b c → sbtw b d c → sbtw a d c)
export circular_preorder (btw_refl) (btw_cyclic_left) (sbtw_trans_left)
/-- A circular partial order is the analogue of a partial order where you can loop around. `≤` and
`<` are replaced by ternary relations `btw` and `sbtw`. `btw` is reflexive, cyclic and
antisymmetric. `sbtw` is transitive. -/
class circular_partial_order (α : Type*) extends circular_preorder α :=
(btw_antisymm {a b c : α} : btw a b c → btw c b a → a = b ∨ b = c ∨ c = a)
export circular_partial_order (btw_antisymm)
/-- A circular order is the analogue of a linear order where you can loop around. `≤` and `<` are
replaced by ternary relations `btw` and `sbtw`. `btw` is reflexive, cyclic, antisymmetric and total.
`sbtw` is transitive. -/
class circular_order (α : Type*) extends circular_partial_order α :=
(btw_total : ∀ a b c : α, btw a b c ∨ btw c b a)
export circular_order (btw_total)
/-! ### Circular preorders -/
section circular_preorder
variables {α : Type*} [circular_preorder α]
lemma btw_rfl {a : α} : btw a a a :=
btw_refl _
-- TODO: `alias` creates a def instead of a lemma.
-- alias btw_cyclic_left ← has_btw.btw.cyclic_left
lemma has_btw.btw.cyclic_left {a b c : α} (h : btw a b c) : btw b c a :=
btw_cyclic_left h
lemma btw_cyclic_right {a b c : α} (h : btw a b c) : btw c a b :=
h.cyclic_left.cyclic_left
alias btw_cyclic_right ← has_btw.btw.cyclic_right
/-- The order of the `↔` has been chosen so that `rw btw_cyclic` cycles to the right while
`rw ←btw_cyclic` cycles to the left (thus following the prepended arrow). -/
lemma btw_cyclic {a b c : α} : btw a b c ↔ btw c a b :=
⟨btw_cyclic_right, btw_cyclic_left⟩
lemma sbtw_iff_btw_not_btw {a b c : α} : sbtw a b c ↔ btw a b c ∧ ¬btw c b a :=
circular_preorder.sbtw_iff_btw_not_btw
lemma btw_of_sbtw {a b c : α} (h : sbtw a b c) : btw a b c :=
(sbtw_iff_btw_not_btw.1 h).1
alias btw_of_sbtw ← has_sbtw.sbtw.btw
lemma not_btw_of_sbtw {a b c : α} (h : sbtw a b c) : ¬btw c b a :=
(sbtw_iff_btw_not_btw.1 h).2
alias not_btw_of_sbtw ← has_sbtw.sbtw.not_btw
lemma not_sbtw_of_btw {a b c : α} (h : btw a b c) : ¬sbtw c b a :=
λ h', h'.not_btw h
alias not_sbtw_of_btw ← has_btw.btw.not_sbtw
lemma sbtw_of_btw_not_btw {a b c : α} (habc : btw a b c) (hcba : ¬btw c b a) : sbtw a b c :=
sbtw_iff_btw_not_btw.2 ⟨habc, hcba⟩
alias sbtw_of_btw_not_btw ← has_btw.btw.sbtw_of_not_btw
lemma sbtw_cyclic_left {a b c : α} (h : sbtw a b c) : sbtw b c a :=
h.btw.cyclic_left.sbtw_of_not_btw (λ h', h.not_btw h'.cyclic_left)
alias sbtw_cyclic_left ← has_sbtw.sbtw.cyclic_left
lemma sbtw_cyclic_right {a b c : α} (h : sbtw a b c) : sbtw c a b :=
h.cyclic_left.cyclic_left
alias sbtw_cyclic_right ← has_sbtw.sbtw.cyclic_right
/-- The order of the `↔` has been chosen so that `rw sbtw_cyclic` cycles to the right while
`rw ←sbtw_cyclic` cycles to the left (thus following the prepended arrow). -/
lemma sbtw_cyclic {a b c : α} : sbtw a b c ↔ sbtw c a b :=
⟨sbtw_cyclic_right, sbtw_cyclic_left⟩
-- TODO: `alias` creates a def instead of a lemma.
-- alias btw_trans_left ← has_btw.btw.trans_left
lemma has_sbtw.sbtw.trans_left {a b c d : α} (h : sbtw a b c) : sbtw b d c → sbtw a d c :=
sbtw_trans_left h
lemma sbtw_trans_right {a b c d : α} (hbc : sbtw a b c) (hcd : sbtw a c d) : sbtw a b d :=
(hbc.cyclic_left.trans_left hcd.cyclic_left).cyclic_right
alias sbtw_trans_right ← has_sbtw.sbtw.trans_right
lemma sbtw_asymm {a b c : α} (h : sbtw a b c) : ¬ sbtw c b a :=
h.btw.not_sbtw
alias sbtw_asymm ← has_sbtw.sbtw.not_sbtw
lemma sbtw_irrefl_left_right {a b : α} : ¬ sbtw a b a := λ h, h.not_btw h.btw
lemma sbtw_irrefl_left {a b : α} : ¬ sbtw a a b := λ h, sbtw_irrefl_left_right h.cyclic_left
lemma sbtw_irrefl_right {a b : α} : ¬ sbtw a b b := λ h, sbtw_irrefl_left_right h.cyclic_right
lemma sbtw_irrefl (a : α) : ¬ sbtw a a a := sbtw_irrefl_left_right
end circular_preorder
/-! ### Circular partial orders -/
section circular_partial_order
variables {α : Type*} [circular_partial_order α]
-- TODO: `alias` creates a def instead of a lemma.
-- alias btw_antisymm ← has_btw.btw.antisymm
lemma has_btw.btw.antisymm {a b c : α} (h : btw a b c) : btw c b a → a = b ∨ b = c ∨ c = a :=
btw_antisymm h
end circular_partial_order
/-! ### Circular orders -/
section circular_order
variables {α : Type*} [circular_order α]
lemma btw_refl_left_right (a b : α) : btw a b a :=
(or_self _).1 (btw_total a b a)
lemma btw_rfl_left_right {a b : α} : btw a b a :=
btw_refl_left_right _ _
lemma btw_refl_left (a b : α) : btw a a b :=
btw_rfl_left_right.cyclic_right
lemma btw_rfl_left {a b : α} : btw a a b :=
btw_refl_left _ _
lemma btw_refl_right (a b : α) : btw a b b :=
btw_rfl_left_right.cyclic_left
lemma btw_rfl_right {a b : α} : btw a b b :=
btw_refl_right _ _
lemma sbtw_iff_not_btw {a b c : α} : sbtw a b c ↔ ¬ btw c b a :=
begin
rw sbtw_iff_btw_not_btw,
exact and_iff_right_of_imp (btw_total _ _ _).resolve_left,
end
lemma btw_iff_not_sbtw {a b c : α} : btw a b c ↔ ¬ sbtw c b a :=
iff_not_comm.1 sbtw_iff_not_btw
end circular_order
/-! ### Circular intervals -/
namespace set
section circular_preorder
variables {α : Type*} [circular_preorder α]
/-- Closed-closed circular interval -/
def cIcc (a b : α) : set α := {x | btw a x b}
/-- Open-open circular interval -/
def cIoo (a b : α) : set α := {x | sbtw a x b}
@[simp] lemma mem_cIcc {a b x : α} : x ∈ cIcc a b ↔ btw a x b := iff.rfl
@[simp] lemma mem_cIoo {a b x : α} : x ∈ cIoo a b ↔ sbtw a x b := iff.rfl
end circular_preorder
section circular_order
variables {α : Type*} [circular_order α]
lemma left_mem_cIcc (a b : α) : a ∈ cIcc a b := btw_rfl_left
lemma right_mem_cIcc (a b : α) : b ∈ cIcc a b := btw_rfl_right
lemma compl_cIcc {a b : α} : (cIcc a b)ᶜ = cIoo b a :=
begin
ext,
rw [set.mem_cIoo, sbtw_iff_not_btw],
refl,
end
lemma compl_cIoo {a b : α} : (cIoo a b)ᶜ = cIcc b a :=
begin
ext,
rw [set.mem_cIcc, btw_iff_not_sbtw],
refl,
end
end circular_order
end set
/-! ### Circularizing instances -/
/-- The betweenness relation obtained from "looping around" `≤`.
See note [reducible non-instances]. -/
@[reducible]
def has_le.to_has_btw (α : Type*) [has_le α] : has_btw α :=
{ btw := λ a b c, (a ≤ b ∧ b ≤ c) ∨ (b ≤ c ∧ c ≤ a) ∨ (c ≤ a ∧ a ≤ b) }
/-- The strict betweenness relation obtained from "looping around" `<`.
See note [reducible non-instances]. -/
@[reducible]
def has_lt.to_has_sbtw (α : Type*) [has_lt α] : has_sbtw α :=
{ sbtw := λ a b c, (a < b ∧ b < c) ∨ (b < c ∧ c < a) ∨ (c < a ∧ a < b) }
/-- The circular preorder obtained from "looping around" a preorder.
See note [reducible non-instances]. -/
@[reducible]
def preorder.to_circular_preorder (α : Type*) [preorder α] : circular_preorder α :=
{ btw := λ a b c, (a ≤ b ∧ b ≤ c) ∨ (b ≤ c ∧ c ≤ a) ∨ (c ≤ a ∧ a ≤ b),
sbtw := λ a b c, (a < b ∧ b < c) ∨ (b < c ∧ c < a) ∨ (c < a ∧ a < b),
btw_refl := λ a, or.inl ⟨le_rfl, le_rfl⟩,
btw_cyclic_left := λ a b c h, begin
unfold btw at ⊢ h,
rwa [←or.assoc, or_comm],
end,
sbtw_trans_left := λ a b c d, begin
rintro (⟨hab, hbc⟩ | ⟨hbc, hca⟩ | ⟨hca, hab⟩) (⟨hbd, hdc⟩ | ⟨hdc, hcb⟩ | ⟨hcb, hbd⟩),
{ exact or.inl ⟨hab.trans hbd, hdc⟩ },
{ exact (hbc.not_lt hcb).elim },
{ exact (hbc.not_lt hcb).elim },
{ exact or.inr (or.inl ⟨hdc, hca⟩) },
{ exact or.inr (or.inl ⟨hdc, hca⟩) },
{ exact (hbc.not_lt hcb).elim },
{ exact or.inr (or.inl ⟨hdc, hca⟩) },
{ exact or.inr (or.inl ⟨hdc, hca⟩) },
{ exact or.inr (or.inr ⟨hca, hab.trans hbd⟩) }
end,
sbtw_iff_btw_not_btw := λ a b c, begin
simp_rw lt_iff_le_not_le,
set x₀ := a ≤ b,
set x₁ := b ≤ c,
set x₂ := c ≤ a,
have : x₀ → x₁ → a ≤ c := le_trans,
have : x₁ → x₂ → b ≤ a := le_trans,
have : x₂ → x₀ → c ≤ b := le_trans,
clear_value x₀ x₁ x₂,
tauto!,
end }
/-- The circular partial order obtained from "looping around" a partial order.
See note [reducible non-instances]. -/
@[reducible]
def partial_order.to_circular_partial_order (α : Type*) [partial_order α] :
circular_partial_order α :=
{ btw_antisymm := λ a b c, begin
rintro (⟨hab, hbc⟩ | ⟨hbc, hca⟩ | ⟨hca, hab⟩) (⟨hcb, hba⟩ | ⟨hba, hac⟩ | ⟨hac, hcb⟩),
{ exact or.inl (hab.antisymm hba) },
{ exact or.inl (hab.antisymm hba) },
{ exact or.inr (or.inl $ hbc.antisymm hcb) },
{ exact or.inr (or.inl $ hbc.antisymm hcb) },
{ exact or.inr (or.inr $ hca.antisymm hac) },
{ exact or.inr (or.inl $ hbc.antisymm hcb) },
{ exact or.inl (hab.antisymm hba) },
{ exact or.inl (hab.antisymm hba) },
{ exact or.inr (or.inr $ hca.antisymm hac) }
end,
.. preorder.to_circular_preorder α }
/-- The circular order obtained from "looping around" a linear order.
See note [reducible non-instances]. -/
@[reducible]
def linear_order.to_circular_order (α : Type*) [linear_order α] :
circular_order α :=
{ btw_total := λ a b c, begin
cases le_total a b with hab hba; cases le_total b c with hbc hcb;
cases le_total c a with hca hac,
{ exact or.inl (or.inl ⟨hab, hbc⟩) },
{ exact or.inl (or.inl ⟨hab, hbc⟩) },
{ exact or.inl (or.inr $ or.inr ⟨hca, hab⟩) },
{ exact or.inr (or.inr $ or.inr ⟨hac, hcb⟩) },
{ exact or.inl (or.inr $ or.inl ⟨hbc, hca⟩) },
{ exact or.inr (or.inr $ or.inl ⟨hba, hac⟩) },
{ exact or.inr (or.inl ⟨hcb, hba⟩) },
{ exact or.inr (or.inr $ or.inl ⟨hba, hac⟩) }
end,
.. partial_order.to_circular_partial_order α }
/-! ### Dual constructions -/
section order_dual
instance (α : Type*) [has_btw α] : has_btw αᵒᵈ := ⟨λ a b c : α, btw c b a⟩
instance (α : Type*) [has_sbtw α] : has_sbtw αᵒᵈ := ⟨λ a b c : α, sbtw c b a⟩
instance (α : Type*) [h : circular_preorder α] : circular_preorder αᵒᵈ :=
{ btw_refl := btw_refl,
btw_cyclic_left := λ a b c, btw_cyclic_right,
sbtw_trans_left := λ a b c d habc hbdc, hbdc.trans_right habc,
sbtw_iff_btw_not_btw := λ a b c, @sbtw_iff_btw_not_btw α _ c b a,
.. order_dual.has_btw α,
.. order_dual.has_sbtw α }
instance (α : Type*) [circular_partial_order α] : circular_partial_order αᵒᵈ :=
{ btw_antisymm := λ a b c habc hcba, @btw_antisymm α _ _ _ _ hcba habc,
.. order_dual.circular_preorder α }
instance (α : Type*) [circular_order α] : circular_order αᵒᵈ :=
{ btw_total := λ a b c, btw_total c b a, .. order_dual.circular_partial_order α }
end order_dual
|
a751f3adf5b872b67ca566ef6ab4de539ddcb18a | b73bd2854495d87ad5ce4f247cfcd6faa7e71c7e | /src/game/world3/level6.lean | c927ee266f2843eb3a55d592dd8b0209c99f870f | [] | no_license | agusakov/category-theory-game | 20db0b26270e0c95a3d5605498570273d72f731d | 652dd7e90ae706643b2a597e2c938403653e167d | refs/heads/master | 1,669,201,216,310 | 1,595,740,057,000 | 1,595,740,057,000 | 280,895,295 | 12 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 793 | lean | import category_theory.category.default
universes v u -- The order in this declaration matters: v often needs to be explicitly specified while u often can be omitted
namespace category_theory
variables (C : Type u) [category.{v} C]
--rewrite this
/-
# Category world
## Level 7: Composition of monomorphisms
Now we show that the composition of two monomorphisms produces another monomorphism.-/
/- Lemma
If $$f : X ⟶ Y$$ and $$g : X ⟶ Y$$ are monomorphisms, then $$f ≫ g : X ⟶ Z$$ is a monomorphism.
-/
lemma epi_comp' {X Y Z : C} (f : X ⟶ Y) [epi f] (g : Y ⟶ Z) [epi g] : epi (f ≫ g) :=
begin
split,
intros Z h l hyp,
rw ← cancel_epi g,
rw ← cancel_epi f,
rw ← category.assoc,
rw ← category.assoc,
exact hyp,
end
end category_theory |
3e02bc1d0aebdbed7118a58620278d9d45cc75f7 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/user_attribute.lean | 01ae0ccab6eceed48a8cf0543bfc00c60e05f891 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 1,017 | lean | @[user_attribute]
meta def foo_attr : user_attribute := { name := `foo, descr := "bar" }
attribute [foo] eq.refl
#print [foo]
#print eq.refl
run_cmd attribute.get_instances `foo >>= tactic.pp >>= tactic.trace
#print "---"
-- compound names
@[user_attribute]
meta def foo_baz_attr : user_attribute := { name := `foo.baz, descr := "bar" }
attribute [foo.baz] eq.refl
#print [foo.baz]
#print eq.refl
run_cmd attribute.get_instances `foo.baz >>= tactic.pp >>= tactic.trace
-- can't redeclare attributes
@[user_attribute]
meta def duplicate : user_attribute := { name := `reducible, descr := "bar" }
-- wrong type
@[user_attribute]
meta def bar := "bar"
section
variable x : string
@[user_attribute]
meta def baz_attr : user_attribute := { name := `baz, descr := x }
end
-- parameterized attributes
@[user_attribute] meta def pattr : user_attribute unit name :=
{ name := `pattr, descr := "pattr", parser := lean.parser.ident }
@[pattr poing]
def foo := 1
run_cmd pattr.get_param `foo >>= tactic.trace
|
2a593302116cc4731587e33627744c023c47b920 | 9b9a16fa2cb737daee6b2785474678b6fa91d6d4 | /src/data/hash_map.lean | afee40b9ef4b40c18ecf2f300ca9ce54c57eda56 | [
"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 | 27,757 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Mario Carneiro
-/
import data.list.basic data.pnat data.array.lemmas
universes u v w
/-- `bucket_array α β` is the underlying data type for `hash_map α β`,
an array of linked lists of key-value pairs. -/
def bucket_array (α : Type u) (β : α → Type v) (n : ℕ+) :=
array n.1 (list Σ a, β a)
/-- Make a hash_map index from a `nat` hash value and a (positive) buffer size -/
def hash_map.mk_idx (n : ℕ+) (i : nat) : fin n.1 :=
⟨i % n.1, nat.mod_lt _ n.2⟩
namespace bucket_array
section
parameters {α : Type u} {β : α → Type v} (hash_fn : α → nat)
variables {n : ℕ+} (data : bucket_array α β n)
/-- Read the bucket corresponding to an element -/
def read (a : α) : list Σ a, β a :=
let bidx := hash_map.mk_idx n (hash_fn a) in
data.read bidx
/-- Write the bucket corresponding to an element -/
def write (a : α) (l : list Σ a, β a) : bucket_array α β n :=
let bidx := hash_map.mk_idx n (hash_fn a) in
data.write bidx l
/-- Modify (read, apply `f`, and write) the bucket corresponding to an element -/
def modify (a : α) (f : list (Σ a, β a) → list (Σ a, β a)) : bucket_array α β n :=
let bidx := hash_map.mk_idx n (hash_fn a) in
array.write data bidx (f (array.read data bidx))
/-- The list of all key-value pairs in the bucket list -/
def as_list : list Σ a, β a := data.to_list.join
theorem mem_as_list {a : Σ a, β a} : a ∈ data.as_list ↔ ∃i, a ∈ array.read data i :=
have (∃ (l : list (Σ (a : α), β a)) (i : fin (n.val)), a ∈ l ∧ array.read data i = l) ↔
∃ (i : fin (n.val)), a ∈ array.read data i,
by rw exists_swap; exact exists_congr (λ i, by simp),
by simp [as_list]; simpa [array.mem.def, and_comm]
/-- Fold a function `f` over the key-value pairs in the bucket list -/
def foldl {δ : Type w} (d : δ) (f : δ → Π a, β a → δ) : δ :=
data.foldl d (λ b d, b.foldl (λ r a, f r a.1 a.2) d)
theorem foldl_eq {δ : Type w} (d : δ) (f : δ → Π a, β a → δ) :
data.foldl d f = data.as_list.foldl (λ r a, f r a.1 a.2) d :=
by rw [foldl, as_list, list.foldl_join, ← array.to_list_foldl]
end
end bucket_array
namespace hash_map
section
parameters {α : Type u} {β : α → Type v} (hash_fn : α → nat)
/-- Insert the pair `⟨a, b⟩` into the correct location in the bucket array
(without checking for duplication) -/
def reinsert_aux {n} (data : bucket_array α β n) (a : α) (b : β a) : bucket_array α β n :=
data.modify hash_fn a (λl, ⟨a, b⟩ :: l)
parameter [decidable_eq α]
/-- Search a bucket for a key `a` and return the value -/
def find_aux (a : α) : list (Σ a, β a) → option (β a)
| [] := none
| (⟨a',b⟩::t) := if h : a' = a then some (eq.rec_on h b) else find_aux t
theorem find_aux_iff {a : α} {b : β a} : Π {l : list Σ a, β a}, (l.map sigma.fst).nodup → (find_aux a l = some b ↔ sigma.mk a b ∈ l)
| [] nd := ⟨λn, by injection n, false.elim⟩
| (⟨a',b'⟩::t) nd := begin
by_cases a' = a,
{ clear find_aux_iff, subst h,
suffices : b' = b ↔ b' = b ∨ sigma.mk a' b ∈ t, {simpa [find_aux, eq_comm]},
refine (or_iff_left_of_imp (λ m, _)).symm,
have : a' ∉ t.map sigma.fst, from list.not_mem_of_nodup_cons nd,
exact this.elim (list.mem_map_of_mem sigma.fst m) },
{ have : sigma.mk a b ≠ ⟨a', b'⟩,
{ intro e, injection e with e, exact h e.symm },
simp at nd, simp [find_aux, h, ne.symm h, find_aux_iff, nd] }
end
/-- Returns `tt` if the bucket `l` contains the key `a` -/
def contains_aux (a : α) (l : list Σ a, β a) : bool :=
(find_aux a l).is_some
theorem contains_aux_iff {a : α} {l : list Σ a, β a} (nd : (l.map sigma.fst).nodup) : contains_aux a l ↔ a ∈ l.map sigma.fst :=
begin
unfold contains_aux,
cases h : find_aux a l with b; simp,
{ assume (b : β a) (m : sigma.mk a b ∈ l),
rw (find_aux_iff nd).2 m at h,
contradiction },
{ show ∃ (b : β a), sigma.mk a b ∈ l,
exact ⟨_, (find_aux_iff nd).1 h⟩ },
end
/-- Modify a bucket to replace a value in the list. Leaves the list
unchanged if the key is not found. -/
def replace_aux (a : α) (b : β a) : list (Σ a, β a) → list (Σ a, β a)
| [] := []
| (⟨a', b'⟩::t) := if a' = a then ⟨a, b⟩::t else ⟨a', b'⟩ :: replace_aux t
/-- Modify a bucket to remove a key, if it exists. -/
def erase_aux (a : α) : list (Σ a, β a) → list (Σ a, β a)
| [] := []
| (⟨a', b'⟩::t) := if a' = a then t else ⟨a', b'⟩ :: erase_aux t
/-- The predicate `valid bkts sz` means that `bkts` satisfies the `hash_map`
invariants: There are exactly `sz` elements in it, every pair is in the
bucket determined by its key and the hash function, and no key appears
multiple times in the list. -/
structure valid {n} (bkts : bucket_array α β n) (sz : nat) : Prop :=
(len : bkts.as_list.length = sz)
(idx : ∀ {i} {a : Σ a, β a}, a ∈ array.read bkts i →
mk_idx n (hash_fn a.1) = i)
(nodup : ∀i, ((array.read bkts i).map sigma.fst).nodup)
theorem valid.idx_enum {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz)
{i l} (he : (i, l) ∈ bkts.to_list.enum) {a} {b : β a} (hl : sigma.mk a b ∈ l) :
∃ h, mk_idx n (hash_fn a) = ⟨i, h⟩ :=
(array.mem_to_list_enum.mp he).imp (λ h e, by subst e; exact v.idx hl)
theorem valid.idx_enum_1 {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz)
{i l} (he : (i, l) ∈ bkts.to_list.enum) {a} {b : β a} (hl : sigma.mk a b ∈ l) :
(mk_idx n (hash_fn a)).1 = i :=
let ⟨h, e⟩ := v.idx_enum _ he hl in by rw e; refl
theorem valid.as_list_nodup {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) : (bkts.as_list.map sigma.fst).nodup :=
begin
suffices : (bkts.to_list.map (list.map sigma.fst)).pairwise list.disjoint,
{ simp [bucket_array.as_list, list.nodup_join, this],
change ∀ l s, array.mem s bkts → list.map sigma.fst s = l → l.nodup,
introv m e, subst e, cases m with i e, subst e,
apply v.nodup },
rw [← list.enum_map_snd bkts.to_list, list.pairwise_map, list.pairwise_map],
have : (bkts.to_list.enum.map prod.fst).nodup := by simp [list.nodup_range],
refine list.pairwise.imp_of_mem _ ((list.pairwise_map _).1 this),
rw prod.forall, intros i l₁,
rw prod.forall, intros j l₂ me₁ me₂ ij,
simp [list.disjoint], intros a b ml₁ b' ml₂,
apply ij, rwa [← v.idx_enum_1 _ me₁ ml₁, ← v.idx_enum_1 _ me₂ ml₂]
end
theorem mk_as_list (n : ℕ+) : bucket_array.as_list (mk_array n.1 [] : bucket_array α β n) = [] :=
list.eq_nil_of_forall_not_mem $ λ x m,
let ⟨i, h⟩ := (bucket_array.mem_as_list _).1 m in h
theorem mk_valid (n : ℕ+) : @valid n (mk_array n.1 []) 0 :=
⟨by simp [mk_as_list], λ i a h, by cases h, λ i, list.nodup_nil⟩
theorem valid.find_aux_iff {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {a : α} {b : β a} :
find_aux a (bkts.read hash_fn a) = some b ↔ sigma.mk a b ∈ bkts.as_list :=
(find_aux_iff (v.nodup _)).trans $
by rw bkts.mem_as_list; exact ⟨λ h, ⟨_, h⟩, λ ⟨i, h⟩, (v.idx h).symm ▸ h⟩
theorem valid.contains_aux_iff {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) (a : α) :
contains_aux a (bkts.read hash_fn a) ↔ a ∈ bkts.as_list.map sigma.fst :=
by simp [contains_aux, option.is_some_iff_exists, v.find_aux_iff hash_fn]
section
parameters {n : ℕ+} {bkts : bucket_array α β n}
{bidx : fin n.1} {f : list (Σ a, β a) → list (Σ a, β a)}
(u v1 v2 w : list Σ a, β a)
local notation `L` := array.read bkts bidx
private def bkts' : bucket_array α β n := array.write bkts bidx (f L)
variables (hl : L = u ++ v1 ++ w)
(hfl : f L = u ++ v2 ++ w)
include hl hfl
theorem append_of_modify : ∃ u' w', bkts.as_list = u' ++ v1 ++ w' ∧ bkts'.as_list = u' ++ v2 ++ w' :=
begin
unfold bucket_array.as_list,
have h : bidx.1 < bkts.to_list.length, {simp [bidx.2]},
refine ⟨(bkts.to_list.take bidx.1).join ++ u, w ++ (bkts.to_list.drop (bidx.1+1)).join, _, _⟩,
{ conv { to_lhs,
rw [← list.take_append_drop bidx.1 bkts.to_list, list.drop_eq_nth_le_cons h],
simp [hl] }, simp },
{ conv { to_lhs,
rw [bkts', array.write_to_list, list.update_nth_eq_take_cons_drop _ h],
simp [hfl] }, simp }
end
variables (hvnd : (v2.map sigma.fst).nodup)
(hal : ∀ (a : Σ a, β a), a ∈ v2 → mk_idx n (hash_fn a.1) = bidx)
(djuv : (u.map sigma.fst).disjoint (v2.map sigma.fst))
(djwv : (w.map sigma.fst).disjoint (v2.map sigma.fst))
include hvnd hal djuv djwv
theorem valid.modify {sz : ℕ} (v : valid bkts sz) : sz + v2.length ≥ v1.length ∧ valid bkts' (sz + v2.length - v1.length) :=
begin
rcases append_of_modify u v1 v2 w hl hfl with ⟨u', w', e₁, e₂⟩,
rw [← v.len, e₁],
suffices : valid bkts' (u' ++ v2 ++ w').length,
{ simpa [ge, nat.le_add_right, nat.add_sub_cancel_left] },
refine ⟨congr_arg _ e₂, λ i a, _, λ i, _⟩,
{ by_cases bidx = i,
{ subst i, rw [bkts', array.read_write, hfl],
have := @valid.idx _ _ _ v bidx a,
simp only [hl, list.mem_append, or_imp_distrib, forall_and_distrib] at this ⊢,
exact ⟨⟨this.1.1, hal _⟩, this.2⟩ },
{ rw [bkts', array.read_write_of_ne _ _ h], apply v.idx } },
{ by_cases bidx = i,
{ subst i, rw [bkts', array.read_write, hfl],
have := @valid.nodup _ _ _ v bidx,
simp [hl, list.nodup_append] at this,
simp [list.nodup_append, this, hvnd, djuv, djwv.symm] },
{ rw [bkts', array.read_write_of_ne _ _ h], apply v.nodup } }
end
end
theorem valid.replace_aux (a : α) (b : β a) : Π (l : list (Σ a, β a)), a ∈ l.map sigma.fst →
∃ (u w : list Σ a, β a) b', l = u ++ [⟨a, b'⟩] ++ w ∧ replace_aux a b l = u ++ [⟨a, b⟩] ++ w
| [] := false.elim
| (⟨a', b'⟩::t) := begin
by_cases e : a' = a,
{ subst a',
suffices : ∃ u w (b'' : β a),
sigma.mk a b' :: t = u ++ ⟨a, b''⟩ :: w ∧
replace_aux a b (⟨a, b'⟩ :: t) = u ++ ⟨a, b⟩ :: w, {simpa},
refine ⟨[], t, b', _⟩, simp [replace_aux] },
{ suffices : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (b'' : β a),
sigma.mk a' b' :: t = u ++ ⟨a, b''⟩ :: w ∧
sigma.mk a' b' :: replace_aux a b t = u ++ ⟨a, b⟩ :: w,
{ simpa [replace_aux, ne.symm e, e] },
intros x m,
have IH : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (b'' : β a),
t = u ++ ⟨a, b''⟩ :: w ∧ replace_aux a b t = u ++ ⟨a, b⟩ :: w,
{ simpa using valid.replace_aux t },
rcases IH x m with ⟨u, w, b'', hl, hfl⟩,
exact ⟨⟨a', b'⟩ :: u, w, b'', by simp [hl, hfl.symm, ne.symm e]⟩ }
end
theorem valid.replace {n : ℕ+}
{bkts : bucket_array α β n} {sz : ℕ} (a : α) (b : β a)
(Hc : contains_aux a (bkts.read hash_fn a))
(v : valid bkts sz) : valid (bkts.modify hash_fn a (replace_aux a b)) sz :=
begin
have nd := v.nodup (mk_idx n (hash_fn a)),
rcases hash_map.valid.replace_aux a b (array.read bkts (mk_idx n (hash_fn a)))
((contains_aux_iff nd).1 Hc) with ⟨u, w, b', hl, hfl⟩,
simp [hl, list.nodup_append] at nd,
refine (v.modify hash_fn
u [⟨a, b'⟩] [⟨a, b⟩] w hl hfl (list.nodup_singleton _)
(λa' e, by simp at e; rw e)
(λa' e1 e2, _)
(λa' e1 e2, _)).2;
{ revert e1, simp [-sigma.exists] at e2, subst a', simp [nd] }
end
theorem valid.insert {n : ℕ+}
{bkts : bucket_array α β n} {sz : ℕ} (a : α) (b : β a)
(Hnc : ¬ contains_aux a (bkts.read hash_fn a))
(v : valid bkts sz) : valid (reinsert_aux bkts a b) (sz+1) :=
begin
have nd := v.nodup (mk_idx n (hash_fn a)),
refine (v.modify hash_fn
[] [] [⟨a, b⟩] (bkts.read hash_fn a) rfl rfl (list.nodup_singleton _)
(λa' e, by simp at e; rw e)
(λa', false.elim)
(λa' e1 e2, _)).2,
simp [-sigma.exists] at e2, subst a',
exact Hnc ((contains_aux_iff nd).2 e1)
end
theorem valid.erase_aux (a : α) : Π (l : list (Σ a, β a)), a ∈ l.map sigma.fst →
∃ (u w : list Σ a, β a) b, l = u ++ [⟨a, b⟩] ++ w ∧ erase_aux a l = u ++ [] ++ w
| [] := false.elim
| (⟨a', b'⟩::t) := begin
by_cases e : a' = a,
{ subst a',
simpa [erase_aux, and_comm] using show ∃ u w (x : β a),
t = u ++ w ∧ sigma.mk a b' :: t = u ++ ⟨a, x⟩ :: w, from ⟨[], t, b', by simp⟩ },
{ simp [erase_aux, e, ne.symm e],
suffices : ∀ (b : β a) (_ : sigma.mk a b ∈ t), ∃ u w (x : β a),
sigma.mk a' b' :: t = u ++ ⟨a, x⟩ :: w ∧
sigma.mk a' b' :: erase_aux a t = u ++ w,
{ simpa [replace_aux, ne.symm e, e] },
intros b m,
have IH : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (x : β a),
t = u ++ ⟨a, x⟩ :: w ∧ erase_aux a t = u ++ w,
{ simpa using valid.erase_aux t },
rcases IH b m with ⟨u, w, b'', hl, hfl⟩,
exact ⟨⟨a', b'⟩ :: u, w, b'', by simp [hl, hfl.symm]⟩ }
end
theorem valid.erase {n} {bkts : bucket_array α β n} {sz}
(a : α) (Hc : contains_aux a (bkts.read hash_fn a))
(v : valid bkts sz) : valid (bkts.modify hash_fn a (erase_aux a)) (sz-1) :=
begin
have nd := v.nodup (mk_idx n (hash_fn a)),
rcases hash_map.valid.erase_aux a (array.read bkts (mk_idx n (hash_fn a)))
((contains_aux_iff nd).1 Hc) with ⟨u, w, b, hl, hfl⟩,
refine (v.modify hash_fn u [⟨a, b⟩] [] w hl hfl list.nodup_nil _ _ _).2;
{ intros, simp at *; contradiction }
end
end
end hash_map
/-- A hash map data structure, representing a finite key-value map
with key type `α` and value type `β` (which may depend on `α`). -/
structure hash_map (α : Type u) [decidable_eq α] (β : α → Type v) :=
(hash_fn : α → nat)
(size : ℕ)
(nbuckets : ℕ+)
(buckets : bucket_array α β nbuckets)
(is_valid : hash_map.valid hash_fn buckets size)
/-- Construct an empty hash map with buffer size `nbuckets` (default 8). -/
def mk_hash_map {α : Type u} [decidable_eq α] {β : α → Type v} (hash_fn : α → nat) (nbuckets := 8) : hash_map α β :=
let n := if nbuckets = 0 then 8 else nbuckets in
let nz : n > 0 := by abstract { cases nbuckets, {simp, tactic.comp_val}, simp [if_pos, nat.succ_ne_zero], apply nat.zero_lt_succ} in
{ hash_fn := hash_fn,
size := 0,
nbuckets := ⟨n, nz⟩,
buckets := mk_array n [],
is_valid := hash_map.mk_valid _ _ }
namespace hash_map
variables {α : Type u} {β : α → Type v} [decidable_eq α]
/-- Return the value corresponding to a key, or `none` if not found -/
def find (m : hash_map α β) (a : α) : option (β a) :=
find_aux a (m.buckets.read m.hash_fn a)
/-- Return `tt` if the key exists in the map -/
def contains (m : hash_map α β) (a : α) : bool :=
(m.find a).is_some
instance : has_mem α (hash_map α β) := ⟨λa m, m.contains a⟩
/-- Fold a function over the key-value pairs in the map -/
def fold {δ : Type w} (m : hash_map α β) (d : δ) (f : δ → Π a, β a → δ) : δ :=
m.buckets.foldl d f
/-- The list of key-value pairs in the map -/
def entries (m : hash_map α β) : list Σ a, β a :=
m.buckets.as_list
/-- The list of keys in the map -/
def keys (m : hash_map α β) : list α :=
m.entries.map sigma.fst
theorem find_iff (m : hash_map α β) (a : α) (b : β a) :
m.find a = some b ↔ sigma.mk a b ∈ m.entries :=
m.is_valid.find_aux_iff _
theorem contains_iff (m : hash_map α β) (a : α) :
m.contains a ↔ a ∈ m.keys :=
m.is_valid.contains_aux_iff _ _
theorem entries_empty (hash_fn : α → nat) (n) :
(@mk_hash_map α _ β hash_fn n).entries = [] :=
by dsimp [entries, mk_hash_map]; rw mk_as_list
theorem keys_empty (hash_fn : α → nat) (n) :
(@mk_hash_map α _ β hash_fn n).keys = [] :=
by dsimp [keys]; rw entries_empty; refl
theorem find_empty (hash_fn : α → nat) (n a) :
(@mk_hash_map α _ β hash_fn n).find a = none :=
by induction h : (@mk_hash_map α _ β hash_fn n).find a; [refl,
{ have := (find_iff _ _ _).1 h, rw entries_empty at this, contradiction }]
theorem not_contains_empty (hash_fn : α → nat) (n a) :
¬ (@mk_hash_map α _ β hash_fn n).contains a :=
by apply bool_iff_false.2; dsimp [contains]; rw [find_empty]; refl
theorem insert_lemma (hash_fn : α → nat) {n n'}
{bkts : bucket_array α β n} {sz} (v : valid hash_fn bkts sz) :
valid hash_fn (bkts.foldl (mk_array _ [] : bucket_array α β n') (reinsert_aux hash_fn)) sz :=
begin
suffices : ∀ (l : list Σ a, β a) (t : bucket_array α β n') sz,
valid hash_fn t sz → ((l ++ t.as_list).map sigma.fst).nodup →
valid hash_fn (l.foldl (λr (a : Σ a, β a), reinsert_aux hash_fn r a.1 a.2) t) (sz + l.length),
{ have p := this bkts.as_list _ _ (mk_valid _ _),
rw [mk_as_list, list.append_nil, zero_add, v.len] at p,
rw bucket_array.foldl_eq,
exact p (v.as_list_nodup _) },
intro l, induction l with c l IH; intros t sz v nd, {exact v},
rw show sz + (c :: l).length = sz + 1 + l.length, by simp,
rcases (show (l.map sigma.fst).nodup ∧
((bucket_array.as_list t).map sigma.fst).nodup ∧
c.fst ∉ l.map sigma.fst ∧
c.fst ∉ (bucket_array.as_list t).map sigma.fst ∧
(l.map sigma.fst).disjoint ((bucket_array.as_list t).map sigma.fst),
by simpa [list.nodup_append, not_or_distrib, and_comm, and.left_comm] using nd)
with ⟨nd1, nd2, nm1, nm2, dj⟩,
have v' := v.insert _ _ c.2 (λHc, nm2 $ (v.contains_aux_iff _ c.1).1 Hc),
apply IH _ _ v',
suffices : ∀ ⦃a : α⦄ (b : β a), sigma.mk a b ∈ l →
∀ (b' : β a), sigma.mk a b' ∈ (reinsert_aux hash_fn t c.1 c.2).as_list → false,
{ simpa [list.nodup_append, nd1, v'.as_list_nodup _, list.disjoint] },
intros a b m1 b' m2,
rcases (reinsert_aux hash_fn t c.1 c.2).mem_as_list.1 m2 with ⟨i, im⟩,
have : sigma.mk a b' ∉ array.read t i,
{ intro m3,
have : a ∈ list.map sigma.fst t.as_list :=
list.mem_map_of_mem sigma.fst (t.mem_as_list.2 ⟨_, m3⟩),
exact dj (list.mem_map_of_mem sigma.fst m1) this },
by_cases h : mk_idx n' (hash_fn c.1) = i,
{ subst h,
have e : sigma.mk a b' = ⟨c.1, c.2⟩,
{ simpa [reinsert_aux, bucket_array.modify, array.read_write, this] using im },
injection e with e, subst a,
exact nm1.elim (@list.mem_map_of_mem _ _ sigma.fst _ _ m1) },
{ apply this,
simpa [reinsert_aux, bucket_array.modify, array.read_write_of_ne _ _ h] using im }
end
/-- Insert a key-value pair into the map. (Modifies `m` in-place when applicable) -/
def insert : Π (m : hash_map α β) (a : α) (b : β a), hash_map α β
| ⟨hash_fn, size, n, buckets, v⟩ a b :=
let bkt := buckets.read hash_fn a in
if hc : contains_aux a bkt then
{ hash_fn := hash_fn,
size := size,
nbuckets := n,
buckets := buckets.modify hash_fn a (replace_aux a b),
is_valid := v.replace _ a b hc }
else
let size' := size + 1,
buckets' := buckets.modify hash_fn a (λl, ⟨a, b⟩::l),
valid' := v.insert _ a b hc in
if size' ≤ n.1 then
{ hash_fn := hash_fn,
size := size',
nbuckets := n,
buckets := buckets',
is_valid := valid' }
else
let n' : ℕ+ := ⟨n.1 * 2, mul_pos n.2 dec_trivial⟩,
buckets'' : bucket_array α β n' :=
buckets'.foldl (mk_array _ []) (reinsert_aux hash_fn) in
{ hash_fn := hash_fn,
size := size',
nbuckets := n',
buckets := buckets'',
is_valid := insert_lemma _ valid' }
theorem mem_insert : Π (m : hash_map α β) (a b a' b'),
(sigma.mk a' b' : sigma β) ∈ (m.insert a b).entries ↔
if a = a' then b == b' else sigma.mk a' b' ∈ m.entries
| ⟨hash_fn, size, n, bkts, v⟩ a b a' b' := begin
let bkt := bkts.read hash_fn a,
have nd : (bkt.map sigma.fst).nodup := v.nodup (mk_idx n (hash_fn a)),
have lem : Π (bkts' : bucket_array α β n) (v1 u w)
(hl : bucket_array.as_list bkts = u ++ v1 ++ w)
(hfl : bucket_array.as_list bkts' = u ++ [⟨a, b⟩] ++ w)
(veq : (v1 = [] ∧ ¬ contains_aux a bkt) ∨ ∃b'', v1 = [⟨a, b''⟩]),
sigma.mk a' b' ∈ bkts'.as_list ↔
if a = a' then b == b' else sigma.mk a' b' ∈ bkts.as_list,
{ intros bkts' v1 u w hl hfl veq,
rw [hl, hfl],
by_cases h : a = a',
{ subst a',
suffices : b = b' ∨ sigma.mk a b' ∈ u ∨ sigma.mk a b' ∈ w ↔ b = b',
{ simpa [eq_comm, or.left_comm] },
refine or_iff_left_of_imp (not.elim $ not_or_distrib.2 _),
rcases veq with ⟨rfl, Hnc⟩ | ⟨b'', rfl⟩,
{ have na := (not_iff_not_of_iff $ v.contains_aux_iff _ _).1 Hnc,
simp [hl, not_or_distrib] at na, simp [na] },
{ have nd' := v.as_list_nodup _,
simp [hl, list.nodup_append] at nd', simp [nd'] } },
{ suffices : sigma.mk a' b' ∉ v1, {simp [h, ne.symm h, this]},
rcases veq with ⟨rfl, Hnc⟩ | ⟨b'', rfl⟩; simp [ne.symm h] } },
by_cases Hc : (contains_aux a bkt : Prop),
{ rcases hash_map.valid.replace_aux a b (array.read bkts (mk_idx n (hash_fn a)))
((contains_aux_iff nd).1 Hc) with ⟨u', w', b'', hl', hfl'⟩,
rcases (append_of_modify u' [⟨a, b''⟩] [⟨a, b⟩] w' hl' hfl') with ⟨u, w, hl, hfl⟩,
simpa [insert, @dif_pos (contains_aux a bkt) _ Hc]
using lem _ _ u w hl hfl (or.inr ⟨b'', rfl⟩) },
{ let size' := size + 1,
let bkts' := bkts.modify hash_fn a (λl, ⟨a, b⟩::l),
have mi : sigma.mk a' b' ∈ bkts'.as_list ↔
if a = a' then b == b' else sigma.mk a' b' ∈ bkts.as_list :=
let ⟨u, w, hl, hfl⟩ := append_of_modify [] [] [⟨a, b⟩] _ rfl rfl in
lem bkts' _ u w hl hfl $ or.inl ⟨rfl, Hc⟩,
simp [insert, @dif_neg (contains_aux a bkt) _ Hc],
by_cases h : size' ≤ n.1,
-- TODO(Mario): Why does the by_cases assumption look different than the stated one?
{ simpa [show size' ≤ n.1, from h] using mi },
{ let n' : ℕ+ := ⟨n.1 * 2, mul_pos n.2 dec_trivial⟩,
let bkts'' : bucket_array α β n' := bkts'.foldl (mk_array _ []) (reinsert_aux hash_fn),
suffices : sigma.mk a' b' ∈ bkts''.as_list ↔ sigma.mk a' b' ∈ bkts'.as_list.reverse,
{ simpa [show ¬ size' ≤ n.1, from h, mi] },
rw [show bkts'' = bkts'.as_list.foldl _ _, from bkts'.foldl_eq _ _,
← list.foldr_reverse],
induction bkts'.as_list.reverse with a l IH,
{ simp [mk_as_list] },
{ cases a with a'' b'',
let B := l.foldr (λ (y : sigma β) (x : bucket_array α β n'),
reinsert_aux hash_fn x y.1 y.2) (mk_array n'.1 []),
rcases append_of_modify [] [] [⟨a'', b''⟩] _ rfl rfl with ⟨u, w, hl, hfl⟩,
simp [IH.symm, or.left_comm, show B.as_list = _, from hl,
show (reinsert_aux hash_fn B a'' b'').as_list = _, from hfl] } } }
end
theorem find_insert_eq (m : hash_map α β) (a : α) (b : β a) : (m.insert a b).find a = some b :=
(find_iff (m.insert a b) a b).2 $ (mem_insert m a b a b).2 $ by rw if_pos rfl
theorem find_insert_ne (m : hash_map α β) (a a' : α) (b : β a) (h : a ≠ a') :
(m.insert a b).find a' = m.find a' :=
option.eq_of_eq_some $ λb',
let t := mem_insert m a b a' b' in
(find_iff _ _ _).trans $ iff.trans (by rwa if_neg h at t) (find_iff _ _ _).symm
theorem find_insert (m : hash_map α β) (a' a : α) (b : β a) :
(m.insert a b).find a' = if h : a = a' then some (eq.rec_on h b) else m.find a' :=
if h : a = a' then by rw dif_pos h; exact
match a', h with ._, rfl := find_insert_eq m a b end
else by rw dif_neg h; exact find_insert_ne m a a' b h
/-- Insert a list of key-value pairs into the map. (Modifies `m` in-place when applicable) -/
def insert_all (l : list (Σ a, β a)) (m : hash_map α β) : hash_map α β :=
l.foldl (λ m ⟨a, b⟩, insert m a b) m
/-- Construct a hash map from a list of key-value pairs. -/
def of_list (l : list (Σ a, β a)) (hash_fn) : hash_map α β :=
insert_all l (mk_hash_map hash_fn (2 * l.length))
/-- Remove a key from the map. (Modifies `m` in-place when applicable) -/
def erase (m : hash_map α β) (a : α) : hash_map α β :=
match m with ⟨hash_fn, size, n, buckets, v⟩ :=
if hc : contains_aux a (buckets.read hash_fn a) then
{ hash_fn := hash_fn,
size := size - 1,
nbuckets := n,
buckets := buckets.modify hash_fn a (erase_aux a),
is_valid := v.erase _ a hc }
else m
end
theorem mem_erase : Π (m : hash_map α β) (a a' b'),
(sigma.mk a' b' : sigma β) ∈ (m.erase a).entries ↔
a ≠ a' ∧ sigma.mk a' b' ∈ m.entries
| ⟨hash_fn, size, n, bkts, v⟩ a a' b' := begin
let bkt := bkts.read hash_fn a,
by_cases Hc : (contains_aux a bkt : Prop),
{ let bkts' := bkts.modify hash_fn a (erase_aux a),
suffices : sigma.mk a' b' ∈ bkts'.as_list ↔ a ≠ a' ∧ sigma.mk a' b' ∈ bkts.as_list,
{ simpa [erase, @dif_pos (contains_aux a bkt) _ Hc] },
have nd := v.nodup (mk_idx n (hash_fn a)),
rcases valid.erase_aux a bkt ((contains_aux_iff nd).1 Hc) with ⟨u', w', b, hl', hfl'⟩,
rcases append_of_modify u' [⟨a, b⟩] [] _ hl' hfl' with ⟨u, w, hl, hfl⟩,
suffices : ∀_:sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w, a ≠ a',
{ have : sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w ↔ (¬a = a' ∧ a' = a) ∧ b' == b ∨
¬a = a' ∧ (sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w),
{ simp [eq_comm, not_and_self_iff, and_iff_right_of_imp this] },
simpa [hl, show bkts'.as_list = _, from hfl, and_or_distrib_left,
and_comm, and.left_comm, or.left_comm] },
intros m e, subst a', revert m, apply not_or_distrib.2,
have nd' := v.as_list_nodup _,
simp [hl, list.nodup_append] at nd', simp [nd'] },
{ suffices : ∀_:sigma.mk a' b' ∈ bucket_array.as_list bkts, a ≠ a',
{ simp [erase, @dif_neg (contains_aux a bkt) _ Hc, entries, and_iff_right_of_imp this] },
intros m e, subst a',
exact Hc ((v.contains_aux_iff _ _).2 (list.mem_map_of_mem sigma.fst m)) }
end
theorem find_erase_eq (m : hash_map α β) (a : α) : (m.erase a).find a = none :=
begin
cases h : (m.erase a).find a with b, {refl},
exact absurd rfl ((mem_erase m a a b).1 ((find_iff (m.erase a) a b).1 h)).left
end
theorem find_erase_ne (m : hash_map α β) (a a' : α) (h : a ≠ a') :
(m.erase a).find a' = m.find a' :=
option.eq_of_eq_some $ λb',
(find_iff _ _ _).trans $ (mem_erase m a a' b').trans $
(and_iff_right h).trans (find_iff _ _ _).symm
theorem find_erase (m : hash_map α β) (a' a : α) :
(m.erase a).find a' = if a = a' then none else m.find a' :=
if h : a = a' then by subst a'; simp [find_erase_eq m a]
else by rw if_neg h; exact find_erase_ne m a a' h
section string
variables [has_to_string α] [∀ a, has_to_string (β a)]
open prod
private def key_data_to_string (a : α) (b : β a) (first : bool) : string :=
(if first then "" else ", ") ++ sformat!"{a} ← {b}"
private def to_string (m : hash_map α β) : string :=
"⟨" ++ (fst (fold m ("", tt) (λ p a b, (fst p ++ key_data_to_string a b (snd p), ff)))) ++ "⟩"
instance : has_to_string (hash_map α β) :=
⟨to_string⟩
end string
section format
open format prod
variables [has_to_format α] [∀ a, has_to_format (β a)]
private meta def format_key_data (a : α) (b : β a) (first : bool) : format :=
(if first then to_fmt "" else to_fmt "," ++ line) ++ to_fmt a ++ space ++ to_fmt "←" ++ space ++ to_fmt b
private meta def to_format (m : hash_map α β) : format :=
group $ to_fmt "⟨" ++ nest 1 (fst (fold m (to_fmt "", tt) (λ p a b, (fst p ++ format_key_data a b (snd p), ff)))) ++
to_fmt "⟩"
meta instance : has_to_format (hash_map α β) :=
⟨to_format⟩
end format
end hash_map
|
0be19ad91a5da63da1e36829dc4c26e3aeb2ec81 | 626e312b5c1cb2d88fca108f5933076012633192 | /src/topology/metric_space/emetric_space.lean | 95b49c542367d079243ef0c73a1413bdd7137d04 | [
"Apache-2.0"
] | permissive | Bioye97/mathlib | 9db2f9ee54418d29dd06996279ba9dc874fd6beb | 782a20a27ee83b523f801ff34efb1a9557085019 | refs/heads/master | 1,690,305,956,488 | 1,631,067,774,000 | 1,631,067,774,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 46,829 | lean | /-
Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel
-/
import data.real.ennreal
import data.finset.intervals
import topology.uniform_space.uniform_embedding
import topology.uniform_space.pi
import topology.uniform_space.uniform_convergence
/-!
# Extended metric spaces
This file is devoted to the definition and study of `emetric_spaces`, i.e., metric
spaces in which the distance is allowed to take the value ∞. This extended distance is
called `edist`, and takes values in `ℝ≥0∞`.
Many definitions and theorems expected on emetric spaces are already introduced on uniform spaces
and topological spaces. For example: open and closed sets, compactness, completeness, continuity and
uniform continuity.
The class `emetric_space` therefore extends `uniform_space` (and `topological_space`).
Since a lot of elementary properties don't require `eq_of_edist_eq_zero` we start setting up the
theory of `pseudo_emetric_space`, where we don't require `edist x y = 0 → x = y` and we specialize
to `emetric_space` at the end.
-/
open set filter classical
noncomputable theory
open_locale uniformity topological_space big_operators filter nnreal ennreal
universes u v w
variables {α : Type u} {β : Type v}
/-- Characterizing uniformities associated to a (generalized) distance function `D`
in terms of the elements of the uniformity. -/
theorem uniformity_dist_of_mem_uniformity [linear_order β] {U : filter (α × α)} (z : β)
(D : α → α → β) (H : ∀ s, s ∈ U ↔ ∃ε>z, ∀{a b:α}, D a b < ε → (a, b) ∈ s) :
U = ⨅ ε>z, 𝓟 {p:α×α | D p.1 p.2 < ε} :=
le_antisymm
(le_infi $ λ ε, le_infi $ λ ε0, le_principal_iff.2 $ (H _).2 ⟨ε, ε0, λ a b, id⟩)
(λ r ur, let ⟨ε, ε0, h⟩ := (H _).1 ur in
mem_infi_of_mem ε $ mem_infi_of_mem ε0 $ mem_principal.2 $ λ ⟨a, b⟩, h)
/-- `has_edist α` means that `α` is equipped with an extended distance. -/
class has_edist (α : Type*) := (edist : α → α → ℝ≥0∞)
export has_edist (edist)
/-- Creating a uniform space from an extended distance. -/
def uniform_space_of_edist
(edist : α → α → ℝ≥0∞)
(edist_self : ∀ x : α, edist x x = 0)
(edist_comm : ∀ x y : α, edist x y = edist y x)
(edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z) : uniform_space α :=
uniform_space.of_core {
uniformity := (⨅ ε>0, 𝓟 {p:α×α | edist p.1 p.2 < ε}),
refl := le_infi $ assume ε, le_infi $
by simp [set.subset_def, id_rel, edist_self, (>)] {contextual := tt},
comp :=
le_infi $ assume ε, le_infi $ assume h,
have (2 : ℝ≥0∞) = (2 : ℕ) := by simp,
have A : 0 < ε / 2 := ennreal.div_pos_iff.2
⟨ne_of_gt h, by { convert ennreal.nat_ne_top 2 }⟩,
lift'_le
(mem_infi_of_mem (ε / 2) $ mem_infi_of_mem A (subset.refl _)) $
have ∀ (a b c : α), edist a c < ε / 2 → edist c b < ε / 2 → edist a b < ε,
from assume a b c hac hcb,
calc edist a b ≤ edist a c + edist c b : edist_triangle _ _ _
... < ε / 2 + ε / 2 : ennreal.add_lt_add hac hcb
... = ε : by rw [ennreal.add_halves],
by simpa [comp_rel],
symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h,
tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [edist_comm] }
-- the uniform structure is embedded in the emetric space structure
-- to avoid instance diamond issues. See Note [forgetful inheritance].
/-- Extended (pseudo) metric spaces, with an extended distance `edist` possibly taking the
value ∞
Each pseudo_emetric space induces a canonical `uniform_space` and hence a canonical
`topological_space`.
This is enforced in the type class definition, by extending the `uniform_space` structure. When
instantiating a `pseudo_emetric_space` structure, the uniformity fields are not necessary, they
will be filled in by default. There is a default value for the uniformity, that can be substituted
in cases of interest, for instance when instantiating a `pseudo_emetric_space` structure
on a product.
Continuity of `edist` is proved in `topology.instances.ennreal`
-/
class pseudo_emetric_space (α : Type u) extends has_edist α : Type u :=
(edist_self : ∀ x : α, edist x x = 0)
(edist_comm : ∀ x y : α, edist x y = edist y x)
(edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z)
(to_uniform_space : uniform_space α :=
uniform_space_of_edist edist edist_self edist_comm edist_triangle)
(uniformity_edist : 𝓤 α = ⨅ ε>0, 𝓟 {p:α×α | edist p.1 p.2 < ε} . control_laws_tac)
/- Pseudoemetric spaces are less common than metric spaces. Therefore, we work in a dedicated
namespace, while notions associated to metric spaces are mostly in the root namespace. -/
variables [pseudo_emetric_space α]
@[priority 100] -- see Note [lower instance priority]
instance pseudo_emetric_space.to_uniform_space' : uniform_space α :=
pseudo_emetric_space.to_uniform_space
export pseudo_emetric_space (edist_self edist_comm edist_triangle)
attribute [simp] edist_self
/-- Triangle inequality for the extended distance -/
theorem edist_triangle_left (x y z : α) : edist x y ≤ edist z x + edist z y :=
by rw edist_comm z; apply edist_triangle
theorem edist_triangle_right (x y z : α) : edist x y ≤ edist x z + edist y z :=
by rw edist_comm y; apply edist_triangle
lemma edist_triangle4 (x y z t : α) :
edist x t ≤ edist x y + edist y z + edist z t :=
calc
edist x t ≤ edist x z + edist z t : edist_triangle x z t
... ≤ (edist x y + edist y z) + edist z t : add_le_add_right (edist_triangle x y z) _
/-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/
lemma edist_le_Ico_sum_edist (f : ℕ → α) {m n} (h : m ≤ n) :
edist (f m) (f n) ≤ ∑ i in finset.Ico m n, edist (f i) (f (i + 1)) :=
begin
revert n,
refine nat.le_induction _ _,
{ simp only [finset.sum_empty, finset.Ico.self_eq_empty, edist_self],
-- TODO: Why doesn't Lean close this goal automatically? `apply le_refl` fails too.
exact le_refl (0:ℝ≥0∞) },
{ assume n hn hrec,
calc edist (f m) (f (n+1)) ≤ edist (f m) (f n) + edist (f n) (f (n+1)) : edist_triangle _ _ _
... ≤ ∑ i in finset.Ico m n, _ + _ : add_le_add hrec (le_refl _)
... = ∑ i in finset.Ico m (n+1), _ :
by rw [finset.Ico.succ_top hn, finset.sum_insert, add_comm]; simp }
end
/-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/
lemma edist_le_range_sum_edist (f : ℕ → α) (n : ℕ) :
edist (f 0) (f n) ≤ ∑ i in finset.range n, edist (f i) (f (i + 1)) :=
finset.Ico.zero_bot n ▸ edist_le_Ico_sum_edist f (nat.zero_le n)
/-- A version of `edist_le_Ico_sum_edist` with each intermediate distance replaced
with an upper estimate. -/
lemma edist_le_Ico_sum_of_edist_le {f : ℕ → α} {m n} (hmn : m ≤ n)
{d : ℕ → ℝ≥0∞} (hd : ∀ {k}, m ≤ k → k < n → edist (f k) (f (k + 1)) ≤ d k) :
edist (f m) (f n) ≤ ∑ i in finset.Ico m n, d i :=
le_trans (edist_le_Ico_sum_edist f hmn) $
finset.sum_le_sum $ λ k hk, hd (finset.Ico.mem.1 hk).1 (finset.Ico.mem.1 hk).2
/-- A version of `edist_le_range_sum_edist` with each intermediate distance replaced
with an upper estimate. -/
lemma edist_le_range_sum_of_edist_le {f : ℕ → α} (n : ℕ)
{d : ℕ → ℝ≥0∞} (hd : ∀ {k}, k < n → edist (f k) (f (k + 1)) ≤ d k) :
edist (f 0) (f n) ≤ ∑ i in finset.range n, d i :=
finset.Ico.zero_bot n ▸ edist_le_Ico_sum_of_edist_le (zero_le n) (λ _ _, hd)
/-- Reformulation of the uniform structure in terms of the extended distance -/
theorem uniformity_pseudoedist :
𝓤 α = ⨅ ε>0, 𝓟 {p:α×α | edist p.1 p.2 < ε} :=
pseudo_emetric_space.uniformity_edist
theorem uniformity_basis_edist :
(𝓤 α).has_basis (λ ε : ℝ≥0∞, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 < ε}) :=
(@uniformity_pseudoedist α _).symm ▸ has_basis_binfi_principal
(λ r hr p hp, ⟨min r p, lt_min hr hp,
λ x hx, lt_of_lt_of_le hx (min_le_left _ _),
λ x hx, lt_of_lt_of_le hx (min_le_right _ _)⟩)
⟨1, ennreal.zero_lt_one⟩
/-- Characterization of the elements of the uniformity in terms of the extended distance -/
theorem mem_uniformity_edist {s : set (α×α)} :
s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, edist a b < ε → (a, b) ∈ s) :=
uniformity_basis_edist.mem_uniformity_iff
/-- Given `f : β → ℝ≥0∞`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`.
For specific bases see `uniformity_basis_edist`, `uniformity_basis_edist'`,
`uniformity_basis_edist_nnreal`, and `uniformity_basis_edist_inv_nat`. -/
protected theorem emetric.mk_uniformity_basis {β : Type*} {p : β → Prop} {f : β → ℝ≥0∞}
(hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) :
(𝓤 α).has_basis p (λ x, {p:α×α | edist p.1 p.2 < f x}) :=
begin
refine ⟨λ s, uniformity_basis_edist.mem_iff.trans _⟩,
split,
{ rintros ⟨ε, ε₀, hε⟩,
rcases hf ε ε₀ with ⟨i, hi, H⟩,
exact ⟨i, hi, λ x hx, hε $ lt_of_lt_of_le hx H⟩ },
{ exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, H⟩ }
end
/-- Given `f : β → ℝ≥0∞`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then closed `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`.
For specific bases see `uniformity_basis_edist_le` and `uniformity_basis_edist_le'`. -/
protected theorem emetric.mk_uniformity_basis_le {β : Type*} {p : β → Prop} {f : β → ℝ≥0∞}
(hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) :
(𝓤 α).has_basis p (λ x, {p:α×α | edist p.1 p.2 ≤ f x}) :=
begin
refine ⟨λ s, uniformity_basis_edist.mem_iff.trans _⟩,
split,
{ rintros ⟨ε, ε₀, hε⟩,
rcases exists_between ε₀ with ⟨ε', hε'⟩,
rcases hf ε' hε'.1 with ⟨i, hi, H⟩,
exact ⟨i, hi, λ x hx, hε $ lt_of_le_of_lt (le_trans hx H) hε'.2⟩ },
{ exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, λ x hx, H (le_of_lt hx)⟩ }
end
theorem uniformity_basis_edist_le :
(𝓤 α).has_basis (λ ε : ℝ≥0∞, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 ≤ ε}) :=
emetric.mk_uniformity_basis_le (λ _, id) (λ ε ε₀, ⟨ε, ε₀, le_refl ε⟩)
theorem uniformity_basis_edist' (ε' : ℝ≥0∞) (hε' : 0 < ε') :
(𝓤 α).has_basis (λ ε : ℝ≥0∞, ε ∈ Ioo 0 ε') (λ ε, {p:α×α | edist p.1 p.2 < ε}) :=
emetric.mk_uniformity_basis (λ _, and.left)
(λ ε ε₀, let ⟨δ, hδ⟩ := exists_between hε' in
⟨min ε δ, ⟨lt_min ε₀ hδ.1, lt_of_le_of_lt (min_le_right _ _) hδ.2⟩, min_le_left _ _⟩)
theorem uniformity_basis_edist_le' (ε' : ℝ≥0∞) (hε' : 0 < ε') :
(𝓤 α).has_basis (λ ε : ℝ≥0∞, ε ∈ Ioo 0 ε') (λ ε, {p:α×α | edist p.1 p.2 ≤ ε}) :=
emetric.mk_uniformity_basis_le (λ _, and.left)
(λ ε ε₀, let ⟨δ, hδ⟩ := exists_between hε' in
⟨min ε δ, ⟨lt_min ε₀ hδ.1, lt_of_le_of_lt (min_le_right _ _) hδ.2⟩, min_le_left _ _⟩)
theorem uniformity_basis_edist_nnreal :
(𝓤 α).has_basis (λ ε : ℝ≥0, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 < ε}) :=
emetric.mk_uniformity_basis (λ _, ennreal.coe_pos.2)
(λ ε ε₀, let ⟨δ, hδ⟩ := ennreal.lt_iff_exists_nnreal_btwn.1 ε₀ in
⟨δ, ennreal.coe_pos.1 hδ.1, le_of_lt hδ.2⟩)
theorem uniformity_basis_edist_inv_nat :
(𝓤 α).has_basis (λ _, true) (λ n:ℕ, {p:α×α | edist p.1 p.2 < (↑n)⁻¹}) :=
emetric.mk_uniformity_basis
(λ n _, ennreal.inv_pos.2 $ ennreal.nat_ne_top n)
(λ ε ε₀, let ⟨n, hn⟩ := ennreal.exists_inv_nat_lt (ne_of_gt ε₀) in ⟨n, trivial, le_of_lt hn⟩)
theorem uniformity_basis_edist_inv_two_pow :
(𝓤 α).has_basis (λ _, true) (λ n:ℕ, {p:α×α | edist p.1 p.2 < 2⁻¹ ^ n}) :=
emetric.mk_uniformity_basis
(λ n _, ennreal.pow_pos (ennreal.inv_pos.2 ennreal.two_ne_top) _)
(λ ε ε₀, let ⟨n, hn⟩ := ennreal.exists_inv_two_pow_lt (ne_of_gt ε₀) in ⟨n, trivial, le_of_lt hn⟩)
/-- Fixed size neighborhoods of the diagonal belong to the uniform structure -/
theorem edist_mem_uniformity {ε:ℝ≥0∞} (ε0 : 0 < ε) :
{p:α×α | edist p.1 p.2 < ε} ∈ 𝓤 α :=
mem_uniformity_edist.2 ⟨ε, ε0, λ a b, id⟩
namespace emetric
theorem uniformity_has_countable_basis : is_countably_generated (𝓤 α) :=
is_countably_generated_of_seq ⟨_, uniformity_basis_edist_inv_nat.eq_infi⟩
/-- ε-δ characterization of uniform continuity on a set for pseudoemetric spaces -/
theorem uniform_continuous_on_iff [pseudo_emetric_space β] {f : α → β} {s : set α} :
uniform_continuous_on f s ↔ ∀ ε > 0, ∃ δ > 0,
∀{a b}, a ∈ s → b ∈ s → edist a b < δ → edist (f a) (f b) < ε :=
uniformity_basis_edist.uniform_continuous_on_iff uniformity_basis_edist
/-- ε-δ characterization of uniform continuity on pseudoemetric spaces -/
theorem uniform_continuous_iff [pseudo_emetric_space β] {f : α → β} :
uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0,
∀{a b:α}, edist a b < δ → edist (f a) (f b) < ε :=
uniformity_basis_edist.uniform_continuous_iff uniformity_basis_edist
/-- ε-δ characterization of uniform embeddings on pseudoemetric spaces -/
theorem uniform_embedding_iff [pseudo_emetric_space β] {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧
∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ :=
uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl
⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (edist_mem_uniformity δ0),
⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 tu in
⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩,
λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_edist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in
⟨_, edist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩
/-- If a map between pseudoemetric spaces is a uniform embedding then the edistance between `f x`
and `f y` is controlled in terms of the distance between `x` and `y`. -/
theorem controlled_of_uniform_embedding [pseudo_emetric_space β] {f : α → β} :
uniform_embedding f →
(∀ ε > 0, ∃ δ > 0, ∀ {a b : α}, edist a b < δ → edist (f a) (f b) < ε) ∧
(∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ) :=
begin
assume h,
exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1,
(uniform_embedding_iff.1 h).2.2⟩,
end
/-- ε-δ characterization of Cauchy sequences on pseudoemetric spaces -/
protected lemma cauchy_iff {f : filter α} :
cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, edist x y < ε :=
by rw ← ne_bot_iff; exact uniformity_basis_edist.cauchy_iff
/-- A very useful criterion to show that a space is complete is to show that all sequences
which satisfy a bound of the form `edist (u n) (u m) < B N` for all `n m ≥ N` are
converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to
`0`, which makes it possible to use arguments of converging series, while this is impossible
to do in general for arbitrary Cauchy sequences. -/
theorem complete_of_convergent_controlled_sequences (B : ℕ → ℝ≥0∞) (hB : ∀n, 0 < B n)
(H : ∀u : ℕ → α, (∀N n m : ℕ, N ≤ n → N ≤ m → edist (u n) (u m) < B N) →
∃x, tendsto u at_top (𝓝 x)) :
complete_space α :=
uniform_space.complete_of_convergent_controlled_sequences
uniformity_has_countable_basis
(λ n, {p:α×α | edist p.1 p.2 < B n}) (λ n, edist_mem_uniformity $ hB n) H
/-- A sequentially complete pseudoemetric space is complete. -/
theorem complete_of_cauchy_seq_tendsto :
(∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) → complete_space α :=
uniform_space.complete_of_cauchy_seq_tendsto uniformity_has_countable_basis
/-- Expressing locally uniform convergence on a set using `edist`. -/
lemma tendsto_locally_uniformly_on_iff {ι : Type*} [topological_space β]
{F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} :
tendsto_locally_uniformly_on F f p s ↔
∀ ε > 0, ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, ∀ᶠ n in p, ∀ y ∈ t, edist (f y) (F n y) < ε :=
begin
refine ⟨λ H ε hε, H _ (edist_mem_uniformity hε), λ H u hu x hx, _⟩,
rcases mem_uniformity_edist.1 hu with ⟨ε, εpos, hε⟩,
rcases H ε εpos x hx with ⟨t, ht, Ht⟩,
exact ⟨t, ht, Ht.mono (λ n hs x hx, hε (hs x hx))⟩
end
/-- Expressing uniform convergence on a set using `edist`. -/
lemma tendsto_uniformly_on_iff {ι : Type*}
{F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} :
tendsto_uniformly_on F f p s ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x ∈ s, edist (f x) (F n x) < ε :=
begin
refine ⟨λ H ε hε, H _ (edist_mem_uniformity hε), λ H u hu, _⟩,
rcases mem_uniformity_edist.1 hu with ⟨ε, εpos, hε⟩,
exact (H ε εpos).mono (λ n hs x hx, hε (hs x hx))
end
/-- Expressing locally uniform convergence using `edist`. -/
lemma tendsto_locally_uniformly_iff {ι : Type*} [topological_space β]
{F : ι → β → α} {f : β → α} {p : filter ι} :
tendsto_locally_uniformly F f p ↔
∀ ε > 0, ∀ (x : β), ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, edist (f y) (F n y) < ε :=
by simp only [← tendsto_locally_uniformly_on_univ, tendsto_locally_uniformly_on_iff,
mem_univ, forall_const, exists_prop, nhds_within_univ]
/-- Expressing uniform convergence using `edist`. -/
lemma tendsto_uniformly_iff {ι : Type*}
{F : ι → β → α} {f : β → α} {p : filter ι} :
tendsto_uniformly F f p ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x, edist (f x) (F n x) < ε :=
by simp only [← tendsto_uniformly_on_univ, tendsto_uniformly_on_iff, mem_univ, forall_const]
end emetric
open emetric
/-- Auxiliary function to replace the uniformity on a pseudoemetric space with
a uniformity which is equal to the original one, but maybe not defeq.
This is useful if one wants to construct a pseudoemetric space with a
specified uniformity. See Note [forgetful inheritance] explaining why having definitionally
the right uniformity is often important.
-/
def pseudo_emetric_space.replace_uniformity {α} [U : uniform_space α] (m : pseudo_emetric_space α)
(H : @uniformity _ U = @uniformity _ pseudo_emetric_space.to_uniform_space) :
pseudo_emetric_space α :=
{ edist := @edist _ m.to_has_edist,
edist_self := edist_self,
edist_comm := edist_comm,
edist_triangle := edist_triangle,
to_uniform_space := U,
uniformity_edist := H.trans (@pseudo_emetric_space.uniformity_edist α _) }
/-- The extended pseudometric induced by a function taking values in a pseudoemetric space. -/
def pseudo_emetric_space.induced {α β} (f : α → β)
(m : pseudo_emetric_space β) : pseudo_emetric_space α :=
{ edist := λ x y, edist (f x) (f y),
edist_self := λ x, edist_self _,
edist_comm := λ x y, edist_comm _ _,
edist_triangle := λ x y z, edist_triangle _ _ _,
to_uniform_space := uniform_space.comap f m.to_uniform_space,
uniformity_edist := begin
apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, edist (f x) (f y)),
refine λ s, mem_comap.trans _,
split; intro H,
{ rcases H with ⟨r, ru, rs⟩,
rcases mem_uniformity_edist.1 ru with ⟨ε, ε0, hε⟩,
refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h },
{ rcases H with ⟨ε, ε0, hε⟩,
exact ⟨_, edist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ }
end }
/-- Pseudoemetric space instance on subsets of pseudoemetric spaces -/
instance {α : Type*} {p : α → Prop} [t : pseudo_emetric_space α] :
pseudo_emetric_space (subtype p) := t.induced coe
/-- The extended psuedodistance on a subset of a pseudoemetric space is the restriction of
the original pseudodistance, by definition -/
theorem subtype.edist_eq {p : α → Prop} (x y : subtype p) : edist x y = edist (x : α) y := rfl
/-- The product of two pseudoemetric spaces, with the max distance, is an extended
pseudometric spaces. We make sure that the uniform structure thus constructed is the one
corresponding to the product of uniform spaces, to avoid diamond problems. -/
instance prod.pseudo_emetric_space_max [pseudo_emetric_space β] : pseudo_emetric_space (α × β) :=
{ edist := λ x y, max (edist x.1 y.1) (edist x.2 y.2),
edist_self := λ x, by simp,
edist_comm := λ x y, by simp [edist_comm],
edist_triangle := λ x y z, max_le
(le_trans (edist_triangle _ _ _) (add_le_add (le_max_left _ _) (le_max_left _ _)))
(le_trans (edist_triangle _ _ _) (add_le_add (le_max_right _ _) (le_max_right _ _))),
uniformity_edist := begin
refine uniformity_prod.trans _,
simp [pseudo_emetric_space.uniformity_edist, comap_infi],
rw ← infi_inf_eq, congr, funext,
rw ← infi_inf_eq, congr, funext,
simp [inf_principal, ext_iff, max_lt_iff]
end,
to_uniform_space := prod.uniform_space }
lemma prod.edist_eq [pseudo_emetric_space β] (x y : α × β) :
edist x y = max (edist x.1 y.1) (edist x.2 y.2) :=
rfl
section pi
open finset
variables {π : β → Type*} [fintype β]
/-- The product of a finite number of pseudoemetric spaces, with the max distance, is still
a pseudoemetric space.
This construction would also work for infinite products, but it would not give rise
to the product topology. Hence, we only formalize it in the good situation of finitely many
spaces. -/
instance pseudo_emetric_space_pi [∀b, pseudo_emetric_space (π b)] :
pseudo_emetric_space (Πb, π b) :=
{ edist := λ f g, finset.sup univ (λb, edist (f b) (g b)),
edist_self := assume f, bot_unique $ finset.sup_le $ by simp,
edist_comm := assume f g, by unfold edist; congr; funext a; exact edist_comm _ _,
edist_triangle := assume f g h,
begin
simp only [finset.sup_le_iff],
assume b hb,
exact le_trans (edist_triangle _ (g b) _) (add_le_add (le_sup hb) (le_sup hb))
end,
to_uniform_space := Pi.uniform_space _,
uniformity_edist := begin
simp only [Pi.uniformity, pseudo_emetric_space.uniformity_edist, comap_infi, gt_iff_lt,
preimage_set_of_eq, comap_principal],
rw infi_comm, congr, funext ε,
rw infi_comm, congr, funext εpos,
change 0 < ε at εpos,
simp [set.ext_iff, εpos]
end }
lemma edist_pi_def [Π b, pseudo_emetric_space (π b)] (f g : Π b, π b) :
edist f g = finset.sup univ (λb, edist (f b) (g b)) := rfl
@[simp]
lemma edist_pi_const [nonempty β] (a b : α) :
edist (λ x : β, a) (λ _, b) = edist a b := finset.sup_const univ_nonempty (edist a b)
lemma edist_le_pi_edist [Π b, pseudo_emetric_space (π b)] (f g : Π b, π b) (b : β) :
edist (f b) (g b) ≤ edist f g :=
finset.le_sup (finset.mem_univ b)
lemma edist_pi_le_iff [Π b, pseudo_emetric_space (π b)] {f g : Π b, π b} {d : ℝ≥0∞} :
edist f g ≤ d ↔ ∀ b, edist (f b) (g b) ≤ d :=
finset.sup_le_iff.trans $ by simp only [finset.mem_univ, forall_const]
end pi
namespace emetric
variables {x y z : α} {ε ε₁ ε₂ : ℝ≥0∞} {s : set α}
/-- `emetric.ball x ε` is the set of all points `y` with `edist y x < ε` -/
def ball (x : α) (ε : ℝ≥0∞) : set α := {y | edist y x < ε}
@[simp] theorem mem_ball : y ∈ ball x ε ↔ edist y x < ε := iff.rfl
theorem mem_ball' : y ∈ ball x ε ↔ edist x y < ε := by rw edist_comm; refl
/-- `emetric.closed_ball x ε` is the set of all points `y` with `edist y x ≤ ε` -/
def closed_ball (x : α) (ε : ℝ≥0∞) := {y | edist y x ≤ ε}
@[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ edist y x ≤ ε := iff.rfl
@[simp] theorem closed_ball_top (x : α) : closed_ball x ∞ = univ :=
eq_univ_of_forall $ λ y, @le_top _ _ (edist y x)
theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε :=
assume y hy, le_of_lt hy
theorem pos_of_mem_ball (hy : y ∈ ball x ε) : 0 < ε :=
lt_of_le_of_lt (zero_le _) hy
theorem mem_ball_self (h : 0 < ε) : x ∈ ball x ε :=
show edist x x < ε, by rw edist_self; assumption
theorem mem_closed_ball_self : x ∈ closed_ball x ε :=
show edist x x ≤ ε, by rw edist_self; exact bot_le
theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε :=
by simp [edist_comm]
theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ :=
λ y (yx : _ < ε₁), lt_of_lt_of_le yx h
theorem closed_ball_subset_closed_ball (h : ε₁ ≤ ε₂) :
closed_ball x ε₁ ⊆ closed_ball x ε₂ :=
λ y (yx : _ ≤ ε₁), le_trans yx h
theorem ball_disjoint (h : ε₁ + ε₂ ≤ edist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩,
not_lt_of_le (edist_triangle_left x y z)
(lt_of_lt_of_le (ennreal.add_lt_add h₁ h₂) h)
theorem ball_subset (h : edist x y + ε₁ ≤ ε₂) (h' : edist x y < ⊤) : ball x ε₁ ⊆ ball y ε₂ :=
λ z zx, calc
edist z y ≤ edist z x + edist x y : edist_triangle _ _ _
... = edist x y + edist z x : add_comm _ _
... < edist x y + ε₁ : (ennreal.add_lt_add_iff_left h').2 zx
... ≤ ε₂ : h
theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε :=
begin
have : 0 < ε - edist y x := by simpa using h,
refine ⟨ε - edist y x, this, ball_subset _ _⟩,
{ rw ennreal.add_sub_cancel_of_le (le_of_lt h), apply le_refl _},
{ have : edist y x ≠ ⊤ := ne_top_of_lt h, apply lt_top_iff_ne_top.2 this }
end
theorem ball_eq_empty_iff : ball x ε = ∅ ↔ ε = 0 :=
eq_empty_iff_forall_not_mem.trans
⟨λh, le_bot_iff.1 (le_of_not_gt (λ ε0, h _ (mem_ball_self ε0))),
λε0 y h, not_lt_of_le (le_of_eq ε0) (pos_of_mem_ball h)⟩
/-- Relation “two points are at a finite edistance” is an equivalence relation. -/
def edist_lt_top_setoid : setoid α :=
{ r := λ x y, edist x y < ⊤,
iseqv := ⟨λ x, by { rw edist_self, exact ennreal.coe_lt_top },
λ x y h, by rwa edist_comm,
λ x y z hxy hyz, lt_of_le_of_lt (edist_triangle x y z) (ennreal.add_lt_top.2 ⟨hxy, hyz⟩)⟩ }
@[simp] lemma ball_zero : ball x 0 = ∅ :=
by rw [emetric.ball_eq_empty_iff]
theorem nhds_basis_eball : (𝓝 x).has_basis (λ ε:ℝ≥0∞, 0 < ε) (ball x) :=
nhds_basis_uniformity uniformity_basis_edist
theorem nhds_basis_closed_eball : (𝓝 x).has_basis (λ ε:ℝ≥0∞, 0 < ε) (closed_ball x) :=
nhds_basis_uniformity uniformity_basis_edist_le
theorem nhds_eq : 𝓝 x = (⨅ε>0, 𝓟 (ball x ε)) :=
nhds_basis_eball.eq_binfi
theorem mem_nhds_iff : s ∈ 𝓝 x ↔ ∃ε>0, ball x ε ⊆ s := nhds_basis_eball.mem_iff
theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s :=
by simp [is_open_iff_nhds, mem_nhds_iff]
theorem is_open_ball : is_open (ball x ε) :=
is_open_iff.2 $ λ y, exists_ball_subset_ball
theorem is_closed_ball_top : is_closed (ball x ⊤) :=
is_open_compl_iff.1 $ is_open_iff.2 $ λ y hy, ⟨⊤, ennreal.coe_lt_top, subset_compl_iff_disjoint.2 $
ball_disjoint $ by { rw ennreal.top_add, exact le_of_not_lt hy }⟩
theorem ball_mem_nhds (x : α) {ε : ℝ≥0∞} (ε0 : 0 < ε) : ball x ε ∈ 𝓝 x :=
is_open_ball.mem_nhds (mem_ball_self ε0)
theorem closed_ball_mem_nhds (x : α) {ε : ℝ≥0∞} (ε0 : 0 < ε) : closed_ball x ε ∈ 𝓝 x :=
mem_of_superset (ball_mem_nhds x ε0) ball_subset_closed_ball
theorem ball_prod_same [pseudo_emetric_space β] (x : α) (y : β) (r : ℝ≥0∞) :
(ball x r).prod (ball y r) = ball (x, y) r :=
ext $ λ z, max_lt_iff.symm
theorem closed_ball_prod_same [pseudo_emetric_space β] (x : α) (y : β) (r : ℝ≥0∞) :
(closed_ball x r).prod (closed_ball y r) = closed_ball (x, y) r :=
ext $ λ z, max_le_iff.symm
/-- ε-characterization of the closure in pseudoemetric spaces -/
theorem mem_closure_iff :
x ∈ closure s ↔ ∀ε>0, ∃y ∈ s, edist x y < ε :=
(mem_closure_iff_nhds_basis nhds_basis_eball).trans $
by simp only [mem_ball, edist_comm x]
theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} :
tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, edist (u x) a < ε :=
nhds_basis_eball.tendsto_right_iff
theorem tendsto_at_top [nonempty β] [semilattice_sup β] {u : β → α} {a : α} :
tendsto u at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, edist (u n) a < ε :=
(at_top_basis.tendsto_iff nhds_basis_eball).trans $
by simp only [exists_prop, true_and, mem_Ici, mem_ball]
/-- In a pseudoemetric space, Cauchy sequences are characterized by the fact that, eventually,
the pseudoedistance between its elements is arbitrarily small -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem cauchy_seq_iff [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, edist (u m) (u n) < ε :=
uniformity_basis_edist.cauchy_seq_iff
/-- A variation around the emetric characterization of Cauchy sequences -/
theorem cauchy_seq_iff' [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ ∀ε>(0 : ℝ≥0∞), ∃N, ∀n≥N, edist (u n) (u N) < ε :=
uniformity_basis_edist.cauchy_seq_iff'
/-- A variation of the emetric characterization of Cauchy sequences that deals with
`ℝ≥0` upper bounds. -/
theorem cauchy_seq_iff_nnreal [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ ∀ ε : ℝ≥0, 0 < ε → ∃ N, ∀ n, N ≤ n → edist (u n) (u N) < ε :=
uniformity_basis_edist_nnreal.cauchy_seq_iff'
theorem totally_bounded_iff {s : set α} :
totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε :=
⟨λ H ε ε0, H _ (edist_mem_uniformity ε0),
λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru,
⟨t, ft, h⟩ := H ε ε0 in
⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩
theorem totally_bounded_iff' {s : set α} :
totally_bounded s ↔ ∀ ε > 0, ∃t⊆s, finite t ∧ s ⊆ ⋃y∈t, ball y ε :=
⟨λ H ε ε0, (totally_bounded_iff_subset.1 H) _ (edist_mem_uniformity ε0),
λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru,
⟨t, _, ft, h⟩ := H ε ε0 in
⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩
section first_countable
@[priority 100] -- see Note [lower instance priority]
instance (α : Type u) [pseudo_emetric_space α] :
topological_space.first_countable_topology α :=
uniform_space.first_countable_topology uniformity_has_countable_basis
end first_countable
section compact
/-- For a set `s` in a pseudo emetric space, if for every `ε > 0` there exists a countable
set that is `ε`-dense in `s`, then there exists a countable subset `t ⊆ s` that is dense in `s`. -/
lemma subset_countable_closure_of_almost_dense_set (s : set α)
(hs : ∀ ε > 0, ∃ t : set α, countable t ∧ s ⊆ ⋃ x ∈ t, closed_ball x ε) :
∃ t ⊆ s, (countable t ∧ s ⊆ closure t) :=
begin
rcases s.eq_empty_or_nonempty with rfl|⟨x₀, hx₀⟩,
{ exact ⟨∅, empty_subset _, countable_empty, empty_subset _⟩ },
choose! T hTc hsT using (λ n : ℕ, hs n⁻¹ (by simp)),
have : ∀ r x, ∃ y ∈ s, closed_ball x r ∩ s ⊆ closed_ball y (r * 2),
{ intros r x,
rcases (closed_ball x r ∩ s).eq_empty_or_nonempty with he|⟨y, hxy, hys⟩,
{ refine ⟨x₀, hx₀, _⟩, rw he, exact empty_subset _ },
{ refine ⟨y, hys, λ z hz, _⟩,
calc edist z y ≤ edist z x + edist y x : edist_triangle_right _ _ _
... ≤ r + r : add_le_add hz.1 hxy
... = r * 2 : (mul_two r).symm } },
choose f hfs hf,
refine ⟨⋃ n : ℕ, (f n⁻¹) '' (T n), Union_subset $ λ n, image_subset_iff.2 (λ z hz, hfs _ _),
countable_Union $ λ n, (hTc n).image _, _⟩,
refine λ x hx, mem_closure_iff.2 (λ ε ε0, _),
rcases ennreal.exists_inv_nat_lt (ennreal.half_pos ε0).ne' with ⟨n, hn⟩,
rcases mem_bUnion_iff.1 (hsT n hx) with ⟨y, hyn, hyx⟩,
refine ⟨f n⁻¹ y, mem_Union.2 ⟨n, mem_image_of_mem _ hyn⟩, _⟩,
calc edist x (f n⁻¹ y) ≤ n⁻¹ * 2 : hf _ _ ⟨hyx, hx⟩
... < ε : ennreal.mul_lt_of_lt_div hn
end
/-- A compact set in a pseudo emetric space is separable, i.e., it is a subset of the closure of a
countable set. -/
lemma subset_countable_closure_of_compact {s : set α} (hs : is_compact s) :
∃ t ⊆ s, (countable t ∧ s ⊆ closure t) :=
begin
refine subset_countable_closure_of_almost_dense_set s (λ ε hε, _),
rcases totally_bounded_iff'.1 hs.totally_bounded ε hε with ⟨t, hts, htf, hst⟩,
exact ⟨t, htf.countable,
subset.trans hst (bUnion_subset_bUnion_right $ λ _ _, ball_subset_closed_ball)⟩
end
end compact
section second_countable
open topological_space
variables (α)
/-- A separable pseudoemetric space is second countable: one obtains a countable basis by taking
the balls centered at points in a dense subset, and with rational radii. We do not register
this as an instance, as there is already an instance going in the other direction
from second countable spaces to separable spaces, and we want to avoid loops. -/
lemma second_countable_of_separable [separable_space α] :
second_countable_topology α :=
uniform_space.second_countable_of_separable uniformity_has_countable_basis
/-- A sigma compact pseudo emetric space has second countable topology. This is not an instance
to avoid a loop with `sigma_compact_space_of_locally_compact_second_countable`. -/
lemma second_countable_of_sigma_compact [sigma_compact_space α] :
second_countable_topology α :=
begin
suffices : separable_space α, by exactI second_countable_of_separable α,
choose T hTsub hTc hsubT
using λ n, subset_countable_closure_of_compact (is_compact_compact_covering α n),
refine ⟨⟨⋃ n, T n, countable_Union hTc, λ x, _⟩⟩,
rcases Union_eq_univ_iff.1 (Union_compact_covering α) x with ⟨n, hn⟩,
exact closure_mono (subset_Union _ n) (hsubT _ hn)
end
variable {α}
lemma second_countable_of_almost_dense_set
(hs : ∀ ε > 0, ∃ t : set α, countable t ∧ (⋃ x ∈ t, closed_ball x ε) = univ) :
second_countable_topology α :=
begin
suffices : separable_space α, by exactI second_countable_of_separable α,
rcases subset_countable_closure_of_almost_dense_set (univ : set α) (λ ε ε0, _)
with ⟨t, -, htc, ht⟩,
{ exact ⟨⟨t, htc, λ x, ht (mem_univ x)⟩⟩ },
{ rcases hs ε ε0 with ⟨t, htc, ht⟩,
exact ⟨t, htc, univ_subset_iff.2 ht⟩ }
end
end second_countable
section diam
/-- The diameter of a set in a pseudoemetric space, named `emetric.diam` -/
def diam (s : set α) := ⨆ (x ∈ s) (y ∈ s), edist x y
lemma diam_le_iff {d : ℝ≥0∞} :
diam s ≤ d ↔ ∀ (x ∈ s) (y ∈ s), edist x y ≤ d :=
by simp only [diam, supr_le_iff]
lemma diam_image_le_iff {d : ℝ≥0∞} {f : β → α} {s : set β} :
diam (f '' s) ≤ d ↔ ∀ (x ∈ s) (y ∈ s), edist (f x) (f y) ≤ d :=
by simp only [diam_le_iff, ball_image_iff]
lemma edist_le_of_diam_le {d} (hx : x ∈ s) (hy : y ∈ s) (hd : diam s ≤ d) : edist x y ≤ d :=
diam_le_iff.1 hd x hx y hy
/-- If two points belong to some set, their edistance is bounded by the diameter of the set -/
lemma edist_le_diam_of_mem (hx : x ∈ s) (hy : y ∈ s) : edist x y ≤ diam s :=
edist_le_of_diam_le hx hy le_rfl
/-- If the distance between any two points in a set is bounded by some constant, this constant
bounds the diameter. -/
lemma diam_le {d : ℝ≥0∞} (h : ∀ (x ∈ s) (y ∈ s), edist x y ≤ d) : diam s ≤ d := diam_le_iff.2 h
/-- The diameter of a subsingleton vanishes. -/
lemma diam_subsingleton (hs : s.subsingleton) : diam s = 0 :=
nonpos_iff_eq_zero.1 $ diam_le $
λ x hx y hy, (hs hx hy).symm ▸ edist_self y ▸ le_rfl
/-- The diameter of the empty set vanishes -/
@[simp] lemma diam_empty : diam (∅ : set α) = 0 :=
diam_subsingleton subsingleton_empty
/-- The diameter of a singleton vanishes -/
@[simp] lemma diam_singleton : diam ({x} : set α) = 0 :=
diam_subsingleton subsingleton_singleton
lemma diam_Union_mem_option {ι : Type*} (o : option ι) (s : ι → set α) :
diam (⋃ i ∈ o, s i) = ⨆ i ∈ o, diam (s i) :=
by cases o; simp
lemma diam_insert : diam (insert x s) = max (⨆ y ∈ s, edist x y) (diam s) :=
eq_of_forall_ge_iff $ λ d, by simp only [diam_le_iff, ball_insert_iff,
edist_self, edist_comm x, max_le_iff, supr_le_iff, zero_le, true_and,
forall_and_distrib, and_self, ← and_assoc]
lemma diam_pair : diam ({x, y} : set α) = edist x y :=
by simp only [supr_singleton, diam_insert, diam_singleton, ennreal.max_zero_right]
lemma diam_triple :
diam ({x, y, z} : set α) = max (max (edist x y) (edist x z)) (edist y z) :=
by simp only [diam_insert, supr_insert, supr_singleton, diam_singleton,
ennreal.max_zero_right, ennreal.sup_eq_max]
/-- The diameter is monotonous with respect to inclusion -/
lemma diam_mono {s t : set α} (h : s ⊆ t) : diam s ≤ diam t :=
diam_le $ λ x hx y hy, edist_le_diam_of_mem (h hx) (h hy)
/-- The diameter of a union is controlled by the diameter of the sets, and the edistance
between two points in the sets. -/
lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) :
diam (s ∪ t) ≤ diam s + edist x y + diam t :=
begin
have A : ∀a ∈ s, ∀b ∈ t, edist a b ≤ diam s + edist x y + diam t := λa ha b hb, calc
edist a b ≤ edist a x + edist x y + edist y b : edist_triangle4 _ _ _ _
... ≤ diam s + edist x y + diam t :
add_le_add (add_le_add (edist_le_diam_of_mem ha xs) (le_refl _)) (edist_le_diam_of_mem yt hb),
refine diam_le (λa ha b hb, _),
cases (mem_union _ _ _).1 ha with h'a h'a; cases (mem_union _ _ _).1 hb with h'b h'b,
{ calc edist a b ≤ diam s : edist_le_diam_of_mem h'a h'b
... ≤ diam s + (edist x y + diam t) : le_self_add
... = diam s + edist x y + diam t : (add_assoc _ _ _).symm },
{ exact A a h'a b h'b },
{ have Z := A b h'b a h'a, rwa [edist_comm] at Z },
{ calc edist a b ≤ diam t : edist_le_diam_of_mem h'a h'b
... ≤ (diam s + edist x y) + diam t : le_add_self }
end
lemma diam_union' {t : set α} (h : (s ∩ t).nonempty) : diam (s ∪ t) ≤ diam s + diam t :=
let ⟨x, ⟨xs, xt⟩⟩ := h in by simpa using diam_union xs xt
lemma diam_closed_ball {r : ℝ≥0∞} : diam (closed_ball x r) ≤ 2 * r :=
diam_le $ λa ha b hb, calc
edist a b ≤ edist a x + edist b x : edist_triangle_right _ _ _
... ≤ r + r : add_le_add ha hb
... = 2 * r : (two_mul r).symm
lemma diam_ball {r : ℝ≥0∞} : diam (ball x r) ≤ 2 * r :=
le_trans (diam_mono ball_subset_closed_ball) diam_closed_ball
lemma diam_pi_le_of_le {π : β → Type*} [fintype β] [∀ b, pseudo_emetric_space (π b)]
{s : Π (b : β), set (π b)} {c : ℝ≥0∞} (h : ∀ b, diam (s b) ≤ c) :
diam (set.pi univ s) ≤ c :=
begin
apply diam_le (λ x hx y hy, edist_pi_le_iff.mpr _),
rw [mem_univ_pi] at hx hy,
exact λ b, diam_le_iff.1 (h b) (x b) (hx b) (y b) (hy b),
end
end diam
end emetric --namespace
/-- We now define `emetric_space`, extending `pseudo_emetric_space`. -/
class emetric_space (α : Type u) extends pseudo_emetric_space α : Type u :=
(eq_of_edist_eq_zero : ∀ {x y : α}, edist x y = 0 → x = y)
variables {γ : Type w} [emetric_space γ]
@[priority 100] -- see Note [lower instance priority]
instance emetric_space.to_uniform_space' : uniform_space γ :=
pseudo_emetric_space.to_uniform_space
export emetric_space (eq_of_edist_eq_zero)
/-- Characterize the equality of points by the vanishing of their extended distance -/
@[simp] theorem edist_eq_zero {x y : γ} : edist x y = 0 ↔ x = y :=
iff.intro eq_of_edist_eq_zero (assume : x = y, this ▸ edist_self _)
@[simp] theorem zero_eq_edist {x y : γ} : 0 = edist x y ↔ x = y :=
iff.intro (assume h, eq_of_edist_eq_zero (h.symm))
(assume : x = y, this ▸ (edist_self _).symm)
theorem edist_le_zero {x y : γ} : (edist x y ≤ 0) ↔ x = y :=
nonpos_iff_eq_zero.trans edist_eq_zero
@[simp] theorem edist_pos {x y : γ} : 0 < edist x y ↔ x ≠ y := by simp [← not_le]
/-- Two points coincide if their distance is `< ε` for all positive ε -/
theorem eq_of_forall_edist_le {x y : γ} (h : ∀ε > 0, edist x y ≤ ε) : x = y :=
eq_of_edist_eq_zero (eq_of_le_of_forall_le_of_dense bot_le h)
/-- A map between emetric spaces is a uniform embedding if and only if the edistance between `f x`
and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/
theorem uniform_embedding_iff' [emetric_space β] {f : γ → β} :
uniform_embedding f ↔
(∀ ε > 0, ∃ δ > 0, ∀ {a b : γ}, edist a b < δ → edist (f a) (f b) < ε) ∧
(∀ δ > 0, ∃ ε > 0, ∀ {a b : γ}, edist (f a) (f b) < ε → edist a b < δ) :=
begin
split,
{ assume h,
exact ⟨emetric.uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1,
(uniform_embedding_iff.1 h).2.2⟩ },
{ rintros ⟨h₁, h₂⟩,
refine uniform_embedding_iff.2 ⟨_, emetric.uniform_continuous_iff.2 h₁, h₂⟩,
assume x y hxy,
have : edist x y ≤ 0,
{ refine le_of_forall_lt' (λδ δpos, _),
rcases h₂ δ δpos with ⟨ε, εpos, hε⟩,
have : edist (f x) (f y) < ε, by simpa [hxy],
exact hε this },
simpa using this }
end
/-- An emetric space is separated -/
@[priority 100] -- see Note [lower instance priority]
instance to_separated : separated_space γ :=
separated_def.2 $ λ x y h, eq_of_forall_edist_le $
λ ε ε0, le_of_lt (h _ (edist_mem_uniformity ε0))
/-- If a `pseudo_emetric_space` is separated, then it is an `emetric_space`. -/
def emetric_of_t2_pseudo_emetric_space {α : Type*} [pseudo_emetric_space α]
(h : separated_space α) : emetric_space α :=
{ eq_of_edist_eq_zero := λ x y hdist,
begin
refine separated_def.1 h x y (λ s hs, _),
obtain ⟨ε, hε, H⟩ := mem_uniformity_edist.1 hs,
exact H (show edist x y < ε, by rwa [hdist])
end
..‹pseudo_emetric_space α› }
/-- Auxiliary function to replace the uniformity on an emetric space with
a uniformity which is equal to the original one, but maybe not defeq.
This is useful if one wants to construct an emetric space with a
specified uniformity. See Note [forgetful inheritance] explaining why having definitionally
the right uniformity is often important.
-/
def emetric_space.replace_uniformity {γ} [U : uniform_space γ] (m : emetric_space γ)
(H : @uniformity _ U = @uniformity _ pseudo_emetric_space.to_uniform_space) :
emetric_space γ :=
{ edist := @edist _ m.to_has_edist,
edist_self := edist_self,
eq_of_edist_eq_zero := @eq_of_edist_eq_zero _ _,
edist_comm := edist_comm,
edist_triangle := edist_triangle,
to_uniform_space := U,
uniformity_edist := H.trans (@pseudo_emetric_space.uniformity_edist γ _) }
/-- The extended metric induced by an injective function taking values in a emetric space. -/
def emetric_space.induced {γ β} (f : γ → β) (hf : function.injective f)
(m : emetric_space β) : emetric_space γ :=
{ edist := λ x y, edist (f x) (f y),
edist_self := λ x, edist_self _,
eq_of_edist_eq_zero := λ x y h, hf (edist_eq_zero.1 h),
edist_comm := λ x y, edist_comm _ _,
edist_triangle := λ x y z, edist_triangle _ _ _,
to_uniform_space := uniform_space.comap f m.to_uniform_space,
uniformity_edist := begin
apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, edist (f x) (f y)),
refine λ s, mem_comap.trans _,
split; intro H,
{ rcases H with ⟨r, ru, rs⟩,
rcases mem_uniformity_edist.1 ru with ⟨ε, ε0, hε⟩,
refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h },
{ rcases H with ⟨ε, ε0, hε⟩,
exact ⟨_, edist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ }
end }
/-- Emetric space instance on subsets of emetric spaces -/
instance {α : Type*} {p : α → Prop} [t : emetric_space α] : emetric_space (subtype p) :=
t.induced coe (λ x y, subtype.ext_iff_val.2)
/-- The product of two emetric spaces, with the max distance, is an extended
metric spaces. We make sure that the uniform structure thus constructed is the one
corresponding to the product of uniform spaces, to avoid diamond problems. -/
instance prod.emetric_space_max [emetric_space β] : emetric_space (γ × β) :=
{ eq_of_edist_eq_zero := λ x y h, begin
cases max_le_iff.1 (le_of_eq h) with h₁ h₂,
have A : x.fst = y.fst := edist_le_zero.1 h₁,
have B : x.snd = y.snd := edist_le_zero.1 h₂,
exact prod.ext_iff.2 ⟨A, B⟩
end,
..prod.pseudo_emetric_space_max }
/-- Reformulation of the uniform structure in terms of the extended distance -/
theorem uniformity_edist :
𝓤 γ = ⨅ ε>0, 𝓟 {p:γ×γ | edist p.1 p.2 < ε} :=
pseudo_emetric_space.uniformity_edist
section pi
open finset
variables {π : β → Type*} [fintype β]
/-- The product of a finite number of emetric spaces, with the max distance, is still
an emetric space.
This construction would also work for infinite products, but it would not give rise
to the product topology. Hence, we only formalize it in the good situation of finitely many
spaces. -/
instance emetric_space_pi [∀b, emetric_space (π b)] : emetric_space (Πb, π b) :=
{ eq_of_edist_eq_zero := assume f g eq0,
begin
have eq1 : sup univ (λ (b : β), edist (f b) (g b)) ≤ 0 := le_of_eq eq0,
simp only [finset.sup_le_iff] at eq1,
exact (funext $ assume b, edist_le_zero.1 $ eq1 b $ mem_univ b),
end,
..pseudo_emetric_space_pi }
end pi
namespace emetric
/-- A compact set in an emetric space is separable, i.e., it is the closure of a countable set. -/
lemma countable_closure_of_compact {s : set γ} (hs : is_compact s) :
∃ t ⊆ s, (countable t ∧ s = closure t) :=
begin
rcases subset_countable_closure_of_compact hs with ⟨t, hts, htc, hsub⟩,
exact ⟨t, hts, htc, subset.antisymm hsub (closure_minimal hts hs.is_closed)⟩
end
section diam
variables {s : set γ}
lemma diam_eq_zero_iff : diam s = 0 ↔ s.subsingleton :=
⟨λ h x hx y hy, edist_le_zero.1 $ h ▸ edist_le_diam_of_mem hx hy, diam_subsingleton⟩
lemma diam_pos_iff : 0 < diam s ↔ ∃ (x ∈ s) (y ∈ s), x ≠ y :=
begin
have := not_congr (@diam_eq_zero_iff _ _ s),
dunfold set.subsingleton at this,
push_neg at this,
simpa only [pos_iff_ne_zero, exists_prop] using this
end
end diam
end emetric
|
6804c9888b70acdb461f7d4a723e35b733f64a66 | 71c05f289da9a856f1d16197a32b6a874eb1052e | /hlean/int_order.hlean | 9f61edb2ce4d232df0d1037347cb52ddcf3b6e20 | [] | no_license | gbaz/works-in-progress | e7701b1c2f8d0cfb6bfb8d84fc4d21ffd9937521 | af7bacd7f68649106a3581c232548958aca79a08 | refs/heads/master | 1,661,485,946,844 | 1,659,018,408,000 | 1,659,018,408,000 | 32,045,948 | 14 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 16,403 | hlean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Jeremy Avigad
The order relation on the integers. We show that int is an instance of linear_comm_ordered_ring
and transfer the results.
-/
import types.int.basic algebra.ordered_ring types algebra.group
open nat
open decidable
open eq.ops sum algebra prod eq
open int
namespace int
-- Things to Add to Nat
theorem of_nat_add (n m : nat) : of_nat (n + m) = of_nat n + of_nat m := rfl
theorem of_nat_mul (n m : ℕ) : of_nat (n * m) = of_nat n * of_nat m := rfl
--Things to Add to Int
protected definition prio : num := num.pred std.priority.default
-- Main
private definition nonneg (a : ℤ) : Type.{0} := int.cases_on a (take n, unit) (take n, empty)
definition le (a b : ℤ) : Type.{0} := nonneg (b - a)
definition lt (a b : ℤ) : Type.{0} := le (int.add a (of_nat (nat.succ nat.zero))) b
infix [priority int.prio] - := int.sub
infix [priority int.prio] <= := int.le
infix [priority int.prio] ≤ := int.le
infix [priority int.prio] < := int.lt
local attribute nonneg [reducible]
private definition decidable_nonneg [instance] (a : ℤ) : decidable (nonneg a) := int.cases_on a _ _
definition decidable_le [instance] (a b : ℤ) : decidable (a ≤ b) := decidable_nonneg _
definition decidable_lt [instance] (a b : ℤ) : decidable (a < b) := decidable_nonneg _
private theorem nonneg.elim {a : ℤ} : nonneg a → Σn : ℕ, a = n :=
int.cases_on a (take n H, sigma.mk n rfl) (take n', empty.elim)
private theorem nonneg_or_nonneg_neg (a : ℤ) : nonneg a ⊎ nonneg (-a) :=
int.cases_on a (take n, sum.inl unit.star) (take n, sum.inr unit.star)
theorem le.intro {a b : ℤ} {n : ℕ} (H : a + n = b) : a ≤ b :=
have n = b - a, from eq_add_neg_of_add_eq (!add.comm ▸ H),
show nonneg (b - a), from this ▸ unit.star
theorem le.elim {a b : ℤ} (H : a ≤ b) : Σn : ℕ, int.add a n = b := --TODO note this is terrible
obtain (n : ℕ) (H1 : b - a = n), from nonneg.elim H,
sigma.mk n (!add.comm ▸ add_eq_of_eq_add_neg (H1⁻¹))
theorem le.total (a b : ℤ) : a ≤ b ⊎ b ≤ a :=
begin
cases (nonneg_or_nonneg_neg (b - a)),
apply sum.inl,
exact a_1,
apply sum.inr,
exact (transport nonneg (neg_sub b a) a_1)
end
theorem of_nat_le_of_nat_of_le {m n : ℕ} (H : #nat m ≤ n) : of_nat m ≤ of_nat n :=
obtain (k : ℕ) (Hk : m + k = n), from nat.le.elim H,
le.intro (Hk ▸ (of_nat_add m k)⁻¹)
theorem le_of_of_nat_le_of_nat {m n : ℕ} (H : of_nat m ≤ of_nat n) : (#nat m ≤ n) :=
obtain (k : ℕ) (Hk : of_nat m + of_nat k = of_nat n), from le.elim H,
have m + k = n, from of_nat.inj (of_nat_add m k ⬝ Hk),
nat.le.intro this
theorem of_nat_le_of_nat (m n : ℕ) : of_nat m ≤ of_nat n ↔ m ≤ n :=
iff.intro le_of_of_nat_le_of_nat of_nat_le_of_nat_of_le
theorem lt_add_succ (a : ℤ) (n : ℕ) : a < a + succ n :=
le.intro (show a + 1 + n = a + succ n, from
calc
a + 1 + n = a + (1 + n) : add.assoc
... = a + (n + 1) : nat.add.comm
... = a + succ n : rfl)
theorem lt.intro {a b : ℤ} {n : ℕ} (H : a + succ n = b) : a < b :=
H ▸ lt_add_succ a n
set_option pp.all true
--protected definition int_has_one [instance] [reducible] : has_one ℤ := has_one.mk (of_nat (nat.succ nat.zero))
theorem lt.elim {a b : ℤ} (H : a < b) : Σn : ℕ, int.add a (succ n) = b :=
begin
cases (le.elim H),
esimp [succ],
fapply sigma.mk,
assumption,
rewrite [add.comm a_1 (of_nat (nat.succ nat.zero)), (add.assoc a (of_nat (nat.succ nat.zero)) a_1)⁻¹],
assumption
end
theorem of_nat_lt_of_nat (n m : ℕ) : of_nat n < of_nat m ↔ n < m :=
calc
of_nat n < of_nat m ↔ of_nat n + 1 ≤ of_nat m : iff.refl
... ↔ of_nat (nat.succ n) ≤ of_nat m : of_nat_succ n ▸ !iff.refl
... ↔ nat.succ n ≤ m : of_nat_le_of_nat
... ↔ n < m : iff.symm (lt_iff_succ_le _ _)
theorem lt_of_of_nat_lt_of_nat {m n : ℕ} (H : of_nat m < of_nat n) : #nat m < n := iff.mp !of_nat_lt_of_nat H
theorem of_nat_lt_of_nat_of_lt {m n : ℕ} (H : #nat m < n) : of_nat m < of_nat n := iff.mp' !of_nat_lt_of_nat H
/- show that the integers form an ordered additive group -/
theorem le.refl (a : ℤ) : a ≤ a :=
le.intro (add_zero a)
theorem le.trans {a b c : ℤ} (H1 : a ≤ b) (H2 : b ≤ c) : a ≤ c :=
obtain (n : ℕ) (Hn : a + n = b), from le.elim H1,
obtain (m : ℕ) (Hm : b + m = c), from le.elim H2,
have a + of_nat (n + m) = c, from
calc
a + of_nat (n + m) = a + (of_nat n + m) : {of_nat_add n m}
... = a + n + m : (add.assoc a n m)⁻¹
... = b + m : {Hn}
... = c : Hm,
le.intro this
theorem le.antisymm : Π {a b : ℤ}, a ≤ b → b ≤ a → a = b :=
take a b : ℤ, assume (H₁ : a ≤ b) (H₂ : b ≤ a),
obtain (n : ℕ) (Hn : a + n = b), from le.elim H₁,
obtain (m : ℕ) (Hm : b + m = a), from le.elim H₂,
have a + of_nat (n + m) = a + 0, from
calc
a + of_nat (n + m) = a + (of_nat n + m) : of_nat_add
... = a + n + m : add.assoc
... = b + m : Hn
... = a : Hm
... = a + 0 : add_zero,
have of_nat (n + m) = of_nat 0, from add.left_cancel this,
have n + m = 0, from of_nat.inj this,
have n = 0, from nat.eq_zero_of_add_eq_zero_right this,
show a = b, from
calc
a = a + 0 : add_zero
... = a + n : this
... = b : Hn
theorem lt.irrefl (a : ℤ) : ¬ a < a :=
(suppose a < a,
obtain (n : ℕ) (Hn : a + succ n = a), from lt.elim this,
have a + succ n = a + 0, from
Hn ⬝ !add_zero⁻¹,
!succ_ne_zero (of_nat.inj (add.left_cancel this)))
theorem ne_of_lt {a b : ℤ} (H : a < b) : a ≠ b :=
(suppose a = b, absurd (this ▸ H) (lt.irrefl b))
theorem le_of_lt {a b : ℤ} (H : a < b) : a ≤ b :=
obtain (n : ℕ) (Hn : a + succ n = b), from lt.elim H,
le.intro Hn
theorem lt_iff_le_and_ne (a b : ℤ) : a < b ↔ (a ≤ b × a ≠ b) :=
iff.intro
(assume H, pair (le_of_lt H) (ne_of_lt H))
(assume H,
have a ≤ b, from prod.pr1 H,
have a ≠ b, from prod.pr2 H,
obtain (n : ℕ) (Hn : a + n = b), from le.elim `a ≤ b`,
have n ≠ 0, from (assume H' : n = 0, `a ≠ b` (!add_zero ▸ H' ▸ Hn)),
obtain (k : ℕ) (Hk : n = nat.succ k), from nat.exists_eq_succ_of_ne_zero this,
lt.intro (Hk ▸ Hn))
theorem le_iff_lt_or_eq (a b : ℤ) : a ≤ b ↔ (a < b ⊎ a = b) :=
iff.intro
(assume H,
by_cases
(suppose a = b, sum.inr this)
(suppose a ≠ b,
obtain (n : ℕ) (Hn : a + n = b), from le.elim H,
have n ≠ 0, from (assume H' : n = 0, `a ≠ b` (!add_zero ▸ H' ▸ Hn)),
obtain (k : ℕ) (Hk : n = nat.succ k), from nat.exists_eq_succ_of_ne_zero this,
sum.inl (lt.intro (Hk ▸ Hn))))
(assume H,
sum.rec_on H
(assume H1, le_of_lt H1)
(assume H1, H1 ▸ !le.refl))
theorem lt_succ (a : ℤ) : a < a + 1 :=
le.refl (a + 1)
theorem add_le_add_left {a b : ℤ} (H : a ≤ b) (c : ℤ) : c + a ≤ c + b :=
obtain (n : ℕ) (Hn : a + n = b), from le.elim H,
have H2 : c + a + n = c + b, from
calc
c + a + n = c + (a + n) : add.assoc c a n
... = c + b : {Hn},
le.intro H2
theorem add_lt_add_left {a b : ℤ} (H : a < b) (c : ℤ) : c + a < c + b :=
let H' := le_of_lt H in
(iff.mp' (lt_iff_le_and_ne _ _)) (pair (add_le_add_left H' _)
(take Heq, let Heq' := add_left_cancel Heq in
!lt.irrefl (Heq' ▸ H)))
theorem mul_nonneg {a b : ℤ} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a * b :=
obtain (n : ℕ) (Hn : 0 + n = a), from le.elim Ha,
obtain (m : ℕ) (Hm : 0 + m = b), from le.elim Hb,
le.intro
(inverse
(calc
a * b = (0 + n) * b : Hn
... = n * b : nat.zero_add
... = n * (0 + m) : {Hm⁻¹}
... = n * m : nat.zero_add
... = 0 + n * m : zero_add))
theorem mul_pos {a b : ℤ} (Ha : 0 < a) (Hb : 0 < b) : 0 < a * b :=
obtain (n : ℕ) (Hn : 0 + nat.succ n = a), from lt.elim Ha,
obtain (m : ℕ) (Hm : 0 + nat.succ m = b), from lt.elim Hb,
lt.intro
(inverse
(calc
a * b = (0 + nat.succ n) * b : Hn
... = nat.succ n * b : nat.zero_add
... = nat.succ n * (0 + nat.succ m) : {Hm⁻¹}
... = nat.succ n * nat.succ m : nat.zero_add
... = of_nat (nat.succ n * nat.succ m) : of_nat_mul
... = of_nat (nat.succ n * m + nat.succ n) : nat.mul_succ
... = of_nat (nat.succ (nat.succ n * m + n)) : nat.add_succ
... = 0 + nat.succ (nat.succ n * m + n) : zero_add))
theorem zero_lt_one : (0 : ℤ) < 1 := unit.star
theorem not_le_of_gt {a b : ℤ} (H : a < b) : ¬ b ≤ a :=
assume Hba,
let Heq := le.antisymm (le_of_lt H) Hba in
!lt.irrefl (Heq ▸ H)
theorem lt_of_lt_of_le {a b c : ℤ} (Hab : a < b) (Hbc : b ≤ c) : a < c :=
let Hab' := le_of_lt Hab in
let Hac := le.trans Hab' Hbc in
(iff.mp' !lt_iff_le_and_ne) (pair Hac
(assume Heq, not_le_of_gt (Heq ▸ Hab) Hbc))
theorem lt_of_le_of_lt {a b c : ℤ} (Hab : a ≤ b) (Hbc : b < c) : a < c :=
let Hbc' := le_of_lt Hbc in
let Hac := le.trans Hab Hbc' in
(iff.mp' !lt_iff_le_and_ne) (pair Hac
(assume Heq, not_le_of_gt (Heq⁻¹ ▸ Hbc) Hab))
section migrate_algebra
open [classes] algebra
--print fields linear_ordered_comm_ring
protected definition linear_ordered_comm_ring [reducible] :
algebra.linear_ordered_comm_ring int :=
⦃algebra.linear_ordered_comm_ring, int.integral_domain,
le := le,
le_refl := le.refl,
le_trans := @le.trans,
le_antisymm := @le.antisymm,
lt := lt,
-- le_of_lt := @le_of_lt,
-- lt_irrefl := lt.irrefl,
-- lt_of_lt_of_le := @lt_of_lt_of_le,
-- lt_of_le_of_lt := @lt_of_le_of_lt,
add_le_add_left := @add_le_add_left,
mul_nonneg := @mul_nonneg,
mul_pos := @mul_pos,
lt_iff_le_and_ne := lt_iff_le_and_ne,
le_iff_lt_or_eq := le_iff_lt_or_eq,
le_total := le.total,
zero_ne_one := zero_ne_one
-- zero_lt_one := zero_lt_one,
-- add_lt_add_left := @add_lt_add_left
⦄
protected definition decidable_linear_ordered_comm_ring [reducible] :
algebra.decidable_linear_ordered_comm_ring int :=
⦃algebra.decidable_linear_ordered_comm_ring,
int.linear_ordered_comm_ring,
decidable_lt := decidable_lt⦄
local attribute int.integral_domain [instance]
local attribute int.linear_ordered_comm_ring [instance]
local attribute int.decidable_linear_ordered_comm_ring [instance]
definition ge [reducible] (a b : ℤ) := algebra.has_le.ge a b
definition gt [reducible] (a b : ℤ) := algebra.has_lt.gt a b
infix >= := int.ge
infix ≥ := int.ge
infix > := int.gt
definition decidable_ge [instance] (a b : ℤ) : decidable (a ≥ b) :=
show decidable (b ≤ a), from _
definition decidable_gt [instance] (a b : ℤ) : decidable (a > b) :=
show decidable (b < a), from _
-- definition min : ℤ → ℤ → ℤ := algebra.min
-- definition max : ℤ → ℤ → ℤ := algebra.max
definition abs : ℤ → ℤ := algebra.abs
definition sign : ℤ → ℤ := algebra.sign
migrate from algebra with int
replacing has_le.ge → ge, has_lt.gt → gt, dvd → dvd, sub → sub, -- min → min, max → max,
abs → abs, sign → sign
attribute le.trans ge.trans lt.trans gt.trans [trans]
attribute lt_of_lt_of_le lt_of_le_of_lt gt_of_gt_of_ge gt_of_ge_of_gt [trans]
end migrate_algebra
/- more facts specific to int -/
theorem of_nat_nonneg (n : ℕ) : 0 ≤ of_nat n := unit.star
theorem of_nat_pos {n : ℕ} (Hpos : #nat n > 0) : of_nat n > 0 :=
of_nat_lt_of_nat_of_lt Hpos
theorem of_nat_succ_pos (n : nat) : of_nat (nat.succ n) > 0 :=
of_nat_pos !nat.succ_pos
theorem exists_eq_of_nat {a : ℤ} (H : 0 ≤ a) : Σn : ℕ, a = of_nat n :=
obtain (n : ℕ) (H1 : 0 + of_nat n = a), from le.elim H,
sigma.mk n (!zero_add ▸ (H1⁻¹))
theorem exists_eq_neg_of_nat {a : ℤ} (H : a ≤ 0) : Σn : ℕ, a = -(of_nat n) :=
have -a ≥ 0, from iff.mp' !neg_nonneg_iff_nonpos H,
obtain (n : ℕ) (Hn : -a = of_nat n), from exists_eq_of_nat this,
sigma.mk n (eq_neg_of_eq_neg (Hn⁻¹))
theorem of_nat_nat_abs_of_nonneg {a : ℤ} (H : a ≥ 0) : of_nat (nat_abs a) = a :=
obtain (n : ℕ) (Hn : a = of_nat n), from exists_eq_of_nat H,
Hn⁻¹ ▸ ap of_nat (nat_abs_of_nat n)
theorem of_nat_nat_abs_of_nonpos {a : ℤ} (H : a ≤ 0) : of_nat (nat_abs a) = -a :=
have -a ≥ 0, from iff.mp' !neg_nonneg_iff_nonpos H,
calc
of_nat (nat_abs a) = of_nat (nat_abs (-a)) : nat_abs_neg
... = -a : of_nat_nat_abs_of_nonneg this
theorem of_nat_nat_abs (b : ℤ) : nat_abs b = abs b :=
sum.rec_on (le.total 0 b)
(assume H : b ≥ 0, of_nat_nat_abs_of_nonneg H ⬝ (abs_of_nonneg H)⁻¹)
(assume H : b ≤ 0, of_nat_nat_abs_of_nonpos H ⬝ (abs_of_nonpos H)⁻¹)
theorem nat_abs_abs (a : ℤ) : nat_abs (abs a) = nat_abs a :=
abs.by_cases rfl !nat_abs_neg
theorem lt_of_add_one_le {a b : ℤ} (H : int.add a (of_nat (nat.succ nat.zero)) ≤ b) : a < b :=
obtain n (H1 : int.add (int.add a (of_nat (nat.succ nat.zero))) n = b), from le.elim H,
have a + succ n = b, by rewrite [-H1, add.assoc, add.comm (of_nat (nat.succ nat.zero))],
lt.intro this
theorem add_one_le_of_lt {a b : ℤ} (H : a < b) : int.add a (of_nat (nat.succ nat.zero)) ≤ b :=
obtain n (H1 : int.add a (succ n) = b), from lt.elim H,
have int.add (int.add a 1) n = b, by rewrite [-H1, add.assoc, add.comm 1],
le.intro this
theorem lt_add_one_of_le {a b : ℤ} (H : a ≤ b) : a < b + 1 :=
lt_add_of_le_of_pos H unit.star
theorem le_of_lt_add_one {a b : ℤ} (H : a < b + 1) : a ≤ b :=
have H1 : a + 1 ≤ b + 1, from add_one_le_of_lt H,
le_of_add_le_add_right H1
theorem sub_one_le_of_lt {a b : ℤ} (H : a ≤ b) : a - 1 < b :=
lt_of_add_one_le (!sub_add_cancel⁻¹ ▸ H)
theorem lt_of_sub_one_le {a b : ℤ} (H : a - 1 < b) : a ≤ b :=
!sub_add_cancel ▸ add_one_le_of_lt H
theorem le_sub_one_of_lt {a b : ℤ} (H : a < b) : a ≤ b - 1 :=
le_of_lt_add_one (!sub_add_cancel⁻¹ ▸ H)
theorem lt_of_le_sub_one {a b : ℤ} (H : a ≤ b - 1) : a < b :=
!sub_add_cancel ▸ (lt_add_one_of_le H)
theorem sign_of_succ (n : nat) : sign (nat.succ n) = 1 :=
sign_of_pos (of_nat_pos !nat.succ_pos)
theorem exists_eq_neg_succ_of_nat {a : ℤ} : a < 0 → Σm : ℕ, a = int.neg_succ_of_nat m :=
int.cases_on a
(take m H, absurd (of_nat_nonneg m : 0 ≤ m) (not_le_of_gt H))
(take m H, sigma.mk m rfl)
theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : a ≥ 0) (H' : a * b = 1) : a = 1 :=
have a * b > 0, by rewrite H'; apply unit.star,
have b > 0, from pos_of_mul_pos_left this H,
have a > 0, from pos_of_mul_pos_right `a * b > 0` (le_of_lt `b > 0`),
sum.rec_on (le_or_gt a 1)
(suppose a ≤ 1,
show a = 1, from le.antisymm this (add_one_le_of_lt `a > 0`))
(suppose a > 1,
assert a * b ≥ 2 * 1,
from mul_le_mul (add_one_le_of_lt `a > 1`) (add_one_le_of_lt `b > 0`) unit.star H,
have empty, by rewrite [H' at this]; exact this,
empty.elim this)
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 (!mul.comm ▸ H')
theorem eq_one_of_mul_eq_self_left {a b : ℤ} (Hpos : a ≠ 0) (H : b * a = a) : b = 1 :=
eq_of_mul_eq_mul_right Hpos (H ⬝ (one_mul a)⁻¹)
theorem eq_one_of_mul_eq_self_right {a b : ℤ} (Hpos : b ≠ 0) (H : b * a = b) : a = 1 :=
eq_one_of_mul_eq_self_left Hpos (!mul.comm ▸ H)
theorem eq_one_of_dvd_one {a : ℤ} (H : a ≥ 0) (H' : a ∣ 1) : a = 1 :=
dvd.elim H'
(take b,
suppose 1 = a * b,
eq_one_of_mul_eq_one_right H this⁻¹)
end int
|
ccfb33b5abd31fb79d079d4faa48b1c0848a2123 | 1dd482be3f611941db7801003235dc84147ec60a | /src/algebra/ordered_group.lean | d0b1cfc0c20d0c5976c75305e786590879018048 | [
"Apache-2.0"
] | permissive | sanderdahmen/mathlib | 479039302bd66434bb5672c2a4cecf8d69981458 | 8f0eae75cd2d8b7a083cf935666fcce4565df076 | refs/heads/master | 1,587,491,322,775 | 1,549,672,060,000 | 1,549,672,060,000 | 169,748,224 | 0 | 0 | Apache-2.0 | 1,549,636,694,000 | 1,549,636,694,000 | null | UTF-8 | Lean | false | false | 24,244 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Johannes Hölzl
Ordered monoids and groups.
-/
import algebra.group order.bounded_lattice tactic.basic
universe u
variable {α : Type u}
section old_structure_cmd
set_option old_structure_cmd true
/-- An ordered (additive) commutative monoid is a commutative monoid
with a partial order such that addition is an order embedding, i.e.
`a + b ≤ a + c ↔ b ≤ c`. These monoids are automatically cancellative. -/
class ordered_comm_monoid (α : Type*) extends add_comm_monoid α, partial_order α :=
(add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b)
(lt_of_add_lt_add_left : ∀ a b c : α, a + b < a + c → b < c)
/-- A canonically ordered monoid is an ordered commutative monoid
in which the ordering coincides with the divisibility relation,
which is to say, `a ≤ b` iff there exists `c` with `b = a + c`.
This is satisfied by the natural numbers, for example, but not
the integers or other ordered groups. -/
class canonically_ordered_monoid (α : Type*) extends ordered_comm_monoid α :=
(le_iff_exists_add : ∀a b:α, a ≤ b ↔ ∃c, b = a + c)
end old_structure_cmd
section ordered_comm_monoid
variables [ordered_comm_monoid α] {a b c d : α}
lemma add_le_add_left' (h : a ≤ b) : c + a ≤ c + b :=
ordered_comm_monoid.add_le_add_left a b h c
lemma add_le_add_right' (h : a ≤ b) : a + c ≤ b + c :=
add_comm c a ▸ add_comm c b ▸ add_le_add_left' h
lemma lt_of_add_lt_add_left' : a + b < a + c → b < c :=
ordered_comm_monoid.lt_of_add_lt_add_left a b c
lemma add_le_add' (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d :=
le_trans (add_le_add_right' h₁) (add_le_add_left' h₂)
lemma le_add_of_nonneg_right' (h : b ≥ 0) : a ≤ a + b :=
have a + b ≥ a + 0, from add_le_add_left' h,
by rwa add_zero at this
lemma le_add_of_nonneg_left' (h : b ≥ 0) : a ≤ b + a :=
have 0 + a ≤ b + a, from add_le_add_right' h,
by rwa zero_add at this
lemma lt_of_add_lt_add_right' (h : a + b < c + b) : a < c :=
lt_of_add_lt_add_left'
(show b + a < b + c, begin rw [add_comm b a, add_comm b c], assumption end)
-- here we start using properties of zero.
lemma le_add_of_nonneg_of_le' (ha : 0 ≤ a) (hbc : b ≤ c) : b ≤ a + c :=
zero_add b ▸ add_le_add' ha hbc
lemma le_add_of_le_of_nonneg' (hbc : b ≤ c) (ha : 0 ≤ a) : b ≤ c + a :=
add_zero b ▸ add_le_add' hbc ha
lemma add_nonneg' (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=
le_add_of_nonneg_of_le' ha hb
lemma add_pos_of_pos_of_nonneg' (ha : 0 < a) (hb : 0 ≤ b) : 0 < a + b :=
lt_of_lt_of_le ha $ le_add_of_nonneg_right' hb
lemma add_pos' (ha : 0 < a) (hb : 0 < b) : 0 < a + b :=
add_pos_of_pos_of_nonneg' ha $ le_of_lt hb
lemma add_pos_of_nonneg_of_pos' (ha : 0 ≤ a) (hb : 0 < b) : 0 < a + b :=
lt_of_lt_of_le hb $ le_add_of_nonneg_left' ha
lemma add_nonpos' (ha : a ≤ 0) (hb : b ≤ 0) : a + b ≤ 0 :=
zero_add (0:α) ▸ (add_le_add' ha hb)
lemma add_le_of_nonpos_of_le' (ha : a ≤ 0) (hbc : b ≤ c) : a + b ≤ c :=
zero_add c ▸ add_le_add' ha hbc
lemma add_le_of_le_of_nonpos' (hbc : b ≤ c) (ha : a ≤ 0) : b + a ≤ c :=
add_zero c ▸ add_le_add' hbc ha
lemma add_neg_of_neg_of_nonpos' (ha : a < 0) (hb : b ≤ 0) : a + b < 0 :=
lt_of_le_of_lt (add_le_of_le_of_nonpos' (le_refl _) hb) ha
lemma add_neg_of_nonpos_of_neg' (ha : a ≤ 0) (hb : b < 0) : a + b < 0 :=
lt_of_le_of_lt (add_le_of_nonpos_of_le' ha (le_refl _)) hb
lemma add_neg' (ha : a < 0) (hb : b < 0) : a + b < 0 :=
add_neg_of_nonpos_of_neg' (le_of_lt ha) hb
lemma lt_add_of_nonneg_of_lt' (ha : 0 ≤ a) (hbc : b < c) : b < a + c :=
lt_of_lt_of_le hbc $ le_add_of_nonneg_left' ha
lemma lt_add_of_lt_of_nonneg' (hbc : b < c) (ha : 0 ≤ a) : b < c + a :=
lt_of_lt_of_le hbc $ le_add_of_nonneg_right' ha
lemma lt_add_of_pos_of_lt' (ha : 0 < a) (hbc : b < c) : b < a + c :=
lt_add_of_nonneg_of_lt' (le_of_lt ha) hbc
lemma lt_add_of_lt_of_pos' (hbc : b < c) (ha : 0 < a) : b < c + a :=
lt_add_of_lt_of_nonneg' hbc (le_of_lt ha)
lemma add_lt_of_nonpos_of_lt' (ha : a ≤ 0) (hbc : b < c) : a + b < c :=
lt_of_le_of_lt (add_le_of_nonpos_of_le' ha (le_refl _)) hbc
lemma add_lt_of_lt_of_nonpos' (hbc : b < c) (ha : a ≤ 0) : b + a < c :=
lt_of_le_of_lt (add_le_of_le_of_nonpos' (le_refl _) ha) hbc
lemma add_lt_of_neg_of_lt' (ha : a < 0) (hbc : b < c) : a + b < c :=
add_lt_of_nonpos_of_lt' (le_of_lt ha) hbc
lemma add_lt_of_lt_of_neg' (hbc : b < c) (ha : a < 0) : b + a < c :=
add_lt_of_lt_of_nonpos' hbc (le_of_lt ha)
lemma add_eq_zero_iff' (ha : 0 ≤ a) (hb : 0 ≤ b) : a + b = 0 ↔ a = 0 ∧ b = 0 :=
iff.intro
(assume hab : a + b = 0,
have a ≤ 0, from hab ▸ le_add_of_le_of_nonneg' (le_refl _) hb,
have a = 0, from le_antisymm this ha,
have b ≤ 0, from hab ▸ le_add_of_nonneg_of_le' ha (le_refl _),
have b = 0, from le_antisymm this hb,
and.intro ‹a = 0› ‹b = 0›)
(assume ⟨ha', hb'⟩, by rw [ha', hb', add_zero])
lemma bit0_pos {a : α} (h : 0 < a) : 0 < bit0 a :=
add_pos' h h
end ordered_comm_monoid
namespace units
instance [monoid α] [preorder α] : preorder (units α) :=
preorder.lift (coe : units α → α)
@[simp] theorem coe_le_coe [monoid α] [preorder α] {a b : units α} :
(a : α) ≤ b ↔ a ≤ b := iff.rfl
@[simp] theorem coe_lt_coe [monoid α] [preorder α] {a b : units α} :
(a : α) < b ↔ a < b := iff.rfl
instance [monoid α] [partial_order α] : partial_order (units α) :=
partial_order.lift (coe : units α → α) (by ext)
instance [monoid α] [linear_order α] : linear_order (units α) :=
linear_order.lift (coe : units α → α) (by ext)
instance [monoid α] [decidable_linear_order α] : decidable_linear_order (units α) :=
decidable_linear_order.lift (coe : units α → α) (by ext)
theorem max_coe [monoid α] [decidable_linear_order α] {a b : units α} :
(↑(max a b) : α) = max a b :=
by by_cases a ≤ b; simp [max, h]
theorem min_coe [monoid α] [decidable_linear_order α] {a b : units α} :
(↑(min a b) : α) = min a b :=
by by_cases a ≤ b; simp [min, h]
end units
namespace with_zero
open lattice
instance [partial_order α] : partial_order (with_zero α) := with_bot.partial_order
instance [partial_order α] : order_bot (with_zero α) := with_bot.order_bot
instance [lattice α] : lattice (with_zero α) := with_bot.lattice
instance [linear_order α] : linear_order (with_zero α) := with_bot.linear_order
instance [decidable_linear_order α] :
decidable_linear_order (with_zero α) := with_bot.decidable_linear_order
def ordered_comm_monoid [ordered_comm_monoid α]
(zero_le : ∀ a : α, 0 ≤ a) : ordered_comm_monoid (with_zero α) :=
begin
suffices, refine {
add_le_add_left := this,
..with_zero.partial_order,
..with_zero.add_comm_monoid, .. },
{ intros a b c h,
have h' := lt_iff_le_not_le.1 h,
rw lt_iff_le_not_le at ⊢,
refine ⟨λ b h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩,
cases h₂, cases c with c,
{ cases h'.2 (this _ _ bot_le a) },
{ refine ⟨_, rfl, _⟩,
cases a with a,
{ exact with_bot.some_le_some.1 h'.1 },
{ exact le_of_lt (lt_of_add_lt_add_left' $
with_bot.some_lt_some.1 h), } } },
{ intros a b h c ca h₂,
cases b with b,
{ rw le_antisymm h bot_le at h₂,
exact ⟨_, h₂, le_refl _⟩ },
cases a with a,
{ change c + 0 = some ca at h₂,
simp at h₂, simp [h₂],
exact ⟨_, rfl, by simpa using add_le_add_left' (zero_le b)⟩ },
{ simp at h,
cases c with c; change some _ = _ at h₂;
simp [-add_comm] at h₂; subst ca; refine ⟨_, rfl, _⟩,
{ exact h },
{ exact add_le_add_left' h } } }
end
end with_zero
namespace with_top
open lattice
instance [add_semigroup α] : add_semigroup (with_top α) :=
{ add := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a + b)),
..@additive.add_semigroup _ $ @with_zero.semigroup (multiplicative α) _ }
lemma coe_add [add_semigroup α] {a b : α} : ((a + b : α) : with_top α) = a + b := rfl
instance [add_comm_semigroup α] : add_comm_semigroup (with_top α) :=
{ ..@additive.add_comm_semigroup _ $
@with_zero.comm_semigroup (multiplicative α) _ }
instance [add_monoid α] : add_monoid (with_top α) :=
{ zero := some 0,
add := (+),
..@additive.add_monoid _ $ @with_zero.monoid (multiplicative α) _ }
instance [add_comm_monoid α] : add_comm_monoid (with_top α) :=
{ zero := 0,
add := (+),
..@additive.add_comm_monoid _ $
@with_zero.comm_monoid (multiplicative α) _ }
instance [ordered_comm_monoid α] : ordered_comm_monoid (with_top α) :=
begin
suffices, refine {
add_le_add_left := this,
..with_top.partial_order,
..with_top.add_comm_monoid, ..},
{ intros a b c h,
refine ⟨λ c h₂, _, λ h₂, h.2 $ this _ _ h₂ _⟩,
cases h₂, cases a with a,
{ exact (not_le_of_lt h).elim le_top },
cases b with b,
{ exact (not_le_of_lt h).elim le_top },
{ exact ⟨_, rfl, le_of_lt (lt_of_add_lt_add_left' $
with_top.some_lt_some.1 h)⟩ } },
{ intros a b h c cb h₂,
cases c with c, {cases h₂},
cases b with b; cases h₂,
cases a with a, {cases le_antisymm h le_top},
simp at h,
exact ⟨_, rfl, add_le_add_left' h⟩, }
end
@[simp] lemma zero_lt_top [ordered_comm_monoid α] : (0 : with_top α) < ⊤ :=
coe_lt_top 0
@[simp] lemma zero_lt_coe [ordered_comm_monoid α] (a : α) : (0 : with_top α) < a ↔ 0 < a :=
coe_lt_coe
@[simp] lemma add_top [ordered_comm_monoid α] : ∀{a : with_top α}, a + ⊤ = ⊤
| none := rfl
| (some a) := rfl
@[simp] lemma top_add [ordered_comm_monoid α] {a : with_top α} : ⊤ + a = ⊤ := rfl
lemma add_eq_top [ordered_comm_monoid α] (a b : with_top α) : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ :=
by cases a; cases b; simp [none_eq_top, some_eq_coe, coe_add.symm]
lemma add_lt_top [ordered_comm_monoid α] (a b : with_top α) : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ :=
begin
apply not_iff_not.1,
simp [lt_top_iff_ne_top, add_eq_top],
finish,
apply classical.dec _,
apply classical.dec _,
end
instance [canonically_ordered_monoid α] : canonically_ordered_monoid (with_top α) :=
{ le_iff_exists_add := assume a b,
match a, b with
| a, none := show a ≤ ⊤ ↔ ∃c, ⊤ = a + c, by simp; refine ⟨⊤, _⟩; cases a; refl
| (some a), (some b) := show (a:with_top α) ≤ ↑b ↔ ∃c:with_top α, ↑b = ↑a + c,
begin
simp [canonically_ordered_monoid.le_iff_exists_add, -add_comm],
split,
{ rintro ⟨c, rfl⟩, refine ⟨c, _⟩, simp [coe_add] },
{ exact assume h, match b, h with _, ⟨some c, rfl⟩ := ⟨_, rfl⟩ end }
end
| none, some b := show (⊤ : with_top α) ≤ b ↔ ∃c:with_top α, ↑b = ⊤ + c, by simp
end,
.. with_top.ordered_comm_monoid }
end with_top
namespace with_bot
open lattice
instance [add_semigroup α] : add_semigroup (with_bot α) := with_top.add_semigroup
instance [add_comm_semigroup α] : add_comm_semigroup (with_bot α) := with_top.add_comm_semigroup
instance [add_monoid α] : add_monoid (with_bot α) := with_top.add_monoid
instance [add_comm_monoid α] : add_comm_monoid (with_bot α) := with_top.add_comm_monoid
instance [ordered_comm_monoid α] : ordered_comm_monoid (with_bot α) :=
begin
suffices, refine {
add_le_add_left := this,
..with_bot.partial_order,
..with_bot.add_comm_monoid, ..},
{ intros a b c h,
have h' := h,
rw lt_iff_le_not_le at h' ⊢,
refine ⟨λ b h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩,
cases h₂, cases a with a,
{ exact (not_le_of_lt h).elim bot_le },
cases c with c,
{ exact (not_le_of_lt h).elim bot_le },
{ exact ⟨_, rfl, le_of_lt (lt_of_add_lt_add_left' $
with_bot.some_lt_some.1 h)⟩ } },
{ intros a b h c ca h₂,
cases c with c, {cases h₂},
cases a with a; cases h₂,
cases b with b, {cases le_antisymm h bot_le},
simp at h,
exact ⟨_, rfl, add_le_add_left' h⟩, }
end
@[simp] lemma coe_zero [add_monoid α] : ((0 : α) : with_bot α) = 0 := rfl
@[simp] lemma coe_add [add_semigroup α] (a b : α) : ((a + b : α) : with_bot α) = a + b := rfl
@[simp] lemma bot_add [ordered_comm_monoid α] (a : with_bot α) : ⊥ + a = ⊥ := rfl
@[simp] lemma add_bot [ordered_comm_monoid α] (a : with_bot α) : a + ⊥ = ⊥ := by cases a; refl
instance has_one [has_one α] : has_one (with_bot α) := ⟨(1 : α)⟩
@[simp] lemma coe_one [has_one α] : ((1 : α) : with_bot α) = 1 := rfl
end with_bot
section canonically_ordered_monoid
variables [canonically_ordered_monoid α] {a b c d : α}
lemma le_iff_exists_add : a ≤ b ↔ ∃c, b = a + c :=
canonically_ordered_monoid.le_iff_exists_add a b
@[simp] lemma zero_le (a : α) : 0 ≤ a := le_iff_exists_add.mpr ⟨a, by simp⟩
@[simp] lemma add_eq_zero_iff : a + b = 0 ↔ a = 0 ∧ b = 0 :=
add_eq_zero_iff' (zero_le _) (zero_le _)
@[simp] lemma le_zero_iff_eq : a ≤ 0 ↔ a = 0 :=
iff.intro
(assume h, le_antisymm h (zero_le a))
(assume h, h ▸ le_refl a)
protected lemma zero_lt_iff_ne_zero : 0 < a ↔ a ≠ 0 :=
iff.intro ne_of_gt $ assume hne, lt_of_le_of_ne (zero_le _) hne.symm
lemma le_add_left (h : a ≤ c) : a ≤ b + c :=
calc a = 0 + a : by simp
... ≤ b + c : add_le_add' (zero_le _) h
lemma le_add_right (h : a ≤ b) : a ≤ b + c :=
calc a = a + 0 : by simp
... ≤ b + c : add_le_add' h (zero_le _)
instance with_zero.canonically_ordered_monoid :
canonically_ordered_monoid (with_zero α) :=
{ le_iff_exists_add := λ a b, begin
cases a with a,
{ exact iff_of_true lattice.bot_le ⟨b, (zero_add b).symm⟩ },
cases b with b,
{ exact iff_of_false
(mt (le_antisymm lattice.bot_le) (by simp))
(λ ⟨c, h⟩, by cases c; cases h) },
{ simp [le_iff_exists_add, -add_comm],
split; intro h; rcases h with ⟨c, h⟩,
{ exact ⟨some c, congr_arg some h⟩ },
{ cases c; cases h,
{ exact ⟨_, (add_zero _).symm⟩ },
{ exact ⟨_, rfl⟩ } } }
end,
..with_zero.ordered_comm_monoid zero_le }
end canonically_ordered_monoid
instance ordered_cancel_comm_monoid.to_ordered_comm_monoid
[H : ordered_cancel_comm_monoid α] : ordered_comm_monoid α :=
{ lt_of_add_lt_add_left := @lt_of_add_lt_add_left _ _, ..H }
section ordered_cancel_comm_monoid
variables [ordered_cancel_comm_monoid α] {a b c : α}
@[simp] lemma add_le_add_iff_left (a : α) {b c : α} : a + b ≤ a + c ↔ b ≤ c :=
⟨le_of_add_le_add_left, λ h, add_le_add_left h _⟩
@[simp] lemma add_le_add_iff_right (c : α) : a + c ≤ b + c ↔ a ≤ b :=
add_comm c a ▸ add_comm c b ▸ add_le_add_iff_left c
@[simp] lemma add_lt_add_iff_left (a : α) {b c : α} : a + b < a + c ↔ b < c :=
⟨lt_of_add_lt_add_left, λ h, add_lt_add_left h _⟩
@[simp] lemma add_lt_add_iff_right (c : α) : a + c < b + c ↔ a < b :=
add_comm c a ▸ add_comm c b ▸ add_lt_add_iff_left c
@[simp] lemma le_add_iff_nonneg_right (a : α) {b : α} : a ≤ a + b ↔ 0 ≤ b :=
have a + 0 ≤ a + b ↔ 0 ≤ b, from add_le_add_iff_left a,
by rwa add_zero at this
@[simp] lemma le_add_iff_nonneg_left (a : α) {b : α} : a ≤ b + a ↔ 0 ≤ b :=
by rw [add_comm, le_add_iff_nonneg_right]
@[simp] lemma lt_add_iff_pos_right (a : α) {b : α} : a < a + b ↔ 0 < b :=
have a + 0 < a + b ↔ 0 < b, from add_lt_add_iff_left a,
by rwa add_zero at this
@[simp] lemma lt_add_iff_pos_left (a : α) {b : α} : a < b + a ↔ 0 < b :=
by rw [add_comm, lt_add_iff_pos_right]
lemma add_eq_zero_iff_eq_zero_of_nonneg
(ha : 0 ≤ a) (hb : 0 ≤ b) : a + b = 0 ↔ a = 0 ∧ b = 0 :=
⟨λ hab : a + b = 0,
by split; apply le_antisymm; try {assumption};
rw ← hab; simp [ha, hb],
λ ⟨ha', hb'⟩, by rw [ha', hb', add_zero]⟩
lemma with_top.add_lt_add_iff_left :
∀{a b c : with_top α}, a < ⊤ → (a + c < a + b ↔ c < b)
| none := assume b c h, (lt_irrefl ⊤ h).elim
| (some a) :=
begin
assume b c h,
cases b; cases c;
simp [with_top.none_eq_top, with_top.some_eq_coe, with_top.coe_lt_top, with_top.coe_lt_coe],
{ rw [← with_top.coe_add], exact with_top.coe_lt_top _ },
{ rw [← with_top.coe_add, ← with_top.coe_add, with_top.coe_lt_coe],
exact add_lt_add_iff_left _ }
end
lemma with_top.add_lt_add_iff_right
{a b c : with_top α} : a < ⊤ → (c + a < b + a ↔ c < b) :=
by simpa [add_comm] using @with_top.add_lt_add_iff_left _ _ a b c
end ordered_cancel_comm_monoid
section ordered_comm_group
variables [ordered_comm_group α] {a b c : α}
@[simp] lemma neg_le_neg_iff : -a ≤ -b ↔ b ≤ a :=
have a + b + -a ≤ a + b + -b ↔ -a ≤ -b, from add_le_add_iff_left _,
by simp at this; simp [this]
lemma neg_le : -a ≤ b ↔ -b ≤ a :=
have -a ≤ -(-b) ↔ -b ≤ a, from neg_le_neg_iff,
by rwa neg_neg at this
lemma le_neg : a ≤ -b ↔ b ≤ -a :=
have -(-a) ≤ -b ↔ b ≤ -a, from neg_le_neg_iff,
by rwa neg_neg at this
@[simp] lemma neg_nonpos : -a ≤ 0 ↔ 0 ≤ a :=
have -a ≤ -0 ↔ 0 ≤ a, from neg_le_neg_iff,
by rwa neg_zero at this
@[simp] lemma neg_nonneg : 0 ≤ -a ↔ a ≤ 0 :=
have -0 ≤ -a ↔ a ≤ 0, from neg_le_neg_iff,
by rwa neg_zero at this
@[simp] lemma neg_lt_neg_iff : -a < -b ↔ b < a :=
have a + b + -a < a + b + -b ↔ -a < -b, from add_lt_add_iff_left _,
by simp at this; simp [this]
lemma neg_lt_zero : -a < 0 ↔ 0 < a :=
have -a < -0 ↔ 0 < a, from neg_lt_neg_iff,
by rwa neg_zero at this
lemma neg_pos : 0 < -a ↔ a < 0 :=
have -0 < -a ↔ a < 0, from neg_lt_neg_iff,
by rwa neg_zero at this
lemma neg_lt : -a < b ↔ -b < a :=
have -a < -(-b) ↔ -b < a, from neg_lt_neg_iff,
by rwa neg_neg at this
lemma lt_neg : a < -b ↔ b < -a :=
have -(-a) < -b ↔ b < -a, from neg_lt_neg_iff,
by rwa neg_neg at this
lemma sub_le_sub_iff_left (a : α) {b c : α} : a - b ≤ a - c ↔ c ≤ b :=
(add_le_add_iff_left _).trans neg_le_neg_iff
lemma sub_le_sub_iff_right (c : α) : a - c ≤ b - c ↔ a ≤ b :=
add_le_add_iff_right _
lemma sub_lt_sub_iff_left (a : α) {b c : α} : a - b < a - c ↔ c < b :=
(add_lt_add_iff_left _).trans neg_lt_neg_iff
lemma sub_lt_sub_iff_right (c : α) : a - c < b - c ↔ a < b :=
add_lt_add_iff_right _
@[simp] lemma sub_nonneg : 0 ≤ a - b ↔ b ≤ a :=
have a - a ≤ a - b ↔ b ≤ a, from sub_le_sub_iff_left a,
by rwa sub_self at this
@[simp] lemma sub_nonpos : a - b ≤ 0 ↔ a ≤ b :=
have a - b ≤ b - b ↔ a ≤ b, from sub_le_sub_iff_right b,
by rwa sub_self at this
@[simp] lemma sub_pos : 0 < a - b ↔ b < a :=
have a - a < a - b ↔ b < a, from sub_lt_sub_iff_left a,
by rwa sub_self at this
@[simp] lemma sub_lt_zero : a - b < 0 ↔ a < b :=
have a - b < b - b ↔ a < b, from sub_lt_sub_iff_right b,
by rwa sub_self at this
lemma le_neg_add_iff_add_le : b ≤ -a + c ↔ a + b ≤ c :=
have -a + (a + b) ≤ -a + c ↔ a + b ≤ c, from add_le_add_iff_left _,
by rwa neg_add_cancel_left at this
lemma le_sub_iff_add_le' : b ≤ c - a ↔ a + b ≤ c :=
by rw [sub_eq_add_neg, add_comm, le_neg_add_iff_add_le]
lemma le_sub_iff_add_le : a ≤ c - b ↔ a + b ≤ c :=
by rw [le_sub_iff_add_le', add_comm]
@[simp] lemma neg_add_le_iff_le_add : -b + a ≤ c ↔ a ≤ b + c :=
have -b + a ≤ -b + (b + c) ↔ a ≤ b + c, from add_le_add_iff_left _,
by rwa neg_add_cancel_left at this
lemma sub_le_iff_le_add' : a - b ≤ c ↔ a ≤ b + c :=
by rw [sub_eq_add_neg, add_comm, neg_add_le_iff_le_add]
lemma sub_le_iff_le_add : a - c ≤ b ↔ a ≤ b + c :=
by rw [sub_le_iff_le_add', add_comm]
@[simp] lemma add_neg_le_iff_le_add : a + -c ≤ b ↔ a ≤ b + c :=
sub_le_iff_le_add
@[simp] lemma add_neg_le_iff_le_add' : a + -b ≤ c ↔ a ≤ b + c :=
sub_le_iff_le_add'
lemma neg_add_le_iff_le_add' : -c + a ≤ b ↔ a ≤ b + c :=
by rw [neg_add_le_iff_le_add, add_comm]
@[simp] lemma neg_le_sub_iff_le_add : -b ≤ a - c ↔ c ≤ a + b :=
le_sub_iff_add_le.trans neg_add_le_iff_le_add'
lemma neg_le_sub_iff_le_add' : -a ≤ b - c ↔ c ≤ a + b :=
by rw [neg_le_sub_iff_le_add, add_comm]
lemma sub_le : a - b ≤ c ↔ a - c ≤ b :=
sub_le_iff_le_add'.trans sub_le_iff_le_add.symm
theorem le_sub : a ≤ b - c ↔ c ≤ b - a :=
le_sub_iff_add_le'.trans le_sub_iff_add_le.symm
@[simp] lemma lt_neg_add_iff_add_lt : b < -a + c ↔ a + b < c :=
have -a + (a + b) < -a + c ↔ a + b < c, from add_lt_add_iff_left _,
by rwa neg_add_cancel_left at this
lemma lt_sub_iff_add_lt' : b < c - a ↔ a + b < c :=
by rw [sub_eq_add_neg, add_comm, lt_neg_add_iff_add_lt]
lemma lt_sub_iff_add_lt : a < c - b ↔ a + b < c :=
by rw [lt_sub_iff_add_lt', add_comm]
@[simp] lemma neg_add_lt_iff_lt_add : -b + a < c ↔ a < b + c :=
have -b + a < -b + (b + c) ↔ a < b + c, from add_lt_add_iff_left _,
by rwa neg_add_cancel_left at this
lemma sub_lt_iff_lt_add' : a - b < c ↔ a < b + c :=
by rw [sub_eq_add_neg, add_comm, neg_add_lt_iff_lt_add]
lemma sub_lt_iff_lt_add : a - c < b ↔ a < b + c :=
by rw [sub_lt_iff_lt_add', add_comm]
lemma neg_add_lt_iff_lt_add_right : -c + a < b ↔ a < b + c :=
by rw [neg_add_lt_iff_lt_add, add_comm]
@[simp] lemma neg_lt_sub_iff_lt_add : -b < a - c ↔ c < a + b :=
lt_sub_iff_add_lt.trans neg_add_lt_iff_lt_add_right
lemma neg_lt_sub_iff_lt_add' : -a < b - c ↔ c < a + b :=
by rw [neg_lt_sub_iff_lt_add, add_comm]
lemma sub_lt : a - b < c ↔ a - c < b :=
sub_lt_iff_lt_add'.trans sub_lt_iff_lt_add.symm
theorem lt_sub : a < b - c ↔ c < b - a :=
lt_sub_iff_add_lt'.trans lt_sub_iff_add_lt.symm
lemma sub_le_self_iff (a : α) {b : α} : a - b ≤ a ↔ 0 ≤ b :=
sub_le_iff_le_add'.trans (le_add_iff_nonneg_left _)
lemma sub_lt_self_iff (a : α) {b : α} : a - b < a ↔ 0 < b :=
sub_lt_iff_lt_add'.trans (lt_add_iff_pos_left _)
end ordered_comm_group
set_option old_structure_cmd true
/-- This is not so much a new structure as a construction mechanism
for ordered groups, by specifying only the "positive cone" of the group. -/
class nonneg_comm_group (α : Type*) extends add_comm_group α :=
(nonneg : α → Prop)
(pos : α → Prop := λ a, nonneg a ∧ ¬ nonneg (neg a))
(pos_iff : ∀ a, pos a ↔ nonneg a ∧ ¬ nonneg (-a) . order_laws_tac)
(zero_nonneg : nonneg 0)
(add_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a + b))
(nonneg_antisymm : ∀ {a}, nonneg a → nonneg (-a) → a = 0)
namespace nonneg_comm_group
variable [s : nonneg_comm_group α]
include s
@[reducible] instance to_ordered_comm_group : ordered_comm_group α :=
{ le := λ a b, nonneg (b - a),
lt := λ a b, pos (b - a),
lt_iff_le_not_le := λ a b, by simp; rw [pos_iff]; simp,
le_refl := λ a, by simp [zero_nonneg],
le_trans := λ a b c nab nbc, by simp [-sub_eq_add_neg];
rw ← sub_add_sub_cancel; exact add_nonneg nbc nab,
le_antisymm := λ a b nab nba, eq_of_sub_eq_zero $
nonneg_antisymm nba (by rw neg_sub; exact nab),
add_le_add_left := λ a b nab c, by simpa [(≤), preorder.le] using nab,
add_lt_add_left := λ a b nab c, by simpa [(<), preorder.lt] using nab, ..s }
theorem nonneg_def {a : α} : nonneg a ↔ 0 ≤ a :=
show _ ↔ nonneg _, by simp
theorem pos_def {a : α} : pos a ↔ 0 < a :=
show _ ↔ pos _, by simp
theorem not_zero_pos : ¬ pos (0 : α) :=
mt pos_def.1 (lt_irrefl _)
theorem zero_lt_iff_nonneg_nonneg {a : α} :
0 < a ↔ nonneg a ∧ ¬ nonneg (-a) :=
pos_def.symm.trans (pos_iff α _)
theorem nonneg_total_iff :
(∀ a : α, nonneg a ∨ nonneg (-a)) ↔
(∀ a b : α, a ≤ b ∨ b ≤ a) :=
⟨λ h a b, by have := h (b - a); rwa [neg_sub] at this,
λ h a, by rw [nonneg_def, nonneg_def, neg_nonneg]; apply h⟩
def to_decidable_linear_ordered_comm_group
[decidable_pred (@nonneg α _)]
(nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a))
: decidable_linear_ordered_comm_group α :=
{ le := (≤),
lt := (<),
lt_iff_le_not_le := @lt_iff_le_not_le _ _,
le_refl := @le_refl _ _,
le_trans := @le_trans _ _,
le_antisymm := @le_antisymm _ _,
le_total := nonneg_total_iff.1 nonneg_total,
decidable_le := by apply_instance,
decidable_eq := by apply_instance,
decidable_lt := by apply_instance,
..@nonneg_comm_group.to_ordered_comm_group _ s }
end nonneg_comm_group
|
d1a8f068734b475bba9ef740a4b33a15879e7821 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/1365.lean | 555cdcfee74cf423d852dacc0998e7e1576f5636 | [
"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 | 376 | lean | structure Foo where
a : Option Bool
b : Option Bool
c : Option Bool
d : Option Bool
e : Option Bool
f : Option Bool
g : Option Bool
h : Option Bool
i : Option Bool
j : Option Bool
k : Option Bool
l : Option Bool
m : Option Bool
n : Option Bool
o : Option Bool
p : Option Bool
q : Option Bool
r : Option Bool
s : Option Bool
deriving Repr
|
e741a48518d240c467f4f42bcbb81d592bae1afb | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/elabissues/issues4.lean | dc6b10370f68ffb31d2d0a85607d34d42d9ef87a | [
"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 | 657 | lean | -- From @joehendrix
-- The imul doesn't type check as Lean won't try to coerce from a reg (bv 64) to a expr (bv ?u)
inductive MCType
| bv : Nat → MCType
open MCType
inductive Reg : MCType → Type
| rax : Reg (bv 64)
inductive Expr : MCType → Type
| r : ∀{tp:MCType}, Reg tp → Expr tp
| sextC {s:Nat} (x : Expr (bv s)) (t:Nat) : Expr (bv t)
instance reg_is_expr {tp:MCType} : HasCoe (Reg tp) (Expr tp) := ⟨Expr.r⟩
def bvmul {w:Nat} (x y : Expr (bv w)) : Expr (bv w) := x
def sext {s:Nat} (x : Expr (bv s)) (t:Nat) : Expr (bv t) := Expr.sextC x t
def imul {u:Nat} (e:Expr (bv 64)) : Expr (bv 128) :=
bvmul (sext Reg.rax 128) (sext e _)
|
b098a45f703289fbbd0fd558df29c89b07ff4483 | 92b50235facfbc08dfe7f334827d47281471333b | /tests/lean/errors.lean | 591cf06b18bf39bebb243ba6922fd14969cc1eab | [
"Apache-2.0"
] | permissive | htzh/lean | 24f6ed7510ab637379ec31af406d12584d31792c | d70c79f4e30aafecdfc4a60b5d3512199200ab6e | refs/heads/master | 1,607,677,731,270 | 1,437,089,952,000 | 1,437,089,952,000 | 37,078,816 | 0 | 0 | null | 1,433,780,956,000 | 1,433,780,955,000 | null | UTF-8 | Lean | false | false | 516 | lean | namespace foo
definition tst1 : nat → nat → nat :=
a + b -- ERROR
check tst1
definition tst2 : nat → nat → nat :=
begin
intro a,
intro b,
cases add wth (a, b), -- ERROR
exact a,
exact b,
end
section
parameter A : Type
definition tst3 : A → A → A :=
begin
intro a,
apply b, -- ERROR
exact a
end
check tst3
end
end foo
open nat
definition bla : nat :=
foo.tst1 0 0 + foo.tst2 0 0 + foo.tst3 nat 1 1
check foo.tst1
check foo.tst2
check foo.tst3
|
8d9c7281fa4eb5beb580bcfa36cbd1c1bd5c5011 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /tests/lean/run/elab_cmd.lean | 00f2053db386c5b6da43a376184a54d1013da859 | [
"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 | 656 | lean | import Lean
open Lean.Elab.Term
open Lean.Elab.Command
elab "∃'" b:term "," P:term : term => do
let ex ← `(Exists (fun $b => $P));
elabTerm ex none
elab "#check2" b:term : command => do
let cmd ← `(#check $b #check $b);
elabCommand cmd
#check ∃' x, x > 0
#check ∃' (x : UInt32), x > 0
#check2 10
elab "try" t:tactic : tactic => do
let t' ← `(tactic| first | $t | skip);
Lean.Elab.Tactic.evalTactic t'
theorem tst (x y z : Nat) : y = z → x = x → x = y → x = z :=
by {
intro h1; intro h2; intro h3;
apply @Eq.trans;
try exact h1; -- `exact h1` fails
trace_state;
try exact h3;
trace_state;
try exact h1;
}
|
20a8273b87a0db432de5076a0d401d4ac89f3f9f | cad9160f67da6c3eecba9bb47aa1acdf5862cd58 | /src/chapter1.lean | d42f3bccc537358c8cb8bb7488619876a7e84ae3 | [] | no_license | paraseba/topology-janich | 902a9ecf06aa225215a4cf144bf0d6e0043b6135 | 270f3d2de019c093c86ac967b4ec9a4a2f25a5d3 | refs/heads/master | 1,671,576,401,688 | 1,599,706,090,000 | 1,599,706,090,000 | 291,599,865 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 25,890 | lean | import tactic.basic
import tactic.linarith
import data.set.basic
import data.set.lattice
import data.real.basic
import data.set.finite
import .sets
--import topology.basic
namespace topology
open set
universes u w
-- Copied from mathlib
structure topological_space (α : Type u) :=
(is_open : set α → Prop)
(is_open_univ : is_open set.univ)
(is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t))
(is_open_sUnion : ∀s:(set (set α)), (∀t∈s, is_open t) → is_open (⋃₀ s))
attribute [class] topological_space
variables {α : Type*} {β : Type*}
variables [topo: topological_space α]
include topo
def is_open (s : set α) : Prop := topological_space.is_open topo s
def is_closed (s : set α) : Prop := is_open sᶜ
lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) :=
topological_space.is_open_sUnion topo s h
lemma is_open_Union {ι : Sort w} {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) :=
is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i
lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) :
is_open (⋃i∈s, f i) :=
is_open_Union $ assume i, is_open_Union $ assume hi, h i hi
@[simp]
lemma is_open_empty : topo.is_open ∅ :=
begin
have h : is_open (⋃₀ ∅) := topo.is_open_sUnion _ (ball_empty_iff.mpr true.intro),
rw sUnion_empty at h,
exact h,
end
@[simp]
lemma open_iff_closed_compl {s: set α} : is_open s ↔ is_closed sᶜ :=
begin
unfold is_closed,
rw compl_compl,
end
-- page 5
def neighborhood (s : set α) (x : α) := ∃ V, is_open V ∧ (x ∈ V) ∧ (V ⊆ s)
def interior_point (x : α) (s : set α) := neighborhood s x
def exterior_point (x : α) (s : set α) := neighborhood sᶜ x
def interior (s : set α) := {x | interior_point x s}
def closure (s : set α) := {x | ¬ exterior_point x s}
lemma nhb_of_open {x:α} {s : set α} (o : is_open s) (h : x ∈ s) :
neighborhood s x :=
⟨ s, o, h, subset.rfl ⟩
lemma set_in_closure (s : set α) : s ⊆ closure s :=
begin
intros x xin,
unfold closure, unfold exterior_point, unfold neighborhood,
intros ex,
rcases ex with ⟨ _, _, xint, tincomp⟩,
have : x ∈ sᶜ := tincomp xint,
contradiction,
end
lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t :=
begin
unfold closure exterior_point neighborhood,
simp,
intros x hx O Oopen xinO Ointc,
rw ← compl_subset_compl at h,
exact (hx O Oopen xinO) (subset.trans Ointc h)
end
lemma closed_inter_of_closed (ss: set (set α)) (h: ∀ s ∈ ss, is_closed s)
: is_closed ⋂₀ ss :=
begin
unfold is_closed,
rw compl_sInter,
apply is_open_sUnion,
intros s hs,
simp at hs,
rcases hs with ⟨t, tinss, scomp ⟩,
have : is_closed t := h t tinss,
rw ← scomp,
exact this,
end
theorem closure_is_intersection (s : set α) :
closure s = ⋂₀ {t | is_closed t ∧ s ⊆ t} :=
begin
ext,
split,
{
intros xincl,
unfold closure at xincl,
unfold exterior_point at xincl,
unfold neighborhood at xincl,
simp at xincl,
let U := ⋂₀ {t | is_closed t ∧ s ⊆ t},
change x ∈ U,
have closed_u : is_closed U, {
apply closed_inter_of_closed,
intros t tinU,
rw mem_set_of_eq at tinU,
exact tinU.1
},
have inSC : Uᶜ ⊆ sᶜ, {
rw compl_subset_compl,
intros a ha,
rw mem_sInter,
intros t tinU,
rw mem_set_of_eq at tinU,
exact tinU.2 ha,
},
rw ← compl_compl U at closed_u,
have baz := mt (xincl Uᶜ closed_u) (not_not.mpr inSC),
have baz' : x ∈ U := set.not_not_mem.mp baz,
exact baz',
},
{
simp,
intros H,
unfold closure,
unfold exterior_point,
unfold neighborhood,
simp,
intros t t_open x_in_t,
rw not_subset,
have := mt(H tᶜ t_open) (not_not.mpr x_in_t),
cases (nonempty_of_not_subset this) with a ain,
use a,
simp,
exact ⟨ set.not_not_mem.mp (not_mem_of_mem_diff ain), mem_of_mem_diff ain ⟩,
}
end
lemma closure_closed {s : set α} : is_closed (closure s) :=
begin
rw closure_is_intersection,
apply closed_inter_of_closed,
intros _ h,
exact h.1
end
-- page 6
-- a set is open iff all its points are interior
theorem open_iff_all_int (s: set α) : is_open s ↔ ∀ x ∈ s, interior_point x s :=
begin
split,
{ show is_open s → ∀ x ∈ s, interior_point x s,
intros op x hx,
exact ⟨ s, op, hx, by refl ⟩ },
{
show (∀ x ∈ s, interior_point x s) → is_open s,
intros h,
choose! V isOpen xinV Vins using h,
rw union_of_sub s V (λ x xin, ⟨ xinV x xin , Vins x xin⟩),
apply is_open_sUnion,
intros s',
intros hs',
simp at hs',
rcases hs' with ⟨ x, hx, vxs⟩ ,
rw ← vxs,
exact isOpen x hx,
}
end
theorem interior_is_union_of_open (s : set α) :
interior s = ⋃₀ {s' | is_open s' ∧ s' ⊆ s} :=
begin
ext,
simp,
split,
{
show x ∈ interior s → ∃ t, (is_closed tᶜ ∧ t ⊆ s) ∧ x ∈ t,
intros xint,
rcases xint with ⟨ V, isOpen, xin, Vin⟩,
exact ⟨ V, ⟨open_iff_closed_compl.mp isOpen, Vin⟩, xin⟩,
},
{
show (∃ t, (is_closed tᶜ ∧ t ⊆ s) ∧ x ∈ t) → x ∈ interior s,
simp,
intros s' isOpen sin' xin,
exact ⟨ s', open_iff_closed_compl.mpr isOpen, xin, sin' ⟩,
}
end
lemma interior_is_open (s : set α) : is_open (interior s) :=
begin
rw interior_is_union_of_open _,
apply is_open_sUnion,
simp,
intros t topen _,
exact topen
end
lemma closed_iff_equal_to_closure {s : set α} :
is_closed s ↔ s = closure s :=
begin
rw closure_is_intersection,
split,
{ show is_closed s → s = ⋂₀{t | is_closed t ∧ s ⊆ t},
intros closed,
ext,
split,
{
show x ∈ s → x ∈ ⋂₀{t | is_closed t ∧ s ⊆ t},
intros xin t tin,
exact tin.2 xin,
},
{
show x ∈ ⋂₀{t | is_closed t ∧ s ⊆ t} → x ∈ s,
intros xin,
simp at xin,
exact xin s closed subset.rfl,
},
},
{ show s = ⋂₀{t | is_closed t ∧ s ⊆ t} → is_closed s,
intros sint,
rw sint,
apply closed_inter_of_closed,
intros t tin,
exact tin.1,
}
end
lemma open_iff_compl_eq_closure (s : set α) :
is_open s ↔ sᶜ = closure sᶜ :=
begin
rw ← closed_iff_equal_to_closure,
simp,
end
lemma closure_idempotent {s : set α} :
closure (closure s) = closure s :=
(closed_iff_equal_to_closure.mp closure_closed).symm
omit topo
structure metric_space (α : Type u) :=
(metric : α → α → ℝ)
(positive : ∀ x y, x ≠ y → metric x y > 0)
(zero_self : ∀ x, metric x x = 0)
(symm : ∀ x y, metric x y = metric y x)
(triangle : ∀ x y z, metric x z ≤ metric x y + metric y z)
attribute [class] metric_space
variable [ms : metric_space α]
include ms
def εBall (x : α) (ε : ℝ) := { y : α | ms.metric x y < ε}
lemma smaller_ball_subset {x : α} (a b : ℝ) (h : a ≤ b)
: εBall x a ⊆ εBall x b :=
begin
intros y yina,
unfold εBall,
have H : ms.metric x y < b :=
calc ms.metric x y < a : yina
... ≤ b : h,
simp [H],
end
instance metric_topology : topological_space α :=
{
is_open := λ s, ∀ x ∈ s, ∃ ε > 0, εBall x ε ⊆ s,
is_open_univ := λ _ _, ⟨ 1, by linarith, by simp ⟩,
is_open_inter := by {
intros s t sball tball x xin,
rcases sball x xin.1 with ⟨εs,εspos,hεs⟩,
rcases tball x xin.2 with ⟨εt,εtpos,hεt⟩,
use min εs εt,
split,
{exact lt_min εspos εtpos,},
{
apply subset_inter,
{
have : εBall x (min εs εt) ⊆ εBall x εs, {
apply smaller_ball_subset,
apply min_le_left
},
exact subset.trans this hεs,
},
{
have : εBall x (min εs εt) ⊆ εBall x εt, {
apply smaller_ball_subset,
apply min_le_right
},
exact subset.trans this hεt,
}
}
},
is_open_sUnion := by {
intros ss h x xin,
rw mem_sUnion at xin,
rcases xin with ⟨ t, tinss, xint ⟩,
rcases h t tinss x xint with ⟨ ε, εpos, ball_in_t ⟩,
exact ⟨ ε, εpos, subset_sUnion_of_subset ss t ball_in_t tinss ⟩
}
}
lemma εBall_open {ε : ℝ} (x : α) (h: ε > 0) : is_open (εBall x ε) :=
begin
intros y xinball,
use ε - ms.metric x y,
split,
{
show 0 < ε - ms.metric x y,
exact sub_pos.mpr xinball,
},
{
show εBall y (ε - ms.metric x y) ⊆ εBall x ε,
intros z zin,
exact
calc ms.metric x z ≤ ms.metric x y + ms.metric y z : by apply ms.triangle
... < ms.metric x y + (ε - ms.metric x y) : add_lt_add_left zin _
... = ε : by {ring},
}
end
lemma center_in_ball {ε : ℝ} (x : α) (h: ε > 0) : x ∈ εBall x ε :=
begin
change ms.metric x x < ε,
rw ms.zero_self,
exact h
end
omit ms
def subspace (U : set α) (ts : topological_space α) : topological_space U :=
{
is_open := λ s, ∃ (V : set α), is_open V ∧ (V ∩ U) = subtype.val '' s,
is_open_univ := ⟨univ, ts.is_open_univ, by {simp [univ_inter]}⟩,
is_open_inter := by {
rintros s t ⟨V,OV,VU⟩ ⟨W,OW,WU⟩,
use V ∩ W,
split,
{exact ts.is_open_inter V W OV OW,},
{
rw ← image_inter,
{
suffices : V ∩ W ∩ U = V ∩ U ∩ W ∩ U,
{ rw this,
rw VU,
rw inter_assoc,
rw WU,
},
show V ∩ W ∩ U = V ∩ U ∩ W ∩ U, tidy,
},
{exact subtype.val_injective}
}
},
is_open_sUnion := by {
intros ss h,
choose! s op eq using h,
use ⋃i:ss, s i,
split,
{
refine is_open_Union _,
finish,
},
{
rw Union_inter,
rw sUnion_eq_Union,
finish [image_Union],
}
},
}
def disjoint_union (s: topological_space α) (t: topological_space β) :
topological_space (α ⊕ β) :=
{
is_open := λ s:set (α ⊕ β), ∃ (u: set α) (v: set β), (u ⊕ v) = s,
is_open_univ := sorry,
is_open_inter := sorry,
is_open_sUnion := sorry,
}
noncomputable def reals_metric_space : metric_space ℝ :=
{
metric := λ x y, abs (x - y),
positive := λ x y ne, by {
apply abs_pos_of_ne_zero,
exact sub_ne_zero_of_ne ne
},
zero_self := by simp,
symm := λ x y, by {
have arg : abs (x-y) = abs (y-x), by {
calc abs (x-y) = abs (- (y - x)) : by simp
... = abs (y-x) : abs_neg _,
},
rw arg,
},
triangle := λ x y z, by apply abs_sub_le,
}
noncomputable instance : metric_space ℝ := reals_metric_space
--noncomputable def reals_metric_topology : topological_space ℝ := topology.metric_topology _
--noncomputable instance : topological_space ℝ := reals_metric_topology
structure basis [topological_space α]
(sets : set (set α))
(sets_open : ∀ s ∈ sets, is_open s)
(union_of_open : ∀ s, is_open s → ∃ ss ⊆ sets, ⋃₀ ss = s)
structure is_subbasis (ts: topological_space α) :=
(sets : set (set α))
(sets_open : ∀ s ∈ sets, ts.is_open s)
-- infinite union of finite intersections of sets in sets
(union_of_fin_inter :
∀ (s:set α), is_open s →
∃ sss : set (set (set α)),
(∀ (ss ∈ sss), set.finite ss ∧ ∀ s ∈ ss, s ∈ sets) ∧
⋃₀ ((λ i, ⋂₀ i) '' sss) = s )
def subbasis_generated (basis : set (set α)) : topological_space α :=
{
is_open := λ s,
∃ sss : set (set (set α)),
(∀ ss ∈ sss, set.finite ss ∧ ∀ s ∈ ss, s ∈ basis) ∧
⋃₀ ((λ i, ⋂₀ i) '' sss) = s,
is_open_univ := ⟨ {{}}, by simp ⟩ ,
is_open_inter := λ s t, by {
intros os ot,
choose! scomp scond sexp using os,
choose! tcomp tcond texp using ot,
have bar := (⋃p ∈ scomp.prod tcomp, (p : (set (set α)) × (set (set α ))).1 ∩ p.2),
have baz := { ss | ss ∈ (scomp.prod tcomp) },
have baz' := (λ (ss : (set (set α)) × (set (set α ))), (ss.1) ∩ (ss.2)) '' baz,
--use baz',
use (λ (ss : (set (set α)) × (set (set α ))), (ss.1) ∩ (ss.2)) '' { ss | ss ∈ (scomp.prod tcomp) },
sorry
},
is_open_sUnion := sorry
}
section continuity
variables [ta: topological_space α] [tb: topological_space β]
include ta tb
def continuous (f: α → β) :=
∀ V, (is_open V) → is_open (f⁻¹' V)
lemma preimage_closed_iff_cont {f: α → β} :
continuous f ↔ ∀ V, (is_closed V) → is_closed (f⁻¹' V) :=
begin
split,
{ intros fcont V Vclosed,
rw ← compl_compl V,
have vcomp_open : is_open Vᶜ, {unfold is_closed at Vclosed, exact Vclosed},
rw preimage_compl,
rw is_closed,
rw compl_compl,
exact fcont Vᶜ vcomp_open,
},
{
intro h,
intros V Vopen,
rw ← compl_compl V,
have vcomp_closed : is_closed Vᶜ, {unfold is_closed, rw compl_compl, exact Vopen},
rw preimage_compl,
exact h Vᶜ vcomp_closed,
}
end
lemma preimage_nhb_nhb_iff_cont (f : α → β) :
continuous f ↔ ∀ (x: α) (N : set β), neighborhood N (f x) → neighborhood (f⁻¹' N) x :=
begin
split,
{
intros cont x N Nfx,
rcases Nfx with ⟨O, oOpen, fxin, OsubN⟩,
have h1 : is_open (f⁻¹' O) := cont O oOpen,
have h2 : (f⁻¹' O) ⊆ f⁻¹' N := preimage_mono OsubN,
exact ⟨ f⁻¹' O, h1, fxin, h2 ⟩,
},
{
intros h O Oopen,
apply (open_iff_all_int (f⁻¹' O)).mpr,
intros x xin,
exact h x O (nhb_of_open Oopen xin),
},
end
lemma closure_preimage_sub_preimage_of_closure_iff_cont (f : α → β) :
continuous f ↔ ∀ B, closure (f⁻¹' B) ⊆ f⁻¹' (closure B) :=
begin
split,
{
intros cont B,
have : B ⊆ closure B := set_in_closure B,
have : f⁻¹' B ⊆ f⁻¹' (closure B) := preimage_mono this,
have h : closure (f⁻¹' B) ⊆ closure (f⁻¹' (closure B)) :=
closure_mono this,
have : is_closed (f⁻¹' (closure B)) :=
preimage_closed_iff_cont.mp cont (closure B) closure_closed,
rw ← closed_iff_equal_to_closure.mp this at h,
exact h,
},
{
intros h,
apply preimage_closed_iff_cont.mpr,
intros V Vclosed,
have h1 : f⁻¹' V ⊆ closure (f⁻¹' V) := set_in_closure _,
have h2 : closure (f⁻¹' V) ⊆ f⁻¹' V, {
have w : f⁻¹' V = f⁻¹' (closure V), {rw ← closed_iff_equal_to_closure.mp Vclosed,},
have z := h V,
rw w at z |-,
exact z,
},
rw subset.antisymm h1 h2,
apply closure_closed,
}
end
-- page 13
example
(f : ℝ → ℝ)
(cont: continuous f) :
∀ (x0 : ℝ) (ε > 0), ∃ δ > 0, ∀ (x : ℝ), abs(x - x0) < δ → abs (f x - f x0) < ε :=
begin
intros x0 e epos,
have open_x := cont (εBall (f x0) e) (εBall_open (f x0) epos),
have : ∃ (ε > 0), εBall x0 ε ⊆ f ⁻¹' εBall (f x0) e :=
open_x x0 (center_in_ball (f x0) epos),
rcases this with ⟨ δ, δpos, ball_in ⟩,
use δ,
split,
{exact δpos,},
{
intros x xin,
have : x ∈ εBall x0 δ := by {rw abs_sub at xin, exact xin},
have : x ∈ f ⁻¹' εBall (f x0) e := ball_in this,
simp at this,
rw ← neg_sub,
rw abs_neg,
exact this,
}
end
class homeomorphism (f : α → β) :=
(inv : β → α)
(left_inv : inv ∘ f = id)
(right_inv : f ∘ inv = id)
(cont: continuous f)
(invcont: continuous inv)
def has_homeomorphism (f : α → β) := nonempty (homeomorphism f)
@[simp]
lemma left_inverse_hom (f : α → β) (h: homeomorphism f) :
function.left_inverse h.inv f :=
begin
intros x,
change (h.inv ∘ f) x = x,
rw h.left_inv,
refl
end
@[simp]
lemma right_inverse_hom (f : α → β) (h: homeomorphism f) :
function.right_inverse h.inv f :=
begin
intros x,
change (f ∘ h.inv) x = x,
rw h.right_inv,
refl
end
lemma homeo_of_open_iff_to_open (f : α → β) (b: function.bijective f) :
has_homeomorphism f ↔ (∀ U, is_open U ↔ is_open (f '' U)) :=
begin
split,
{
intros has,
have h := has.some,
intros U,
split,
{
show is_open U → is_open (f '' U),
intro Uopen,
have : f '' U = (h.inv)⁻¹' U, {
rw image_eq_preimage_of_inverse,
simp, simp,
},
have bar := h.invcont,
have baz := bar U Uopen,
rw ← this at baz,
exact baz,
},
{
show is_open (f '' U) → is_open U,
intros imgopen,
have : U = f⁻¹' (f '' U), {
rw preimage_image_eq,
apply function.left_inverse.injective,
exact left_inverse_hom f h,
},
have bar := h.cont,
have baz := bar (f '' U) imgopen,
rw ← this at baz,
exact baz,
}
},
{
intros h,
exact nonempty.intro {
inv := sorry,
left_inv := sorry,
right_inv := sorry,
cont := sorry,
invcont := sorry,
},
}
end
end continuity
section caracterization
def connected (ts: topological_space α) :=
¬ ∃ U V,
ts.is_open U ∧
ts.is_open V ∧
U ∪ V = univ ∧
U ∩ V = ∅ ∧
U.nonempty ∧
V.nonempty
lemma connected_no_open_close [topo : topological_space α] (h: connected topo) :
∀ (s: set α), is_open s → is_closed s → s = univ ∨ s = ∅ :=
begin
intros s opens closeds,
have h1 : is_open sᶜ := closeds,
have h2 : s ∪ sᶜ = univ := union_compl_self _,
have h3 : s ∩ sᶜ = ∅ := inter_compl_self _,
have := forall_not_of_not_exists h s ,
push_neg at this,
have : s.nonempty → ¬ sᶜ.nonempty := this sᶜ opens h1 h2 h3,
rw not_nonempty_iff_eq_empty at this,
rw compl_empty_iff at this,
by_cases H : s.nonempty,
{exact or.inl (this H)},
{exact or.inr (not_nonempty_iff_eq_empty.mp H)}
end
def unit_interval : set ℝ := {x | 0 ≤ x ∧ x ≤ 1}
def unit_interval_t := { x:ℝ // 0 ≤ x ∧ x ≤ 1}
instance : has_zero unit_interval_t := {
zero := ⟨0, and.intro rfl.ge zero_le_one ⟩
}
instance : has_one unit_interval_t := {
one := ⟨1, and.intro zero_le_one rfl.ge ⟩
}
noncomputable instance unit_interval_topology : topological_space unit_interval_t :=
subspace unit_interval (topology.metric_topology)
def path_connected (ts: topological_space α) :=
∀ (a b : α), ∃ (f: unit_interval_t → α), continuous f ∧ f 0 = a ∧ f 1 = b
variables [ta: topological_space α] [tb: topological_space β]
theorem connected_of_image {f : α → β} (cont: continuous f):
connected ta → connected (subspace (range f) tb) :=
begin
intros fcontinuous,
show connected (subspace (range f) tb),
unfold connected,
by_contradiction H,
rcases H with ⟨ U, V, openU, openV, union_univ, disjoint, nonemptyU, nonemptyV⟩,
have containing :
∀ (W : set ↥(range f)),
W.nonempty → (subspace (range f) tb).is_open W →
∃ (s: set β), is_open s ∧
nonempty s ∧
s ∩ (range f) = coe '' W ∧
is_open (f⁻¹' s) ∧
(f ⁻¹' s).nonempty , {
intros W nonemptyW openW,
rcases openW with ⟨ s, closeds, BintRange ⟩ ,
have nonemptys : nonempty ↥s, {
have := nonempty_image_iff.mpr nonemptyW ,
rw ← BintRange at this,
exact nonempty.to_subtype (nonempty_inter_iff_nonempty this).left,
},
have openpre : is_open (f⁻¹' s), {
apply cont,
exact closeds,
},
have nepre : (f ⁻¹' s).nonempty , {
apply preimage_nonempty_of_inter_range,
rw BintRange,
apply set.nonempty.image,
exact nonemptyW,
},
exact ⟨ s, closeds, nonemptys, BintRange, openpre, nepre ⟩,
},
rcases containing U nonemptyU openU with
⟨ U', openU', nonemptyU', U'inter, hu, neu_pre ⟩,
rcases containing V nonemptyV openV with
⟨ V', openV', nonemptyV', V'inter, hv, nev_pre ⟩,
have hinter : (f⁻¹' U') ∩ (f⁻¹' V') = ∅ :=
calc (f⁻¹' U') ∩ (f⁻¹' V') = f⁻¹' (U' ∩ V') : by rw preimage_inter
... = f⁻¹' ((U' ∩ range f) ∩ (V' ∩ range f)) : by simp [preimage_range_inter]
... = f⁻¹' ((coe '' U) ∩ (coe '' V)) : by simp [U'inter, V'inter]
... = f⁻¹' (coe '' (U ∩ V)) : by simp [image_inter, subtype.coe_injective]
... = f⁻¹' (coe '' ∅) : by rw disjoint
... = ∅ : by {rw image_empty, rw preimage_empty},
have hunion : (f⁻¹' U') ∪ (f⁻¹' V') = univ :=
calc (f⁻¹' U') ∪ (f⁻¹' V') = f⁻¹' (U' ∩ range f) ∪ f⁻¹' (V' ∩ range f)
: by simp [preimage_range_inter]
... = f⁻¹' (coe '' U) ∪ f⁻¹' (coe '' V) : by rw [U'inter, V'inter]
... = f⁻¹' ( (coe '' U) ∪ (coe '' V) ) : by rw preimage_union
... = f⁻¹' (coe '' (U ∪ V)) : by rw image_union
... = f⁻¹' (coe '' univ) : by rw union_univ
... = f⁻¹' univ : by simp [subtype.coe_image_univ]
... = univ : by rw preimage_univ,
show false,
unfold connected at fcontinuous,
push_neg at fcontinuous,
exact fcontinuous (f⁻¹' U') (f⁻¹' V') hu hv hunion hinter neu_pre nev_pre,
end
def hausdorff (ts: topological_space α) :=
∀ x y : α, x ≠ y →
∃ U V : set α,
neighborhood U x ∧
neighborhood V y ∧
U ∩ V = ∅
end caracterization
def sequence {α : Type*} := ℕ → α
include topo
def limit (seq: sequence) (l: α) :=
∀ (U : set α), (neighborhood U l) → ∃ n0, ∀ n, n ≥ n0 → seq n ∈ U
theorem unique_limit_of_hausdorff (haus: hausdorff topo) (seq: sequence) (l m : α) :
(limit seq l) → (limit seq m) → l = m :=
begin
intros ll lm,
by_cases H: l = m, exact H,
rcases haus l m H with ⟨ U, V, neiU, neiV, uvinter ⟩ ,
rcases ll U neiU with ⟨ n0u, hnu ⟩ ,
rcases lm V neiV with ⟨ n0v, hnv ⟩ ,
let n := max n0u n0v,
have h1 : seq n ∈ U := hnu n (le_max_left _ _),
have h2 : seq n ∈ V := hnv n (le_max_right _ _),
have : seq n ∈ U ∩ V := mem_inter h1 h2,
exfalso,
rw uvinter at this,
rw mem_empty_eq at this,
exact this
end
lemma subset_hausdorff {s : set α} (haus: hausdorff topo) : hausdorff (subspace s topo) :=
begin
intros x y nexy,
have : ↑x ≠ ↑y := λ eq, nexy (set_coe.ext eq),
rcases haus x y this with ⟨ U, V, nU, nV, intuv ⟩,
rcases nU with ⟨ opu, open_opu, xin , opuinu ⟩ ,
rcases nV with ⟨ opv, open_opv, yin , opvinv ⟩ ,
use { a | ↑ a ∈ s ∩ opu },
use { a | ↑ a ∈ s ∩ opv },
have aux : ∀ (a b : set α), a ∩ b = b ∩ a ∩ b := λ a b,
calc a ∩ b = a ∩ (b ∩ b) : by simp [inter_self]
... = (a ∩ b) ∩ b : by simp [inter_assoc]
... = (b ∩ a) ∩ b : by simp [inter_comm]
... = b ∩ a ∩ b : by simp,
split,
{
use { a | ↑ a ∈ s ∩ opu },
split,
{
use opu,
rw ← inter_of_subtype,
split,
exact open_opu,
apply aux,
},
{
simp at *,
exact xin,
}
},
{
use { a | ↑ a ∈ s ∩ opv },
split,
{
use opv,
rw ← inter_of_subtype,
split,
exact open_opv,
apply aux,
},
{
simp at *,
exact yin,
},
simp,
change {a : ↥s | ↑a ∈ opu ∩ opv } = ∅,
have : opu ∩ opv = ∅ := by {
have := inter_subset_inter opuinu opvinv,
rw intuv at this,
exact eq_empty_of_subset_empty this,
},
simp [this],
}
end
end topology |
f3958c7f1baa004bbf38e6f73cdd4c8af73bfea6 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/defeq_simp1.lean | d802dfe9403ca84f7472735d4ca7e69c87a2975b | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 737 | lean | @[reducible]
def nat_has_add2 : has_add nat :=
⟨λ x y : nat, nat.add x y⟩
set_option pp.all true
open tactic
local attribute [-simp] id.def
example (a b : nat) (H : @has_add.add nat (id (id nat.has_add)) a b = @has_add.add nat nat_has_add2 a b) : true :=
by do
s ← simp_lemmas.mk_default,
get_local `H >>= infer_type >>= s^.dsimplify >>= trace,
constructor
example (a b : nat) (H : (λ x : nat, @has_add.add nat (id (id nat.has_add)) a b) = (λ x : nat, @has_add.add nat nat_has_add2 a x)) : true :=
by do
s ← simp_lemmas.mk_default,
get_local `H >>= infer_type >>= s^.dsimplify >>= trace,
constructor
attribute [reducible]
definition nat_has_add3 : nat → has_add nat :=
λ n, has_add.mk (λ x y : nat, x + y)
|
f971d87c849a4d1cd3f0ba8af4c9765ce2d00043 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/control/traversable/equiv.lean | ea9d1588094a30ccc799e7b5aaa9939dbba7b483 | [
"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,229 | 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 control.traversable.lemmas
import logic.equiv.defs
/-!
# Transferring `traversable` instances along isomorphisms
This file allows to transfer `traversable` instances along isomorphisms.
## Main declarations
* `equiv.map`: Turns functorially a function `α → β` into a function `t' α → t' β` using the functor
`t` and the equivalence `Π α, t α ≃ t' α`.
* `equiv.functor`: `equiv.map` as a functor.
* `equiv.traverse`: Turns traversably a function `α → m β` into a function `t' α → m (t' β)` using
the traversable functor `t` and the equivalence `Π α, t α ≃ t' α`.
* `equiv.traversable`: `equiv.traverse` as a traversable functor.
* `equiv.is_lawful_traversable`: `equiv.traverse` as a lawful traversable functor.
-/
universes u
namespace equiv
section functor
parameters {t t' : Type u → Type u}
parameters (eqv : Π α, t α ≃ t' α)
variables [functor t]
open functor
/-- Given a functor `t`, a function `t' : Type u → Type u`, and
equivalences `t α ≃ t' α` for all `α`, then every function `α → β` can
be mapped to a function `t' α → t' β` functorially (see
`equiv.functor`). -/
protected def map {α β : Type u} (f : α → β) (x : t' α) : t' β :=
eqv β $ map f ((eqv α).symm x)
/-- The function `equiv.map` transfers the functoriality of `t` to
`t'` using the equivalences `eqv`. -/
protected def functor : functor t' :=
{ map := @equiv.map _ }
variables [is_lawful_functor t]
protected lemma id_map {α : Type u} (x : t' α) : equiv.map id x = x :=
by simp [equiv.map, id_map]
protected lemma comp_map {α β γ : Type u} (g : α → β) (h : β → γ) (x : t' α) :
equiv.map (h ∘ g) x = equiv.map h (equiv.map g x) :=
by simp [equiv.map]; apply comp_map
protected lemma is_lawful_functor : @is_lawful_functor _ equiv.functor :=
{ id_map := @equiv.id_map _ _,
comp_map := @equiv.comp_map _ _ }
protected lemma is_lawful_functor' [F : _root_.functor t']
(h₀ : ∀ {α β} (f : α → β), _root_.functor.map f = equiv.map f)
(h₁ : ∀ {α β} (f : β), _root_.functor.map_const f = (equiv.map ∘ function.const α) f) :
_root_.is_lawful_functor t' :=
begin
have : F = equiv.functor,
{ casesI F, dsimp [equiv.functor],
congr; ext; [rw ← h₀, rw ← h₁] },
substI this,
exact equiv.is_lawful_functor
end
end functor
section traversable
parameters {t t' : Type u → Type u}
parameters (eqv : Π α, t α ≃ t' α)
variables [traversable t]
variables {m : Type u → Type u} [applicative m]
variables {α β : Type u}
/-- Like `equiv.map`, a function `t' : Type u → Type u` can be given
the structure of a traversable functor using a traversable functor
`t'` and equivalences `t α ≃ t' α` for all α. See `equiv.traversable`. -/
protected def traverse (f : α → m β) (x : t' α) : m (t' β) :=
eqv β <$> traverse f ((eqv α).symm x)
/-- The function `equiv.traverse` transfers a traversable functor
instance across the equivalences `eqv`. -/
protected def traversable : traversable t' :=
{ to_functor := equiv.functor eqv,
traverse := @equiv.traverse _ }
end traversable
section equiv
parameters {t t' : Type u → Type u}
parameters (eqv : Π α, t α ≃ t' α)
variables [traversable t] [is_lawful_traversable t]
variables {F G : Type u → Type u} [applicative F] [applicative G]
variables [is_lawful_applicative F] [is_lawful_applicative G]
variables (η : applicative_transformation F G)
variables {α β γ : Type u}
open is_lawful_traversable functor
protected lemma id_traverse (x : t' α) :
equiv.traverse eqv id.mk x = x :=
by simp! [equiv.traverse,id_bind,id_traverse,functor.map] with functor_norm
protected lemma traverse_eq_map_id (f : α → β) (x : t' α) :
equiv.traverse eqv (id.mk ∘ f) x = id.mk (equiv.map eqv f x) :=
by simp [equiv.traverse, traverse_eq_map_id] with functor_norm; refl
protected lemma comp_traverse (f : β → F γ) (g : α → G β) (x : t' α) :
equiv.traverse eqv (comp.mk ∘ functor.map f ∘ g) x =
comp.mk (equiv.traverse eqv f <$> equiv.traverse eqv g x) :=
by simp [equiv.traverse,comp_traverse] with functor_norm; congr; ext; simp
protected lemma naturality (f : α → F β) (x : t' α) :
η (equiv.traverse eqv f x) = equiv.traverse eqv (@η _ ∘ f) x :=
by simp only [equiv.traverse] with functor_norm
/-- The fact that `t` is a lawful traversable functor carries over the
equivalences to `t'`, with the traversable functor structure given by
`equiv.traversable`. -/
protected def is_lawful_traversable : @is_lawful_traversable t' (equiv.traversable eqv) :=
{ to_is_lawful_functor := @equiv.is_lawful_functor _ _ eqv _ _,
id_traverse := @equiv.id_traverse _ _,
comp_traverse := @equiv.comp_traverse _ _,
traverse_eq_map_id := @equiv.traverse_eq_map_id _ _,
naturality := @equiv.naturality _ _ }
/-- If the `traversable t'` instance has the properties that `map`,
`map_const`, and `traverse` are equal to the ones that come from
carrying the traversable functor structure from `t` over the
equivalences, then the fact that `t` is a lawful traversable functor
carries over as well. -/
protected def is_lawful_traversable' [_i : traversable t']
(h₀ : ∀ {α β} (f : α → β),
map f = equiv.map eqv f)
(h₁ : ∀ {α β} (f : β),
map_const f = (equiv.map eqv ∘ function.const α) f)
(h₂ : ∀ {F : Type u → Type u} [applicative F],
by exactI ∀ [is_lawful_applicative F]
{α β} (f : α → F β),
traverse f = equiv.traverse eqv f) :
_root_.is_lawful_traversable t' :=
begin
-- we can't use the same approach as for `is_lawful_functor'` because
-- h₂ needs a `is_lawful_applicative` assumption
refine {to_is_lawful_functor :=
equiv.is_lawful_functor' eqv @h₀ @h₁, ..}; introsI,
{ rw [h₂, equiv.id_traverse], apply_instance },
{ rw [h₂, equiv.comp_traverse f g x, h₂], congr,
rw [h₂], all_goals { apply_instance } },
{ rw [h₂, equiv.traverse_eq_map_id, h₀]; apply_instance },
{ rw [h₂, equiv.naturality, h₂]; apply_instance }
end
end equiv
end equiv
|
a594828210a42587598e160ce5828b3084676066 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/measure_theory/function/simple_func_dense.lean | 0718165a1924bd75e5cc30ffa7f62a5395d198e6 | [
"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 | 41,723 | lean | /-
Copyright (c) 2019 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou, Yury Kudryashov, Heather Macbeth
-/
import measure_theory.function.l1_space
/-!
# Density of simple functions
Show that each Borel measurable function can be approximated pointwise, and each `Lᵖ` Borel
measurable function can be approximated in `Lᵖ` norm, by a sequence of simple functions.
## Main definitions
* `measure_theory.simple_func.nearest_pt (e : ℕ → α) (N : ℕ) : α →ₛ ℕ`: the `simple_func` sending
each `x : α` to the point `e k` which is the nearest to `x` among `e 0`, ..., `e N`.
* `measure_theory.simple_func.approx_on (f : β → α) (hf : measurable f) (s : set α) (y₀ : α)
(h₀ : y₀ ∈ s) [separable_space s] (n : ℕ) : β →ₛ α` : a simple function that takes values in `s`
and approximates `f`.
* `measure_theory.Lp.simple_func`, the type of `Lp` simple functions
* `coe_to_Lp`, the embedding of `Lp.simple_func E p μ` into `Lp E p μ`
## Main results
* `tendsto_approx_on` (pointwise convergence): If `f x ∈ s`, then the sequence of simple
approximations `measure_theory.simple_func.approx_on f hf s y₀ h₀ n`, evaluated at `x`,
tends to `f x` as `n` tends to `∞`.
* `tendsto_approx_on_univ_Lp` (Lᵖ convergence): If `E` is a `normed_group` and `f` is measurable
and `mem_ℒp` (for `p < ∞`), then the simple functions `simple_func.approx_on f hf s 0 h₀ n` may
be considered as elements of `Lp E p μ`, and they tend in Lᵖ to `f`.
* `Lp.simple_func.dense_embedding`: the embedding `coe_to_Lp` of the `Lp` simple functions into
`Lp` is dense.
* `Lp.simple_func.induction`, `Lp.induction`, `mem_ℒp.induction`, `integrable.induction`: to prove
a predicate for all elements of one of these classes of functions, it suffices to check that it
behaves correctly on simple functions.
## TODO
For `E` finite-dimensional, simple functions `α →ₛ E` are dense in L^∞ -- prove this.
## Notations
* `α →ₛ β` (local notation): the type of simple functions `α → β`.
* `α →₁ₛ[μ] E`: the type of `L1` simple functions `α → β`.
-/
open set function filter topological_space ennreal emetric finset
open_locale classical topological_space ennreal measure_theory big_operators
variables {α β ι E F 𝕜 : Type*}
noncomputable theory
namespace measure_theory
local infixr ` →ₛ `:25 := simple_func
namespace simple_func
/-! ### Pointwise approximation by simple functions -/
section pointwise
variables [measurable_space α] [emetric_space α] [opens_measurable_space α]
/-- `nearest_pt_ind e N x` is the index `k` such that `e k` is the nearest point to `x` among the
points `e 0`, ..., `e N`. If more than one point are at the same distance from `x`, then
`nearest_pt_ind e N x` returns the least of their indexes. -/
noncomputable def nearest_pt_ind (e : ℕ → α) : ℕ → α →ₛ ℕ
| 0 := const α 0
| (N + 1) := piecewise (⋂ k ≤ N, {x | edist (e (N + 1)) x < edist (e k) x})
(measurable_set.Inter $ λ k, measurable_set.Inter_Prop $ λ hk,
measurable_set_lt measurable_edist_right measurable_edist_right)
(const α $ N + 1) (nearest_pt_ind N)
/-- `nearest_pt e N x` is the nearest point to `x` among the points `e 0`, ..., `e N`. If more than
one point are at the same distance from `x`, then `nearest_pt e N x` returns the point with the
least possible index. -/
noncomputable def nearest_pt (e : ℕ → α) (N : ℕ) : α →ₛ α :=
(nearest_pt_ind e N).map e
@[simp] lemma nearest_pt_ind_zero (e : ℕ → α) : nearest_pt_ind e 0 = const α 0 := rfl
@[simp] lemma nearest_pt_zero (e : ℕ → α) : nearest_pt e 0 = const α (e 0) := rfl
lemma nearest_pt_ind_succ (e : ℕ → α) (N : ℕ) (x : α) :
nearest_pt_ind e (N + 1) x =
if ∀ k ≤ N, edist (e (N + 1)) x < edist (e k) x
then N + 1 else nearest_pt_ind e N x :=
by { simp only [nearest_pt_ind, coe_piecewise, set.piecewise], congr, simp }
lemma nearest_pt_ind_le (e : ℕ → α) (N : ℕ) (x : α) : nearest_pt_ind e N x ≤ N :=
begin
induction N with N ihN, { simp },
simp only [nearest_pt_ind_succ],
split_ifs,
exacts [le_rfl, ihN.trans N.le_succ]
end
lemma edist_nearest_pt_le (e : ℕ → α) (x : α) {k N : ℕ} (hk : k ≤ N) :
edist (nearest_pt e N x) x ≤ edist (e k) x :=
begin
induction N with N ihN generalizing k,
{ simp [nonpos_iff_eq_zero.1 hk, le_refl] },
{ simp only [nearest_pt, nearest_pt_ind_succ, map_apply],
split_ifs,
{ rcases hk.eq_or_lt with rfl|hk,
exacts [le_rfl, (h k (nat.lt_succ_iff.1 hk)).le] },
{ push_neg at h,
rcases h with ⟨l, hlN, hxl⟩,
rcases hk.eq_or_lt with rfl|hk,
exacts [(ihN hlN).trans hxl, ihN (nat.lt_succ_iff.1 hk)] } }
end
lemma tendsto_nearest_pt {e : ℕ → α} {x : α} (hx : x ∈ closure (range e)) :
tendsto (λ N, nearest_pt e N x) at_top (𝓝 x) :=
begin
refine (at_top_basis.tendsto_iff nhds_basis_eball).2 (λ ε hε, _),
rcases emetric.mem_closure_iff.1 hx ε hε with ⟨_, ⟨N, rfl⟩, hN⟩,
rw [edist_comm] at hN,
exact ⟨N, trivial, λ n hn, (edist_nearest_pt_le e x hn).trans_lt hN⟩
end
variables [measurable_space β] {f : β → α}
/-- Approximate a measurable function by a sequence of simple functions `F n` such that
`F n x ∈ s`. -/
noncomputable def approx_on (f : β → α) (hf : measurable f) (s : set α) (y₀ : α) (h₀ : y₀ ∈ s)
[separable_space s] (n : ℕ) :
β →ₛ α :=
by haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩;
exact comp (nearest_pt (λ k, nat.cases_on k y₀ (coe ∘ dense_seq s) : ℕ → α) n) f hf
@[simp] lemma approx_on_zero {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (x : β) :
approx_on f hf s y₀ h₀ 0 x = y₀ :=
rfl
lemma approx_on_mem {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (n : ℕ) (x : β) :
approx_on f hf s y₀ h₀ n x ∈ s :=
begin
haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩,
suffices : ∀ n, (nat.cases_on n y₀ (coe ∘ dense_seq s) : α) ∈ s, { apply this },
rintro (_|n),
exacts [h₀, subtype.mem _]
end
@[simp] lemma approx_on_comp {γ : Type*} [measurable_space γ] {f : β → α} (hf : measurable f)
{g : γ → β} (hg : measurable g) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] (n : ℕ) :
approx_on (f ∘ g) (hf.comp hg) s y₀ h₀ n = (approx_on f hf s y₀ h₀ n).comp g hg :=
rfl
lemma tendsto_approx_on {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] {x : β} (hx : f x ∈ closure s) :
tendsto (λ n, approx_on f hf s y₀ h₀ n x) at_top (𝓝 $ f x) :=
begin
haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩,
rw [← @subtype.range_coe _ s, ← image_univ, ← (dense_range_dense_seq s).closure_eq] at hx,
simp only [approx_on, coe_comp],
refine tendsto_nearest_pt (closure_minimal _ is_closed_closure hx),
simp only [nat.range_cases_on, closure_union, range_comp coe],
exact subset.trans (image_closure_subset_closure_image continuous_subtype_coe)
(subset_union_right _ _)
end
lemma edist_approx_on_mono {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (x : β) {m n : ℕ} (h : m ≤ n) :
edist (approx_on f hf s y₀ h₀ n x) (f x) ≤ edist (approx_on f hf s y₀ h₀ m x) (f x) :=
begin
dsimp only [approx_on, coe_comp, (∘)],
exact edist_nearest_pt_le _ _ ((nearest_pt_ind_le _ _ _).trans h)
end
lemma edist_approx_on_le {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (x : β) (n : ℕ) :
edist (approx_on f hf s y₀ h₀ n x) (f x) ≤ edist y₀ (f x) :=
edist_approx_on_mono hf h₀ x (zero_le n)
lemma edist_approx_on_y0_le {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s)
[separable_space s] (x : β) (n : ℕ) :
edist y₀ (approx_on f hf s y₀ h₀ n x) ≤ edist y₀ (f x) + edist y₀ (f x) :=
calc edist y₀ (approx_on f hf s y₀ h₀ n x) ≤
edist y₀ (f x) + edist (approx_on f hf s y₀ h₀ n x) (f x) : edist_triangle_right _ _ _
... ≤ edist y₀ (f x) + edist y₀ (f x) : add_le_add_left (edist_approx_on_le hf h₀ x n) _
end pointwise
/-! ### Lp approximation by simple functions -/
section Lp
variables [measurable_space β]
variables [measurable_space E] [normed_group E] {q : ℝ} {p : ℝ≥0∞}
lemma nnnorm_approx_on_le [opens_measurable_space E] {f : β → E} (hf : measurable f)
{s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (x : β) (n : ℕ) :
∥approx_on f hf s y₀ h₀ n x - f x∥₊ ≤ ∥f x - y₀∥₊ :=
begin
have := edist_approx_on_le hf h₀ x n,
rw edist_comm y₀ at this,
simp only [edist_nndist, nndist_eq_nnnorm] at this,
exact_mod_cast this
end
lemma norm_approx_on_y₀_le [opens_measurable_space E] {f : β → E} (hf : measurable f)
{s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (x : β) (n : ℕ) :
∥approx_on f hf s y₀ h₀ n x - y₀∥ ≤ ∥f x - y₀∥ + ∥f x - y₀∥ :=
begin
have := edist_approx_on_y0_le hf h₀ x n,
repeat { rw [edist_comm y₀, edist_eq_coe_nnnorm_sub] at this },
exact_mod_cast this,
end
lemma norm_approx_on_zero_le [opens_measurable_space E] {f : β → E} (hf : measurable f)
{s : set E} (h₀ : (0 : E) ∈ s) [separable_space s] (x : β) (n : ℕ) :
∥approx_on f hf s 0 h₀ n x∥ ≤ ∥f x∥ + ∥f x∥ :=
begin
have := edist_approx_on_y0_le hf h₀ x n,
simp [edist_comm (0 : E), edist_eq_coe_nnnorm] at this,
exact_mod_cast this,
end
lemma tendsto_approx_on_Lp_snorm [opens_measurable_space E]
{f : β → E} (hf : measurable f) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s]
(hp_ne_top : p ≠ ∞) {μ : measure β} (hμ : ∀ᵐ x ∂μ, f x ∈ closure s)
(hi : snorm (λ x, f x - y₀) p μ < ∞) :
tendsto (λ n, snorm (approx_on f hf s y₀ h₀ n - f) p μ) at_top (𝓝 0) :=
begin
by_cases hp_zero : p = 0,
{ simpa only [hp_zero, snorm_exponent_zero] using tendsto_const_nhds },
have hp : 0 < p.to_real := to_real_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr hp_zero, hp_ne_top⟩,
suffices : tendsto (λ n, ∫⁻ x, ∥approx_on f hf s y₀ h₀ n x - f x∥₊ ^ p.to_real ∂μ) at_top (𝓝 0),
{ simp only [snorm_eq_lintegral_rpow_nnnorm hp_zero hp_ne_top],
convert continuous_rpow_const.continuous_at.tendsto.comp this;
simp [_root_.inv_pos.mpr hp] },
-- We simply check the conditions of the Dominated Convergence Theorem:
-- (1) The function "`p`-th power of distance between `f` and the approximation" is measurable
have hF_meas : ∀ n, measurable (λ x, (∥approx_on f hf s y₀ h₀ n x - f x∥₊ : ℝ≥0∞) ^ p.to_real),
{ simpa only [← edist_eq_coe_nnnorm_sub] using
λ n, (approx_on f hf s y₀ h₀ n).measurable_bind (λ y x, (edist y (f x)) ^ p.to_real)
(λ y, (measurable_edist_right.comp hf).pow_const p.to_real) },
-- (2) The functions "`p`-th power of distance between `f` and the approximation" are uniformly
-- bounded, at any given point, by `λ x, ∥f x - y₀∥ ^ p.to_real`
have h_bound : ∀ n, (λ x, (∥approx_on f hf s y₀ h₀ n x - f x∥₊ : ℝ≥0∞) ^ p.to_real)
≤ᵐ[μ] (λ x, ∥f x - y₀∥₊ ^ p.to_real),
{ exact λ n, eventually_of_forall
(λ x, rpow_le_rpow (coe_mono (nnnorm_approx_on_le hf h₀ x n)) to_real_nonneg) },
-- (3) The bounding function `λ x, ∥f x - y₀∥ ^ p.to_real` has finite integral
have h_fin : ∫⁻ (a : β), ∥f a - y₀∥₊ ^ p.to_real ∂μ ≠ ⊤,
from (lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp_zero hp_ne_top hi).ne,
-- (4) The functions "`p`-th power of distance between `f` and the approximation" tend pointwise
-- to zero
have h_lim : ∀ᵐ (a : β) ∂μ,
tendsto (λ n, (∥approx_on f hf s y₀ h₀ n a - f a∥₊ : ℝ≥0∞) ^ p.to_real) at_top (𝓝 0),
{ filter_upwards [hμ],
intros a ha,
have : tendsto (λ n, (approx_on f hf s y₀ h₀ n) a - f a) at_top (𝓝 (f a - f a)),
{ exact (tendsto_approx_on hf h₀ ha).sub tendsto_const_nhds },
convert continuous_rpow_const.continuous_at.tendsto.comp (tendsto_coe.mpr this.nnnorm),
simp [zero_rpow_of_pos hp] },
-- Then we apply the Dominated Convergence Theorem
simpa using tendsto_lintegral_of_dominated_convergence _ hF_meas h_bound h_fin h_lim,
end
lemma mem_ℒp_approx_on [borel_space E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : mem_ℒp f p μ) {s : set E} {y₀ : E}
(h₀ : y₀ ∈ s) [separable_space s] (hi₀ : mem_ℒp (λ x, y₀) p μ) (n : ℕ) :
mem_ℒp (approx_on f fmeas s y₀ h₀ n) p μ :=
begin
refine ⟨(approx_on f fmeas s y₀ h₀ n).ae_measurable, _⟩,
suffices : snorm (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ < ⊤,
{ have : mem_ℒp (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ :=
⟨(approx_on f fmeas s y₀ h₀ n - const β y₀).ae_measurable, this⟩,
convert snorm_add_lt_top this hi₀,
ext x,
simp },
-- We don't necessarily have `mem_ℒp (λ x, f x - y₀) p μ`, because the `ae_measurable` part
-- requires `ae_measurable.add`, which requires second-countability
have hf' : mem_ℒp (λ x, ∥f x - y₀∥) p μ,
{ have h_meas : measurable (λ x, ∥f x - y₀∥),
{ simp only [← dist_eq_norm],
exact (continuous_id.dist continuous_const).measurable.comp fmeas },
refine ⟨h_meas.ae_measurable, _⟩,
rw snorm_norm,
convert snorm_add_lt_top hf hi₀.neg,
ext x,
simp [sub_eq_add_neg] },
have : ∀ᵐ x ∂μ, ∥approx_on f fmeas s y₀ h₀ n x - y₀∥ ≤ ∥(∥f x - y₀∥ + ∥f x - y₀∥)∥,
{ refine eventually_of_forall _,
intros x,
convert norm_approx_on_y₀_le fmeas h₀ x n,
rw [real.norm_eq_abs, abs_of_nonneg],
exact add_nonneg (norm_nonneg _) (norm_nonneg _) },
calc snorm (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ
≤ snorm (λ x, ∥f x - y₀∥ + ∥f x - y₀∥) p μ : snorm_mono_ae this
... < ⊤ : snorm_add_lt_top hf' hf',
end
lemma tendsto_approx_on_univ_Lp_snorm [opens_measurable_space E] [second_countable_topology E]
{f : β → E} (hp_ne_top : p ≠ ∞) {μ : measure β} (fmeas : measurable f) (hf : snorm f p μ < ∞) :
tendsto (λ n, snorm (approx_on f fmeas univ 0 trivial n - f) p μ) at_top (𝓝 0) :=
tendsto_approx_on_Lp_snorm fmeas trivial hp_ne_top (by simp) (by simpa using hf)
lemma mem_ℒp_approx_on_univ [borel_space E] [second_countable_topology E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : mem_ℒp f p μ) (n : ℕ) :
mem_ℒp (approx_on f fmeas univ 0 trivial n) p μ :=
mem_ℒp_approx_on fmeas hf (mem_univ _) zero_mem_ℒp n
lemma tendsto_approx_on_univ_Lp [borel_space E] [second_countable_topology E]
{f : β → E} [hp : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) {μ : measure β} (fmeas : measurable f)
(hf : mem_ℒp f p μ) :
tendsto (λ n, (mem_ℒp_approx_on_univ fmeas hf n).to_Lp (approx_on f fmeas univ 0 trivial n))
at_top (𝓝 (hf.to_Lp f)) :=
by simp [Lp.tendsto_Lp_iff_tendsto_ℒp'', tendsto_approx_on_univ_Lp_snorm hp_ne_top fmeas hf.2]
end Lp
/-! ### L1 approximation by simple functions -/
section integrable
variables [measurable_space β]
variables [measurable_space E] [normed_group E]
lemma tendsto_approx_on_L1_nnnorm [opens_measurable_space E]
{f : β → E} (hf : measurable f) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s]
{μ : measure β} (hμ : ∀ᵐ x ∂μ, f x ∈ closure s) (hi : has_finite_integral (λ x, f x - y₀) μ) :
tendsto (λ n, ∫⁻ x, ∥approx_on f hf s y₀ h₀ n x - f x∥₊ ∂μ) at_top (𝓝 0) :=
by simpa [snorm_one_eq_lintegral_nnnorm] using tendsto_approx_on_Lp_snorm hf h₀ one_ne_top hμ
(by simpa [snorm_one_eq_lintegral_nnnorm] using hi)
lemma integrable_approx_on [borel_space E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : integrable f μ)
{s : set E} {y₀ : E} (h₀ : y₀ ∈ s)
[separable_space s] (hi₀ : integrable (λ x, y₀) μ) (n : ℕ) :
integrable (approx_on f fmeas s y₀ h₀ n) μ :=
begin
rw ← mem_ℒp_one_iff_integrable at hf hi₀ ⊢,
exact mem_ℒp_approx_on fmeas hf h₀ hi₀ n,
end
lemma tendsto_approx_on_univ_L1_nnnorm [opens_measurable_space E] [second_countable_topology E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : integrable f μ) :
tendsto (λ n, ∫⁻ x, ∥approx_on f fmeas univ 0 trivial n x - f x∥₊ ∂μ) at_top (𝓝 0) :=
tendsto_approx_on_L1_nnnorm fmeas trivial (by simp) (by simpa using hf.2)
lemma integrable_approx_on_univ [borel_space E] [second_countable_topology E]
{f : β → E} {μ : measure β} (fmeas : measurable f) (hf : integrable f μ) (n : ℕ) :
integrable (approx_on f fmeas univ 0 trivial n) μ :=
integrable_approx_on fmeas hf _ (integrable_zero _ _ _) n
end integrable
section simple_func_properties
variables [measurable_space α]
variables [normed_group E] [measurable_space E] [normed_group F]
variables {μ : measure α} {p : ℝ≥0∞}
/-!
### Properties of simple functions in `Lp` spaces
A simple function `f : α →ₛ E` into a normed group `E` verifies, for a measure `μ`:
- `mem_ℒp f 0 μ` and `mem_ℒp f ∞ μ`, since `f` is a.e.-measurable and bounded,
- for `0 < p < ∞`,
`mem_ℒp f p μ ↔ integrable f μ ↔ f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞`.
-/
lemma exists_forall_norm_le (f : α →ₛ F) : ∃ C, ∀ x, ∥f x∥ ≤ C :=
exists_forall_le (f.map (λ x, ∥x∥))
lemma mem_ℒp_zero (f : α →ₛ E) (μ : measure α) : mem_ℒp f 0 μ :=
mem_ℒp_zero_iff_ae_measurable.mpr f.ae_measurable
lemma mem_ℒp_top (f : α →ₛ E) (μ : measure α) : mem_ℒp f ∞ μ :=
let ⟨C, hfC⟩ := f.exists_forall_norm_le in
mem_ℒp_top_of_bound f.ae_measurable C $ eventually_of_forall hfC
protected lemma snorm'_eq {p : ℝ} (f : α →ₛ F) (μ : measure α) :
snorm' f p μ = (∑ y in f.range, (nnnorm y : ℝ≥0∞) ^ p * μ (f ⁻¹' {y})) ^ (1/p) :=
have h_map : (λ a, (nnnorm (f a) : ℝ≥0∞) ^ p) = f.map (λ a : F, (nnnorm a : ℝ≥0∞) ^ p), by simp,
by rw [snorm', h_map, lintegral_eq_lintegral, map_lintegral]
lemma measure_preimage_lt_top_of_mem_ℒp (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) (f : α →ₛ E)
(hf : mem_ℒp f p μ) (y : E) (hy_ne : y ≠ 0) :
μ (f ⁻¹' {y}) < ∞ :=
begin
have hp_pos_real : 0 < p.to_real, from ennreal.to_real_pos_iff.mpr ⟨hp_pos, hp_ne_top⟩,
have hf_snorm := mem_ℒp.snorm_lt_top hf,
rw [snorm_eq_snorm' hp_pos.ne.symm hp_ne_top, f.snorm'_eq,
← @ennreal.lt_rpow_one_div_iff _ _ (1 / p.to_real) (by simp [hp_pos_real]),
@ennreal.top_rpow_of_pos (1 / (1 / p.to_real)) (by simp [hp_pos_real]),
ennreal.sum_lt_top_iff] at hf_snorm,
by_cases hyf : y ∈ f.range,
swap,
{ suffices h_empty : f ⁻¹' {y} = ∅,
by { rw [h_empty, measure_empty], exact ennreal.coe_lt_top, },
ext1 x,
rw [set.mem_preimage, set.mem_singleton_iff, mem_empty_eq, iff_false],
refine λ hxy, hyf _,
rw [mem_range, set.mem_range],
exact ⟨x, hxy⟩, },
specialize hf_snorm y hyf,
rw ennreal.mul_lt_top_iff at hf_snorm,
cases hf_snorm,
{ exact hf_snorm.2, },
cases hf_snorm,
{ refine absurd _ hy_ne,
simpa [hp_pos_real] using hf_snorm, },
{ simp [hf_snorm], },
end
lemma mem_ℒp_of_finite_measure_preimage (p : ℝ≥0∞) {f : α →ₛ E} (hf : ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞) :
mem_ℒp f p μ :=
begin
by_cases hp0 : p = 0,
{ rw [hp0, mem_ℒp_zero_iff_ae_measurable], exact f.ae_measurable, },
by_cases hp_top : p = ∞,
{ rw hp_top, exact mem_ℒp_top f μ, },
refine ⟨f.ae_measurable, _⟩,
rw [snorm_eq_snorm' hp0 hp_top, f.snorm'_eq],
refine ennreal.rpow_lt_top_of_nonneg (by simp) (ennreal.sum_lt_top_iff.mpr (λ y hy, _)).ne,
by_cases hy0 : y = 0,
{ simp [hy0, ennreal.to_real_pos_iff.mpr ⟨lt_of_le_of_ne (zero_le _) (ne.symm hp0), hp_top⟩], },
{ refine ennreal.mul_lt_top _ (hf y hy0).ne,
exact (ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg ennreal.coe_ne_top).ne },
end
lemma mem_ℒp_iff {f : α →ₛ E} (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) :
mem_ℒp f p μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ :=
⟨λ h, measure_preimage_lt_top_of_mem_ℒp hp_pos hp_ne_top f h,
λ h, mem_ℒp_of_finite_measure_preimage p h⟩
lemma integrable_iff {f : α →ₛ E} : integrable f μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ :=
mem_ℒp_one_iff_integrable.symm.trans $ mem_ℒp_iff ennreal.zero_lt_one ennreal.coe_ne_top
lemma mem_ℒp_iff_integrable {f : α →ₛ E} (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) :
mem_ℒp f p μ ↔ integrable f μ :=
(mem_ℒp_iff hp_pos hp_ne_top).trans integrable_iff.symm
lemma mem_ℒp_iff_fin_meas_supp {f : α →ₛ E} (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) :
mem_ℒp f p μ ↔ f.fin_meas_supp μ :=
(mem_ℒp_iff hp_pos hp_ne_top).trans fin_meas_supp_iff.symm
lemma integrable_iff_fin_meas_supp {f : α →ₛ E} : integrable f μ ↔ f.fin_meas_supp μ :=
integrable_iff.trans fin_meas_supp_iff.symm
lemma fin_meas_supp.integrable {f : α →ₛ E} (h : f.fin_meas_supp μ) : integrable f μ :=
integrable_iff_fin_meas_supp.2 h
lemma integrable_pair [measurable_space F] {f : α →ₛ E} {g : α →ₛ F} :
integrable f μ → integrable g μ → integrable (pair f g) μ :=
by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair
lemma mem_ℒp_of_is_finite_measure (f : α →ₛ E) (p : ℝ≥0∞) (μ : measure α) [is_finite_measure μ] :
mem_ℒp f p μ :=
let ⟨C, hfC⟩ := f.exists_forall_norm_le in
mem_ℒp.of_bound f.ae_measurable C $ eventually_of_forall hfC
lemma integrable_of_is_finite_measure [is_finite_measure μ] (f : α →ₛ E) : integrable f μ :=
mem_ℒp_one_iff_integrable.mp (f.mem_ℒp_of_is_finite_measure 1 μ)
lemma measure_preimage_lt_top_of_integrable (f : α →ₛ E) (hf : integrable f μ) {x : E}
(hx : x ≠ 0) :
μ (f ⁻¹' {x}) < ∞ :=
integrable_iff.mp hf x hx
lemma measure_support_lt_top [has_zero β] (f : α →ₛ β) (hf : ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞) :
μ (support f) < ∞ :=
begin
rw support_eq,
refine (measure_bUnion_finset_le _ _).trans_lt (ennreal.sum_lt_top_iff.mpr (λ y hy, _)),
rw finset.mem_filter at hy,
exact hf y hy.2,
end
lemma measure_support_lt_top_of_mem_ℒp (f : α →ₛ E) (hf : mem_ℒp f p μ) (hp_ne_zero : p ≠ 0)
(hp_ne_top : p ≠ ∞) :
μ (support f) < ∞ :=
f.measure_support_lt_top ((mem_ℒp_iff (pos_iff_ne_zero.mpr hp_ne_zero) hp_ne_top).mp hf)
lemma measure_support_lt_top_of_integrable (f : α →ₛ E) (hf : integrable f μ) :
μ (support f) < ∞ :=
f.measure_support_lt_top (integrable_iff.mp hf)
lemma measure_lt_top_of_mem_ℒp_indicator (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) {c : E} (hc : c ≠ 0)
{s : set α} (hs : measurable_set s)
(hcs : mem_ℒp ((const α c).piecewise s hs (const α 0)) p μ) :
μ s < ⊤ :=
begin
have : function.support (const α c) = set.univ := function.support_const hc,
simpa only [mem_ℒp_iff_fin_meas_supp hp_pos hp_ne_top, fin_meas_supp_iff_support,
support_indicator, set.inter_univ, this] using hcs
end
end simple_func_properties
end simple_func
/-! Construction of the space of `Lp` simple functions, and its dense embedding into `Lp`. -/
namespace Lp
open ae_eq_fun
variables
[measurable_space α]
[normed_group E] [second_countable_topology E] [measurable_space E] [borel_space E]
[normed_group F] [second_countable_topology F] [measurable_space F] [borel_space F]
(p : ℝ≥0∞) (μ : measure α)
variables (E)
/-- `Lp.simple_func` is a subspace of Lp consisting of equivalence classes of an integrable simple
function. -/
def simple_func : add_subgroup (Lp E p μ) :=
{ carrier := {f : Lp E p μ | ∃ (s : α →ₛ E), (ae_eq_fun.mk s s.ae_measurable : α →ₘ[μ] E) = f},
zero_mem' := ⟨0, rfl⟩,
add_mem' := λ f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t,
by simp only [←hs, ←ht, mk_add_mk, add_subgroup.coe_add, mk_eq_mk, simple_func.coe_add]⟩,
neg_mem' := λ f ⟨s, hs⟩, ⟨-s,
by simp only [←hs, neg_mk, simple_func.coe_neg, mk_eq_mk, add_subgroup.coe_neg]⟩ }
variables {E p μ}
namespace simple_func
section instances
/-! Simple functions in Lp space form a `normed_space`. -/
@[norm_cast] lemma coe_coe (f : Lp.simple_func E p μ) : ⇑(f : Lp E p μ) = f := rfl
protected lemma eq' {f g : Lp.simple_func E p μ} : (f : α →ₘ[μ] E) = (g : α →ₘ[μ] E) → f = g :=
subtype.eq ∘ subtype.eq
/-! Implementation note: If `Lp.simple_func E p μ` were defined as a `𝕜`-submodule of `Lp E p μ`,
then the next few lemmas, putting a normed `𝕜`-group structure on `Lp.simple_func E p μ`, would be
unnecessary. But instead, `Lp.simple_func E p μ` is defined as an `add_subgroup` of `Lp E p μ`,
which does not permit this (but has the advantage of working when `E` itself is a normed group,
i.e. has no scalar action). -/
variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜]
/-- If `E` is a normed space, `Lp.simple_func E p μ` is a `has_scalar`. Not declared as an
instance as it is (as of writing) used only in the construction of the Bochner integral. -/
protected def has_scalar : has_scalar 𝕜 (Lp.simple_func E p μ) := ⟨λk f, ⟨k • f,
begin
rcases f with ⟨f, ⟨s, hs⟩⟩,
use k • s,
apply eq.trans (smul_mk k s s.ae_measurable).symm _,
rw hs,
refl,
end ⟩⟩
local attribute [instance] simple_func.has_scalar
@[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : Lp.simple_func E p μ) :
((c • f : Lp.simple_func E p μ) : Lp E p μ) = c • (f : Lp E p μ) := rfl
/-- If `E` is a normed space, `Lp.simple_func E p μ` is a module. Not declared as an
instance as it is (as of writing) used only in the construction of the Bochner integral. -/
protected def module : module 𝕜 (Lp.simple_func E p μ) :=
{ one_smul := λf, by { ext1, exact one_smul _ _ },
mul_smul := λx y f, by { ext1, exact mul_smul _ _ _ },
smul_add := λx f g, by { ext1, exact smul_add _ _ _ },
smul_zero := λx, by { ext1, exact smul_zero _ },
add_smul := λx y f, by { ext1, exact add_smul _ _ _ },
zero_smul := λf, by { ext1, exact zero_smul _ _ } }
local attribute [instance] simple_func.module
/-- If `E` is a normed space, `Lp.simple_func E p μ` is a normed space. Not declared as an
instance as it is (as of writing) used only in the construction of the Bochner integral. -/
protected def normed_space [fact (1 ≤ p)] : normed_space 𝕜 (Lp.simple_func E p μ) :=
⟨ λc f, by { rw [coe_norm_subgroup, coe_norm_subgroup, coe_smul, norm_smul] } ⟩
end instances
local attribute [instance] simple_func.module simple_func.normed_space
section to_Lp
/-- Construct the equivalence class `[f]` of a simple function `f` satisfying `mem_ℒp`. -/
@[reducible] def to_Lp (f : α →ₛ E) (hf : mem_ℒp f p μ) : (Lp.simple_func E p μ) :=
⟨hf.to_Lp f, ⟨f, rfl⟩⟩
lemma to_Lp_eq_to_Lp (f : α →ₛ E) (hf : mem_ℒp f p μ) :
(to_Lp f hf : Lp E p μ) = hf.to_Lp f := rfl
lemma to_Lp_eq_mk (f : α →ₛ E) (hf : mem_ℒp f p μ) :
(to_Lp f hf : α →ₘ[μ] E) = ae_eq_fun.mk f f.ae_measurable := rfl
lemma to_Lp_zero : to_Lp (0 : α →ₛ E) zero_mem_ℒp = (0 : Lp.simple_func E p μ) := rfl
lemma to_Lp_add (f g : α →ₛ E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) :
to_Lp (f + g) (hf.add hg) = to_Lp f hf + to_Lp g hg := rfl
lemma to_Lp_neg (f : α →ₛ E) (hf : mem_ℒp f p μ) :
to_Lp (-f) hf.neg = -to_Lp f hf := rfl
lemma to_Lp_sub (f g : α →ₛ E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) :
to_Lp (f - g) (hf.sub hg) = to_Lp f hf - to_Lp g hg :=
by { simp only [sub_eq_add_neg, ← to_Lp_neg, ← to_Lp_add], refl }
variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜]
lemma to_Lp_smul (f : α →ₛ E) (hf : mem_ℒp f p μ) (c : 𝕜) :
to_Lp (c • f) (hf.const_smul c) = c • to_Lp f hf := rfl
lemma norm_to_Lp [fact (1 ≤ p)] (f : α →ₛ E) (hf : mem_ℒp f p μ) :
∥to_Lp f hf∥ = ennreal.to_real (snorm f p μ) :=
norm_to_Lp f hf
end to_Lp
section to_simple_func
/-- Find a representative of a `Lp.simple_func`. -/
def to_simple_func (f : Lp.simple_func E p μ) : α →ₛ E := classical.some f.2
/-- `(to_simple_func f)` is measurable. -/
@[measurability]
protected lemma measurable (f : Lp.simple_func E p μ) : measurable (to_simple_func f) :=
(to_simple_func f).measurable
@[measurability]
protected lemma ae_measurable (f : Lp.simple_func E p μ) : ae_measurable (to_simple_func f) μ :=
(simple_func.measurable f).ae_measurable
lemma to_simple_func_eq_to_fun (f : Lp.simple_func E p μ) : to_simple_func f =ᵐ[μ] f :=
show ⇑(to_simple_func f) =ᵐ[μ] ⇑(f : α →ₘ[μ] E), by
begin
convert (ae_eq_fun.coe_fn_mk (to_simple_func f) (simple_func.ae_measurable f)).symm using 2,
exact (classical.some_spec f.2).symm,
end
/-- `to_simple_func f` satisfies the predicate `mem_ℒp`. -/
protected lemma mem_ℒp (f : Lp.simple_func E p μ) : mem_ℒp (to_simple_func f) p μ :=
mem_ℒp.ae_eq (to_simple_func_eq_to_fun f).symm $ mem_Lp_iff_mem_ℒp.mp (f : Lp E p μ).2
lemma to_Lp_to_simple_func (f : Lp.simple_func E p μ) :
to_Lp (to_simple_func f) (simple_func.mem_ℒp f) = f :=
simple_func.eq' (classical.some_spec f.2)
lemma to_simple_func_to_Lp (f : α →ₛ E) (hfi : mem_ℒp f p μ) :
to_simple_func (to_Lp f hfi) =ᵐ[μ] f :=
by { rw ← mk_eq_mk, exact classical.some_spec (to_Lp f hfi).2 }
variables (E μ)
lemma zero_to_simple_func : to_simple_func (0 : Lp.simple_func E p μ) =ᵐ[μ] 0 :=
begin
filter_upwards [to_simple_func_eq_to_fun (0 : Lp.simple_func E p μ), Lp.coe_fn_zero E 1 μ],
assume a h₁ h₂,
rwa h₁,
end
variables {E μ}
lemma add_to_simple_func (f g : Lp.simple_func E p μ) :
to_simple_func (f + g) =ᵐ[μ] to_simple_func f + to_simple_func g :=
begin
filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f,
to_simple_func_eq_to_fun g, Lp.coe_fn_add (f : Lp E p μ) g],
assume a,
simp only [← coe_coe, add_subgroup.coe_add, pi.add_apply],
iterate 4 { assume h, rw h }
end
lemma neg_to_simple_func (f : Lp.simple_func E p μ) :
to_simple_func (-f) =ᵐ[μ] - to_simple_func f :=
begin
filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f,
Lp.coe_fn_neg (f : Lp E p μ)],
assume a,
simp only [pi.neg_apply, add_subgroup.coe_neg, ← coe_coe],
repeat { assume h, rw h }
end
lemma sub_to_simple_func (f g : Lp.simple_func E p μ) :
to_simple_func (f - g) =ᵐ[μ] to_simple_func f - to_simple_func g :=
begin
filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f,
to_simple_func_eq_to_fun g, Lp.coe_fn_sub (f : Lp E p μ) g],
assume a,
simp only [add_subgroup.coe_sub, pi.sub_apply, ← coe_coe],
repeat { assume h, rw h }
end
variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜]
lemma smul_to_simple_func (k : 𝕜) (f : Lp.simple_func E p μ) :
to_simple_func (k • f) =ᵐ[μ] k • to_simple_func f :=
begin
filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f,
Lp.coe_fn_smul k (f : Lp E p μ)],
assume a,
simp only [pi.smul_apply, coe_smul, ← coe_coe],
repeat { assume h, rw h }
end
lemma norm_to_simple_func [fact (1 ≤ p)] (f : Lp.simple_func E p μ) :
∥f∥ = ennreal.to_real (snorm (to_simple_func f) p μ) :=
by simpa [to_Lp_to_simple_func] using norm_to_Lp (to_simple_func f) (simple_func.mem_ℒp f)
end to_simple_func
section induction
variables (p)
/-- The characteristic function of a finite-measure measurable set `s`, as an `Lp` simple function.
-/
def indicator_const {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) :
Lp.simple_func E p μ :=
to_Lp ((simple_func.const _ c).piecewise s hs (simple_func.const _ 0))
(mem_ℒp_indicator_const p hs c (or.inr hμs))
variables {p}
@[simp] lemma coe_indicator_const {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) :
(↑(indicator_const p hs hμs c) : Lp E p μ) = indicator_const_Lp p hs hμs c :=
rfl
lemma to_simple_func_indicator_const {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) :
to_simple_func (indicator_const p hs hμs c)
=ᵐ[μ] (simple_func.const _ c).piecewise s hs (simple_func.const _ 0) :=
Lp.simple_func.to_simple_func_to_Lp _ _
/-- To prove something for an arbitrary `Lp` simple function, with `0 < p < ∞`, it suffices to show
that the property holds for (multiples of) characteristic functions of finite-measure measurable
sets and is closed under addition (of functions with disjoint support). -/
@[elab_as_eliminator]
protected lemma induction (hp_pos : 0 < p) (hp_ne_top : p ≠ ∞) {P : Lp.simple_func E p μ → Prop}
(h_ind : ∀ (c : E) {s : set α} (hs : measurable_set s) (hμs : μ s < ∞),
P (Lp.simple_func.indicator_const p hs hμs.ne c))
(h_add : ∀ ⦃f g : α →ₛ E⦄, ∀ hf : mem_ℒp f p μ, ∀ hg : mem_ℒp g p μ,
disjoint (support f) (support g) → P (Lp.simple_func.to_Lp f hf)
→ P (Lp.simple_func.to_Lp g hg) → P (Lp.simple_func.to_Lp f hf + Lp.simple_func.to_Lp g hg))
(f : Lp.simple_func E p μ) : P f :=
begin
suffices : ∀ f : α →ₛ E, ∀ hf : mem_ℒp f p μ, P (to_Lp f hf),
{ rw ← to_Lp_to_simple_func f,
apply this }, clear f,
refine simple_func.induction _ _,
{ intros c s hs hf,
by_cases hc : c = 0,
{ convert h_ind 0 measurable_set.empty (by simp) using 1,
ext1,
simp [hc] },
exact h_ind c hs (simple_func.measure_lt_top_of_mem_ℒp_indicator hp_pos hp_ne_top hc hs hf) },
{ intros f g hfg hf hg hfg',
obtain ⟨hf', hg'⟩ : mem_ℒp f p μ ∧ mem_ℒp g p μ,
{ exact (mem_ℒp_add_of_disjoint hfg f.measurable g.measurable).mp hfg' },
exact h_add hf' hg' hfg (hf hf') (hg hg') },
end
end induction
section coe_to_Lp
variables [fact (1 ≤ p)]
protected lemma uniform_continuous :
uniform_continuous (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
uniform_continuous_comap
protected lemma uniform_embedding :
uniform_embedding (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
uniform_embedding_comap subtype.val_injective
protected lemma uniform_inducing : uniform_inducing (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
simple_func.uniform_embedding.to_uniform_inducing
protected lemma dense_embedding (hp_ne_top : p ≠ ∞) :
dense_embedding (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
begin
apply simple_func.uniform_embedding.dense_embedding,
assume f,
rw mem_closure_iff_seq_limit,
have hfi' : mem_ℒp f p μ := Lp.mem_ℒp f,
refine ⟨λ n, ↑(to_Lp (simple_func.approx_on f (Lp.measurable f) univ 0 trivial n)
(simple_func.mem_ℒp_approx_on_univ (Lp.measurable f) hfi' n)), λ n, mem_range_self _, _⟩,
convert simple_func.tendsto_approx_on_univ_Lp hp_ne_top (Lp.measurable f) hfi',
rw to_Lp_coe_fn f (Lp.mem_ℒp f)
end
protected lemma dense_inducing (hp_ne_top : p ≠ ∞) :
dense_inducing (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
(simple_func.dense_embedding hp_ne_top).to_dense_inducing
protected lemma dense_range (hp_ne_top : p ≠ ∞) :
dense_range (coe : (Lp.simple_func E p μ) → (Lp E p μ)) :=
(simple_func.dense_inducing hp_ne_top).dense
variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜]
variables (α E 𝕜)
/-- The embedding of Lp simple functions into Lp functions, as a continuous linear map. -/
def coe_to_Lp : (Lp.simple_func E p μ) →L[𝕜] (Lp E p μ) :=
{ map_smul' := λk f, rfl,
cont := Lp.simple_func.uniform_continuous.continuous,
.. add_subgroup.subtype (Lp.simple_func E p μ) }
variables {α E 𝕜}
end coe_to_Lp
end simple_func
end Lp
variables [measurable_space α] [normed_group E] [measurable_space E] [borel_space E]
[second_countable_topology E] {f : α → E} {p : ℝ≥0∞} {μ : measure α}
/-- To prove something for an arbitrary `Lp` function in a second countable Borel normed group, it
suffices to show that
* the property holds for (multiples of) characteristic functions;
* is closed under addition;
* the set of functions in `Lp` for which the property holds is closed.
-/
@[elab_as_eliminator]
lemma Lp.induction [_i : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) (P : Lp E p μ → Prop)
(h_ind : ∀ (c : E) {s : set α} (hs : measurable_set s) (hμs : μ s < ∞),
P (Lp.simple_func.indicator_const p hs hμs.ne c))
(h_add : ∀ ⦃f g⦄, ∀ hf : mem_ℒp f p μ, ∀ hg : mem_ℒp g p μ, disjoint (support f) (support g) →
P (hf.to_Lp f) → P (hg.to_Lp g) → P ((hf.to_Lp f) + (hg.to_Lp g)))
(h_closed : is_closed {f : Lp E p μ | P f}) :
∀ f : Lp E p μ, P f :=
begin
refine λ f, (Lp.simple_func.dense_range hp_ne_top).induction_on f h_closed _,
refine Lp.simple_func.induction (lt_of_lt_of_le ennreal.zero_lt_one _i.elim) hp_ne_top _ _,
{ exact λ c s, h_ind c },
{ exact λ f g hf hg, h_add hf hg },
end
/-- To prove something for an arbitrary `mem_ℒp` function in a second countable
Borel normed group, it suffices to show that
* the property holds for (multiples of) characteristic functions;
* is closed under addition;
* the set of functions in the `Lᵖ` space for which the property holds is closed.
* the property is closed under the almost-everywhere equal relation.
It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions
can be added once we need them (for example in `h_add` it is only necessary to consider the sum of
a simple function with a multiple of a characteristic function and that the intersection
of their images is a subset of `{0}`).
-/
@[elab_as_eliminator]
lemma mem_ℒp.induction [_i : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) (P : (α → E) → Prop)
(h_ind : ∀ (c : E) ⦃s⦄, measurable_set s → μ s < ∞ → P (s.indicator (λ _, c)))
(h_add : ∀ ⦃f g : α → E⦄, disjoint (support f) (support g) → mem_ℒp f p μ → mem_ℒp g p μ →
P f → P g → P (f + g))
(h_closed : is_closed {f : Lp E p μ | P f} )
(h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → mem_ℒp f p μ → P f → P g) :
∀ ⦃f : α → E⦄ (hf : mem_ℒp f p μ), P f :=
begin
have : ∀ (f : simple_func α E), mem_ℒp f p μ → P f,
{ refine simple_func.induction _ _,
{ intros c s hs h,
by_cases hc : c = 0,
{ subst hc, convert h_ind 0 measurable_set.empty (by simp) using 1, ext, simp [const] },
have hp_pos : 0 < p := lt_of_lt_of_le ennreal.zero_lt_one _i.elim,
exact h_ind c hs (simple_func.measure_lt_top_of_mem_ℒp_indicator hp_pos hp_ne_top hc hs h) },
{ intros f g hfg hf hg int_fg,
rw [simple_func.coe_add, mem_ℒp_add_of_disjoint hfg f.measurable g.measurable] at int_fg,
refine h_add hfg int_fg.1 int_fg.2 (hf int_fg.1) (hg int_fg.2) } },
have : ∀ (f : Lp.simple_func E p μ), P f,
{ intro f,
exact h_ae (Lp.simple_func.to_simple_func_eq_to_fun f) (Lp.simple_func.mem_ℒp f)
(this (Lp.simple_func.to_simple_func f) (Lp.simple_func.mem_ℒp f)) },
have : ∀ (f : Lp E p μ), P f :=
λ f, (Lp.simple_func.dense_range hp_ne_top).induction_on f h_closed this,
exact λ f hf, h_ae hf.coe_fn_to_Lp (Lp.mem_ℒp _) (this (hf.to_Lp f)),
end
section integrable
local attribute [instance] fact_one_le_one_ennreal
notation α ` →₁ₛ[`:25 μ `] ` E := @measure_theory.Lp.simple_func α E _ _ _ _ _ 1 μ
lemma L1.simple_func.to_Lp_one_eq_to_L1 (f : α →ₛ E) (hf : integrable f μ) :
(Lp.simple_func.to_Lp f (mem_ℒp_one_iff_integrable.2 hf) : α →₁[μ] E) = hf.to_L1 f :=
rfl
protected lemma L1.simple_func.integrable (f : α →₁ₛ[μ] E) :
integrable (Lp.simple_func.to_simple_func f) μ :=
by { rw ← mem_ℒp_one_iff_integrable, exact (Lp.simple_func.mem_ℒp f) }
/-- To prove something for an arbitrary integrable function in a second countable
Borel normed group, it suffices to show that
* the property holds for (multiples of) characteristic functions;
* is closed under addition;
* the set of functions in the `L¹` space for which the property holds is closed.
* the property is closed under the almost-everywhere equal relation.
It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions
can be added once we need them (for example in `h_add` it is only necessary to consider the sum of
a simple function with a multiple of a characteristic function and that the intersection
of their images is a subset of `{0}`).
-/
@[elab_as_eliminator]
lemma integrable.induction (P : (α → E) → Prop)
(h_ind : ∀ (c : E) ⦃s⦄, measurable_set s → μ s < ∞ → P (s.indicator (λ _, c)))
(h_add : ∀ ⦃f g : α → E⦄, disjoint (support f) (support g) → integrable f μ → integrable g μ →
P f → P g → P (f + g))
(h_closed : is_closed {f : α →₁[μ] E | P f} )
(h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → integrable f μ → P f → P g) :
∀ ⦃f : α → E⦄ (hf : integrable f μ), P f :=
begin
simp only [← mem_ℒp_one_iff_integrable] at *,
exact mem_ℒp.induction one_ne_top P h_ind h_add h_closed h_ae
end
end integrable
end measure_theory
|
93f0b0c67a3314b3337193cedc846a1df1d8339e | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/set_theory/surreal.lean | cff8331ee6ad236634f008bf12f98d76f914abea | [
"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 | 14,948 | lean | /-
Copyright (c) 2019 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Scott Morrison
-/
import set_theory.pgame
/-!
# Surreal numbers
The basic theory of surreal numbers, built on top of the theory of combinatorial (pre-)games.
A pregame is `numeric` if all the Left options are strictly smaller than all the Right options,
and all those options are themselves numeric. In terms of combinatorial games, the
numeric games have "frozen"; you can only make your position worse by playing, and Left is some
definite "number" of moves ahead (or behind) Right.
A surreal number is an equivalence class of numeric pregames.
In fact, the surreals form a complete ordered field, containing a copy of the reals (and much else besides!)
but we do not yet have a complete development.
## Order properties
Surreal numbers inherit the relations `≤` and `<` from games, and these relations
satisfy the axioms of a partial order (recall that `x < y ↔ x ≤ y ∧ ¬ y ≤ x` did not hold for games).
## Algebraic operations
At this point, we have defined addition and negation (from pregames), and shown that surreals
form an additive semigroup. It would be very little work to finish showing that the surreals form
an ordered commutative group.
We define the operations of multiplication and inverse on surreals, but do not yet establish any of the
necessary properties to show the surreals form an ordered field.
## Embeddings
It would be nice projects to define the group homomorphism `surreal → game`, and also
`ℤ → surreal`, and then the homomorphic inclusion of the dyadic rationals into surreals, and finally
via dyadic Dedekind cuts the homomorphic inclusion of the reals into the surreals.
One can also map all the cardinals into the surreals!
## References
* [Conway, *On numbers and games*][conway2001]
-/
universes u
namespace pgame
/- Multiplicative operations can be defined at the level of pre-games, but as
they are only useful on surreal numbers, we define them here. -/
/-- The product of `x = {xL | xR}` and `y = {yL | yR}` is
`{xL*y + x*yL - xL*yL, xR*y + x*yR - xR*yR | xL*y + x*yR - xL*yR, x*yL + xR*y - xR*yL }`. -/
def mul (x y : pgame) : pgame :=
begin
induction x with xl xr xL xR IHxl IHxr generalizing y,
induction y with yl yr yL yR IHyl IHyr,
have y := mk yl yr yL yR,
refine ⟨xl × yl ⊕ xr × yr, xl × yr ⊕ xr × yl, _, _⟩; rintro (⟨i, j⟩ | ⟨i, j⟩),
{ exact IHxl i y + IHyl j - IHxl i (yL j) },
{ exact IHxr i y + IHyr j - IHxr i (yR j) },
{ exact IHxl i y + IHyr j - IHxl i (yR j) },
{ exact IHxr i y + IHyl j - IHxr i (yL j) }
end
instance : has_mul pgame := ⟨mul⟩
/-- Because the two halves of the definition of inv produce more elements
of each side, we have to define the two families inductively.
This is the indexing set for the function, and `inv_val` is the function part. -/
inductive inv_ty (l r : Type u) : bool → Type u
| zero {} : inv_ty ff
| left₁ : r → inv_ty ff → inv_ty ff
| left₂ : l → inv_ty tt → inv_ty ff
| right₁ : l → inv_ty ff → inv_ty tt
| right₂ : r → inv_ty tt → inv_ty tt
/-- Because the two halves of the definition of inv produce more elements
of each side, we have to define the two families inductively.
This is the function part, defined by recursion on `inv_ty`. -/
def inv_val {l r} (L : l → pgame) (R : r → pgame)
(IHl : l → pgame) (IHr : r → pgame) : ∀ {b}, inv_ty l r b → pgame
| _ inv_ty.zero := 0
| _ (inv_ty.left₁ i j) := (1 + (R i - mk l r L R) * inv_val j) * IHr i
| _ (inv_ty.left₂ i j) := (1 + (L i - mk l r L R) * inv_val j) * IHl i
| _ (inv_ty.right₁ i j) := (1 + (L i - mk l r L R) * inv_val j) * IHl i
| _ (inv_ty.right₂ i j) := (1 + (R i - mk l r L R) * inv_val j) * IHr i
/-- The inverse of a positive surreal number `x = {L | R}` is
given by `x⁻¹ = {0,
(1 + (R - x) * x⁻¹L) * R, (1 + (L - x) * x⁻¹R) * L |
(1 + (L - x) * x⁻¹L) * L, (1 + (R - x) * x⁻¹R) * R}`.
Because the two halves `x⁻¹L, x⁻¹R` of `x⁻¹` are used in their own
definition, the sets and elements are inductively generated. -/
def inv' : pgame → pgame
| ⟨l, r, L, R⟩ :=
let l' := {i // 0 < L i},
L' : l' → pgame := λ i, L i.1,
IHl' : l' → pgame := λ i, inv' (L i.1),
IHr := λ i, inv' (R i) in
⟨inv_ty l' r ff, inv_ty l' r tt,
inv_val L' R IHl' IHr, inv_val L' R IHl' IHr⟩
/-- The inverse of a surreal number in terms of the inverse on
positive surreals. -/
noncomputable def inv (x : pgame) : pgame :=
by classical; exact
if x = 0 then 0 else if 0 < x then inv' x else inv' (-x)
noncomputable instance : has_inv pgame := ⟨inv⟩
noncomputable instance : has_div pgame := ⟨λ x y, x * y⁻¹⟩
/-- A pre-game is numeric if
everything in the L set is less than everything in the R set,
and all the elements of L and R are also numeric. -/
def numeric : pgame → Prop
| ⟨l, r, L, R⟩ :=
(∀ i j, L i < R j) ∧ (∀ i, numeric (L i)) ∧ (∀ i, numeric (R i))
lemma numeric.move_left {x : pgame} (o : numeric x) (i : x.left_moves) :
numeric (x.move_left i) :=
begin
cases x with xl xr xL xR,
exact o.2.1 i,
end
lemma numeric.move_right {x : pgame} (o : numeric x) (j : x.right_moves) :
numeric (x.move_right j) :=
begin
cases x with xl xr xL xR,
exact o.2.2 j,
end
@[elab_as_eliminator]
theorem numeric_rec {C : pgame → Prop}
(H : ∀ l r (L : l → pgame) (R : r → pgame),
(∀ i j, L i < R j) → (∀ i, numeric (L i)) → (∀ i, numeric (R i)) →
(∀ i, C (L i)) → (∀ i, C (R i)) → C ⟨l, r, L, R⟩) :
∀ x, numeric x → C x
| ⟨l, r, L, R⟩ ⟨h, hl, hr⟩ :=
H _ _ _ _ h hl hr (λ i, numeric_rec _ (hl i)) (λ i, numeric_rec _ (hr i))
theorem lt_asymm {x y : pgame} (ox : numeric x) (oy : numeric y) : x < y → ¬ y < x :=
begin
refine numeric_rec (λ xl xr xL xR hx oxl oxr IHxl IHxr, _) x ox y oy,
refine numeric_rec (λ yl yr yL yR hy oyl oyr IHyl IHyr, _),
rw [mk_lt_mk, mk_lt_mk], rintro (⟨i, h₁⟩ | ⟨j, h₁⟩) (⟨i, h₂⟩ | ⟨j, h₂⟩),
{ exact IHxl _ _ (oyl _) (lt_of_le_mk h₁) (lt_of_le_mk h₂) },
{ exact not_lt.2 (le_trans h₂ h₁) (hy _ _) },
{ exact not_lt.2 (le_trans h₁ h₂) (hx _ _) },
{ exact IHxr _ _ (oyr _) (lt_of_mk_le h₁) (lt_of_mk_le h₂) },
end
theorem le_of_lt {x y : pgame} (ox : numeric x) (oy : numeric y) (h : x < y) : x ≤ y :=
not_lt.1 (lt_asymm ox oy h)
/-- On numeric pre-games, `<` and `≤` satisfy the axioms of a partial order (even though they
don't on all pre-games). -/
theorem lt_iff_le_not_le {x y : pgame} (ox : numeric x) (oy : numeric y) : x < y ↔ x ≤ y ∧ ¬ y ≤ x :=
⟨λ h, ⟨le_of_lt ox oy h, not_le.2 h⟩, λ h, not_le.1 h.2⟩
theorem numeric_zero : numeric 0 :=
⟨by rintros ⟨⟩ ⟨⟩, ⟨by rintros ⟨⟩, by rintros ⟨⟩⟩⟩
theorem numeric_one : numeric 1 :=
⟨by rintros ⟨⟩ ⟨⟩, ⟨λ x, numeric_zero, by rintros ⟨⟩⟩⟩
theorem numeric_neg : Π {x : pgame} (o : numeric x), numeric (-x)
| ⟨l, r, L, R⟩ o :=
⟨λ j i, lt_iff_neg_gt.1 (o.1 i j),
⟨λ j, numeric_neg (o.2.2 j), λ i, numeric_neg (o.2.1 i)⟩⟩
theorem numeric.move_left_lt {x : pgame.{u}} (o : numeric x) (i : x.left_moves) :
x.move_left i < x :=
begin
rw lt_def_le,
left,
use i,
end
theorem numeric.move_left_le {x : pgame} (o : numeric x) (i : x.left_moves) :
x.move_left i ≤ x :=
le_of_lt (o.move_left i) o (o.move_left_lt i)
theorem numeric.lt_move_right {x : pgame} (o : numeric x) (j : x.right_moves) :
x < x.move_right j :=
begin
rw lt_def_le,
right,
use j,
end
theorem numeric.le_move_right {x : pgame} (o : numeric x) (j : x.right_moves) :
x ≤ x.move_right j :=
le_of_lt o (o.move_right j) (o.lt_move_right j)
theorem add_lt_add
{w x y z : pgame.{u}} (ow : numeric w) (ox : numeric x) (oy : numeric y) (oz : numeric z)
(hwx : w < x) (hyz : y < z) : w + y < x + z :=
begin
rw lt_def_le at *,
rcases hwx with ⟨ix, hix⟩|⟨jw, hjw⟩;
rcases hyz with ⟨iz, hiz⟩|⟨jy, hjy⟩,
{ left,
use (left_moves_add x z).symm (sum.inl ix),
simp only [add_move_left_inl],
calc w + y ≤ move_left x ix + y : add_le_add_right hix
... ≤ move_left x ix + move_left z iz : add_le_add_left hiz
... ≤ move_left x ix + z : add_le_add_left (oz.move_left_le iz) },
{ left,
use (left_moves_add x z).symm (sum.inl ix),
simp only [add_move_left_inl],
calc w + y ≤ move_left x ix + y : add_le_add_right hix
... ≤ move_left x ix + move_right y jy : add_le_add_left (oy.le_move_right jy)
... ≤ move_left x ix + z : add_le_add_left hjy },
{ right,
use (right_moves_add w y).symm (sum.inl jw),
simp only [add_move_right_inl],
calc move_right w jw + y ≤ x + y : add_le_add_right hjw
... ≤ x + move_left z iz : add_le_add_left hiz
... ≤ x + z : add_le_add_left (oz.move_left_le iz), },
{ right,
use (right_moves_add w y).symm (sum.inl jw),
simp only [add_move_right_inl],
calc move_right w jw + y ≤ x + y : add_le_add_right hjw
... ≤ x + move_right y jy : add_le_add_left (oy.le_move_right jy)
... ≤ x + z : add_le_add_left hjy, },
end
theorem numeric_add : Π {x y : pgame} (ox : numeric x) (oy : numeric y), numeric (x + y)
| ⟨xl, xr, xL, xR⟩ ⟨yl, yr, yL, yR⟩ ox oy :=
⟨begin
rintros (ix|iy) (jx|jy),
{ show xL ix + ⟨yl, yr, yL, yR⟩ < xR jx + ⟨yl, yr, yL, yR⟩,
exact add_lt_add_right (ox.1 ix jx), },
{ show xL ix + ⟨yl, yr, yL, yR⟩ < ⟨xl, xr, xL, xR⟩ + yR jy,
apply add_lt_add (ox.move_left ix) ox oy (oy.move_right jy),
apply ox.move_left_lt,
apply oy.lt_move_right },
{ -- show ⟨xl, xr, xL, xR⟩ + yL iy < xR jx + ⟨yl, yr, yL, yR⟩, -- fails?
apply add_lt_add ox (ox.move_right jx) (oy.move_left iy) oy,
apply ox.lt_move_right,
apply oy.move_left_lt, },
{ -- show ⟨xl, xr, xL, xR⟩ + yL iy < ⟨xl, xr, xL, xR⟩ + yR jy, -- fails?
exact @add_lt_add_left ⟨xl, xr, xL, xR⟩ _ _ (oy.1 iy jy), }
end,
begin
split,
{ rintros (ix|iy),
{ apply numeric_add (ox.move_left ix) oy, },
{ apply numeric_add ox (oy.move_left iy), }, },
{ rintros (jx|jy),
{ apply numeric_add (ox.move_right jx) oy, },
{ apply numeric_add ox (oy.move_right jy), }, },
end⟩
using_well_founded { dec_tac := pgame_wf_tac }
-- TODO prove
-- theorem numeric_nat (n : ℕ) : numeric n := sorry
-- theorem numeric_omega : numeric omega := sorry
end pgame
/-- The equivalence on numeric pre-games. -/
def surreal.equiv (x y : {x // pgame.numeric x}) : Prop := x.1.equiv y.1
local infix ` ≈ ` := surreal.equiv
instance surreal.setoid : setoid {x // pgame.numeric x} :=
⟨λ x y, x.1.equiv y.1,
λ x, pgame.equiv_refl _,
λ x y, pgame.equiv_symm,
λ x y z, pgame.equiv_trans⟩
/-- The type of surreal numbers. These are the numeric pre-games quotiented
by the equivalence relation `x ≈ y ↔ x ≤ y ∧ y ≤ x`. In the quotient,
the order becomes a total order. -/
def surreal := quotient surreal.setoid
namespace surreal
open pgame
/-- Construct a surreal number from a numeric pre-game. -/
def mk (x : pgame) (h : x.numeric) : surreal := quotient.mk ⟨x, h⟩
instance : has_zero surreal :=
{ zero := ⟦⟨0, numeric_zero⟩⟧ }
instance : has_one surreal :=
{ one := ⟦⟨1, numeric_one⟩⟧ }
instance : inhabited surreal := ⟨0⟩
/-- Lift an equivalence-respecting function on pre-games to surreals. -/
def lift {α} (f : ∀ x, numeric x → α)
(H : ∀ {x y} (hx : numeric x) (hy : numeric y), x.equiv y → f x hx = f y hy) : surreal → α :=
quotient.lift (λ x : {x // numeric x}, f x.1 x.2) (λ x y, H x.2 y.2)
/-- Lift a binary equivalence-respecting function on pre-games to surreals. -/
def lift₂ {α} (f : ∀ x y, numeric x → numeric y → α)
(H : ∀ {x₁ y₁ x₂ y₂} (ox₁ : numeric x₁) (oy₁ : numeric y₁) (ox₂ : numeric x₂) (oy₂ : numeric y₂),
x₁.equiv x₂ → y₁.equiv y₂ → f x₁ y₁ ox₁ oy₁ = f x₂ y₂ ox₂ oy₂) : surreal → surreal → α :=
lift (λ x ox, lift (λ y oy, f x y ox oy) (λ y₁ y₂ oy₁ oy₂ h, H _ _ _ _ (equiv_refl _) h))
(λ x₁ x₂ ox₁ ox₂ h, funext $ quotient.ind $ by exact λ ⟨y, oy⟩, H _ _ _ _ h (equiv_refl _))
/-- The relation `x ≤ y` on surreals. -/
def le : surreal → surreal → Prop :=
lift₂ (λ x y _ _, x ≤ y) (λ x₁ y₁ x₂ y₂ _ _ _ _ hx hy, propext (le_congr hx hy))
/-- The relation `x < y` on surreals. -/
def lt : surreal → surreal → Prop :=
lift₂ (λ x y _ _, x < y) (λ x₁ y₁ x₂ y₂ _ _ _ _ hx hy, propext (lt_congr hx hy))
theorem not_le : ∀ {x y : surreal}, ¬ le x y ↔ lt y x :=
by rintro ⟨⟨x, ox⟩⟩ ⟨⟨y, oy⟩⟩; exact not_le
instance : preorder surreal :=
{ le := le,
lt := lt,
le_refl := by rintro ⟨⟨x, ox⟩⟩; exact le_refl _,
le_trans := by rintro ⟨⟨x, ox⟩⟩ ⟨⟨y, oy⟩⟩ ⟨⟨z, oz⟩⟩; exact le_trans,
lt_iff_le_not_le := by rintro ⟨⟨x, ox⟩⟩ ⟨⟨y, oy⟩⟩; exact lt_iff_le_not_le ox oy }
instance : partial_order surreal :=
{ le_antisymm := by rintro ⟨⟨x, ox⟩⟩ ⟨⟨y, oy⟩⟩ h₁ h₂; exact quot.sound ⟨h₁, h₂⟩,
..surreal.preorder }
instance : linear_order surreal :=
{ le_total := by rintro ⟨⟨x, ox⟩⟩ ⟨⟨y, oy⟩⟩; classical; exact
or_iff_not_imp_left.2 (λ h, le_of_lt oy ox (pgame.not_le.1 h)),
..surreal.partial_order }
def add : surreal → surreal → surreal :=
surreal.lift₂
(λ (x y : pgame) (ox) (oy), ⟦⟨x + y, numeric_add ox oy⟩⟧)
(λ x₁ y₁ x₂ y₂ _ _ _ _ hx hy, quot.sound (pgame.add_congr hx hy))
instance : has_add surreal := ⟨add⟩
theorem add_assoc : ∀ (x y z : surreal), (x + y) + z = x + (y + z) :=
begin
rintros ⟨x⟩ ⟨y⟩ ⟨z⟩,
apply quot.sound,
exact add_assoc_equiv,
end
instance : add_semigroup surreal :=
{ add_assoc := add_assoc,
..(by apply_instance : has_add surreal) }
-- We conclude with some ideas for further work on surreals; these would make fun projects.
-- TODO construct the remaining instances:
-- add_monoid, add_group, add_comm_semigroup, add_comm_group, ordered_comm_group,
-- as per the instances for `game`
-- TODO define the inclusion of groups `surreal → game`
-- TODO define the dyadic rationals, and show they map into the surreals via the formula
-- m / 2^n ↦ { (m-1) / 2^n | (m+1) / 2^n }
-- TODO show this is a group homomorphism, and injective
-- TODO map the reals into the surreals, using dyadic Dedekind cuts
-- TODO show this is a group homomorphism, and injective
-- TODO define the field structure on the surreals
-- TODO show the maps from the dyadic rationals and from the reals into the surreals are multiplicative
end surreal
|
8c115c56c73dcfe37edc754a77702f548a8cf343 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/topology/algebra/weak_dual_topology.lean | a45d0da0838337f0bfc2b491f57cd7c30863a4d1 | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,756 | lean | /-
Copyright (c) 2021 Kalle Kytölä. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kalle Kytölä
-/
import topology.algebra.module
/-!
# Weak dual topology
This file defines the weak-* topology on duals of suitable topological modules `E` over suitable
topological semirings `𝕜`. The (weak) dual consists of continuous linear functionals `E →L[𝕜] 𝕜`
from `E` to scalars `𝕜`. The weak-* topology is the coarsest topology on this dual
`weak_dual 𝕜 E := (E →L[𝕜] 𝕜)` w.r.t. which the evaluation maps at all `z : E` are continuous.
The weak dual is a module over `𝕜` if the semiring `𝕜` is commutative.
## Main definitions
The main definitions are the type `weak_dual 𝕜 E` and a topology instance on it.
* `weak_dual 𝕜 E` is a type synonym for `dual 𝕜 E` (when the latter is defined): both are equal to
the type `E →L[𝕜] 𝕜` of continuous linear maps from a module `E` over `𝕜` to the ring `𝕜`.
* The instance `weak_dual.topological_space` is the weak-* topology on `weak_dual 𝕜 E`, i.e., the
coarsest topology making the evaluation maps at all `z : E` continuous.
## Main results
We establish that `weak_dual 𝕜 E` has the following structure:
* `weak_dual.has_continuous_add`: The addition in `weak_dual 𝕜 E` is continuous.
* `weak_dual.module`: If the scalars `𝕜` are a commutative semiring, then `weak_dual 𝕜 E` is a
module over `𝕜`.
* `weak_dual.has_continuous_smul`: If the scalars `𝕜` are a commutative semiring, then the scalar
multiplication by `𝕜` in `weak_dual 𝕜 E` is continuous.
We prove the following results characterizing the weak-* topology:
* `weak_dual.eval_continuous`: For any `z : E`, the evaluation mapping `weak_dual 𝕜 E → 𝕜` taking
`x'`to `x' z` is continuous.
* `weak_dual.continuous_of_continuous_eval`: For a mapping to `weak_dual 𝕜 E` to be continuous,
it suffices that its compositions with evaluations at all points `z : E` are continuous.
* `weak_dual.tendsto_iff_forall_eval_tendsto`: Convergence in `weak_dual 𝕜 E` can be characterized
in terms of convergence of the evaluations at all points `z : E`.
## Notations
No new notation is introduced.
## Implementation notes
The weak-* topology is defined as the induced topology under the mapping that associates to a dual
element `x'` the functional `E → 𝕜`, when the space `E → 𝕜` of functionals is equipped with the
topology of pointwise convergence (product topology).
Typically one might assume that `𝕜` is a topological semiring in the sense of the typeclasses
`topological_space 𝕜`, `semiring 𝕜`, `has_continuous_add 𝕜`, `has_continuous_mul 𝕜`,
and that the space `E` is a topological module over `𝕜` in the sense of the typeclasses
`topological_space E`, `add_comm_monoid E`, `has_continuous_add E`, `module 𝕜 E`,
`has_continuous_smul 𝕜 E`. The definitions and results are, however, given with weaker assumptions
when possible.
## References
* https://en.wikipedia.org/wiki/Weak_topology#Weak-*_topology
## Tags
weak-star, weak dual
-/
noncomputable theory
open filter
open_locale topological_space
section weak_star_topology
/-!
### Weak star topology on duals of topological modules
-/
universe variables u v
variables (𝕜 : Type*) [topological_space 𝕜] [semiring 𝕜]
variables (E : Type*) [topological_space E] [add_comm_monoid E] [module 𝕜 E]
/-- The weak dual of a topological module `E` over a topological semiring `𝕜` consists of
continuous linear functionals from `E` to scalars `𝕜`. It is a type synonym with the usual dual
(when the latter is defined), but will be equipped with a different topology. -/
@[derive [inhabited, has_coe_to_fun]]
def weak_dual := E →L[𝕜] 𝕜
instance [has_continuous_add 𝕜] : add_comm_monoid (weak_dual 𝕜 E) :=
continuous_linear_map.add_comm_monoid
namespace weak_dual
/-- The weak-* topology instance `weak_dual.topological_space` on the dual of a topological module
`E` over a topological semiring `𝕜` is defined as the induced topology under the mapping that
associates to a dual element `x' : weak_dual 𝕜 E` the functional `E → 𝕜`, when the space `E → 𝕜`
of functionals is equipped with the topology of pointwise convergence (product topology). -/
instance : topological_space (weak_dual 𝕜 E) :=
topological_space.induced (λ x' : weak_dual 𝕜 E, λ z : E, x' z) Pi.topological_space
lemma coe_fn_continuous :
continuous (λ (x' : (weak_dual 𝕜 E)), (λ (z : E), x' z)) :=
continuous_induced_dom
lemma eval_continuous (z : E) : continuous (λ (x' : weak_dual 𝕜 E), x' z) :=
(continuous_pi_iff.mp (coe_fn_continuous 𝕜 E)) z
lemma continuous_of_continuous_eval {α : Type u} [topological_space α]
{g : α → weak_dual 𝕜 E} (h : ∀ z, continuous (λ a, g a z)) : continuous g :=
continuous_induced_rng (continuous_pi_iff.mpr h)
theorem tendsto_iff_forall_eval_tendsto {γ : Type u} {F : filter γ}
{ψs : γ → weak_dual 𝕜 E} {ψ : weak_dual 𝕜 E} :
tendsto ψs F (𝓝 ψ) ↔ ∀ z : E, tendsto (λ i, ψs i z) F (𝓝 (ψ z)) :=
begin
rw ←tendsto_pi,
split,
{ intros weak_star_conv,
exact (((coe_fn_continuous 𝕜 E).tendsto ψ).comp weak_star_conv), },
{ intro h_lim_forall,
rwa [nhds_induced, tendsto_comap_iff], },
end
/-- Addition in `weak_dual 𝕜 E` is continuous. -/
instance [has_continuous_add 𝕜] : has_continuous_add (weak_dual 𝕜 E) :=
{ continuous_add := begin
apply continuous_of_continuous_eval,
intros z,
have h : continuous (λ p : 𝕜 × 𝕜, p.1 + p.2) := continuous_add,
exact h.comp ((eval_continuous 𝕜 E z).prod_map (eval_continuous 𝕜 E z)),
end, }
/-- If the scalars `𝕜` are a commutative semiring, then `weak_dual 𝕜 E` is a module over `𝕜`. -/
instance (𝕜 : Type u) [topological_space 𝕜] [comm_semiring 𝕜]
[has_continuous_add 𝕜] [has_continuous_mul 𝕜]
(E : Type*) [topological_space E] [add_comm_group E] [module 𝕜 E] :
module 𝕜 (weak_dual 𝕜 E) :=
continuous_linear_map.module
/-- Scalar multiplication in `weak_dual 𝕜 E` is continuous (when `𝕜` is a commutative
semiring). -/
instance (𝕜 : Type u) [topological_space 𝕜] [comm_semiring 𝕜]
[has_continuous_add 𝕜] [has_continuous_mul 𝕜]
(E : Type*) [topological_space E] [add_comm_group E]
[module 𝕜 E] :
has_continuous_smul 𝕜 (weak_dual 𝕜 E) :=
{ continuous_smul := begin
apply continuous_of_continuous_eval,
intros z,
have h : continuous (λ p : 𝕜 × 𝕜, p.1 * p.2) := continuous_mul,
exact h.comp ((continuous_id').prod_map (eval_continuous 𝕜 E z)),
end, }
end weak_dual
end weak_star_topology
|
638367d76b1208797d2184a56bfec9bd9bd27aa4 | 54deab7025df5d2df4573383df7e1e5497b7a2c2 | /data/list/set.lean | c3f46cbed4c7ba69ad8ca9476efa69bd08c1ebc7 | [
"Apache-2.0"
] | permissive | HGldJ1966/mathlib | f8daac93a5b4ae805cfb0ecebac21a9ce9469009 | c5c5b504b918a6c5e91e372ee29ed754b0513e85 | refs/heads/master | 1,611,340,395,683 | 1,503,040,489,000 | 1,503,040,489,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 27,807 | lean | /-
Copyright (c) 2015 Leonardo de Moura. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad
Set-like operations on lists.
-/
import data.list.basic data.list.comb
open nat function decidable
universe variables uu vv
variables {α : Type uu} {β : Type vv}
namespace list
section insert
variable [decidable_eq α]
@[simp] theorem insert_nil (a : α) : insert a nil = [a] := rfl
theorem insert.def (a : α) (l : list α) : insert a l = if a ∈ l then l else a :: l := rfl
@[simp] theorem insert_of_mem {a : α} {l : list α} (h : a ∈ l) : insert a l = l :=
by simp [insert.def, h]
@[simp] theorem insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) : insert a l = a :: l :=
by simp [insert.def, h]
@[simp] theorem mem_insert_iff {a b : α} {l : list α} : a ∈ insert b l ↔ a = b ∨ a ∈ l :=
begin
by_cases b ∈ l with h'; simp [h'],
apply (or_iff_right_of_imp _).symm,
exact λ e, e.symm ▸ h'
end
@[simp] theorem mem_insert_self (a : α) (l : list α) : a ∈ insert a l :=
mem_insert_iff.2 (or.inl rfl)
@[simp] theorem mem_insert_of_mem {a b : α} {l : list α} (h : a ∈ l) : a ∈ insert b l :=
mem_insert_iff.2 (or.inr h)
theorem eq_or_mem_of_mem_insert {a b : α} {l : list α} (h : a ∈ insert b l) : a = b ∨ a ∈ l :=
mem_insert_iff.1 h
@[simp] theorem length_insert_of_mem {a : α} [decidable_eq α] {l : list α} (h : a ∈ l) :
length (insert a l) = length l :=
by simp [h]
@[simp] theorem length_insert_of_not_mem {a : α} [decidable_eq α] {l : list α} (h : a ∉ l) :
length (insert a l) = length l + 1 :=
by simp [h]
theorem forall_mem_insert_of_forall_mem {p : α → Prop} {a : α} {l : list α}
(h₁ : p a) (h₂ : ∀ x ∈ l, p x) :
∀ x ∈ insert a l, p x :=
if h : a ∈ l then begin simp [h], exact h₂ end
else begin simp [h], intros b hb, cases hb with h₃ h₃, {rw h₃, assumption}, exact h₂ _ h₃ end
end insert
section erase
variable [decidable_eq α]
@[simp] theorem erase_nil (a : α) : [].erase a = [] := rfl
@[simp] theorem erase_cons_head (a : α) (l : list α) : (a :: l).erase a = l :=
by simp [list.erase]
@[simp] theorem erase_cons_tail {a b : α} (l : list α) (h : b ≠ a) : (b::l).erase a = b :: l.erase a :=
by simp [list.erase, h]
@[simp] theorem length_erase_of_mem {a : α} : ∀{l:list α}, a ∈ l → length (l.erase a) = pred (length l)
| [] h := rfl
| [x] h := begin simp at h, simp [h] end
| (x::y::xs) h := if h' : x = a then
by simp [h', one_add]
else
have ainyxs : a ∈ y::xs, from or_resolve_right h $ by cc,
by simp [h', length_erase_of_mem ainyxs, one_add]
@[simp] theorem erase_of_not_mem {a : α} : ∀{l : list α}, a ∉ l → l.erase a = l
| [] h := rfl
| (x::xs) h :=
have anex : x ≠ a, from λ aeqx : x = a, absurd (or.inl aeqx.symm) h,
have aninxs : a ∉ xs, from λ ainxs : a ∈ xs, absurd (or.inr ainxs) h,
by simp [anex, erase_of_not_mem aninxs]
theorem erase_append_left {a : α} : ∀ {l₁:list α} (l₂), a ∈ l₁ → (l₁++l₂).erase a = l₁.erase a ++ l₂
| [] l₂ h := absurd h (not_mem_nil a)
| (x::xs) l₂ h := if h' : x = a then by simp [h']
else
have a ∈ xs, from mem_of_ne_of_mem (assume h, h' h.symm) h,
by simp [erase_append_left l₂ this, h']
theorem erase_append_right {a : α} : ∀{l₁ : list α} (l₂), a ∉ l₁ → (l₁++l₂).erase a = l₁ ++ l₂.erase a
| [] l₂ h := rfl
| (x::xs) l₂ h := if h' : x = a then begin simp [h'] at h, contradiction end
else
have a ∉ xs, from not_mem_of_not_mem_cons h,
by simp [erase_append_right l₂ this, h']
theorem erase_sublist (a : α) : ∀(l : list α), l.erase a <+ l
| [] := sublist.refl nil
| (x :: xs) := if h : x = a then
by simp [h]
else
begin simp [h], apply cons_sublist_cons, apply erase_sublist xs end
theorem erase_subset (a : α) (l : list α) : l.erase a ⊆ l :=
subset_of_sublist (erase_sublist a l)
theorem mem_erase_of_ne_of_mem {a b : α} : ∀ {l : list α}, a ≠ b → a ∈ l → a ∈ l.erase b
| [] aneb anil := begin simp at anil, contradiction end
| (c :: l) aneb acl := if h : c = b then
begin simp [h, aneb] at acl, simp [h, acl] end
else
begin
simp [h], simp at acl, cases acl with h' h',
{ simp [h'] },
simp [mem_erase_of_ne_of_mem aneb h']
end
theorem mem_of_mem_erase {a b : α} : ∀{l:list α}, a ∈ l.erase b → a ∈ l
| [] h := begin simp at h, contradiction end
| (c :: l) h := if h' : c = b then
begin simp [h'] at h, simp [h] end
else
begin
simp [h'] at h, cases h with h'' h'',
{ simp [h''] },
simp [mem_of_mem_erase h'']
end
end erase
/- disjoint -/
section disjoint
def disjoint (l₁ l₂ : list α) : Prop := ∀ ⦃a⦄, (a ∈ l₁ → a ∈ l₂ → false)
theorem disjoint_left {l₁ l₂ : list α} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₁ → a ∉ l₂ :=
λ d, d
theorem disjoint_right {l₁ l₂ : list α} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₂ → a ∉ l₁ :=
λ d a i₂ i₁, d i₁ i₂
theorem disjoint.symm {l₁ l₂ : list α} : disjoint l₁ l₂ → disjoint l₂ l₁ :=
λ d a i₂ i₁, d i₁ i₂
theorem disjoint_comm {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ disjoint l₂ l₁ :=
⟨disjoint.symm, disjoint.symm⟩
theorem disjoint_of_subset_left {l₁ l₂ l : list α} : l₁ ⊆ l → disjoint l l₂ → disjoint l₁ l₂ :=
λ ss d x xinl₁, d (ss xinl₁)
theorem disjoint_of_subset_right {l₁ l₂ l : list α} : l₂ ⊆ l → disjoint l₁ l → disjoint l₁ l₂ :=
λ ss d x xinl xinl₁, d xinl (ss xinl₁)
theorem disjoint_of_disjoint_cons_left {a : α} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ :=
disjoint_of_subset_left (list.subset_cons _ _)
theorem disjoint_of_disjoint_cons_right {a : α} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ :=
disjoint_of_subset_right (list.subset_cons _ _)
theorem disjoint_nil_left (l : list α) : disjoint [] l :=
λ a ab, absurd ab (not_mem_nil a)
theorem disjoint_singleton {l : list α} {a : α} : disjoint l [a] ↔ a ∉ l :=
⟨λ d h, d h (mem_cons_self _ _), λ m b bl ba, m (mem_singleton ba ▸ bl)⟩
theorem singleton_disjoint {l : list α} {a : α} : disjoint [a] l ↔ a ∉ l :=
disjoint_comm.trans disjoint_singleton
theorem disjoint_cons_of_not_mem_of_disjoint {a : α} {l₁ l₂ : list α} :
a ∉ l₂ → disjoint l₁ l₂ → disjoint (a::l₁) l₂ :=
λ nainl₂ d x (xinal₁ : x ∈ a::l₁),
or.elim (eq_or_mem_of_mem_cons xinal₁)
(λ xeqa : x = a, eq.symm xeqa ▸ nainl₂)
(λ xinl₁ : x ∈ l₁, disjoint_left d xinl₁)
theorem disjoint_append_of_disjoint_left {l₁ l₂ l : list α} :
disjoint l₁ l → disjoint l₂ l → disjoint (l₁++l₂) l :=
λ d₁ d₂ x h, or.elim (mem_append.1 h) (@d₁ x) (@d₂ x)
theorem disjoint_of_disjoint_append_left_left {l₁ l₂ l : list α} : disjoint (l₁++l₂) l → disjoint l₁ l :=
disjoint_of_subset_left (list.subset_append_left _ _)
theorem disjoint_of_disjoint_append_left_right {l₁ l₂ l : list α} : disjoint (l₁++l₂) l → disjoint l₂ l :=
disjoint_of_subset_left (list.subset_append_right _ _)
theorem disjoint_of_disjoint_append_right_left {l₁ l₂ l : list α} : disjoint l (l₁++l₂) → disjoint l l₁ :=
disjoint_of_subset_right (list.subset_append_left _ _)
theorem disjoint_of_disjoint_append_right_right {l₁ l₂ l : list α} : disjoint l (l₁++l₂) → disjoint l l₂ :=
disjoint_of_subset_right (list.subset_append_right _ _)
end disjoint
/- upto -/
def upto : nat → list nat
| 0 := []
| (n+1) := n :: upto n
@[simp] theorem upto_nil : upto 0 = nil := rfl
@[simp] theorem upto_succ (n : nat) : upto (succ n) = n :: upto n := rfl
@[simp] theorem length_upto : ∀ n, length (upto n) = n
| 0 := rfl
| (succ n) := begin rw [upto_succ, length_cons, length_upto] end
theorem upto_ne_nil_of_ne_zero {n : ℕ} (h : n ≠ 0) : upto n ≠ nil :=
assume : upto n = nil,
have upto n = upto 0, from upto_nil ▸ this,
have n = 0, from calc
n = length (upto n) : by rw length_upto
... = length (upto 0) : by rw this
... = 0 : by rw length_upto,
h this
theorem lt_of_mem_upto : ∀ ⦃n i⦄, i ∈ upto n → i < n
| 0 := assume i imem, absurd imem (not_mem_nil _)
| (succ n) := assume i imem,
or.elim (eq_or_mem_of_mem_cons imem)
(λ h, begin rw h, apply lt_succ_self end)
(λ h, lt.trans (lt_of_mem_upto h) (lt_succ_self n))
theorem mem_upto_succ_of_mem_upto {n i : nat} : i ∈ upto n → i ∈ upto (succ n) :=
assume i, mem_cons_of_mem _ i
theorem mem_upto_of_lt : ∀ ⦃n i : nat⦄, i < n → i ∈ upto n
| 0 := λ i h, absurd h (not_lt_zero i)
| (succ n) := λ i h,
begin
cases lt_or_eq_of_le (le_of_lt_succ h) with ilt ieq,
{ apply mem_upto_succ_of_mem_upto, apply mem_upto_of_lt ilt },
simp [ieq]
end
theorem upto_step : ∀ (n : nat), upto (succ n) = (map succ (upto n)) ++ [0]
| 0 := rfl
| (succ n) := by simp [(upto_step n).symm]
/- union -/
section union
variable [decidable_eq α]
@[simp] theorem nil_union (l : list α) : [] ∪ l = l := rfl
@[simp] theorem cons_union (l₁ l₂ : list α) (a : α) : a :: l₁ ∪ l₂ = insert a (l₁ ∪ l₂) := rfl
@[simp] theorem mem_union_iff {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∪ l₂ ↔ a ∈ l₁ ∨ a ∈ l₂ :=
by induction l₁; simp *
theorem mem_or_mem_of_mem_union {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∪ l₂ → a ∈ l₁ ∨ a ∈ l₂ :=
mem_union_iff.1
theorem mem_union_left {a : α} {l₁ : list α} (h : a ∈ l₁) (l₂ : list α) : a ∈ l₁ ∪ l₂ :=
mem_union_iff.2 (or.inl h)
theorem mem_union_right {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) : a ∈ l₁ ∪ l₂ :=
mem_union_iff.2 (or.inr h)
theorem forall_mem_union {p : α → Prop} {l₁ l₂ : list α} (h₁ : ∀ x ∈ l₁, p x) (h₂ : ∀ x ∈ l₂, p x) :
∀ x ∈ l₁ ∪ l₂, p x :=
by simp [or_imp_iff_and_imp, forall_and_distrib]; exact ⟨h₁, h₂⟩
theorem forall_mem_of_forall_mem_union_left {p : α → Prop} {l₁ l₂ : list α}
(h : ∀ x ∈ l₁ ∪ l₂, p x) :
∀ x ∈ l₁, p x :=
by intros x xl₁; apply h; apply mem_union_left xl₁
theorem forall_mem_of_forall_mem_union_right {p : α → Prop} {l₁ l₂ : list α}
(h : ∀ x ∈ l₁ ∪ l₂, p x) :
∀ x ∈ l₂, p x :=
by intros x xl₂; apply h; apply mem_union_right l₁ xl₂
end union
/- inter -/
section inter
variable [decidable_eq α]
@[simp] theorem inter_nil (l : list α) : [] ∩ l = [] := rfl
@[simp] theorem inter_cons_of_mem {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) :
(a::l₁) ∩ l₂ = a :: (l₁ ∩ l₂) :=
if_pos h
@[simp] theorem inter_cons_of_not_mem {a : α} (l₁ : list α) {l₂ : list α} (h : a ∉ l₂) :
(a::l₁) ∩ l₂ = l₁ ∩ l₂ :=
if_neg h
theorem mem_of_mem_inter_left : ∀ {l₁ l₂ : list α} {a : α}, a ∈ l₁ ∩ l₂ → a ∈ l₁
| [] l₂ a i := absurd i (not_mem_nil a)
| (b::l₁) l₂ a i := by_cases
(λ binl₂ : b ∈ l₂,
have aux : a ∈ b :: l₁ ∩ l₂, begin rw [inter_cons_of_mem _ binl₂] at i, exact i end,
or.elim (eq_or_mem_of_mem_cons aux)
(λ aeqb : a = b, begin rw [aeqb], apply mem_cons_self end)
(λ aini, mem_cons_of_mem _ (mem_of_mem_inter_left aini)))
(λ nbinl₂ : b ∉ l₂,
have ainl₁ : a ∈ l₁,
begin rw [inter_cons_of_not_mem _ nbinl₂] at i, exact (mem_of_mem_inter_left i) end,
mem_cons_of_mem _ ainl₁)
theorem mem_of_mem_inter_right : ∀ {l₁ l₂ : list α} {a : α}, a ∈ l₁ ∩ l₂ → a ∈ l₂
| [] l₂ a i := absurd i (not_mem_nil _)
| (b::l₁) l₂ a i := by_cases
(λ binl₂ : b ∈ l₂,
have aux : a ∈ b :: l₁ ∩ l₂, begin rw [inter_cons_of_mem _ binl₂] at i, exact i end,
or.elim (eq_or_mem_of_mem_cons aux)
(λ aeqb : a = b, begin rw [aeqb], exact binl₂ end)
(λ aini : a ∈ l₁ ∩ l₂, mem_of_mem_inter_right aini))
(λ nbinl₂ : b ∉ l₂,
begin rw [inter_cons_of_not_mem _ nbinl₂] at i, exact (mem_of_mem_inter_right i) end)
theorem mem_inter_of_mem_of_mem : ∀ {l₁ l₂ : list α} {a : α}, a ∈ l₁ → a ∈ l₂ → a ∈ l₁ ∩ l₂
| [] l₂ a i₁ i₂ := absurd i₁ (not_mem_nil _)
| (b::l₁) l₂ a i₁ i₂ := by_cases
(λ binl₂ : b ∈ l₂,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqb : a = b,
begin rw [inter_cons_of_mem _ binl₂, aeqb], apply mem_cons_self end)
(λ ainl₁ : a ∈ l₁,
begin
rw [inter_cons_of_mem _ binl₂],
apply mem_cons_of_mem,
exact (mem_inter_of_mem_of_mem ainl₁ i₂)
end))
(λ nbinl₂ : b ∉ l₂,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqb : a = b,
begin rw aeqb at i₂, exact absurd i₂ nbinl₂ end)
(λ ainl₁ : a ∈ l₁,
begin rw [inter_cons_of_not_mem _ nbinl₂], exact (mem_inter_of_mem_of_mem ainl₁ i₂) end))
@[simp] theorem mem_inter_iff (a : α) (l₁ l₂ : list α) : a ∈ l₁ ∩ l₂ ↔ a ∈ l₁ ∧ a ∈ l₂ :=
iff.intro
(λ h, and.intro (mem_of_mem_inter_left h) (mem_of_mem_inter_right h))
(λ h, mem_inter_of_mem_of_mem h.left h.right)
theorem inter_eq_nil_of_disjoint : ∀ {l₁ l₂ : list α}, disjoint l₁ l₂ → l₁ ∩ l₂ = []
| [] l₂ d := rfl
| (a::l₁) l₂ d :=
have aux_eq : l₁ ∩ l₂ = [], from inter_eq_nil_of_disjoint (disjoint_of_disjoint_cons_left d),
have nainl₂ : a ∉ l₂, from disjoint_left d (mem_cons_self _ _),
by rw [inter_cons_of_not_mem _ nainl₂, aux_eq]
theorem forall_mem_inter_of_forall_left {p : α → Prop} {l₁ : list α} (h : ∀ x ∈ l₁, p x)
(l₂ : list α) :
∀ x, x ∈ l₁ ∩ l₂ → p x :=
λ x xl₁l₂, h x (mem_of_mem_inter_left xl₁l₂)
theorem forall_mem_inter_of_forall_right {p : α → Prop} (l₁ : list α) {l₂ : list α}
(h : ∀ x ∈ l₂, p x) :
∀ x, x ∈ l₁ ∩ l₂ → p x :=
λ x xl₁l₂, h x (mem_of_mem_inter_right xl₁l₂)
end inter
/- no duplicates predicate -/
inductive nodup {α : Type uu} : list α → Prop
| ndnil : nodup []
| ndcons : ∀ {a : α} {l : list α}, a ∉ l → nodup l → nodup (a::l)
section nodup
open nodup
theorem nodup_nil : @nodup α [] :=
ndnil
theorem nodup_cons {a : α} {l : list α} : a ∉ l → nodup l → nodup (a::l) :=
λ i n, ndcons i n
theorem nodup_singleton (a : α) : nodup [a] :=
nodup_cons (not_mem_nil a) nodup_nil
theorem nodup_of_nodup_cons : ∀ {a : α} {l : list α}, nodup (a::l) → nodup l
| a xs (ndcons i n) := n
theorem not_mem_of_nodup_cons : ∀ {a : α} {l : list α}, nodup (a::l) → a ∉ l
| a xs (ndcons i n) := i
theorem not_nodup_cons_of_mem {a : α} {l : list α} : a ∈ l → ¬ nodup (a :: l) :=
λ ainl d, absurd ainl (not_mem_of_nodup_cons d)
theorem nodup_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → nodup l₂ → nodup l₁
| ._ ._ sublist.slnil h := h
| ._ ._ (sublist.cons l₁ l₂ a s) (ndcons i n) := nodup_of_sublist s n
| ._ ._ (sublist.cons2 l₁ l₂ a s) (ndcons i n) :=
ndcons (λh, i (subset_of_sublist s h)) (nodup_of_sublist s n)
theorem not_nodup_cons_of_not_nodup {a : α} {l : list α} : ¬ nodup l → ¬ nodup (a :: l) :=
mt nodup_of_nodup_cons
theorem nodup_of_nodup_append_left {l₁ l₂ : list α} : nodup (l₁++l₂) → nodup l₁ :=
nodup_of_sublist (sublist_append_left l₁ l₂)
theorem nodup_of_nodup_append_right : ∀ {l₁ l₂ : list α}, nodup (l₁++l₂) → nodup l₂
| [] l₂ n := n
| (x::xs) l₂ n := nodup_of_nodup_append_right (nodup_of_nodup_cons n)
theorem disjoint_of_nodup_append : ∀ {l₁ l₂ : list α}, nodup (l₁++l₂) → disjoint l₁ l₂
| [] l₂ d := disjoint_nil_left l₂
| (x::xs) l₂ d := begin
simp at d,
apply disjoint_cons_of_not_mem_of_disjoint,
exact mt (mem_append_right _) (not_mem_of_nodup_cons d),
exact disjoint_of_nodup_append (nodup_of_nodup_cons d),
end
theorem nodup_append :
∀ {l₁ l₂ : list α}, nodup l₁ → nodup l₂ → disjoint l₁ l₂ → nodup (l₁++l₂)
| [] l₂ d₁ d₂ dsj := begin rw [nil_append], exact d₂ end
| (x::xs) l₂ d₁ d₂ dsj :=
have ndxs : nodup xs, from nodup_of_nodup_cons d₁,
have disjoint xs l₂, from disjoint_of_disjoint_cons_left dsj,
have ndxsl₂ : nodup (xs++l₂), from nodup_append ndxs d₂ this,
have nxinxs : x ∉ xs, from not_mem_of_nodup_cons d₁,
have x ∉ l₂, from disjoint_left dsj (mem_cons_self x xs),
have x ∉ xs++l₂, from not_mem_append nxinxs this,
nodup_cons this ndxsl₂
theorem nodup_app_comm {l₁ l₂ : list α} (d : nodup (l₁++l₂)) : nodup (l₂++l₁) :=
have d₁ : nodup l₁, from nodup_of_nodup_append_left d,
have d₂ : nodup l₂, from nodup_of_nodup_append_right d,
have dsj : disjoint l₁ l₂, from disjoint_of_nodup_append d,
nodup_append d₂ d₁ dsj.symm
theorem nodup_head {a : α} {l₁ l₂ : list α} (d : nodup (l₁++(a::l₂))) : nodup (a::(l₁++l₂)) :=
have d₁ : nodup (a::(l₂++l₁)), from nodup_app_comm d,
have d₂ : nodup (l₂++l₁), from nodup_of_nodup_cons d₁,
have d₃ : nodup (l₁++l₂), from nodup_app_comm d₂,
have nain : a ∉ l₂++l₁, from not_mem_of_nodup_cons d₁,
have nain₂ : a ∉ l₂, from mt (mem_append_left _) nain,
have nain₁ : a ∉ l₁, from mt (mem_append_right _) nain,
nodup_cons (not_mem_append nain₁ nain₂) d₃
theorem nodup_middle {a : α} {l₁ l₂ : list α} (d : nodup (a::(l₁++l₂))) : nodup (l₁++(a::l₂)) :=
have d₁ : nodup (l₁++l₂), from nodup_of_nodup_cons d,
have nain : a ∉ l₁++l₂, from not_mem_of_nodup_cons d,
have disj : disjoint l₁ l₂, from disjoint_of_nodup_append d₁,
have d₂ : nodup l₁, from nodup_of_nodup_append_left d₁,
have d₃ : nodup l₂, from nodup_of_nodup_append_right d₁,
have nain₂ : a ∉ l₂, from mt (mem_append_right _) nain,
have nain₁ : a ∉ l₁, from mt (mem_append_left _) nain,
have d₄ : nodup (a::l₂), from nodup_cons nain₂ d₃,
have disj₂ : disjoint l₁ (a::l₂), from (disjoint_cons_of_not_mem_of_disjoint nain₁ disj.symm).symm,
nodup_append d₂ d₄ disj₂
theorem nodup_of_nodup_map (f : α → β) : ∀ {l : list α}, nodup (map f l) → nodup l
| [] d := ndnil
| (a::l) d := ndcons (mt (mem_map f) (not_mem_of_nodup_cons d))
(nodup_of_nodup_map (nodup_of_nodup_cons d))
theorem nodup_map {f : α → β} (inj : injective f) : ∀ {l : list α}, nodup l → nodup (map f l)
| [] n := begin apply nodup_nil end
| (x::xs) n :=
have nxinxs : x ∉ xs, from not_mem_of_nodup_cons n,
have ndxs : nodup xs, from nodup_of_nodup_cons n,
have ndmfxs : nodup (map f xs), from nodup_map ndxs,
have nfxinm : f x ∉ map f xs, from
λ ab : f x ∈ map f xs,
match (exists_of_mem_map ab) with
| ⟨(y : α), (yinxs : y ∈ xs), (fyfx : f y = f x)⟩ :=
have yeqx : y = x, from inj fyfx,
begin subst y, contradiction end
end,
nodup_cons nfxinm ndmfxs
instance nodup_decidable [decidable_eq α] : ∀ l : list α, decidable (nodup l)
| [] := is_true ndnil
| (a :: l) := if h : a ∈ l
then is_false (λ nd, not_mem_of_nodup_cons nd h)
else decidable_of_decidable_of_iff (nodup_decidable l)
⟨nodup_cons h, nodup_of_nodup_cons⟩
theorem nodup_erase_of_nodup [decidable_eq α] (a : α) : ∀ {l}, nodup l → nodup (l.erase a)
| [] n := nodup_nil
| (b::l) n := by_cases
(λ aeqb : b = a, begin rw [aeqb, erase_cons_head], exact (nodup_of_nodup_cons n) end)
(λ aneb : b ≠ a,
have nbinl : b ∉ l, from not_mem_of_nodup_cons n,
have ndl : nodup l, from nodup_of_nodup_cons n,
have ndeal : nodup (l.erase a), from nodup_erase_of_nodup ndl,
have nbineal : b ∉ l.erase a, from λ i, absurd (erase_subset _ _ i) nbinl,
have aux : nodup (b :: l.erase a), from nodup_cons nbineal ndeal,
begin rw [erase_cons_tail _ aneb], exact aux end)
theorem mem_erase_of_nodup [decidable_eq α] (a : α) : ∀ {l}, nodup l → a ∉ l.erase a
| [] n := (not_mem_nil _)
| (b::l) n :=
have ndl : nodup l, from nodup_of_nodup_cons n,
have naineal : a ∉ l.erase a, from mem_erase_of_nodup ndl,
have nbinl : b ∉ l, from not_mem_of_nodup_cons n,
by_cases
(λ aeqb : b = a, begin rw [aeqb.symm, erase_cons_head], exact nbinl end)
(λ aneb : b ≠ a,
have aux : a ∉ b :: l.erase a, from
assume ainbeal : a ∈ b :: l.erase a, or.elim (eq_or_mem_of_mem_cons ainbeal)
(λ aeqb : a = b, absurd aeqb.symm aneb)
(λ aineal : a ∈ l.erase a, absurd aineal naineal),
begin rw [erase_cons_tail _ aneb], exact aux end)
def erase_dup [decidable_eq α] : list α → list α
| [] := []
| (x :: xs) := if x ∈ xs then erase_dup xs else x :: erase_dup xs
@[simp] theorem erase_dup_nil [decidable_eq α] : erase_dup [] = ([] : list α) := rfl
@[simp] theorem erase_dup_cons_of_mem [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) :
erase_dup (a::l) = erase_dup l :=
by simp [erase_dup, h]
@[simp] theorem erase_dup_cons_of_not_mem [decidable_eq α] {a : α} {l : list α} (h : a ∉ l) :
erase_dup (a::l) = a :: erase_dup l :=
by simp [erase_dup, h]
theorem mem_erase_dup [decidable_eq α] {a : α} {l : list α} : a ∈ erase_dup l ↔ a ∈ l :=
begin
induction l with b l ih; simp,
by_cases b ∈ l with h'; simp [h', ih],
apply (or_iff_right_of_imp _).symm,
exact λ e, e.symm ▸ h'
end
theorem erase_dup_sublist [decidable_eq α] : ∀ (l : list α), erase_dup l <+ l
| [] := sublist.slnil
| (b::l) := if h : b ∈ l then
by simp[erase_dup, h]; exact sublist_cons_of_sublist _ (erase_dup_sublist l)
else
by simp[erase_dup, h]; exact cons_sublist_cons _ (erase_dup_sublist l)
theorem erase_dup_subset [decidable_eq α] (l : list α) : erase_dup l ⊆ l :=
λ a, mem_erase_dup.1
theorem subset_erase_dup [decidable_eq α] (l : list α) : l ⊆ erase_dup l :=
λ a, mem_erase_dup.2
theorem nodup_erase_dup [decidable_eq α] : ∀ l : list α, nodup (erase_dup l)
| [] := nodup_nil
| (a::l) := by by_cases a ∈ l with h; simp [h]; [
apply nodup_erase_dup,
exact nodup_cons (mt mem_erase_dup.1 h) (nodup_erase_dup _) ]
theorem erase_dup_eq_of_nodup [decidable_eq α] {l : list α} (d : nodup l) : erase_dup l = l :=
by induction d; simp *
private def dgen (a : α) : ∀ l, nodup l → nodup (map (λ b : β, (a, b)) l)
| [] h := nodup_nil
| (x::l) h :=
have dl : nodup l, from nodup_of_nodup_cons h,
have dm : nodup (map (λ b : β, (a, b)) l), from dgen l dl,
have nxin : x ∉ l, from not_mem_of_nodup_cons h,
have npin : (a, x) ∉ map (λ b, (a, b)) l, from
assume pin, absurd (mem_of_mem_map_pair₁ pin) nxin,
nodup_cons npin dm
theorem nodup_product : ∀ {l₁ : list α} {l₂ : list β}, nodup l₁ → nodup l₂ → nodup (product l₁ l₂)
| [] l₂ n₁ n₂ := nodup_nil
| (a::l₁) l₂ n₁ n₂ :=
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons n₁,
have n₃ : nodup l₁, from nodup_of_nodup_cons n₁,
have n₄ : nodup (product l₁ l₂), from nodup_product n₃ n₂,
have dm : nodup (map (λ b : β, (a, b)) l₂), from dgen a l₂ n₂,
have dsj : disjoint (map (λ b : β, (a, b)) l₂) (product l₁ l₂), from
λ p : α × β, match p with
| (a₁, b₁) :=
λ (i₁ : (a₁, b₁) ∈ map (λ b, (a, b)) l₂) (i₂ : (a₁, b₁) ∈ product l₁ l₂),
have a₁inl₁ : a₁ ∈ l₁, from mem_of_mem_product_left i₂,
have a₁ = a, from eq_of_mem_map_pair₁ i₁,
have a ∈ l₁, begin rw ←this, assumption end,
absurd this nainl₁
end,
nodup_append dm n₄ dsj
theorem nodup_filter (p : α → Prop) [decidable_pred p] :
∀ {l : list α}, nodup l → nodup (filter p l)
| [] nd := nodup_nil
| (a::l) nd :=
have nainl : a ∉ l, from not_mem_of_nodup_cons nd,
have ndl : nodup l, from nodup_of_nodup_cons nd,
have ndf : nodup (filter p l), from nodup_filter ndl,
have nainf : a ∉ filter p l, from
assume ainf, absurd (mem_of_mem_filter ainf) nainl,
by_cases
(λ pa : p a, begin rw [filter_cons_of_pos _ pa], exact (nodup_cons nainf ndf) end)
(λ npa : ¬ p a, begin rw [filter_cons_of_neg _ npa], exact ndf end)
theorem dmap_nodup_of_dinj {p : α → Prop} [h : decidable_pred p] {f : Π a, p a → β} (pdi : dinj p f) :
∀ {l : list α}, nodup l → nodup (dmap p f l)
| [] := assume P, nodup.ndnil
| (a::l) := assume Pnodup,
if pa : p a then
begin
rw [dmap_cons_of_pos pa],
apply nodup_cons,
apply (not_mem_dmap_of_dinj_of_not_mem pdi pa),
exact not_mem_of_nodup_cons Pnodup,
exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup)
end
else
begin
rw [dmap_cons_of_neg pa],
exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup)
end
theorem nodup_concat {a : α} {l : list α} (h : a ∉ l) (h' : nodup l) : nodup (concat l a) :=
by simp; exact nodup_append h' (nodup_singleton _) (disjoint_singleton.2 h)
theorem nodup_insert [decidable_eq α] {a : α} {l : list α} (h : nodup l) : nodup (insert a l) :=
by by_cases a ∈ l with h'; simp [h', h]; apply nodup_cons h' h
theorem nodup_upto : ∀ n, nodup (upto n)
| 0 := nodup_nil
| (n+1) :=
have d : nodup (upto n), from nodup_upto n,
have n : n ∉ upto n, from
assume i : n ∈ upto n, absurd (lt_of_mem_upto i) (nat.lt_irrefl n),
nodup_cons n d
theorem nodup_union [decidable_eq α] (l₁ : list α) {l₂ : list α} (h : nodup l₂) :
nodup (l₁ ∪ l₂) :=
begin
induction l₁ with a l₁ ih generalizing l₂,
{ exact h },
simp,
apply nodup_insert,
exact ih h
end
theorem nodup_inter_of_nodup [decidable_eq α] : ∀ {l₁ : list α} (l₂), nodup l₁ → nodup (l₁ ∩ l₂)
| [] l₂ d := nodup_nil
| (a::l₁) l₂ d :=
have d₁ : nodup l₁, from nodup_of_nodup_cons d,
have d₂ : nodup (l₁ ∩ l₂), from nodup_inter_of_nodup _ d₁,
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons d,
have naini : a ∉ l₁ ∩ l₂, from λ i, absurd (mem_of_mem_inter_left i) nainl₁,
by_cases
(λ ainl₂ : a ∈ l₂, begin rw [inter_cons_of_mem _ ainl₂], exact (nodup_cons naini d₂) end)
(λ nainl₂ : a ∉ l₂, begin rw [inter_cons_of_not_mem _ nainl₂], exact d₂ end)
end nodup
end list
|
f04fe1ed0fcc3ccfc19ee3e5a69d420723f37553 | 5749d8999a76f3a8fddceca1f6941981e33aaa96 | /src/topology/algebra/uniform_group.lean | 235363e75553a4fe690980bbe696b7909f883942 | [
"Apache-2.0"
] | permissive | jdsalchow/mathlib | 13ab43ef0d0515a17e550b16d09bd14b76125276 | 497e692b946d93906900bb33a51fd243e7649406 | refs/heads/master | 1,585,819,143,348 | 1,580,072,892,000 | 1,580,072,892,000 | 154,287,128 | 0 | 0 | Apache-2.0 | 1,540,281,610,000 | 1,540,281,609,000 | null | UTF-8 | Lean | false | false | 19,539 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Johannes Hölzl
Uniform structure on topological groups:
* `topological_add_group.to_uniform_space` and `topological_add_group_is_uniform` can be used to
construct a canonical uniformity for a topological add group.
* extension of ℤ-bilinear maps to complete groups (useful for ring completions)
* `add_group_with_zero_nhd`: construct the topological structure from a group with a neighbourhood
around zero. Then with `topological_add_group.to_uniform_space` one can derive a `uniform_space`.
-/
import topology.uniform_space.uniform_embedding topology.uniform_space.complete_separated
import topology.algebra.group
noncomputable theory
open_locale classical uniformity topological_space
section uniform_add_group
open filter set
variables {α : Type*} {β : Type*}
/-- A uniform (additive) group is a group in which the addition and negation are
uniformly continuous. -/
class uniform_add_group (α : Type*) [uniform_space α] [add_group α] : Prop :=
(uniform_continuous_sub : uniform_continuous (λp:α×α, p.1 - p.2))
theorem uniform_add_group.mk' {α} [uniform_space α] [add_group α]
(h₁ : uniform_continuous (λp:α×α, p.1 + p.2))
(h₂ : uniform_continuous (λp:α, -p)) : uniform_add_group α :=
⟨h₁.comp (uniform_continuous_fst.prod_mk (h₂.comp uniform_continuous_snd))⟩
variables [uniform_space α] [add_group α] [uniform_add_group α]
lemma uniform_continuous_sub : uniform_continuous (λp:α×α, p.1 - p.2) :=
uniform_add_group.uniform_continuous_sub α
lemma uniform_continuous.sub [uniform_space β] {f : β → α} {g : β → α}
(hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (λx, f x - g x) :=
uniform_continuous_sub.comp (hf.prod_mk hg)
lemma uniform_continuous.neg [uniform_space β] {f : β → α}
(hf : uniform_continuous f) : uniform_continuous (λx, - f x) :=
have uniform_continuous (λx, 0 - f x),
from uniform_continuous_const.sub hf,
by simp * at *
lemma uniform_continuous_neg : uniform_continuous (λx:α, - x) :=
uniform_continuous_id.neg
lemma uniform_continuous.add [uniform_space β] {f : β → α} {g : β → α}
(hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (λx, f x + g x) :=
have uniform_continuous (λx, f x - - g x), from hf.sub hg.neg,
by simp * at *
lemma uniform_continuous_add : uniform_continuous (λp:α×α, p.1 + p.2) :=
uniform_continuous_fst.add uniform_continuous_snd
@[priority 10]
instance uniform_add_group.to_topological_add_group : topological_add_group α :=
{ continuous_add := uniform_continuous_add.continuous,
continuous_neg := uniform_continuous_neg.continuous }
instance [uniform_space β] [add_group β] [uniform_add_group β] : uniform_add_group (α × β) :=
⟨((uniform_continuous_fst.comp uniform_continuous_fst).sub
(uniform_continuous_fst.comp uniform_continuous_snd)).prod_mk
((uniform_continuous_snd.comp uniform_continuous_fst).sub
(uniform_continuous_snd.comp uniform_continuous_snd))⟩
lemma uniformity_translate (a : α) : (𝓤 α).map (λx:α×α, (x.1 + a, x.2 + a)) = 𝓤 α :=
le_antisymm
(uniform_continuous_id.add uniform_continuous_const)
(calc 𝓤 α =
((𝓤 α).map (λx:α×α, (x.1 + -a, x.2 + -a))).map (λx:α×α, (x.1 + a, x.2 + a)) :
by simp [filter.map_map, (∘)]; exact filter.map_id.symm
... ≤ (𝓤 α).map (λx:α×α, (x.1 + a, x.2 + a)) :
filter.map_mono (uniform_continuous_id.add uniform_continuous_const))
lemma uniform_embedding_translate (a : α) : uniform_embedding (λx:α, x + a) :=
{ comap_uniformity := begin
rw [← uniformity_translate a, comap_map] {occs := occurrences.pos [1]},
rintros ⟨p₁, p₂⟩ ⟨q₁, q₂⟩,
simp [prod.eq_iff_fst_eq_snd_eq] {contextual := tt}
end,
inj := assume x y, eq_of_add_eq_add_right }
section
variables (α)
lemma uniformity_eq_comap_nhds_zero : 𝓤 α = comap (λx:α×α, x.2 - x.1) (𝓝 (0:α)) :=
begin
rw [nhds_eq_comap_uniformity, filter.comap_comap_comp],
refine le_antisymm (filter.map_le_iff_le_comap.1 _) _,
{ assume s hs,
rcases mem_uniformity_of_uniform_continuous_invariant uniform_continuous_sub hs with ⟨t, ht, hts⟩,
refine mem_map.2 (mem_sets_of_superset ht _),
rintros ⟨a, b⟩,
simpa [subset_def] using hts a b a },
{ assume s hs,
rcases mem_uniformity_of_uniform_continuous_invariant uniform_continuous_add hs with ⟨t, ht, hts⟩,
refine ⟨_, ht, _⟩,
rintros ⟨a, b⟩, simpa [subset_def] using hts 0 (b - a) a }
end
end
lemma group_separation_rel (x y : α) : (x, y) ∈ separation_rel α ↔ x - y ∈ closure ({0} : set α) :=
have embedding (λa, a + (y - x)), from (uniform_embedding_translate (y - x)).embedding,
show (x, y) ∈ ⋂₀ (𝓤 α).sets ↔ x - y ∈ closure ({0} : set α),
begin
rw [this.closure_eq_preimage_closure_image, uniformity_eq_comap_nhds_zero α, sInter_comap_sets],
simp [mem_closure_iff_nhds, inter_singleton_eq_empty]
end
lemma uniform_continuous_of_tendsto_zero [uniform_space β] [add_group β] [uniform_add_group β]
{f : α → β} [is_add_group_hom f] (h : tendsto f (𝓝 0) (𝓝 0)) :
uniform_continuous f :=
begin
have : ((λx:β×β, x.2 - x.1) ∘ (λx:α×α, (f x.1, f x.2))) = (λx:α×α, f (x.2 - x.1)),
{ simp only [is_add_group_hom.map_sub f] },
rw [uniform_continuous, uniformity_eq_comap_nhds_zero α, uniformity_eq_comap_nhds_zero β,
tendsto_comap_iff, this],
exact tendsto.comp h tendsto_comap
end
lemma uniform_continuous_of_continuous [uniform_space β] [add_group β] [uniform_add_group β]
{f : α → β} [is_add_group_hom f] (h : continuous f) :
uniform_continuous f :=
uniform_continuous_of_tendsto_zero $
suffices tendsto f (𝓝 0) (𝓝 (f 0)), by rwa [is_add_group_hom.map_zero f] at this,
h.tendsto 0
end uniform_add_group
section topological_add_comm_group
universes u v w x
open filter
variables {G : Type u} [add_comm_group G] [topological_space G] [topological_add_group G]
variable (G)
def topological_add_group.to_uniform_space : uniform_space G :=
{ uniformity := comap (λp:G×G, p.2 - p.1) (𝓝 0),
refl :=
by refine map_le_iff_le_comap.1 (le_trans _ (pure_le_nhds 0));
simp [set.subset_def] {contextual := tt},
symm :=
begin
suffices : tendsto ((λp, -p) ∘ (λp:G×G, p.2 - p.1)) (comap (λp:G×G, p.2 - p.1) (𝓝 0)) (𝓝 (-0)),
{ simpa [(∘), tendsto_comap_iff] },
exact tendsto.comp (tendsto.neg tendsto_id) tendsto_comap
end,
comp :=
begin
intros D H,
rw mem_lift'_sets,
{ rcases H with ⟨U, U_nhds, U_sub⟩,
rcases exists_nhds_half U_nhds with ⟨V, ⟨V_nhds, V_sum⟩⟩,
existsi ((λp:G×G, p.2 - p.1) ⁻¹' V),
have H : (λp:G×G, p.2 - p.1) ⁻¹' V ∈ comap (λp:G×G, p.2 - p.1) (𝓝 (0 : G)),
by existsi [V, V_nhds] ; refl,
existsi H,
have comp_rel_sub : comp_rel ((λp:G×G, p.2 - p.1) ⁻¹' V) ((λp:G×G, p.2 - p.1) ⁻¹' V) ⊆ (λp:G×G, p.2 - p.1) ⁻¹' U,
begin
intros p p_comp_rel,
rcases p_comp_rel with ⟨z, ⟨Hz1, Hz2⟩⟩,
simpa using V_sum _ _ Hz1 Hz2
end,
exact set.subset.trans comp_rel_sub U_sub },
{ exact monotone_comp_rel monotone_id monotone_id }
end,
is_open_uniformity :=
begin
intro S,
let S' := λ x, {p : G × G | p.1 = x → p.2 ∈ S},
show is_open S ↔ ∀ (x : G), x ∈ S → S' x ∈ comap (λp:G×G, p.2 - p.1) (𝓝 (0 : G)),
rw [is_open_iff_mem_nhds],
refine forall_congr (assume a, forall_congr (assume ha, _)),
rw [← nhds_translation a, mem_comap_sets, mem_comap_sets],
refine exists_congr (assume t, exists_congr (assume ht, _)),
show (λ (y : G), y - a) ⁻¹' t ⊆ S ↔ (λ (p : G × G), p.snd - p.fst) ⁻¹' t ⊆ S' a,
split,
{ rintros h ⟨x, y⟩ hx rfl, exact h hx },
{ rintros h x hx, exact @h (a, x) hx rfl }
end }
section
local attribute [instance] topological_add_group.to_uniform_space
lemma uniformity_eq_comap_nhds_zero' : 𝓤 G = comap (λp:G×G, p.2 - p.1) (𝓝 (0 : G)) := rfl
variable {G}
lemma topological_add_group_is_uniform : uniform_add_group G :=
have tendsto
((λp:(G×G), p.1 - p.2) ∘ (λp:(G×G)×(G×G), (p.1.2 - p.1.1, p.2.2 - p.2.1)))
(comap (λp:(G×G)×(G×G), (p.1.2 - p.1.1, p.2.2 - p.2.1)) ((𝓝 0).prod (𝓝 0)))
(𝓝 (0 - 0)) :=
(tendsto_fst.sub tendsto_snd).comp tendsto_comap,
begin
constructor,
rw [uniform_continuous, uniformity_prod_eq_prod, tendsto_map'_iff,
uniformity_eq_comap_nhds_zero' G, tendsto_comap_iff, prod_comap_comap_eq],
simpa [(∘)]
end
end
lemma to_uniform_space_eq {α : Type*} [u : uniform_space α] [add_comm_group α] [uniform_add_group α]:
topological_add_group.to_uniform_space α = u :=
begin
ext : 1,
show @uniformity α (topological_add_group.to_uniform_space α) = 𝓤 α,
rw [uniformity_eq_comap_nhds_zero' α, uniformity_eq_comap_nhds_zero α]
end
end topological_add_comm_group
namespace add_comm_group
section Z_bilin
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
variables [add_comm_group α] [add_comm_group β] [add_comm_group γ]
/- TODO: when modules are changed to have more explicit base ring, then change replace `is_Z_bilin`
by using `is_bilinear_map ℤ` from `tensor_product`. -/
class is_Z_bilin (f : α × β → γ) : Prop :=
(add_left : ∀ a a' b, f (a + a', b) = f (a, b) + f (a', b))
(add_right : ∀ a b b', f (a, b + b') = f (a, b) + f (a, b'))
variables (f : α × β → γ) [is_Z_bilin f]
instance is_Z_bilin.comp_hom {g : γ → δ} [add_comm_group δ] [is_add_group_hom g] :
is_Z_bilin (g ∘ f) :=
by constructor; simp [(∘), is_Z_bilin.add_left f, is_Z_bilin.add_right f, is_add_hom.map_add g]
instance is_Z_bilin.comp_swap : is_Z_bilin (f ∘ prod.swap) :=
⟨λ a a' b, is_Z_bilin.add_right f b a a',
λ a b b', is_Z_bilin.add_left f b b' a⟩
lemma is_Z_bilin.zero_left : ∀ b, f (0, b) = 0 :=
begin
intro b,
apply add_self_iff_eq_zero.1,
rw ←is_Z_bilin.add_left f,
simp
end
lemma is_Z_bilin.zero_right : ∀ a, f (a, 0) = 0 :=
is_Z_bilin.zero_left (f ∘ prod.swap)
lemma is_Z_bilin.zero : f (0, 0) = 0 :=
is_Z_bilin.zero_left f 0
lemma is_Z_bilin.neg_left : ∀ a b, f (-a, b) = -f (a, b) :=
begin
intros a b,
apply eq_of_sub_eq_zero,
rw [sub_eq_add_neg, neg_neg, ←is_Z_bilin.add_left f, neg_add_self, is_Z_bilin.zero_left f]
end
lemma is_Z_bilin.neg_right : ∀ a b, f (a, -b) = -f (a, b) :=
assume a b, is_Z_bilin.neg_left (f ∘ prod.swap) b a
lemma is_Z_bilin.sub_left : ∀ a a' b, f (a - a', b) = f (a, b) - f (a', b) :=
begin
intros,
dsimp [algebra.sub],
rw [is_Z_bilin.add_left f, is_Z_bilin.neg_left f]
end
lemma is_Z_bilin.sub_right : ∀ a b b', f (a, b - b') = f (a, b) - f (a,b') :=
assume a b b', is_Z_bilin.sub_left (f ∘ prod.swap) b b' a
end Z_bilin
end add_comm_group
open add_comm_group filter set function
section
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
-- α, β and G are abelian topological groups, G is a uniform space
variables [topological_space α] [add_comm_group α]
variables [topological_space β] [add_comm_group β]
variables {G : Type*} [uniform_space G] [add_comm_group G]
variables {ψ : α × β → G} (hψ : continuous ψ) [ψbilin : is_Z_bilin ψ]
include hψ ψbilin
lemma is_Z_bilin.tendsto_zero_left (x₁ : α) : tendsto ψ (𝓝 (x₁, 0)) (𝓝 0) :=
begin
have := hψ.tendsto (x₁, 0),
rwa [is_Z_bilin.zero_right ψ] at this
end
lemma is_Z_bilin.tendsto_zero_right (y₁ : β) : tendsto ψ (𝓝 (0, y₁)) (𝓝 0) :=
begin
have := hψ.tendsto (0, y₁),
rwa [is_Z_bilin.zero_left ψ] at this
end
end
section
variables {α : Type*} {β : Type*}
variables [topological_space α] [add_comm_group α] [topological_add_group α]
-- β is a dense subgroup of α, inclusion is denoted by e
variables [topological_space β] [add_comm_group β]
variables {e : β → α} [is_add_group_hom e] (de : dense_inducing e)
include de
lemma tendsto_sub_comap_self (x₀ : α) :
tendsto (λt:β×β, t.2 - t.1) (comap (λp:β×β, (e p.1, e p.2)) $ 𝓝 (x₀, x₀)) (𝓝 0) :=
begin
have comm : (λx:α×α, x.2-x.1) ∘ (λt:β×β, (e t.1, e t.2)) = e ∘ (λt:β×β, t.2 - t.1),
{ ext t,
change e t.2 - e t.1 = e (t.2 - t.1),
rwa ← is_add_group_hom.map_sub e t.2 t.1 },
have lim : tendsto (λ x : α × α, x.2-x.1) (𝓝 (x₀, x₀)) (𝓝 (e 0)),
{ have := (continuous_sub.comp continuous_swap).tendsto (x₀, x₀),
simpa [-sub_eq_add_neg, sub_self, eq.symm (is_add_group_hom.map_zero e)] using this },
have := de.tendsto_comap_nhds_nhds lim comm,
simp [-sub_eq_add_neg, this]
end
end
namespace dense_inducing
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
variables {G : Type*}
-- β is a dense subgroup of α, inclusion is denoted by e
-- δ is a dense subgroup of γ, inclusion is denoted by f
variables [topological_space α] [add_comm_group α] [topological_add_group α]
variables [topological_space β] [add_comm_group β] [topological_add_group β]
variables [topological_space γ] [add_comm_group γ] [topological_add_group γ]
variables [topological_space δ] [add_comm_group δ] [topological_add_group δ]
variables [uniform_space G] [add_comm_group G] [uniform_add_group G] [separated G] [complete_space G]
variables {e : β → α} [is_add_group_hom e] (de : dense_inducing e)
variables {f : δ → γ} [is_add_group_hom f] (df : dense_inducing f)
variables {φ : β × δ → G} (hφ : continuous φ) [bilin : is_Z_bilin φ]
include de df hφ bilin
variables {W' : set G} (W'_nhd : W' ∈ 𝓝 (0 : G))
include W'_nhd
private lemma extend_Z_bilin_aux (x₀ : α) (y₁ : δ) :
∃ U₂ ∈ comap e (𝓝 x₀), ∀ x x' ∈ U₂, φ (x' - x, y₁) ∈ W' :=
begin
let Nx := 𝓝 x₀,
let ee := λ u : β × β, (e u.1, e u.2),
have lim1 : tendsto (λ a : β × β, (a.2 - a.1, y₁)) (filter.prod (comap e Nx) (comap e Nx)) (𝓝 (0, y₁)),
{ have := tendsto.prod_mk (tendsto_sub_comap_self de x₀) (tendsto_const_nhds : tendsto (λ (p : β × β), y₁) (comap ee $ 𝓝 (x₀, x₀)) (𝓝 y₁)),
rw [nhds_prod_eq, prod_comap_comap_eq, ←nhds_prod_eq],
exact (this : _) },
have lim := tendsto.comp (is_Z_bilin.tendsto_zero_right hφ y₁) lim1,
rw tendsto_prod_self_iff at lim,
exact lim W' W'_nhd,
end
private lemma extend_Z_bilin_key (x₀ : α) (y₀ : γ) :
∃ U ∈ comap e (𝓝 x₀), ∃ V ∈ comap f (𝓝 y₀),
∀ x x' ∈ U, ∀ y y' ∈ V, φ (x', y') - φ (x, y) ∈ W' :=
begin
let Nx := 𝓝 x₀,
let Ny := 𝓝 y₀,
let dp := dense_inducing.prod de df,
let ee := λ u : β × β, (e u.1, e u.2),
let ff := λ u : δ × δ, (f u.1, f u.2),
have lim_φ : filter.tendsto φ (𝓝 (0, 0)) (𝓝 0),
{ have := hφ.tendsto (0, 0),
rwa [is_Z_bilin.zero φ] at this },
have lim_φ_sub_sub : tendsto (λ (p : (β × β) × (δ × δ)), φ (p.1.2 - p.1.1, p.2.2 - p.2.1))
(filter.prod (comap ee $ 𝓝 (x₀, x₀)) (comap ff $ 𝓝 (y₀, y₀))) (𝓝 0),
{ have lim_sub_sub : tendsto (λ (p : (β × β) × δ × δ), (p.1.2 - p.1.1, p.2.2 - p.2.1))
(filter.prod (comap ee (𝓝 (x₀, x₀))) (comap ff (𝓝 (y₀, y₀)))) (filter.prod (𝓝 0) (𝓝 0)),
{ have := filter.prod_mono (tendsto_sub_comap_self de x₀) (tendsto_sub_comap_self df y₀),
rwa prod_map_map_eq at this },
rw ← nhds_prod_eq at lim_sub_sub,
exact tendsto.comp lim_φ lim_sub_sub },
rcases exists_nhds_quarter W'_nhd with ⟨W, W_nhd, W4⟩,
have : ∃ U₁ ∈ comap e (𝓝 x₀), ∃ V₁ ∈ comap f (𝓝 y₀),
∀ x x' ∈ U₁, ∀ y y' ∈ V₁, φ (x'-x, y'-y) ∈ W,
{ have := tendsto_prod_iff.1 lim_φ_sub_sub W W_nhd,
repeat { rw [nhds_prod_eq, ←prod_comap_comap_eq] at this },
rcases this with ⟨U, U_in, V, V_in, H⟩,
rw [mem_prod_same_iff] at U_in V_in,
rcases U_in with ⟨U₁, U₁_in, HU₁⟩,
rcases V_in with ⟨V₁, V₁_in, HV₁⟩,
existsi [U₁, U₁_in, V₁, V₁_in],
intros x x' x_in x'_in y y' y_in y'_in,
exact H _ _ (HU₁ (mk_mem_prod x_in x'_in)) (HV₁ (mk_mem_prod y_in y'_in)) },
rcases this with ⟨U₁, U₁_nhd, V₁, V₁_nhd, H⟩,
have : ∃ x₁, x₁ ∈ U₁ := exists_mem_of_ne_empty
(forall_sets_ne_empty_iff_ne_bot.2 de.comap_nhds_ne_bot U₁ U₁_nhd),
rcases this with ⟨x₁, x₁_in⟩,
have : ∃ y₁, y₁ ∈ V₁ := exists_mem_of_ne_empty
(forall_sets_ne_empty_iff_ne_bot.2 df.comap_nhds_ne_bot V₁ V₁_nhd),
rcases this with ⟨y₁, y₁_in⟩,
rcases (extend_Z_bilin_aux de df hφ W_nhd x₀ y₁) with ⟨U₂, U₂_nhd, HU⟩,
rcases (extend_Z_bilin_aux df de (hφ.comp continuous_swap) W_nhd y₀ x₁) with ⟨V₂, V₂_nhd, HV⟩,
existsi [U₁ ∩ U₂, inter_mem_sets U₁_nhd U₂_nhd,
V₁ ∩ V₂, inter_mem_sets V₁_nhd V₂_nhd],
rintros x x' ⟨xU₁, xU₂⟩ ⟨x'U₁, x'U₂⟩ y y' ⟨yV₁, yV₂⟩ ⟨y'V₁, y'V₂⟩,
have key_formula : φ(x', y') - φ(x, y) = φ(x' - x, y₁) + φ(x' - x, y' - y₁) + φ(x₁, y' - y) + φ(x - x₁, y' - y),
{ repeat { rw is_Z_bilin.sub_left φ },
repeat { rw is_Z_bilin.sub_right φ },
apply eq_of_sub_eq_zero,
simp },
rw key_formula,
have h₁ := HU x x' xU₂ x'U₂,
have h₂ := H x x' xU₁ x'U₁ y₁ y' y₁_in y'V₁,
have h₃ := HV y y' yV₂ y'V₂,
have h₄ := H x₁ x x₁_in xU₁ y y' yV₁ y'V₁,
exact W4 h₁ h₂ h₃ h₄
end
omit W'_nhd
open dense_inducing
/-- Bourbaki GT III.6.5 Theorem I:
ℤ-bilinear continuous maps from dense images into a complete Hausdorff group extend by continuity.
Note: Bourbaki assumes that α and β are also complete Hausdorff, but this is not necessary. -/
theorem extend_Z_bilin : continuous (extend (de.prod df) φ) :=
begin
refine continuous_extend_of_cauchy _ _,
rintro ⟨x₀, y₀⟩,
split,
{ apply map_ne_bot,
apply comap_ne_bot,
intros U h,
rcases exists_mem_of_ne_empty (mem_closure_iff_nhds.1 ((de.prod df).dense (x₀, y₀)) U h)
with ⟨x, x_in, ⟨z, z_x⟩⟩,
existsi z,
cc },
{ suffices : map (λ (p : (β × δ) × (β × δ)), φ p.2 - φ p.1)
(comap (λ (p : (β × δ) × β × δ), ((e p.1.1, f p.1.2), (e p.2.1, f p.2.2)))
(filter.prod (𝓝 (x₀, y₀)) (𝓝 (x₀, y₀)))) ≤ 𝓝 0,
by rwa [uniformity_eq_comap_nhds_zero G, prod_map_map_eq, ←map_le_iff_le_comap, filter.map_map,
prod_comap_comap_eq],
intros W' W'_nhd,
have key := extend_Z_bilin_key de df hφ W'_nhd x₀ y₀,
rcases key with ⟨U, U_nhd, V, V_nhd, h⟩,
rw mem_comap_sets at U_nhd,
rcases U_nhd with ⟨U', U'_nhd, U'_sub⟩,
rw mem_comap_sets at V_nhd,
rcases V_nhd with ⟨V', V'_nhd, V'_sub⟩,
rw [mem_map, mem_comap_sets, nhds_prod_eq],
existsi set.prod (set.prod U' V') (set.prod U' V'),
rw mem_prod_same_iff,
simp only [exists_prop],
split,
{ change U' ∈ 𝓝 x₀ at U'_nhd,
change V' ∈ 𝓝 y₀ at V'_nhd,
have := prod_mem_prod U'_nhd V'_nhd,
tauto },
{ intros p h',
simp only [set.mem_preimage, set.prod_mk_mem_set_prod_eq] at h',
rcases p with ⟨⟨x, y⟩, ⟨x', y'⟩⟩,
apply h ; tauto } }
end
end dense_inducing
|
b203b4600c49ad8cc1a999b41ea132b2244f8ff9 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/data/set/default.lean | a0c5c9aa12c38fba7499c9ab25ebb5fb0cf34db3 | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 58 | lean | import data.set.lattice data.set.finite data.set.intervals |
80e26a00c7f4e390edd5ba7cce13c1df13f6aa25 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/dynamics/periodic_pts.lean | e18317d1d69c41d165f178f9a826bdab82dc45f9 | [
"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 | 14,975 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import data.nat.prime
import dynamics.fixed_points.basic
import data.pnat.basic
import data.set.lattice
/-!
# Periodic points
A point `x : α` is a periodic point of `f : α → α` of period `n` if `f^[n] x = x`.
## Main definitions
* `is_periodic_pt f n x` : `x` is a periodic point of `f` of period `n`, i.e. `f^[n] x = x`.
We do not require `n > 0` in the definition.
* `pts_of_period f n` : the set `{x | is_periodic_pt f n x}`. Note that `n` is not required to
be the minimal period of `x`.
* `periodic_pts f` : the set of all periodic points of `f`.
* `minimal_period f x` : the minimal period of a point `x` under an endomorphism `f` or zero
if `x` is not a periodic point of `f`.
## Main statements
We provide “dot syntax”-style operations on terms of the form `h : is_periodic_pt f n x` including
arithmetic operations on `n` and `h.map (hg : semiconj_by g f f')`. We also prove that `f`
is bijective on each set `pts_of_period f n` and on `periodic_pts f`. Finally, we prove that `x`
is a periodic point of `f` of period `n` if and only if `minimal_period f x | n`.
## References
* https://en.wikipedia.org/wiki/Periodic_point
-/
open set
namespace function
variables {α : Type*} {β : Type*} {f fa : α → α} {fb : β → β} {x y : α} {m n : ℕ}
/-- A point `x` is a periodic point of `f : α → α` of period `n` if `f^[n] x = x`.
Note that we do not require `0 < n` in this definition. Many theorems about periodic points
need this assumption. -/
def is_periodic_pt (f : α → α) (n : ℕ) (x : α) := is_fixed_pt (f^[n]) x
/-- A fixed point of `f` is a periodic point of `f` of any prescribed period. -/
lemma is_fixed_pt.is_periodic_pt (hf : is_fixed_pt f x) (n : ℕ) : is_periodic_pt f n x :=
hf.iterate n
/-- For the identity map, all points are periodic. -/
lemma is_periodic_id (n : ℕ) (x : α) : is_periodic_pt id n x := (is_fixed_pt_id x).is_periodic_pt n
/-- Any point is a periodic point of period `0`. -/
lemma is_periodic_pt_zero (f : α → α) (x : α) : is_periodic_pt f 0 x := is_fixed_pt_id x
namespace is_periodic_pt
instance [decidable_eq α] {f : α → α} {n : ℕ} {x : α} : decidable (is_periodic_pt f n x) :=
is_fixed_pt.decidable
protected lemma is_fixed_pt (hf : is_periodic_pt f n x) : is_fixed_pt (f^[n]) x := hf
protected lemma map (hx : is_periodic_pt fa n x) {g : α → β} (hg : semiconj g fa fb) :
is_periodic_pt fb n (g x) :=
hx.map (hg.iterate_right n)
lemma apply_iterate (hx : is_periodic_pt f n x) (m : ℕ) : is_periodic_pt f n (f^[m] x) :=
hx.map $ commute.iterate_self f m
protected lemma apply (hx : is_periodic_pt f n x) : is_periodic_pt f n (f x) :=
hx.apply_iterate 1
protected lemma add (hn : is_periodic_pt f n x) (hm : is_periodic_pt f m x) :
is_periodic_pt f (n + m) x :=
by { rw [is_periodic_pt, iterate_add], exact hn.comp hm }
lemma left_of_add (hn : is_periodic_pt f (n + m) x) (hm : is_periodic_pt f m x) :
is_periodic_pt f n x :=
by { rw [is_periodic_pt, iterate_add] at hn, exact hn.left_of_comp hm }
lemma right_of_add (hn : is_periodic_pt f (n + m) x) (hm : is_periodic_pt f n x) :
is_periodic_pt f m x :=
by { rw add_comm at hn, exact hn.left_of_add hm }
protected lemma sub (hm : is_periodic_pt f m x) (hn : is_periodic_pt f n x) :
is_periodic_pt f (m - n) x :=
begin
cases le_total n m with h h,
{ refine left_of_add _ hn,
rwa [tsub_add_cancel_of_le h] },
{ rw [tsub_eq_zero_iff_le.mpr h],
apply is_periodic_pt_zero }
end
protected lemma mul_const (hm : is_periodic_pt f m x) (n : ℕ) : is_periodic_pt f (m * n) x :=
by simp only [is_periodic_pt, iterate_mul, hm.is_fixed_pt.iterate n]
protected lemma const_mul (hm : is_periodic_pt f m x) (n : ℕ) : is_periodic_pt f (n * m) x :=
by simp only [mul_comm n, hm.mul_const n]
lemma trans_dvd (hm : is_periodic_pt f m x) {n : ℕ} (hn : m ∣ n) : is_periodic_pt f n x :=
let ⟨k, hk⟩ := hn in hk.symm ▸ hm.mul_const k
protected lemma iterate (hf : is_periodic_pt f n x) (m : ℕ) : is_periodic_pt (f^[m]) n x :=
begin
rw [is_periodic_pt, ← iterate_mul, mul_comm, iterate_mul],
exact hf.is_fixed_pt.iterate m
end
lemma comp {g : α → α} (hco : commute f g) (hf : is_periodic_pt f n x) (hg : is_periodic_pt g n x) :
is_periodic_pt (f ∘ g) n x :=
by { rw [is_periodic_pt, hco.comp_iterate], exact hf.comp hg }
lemma comp_lcm {g : α → α} (hco : commute f g) (hf : is_periodic_pt f m x)
(hg : is_periodic_pt g n x) :
is_periodic_pt (f ∘ g) (nat.lcm m n) x :=
(hf.trans_dvd $ nat.dvd_lcm_left _ _).comp hco (hg.trans_dvd $ nat.dvd_lcm_right _ _)
lemma left_of_comp {g : α → α} (hco : commute f g) (hfg : is_periodic_pt (f ∘ g) n x)
(hg : is_periodic_pt g n x) : is_periodic_pt f n x :=
begin
rw [is_periodic_pt, hco.comp_iterate] at hfg,
exact hfg.left_of_comp hg
end
lemma iterate_mod_apply (h : is_periodic_pt f n x) (m : ℕ) :
f^[m % n] x = (f^[m] x) :=
by conv_rhs { rw [← nat.mod_add_div m n, iterate_add_apply, (h.mul_const _).eq] }
protected lemma mod (hm : is_periodic_pt f m x) (hn : is_periodic_pt f n x) :
is_periodic_pt f (m % n) x :=
(hn.iterate_mod_apply m).trans hm
protected lemma gcd (hm : is_periodic_pt f m x) (hn : is_periodic_pt f n x) :
is_periodic_pt f (m.gcd n) x :=
begin
revert hm hn,
refine nat.gcd.induction m n (λ n h0 hn, _) (λ m n hm ih hm hn, _),
{ rwa [nat.gcd_zero_left], },
{ rw [nat.gcd_rec],
exact ih (hn.mod hm) hm }
end
/-- If `f` sends two periodic points `x` and `y` of the same positive period to the same point,
then `x = y`. For a similar statement about points of different periods see `eq_of_apply_eq`. -/
lemma eq_of_apply_eq_same (hx : is_periodic_pt f n x) (hy : is_periodic_pt f n y) (hn : 0 < n)
(h : f x = f y) :
x = y :=
by rw [← hx.eq, ← hy.eq, ← iterate_pred_comp_of_pos f hn, comp_app, h]
/-- If `f` sends two periodic points `x` and `y` of positive periods to the same point,
then `x = y`. -/
lemma eq_of_apply_eq (hx : is_periodic_pt f m x) (hy : is_periodic_pt f n y) (hm : 0 < m)
(hn : 0 < n) (h : f x = f y) :
x = y :=
(hx.mul_const n).eq_of_apply_eq_same (hy.const_mul m) (mul_pos hm hn) h
end is_periodic_pt
/-- The set of periodic points of a given (possibly non-minimal) period. -/
def pts_of_period (f : α → α) (n : ℕ) : set α := {x : α | is_periodic_pt f n x}
@[simp] lemma mem_pts_of_period : x ∈ pts_of_period f n ↔ is_periodic_pt f n x :=
iff.rfl
lemma semiconj.maps_to_pts_of_period {g : α → β} (h : semiconj g fa fb) (n : ℕ) :
maps_to g (pts_of_period fa n) (pts_of_period fb n) :=
(h.iterate_right n).maps_to_fixed_pts
lemma bij_on_pts_of_period (f : α → α) {n : ℕ} (hn : 0 < n) :
bij_on f (pts_of_period f n) (pts_of_period f n) :=
⟨(commute.refl f).maps_to_pts_of_period n,
λ x hx y hy hxy, hx.eq_of_apply_eq_same hy hn hxy,
λ x hx, ⟨f^[n.pred] x, hx.apply_iterate _,
by rw [← comp_app f, comp_iterate_pred_of_pos f hn, hx.eq]⟩⟩
lemma directed_pts_of_period_pnat (f : α → α) : directed (⊆) (λ n : ℕ+, pts_of_period f n) :=
λ m n, ⟨m * n, λ x hx, hx.mul_const n, λ x hx, hx.const_mul m⟩
/-- The set of periodic points of a map `f : α → α`. -/
def periodic_pts (f : α → α) : set α := {x : α | ∃ n > 0, is_periodic_pt f n x}
lemma mk_mem_periodic_pts (hn : 0 < n) (hx : is_periodic_pt f n x) :
x ∈ periodic_pts f :=
⟨n, hn, hx⟩
lemma mem_periodic_pts : x ∈ periodic_pts f ↔ ∃ n > 0, is_periodic_pt f n x := iff.rfl
variable (f)
lemma bUnion_pts_of_period : (⋃ n > 0, pts_of_period f n) = periodic_pts f :=
set.ext $ λ x, by simp [mem_periodic_pts]
lemma Union_pnat_pts_of_period : (⋃ n : ℕ+, pts_of_period f n) = periodic_pts f :=
supr_subtype.trans $ bUnion_pts_of_period f
lemma bij_on_periodic_pts : bij_on f (periodic_pts f) (periodic_pts f) :=
Union_pnat_pts_of_period f ▸
bij_on_Union_of_directed (directed_pts_of_period_pnat f) (λ i, bij_on_pts_of_period f i.pos)
variable {f}
lemma semiconj.maps_to_periodic_pts {g : α → β} (h : semiconj g fa fb) :
maps_to g (periodic_pts fa) (periodic_pts fb) :=
λ x ⟨n, hn, hx⟩, ⟨n, hn, hx.map h⟩
open_locale classical
noncomputable theory
/-- Minimal period of a point `x` under an endomorphism `f`. If `x` is not a periodic point of `f`,
then `minimal_period f x = 0`. -/
def minimal_period (f : α → α) (x : α) :=
if h : x ∈ periodic_pts f then nat.find h else 0
lemma is_periodic_pt_minimal_period (f : α → α) (x : α) :
is_periodic_pt f (minimal_period f x) x :=
begin
delta minimal_period,
split_ifs with hx,
{ exact (nat.find_spec hx).snd },
{ exact is_periodic_pt_zero f x }
end
lemma iterate_eq_mod_minimal_period : f^[n] x = (f^[n % minimal_period f x] x) :=
((is_periodic_pt_minimal_period f x).iterate_mod_apply n).symm
lemma minimal_period_pos_of_mem_periodic_pts (hx : x ∈ periodic_pts f) :
0 < minimal_period f x :=
by simp only [minimal_period, dif_pos hx, (nat.find_spec hx).fst.lt]
lemma is_periodic_pt.minimal_period_pos (hn : 0 < n) (hx : is_periodic_pt f n x) :
0 < minimal_period f x :=
minimal_period_pos_of_mem_periodic_pts $ mk_mem_periodic_pts hn hx
lemma minimal_period_pos_iff_mem_periodic_pts :
0 < minimal_period f x ↔ x ∈ periodic_pts f :=
⟨not_imp_not.1 $ λ h,
by simp only [minimal_period, dif_neg h, lt_irrefl 0, not_false_iff],
minimal_period_pos_of_mem_periodic_pts⟩
lemma is_periodic_pt.minimal_period_le (hn : 0 < n) (hx : is_periodic_pt f n x) :
minimal_period f x ≤ n :=
begin
rw [minimal_period, dif_pos (mk_mem_periodic_pts hn hx)],
exact nat.find_min' (mk_mem_periodic_pts hn hx) ⟨hn, hx⟩
end
lemma minimal_period_id : minimal_period id x = 1 :=
((is_periodic_id _ _ ).minimal_period_le nat.one_pos).antisymm
(nat.succ_le_of_lt ((is_periodic_id _ _ ).minimal_period_pos nat.one_pos))
lemma is_fixed_point_iff_minimal_period_eq_one : minimal_period f x = 1 ↔ is_fixed_pt f x :=
begin
refine ⟨λ h, _, λ h, _⟩,
{ rw ← iterate_one f,
refine function.is_periodic_pt.is_fixed_pt _,
rw ← h,
exact is_periodic_pt_minimal_period f x },
{ exact ((h.is_periodic_pt 1).minimal_period_le nat.one_pos).antisymm
(nat.succ_le_of_lt ((h.is_periodic_pt 1).minimal_period_pos nat.one_pos)) }
end
lemma is_periodic_pt.eq_zero_of_lt_minimal_period (hx : is_periodic_pt f n x)
(hn : n < minimal_period f x) :
n = 0 :=
eq.symm $ (eq_or_lt_of_le $ n.zero_le).resolve_right $ λ hn0,
not_lt.2 (hx.minimal_period_le hn0) hn
lemma not_is_periodic_pt_of_pos_of_lt_minimal_period :
∀ {n: ℕ} (n0 : n ≠ 0) (hn : n < minimal_period f x), ¬ is_periodic_pt f n x
| 0 n0 _ := (n0 rfl).elim
| (n + 1) _ hn := λ hp, nat.succ_ne_zero _ (hp.eq_zero_of_lt_minimal_period hn)
lemma is_periodic_pt.minimal_period_dvd (hx : is_periodic_pt f n x) :
minimal_period f x ∣ n :=
(eq_or_lt_of_le $ n.zero_le).elim (λ hn0, hn0 ▸ dvd_zero _) $ λ hn0,
(nat.dvd_iff_mod_eq_zero _ _).2 $
(hx.mod $ is_periodic_pt_minimal_period f x).eq_zero_of_lt_minimal_period $
nat.mod_lt _ $ hx.minimal_period_pos hn0
lemma is_periodic_pt_iff_minimal_period_dvd :
is_periodic_pt f n x ↔ minimal_period f x ∣ n :=
⟨is_periodic_pt.minimal_period_dvd, λ h, (is_periodic_pt_minimal_period f x).trans_dvd h⟩
open nat
lemma minimal_period_eq_minimal_period_iff {g : β → β} {y : β} :
minimal_period f x = minimal_period g y ↔ ∀ n, is_periodic_pt f n x ↔ is_periodic_pt g n y :=
by simp_rw [is_periodic_pt_iff_minimal_period_dvd, dvd_right_iff_eq]
lemma minimal_period_eq_prime {p : ℕ} [hp : fact p.prime] (hper : is_periodic_pt f p x)
(hfix : ¬ is_fixed_pt f x) : minimal_period f x = p :=
(hp.out.eq_one_or_self_of_dvd _ (hper.minimal_period_dvd)).resolve_left
(mt is_fixed_point_iff_minimal_period_eq_one.1 hfix)
lemma minimal_period_eq_prime_pow {p k : ℕ} [hp : fact p.prime] (hk : ¬ is_periodic_pt f (p ^ k) x)
(hk1 : is_periodic_pt f (p ^ (k + 1)) x) : minimal_period f x = p ^ (k + 1) :=
begin
apply nat.eq_prime_pow_of_dvd_least_prime_pow hp.out;
rwa ← is_periodic_pt_iff_minimal_period_dvd
end
lemma commute.minimal_period_of_comp_dvd_lcm {g : α → α} (h : function.commute f g) :
minimal_period (f ∘ g) x ∣ nat.lcm (minimal_period f x) (minimal_period g x) :=
begin
rw [← is_periodic_pt_iff_minimal_period_dvd],
exact (is_periodic_pt_minimal_period f x).comp_lcm h (is_periodic_pt_minimal_period g x)
end
lemma commute.minimal_period_of_comp_dvd_mul {g : α → α} (h : function.commute f g) :
minimal_period (f ∘ g) x ∣ (minimal_period f x) * (minimal_period g x) :=
dvd_trans h.minimal_period_of_comp_dvd_lcm (lcm_dvd_mul _ _)
lemma commute.minimal_period_of_comp_eq_mul_of_coprime {g : α → α} (h : function.commute f g)
(hco : coprime (minimal_period f x) (minimal_period g x)) :
minimal_period (f ∘ g) x = (minimal_period f x) * (minimal_period g x) :=
begin
apply dvd_antisymm (h.minimal_period_of_comp_dvd_mul),
suffices : ∀ {f g : α → α}, commute f g → coprime (minimal_period f x) (minimal_period g x) →
minimal_period f x ∣ minimal_period (f ∘ g) x,
from hco.mul_dvd_of_dvd_of_dvd (this h hco) (h.comp_eq.symm ▸ this h.symm hco.symm),
clear hco h f g,
intros f g h hco,
refine hco.dvd_of_dvd_mul_left (is_periodic_pt.left_of_comp h _ _).minimal_period_dvd,
{ exact (is_periodic_pt_minimal_period _ _).const_mul _ },
{ exact (is_periodic_pt_minimal_period _ _).mul_const _ }
end
private lemma minimal_period_iterate_eq_div_gcd_aux (h : 0 < gcd (minimal_period f x) n) :
minimal_period (f ^[n]) x = minimal_period f x / nat.gcd (minimal_period f x) n :=
begin
apply nat.dvd_antisymm,
{ apply is_periodic_pt.minimal_period_dvd,
rw [is_periodic_pt, is_fixed_pt, ← iterate_mul, ← nat.mul_div_assoc _ (gcd_dvd_left _ _),
mul_comm, nat.mul_div_assoc _ (gcd_dvd_right _ _), mul_comm, iterate_mul],
exact (is_periodic_pt_minimal_period f x).iterate _ },
{ apply coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd h),
apply dvd_of_mul_dvd_mul_right h,
rw [nat.div_mul_cancel (gcd_dvd_left _ _), mul_assoc, nat.div_mul_cancel (gcd_dvd_right _ _),
mul_comm],
apply is_periodic_pt.minimal_period_dvd,
rw [is_periodic_pt, is_fixed_pt, iterate_mul],
exact is_periodic_pt_minimal_period _ _ }
end
lemma minimal_period_iterate_eq_div_gcd (h : n ≠ 0) :
minimal_period (f ^[n]) x = minimal_period f x / nat.gcd (minimal_period f x) n :=
minimal_period_iterate_eq_div_gcd_aux $ gcd_pos_of_pos_right _ (nat.pos_of_ne_zero h)
lemma minimal_period_iterate_eq_div_gcd' (h : x ∈ periodic_pts f) :
minimal_period (f ^[n]) x = minimal_period f x / nat.gcd (minimal_period f x) n :=
minimal_period_iterate_eq_div_gcd_aux $
gcd_pos_of_pos_left n (minimal_period_pos_iff_mem_periodic_pts.mpr h)
end function
|
7c7f7e85981e0b4204953dd4135f1498e89c5dd9 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/measure_theory/measure/outer_measure.lean | d4f4103e491304362d03ccd4871b3760760c52f5 | [
"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 | 66,961 | 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 analysis.specific_limits.basic
import measure_theory.pi_system
import data.countable.basic
import data.fin.vec_notation
/-!
# Outer Measures
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
An outer measure is a function `μ : set α → ℝ≥0∞`, from the powerset of a type to the extended
nonnegative real numbers that satisfies the following conditions:
1. `μ ∅ = 0`;
2. `μ` is monotone;
3. `μ` is countably subadditive. This means that the outer measure of a countable union is at most
the sum of the outer measure on the individual sets.
Note that we do not need `α` to be measurable to define an outer measure.
The outer measures on a type `α` form a complete lattice.
Given an arbitrary function `m : set α → ℝ≥0∞` that sends `∅` to `0` we can define an outer
measure on `α` that on `s` is defined to be the infimum of `∑ᵢ, m (sᵢ)` for all collections of sets
`sᵢ` that cover `s`. This is the unique maximal outer measure that is at most the given function.
We also define this for functions `m` defined on a subset of `set α`, by treating the function as
having value `∞` outside its domain.
Given an outer measure `m`, the Carathéodory-measurable sets are the sets `s` such that
for all sets `t` we have `m t = m (t ∩ s) + m (t \ s)`. This forms a measurable space.
## Main definitions and statements
* `outer_measure.bounded_by` is the greatest outer measure that is at most the given function.
If you know that the given functions sends `∅` to `0`, then `outer_measure.of_function` is a
special case.
* `caratheodory` is the Carathéodory-measurable space of an outer measure.
* `Inf_eq_of_function_Inf_gen` is a characterization of the infimum of outer measures.
* `induced_outer_measure` is the measure induced by a function on a subset of `set α`
## References
* <https://en.wikipedia.org/wiki/Outer_measure>
* <https://en.wikipedia.org/wiki/Carath%C3%A9odory%27s_criterion>
## Tags
outer measure, Carathéodory-measurable, Carathéodory's criterion
-/
noncomputable theory
open set function filter topological_space (second_countable_topology)
open_locale classical big_operators nnreal topology ennreal measure_theory
namespace measure_theory
/-- An outer measure is a countably subadditive monotone function that sends `∅` to `0`. -/
structure outer_measure (α : Type*) :=
(measure_of : set α → ℝ≥0∞)
(empty : measure_of ∅ = 0)
(mono : ∀{s₁ s₂}, s₁ ⊆ s₂ → measure_of s₁ ≤ measure_of s₂)
(Union_nat : ∀(s:ℕ → set α), measure_of (⋃i, s i) ≤ ∑'i, measure_of (s i))
namespace outer_measure
section basic
variables {α β R R' : Type*} {ms : set (outer_measure α)} {m : outer_measure α}
instance : has_coe_to_fun (outer_measure α) (λ _, set α → ℝ≥0∞) := ⟨λ m, m.measure_of⟩
@[simp] lemma measure_of_eq_coe (m : outer_measure α) : m.measure_of = m := rfl
@[simp] theorem empty' (m : outer_measure α) : m ∅ = 0 := m.empty
theorem mono' (m : outer_measure α) {s₁ s₂}
(h : s₁ ⊆ s₂) : m s₁ ≤ m s₂ := m.mono h
theorem mono_null (m : outer_measure α) {s t} (h : s ⊆ t) (ht : m t = 0) : m s = 0 :=
nonpos_iff_eq_zero.mp $ ht ▸ m.mono' h
lemma pos_of_subset_ne_zero (m : outer_measure α) {a b : set α} (hs : a ⊆ b) (hnz : m a ≠ 0) :
0 < m b :=
(lt_of_lt_of_le (pos_iff_ne_zero.mpr hnz) (m.mono hs))
protected theorem Union (m : outer_measure α) {β} [countable β] (s : β → set α) :
m (⋃ i, s i) ≤ ∑' i, m (s i) :=
rel_supr_tsum m m.empty (≤) m.Union_nat s
lemma Union_null [countable β] (m : outer_measure α) {s : β → set α} (h : ∀ i, m (s i) = 0) :
m (⋃ i, s i) = 0 :=
by simpa [h] using m.Union s
@[simp] lemma Union_null_iff [countable β] (m : outer_measure α) {s : β → set α} :
m (⋃ i, s i) = 0 ↔ ∀ i, m (s i) = 0 :=
⟨λ h i, m.mono_null (subset_Union _ _) h, m.Union_null⟩
/-- A version of `Union_null_iff` for unions indexed by Props.
TODO: in the long run it would be better to combine this with `Union_null_iff` by
generalising to `Sort`. -/
@[simp] lemma Union_null_iff' (m : outer_measure α) {ι : Prop} {s : ι → set α} :
m (⋃ i, s i) = 0 ↔ ∀ i, m (s i) = 0 :=
by by_cases i : ι; simp [i]
lemma bUnion_null_iff (m : outer_measure α) {s : set β} (hs : s.countable) {t : β → set α} :
m (⋃ i ∈ s, t i) = 0 ↔ ∀ i ∈ s, m (t i) = 0 :=
by { haveI := hs.to_encodable, rw [bUnion_eq_Union, Union_null_iff, set_coe.forall'] }
lemma sUnion_null_iff (m : outer_measure α) {S : set (set α)} (hS : S.countable) :
m (⋃₀ S) = 0 ↔ ∀ s ∈ S, m s = 0 :=
by rw [sUnion_eq_bUnion, m.bUnion_null_iff hS]
protected lemma Union_finset (m : outer_measure α) (s : β → set α) (t : finset β) :
m (⋃i ∈ t, s i) ≤ ∑ i in t, m (s i) :=
rel_supr_sum m m.empty (≤) m.Union_nat s t
protected lemma union (m : outer_measure α) (s₁ s₂ : set α) :
m (s₁ ∪ s₂) ≤ m s₁ + m s₂ :=
rel_sup_add m m.empty (≤) m.Union_nat s₁ s₂
/-- If a set has zero measure in a neighborhood of each of its points, then it has zero measure
in a second-countable space. -/
lemma null_of_locally_null [topological_space α] [second_countable_topology α] (m : outer_measure α)
(s : set α) (hs : ∀ x ∈ s, ∃ u ∈ 𝓝[s] x, m u = 0) :
m s = 0 :=
begin
choose! u hxu hu₀ using hs,
obtain ⟨t, ts, t_count, ht⟩ : ∃ t ⊆ s, t.countable ∧ s ⊆ ⋃ x ∈ t, u x :=
topological_space.countable_cover_nhds_within hxu,
apply m.mono_null ht,
exact (m.bUnion_null_iff t_count).2 (λ x hx, hu₀ x (ts hx))
end
/-- If `m s ≠ 0`, then for some point `x ∈ s` and any `t ∈ 𝓝[s] x` we have `0 < m t`. -/
lemma exists_mem_forall_mem_nhds_within_pos [topological_space α] [second_countable_topology α]
(m : outer_measure α) {s : set α} (hs : m s ≠ 0) :
∃ x ∈ s, ∀ t ∈ 𝓝[s] x, 0 < m t :=
begin
contrapose! hs,
simp only [nonpos_iff_eq_zero, ← exists_prop] at hs,
exact m.null_of_locally_null s hs
end
/-- If `s : ι → set α` is a sequence of sets, `S = ⋃ n, s n`, and `m (S \ s n)` tends to zero along
some nontrivial filter (usually `at_top` on `ι = ℕ`), then `m S = ⨆ n, m (s n)`. -/
lemma Union_of_tendsto_zero {ι} (m : outer_measure α) {s : ι → set α}
(l : filter ι) [ne_bot l] (h0 : tendsto (λ k, m ((⋃ n, s n) \ s k)) l (𝓝 0)) :
m (⋃ n, s n) = ⨆ n, m (s n) :=
begin
set S := ⋃ n, s n,
set M := ⨆ n, m (s n),
have hsS : ∀ {k}, s k ⊆ S, from λ k, subset_Union _ _,
refine le_antisymm _ (supr_le $ λ n, m.mono hsS),
have A : ∀ k, m S ≤ M + m (S \ s k), from λ k,
calc m S = m (s k ∪ S \ s k) : by rw [union_diff_self, union_eq_self_of_subset_left hsS]
... ≤ m (s k) + m (S \ s k) : m.union _ _
... ≤ M + m (S \ s k) : add_le_add_right (le_supr _ k) _,
have B : tendsto (λ k, M + m (S \ s k)) l (𝓝 (M + 0)), from tendsto_const_nhds.add h0,
rw add_zero at B,
exact ge_of_tendsto' B A
end
/-- If `s : ℕ → set α` is a monotone sequence of sets such that `∑' k, m (s (k + 1) \ s k) ≠ ∞`,
then `m (⋃ n, s n) = ⨆ n, m (s n)`. -/
lemma Union_nat_of_monotone_of_tsum_ne_top (m : outer_measure α) {s : ℕ → set α}
(h_mono : ∀ n, s n ⊆ s (n + 1)) (h0 : ∑' k, m (s (k + 1) \ s k) ≠ ∞) :
m (⋃ n, s n) = ⨆ n, m (s n) :=
begin
refine m.Union_of_tendsto_zero at_top _,
refine tendsto_nhds_bot_mono' (ennreal.tendsto_sum_nat_add _ h0) (λ n, _),
refine (m.mono _).trans (m.Union _),
/- Current goal: `(⋃ k, s k) \ s n ⊆ ⋃ k, s (k + n + 1) \ s (k + n)` -/
have h' : monotone s := @monotone_nat_of_le_succ (set α) _ _ h_mono,
simp only [diff_subset_iff, Union_subset_iff],
intros i x hx,
rcases nat.find_x ⟨i, hx⟩ with ⟨j, hj, hlt⟩, clear hx i,
cases le_or_lt j n with hjn hnj, { exact or.inl (h' hjn hj) },
have : j - (n + 1) + n + 1 = j,
by rw [add_assoc, tsub_add_cancel_of_le hnj.nat_succ_le],
refine or.inr (mem_Union.2 ⟨j - (n + 1), _, hlt _ _⟩),
{ rwa this },
{ rw [← nat.succ_le_iff, nat.succ_eq_add_one, this] }
end
lemma le_inter_add_diff {m : outer_measure α} {t : set α} (s : set α) :
m t ≤ m (t ∩ s) + m (t \ s) :=
by { convert m.union _ _, rw inter_union_diff t s }
lemma diff_null (m : outer_measure α) (s : set α) {t : set α} (ht : m t = 0) :
m (s \ t) = m s :=
begin
refine le_antisymm (m.mono $ diff_subset _ _) _,
calc m s ≤ m (s ∩ t) + m (s \ t) : le_inter_add_diff _
... ≤ m t + m (s \ t) : add_le_add_right (m.mono $ inter_subset_right _ _) _
... = m (s \ t) : by rw [ht, zero_add]
end
lemma union_null (m : outer_measure α) {s₁ s₂ : set α}
(h₁ : m s₁ = 0) (h₂ : m s₂ = 0) : m (s₁ ∪ s₂) = 0 :=
by simpa [h₁, h₂] using m.union s₁ s₂
lemma coe_fn_injective : injective (λ (μ : outer_measure α) (s : set α), μ s) :=
λ μ₁ μ₂ h, by { cases μ₁, cases μ₂, congr, exact h }
@[ext] lemma ext {μ₁ μ₂ : outer_measure α} (h : ∀ s, μ₁ s = μ₂ s) : μ₁ = μ₂ :=
coe_fn_injective $ funext h
/-- A version of `measure_theory.outer_measure.ext` that assumes `μ₁ s = μ₂ s` on all *nonempty*
sets `s`, and gets `μ₁ ∅ = μ₂ ∅` from `measure_theory.outer_measure.empty'`. -/
lemma ext_nonempty {μ₁ μ₂ : outer_measure α} (h : ∀ s : set α, s.nonempty → μ₁ s = μ₂ s) :
μ₁ = μ₂ :=
ext $ λ s, s.eq_empty_or_nonempty.elim (λ he, by rw [he, empty', empty']) (h s)
instance : has_zero (outer_measure α) :=
⟨{ measure_of := λ_, 0,
empty := rfl,
mono := assume _ _ _, le_refl 0,
Union_nat := assume s, zero_le _ }⟩
@[simp] theorem coe_zero : ⇑(0 : outer_measure α) = 0 := rfl
instance : inhabited (outer_measure α) := ⟨0⟩
instance : has_add (outer_measure α) :=
⟨λm₁ m₂,
{ measure_of := λs, m₁ s + m₂ s,
empty := show m₁ ∅ + m₂ ∅ = 0, by simp [outer_measure.empty],
mono := assume s₁ s₂ h, add_le_add (m₁.mono h) (m₂.mono h),
Union_nat := assume s,
calc m₁ (⋃i, s i) + m₂ (⋃i, s i) ≤
(∑'i, m₁ (s i)) + (∑'i, m₂ (s i)) :
add_le_add (m₁.Union_nat s) (m₂.Union_nat s)
... = _ : ennreal.tsum_add.symm}⟩
@[simp] theorem coe_add (m₁ m₂ : outer_measure α) : ⇑(m₁ + m₂) = m₁ + m₂ := rfl
theorem add_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ + m₂) s = m₁ s + m₂ s := rfl
section has_smul
variables [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞]
variables [has_smul R' ℝ≥0∞] [is_scalar_tower R' ℝ≥0∞ ℝ≥0∞]
instance : has_smul R (outer_measure α) :=
⟨λ c m,
{ measure_of := λ s, c • m s,
empty := by rw [←smul_one_mul c (_ : ℝ≥0∞), empty', mul_zero],
mono := λ s t h, begin
rw [←smul_one_mul c (m s), ←smul_one_mul c (m t)],
exact ennreal.mul_left_mono (m.mono h),
end,
Union_nat := λ s, begin
simp_rw [←smul_one_mul c (m _), ennreal.tsum_mul_left],
exact ennreal.mul_left_mono (m.Union _)
end }⟩
@[simp] lemma coe_smul (c : R) (m : outer_measure α) : ⇑(c • m) = c • m := rfl
lemma smul_apply (c : R) (m : outer_measure α) (s : set α) : (c • m) s = c • m s := rfl
instance [smul_comm_class R R' ℝ≥0∞] : smul_comm_class R R' (outer_measure α) :=
⟨λ _ _ _, ext $ λ _, smul_comm _ _ _⟩
instance [has_smul R R'] [is_scalar_tower R R' ℝ≥0∞] : is_scalar_tower R R' (outer_measure α) :=
⟨λ _ _ _, ext $ λ _, smul_assoc _ _ _⟩
instance [has_smul Rᵐᵒᵖ ℝ≥0∞] [is_central_scalar R ℝ≥0∞] :
is_central_scalar R (outer_measure α) :=
⟨λ _ _, ext $ λ _, op_smul_eq_smul _ _⟩
end has_smul
instance [monoid R] [mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] :
mul_action R (outer_measure α) :=
injective.mul_action _ coe_fn_injective coe_smul
instance add_comm_monoid : add_comm_monoid (outer_measure α) :=
injective.add_comm_monoid (show outer_measure α → set α → ℝ≥0∞, from coe_fn)
coe_fn_injective rfl (λ _ _, rfl) (λ _ _, rfl)
/-- `coe_fn` as an `add_monoid_hom`. -/
@[simps] def coe_fn_add_monoid_hom : outer_measure α →+ (set α → ℝ≥0∞) :=
⟨coe_fn, coe_zero, coe_add⟩
instance [monoid R] [distrib_mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] :
distrib_mul_action R (outer_measure α) :=
injective.distrib_mul_action coe_fn_add_monoid_hom coe_fn_injective coe_smul
instance [semiring R] [module R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] : module R (outer_measure α) :=
injective.module R coe_fn_add_monoid_hom coe_fn_injective coe_smul
instance : has_bot (outer_measure α) := ⟨0⟩
@[simp] theorem coe_bot : (⊥ : outer_measure α) = 0 := rfl
instance outer_measure.partial_order : partial_order (outer_measure α) :=
{ le := λm₁ m₂, ∀s, m₁ s ≤ m₂ s,
le_refl := assume a s, le_rfl,
le_trans := assume a b c hab hbc s, le_trans (hab s) (hbc s),
le_antisymm := assume a b hab hba, ext $ assume s, le_antisymm (hab s) (hba s) }
instance outer_measure.order_bot : order_bot (outer_measure α) :=
{ bot_le := assume a s, by simp only [coe_zero, pi.zero_apply, coe_bot, zero_le],
..outer_measure.has_bot }
lemma univ_eq_zero_iff (m : outer_measure α) : m univ = 0 ↔ m = 0 :=
⟨λ h, bot_unique $ λ s, (m.mono' $ subset_univ s).trans_eq h, λ h, h.symm ▸ rfl⟩
section supremum
instance : has_Sup (outer_measure α) :=
⟨λms,
{ measure_of := λs, ⨆ m ∈ ms, (m : outer_measure α) s,
empty := nonpos_iff_eq_zero.1 $ supr₂_le $ λ m h, le_of_eq m.empty,
mono := assume s₁ s₂ hs, supr₂_mono $ assume m hm, m.mono hs,
Union_nat := assume f, supr₂_le $ assume m hm,
calc m (⋃i, f i) ≤ ∑' (i : ℕ), m (f i) : m.Union_nat _
... ≤ ∑'i, (⨆ m ∈ ms, (m : outer_measure α) (f i)) :
ennreal.tsum_le_tsum $ λ i, le_supr₂ m hm }⟩
instance : complete_lattice (outer_measure α) :=
{ .. outer_measure.order_bot, .. complete_lattice_of_Sup (outer_measure α)
(λ ms, ⟨λ m hm s, le_supr₂ m hm, λ m hm s, supr₂_le (λ m' hm', hm hm' s)⟩) }
@[simp] theorem Sup_apply (ms : set (outer_measure α)) (s : set α) :
(Sup ms) s = ⨆ m ∈ ms, (m : outer_measure α) s := rfl
@[simp] theorem supr_apply {ι} (f : ι → outer_measure α) (s : set α) :
(⨆ i : ι, f i) s = ⨆ i, f i s :=
by rw [supr, Sup_apply, supr_range, supr]
@[norm_cast] theorem coe_supr {ι} (f : ι → outer_measure α) :
⇑(⨆ i, f i) = ⨆ i, f i :=
funext $ λ s, by rw [supr_apply, _root_.supr_apply]
@[simp] theorem sup_apply (m₁ m₂ : outer_measure α) (s : set α) :
(m₁ ⊔ m₂) s = m₁ s ⊔ m₂ s :=
by have := supr_apply (λ b, cond b m₁ m₂) s;
rwa [supr_bool_eq, supr_bool_eq] at this
theorem smul_supr [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] {ι}
(f : ι → outer_measure α) (c : R) :
c • (⨆ i, f i) = ⨆ i, c • f i :=
ext $ λ s, by simp only [smul_apply, supr_apply, ennreal.smul_supr]
end supremum
@[mono] lemma mono'' {m₁ m₂ : outer_measure α} {s₁ s₂ : set α} (hm : m₁ ≤ m₂) (hs : s₁ ⊆ s₂) :
m₁ s₁ ≤ m₂ s₂ :=
(hm s₁).trans (m₂.mono hs)
/-- The pushforward of `m` along `f`. The outer measure on `s` is defined to be `m (f ⁻¹' s)`. -/
def map {β} (f : α → β) : outer_measure α →ₗ[ℝ≥0∞] outer_measure β :=
{ to_fun := λ m,
{ measure_of := λs, m (f ⁻¹' s),
empty := m.empty,
mono := λ s t h, m.mono (preimage_mono h),
Union_nat := λ s, by rw [preimage_Union]; exact
m.Union_nat (λ i, f ⁻¹' s i) },
map_add' := λ m₁ m₂, coe_fn_injective rfl,
map_smul' := λ c m, coe_fn_injective rfl }
@[simp] theorem map_apply {β} (f : α → β)
(m : outer_measure α) (s : set β) : map f m s = m (f ⁻¹' s) := rfl
@[simp] theorem map_id (m : outer_measure α) : map id m = m :=
ext $ λ s, rfl
@[simp] theorem map_map {β γ} (f : α → β) (g : β → γ)
(m : outer_measure α) : map g (map f m) = map (g ∘ f) m :=
ext $ λ s, rfl
@[mono] theorem map_mono {β} (f : α → β) : monotone (map f) :=
λ m m' h s, h _
@[simp] theorem map_sup {β} (f : α → β) (m m' : outer_measure α) :
map f (m ⊔ m') = map f m ⊔ map f m' :=
ext $ λ s, by simp only [map_apply, sup_apply]
@[simp] theorem map_supr {β ι} (f : α → β) (m : ι → outer_measure α) :
map f (⨆ i, m i) = ⨆ i, map f (m i) :=
ext $ λ s, by simp only [map_apply, supr_apply]
instance : functor outer_measure := {map := λ α β f, map f}
instance : is_lawful_functor outer_measure :=
{ id_map := λ α, map_id,
comp_map := λ α β γ f g m, (map_map f g m).symm }
/-- The dirac outer measure. -/
def dirac (a : α) : outer_measure α :=
{ measure_of := λs, indicator s (λ _, 1) a,
empty := by simp,
mono := λ s t h, indicator_le_indicator_of_subset h (λ _, zero_le _) a,
Union_nat := λ s,
if hs : a ∈ ⋃ n, s n then let ⟨i, hi⟩ := mem_Union.1 hs in
calc indicator (⋃ n, s n) (λ _, (1 : ℝ≥0∞)) a = 1 : indicator_of_mem hs _
... = indicator (s i) (λ _, 1) a : (indicator_of_mem hi _).symm
... ≤ ∑' n, indicator (s n) (λ _, 1) a : ennreal.le_tsum _
else by simp only [indicator_of_not_mem hs, zero_le]}
@[simp] theorem dirac_apply (a : α) (s : set α) :
dirac a s = indicator s (λ _, 1) a := rfl
/-- The sum of an (arbitrary) collection of outer measures. -/
def sum {ι} (f : ι → outer_measure α) : outer_measure α :=
{ measure_of := λs, ∑' i, f i s,
empty := by simp,
mono := λ s t h, ennreal.tsum_le_tsum (λ i, (f i).mono' h),
Union_nat := λ s, by rw ennreal.tsum_comm; exact
ennreal.tsum_le_tsum (λ i, (f i).Union_nat _) }
@[simp] theorem sum_apply {ι} (f : ι → outer_measure α) (s : set α) :
sum f s = ∑' i, f i s := rfl
theorem smul_dirac_apply (a : ℝ≥0∞) (b : α) (s : set α) :
(a • dirac b) s = indicator s (λ _, a) b :=
by simp only [smul_apply, smul_eq_mul, dirac_apply, ← indicator_mul_right _ (λ _, a), mul_one]
/-- Pullback of an `outer_measure`: `comap f μ s = μ (f '' s)`. -/
def comap {β} (f : α → β) : outer_measure β →ₗ[ℝ≥0∞] outer_measure α :=
{ to_fun := λ m,
{ measure_of := λ s, m (f '' s),
empty := by simp,
mono := λ s t h, m.mono $ image_subset f h,
Union_nat := λ s, by { rw [image_Union], apply m.Union_nat } },
map_add' := λ m₁ m₂, rfl,
map_smul' := λ c m, rfl }
@[simp] lemma comap_apply {β} (f : α → β) (m : outer_measure β) (s : set α) :
comap f m s = m (f '' s) :=
rfl
@[mono] lemma comap_mono {β} (f : α → β) :
monotone (comap f) :=
λ m m' h s, h _
@[simp] theorem comap_supr {β ι} (f : α → β) (m : ι → outer_measure β) :
comap f (⨆ i, m i) = ⨆ i, comap f (m i) :=
ext $ λ s, by simp only [comap_apply, supr_apply]
/-- Restrict an `outer_measure` to a set. -/
def restrict (s : set α) : outer_measure α →ₗ[ℝ≥0∞] outer_measure α :=
(map coe).comp (comap (coe : s → α))
@[simp] lemma restrict_apply (s t : set α) (m : outer_measure α) :
restrict s m t = m (t ∩ s) :=
by simp [restrict]
@[mono] lemma restrict_mono {s t : set α} (h : s ⊆ t) {m m' : outer_measure α} (hm : m ≤ m') :
restrict s m ≤ restrict t m' :=
λ u, by { simp only [restrict_apply], exact (hm _).trans (m'.mono $ inter_subset_inter_right _ h) }
@[simp] lemma restrict_univ (m : outer_measure α) : restrict univ m = m := ext $ λ s, by simp
@[simp] lemma restrict_empty (m : outer_measure α) : restrict ∅ m = 0 := ext $ λ s, by simp
@[simp] lemma restrict_supr {ι} (s : set α) (m : ι → outer_measure α) :
restrict s (⨆ i, m i) = ⨆ i, restrict s (m i) :=
by simp [restrict]
lemma map_comap {β} (f : α → β) (m : outer_measure β) :
map f (comap f m) = restrict (range f) m :=
ext $ λ s, congr_arg m $ by simp only [image_preimage_eq_inter_range, subtype.range_coe]
lemma map_comap_le {β} (f : α → β) (m : outer_measure β) :
map f (comap f m) ≤ m :=
λ s, m.mono $ image_preimage_subset _ _
lemma restrict_le_self (m : outer_measure α) (s : set α) :
restrict s m ≤ m :=
map_comap_le _ _
@[simp] lemma map_le_restrict_range {β} {ma : outer_measure α} {mb : outer_measure β} {f : α → β} :
map f ma ≤ restrict (range f) mb ↔ map f ma ≤ mb :=
⟨λ h, h.trans (restrict_le_self _ _), λ h s, by simpa using h (s ∩ range f)⟩
lemma map_comap_of_surjective {β} {f : α → β} (hf : surjective f) (m : outer_measure β) :
map f (comap f m) = m :=
ext $ λ s, by rw [map_apply, comap_apply, hf.image_preimage]
lemma le_comap_map {β} (f : α → β) (m : outer_measure α) :
m ≤ comap f (map f m) :=
λ s, m.mono $ subset_preimage_image _ _
lemma comap_map {β} {f : α → β} (hf : injective f) (m : outer_measure α) :
comap f (map f m) = m :=
ext $ λ s, by rw [comap_apply, map_apply, hf.preimage_image]
@[simp] theorem top_apply {s : set α} (h : s.nonempty) : (⊤ : outer_measure α) s = ∞ :=
let ⟨a, as⟩ := h in
top_unique $ le_trans (by simp [smul_dirac_apply, as]) (le_supr₂ (∞ • dirac a) trivial)
theorem top_apply' (s : set α) : (⊤ : outer_measure α) s = ⨅ (h : s = ∅), 0 :=
s.eq_empty_or_nonempty.elim (λ h, by simp [h]) (λ h, by simp [h, h.ne_empty])
@[simp] theorem comap_top (f : α → β) : comap f ⊤ = ⊤ :=
ext_nonempty $ λ s hs, by rw [comap_apply, top_apply hs, top_apply (hs.image _)]
theorem map_top (f : α → β) : map f ⊤ = restrict (range f) ⊤ :=
ext $ λ s, by rw [map_apply, restrict_apply, ← image_preimage_eq_inter_range,
top_apply', top_apply', set.image_eq_empty]
theorem map_top_of_surjective (f : α → β) (hf : surjective f) : map f ⊤ = ⊤ :=
by rw [map_top, hf.range_eq, restrict_univ]
end basic
section of_function
set_option eqn_compiler.zeta true
variables {α : Type*} (m : set α → ℝ≥0∞) (m_empty : m ∅ = 0)
include m_empty
/-- Given any function `m` assigning measures to sets satisying `m ∅ = 0`, there is
a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. -/
protected def of_function : outer_measure α :=
let μ := λs, ⨅{f : ℕ → set α} (h : s ⊆ ⋃i, f i), ∑'i, m (f i) in
{ measure_of := μ,
empty := le_antisymm
(infi_le_of_le (λ_, ∅) $ infi_le_of_le (empty_subset _) $ by simp [m_empty])
(zero_le _),
mono := assume s₁ s₂ hs, infi_mono $ assume f,
infi_mono' $ assume hb, ⟨hs.trans hb, le_rfl⟩,
Union_nat := assume s, ennreal.le_of_forall_pos_le_add $ begin
assume ε hε (hb : ∑'i, μ (s i) < ∞),
rcases ennreal.exists_pos_sum_of_countable (ennreal.coe_pos.2 hε).ne' ℕ with ⟨ε', hε', hl⟩,
refine le_trans _ (add_le_add_left (le_of_lt hl) _),
rw ← ennreal.tsum_add,
choose f hf using show
∀i, ∃f:ℕ → set α, s i ⊆ (⋃i, f i) ∧ ∑'i, m (f i) < μ (s i) + ε' i,
{ intro,
have : μ (s i) < μ (s i) + ε' i :=
ennreal.lt_add_right
(ne_top_of_le_ne_top hb.ne $ ennreal.le_tsum _)
(by simpa using (hε' i).ne'),
simpa [μ, infi_lt_iff] },
refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hf i).2),
rw [← ennreal.tsum_prod, ← nat.mkpair_equiv.symm.tsum_eq],
swap, {apply_instance},
refine infi_le_of_le _ (infi_le _ _),
exact Union_subset (λ i, subset.trans (hf i).1 $
Union_subset $ λ j, subset.trans (by simp) $
subset_Union _ $ nat.mkpair_equiv (i, j)),
end }
lemma of_function_apply (s : set α) :
outer_measure.of_function m m_empty s =
(⨅ (t : ℕ → set α) (h : s ⊆ Union t), ∑' n, m (t n)) := rfl
variables {m m_empty}
theorem of_function_le (s : set α) : outer_measure.of_function m m_empty s ≤ m s :=
let f : ℕ → set α := λi, nat.cases_on i s (λ _, ∅) in
infi_le_of_le f $ infi_le_of_le (subset_Union f 0) $ le_of_eq $
tsum_eq_single 0 $ by rintro (_|i); simp [f, m_empty]
theorem of_function_eq (s : set α) (m_mono : ∀ ⦃t : set α⦄, s ⊆ t → m s ≤ m t)
(m_subadd : ∀ (s : ℕ → set α), m (⋃i, s i) ≤ ∑'i, m (s i)) :
outer_measure.of_function m m_empty s = m s :=
le_antisymm (of_function_le s) $ le_infi $ λ f, le_infi $ λ hf, le_trans (m_mono hf) (m_subadd f)
theorem le_of_function {μ : outer_measure α} :
μ ≤ outer_measure.of_function m m_empty ↔ ∀ s, μ s ≤ m s :=
⟨λ H s, le_trans (H s) (of_function_le s),
λ H s, le_infi $ λ f, le_infi $ λ hs,
le_trans (μ.mono hs) $ le_trans (μ.Union f) $
ennreal.tsum_le_tsum $ λ i, H _⟩
lemma is_greatest_of_function :
is_greatest {μ : outer_measure α | ∀ s, μ s ≤ m s} (outer_measure.of_function m m_empty) :=
⟨λ s, of_function_le _, λ μ, le_of_function.2⟩
lemma of_function_eq_Sup : outer_measure.of_function m m_empty = Sup {μ | ∀ s, μ s ≤ m s} :=
(@is_greatest_of_function α m m_empty).is_lub.Sup_eq.symm
/-- If `m u = ∞` for any set `u` that has nonempty intersection both with `s` and `t`, then
`μ (s ∪ t) = μ s + μ t`, where `μ = measure_theory.outer_measure.of_function m m_empty`.
E.g., if `α` is an (e)metric space and `m u = ∞` on any set of diameter `≥ r`, then this lemma
implies that `μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s`
and `y ∈ t`. -/
lemma of_function_union_of_top_of_nonempty_inter {s t : set α}
(h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → m u = ∞) :
outer_measure.of_function m m_empty (s ∪ t) =
outer_measure.of_function m m_empty s + outer_measure.of_function m m_empty t :=
begin
refine le_antisymm (outer_measure.union _ _ _) (le_infi $ λ f, le_infi $ λ hf, _),
set μ := outer_measure.of_function m m_empty,
rcases em (∃ i, (s ∩ f i).nonempty ∧ (t ∩ f i).nonempty) with ⟨i, hs, ht⟩|he,
{ calc μ s + μ t ≤ ∞ : le_top
... = m (f i) : (h (f i) hs ht).symm
... ≤ ∑' i, m (f i) : ennreal.le_tsum i },
set I := λ s, {i : ℕ | (s ∩ f i).nonempty},
have hd : disjoint (I s) (I t), from disjoint_iff_inf_le.mpr (λ i hi, he ⟨i, hi⟩),
have hI : ∀ u ⊆ s ∪ t, μ u ≤ ∑' i : I u, μ (f i), from λ u hu,
calc μ u ≤ μ (⋃ i : I u, f i) :
μ.mono (λ x hx, let ⟨i, hi⟩ := mem_Union.1 (hf (hu hx)) in mem_Union.2 ⟨⟨i, ⟨x, hx, hi⟩⟩, hi⟩)
... ≤ ∑' i : I u, μ (f i) : μ.Union _,
calc μ s + μ t ≤ (∑' i : I s, μ (f i)) + (∑' i : I t, μ (f i)) :
add_le_add (hI _ $ subset_union_left _ _) (hI _ $ subset_union_right _ _)
... = ∑' i : I s ∪ I t, μ (f i) :
(@tsum_union_disjoint _ _ _ _ _ (λ i, μ (f i)) _ _ _ hd ennreal.summable ennreal.summable).symm
... ≤ ∑' i, μ (f i) :
tsum_le_tsum_of_inj coe subtype.coe_injective (λ _ _, zero_le _) (λ _, le_rfl)
ennreal.summable ennreal.summable
... ≤ ∑' i, m (f i) : ennreal.tsum_le_tsum (λ i, of_function_le _)
end
lemma comap_of_function {β} (f : β → α) (h : monotone m ∨ surjective f) :
comap f (outer_measure.of_function m m_empty) =
outer_measure.of_function (λ s, m (f '' s)) (by rwa set.image_empty) :=
begin
refine le_antisymm (le_of_function.2 $ λ s, _) (λ s, _),
{ rw comap_apply, apply of_function_le },
{ rw [comap_apply, of_function_apply, of_function_apply],
refine infi_mono' (λ t, ⟨λ k, f ⁻¹' (t k), _⟩),
refine infi_mono' (λ ht, _),
rw [set.image_subset_iff, preimage_Union] at ht,
refine ⟨ht, ennreal.tsum_le_tsum $ λ n, _⟩,
cases h,
exacts [h (image_preimage_subset _ _), (congr_arg m (h.image_preimage (t n))).le] }
end
lemma map_of_function_le {β} (f : α → β) :
map f (outer_measure.of_function m m_empty) ≤
outer_measure.of_function (λ s, m (f ⁻¹' s)) m_empty :=
le_of_function.2 $ λ s, by { rw map_apply, apply of_function_le }
lemma map_of_function {β} {f : α → β} (hf : injective f) :
map f (outer_measure.of_function m m_empty) =
outer_measure.of_function (λ s, m (f ⁻¹' s)) m_empty :=
begin
refine (map_of_function_le _).antisymm (λ s, _),
simp only [of_function_apply, map_apply, le_infi_iff],
intros t ht,
refine infi_le_of_le (λ n, (range f)ᶜ ∪ f '' (t n)) (infi_le_of_le _ _),
{ rw [← union_Union, ← inter_subset, ← image_preimage_eq_inter_range, ← image_Union],
exact image_subset _ ht },
{ refine ennreal.tsum_le_tsum (λ n, le_of_eq _),
simp [hf.preimage_image] }
end
lemma restrict_of_function (s : set α) (hm : monotone m) :
restrict s (outer_measure.of_function m m_empty) =
outer_measure.of_function (λ t, m (t ∩ s)) (by rwa set.empty_inter) :=
by simp only [restrict, linear_map.comp_apply, comap_of_function _ (or.inl hm),
map_of_function subtype.coe_injective, subtype.image_preimage_coe]
lemma smul_of_function {c : ℝ≥0∞} (hc : c ≠ ∞) :
c • outer_measure.of_function m m_empty = outer_measure.of_function (c • m) (by simp [m_empty]) :=
begin
ext1 s,
haveI : nonempty {t : ℕ → set α // s ⊆ ⋃ i, t i} := ⟨⟨λ _, s, subset_Union (λ _, s) 0⟩⟩,
simp only [smul_apply, of_function_apply, ennreal.tsum_mul_left, pi.smul_apply, smul_eq_mul,
infi_subtype', ennreal.infi_mul_left (λ h, (hc h).elim)],
end
end of_function
section bounded_by
variables {α : Type*} (m : set α → ℝ≥0∞)
/-- Given any function `m` assigning measures to sets, there is a unique maximal outer measure `μ`
satisfying `μ s ≤ m s` for all `s : set α`. This is the same as `outer_measure.of_function`,
except that it doesn't require `m ∅ = 0`. -/
def bounded_by : outer_measure α :=
outer_measure.of_function (λ s, ⨆ (h : s.nonempty), m s) (by simp [not_nonempty_empty])
variables {m}
theorem bounded_by_le (s : set α) : bounded_by m s ≤ m s :=
(of_function_le _).trans supr_const_le
theorem bounded_by_eq_of_function (m_empty : m ∅ = 0) (s : set α) :
bounded_by m s = outer_measure.of_function m m_empty s :=
begin
have : (λ s : set α, ⨆ (h : s.nonempty), m s) = m,
{ ext1 t, cases t.eq_empty_or_nonempty with h h; simp [h, not_nonempty_empty, m_empty] },
simp [bounded_by, this]
end
theorem bounded_by_apply (s : set α) :
bounded_by m s = ⨅ (t : ℕ → set α) (h : s ⊆ Union t), ∑' n, ⨆ (h : (t n).nonempty), m (t n) :=
by simp [bounded_by, of_function_apply]
theorem bounded_by_eq (s : set α) (m_empty : m ∅ = 0) (m_mono : ∀ ⦃t : set α⦄, s ⊆ t → m s ≤ m t)
(m_subadd : ∀ (s : ℕ → set α), m (⋃i, s i) ≤ ∑'i, m (s i)) : bounded_by m s = m s :=
by rw [bounded_by_eq_of_function m_empty, of_function_eq s m_mono m_subadd]
@[simp] theorem bounded_by_eq_self (m : outer_measure α) : bounded_by m = m :=
ext $ λ s, bounded_by_eq _ m.empty' (λ t ht, m.mono' ht) m.Union
theorem le_bounded_by {μ : outer_measure α} : μ ≤ bounded_by m ↔ ∀ s, μ s ≤ m s :=
begin
rw [bounded_by, le_of_function, forall_congr], intro s,
cases s.eq_empty_or_nonempty with h h; simp [h, not_nonempty_empty]
end
theorem le_bounded_by' {μ : outer_measure α} :
μ ≤ bounded_by m ↔ ∀ s : set α, s.nonempty → μ s ≤ m s :=
by { rw [le_bounded_by, forall_congr], intro s, cases s.eq_empty_or_nonempty with h h; simp [h] }
@[simp] lemma bounded_by_top : bounded_by (⊤ : set α → ℝ≥0∞) = ⊤ :=
begin
rw [eq_top_iff, le_bounded_by'],
intros s hs,
rw top_apply hs,
exact le_rfl,
end
@[simp] lemma bounded_by_zero : bounded_by (0 : set α → ℝ≥0∞) = 0 :=
begin
rw [←coe_bot, eq_bot_iff],
apply bounded_by_le,
end
lemma smul_bounded_by {c : ℝ≥0∞} (hc : c ≠ ∞) : c • bounded_by m = bounded_by (c • m) :=
begin
simp only [bounded_by, smul_of_function hc],
congr' 1 with s : 1,
rcases s.eq_empty_or_nonempty with rfl|hs; simp *
end
lemma comap_bounded_by {β} (f : β → α)
(h : monotone (λ s : {s : set α // s.nonempty}, m s) ∨ surjective f) :
comap f (bounded_by m) = bounded_by (λ s, m (f '' s)) :=
begin
refine (comap_of_function _ _).trans _,
{ refine h.imp (λ H s t hst, supr_le $ λ hs, _) id,
have ht : t.nonempty := hs.mono hst,
exact (@H ⟨s, hs⟩ ⟨t, ht⟩ hst).trans (le_supr (λ h : t.nonempty, m t) ht) },
{ dunfold bounded_by,
congr' with s : 1,
rw nonempty_image_iff }
end
/-- If `m u = ∞` for any set `u` that has nonempty intersection both with `s` and `t`, then
`μ (s ∪ t) = μ s + μ t`, where `μ = measure_theory.outer_measure.bounded_by m`.
E.g., if `α` is an (e)metric space and `m u = ∞` on any set of diameter `≥ r`, then this lemma
implies that `μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s`
and `y ∈ t`. -/
lemma bounded_by_union_of_top_of_nonempty_inter {s t : set α}
(h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → m u = ∞) :
bounded_by m (s ∪ t) = bounded_by m s + bounded_by m t :=
of_function_union_of_top_of_nonempty_inter $ λ u hs ht,
top_unique $ (h u hs ht).ge.trans $ le_supr (λ h, m u) (hs.mono $ inter_subset_right s u)
end bounded_by
section caratheodory_measurable
universe u
parameters {α : Type u} (m : outer_measure α)
include m
local attribute [simp] set.inter_comm set.inter_left_comm set.inter_assoc
variables {s s₁ s₂ : set α}
/-- A set `s` is Carathéodory-measurable for an outer measure `m` if for all sets `t` we have
`m t = m (t ∩ s) + m (t \ s)`. -/
def is_caratheodory (s : set α) : Prop := ∀t, m t = m (t ∩ s) + m (t \ s)
lemma is_caratheodory_iff_le' {s : set α} : is_caratheodory s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t :=
forall_congr $ λ t, le_antisymm_iff.trans $ and_iff_right $ le_inter_add_diff _
@[simp] lemma is_caratheodory_empty : is_caratheodory ∅ :=
by simp [is_caratheodory, m.empty, diff_empty]
lemma is_caratheodory_compl : is_caratheodory s₁ → is_caratheodory s₁ᶜ :=
by simp [is_caratheodory, diff_eq, add_comm]
@[simp] lemma is_caratheodory_compl_iff : is_caratheodory sᶜ ↔ is_caratheodory s :=
⟨λ h, by simpa using is_caratheodory_compl m h, is_caratheodory_compl⟩
lemma is_caratheodory_union (h₁ : is_caratheodory s₁) (h₂ : is_caratheodory s₂) :
is_caratheodory (s₁ ∪ s₂) :=
λ t, begin
rw [h₁ t, h₂ (t ∩ s₁), h₂ (t \ s₁), h₁ (t ∩ (s₁ ∪ s₂)),
inter_diff_assoc _ _ s₁, set.inter_assoc _ _ s₁,
inter_eq_self_of_subset_right (set.subset_union_left _ _),
union_diff_left, h₂ (t ∩ s₁)],
simp [diff_eq, add_assoc]
end
lemma measure_inter_union (h : s₁ ∩ s₂ ⊆ ∅) (h₁ : is_caratheodory s₁) {t : set α} :
m (t ∩ (s₁ ∪ s₂)) = m (t ∩ s₁) + m (t ∩ s₂) :=
by rw [h₁, set.inter_assoc, set.union_inter_cancel_left,
inter_diff_assoc, union_diff_cancel_left h]
lemma is_caratheodory_Union_lt {s : ℕ → set α} :
∀{n:ℕ}, (∀i<n, is_caratheodory (s i)) → is_caratheodory (⋃i<n, s i)
| 0 h := by simp [nat.not_lt_zero]
| (n + 1) h := by rw bUnion_lt_succ; exact is_caratheodory_union m
(is_caratheodory_Union_lt $ assume i hi, h i $ lt_of_lt_of_le hi $ nat.le_succ _)
(h n (le_refl (n + 1)))
lemma is_caratheodory_inter (h₁ : is_caratheodory s₁) (h₂ : is_caratheodory s₂) :
is_caratheodory (s₁ ∩ s₂) :=
by { rw [← is_caratheodory_compl_iff, set.compl_inter],
exact is_caratheodory_union _ (is_caratheodory_compl _ h₁) (is_caratheodory_compl _ h₂) }
lemma is_caratheodory_sum {s : ℕ → set α} (h : ∀i, is_caratheodory (s i))
(hd : pairwise (disjoint on s)) {t : set α} :
∀ {n}, ∑ i in finset.range n, m (t ∩ s i) = m (t ∩ ⋃i<n, s i)
| 0 := by simp [nat.not_lt_zero, m.empty]
| (nat.succ n) := begin
rw [bUnion_lt_succ, finset.sum_range_succ, set.union_comm, is_caratheodory_sum,
m.measure_inter_union _ (h n), add_comm],
intro a,
simpa using λ (h₁ : a ∈ s n) i (hi : i < n) h₂, (hd (ne_of_gt hi)).le_bot ⟨h₁, h₂⟩
end
lemma is_caratheodory_Union_nat {s : ℕ → set α} (h : ∀i, is_caratheodory (s i))
(hd : pairwise (disjoint on s)) : is_caratheodory (⋃i, s i) :=
is_caratheodory_iff_le'.2 $ λ t, begin
have hp : m (t ∩ ⋃i, s i) ≤ (⨆n, m (t ∩ ⋃i<n, s i)),
{ convert m.Union (λ i, t ∩ s i),
{ rw inter_Union },
{ simp [ennreal.tsum_eq_supr_nat, is_caratheodory_sum m h hd] } },
refine le_trans (add_le_add_right hp _) _,
rw ennreal.supr_add,
refine supr_le (λ n, le_trans (add_le_add_left _ _)
(ge_of_eq (is_caratheodory_Union_lt m (λ i _, h i) _))),
refine m.mono (diff_subset_diff_right _),
exact Union₂_subset (λ i _, subset_Union _ i),
end
lemma f_Union {s : ℕ → set α} (h : ∀i, is_caratheodory (s i))
(hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑'i, m (s i) :=
begin
refine le_antisymm (m.Union_nat s) _,
rw ennreal.tsum_eq_supr_nat,
refine supr_le (λ n, _),
have := @is_caratheodory_sum _ m _ h hd univ n,
simp at this, simp [this],
exact m.mono (Union₂_subset (λ i _, subset_Union _ i)),
end
/-- The Carathéodory-measurable sets for an outer measure `m` form a Dynkin system. -/
def caratheodory_dynkin : measurable_space.dynkin_system α :=
{ has := is_caratheodory,
has_empty := is_caratheodory_empty,
has_compl := assume s, is_caratheodory_compl,
has_Union_nat := assume f hf hn, is_caratheodory_Union_nat hn hf }
/-- Given an outer measure `μ`, the Carathéodory-measurable space is
defined such that `s` is measurable if `∀t, μ t = μ (t ∩ s) + μ (t \ s)`. -/
protected def caratheodory : measurable_space α :=
caratheodory_dynkin.to_measurable_space $ assume s₁ s₂, is_caratheodory_inter
lemma is_caratheodory_iff {s : set α} :
measurable_set[caratheodory] s ↔ ∀t, m t = m (t ∩ s) + m (t \ s) :=
iff.rfl
lemma is_caratheodory_iff_le {s : set α} :
measurable_set[caratheodory] s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t :=
is_caratheodory_iff_le'
protected lemma Union_eq_of_caratheodory {s : ℕ → set α}
(h : ∀i, measurable_set[caratheodory] (s i)) (hd : pairwise (disjoint on s)) :
m (⋃i, s i) = ∑'i, m (s i) :=
f_Union h hd
end caratheodory_measurable
variables {α : Type*}
lemma of_function_caratheodory {m : set α → ℝ≥0∞} {s : set α}
{h₀ : m ∅ = 0} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) :
measurable_set[(outer_measure.of_function m h₀).caratheodory] s :=
begin
apply (is_caratheodory_iff_le _).mpr,
refine λ t, le_infi (λ f, le_infi $ λ hf, _),
refine le_trans (add_le_add
(infi_le_of_le (λi, f i ∩ s) $ infi_le _ _)
(infi_le_of_le (λi, f i \ s) $ infi_le _ _)) _,
{ rw ← Union_inter, exact inter_subset_inter_left _ hf },
{ rw ← Union_diff, exact diff_subset_diff_left hf },
{ rw ← ennreal.tsum_add, exact ennreal.tsum_le_tsum (λ i, hs _) }
end
lemma bounded_by_caratheodory {m : set α → ℝ≥0∞} {s : set α}
(hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : measurable_set[(bounded_by m).caratheodory] s :=
begin
apply of_function_caratheodory, intro t,
cases t.eq_empty_or_nonempty with h h,
{ simp [h, not_nonempty_empty] },
{ convert le_trans _ (hs t), { simp [h] }, exact add_le_add supr_const_le supr_const_le }
end
@[simp] theorem zero_caratheodory : (0 : outer_measure α).caratheodory = ⊤ :=
top_unique $ λ s _ t, (add_zero _).symm
theorem top_caratheodory : (⊤ : outer_measure α).caratheodory = ⊤ :=
top_unique $ assume s hs, (is_caratheodory_iff_le _).2 $ assume t,
t.eq_empty_or_nonempty.elim (λ ht, by simp [ht])
(λ ht, by simp only [ht, top_apply, le_top])
theorem le_add_caratheodory (m₁ m₂ : outer_measure α) :
m₁.caratheodory ⊓ m₂.caratheodory ≤ (m₁ + m₂ : outer_measure α).caratheodory :=
λ s ⟨hs₁, hs₂⟩ t, by simp [hs₁ t, hs₂ t, add_left_comm, add_assoc]
theorem le_sum_caratheodory {ι} (m : ι → outer_measure α) :
(⨅ i, (m i).caratheodory) ≤ (sum m).caratheodory :=
λ s h t, by simp [λ i,
measurable_space.measurable_set_infi.1 h i t, ennreal.tsum_add]
theorem le_smul_caratheodory (a : ℝ≥0∞) (m : outer_measure α) :
m.caratheodory ≤ (a • m).caratheodory :=
λ s h t, by simp [h t, mul_add]
@[simp] theorem dirac_caratheodory (a : α) : (dirac a).caratheodory = ⊤ :=
top_unique $ λ s _ t, begin
by_cases ht : a ∈ t, swap, by simp [ht],
by_cases hs : a ∈ s; simp*
end
section Inf_gen
/-- Given a set of outer measures, we define a new function that on a set `s` is defined to be the
infimum of `μ(s)` for the outer measures `μ` in the collection. We ensure that this
function is defined to be `0` on `∅`, even if the collection of outer measures is empty.
The outer measure generated by this function is the infimum of the given outer measures. -/
def Inf_gen (m : set (outer_measure α)) (s : set α) : ℝ≥0∞ :=
⨅ (μ : outer_measure α) (h : μ ∈ m), μ s
lemma Inf_gen_def (m : set (outer_measure α)) (t : set α) :
Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) :=
rfl
lemma Inf_eq_bounded_by_Inf_gen (m : set (outer_measure α)) :
Inf m = outer_measure.bounded_by (Inf_gen m) :=
begin
refine le_antisymm _ _,
{ refine (le_bounded_by.2 $ λ s, le_infi₂ $ λ μ hμ, _),
exact (show Inf m ≤ μ, from Inf_le hμ) s },
{ refine le_Inf _, intros μ hμ t, refine le_trans (bounded_by_le t) (infi₂_le μ hμ) }
end
lemma supr_Inf_gen_nonempty {m : set (outer_measure α)} (h : m.nonempty) (t : set α) :
(⨆ (h : t.nonempty), Inf_gen m t) = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) :=
begin
rcases t.eq_empty_or_nonempty with rfl|ht,
{ rcases h with ⟨μ, hμ⟩,
rw [eq_false_intro not_nonempty_empty, supr_false, eq_comm],
simp_rw [empty'],
apply bot_unique,
refine infi_le_of_le μ (infi_le _ hμ) },
{ simp [ht, Inf_gen_def] }
end
/-- The value of the Infimum of a nonempty set of outer measures on a set is not simply
the minimum value of a measure on that set: it is the infimum sum of measures of countable set of
sets that covers that set, where a different measure can be used for each set in the cover. -/
lemma Inf_apply {m : set (outer_measure α)} {s : set α} (h : m.nonempty) :
Inf m s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t),
∑' n, ⨅ (μ : outer_measure α) (h3 : μ ∈ m), μ (t n) :=
by simp_rw [Inf_eq_bounded_by_Inf_gen, bounded_by_apply, supr_Inf_gen_nonempty h]
/-- The value of the Infimum of a set of outer measures on a nonempty set is not simply
the minimum value of a measure on that set: it is the infimum sum of measures of countable set of
sets that covers that set, where a different measure can be used for each set in the cover. -/
lemma Inf_apply' {m : set (outer_measure α)} {s : set α} (h : s.nonempty) :
Inf m s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t),
∑' n, ⨅ (μ : outer_measure α) (h3 : μ ∈ m), μ (t n) :=
m.eq_empty_or_nonempty.elim (λ hm, by simp [hm, h]) Inf_apply
/-- The value of the Infimum of a nonempty family of outer measures on a set is not simply
the minimum value of a measure on that set: it is the infimum sum of measures of countable set of
sets that covers that set, where a different measure can be used for each set in the cover. -/
lemma infi_apply {ι} [nonempty ι] (m : ι → outer_measure α) (s : set α) :
(⨅ i, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i, m i (t n) :=
by { rw [infi, Inf_apply (range_nonempty m)], simp only [infi_range] }
/-- The value of the Infimum of a family of outer measures on a nonempty set is not simply
the minimum value of a measure on that set: it is the infimum sum of measures of countable set of
sets that covers that set, where a different measure can be used for each set in the cover. -/
lemma infi_apply' {ι} (m : ι → outer_measure α) {s : set α} (hs : s.nonempty) :
(⨅ i, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i, m i (t n) :=
by { rw [infi, Inf_apply' hs], simp only [infi_range] }
/-- The value of the Infimum of a nonempty family of outer measures on a set is not simply
the minimum value of a measure on that set: it is the infimum sum of measures of countable set of
sets that covers that set, where a different measure can be used for each set in the cover. -/
lemma binfi_apply {ι} {I : set ι} (hI : I.nonempty) (m : ι → outer_measure α) (s : set α) :
(⨅ i ∈ I, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i ∈ I, m i (t n) :=
by { haveI := hI.to_subtype, simp only [← infi_subtype'', infi_apply] }
/-- The value of the Infimum of a nonempty family of outer measures on a set is not simply
the minimum value of a measure on that set: it is the infimum sum of measures of countable set of
sets that covers that set, where a different measure can be used for each set in the cover. -/
lemma binfi_apply' {ι} (I : set ι) (m : ι → outer_measure α) {s : set α} (hs : s.nonempty) :
(⨅ i ∈ I, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i ∈ I, m i (t n) :=
by { simp only [← infi_subtype'', infi_apply' _ hs] }
lemma map_infi_le {ι β} (f : α → β) (m : ι → outer_measure α) :
map f (⨅ i, m i) ≤ ⨅ i, map f (m i) :=
(map_mono f).map_infi_le
lemma comap_infi {ι β} (f : α → β) (m : ι → outer_measure β) :
comap f (⨅ i, m i) = ⨅ i, comap f (m i) :=
begin
refine ext_nonempty (λ s hs, _),
refine ((comap_mono f).map_infi_le s).antisymm _,
simp only [comap_apply, infi_apply' _ hs, infi_apply' _ (hs.image _),
le_infi_iff, set.image_subset_iff, preimage_Union],
refine λ t ht, infi_le_of_le _ (infi_le_of_le ht $ ennreal.tsum_le_tsum $ λ k, _),
exact infi_mono (λ i, (m i).mono (image_preimage_subset _ _))
end
lemma map_infi {ι β} {f : α → β} (hf : injective f) (m : ι → outer_measure α) :
map f (⨅ i, m i) = restrict (range f) (⨅ i, map f (m i)) :=
begin
refine eq.trans _ (map_comap _ _),
simp only [comap_infi, comap_map hf]
end
lemma map_infi_comap {ι β} [nonempty ι] {f : α → β} (m : ι → outer_measure β) :
map f (⨅ i, comap f (m i)) = ⨅ i, map f (comap f (m i)) :=
begin
refine (map_infi_le _ _).antisymm (λ s, _),
simp only [map_apply, comap_apply, infi_apply, le_infi_iff],
refine λ t ht, infi_le_of_le (λ n, f '' (t n) ∪ (range f)ᶜ) (infi_le_of_le _ _),
{ rw [← Union_union, set.union_comm, ← inter_subset, ← image_Union,
← image_preimage_eq_inter_range],
exact image_subset _ ht },
{ refine ennreal.tsum_le_tsum (λ n, infi_mono $ λ i, (m i).mono _),
simp }
end
lemma map_binfi_comap {ι β} {I : set ι} (hI : I.nonempty) {f : α → β} (m : ι → outer_measure β) :
map f (⨅ i ∈ I, comap f (m i)) = ⨅ i ∈ I, map f (comap f (m i)) :=
by { haveI := hI.to_subtype, rw [← infi_subtype'', ← infi_subtype''], exact map_infi_comap _ }
lemma restrict_infi_restrict {ι} (s : set α) (m : ι → outer_measure α) :
restrict s (⨅ i, restrict s (m i)) = restrict s (⨅ i, m i) :=
calc restrict s (⨅ i, restrict s (m i)) = restrict (range (coe : s → α)) (⨅ i, restrict s (m i)) :
by rw [subtype.range_coe]
... = map (coe : s → α) (⨅ i, comap coe (m i)) : (map_infi subtype.coe_injective _).symm
... = restrict s (⨅ i, m i) : congr_arg (map coe) (comap_infi _ _).symm
lemma restrict_infi {ι} [nonempty ι] (s : set α) (m : ι → outer_measure α) :
restrict s (⨅ i, m i) = ⨅ i, restrict s (m i) :=
(congr_arg (map coe) (comap_infi _ _)).trans (map_infi_comap _)
lemma restrict_binfi {ι} {I : set ι} (hI : I.nonempty) (s : set α) (m : ι → outer_measure α) :
restrict s (⨅ i ∈ I, m i) = ⨅ i ∈ I, restrict s (m i) :=
by { haveI := hI.to_subtype, rw [← infi_subtype'', ← infi_subtype''], exact restrict_infi _ _ }
/-- This proves that Inf and restrict commute for outer measures, so long as the set of
outer measures is nonempty. -/
lemma restrict_Inf_eq_Inf_restrict
(m : set (outer_measure α)) {s : set α} (hm : m.nonempty) :
restrict s (Inf m) = Inf ((restrict s) '' m) :=
by simp only [Inf_eq_infi, restrict_binfi, hm, infi_image]
end Inf_gen
end outer_measure
open outer_measure
/-! ### Induced Outer Measure
We can extend a function defined on a subset of `set α` to an outer measure.
The underlying function is called `extend`, and the measure it induces is called
`induced_outer_measure`.
Some lemmas below are proven twice, once in the general case, and one where the function `m`
is only defined on measurable sets (i.e. when `P = measurable_set`). In the latter cases, we can
remove some hypotheses in the statement. The general version has the same name, but with a prime
at the end. -/
section extend
variables {α : Type*} {P : α → Prop}
variables (m : Π (s : α), P s → ℝ≥0∞)
/-- We can trivially extend a function defined on a subclass of objects (with codomain `ℝ≥0∞`)
to all objects by defining it to be `∞` on the objects not in the class. -/
def extend (s : α) : ℝ≥0∞ := ⨅ h : P s, m s h
lemma smul_extend {R} [has_zero R] [smul_with_zero R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞]
[no_zero_smul_divisors R ℝ≥0∞] {c : R} (hc : c ≠ 0) :
c • extend m = extend (λ s h, c • m s h) :=
begin
ext1 s,
dsimp [extend],
by_cases h : P s,
{ simp [h] },
{ simp [h, ennreal.smul_top, hc] },
end
lemma extend_eq {s : α} (h : P s) : extend m s = m s h :=
by simp [extend, h]
lemma extend_eq_top {s : α} (h : ¬P s) : extend m s = ∞ :=
by simp [extend, h]
lemma le_extend {s : α} (h : P s) : m s h ≤ extend m s :=
by { simp only [extend, le_infi_iff], intro, refl' }
-- TODO: why this is a bad `congr` lemma?
lemma extend_congr {β : Type*} {Pb : β → Prop} {mb : Π s : β, Pb s → ℝ≥0∞}
{sa : α} {sb : β} (hP : P sa ↔ Pb sb) (hm : ∀ (ha : P sa) (hb : Pb sb), m sa ha = mb sb hb) :
extend m sa = extend mb sb :=
infi_congr_Prop hP (λ h, hm _ _)
@[simp] lemma extend_top {α : Type*} {P : α → Prop} :
extend (λ s h, ∞ : Π (s : α), P s → ℝ≥0∞) = ⊤ :=
funext $ λ x, infi_eq_top.mpr $ λ i, rfl
end extend
section extend_set
variables {α : Type*} {P : set α → Prop}
variables {m : Π (s : set α), P s → ℝ≥0∞}
variables (P0 : P ∅) (m0 : m ∅ P0 = 0)
variables (PU : ∀{{f : ℕ → set α}} (hm : ∀i, P (f i)), P (⋃i, f i))
variables (mU : ∀ {{f : ℕ → set α}} (hm : ∀i, P (f i)), pairwise (disjoint on f) →
m (⋃i, f i) (PU hm) = ∑'i, m (f i) (hm i))
variables (msU : ∀ {{f : ℕ → set α}} (hm : ∀i, P (f i)),
m (⋃i, f i) (PU hm) ≤ ∑'i, m (f i) (hm i))
variables (m_mono : ∀⦃s₁ s₂ : set α⦄ (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂)
lemma extend_empty : extend m ∅ = 0 :=
(extend_eq _ P0).trans m0
lemma extend_Union_nat
{f : ℕ → set α} (hm : ∀i, P (f i))
(mU : m (⋃i, f i) (PU hm) = ∑'i, m (f i) (hm i)) :
extend m (⋃i, f i) = ∑'i, extend m (f i) :=
(extend_eq _ _).trans $ mU.trans $ by { congr' with i, rw extend_eq }
section subadditive
include PU msU
lemma extend_Union_le_tsum_nat'
(s : ℕ → set α) : extend m (⋃i, s i) ≤ ∑'i, extend m (s i) :=
begin
by_cases h : ∀i, P (s i),
{ rw [extend_eq _ (PU h), congr_arg tsum _],
{ apply msU h },
funext i, apply extend_eq _ (h i) },
{ cases not_forall.1 h with i hi,
exact le_trans (le_infi $ λ h, hi.elim h) (ennreal.le_tsum i) }
end
end subadditive
section mono
include m_mono
lemma extend_mono'
⦃s₁ s₂ : set α⦄ (h₁ : P s₁) (hs : s₁ ⊆ s₂) : extend m s₁ ≤ extend m s₂ :=
by { refine le_infi _, intro h₂, rw [extend_eq m h₁], exact m_mono h₁ h₂ hs }
end mono
section unions
include P0 m0 PU mU
lemma extend_Union {β} [countable β] {f : β → set α} (hd : pairwise (disjoint on f))
(hm : ∀ i, P (f i)) :
extend m (⋃i, f i) = ∑'i, extend m (f i) :=
begin
casesI nonempty_encodable β,
rw [← encodable.Union_decode₂, ← tsum_Union_decode₂],
{ exact extend_Union_nat PU
(λ n, encodable.Union_decode₂_cases P0 hm)
(mU _ (encodable.Union_decode₂_disjoint_on hd)) },
{ exact extend_empty P0 m0 }
end
lemma extend_union {s₁ s₂ : set α} (hd : disjoint s₁ s₂) (h₁ : P s₁) (h₂ : P s₂) :
extend m (s₁ ∪ s₂) = extend m s₁ + extend m s₂ :=
begin
rw [union_eq_Union, extend_Union P0 m0 PU mU
(pairwise_disjoint_on_bool.2 hd) (bool.forall_bool.2 ⟨h₂, h₁⟩), tsum_fintype],
simp
end
end unions
variable (m)
/-- Given an arbitrary function on a subset of sets, we can define the outer measure corresponding
to it (this is the unique maximal outer measure that is at most `m` on the domain of `m`). -/
def induced_outer_measure : outer_measure α :=
outer_measure.of_function (extend m) (extend_empty P0 m0)
variables {m P0 m0}
lemma le_induced_outer_measure {μ : outer_measure α} :
μ ≤ induced_outer_measure m P0 m0 ↔ ∀ s (hs : P s), μ s ≤ m s hs :=
le_of_function.trans $ forall_congr $ λ s, le_infi_iff
/-- If `P u` is `false` for any set `u` that has nonempty intersection both with `s` and `t`, then
`μ (s ∪ t) = μ s + μ t`, where `μ = induced_outer_measure m P0 m0`.
E.g., if `α` is an (e)metric space and `P u = diam u < r`, then this lemma implies that
`μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s` and `y ∈ t`. -/
lemma induced_outer_measure_union_of_false_of_nonempty_inter {s t : set α}
(h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → ¬P u) :
induced_outer_measure m P0 m0 (s ∪ t) =
induced_outer_measure m P0 m0 s + induced_outer_measure m P0 m0 t :=
of_function_union_of_top_of_nonempty_inter $ λ u hsu htu, @infi_of_empty _ _ _ ⟨h u hsu htu⟩ _
include msU m_mono
lemma induced_outer_measure_eq_extend' {s : set α} (hs : P s) :
induced_outer_measure m P0 m0 s = extend m s :=
of_function_eq s (λ t, extend_mono' m_mono hs) (extend_Union_le_tsum_nat' PU msU)
lemma induced_outer_measure_eq' {s : set α} (hs : P s) :
induced_outer_measure m P0 m0 s = m s hs :=
(induced_outer_measure_eq_extend' PU msU m_mono hs).trans $ extend_eq _ _
lemma induced_outer_measure_eq_infi (s : set α) :
induced_outer_measure m P0 m0 s = ⨅ (t : set α) (ht : P t) (h : s ⊆ t), m t ht :=
begin
apply le_antisymm,
{ simp only [le_infi_iff], intros t ht hs,
refine le_trans (mono' _ hs) _,
exact le_of_eq (induced_outer_measure_eq' _ msU m_mono _) },
{ refine le_infi _, intro f, refine le_infi _, intro hf,
refine le_trans _ (extend_Union_le_tsum_nat' _ msU _),
refine le_infi _, intro h2f,
refine infi_le_of_le _ (infi_le_of_le h2f $ infi_le _ hf) }
end
lemma induced_outer_measure_preimage (f : α ≃ α) (Pm : ∀ (s : set α), P (f ⁻¹' s) ↔ P s)
(mm : ∀ (s : set α) (hs : P s), m (f ⁻¹' s) ((Pm _).mpr hs) = m s hs)
{A : set α} : induced_outer_measure m P0 m0 (f ⁻¹' A) = induced_outer_measure m P0 m0 A :=
begin
simp only [induced_outer_measure_eq_infi _ msU m_mono], symmetry,
refine f.injective.preimage_surjective.infi_congr (preimage f) (λ s, _),
refine infi_congr_Prop (Pm s) _, intro hs,
refine infi_congr_Prop f.surjective.preimage_subset_preimage_iff _,
intro h2s, exact mm s hs
end
lemma induced_outer_measure_exists_set {s : set α}
(hs : induced_outer_measure m P0 m0 s ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) :
∃ (t : set α) (ht : P t), s ⊆ t ∧
induced_outer_measure m P0 m0 t ≤ induced_outer_measure m P0 m0 s + ε :=
begin
have := ennreal.lt_add_right hs hε,
conv at this {to_lhs, rw induced_outer_measure_eq_infi _ msU m_mono },
simp only [infi_lt_iff] at this,
rcases this with ⟨t, h1t, h2t, h3t⟩,
exact ⟨t, h1t, h2t,
le_trans (le_of_eq $ induced_outer_measure_eq' _ msU m_mono h1t) (le_of_lt h3t)⟩
end
/-- To test whether `s` is Carathéodory-measurable we only need to check the sets `t` for which
`P t` holds. See `of_function_caratheodory` for another way to show the Carathéodory-measurability
of `s`.
-/
lemma induced_outer_measure_caratheodory (s : set α) :
measurable_set[(induced_outer_measure m P0 m0).caratheodory] s ↔ ∀ (t : set α), P t →
induced_outer_measure m P0 m0 (t ∩ s) + induced_outer_measure m P0 m0 (t \ s) ≤
induced_outer_measure m P0 m0 t :=
begin
rw is_caratheodory_iff_le,
split,
{ intros h t ht, exact h t },
{ intros h u, conv_rhs { rw induced_outer_measure_eq_infi _ msU m_mono },
refine le_infi _, intro t, refine le_infi _, intro ht, refine le_infi _, intro h2t,
refine le_trans _ (le_trans (h t ht) $ le_of_eq $ induced_outer_measure_eq' _ msU m_mono ht),
refine add_le_add (mono' _ $ set.inter_subset_inter_left _ h2t)
(mono' _ $ diff_subset_diff_left h2t) }
end
end extend_set
/-! If `P` is `measurable_set` for some measurable space, then we can remove some hypotheses of the
above lemmas. -/
section measurable_space
variables {α : Type*} [measurable_space α]
variables {m : Π (s : set α), measurable_set s → ℝ≥0∞}
variables (m0 : m ∅ measurable_set.empty = 0)
variable (mU : ∀ {{f : ℕ → set α}} (hm : ∀i, measurable_set (f i)), pairwise (disjoint on f) →
m (⋃i, f i) (measurable_set.Union hm) = ∑'i, m (f i) (hm i))
include m0 mU
lemma extend_mono {s₁ s₂ : set α} (h₁ : measurable_set s₁) (hs : s₁ ⊆ s₂) :
extend m s₁ ≤ extend m s₂ :=
begin
refine le_infi _, intro h₂,
have := extend_union measurable_set.empty m0 measurable_set.Union mU disjoint_sdiff_self_right
h₁ (h₂.diff h₁),
rw union_diff_cancel hs at this,
rw ← extend_eq m,
exact le_iff_exists_add.2 ⟨_, this⟩,
end
lemma extend_Union_le_tsum_nat : ∀ (s : ℕ → set α), extend m (⋃i, s i) ≤ ∑'i, extend m (s i) :=
begin
refine extend_Union_le_tsum_nat' measurable_set.Union _, intros f h,
simp [Union_disjointed.symm] {single_pass := tt},
rw [mU (measurable_set.disjointed h) (disjoint_disjointed _)],
refine ennreal.tsum_le_tsum (λ i, _),
rw [← extend_eq m, ← extend_eq m],
exact extend_mono m0 mU (measurable_set.disjointed h _) (disjointed_le f _),
end
lemma induced_outer_measure_eq_extend {s : set α} (hs : measurable_set s) :
induced_outer_measure m measurable_set.empty m0 s = extend m s :=
of_function_eq s (λ t, extend_mono m0 mU hs) (extend_Union_le_tsum_nat m0 mU)
lemma induced_outer_measure_eq {s : set α} (hs : measurable_set s) :
induced_outer_measure m measurable_set.empty m0 s = m s hs :=
(induced_outer_measure_eq_extend m0 mU hs).trans $ extend_eq _ _
end measurable_space
namespace outer_measure
variables {α : Type*} [measurable_space α] (m : outer_measure α)
/-- Given an outer measure `m` we can forget its value on non-measurable sets, and then consider
`m.trim`, the unique maximal outer measure less than that function. -/
def trim : outer_measure α :=
induced_outer_measure (λ s _, m s) measurable_set.empty m.empty
theorem le_trim : m ≤ m.trim :=
le_of_function.mpr $ λ s, le_infi $ λ _, le_rfl
theorem trim_eq {s : set α} (hs : measurable_set s) : m.trim s = m s :=
induced_outer_measure_eq' measurable_set.Union (λ f hf, m.Union_nat f) (λ _ _ _ _ h, m.mono h) hs
theorem trim_congr {m₁ m₂ : outer_measure α}
(H : ∀ {s : set α}, measurable_set s → m₁ s = m₂ s) :
m₁.trim = m₂.trim :=
by { unfold trim, congr, funext s hs, exact H hs }
@[mono] theorem trim_mono : monotone (trim : outer_measure α → outer_measure α) :=
λ m₁ m₂ H s, infi₂_mono $ λ f hs, ennreal.tsum_le_tsum $ λ b, infi_mono $ λ hf, H _
theorem le_trim_iff {m₁ m₂ : outer_measure α} :
m₁ ≤ m₂.trim ↔ ∀ s, measurable_set s → m₁ s ≤ m₂ s :=
le_of_function.trans $ forall_congr $ λ s, le_infi_iff
theorem trim_le_trim_iff {m₁ m₂ : outer_measure α} :
m₁.trim ≤ m₂.trim ↔ ∀ s, measurable_set s → m₁ s ≤ m₂ s :=
le_trim_iff.trans $ forall₂_congr $ λ s hs, by rw [trim_eq _ hs]
theorem trim_eq_trim_iff {m₁ m₂ : outer_measure α} :
m₁.trim = m₂.trim ↔ ∀ s, measurable_set s → m₁ s = m₂ s :=
by simp only [le_antisymm_iff, trim_le_trim_iff, forall_and_distrib]
theorem trim_eq_infi (s : set α) : m.trim s = ⨅ t (st : s ⊆ t) (ht : measurable_set t), m t :=
by { simp only [infi_comm] {single_pass := tt}, exact induced_outer_measure_eq_infi
measurable_set.Union (λ f _, m.Union_nat f) (λ _ _ _ _ h, m.mono h) s }
theorem trim_eq_infi' (s : set α) : m.trim s = ⨅ t : {t // s ⊆ t ∧ measurable_set t}, m t :=
by simp [infi_subtype, infi_and, trim_eq_infi]
theorem trim_trim (m : outer_measure α) : m.trim.trim = m.trim :=
trim_eq_trim_iff.2 $ λ s, m.trim_eq
@[simp] theorem trim_top : (⊤ : outer_measure α).trim = ⊤ := eq_top_iff.2 $ le_trim _
@[simp] theorem trim_zero : (0 : outer_measure α).trim = 0 :=
ext $ λ s, le_antisymm
(le_trans ((trim 0).mono (subset_univ s)) $
le_of_eq $ trim_eq _ measurable_set.univ)
(zero_le _)
theorem trim_sum_ge {ι} (m : ι → outer_measure α) : sum (λ i, (m i).trim) ≤ (sum m).trim :=
λ s, by simp [trim_eq_infi]; exact
λ t st ht, ennreal.tsum_le_tsum (λ i,
infi_le_of_le t $ infi_le_of_le st $ infi_le _ ht)
lemma exists_measurable_superset_eq_trim (m : outer_measure α) (s : set α) :
∃ t, s ⊆ t ∧ measurable_set t ∧ m t = m.trim s :=
begin
simp only [trim_eq_infi], set ms := ⨅ (t : set α) (st : s ⊆ t) (ht : measurable_set t), m t,
by_cases hs : ms = ∞,
{ simp only [hs],
simp only [infi_eq_top] at hs,
exact ⟨univ, subset_univ s, measurable_set.univ, hs _ (subset_univ s) measurable_set.univ⟩ },
{ have : ∀ r > ms, ∃ t, s ⊆ t ∧ measurable_set t ∧ m t < r,
{ intros r hs,
simpa [infi_lt_iff] using hs },
have : ∀ n : ℕ, ∃ t, s ⊆ t ∧ measurable_set t ∧ m t < ms + n⁻¹,
{ assume n,
refine this _ (ennreal.lt_add_right hs _),
simp },
choose t hsub hm hm',
refine ⟨⋂ n, t n, subset_Inter hsub, measurable_set.Inter hm, _⟩,
have : tendsto (λ n : ℕ, ms + n⁻¹) at_top (𝓝 (ms + 0)),
from tendsto_const_nhds.add ennreal.tendsto_inv_nat_nhds_zero,
rw add_zero at this,
refine le_antisymm (ge_of_tendsto' this $ λ n, _) _,
{ exact le_trans (m.mono' $ Inter_subset t n) (hm' n).le },
{ refine infi_le_of_le (⋂ n, t n) _,
refine infi_le_of_le (subset_Inter hsub) _,
refine infi_le _ (measurable_set.Inter hm) } }
end
lemma exists_measurable_superset_of_trim_eq_zero
{m : outer_measure α} {s : set α} (h : m.trim s = 0) :
∃t, s ⊆ t ∧ measurable_set t ∧ m t = 0 :=
begin
rcases exists_measurable_superset_eq_trim m s with ⟨t, hst, ht, hm⟩,
exact ⟨t, hst, ht, h ▸ hm⟩
end
/-- If `μ i` is a countable family of outer measures, then for every set `s` there exists
a measurable set `t ⊇ s` such that `μ i t = (μ i).trim s` for all `i`. -/
lemma exists_measurable_superset_forall_eq_trim {ι} [countable ι] (μ : ι → outer_measure α)
(s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ ∀ i, μ i t = (μ i).trim s :=
begin
choose t hst ht hμt using λ i, (μ i).exists_measurable_superset_eq_trim s,
replace hst := subset_Inter hst,
replace ht := measurable_set.Inter ht,
refine ⟨⋂ i, t i, hst, ht, λ i, le_antisymm _ _⟩,
exacts [hμt i ▸ (μ i).mono (Inter_subset _ _),
(mono' _ hst).trans_eq ((μ i).trim_eq ht)]
end
/-- If `m₁ s = op (m₂ s) (m₃ s)` for all `s`, then the same is true for `m₁.trim`, `m₂.trim`,
and `m₃ s`. -/
theorem trim_binop {m₁ m₂ m₃ : outer_measure α} {op : ℝ≥0∞ → ℝ≥0∞ → ℝ≥0∞}
(h : ∀ s, m₁ s = op (m₂ s) (m₃ s)) (s : set α) :
m₁.trim s = op (m₂.trim s) (m₃.trim s) :=
begin
rcases exists_measurable_superset_forall_eq_trim (![m₁, m₂, m₃]) s
with ⟨t, hst, ht, htm⟩,
simp only [fin.forall_fin_succ, matrix.cons_val_zero, matrix.cons_val_succ] at htm,
rw [← htm.1, ← htm.2.1, ← htm.2.2.1, h]
end
/-- If `m₁ s = op (m₂ s)` for all `s`, then the same is true for `m₁.trim` and `m₂.trim`. -/
theorem trim_op {m₁ m₂ : outer_measure α} {op : ℝ≥0∞ → ℝ≥0∞}
(h : ∀ s, m₁ s = op (m₂ s)) (s : set α) :
m₁.trim s = op (m₂.trim s) :=
@trim_binop α _ m₁ m₂ 0 (λ a b, op a) h s
/-- `trim` is additive. -/
theorem trim_add (m₁ m₂ : outer_measure α) : (m₁ + m₂).trim = m₁.trim + m₂.trim :=
ext $ trim_binop (add_apply m₁ m₂)
/-- `trim` respects scalar multiplication. -/
theorem trim_smul {R : Type*} [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞]
(c : R) (m : outer_measure α) :
(c • m).trim = c • m.trim :=
ext $ trim_op (smul_apply c m)
/-- `trim` sends the supremum of two outer measures to the supremum of the trimmed measures. -/
theorem trim_sup (m₁ m₂ : outer_measure α) : (m₁ ⊔ m₂).trim = m₁.trim ⊔ m₂.trim :=
ext $ λ s, (trim_binop (sup_apply m₁ m₂) s).trans (sup_apply _ _ _).symm
/-- `trim` sends the supremum of a countable family of outer measures to the supremum
of the trimmed measures. -/
lemma trim_supr {ι} [countable ι] (μ : ι → outer_measure α) : trim (⨆ i, μ i) = ⨆ i, trim (μ i) :=
begin
simp_rw [←@supr_plift_down _ ι],
ext1 s,
haveI : countable (option $ plift ι) := @option.countable (plift ι) _,
obtain ⟨t, hst, ht, hμt⟩ := exists_measurable_superset_forall_eq_trim
(option.elim (⨆ i, μ (plift.down i)) (μ ∘ plift.down)) s,
simp only [option.forall, option.elim] at hμt,
simp only [supr_apply, ← hμt.1, ← hμt.2]
end
/-- The trimmed property of a measure μ states that `μ.to_outer_measure.trim = μ.to_outer_measure`.
This theorem shows that a restricted trimmed outer measure is a trimmed outer measure. -/
lemma restrict_trim {μ : outer_measure α} {s : set α} (hs : measurable_set s) :
(restrict s μ).trim = restrict s μ.trim :=
begin
refine le_antisymm (λ t, _) (le_trim_iff.2 $ λ t ht, _),
{ rw restrict_apply,
rcases μ.exists_measurable_superset_eq_trim (t ∩ s) with ⟨t', htt', ht', hμt'⟩,
rw [← hμt'], rw inter_subset at htt',
refine (mono' _ htt').trans _,
rw [trim_eq _ (hs.compl.union ht'), restrict_apply, union_inter_distrib_right,
compl_inter_self, set.empty_union],
exact μ.mono' (inter_subset_left _ _) },
{ rw [restrict_apply, trim_eq _ (ht.inter hs), restrict_apply],
exact le_rfl }
end
end outer_measure
end measure_theory
|
b9d243b5b9e42c0dcfc1912ab0e8a30c38a4c2b1 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/category_theory/limits/over.lean | 12ef920cd1f8e7a5a828d0e0ae1c93b0a0820390 | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 5,314 | lean | /-
Copyright (c) 2018 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Reid Barton, Bhavik Mehta
-/
import category_theory.over
import category_theory.limits.preserves.basic
universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation
open category_theory category_theory.limits
variables {J : Type v} [small_category J]
variables {C : Type u} [category.{v} C]
variable {X : C}
namespace category_theory.functor
@[simps] def to_cocone (F : J ⥤ over X) : cocone (F ⋙ over.forget) :=
{ X := X,
ι := { app := λ j, (F.obj j).hom } }
@[simps] def to_cone (F : J ⥤ under X) : cone (F ⋙ under.forget) :=
{ X := X,
π := { app := λ j, (F.obj j).hom } }
end category_theory.functor
namespace category_theory.over
@[simps] def colimit (F : J ⥤ over X) [has_colimit (F ⋙ forget)] : cocone F :=
{ X := mk $ colimit.desc (F ⋙ forget) F.to_cocone,
ι :=
{ app := λ j, hom_mk $ colimit.ι (F ⋙ forget) j,
naturality' :=
begin
intros j j' f,
have := colimit.w (F ⋙ forget) f,
tidy
end } }
def forget_colimit_is_colimit (F : J ⥤ over X) [has_colimit (F ⋙ forget)] :
is_colimit (forget.map_cocone (colimit F)) :=
is_colimit.of_iso_colimit (colimit.is_colimit (F ⋙ forget)) (cocones.ext (iso.refl _) (by tidy))
instance : reflects_colimits (forget : over X ⥤ C) :=
{ reflects_colimits_of_shape := λ J 𝒥,
{ reflects_colimit := λ F,
by constructor; exactI λ t ht,
{ desc := λ s, hom_mk (ht.desc (forget.map_cocone s))
begin
apply ht.hom_ext, intro j,
rw [←category.assoc, ht.fac],
transitivity (F.obj j).hom,
exact w (s.ι.app j), -- TODO: How to write (s.ι.app j).w?
exact (w (t.ι.app j)).symm,
end,
fac' := begin
intros s j, ext, exact ht.fac (forget.map_cocone s) j
-- TODO: Ask Simon about multiple ext lemmas for defeq types (comma_morphism & over.category.hom)
end,
uniq' :=
begin
intros s m w,
ext1 j,
exact ht.uniq (forget.map_cocone s) m.left (λ j, congr_arg comma_morphism.left (w j))
end } } }
instance has_colimit {F : J ⥤ over X} [has_colimit (F ⋙ forget)] : has_colimit F :=
{ cocone := colimit F,
is_colimit := reflects_colimit.reflects (forget_colimit_is_colimit F) }
instance has_colimits_of_shape [has_colimits_of_shape J C] :
has_colimits_of_shape J (over X) :=
{ has_colimit := λ F, by apply_instance }
instance has_colimits [has_colimits C] : has_colimits (over X) :=
{ has_colimits_of_shape := λ J 𝒥, by resetI; apply_instance }
instance forget_preserves_colimit {X : C} {F : J ⥤ over X} [has_colimit (F ⋙ forget)] :
preserves_colimit F (forget : over X ⥤ C) :=
preserves_colimit_of_preserves_colimit_cocone (colimit.is_colimit F) (forget_colimit_is_colimit F)
instance forget_preserves_colimits_of_shape [has_colimits_of_shape J C] {X : C} :
preserves_colimits_of_shape J (forget : over X ⥤ C) :=
{ preserves_colimit := λ F, by apply_instance }
instance forget_preserves_colimits [has_colimits C] {X : C} :
preserves_colimits (forget : over X ⥤ C) :=
{ preserves_colimits_of_shape := λ J 𝒥, by apply_instance }
end category_theory.over
namespace category_theory.under
@[simps] def limit (F : J ⥤ under X) [has_limit (F ⋙ forget)] : cone F :=
{ X := mk $ limit.lift (F ⋙ forget) F.to_cone,
π :=
{ app := λ j, hom_mk $ limit.π (F ⋙ forget) j,
naturality' :=
begin
intros j j' f,
have := (limit.w (F ⋙ forget) f).symm,
tidy
end } }
def forget_limit_is_limit (F : J ⥤ under X) [has_limit (F ⋙ forget)] :
is_limit (forget.map_cone (limit F)) :=
is_limit.of_iso_limit (limit.is_limit (F ⋙ forget)) (cones.ext (iso.refl _) (by tidy))
instance : reflects_limits (forget : under X ⥤ C) :=
{ reflects_limits_of_shape := λ J 𝒥,
{ reflects_limit := λ F,
by constructor; exactI λ t ht,
{ lift := λ s, hom_mk (ht.lift (forget.map_cone s))
begin
apply ht.hom_ext, intro j,
rw [category.assoc, ht.fac],
transitivity (F.obj j).hom,
exact w (s.π.app j),
exact (w (t.π.app j)).symm,
end,
fac' := begin
intros s j, ext, exact ht.fac (forget.map_cone s) j
end,
uniq' :=
begin
intros s m w,
ext1 j,
exact ht.uniq (forget.map_cone s) m.right (λ j, congr_arg comma_morphism.right (w j))
end } } }
instance has_limit {F : J ⥤ under X} [has_limit (F ⋙ forget)] : has_limit F :=
{ cone := limit F,
is_limit := reflects_limit.reflects (forget_limit_is_limit F) }
instance has_limits_of_shape [has_limits_of_shape J C] :
has_limits_of_shape J (under X) :=
{ has_limit := λ F, by apply_instance }
instance has_limits [has_limits C] : has_limits (under X) :=
{ has_limits_of_shape := λ J 𝒥, by resetI; apply_instance }
instance forget_preserves_limits [has_limits C] {X : C} :
preserves_limits (forget : under X ⥤ C) :=
{ preserves_limits_of_shape := λ J 𝒥,
{ preserves_limit := λ F, by exactI
preserves_limit_of_preserves_limit_cone (limit.is_limit F) (forget_limit_is_limit F) } }
end category_theory.under
|
5cd618b57e1271f5dab043aee614172de393db7a | 6b45072eb2b3db3ecaace2a7a0241ce81f815787 | /tools/auto/experiments/test2.lean | 7afe89d3f3ce4643839ed2daec32a94b90f8dd74 | [] | no_license | avigad/library_dev | 27b47257382667b5eb7e6476c4f5b0d685dd3ddc | 9d8ac7c7798ca550874e90fed585caad030bbfac | refs/heads/master | 1,610,452,468,791 | 1,500,712,839,000 | 1,500,713,478,000 | 69,311,142 | 1 | 0 | null | 1,474,942,903,000 | 1,474,942,902,000 | null | UTF-8 | Lean | false | false | 12,710 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Nathaniel Thomas
More examples to test automation, stolen shamelessly by Jeremy from Nathaniel's "tauto".
-/
import ..finish
open nat
section
variables (a b c d e f : Prop)
variable even : ℕ → Prop
variable P : ℕ → Prop
-- these next five are things that tauto doesn't get
example : (∀ x, P x) ∧ b → (∀ y, P y) ∧ P 0 ∨ b ∧ P 0 := by finish
example : (∀ A, A ∨ ¬A) → ∀ x y : ℕ, x = y ∨ x ≠ y := by finish
example : ∀ b1 b2, b1 = b2 ↔ (b1 = tt ↔ b2 = tt) := begin intro b1, cases b1; finish [iff_def] end
example : ∀ (P Q : nat → Prop), (∀ n, Q n → P n) → (∀ n, Q n) → P 2 := by finish
example (a b c : Prop) : ¬ true ∨ false ∨ b ↔ b := by finish
example : true := by finish
example : false → a := by finish
example : a → a := by finish
example : (a → b) → a → b := by finish
example : ¬ a → ¬ a := by finish
example : a → (false ∨ a) := by finish
example : (a → b → c) → (a → b) → a → c := by finish
example : a → ¬ a → (a → b) → (a ∨ b) → (a ∧ b) → a → false := by finish
example : ((a ∧ b) ∧ c) → b := by finish
example : ((a → b) → c) → b → c := by finish
example : (a ∨ b) → (b ∨ a) := by finish
example : (a → b ∧ c) → (a → b) ∨ (a → c) := by finish
example : ∀ (x0 : a ∨ b) (x1 : b ∧ c), a → b := by finish
example : a → b → (c ∨ b) := by finish
example : (a ∧ b → c) → b → a → c := by finish
example : (a ∨ b → c) → a → c := by finish
example : (a ∨ b → c) → b → c := by finish
example : (a ∧ b) → (b ∧ a) := by finish
example : (a ↔ b) → a → b := by finish
example : a → ¬¬a := by finish
example : ¬¬(a ∨ ¬a) := by finish
example : ¬¬(a ∨ b → a ∨ b) := by finish
example : ¬¬((∀ n, even n) ∨ ¬(∀ m, even m)) := by finish
example : (¬¬b → b) → (a → b) → ¬¬a → b := by finish
example : (¬¬b → b) → (¬b → ¬ a) → ¬¬a → b := by finish
example : ((a → b → false) → false) → (b → false) → false := by finish
example : ((((c → false) → a) → ((b → false) → a) → false) → false) →
(((c → b → false) → false) → false) → ¬a → a := by finish
example (p q r : Prop) (a b : nat) : true → a = a → q → q → p → p := by finish
example : ∀ (F F' : Prop), F ∧ F' → F := by finish
example : ∀ (F1 F2 F3 : Prop), ((¬F1 ∧ F3) ∨ (F2 ∧ ¬F3)) → (F2 → F1) → (F2 → F3) → ¬F2 := by finish
example : ∀ (f : nat → Prop), f 2 → ∃ x, f x := by finish
example : true ∧ true ∧ true ∧ true ∧ true ∧ true ∧ true := by finish
example : ∀ (P : nat → Prop), P 0 → (P 0 → P 1) → (P 1 → P 2) → (P 2) := by finish
example : ¬¬¬¬¬a → ¬¬¬¬¬¬¬¬a → false := by finish
example : ∀ n, ¬¬(even n ∨ ¬even n) := by finish
example : ∀ (p q r s : Prop) (a b : nat), r ∨ s → p ∨ q → a = b → q ∨ p := by finish
example : (∀ x, P x) → (∀ y, P y) := by finish
/- TODO(Jeremy): reinstate after simp * at * bug is fixed.
example : ((a ↔ b) → (b ↔ c)) → ((b ↔ c) → (c ↔ a)) → ((c ↔ a) → (a ↔ b)) → (a ↔ b) :=
by finish [iff_def]
-/
example : ((¬a ∨ b) ∧ (¬b ∨ b) ∧ (¬a ∨ ¬b) ∧ (¬b ∨ ¬b) → false) → ¬((a → b) → b) → false := by finish
example : ¬((a → b) → b) → ((¬b ∨ ¬b) ∧ (¬b ∨ ¬a) ∧ (b ∨ ¬b) ∧ (b ∨ ¬a) → false) → false := by finish
example : (¬a ↔ b) → (¬b ↔ a) → (¬¬a ↔ a) := by finish
example : (¬ a ↔ b) → (¬ (c ∨ e) ↔ d ∧ f) → (¬ (c ∨ a ∨ e) ↔ d ∧ b ∧ f) := by finish
example {A : Type} (p q : A → Prop) (a b : A) : q a → p b → ∃ x, (p x ∧ x = b) ∨ q x := by finish
example {A : Type} (p q : A → Prop) (a b : A) : p b → ∃ x, q x ∨ (p x ∧ x = b) := by finish
example : ¬ a → b → a → c := by finish
example : a → b → b → ¬ a → c := by finish
example (a b : nat) : a = b → b = a := by finish
-- good examples of things we don't get, even using the simplifier
example (a b c : nat) : a = b → a = c → b = c := by finish
example (p : nat → Prop) (a b c : nat) : a = b → a = c → p b → p c := by finish
example (p : Prop) (a b : nat) : a = b → p → p := by finish
-- safe should look for contradictions with constructors
example (a : nat) : (0 : ℕ) = succ a → a = a → false := by finish
example (p : Prop) (a b c : nat) : [a, b, c] = [] → p := by finish
example (a b c : nat) : succ (succ a) = succ (succ b) → c = c := by finish
example (p : Prop) (a b : nat) : a = b → b ≠ a → p := by finish
example : (a ↔ b) → ((b ↔ a) ↔ (a ↔ b)) := by finish
example (a b c : nat) : b = c → (a = b ↔ c = a) := by finish [iff_def]
example : ¬¬¬¬¬¬¬¬a → ¬¬¬¬¬a → false := by finish
example (a b c : Prop) : a ∧ b ∧ c ↔ c ∧ b ∧ a := by finish
example (a b c : Prop) : a ∧ false ∧ c ↔ false := by finish
example (a b c : Prop) : a ∨ false ∨ b ↔ b ∨ a := by finish
example : a ∧ not a ↔ false := by finish
example : a ∧ b ∧ true → b ∧ a := by finish
example (A : Type) (a₁ a₂ : A) : a₁ = a₂ →
(λ (B : Type) (f : A → B), f a₁) = (λ (B : Type) (f : A → B), f a₂) := by finish
example (a : nat) : ¬ a = a → false := by finish
example (A : Type) (p : Prop) (a b c : A) : a = b → b ≠ a → p := by finish
example (p q r s : Prop) : r ∧ s → p ∧ q → q ∧ p := by finish
example (p q : Prop) : p ∧ p ∧ q ∧ q → q ∧ p := by finish
example (p : nat → Prop) (q : nat → nat → Prop) :
(∃ x y, p x ∧ q x y) → q 0 0 ∧ q 1 1 → (∃ x, p x) := by finish
example (p q r s : Prop) (a b : nat) : r ∨ s → p ∨ q → a = b → q ∨ p := by finish
example (p q r : Prop) (a b : nat) : true → a = a → q → q → p → p := by finish
example (a b : Prop) : a → b → a := by finish
example (p q : nat → Prop) (a b : nat) : p a → q b → ∃ x, p x := by finish
example : ∀ b1 b2, b1 && b2 = ff ↔ (b1 = ff ∨ b2 = ff) := by finish
example : ∀ b1 b2, b1 && b2 = tt ↔ (b1 = tt ∧ b2 = tt) := by finish
example : ∀ b1 b2, b1 || b2 = ff ↔ (b1 = ff ∧ b2 = ff) := by finish
example : ∀ b1 b2, b1 || b2 = tt ↔ (b1 = tt ∨ b2 = tt) := by finish
example : ∀ b, bnot b = tt ↔ b = ff := by finish
example : ∀ b, bnot b = ff ↔ b = tt := by finish
example : ∀ b c, b = c ↔ ¬ (b = bnot c) := by intros b c; cases b; cases c; finish [iff_def]
inductive and3 (a b c : Prop) : Prop
| mk : a → b → c → and3
example (h : and3 a b c) : and3 b c a := by cases h; split; finish
inductive or3 (a b c : Prop) : Prop
| in1 : a → or3
| in2 : b → or3
| in3 : c → or3
/- TODO(Jeremy): write a tactic that tries all constructors
example (h : a) : or3 a b c := sorry
example (h : b) : or3 a b c := sorry
example (h : c) : or3 a b c := sorry
-/
variables (A₁ A₂ A₃ A₄ B₁ B₂ B₃ B₄ : Prop)
-- H first, all pos
example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄)
(a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) : B₄ := by finish
example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄)
(a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₄) : B₃ := by finish
example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄)
(a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n3 : ¬B₃) (n3 : ¬B₄) : B₂ := by finish
example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄)
(a1 : A₁) (a2 : A₂) (a3 : A₃) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : B₁ := by finish
example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄)
(a1 : A₁) (a2 : A₂) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₃ := by finish
example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄)
(a1 : A₁) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₂ := by finish
example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄)
(a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₁ := by finish
-- H last, all pos
example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃)
(H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₄ := by finish
example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₄)
(H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₃ := by finish
example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n3 : ¬B₃) (n3 : ¬B₄)
(H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₂ := by finish
example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄)
(H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₁ := by finish
example (a1 : A₁) (a2 : A₂) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄)
(H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₃ := by finish
example (a1 : A₁) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄)
(H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₂ := by finish
example (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄)
(H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₁ := by finish
-- H first, all neg
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄)
(n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) : ¬B₄ := by finish
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄)
(n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b4 : B₄) : ¬B₃ := by finish
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄)
(n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b3 : B₃) (b4 : B₄) : ¬B₂ := by finish
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄)
(n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬B₁ := by finish
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄)
(n1 : ¬A₁) (n2 : ¬A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₃ := by finish
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄)
(n1 : ¬A₁) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₂ := by finish
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄)
(n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₁ := by finish
-- H last, all neg
example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃)
(H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₄ := by finish
example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b4 : B₄)
(H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₃ := by finish
example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b3 : B₃) (b4 : B₄)
(H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₂ := by finish
example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b2 : B₂) (b3 : B₃) (b4 : B₄)
(H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₁ := by finish
example (n1 : ¬A₁) (n2 : ¬A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄)
(H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₃ := by finish
example (n1 : ¬A₁) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄)
(H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₂ := by finish
example (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄)
(H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₁ := by finish
section club
variables Scottish RedSocks WearKilt Married GoOutSunday : Prop
lemma NoMember : (¬Scottish → RedSocks) → (WearKilt ∨ ¬RedSocks) → (Married → ¬GoOutSunday) →
(GoOutSunday ↔ Scottish) → (WearKilt → Scottish ∧ Married) →
(Scottish → WearKilt) → false := by finish
end club
end
|
bb45b3a591841680fa2f254dced580cf3d31ec62 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/finset/noncomm_prod.lean | f3b54b4642b67d68d4d96527782d6b27d9f29c0e | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 15,375 | lean | /-
Copyright (c) 2021 Yakov Pechersky. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yakov Pechersky
-/
import algebra.big_operators.basic
/-!
# Products (respectively, sums) over a finset or a multiset.
The regular `finset.prod` and `multiset.prod` require `[comm_monoid α]`.
Often, there are collections `s : finset α` where `[monoid α]` and we know,
in a dependent fashion, that for all the terms `∀ (x ∈ s) (y ∈ s), commute x y`.
This allows to still have a well-defined product over `s`.
## Main definitions
- `finset.noncomm_prod`, requiring a proof of commutativity of held terms
- `multiset.noncomm_prod`, requiring a proof of commutativity of held terms
## Implementation details
While `list.prod` is defined via `list.foldl`, `noncomm_prod` is defined via
`multiset.foldr` for neater proofs and definitions. By the commutativity assumption,
the two must be equal.
-/
variables {α β γ : Type*} (f : α → β → β) (op : α → α → α)
namespace multiset
/-- Fold of a `s : multiset α` with `f : α → β → β`, given a proof that `left_commutative f`
on all elements `x ∈ s`. -/
def noncomm_foldr (s : multiset α) (comm : ∀ (x ∈ s) (y ∈ s) b, f x (f y b) = f y (f x b)) (b : β) :
β :=
s.attach.foldr (f ∘ subtype.val) (λ ⟨x, hx⟩ ⟨y, hy⟩, comm x hx y hy) b
@[simp] lemma noncomm_foldr_coe (l : list α)
(comm : ∀ (x ∈ (l : multiset α)) (y ∈ (l : multiset α)) b, f x (f y b) = f y (f x b)) (b : β) :
noncomm_foldr f (l : multiset α) comm b = l.foldr f b :=
begin
simp only [noncomm_foldr, coe_foldr, coe_attach, list.attach],
rw ←list.foldr_map,
simp [list.map_pmap, list.pmap_eq_map]
end
@[simp] lemma noncomm_foldr_empty
(h : ∀ (x ∈ (0 : multiset α)) (y ∈ (0 : multiset α)) b, f x (f y b) = f y (f x b)) (b : β) :
noncomm_foldr f (0 : multiset α) h b = b := rfl
lemma noncomm_foldr_cons (s : multiset α) (a : α)
(h : ∀ (x ∈ (a ::ₘ s)) (y ∈ (a ::ₘ s)) b, f x (f y b) = f y (f x b))
(h' : ∀ (x ∈ s) (y ∈ s) b, f x (f y b) = f y (f x b))
(b : β) :
noncomm_foldr f (a ::ₘ s) h b = f a (noncomm_foldr f s h' b) :=
begin
induction s using quotient.induction_on,
simp
end
lemma noncomm_foldr_eq_foldr (s : multiset α) (h : left_commutative f) (b : β) :
noncomm_foldr f s (λ x _ y _, h x y) b = foldr f h b s :=
begin
induction s using quotient.induction_on,
simp
end
variables [assoc : is_associative α op]
include assoc
/-- Fold of a `s : multiset α` with an associative `op : α → α → α`, given a proofs that `op`
is commutative on all elements `x ∈ s`. -/
def noncomm_fold (s : multiset α)
(comm : ∀ (x ∈ s) (y ∈ s), op x y = op y x)
(a : α) : α :=
noncomm_foldr op s (λ x hx y hy b, by rw [←assoc.assoc, comm _ hx _ hy, assoc.assoc]) a
@[simp] lemma noncomm_fold_coe (l : list α)
(comm : ∀ (x ∈ (l : multiset α)) (y ∈ (l : multiset α)), op x y = op y x)
(a : α) :
noncomm_fold op (l : multiset α) comm a = l.foldr op a :=
by simp [noncomm_fold]
@[simp] lemma noncomm_fold_empty
(h : ∀ (x ∈ (0 : multiset α)) (y ∈ (0 : multiset α)), op x y = op y x) (a : α) :
noncomm_fold op (0 : multiset α) h a = a := rfl
lemma noncomm_fold_cons (s : multiset α) (a : α)
(h : ∀ (x ∈ a ::ₘ s) (y ∈ a ::ₘ s), op x y = op y x)
(h' : ∀ (x ∈ s) (y ∈ s), op x y = op y x)
(x : α) :
noncomm_fold op (a ::ₘ s) h x = op a (noncomm_fold op s h' x) :=
begin
induction s using quotient.induction_on,
simp
end
lemma noncomm_fold_eq_fold (s : multiset α) [is_commutative α op]
(a : α) :
noncomm_fold op s (λ x _ y _, is_commutative.comm x y) a = fold op a s :=
begin
induction s using quotient.induction_on,
simp
end
omit assoc
variables [monoid α] [monoid β]
/-- Product of a `s : multiset α` with `[monoid α]`, given a proof that `*` commutes
on all elements `x ∈ s`. -/
@[to_additive "Sum of a `s : multiset α` with `[add_monoid α]`, given a proof that `+` commutes
on all elements `x ∈ s`." ]
def noncomm_prod (s : multiset α) (comm : ∀ (x ∈ s) (y ∈ s), commute x y) : α :=
s.noncomm_fold (*) comm 1
@[simp, to_additive] lemma noncomm_prod_coe (l : list α)
(comm : ∀ (x ∈ (l : multiset α)) (y ∈ (l : multiset α)), commute x y) :
noncomm_prod (l : multiset α) comm = l.prod :=
begin
rw [noncomm_prod],
simp only [noncomm_fold_coe],
induction l with hd tl hl,
{ simp },
{ rw [list.prod_cons, list.foldr, hl],
intros x hx y hy,
exact comm x (list.mem_cons_of_mem _ hx) y (list.mem_cons_of_mem _ hy) }
end
@[simp, to_additive] lemma noncomm_prod_empty
(h : ∀ (x ∈ (0 : multiset α)) (y ∈ (0 : multiset α)), commute x y) :
noncomm_prod (0 : multiset α) h = 1 := rfl
@[simp, to_additive] lemma noncomm_prod_cons (s : multiset α) (a : α)
(comm : ∀ (x ∈ a ::ₘ s) (y ∈ a ::ₘ s), commute x y) :
noncomm_prod (a ::ₘ s) comm = a * noncomm_prod s
(λ x hx y hy, comm _ (mem_cons_of_mem hx) _ (mem_cons_of_mem hy)) :=
begin
induction s using quotient.induction_on,
simp
end
@[to_additive] lemma noncomm_prod_cons' (s : multiset α) (a : α)
(comm : ∀ (x ∈ a ::ₘ s) (y ∈ a ::ₘ s), commute x y) :
noncomm_prod (a ::ₘ s) comm = noncomm_prod s
(λ x hx y hy, comm _ (mem_cons_of_mem hx) _ (mem_cons_of_mem hy)) * a :=
begin
induction s using quotient.induction_on with s,
simp only [quot_mk_to_coe, cons_coe, noncomm_prod_coe, list.prod_cons],
induction s with hd tl IH,
{ simp },
{ rw [list.prod_cons, mul_assoc, ←IH, ←mul_assoc, ←mul_assoc],
{ congr' 1,
apply comm;
simp },
{ intros x hx y hy,
simp only [quot_mk_to_coe, list.mem_cons_iff, mem_coe, cons_coe] at hx hy,
apply comm,
{ cases hx;
simp [hx] },
{ cases hy;
simp [hy] } } }
end
@[protected, to_additive]
lemma nocomm_prod_map_aux (s : multiset α)
(comm : ∀ (x ∈ s) (y ∈ s), commute x y)
{F : Type*} [monoid_hom_class F α β] (f : F) :
∀ (x ∈ s.map f) (y ∈ s.map f), commute x y :=
begin
simp only [multiset.mem_map],
rintros _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩,
exact (comm _ hx _ hy).map f,
end
@[to_additive]
lemma noncomm_prod_map (s : multiset α) (comm : ∀ (x ∈ s) (y ∈ s), commute x y)
{F : Type*} [monoid_hom_class F α β] (f : F) :
f (s.noncomm_prod comm) = (s.map f).noncomm_prod (nocomm_prod_map_aux s comm f) :=
begin
induction s using quotient.induction_on,
simpa using map_list_prod f _,
end
@[to_additive noncomm_sum_eq_card_nsmul]
lemma noncomm_prod_eq_pow_card (s : multiset α) (comm : ∀ (x ∈ s) (y ∈ s), commute x y)
(m : α) (h : ∀ (x ∈ s), x = m) : s.noncomm_prod comm = m ^ s.card :=
begin
induction s using quotient.induction_on,
simp only [quot_mk_to_coe, noncomm_prod_coe, coe_card, mem_coe] at *,
exact list.prod_eq_pow_card _ m h,
end
@[to_additive] lemma noncomm_prod_eq_prod {α : Type*} [comm_monoid α] (s : multiset α) :
noncomm_prod s (λ _ _ _ _, commute.all _ _) = prod s :=
begin
induction s using quotient.induction_on,
simp
end
@[to_additive noncomm_sum_add_commute]
lemma noncomm_prod_commute (s : multiset α)
(comm : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → commute x y)
(y : α) (h : ∀ (x : α), x ∈ s → commute y x) : commute y (s.noncomm_prod comm) :=
begin
induction s using quotient.induction_on,
simp only [quot_mk_to_coe, noncomm_prod_coe],
exact commute.list_prod_right _ _ h,
end
end multiset
namespace finset
variables [monoid β] [monoid γ]
/-- Product of a `s : finset α` mapped with `f : α → β` with `[monoid β]`,
given a proof that `*` commutes on all elements `f x` for `x ∈ s`. -/
@[to_additive "Sum of a `s : finset α` mapped with `f : α → β` with `[add_monoid β]`,
given a proof that `+` commutes on all elements `f x` for `x ∈ s`."]
def noncomm_prod (s : finset α) (f : α → β) (comm : ∀ (x ∈ s) (y ∈ s), commute (f x) (f y)) : β :=
(s.1.map f).noncomm_prod (by simpa [multiset.mem_map, ←finset.mem_def] using comm)
@[congr, to_additive]
lemma noncomm_prod_congr
{s₁ s₂ : finset α} {f g : α → β} (h₁ : s₁ = s₂) (h₂ : ∀ (x ∈ s₂), f x = g x)
(comm : ∀ (x ∈ s₁) (y ∈ s₁), commute (f x) (f y)) :
noncomm_prod s₁ f comm = noncomm_prod s₂ g
(λ x hx y hy, h₂ x hx ▸ h₂ y hy ▸ comm x (h₁.symm ▸ hx) y (h₁.symm ▸ hy)) :=
by simp_rw [noncomm_prod, multiset.map_congr (congr_arg _ h₁) h₂]
@[simp, to_additive] lemma noncomm_prod_to_finset [decidable_eq α] (l : list α) (f : α → β)
(comm : ∀ (x ∈ l.to_finset) (y ∈ l.to_finset), commute (f x) (f y))
(hl : l.nodup) :
noncomm_prod l.to_finset f comm = (l.map f).prod :=
begin
rw ←list.dedup_eq_self at hl,
simp [noncomm_prod, hl]
end
@[simp, to_additive] lemma noncomm_prod_empty (f : α → β)
(h : ∀ (x ∈ (∅ : finset α)) (y ∈ (∅ : finset α)), commute (f x) (f y)) :
noncomm_prod (∅ : finset α) f h = 1 := rfl
@[simp, to_additive] lemma noncomm_prod_insert_of_not_mem [decidable_eq α] (s : finset α) (a : α)
(f : α → β)
(comm : ∀ (x ∈ insert a s) (y ∈ insert a s), commute (f x) (f y))
(ha : a ∉ s) :
noncomm_prod (insert a s) f comm = f a * noncomm_prod s f
(λ x hx y hy, comm _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy)) :=
by simp [insert_val_of_not_mem ha, noncomm_prod]
@[to_additive] lemma noncomm_prod_insert_of_not_mem' [decidable_eq α] (s : finset α) (a : α)
(f : α → β)
(comm : ∀ (x ∈ insert a s) (y ∈ insert a s), commute (f x) (f y))
(ha : a ∉ s) :
noncomm_prod (insert a s) f comm = noncomm_prod s f
(λ x hx y hy, comm _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy)) * f a :=
by simp [noncomm_prod, insert_val_of_not_mem ha, multiset.noncomm_prod_cons']
@[simp, to_additive] lemma noncomm_prod_singleton (a : α) (f : α → β) :
noncomm_prod ({a} : finset α) f
(λ x hx y hy, by rw [mem_singleton.mp hx, mem_singleton.mp hy]) = f a :=
by simp [noncomm_prod, multiset.singleton_eq_cons]
@[to_additive]
lemma noncomm_prod_map (s : finset α) (f : α → β)
(comm : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → commute (f x) (f y))
{F : Type*} [monoid_hom_class F β γ] (g : F) :
g (s.noncomm_prod f comm) = s.noncomm_prod (λ i, g (f i))
(λ x hx y hy, (comm x hx y hy).map g) :=
by simp [noncomm_prod, multiset.noncomm_prod_map]
@[to_additive noncomm_sum_eq_card_nsmul]
lemma noncomm_prod_eq_pow_card (s : finset α) (f : α → β)
(comm : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → commute (f x) (f y))
(m : β) (h : ∀ (x : α), x ∈ s → f x = m) : s.noncomm_prod f comm = m ^ s.card :=
begin
rw [noncomm_prod, multiset.noncomm_prod_eq_pow_card _ _ m],
simp only [finset.card_def, multiset.card_map],
simpa using h,
end
@[to_additive noncomm_sum_add_commute]
lemma noncomm_prod_commute (s : finset α) (f : α → β)
(comm : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → commute (f x) (f y))
(y : β) (h : ∀ (x : α), x ∈ s → commute y (f x)) : commute y (s.noncomm_prod f comm) :=
begin
apply multiset.noncomm_prod_commute,
intro y,
rw multiset.mem_map,
rintros ⟨x, ⟨hx, rfl⟩⟩,
exact h x hx,
end
@[to_additive] lemma noncomm_prod_eq_prod {β : Type*} [comm_monoid β] (s : finset α) (f : α → β) :
noncomm_prod s f (λ _ _ _ _, commute.all _ _) = s.prod f :=
begin
classical,
induction s using finset.induction_on with a s ha IH,
{ simp },
{ simp [ha, IH] }
end
/- The non-commutative version of `finset.prod_union` -/
@[to_additive "The non-commutative version of `finset.sum_union`"]
lemma noncomm_prod_union_of_disjoint [decidable_eq α] {s t : finset α}
(h : disjoint s t) (f : α → β)
(comm : ∀ (x ∈ s ∪ t) (y ∈ s ∪ t), commute (f x) (f y))
(scomm : ∀ (x ∈ s) (y ∈ s), commute (f x) (f y) :=
λ _ hx _ hy, comm _ (mem_union_left _ hx) _ (mem_union_left _ hy))
(tcomm : ∀ (x ∈ t) (y ∈ t), commute (f x) (f y) :=
λ _ hx _ hy, comm _ (mem_union_right _ hx) _ (mem_union_right _ hy)) :
noncomm_prod (s ∪ t) f comm = noncomm_prod s f scomm * noncomm_prod t f tcomm :=
begin
obtain ⟨sl, sl', rfl⟩ := exists_list_nodup_eq s,
obtain ⟨tl, tl', rfl⟩ := exists_list_nodup_eq t,
rw list.disjoint_to_finset_iff_disjoint at h,
simp [sl', tl', noncomm_prod_to_finset, ←list.prod_append, ←list.to_finset_append,
sl'.append tl' h]
end
@[protected, to_additive]
lemma noncomm_prod_mul_distrib_aux {s : finset α} {f : α → β} {g : α → β}
(comm_ff : ∀ (x ∈ s) (y ∈ s), commute (f x) (f y))
(comm_gg : ∀ (x ∈ s) (y ∈ s), commute (g x) (g y))
(comm_gf : ∀ (x ∈ s) (y ∈ s), x ≠ y → commute (g x) (f y)) :
(∀ (x ∈ s) (y ∈ s), commute ((f * g) x) ((f * g) y)) :=
begin
intros x hx y hy,
by_cases h : x = y, { subst h },
apply commute.mul_left; apply commute.mul_right,
{ exact comm_ff x hx y hy },
{ exact (comm_gf y hy x hx (ne.symm h)).symm },
{ exact comm_gf x hx y hy h },
{ exact comm_gg x hx y hy },
end
/-- The non-commutative version of `finset.prod_mul_distrib` -/
@[to_additive "The non-commutative version of `finset.sum_add_distrib`"]
lemma noncomm_prod_mul_distrib {s : finset α} (f : α → β) (g : α → β)
(comm_ff : ∀ (x ∈ s) (y ∈ s), commute (f x) (f y))
(comm_gg : ∀ (x ∈ s) (y ∈ s), commute (g x) (g y))
(comm_gf : ∀ (x ∈ s) (y ∈ s), x ≠ y → commute (g x) (f y)) :
noncomm_prod s (f * g) (noncomm_prod_mul_distrib_aux comm_ff comm_gg comm_gf)
= noncomm_prod s f comm_ff * noncomm_prod s g comm_gg :=
begin
classical,
induction s using finset.induction_on with x s hnmem ih,
{ simp, },
{ simp only [finset.noncomm_prod_insert_of_not_mem _ _ _ _ hnmem],
specialize ih
(λ x hx y hy, comm_ff x (mem_insert_of_mem hx) y (mem_insert_of_mem hy))
(λ x hx y hy, comm_gg x (mem_insert_of_mem hx) y (mem_insert_of_mem hy))
(λ x hx y hy hne, comm_gf x (mem_insert_of_mem hx) y (mem_insert_of_mem hy) hne),
rw [ih, pi.mul_apply],
simp only [mul_assoc],
congr' 1,
simp only [← mul_assoc],
congr' 1,
apply noncomm_prod_commute,
intros y hy,
have : x ≠ y, by {rintro rfl, contradiction},
exact comm_gf x (mem_insert_self x s) y (mem_insert_of_mem hy) this, }
end
section finite_pi
variables {ι : Type*} [fintype ι] [decidable_eq ι] {M : ι → Type*} [∀ i, monoid (M i)]
variables (x : Π i, M i)
@[to_additive]
lemma noncomm_prod_mul_single :
univ.noncomm_prod (λ i, pi.mul_single i (x i)) (λ i _ j _, pi.mul_single_apply_commute x i j)
= x :=
begin
ext i,
apply (univ.noncomm_prod_map (λ i, monoid_hom.single M i (x i)) _
(pi.eval_monoid_hom M i)).trans,
rw [ ← insert_erase (mem_univ i),
noncomm_prod_insert_of_not_mem' _ _ _ _ (not_mem_erase _ _),
noncomm_prod_eq_pow_card,
one_pow],
{ simp, },
{ intros i h, simp at h, simp [h], },
end
@[to_additive]
lemma _root_.monoid_hom.pi_ext {f g : (Π i, M i) →* γ}
(h : ∀ i x, f (pi.mul_single i x) = g (pi.mul_single i x)) :
f = g :=
begin
ext x,
rw [← noncomm_prod_mul_single x, univ.noncomm_prod_map, univ.noncomm_prod_map],
congr' 1 with i, exact h i (x i),
end
end finite_pi
end finset
|
d5904249e3dd74c3cdb5fc6ef396ae359593a7e8 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/topology/sober.lean | dbb1bd5e2132247b39652c21eb29b4d3b155e9e9 | [
"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 | 12,502 | lean | /-
Copyright (c) 2021 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import topology.separation
import topology.continuous_function.basic
/-!
# Sober spaces
A quasi-sober space is a topological space where every
irreducible closed subset has a generic point.
A sober space is a quasi-sober space where every irreducible closed subset
has a *unique* generic point. This is if and only if the space is T0, and thus sober spaces can be
stated via `[quasi_sober α] [t0_space α]`.
## Main definition
* `specializes` : `specializes x y` (`x ⤳ y`) means that `x` specializes to `y`, i.e.
`y` is in the closure of `x`.
* `specialization_preorder` : specialization gives a preorder on a topological space.
* `specialization_order` : specialization gives a partial order on a T0 space.
* `is_generic_point` : `x` is the generic point of `S` if `S` is the closure of `x`.
* `quasi_sober` : A space is quasi-sober if every irreducible closed subset has a generic point.
-/
variables {α β : Type*} [topological_space α] [topological_space β]
section specialize_order
/-- `x` specializes to `y` if `y` is in the closure of `x`. The notation used is `x ⤳ y`. -/
def specializes (x y : α) : Prop := y ∈ closure ({x} : set α)
infix ` ⤳ `:300 := specializes
lemma specializes_def (x y : α) : x ⤳ y ↔ y ∈ closure ({x} : set α) := iff.rfl
lemma specializes_iff_closure_subset {x y : α} :
x ⤳ y ↔ closure ({y} : set α) ⊆ closure ({x} : set α) :=
is_closed_closure.mem_iff_closure_subset
lemma specializes_rfl {x : α} : x ⤳ x := subset_closure (set.mem_singleton x)
lemma specializes_refl (x : α) : x ⤳ x := specializes_rfl
lemma specializes.trans {x y z : α} : x ⤳ y → y ⤳ z → x ⤳ z :=
by { simp_rw specializes_iff_closure_subset, exact λ a b, b.trans a }
lemma specializes_iff_forall_closed {x y : α} :
x ⤳ y ↔ ∀ (Z : set α) (h : is_closed Z), x ∈ Z → y ∈ Z :=
begin
split,
{ intros h Z hZ,
rw [hZ.mem_iff_closure_subset, hZ.mem_iff_closure_subset],
exact (specializes_iff_closure_subset.mp h).trans },
{ intro h, exact h _ is_closed_closure (subset_closure $ set.mem_singleton x) }
end
lemma specializes_iff_forall_open {x y : α} :
x ⤳ y ↔ ∀ (U : set α) (h : is_open U), y ∈ U → x ∈ U :=
begin
rw specializes_iff_forall_closed,
exact ⟨λ h U hU, not_imp_not.mp (h _ (is_closed_compl_iff.mpr hU)),
λ h U hU, not_imp_not.mp (h _ (is_open_compl_iff.mpr hU))⟩,
end
lemma indistinguishable_iff_specializes_and (x y : α) :
indistinguishable x y ↔ x ⤳ y ∧ y ⤳ x :=
(indistinguishable_iff_closure x y).trans (and_comm _ _)
lemma specializes_antisymm [t0_space α] (x y : α) : x ⤳ y → y ⤳ x → x = y :=
λ h₁ h₂, ((indistinguishable_iff_specializes_and _ _).mpr ⟨h₁, h₂⟩).eq
lemma specializes.map {x y : α} (h : x ⤳ y) {f : α → β} (hf : continuous f) : f x ⤳ f y :=
begin
rw [specializes_def, ← set.image_singleton],
exact image_closure_subset_closure_image hf ⟨_, h, rfl⟩,
end
lemma continuous_map.map_specialization {x y : α} (h : x ⤳ y) (f : C(α, β)) : f x ⤳ f y :=
h.map f.2
lemma specializes.eq [t1_space α] {x y : α} (h : x ⤳ y) : x = y :=
(set.mem_singleton_iff.mp
((specializes_iff_forall_closed.mp h) _ (t1_space.t1 _) (set.mem_singleton _))).symm
@[simp] lemma specializes_iff_eq [t1_space α] {x y : α} : x ⤳ y ↔ x = y :=
⟨specializes.eq, λ h, h ▸ specializes_refl _⟩
variable (α)
/-- Specialization forms a preorder on the topological space. -/
def specialization_preorder : preorder α :=
{ le := λ x y, y ⤳ x,
le_refl := λ x, specializes_refl x,
le_trans := λ _ _ _ h₁ h₂, specializes.trans h₂ h₁ }
local attribute [instance] specialization_preorder
/-- Specialization forms a partial order on a t0 topological space. -/
def specialization_order [t0_space α] : partial_order α :=
{ le_antisymm := λ _ _ h₁ h₂, specializes_antisymm _ _ h₂ h₁,
.. specialization_preorder α }
variable {α}
lemma specialization_order.monotone_of_continuous (f : α → β) (hf : continuous f) : monotone f :=
λ x y h, specializes.map h hf
end specialize_order
section generic_point
/-- `x` is a generic point of `S` if `S` is the closure of `x`. -/
def is_generic_point (x : α) (S : set α) : Prop := closure ({x} : set α) = S
lemma is_generic_point_def {x : α} {S : set α} : is_generic_point x S ↔ closure ({x} : set α) = S :=
iff.rfl
lemma is_generic_point.def {x : α} {S : set α} (h : is_generic_point x S) :
closure ({x} : set α) = S := h
lemma is_generic_point_closure {x : α} : is_generic_point x (closure ({x} : set α)) := refl _
variables {x : α} {S : set α} (h : is_generic_point x S)
include h
lemma is_generic_point.specializes {y : α} (h' : y ∈ S) :
x ⤳ y := by rwa ← h.def at h'
lemma is_generic_point.mem : x ∈ S :=
h.def ▸ subset_closure (set.mem_singleton x)
lemma is_generic_point.is_closed : is_closed S :=
h.def ▸ is_closed_closure
lemma is_generic_point.is_irreducible : is_irreducible S :=
h.def ▸ is_irreducible_singleton.closure
lemma is_generic_point.eq [t0_space α] {y : α} (h' : is_generic_point y S) : x = y :=
specializes_antisymm _ _ (h.specializes h'.mem) (h'.specializes h.mem)
lemma is_generic_point.mem_open_set_iff
{U : set α} (hU : is_open U) : x ∈ U ↔ (S ∩ U).nonempty :=
⟨λ h', ⟨x, h.mem, h'⟩,
λ h', specializes_iff_forall_open.mp (h.specializes h'.some_spec.1) U hU h'.some_spec.2⟩
lemma is_generic_point.disjoint_iff
{U : set α} (hU : is_open U) : disjoint S U ↔ x ∉ U :=
by rw [h.mem_open_set_iff hU, ← set.ne_empty_iff_nonempty, not_not, set.disjoint_iff_inter_eq_empty]
lemma is_generic_point.mem_closed_set_iff
{Z : set α} (hZ : is_closed Z) : x ∈ Z ↔ S ⊆ Z :=
by rw [← is_generic_point_def.mp h, hZ.closure_subset_iff, set.singleton_subset_iff]
lemma is_generic_point.image {f : α → β} (hf : continuous f) :
is_generic_point (f x) (closure (f '' S)) :=
begin
rw [is_generic_point_def, ← is_generic_point_def.mp h],
apply le_antisymm,
{ exact closure_mono
(set.singleton_subset_iff.mpr ⟨_, subset_closure $ set.mem_singleton x, rfl⟩) },
{ convert is_closed_closure.closure_subset_iff.mpr (image_closure_subset_closure_image hf),
rw set.image_singleton }
end
omit h
lemma is_generic_point_iff_forall_closed {x : α} {S : set α} (hS : is_closed S) (hxS : x ∈ S) :
is_generic_point x S ↔ ∀ (Z : set α) (hZ : is_closed Z) (hxZ : x ∈ Z), S ⊆ Z :=
begin
split,
{ intros h Z hZ hxZ, exact (h.mem_closed_set_iff hZ).mp hxZ },
{ intro h,
apply le_antisymm,
{ rwa [set.le_eq_subset, hS.closure_subset_iff, set.singleton_subset_iff] },
{ exact h _ is_closed_closure (subset_closure $ set.mem_singleton x) } }
end
end generic_point
section sober
/-- A space is sober if every irreducible closed subset has a generic point. -/
@[mk_iff]
class quasi_sober (α : Type*) [topological_space α] : Prop :=
(sober : ∀ {S : set α} (hS₁ : is_irreducible S) (hS₂ : is_closed S), ∃ x, is_generic_point x S)
/-- A generic point of the closure of an irreducible space. -/
noncomputable
def is_irreducible.generic_point [quasi_sober α] {S : set α} (hS : is_irreducible S) : α :=
(quasi_sober.sober hS.closure is_closed_closure).some
lemma is_irreducible.generic_point_spec [quasi_sober α] {S : set α} (hS : is_irreducible S) :
is_generic_point hS.generic_point (closure S) :=
(quasi_sober.sober hS.closure is_closed_closure).some_spec
@[simp] lemma is_irreducible.generic_point_closure_eq [quasi_sober α] {S : set α}
(hS : is_irreducible S) : closure ({hS.generic_point} : set α) = closure S :=
hS.generic_point_spec
variable (α)
/-- A generic point of a sober irreducible space. -/
noncomputable
def generic_point [quasi_sober α] [irreducible_space α] : α :=
(irreducible_space.is_irreducible_univ α).generic_point
lemma generic_point_spec [quasi_sober α] [irreducible_space α] :
is_generic_point (generic_point α) ⊤ :=
by simpa using (irreducible_space.is_irreducible_univ α).generic_point_spec
@[simp]
lemma generic_point_closure [quasi_sober α] [irreducible_space α] :
closure ({generic_point α} : set α) = ⊤ := generic_point_spec α
variable {α}
lemma generic_point_specializes [quasi_sober α] [irreducible_space α] (x : α) :
generic_point α ⤳ x := (is_irreducible.generic_point_spec _).specializes (by simp)
local attribute [instance, priority 10] specialization_order
/-- The closed irreducible subsets of a sober space bijects with the points of the space. -/
noncomputable
def irreducible_set_equiv_points [quasi_sober α] [t0_space α] :
{ s : set α | is_irreducible s ∧ is_closed s } ≃o α :=
{ to_fun := λ s, s.prop.1.generic_point,
inv_fun := λ x, ⟨closure ({x} : set α), is_irreducible_singleton.closure, is_closed_closure⟩,
left_inv := λ s,
subtype.eq $ eq.trans (s.prop.1.generic_point_spec) $ closure_eq_iff_is_closed.mpr s.2.2,
right_inv := λ x, is_irreducible_singleton.closure.generic_point_spec.eq
(by { convert is_generic_point_closure using 1, rw closure_closure }),
map_rel_iff' := λ s t, by { change _ ⤳ _ ↔ _, rw specializes_iff_closure_subset,
simp [s.prop.2.closure_eq, t.prop.2.closure_eq, ← subtype.coe_le_coe] } }
lemma closed_embedding.quasi_sober {f : α → β} (hf : closed_embedding f) [quasi_sober β] :
quasi_sober α :=
begin
constructor,
intros S hS hS',
have hS'' := hS.image f hf.continuous.continuous_on,
obtain ⟨x, hx⟩ := quasi_sober.sober hS'' (hf.is_closed_map _ hS'),
obtain ⟨y, hy, rfl⟩ := hx.mem,
use y,
change _ = _ at hx,
apply set.image_injective.mpr hf.inj,
rw [← hx, ← hf.closure_image_eq, set.image_singleton]
end
lemma open_embedding.quasi_sober {f : α → β} (hf : open_embedding f) [quasi_sober β] :
quasi_sober α :=
begin
constructor,
intros S hS hS',
have hS'' := hS.image f hf.continuous.continuous_on,
obtain ⟨x, hx⟩ := quasi_sober.sober hS''.closure is_closed_closure,
obtain ⟨T, hT, rfl⟩ := hf.to_inducing.is_closed_iff.mp hS',
rw set.image_preimage_eq_inter_range at hx hS'',
have hxT : x ∈ T,
{ rw ← hT.closure_eq, exact closure_mono (set.inter_subset_left _ _) hx.mem },
have hxU : x ∈ set.range f,
{ rw hx.mem_open_set_iff hf.open_range,
refine set.nonempty.mono _ hS''.1,
simpa using subset_closure },
rcases hxU with ⟨y, rfl⟩,
use y,
change _ = _,
rw [hf.to_embedding.closure_eq_preimage_closure_image,
set.image_singleton, (show _ = _, from hx)],
apply set.image_injective.mpr hf.inj,
ext z,
simp only [set.image_preimage_eq_inter_range, set.mem_inter_eq, and.congr_left_iff],
exact λ hy, ⟨λ h, hT.closure_eq ▸ closure_mono (set.inter_subset_left _ _) h,
λ h, subset_closure ⟨h, hy⟩⟩
end
/-- A space is quasi sober if it can be covered by open quasi sober subsets. -/
lemma quasi_sober_of_open_cover (S : set (set α)) (hS : ∀ s : S, is_open (s : set α))
[hS' : ∀ s : S, quasi_sober s] (hS'' : ⋃₀ S = ⊤) : quasi_sober α :=
begin
rw quasi_sober_iff,
intros t h h',
obtain ⟨x, hx⟩ := h.1,
obtain ⟨U, hU, hU'⟩ : x ∈ ⋃₀S := by { rw hS'', trivial },
haveI : quasi_sober U := hS' ⟨U, hU⟩,
have H : is_preirreducible (coe ⁻¹' t : set U) :=
h.2.preimage (hS ⟨U, hU⟩).open_embedding_subtype_coe,
replace H : is_irreducible (coe ⁻¹' t : set U) := ⟨⟨⟨x, hU'⟩, by simpa using hx⟩, H⟩,
use H.generic_point,
have := continuous_subtype_coe.closure_preimage_subset _ H.generic_point_spec.mem,
rw h'.closure_eq at this,
apply le_antisymm,
{ apply h'.closure_subset_iff.mpr, simpa using this },
rw [← set.image_singleton, ← closure_closure],
have := closure_mono (image_closure_subset_closure_image (@continuous_subtype_coe α _ U)),
refine set.subset.trans _ this,
rw H.generic_point_spec.def,
refine (subset_closure_inter_of_is_preirreducible_of_is_open
h.2 (hS ⟨U, hU⟩) ⟨x, hx, hU'⟩).trans (closure_mono _),
rw ← subtype.image_preimage_coe,
exact set.image_subset _ subset_closure,
end
@[priority 100]
instance t2_space.quasi_sober [t2_space α] : quasi_sober α :=
begin
constructor,
rintro S h -,
obtain ⟨x, rfl⟩ := (is_irreducible_iff_singleton S).mp h,
exact ⟨x, (t1_space.t1 x).closure_eq⟩
end
end sober
|
133f1b7ef6ce2f2423f12274ed0099a7ad01ffcf | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/dual_number.lean | 31cacf6c8cbeb37c07c5e9a083b3bf1a4a257b73 | [
"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,462 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import algebra.triv_sq_zero_ext
/-!
# Dual numbers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
The dual numbers over `R` are of the form `a + bε`, where `a` and `b` are typically elements of a
commutative ring `R`, and `ε` is a symbol satisfying `ε^2 = 0`. They are a special case of
`triv_sq_zero_ext R M` with `M = R`.
## Notation
In the `dual_number` locale:
* `R[ε]` is a shorthand for `dual_number R`
* `ε` is a shorthand for `dual_number.eps`
## Main definitions
* `dual_number`
* `dual_number.eps`
* `dual_number.lift`
## Implementation notes
Rather than duplicating the API of `triv_sq_zero_ext`, this file reuses the functions there.
## References
* https://en.wikipedia.org/wiki/Dual_number
-/
variables {R : Type*}
/-- The type of dual numbers, numbers of the form $a + bε$ where $ε^2 = 0$.-/
abbreviation dual_number (R : Type*) : Type* := triv_sq_zero_ext R R
/-- The unit element $ε$ that squares to zero. -/
def dual_number.eps [has_zero R] [has_one R] : dual_number R := triv_sq_zero_ext.inr 1
localized "notation (name := dual_number.eps) `ε` := dual_number.eps" in dual_number
localized "postfix (name := dual_number) `[ε]`:1025 := dual_number" in dual_number
open_locale dual_number
namespace dual_number
open triv_sq_zero_ext
@[simp] lemma fst_eps [has_zero R] [has_one R] : fst ε = (0 : R) := fst_inr _ _
@[simp] lemma snd_eps [has_zero R] [has_one R] : snd ε = (1 : R) := snd_inr _ _
/-- A version of `triv_sq_zero_ext.snd_mul` with `*` instead of `•`. -/
@[simp] lemma snd_mul [semiring R] (x y : R[ε]) : snd (x * y) = fst x * snd y + snd x * fst y :=
snd_mul _ _
@[simp] lemma eps_mul_eps [semiring R] : (ε * ε : R[ε]) = 0 := inr_mul_inr _ _ _
@[simp] lemma inr_eq_smul_eps [mul_zero_one_class R] (r : R) : inr r = (r • ε : R[ε]) :=
ext (mul_zero r).symm (mul_one r).symm
/-- For two algebra morphisms out of `R[ε]` to agree, it suffices for them to agree on `ε`. -/
@[ext] lemma alg_hom_ext {A} [comm_semiring R] [semiring A] [algebra R A]
⦃f g : R[ε] →ₐ[R] A⦄ (h : f ε = g ε) : f = g :=
alg_hom_ext' $ linear_map.ext_ring $ h
variables {A : Type*} [comm_semiring R] [semiring A] [algebra R A]
/-- A universal property of the dual numbers, providing a unique `R[ε] →ₐ[R] A` for every element
of `A` which squares to `0`.
This isomorphism is named to match the very similar `complex.lift`. -/
@[simps {attrs := []}]
def lift : {e : A // e * e = 0} ≃ (R[ε] →ₐ[R] A) :=
equiv.trans
(show {e : A // e * e = 0} ≃ {f : R →ₗ[R] A // ∀ x y, f x * f y = 0}, from
(linear_map.ring_lmap_equiv_self R ℕ A).symm.to_equiv.subtype_equiv $ λ a, begin
dsimp,
simp_rw smul_mul_smul,
refine ⟨λ h x y, h.symm ▸ smul_zero _, λ h, by simpa using h 1 1⟩,
end)
triv_sq_zero_ext.lift
/- When applied to `ε`, `dual_number.lift` produces the element of `A` that squares to 0. -/
@[simp]
lemma lift_apply_eps (e : {e : A // e * e = 0}) : lift e (ε : R[ε]) = e :=
(triv_sq_zero_ext.lift_aux_apply_inr _ _ _).trans $ one_smul _ _
/- Lifting `dual_number.eps` itself gives the identity. -/
@[simp]
lemma lift_eps : lift ⟨ε, by exact eps_mul_eps⟩ = alg_hom.id R R[ε] :=
alg_hom_ext $ lift_apply_eps _
end dual_number
|
74ec62a38d4a35d69f4379a38588c68ff2a3c798 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/protected_test.lean | e3f46d456f924f6a01159d13496ad7d32151689e | [
"Apache-2.0"
] | permissive | soonhokong/lean | cb8aa01055ffe2af0fb99a16b4cda8463b882cd1 | 38607e3eb57f57f77c0ac114ad169e9e4262e24f | refs/heads/master | 1,611,187,284,081 | 1,450,766,737,000 | 1,476,122,547,000 | 11,513,992 | 2 | 0 | null | 1,401,763,102,000 | 1,374,182,235,000 | C++ | UTF-8 | Lean | false | false | 282 | lean | set_option pp.metavar_args true
namespace nat
check induction_on -- ERROR
check rec_on -- ERROR
check nat.induction_on
check le.rec_on -- OK
check nat.le.rec_on
namespace le
check rec_on -- ERROR
check le.rec_on
end le
end nat
|
00b9019b01a371bd00e4e5282e8ca34f4190f8d9 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/algebra/group_power/lemmas.lean | fd58b01c841b25c6afd6a729228ce29c81134527 | [
"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 | 26,118 | lean | /-
Copyright (c) 2015 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Robert Y. Lewis
-/
import algebra.group_power.basic
import algebra.opposites
import data.list.basic
import data.int.cast
import data.equiv.basic
import data.equiv.mul_add
import deprecated.group
/-!
# Lemmas about power operations on monoids and groups
This file contains lemmas about `monoid.pow`, `group.pow`, `nsmul`, `gsmul`
which require additional imports besides those available in `.basic`.
-/
universes u v w x y z u₁ u₂
variables {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z}
{R : Type u₁} {S : Type u₂}
/-!
### (Additive) monoid
-/
section monoid
variables [monoid M] [monoid N] [add_monoid A] [add_monoid B]
@[simp] theorem nsmul_one [has_one A] : ∀ n : ℕ, n •ℕ (1 : A) = n :=
add_monoid_hom.eq_nat_cast
⟨λ n, n •ℕ (1 : A), zero_nsmul _, λ _ _, add_nsmul _ _ _⟩
(one_nsmul _)
@[simp, priority 500]
theorem list.prod_repeat (a : M) (n : ℕ) : (list.repeat a n).prod = a ^ n :=
begin
induction n with n ih,
{ refl },
{ rw [list.repeat_succ, list.prod_cons, ih], refl, }
end
@[simp, priority 500]
theorem list.sum_repeat : ∀ (a : A) (n : ℕ), (list.repeat a n).sum = n •ℕ a :=
@list.prod_repeat (multiplicative A) _
@[simp, norm_cast] lemma units.coe_pow (u : units M) (n : ℕ) : ((u ^ n : units M) : M) = u ^ n :=
(units.coe_hom M).map_pow u n
lemma is_unit_of_pow_eq_one (x : M) (n : ℕ) (hx : x ^ n = 1) (hn : 0 < n) :
is_unit x :=
begin
cases n, { exact (nat.not_lt_zero _ hn).elim },
refine ⟨⟨x, x ^ n, _, _⟩, rfl⟩,
{ rwa [pow_succ] at hx },
{ rwa [pow_succ'] at hx }
end
end monoid
theorem nat.nsmul_eq_mul (m n : ℕ) : m •ℕ n = m * n :=
by induction m with m ih; [rw [zero_nsmul, zero_mul],
rw [succ_nsmul', ih, nat.succ_mul]]
section group
variables [group G] [group H] [add_group A] [add_group B]
open int
local attribute [ematch] le_of_lt
open nat
theorem gsmul_one [has_one A] (n : ℤ) : n •ℤ (1 : A) = n :=
by cases n; simp
lemma gpow_add_one (a : G) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a
| (of_nat n) := by simp [← int.coe_nat_succ, pow_succ']
| -[1+0] := by simp [int.neg_succ_of_nat_eq]
| -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, gpow_neg, neg_add, neg_add_cancel_right, gpow_neg,
← int.coe_nat_succ, gpow_coe_nat, gpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev,
inv_mul_cancel_right]
theorem add_one_gsmul : ∀ (a : A) (i : ℤ), (i + 1) •ℤ a = i •ℤ a + a :=
@gpow_add_one (multiplicative A) _
lemma gpow_sub_one (a : G) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ :=
calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : (mul_inv_cancel_right _ _).symm
... = a^n * a⁻¹ : by rw [← gpow_add_one, sub_add_cancel]
lemma gpow_add (a : G) (m n : ℤ) : a ^ (m + n) = a ^ m * a ^ n :=
begin
induction n using int.induction_on with n ihn n ihn,
case hz : { simp },
{ simp only [← add_assoc, gpow_add_one, ihn, mul_assoc] },
{ rw [gpow_sub_one, ← mul_assoc, ← ihn, ← gpow_sub_one, add_sub_assoc] }
end
lemma mul_self_gpow (b : G) (m : ℤ) : b*b^m = b^(m+1) :=
by { conv_lhs {congr, rw ← gpow_one b }, rw [← gpow_add, add_comm] }
lemma mul_gpow_self (b : G) (m : ℤ) : b^m*b = b^(m+1) :=
by { conv_lhs {congr, skip, rw ← gpow_one b }, rw [← gpow_add, add_comm] }
theorem add_gsmul : ∀ (a : A) (i j : ℤ), (i + j) •ℤ a = i •ℤ a + j •ℤ a :=
@gpow_add (multiplicative A) _
lemma gpow_sub (a : G) (m n : ℤ) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ :=
by rw [sub_eq_add_neg, gpow_add, gpow_neg]
lemma sub_gsmul (m n : ℤ) (a : A) : (m - n) •ℤ a = m •ℤ a - n •ℤ a :=
@gpow_sub (multiplicative A) _ _ _ _
theorem gpow_one_add (a : G) (i : ℤ) : a ^ (1 + i) = a * a ^ i :=
by rw [gpow_add, gpow_one]
theorem one_add_gsmul : ∀ (a : A) (i : ℤ), (1 + i) •ℤ a = a + i •ℤ a :=
@gpow_one_add (multiplicative A) _
theorem gpow_mul_comm (a : G) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i :=
by rw [← gpow_add, ← gpow_add, add_comm]
theorem gsmul_add_comm : ∀ (a : A) (i j), i •ℤ a + j •ℤ a = j •ℤ a + i •ℤ a :=
@gpow_mul_comm (multiplicative A) _
theorem gpow_mul (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ m) ^ n :=
int.induction_on n (by simp) (λ n ihn, by simp [mul_add, gpow_add, ihn])
(λ n ihn, by simp only [mul_sub, gpow_sub, ihn, mul_one, gpow_one])
theorem gsmul_mul' : ∀ (a : A) (m n : ℤ), m * n •ℤ a = n •ℤ (m •ℤ a) :=
@gpow_mul (multiplicative A) _
theorem gpow_mul' (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m :=
by rw [mul_comm, gpow_mul]
theorem gsmul_mul (a : A) (m n : ℤ) : m * n •ℤ a = m •ℤ (n •ℤ a) :=
by rw [mul_comm, gsmul_mul']
theorem gpow_bit0 (a : G) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _
theorem bit0_gsmul (a : A) (n : ℤ) : bit0 n •ℤ a = n •ℤ a + n •ℤ a := gpow_add _ _ _
theorem gpow_bit1 (a : G) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a :=
by rw [bit1, gpow_add, gpow_bit0, gpow_one]
theorem bit1_gsmul : ∀ (a : A) (n : ℤ), bit1 n •ℤ a = n •ℤ a + n •ℤ a + a :=
@gpow_bit1 (multiplicative A) _
theorem monoid_hom.map_gpow (f : G →* H) (a : G) (n : ℤ) : f (a ^ n) = f a ^ n :=
by cases n; [exact f.map_pow _ _, exact (f.map_inv _).trans (congr_arg _ $ f.map_pow _ _)]
theorem add_monoid_hom.map_gsmul (f : A →+ B) (a : A) (n : ℤ) : f (n •ℤ a) = n •ℤ f a :=
f.to_multiplicative.map_gpow a n
@[simp, norm_cast] lemma units.coe_gpow (u : units G) (n : ℤ) : ((u ^ n : units G) : G) = u ^ n :=
(units.coe_hom G).map_gpow u n
end group
section ordered_add_comm_group
variables [ordered_add_comm_group A]
/-! Lemmas about `gsmul` under ordering, placed here (rather than in `algebra.group_power.basic`
with their friends) because they require facts from `data.int.basic`-/
open int
lemma gsmul_pos {a : A} (ha : 0 < a) {k : ℤ} (hk : (0:ℤ) < k) : 0 < k •ℤ a :=
begin
lift k to ℕ using int.le_of_lt hk,
apply nsmul_pos ha,
exact coe_nat_pos.mp hk,
end
theorem gsmul_le_gsmul {a : A} {n m : ℤ} (ha : 0 ≤ a) (h : n ≤ m) : n •ℤ a ≤ m •ℤ a :=
calc n •ℤ a = n •ℤ a + 0 : (add_zero _).symm
... ≤ n •ℤ a + (m - n) •ℤ a : add_le_add_left (gsmul_nonneg ha (sub_nonneg.mpr h)) _
... = m •ℤ a : by { rw [← add_gsmul], simp }
theorem gsmul_lt_gsmul {a : A} {n m : ℤ} (ha : 0 < a) (h : n < m) : n •ℤ a < m •ℤ a :=
calc n •ℤ a = n •ℤ a + 0 : (add_zero _).symm
... < n •ℤ a + (m - n) •ℤ a : add_lt_add_left (gsmul_pos ha (sub_pos.mpr h)) _
... = m •ℤ a : by { rw [← add_gsmul], simp }
end ordered_add_comm_group
section linear_ordered_add_comm_group
variable [linear_ordered_add_comm_group A]
theorem gsmul_le_gsmul_iff {a : A} {n m : ℤ} (ha : 0 < a) : n •ℤ a ≤ m •ℤ a ↔ n ≤ m :=
begin
refine ⟨λ h, _, gsmul_le_gsmul $ le_of_lt ha⟩,
by_contra H,
exact lt_irrefl _ (lt_of_lt_of_le (gsmul_lt_gsmul ha (not_le.mp H)) h)
end
theorem gsmul_lt_gsmul_iff {a : A} {n m : ℤ} (ha : 0 < a) : n •ℤ a < m •ℤ a ↔ n < m :=
begin
refine ⟨λ h, _, gsmul_lt_gsmul ha⟩,
by_contra H,
exact lt_irrefl _ (lt_of_le_of_lt (gsmul_le_gsmul (le_of_lt ha) $ not_lt.mp H) h)
end
theorem nsmul_le_nsmul_iff {a : A} {n m : ℕ} (ha : 0 < a) : n •ℕ a ≤ m •ℕ a ↔ n ≤ m :=
begin
refine ⟨λ h, _, nsmul_le_nsmul $ le_of_lt ha⟩,
by_contra H,
exact lt_irrefl _ (lt_of_lt_of_le (nsmul_lt_nsmul ha (not_le.mp H)) h)
end
theorem nsmul_lt_nsmul_iff {a : A} {n m : ℕ} (ha : 0 < a) : n •ℕ a < m •ℕ a ↔ n < m :=
begin
refine ⟨λ h, _, nsmul_lt_nsmul ha⟩,
by_contra H,
exact lt_irrefl _ (lt_of_le_of_lt (nsmul_le_nsmul (le_of_lt ha) $ not_lt.mp H) h)
end
end linear_ordered_add_comm_group
@[simp] lemma with_bot.coe_nsmul [add_monoid A] (a : A) (n : ℕ) :
((nsmul n a : A) : with_bot A) = nsmul n a :=
add_monoid_hom.map_nsmul ⟨(coe : A → with_bot A), with_bot.coe_zero, with_bot.coe_add⟩ a n
theorem nsmul_eq_mul' [semiring R] (a : R) (n : ℕ) : n •ℕ a = a * n :=
by induction n with n ih; [rw [zero_nsmul, nat.cast_zero, mul_zero],
rw [succ_nsmul', ih, nat.cast_succ, mul_add, mul_one]]
@[simp] theorem nsmul_eq_mul [semiring R] (n : ℕ) (a : R) : n •ℕ a = n * a :=
by rw [nsmul_eq_mul', (n.cast_commute a).eq]
theorem mul_nsmul_left [semiring R] (a b : R) (n : ℕ) : n •ℕ (a * b) = a * (n •ℕ b) :=
by rw [nsmul_eq_mul', nsmul_eq_mul', mul_assoc]
theorem mul_nsmul_assoc [semiring R] (a b : R) (n : ℕ) : n •ℕ (a * b) = n •ℕ a * b :=
by rw [nsmul_eq_mul, nsmul_eq_mul, mul_assoc]
@[simp, norm_cast] theorem nat.cast_pow [semiring R] (n m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m :=
by induction m with m ih; [exact nat.cast_one, rw [pow_succ', pow_succ', nat.cast_mul, ih]]
@[simp, norm_cast] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m :=
by induction m with m ih; [exact int.coe_nat_one, rw [pow_succ', pow_succ', int.coe_nat_mul, ih]]
theorem int.nat_abs_pow (n : ℤ) (k : ℕ) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k :=
by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, pow_succ', ih]]
-- The next four lemmas allow us to replace multiplication by a numeral with a `gsmul` expression.
-- They are used by the `noncomm_ring` tactic, to normalise expressions before passing to `abel`.
lemma bit0_mul [ring R] {n r : R} : bit0 n * r = gsmul 2 (n * r) :=
by { dsimp [bit0], rw [add_mul, add_gsmul, one_gsmul], }
lemma mul_bit0 [ring R] {n r : R} : r * bit0 n = gsmul 2 (r * n) :=
by { dsimp [bit0], rw [mul_add, add_gsmul, one_gsmul], }
lemma bit1_mul [ring R] {n r : R} : bit1 n * r = gsmul 2 (n * r) + r :=
by { dsimp [bit1], rw [add_mul, bit0_mul, one_mul], }
lemma mul_bit1 [ring R] {n r : R} : r * bit1 n = gsmul 2 (r * n) + r :=
by { dsimp [bit1], rw [mul_add, mul_bit0, mul_one], }
@[simp] theorem gsmul_eq_mul [ring R] (a : R) : ∀ n, n •ℤ a = n * a
| (n : ℕ) := nsmul_eq_mul _ _
| -[1+ n] := show -(_ •ℕ _)=-_*_, by rw [neg_mul_eq_neg_mul_symm, nsmul_eq_mul, nat.cast_succ]
theorem gsmul_eq_mul' [ring R] (a : R) (n : ℤ) : n •ℤ a = a * n :=
by rw [gsmul_eq_mul, (n.cast_commute a).eq]
theorem mul_gsmul_left [ring R] (a b : R) (n : ℤ) : n •ℤ (a * b) = a * (n •ℤ b) :=
by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc]
theorem mul_gsmul_assoc [ring R] (a b : R) (n : ℤ) : n •ℤ (a * b) = n •ℤ a * b :=
by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc]
@[simp]
lemma gsmul_int_int (a b : ℤ) : a •ℤ b = a * b := by simp [gsmul_eq_mul]
lemma gsmul_int_one (n : ℤ) : n •ℤ 1 = n := by simp
@[simp, norm_cast] theorem int.cast_pow [ring R] (n : ℤ) (m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m :=
by induction m with m ih; [exact int.cast_one,
rw [pow_succ, pow_succ, int.cast_mul, ih]]
lemma neg_one_pow_eq_pow_mod_two [ring R] {n : ℕ} : (-1 : R) ^ n = (-1) ^ (n % 2) :=
by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two]
section linear_ordered_semiring
variable [linear_ordered_semiring R]
/-- Bernoulli's inequality. This version works for semirings but requires
an additional hypothesis `0 ≤ a * a`. -/
theorem one_add_mul_le_pow' {a : R} (Hsqr : 0 ≤ a * a) (H : 0 ≤ 1 + a) :
∀ (n : ℕ), 1 + n •ℕ a ≤ (1 + a) ^ n
| 0 := le_of_eq $ add_zero _
| (n+1) :=
calc 1 + (n + 1) •ℕ a ≤ (1 + a) * (1 + n •ℕ a) :
by simpa [succ_nsmul, mul_add, add_mul, mul_nsmul_left, add_comm, add_left_comm]
using nsmul_nonneg Hsqr n
... ≤ (1 + a)^(n+1) : mul_le_mul_of_nonneg_left (one_add_mul_le_pow' n) H
private lemma pow_lt_pow_of_lt_one_aux {a : R} (h : 0 < a) (ha : a < 1) (i : ℕ) :
∀ k : ℕ, a ^ (i + k + 1) < a ^ i
| 0 :=
begin
simp only [add_zero],
rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one
end
| (k+1) :=
begin
rw ←one_mul (a^i),
apply mul_lt_mul ha _ _ zero_le_one,
{ apply le_of_lt, apply pow_lt_pow_of_lt_one_aux },
{ show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h }
end
private lemma pow_le_pow_of_le_one_aux {a : R} (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) :
∀ k : ℕ, a ^ (i + k) ≤ a ^ i
| 0 := by simp
| (k+1) := by rw [←add_assoc, ←one_mul (a^i)];
exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one
lemma pow_lt_pow_of_lt_one {a : R} (h : 0 < a) (ha : a < 1)
{i j : ℕ} (hij : i < j) : a ^ j < a ^ i :=
let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in
by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _
lemma pow_le_pow_of_le_one {a : R} (h : 0 ≤ a) (ha : a ≤ 1)
{i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i :=
let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in
by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _
lemma pow_le_one {x : R} : ∀ (n : ℕ) (h0 : 0 ≤ x) (h1 : x ≤ 1), x ^ n ≤ 1
| 0 h0 h1 := le_refl (1 : R)
| (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1)
end linear_ordered_semiring
/-- Bernoulli's inequality for `n : ℕ`, `-2 ≤ a`. -/
theorem one_add_mul_le_pow [linear_ordered_ring R] {a : R} (H : -2 ≤ a) :
∀ (n : ℕ), 1 + n •ℕ a ≤ (1 + a) ^ n
| 0 := le_of_eq $ add_zero _
| 1 := by simp
| (n+2) :=
have H' : 0 ≤ 2 + a,
from neg_le_iff_add_nonneg.1 H,
have 0 ≤ n •ℕ (a * a * (2 + a)) + a * a,
from add_nonneg (nsmul_nonneg (mul_nonneg (mul_self_nonneg a) H') n)
(mul_self_nonneg a),
calc 1 + (n + 2) •ℕ a ≤ 1 + (n + 2) •ℕ a + (n •ℕ (a * a * (2 + a)) + a * a) :
(le_add_iff_nonneg_right _).2 this
... = (1 + a) * (1 + a) * (1 + n •ℕ a) :
by { simp only [add_mul, mul_add, mul_two, mul_one, one_mul, succ_nsmul, nsmul_add,
mul_nsmul_assoc, (mul_nsmul_left _ _ _).symm],
ac_refl }
... ≤ (1 + a) * (1 + a) * (1 + a)^n :
mul_le_mul_of_nonneg_left (one_add_mul_le_pow n) (mul_self_nonneg (1 + a))
... = (1 + a)^(n + 2) : by simp only [pow_succ, mul_assoc]
/-- Bernoulli's inequality reformulated to estimate `a^n`. -/
theorem one_add_sub_mul_le_pow [linear_ordered_ring R]
{a : R} (H : -1 ≤ a) (n : ℕ) : 1 + n •ℕ (a - 1) ≤ a ^ n :=
have -2 ≤ a - 1, by { rw [bit0, neg_add], exact sub_le_sub_right H 1 },
by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow this n
namespace int
lemma units_pow_two (u : units ℤ) : u ^ 2 = 1 :=
(units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl)
lemma units_pow_eq_pow_mod_two (u : units ℤ) (n : ℕ) : u ^ n = u ^ (n % 2) :=
by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one]
@[simp] lemma nat_abs_pow_two (x : ℤ) : (x.nat_abs ^ 2 : ℤ) = x ^ 2 :=
by rw [pow_two, int.nat_abs_mul_self', pow_two]
lemma abs_le_self_pow_two (a : ℤ) : (int.nat_abs a : ℤ) ≤ a ^ 2 :=
by { rw [← int.nat_abs_pow_two a, pow_two], norm_cast, apply nat.le_mul_self }
lemma le_self_pow_two (b : ℤ) : b ≤ b ^ 2 := le_trans (le_nat_abs) (abs_le_self_pow_two _)
end int
variables (M G A)
/-- Monoid homomorphisms from `multiplicative ℕ` are defined by the image
of `multiplicative.of_add 1`. -/
def powers_hom [monoid M] : M ≃ (multiplicative ℕ →* M) :=
{ to_fun := λ x, ⟨λ n, x ^ n.to_add, pow_zero x, λ m n, pow_add x m n⟩,
inv_fun := λ f, f (multiplicative.of_add 1),
left_inv := pow_one,
right_inv := λ f, monoid_hom.ext $ λ n, by { simp [← f.map_pow, ← of_add_nsmul] } }
/-- Monoid homomorphisms from `multiplicative ℤ` are defined by the image
of `multiplicative.of_add 1`. -/
def gpowers_hom [group G] : G ≃ (multiplicative ℤ →* G) :=
{ to_fun := λ x, ⟨λ n, x ^ n.to_add, gpow_zero x, λ m n, gpow_add x m n⟩,
inv_fun := λ f, f (multiplicative.of_add 1),
left_inv := gpow_one,
right_inv := λ f, monoid_hom.ext $ λ n, by { simp [← f.map_gpow, ← of_add_gsmul ] } }
/-- Additive homomorphisms from `ℕ` are defined by the image of `1`. -/
def multiples_hom [add_monoid A] : A ≃ (ℕ →+ A) :=
{ to_fun := λ x, ⟨λ n, n •ℕ x, zero_nsmul x, λ m n, add_nsmul _ _ _⟩,
inv_fun := λ f, f 1,
left_inv := one_nsmul,
right_inv := λ f, add_monoid_hom.ext_nat $ one_nsmul (f 1) }
/-- Additive homomorphisms from `ℤ` are defined by the image of `1`. -/
def gmultiples_hom [add_group A] : A ≃ (ℤ →+ A) :=
{ to_fun := λ x, ⟨λ n, n •ℤ x, zero_gsmul x, λ m n, add_gsmul _ _ _⟩,
inv_fun := λ f, f 1,
left_inv := one_gsmul,
right_inv := λ f, add_monoid_hom.ext_int $ one_gsmul (f 1) }
variables {M G A}
@[simp] lemma powers_hom_apply [monoid M] (x : M) (n : multiplicative ℕ) :
powers_hom M x n = x ^ n.to_add := rfl
@[simp] lemma powers_hom_symm_apply [monoid M] (f : multiplicative ℕ →* M) :
(powers_hom M).symm f = f (multiplicative.of_add 1) := rfl
@[simp] lemma gpowers_hom_apply [group G] (x : G) (n : multiplicative ℤ) :
gpowers_hom G x n = x ^ n.to_add := rfl
@[simp] lemma gpowers_hom_symm_apply [group G] (f : multiplicative ℤ →* G) :
(gpowers_hom G).symm f = f (multiplicative.of_add 1) := rfl
@[simp] lemma multiples_hom_apply [add_monoid A] (x : A) (n : ℕ) :
multiples_hom A x n = n •ℕ x := rfl
@[simp] lemma multiples_hom_symm_apply [add_monoid A] (f : ℕ →+ A) :
(multiples_hom A).symm f = f 1 := rfl
@[simp] lemma gmultiples_hom_apply [add_group A] (x : A) (n : ℤ) :
gmultiples_hom A x n = n •ℤ x := rfl
@[simp] lemma gmultiples_hom_symm_apply [add_group A] (f : ℤ →+ A) :
(gmultiples_hom A).symm f = f 1 := rfl
lemma monoid_hom.apply_mnat [monoid M] (f : multiplicative ℕ →* M) (n : multiplicative ℕ) :
f n = (f (multiplicative.of_add 1)) ^ n.to_add :=
by rw [← powers_hom_symm_apply, ← powers_hom_apply, equiv.apply_symm_apply]
@[ext] lemma monoid_hom.ext_mnat [monoid M] ⦃f g : multiplicative ℕ →* M⦄
(h : f (multiplicative.of_add 1) = g (multiplicative.of_add 1)) : f = g :=
monoid_hom.ext $ λ n, by rw [f.apply_mnat, g.apply_mnat, h]
lemma monoid_hom.apply_mint [group M] (f : multiplicative ℤ →* M) (n : multiplicative ℤ) :
f n = (f (multiplicative.of_add 1)) ^ n.to_add :=
by rw [← gpowers_hom_symm_apply, ← gpowers_hom_apply, equiv.apply_symm_apply]
@[ext] lemma monoid_hom.ext_mint [group M] ⦃f g : multiplicative ℤ →* M⦄
(h : f (multiplicative.of_add 1) = g (multiplicative.of_add 1)) : f = g :=
monoid_hom.ext $ λ n, by rw [f.apply_mint, g.apply_mint, h]
lemma add_monoid_hom.apply_nat [add_monoid M] (f : ℕ →+ M) (n : ℕ) :
f n = n •ℕ (f 1) :=
by rw [← multiples_hom_symm_apply, ← multiples_hom_apply, equiv.apply_symm_apply]
/-! `add_monoid_hom.ext_nat` is defined in `data.nat.cast` -/
lemma add_monoid_hom.apply_int [add_group M] (f : ℤ →+ M) (n : ℤ) :
f n = n •ℤ (f 1) :=
by rw [← gmultiples_hom_symm_apply, ← gmultiples_hom_apply, equiv.apply_symm_apply]
/-! `add_monoid_hom.ext_int` is defined in `data.int.cast` -/
variables (M G A)
/-- If `M` is commutative, `powers_hom` is a multiplicative equivalence. -/
def powers_mul_hom [comm_monoid M] : M ≃* (multiplicative ℕ →* M) :=
{ map_mul' := λ a b, monoid_hom.ext $ by simp [mul_pow],
..powers_hom M}
/-- If `M` is commutative, `gpowers_hom` is a multiplicative equivalence. -/
def gpowers_mul_hom [comm_group G] : G ≃* (multiplicative ℤ →* G) :=
{ map_mul' := λ a b, monoid_hom.ext $ by simp [mul_gpow],
..gpowers_hom G}
/-- If `M` is commutative, `multiples_hom` is an additive equivalence. -/
def multiples_add_hom [add_comm_monoid A] : A ≃+ (ℕ →+ A) :=
{ map_add' := λ a b, add_monoid_hom.ext $ by simp [nsmul_add],
..multiples_hom A}
/-- If `M` is commutative, `gmultiples_hom` is an additive equivalence. -/
def gmultiples_add_hom [add_comm_group A] : A ≃+ (ℤ →+ A) :=
{ map_add' := λ a b, add_monoid_hom.ext $ by simp [gsmul_add],
..gmultiples_hom A}
variables {M G A}
@[simp] lemma powers_mul_hom_apply [comm_monoid M] (x : M) (n : multiplicative ℕ) :
powers_mul_hom M x n = x ^ n.to_add := rfl
@[simp] lemma powers_mul_hom_symm_apply [comm_monoid M] (f : multiplicative ℕ →* M) :
(powers_mul_hom M).symm f = f (multiplicative.of_add 1) := rfl
@[simp] lemma gpowers_mul_hom_apply [comm_group G] (x : G) (n : multiplicative ℤ) :
gpowers_mul_hom G x n = x ^ n.to_add := rfl
@[simp] lemma gpowers_mul_hom_symm_apply [comm_group G] (f : multiplicative ℤ →* G) :
(gpowers_mul_hom G).symm f = f (multiplicative.of_add 1) := rfl
@[simp] lemma multiples_add_hom_apply [add_comm_monoid A] (x : A) (n : ℕ) :
multiples_add_hom A x n = n •ℕ x := rfl
@[simp] lemma multiples_add_hom_symm_apply [add_comm_monoid A] (f : ℕ →+ A) :
(multiples_add_hom A).symm f = f 1 := rfl
@[simp] lemma gmultiples_add_hom_apply [add_comm_group A] (x : A) (n : ℤ) :
gmultiples_add_hom A x n = n •ℤ x := rfl
@[simp] lemma gmultiples_add_hom_symm_apply [add_comm_group A] (f : ℤ →+ A) :
(gmultiples_add_hom A).symm f = f 1 := rfl
/-!
### Commutativity (again)
Facts about `semiconj_by` and `commute` that require `gpow` or `gsmul`, or the fact that integer
multiplication equals semiring multiplication.
-/
namespace semiconj_by
section
variables [semiring R] {a x y : R}
@[simp] lemma cast_nat_mul_right (h : semiconj_by a x y) (n : ℕ) : semiconj_by a ((n : R) * x) (n * y) :=
semiconj_by.mul_right (nat.commute_cast _ _) h
@[simp] lemma cast_nat_mul_left (h : semiconj_by a x y) (n : ℕ) : semiconj_by ((n : R) * a) x y :=
semiconj_by.mul_left (nat.cast_commute _ _) h
@[simp] lemma cast_nat_mul_cast_nat_mul (h : semiconj_by a x y) (m n : ℕ) :
semiconj_by ((m : R) * a) (n * x) (n * y) :=
(h.cast_nat_mul_left m).cast_nat_mul_right n
end
variables [monoid M] [group G] [ring R]
@[simp] lemma units_gpow_right {a : M} {x y : units M} (h : semiconj_by a x y) :
∀ m : ℤ, semiconj_by a (↑(x^m)) (↑(y^m))
| (n : ℕ) := by simp only [gpow_coe_nat, units.coe_pow, h, pow_right]
| -[1+n] := by simp only [gpow_neg_succ_of_nat, units.coe_pow, units_inv_right, h, pow_right]
variables {a b x y x' y' : R}
@[simp] lemma cast_int_mul_right (h : semiconj_by a x y) (m : ℤ) :
semiconj_by a ((m : ℤ) * x) (m * y) :=
semiconj_by.mul_right (int.commute_cast _ _) h
@[simp] lemma cast_int_mul_left (h : semiconj_by a x y) (m : ℤ) : semiconj_by ((m : R) * a) x y :=
semiconj_by.mul_left (int.cast_commute _ _) h
@[simp] lemma cast_int_mul_cast_int_mul (h : semiconj_by a x y) (m n : ℤ) :
semiconj_by ((m : R) * a) (n * x) (n * y) :=
(h.cast_int_mul_left m).cast_int_mul_right n
end semiconj_by
namespace commute
section
variables [semiring R] {a b : R}
@[simp] theorem cast_nat_mul_right (h : commute a b) (n : ℕ) : commute a ((n : R) * b) :=
h.cast_nat_mul_right n
@[simp] theorem cast_nat_mul_left (h : commute a b) (n : ℕ) : commute ((n : R) * a) b :=
h.cast_nat_mul_left n
@[simp] theorem cast_nat_mul_cast_nat_mul (h : commute a b) (m n : ℕ) :
commute ((m : R) * a) (n * b) :=
h.cast_nat_mul_cast_nat_mul m n
@[simp] theorem self_cast_nat_mul (n : ℕ) : commute a (n * a) :=
(commute.refl a).cast_nat_mul_right n
@[simp] theorem cast_nat_mul_self (n : ℕ) : commute ((n : R) * a) a :=
(commute.refl a).cast_nat_mul_left n
@[simp] theorem self_cast_nat_mul_cast_nat_mul (m n : ℕ) : commute ((m : R) * a) (n * a) :=
(commute.refl a).cast_nat_mul_cast_nat_mul m n
end
variables [monoid M] [group G] [ring R]
@[simp] lemma units_gpow_right {a : M} {u : units M} (h : commute a u) (m : ℤ) :
commute a (↑(u^m)) :=
h.units_gpow_right m
@[simp] lemma units_gpow_left {u : units M} {a : M} (h : commute ↑u a) (m : ℤ) :
commute (↑(u^m)) a :=
(h.symm.units_gpow_right m).symm
variables {a b : R}
@[simp] lemma cast_int_mul_right (h : commute a b) (m : ℤ) : commute a (m * b) :=
h.cast_int_mul_right m
@[simp] lemma cast_int_mul_left (h : commute a b) (m : ℤ) : commute ((m : R) * a) b :=
h.cast_int_mul_left m
lemma cast_int_mul_cast_int_mul (h : commute a b) (m n : ℤ) : commute ((m : R) * a) (n * b) :=
h.cast_int_mul_cast_int_mul m n
variables (a) (m n : ℤ)
@[simp] theorem self_cast_int_mul : commute a (n * a) := (commute.refl a).cast_int_mul_right n
@[simp] theorem cast_int_mul_self : commute ((n : R) * a) a := (commute.refl a).cast_int_mul_left n
theorem self_cast_int_mul_cast_int_mul : commute ((m : R) * a) (n * a) :=
(commute.refl a).cast_int_mul_cast_int_mul m n
end commute
section multiplicative
open multiplicative
@[simp] lemma nat.to_add_pow (a : multiplicative ℕ) (b : ℕ) : to_add (a ^ b) = to_add a * b :=
begin
induction b with b ih,
{ erw [pow_zero, to_add_one, mul_zero] },
{ simp [*, pow_succ, add_comm, nat.mul_succ] }
end
@[simp] lemma nat.of_add_mul (a b : ℕ) : of_add (a * b) = of_add a ^ b :=
(nat.to_add_pow _ _).symm
@[simp] lemma int.to_add_pow (a : multiplicative ℤ) (b : ℕ) : to_add (a ^ b) = to_add a * b :=
by induction b; simp [*, mul_add, pow_succ, add_comm]
@[simp] lemma int.to_add_gpow (a : multiplicative ℤ) (b : ℤ) : to_add (a ^ b) = to_add a * b :=
int.induction_on b (by simp)
(by simp [gpow_add, mul_add] {contextual := tt})
(by simp [gpow_add, mul_add, sub_eq_add_neg] {contextual := tt})
@[simp] lemma int.of_add_mul (a b : ℤ) : of_add (a * b) = of_add a ^ b :=
(int.to_add_gpow _ _).symm
end multiplicative
namespace units
variables [monoid M]
lemma conj_pow (u : units M) (x : M) (n : ℕ) : (↑u * x * ↑(u⁻¹))^n = u * x^n * ↑(u⁻¹) :=
(divp_eq_iff_mul_eq.2 ((u.mk_semiconj_by x).pow_right n).eq.symm).symm
lemma conj_pow' (u : units M) (x : M) (n : ℕ) : (↑(u⁻¹) * x * u)^n = ↑(u⁻¹) * x^n * u:=
(u⁻¹).conj_pow x n
open opposite
/-- Moving to the opposite monoid commutes with taking powers. -/
@[simp] lemma op_pow (x : M) (n : ℕ) : op (x ^ n) = (op x) ^ n :=
begin
induction n with n h,
{ simp },
{ rw [pow_succ', op_mul, h, pow_succ] }
end
@[simp] lemma unop_pow (x : Mᵒᵖ) (n : ℕ) : unop (x ^ n) = (unop x) ^ n :=
begin
induction n with n h,
{ simp },
{ rw [pow_succ', unop_mul, h, pow_succ] }
end
end units
|
e97c42429840d4fef5e83784fe5577dec482f187 | 94096349332b0a0e223a22a3917c8f253cd39235 | /src/game/world1/level1.lean | ff0319b13a2896a38e9c6c398b36896a9d66e756 | [] | no_license | robertylewis/natural_number_game | 26156e10ef7b45248549915cc4d1ab3d8c3afc85 | b210c05cd627242f791db1ee3f365ee7829674c9 | refs/heads/master | 1,598,964,725,038 | 1,572,602,236,000 | 1,572,602,236,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,351 | lean | import mynat.definition -- imports the natural numbers {0,1,2,3,4,...}.
import mynat.add -- imports definition of addition on the natural numbers.
import mynat.mul -- imports definition of multiplication on the natural numbers.
namespace mynat -- hide
-- World name : Tutorial world
/-
# The Natural Number Game, version 1.03.
## By Kevin Buzzard and Mohammad Pedramfar.
Special thanks to Rob Lewis for tactic hackery, Sian Carey for power world (world 4),
and, last but not least, all the people who fed back comments, including
the 2019-20 Imperial College 1st year maths beta tester students, Marie-Amélie Lawn,
Toby Gee, Joseph Myers, and all the people who have been in touch
via the <a href="https://leanprover.zulipchat.com/" target="blank">Lean Zulip chat</a>
or the <a href="https://xenaproject.wordpress.com/" target="blank">Xena Project blog</a>
or via <a href="https://twitter.com/home" target="blank">Twitter</a>.
The natural number game is brought to you by the Xena project, a project based at Imperial College London
whose aim is to get mathematics undergraduates using computer theorem provers.
Lean is a computer theorem prover being developed at Microsoft Research.
# What is this game?
Welcome to the natural number game -- a game which shows the power of induction.
In this game, you get own version of the natural numbers, called `mynat`, in a programming
language called Lean. Your version of the natural numbers satisfies something called
the principle of mathematical induction, and a couple
of other things too (Peano's axioms). Unfortunately, nobody has proved any theorems about these
natural numbers yet. For example, addition will be defined for you,
but nobody has proved that `x + y = y + x` yet. This is your job. You're going to
prove mathematical theorems using the Lean theorem prover. In other words, you're going to solve
levels in a computer game.
You're going to prove these theorems using *tactics*. This introductory world, Tutorial World,
will take you through some of these tactics. During your proofs, your "goal" (i.e. what you're
supposed to be proving) will be displayed with a `⊢` symbol in front of it. If the top
right hand box reports "Theorem Proved!", you have closed all the goals in the level
and can move on to the next level in the world you're in.
# World 1: Tutorial World
## Level 1: the `refl` tactic.
Let's start with the `refl` tactic. `refl` stands for "reflexivity", which is a fancy
way of saying that it will prove any goal of the form `A = A`. It doesn't matter how
complicated `A` is, all that matters is that the left hand side is *exactly equal* to the
right hand side (a computer scientist would say "definitionally equal"). I really mean
"press the same buttons on your computer in the same order" equal.
For example, `x * y + z = x * y + z` can be proved by `refl`, but `x + y = y + x` cannot.
Let's see `refl` in action! At the bottom of the text in this box, there's a lemma,
which says that if $x$, $y$ and $z$ are natural numbers then $xy + z = xy + z$.
Locate this lemma (if you can't see the lemma and these instructions at the same time, make this box wider
by dragging the sides). Let's supply the proof. Click on the word `sorry` and then delete it.
When the system finishes being busy, you'll be able to see your goal -- the objective
of this level -- in the box on the top right. Remember that the goal is
the thing with the weird `⊢` thing just before it. The goal in this case is `x * y + z = x * y + z`,
where `x`, `y` and `z` are some of your very own natural numbers.
That's a pretty easy goal to prove -- you can just prove it with the `refl` tactic.
Where it used to say `sorry`, write
`refl,`
**and don't forget the comma**. Then hit enter to go onto the next line.
If all is well, Lean should tell you "Proof complete!" in the top right box, and there
should be no errors in the bottom right box. You just did the first
level of the tutorial! And you also learnt how to avoid by *far* the most
common mistake that beginner users make -- **every line must end with a comma**.
If things go weird and you don't understand why the top right box is empty,
check for missing commas. Also check you've spelt `refl` correctly: it's REFL
for "reflexivity".
For each level, the idea is to get Lean into this state: with the top right
box saying "Proof complete!" and the bottom right box empty (i.e. with no errors in).
If you want to be reminded about the `refl` tactic, you can click on the "Tactics" drop
down menu on the left. Resize the window if it's too small.
Now click on "next level" in the top right of your browser to go onto the second level of
tutorial world, where we'll learn about the `rw` tactic. [NB don't click on "next world",
we're not ready for addition world yet]
-/
/- Lemma : no-side-bar
For all natural numbers $x$, $y$ and $z$, we have $xy + z = xy + z$.
-/
lemma example1 (x y z : mynat) : x * y + z = x * y + z :=
begin [less_leaky]
refl
end
/- Tactic : refl
The `refl` tactic will close any goal of the form `A = B`
where `A` and `B` are *exactly the same thing*.
### Example:
If it looks like this in the top right hand box:
```
a b c d : mynat
⊢ (a + b) * (c + d) = (a + b) * (c + d)
```
then
`refl,`
will close the goal and solve the level. Don't forget the comma.
-/
end mynat -- hide
|
0f5c4afe14690b9a3f18c04049fc434963256bae | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /library/init/meta/attribute.lean | a963246c64bc4d437ba6851cd87f6cceaf7ae60d | [
"Apache-2.0"
] | permissive | bre7k30/lean | de893411bcfa7b3c5572e61b9e1c52951b310aa4 | 5a924699d076dab1bd5af23a8f910b433e598d7a | refs/heads/master | 1,610,900,145,817 | 1,488,006,845,000 | 1,488,006,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,601 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
prelude
import init.meta.tactic init.meta.rb_map init.meta.quote
meta constant attribute.get_instances : name → tactic (list name)
meta constant attribute.fingerprint : name → tactic nat
structure user_attribute :=
(name : name)
(descr : string)
/- Registers a new user-defined attribute. The argument must be the name of a definition of type
`user_attribute` or a sub-structure. -/
meta constant attribute.register : name → command
meta structure caching_user_attribute (α : Type) extends user_attribute :=
(mk_cache : list _root_.name → tactic α)
(dependencies : list _root_.name)
meta constant caching_user_attribute.get_cache : Π {α : Type}, caching_user_attribute α → tactic α
open tactic
meta def register_attribute := attribute.register
meta def mk_name_set_attr (attr_name : name) : command :=
do t ← to_expr ``(caching_user_attribute name_set),
v ← to_expr ``({name := %%(quote attr_name),
descr := "name_set attribute",
mk_cache := λ ns, return (name_set.of_list ns),
dependencies := [] } : caching_user_attribute name_set),
add_meta_definition attr_name [] t v,
register_attribute attr_name
meta def get_name_set_for_attr (attr_name : name) : tactic name_set :=
do
cnst ← return (expr.const attr_name []),
attr ← eval_expr (caching_user_attribute name_set) cnst,
caching_user_attribute.get_cache attr
|
b68a650c120638cf113ccf982fdee8fde53eb6c5 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/topology/partition_of_unity.lean | 7f7e1ace7b685dd29ed1b5cc6e62ce525ba4572b | [
"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 | 22,935 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import algebra.big_operators.finprod
import set_theory.ordinal.basic
import topology.continuous_function.algebra
import topology.paracompact
import topology.shrinking_lemma
import topology.urysohns_lemma
/-!
# Continuous partition of unity
In this file we define `partition_of_unity (ι X : Type*) [topological_space X] (s : set X := univ)`
to be a continuous partition of unity on `s` indexed by `ι`. More precisely, `f : partition_of_unity
ι X s` is a collection of continuous functions `f i : C(X, ℝ)`, `i : ι`, such that
* the supports of `f i` form a locally finite family of sets;
* each `f i` is nonnegative;
* `∑ᶠ i, f i x = 1` for all `x ∈ s`;
* `∑ᶠ i, f i x ≤ 1` for all `x : X`.
In the case `s = univ` the last assumption follows from the previous one but it is convenient to
have this assumption in the case `s ≠ univ`.
We also define a bump function covering,
`bump_covering (ι X : Type*) [topological_space X] (s : set X := univ)`, to be a collection of
functions `f i : C(X, ℝ)`, `i : ι`, such that
* the supports of `f i` form a locally finite family of sets;
* each `f i` is nonnegative;
* for each `x ∈ s` there exists `i : ι` such that `f i y = 1` in a neighborhood of `x`.
The term is motivated by the smooth case.
If `f` is a bump function covering indexed by a linearly ordered type, then
`g i x = f i x * ∏ᶠ j < i, (1 - f j x)` is a partition of unity, see
`bump_covering.to_partition_of_unity`. Note that only finitely many terms `1 - f j x` are not equal
to one, so this product is well-defined.
Note that `g i x = ∏ᶠ j ≤ i, (1 - f j x) - ∏ᶠ j < i, (1 - f j x)`, so most terms in the sum
`∑ᶠ i, g i x` cancel, and we get `∑ᶠ i, g i x = 1 - ∏ᶠ i, (1 - f i x)`, and the latter product
equals zero because one of `f i x` is equal to one.
We say that a partition of unity or a bump function covering `f` is *subordinate* to a family of
sets `U i`, `i : ι`, if the closure of the support of each `f i` is included in `U i`. We use
Urysohn's Lemma to prove that a locally finite open covering of a normal topological space admits a
subordinate bump function covering (hence, a subordinate partition of unity), see
`bump_covering.exists_is_subordinate_of_locally_finite`. If `X` is a paracompact space, then any
open covering admits a locally finite refinement, hence it admits a subordinate bump function
covering and a subordinate partition of unity, see `bump_covering.exists_is_subordinate`.
We also provide two slightly more general versions of these lemmas,
`bump_covering.exists_is_subordinate_of_locally_finite_of_prop` and
`bump_covering.exists_is_subordinate_of_prop`, to be used later in the construction of a smooth
partition of unity.
## Implementation notes
Most (if not all) books only define a partition of unity of the whole space. However, quite a few
proofs only deal with `f i` such that `tsupport (f i)` meets a specific closed subset, and
it is easier to formalize these proofs if we don't have other functions right away.
We use `well_ordering_rel j i` instead of `j < i` in the definition of
`bump_covering.to_partition_of_unity` to avoid a `[linear_order ι]` assumption. While
`well_ordering_rel j i` is a well order, not only a strict linear order, we never use this property.
## Tags
partition of unity, bump function, Urysohn's lemma, normal space, paracompact space
-/
universes u v
open function set filter
open_locale big_operators topological_space classical
noncomputable theory
/-- A continuous partition of unity on a set `s : set X` is a collection of continuous functions
`f i` such that
* the supports of `f i` form a locally finite family of sets, i.e., for every point `x : X` there
exists a neighborhood `U ∋ x` such that all but finitely many functions `f i` are zero on `U`;
* the functions `f i` are nonnegative;
* the sum `∑ᶠ i, f i x` is equal to one for every `x ∈ s` and is less than or equal to one
otherwise.
If `X` is a normal paracompact space, then `partition_of_unity.exists_is_subordinate` guarantees
that for every open covering `U : set (set X)` of `s` there exists a partition of unity that is
subordinate to `U`.
-/
structure partition_of_unity (ι X : Type*) [topological_space X] (s : set X := univ) :=
(to_fun : ι → C(X, ℝ))
(locally_finite' : locally_finite (λ i, support (to_fun i)))
(nonneg' : 0 ≤ to_fun)
(sum_eq_one' : ∀ x ∈ s, ∑ᶠ i, to_fun i x = 1)
(sum_le_one' : ∀ x, ∑ᶠ i, to_fun i x ≤ 1)
/-- A `bump_covering ι X s` is an indexed family of functions `f i`, `i : ι`, such that
* the supports of `f i` form a locally finite family of sets, i.e., for every point `x : X` there
exists a neighborhood `U ∋ x` such that all but finitely many functions `f i` are zero on `U`;
* for all `i`, `x` we have `0 ≤ f i x ≤ 1`;
* each point `x ∈ s` belongs to the interior of `{x | f i x = 1}` for some `i`.
One of the main use cases for a `bump_covering` is to define a `partition_of_unity`, see
`bump_covering.to_partition_of_unity`, but some proofs can directly use a `bump_covering` instead of
a `partition_of_unity`.
If `X` is a normal paracompact space, then `bump_covering.exists_is_subordinate` guarantees that for
every open covering `U : set (set X)` of `s` there exists a `bump_covering` of `s` that is
subordinate to `U`.
-/
structure bump_covering (ι X : Type*) [topological_space X] (s : set X := univ) :=
(to_fun : ι → C(X, ℝ))
(locally_finite' : locally_finite (λ i, support (to_fun i)))
(nonneg' : 0 ≤ to_fun)
(le_one' : to_fun ≤ 1)
(eventually_eq_one' : ∀ x ∈ s, ∃ i, to_fun i =ᶠ[𝓝 x] 1)
variables {ι : Type u} {X : Type v} [topological_space X]
namespace partition_of_unity
variables {E : Type*} [add_comm_monoid E] [smul_with_zero ℝ E] [topological_space E]
[has_continuous_smul ℝ E] {s : set X} (f : partition_of_unity ι X s)
instance : has_coe_to_fun (partition_of_unity ι X s) (λ _, ι → C(X, ℝ)) := ⟨to_fun⟩
protected lemma locally_finite : locally_finite (λ i, support (f i)) := f.locally_finite'
lemma locally_finite_tsupport : locally_finite (λ i, tsupport (f i)) := f.locally_finite.closure
lemma nonneg (i : ι) (x : X) : 0 ≤ f i x := f.nonneg' i x
lemma sum_eq_one {x : X} (hx : x ∈ s) : ∑ᶠ i, f i x = 1 := f.sum_eq_one' x hx
/-- If `f` is a partition of unity on `s`, then for every `x ∈ s` there exists an index `i` such
that `0 < f i x`. -/
lemma exists_pos {x : X} (hx : x ∈ s) : ∃ i, 0 < f i x :=
begin
have H := f.sum_eq_one hx,
contrapose! H,
simpa only [λ i, (H i).antisymm (f.nonneg i x), finsum_zero] using zero_ne_one
end
lemma sum_le_one (x : X) : ∑ᶠ i, f i x ≤ 1 := f.sum_le_one' x
lemma sum_nonneg (x : X) : 0 ≤ ∑ᶠ i, f i x := finsum_nonneg $ λ i, f.nonneg i x
lemma le_one (i : ι) (x : X) : f i x ≤ 1 :=
(single_le_finsum i (f.locally_finite.point_finite x) (λ j, f.nonneg j x)).trans (f.sum_le_one x)
/-- If `f` is a partition of unity on `s : set X` and `g : X → E` is continuous at every point of
the topological support of some `f i`, then `λ x, f i x • g x` is continuous on the whole space. -/
lemma continuous_smul {g : X → E} {i : ι} (hg : ∀ x ∈ tsupport (f i), continuous_at g x) :
continuous (λ x, f i x • g x) :=
continuous_of_tsupport $ λ x hx, ((f i).continuous_at x).smul $
hg x $ tsupport_smul_subset_left _ _ hx
/-- If `f` is a partition of unity on a set `s : set X` and `g : ι → X → E` is a family of functions
such that each `g i` is continuous at every point of the topological support of `f i`, then the sum
`λ x, ∑ᶠ i, f i x • g i x` is continuous on the whole space. -/
lemma continuous_finsum_smul [has_continuous_add E] {g : ι → X → E}
(hg : ∀ i (x ∈ tsupport (f i)), continuous_at (g i) x) :
continuous (λ x, ∑ᶠ i, f i x • g i x) :=
continuous_finsum (λ i, f.continuous_smul (hg i)) $
f.locally_finite.subset $ λ i, support_smul_subset_left _ _
/-- A partition of unity `f i` is subordinate to a family of sets `U i` indexed by the same type if
for each `i` the closure of the support of `f i` is a subset of `U i`. -/
def is_subordinate (U : ι → set X) : Prop :=
∀ i, tsupport (f i) ⊆ U i
variables {f}
lemma exists_finset_nhd_support_subset {U : ι → set X}
(hso : f.is_subordinate U) (ho : ∀ i, is_open (U i)) (x : X) :
∃ (is : finset ι) {n : set X} (hn₁ : n ∈ 𝓝 x) (hn₂ : n ⊆ ⋂ i ∈ is, U i), ∀ (z ∈ n),
support (λ i, f i z) ⊆ is :=
f.locally_finite.exists_finset_nhd_support_subset hso ho x
/-- If `f` is a partition of unity that is subordinate to a family of open sets `U i` and
`g : ι → X → E` is a family of functions such that each `g i` is continuous on `U i`, then the sum
`λ x, ∑ᶠ i, f i x • g i x` is a continuous function. -/
lemma is_subordinate.continuous_finsum_smul [has_continuous_add E] {U : ι → set X}
(ho : ∀ i, is_open (U i)) (hf : f.is_subordinate U) {g : ι → X → E}
(hg : ∀ i, continuous_on (g i) (U i)) :
continuous (λ x, ∑ᶠ i, f i x • g i x) :=
f.continuous_finsum_smul $ λ i x hx, (hg i).continuous_at $ (ho i).mem_nhds $ hf i hx
end partition_of_unity
namespace bump_covering
variables {s : set X} (f : bump_covering ι X s)
instance : has_coe_to_fun (bump_covering ι X s) (λ _, ι → C(X, ℝ)) := ⟨to_fun⟩
protected lemma locally_finite : locally_finite (λ i, support (f i)) := f.locally_finite'
lemma locally_finite_tsupport : locally_finite (λ i, tsupport (f i)) := f.locally_finite.closure
protected lemma point_finite (x : X) : {i | f i x ≠ 0}.finite := f.locally_finite.point_finite x
lemma nonneg (i : ι) (x : X) : 0 ≤ f i x := f.nonneg' i x
lemma le_one (i : ι) (x : X) : f i x ≤ 1 := f.le_one' i x
/-- A `bump_covering` that consists of a single function, uniformly equal to one, defined as an
example for `inhabited` instance. -/
protected def single (i : ι) (s : set X) : bump_covering ι X s :=
{ to_fun := pi.single i 1,
locally_finite' := λ x,
begin
refine ⟨univ, univ_mem, (finite_singleton i).subset _⟩,
rintro j ⟨x, hx, -⟩,
contrapose! hx,
rw [mem_singleton_iff] at hx,
simp [hx]
end,
nonneg' := le_update_iff.2 ⟨λ x, zero_le_one, λ _ _, le_rfl⟩,
le_one' := update_le_iff.2 ⟨le_rfl, λ _ _ _, zero_le_one⟩,
eventually_eq_one' := λ x _, ⟨i, by simp⟩ }
@[simp] lemma coe_single (i : ι) (s : set X) : ⇑(bump_covering.single i s) = pi.single i 1 := rfl
instance [inhabited ι] : inhabited (bump_covering ι X s) :=
⟨bump_covering.single default s⟩
/-- A collection of bump functions `f i` is subordinate to a family of sets `U i` indexed by the
same type if for each `i` the closure of the support of `f i` is a subset of `U i`. -/
def is_subordinate (f : bump_covering ι X s) (U : ι → set X) : Prop :=
∀ i, tsupport (f i) ⊆ U i
lemma is_subordinate.mono {f : bump_covering ι X s} {U V : ι → set X} (hU : f.is_subordinate U)
(hV : ∀ i, U i ⊆ V i) :
f.is_subordinate V :=
λ i, subset.trans (hU i) (hV i)
/-- If `X` is a normal topological space and `U i`, `i : ι`, is a locally finite open covering of a
closed set `s`, then there exists a `bump_covering ι X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. This version assumes that `p : (X → ℝ) → Prop` is a predicate
that satisfies Urysohn's lemma, and provides a `bump_covering` such that each function of the
covering satisfies `p`. -/
lemma exists_is_subordinate_of_locally_finite_of_prop [normal_space X] (p : (X → ℝ) → Prop)
(h01 : ∀ s t, is_closed s → is_closed t → disjoint s t →
∃ f : C(X, ℝ), p f ∧ eq_on f 0 s ∧ eq_on f 1 t ∧ ∀ x, f x ∈ Icc (0 : ℝ) 1)
(hs : is_closed s) (U : ι → set X) (ho : ∀ i, is_open (U i)) (hf : locally_finite U)
(hU : s ⊆ ⋃ i, U i) :
∃ f : bump_covering ι X s, (∀ i, p (f i)) ∧ f.is_subordinate U :=
begin
rcases exists_subset_Union_closure_subset hs ho (λ x _, hf.point_finite x) hU
with ⟨V, hsV, hVo, hVU⟩,
have hVU' : ∀ i, V i ⊆ U i, from λ i, subset.trans subset_closure (hVU i),
rcases exists_subset_Union_closure_subset hs hVo
(λ x _, (hf.subset hVU').point_finite x) hsV with ⟨W, hsW, hWo, hWV⟩,
choose f hfp hf0 hf1 hf01
using λ i, h01 _ _ (is_closed_compl_iff.2 $ hVo i)
is_closed_closure (disjoint_right.2 $ λ x hx, not_not.2 (hWV i hx)),
have hsupp : ∀ i, support (f i) ⊆ V i,
from λ i, support_subset_iff'.2 (hf0 i),
refine ⟨⟨f, hf.subset (λ i, subset.trans (hsupp i) (hVU' i)),
λ i x, (hf01 i x).1, λ i x, (hf01 i x).2, λ x hx, _⟩, hfp,
λ i, subset.trans (closure_mono (hsupp i)) (hVU i)⟩,
rcases mem_Union.1 (hsW hx) with ⟨i, hi⟩,
exact ⟨i, ((hf1 i).mono subset_closure).eventually_eq_of_mem ((hWo i).mem_nhds hi)⟩
end
/-- If `X` is a normal topological space and `U i`, `i : ι`, is a locally finite open covering of a
closed set `s`, then there exists a `bump_covering ι X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. -/
lemma exists_is_subordinate_of_locally_finite [normal_space X] (hs : is_closed s)
(U : ι → set X) (ho : ∀ i, is_open (U i)) (hf : locally_finite U)
(hU : s ⊆ ⋃ i, U i) :
∃ f : bump_covering ι X s, f.is_subordinate U :=
let ⟨f, _, hfU⟩ :=
exists_is_subordinate_of_locally_finite_of_prop (λ _, true)
(λ s t hs ht hd, (exists_continuous_zero_one_of_closed hs ht hd).imp $ λ f hf, ⟨trivial, hf⟩)
hs U ho hf hU
in ⟨f, hfU⟩
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `bump_covering ι X s` that is subordinate to `U`. This version assumes that
`p : (X → ℝ) → Prop` is a predicate that satisfies Urysohn's lemma, and provides a
`bump_covering` such that each function of the covering satisfies `p`. -/
lemma exists_is_subordinate_of_prop [normal_space X] [paracompact_space X] (p : (X → ℝ) → Prop)
(h01 : ∀ s t, is_closed s → is_closed t → disjoint s t →
∃ f : C(X, ℝ), p f ∧ eq_on f 0 s ∧ eq_on f 1 t ∧ ∀ x, f x ∈ Icc (0 : ℝ) 1)
(hs : is_closed s) (U : ι → set X) (ho : ∀ i, is_open (U i)) (hU : s ⊆ ⋃ i, U i) :
∃ f : bump_covering ι X s, (∀ i, p (f i)) ∧ f.is_subordinate U :=
begin
rcases precise_refinement_set hs _ ho hU with ⟨V, hVo, hsV, hVf, hVU⟩,
rcases exists_is_subordinate_of_locally_finite_of_prop p h01 hs V hVo hVf hsV with ⟨f, hfp, hf⟩,
exact ⟨f, hfp, hf.mono hVU⟩
end
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `bump_covering ι X s` that is subordinate to `U`. -/
lemma exists_is_subordinate [normal_space X] [paracompact_space X]
(hs : is_closed s) (U : ι → set X) (ho : ∀ i, is_open (U i)) (hU : s ⊆ ⋃ i, U i) :
∃ f : bump_covering ι X s, f.is_subordinate U :=
begin
rcases precise_refinement_set hs _ ho hU with ⟨V, hVo, hsV, hVf, hVU⟩,
rcases exists_is_subordinate_of_locally_finite hs V hVo hVf hsV with ⟨f, hf⟩,
exact ⟨f, hf.mono hVU⟩
end
/-- Index of a bump function such that `fs i =ᶠ[𝓝 x] 1`. -/
def ind (x : X) (hx : x ∈ s) : ι := (f.eventually_eq_one' x hx).some
lemma eventually_eq_one (x : X) (hx : x ∈ s) : f (f.ind x hx) =ᶠ[𝓝 x] 1 :=
(f.eventually_eq_one' x hx).some_spec
lemma ind_apply (x : X) (hx : x ∈ s) : f (f.ind x hx) x = 1 :=
(f.eventually_eq_one x hx).eq_of_nhds
/-- Partition of unity defined by a `bump_covering`. We use this auxiliary definition to prove some
properties of the new family of functions before bundling it into a `partition_of_unity`. Do not use
this definition, use `bump_function.to_partition_of_unity` instead.
The partition of unity is given by the formula `g i x = f i x * ∏ᶠ j < i, (1 - f j x)`. In other
words, `g i x = ∏ᶠ j < i, (1 - f j x) - ∏ᶠ j ≤ i, (1 - f j x)`, so
`∑ᶠ i, g i x = 1 - ∏ᶠ j, (1 - f j x)`. If `x ∈ s`, then one of `f j x` equals one, hence the product
of `1 - f j x` vanishes, and `∑ᶠ i, g i x = 1`.
In order to avoid an assumption `linear_order ι`, we use `well_ordering_rel` instead of `(<)`. -/
def to_pou_fun (i : ι) (x : X) : ℝ :=
f i x * ∏ᶠ j (hj : well_ordering_rel j i), (1 - f j x)
lemma to_pou_fun_zero_of_zero {i : ι} {x : X} (h : f i x = 0) :
f.to_pou_fun i x = 0 :=
by rw [to_pou_fun, h, zero_mul]
lemma support_to_pou_fun_subset (i : ι) :
support (f.to_pou_fun i) ⊆ support (f i) :=
λ x, mt $ f.to_pou_fun_zero_of_zero
lemma to_pou_fun_eq_mul_prod (i : ι) (x : X) (t : finset ι)
(ht : ∀ j, well_ordering_rel j i → f j x ≠ 0 → j ∈ t) :
f.to_pou_fun i x = f i x * ∏ j in t.filter (λ j, well_ordering_rel j i), (1 - f j x) :=
begin
refine congr_arg _ (finprod_cond_eq_prod_of_cond_iff _ (λ j hj, _)),
rw [ne.def, sub_eq_self] at hj,
rw [finset.mem_filter, iff.comm, and_iff_right_iff_imp],
exact flip (ht j) hj
end
lemma sum_to_pou_fun_eq (x : X) :
∑ᶠ i, f.to_pou_fun i x = 1 - ∏ᶠ i, (1 - f i x) :=
begin
set s := (f.point_finite x).to_finset,
have hs : (s : set ι) = {i | f i x ≠ 0} := finite.coe_to_finset _,
have A : support (λ i, to_pou_fun f i x) ⊆ s,
{ rw hs,
exact λ i hi, f.support_to_pou_fun_subset i hi },
have B : mul_support (λ i, 1 - f i x) ⊆ s,
{ rw [hs, mul_support_one_sub], exact λ i, id },
letI : linear_order ι := linear_order_of_STO' well_ordering_rel,
rw [finsum_eq_sum_of_support_subset _ A, finprod_eq_prod_of_mul_support_subset _ B,
finset.prod_one_sub_ordered, sub_sub_cancel],
refine finset.sum_congr rfl (λ i hi, _),
convert f.to_pou_fun_eq_mul_prod _ _ _ (λ j hji hj, _),
rwa finite.mem_to_finset
end
lemma exists_finset_to_pou_fun_eventually_eq (i : ι) (x : X) :
∃ t : finset ι, f.to_pou_fun i =ᶠ[𝓝 x]
f i * ∏ j in t.filter (λ j, well_ordering_rel j i), (1 - f j) :=
begin
rcases f.locally_finite x with ⟨U, hU, hf⟩,
use hf.to_finset,
filter_upwards [hU] with y hyU,
simp only [pi.mul_apply, finset.prod_apply],
apply to_pou_fun_eq_mul_prod,
intros j hji hj,
exact hf.mem_to_finset.2 ⟨y, ⟨hj, hyU⟩⟩
end
lemma continuous_to_pou_fun (i : ι) : continuous (f.to_pou_fun i) :=
begin
refine ((f i).continuous.mul $
continuous_finprod_cond (λ j _, continuous_const.sub (f j).continuous) _),
simp only [mul_support_one_sub],
exact f.locally_finite
end
/-- The partition of unity defined by a `bump_covering`.
The partition of unity is given by the formula `g i x = f i x * ∏ᶠ j < i, (1 - f j x)`. In other
words, `g i x = ∏ᶠ j < i, (1 - f j x) - ∏ᶠ j ≤ i, (1 - f j x)`, so
`∑ᶠ i, g i x = 1 - ∏ᶠ j, (1 - f j x)`. If `x ∈ s`, then one of `f j x` equals one, hence the product
of `1 - f j x` vanishes, and `∑ᶠ i, g i x = 1`.
In order to avoid an assumption `linear_order ι`, we use `well_ordering_rel` instead of `(<)`. -/
def to_partition_of_unity : partition_of_unity ι X s :=
{ to_fun := λ i, ⟨f.to_pou_fun i, f.continuous_to_pou_fun i⟩,
locally_finite' := f.locally_finite.subset f.support_to_pou_fun_subset,
nonneg' := λ i x, mul_nonneg (f.nonneg i x)
(finprod_cond_nonneg $ λ j hj, sub_nonneg.2 $ f.le_one j x),
sum_eq_one' := λ x hx,
begin
simp only [continuous_map.coe_mk, sum_to_pou_fun_eq, sub_eq_self],
apply finprod_eq_zero (λ i, 1 - f i x) (f.ind x hx),
{ simp only [f.ind_apply x hx, sub_self] },
{ rw mul_support_one_sub, exact f.point_finite x }
end,
sum_le_one' := λ x,
begin
simp only [continuous_map.coe_mk, sum_to_pou_fun_eq, sub_le_self_iff],
exact finprod_nonneg (λ i, sub_nonneg.2 $ f.le_one i x)
end }
lemma to_partition_of_unity_apply (i : ι) (x : X) :
f.to_partition_of_unity i x = f i x * ∏ᶠ j (hj : well_ordering_rel j i), (1 - f j x) :=
rfl
lemma to_partition_of_unity_eq_mul_prod (i : ι) (x : X) (t : finset ι)
(ht : ∀ j, well_ordering_rel j i → f j x ≠ 0 → j ∈ t) :
f.to_partition_of_unity i x = f i x * ∏ j in t.filter (λ j, well_ordering_rel j i), (1 - f j x) :=
f.to_pou_fun_eq_mul_prod i x t ht
lemma exists_finset_to_partition_of_unity_eventually_eq (i : ι) (x : X) :
∃ t : finset ι, f.to_partition_of_unity i =ᶠ[𝓝 x]
f i * ∏ j in t.filter (λ j, well_ordering_rel j i), (1 - f j) :=
f.exists_finset_to_pou_fun_eventually_eq i x
lemma to_partition_of_unity_zero_of_zero {i : ι} {x : X} (h : f i x = 0) :
f.to_partition_of_unity i x = 0 :=
f.to_pou_fun_zero_of_zero h
lemma support_to_partition_of_unity_subset (i : ι) :
support (f.to_partition_of_unity i) ⊆ support (f i) :=
f.support_to_pou_fun_subset i
lemma sum_to_partition_of_unity_eq (x : X) :
∑ᶠ i, f.to_partition_of_unity i x = 1 - ∏ᶠ i, (1 - f i x) :=
f.sum_to_pou_fun_eq x
lemma is_subordinate.to_partition_of_unity {f : bump_covering ι X s} {U : ι → set X}
(h : f.is_subordinate U) :
f.to_partition_of_unity.is_subordinate U :=
λ i, subset.trans (closure_mono $ f.support_to_partition_of_unity_subset i) (h i)
end bump_covering
namespace partition_of_unity
variables {s : set X}
instance [inhabited ι] : inhabited (partition_of_unity ι X s) :=
⟨bump_covering.to_partition_of_unity default⟩
/-- If `X` is a normal topological space and `U` is a locally finite open covering of a closed set
`s`, then there exists a `partition_of_unity ι X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. -/
lemma exists_is_subordinate_of_locally_finite [normal_space X] (hs : is_closed s)
(U : ι → set X) (ho : ∀ i, is_open (U i)) (hf : locally_finite U)
(hU : s ⊆ ⋃ i, U i) :
∃ f : partition_of_unity ι X s, f.is_subordinate U :=
let ⟨f, hf⟩ := bump_covering.exists_is_subordinate_of_locally_finite hs U ho hf hU
in ⟨f.to_partition_of_unity, hf.to_partition_of_unity⟩
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `partition_of_unity ι X s` that is subordinate to `U`. -/
lemma exists_is_subordinate [normal_space X] [paracompact_space X] (hs : is_closed s)
(U : ι → set X) (ho : ∀ i, is_open (U i)) (hU : s ⊆ ⋃ i, U i) :
∃ f : partition_of_unity ι X s, f.is_subordinate U :=
let ⟨f, hf⟩ := bump_covering.exists_is_subordinate hs U ho hU
in ⟨f.to_partition_of_unity, hf.to_partition_of_unity⟩
end partition_of_unity
|
0d001a8d2a6922bfeb70790f3d781e4a7fbf04b0 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/algebra/group/type_tags.lean | 3f056b4e99ad0afb6aa3638e84f004e7ba26125f | [
"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,041 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import algebra.group.hom
/-!
# Type tags that turn additive structures into multiplicative, and vice versa
We define two type tags:
* `additive α`: turns any multiplicative structure on `α` into the corresponding
additive structure on `additive α`;
* `multiplicative α`: turns any additive structure on `α` into the corresponding
multiplicative structure on `multiplicative α`.
We also define instances `additive.*` and `multiplicative.*` that actually transfer the structures.
-/
universes u v
variables {α : Type u} {β : Type v}
/-- If `α` carries some multiplicative structure, then `additive α` carries the corresponding
additive structure. -/
def additive (α : Type*) := α
/-- If `α` carries some additive structure, then `multiplicative α` carries the corresponding
multiplicative structure. -/
def multiplicative (α : Type*) := α
instance [inhabited α] : inhabited (additive α) := ⟨(default _ : α)⟩
instance [inhabited α] : inhabited (multiplicative α) := ⟨(default _ : α)⟩
instance additive.has_add [has_mul α] : has_add (additive α) :=
{ add := ((*) : α → α → α) }
instance [has_add α] : has_mul (multiplicative α) :=
{ mul := ((+) : α → α → α) }
instance [semigroup α] : add_semigroup (additive α) :=
{ add_assoc := @mul_assoc α _,
..additive.has_add }
instance [add_semigroup α] : semigroup (multiplicative α) :=
{ mul_assoc := @add_assoc α _,
..multiplicative.has_mul }
instance [comm_semigroup α] : add_comm_semigroup (additive α) :=
{ add_comm := @mul_comm _ _,
..additive.add_semigroup }
instance [add_comm_semigroup α] : comm_semigroup (multiplicative α) :=
{ mul_comm := @add_comm _ _,
..multiplicative.semigroup }
instance [left_cancel_semigroup α] : add_left_cancel_semigroup (additive α) :=
{ add_left_cancel := @mul_left_cancel _ _,
..additive.add_semigroup }
instance [add_left_cancel_semigroup α] : left_cancel_semigroup (multiplicative α) :=
{ mul_left_cancel := @add_left_cancel _ _,
..multiplicative.semigroup }
instance [right_cancel_semigroup α] : add_right_cancel_semigroup (additive α) :=
{ add_right_cancel := @mul_right_cancel _ _,
..additive.add_semigroup }
instance [add_right_cancel_semigroup α] : right_cancel_semigroup (multiplicative α) :=
{ mul_right_cancel := @add_right_cancel _ _,
..multiplicative.semigroup }
instance [monoid α] : add_monoid (additive α) :=
{ zero := (1 : α),
zero_add := @one_mul _ _,
add_zero := @mul_one _ _,
..additive.add_semigroup }
instance [add_monoid α] : monoid (multiplicative α) :=
{ one := (0 : α),
one_mul := @zero_add _ _,
mul_one := @add_zero _ _,
..multiplicative.semigroup }
instance [comm_monoid α] : add_comm_monoid (additive α) :=
{ add_comm := @mul_comm α _,
..additive.add_monoid }
instance [add_comm_monoid α] : comm_monoid (multiplicative α) :=
{ mul_comm := @add_comm α _,
..multiplicative.monoid }
instance [group α] : add_group (additive α) :=
{ neg := @has_inv.inv α _,
add_left_neg := @mul_left_inv _ _,
..additive.add_monoid }
instance [add_group α] : group (multiplicative α) :=
{ inv := @has_neg.neg α _,
mul_left_inv := @add_left_neg _ _,
..multiplicative.monoid }
instance [comm_group α] : add_comm_group (additive α) :=
{ add_comm := @mul_comm α _,
..additive.add_group }
instance [add_comm_group α] : comm_group (multiplicative α) :=
{ mul_comm := @add_comm α _,
..multiplicative.group }
/-- Reinterpret `f : α →+ β` as `multiplicative α →* multiplicative β`. -/
def add_monoid_hom.to_multiplicative [add_monoid α] [add_monoid β] (f : α →+ β) :
multiplicative α →* multiplicative β :=
⟨f.1, f.2, f.3⟩
/-- Reinterpret `f : α →* β` as `additive α →+ additive β`. -/
def monoid_hom.to_additive [monoid α] [monoid β] (f : α →* β) : additive α →+ additive β :=
⟨f.1, f.2, f.3⟩
|
3ca62c71d028443c5441c0730cccca43017cf51d | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/measure_theory/decomposition/unsigned_hahn.lean | 1f6150bc2c764b457db8460ea72532e4db7688f7 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 8,503 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import measure_theory.measure.measure_space
/-!
# Unsigned Hahn decomposition theorem
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file proves the unsigned version of the Hahn decomposition theorem.
## Main statements
* `hahn_decomposition` : Given two finite measures `μ` and `ν`, there exists a measurable set `s`
such that any measurable set `t` included in `s` satisfies `ν t ≤ μ t`, and any
measurable set `u` included in the complement of `s` satisfies `μ u ≤ ν u`.
## Tags
Hahn decomposition
-/
open set filter
open_locale classical topology ennreal
namespace measure_theory
variables {α : Type*} [measurable_space α] {μ ν : measure α}
/-- **Hahn decomposition theorem** -/
lemma hahn_decomposition [is_finite_measure μ] [is_finite_measure ν] :
∃s, measurable_set s ∧
(∀t, measurable_set t → t ⊆ s → ν t ≤ μ t) ∧
(∀t, measurable_set t → t ⊆ sᶜ → μ t ≤ ν t) :=
begin
let d : set α → ℝ := λs, ((μ s).to_nnreal : ℝ) - (ν s).to_nnreal,
let c : set ℝ := d '' {s | measurable_set s },
let γ : ℝ := Sup c,
have hμ : ∀ s, μ s ≠ ∞ := measure_ne_top μ,
have hν : ∀ s, ν s ≠ ∞ := measure_ne_top ν,
have to_nnreal_μ : ∀s, ((μ s).to_nnreal : ℝ≥0∞) = μ s :=
(assume s, ennreal.coe_to_nnreal $ hμ _),
have to_nnreal_ν : ∀s, ((ν s).to_nnreal : ℝ≥0∞) = ν s :=
(assume s, ennreal.coe_to_nnreal $ hν _),
have d_empty : d ∅ = 0,
{ change _ - _ = _, rw [measure_empty, measure_empty, sub_self] },
have d_split : ∀s t, measurable_set s → measurable_set t →
d s = d (s \ t) + d (s ∩ t),
{ assume s t hs ht,
simp only [d],
rw [← measure_inter_add_diff s ht, ← measure_inter_add_diff s ht,
ennreal.to_nnreal_add (hμ _) (hμ _), ennreal.to_nnreal_add (hν _) (hν _),
nnreal.coe_add, nnreal.coe_add],
simp only [sub_eq_add_neg, neg_add],
abel },
have d_Union : ∀(s : ℕ → set α), monotone s →
tendsto (λn, d (s n)) at_top (𝓝 (d (⋃n, s n))),
{ assume s hm,
refine tendsto.sub _ _;
refine (nnreal.tendsto_coe.2 $ (ennreal.tendsto_to_nnreal _).comp $
tendsto_measure_Union hm),
exact hμ _,
exact hν _ },
have d_Inter : ∀(s : ℕ → set α), (∀n, measurable_set (s n)) → (∀n m, n ≤ m → s m ⊆ s n) →
tendsto (λn, d (s n)) at_top (𝓝 (d (⋂n, s n))),
{ assume s hs hm,
refine tendsto.sub _ _;
refine (nnreal.tendsto_coe.2 $ (ennreal.tendsto_to_nnreal $ _).comp $
tendsto_measure_Inter hs hm _),
exacts [hμ _, ⟨0, hμ _⟩, hν _, ⟨0, hν _⟩] },
have bdd_c : bdd_above c,
{ use (μ univ).to_nnreal,
rintros r ⟨s, hs, rfl⟩,
refine le_trans (sub_le_self _ $ nnreal.coe_nonneg _) _,
rw [nnreal.coe_le_coe, ← ennreal.coe_le_coe, to_nnreal_μ, to_nnreal_μ],
exact measure_mono (subset_univ _) },
have c_nonempty : c.nonempty := nonempty.image _ ⟨_, measurable_set.empty⟩,
have d_le_γ : ∀s, measurable_set s → d s ≤ γ := assume s hs, le_cSup bdd_c ⟨s, hs, rfl⟩,
have : ∀n:ℕ, ∃s : set α, measurable_set s ∧ γ - (1/2)^n < d s,
{ assume n,
have : γ - (1/2)^n < γ := sub_lt_self γ (pow_pos (half_pos zero_lt_one) n),
rcases exists_lt_of_lt_cSup c_nonempty this with ⟨r, ⟨s, hs, rfl⟩, hlt⟩,
exact ⟨s, hs, hlt⟩ },
rcases classical.axiom_of_choice this with ⟨e, he⟩,
change ℕ → set α at e,
have he₁ : ∀n, measurable_set (e n) := assume n, (he n).1,
have he₂ : ∀n, γ - (1/2)^n < d (e n) := assume n, (he n).2,
let f : ℕ → ℕ → set α := λn m, (finset.Ico n (m + 1)).inf e,
have hf : ∀n m, measurable_set (f n m),
{ assume n m,
simp only [f, finset.inf_eq_infi],
exact measurable_set.bInter (to_countable _) (assume i _, he₁ _) },
have f_subset_f : ∀{a b c d}, a ≤ b → c ≤ d → f a d ⊆ f b c,
{ assume a b c d hab hcd,
dsimp only [f],
rw [finset.inf_eq_infi, finset.inf_eq_infi],
exact bInter_subset_bInter_left (finset.Ico_subset_Ico hab $ nat.succ_le_succ hcd) },
have f_succ : ∀n m, n ≤ m → f n (m + 1) = f n m ∩ e (m + 1),
{ assume n m hnm,
have : n ≤ m + 1 := le_of_lt (nat.succ_le_succ hnm),
simp only [f],
rw [nat.Ico_succ_right_eq_insert_Ico this, finset.inf_insert, set.inter_comm],
refl },
have le_d_f : ∀n m, m ≤ n → γ - 2 * ((1 / 2) ^ m) + (1 / 2) ^ n ≤ d (f m n),
{ assume n m h,
refine nat.le_induction _ _ n h,
{ have := he₂ m,
simp only [f],
rw [nat.Ico_succ_singleton, finset.inf_singleton],
linarith },
{ assume n (hmn : m ≤ n) ih,
have : γ + (γ - 2 * (1 / 2)^m + (1 / 2) ^ (n + 1)) ≤ γ + d (f m (n + 1)),
{ calc γ + (γ - 2 * (1 / 2)^m + (1 / 2) ^ (n+1)) ≤
γ + (γ - 2 * (1 / 2)^m + ((1 / 2) ^ n - (1/2)^(n+1))) :
begin
refine add_le_add_left (add_le_add_left _ _) γ,
simp only [pow_add, pow_one, le_sub_iff_add_le],
linarith
end
... = (γ - (1 / 2)^(n+1)) + (γ - 2 * (1 / 2)^m + (1 / 2)^n) :
by simp only [sub_eq_add_neg]; abel
... ≤ d (e (n + 1)) + d (f m n) : add_le_add (le_of_lt $ he₂ _) ih
... ≤ d (e (n + 1)) + d (f m n \ e (n + 1)) + d (f m (n + 1)) :
by rw [f_succ _ _ hmn, d_split (f m n) (e (n + 1)) (hf _ _) (he₁ _), add_assoc]
... = d (e (n + 1) ∪ f m n) + d (f m (n + 1)) :
begin
rw [d_split (e (n + 1) ∪ f m n) (e (n + 1)),
union_diff_left, union_inter_cancel_left],
abel,
exact (he₁ _).union (hf _ _),
exact (he₁ _)
end
... ≤ γ + d (f m (n + 1)) :
add_le_add_right (d_le_γ _ $ (he₁ _).union (hf _ _)) _ },
exact (add_le_add_iff_left γ).1 this } },
let s := ⋃ m, ⋂n, f m n,
have γ_le_d_s : γ ≤ d s,
{ have hγ : tendsto (λm:ℕ, γ - 2 * (1/2)^m) at_top (𝓝 γ),
{ suffices : tendsto (λm:ℕ, γ - 2 * (1/2)^m) at_top (𝓝 (γ - 2 * 0)),
{ simpa only [mul_zero, tsub_zero] },
exact (tendsto_const_nhds.sub $ tendsto_const_nhds.mul $
tendsto_pow_at_top_nhds_0_of_lt_1
(le_of_lt $ half_pos $ zero_lt_one) (half_lt_self zero_lt_one)) },
have hd : tendsto (λm, d (⋂n, f m n)) at_top (𝓝 (d (⋃ m, ⋂ n, f m n))),
{ refine d_Union _ _,
exact assume n m hnm, subset_Inter
(assume i, subset.trans (Inter_subset (f n) i) $ f_subset_f hnm $ le_rfl) },
refine le_of_tendsto_of_tendsto' hγ hd (assume m, _),
have : tendsto (λn, d (f m n)) at_top (𝓝 (d (⋂ n, f m n))),
{ refine d_Inter _ _ _,
{ assume n, exact hf _ _ },
{ assume n m hnm, exact f_subset_f le_rfl hnm } },
refine ge_of_tendsto this (eventually_at_top.2 ⟨m, assume n hmn, _⟩),
change γ - 2 * (1 / 2) ^ m ≤ d (f m n),
refine le_trans _ (le_d_f _ _ hmn),
exact le_add_of_le_of_nonneg le_rfl (pow_nonneg (le_of_lt $ half_pos $ zero_lt_one) _) },
have hs : measurable_set s :=
measurable_set.Union (assume n, measurable_set.Inter (assume m, hf _ _)),
refine ⟨s, hs, _, _⟩,
{ assume t ht hts,
have : 0 ≤ d t := ((add_le_add_iff_left γ).1 $
calc γ + 0 ≤ d s : by rw [add_zero]; exact γ_le_d_s
... = d (s \ t) + d t : by rw [d_split _ _ hs ht, inter_eq_self_of_subset_right hts]
... ≤ γ + d t : add_le_add (d_le_γ _ (hs.diff ht)) le_rfl),
rw [← to_nnreal_μ, ← to_nnreal_ν, ennreal.coe_le_coe, ← nnreal.coe_le_coe],
simpa only [d, le_sub_iff_add_le, zero_add] using this },
{ assume t ht hts,
have : d t ≤ 0,
exact ((add_le_add_iff_left γ).1 $
calc γ + d t ≤ d s + d t : add_le_add γ_le_d_s le_rfl
... = d (s ∪ t) :
by rw [d_split _ _ (hs.union ht) ht, union_diff_right, union_inter_cancel_right,
(subset_compl_iff_disjoint_left.1 hts).sdiff_eq_left]
... ≤ γ + 0 : by rw [add_zero]; exact d_le_γ _ (hs.union ht)),
rw [← to_nnreal_μ, ← to_nnreal_ν, ennreal.coe_le_coe, ← nnreal.coe_le_coe],
simpa only [d, sub_le_iff_le_add, zero_add] using this }
end
end measure_theory
|
e329203f0cb8b9befaac117f9a98c59972ea3361 | a51edd9a1700339fa6dc7dc428eb5dfa3994b8bc | /src/misc/encodable2.lean | d80c2824073ab357b2d4be7037b14026e031ca35 | [] | no_license | avigad/formal_logic | 83f5c0534b3e9e7da53eff01bb82289daad65555 | 59d7fe7cb7a7927fb72d89d4fd40965bcd769349 | refs/heads/master | 1,585,302,642,116 | 1,541,000,469,000 | 1,541,000,469,000 | 146,376,915 | 1 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 4,521 | lean | import data.equiv.encodable data.equiv.list data.fin data.finset data.fintype
/-
`Wfin α ar` is the type of finitely branching trees with labels from α, where
a node labeled `a` has `ar a` children.
-/
inductive Wfin {α : Type*} (ar : α → ℕ)
| mk (a : α) (f : fin (ar a) → Wfin) : Wfin
namespace Wfin
variables {α : Type*} {ar : α → ℕ}
def depth : Wfin ar → ℕ
| ⟨a, f⟩ := finset.sup finset.univ (λ n, depth (f n)) + 1
def not_depth_le_zero (t : Wfin ar) : ¬ t.depth ≤ 0 :=
by { cases t, apply nat.not_succ_le_zero }
lemma depth_lt_depth_mk (a : α) (f : fin (ar a) → Wfin ar) (i : fin (ar a)) :
depth (f i) < depth ⟨a, f⟩ :=
nat.lt_succ_of_le (finset.le_sup (finset.mem_univ i))
end Wfin
/-
Show `Wfin` types are encodable.
-/
namespace encodable
@[reducible] private def Wfin' {α : Type*} (ar : α → ℕ) (n : ℕ) :=
{ t : Wfin ar // t.depth ≤ n}
variables {α : Type*} {ar : α → ℕ}
private def encodable_zero : encodable (Wfin' ar 0) :=
let f : Wfin' ar 0 → empty := λ ⟨x, h⟩, absurd h (Wfin.not_depth_le_zero _),
finv : empty → Wfin' ar 0 := by { intro x, cases x} in
have ∀ x, finv (f x) = x, from λ ⟨x, h⟩, absurd h (Wfin.not_depth_le_zero _),
encodable.of_left_inverse f finv this
private def f (n : ℕ) : Wfin' ar (n + 1) → Σ a : α, fin (ar a) → Wfin' ar n
| ⟨t, h⟩ :=
begin
cases t with a f,
have h₀ : ∀ i : fin (ar a), Wfin.depth (f i) ≤ n,
from λ i, nat.le_of_lt_succ (lt_of_lt_of_le (Wfin.depth_lt_depth_mk a f i) h),
exact ⟨a, λ i : fin (ar a), ⟨f i, h₀ i⟩⟩
end
private def finv (n : ℕ) :
(Σ a : α, fin (ar a) → Wfin' ar n) → Wfin' ar (n + 1)
| ⟨a, f⟩ :=
let f' := λ i : fin (ar a), (f i).val in
have Wfin.depth ⟨a, f'⟩ ≤ n + 1,
from add_le_add_right (finset.sup_le (λ b h, (f b).2)) 1,
⟨⟨a, f'⟩, this⟩
variable [encodable α]
private def encodable_succ (n : nat) (h : encodable (Wfin' ar n)) :
encodable (Wfin' ar (n + 1)) :=
encodable.of_left_inverse (f n) (finv n)
(by { intro t, cases t with t h, cases t with a fn, reflexivity })
instance : encodable (Wfin ar) :=
begin
haveI h' : Π n, encodable (Wfin' ar n) :=
λ n, nat.rec_on n encodable_zero encodable_succ,
let f : Wfin ar → Σ n, Wfin' ar n := λ t, ⟨t.depth, ⟨t, le_refl _⟩⟩,
let finv : (Σ n, Wfin' ar n) → Wfin ar := λ p, p.2.1,
have : ∀ t, finv (f t) = t, from λ t, rfl,
exact encodable.of_left_inverse f finv this
end
end encodable
/-
Make it easier to construct funtions from a small `fin`.
-/
namespace fin
variable {α : Type*}
def mk_fn0 : fin 0 → α
| ⟨_, h⟩ := absurd h dec_trivial
def mk_fn1 (t : α) : fin 1 → α
| ⟨0, _⟩ := t
| ⟨n+1, h⟩ := absurd h dec_trivial
def mk_fn2 (s t : α) : fin 2 → α
| ⟨0, _⟩ := s
| ⟨1, _⟩ := t
| ⟨n+2, h⟩ := absurd h dec_trivial
attribute [simp] mk_fn0 mk_fn1 mk_fn2
end fin
/-
Show propositional formulas are encodable.
-/
inductive prop_form (α : Type*)
| var : α → prop_form
| not : prop_form → prop_form
| and : prop_form → prop_form → prop_form
| or : prop_form → prop_form → prop_form
namespace prop_form
private def constructors (α : Type*) := α ⊕ unit ⊕ unit ⊕ unit
local notation `cvar` a := sum.inl a
local notation `cnot` := sum.inr (sum.inl unit.star)
local notation `cand` := sum.inr (sum.inr (sum.inr unit.star))
local notation `cor` := sum.inr (sum.inr (sum.inl unit.star))
@[simp]
private def arity (α : Type*) : constructors α → nat
| (cvar a) := 0
| cnot := 1
| cand := 2
| cor := 2
variable {α : Type*}
private def f : prop_form α → Wfin (arity α)
| (var a) := ⟨cvar a, fin.mk_fn0⟩
| (not p) := ⟨cnot, fin.mk_fn1 (f p)⟩
| (and p q) := ⟨cand, fin.mk_fn2 (f p) (f q)⟩
| (or p q) := ⟨cor, fin.mk_fn2 (f p) (f q)⟩
private def finv : Wfin (arity α) → prop_form α
| ⟨cvar a, fn⟩ := var a
| ⟨cnot, fn⟩ := not (finv (fn ⟨0, dec_trivial⟩))
| ⟨cand, fn⟩ := and (finv (fn ⟨0, dec_trivial⟩)) (finv (fn ⟨1, dec_trivial⟩))
| ⟨cor, fn⟩ := or (finv (fn ⟨0, dec_trivial⟩)) (finv (fn ⟨1, dec_trivial⟩))
instance [encodable α] : encodable (prop_form α) :=
begin
haveI : encodable (constructors α) :=
by { unfold constructors, apply_instance },
exact encodable.of_left_inverse f finv
(by { intro p, induction p; simp [f, finv, *] })
end
end prop_form
|
dec29c91aa5e851be1fae7046d45fad5ae148967 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/ring_theory/ideal/norm.lean | 8cb9be9f0ad18b873a3b304d701bad38bd70c219 | [
"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 | 17,638 | lean | /-
Copyright (c) 2022 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen, Alex J. Best
-/
import data.finsupp.fintype
import data.int.absolute_value
import data.int.associated
import data.matrix.notation
import data.zmod.quotient
import linear_algebra.free_module.determinant
import linear_algebra.free_module.ideal_quotient
import linear_algebra.free_module.pid
import linear_algebra.isomorphisms
import ring_theory.dedekind_domain.ideal
import ring_theory.norm
/-!
# Ideal norms
This file defines the absolute ideal norm `ideal.abs_norm (I : ideal R) : ℕ` as the cardinality of
the quotient `R ⧸ I` (setting it to 0 if the cardinality is infinite).
## Main definitions
* `submodule.card_quot (S : submodule R M)`: the cardinality of the quotient `M ⧸ S`, in `ℕ`.
This maps `⊥` to `0` and `⊤` to `1`.
* `ideal.abs_norm (I : ideal R)`: the absolute ideal norm, defined as
the cardinality of the quotient `R ⧸ I`, as a bundled monoid-with-zero homomorphism.
## Main results
* `map_mul ideal.abs_norm`: multiplicativity of the ideal norm is bundled in
the definition of `ideal.abs_norm`
* `ideal.nat_abs_det_basis_change`: the ideal norm is given by the determinant
of the basis change matrix
* `ideal.abs_norm_span_singleton`: the ideal norm of a principal ideal is the
norm of its generator
## TODO
Define the relative norm.
-/
open_locale big_operators
namespace submodule
variables {R M : Type*} [ring R] [add_comm_group M] [module R M]
section
/-- The cardinality of `(M ⧸ S)`, if `(M ⧸ S)` is finite, and `0` otherwise.
This is used to define the absolute ideal norm `ideal.abs_norm`.
-/
noncomputable def card_quot (S : submodule R M) : ℕ :=
add_subgroup.index S.to_add_subgroup
@[simp] lemma card_quot_apply (S : submodule R M) [fintype (M ⧸ S)] :
card_quot S = fintype.card (M ⧸ S) :=
add_subgroup.index_eq_card _
variables (R M)
@[simp] lemma card_quot_bot [infinite M] : card_quot (⊥ : submodule R M) = 0 :=
add_subgroup.index_bot.trans nat.card_eq_zero_of_infinite
@[simp] lemma card_quot_top : card_quot (⊤ : submodule R M) = 1 :=
add_subgroup.index_top
variables {R M}
@[simp] lemma card_quot_eq_one_iff {P : submodule R M} : card_quot P = 1 ↔ P = ⊤ :=
add_subgroup.index_eq_one.trans (by simp [set_like.ext_iff])
end
end submodule
variables {S : Type*} [comm_ring S] [is_domain S]
open submodule
/-- Multiplicity of the ideal norm, for coprime ideals.
This is essentially just a repackaging of the Chinese Remainder Theorem.
-/
lemma card_quot_mul_of_coprime [is_dedekind_domain S] [module.free ℤ S] [module.finite ℤ S]
{I J : ideal S} (coprime : I ⊔ J = ⊤) : card_quot (I * J) = card_quot I * card_quot J :=
begin
let b := module.free.choose_basis ℤ S,
casesI is_empty_or_nonempty (module.free.choose_basis_index ℤ S),
{ haveI : subsingleton S := function.surjective.subsingleton b.repr.to_equiv.symm.surjective,
nontriviality S,
exfalso,
exact not_nontrivial_iff_subsingleton.mpr ‹subsingleton S› ‹nontrivial S› },
haveI : infinite S := infinite.of_surjective _ b.repr.to_equiv.surjective,
by_cases hI : I = ⊥,
{ rw [hI, submodule.bot_mul, card_quot_bot, zero_mul] },
by_cases hJ : J = ⊥,
{ rw [hJ, submodule.mul_bot, card_quot_bot, mul_zero] },
have hIJ : I * J ≠ ⊥ := mt ideal.mul_eq_bot.mp (not_or hI hJ),
letI := classical.dec_eq (module.free.choose_basis_index ℤ S),
letI := I.fintype_quotient_of_free_of_ne_bot hI,
letI := J.fintype_quotient_of_free_of_ne_bot hJ,
letI := (I * J).fintype_quotient_of_free_of_ne_bot hIJ,
rw [card_quot_apply, card_quot_apply, card_quot_apply,
fintype.card_eq.mpr ⟨(ideal.quotient_mul_equiv_quotient_prod I J coprime).to_equiv⟩,
fintype.card_prod]
end
/-- If the `d` from `ideal.exists_mul_add_mem_pow_succ` is unique, up to `P`,
then so are the `c`s, up to `P ^ (i + 1)`.
Inspired by [Neukirch], proposition 6.1 -/
lemma ideal.mul_add_mem_pow_succ_inj
(P : ideal S) {i : ℕ} (a d d' e e' : S) (a_mem : a ∈ P ^ i)
(e_mem : e ∈ P ^ (i + 1)) (e'_mem : e' ∈ P ^ (i + 1))
(h : d - d' ∈ P) : (a * d + e) - (a * d' + e') ∈ P ^ (i + 1) :=
begin
have : a * d - a * d' ∈ P ^ (i + 1),
{ convert ideal.mul_mem_mul a_mem h; simp [mul_sub, pow_succ, mul_comm] },
convert ideal.add_mem _ this (ideal.sub_mem _ e_mem e'_mem),
ring,
end
section P_prime
variables {P : ideal S} [P_prime : P.is_prime] (hP : P ≠ ⊥)
include P_prime hP
/-- If `a ∈ P^i \ P^(i+1)` and `c ∈ P^i`, then `a * d + e = c` for `e ∈ P^(i+1)`.
`ideal.mul_add_mem_pow_succ_unique` shows the choice of `d` is unique, up to `P`.
Inspired by [Neukirch], proposition 6.1 -/
lemma ideal.exists_mul_add_mem_pow_succ [is_dedekind_domain S] {i : ℕ}
(a c : S) (a_mem : a ∈ P ^ i) (a_not_mem : a ∉ P ^ (i + 1)) (c_mem : c ∈ P ^ i) :
∃ (d : S) (e ∈ P ^ (i + 1)), a * d + e = c :=
begin
suffices eq_b : P ^ i = ideal.span {a} ⊔ P ^ (i + 1),
{ rw eq_b at c_mem,
simp only [mul_comm a],
exact ideal.mem_span_singleton_sup.mp c_mem },
refine (ideal.eq_prime_pow_of_succ_lt_of_le hP
(lt_of_le_of_ne le_sup_right _)
(sup_le (ideal.span_le.mpr (set.singleton_subset_iff.mpr a_mem))
(ideal.pow_succ_lt_pow hP i).le)).symm,
contrapose! a_not_mem with this,
rw this,
exact mem_sup.mpr ⟨a, mem_span_singleton_self a, 0, by simp, by simp⟩
end
lemma ideal.mem_prime_of_mul_mem_pow [is_dedekind_domain S]
{P : ideal S} [P_prime : P.is_prime] (hP : P ≠ ⊥) {i : ℕ}
{a b : S} (a_not_mem : a ∉ P ^ (i + 1))
(ab_mem : a * b ∈ P ^ (i + 1)) : b ∈ P :=
begin
simp only [← ideal.span_singleton_le_iff_mem, ← ideal.dvd_iff_le, pow_succ,
← ideal.span_singleton_mul_span_singleton] at a_not_mem ab_mem ⊢,
exact (prime_pow_succ_dvd_mul (ideal.prime_of_is_prime hP P_prime) ab_mem).resolve_left a_not_mem
end
/-- The choice of `d` in `ideal.exists_mul_add_mem_pow_succ` is unique, up to `P`.
Inspired by [Neukirch], proposition 6.1 -/
lemma ideal.mul_add_mem_pow_succ_unique [is_dedekind_domain S] {i : ℕ}
(a d d' e e' : S) (a_not_mem : a ∉ P ^ (i + 1))
(e_mem : e ∈ P ^ (i + 1)) (e'_mem : e' ∈ P ^ (i + 1))
(h : (a * d + e) - (a * d' + e') ∈ P ^ (i + 1)) : d - d' ∈ P :=
begin
have : e' - e ∈ P ^ (i + 1) := ideal.sub_mem _ e'_mem e_mem,
have h' : a * (d - d') ∈ P ^ (i + 1),
{ convert ideal.add_mem _ h (ideal.sub_mem _ e'_mem e_mem),
ring },
exact ideal.mem_prime_of_mul_mem_pow hP a_not_mem h'
end
/-- Multiplicity of the ideal norm, for powers of prime ideals. -/
lemma card_quot_pow_of_prime [is_dedekind_domain S] [module.finite ℤ S] [module.free ℤ S] {i : ℕ} :
card_quot (P ^ i) = card_quot P ^ i :=
begin
let b := module.free.choose_basis ℤ S,
classical,
induction i with i ih,
{ simp },
letI := ideal.fintype_quotient_of_free_of_ne_bot (P ^ i.succ) (pow_ne_zero _ hP),
letI := ideal.fintype_quotient_of_free_of_ne_bot (P ^ i) (pow_ne_zero _ hP),
letI := ideal.fintype_quotient_of_free_of_ne_bot P hP,
have : P ^ (i + 1) < P ^ i := ideal.pow_succ_lt_pow hP i,
suffices hquot : map (P ^ i.succ).mkq (P ^ i) ≃ S ⧸ P,
{ rw [pow_succ (card_quot P), ← ih, card_quot_apply (P ^ i.succ),
← card_quotient_mul_card_quotient (P ^ i) (P ^ i.succ) this.le,
card_quot_apply (P ^ i), card_quot_apply P],
congr' 1,
rw fintype.card_eq,
exact ⟨hquot⟩ },
choose a a_mem a_not_mem using set_like.exists_of_lt this,
choose f g hg hf using λ c (hc : c ∈ P ^ i),
ideal.exists_mul_add_mem_pow_succ hP a c a_mem a_not_mem hc,
choose k hk_mem hk_eq using λ c' (hc' : c' ∈ (map (mkq (P ^ i.succ)) (P ^ i))),
submodule.mem_map.mp hc',
refine equiv.of_bijective (λ c', quotient.mk' (f (k c' c'.prop) (hk_mem c' c'.prop))) ⟨_, _⟩,
{ rintros ⟨c₁', hc₁'⟩ ⟨c₂', hc₂'⟩ h,
rw [subtype.mk_eq_mk, ← hk_eq _ hc₁', ← hk_eq _ hc₂', mkq_apply, mkq_apply,
submodule.quotient.eq, ← hf _ (hk_mem _ hc₁'), ← hf _ (hk_mem _ hc₂')],
refine ideal.mul_add_mem_pow_succ_inj _ _ _ _ _ _ a_mem (hg _ _) (hg _ _) _,
simpa only [submodule.quotient.mk'_eq_mk, submodule.quotient.mk'_eq_mk, submodule.quotient.eq]
using h, },
{ intros d',
refine quotient.induction_on' d' (λ d, _),
have hd' := mem_map.mpr ⟨a * d, ideal.mul_mem_right d _ a_mem, rfl⟩,
refine ⟨⟨_, hd'⟩, _⟩,
simp only [submodule.quotient.mk'_eq_mk, ideal.quotient.mk_eq_mk, ideal.quotient.eq,
subtype.coe_mk],
refine ideal.mul_add_mem_pow_succ_unique hP a _ _ _ _ a_not_mem
(hg _ (hk_mem _ hd'))
(zero_mem _)
_,
rw [hf, add_zero],
exact (submodule.quotient.eq _).mp (hk_eq _ hd') }
end
end P_prime
/-- Multiplicativity of the ideal norm in number rings. -/
theorem card_quot_mul [is_dedekind_domain S] [module.free ℤ S] [module.finite ℤ S] (I J : ideal S) :
card_quot (I * J) = card_quot I * card_quot J :=
begin
let b := module.free.choose_basis ℤ S,
casesI is_empty_or_nonempty (module.free.choose_basis_index ℤ S),
{ haveI : subsingleton S := function.surjective.subsingleton b.repr.to_equiv.symm.surjective,
nontriviality S,
exfalso,
exact not_nontrivial_iff_subsingleton.mpr ‹subsingleton S› ‹nontrivial S›, },
haveI : infinite S := infinite.of_surjective _ b.repr.to_equiv.surjective,
exact unique_factorization_monoid.multiplicative_of_coprime card_quot I J
(card_quot_bot _ _)
(λ I J hI, by simp [ideal.is_unit_iff.mp hI, ideal.mul_top])
(λ I i hI, have ideal.is_prime I := ideal.is_prime_of_prime hI,
by exactI card_quot_pow_of_prime hI.ne_zero)
(λ I J hIJ, card_quot_mul_of_coprime (ideal.is_unit_iff.mp (hIJ _
(ideal.dvd_iff_le.mpr le_sup_left)
(ideal.dvd_iff_le.mpr le_sup_right))))
end
/-- The absolute norm of the ideal `I : ideal R` is the cardinality of the quotient `R ⧸ I`. -/
noncomputable def ideal.abs_norm [infinite S] [is_dedekind_domain S]
[module.free ℤ S] [module.finite ℤ S] :
ideal S →*₀ ℕ :=
{ to_fun := submodule.card_quot,
map_mul' := λ I J, by rw card_quot_mul,
map_one' := by rw [ideal.one_eq_top, card_quot_top],
map_zero' := by rw [ideal.zero_eq_bot, card_quot_bot] }
namespace ideal
variables [infinite S] [is_dedekind_domain S] [module.free ℤ S] [module.finite ℤ S]
lemma abs_norm_apply (I : ideal S) : abs_norm I = card_quot I := rfl
@[simp] lemma abs_norm_bot : abs_norm (⊥ : ideal S) = 0 :=
by rw [← ideal.zero_eq_bot, _root_.map_zero]
@[simp] lemma abs_norm_top : abs_norm (⊤ : ideal S) = 1 :=
by rw [← ideal.one_eq_top, _root_.map_one]
@[simp] lemma abs_norm_eq_one_iff {I : ideal S} : abs_norm I = 1 ↔ I = ⊤ :=
by rw [abs_norm_apply, card_quot_eq_one_iff]
/-- Let `e : S ≃ I` be an additive isomorphism (therefore a `ℤ`-linear equiv).
Then an alternative way to compute the norm of `I` is given by taking the determinant of `e`.
See `nat_abs_det_basis_change` for a more familiar formulation of this result. -/
theorem nat_abs_det_equiv (I : ideal S) {E : Type*} [add_equiv_class E S I] (e : E) :
int.nat_abs (linear_map.det
((submodule.subtype I).restrict_scalars ℤ ∘ₗ add_monoid_hom.to_int_linear_map (e : S →+ I))) =
ideal.abs_norm I :=
begin
-- `S ⧸ I` might be infinite if `I = ⊥`, but then `e` can't be an equiv.
by_cases hI : I = ⊥,
{ unfreezingI { subst hI },
have : (1 : S) ≠ 0 := one_ne_zero,
have : (1 : S) = 0 := equiv_like.injective e (subsingleton.elim _ _),
contradiction },
let ι := module.free.choose_basis_index ℤ S,
let b := module.free.choose_basis ℤ S,
casesI is_empty_or_nonempty ι,
{ nontriviality S,
exact (not_nontrivial_iff_subsingleton.mpr
(function.surjective.subsingleton b.repr.to_equiv.symm.surjective)
(by apply_instance)).elim },
-- Thus `(S ⧸ I)` is isomorphic to a product of `zmod`s, so it is a fintype.
letI := ideal.fintype_quotient_of_free_of_ne_bot I hI,
-- Use the Smith normal form to choose a nice basis for `I`.
letI := classical.dec_eq ι,
let a := I.smith_coeffs b hI,
let b' := I.ring_basis b hI,
let ab := I.self_basis b hI,
have ab_eq := I.self_basis_def b hI,
let e' : S ≃ₗ[ℤ] I := b'.equiv ab (equiv.refl _),
let f : S →ₗ[ℤ] S := (I.subtype.restrict_scalars ℤ).comp (e' : S →ₗ[ℤ] I),
let f_apply : ∀ x, f x = b'.equiv ab (equiv.refl _) x := λ x, rfl,
suffices : (linear_map.det f).nat_abs = ideal.abs_norm I,
{ calc (linear_map.det ((submodule.subtype I).restrict_scalars ℤ ∘ₗ _)).nat_abs
= (linear_map.det ((submodule.subtype I).restrict_scalars ℤ ∘ₗ
(↑(add_equiv.to_int_linear_equiv ↑e) : S →ₗ[ℤ] I))).nat_abs : rfl
... = (linear_map.det ((submodule.subtype I).restrict_scalars ℤ ∘ₗ _)).nat_abs :
int.nat_abs_eq_iff_associated.mpr (linear_map.associated_det_comp_equiv _ _ _)
... = abs_norm I : this },
have ha : ∀ i, f (b' i) = a i • b' i,
{ intro i, rw [f_apply, b'.equiv_apply, equiv.refl_apply, ab_eq] },
have mem_I_iff : ∀ x, x ∈ I ↔ ∀ i, a i ∣ b'.repr x i,
{ intro x, simp_rw [ab.mem_ideal_iff', ab_eq],
have : ∀ (c : ι → ℤ) i, b'.repr (∑ (j : ι), c j • a j • b' j) i = a i * c i,
{ intros c i,
simp only [← mul_action.mul_smul, b'.repr_sum_self, mul_comm] },
split,
{ rintro ⟨c, rfl⟩ i, exact ⟨c i, this c i⟩ },
{ rintros ha,
choose c hc using ha, exact ⟨c, b'.ext_elem (λ i, trans (hc i) (this c i).symm)⟩ } },
-- `det f` is equal to `∏ i, a i`,
letI := classical.dec_eq ι,
calc int.nat_abs (linear_map.det f)
= int.nat_abs (linear_map.to_matrix b' b' f).det : by rw linear_map.det_to_matrix
... = int.nat_abs (matrix.diagonal a).det : _
... = int.nat_abs (∏ i, a i) : by rw matrix.det_diagonal
... = ∏ i, int.nat_abs (a i) : map_prod int.nat_abs_hom a finset.univ
... = fintype.card (S ⧸ I) : _
... = abs_norm I : (submodule.card_quot_apply _).symm,
-- since `linear_map.to_matrix b' b' f` is the diagonal matrix with `a` along the diagonal.
{ congr, ext i j,
rw [linear_map.to_matrix_apply, ha, linear_equiv.map_smul, basis.repr_self, finsupp.smul_single,
smul_eq_mul, mul_one],
by_cases h : i = j,
{ rw [h, matrix.diagonal_apply_eq, finsupp.single_eq_same] },
{ rw [matrix.diagonal_apply_ne _ h, finsupp.single_eq_of_ne (ne.symm h)] } },
-- Now we map everything through the linear equiv `S ≃ₗ (ι → ℤ)`,
-- which maps `(S ⧸ I)` to `Π i, zmod (a i).nat_abs`.
haveI : ∀ i, ne_zero ((a i).nat_abs) := λ i,
⟨int.nat_abs_ne_zero_of_ne_zero (ideal.smith_coeffs_ne_zero b I hI i)⟩,
simp_rw [fintype.card_eq.mpr ⟨(ideal.quotient_equiv_pi_zmod I b hI).to_equiv⟩, fintype.card_pi,
zmod.card] ,
end
/-- Let `b` be a basis for `S` over `ℤ` and `bI` a basis for `I` over `ℤ` of the same dimension.
Then an alternative way to compute the norm of `I` is given by taking the determinant of `bI`
over `b`. -/
theorem nat_abs_det_basis_change {ι : Type*} [fintype ι] [decidable_eq ι]
(b : basis ι ℤ S) (I : ideal S) (bI : basis ι ℤ I) :
(b.det (coe ∘ bI)).nat_abs = ideal.abs_norm I :=
begin
let e := b.equiv bI (equiv.refl _),
calc (b.det ((submodule.subtype I).restrict_scalars ℤ ∘ bI)).nat_abs
= (linear_map.det ((submodule.subtype I).restrict_scalars ℤ ∘ₗ (e : S →ₗ[ℤ] I))).nat_abs
: by rw basis.det_comp_basis
... = _ : nat_abs_det_equiv I e
end
@[simp]
lemma abs_norm_span_singleton (r : S) :
abs_norm (span ({r} : set S)) = (algebra.norm ℤ r).nat_abs :=
begin
rw algebra.norm_apply,
by_cases hr : r = 0,
{ simp only [hr, ideal.span_zero, algebra.coe_lmul_eq_mul, eq_self_iff_true, ideal.abs_norm_bot,
linear_map.det_zero'', set.singleton_zero, _root_.map_zero, int.nat_abs_zero] },
letI := ideal.fintype_quotient_of_free_of_ne_bot (span {r}) (mt span_singleton_eq_bot.mp hr),
let b := module.free.choose_basis ℤ S,
rw [← nat_abs_det_equiv _ (b.equiv (basis_span_singleton b hr) (equiv.refl _))],
swap, apply_instance,
congr,
refine b.ext (λ i, _),
simp
end
lemma abs_norm_dvd_abs_norm_of_le {I J : ideal S} (h : J ≤ I) : I.abs_norm ∣ J.abs_norm :=
map_dvd abs_norm (dvd_iff_le.mpr h)
@[simp]
lemma abs_norm_span_insert (r : S) (s : set S) :
abs_norm (span (insert r s)) ∣ gcd (abs_norm (span s)) (algebra.norm ℤ r).nat_abs :=
(dvd_gcd_iff _ _ _).mpr
⟨abs_norm_dvd_abs_norm_of_le (span_mono (set.subset_insert _ _)),
trans
(abs_norm_dvd_abs_norm_of_le (span_mono (set.singleton_subset_iff.mpr (set.mem_insert _ _))))
(by rw abs_norm_span_singleton)⟩
lemma irreducible_of_irreducible_abs_norm {I : ideal S} (hI : irreducible I.abs_norm) :
irreducible I :=
irreducible_iff.mpr
⟨λ h, hI.not_unit (by simpa only [ideal.is_unit_iff, nat.is_unit_iff, abs_norm_eq_one_iff]
using h),
by rintro a b rfl; simpa only [ideal.is_unit_iff, nat.is_unit_iff, abs_norm_eq_one_iff]
using hI.is_unit_or_is_unit (_root_.map_mul abs_norm a b)⟩
lemma is_prime_of_irreducible_abs_norm {I : ideal S} (hI : irreducible I.abs_norm) :
I.is_prime :=
is_prime_of_prime (unique_factorization_monoid.irreducible_iff_prime.mp
(irreducible_of_irreducible_abs_norm hI))
lemma prime_of_irreducible_abs_norm_span {a : S} (ha : a ≠ 0)
(hI : irreducible (ideal.span ({a} : set S)).abs_norm) :
prime a :=
(ideal.span_singleton_prime ha).mp (is_prime_of_irreducible_abs_norm hI)
end ideal
|
d77caf7431a8f36d2ab9be8a8a3039a1f15a0824 | 4a092885406df4e441e9bb9065d9405dacb94cd8 | /src/for_mathlib/subgroup.lean | 3f4a8547f045f8fb5acfff39b773ad75442eb392 | [
"Apache-2.0"
] | permissive | semorrison/lean-perfectoid-spaces | 78c1572cedbfae9c3e460d8aaf91de38616904d8 | bb4311dff45791170bcb1b6a983e2591bee88a19 | refs/heads/master | 1,588,841,765,494 | 1,554,805,620,000 | 1,554,805,620,000 | 180,353,546 | 0 | 1 | null | 1,554,809,880,000 | 1,554,809,880,000 | null | UTF-8 | Lean | false | false | 2,251 | lean | import group_theory.subgroup
import algebra.pi_instances
import for_mathlib.submonoid
section
variables {α : Type*} [group α] {β : Type*} [group β]
@[to_additive is_add_subgroup.inter]
instance is_subgroup.inter (s₁ s₂ : set α) [is_subgroup s₁] [is_subgroup s₂] :
is_subgroup (s₁ ∩ s₂) :=
{ inv_mem := λ x hx, ⟨is_subgroup.inv_mem hx.1, is_subgroup.inv_mem hx.2⟩,
..is_submonoid.inter s₁ s₂ }
@[to_additive is_add_subgroup.prod]
instance is_subgroup.prod (s : set α) (t : set β) [is_subgroup s] [is_subgroup t] :
is_subgroup (s.prod t) :=
{ one_mem := by rw set.mem_prod; split; apply is_submonoid.one_mem,
mul_mem := by intros; rw set.mem_prod at *; split; apply is_submonoid.mul_mem; tauto,
inv_mem := by intros; rw set.mem_prod at *; split; apply is_subgroup.inv_mem; tauto }
end
namespace add_group
-- TODO(jmc): generalise using to_additive
variables {α : Type*} {β : Type*} [add_group α] [add_group β] (f : α → β) [is_add_group_hom f]
lemma image_closure (s : set α) : f '' closure s = closure (f '' s) :=
le_antisymm
(by rintros _ ⟨x, hx, rfl⟩; exact in_closure.rec_on hx
(λ a ha, mem_closure ⟨a, ha, rfl⟩)
(by {rw [is_add_monoid_hom.map_zero f]; apply is_add_submonoid.zero_mem, })
(by {intros, rw [is_add_group_hom.neg f]; apply is_add_subgroup.neg_mem, assumption })
(by {intros, rw [is_add_monoid_hom.map_add f]; apply is_add_submonoid.add_mem, assumption' }))
(closure_subset $ set.image_subset _ subset_closure)
end add_group
namespace add_monoid
-- TODO(jmc): generalise using to_additive
variables {α : Type*} [add_comm_monoid α] {β : Type*}
local attribute [instance] classical.prop_decidable
lemma sum_mem_closure (s : set α) (ι : finset β) (f : β → α) (h : ∀ i ∈ ι, f i ∈ s) :
ι.sum f ∈ add_monoid.closure s :=
begin
revert h,
apply finset.induction_on ι,
{ intros, rw finset.sum_empty, apply is_add_submonoid.zero_mem },
{ intros i ι' hi IH h,
rw finset.sum_insert hi,
apply is_add_submonoid.add_mem,
{ apply add_monoid.subset_closure,
apply h,
apply finset.mem_insert_self },
{ apply IH,
intros i' hi',
apply h,
apply finset.mem_insert_of_mem hi' } }
end
end add_monoid
|
3f8d7062b11913b871e772ad138289f1a3247636 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/number_theory/cyclotomic/gal.lean | 528db295c59e2b3bd354e0f9af64cfef3525886f | [
"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,227 | lean | /-
Copyright (c) 2022 Eric Rodriguez. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Rodriguez
-/
import number_theory.cyclotomic.primitive_roots
import field_theory.polynomial_galois_group
/-!
# Galois group of cyclotomic extensions
In this file, we show the relationship between the Galois group of `K(ζₙ)` and `(zmod n)ˣ`;
it is always a subgroup, and if the `n`th cyclotomic polynomial is irreducible, they are isomorphic.
## Main results
* `is_primitive_root.aut_to_pow_injective`: `is_primitive_root.aut_to_pow` is injective
in the case that it's considered over a cyclotomic field extension.
* `is_cyclotomic_extension.aut_equiv_pow`: If the `n`th cyclotomic polynomial is irreducible
in `K`, then `aut_to_pow` is a `mul_equiv` (for example, in `ℚ` and certain `𝔽ₚ`).
* `gal_X_pow_equiv_units_zmod`, `gal_cyclotomic_equiv_units_zmod`: Repackage `aut_equiv_pow` in
terms of `polynomial.gal`.
* `is_cyclotomic_extension.aut.comm_group`: Cyclotomic extensions are abelian.
## References
* https://kconrad.math.uconn.edu/blurbs/galoistheory/cyclotomic.pdf
## TODO
* We currently can get away with the fact that the power of a primitive root is a primitive root,
but the correct long-term solution for computing other explicit Galois groups is creating
`power_basis.map_conjugate`; but figuring out the exact correct assumptions + proof for this is
mathematically nontrivial. (Current thoughts: the correct condition is that the annihilating
ideal of both elements is equal. This may not hold in an ID, and definitely holds in an ICD.)
-/
variables {n : ℕ+} (K : Type*) [field K] {L : Type*} {μ : L}
open polynomial is_cyclotomic_extension
open_locale cyclotomic
namespace is_primitive_root
variables [comm_ring L] [is_domain L] (hμ : is_primitive_root μ n) [algebra K L]
[is_cyclotomic_extension {n} K L]
/-- `is_primitive_root.aut_to_pow` is injective in the case that it's considered over a cyclotomic
field extension. -/
lemma aut_to_pow_injective : function.injective $ hμ.aut_to_pow K :=
begin
intros f g hfg,
apply_fun units.val at hfg,
simp only [is_primitive_root.coe_aut_to_pow_apply, units.val_eq_coe] at hfg,
generalize_proofs hf' hg' at hfg,
have hf := hf'.some_spec,
have hg := hg'.some_spec,
generalize_proofs hζ at hf hg,
suffices : f hμ.to_roots_of_unity = g hμ.to_roots_of_unity,
{ apply alg_equiv.coe_alg_hom_injective,
apply (hμ.power_basis K).alg_hom_ext,
exact this },
rw zmod.eq_iff_modeq_nat at hfg,
refine (hf.trans _).trans hg.symm,
rw [←roots_of_unity.coe_pow _ hf'.some, ←roots_of_unity.coe_pow _ hg'.some],
congr' 1,
rw [pow_eq_pow_iff_modeq],
convert hfg,
rw [hμ.eq_order_of],
rw [←hμ.coe_to_roots_of_unity_coe] {occs := occurrences.pos [2]},
rw [order_of_units, order_of_subgroup]
end
end is_primitive_root
namespace is_cyclotomic_extension
variables [comm_ring L] [is_domain L] (hμ : is_primitive_root μ n) [algebra K L]
[is_cyclotomic_extension {n} K L]
/-- Cyclotomic extensions are abelian. -/
noncomputable def aut.comm_group : comm_group (L ≃ₐ[K] L) :=
((zeta_spec n K L).aut_to_pow_injective K).comm_group _
(map_one _) (map_mul _) (map_inv _) (map_div _) (map_pow _) (map_zpow _)
variables (h : irreducible (cyclotomic n K)) {K} (L)
include h
/-- The `mul_equiv` that takes an automorphism `f` to the element `k : (zmod n)ˣ` such that
`f μ = μ ^ k`. A stronger version of `is_primitive_root.aut_to_pow`. -/
@[simps] noncomputable def aut_equiv_pow : (L ≃ₐ[K] L) ≃* (zmod n)ˣ :=
let hζ := zeta_spec n K L,
hμ := λ t, hζ.pow_of_coprime _ (zmod.val_coe_unit_coprime t) in
{ inv_fun := λ t, (hζ.power_basis K).equiv_of_minpoly ((hμ t).power_basis K)
begin
haveI := is_cyclotomic_extension.ne_zero' n K L,
simp only [is_primitive_root.power_basis_gen],
have hr := is_primitive_root.minpoly_eq_cyclotomic_of_irreducible
((zeta_spec n K L).pow_of_coprime _ (zmod.val_coe_unit_coprime t)) h,
exact ((zeta_spec n K L).minpoly_eq_cyclotomic_of_irreducible h).symm.trans hr
end,
left_inv := λ f, begin
simp only [monoid_hom.to_fun_eq_coe],
apply alg_equiv.coe_alg_hom_injective,
apply (hζ.power_basis K).alg_hom_ext,
simp only [alg_equiv.coe_alg_hom, alg_equiv.map_pow],
rw power_basis.equiv_of_minpoly_gen,
simp only [is_primitive_root.power_basis_gen, is_primitive_root.aut_to_pow_spec],
end,
right_inv := λ x, begin
simp only [monoid_hom.to_fun_eq_coe],
generalize_proofs _ _ h,
have key := hζ.aut_to_pow_spec K ((hζ.power_basis K).equiv_of_minpoly
((hμ x).power_basis K) h),
have := (hζ.power_basis K).equiv_of_minpoly_gen ((hμ x).power_basis K) h,
rw hζ.power_basis_gen K at this,
rw [this, is_primitive_root.power_basis_gen] at key,
rw ← hζ.coe_to_roots_of_unity_coe at key {occs := occurrences.pos [1, 5]},
simp only [←coe_coe, ←roots_of_unity.coe_pow] at key,
replace key := roots_of_unity.coe_injective key,
rw [pow_eq_pow_iff_modeq, ←order_of_subgroup, ←order_of_units, hζ.coe_to_roots_of_unity_coe,
←(zeta_spec n K L).eq_order_of, ←zmod.eq_iff_modeq_nat] at key,
simp only [zmod.nat_cast_val, zmod.cast_id', id.def] at key,
exact units.ext key
end,
.. (zeta_spec n K L).aut_to_pow K }
include hμ
variables {L}
/-- Maps `μ` to the `alg_equiv` that sends `is_cyclotomic_extension.zeta` to `μ`. -/
noncomputable def from_zeta_aut : L ≃ₐ[K] L :=
let hζ := (zeta_spec n K L).eq_pow_of_pow_eq_one hμ.pow_eq_one n.pos in
(aut_equiv_pow L h).symm $ zmod.unit_of_coprime hζ.some $
((zeta_spec n K L).pow_iff_coprime n.pos hζ.some).mp $ hζ.some_spec.some_spec.symm ▸ hμ
lemma from_zeta_aut_spec : from_zeta_aut hμ h (zeta n K L) = μ :=
begin
simp_rw [from_zeta_aut, aut_equiv_pow_symm_apply],
generalize_proofs _ hζ h _ hμ _,
rw [←hζ.power_basis_gen K] {occs := occurrences.pos [4]},
rw [power_basis.equiv_of_minpoly_gen, hμ.power_basis_gen K],
convert h.some_spec.some_spec,
exact zmod.val_cast_of_lt h.some_spec.some
end
end is_cyclotomic_extension
section gal
variables [field L] (hμ : is_primitive_root μ n) [algebra K L]
[is_cyclotomic_extension {n} K L] (h : irreducible (cyclotomic n K)) {K}
/-- `is_cyclotomic_extension.aut_equiv_pow` repackaged in terms of `gal`. Asserts that the
Galois group of `cyclotomic n K` is equivalent to `(zmod n)ˣ` if `cyclotomic n K` is irreducible in
the base field. -/
noncomputable def gal_cyclotomic_equiv_units_zmod :
(cyclotomic n K).gal ≃* (zmod n)ˣ :=
(alg_equiv.aut_congr (is_splitting_field.alg_equiv _ _)).symm.trans
(is_cyclotomic_extension.aut_equiv_pow L h)
/-- `is_cyclotomic_extension.aut_equiv_pow` repackaged in terms of `gal`. Asserts that the
Galois group of `X ^ n - 1` is equivalent to `(zmod n)ˣ` if `cyclotomic n K` is irreducible in the
base field. -/
noncomputable def gal_X_pow_equiv_units_zmod :
(X ^ (n : ℕ) - 1).gal ≃* (zmod n)ˣ :=
(alg_equiv.aut_congr (is_splitting_field.alg_equiv _ _)).symm.trans
(is_cyclotomic_extension.aut_equiv_pow L h)
end gal
|
dd5e0fb9fe622685bcad2546e8101fba9d5e5427 | bb31430994044506fa42fd667e2d556327e18dfe | /src/ring_theory/polynomial/vieta.lean | 529f82e0f12cc17d22b6ffc203c8d59bed767e96 | [
"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,910 | lean | /-
Copyright (c) 2020 Hanting Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Hanting Zhang
-/
import data.polynomial.splits
import ring_theory.mv_polynomial.symmetric
/-!
# Vieta's Formula
The main result is `multiset.prod_X_add_C_eq_sum_esymm`, which shows that the product of
linear terms `X + λ` with `λ` in a `multiset s` is equal to a linear combination of the
symmetric functions `esymm s`.
From this, we deduce `mv_polynomial.prod_X_add_C_eq_sum_esymm` which is the equivalent formula
for the product of linear terms `X + X i` with `i` in a `fintype σ` as a linear combination
of the symmetric polynomials `esymm σ R j`.
For `R` be an integral domain (so that `p.roots` is defined for any `p : R[X]` as a multiset),
we derive `polynomial.coeff_eq_esymm_roots_of_card`, the relationship between the coefficients and
the roots of `p` for a polynomial `p` that splits (i.e. having as many roots as its degree).
-/
open_locale big_operators polynomial
namespace multiset
open polynomial
section semiring
variables {R : Type*} [comm_semiring R]
/-- A sum version of Vieta's formula for `multiset`: the product of the linear terms `X + λ` where
`λ` runs through a multiset `s` is equal to a linear combination of the symmetric functions
`esymm s` of the `λ`'s .-/
lemma prod_X_add_C_eq_sum_esymm (s : multiset R) :
(s.map (λ r, X + C r)).prod =
∑ j in finset.range (s.card + 1), C (s.esymm j) * X ^ (s.card - j) :=
begin
classical,
rw [prod_map_add, antidiagonal_eq_map_powerset, map_map, ←bind_powerset_len, function.comp,
map_bind, sum_bind, finset.sum_eq_multiset_sum, finset.range_val, map_congr (eq.refl _)],
intros _ _,
rw [esymm, ←sum_hom', ←sum_map_mul_right, map_congr (eq.refl _)],
intros _ ht,
rw mem_powerset_len at ht,
simp [ht, map_const, prod_repeat, prod_hom', map_id', card_sub],
end
/-- Vieta's formula for the coefficients of the product of linear terms `X + λ` where `λ` runs
through a multiset `s` : the `k`th coefficient is the symmetric function `esymm (card s - k) s`. -/
lemma prod_X_add_C_coeff (s : multiset R) {k : ℕ} (h : k ≤ s.card) :
(s.map (λ r, X + C r)).prod.coeff k = s.esymm (s.card - k) :=
begin
convert polynomial.ext_iff.mp (prod_X_add_C_eq_sum_esymm s) k,
simp_rw [finset_sum_coeff, coeff_C_mul_X_pow],
rw finset.sum_eq_single_of_mem (s.card - k) _,
{ rw if_pos (nat.sub_sub_self h).symm, },
{ intros j hj1 hj2,
suffices : k ≠ card s - j,
{ rw if_neg this, },
{ intro hn,
rw [hn, nat.sub_sub_self (nat.lt_succ_iff.mp (finset.mem_range.mp hj1))] at hj2,
exact ne.irrefl hj2, }},
{ rw finset.mem_range,
exact nat.sub_lt_succ s.card k }
end
lemma prod_X_add_C_coeff' {σ} (s : multiset σ) (r : σ → R) {k : ℕ} (h : k ≤ s.card) :
(s.map (λ i, X + C (r i))).prod.coeff k = (s.map r).esymm (s.card - k) :=
by rw [← map_map (λ r, X + C r) r, prod_X_add_C_coeff]; rwa s.card_map r
lemma _root_.finset.prod_X_add_C_coeff {σ} (s : finset σ) (r : σ → R) {k : ℕ} (h : k ≤ s.card) :
(∏ i in s, (X + C (r i))).coeff k = ∑ t in s.powerset_len (s.card - k), ∏ i in t, r i :=
by { rw [finset.prod, prod_X_add_C_coeff' _ r h, finset.esymm_map_val], refl }
end semiring
section ring
variables {R : Type*} [comm_ring R]
lemma esymm_neg (s : multiset R) (k : ℕ) :
(map has_neg.neg s).esymm k = (-1) ^ k * esymm s k :=
begin
rw [esymm, esymm, ←multiset.sum_map_mul_left, multiset.powerset_len_map, multiset.map_map,
map_congr (eq.refl _)],
intros x hx,
rw [(by { exact (mem_powerset_len.mp hx).right.symm }), ←prod_repeat, ←multiset.map_const],
nth_rewrite 2 ←map_id' x,
rw [←prod_map_mul, map_congr (eq.refl _)],
exact λ z _, neg_one_mul z,
end
lemma prod_X_sub_C_eq_sum_esymm (s : multiset R) :
(s.map (λ t, X - C t)).prod =
∑ j in finset.range (s.card + 1), (-1) ^ j * (C (s.esymm j) * X ^ (s.card - j)) :=
begin
conv_lhs { congr, congr, funext, rw sub_eq_add_neg, rw ←map_neg C _, },
convert prod_X_add_C_eq_sum_esymm (map (λ t, -t) s) using 1,
{ rwa map_map, },
{ simp only [esymm_neg, card_map, mul_assoc, map_mul, map_pow, map_neg, map_one], },
end
lemma prod_X_sub_C_coeff (s : multiset R) {k : ℕ} (h : k ≤ s.card) :
(s.map (λ t, X - C t)).prod.coeff k = (-1) ^ (s.card - k) * s.esymm (s.card - k) :=
begin
conv_lhs { congr, congr, congr, funext, rw sub_eq_add_neg, rw ←map_neg C _, },
convert prod_X_add_C_coeff (map (λ t, -t) s) _ using 1,
{ rwa map_map, },
{ rwa [esymm_neg, card_map] },
{ rwa card_map },
end
/-- Vieta's formula for the coefficients and the roots of a polynomial over an integral domain
with as many roots as its degree. -/
theorem _root_.polynomial.coeff_eq_esymm_roots_of_card [is_domain R] {p : R[X]}
(hroots : p.roots.card = p.nat_degree) {k : ℕ} (h : k ≤ p.nat_degree) :
p.coeff k = p.leading_coeff * (-1) ^ (p.nat_degree - k) * p.roots.esymm (p.nat_degree - k) :=
begin
conv_lhs { rw ← C_leading_coeff_mul_prod_multiset_X_sub_C hroots },
rw [coeff_C_mul, mul_assoc], congr,
convert p.roots.prod_X_sub_C_coeff _ using 3; rw hroots, exact h,
end
/-- Vieta's formula for split polynomials over a field. -/
theorem _root_.polynomial.coeff_eq_esymm_roots_of_splits {F} [field F] {p : F[X]}
(hsplit : p.splits (ring_hom.id F)) {k : ℕ} (h : k ≤ p.nat_degree) :
p.coeff k = p.leading_coeff * (-1) ^ (p.nat_degree - k) * p.roots.esymm (p.nat_degree - k) :=
polynomial.coeff_eq_esymm_roots_of_card (splits_iff_card_roots.1 hsplit) h
end ring
end multiset
section mv_polynomial
open finset polynomial fintype
variables (R σ : Type*) [comm_semiring R] [fintype σ]
/-- A sum version of Vieta's formula for `mv_polynomial`: viewing `X i` as variables,
the product of linear terms `λ + X i` is equal to a linear combination of
the symmetric polynomials `esymm σ R j`. -/
lemma mv_polynomial.prod_C_add_X_eq_sum_esymm :
∏ i : σ, (X + C (mv_polynomial.X i)) =
∑ j in range (card σ + 1), (C (mv_polynomial.esymm σ R j) * X ^ (card σ - j)) :=
begin
let s := finset.univ.val.map (λ i : σ, mv_polynomial.X i),
rw (_ : card σ = s.card),
{ simp_rw [mv_polynomial.esymm_eq_multiset_esymm σ R, finset.prod_eq_multiset_prod],
convert multiset.prod_X_add_C_eq_sum_esymm s,
rwa multiset.map_map, },
{ rw multiset.card_map, refl, }
end
lemma mv_polynomial.prod_X_add_C_coeff (k : ℕ) (h : k ≤ card σ) :
(∏ i : σ, (X + C (mv_polynomial.X i))).coeff k = mv_polynomial.esymm σ R (card σ - k) :=
begin
let s := finset.univ.val.map (λ i, (mv_polynomial.X i : mv_polynomial σ R)),
rw (_ : card σ = s.card) at ⊢ h,
{ rw [mv_polynomial.esymm_eq_multiset_esymm σ R, finset.prod_eq_multiset_prod],
convert multiset.prod_X_add_C_coeff s h,
rwa multiset.map_map },
repeat { rw multiset.card_map, refl, },
end
end mv_polynomial
|
3f1fc3ca32ec8b1b8b11a9fe51dc69696ae5fd28 | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/logic/unnamed_398.lean | 095b67256c31f7e7a41d6bd14f2d32e4d598dd1d | [] | no_license | jamescheuk91/mathematics_in_lean | 09f1f87d2b0dce53464ff0cbe592c568ff59cf5e | 4452499264e2975bca2f42565c0925506ba5dda3 | refs/heads/master | 1,679,716,410,967 | 1,613,957,947,000 | 1,613,957,947,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 243 | lean | import data.real.basic
variables (f g : ℝ → ℝ)
-- BEGIN
example {c : ℝ} (mf : monotone f) (nnc : 0 ≤ c) :
monotone (λ x, c * f x) :=
sorry
example (mf : monotone f) (mg : monotone g) :
monotone (λ x, f (g x)) :=
sorry
-- END |
06f992f6a43aa584aa1537226050d1a9be0b2330 | d8820d2c92be8052d13f9c8f8c483a6e15c5f566 | /src/Stuff_for_reference/prop.lean | e1df2de641d269fa93fc059f0083ff8115ac8f32 | [] | 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 | 5,872 | lean | import tactic.ring
import tactic.linarith
-- Testing ground for theorems
theorem contrapositive
(P Q : Prop) (HPQ : P → Q) : ¬ Q → ¬ P :=
by {intros hnq hp, from hnq (HPQ hp)}
theorem and_transitive (P Q R : Prop) :
(P ∧ Q) ∧ (Q ∧ R) → (P ∧ R) :=
by {intro h, from ⟨h.left.left, h.right.right⟩}
theorem iff_symmetric
(P Q : Prop) : (P ↔ Q) ↔ (Q ↔ P) :=
by {split,
{intro h, rw h},
{intro h, rw h}
}
example
(P Q R : Prop) : (P ↔ Q) ∧ (Q ↔ R) → (P ↔ R) :=
by {
intro h, split,
{intro hp, rwa [←h.right, ←h.left]},
{intro hr, rwa [h.left, h.right]}
}
example
(P : Prop) : ¬ (¬ P) ↔ P :=
by {
split,
{intro h,
cases classical.em P,
{assumption},
{contradiction} },
{intros hp hnp, contradiction}
}
example
(P Q : Prop) : (¬ Q → ¬ P) → (P → Q) :=
by {
intros h hp,
apply classical.by_contradiction,
intro hnq,
from (h hnq) hp
}
example (P Q R : Prop) : (P → Q) ∧ (Q → R) → (P → R) :=
by {intros h hp, from h.right (h.left hp)}
example
(X : Type) (P Q : X → Prop) (a : X) (HP : ∀ x : X, P x) (HQ : ∀ x : X, Q x): P a ∧ Q a :=
by {from ⟨HP a, HQ a⟩}
example (P Q : Prop) : ¬ P → ¬ (P ∧ Q) :=
by {intro h, push_neg, left, assumption}
example
(P Q R : Prop) : P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R) :=
by {
split,
{intro h, cases h.right with Q R,
all_goals { {left, from ⟨h.left, Q⟩} <|> {right, from ⟨h.left, R⟩} } },
{intro h, cases h,
all_goals {split, from h.left},
{left, from h.right},
{right, from h.right} }
}
example
(P Q : Prop) : ¬ (P ∧ Q) → ¬ P ∨ ¬ Q :=
by {push_neg, intro, assumption}
example
(P Q : Prop) : ¬ (P ∧ ¬ Q) → (P → Q) :=
by {
push_neg, intros h hp, cases h,
{contradiction},
{assumption}
}
example
(P Q : Prop) : ¬ (P → Q) → (P ∧ ¬ Q) :=
by {push_neg, intro, assumption}
example
(P Q : Prop) : (P ∧ ¬ Q) → ¬ (P → Q) :=
by {push_neg, intro, assumption}
variables p q r : Prop
example : p ∨ q → q ∨ p :=
by {intro h, cases h, all_goals{{left, assumption} <|> {right, assumption}}}
example : ¬ (p ↔ ¬ p) :=
by {
intro h,
cases classical.em p with hp hnp,
{from (h.mp hp) hp},
{from hnp (h.mpr hnp)}
}
example : p ∨ ¬ p :=
by {
cases classical.em p,
all_goals { {left, assumption} <|> {right, assumption} }
}
example : (p → q) ↔ (¬ q → ¬ p) :=
by {
split,
all_goals {intros a b},
{intro h, from b (a h)},
{apply classical.by_contradiction, intro h, from (a h) b}
}
variables (X : Type) (P Q : X → Prop)
example : (∀ x, P x ∧ Q x) → (∀ x, Q x ∧ P x) :=
by {
intros h x,
split, all_goals { {from (h x).right} <|> {from (h x).left} }
}
example : (∃ x, P x ∨ Q x) → (∃ x, Q x ∨ P x) :=
by {
rintro ⟨x, hx⟩, existsi x,
cases hx,
all_goals { {left, assumption} <|> {right, assumption}}
}
example : (∀ x, P x) ∧ (∀ x, Q x) ↔ (∀ x, P x ∧ Q x) :=
by {
split,
{rintros ⟨hp, hq⟩ x, from ⟨hp x, hq x⟩},
{intro h, split,
all_goals {intro x, from (h x).left <|> from (h x).right} }
}
example : (∃ x, P x) ∨ (∃ x, Q x) ↔ (∃ x, P x ∨ Q x) :=
by {
split,
{intro h, repeat {rcases h with ⟨x, hx⟩},
all_goals {existsi x, {left, assumption} <|> {right, assumption} }
},
{rintro ⟨x, hx⟩, cases hx,
{left, existsi x, assumption},
{right, existsi x, assumption} }
}
example (α : Type) (p q : α → Prop) : (∀ x : α, p x ∧ q x) → ∀ x : α, p x :=
by {intros h x, from (h x).left}
example : ¬ (∀ x, P x) ↔ ∃ x, ¬ P x :=
by {push_neg, refl}
def even (a : ℤ) := ∃ b : ℤ, a = 2 * b
def odd (a : ℤ) := ∃ b : ℤ, a = 2 * b + 1
theorem timeseven
(a : ℤ) : even a → even (a * a) :=
begin
intro h,
have h1 : ∃ b : ℤ, a = 2 * b, exact h,
have h2 : ∃ b : ℤ, a * a = 2 * b,
cases h1 with hz ha,
existsi 2 * hz * hz,
rw ha,
have hc : 2 * hz = hz * 2,
rw mul_comm,
rw [hc, mul_assoc, mul_assoc, hc, mul_left_comm],
exact h2,
end
theorem timesodd
(a : ℤ) : odd a → odd (a * a) :=
begin
intro h,
have h1 : ∃ b : ℤ, a = 2 * b + 1, exact h,
have h2 : ∃ b : ℤ, a * a = 2 * b + 1,
cases h1 with x ha,
existsi 2 * x * x + 2 * x,
rw ha,
ring,
exact h2,
end
theorem eventimesodd
(a b : ℤ) : odd a ∧ even b → even (a * b) :=
begin
intro h,
cases h with ha hb,
have h1 : ∃ c : ℤ, a = 2 * c + 1, exact ha,
have h2 : ∃ c : ℤ, b = 2 * c, exact hb,
have h3 : ∃ c : ℤ, a * b = 2 * c,
cases h1 with x0 hk,
cases h2 with x1 hm,
existsi x1 * (2 * x0 + 1),
rw hk, rw hm,
ring,
exact h3,
end
theorem orderx (x y z : ℕ) (h : z > 0) : x > y → x * z > y * z :=
by {from (mul_lt_mul_right h).mpr}
theorem orderone (a b : ℕ) (ha : a > 1) : b > 1 → a * b > a :=
begin
intro h,
have hb : b > 0,
have h0 : 1 > 0,
simp,
exact trans h h0,
have hc : a = a * 1, simp,
rw hc,
have hd : 1 * b = b, simp,
rw mul_assoc,
rw hd,
rw mul_comm,
have he : a * 1 = 1 * a, simp,
rw he,
apply orderx,
have hf : 1 > 0, simp,
exact trans ha hf,
exact h,
end
theorem ordermul
(a b n : ℕ) (h1 : a > 1) (h2 : b > 1): n > a * b → n > a ∧ n > b :=
begin
intro h,
split,
have hab : a * b > a,
apply orderone, exact h1, exact h2,
exact trans h hab,
have hba : a * b > b,
rw mul_comm, apply orderone, exact h2, exact h1,
exact trans h hba,
end
example (P Q : Prop) : ((P → Q) → P) → P :=
begin
intro h,
cases classical.em P with hp hnp,
assumption,
have ha : ¬ P → ¬ (P → Q),
apply contrapositive,
assumption,
have hb : ¬ (P → Q) := ha hnp,
revert hb,
push_neg,
intro hc,
exact hc.left,
end
example (a b : ℕ) : a + a = b + b → a = b :=
by{ring, rw nat.mul_left_inj, intro; assumption, simp} |
5a7afce62fe0ac5bbbfdf026a2bd039fdf767dcf | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch2/ex0704.lean | c1204c90f65e9d6af256643553386ab65fb98ba7 | [] | no_license | Ailrun/Theorem_Proving_in_Lean | ae6a23f3c54d62d401314d6a771e8ff8b4132db2 | 2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68 | refs/heads/master | 1,609,838,270,467 | 1,586,846,743,000 | 1,586,846,743,000 | 240,967,761 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 284 | lean | namespace foo
def a : ℕ := 5
def f (x : ℕ) : ℕ := x + 7
def fa : ℕ := f a
namespace bar
def ffa : ℕ := f (f a)
#check fa
#check ffa
end bar
#check fa
#check bar.ffa
end foo
#check foo.fa
#check foo.bar.ffa
open foo
#check fa
#check bar.ffa
|
311ee4f2e08fd798fed9b14cb2f30254c779ca3b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/limits/functor_category.lean | ec1cb9b3364e825a66b5a2e95cbbb3621636b359 | [
"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 | 18,198 | 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.limits.preserves.limits
/-!
# (Co)limits in functor categories.
We show that if `D` has limits, then the functor category `C ⥤ D` also has limits
(`category_theory.limits.functor_category_has_limits`),
and the evaluation functors preserve limits
(`category_theory.limits.evaluation_preserves_limits`)
(and similarly for colimits).
We also show that `F : D ⥤ K ⥤ C` preserves (co)limits if it does so for each `k : K`
(`category_theory.limits.preserves_limits_of_evaluation` and
`category_theory.limits.preserves_colimits_of_evaluation`).
-/
open category_theory category_theory.category category_theory.functor
-- morphism levels before object levels. See note [category_theory universes].
universes w' w v₁ v₂ u₁ u₂ v v' u u'
namespace category_theory.limits
variables {C : Type u} [category.{v} C] {D : Type u'} [category.{v'} D]
variables {J : Type u₁} [category.{v₁} J] {K : Type u₂} [category.{v₂} K]
@[simp, reassoc]
lemma limit.lift_π_app (H : J ⥤ K ⥤ C) [has_limit H] (c : cone H) (j : J) (k : K) :
(limit.lift H c).app k ≫ (limit.π H j).app k = (c.π.app j).app k :=
congr_app (limit.lift_π c j) k
@[simp, reassoc]
lemma colimit.ι_desc_app (H : J ⥤ K ⥤ C) [has_colimit H] (c : cocone H) (j : J) (k : K) :
(colimit.ι H j).app k ≫ (colimit.desc H c).app k = (c.ι.app j).app k :=
congr_app (colimit.ι_desc c j) k
/--
The evaluation functors jointly reflect limits: that is, to show a cone is a limit of `F`
it suffices to show that each evaluation cone is a limit. In other words, to prove a cone is
limiting you can show it's pointwise limiting.
-/
def evaluation_jointly_reflects_limits {F : J ⥤ K ⥤ C} (c : cone F)
(t : Π (k : K), is_limit (((evaluation K C).obj k).map_cone c)) : is_limit c :=
{ lift := λ s,
{ app := λ k, (t k).lift ⟨s.X.obj k, whisker_right s.π ((evaluation K C).obj k)⟩,
naturality' := λ X Y f, (t Y).hom_ext $ λ j,
begin
rw [assoc, (t Y).fac _ j],
simpa using
((t X).fac_assoc ⟨s.X.obj X, whisker_right s.π ((evaluation K C).obj X)⟩ j _).symm,
end },
fac' := λ s j, nat_trans.ext _ _ $ funext $ λ k, (t k).fac _ j,
uniq' := λ s m w, nat_trans.ext _ _ $ funext $ λ x, (t x).hom_ext $ λ j,
(congr_app (w j) x).trans
((t x).fac ⟨s.X.obj _, whisker_right s.π ((evaluation K C).obj _)⟩ j).symm }
/--
Given a functor `F` and a collection of limit cones for each diagram `X ↦ F X k`, we can stitch
them together to give a cone for the diagram `F`.
`combined_is_limit` shows that the new cone is limiting, and `eval_combined` shows it is
(essentially) made up of the original cones.
-/
@[simps] def combine_cones (F : J ⥤ K ⥤ C) (c : Π (k : K), limit_cone (F.flip.obj k)) :
cone F :=
{ X :=
{ obj := λ k, (c k).cone.X,
map := λ k₁ k₂ f, (c k₂).is_limit.lift ⟨_, (c k₁).cone.π ≫ F.flip.map f⟩,
map_id' := λ k, (c k).is_limit.hom_ext (λ j, by { dsimp, simp }),
map_comp' := λ k₁ k₂ k₃ f₁ f₂, (c k₃).is_limit.hom_ext (λ j, by simp) },
π :=
{ app := λ j, { app := λ k, (c k).cone.π.app j },
naturality' := λ j₁ j₂ g, nat_trans.ext _ _ $ funext $ λ k, (c k).cone.π.naturality g } }
/-- The stitched together cones each project down to the original given cones (up to iso). -/
def evaluate_combined_cones (F : J ⥤ K ⥤ C) (c : Π (k : K), limit_cone (F.flip.obj k)) (k : K) :
((evaluation K C).obj k).map_cone (combine_cones F c) ≅ (c k).cone :=
cones.ext (iso.refl _) (by tidy)
/-- Stitching together limiting cones gives a limiting cone. -/
def combined_is_limit (F : J ⥤ K ⥤ C) (c : Π (k : K), limit_cone (F.flip.obj k)) :
is_limit (combine_cones F c) :=
evaluation_jointly_reflects_limits _
(λ k, (c k).is_limit.of_iso_limit (evaluate_combined_cones F c k).symm)
/--
The evaluation functors jointly reflect colimits: that is, to show a cocone is a colimit of `F`
it suffices to show that each evaluation cocone is a colimit. In other words, to prove a cocone is
colimiting you can show it's pointwise colimiting.
-/
def evaluation_jointly_reflects_colimits {F : J ⥤ K ⥤ C} (c : cocone F)
(t : Π (k : K), is_colimit (((evaluation K C).obj k).map_cocone c)) : is_colimit c :=
{ desc := λ s,
{ app := λ k, (t k).desc ⟨s.X.obj k, whisker_right s.ι ((evaluation K C).obj k)⟩,
naturality' := λ X Y f, (t X).hom_ext $ λ j,
begin
rw [(t X).fac_assoc _ j],
erw ← (c.ι.app j).naturality_assoc f,
erw (t Y).fac ⟨s.X.obj _, whisker_right s.ι _⟩ j,
dsimp,
simp,
end },
fac' := λ s j, nat_trans.ext _ _ $ funext $ λ k, (t k).fac _ j,
uniq' := λ s m w, nat_trans.ext _ _ $ funext $ λ x, (t x).hom_ext $ λ j,
(congr_app (w j) x).trans
((t x).fac ⟨s.X.obj _, whisker_right s.ι ((evaluation K C).obj _)⟩ j).symm }
/--
Given a functor `F` and a collection of colimit cocones for each diagram `X ↦ F X k`, we can stitch
them together to give a cocone for the diagram `F`.
`combined_is_colimit` shows that the new cocone is colimiting, and `eval_combined` shows it is
(essentially) made up of the original cocones.
-/
@[simps] def combine_cocones (F : J ⥤ K ⥤ C) (c : Π (k : K), colimit_cocone (F.flip.obj k)) :
cocone F :=
{ X :=
{ obj := λ k, (c k).cocone.X,
map := λ k₁ k₂ f, (c k₁).is_colimit.desc ⟨_, F.flip.map f ≫ (c k₂).cocone.ι⟩,
map_id' := λ k, (c k).is_colimit.hom_ext (λ j, by { dsimp, simp }),
map_comp' := λ k₁ k₂ k₃ f₁ f₂, (c k₁).is_colimit.hom_ext (λ j, by simp) },
ι :=
{ app := λ j, { app := λ k, (c k).cocone.ι.app j },
naturality' := λ j₁ j₂ g, nat_trans.ext _ _ $ funext $ λ k, (c k).cocone.ι.naturality g } }
/-- The stitched together cocones each project down to the original given cocones (up to iso). -/
def evaluate_combined_cocones
(F : J ⥤ K ⥤ C) (c : Π (k : K), colimit_cocone (F.flip.obj k)) (k : K) :
((evaluation K C).obj k).map_cocone (combine_cocones F c) ≅ (c k).cocone :=
cocones.ext (iso.refl _) (by tidy)
/-- Stitching together colimiting cocones gives a colimiting cocone. -/
def combined_is_colimit (F : J ⥤ K ⥤ C) (c : Π (k : K), colimit_cocone (F.flip.obj k)) :
is_colimit (combine_cocones F c) :=
evaluation_jointly_reflects_colimits _
(λ k, (c k).is_colimit.of_iso_colimit (evaluate_combined_cocones F c k).symm)
noncomputable theory
instance functor_category_has_limits_of_shape
[has_limits_of_shape J C] : has_limits_of_shape J (K ⥤ C) :=
{ has_limit := λ F, has_limit.mk
{ cone := combine_cones F (λ k, get_limit_cone _),
is_limit := combined_is_limit _ _ } }
instance functor_category_has_colimits_of_shape
[has_colimits_of_shape J C] : has_colimits_of_shape J (K ⥤ C) :=
{ has_colimit := λ F, has_colimit.mk
{ cocone := combine_cocones _ (λ k, get_colimit_cocone _),
is_colimit := combined_is_colimit _ _ } }
instance functor_category_has_limits_of_size [has_limits_of_size.{v₁ u₁} C] :
has_limits_of_size.{v₁ u₁} (K ⥤ C) := ⟨infer_instance⟩
instance functor_category_has_colimits_of_size [has_colimits_of_size.{v₁ u₁} C] :
has_colimits_of_size.{v₁ u₁} (K ⥤ C) := ⟨infer_instance⟩
instance evaluation_preserves_limits_of_shape [has_limits_of_shape J C] (k : K) :
preserves_limits_of_shape J ((evaluation K C).obj k) :=
{ preserves_limit :=
λ F, preserves_limit_of_preserves_limit_cone (combined_is_limit _ _) $
is_limit.of_iso_limit (limit.is_limit _)
(evaluate_combined_cones F _ k).symm }
/--
If `F : J ⥤ K ⥤ C` is a functor into a functor category which has a limit,
then the evaluation of that limit at `k` is the limit of the evaluations of `F.obj j` at `k`.
-/
def limit_obj_iso_limit_comp_evaluation [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) (k : K) :
(limit F).obj k ≅ limit (F ⋙ ((evaluation K C).obj k)) :=
preserves_limit_iso ((evaluation K C).obj k) F
@[simp, reassoc]
lemma limit_obj_iso_limit_comp_evaluation_hom_π
[has_limits_of_shape J C] (F : J ⥤ (K ⥤ C)) (j : J) (k : K) :
(limit_obj_iso_limit_comp_evaluation F k).hom ≫ limit.π (F ⋙ ((evaluation K C).obj k)) j =
(limit.π F j).app k :=
begin
dsimp [limit_obj_iso_limit_comp_evaluation],
simp,
end
@[simp, reassoc]
lemma limit_obj_iso_limit_comp_evaluation_inv_π_app
[has_limits_of_shape J C] (F : J ⥤ (K ⥤ C)) (j : J) (k : K):
(limit_obj_iso_limit_comp_evaluation F k).inv ≫ (limit.π F j).app k =
limit.π (F ⋙ ((evaluation K C).obj k)) j :=
begin
dsimp [limit_obj_iso_limit_comp_evaluation],
rw iso.inv_comp_eq,
simp,
end
@[simp, reassoc]
lemma limit_map_limit_obj_iso_limit_comp_evaluation_hom
[has_limits_of_shape J C] {i j : K} (F : J ⥤ K ⥤ C) (f : i ⟶ j) :
(limit F).map f ≫ (limit_obj_iso_limit_comp_evaluation _ _).hom =
(limit_obj_iso_limit_comp_evaluation _ _).hom ≫
lim_map (whisker_left _ ((evaluation _ _).map f)) :=
by { ext, dsimp, simp }
@[simp, reassoc]
lemma limit_obj_iso_limit_comp_evaluation_inv_limit_map
[has_limits_of_shape J C] {i j : K} (F : J ⥤ K ⥤ C) (f : i ⟶ j) :
(limit_obj_iso_limit_comp_evaluation _ _).inv ≫ (limit F).map f =
lim_map (whisker_left _ ((evaluation _ _).map f)) ≫
(limit_obj_iso_limit_comp_evaluation _ _).inv :=
by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv,
limit_map_limit_obj_iso_limit_comp_evaluation_hom]
@[ext]
lemma limit_obj_ext {H : J ⥤ K ⥤ C} [has_limits_of_shape J C]
{k : K} {W : C} {f g : W ⟶ (limit H).obj k}
(w : ∀ j, f ≫ (limits.limit.π H j).app k = g ≫ (limits.limit.π H j).app k) : f = g :=
begin
apply (cancel_mono (limit_obj_iso_limit_comp_evaluation H k).hom).1,
ext,
simpa using w j,
end
instance evaluation_preserves_colimits_of_shape [has_colimits_of_shape J C] (k : K) :
preserves_colimits_of_shape J ((evaluation K C).obj k) :=
{ preserves_colimit :=
λ F, preserves_colimit_of_preserves_colimit_cocone (combined_is_colimit _ _) $
is_colimit.of_iso_colimit (colimit.is_colimit _)
(evaluate_combined_cocones F _ k).symm }
/--
If `F : J ⥤ K ⥤ C` is a functor into a functor category which has a colimit,
then the evaluation of that colimit at `k` is the colimit of the evaluations of `F.obj j` at `k`.
-/
def colimit_obj_iso_colimit_comp_evaluation [has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) (k : K) :
(colimit F).obj k ≅ colimit (F ⋙ ((evaluation K C).obj k)) :=
preserves_colimit_iso ((evaluation K C).obj k) F
@[simp, reassoc]
lemma colimit_obj_iso_colimit_comp_evaluation_ι_inv
[has_colimits_of_shape J C] (F : J ⥤ (K ⥤ C)) (j : J) (k : K) :
colimit.ι (F ⋙ ((evaluation K C).obj k)) j ≫ (colimit_obj_iso_colimit_comp_evaluation F k).inv =
(colimit.ι F j).app k :=
begin
dsimp [colimit_obj_iso_colimit_comp_evaluation],
simp,
end
@[simp, reassoc]
lemma colimit_obj_iso_colimit_comp_evaluation_ι_app_hom
[has_colimits_of_shape J C] (F : J ⥤ (K ⥤ C)) (j : J) (k : K) :
(colimit.ι F j).app k ≫ (colimit_obj_iso_colimit_comp_evaluation F k).hom =
colimit.ι (F ⋙ ((evaluation K C).obj k)) j :=
begin
dsimp [colimit_obj_iso_colimit_comp_evaluation],
rw ←iso.eq_comp_inv,
simp,
end
@[simp, reassoc]
lemma colimit_obj_iso_colimit_comp_evaluation_inv_colimit_map
[has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) {i j : K} (f : i ⟶ j) :
(colimit_obj_iso_colimit_comp_evaluation _ _).inv ≫ (colimit F).map f =
colim_map (whisker_left _ ((evaluation _ _).map f)) ≫
(colimit_obj_iso_colimit_comp_evaluation _ _).inv :=
by { ext, dsimp, simp }
@[simp, reassoc]
lemma colimit_map_colimit_obj_iso_colimit_comp_evaluation_hom
[has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) {i j : K} (f : i ⟶ j) :
(colimit F).map f ≫ (colimit_obj_iso_colimit_comp_evaluation _ _).hom =
(colimit_obj_iso_colimit_comp_evaluation _ _).hom ≫
colim_map (whisker_left _ ((evaluation _ _).map f)) :=
by rw [← iso.inv_comp_eq, ← category.assoc, ← iso.eq_comp_inv,
colimit_obj_iso_colimit_comp_evaluation_inv_colimit_map]
@[ext]
lemma colimit_obj_ext {H : J ⥤ K ⥤ C} [has_colimits_of_shape J C]
{k : K} {W : C} {f g : (colimit H).obj k ⟶ W}
(w : ∀ j, (colimit.ι H j).app k ≫ f = (colimit.ι H j).app k ≫ g) : f = g :=
begin
apply (cancel_epi (colimit_obj_iso_colimit_comp_evaluation H k).inv).1,
ext,
simpa using w j,
end
instance evaluation_preserves_limits [has_limits C] (k : K) :
preserves_limits ((evaluation K C).obj k) :=
{ preserves_limits_of_shape := λ J 𝒥, by resetI; apply_instance }
/-- `F : D ⥤ K ⥤ C` preserves the limit of some `G : J ⥤ D` if it does for each `k : K`. -/
def preserves_limit_of_evaluation (F : D ⥤ K ⥤ C) (G : J ⥤ D)
(H : Π (k : K), preserves_limit G (F ⋙ (evaluation K C).obj k : D ⥤ C)) :
preserves_limit G F := ⟨λ c hc,
begin
apply evaluation_jointly_reflects_limits,
intro X,
haveI := H X,
change is_limit ((F ⋙ (evaluation K C).obj X).map_cone c),
exact preserves_limit.preserves hc,
end⟩
/-- `F : D ⥤ K ⥤ C` preserves limits of shape `J` if it does for each `k : K`. -/
def preserves_limits_of_shape_of_evaluation (F : D ⥤ K ⥤ C) (J : Type*) [category J]
(H : Π (k : K), preserves_limits_of_shape J (F ⋙ (evaluation K C).obj k)) :
preserves_limits_of_shape J F :=
⟨λ G, preserves_limit_of_evaluation F G (λ k, preserves_limits_of_shape.preserves_limit)⟩
/-- `F : D ⥤ K ⥤ C` preserves all limits if it does for each `k : K`. -/
def preserves_limits_of_evaluation (F : D ⥤ K ⥤ C)
(H : Π (k : K), preserves_limits_of_size.{w' w} (F ⋙ (evaluation K C).obj k)) :
preserves_limits_of_size.{w' w} F :=
⟨λ L hL, by exactI preserves_limits_of_shape_of_evaluation
F L (λ k, preserves_limits_of_size.preserves_limits_of_shape)⟩
/-- The constant functor `C ⥤ (D ⥤ C)` preserves limits. -/
instance preserves_limits_const : preserves_limits_of_size.{w' w} (const D : C ⥤ _) :=
preserves_limits_of_evaluation _ $ λ X, preserves_limits_of_nat_iso $ iso.symm $
const_comp_evaluation_obj _ _
instance evaluation_preserves_colimits [has_colimits C] (k : K) :
preserves_colimits ((evaluation K C).obj k) :=
{ preserves_colimits_of_shape := λ J 𝒥, by resetI; apply_instance }
/-- `F : D ⥤ K ⥤ C` preserves the colimit of some `G : J ⥤ D` if it does for each `k : K`. -/
def preserves_colimit_of_evaluation (F : D ⥤ K ⥤ C) (G : J ⥤ D)
(H : Π (k), preserves_colimit G (F ⋙ (evaluation K C).obj k)) : preserves_colimit G F := ⟨λ c hc,
begin
apply evaluation_jointly_reflects_colimits,
intro X,
haveI := H X,
change is_colimit ((F ⋙ (evaluation K C).obj X).map_cocone c),
exact preserves_colimit.preserves hc,
end⟩
/-- `F : D ⥤ K ⥤ C` preserves all colimits of shape `J` if it does for each `k : K`. -/
def preserves_colimits_of_shape_of_evaluation (F : D ⥤ K ⥤ C) (J : Type*) [category J]
(H : Π (k : K), preserves_colimits_of_shape J (F ⋙ (evaluation K C).obj k)) :
preserves_colimits_of_shape J F :=
⟨λ G, preserves_colimit_of_evaluation F G (λ k, preserves_colimits_of_shape.preserves_colimit)⟩
/-- `F : D ⥤ K ⥤ C` preserves all colimits if it does for each `k : K`. -/
def preserves_colimits_of_evaluation (F : D ⥤ K ⥤ C)
(H : Π (k : K), preserves_colimits_of_size.{w' w} (F ⋙ (evaluation K C).obj k)) :
preserves_colimits_of_size.{w' w} F :=
⟨λ L hL, by exactI preserves_colimits_of_shape_of_evaluation
F L (λ k, preserves_colimits_of_size.preserves_colimits_of_shape)⟩
/-- The constant functor `C ⥤ (D ⥤ C)` preserves colimits. -/
instance preserves_colimits_const : preserves_colimits_of_size.{w' w} (const D : C ⥤ _) :=
preserves_colimits_of_evaluation _ $ λ X, preserves_colimits_of_nat_iso $ iso.symm $
const_comp_evaluation_obj _ _
open category_theory.prod
/-- The limit of a diagram `F : J ⥤ K ⥤ C` is isomorphic to the functor given by
the individual limits on objects. -/
@[simps]
def limit_iso_flip_comp_lim [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) :
limit F ≅ F.flip ⋙ lim :=
nat_iso.of_components (limit_obj_iso_limit_comp_evaluation F) $ by tidy
/-- A variant of `limit_iso_flip_comp_lim` where the arguemnts of `F` are flipped. -/
@[simps]
def limit_flip_iso_comp_lim [has_limits_of_shape J C] (F : K ⥤ J ⥤ C) :
limit F.flip ≅ F ⋙ lim :=
nat_iso.of_components (λ k,
limit_obj_iso_limit_comp_evaluation F.flip k ≪≫
has_limit.iso_of_nat_iso (flip_comp_evaluation _ _)) $ by tidy
/--
For a functor `G : J ⥤ K ⥤ C`, its limit `K ⥤ C` is given by `(G' : K ⥤ J ⥤ C) ⋙ lim`.
Note that this does not require `K` to be small.
-/
@[simps] def limit_iso_swap_comp_lim [has_limits_of_shape J C] (G : J ⥤ K ⥤ C) :
limit G ≅ curry.obj (swap K J ⋙ uncurry.obj G) ⋙ lim :=
limit_iso_flip_comp_lim G ≪≫ iso_whisker_right (flip_iso_curry_swap_uncurry _) _
/-- The colimit of a diagram `F : J ⥤ K ⥤ C` is isomorphic to the functor given by
the individual colimits on objects. -/
@[simps]
def colimit_iso_flip_comp_colim [has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) :
colimit F ≅ F.flip ⋙ colim :=
nat_iso.of_components (colimit_obj_iso_colimit_comp_evaluation F) $ by tidy
/-- A variant of `colimit_iso_flip_comp_colim` where the arguemnts of `F` are flipped. -/
@[simps]
def colimit_flip_iso_comp_colim [has_colimits_of_shape J C] (F : K ⥤ J ⥤ C) :
colimit F.flip ≅ F ⋙ colim :=
nat_iso.of_components (λ k,
colimit_obj_iso_colimit_comp_evaluation _ _ ≪≫
has_colimit.iso_of_nat_iso (flip_comp_evaluation _ _)) $ by tidy
/--
For a functor `G : J ⥤ K ⥤ C`, its colimit `K ⥤ C` is given by `(G' : K ⥤ J ⥤ C) ⋙ colim`.
Note that this does not require `K` to be small.
-/
@[simps]
def colimit_iso_swap_comp_colim [has_colimits_of_shape J C] (G : J ⥤ K ⥤ C) :
colimit G ≅ curry.obj (swap K J ⋙ uncurry.obj G) ⋙ colim :=
colimit_iso_flip_comp_colim G ≪≫ iso_whisker_right (flip_iso_curry_swap_uncurry _) _
end category_theory.limits
|
05bc9651ba22ff4daedd2c3e0eb51b444a88c0c1 | 7282d49021d38dacd06c4ce45a48d09627687fe0 | /tests/lean/simp3.lean | 991e485877c4856c1143954432ce8b29c559bc71 | [
"Apache-2.0"
] | permissive | steveluc/lean | 5a0b4431acefaf77f15b25bbb49294c2449923ad | 92ba4e8b2d040a799eda7deb8d2a7cdd3e69c496 | refs/heads/master | 1,611,332,256,930 | 1,391,013,244,000 | 1,391,013,244,000 | 16,361,079 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,421 | lean | definition double x := x + x
(*
function show(x) print(x) end
local t1 = parse_lean("0 ≠ 1")
show(simplify(t1))
local t2 = parse_lean("3 ≥ 2")
show(simplify(t2))
local t3 = parse_lean("double (double 2) + 1")
show(simplify(t3))
local t4 = parse_lean("0 = 1")
show(simplify(t4))
*)
(*
local opt = options({"simplifier", "unfold"}, true, {"simplifier", "eval"}, false)
local t1 = parse_lean("double (double 2) + 1 ≥ 3")
show(simplify(t1, 'default', opt))
*)
set_opaque Nat::ge false
add_rewrite Nat::add_assoc
add_rewrite Nat::distributer
add_rewrite Nat::distributel
(*
local opt = options({"simplifier", "unfold"}, true, {"simplifier", "eval"}, false)
local t1 = parse_lean("2 * double (double 2) + 1 ≥ 3")
show(simplify(t1, 'default', opt))
*)
variables a b c d : Nat
import if_then_else
add_rewrite if_true
add_rewrite if_false
add_rewrite if_a_a
add_rewrite Nat::add_zeror
add_rewrite Nat::add_zerol
add_rewrite eq_id
(*
local t1 = parse_lean("(a + b) * (c + d)")
local r, pr = simplify(t1)
print(r)
print(pr)
*)
theorem congr2_congr1 {A B C : TypeU} {f g : A → B} (h : B → C) (Hfg : f = g) (a : A) :
congr2 h (congr1 a Hfg) = congr2 (λ x, h (x a)) Hfg
:= proof_irrel (congr2 h (congr1 a Hfg)) (congr2 (λ x, h (x a)) Hfg)
theorem congr2_congr2 {A B C : TypeU} {a b : A} (f : A → B) (h : B → C) (Hab : a = b) :
congr2 h (congr2 f Hab) = congr2 (λ x, h (f x)) Hab
:= proof_irrel (congr2 h (congr2 f Hab)) (congr2 (λ x, h (f x)) Hab)
theorem congr1_congr2 {A B C : TypeU} {a b : A} (f : A → B → C) (Hab : a = b) (c : B):
congr1 c (congr2 f Hab) = congr2 (λ x, f x c) Hab
:= proof_irrel (congr1 c (congr2 f Hab)) (congr2 (λ x, f x c) Hab)
rewrite_set proofsimp
add_rewrite congr2_congr1 congr2_congr2 congr1_congr2 : proofsimp
(*
local t2 = parse_lean("(if a > 0 then b else b + 0) + 10 = (if a > 0 then b else b) + 10")
local r, pr = simplify(t2)
print(r)
print(pr)
show(simplify(pr, 'proofsimp'))
*)
(*
local t1 = parse_lean("(a + b) * (a + b)")
local t2 = simplify(t1)
print(t2)
*)
-- add_rewrite imp_truer imp_truel imp_falsel imp_falser not_true not_false
-- print rewrite_set
(*
local t1 = parse_lean("true → false")
print(simplify(t1))
*)
(*
local t1 = parse_lean("true → true")
print(simplify(t1))
*)
(*
local t1 = parse_lean("false → false")
print(simplify(t1))
*)
(*
local t1 = parse_lean("true ↔ false")
print(simplify(t1))
*) |
b69b9dd73626e61e831f167e14f320a06295b80d | 618003631150032a5676f229d13a079ac875ff77 | /src/data/equiv/fin.lean | 8af90529f11d5a6066fc66e830599ab8f677c630 | [
"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 | 3,752 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Kenny Lau
-/
import data.fin
import data.equiv.basic
/-!
# Equivalences for `fin n`
-/
universe variables u
variables {m n : ℕ}
/-- Equivalence between `fin 0` and `empty`. -/
def fin_zero_equiv : fin 0 ≃ empty :=
⟨fin_zero_elim, empty.elim, assume a, fin_zero_elim a, assume a, empty.elim a⟩
/-- Equivalence between `fin 0` and `pempty`. -/
def fin_zero_equiv' : fin 0 ≃ pempty.{u} :=
equiv.equiv_pempty fin.elim0
/-- Equivalence between `fin 1` and `punit`. -/
def fin_one_equiv : fin 1 ≃ punit :=
⟨λ_, (), λ_, 0, fin.cases rfl (λa, fin_zero_elim a), assume ⟨⟩, rfl⟩
/-- Equivalence between `fin 2` and `bool`. -/
def fin_two_equiv : fin 2 ≃ bool :=
⟨@fin.cases 1 (λ_, bool) ff (λ_, tt),
λb, cond b 1 0,
begin
refine fin.cases _ _, refl,
refine fin.cases _ _, refl,
exact λi, fin_zero_elim i
end,
assume b, match b with tt := rfl | ff := rfl end⟩
/-- Equivalence between `fin n.succ` and `option (fin n)` -/
def fin_succ_equiv (n : ℕ) : fin n.succ ≃ option (fin n) :=
⟨λ x, fin.cases none some x, λ x, option.rec_on x 0 fin.succ,
λ x, fin.cases rfl (λ i, show (option.rec_on (fin.cases none some (fin.succ i) : option (fin n))
0 fin.succ : fin n.succ) = _, by rw fin.cases_succ) x,
by rintro ⟨none | x⟩; [refl, exact fin.cases_succ _]⟩
/-- Equivalence between `fin m ⊕ fin n` and `fin (m + n)` -/
def sum_fin_sum_equiv : fin m ⊕ fin n ≃ fin (m + n) :=
{ to_fun := λ x, sum.rec_on x
(λ y, ⟨y.1, nat.lt_of_lt_of_le y.2 $ nat.le_add_right m n⟩)
(λ y, ⟨m + y.1, nat.add_lt_add_left y.2 m⟩),
inv_fun := λ x, if H : x.1 < m
then sum.inl ⟨x.1, H⟩
else sum.inr ⟨x.1 - m, nat.lt_of_add_lt_add_left $
show m + (x.1 - m) < m + n,
from (nat.add_sub_of_le $ le_of_not_gt H).symm ▸ x.2⟩,
left_inv := λ x, begin
cases x with y y,
{ simp [y.2, fin.ext_iff] },
{ have H : ¬m + y.val < m := not_lt_of_ge (nat.le_add_right _ _),
simp [H, nat.add_sub_cancel_left, fin.ext_iff] }
end,
right_inv := λ x, begin
by_cases H : x.1 < m,
{ dsimp, rw [dif_pos H], simp },
{ dsimp, rw [dif_neg H], simp [fin.ext_iff, nat.add_sub_of_le (le_of_not_gt H)] }
end }
/-- Equivalence between `fin m × fin n` and `fin (m * n)` -/
def fin_prod_fin_equiv : fin m × fin n ≃ fin (m * n) :=
{ to_fun := λ x, ⟨x.2.1 + n * x.1.1,
calc x.2.1 + n * x.1.1 + 1
= x.1.1 * n + x.2.1 + 1 : by ac_refl
... ≤ x.1.1 * n + n : nat.add_le_add_left x.2.2 _
... = (x.1.1 + 1) * n : eq.symm $ nat.succ_mul _ _
... ≤ m * n : nat.mul_le_mul_right _ x.1.2⟩,
inv_fun := λ x,
have H : 0 < n, from nat.pos_of_ne_zero $ λ H, nat.not_lt_zero x.1 $ by subst H; from x.2,
(⟨x.1 / n, (nat.div_lt_iff_lt_mul _ _ H).2 x.2⟩,
⟨x.1 % n, nat.mod_lt _ H⟩),
left_inv := λ ⟨x, y⟩,
have H : 0 < n, from nat.pos_of_ne_zero $ λ H, nat.not_lt_zero y.1 $ H ▸ y.2,
prod.ext
(fin.eq_of_veq $ calc
(y.1 + n * x.1) / n
= y.1 / n + x.1 : nat.add_mul_div_left _ _ H
... = 0 + x.1 : by rw nat.div_eq_of_lt y.2
... = x.1 : nat.zero_add x.1)
(fin.eq_of_veq $ calc
(y.1 + n * x.1) % n
= y.1 % n : nat.add_mul_mod_self_left _ _ _
... = y.1 : nat.mod_eq_of_lt y.2),
right_inv := λ x, fin.eq_of_veq $ nat.mod_add_div _ _ }
/-- `fin 0` is a subsingleton. -/
instance subsingleton_fin_zero : subsingleton (fin 0) :=
fin_zero_equiv.subsingleton
/-- `fin 1` is a subsingleton. -/
instance subsingleton_fin_one : subsingleton (fin 1) :=
fin_one_equiv.subsingleton
|
2d1fc8ebed066a904d29db2f51848871626bf537 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /tests/lean/nat_pp.lean | 74968b0b1f88c8fdddb5d7264e9f774995bbf0c5 | [
"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 | 106 | lean | eval nat.add (nat.of_num 3) (nat.of_num 6)
open nat
eval nat.add (nat.of_num 3) (nat.of_num 6)
eval 3 + 6
|
8856f3e7a9bedb157466940ecf6ff4149beaa250 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /hott/types/default.hlean | cc59ea54bcf5d91ae3a144d5ef25a0a7b45c848a | [
"Apache-2.0"
] | permissive | soonhokong/lean-osx | 4a954262c780e404c1369d6c06516161d07fcb40 | 3670278342d2f4faa49d95b46d86642d7875b47c | refs/heads/master | 1,611,410,334,552 | 1,474,425,686,000 | 1,474,425,686,000 | 12,043,103 | 5 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 254 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import .bool .prod .sigma .pi .arrow .pointed .fiber
import .nat .int
import .eq .equiv .trunc
|
1e08960e23fbf9228c5778c50361a7eec3457b61 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/compiler/str.lean | 1ab37b21f7d9315ab56cfcb0e9580ddacf8c7576 | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 934 | lean | def showChars : Nat → String → String.Pos → IO Unit
| 0, _, _ => pure ()
| n+1, s, idx =>
unless (s.atEnd idx) $
IO.println (">> " ++ toString (s.get idx)) *>
showChars n s (s.next idx)
def main : IO UInt32 :=
let s₁ := "hello α_world_β";
let b : String.Pos := 0;
let e := s₁.bsize;
IO.println (s₁.extract b e) *>
IO.println (s₁.extract (b+2) e) *>
IO.println (s₁.extract (b+2) (e-1)) *>
IO.println (s₁.extract (b+2) (e-2)) *>
IO.println (s₁.extract (b+7) e) *>
IO.println (s₁.extract (b+8) e) *>
IO.println (toString e) *>
IO.println (repr " aaa ".trim) *>
showChars s₁.length s₁ 0 *>
IO.println ("abc".isPrefixOf "abcd") *>
IO.println ("abcd".isPrefixOf "abcd") *>
IO.println ("".isPrefixOf "abcd") *>
IO.println ("".isPrefixOf "") *>
IO.println ("ab".isPrefixOf "cb") *>
IO.println ("ab".isPrefixOf "a") *>
IO.println ("αb".isPrefixOf "αbc") *>
pure 0
|
981d0a290474ee02ad04b143ad50ee951d79f9f4 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/computability/language.lean | 1e6f2e430dbbfcb91295dc943e7925008c3dd9ad | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 6,748 | lean | /-
Copyright (c) 2020 Fox Thomson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Fox Thomson
-/
import data.set.lattice
import data.list.basic
import algebra.group_power.order
/-!
# Languages
This file contains the definition and operations on formal languages over an alphabet. Note strings
are implemented as lists over the alphabet.
The operations in this file define a [Kleene algebra](https://en.wikipedia.org/wiki/Kleene_algebra)
over the languages.
-/
universes u v
variables {α : Type u}
/-- A language is a set of strings over an alphabet. -/
@[derive [has_mem (list α), has_singleton (list α), has_insert (list α), complete_boolean_algebra]]
def language (α) := set (list α)
namespace language
local attribute [reducible] language
/-- Zero language has no elements. -/
instance : has_zero (language α) := ⟨(∅ : set _)⟩
/-- `1 : language α` contains only one element `[]`. -/
instance : has_one (language α) := ⟨{[]}⟩
instance : inhabited (language α) := ⟨0⟩
/-- The sum of two languages is the union of -/
instance : has_add (language α) := ⟨set.union⟩
instance : has_mul (language α) := ⟨set.image2 (++)⟩
lemma zero_def : (0 : language α) = (∅ : set _) := rfl
lemma one_def : (1 : language α) = {[]} := rfl
lemma add_def (l m : language α) : l + m = l ∪ m := rfl
lemma mul_def (l m : language α) : l * m = set.image2 (++) l m := rfl
/-- The star of a language `L` is the set of all strings which can be written by concatenating
strings from `L`. -/
def star (l : language α) : language α :=
{ x | ∃ S : list (list α), x = S.join ∧ ∀ y ∈ S, y ∈ l}
lemma star_def (l : language α) :
l.star = { x | ∃ S : list (list α), x = S.join ∧ ∀ y ∈ S, y ∈ l} := rfl
@[simp] lemma mem_one (x : list α) : x ∈ (1 : language α) ↔ x = [] := by refl
@[simp] lemma mem_add (l m : language α) (x : list α) : x ∈ l + m ↔ x ∈ l ∨ x ∈ m :=
by simp [add_def]
lemma mem_mul (l m : language α) (x : list α) : x ∈ l * m ↔ ∃ a b, a ∈ l ∧ b ∈ m ∧ a ++ b = x :=
by simp [mul_def]
lemma mem_star (l : language α) (x : list α) :
x ∈ l.star ↔ ∃ S : list (list α), x = S.join ∧ ∀ y ∈ S, y ∈ l :=
iff.rfl
instance : semiring (language α) :=
{ add := (+),
add_assoc := set.union_assoc,
zero := 0,
zero_add := set.empty_union,
add_zero := set.union_empty,
add_comm := set.union_comm,
mul := (*),
mul_assoc := λ l m n,
by simp only [mul_def, set.image2_image2_left, set.image2_image2_right, list.append_assoc],
zero_mul := by simp [zero_def, mul_def],
mul_zero := by simp [zero_def, mul_def],
one := 1,
one_mul := λ l, by simp [mul_def, one_def],
mul_one := λ l, by simp [mul_def, one_def],
left_distrib := λ l m n, by simp only [mul_def, add_def, set.image2_union_right],
right_distrib := λ l m n, by simp only [mul_def, add_def, set.image2_union_left] }
@[simp] lemma add_self (l : language α) : l + l = l := sup_idem
lemma star_def_nonempty (l : language α) :
l.star = {x | ∃ S : list (list α), x = S.join ∧ ∀ y ∈ S, y ∈ l ∧ y ≠ []} :=
begin
ext x,
split,
{ rintro ⟨S, rfl, h⟩,
refine ⟨S.filter (λ l, ¬list.empty l), by simp, λ y hy, _⟩,
rw [list.mem_filter, list.empty_iff_eq_nil] at hy,
exact ⟨h y hy.1, hy.2⟩ },
{ rintro ⟨S, hx, h⟩,
exact ⟨S, hx, λ y hy, (h y hy).1⟩ }
end
lemma le_iff (l m : language α) : l ≤ m ↔ l + m = m := sup_eq_right.symm
lemma le_mul_congr {l₁ l₂ m₁ m₂ : language α} : l₁ ≤ m₁ → l₂ ≤ m₂ → l₁ * l₂ ≤ m₁ * m₂ :=
begin
intros h₁ h₂ x hx,
simp only [mul_def, exists_and_distrib_left, set.mem_image2, set.image_prod] at hx ⊢,
tauto
end
lemma le_add_congr {l₁ l₂ m₁ m₂ : language α} : l₁ ≤ m₁ → l₂ ≤ m₂ → l₁ + l₂ ≤ m₁ + m₂ := sup_le_sup
lemma mem_supr {ι : Sort v} {l : ι → language α} {x : list α} :
x ∈ (⨆ i, l i) ↔ ∃ i, x ∈ l i :=
set.mem_Union
lemma supr_mul {ι : Sort v} (l : ι → language α) (m : language α) :
(⨆ i, l i) * m = ⨆ i, l i * m :=
set.image2_Union_left _ _ _
lemma mul_supr {ι : Sort v} (l : ι → language α) (m : language α) :
m * (⨆ i, l i) = ⨆ i, m * l i :=
set.image2_Union_right _ _ _
lemma supr_add {ι : Sort v} [nonempty ι] (l : ι → language α) (m : language α) :
(⨆ i, l i) + m = ⨆ i, l i + m := supr_sup
lemma add_supr {ι : Sort v} [nonempty ι] (l : ι → language α) (m : language α) :
m + (⨆ i, l i) = ⨆ i, m + l i := sup_supr
lemma mem_pow {l : language α} {x : list α} {n : ℕ} :
x ∈ l ^ n ↔ ∃ S : list (list α), x = S.join ∧ S.length = n ∧ ∀ y ∈ S, y ∈ l :=
begin
induction n with n ihn generalizing x,
{ simp only [mem_one, pow_zero, list.length_eq_zero],
split,
{ rintro rfl, exact ⟨[], rfl, rfl, λ y h, h.elim⟩ },
{ rintro ⟨_, rfl, rfl, _⟩, refl } },
{ simp only [pow_succ, mem_mul, ihn],
split,
{ rintro ⟨a, b, ha, ⟨S, rfl, rfl, hS⟩, rfl⟩,
exact ⟨a :: S, rfl, rfl, list.forall_mem_cons.2 ⟨ha, hS⟩⟩ },
{ rintro ⟨_|⟨a, S⟩, rfl, hn, hS⟩; cases hn,
rw list.forall_mem_cons at hS,
exact ⟨a, _, hS.1, ⟨S, rfl, rfl, hS.2⟩, rfl⟩ } }
end
lemma star_eq_supr_pow (l : language α) : l.star = ⨆ i : ℕ, l ^ i :=
begin
ext x,
simp only [mem_star, mem_supr, mem_pow],
split,
{ rintro ⟨S, rfl, hS⟩, exact ⟨_, S, rfl, rfl, hS⟩ },
{ rintro ⟨_, S, rfl, rfl, hS⟩, exact ⟨S, rfl, hS⟩ }
end
lemma mul_self_star_comm (l : language α) : l.star * l = l * l.star :=
by simp only [star_eq_supr_pow, mul_supr, supr_mul, ← pow_succ, ← pow_succ']
@[simp] lemma one_add_self_mul_star_eq_star (l : language α) : 1 + l * l.star = l.star :=
begin
simp only [star_eq_supr_pow, mul_supr, ← pow_succ, ← pow_zero l],
exact sup_supr_nat_succ _
end
@[simp] lemma one_add_star_mul_self_eq_star (l : language α) : 1 + l.star * l = l.star :=
by rw [mul_self_star_comm, one_add_self_mul_star_eq_star]
lemma star_mul_le_right_of_mul_le_right (l m : language α) : l * m ≤ m → l.star * m ≤ m :=
begin
intro h,
rw [star_eq_supr_pow, supr_mul],
refine supr_le _,
intro n,
induction n with n ih,
{ simp },
rw [pow_succ', mul_assoc (l^n) l m],
exact le_trans (le_mul_congr (le_refl _) h) ih,
end
lemma star_mul_le_left_of_mul_le_left (l m : language α) : m * l ≤ m → m * l.star ≤ m :=
begin
intro h,
rw [star_eq_supr_pow, mul_supr],
refine supr_le _,
intro n,
induction n with n ih,
{ simp },
rw [pow_succ, ←mul_assoc m l (l^n)],
exact le_trans (le_mul_congr h (le_refl _)) ih
end
end language
|
6389dae5ab7073c0fb30f105c23685b9cc8f471c | 78269ad0b3c342b20786f60690708b6e328132b0 | /src/library_dev/data/nat/dvd.lean | 8115c7adfffa8c76813b617a08a2de7ade2552bf | [] | no_license | dselsam/library_dev | e74f46010fee9c7b66eaa704654cad0fcd2eefca | 1b4e34e7fb067ea5211714d6d3ecef5132fc8218 | refs/heads/master | 1,610,372,841,675 | 1,497,014,421,000 | 1,497,014,421,000 | 86,526,137 | 0 | 0 | null | 1,490,752,133,000 | 1,490,752,132,000 | null | UTF-8 | Lean | false | false | 3,168 | lean | import ...algebra.ring
import init.data.nat.basic
namespace nat
protected theorem dvd_add_iff_right {k m n : ℕ} (h : k ∣ m) : k ∣ n ↔ k ∣ m + n :=
⟨dvd_add h, dvd.elim h $ λd hd, match m, hd with
| ._, rfl := λh₂, dvd.elim h₂ $ λe he, ⟨e - d,
by rw [nat.mul_sub_left_distrib, -he, nat.add_sub_cancel_left]⟩
end⟩
protected theorem dvd_add_iff_left {k m n : ℕ} (h : k ∣ n) : k ∣ m ↔ k ∣ m + n :=
by rw add_comm; exact nat.dvd_add_iff_right h
theorem dvd_sub {k m n : ℕ} (H : n ≤ m) (h₁ : k ∣ m) (h₂ : k ∣ n) : k ∣ m - n :=
(nat.dvd_add_iff_left h₂).2 $ by rw nat.sub_add_cancel H; exact h₁
theorem dvd_mod_iff {k m n : ℕ} (h : k ∣ n) : k ∣ m % n ↔ k ∣ m :=
let t := @nat.dvd_add_iff_left _ (m % n) _ (dvd_trans h (dvd_mul_right n (m / n))) in
by rwa mod_add_div at t
lemma le_of_dvd {m n : ℕ} (h : n > 0) : m ∣ n → m ≤ n :=
λ⟨k, e⟩, by {
revert h, rw e, refine k.cases_on _ _,
exact λhn, absurd hn (lt_irrefl _),
exact λk _, let t := mul_le_mul_left m (succ_pos k) in by rwa mul_one at t }
theorem dvd.antisymm : Π {m n : ℕ}, m ∣ n → n ∣ m → m = n
| m 0 h₁ h₂ := eq_zero_of_zero_dvd h₂
| 0 n h₁ h₂ := (eq_zero_of_zero_dvd h₁).symm
| (succ m) (succ n) h₁ h₂ := le_antisymm (le_of_dvd (succ_pos _) h₁) (le_of_dvd (succ_pos _) h₂)
theorem pos_of_dvd_of_pos {m n : ℕ} (H1 : m ∣ n) (H2 : n > 0) : m > 0 :=
nat.pos_of_ne_zero $ λm0, by rw m0 at H1; rw eq_zero_of_zero_dvd H1 at H2; exact lt_irrefl _ H2
theorem eq_one_of_dvd_one {n : ℕ} (H : n ∣ 1) : n = 1 :=
le_antisymm (le_of_dvd dec_trivial H) (pos_of_dvd_of_pos H dec_trivial)
theorem dvd_of_mod_eq_zero {m n : ℕ} (H : n % m = 0) : m ∣ n :=
dvd.intro (n / m) $ let t := mod_add_div n m in by simp [H] at t; exact t
theorem mod_eq_zero_of_dvd {m n : ℕ} (H : m ∣ n) : n % m = 0 :=
dvd.elim H (λ z H1, by rw [H1, mul_mod_right])
theorem dvd_iff_mod_eq_zero (m n : ℕ) : m ∣ n ↔ n % m = 0 :=
⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩
instance decidable_dvd : decidable_rel (@has_dvd.dvd nat _) :=
λm n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm
protected theorem mul_div_cancel' {m n : ℕ} (H : n ∣ m) : n * (m / n) = m :=
let t := mod_add_div m n in by rwa [mod_eq_zero_of_dvd H, zero_add] at t
protected theorem div_mul_cancel {m n : ℕ} (H : n ∣ m) : m / n * n = m :=
by rw [mul_comm, nat.mul_div_cancel' H]
protected theorem mul_div_assoc (m : ℕ) {n k : ℕ} (H : k ∣ n) : m * n / k = m * (n / k) :=
or.elim (eq_zero_or_pos k)
(λh, by rw [h, nat.div_zero, nat.div_zero, mul_zero])
(λh, have m * n / k = m * (n / k * k) / k, by rw nat.div_mul_cancel H,
by rw[this, -mul_assoc, nat.mul_div_cancel _ h])
theorem dvd_of_mul_dvd_mul_left {m n k : ℕ} (kpos : k > 0) (H : k * m ∣ k * n) : m ∣ n :=
dvd.elim H (λl H1, by rw mul_assoc at H1; exact ⟨_, eq_of_mul_eq_mul_left kpos H1⟩)
theorem dvd_of_mul_dvd_mul_right {m n k : ℕ} (kpos : k > 0) (H : m * k ∣ n * k) : m ∣ n :=
by rw [mul_comm m k, mul_comm n k] at H; exact dvd_of_mul_dvd_mul_left kpos H
end nat
|
37ff0cea2c53f58e0c3844d8788d6464fa73b498 | 80d0f8071ea62262937ab36f5887a61735adea09 | /src/certigrad/tensor.lean | b6a274204113484638ac43adc77ab9f0af9b50dd | [
"Apache-2.0"
] | permissive | wudcscheme/certigrad | 94805fa6a61f993c69a824429a103c9613a65a48 | c9a06e93f1ec58196d6d3b8563b29868d916727f | refs/heads/master | 1,679,386,475,077 | 1,551,651,022,000 | 1,551,651,022,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,980 | lean | /-
Copyright (c) 2017 Daniel Selsam. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Daniel Selsam
Tensors and basic tensor operations.
-/
import .util .rng .dvec .id
run_cmd mk_simp_attr `cgsimp
namespace certigrad
@[reducible] def S : Type := list ℕ
constant T (shape : S) : Type
notation `ℝ` := T []
namespace T
-- Constants that compute (excluding const, lt, le)
constant const (α : ℝ) (shape : S) : T shape
constant eps (shape : S) : T shape
constant zero (shape : S) : T shape
constant one (shape : S) : T shape
constant pi (shape : S) : T shape
constant neg {shape : S} (x : T shape) : T shape
constant inv {shape : S} (x : T shape) : T shape
constant log {shape : S} (x : T shape) : T shape
constant exp {shape : S} (x : T shape) : T shape
constant sqrt {shape : S} (x : T shape) : T shape
constant tanh {shape : S} (x : T shape) : T shape
constant add {shape : S} (x y : T shape) : T shape
constant mul {shape : S} (x y : T shape) : T shape
constant sub {shape : S} (x y : T shape) : T shape
constant div {shape : S} (x y : T shape) : T shape
constant lt {shape : S} (x y : T shape) : Prop
constant le {shape : S} (x y : T shape) : Prop
constant pow {shape : S} (x : T shape) (α : ℝ) : T shape
constant of_nat (n : ℕ) : ℝ
constant round (α : ℝ) : ℕ
constant fail (shape : S) : T shape
constant silent_fail (shape : S) : T shape
constant error {shape : S} (s : string) : T shape
-- Algebraic instances
@[inline, priority 10000] instance (shape : S) : has_zero (T shape) := ⟨T.zero shape⟩
@[inline, priority 10000] instance (shape : S) : has_one (T shape) := ⟨T.one shape⟩
@[inline, priority 10000] instance (shape : S) : has_neg (T shape) := ⟨T.neg⟩
@[inline, priority 10000] instance (shape : S) : has_add (T shape) := ⟨T.add⟩
@[inline, priority 10000] instance (shape : S) : has_mul (T shape) := ⟨T.mul⟩
@[inline, priority 10000] instance (shape : S) : has_lt (T shape) := ⟨T.lt⟩
@[inline, priority 10000] instance (shape : S) : has_le (T shape) := ⟨T.le⟩
@[inline, priority 10000] instance (shape : S) : has_inv (T shape) := ⟨T.inv⟩
@[inline, priority 10000] instance (shape : S) : has_div (T shape) := ⟨λ x y, x * y⁻¹⟩
namespace IL
-- Instance Lemmas
axiom add_comm {shape : S} : ∀ (x y : T shape), x + y = y + x
axiom add_assoc {shape : S} : ∀ (x y z : T shape), x + y + z = x + (y + z)
axiom zero_add {shape : S} : ∀ (x : T shape), 0 + x = x
axiom add_zero {shape : S} : ∀ (x : T shape), x + 0 = x
axiom add_left_neg {shape : S} : ∀ (x : T shape), -x + x = 0
axiom mul_comm {shape : S} : ∀ (x y : T shape), x * y = y * x
axiom mul_assoc {shape : S} : ∀ (x y z : T shape), x * y * z = x * (y * z)
axiom one_mul {shape : S} : ∀ (x : T shape), 1 * x = x
axiom mul_one {shape : S} : ∀ (x : T shape), x * 1 = x
axiom left_distrib {shape : S} : ∀ (x y z : T shape), x * (y + z) = x * y + x * z
axiom right_distrib {shape : S} : ∀ (x y z : T shape), (x + y) * z = x * z + y * z
axiom le_refl {shape : S} : ∀ (x : T shape), x ≤ x
axiom le_trans {shape : S} : ∀ (x y z : T shape), x ≤ y → y ≤ z → x ≤ z
axiom le_antisymm {shape : S} : ∀ (x y : T shape), x ≤ y → y ≤ x → x = y
axiom le_of_lt {shape : S} : ∀ (x y : T shape), x < y → x ≤ y
axiom lt_of_lt_of_le {shape : S} : ∀ (x y z : T shape), x < y → y ≤ z → x < z
axiom lt_of_le_of_lt {shape : S} : ∀ (x y z : T shape), x ≤ y → y < z → x < z
axiom lt_irrefl {shape : S} : ∀ (x : T shape), ¬x < x
axiom add_le_add_left {shape : S} : ∀ (x y : T shape), x ≤ y → ∀ (z : T shape), z + x ≤ z + y
axiom add_lt_add_left {shape : S} : ∀ (x y : T shape), x < y → ∀ (z : T shape), z + x < z + y
axiom zero_ne_one {shape : S} : (0 : T shape) ≠ (1 : T shape)
axiom mul_nonneg {shape : S} : ∀ (x y : T shape), 0 ≤ x → 0 ≤ y → 0 ≤ x * y
axiom mul_pos {shape : S} : ∀ (x y : T shape), 0 < x → 0 < y → 0 < x * y
axiom le_iff_lt_or_eq {shape : S} : ∀ (x y : T shape), x ≤ y ↔ x < y ∨ x = y
end IL
@[inline] instance (shape : S) : ordered_comm_ring (T shape) :=
{
-- defs
zero := T.zero shape, one := T.one shape, add := T.add, neg := T.neg, mul := T.mul,
-- noncomputable defs
le := T.le, lt := T.lt,
-- axioms
add_comm := T.IL.add_comm, add_assoc := T.IL.add_assoc, zero_add := T.IL.zero_add,
add_zero := T.IL.add_zero, add_left_neg := T.IL.add_left_neg,
mul_comm := T.IL.mul_comm, mul_assoc := T.IL.mul_assoc, one_mul := T.IL.one_mul, mul_one := T.IL.mul_one,
left_distrib := T.IL.left_distrib, right_distrib := T.IL.right_distrib,
le_refl := T.IL.le_refl, le_trans := T.IL.le_trans, le_antisymm := T.IL.le_antisymm,
le_of_lt := T.IL.le_of_lt, lt_of_lt_of_le := T.IL.lt_of_lt_of_le, lt_of_le_of_lt := T.IL.lt_of_le_of_lt,
lt_irrefl := T.IL.lt_irrefl, add_le_add_left := T.IL.add_le_add_left, add_lt_add_left := T.IL.add_lt_add_left,
zero_ne_one := T.IL.zero_ne_one, mul_nonneg := T.IL.mul_nonneg, mul_pos := T.IL.mul_pos
}
@[inline] instance (shape : S) : has_sub (T shape) := by apply_instance
attribute [inline] ordered_comm_ring.to_ordered_ring ordered_ring.to_ring ring.to_add_comm_group add_comm_group.to_add_group algebra.sub
-- We never want to do algebra with this
def scalar_mul {shape : S} (α : ℝ) (x : T shape) : T shape := const α shape * x
constant transpose {m n : ℕ} (M : T [m, n]) : T [n, m]
constant sum : Π {shape : S}, T shape → ℝ
constant prod : Π {shape : S}, T shape → ℝ
constant get_row {m n : ℕ} (M : T [m, n]) (ridx : ℕ) : T [n]
constant sum_cols {nrows ncols : ℕ} (M : T [nrows, ncols]) : T [nrows]
constant get_col {m n : ℕ} (M : T [m, n]) (cidx : ℕ) : T [m]
constant get_col_range {m n : ℕ} (ncols : ℕ) (M : T [m, n]) (cidx : ℕ) : T [m, ncols]
constant replicate_col {m : ℕ} (v : T [m]) (n : ℕ) : T [m, n]
constant gemv {m n : ℕ} (M : T [m, n]) (x : T [n]) : T [m]
constant gemm {m n p : ℕ} (M : T [m, n]) (N : T [n, p]) : T [m, p]
constant append_col {n p : ℕ} (N : T [n, p]) (x : T [n]) : T [n, p+1]
constant sample_mvn : Π {shape : S} (μ σ : T shape) (rng : RNG), T shape × RNG
constant sample_uniform : Π (shape : S) (low high : ℝ) (rng : RNG), T shape × RNG
constant to_string {shape : S} : T shape → string
/- Other constants -/
constant is_integrable : Π {shape₁ shape₂ : S}, (T shape₁ → T shape₂) → Prop
constant integral : Π {shape₁ shape₂ : S}, (T shape₁ → T shape₂) → T shape₂
constant is_uniformly_integrable_around : Π {shape₁ shape₂ shape₃ : S} (f : T shape₁ → T shape₂ → T shape₃) (θ : T shape₁), Prop
-- ω(exp -x²) ∧ o(exp x²)
constant is_btw_exp₂ {shape₁ shape₂ : S} (f : T shape₁ → T shape₂) : Prop
constant is_sub_quadratic {shape₁ shape₂ : S} (f : T shape₁ → T shape₂) : Prop
constant is_bounded_btw_exp₂_around {shape₁ shape₂ shape₃ : S} (f : Π (x : T shape₁) (θ : T shape₂), T shape₃) (θ : T shape₂) : Prop
-- continuously differentiable
constant is_cdifferentiable : Π {ishape : S}, (T ishape → ℝ) → T ishape → Prop
constant grad : Π {ishape : S}, (T ishape → ℝ) → (T ishape → T ishape)
constant D {ishape oshape : S} : (T ishape → T oshape) → T ishape → T (ishape ++ oshape)
constant tmulT {ishape oshape : S} : T (ishape ++ oshape) → T oshape → T ishape
constant is_continuous {ishape oshape : S} : (T ishape → T oshape) → T ishape → Prop
noncomputable def dintegral {oshape : S} : Π {ishapes : list S}, (dvec T ishapes → T oshape) → T oshape
| [] f := f ⟦⟧
| (ishape::ishapes) f := integral (λ (x : T ishape), @dintegral ishapes (λ (v : dvec T ishapes), f (x ::: v)))
noncomputable def is_dintegrable {oshape : S} : Π {ishapes : list S}, (dvec T ishapes → T oshape) → Prop
| [] f := true
| (ishape::ishapes) f := is_integrable (λ (x : T ishape), @dintegral _ ishapes (λ (v : dvec T ishapes), f (x ::: v)))
∧ ∀ (x : T ishape), is_dintegrable (λ (v : dvec T ishapes), f (x ::: v))
/- Notation -/
notation `π` := pi []
notation `∫` := integral
notation `∇` := grad
/- Other instances -/
instance {shape : S} : has_to_string (T shape) := has_to_string.mk T.to_string
@[inline] instance {shape : S} : inhabited (T shape) := ⟨T.zero shape⟩ -- ⟨silent_fail _⟩ --⟨T.zero shape⟩ (switch back once no course-of-values)
@[inline] instance {shape : S} : has_smul (ℝ) (T shape) := ⟨scalar_mul⟩
/- Derived definitions -/
def softplus {shape : S} (x : T shape) : T shape := log (exp x + 1)
def sigmoid {shape : S} (x : T shape) : T shape := 1 / (1 + exp (- x))
def dot {shape : S} (x y : T shape) : ℝ := sum (x * y)
def square {shape : S} (x : T shape) : T shape := x * x
def mvn_pdf {shape : S} (μ σ x : T shape) : ℝ :=
prod ((sqrt ((2 * pi shape) * square σ))⁻¹ * exp ((- 2⁻¹) * (square $ (x - μ) / σ)))
def mvn_logpdf {shape : S} (μ σ x : T shape) : ℝ :=
(- 2⁻¹) * sum (square ((x - μ) / σ) + log (2 * pi shape) + log (square σ))
def mvn_grad_logpdf_μ {shape : S} (μ σ x : T shape) : T shape :=
(x - μ) / (square σ)
def mvn_grad_logpdf_σ {shape : S} (μ σ x : T shape) : T shape :=
square (x - μ) / (σ * square σ) - σ⁻¹
def mvn_std_logpdf {shape : S} (x : T shape) : ℝ := mvn_logpdf 0 1 x
def mvn_kl {shape : S} (μ σ : T shape) : ℝ :=
(- 2⁻¹) * sum (1 + log (square σ) - square μ - square σ)
def mvn_empirical_kl {shape : S} (μ σ z : T shape) : ℝ :=
mvn_logpdf μ σ z - mvn_std_logpdf z
def bernoulli_neglogpdf {shape : S} (p z : T shape) : ℝ :=
- sum (z * log (eps shape + p) + (1 - z) * log (eps shape + (1 - p)))
def force {shape₁ : S} (x : T shape₁) (shape₂ : S) : T shape₂ :=
if H : shape₁ = shape₂ then eq.rec_on H x else T.error ("force-failed: " ++ _root_.to_string shape₁ ++ " != " ++ _root_.to_string shape₂)
end T
end certigrad
|
ffb98523da770522efcc49ef5a27d8ea440cbd60 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/fin/basic.lean | 1579e1a3837baae45e8cee7ebe2eb203ec67db51 | [
"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 | 73,683 | lean | /-
Copyright (c) 2017 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis, Keeley Hoek
-/
import algebra.ne_zero
import algebra.order.with_zero
import order.rel_iso.basic
import data.nat.order.basic
import order.hom.set
/-!
# The finite type with `n` elements
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
`fin n` is the type whose elements are natural numbers smaller than `n`.
This file expands on the development in the core library.
## Main definitions
### Induction principles
* `fin_zero_elim` : Elimination principle for the empty set `fin 0`, generalizes `fin.elim0`.
* `fin.succ_rec` : Define `C n i` by induction on `i : fin n` interpreted
as `(0 : fin (n - i)).succ.succ…`. This function has two arguments: `H0 n` defines
`0`-th element `C (n+1) 0` of an `(n+1)`-tuple, and `Hs n i` defines `(i+1)`-st element
of `(n+1)`-tuple based on `n`, `i`, and `i`-th element of `n`-tuple.
* `fin.succ_rec_on` : same as `fin.succ_rec` but `i : fin n` is the first argument;
* `fin.induction` : Define `C i` by induction on `i : fin (n + 1)`, separating into the
`nat`-like base cases of `C 0` and `C (i.succ)`.
* `fin.induction_on` : same as `fin.induction` but with `i : fin (n + 1)` as the first argument.
* `fin.cases` : define `f : Π i : fin n.succ, C i` by separately handling the cases `i = 0` and
`i = fin.succ j`, `j : fin n`, defined using `fin.induction`.
* `fin.reverse_induction`: reverse induction on `i : fin (n + 1)`; given `C (fin.last n)` and
`∀ i : fin n, C (fin.succ i) → C (fin.cast_succ i)`, constructs all values `C i` by going down;
* `fin.last_cases`: define `f : Π i, fin (n + 1), C i` by separately handling the cases
`i = fin.last n` and `i = fin.cast_succ j`, a special case of `fin.reverse_induction`;
* `fin.add_cases`: define a function on `fin (m + n)` by separately handling the cases
`fin.cast_add n i` and `fin.nat_add m i`;
* `fin.succ_above_cases`: given `i : fin (n + 1)`, define a function on `fin (n + 1)` by separately
handling the cases `j = i` and `j = fin.succ_above i k`, same as `fin.insert_nth` but marked
as eliminator and works for `Sort*`.
### Order embeddings and an order isomorphism
* `fin.order_iso_subtype` : coercion to `{ i // i < n }` as an `order_iso`;
* `fin.coe_embedding` : coercion to natural numbers as an `embedding`;
* `fin.coe_order_embedding` : coercion to natural numbers as an `order_embedding`;
* `fin.succ_embedding` : `fin.succ` as an `order_embedding`;
* `fin.cast_le h` : embed `fin n` into `fin m`, `h : n ≤ m`;
* `fin.cast eq` : order isomorphism between `fin n` and fin m` provided that `n = m`,
see also `equiv.fin_congr`;
* `fin.cast_add m` : embed `fin n` into `fin (n+m)`;
* `fin.cast_succ` : embed `fin n` into `fin (n+1)`;
* `fin.succ_above p` : embed `fin n` into `fin (n + 1)` with a hole around `p`;
* `fin.add_nat m i` : add `m` on `i` on the right, generalizes `fin.succ`;
* `fin.nat_add n i` adds `n` on `i` on the left;
### Other casts
* `fin.of_nat'`: given a positive number `n` (deduced from `[ne_zero n]`), `fin.of_nat' i` is
`i % n` interpreted as an element of `fin n`;
* `fin.cast_lt i h` : embed `i` into a `fin` where `h` proves it belongs into;
* `fin.pred_above (p : fin n) i` : embed `i : fin (n+1)` into `fin n` by subtracting one if `p < i`;
* `fin.cast_pred` : embed `fin (n + 2)` into `fin (n + 1)` by mapping `fin.last (n + 1)` to
`fin.last n`;
* `fin.sub_nat i h` : subtract `m` from `i ≥ m`, generalizes `fin.pred`;
* `fin.clamp n m` : `min n m` as an element of `fin (m + 1)`;
* `fin.div_nat i` : divides `i : fin (m * n)` by `n`;
* `fin.mod_nat i` : takes the mod of `i : fin (m * n)` by `n`;
### Misc definitions
* `fin.last n` : The greatest value of `fin (n+1)`.
* `fin.rev : fin n → fin n` : the antitone involution given by `i ↦ n-(i+1)`
-/
universes u v
open fin nat function
/-- Elimination principle for the empty set `fin 0`, dependent version. -/
def fin_zero_elim {α : fin 0 → Sort u} (x : fin 0) : α x := x.elim0
namespace fin
/-- A non-dependent variant of `elim0`. -/
def elim0' {α : Sort*} (x : fin 0) : α := x.elim0
variables {n m : ℕ} {a b : fin n}
instance fin_to_nat (n : ℕ) : has_coe (fin n) nat := ⟨fin.val⟩
lemma val_injective : function.injective (@fin.val n) := @fin.eq_of_veq n
protected lemma prop (a : fin n) : a.val < n := a.2
@[simp] lemma is_lt (a : fin n) : (a : ℕ) < n := a.2
protected lemma pos (i : fin n) : 0 < n := lt_of_le_of_lt (nat.zero_le _) i.is_lt
lemma pos_iff_nonempty {n : ℕ} : 0 < n ↔ nonempty (fin n) := ⟨λ h, ⟨⟨0, h⟩⟩, λ ⟨i⟩, i.pos⟩
/-- Equivalence between `fin n` and `{ i // i < n }`. -/
@[simps apply symm_apply]
def equiv_subtype : fin n ≃ { i // i < n } :=
{ to_fun := λ a, ⟨a.1, a.2⟩, inv_fun := λ a, ⟨a.1, a.2⟩,
left_inv := λ ⟨_, _⟩, rfl, right_inv := λ ⟨_, _⟩, rfl }
section coe
/-!
### coercions and constructions
-/
@[simp] protected lemma eta (a : fin n) (h : (a : ℕ) < n) : (⟨(a : ℕ), h⟩ : fin n) = a :=
by cases a; refl
@[ext]
lemma ext {a b : fin n} (h : (a : ℕ) = b) : a = b := eq_of_veq h
lemma ext_iff {a b : fin n} : a = b ↔ (a : ℕ) = b :=
iff.intro (congr_arg _) fin.eq_of_veq
lemma coe_injective {n : ℕ} : injective (coe : fin n → ℕ) := fin.val_injective
lemma coe_eq_coe (a b : fin n) : (a : ℕ) = b ↔ a = b :=
ext_iff.symm
lemma eq_iff_veq (a b : fin n) : a = b ↔ a.1 = b.1 :=
⟨veq_of_eq, eq_of_veq⟩
lemma ne_iff_vne (a b : fin n) : a ≠ b ↔ a.1 ≠ b.1 :=
⟨vne_of_ne, ne_of_vne⟩
@[simp, nolint simp_nf] -- built-in reduction doesn't always work
theorem mk_eq_mk {a h a' h'} : @mk n a h = @mk n a' h' ↔ a = a' :=
ext_iff
protected lemma mk.inj_iff {n a b : ℕ} {ha : a < n} {hb : b < n} :
(⟨a, ha⟩ : fin n) = ⟨b, hb⟩ ↔ a = b :=
eq_iff_veq _ _
lemma mk_val {m n : ℕ} (h : m < n) : (⟨m, h⟩ : fin n).val = m := rfl
lemma eq_mk_iff_coe_eq {k : ℕ} {hk : k < n} : a = ⟨k, hk⟩ ↔ (a : ℕ) = k :=
fin.eq_iff_veq a ⟨k, hk⟩
@[simp, norm_cast] lemma coe_mk {m n : ℕ} (h : m < n) : ((⟨m, h⟩ : fin n) : ℕ) = m := rfl
lemma mk_coe (i : fin n) : (⟨i, i.property⟩ : fin n) = i :=
fin.eta _ _
lemma coe_eq_val (a : fin n) : (a : ℕ) = a.val := rfl
@[simp] lemma val_eq_coe (a : fin n) : a.val = a := rfl
/-- Assume `k = l`. If two functions defined on `fin k` and `fin l` are equal on each element,
then they coincide (in the heq sense). -/
protected lemma heq_fun_iff {α : Sort*} {k l : ℕ} (h : k = l) {f : fin k → α} {g : fin l → α} :
f == g ↔ (∀ (i : fin k), f i = g ⟨(i : ℕ), h ▸ i.2⟩) :=
by { subst h, simp [function.funext_iff] }
protected lemma heq_ext_iff {k l : ℕ} (h : k = l) {i : fin k} {j : fin l} :
i == j ↔ (i : ℕ) = (j : ℕ) :=
by { subst h, simp [coe_eq_coe] }
lemma exists_iff {p : fin n → Prop} : (∃ i, p i) ↔ ∃ i h, p ⟨i, h⟩ :=
⟨λ h, exists.elim h (λ ⟨i, hi⟩ hpi, ⟨i, hi, hpi⟩),
λ h, exists.elim h (λ i hi, ⟨⟨i, hi.fst⟩, hi.snd⟩)⟩
lemma forall_iff {p : fin n → Prop} : (∀ i, p i) ↔ ∀ i h, p ⟨i, h⟩ :=
⟨λ h i hi, h ⟨i, hi⟩, λ h ⟨i, hi⟩, h i hi⟩
end coe
section order
/-!
### order
-/
lemma is_le (i : fin (n + 1)) : (i : ℕ) ≤ n := le_of_lt_succ i.is_lt
@[simp] lemma is_le' : (a : ℕ) ≤ n := le_of_lt a.is_lt
lemma lt_iff_coe_lt_coe : a < b ↔ (a : ℕ) < b := iff.rfl
lemma le_iff_coe_le_coe : a ≤ b ↔ (a : ℕ) ≤ b := iff.rfl
lemma mk_lt_of_lt_coe {a : ℕ} (h : a < b) : (⟨a, h.trans b.is_lt⟩ : fin n) < b := h
lemma mk_le_of_le_coe {a : ℕ} (h : a ≤ b) : (⟨a, h.trans_lt b.is_lt⟩ : fin n) ≤ b := h
/-- `a < b` as natural numbers if and only if `a < b` in `fin n`. -/
@[norm_cast, simp] lemma coe_fin_lt {n : ℕ} {a b : fin n} : (a : ℕ) < (b : ℕ) ↔ a < b :=
iff.rfl
/-- `a ≤ b` as natural numbers if and only if `a ≤ b` in `fin n`. -/
@[norm_cast, simp] lemma coe_fin_le {n : ℕ} {a b : fin n} : (a : ℕ) ≤ (b : ℕ) ↔ a ≤ b :=
iff.rfl
instance {n : ℕ} : linear_order (fin n) :=
@linear_order.lift (fin n) _ _ ⟨λ x y, ⟨max x y, max_rec' (< n) x.2 y.2⟩⟩
⟨λ x y, ⟨min x y, min_rec' (< n) x.2 y.2⟩⟩ fin.val fin.val_injective (λ _ _, rfl) (λ _ _, rfl)
@[simp] lemma mk_le_mk {x y : nat} {hx} {hy} : (⟨x, hx⟩ : fin n) ≤ ⟨y, hy⟩ ↔ x ≤ y := iff.rfl
@[simp] lemma mk_lt_mk {x y : nat} {hx} {hy} : (⟨x, hx⟩ : fin n) < ⟨y, hy⟩ ↔ x < y := iff.rfl
@[simp] lemma min_coe : min (a : ℕ) n = a := by simp
@[simp] lemma max_coe : max (a : ℕ) n = n := by simp
instance {n : ℕ} : partial_order (fin n) := by apply_instance
lemma coe_strict_mono : strict_mono (coe : fin n → ℕ) := λ _ _, id
/-- The equivalence `fin n ≃ { i // i < n }` is an order isomorphism. -/
@[simps apply symm_apply]
def order_iso_subtype : fin n ≃o { i // i < n } :=
equiv_subtype.to_order_iso (by simp [monotone]) (by simp [monotone])
/-- The inclusion map `fin n → ℕ` is an embedding. -/
@[simps apply]
def coe_embedding : fin n ↪ ℕ :=
⟨coe, coe_injective⟩
@[simp] lemma equiv_subtype_symm_trans_val_embedding :
equiv_subtype.symm.to_embedding.trans coe_embedding = embedding.subtype (< n) :=
rfl
/-- The inclusion map `fin n → ℕ` is an order embedding. -/
@[simps apply]
def coe_order_embedding (n) : (fin n) ↪o ℕ :=
⟨coe_embedding, λ a b, iff.rfl⟩
/-- The ordering on `fin n` is a well order. -/
instance fin.lt.is_well_order (n) : is_well_order (fin n) (<) :=
(coe_order_embedding n).is_well_order
/-- Use the ordering on `fin n` for checking recursive definitions.
For example, the following definition is not accepted by the termination checker,
unless we declare the `has_well_founded` instance:
```lean
def factorial {n : ℕ} : fin n → ℕ
| ⟨0, _⟩ := 1
| ⟨i + 1, hi⟩ := (i + 1) * factorial ⟨i, i.lt_succ_self.trans hi⟩
```
-/
instance {n : ℕ} : has_well_founded (fin n) :=
⟨_, measure_wf coe⟩
instance has_zero_of_ne_zero [ne_zero n] : has_zero (fin n) :=
⟨⟨0, ne_zero.pos _⟩⟩
/-- Given a positive `n`, `fin.of_nat' i` is `i % n` as an element of `fin n`. -/
def of_nat' [ne_zero n] (i : ℕ) : fin n := ⟨i%n, mod_lt _ $ ne_zero.pos n⟩
instance has_one_of_ne_zero [ne_zero n] : has_one (fin n) :=
⟨of_nat' 1⟩
@[simp] lemma coe_zero (n : ℕ) [ne_zero n] : ((0 : fin n) : ℕ) = 0 := rfl
attribute [simp] val_zero
@[simp] lemma val_zero' (n) [ne_zero n] : (0 : fin n).val = 0 := rfl
@[simp] lemma mk_zero [ne_zero n] :
(⟨0, nat.pos_of_ne_zero (ne_zero.ne n)⟩ : fin n) = (0 : fin _) := rfl
@[simp] lemma zero_le [ne_zero n] (a : fin n) : 0 ≤ a := zero_le a.1
lemma zero_lt_one : (0 : fin (n + 2)) < 1 := nat.zero_lt_one
@[simp] lemma not_lt_zero (a : fin n.succ) : ¬a < 0.
lemma pos_iff_ne_zero [ne_zero n] (a : fin n) : 0 < a ↔ a ≠ 0 :=
by rw [← coe_fin_lt, coe_zero, pos_iff_ne_zero, ne.def, ne.def, ext_iff, coe_zero]
lemma eq_zero_or_eq_succ {n : ℕ} (i : fin (n+1)) : i = 0 ∨ ∃ j : fin n, i = j.succ :=
begin
rcases i with ⟨_|j, h⟩,
{ left, refl, },
{ right, exact ⟨⟨j, nat.lt_of_succ_lt_succ h⟩, rfl⟩, }
end
lemma eq_succ_of_ne_zero {n : ℕ} {i : fin (n + 1)} (hi : i ≠ 0) : ∃ j : fin n, i = j.succ :=
(eq_zero_or_eq_succ i).resolve_left hi
/-- The antitone involution `fin n → fin n` given by `i ↦ n-(i+1)`. -/
def rev : equiv.perm (fin n) :=
involutive.to_perm (λ i, ⟨n - (i + 1), tsub_lt_self i.pos (nat.succ_pos _)⟩) $
λ i, ext $ by rw [coe_mk, coe_mk, ← tsub_tsub,
tsub_tsub_cancel_of_le (nat.add_one_le_iff.2 i.is_lt), add_tsub_cancel_right]
@[simp] lemma coe_rev (i : fin n) : (i.rev : ℕ) = n - (i + 1) := rfl
lemma rev_involutive : involutive (@rev n) := involutive.to_perm_involutive _
lemma rev_injective : injective (@rev n) := rev_involutive.injective
lemma rev_surjective : surjective (@rev n) := rev_involutive.surjective
lemma rev_bijective : bijective (@rev n) := rev_involutive.bijective
@[simp] lemma rev_inj {i j : fin n} : i.rev = j.rev ↔ i = j := rev_injective.eq_iff
@[simp] lemma rev_rev (i : fin n) : i.rev.rev = i := rev_involutive _
@[simp] lemma rev_symm : (@rev n).symm = rev := rfl
lemma rev_eq {n a : ℕ} (i : fin (n+1)) (h : n=a+i) :
i.rev = ⟨a, nat.lt_succ_iff.mpr (nat.le.intro (h.symm))⟩ :=
begin
ext,
dsimp,
conv_lhs { congr, rw h, },
rw [add_assoc, add_tsub_cancel_right],
end
@[simp] lemma rev_le_rev {i j : fin n} : i.rev ≤ j.rev ↔ j ≤ i :=
by simp only [le_iff_coe_le_coe, coe_rev, tsub_le_tsub_iff_left (nat.add_one_le_iff.2 j.is_lt),
add_le_add_iff_right]
@[simp] lemma rev_lt_rev {i j : fin n} : i.rev < j.rev ↔ j < i :=
lt_iff_lt_of_le_iff_le rev_le_rev
/-- `fin.rev n` as an order-reversing isomorphism. -/
@[simps apply to_equiv] def rev_order_iso {n} : (fin n)ᵒᵈ ≃o fin n :=
⟨order_dual.of_dual.trans rev, λ i j, rev_le_rev⟩
@[simp] lemma rev_order_iso_symm_apply (i : fin n) :
rev_order_iso.symm i = order_dual.to_dual i.rev := rfl
/-- The greatest value of `fin (n+1)` -/
def last (n : ℕ) : fin (n+1) := ⟨_, n.lt_succ_self⟩
@[simp, norm_cast] lemma coe_last (n : ℕ) : (last n : ℕ) = n := rfl
lemma last_val (n : ℕ) : (last n).val = n := rfl
theorem le_last (i : fin (n+1)) : i ≤ last n :=
le_of_lt_succ i.is_lt
instance : bounded_order (fin (n+1)) :=
{ top := last n,
le_top := le_last,
bot := 0,
bot_le := zero_le }
instance : lattice (fin (n + 1)) := linear_order.to_lattice
lemma last_pos : (0 : fin (n + 2)) < last (n + 1) :=
by simp [lt_iff_coe_lt_coe]
lemma eq_last_of_not_lt {i : fin (n+1)} (h : ¬ (i : ℕ) < n) : i = last n :=
le_antisymm (le_last i) (not_lt.1 h)
lemma top_eq_last (n : ℕ) : ⊤ = fin.last n := rfl
lemma bot_eq_zero (n : ℕ) : ⊥ = (0 : fin (n+1)) := rfl
section
variables {α : Type*} [preorder α]
open set
/-- If `e` is an `order_iso` between `fin n` and `fin m`, then `n = m` and `e` is the identity
map. In this lemma we state that for each `i : fin n` we have `(e i : ℕ) = (i : ℕ)`. -/
@[simp] lemma coe_order_iso_apply (e : fin n ≃o fin m) (i : fin n) : (e i : ℕ) = i :=
begin
rcases i with ⟨i, hi⟩,
rw [fin.coe_mk],
induction i using nat.strong_induction_on with i h,
refine le_antisymm (forall_lt_iff_le.1 $ λ j hj, _) (forall_lt_iff_le.1 $ λ j hj, _),
{ have := e.symm.lt_iff_lt.2 (mk_lt_of_lt_coe hj),
rw e.symm_apply_apply at this,
convert this,
simpa using h _ this (e.symm _).is_lt },
{ rwa [← h j hj (hj.trans hi), ← lt_iff_coe_lt_coe, e.lt_iff_lt] }
end
instance order_iso_subsingleton : subsingleton (fin n ≃o α) :=
⟨λ e e', by { ext i,
rw [← e.symm.apply_eq_iff_eq, e.symm_apply_apply, ← e'.trans_apply, ext_iff,
coe_order_iso_apply] }⟩
instance order_iso_subsingleton' : subsingleton (α ≃o fin n) :=
order_iso.symm_injective.subsingleton
instance order_iso_unique : unique (fin n ≃o fin n) := unique.mk' _
/-- Two strictly monotone functions from `fin n` are equal provided that their ranges
are equal. -/
lemma strict_mono_unique {f g : fin n → α} (hf : strict_mono f) (hg : strict_mono g)
(h : range f = range g) : f = g :=
have (hf.order_iso f).trans (order_iso.set_congr _ _ h) = hg.order_iso g,
from subsingleton.elim _ _,
congr_arg (function.comp (coe : range g → α)) (funext $ rel_iso.ext_iff.1 this)
/-- Two order embeddings of `fin n` are equal provided that their ranges are equal. -/
lemma order_embedding_eq {f g : fin n ↪o α} (h : range f = range g) : f = g :=
rel_embedding.ext $ funext_iff.1 $ strict_mono_unique f.strict_mono g.strict_mono h
end
end order
section add
/-!
### addition, numerals, and coercion from nat
-/
lemma one_val {n : ℕ} : (1 : fin (n+1)).val = 1 % (n+1) := rfl
lemma coe_one' (n : ℕ) [ne_zero n] : ((1 : fin n) : ℕ) = 1 % n := rfl
@[simp] lemma val_one (n : ℕ) : (1 : fin (n+2)).val = 1 := rfl
@[simp] lemma coe_one (n : ℕ) : ((1 : fin (n+2)) : ℕ) = 1 := rfl
@[simp] lemma mk_one : (⟨1, nat.succ_lt_succ (nat.succ_pos n)⟩ : fin (n + 2)) = (1 : fin _) := rfl
instance {n : ℕ} : nontrivial (fin (n + 2)) := ⟨⟨0, 1, dec_trivial⟩⟩
lemma nontrivial_iff_two_le : nontrivial (fin n) ↔ 2 ≤ n :=
by rcases n with _|_|n; simp [fin.nontrivial, not_nontrivial, nat.succ_le_iff]
lemma subsingleton_iff_le_one : subsingleton (fin n) ↔ n ≤ 1 :=
by rcases n with _|_|n; simp [is_empty.subsingleton, unique.subsingleton, not_subsingleton]
section monoid
instance add_comm_semigroup (n : ℕ) : add_comm_semigroup (fin n) :=
{ add := (+),
add_assoc := by simp [eq_iff_veq, add_def, add_assoc],
add_comm := by simp [eq_iff_veq, add_def, add_comm] }
@[simp] protected lemma add_zero [ne_zero n] (k : fin n) : k + 0 = k :=
by simp [eq_iff_veq, add_def, mod_eq_of_lt (is_lt k)]
@[simp] protected lemma zero_add [ne_zero n] (k : fin n) : (0 : fin n) + k = k :=
by simp [eq_iff_veq, add_def, mod_eq_of_lt (is_lt k)]
instance add_comm_monoid (n : ℕ) [ne_zero n] : add_comm_monoid (fin n) :=
{ add := (+),
zero := 0,
zero_add := fin.zero_add,
add_zero := fin.add_zero,
..fin.add_comm_semigroup n }
instance [ne_zero n] : add_monoid_with_one (fin n) :=
{ one := 1,
nat_cast := fin.of_nat',
nat_cast_zero := rfl,
nat_cast_succ := λ i, eq_of_veq (add_mod _ _ _),
.. fin.add_comm_monoid n }
end monoid
lemma val_add {n : ℕ} : ∀ a b : fin n, (a + b).val = (a.val + b.val) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_add {n : ℕ} : ∀ a b : fin n, ((a + b : fin n) : ℕ) = (a + b) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_add_eq_ite {n : ℕ} (a b : fin n) :
(↑(a + b) : ℕ) = if n ≤ a + b then a + b - n else a + b :=
by rw [fin.coe_add, nat.add_mod_eq_ite,
nat.mod_eq_of_lt (show ↑a < n, from a.2), nat.mod_eq_of_lt (show ↑b < n, from b.2)]
lemma coe_bit0 {n : ℕ} (k : fin n) : ((bit0 k : fin n) : ℕ) = bit0 (k : ℕ) % n :=
by { cases k, refl }
lemma coe_bit1 {n : ℕ} [ne_zero n] (k : fin n) :
((bit1 k : fin n) : ℕ) = bit1 (k : ℕ) % n :=
by simp [bit1, coe_add, coe_bit0, coe_one']
lemma coe_add_one_of_lt {n : ℕ} {i : fin n.succ} (h : i < last _) :
(↑(i + 1) : ℕ) = i + 1 :=
begin
-- First show that `((1 : fin n.succ) : ℕ) = 1`, because `n.succ` is at least 2.
cases n,
{ cases h },
-- Then just unfold the definitions.
rw [fin.coe_add, fin.coe_one, nat.mod_eq_of_lt (nat.succ_lt_succ _)],
exact h
end
@[simp] lemma last_add_one : ∀ n, last n + 1 = 0
| 0 := subsingleton.elim _ _
| (n + 1) := by { ext, rw [coe_add, coe_zero, coe_last, coe_one, nat.mod_self] }
lemma coe_add_one {n : ℕ} (i : fin (n + 1)) :
((i + 1 : fin (n + 1)) : ℕ) = if i = last _ then 0 else i + 1 :=
begin
rcases (le_last i).eq_or_lt with rfl|h,
{ simp },
{ simpa [h.ne] using coe_add_one_of_lt h }
end
section bit
@[simp] lemma mk_bit0 {m n : ℕ} (h : bit0 m < n) :
(⟨bit0 m, h⟩ : fin n) = (bit0 ⟨m, (nat.le_add_right m m).trans_lt h⟩ : fin _) :=
eq_of_veq (nat.mod_eq_of_lt h).symm
@[simp] lemma mk_bit1 {m n : ℕ} [ne_zero n] (h : bit1 m < n) :
(⟨bit1 m, h⟩ : fin n) = (bit1 ⟨m, (nat.le_add_right m m).trans_lt
((m + m).lt_succ_self.trans h)⟩ : fin _) :=
begin
ext,
simp only [bit1, bit0] at h,
simp only [bit1, bit0, coe_add, coe_one', coe_mk, ←nat.add_mod, nat.mod_eq_of_lt h],
end
end bit
@[simp] lemma val_two {n : ℕ} : (2 : fin (n+3)).val = 2 := rfl
@[simp] lemma coe_two {n : ℕ} : ((2 : fin (n+3)) : ℕ) = 2 := rfl
section of_nat_coe
@[simp]
lemma of_nat_eq_coe (n : ℕ) (a : ℕ) : (of_nat a : fin (n+1)) = a :=
rfl
@[simp]
lemma of_nat'_eq_coe (n : ℕ) [ne_zero n] (a : ℕ) : (of_nat' a : fin n) = a :=
rfl
/-- Converting an in-range number to `fin n` produces a result
whose value is the original number. -/
lemma coe_val_of_lt {n : ℕ} [ne_zero n] {a : ℕ} (h : a < n) :
((a : fin n).val) = a :=
begin
rw ←of_nat'_eq_coe,
exact nat.mod_eq_of_lt h
end
/-- Converting the value of a `fin n` to `fin n` results
in the same value. -/
lemma coe_val_eq_self {n : ℕ} [ne_zero n] (a : fin n) : (a.val : fin n) = a :=
begin
rw fin.eq_iff_veq,
exact coe_val_of_lt a.property
end
/-- Coercing an in-range number to `fin n`, and converting back
to `ℕ`, results in that number. -/
lemma coe_coe_of_lt {n : ℕ} [ne_zero n] {a : ℕ} (h : a < n) :
((a : fin n) : ℕ) = a :=
coe_val_of_lt h
/-- Converting a `fin n` to `ℕ` and back results in the same
value. -/
@[simp] lemma coe_coe_eq_self {n : ℕ} [ne_zero n] (a : fin n) : ((a : ℕ) : fin n) = a :=
coe_val_eq_self a
lemma coe_nat_eq_last (n) : (n : fin (n + 1)) = fin.last n :=
by { rw [←fin.of_nat_eq_coe, fin.of_nat, fin.last], simp only [nat.mod_eq_of_lt n.lt_succ_self] }
lemma le_coe_last (i : fin (n + 1)) : i ≤ n :=
by { rw fin.coe_nat_eq_last, exact fin.le_last i }
end of_nat_coe
lemma add_one_pos (i : fin (n + 1)) (h : i < fin.last n) : (0 : fin (n + 1)) < i + 1 :=
begin
cases n,
{ exact absurd h (nat.not_lt_zero _) },
{ rw [lt_iff_coe_lt_coe, coe_last, ←add_lt_add_iff_right 1] at h,
rw [lt_iff_coe_lt_coe, coe_add, coe_zero, coe_one, nat.mod_eq_of_lt h],
exact nat.zero_lt_succ _ }
end
lemma one_pos : (0 : fin (n + 2)) < 1 := succ_pos 0
lemma zero_ne_one : (0 : fin (n + 2)) ≠ 1 := ne_of_lt one_pos
@[simp] lemma zero_eq_one_iff [ne_zero n]: (0 : fin n) = 1 ↔ n = 1 :=
begin
split,
{ intro h,
have := congr_arg (coe : fin n → ℕ) h,
simp only [fin.coe_zero, ← nat.dvd_iff_mod_eq_zero, fin.coe_one', @eq_comm _ 0] at this,
exact eq_one_of_dvd_one this },
{ unfreezingI { rintro rfl }, refl }
end
@[simp] lemma one_eq_zero_iff [ne_zero n]: (1 : fin n) = 0 ↔ n = 1 :=
by rw [eq_comm, zero_eq_one_iff]
end add
section succ
/-!
### succ and casts into larger fin types
-/
@[simp] lemma coe_succ (j : fin n) : (j.succ : ℕ) = j + 1 :=
by cases j; simp [fin.succ]
@[simp]
lemma succ_pos (a : fin n) : (0 : fin (n + 1)) < a.succ := by simp [lt_iff_coe_lt_coe]
/-- `fin.succ` as an `order_embedding` -/
def succ_embedding (n : ℕ) : fin n ↪o fin (n + 1) :=
order_embedding.of_strict_mono fin.succ $ λ ⟨i, hi⟩ ⟨j, hj⟩ h, succ_lt_succ h
@[simp] lemma coe_succ_embedding : ⇑(succ_embedding n) = fin.succ := rfl
@[simp] lemma succ_le_succ_iff : a.succ ≤ b.succ ↔ a ≤ b :=
(succ_embedding n).le_iff_le
@[simp] lemma succ_lt_succ_iff : a.succ < b.succ ↔ a < b :=
(succ_embedding n).lt_iff_lt
lemma succ_injective (n : ℕ) : injective (@fin.succ n) :=
(succ_embedding n).injective
@[simp] lemma succ_inj {a b : fin n} : a.succ = b.succ ↔ a = b :=
(succ_injective n).eq_iff
lemma succ_ne_zero {n} : ∀ k : fin n, fin.succ k ≠ 0
| ⟨k, hk⟩ heq := nat.succ_ne_zero k $ ext_iff.1 heq
@[simp] lemma succ_zero_eq_one [ne_zero n] : fin.succ (0 : fin n) = 1 :=
begin
unfreezingI { cases n },
{ exact (ne_zero.ne 0 rfl).elim },
{ refl }
end
/-- Version of `succ_zero_eq_one` to be used by `dsimp` -/
@[simp] lemma succ_zero_eq_one' : fin.succ (0 : fin (n+1)) = 1 := rfl
@[simp] lemma succ_one_eq_two [ne_zero n] : fin.succ (1 : fin (n + 1)) = 2 :=
begin
unfreezingI { cases n },
{ exact (ne_zero.ne 0 rfl).elim },
{ refl }
end
/-- Version of `succ_one_eq_two` to be used by `dsimp` -/
@[simp] lemma succ_one_eq_two' : fin.succ (1 : fin (n + 2)) = 2 := rfl
@[simp] lemma succ_mk (n i : ℕ) (h : i < n) : fin.succ ⟨i, h⟩ = ⟨i + 1, nat.succ_lt_succ h⟩ :=
rfl
lemma mk_succ_pos (i : ℕ) (h : i < n) : (0 : fin (n + 1)) < ⟨i.succ, add_lt_add_right h 1⟩ :=
by { rw [lt_iff_coe_lt_coe, coe_zero], exact nat.succ_pos i }
lemma one_lt_succ_succ (a : fin n) : (1 : fin (n + 2)) < a.succ.succ :=
begin
cases n,
{ exact fin_zero_elim a },
{ rw [←succ_zero_eq_one, succ_lt_succ_iff], exact succ_pos a }
end
@[simp] lemma add_one_lt_iff {n : ℕ} {k : fin (n + 2)} :
k + 1 < k ↔ k = last _ :=
begin
simp only [lt_iff_coe_lt_coe, coe_add, coe_last, ext_iff],
cases k with k hk,
rcases (le_of_lt_succ hk).eq_or_lt with rfl|hk',
{ simp },
{ simp [hk'.ne, mod_eq_of_lt (succ_lt_succ hk'), le_succ _] }
end
@[simp] lemma add_one_le_iff {n : ℕ} {k : fin (n + 1)} :
k + 1 ≤ k ↔ k = last _ :=
begin
cases n,
{ simp [subsingleton.elim (k + 1) k, subsingleton.elim (fin.last _) k] },
rw [←not_iff_not, ←add_one_lt_iff, lt_iff_le_and_ne, not_and'],
refine ⟨λ h _, h, λ h, h _⟩,
rw [ne.def, ext_iff, coe_add_one],
split_ifs with hk hk;
simp [hk, eq_comm],
end
@[simp] lemma last_le_iff {n : ℕ} {k : fin (n + 1)} :
last n ≤ k ↔ k = last n :=
top_le_iff
@[simp] lemma lt_add_one_iff {n : ℕ} {k : fin (n + 1)} :
k < k + 1 ↔ k < last n :=
begin
rw ←not_iff_not,
simp
end
@[simp] lemma le_zero_iff {n : ℕ} [ne_zero n] {k : fin n} :
k ≤ 0 ↔ k = 0 :=
⟨λ h, fin.eq_of_veq $ by rw [nat.eq_zero_of_le_zero h]; refl, by rintro rfl; refl⟩
lemma succ_succ_ne_one (a : fin n) : fin.succ (fin.succ a) ≠ 1 := ne_of_gt (one_lt_succ_succ a)
/-- `cast_lt i h` embeds `i` into a `fin` where `h` proves it belongs into. -/
def cast_lt (i : fin m) (h : i.1 < n) : fin n := ⟨i.1, h⟩
@[simp] lemma coe_cast_lt (i : fin m) (h : i.1 < n) : (cast_lt i h : ℕ) = i := rfl
@[simp] lemma cast_lt_mk (i n m : ℕ) (hn : i < n) (hm : i < m) : cast_lt ⟨i, hn⟩ hm = ⟨i, hm⟩ := rfl
/-- `cast_le h i` embeds `i` into a larger `fin` type. -/
def cast_le (h : n ≤ m) : fin n ↪o fin m :=
order_embedding.of_strict_mono (λ a, cast_lt a (lt_of_lt_of_le a.2 h)) $ λ a b h, h
@[simp] lemma coe_cast_le (h : n ≤ m) (i : fin n) : (cast_le h i : ℕ) = i := rfl
@[simp] lemma cast_le_mk (i n m : ℕ) (hn : i < n) (h : n ≤ m) :
cast_le h ⟨i, hn⟩ = ⟨i, lt_of_lt_of_le hn h⟩ := rfl
@[simp] lemma cast_le_zero {n m : ℕ} (h : n.succ ≤ m.succ) :
cast_le h 0 = 0 :=
by simp [eq_iff_veq]
@[simp] lemma range_cast_le {n k : ℕ} (h : n ≤ k) :
set.range (cast_le h) = {i | (i : ℕ) < n} :=
set.ext (λ x, ⟨λ ⟨y, hy⟩, hy ▸ y.2, λ hx, ⟨⟨x, hx⟩, fin.ext rfl⟩⟩)
@[simp] lemma coe_of_injective_cast_le_symm {n k : ℕ} (h : n ≤ k) (i : fin k) (hi) :
((equiv.of_injective _ (cast_le h).injective).symm ⟨i, hi⟩ : ℕ) = i :=
begin
rw ← coe_cast_le,
exact congr_arg coe (equiv.apply_of_injective_symm _ _)
end
@[simp] lemma cast_le_succ {m n : ℕ} (h : (m + 1) ≤ (n + 1)) (i : fin m) :
cast_le h i.succ = (cast_le (nat.succ_le_succ_iff.mp h) i).succ :=
by simp [fin.eq_iff_veq]
@[simp] lemma cast_le_cast_le {k m n} (km : k ≤ m) (mn : m ≤ n) (i : fin k) :
fin.cast_le mn (fin.cast_le km i) = fin.cast_le (km.trans mn) i :=
fin.ext (by simp only [coe_cast_le])
@[simp] lemma cast_le_comp_cast_le {k m n} (km : k ≤ m) (mn : m ≤ n) :
fin.cast_le mn ∘ fin.cast_le km = fin.cast_le (km.trans mn) :=
funext (cast_le_cast_le km mn)
/-- `cast eq i` embeds `i` into a equal `fin` type, see also `equiv.fin_congr`. -/
def cast (eq : n = m) : fin n ≃o fin m :=
{ to_equiv := ⟨cast_le eq.le, cast_le eq.symm.le, λ a, eq_of_veq rfl, λ a, eq_of_veq rfl⟩,
map_rel_iff' := λ a b, iff.rfl }
@[simp] lemma symm_cast (h : n = m) : (cast h).symm = cast h.symm := rfl
/-- While `fin.coe_order_iso_apply` is a more general case of this, we mark this `simp` anyway
as it is eligible for `dsimp`. -/
@[simp]
lemma coe_cast (h : n = m) (i : fin n) : (cast h i : ℕ) = i := rfl
@[simp] lemma cast_zero {n' : ℕ} [ne_zero n] {h : n = n'} :
cast h (0 : fin n) = by { haveI : ne_zero n' := by { rw ← h; apply_instance }, exact 0 } :=
ext rfl
@[simp] lemma cast_last {n' : ℕ} {h : n + 1 = n' + 1} :
cast h (last n) = last n' :=
ext (by rw [coe_cast, coe_last, coe_last, nat.succ_injective h])
@[simp] lemma cast_mk (h : n = m) (i : ℕ) (hn : i < n) :
cast h ⟨i, hn⟩ = ⟨i, lt_of_lt_of_le hn h.le⟩ := rfl
@[simp] lemma cast_trans {k : ℕ} (h : n = m) (h' : m = k) {i : fin n} :
cast h' (cast h i) = cast (eq.trans h h') i := rfl
@[simp] lemma cast_refl (h : n = n := rfl) : cast h = order_iso.refl (fin n) :=
by { ext, refl }
lemma cast_le_of_eq {m n : ℕ} (h : m = n) {h' : m ≤ n} :
(cast_le h' : fin m → fin n) = fin.cast h :=
funext (λ _, rfl)
/-- While in many cases `fin.cast` is better than `equiv.cast`/`cast`, sometimes we want to apply
a generic theorem about `cast`. -/
lemma cast_to_equiv (h : n = m) : (cast h).to_equiv = equiv.cast (h ▸ rfl) :=
by { subst h, simp }
/-- While in many cases `fin.cast` is better than `equiv.cast`/`cast`, sometimes we want to apply
a generic theorem about `cast`. -/
lemma cast_eq_cast (h : n = m) : (cast h : fin n → fin m) = _root_.cast (h ▸ rfl) :=
by { subst h, ext, simp }
/-- `cast_add m i` embeds `i : fin n` in `fin (n+m)`. See also `fin.nat_add` and `fin.add_nat`. -/
def cast_add (m) : fin n ↪o fin (n + m) := cast_le $ nat.le_add_right n m
@[simp] lemma coe_cast_add (m : ℕ) (i : fin n) : (cast_add m i : ℕ) = i := rfl
@[simp] lemma cast_add_zero : (cast_add 0 : fin n → fin (n + 0)) = cast rfl := rfl
lemma cast_add_lt {m : ℕ} (n : ℕ) (i : fin m) : (cast_add n i : ℕ) < m := i.2
@[simp] lemma cast_add_mk (m : ℕ) (i : ℕ) (h : i < n) :
cast_add m ⟨i, h⟩ = ⟨i, lt_add_right i n m h⟩ := rfl
@[simp] lemma cast_add_cast_lt (m : ℕ) (i : fin (n + m)) (hi : i.val < n) :
cast_add m (cast_lt i hi) = i :=
ext rfl
@[simp] lemma cast_lt_cast_add (m : ℕ) (i : fin n) :
cast_lt (cast_add m i) (cast_add_lt m i) = i :=
ext rfl
/-- For rewriting in the reverse direction, see `fin.cast_cast_add_left`. -/
lemma cast_add_cast {n n' : ℕ} (m : ℕ) (i : fin n') (h : n' = n) :
cast_add m (fin.cast h i) = fin.cast (congr_arg _ h) (cast_add m i) :=
ext rfl
lemma cast_cast_add_left {n n' m : ℕ} (i : fin n') (h : n' + m = n + m) :
cast h (cast_add m i) = cast_add m (cast (add_right_cancel h) i) :=
ext rfl
@[simp] lemma cast_cast_add_right {n m m' : ℕ} (i : fin n) (h : n + m' = n + m) :
cast h (cast_add m' i) = cast_add m i :=
ext rfl
lemma cast_add_cast_add {m n p : ℕ} (i : fin m) :
cast_add p (cast_add n i) = cast (add_assoc _ _ _).symm (cast_add (n + p) i) :=
ext rfl
/-- The cast of the successor is the succesor of the cast. See `fin.succ_cast_eq` for rewriting in
the reverse direction. -/
@[simp] lemma cast_succ_eq {n' : ℕ} (i : fin n) (h : n.succ = n'.succ) :
cast h i.succ = (cast (nat.succ.inj h) i).succ :=
ext $ by simp
lemma succ_cast_eq {n' : ℕ} (i : fin n) (h : n = n') : (cast h i).succ = cast (by rw h) i.succ :=
ext $ by simp
/-- `cast_succ i` embeds `i : fin n` in `fin (n+1)`. -/
def cast_succ : fin n ↪o fin (n + 1) := cast_add 1
@[simp] lemma coe_cast_succ (i : fin n) : (i.cast_succ : ℕ) = i := rfl
@[simp] lemma cast_succ_mk (n i : ℕ) (h : i < n) : cast_succ ⟨i, h⟩ = ⟨i, nat.lt.step h⟩ := rfl
@[simp] lemma cast_cast_succ {n' : ℕ} {h : n + 1 = n' + 1} {i : fin n} :
cast h (cast_succ i) = cast_succ (cast (nat.succ_injective h) i) :=
by { ext, simp only [coe_cast, coe_cast_succ] }
lemma cast_succ_lt_succ (i : fin n) : i.cast_succ < i.succ :=
lt_iff_coe_lt_coe.2 $ by simp only [coe_cast_succ, coe_succ, nat.lt_succ_self]
lemma le_cast_succ_iff {i : fin (n + 1)} {j : fin n} : i ≤ j.cast_succ ↔ i < j.succ :=
by simpa [lt_iff_coe_lt_coe, le_iff_coe_le_coe] using nat.succ_le_succ_iff.symm
lemma cast_succ_lt_iff_succ_le {n : ℕ} {i : fin n} {j : fin (n+1)} :
i.cast_succ < j ↔ i.succ ≤ j :=
by simpa only [fin.lt_iff_coe_lt_coe, fin.le_iff_coe_le_coe, fin.coe_succ, fin.coe_cast_succ]
using nat.lt_iff_add_one_le
@[simp] lemma succ_last (n : ℕ) : (last n).succ = last (n.succ) := rfl
@[simp] lemma succ_eq_last_succ {n : ℕ} (i : fin n.succ) :
i.succ = last (n + 1) ↔ i = last n :=
by rw [← succ_last, (succ_injective _).eq_iff]
@[simp] lemma cast_succ_cast_lt (i : fin (n + 1)) (h : (i : ℕ) < n) : cast_succ (cast_lt i h) = i :=
fin.eq_of_veq rfl
@[simp] lemma cast_lt_cast_succ {n : ℕ} (a : fin n) (h : (a : ℕ) < n) :
cast_lt (cast_succ a) h = a :=
by cases a; refl
@[simp] lemma cast_succ_lt_cast_succ_iff : a.cast_succ < b.cast_succ ↔ a < b :=
(@cast_succ n).lt_iff_lt
lemma cast_succ_injective (n : ℕ) : injective (@fin.cast_succ n) :=
(cast_succ : fin n ↪o _).injective
lemma cast_succ_inj {a b : fin n} : a.cast_succ = b.cast_succ ↔ a = b :=
(cast_succ_injective n).eq_iff
lemma cast_succ_lt_last (a : fin n) : cast_succ a < last n := lt_iff_coe_lt_coe.mpr a.is_lt
@[simp] lemma cast_succ_zero [ne_zero n] : cast_succ (0 : fin n) = 0 := rfl
@[simp] lemma cast_succ_one {n : ℕ} : fin.cast_succ (1 : fin (n + 2)) = 1 := rfl
/-- `cast_succ i` is positive when `i` is positive -/
lemma cast_succ_pos [ne_zero n] {i : fin n} (h : 0 < i) : 0 < cast_succ i :=
by simpa [lt_iff_coe_lt_coe] using h
@[simp] lemma cast_succ_eq_zero_iff [ne_zero n] (a : fin n) : a.cast_succ = 0 ↔ a = 0 :=
fin.ext_iff.trans $ (fin.ext_iff.trans $ by exact iff.rfl).symm
lemma cast_succ_ne_zero_iff [ne_zero n] (a : fin n) : a.cast_succ ≠ 0 ↔ a ≠ 0 :=
not_iff_not.mpr $ cast_succ_eq_zero_iff a
lemma cast_succ_fin_succ (n : ℕ) (j : fin n) :
cast_succ (fin.succ j) = fin.succ (cast_succ j) :=
by simp [fin.ext_iff]
@[norm_cast, simp] lemma coe_eq_cast_succ : (a : fin (n + 1)) = a.cast_succ :=
begin
ext,
exact coe_val_of_lt (nat.lt.step a.is_lt),
end
@[simp] lemma coe_succ_eq_succ : a.cast_succ + 1 = a.succ :=
begin
cases n,
{ exact fin_zero_elim a },
{ simp [a.is_lt, eq_iff_veq, add_def, nat.mod_eq_of_lt] }
end
lemma lt_succ : a.cast_succ < a.succ :=
by { rw [cast_succ, lt_iff_coe_lt_coe, coe_cast_add, coe_succ], exact lt_add_one a.val }
@[simp] lemma range_cast_succ {n : ℕ} :
set.range (cast_succ : fin n → fin n.succ) = {i | (i : ℕ) < n} :=
range_cast_le _
@[simp] lemma coe_of_injective_cast_succ_symm {n : ℕ} (i : fin (n+1)) (hi) :
((equiv.of_injective cast_succ (cast_succ_injective _)).symm ⟨i, hi⟩ : ℕ) = i :=
begin
rw ← coe_cast_succ,
exact congr_arg coe (equiv.apply_of_injective_symm _ _)
end
lemma succ_cast_succ {n : ℕ} (i : fin n) :
i.cast_succ.succ = i.succ.cast_succ :=
fin.ext (by simp)
/-- `add_nat m i` adds `m` to `i`, generalizes `fin.succ`. -/
def add_nat (m) : fin n ↪o fin (n + m) :=
order_embedding.of_strict_mono (λ i, ⟨(i : ℕ) + m, add_lt_add_right i.2 _⟩) $
λ i j h, lt_iff_coe_lt_coe.2 $ add_lt_add_right h _
@[simp] lemma coe_add_nat (m : ℕ) (i : fin n) : (add_nat m i : ℕ) = i + m := rfl
@[simp] lemma add_nat_one {i : fin n} : add_nat 1 i = i.succ :=
by { ext, rw [coe_add_nat, coe_succ] }
lemma le_coe_add_nat (m : ℕ) (i : fin n) : m ≤ add_nat m i := nat.le_add_left _ _
@[simp] lemma add_nat_mk (n i : ℕ) (hi : i < m) :
add_nat n ⟨i, hi⟩ = ⟨i + n, add_lt_add_right hi n⟩ := rfl
@[simp] lemma cast_add_nat_zero {n n' : ℕ} (i : fin n) (h : n + 0 = n') :
cast h (add_nat 0 i) = cast ((add_zero _).symm.trans h) i :=
ext $ add_zero _
/-- For rewriting in the reverse direction, see `fin.cast_add_nat_left`. -/
lemma add_nat_cast {n n' m : ℕ} (i : fin n') (h : n' = n) :
add_nat m (cast h i) = cast (congr_arg _ h) (add_nat m i) :=
ext rfl
lemma cast_add_nat_left {n n' m : ℕ} (i : fin n') (h : n' + m = n + m) :
cast h (add_nat m i) = add_nat m (cast (add_right_cancel h) i) :=
ext rfl
@[simp] lemma cast_add_nat_right {n m m' : ℕ} (i : fin n) (h : n + m' = n + m) :
cast h (add_nat m' i) = add_nat m i :=
ext $ (congr_arg ((+) (i : ℕ)) (add_left_cancel h) : _)
/-- `nat_add n i` adds `n` to `i` "on the left". -/
def nat_add (n) {m} : fin m ↪o fin (n + m) :=
order_embedding.of_strict_mono (λ i, ⟨n + (i : ℕ), add_lt_add_left i.2 _⟩) $
λ i j h, lt_iff_coe_lt_coe.2 $ add_lt_add_left h _
@[simp] lemma coe_nat_add (n : ℕ) {m : ℕ} (i : fin m) : (nat_add n i : ℕ) = n + i := rfl
@[simp] lemma nat_add_mk (n i : ℕ) (hi : i < m) :
nat_add n ⟨i, hi⟩ = ⟨n + i, add_lt_add_left hi n⟩ := rfl
lemma le_coe_nat_add (m : ℕ) (i : fin n) : m ≤ nat_add m i := nat.le_add_right _ _
lemma nat_add_zero {n : ℕ} : fin.nat_add 0 = (fin.cast (zero_add n).symm).to_rel_embedding :=
by { ext, apply zero_add }
/-- For rewriting in the reverse direction, see `fin.cast_nat_add_right`. -/
lemma nat_add_cast {n n' : ℕ} (m : ℕ) (i : fin n') (h : n' = n) :
nat_add m (cast h i) = cast (congr_arg _ h) (nat_add m i) :=
ext rfl
lemma cast_nat_add_right {n n' m : ℕ} (i : fin n') (h : m + n' = m + n) :
cast h (nat_add m i) = nat_add m (cast (add_left_cancel h) i) :=
ext rfl
@[simp] lemma cast_nat_add_left {n m m' : ℕ} (i : fin n) (h : m' + n = m + n) :
cast h (nat_add m' i) = nat_add m i :=
ext $ (congr_arg (+ (i : ℕ)) (add_right_cancel h) : _)
lemma cast_add_nat_add (p m : ℕ) {n : ℕ} (i : fin n) :
cast_add p (nat_add m i) = cast (add_assoc _ _ _).symm (nat_add m (cast_add p i)) :=
ext rfl
lemma nat_add_cast_add (p m : ℕ) {n : ℕ} (i : fin n) :
nat_add m (cast_add p i) = cast (add_assoc _ _ _) (cast_add p (nat_add m i)) :=
ext rfl
lemma nat_add_nat_add (m n : ℕ) {p : ℕ} (i : fin p) :
nat_add m (nat_add n i) = cast (add_assoc _ _ _) (nat_add (m + n) i) :=
ext $ (add_assoc _ _ _).symm
@[simp] lemma cast_nat_add_zero {n n' : ℕ} (i : fin n) (h : 0 + n = n') :
cast h (nat_add 0 i) = cast ((zero_add _).symm.trans h) i :=
ext $ zero_add _
@[simp] lemma cast_nat_add (n : ℕ) {m : ℕ} (i : fin m) :
cast (add_comm _ _) (nat_add n i) = add_nat n i :=
ext $ add_comm _ _
@[simp] lemma cast_add_nat {n : ℕ} (m : ℕ) (i : fin n) :
cast (add_comm _ _) (add_nat m i) = nat_add m i :=
ext $ add_comm _ _
@[simp] lemma nat_add_last {m n : ℕ} : nat_add n (last m) = last (n + m) := rfl
lemma nat_add_cast_succ {m n : ℕ} {i : fin m} :
nat_add n (cast_succ i) = cast_succ (nat_add n i) := rfl
end succ
section pred
/-!
### pred
-/
@[simp] lemma coe_pred (j : fin (n+1)) (h : j ≠ 0) : (j.pred h : ℕ) = j - 1 :=
by { cases j, refl }
@[simp] lemma succ_pred : ∀(i : fin (n+1)) (h : i ≠ 0), (i.pred h).succ = i
| ⟨0, h⟩ hi := by contradiction
| ⟨n + 1, h⟩ hi := rfl
@[simp] lemma pred_succ (i : fin n) {h : i.succ ≠ 0} : i.succ.pred h = i :=
by { cases i, refl }
lemma pred_eq_iff_eq_succ {n : ℕ} (i : fin (n+1)) (hi : i ≠ 0) (j : fin n) :
i.pred hi = j ↔ i = j.succ :=
⟨λ h, by simp only [← h, fin.succ_pred], λ h, by simp only [h, fin.pred_succ]⟩
@[simp] lemma pred_mk_succ (i : ℕ) (h : i < n + 1) :
fin.pred ⟨i + 1, add_lt_add_right h 1⟩ (ne_of_vne (ne_of_gt (mk_succ_pos i h))) = ⟨i, h⟩ :=
by simp only [ext_iff, coe_pred, coe_mk, add_tsub_cancel_right]
-- This is not a simp lemma by default, because `pred_mk_succ` is nicer when it applies.
lemma pred_mk {n : ℕ} (i : ℕ) (h : i < n + 1) (w) :
fin.pred ⟨i, h⟩ w =
⟨i - 1, by rwa tsub_lt_iff_right (nat.succ_le_of_lt $ nat.pos_of_ne_zero (fin.vne_of_ne w))⟩ :=
rfl
@[simp] lemma pred_le_pred_iff {n : ℕ} {a b : fin n.succ} {ha : a ≠ 0} {hb : b ≠ 0} :
a.pred ha ≤ b.pred hb ↔ a ≤ b :=
by rw [←succ_le_succ_iff, succ_pred, succ_pred]
@[simp] lemma pred_lt_pred_iff {n : ℕ} {a b : fin n.succ} {ha : a ≠ 0} {hb : b ≠ 0} :
a.pred ha < b.pred hb ↔ a < b :=
by rw [←succ_lt_succ_iff, succ_pred, succ_pred]
@[simp] lemma pred_inj :
∀ {a b : fin (n + 1)} {ha : a ≠ 0} {hb : b ≠ 0}, a.pred ha = b.pred hb ↔ a = b
| ⟨0, _⟩ b ha hb := by contradiction
| ⟨i+1, _⟩ ⟨0, _⟩ ha hb := by contradiction
| ⟨i+1, hi⟩ ⟨j+1, hj⟩ ha hb := by simp [fin.eq_iff_veq]
@[simp] lemma pred_one {n : ℕ} : fin.pred (1 : fin (n + 2)) (ne.symm (ne_of_lt one_pos)) = 0 := rfl
lemma pred_add_one (i : fin (n + 2)) (h : (i : ℕ) < n + 1) :
pred (i + 1) (ne_of_gt (add_one_pos _ (lt_iff_coe_lt_coe.mpr h))) = cast_lt i h :=
begin
rw [ext_iff, coe_pred, coe_cast_lt, coe_add, coe_one, mod_eq_of_lt, add_tsub_cancel_right],
exact add_lt_add_right h 1,
end
/-- `sub_nat i h` subtracts `m` from `i`, generalizes `fin.pred`. -/
def sub_nat (m) (i : fin (n + m)) (h : m ≤ (i : ℕ)) : fin n :=
⟨(i : ℕ) - m, by { rw [tsub_lt_iff_right h], exact i.is_lt }⟩
@[simp] lemma coe_sub_nat (i : fin (n + m)) (h : m ≤ i) : (i.sub_nat m h : ℕ) = i - m :=
rfl
@[simp] lemma sub_nat_mk {i : ℕ} (h₁ : i < n + m) (h₂ : m ≤ i) :
sub_nat m ⟨i, h₁⟩ h₂ = ⟨i - m, (tsub_lt_iff_right h₂).2 h₁⟩ :=
rfl
@[simp] lemma pred_cast_succ_succ (i : fin n) :
pred (cast_succ i.succ) (ne_of_gt (cast_succ_pos i.succ_pos)) = i.cast_succ :=
by simp [eq_iff_veq]
@[simp] lemma add_nat_sub_nat {i : fin (n + m)} (h : m ≤ i) :
add_nat m (sub_nat m i h) = i :=
ext $ tsub_add_cancel_of_le h
@[simp] lemma sub_nat_add_nat (i : fin n) (m : ℕ) (h : m ≤ add_nat m i := le_coe_add_nat m i) :
sub_nat m (add_nat m i) h = i :=
ext $ add_tsub_cancel_right i m
@[simp] lemma nat_add_sub_nat_cast {i : fin (n + m)} (h : n ≤ i) :
nat_add n (sub_nat n (cast (add_comm _ _) i) h) = i :=
by simp [← cast_add_nat]
end pred
section div_mod
/-- Compute `i / n`, where `n` is a `nat` and inferred the type of `i`. -/
def div_nat (i : fin (m * n)) : fin m :=
⟨i / n, nat.div_lt_of_lt_mul $ mul_comm m n ▸ i.prop⟩
@[simp] lemma coe_div_nat (i : fin (m * n)) : (i.div_nat : ℕ) = i / n := rfl
/-- Compute `i % n`, where `n` is a `nat` and inferred the type of `i`. -/
def mod_nat (i : fin (m * n)) : fin n :=
⟨i % n, nat.mod_lt _ $ pos_of_mul_pos_right i.pos m.zero_le⟩
@[simp] lemma coe_mod_nat (i : fin (m * n)) : (i.mod_nat : ℕ) = i % n := rfl
end div_mod
section rec
/-!
### recursion and induction principles
-/
/-- Define `C n i` by induction on `i : fin n` interpreted as `(0 : fin (n - i)).succ.succ…`.
This function has two arguments: `H0 n` defines `0`-th element `C (n+1) 0` of an `(n+1)`-tuple,
and `Hs n i` defines `(i+1)`-st element of `(n+1)`-tuple based on `n`, `i`, and `i`-th element
of `n`-tuple. -/
@[elab_as_eliminator] def succ_rec
{C : Π n, fin n → Sort*}
(H0 : Π n, C (succ n) 0)
(Hs : Π n i, C n i → C (succ n) i.succ) : Π {n : ℕ} (i : fin n), C n i
| 0 i := i.elim0
| (succ n) ⟨0, _⟩ := H0 _
| (succ n) ⟨succ i, h⟩ := Hs _ _ (succ_rec ⟨i, lt_of_succ_lt_succ h⟩)
/-- Define `C n i` by induction on `i : fin n` interpreted as `(0 : fin (n - i)).succ.succ…`.
This function has two arguments: `H0 n` defines `0`-th element `C (n+1) 0` of an `(n+1)`-tuple,
and `Hs n i` defines `(i+1)`-st element of `(n+1)`-tuple based on `n`, `i`, and `i`-th element
of `n`-tuple.
A version of `fin.succ_rec` taking `i : fin n` as the first argument. -/
@[elab_as_eliminator] def succ_rec_on {n : ℕ} (i : fin n)
{C : Π n, fin n → Sort*}
(H0 : Π n, C (succ n) 0)
(Hs : Π n i, C n i → C (succ n) i.succ) : C n i :=
i.succ_rec H0 Hs
@[simp] theorem succ_rec_on_zero {C : ∀ n, fin n → Sort*} {H0 Hs} (n) :
@fin.succ_rec_on (succ n) 0 C H0 Hs = H0 n :=
rfl
@[simp] theorem succ_rec_on_succ {C : ∀ n, fin n → Sort*} {H0 Hs} {n} (i : fin n) :
@fin.succ_rec_on (succ n) i.succ C H0 Hs = Hs n i (fin.succ_rec_on i H0 Hs) :=
by cases i; refl
/--
Define `C i` by induction on `i : fin (n + 1)` via induction on the underlying `nat` value.
This function has two arguments: `h0` handles the base case on `C 0`,
and `hs` defines the inductive step using `C i.cast_succ`.
-/
@[elab_as_eliminator] def induction
{C : fin (n + 1) → Sort*}
(h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) :
Π (i : fin (n + 1)), C i :=
begin
rintro ⟨i, hi⟩,
induction i with i IH,
{ rwa [fin.mk_zero] },
{ refine hs ⟨i, lt_of_succ_lt_succ hi⟩ _,
exact IH (lt_of_succ_lt hi) }
end
@[simp] lemma induction_zero {C : fin (n + 1) → Sort*} (h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) :
(induction h0 hs : _) 0 = h0 := rfl
@[simp] lemma induction_succ {C : fin (n + 1) → Sort*} (h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) (i : fin n) :
(induction h0 hs : _) i.succ = hs i (induction h0 hs i.cast_succ) := by cases i; refl
/--
Define `C i` by induction on `i : fin (n + 1)` via induction on the underlying `nat` value.
This function has two arguments: `h0` handles the base case on `C 0`,
and `hs` defines the inductive step using `C i.cast_succ`.
A version of `fin.induction` taking `i : fin (n + 1)` as the first argument.
-/
@[elab_as_eliminator] def induction_on (i : fin (n + 1))
{C : fin (n + 1) → Sort*}
(h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) : C i :=
induction h0 hs i
/-- Define `f : Π i : fin n.succ, C i` by separately handling the cases `i = 0` and
`i = j.succ`, `j : fin n`. -/
@[elab_as_eliminator] def cases
{C : fin (succ n) → Sort*} (H0 : C 0) (Hs : Π i : fin n, C (i.succ)) :
Π (i : fin (succ n)), C i :=
induction H0 (λ i _, Hs i)
@[simp] theorem cases_zero {n} {C : fin (succ n) → Sort*} {H0 Hs} : @fin.cases n C H0 Hs 0 = H0 :=
rfl
@[simp] theorem cases_succ {n} {C : fin (succ n) → Sort*} {H0 Hs} (i : fin n) :
@fin.cases n C H0 Hs i.succ = Hs i :=
by cases i; refl
@[simp] theorem cases_succ' {n} {C : fin (succ n) → Sort*} {H0 Hs} {i : ℕ} (h : i + 1 < n + 1) :
@fin.cases n C H0 Hs ⟨i.succ, h⟩ = Hs ⟨i, lt_of_succ_lt_succ h⟩ :=
by cases i; refl
lemma forall_fin_succ {P : fin (n+1) → Prop} :
(∀ i, P i) ↔ P 0 ∧ (∀ i:fin n, P i.succ) :=
⟨λ H, ⟨H 0, λ i, H _⟩, λ ⟨H0, H1⟩ i, fin.cases H0 H1 i⟩
lemma exists_fin_succ {P : fin (n+1) → Prop} :
(∃ i, P i) ↔ P 0 ∨ (∃i:fin n, P i.succ) :=
⟨λ ⟨i, h⟩, fin.cases or.inl (λ i hi, or.inr ⟨i, hi⟩) i h,
λ h, h.elim (λ h, ⟨0, h⟩) $ λ⟨i, hi⟩, ⟨i.succ, hi⟩⟩
lemma forall_fin_one {p : fin 1 → Prop} : (∀ i, p i) ↔ p 0 := @unique.forall_iff (fin 1) _ p
lemma exists_fin_one {p : fin 1 → Prop} : (∃ i, p i) ↔ p 0 := @unique.exists_iff (fin 1) _ p
lemma forall_fin_two {p : fin 2 → Prop} : (∀ i, p i) ↔ p 0 ∧ p 1 :=
forall_fin_succ.trans $ and_congr_right $ λ _, forall_fin_one
lemma exists_fin_two {p : fin 2 → Prop} : (∃ i, p i) ↔ p 0 ∨ p 1 :=
exists_fin_succ.trans $ or_congr_right' exists_fin_one
lemma fin_two_eq_of_eq_zero_iff {a b : fin 2} (h : a = 0 ↔ b = 0) : a = b :=
by { revert a b, simp [forall_fin_two] }
/--
Define `C i` by reverse induction on `i : fin (n + 1)` via induction on the underlying `nat` value.
This function has two arguments: `hlast` handles the base case on `C (fin.last n)`,
and `hs` defines the inductive step using `C i.succ`, inducting downwards.
-/
@[elab_as_eliminator]
def reverse_induction {n : ℕ}
{C : fin (n + 1) → Sort*}
(hlast : C (fin.last n))
(hs : ∀ i : fin n, C i.succ → C i.cast_succ) :
Π (i : fin (n + 1)), C i
| i :=
if hi : i = fin.last n
then _root_.cast (by rw hi) hlast
else
let j : fin n := ⟨i, lt_of_le_of_ne (nat.le_of_lt_succ i.2) (λ h, hi (fin.ext h))⟩ in
have wf : n + 1 - j.succ < n + 1 - i, begin
cases i,
rw [tsub_lt_tsub_iff_left_of_le];
simp [*, nat.succ_le_iff],
end,
have hi : i = fin.cast_succ j, from fin.ext rfl,
_root_.cast (by rw hi) (hs _ (reverse_induction j.succ))
using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ i : fin (n+1), n + 1 - i)⟩],
dec_tac := `[assumption] }
@[simp] lemma reverse_induction_last {n : ℕ}
{C : fin (n + 1) → Sort*}
(h0 : C (fin.last n))
(hs : ∀ i : fin n, C i.succ → C i.cast_succ) :
(reverse_induction h0 hs (fin.last n) : C (fin.last n)) = h0 :=
by rw [reverse_induction]; simp
@[simp] lemma reverse_induction_cast_succ {n : ℕ}
{C : fin (n + 1) → Sort*}
(h0 : C (fin.last n))
(hs : ∀ i : fin n, C i.succ → C i.cast_succ) (i : fin n):
(reverse_induction h0 hs i.cast_succ : C i.cast_succ) =
hs i (reverse_induction h0 hs i.succ) :=
begin
rw [reverse_induction, dif_neg (ne_of_lt (fin.cast_succ_lt_last i))],
cases i,
refl
end
/-- Define `f : Π i : fin n.succ, C i` by separately handling the cases `i = fin.last n` and
`i = j.cast_succ`, `j : fin n`. -/
@[elab_as_eliminator]
def last_cases {n : ℕ} {C : fin (n + 1) → Sort*}
(hlast : C (fin.last n)) (hcast : (Π (i : fin n), C i.cast_succ)) (i : fin (n + 1)) : C i :=
reverse_induction hlast (λ i _, hcast i) i
@[simp] lemma last_cases_last {n : ℕ} {C : fin (n + 1) → Sort*}
(hlast : C (fin.last n)) (hcast : (Π (i : fin n), C i.cast_succ)) :
(fin.last_cases hlast hcast (fin.last n): C (fin.last n)) = hlast :=
reverse_induction_last _ _
@[simp] lemma last_cases_cast_succ {n : ℕ} {C : fin (n + 1) → Sort*}
(hlast : C (fin.last n)) (hcast : (Π (i : fin n), C i.cast_succ)) (i : fin n) :
(fin.last_cases hlast hcast (fin.cast_succ i): C (fin.cast_succ i)) = hcast i :=
reverse_induction_cast_succ _ _ _
/-- Define `f : Π i : fin (m + n), C i` by separately handling the cases `i = cast_add n i`,
`j : fin m` and `i = nat_add m j`, `j : fin n`. -/
@[elab_as_eliminator]
def add_cases {m n : ℕ} {C : fin (m + n) → Sort u}
(hleft : Π i, C (cast_add n i))
(hright : Π i, C (nat_add m i)) (i : fin (m + n)) : C i :=
if hi : (i : ℕ) < m then eq.rec_on (cast_add_cast_lt n i hi) (hleft (cast_lt i hi))
else eq.rec_on (nat_add_sub_nat_cast (le_of_not_lt hi)) (hright _)
@[simp] lemma add_cases_left {m n : ℕ} {C : fin (m + n) → Sort*}
(hleft : Π i, C (cast_add n i)) (hright : Π i, C (nat_add m i)) (i : fin m) :
@add_cases _ _ C hleft hright (fin.cast_add n i) = hleft i :=
begin
cases i with i hi,
rw [add_cases, dif_pos (cast_add_lt _ _)],
refl
end
@[simp] lemma add_cases_right {m n : ℕ} {C : fin (m + n) → Sort*}
(hleft : Π i, C (cast_add n i)) (hright : Π i, C (nat_add m i)) (i : fin n) :
@add_cases _ _ C hleft hright (nat_add m i) = hright i :=
begin
have : ¬ (nat_add m i : ℕ) < m, from (le_coe_nat_add _ _).not_lt,
rw [add_cases, dif_neg this],
refine eq_of_heq ((eq_rec_heq _ _).trans _), congr' 1,
simp
end
end rec
lemma lift_fun_iff_succ {α : Type*} (r : α → α → Prop) [is_trans α r] {f : fin (n + 1) → α} :
((<) ⇒ r) f f ↔ ∀ i : fin n, r (f i.cast_succ) (f i.succ) :=
begin
split,
{ intros H i,
exact H i.cast_succ_lt_succ },
{ refine λ H i, fin.induction _ _,
{ exact λ h, (h.not_le (zero_le i)).elim },
{ intros j ihj hij,
rw [← le_cast_succ_iff] at hij,
rcases hij.eq_or_lt with rfl|hlt,
exacts [H j, trans (ihj hlt) (H j)] } }
end
/-- A function `f` on `fin (n + 1)` is strictly monotone if and only if `f i < f (i + 1)`
for all `i`. -/
lemma strict_mono_iff_lt_succ {α : Type*} [preorder α] {f : fin (n + 1) → α} :
strict_mono f ↔ ∀ i : fin n, f i.cast_succ < f i.succ :=
lift_fun_iff_succ (<)
/-- A function `f` on `fin (n + 1)` is monotone if and only if `f i ≤ f (i + 1)` for all `i`. -/
lemma monotone_iff_le_succ {α : Type*} [preorder α] {f : fin (n + 1) → α} :
monotone f ↔ ∀ i : fin n, f i.cast_succ ≤ f i.succ :=
monotone_iff_forall_lt.trans $ lift_fun_iff_succ (≤)
/-- A function `f` on `fin (n + 1)` is strictly antitone if and only if `f (i + 1) < f i`
for all `i`. -/
lemma strict_anti_iff_succ_lt {α : Type*} [preorder α] {f : fin (n + 1) → α} :
strict_anti f ↔ ∀ i : fin n, f i.succ < f i.cast_succ :=
lift_fun_iff_succ (>)
/-- A function `f` on `fin (n + 1)` is antitone if and only if `f (i + 1) ≤ f i` for all `i`. -/
lemma antitone_iff_succ_le {α : Type*} [preorder α] {f : fin (n + 1) → α} :
antitone f ↔ ∀ i : fin n, f i.succ ≤ f i.cast_succ :=
antitone_iff_forall_lt.trans $ lift_fun_iff_succ (≥)
section add_group
open nat int
/-- Negation on `fin n` -/
instance (n : ℕ) : has_neg (fin n) := ⟨λ a, ⟨(n - a) % n, nat.mod_lt _ a.pos⟩⟩
/-- Abelian group structure on `fin n`. -/
instance (n : ℕ) [ne_zero n] : add_comm_group (fin n) :=
{ add_left_neg := λ ⟨a, ha⟩, fin.ext $ trans (nat.mod_add_mod _ _ _) $
by { rw [fin.coe_mk, fin.coe_zero, tsub_add_cancel_of_le, nat.mod_self], exact le_of_lt ha },
sub_eq_add_neg := λ ⟨a, ha⟩ ⟨b, hb⟩, fin.ext $
show (a + (n - b)) % n = (a + (n - b) % n) % n, by simp,
sub := fin.sub,
..fin.add_comm_monoid n,
..fin.has_neg n }
/-- Note this is more general than `fin.add_comm_group` as it applies (vacuously) to `fin 0` too. -/
instance (n : ℕ) : has_involutive_neg (fin n) :=
{ neg := has_neg.neg,
neg_neg := nat.cases_on n fin_zero_elim (λ i, neg_neg) }
/-- Note this is more general than `fin.add_comm_group` as it applies (vacuously) to `fin 0` too. -/
instance (n : ℕ) : is_cancel_add (fin n) :=
{ add_left_cancel := nat.cases_on n fin_zero_elim (λ i _ _ _, add_left_cancel),
add_right_cancel := nat.cases_on n fin_zero_elim (λ i _ _ _, add_right_cancel) }
/-- Note this is more general than `fin.add_comm_group` as it applies (vacuously) to `fin 0` too. -/
instance (n : ℕ) : add_left_cancel_semigroup (fin n) :=
{ ..fin.add_comm_semigroup n, .. fin.is_cancel_add n }
/-- Note this is more general than `fin.add_comm_group` as it applies (vacuously) to `fin 0` too. -/
instance (n : ℕ) : add_right_cancel_semigroup (fin n) :=
{ ..fin.add_comm_semigroup n, .. fin.is_cancel_add n }
protected lemma coe_neg (a : fin n) : ((-a : fin n) : ℕ) = (n - a) % n := rfl
protected lemma coe_sub (a b : fin n) : ((a - b : fin n) : ℕ) = (a + (n - b)) % n :=
by cases a; cases b; refl
@[simp] lemma coe_fin_one (a : fin 1) : ↑a = 0 :=
by rw [subsingleton.elim a 0, fin.coe_zero]
@[simp] lemma coe_neg_one : ↑(-1 : fin (n + 1)) = n :=
begin
cases n,
{ simp },
rw [fin.coe_neg, fin.coe_one, nat.succ_sub_one, nat.mod_eq_of_lt],
constructor
end
lemma coe_sub_one {n} (a : fin (n + 1)) : ↑(a - 1) = if a = 0 then n else a - 1 :=
begin
cases n,
{ simp },
split_ifs,
{ simp [h] },
rw [sub_eq_add_neg, coe_add_eq_ite, coe_neg_one, if_pos, add_comm, add_tsub_add_eq_tsub_left],
rw [add_comm ↑a, add_le_add_iff_left, nat.one_le_iff_ne_zero],
rwa fin.ext_iff at h
end
lemma coe_sub_iff_le {n : ℕ} {a b : fin n} :
(↑(a - b) : ℕ) = a - b ↔ b ≤ a :=
begin
cases n, {exact fin_zero_elim a},
rw [le_iff_coe_le_coe, fin.coe_sub, ←add_tsub_assoc_of_le b.is_lt.le a],
cases le_or_lt (b : ℕ) a with h h,
{ simp [←tsub_add_eq_add_tsub h, h, nat.mod_eq_of_lt ((nat.sub_le _ _).trans_lt a.is_lt)] },
{ rw [nat.mod_eq_of_lt, tsub_eq_zero_of_le h.le, tsub_eq_zero_iff_le, ←not_iff_not],
{ simpa [b.is_lt.trans_le (le_add_self)] using h },
{ rwa [tsub_lt_iff_left (b.is_lt.le.trans (le_add_self)), add_lt_add_iff_right] } }
end
lemma coe_sub_iff_lt {n : ℕ} {a b : fin n} :
(↑(a - b) : ℕ) = n + a - b ↔ a < b :=
begin
cases n, {exact fin_zero_elim a},
rw [lt_iff_coe_lt_coe, fin.coe_sub, add_comm],
cases le_or_lt (b : ℕ) a with h h,
{ simpa [add_tsub_assoc_of_le h, ←not_le, h]
using ((nat.mod_lt _ (nat.succ_pos _)).trans_le le_self_add).ne },
{ simp [←tsub_tsub_assoc b.is_lt.le h.le, ←tsub_add_eq_add_tsub b.is_lt.le,
nat.mod_eq_of_lt (tsub_lt_self (nat.succ_pos _) (tsub_pos_of_lt h)), h] }
end
@[simp] lemma lt_sub_one_iff {n : ℕ} {k : fin (n + 2)} :
k < k - 1 ↔ k = 0 :=
begin
rcases k with ⟨(_|k), hk⟩,
simp [lt_iff_coe_lt_coe],
have : (k + 1 + (n + 1)) % (n + 2) = k % (n + 2),
{ rw [add_right_comm, add_assoc, add_mod_right] },
simp [lt_iff_coe_lt_coe, ext_iff, fin.coe_sub, succ_eq_add_one, this,
mod_eq_of_lt ((lt_succ_self _).trans hk)]
end
@[simp] lemma le_sub_one_iff {n : ℕ} {k : fin (n + 1)} :
k ≤ k - 1 ↔ k = 0 :=
begin
cases n,
{ simp [subsingleton.elim (k - 1) k, subsingleton.elim 0 k] },
rw [←lt_sub_one_iff, le_iff_lt_or_eq, lt_sub_one_iff, or_iff_left_iff_imp, eq_comm,
sub_eq_iff_eq_add],
simp
end
@[simp] lemma sub_one_lt_iff {n : ℕ} {k : fin (n + 1)} :
k - 1 < k ↔ 0 < k :=
not_iff_not.1 $ by simp only [not_lt, le_sub_one_iff, le_zero_iff]
lemma last_sub (i : fin (n + 1)) : last n - i = i.rev :=
ext $ by rw [coe_sub_iff_le.2 i.le_last, coe_last, coe_rev, nat.succ_sub_succ_eq_sub]
end add_group
section succ_above
lemma succ_above_aux (p : fin (n + 1)) :
strict_mono (λ i : fin n, if i.cast_succ < p then i.cast_succ else i.succ) :=
(cast_succ : fin n ↪o _).strict_mono.ite (succ_embedding n).strict_mono
(λ i j hij hj, lt_trans ((cast_succ : fin n ↪o _).lt_iff_lt.2 hij) hj)
(λ i, (cast_succ_lt_succ i).le)
/-- `succ_above p i` embeds `fin n` into `fin (n + 1)` with a hole around `p`. -/
def succ_above (p : fin (n + 1)) : fin n ↪o fin (n + 1) :=
order_embedding.of_strict_mono _ p.succ_above_aux
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
embeds `i` by `cast_succ` when the resulting `i.cast_succ < p`. -/
lemma succ_above_below (p : fin (n + 1)) (i : fin n) (h : i.cast_succ < p) :
p.succ_above i = i.cast_succ :=
by { rw [succ_above], exact if_pos h }
@[simp] lemma succ_above_ne_zero_zero [ne_zero n] {a : fin (n + 1)} (ha : a ≠ 0) :
a.succ_above 0 = 0 :=
begin
rw fin.succ_above_below,
{ refl },
{ exact bot_lt_iff_ne_bot.mpr ha }
end
lemma succ_above_eq_zero_iff [ne_zero n] {a : fin (n + 1)} {b : fin n} (ha : a ≠ 0) :
a.succ_above b = 0 ↔ b = 0 :=
by simp only [←succ_above_ne_zero_zero ha, order_embedding.eq_iff_eq]
lemma succ_above_ne_zero [ne_zero n] {a : fin (n + 1)} {b : fin n} (ha : a ≠ 0) (hb : b ≠ 0) :
a.succ_above b ≠ 0 :=
mt (succ_above_eq_zero_iff ha).mp hb
/-- Embedding `fin n` into `fin (n + 1)` with a hole around zero embeds by `succ`. -/
@[simp] lemma succ_above_zero : ⇑(succ_above (0 : fin (n + 1))) = fin.succ := rfl
/-- Embedding `fin n` into `fin (n + 1)` with a hole around `last n` embeds by `cast_succ`. -/
@[simp] lemma succ_above_last : succ_above (fin.last n) = cast_succ :=
by { ext, simp only [succ_above_below, cast_succ_lt_last] }
lemma succ_above_last_apply (i : fin n) : succ_above (fin.last n) i = i.cast_succ :=
by rw succ_above_last
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
embeds `i` by `succ` when the resulting `p < i.succ`. -/
lemma succ_above_above (p : fin (n + 1)) (i : fin n) (h : p ≤ i.cast_succ) :
p.succ_above i = i.succ :=
by simp [succ_above, h.not_lt]
/-- Embedding `i : fin n` into `fin (n + 1)` is always about some hole `p`. -/
lemma succ_above_lt_ge (p : fin (n + 1)) (i : fin n) : i.cast_succ < p ∨ p ≤ i.cast_succ :=
lt_or_ge (cast_succ i) p
/-- Embedding `i : fin n` into `fin (n + 1)` is always about some hole `p`. -/
lemma succ_above_lt_gt (p : fin (n + 1)) (i : fin n) : i.cast_succ < p ∨ p < i.succ :=
or.cases_on (succ_above_lt_ge p i)
(λ h, or.inl h) (λ h, or.inr (lt_of_le_of_lt h (cast_succ_lt_succ i)))
/-- Embedding `i : fin n` into `fin (n + 1)` using a pivot `p` that is greater
results in a value that is less than `p`. -/
@[simp] lemma succ_above_lt_iff (p : fin (n + 1)) (i : fin n) :
p.succ_above i < p ↔ i.cast_succ < p :=
begin
refine iff.intro _ _,
{ intro h,
cases succ_above_lt_ge p i with H H,
{ exact H },
{ rw succ_above_above _ _ H at h,
exact lt_trans (cast_succ_lt_succ i) h } },
{ intro h,
rw succ_above_below _ _ h,
exact h }
end
/-- Embedding `i : fin n` into `fin (n + 1)` using a pivot `p` that is lesser
results in a value that is greater than `p`. -/
lemma lt_succ_above_iff (p : fin (n + 1)) (i : fin n) : p < p.succ_above i ↔ p ≤ i.cast_succ :=
begin
refine iff.intro _ _,
{ intro h,
cases succ_above_lt_ge p i with H H,
{ rw succ_above_below _ _ H at h,
exact le_of_lt h },
{ exact H } },
{ intro h,
rw succ_above_above _ _ h,
exact lt_of_le_of_lt h (cast_succ_lt_succ i) },
end
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
never results in `p` itself -/
theorem succ_above_ne (p : fin (n + 1)) (i : fin n) : p.succ_above i ≠ p :=
begin
intro eq,
by_cases H : i.cast_succ < p,
{ simpa [lt_irrefl, ←succ_above_below _ _ H, eq] using H },
{ simpa [←succ_above_above _ _ (le_of_not_lt H), eq] using cast_succ_lt_succ i }
end
/-- Embedding a positive `fin n` results in a positive fin (n + 1)` -/
lemma succ_above_pos [ne_zero n] (p : fin (n + 1)) (i : fin n) (h : 0 < i) :
0 < p.succ_above i :=
begin
by_cases H : i.cast_succ < p,
{ simpa [succ_above_below _ _ H] using cast_succ_pos h },
{ simp [succ_above_above _ _ (le_of_not_lt H)] },
end
@[simp] lemma succ_above_cast_lt {x y : fin (n + 1)} (h : x < y)
(hx : x.1 < n := lt_of_lt_of_le h y.le_last) :
y.succ_above (x.cast_lt hx) = x :=
by { rw [succ_above_below, cast_succ_cast_lt], exact h }
@[simp] lemma succ_above_pred {x y : fin (n + 1)} (h : x < y)
(hy : y ≠ 0 := (x.zero_le.trans_lt h).ne') :
x.succ_above (y.pred hy) = y :=
by { rw [succ_above_above, succ_pred], simpa [le_iff_coe_le_coe] using nat.le_pred_of_lt h }
lemma cast_lt_succ_above {x : fin n} {y : fin (n + 1)} (h : cast_succ x < y)
(h' : (y.succ_above x).1 < n := lt_of_lt_of_le ((succ_above_lt_iff _ _).2 h) (le_last y)) :
(y.succ_above x).cast_lt h' = x :=
by simp only [succ_above_below _ _ h, cast_lt_cast_succ]
lemma pred_succ_above {x : fin n} {y : fin (n + 1)} (h : y ≤ cast_succ x)
(h' : y.succ_above x ≠ 0 := (y.zero_le.trans_lt $ (lt_succ_above_iff _ _).2 h).ne') :
(y.succ_above x).pred h' = x :=
by simp only [succ_above_above _ _ h, pred_succ]
lemma exists_succ_above_eq {x y : fin (n + 1)} (h : x ≠ y) : ∃ z, y.succ_above z = x :=
begin
cases h.lt_or_lt with hlt hlt,
exacts [⟨_, succ_above_cast_lt hlt⟩, ⟨_, succ_above_pred hlt⟩],
end
@[simp] lemma exists_succ_above_eq_iff {x y : fin (n + 1)} : (∃ z, x.succ_above z = y) ↔ y ≠ x :=
begin
refine ⟨_, exists_succ_above_eq⟩,
rintro ⟨y, rfl⟩,
exact succ_above_ne _ _
end
/-- The range of `p.succ_above` is everything except `p`. -/
@[simp] lemma range_succ_above (p : fin (n + 1)) : set.range (p.succ_above) = {p}ᶜ :=
set.ext $ λ _, exists_succ_above_eq_iff
@[simp] lemma range_succ (n : ℕ) : set.range (fin.succ : fin n → fin (n + 1)) = {0}ᶜ :=
range_succ_above 0
@[simp] lemma exists_succ_eq_iff {x : fin (n + 1)} : (∃ y, fin.succ y = x) ↔ x ≠ 0 :=
@exists_succ_above_eq_iff n 0 x
/-- Given a fixed pivot `x : fin (n + 1)`, `x.succ_above` is injective -/
lemma succ_above_right_injective {x : fin (n + 1)} : injective (succ_above x) :=
(succ_above x).injective
/-- Given a fixed pivot `x : fin (n + 1)`, `x.succ_above` is injective -/
lemma succ_above_right_inj {x : fin (n + 1)} :
x.succ_above a = x.succ_above b ↔ a = b :=
succ_above_right_injective.eq_iff
/-- `succ_above` is injective at the pivot -/
lemma succ_above_left_injective : injective (@succ_above n) :=
λ _ _ h, by simpa [range_succ_above] using congr_arg (λ f : fin n ↪o fin (n + 1), (set.range f)ᶜ) h
/-- `succ_above` is injective at the pivot -/
@[simp] lemma succ_above_left_inj {x y : fin (n + 1)} :
x.succ_above = y.succ_above ↔ x = y :=
succ_above_left_injective.eq_iff
@[simp] lemma zero_succ_above {n : ℕ} (i : fin n) :
(0 : fin (n + 1)).succ_above i = i.succ :=
rfl
@[simp] lemma succ_succ_above_zero {n : ℕ} [ne_zero n] (i : fin n) :
(i.succ).succ_above 0 = 0 :=
succ_above_below _ _ (succ_pos _)
@[simp] lemma succ_succ_above_succ {n : ℕ} (i : fin (n + 1)) (j : fin n) :
(i.succ).succ_above j.succ = (i.succ_above j).succ :=
(lt_or_ge j.cast_succ i).elim
(λ h, have h' : j.succ.cast_succ < i.succ, by simpa [lt_iff_coe_lt_coe] using h,
by { ext, simp [succ_above_below _ _ h, succ_above_below _ _ h'] })
(λ h, have h' : i.succ ≤ j.succ.cast_succ, by simpa [le_iff_coe_le_coe] using h,
by { ext, simp [succ_above_above _ _ h, succ_above_above _ _ h'] })
@[simp] lemma one_succ_above_zero {n : ℕ} :
(1 : fin (n + 2)).succ_above 0 = 0 :=
succ_succ_above_zero 0
/-- By moving `succ` to the outside of this expression, we create opportunities for further
simplification using `succ_above_zero` or `succ_succ_above_zero`. -/
@[simp] lemma succ_succ_above_one {n : ℕ} [ne_zero n] (i : fin (n + 1)) :
(i.succ).succ_above 1 = (i.succ_above 0).succ :=
by rw [← succ_succ_above_succ i 0, succ_zero_eq_one]
@[simp] lemma one_succ_above_succ {n : ℕ} (j : fin n) :
(1 : fin (n + 2)).succ_above j.succ = j.succ.succ :=
succ_succ_above_succ 0 j
@[simp] lemma one_succ_above_one {n : ℕ} :
(1 : fin (n + 3)).succ_above 1 = 2 :=
succ_succ_above_succ 0 0
end succ_above
section pred_above
/-- `pred_above p i` embeds `i : fin (n+1)` into `fin n` by subtracting one if `p < i`. -/
def pred_above (p : fin n) (i : fin (n+1)) : fin n :=
if h : p.cast_succ < i then
i.pred (ne_of_lt (lt_of_le_of_lt (zero_le p.cast_succ) h)).symm
else
i.cast_lt (lt_of_le_of_lt (le_of_not_lt h) p.2)
lemma pred_above_right_monotone (p : fin n) : monotone p.pred_above :=
λ a b H,
begin
dsimp [pred_above],
split_ifs with ha hb hb,
all_goals { simp only [le_iff_coe_le_coe, coe_pred], },
{ exact pred_le_pred H, },
{ calc _ ≤ _ : nat.pred_le _
... ≤ _ : H, },
{ simp at ha, exact le_pred_of_lt (lt_of_le_of_lt ha hb), },
{ exact H, },
end
lemma pred_above_left_monotone (i : fin (n + 1)) : monotone (λ p, pred_above p i) :=
λ a b H,
begin
dsimp [pred_above],
split_ifs with ha hb hb,
all_goals { simp only [le_iff_coe_le_coe, coe_pred] },
{ exact pred_le _, },
{ have : b < a := cast_succ_lt_cast_succ_iff.mpr (hb.trans_le (le_of_not_gt ha)),
exact absurd H this.not_le }
end
/-- `cast_pred` embeds `i : fin (n + 2)` into `fin (n + 1)`
by lowering just `last (n + 1)` to `last n`. -/
def cast_pred (i : fin (n + 2)) : fin (n + 1) :=
pred_above (last n) i
@[simp] lemma cast_pred_zero : cast_pred (0 : fin (n + 2)) = 0 := rfl
@[simp] lemma cast_pred_one : cast_pred (1 : fin (n + 2)) = 1 :=
by { cases n, apply subsingleton.elim, refl }
@[simp] theorem pred_above_zero {i : fin (n + 2)} (hi : i ≠ 0) :
pred_above 0 i = i.pred hi :=
begin
dsimp [pred_above],
rw dif_pos,
exact (pos_iff_ne_zero _).mpr hi,
end
@[simp] lemma cast_pred_last : cast_pred (last (n + 1)) = last n :=
eq_of_veq (by simp [cast_pred, pred_above, cast_succ_lt_last])
@[simp] lemma cast_pred_mk (n i : ℕ) (h : i < n + 1) :
cast_pred ⟨i, lt_succ_of_lt h⟩ = ⟨i, h⟩ :=
begin
have : ¬cast_succ (last n) < ⟨i, lt_succ_of_lt h⟩,
{ simpa [lt_iff_coe_lt_coe] using le_of_lt_succ h },
simp [cast_pred, pred_above, this]
end
lemma coe_cast_pred {n : ℕ} (a : fin (n + 2)) (hx : a < fin.last _) :
(a.cast_pred : ℕ) = a :=
begin
rcases a with ⟨a, ha⟩,
rw cast_pred_mk,
exacts [rfl, hx],
end
lemma pred_above_below (p : fin (n + 1)) (i : fin (n + 2)) (h : i ≤ p.cast_succ) :
p.pred_above i = i.cast_pred :=
begin
have : i ≤ (last n).cast_succ := h.trans p.le_last,
simp [pred_above, cast_pred, h.not_lt, this.not_lt]
end
@[simp] lemma pred_above_last : pred_above (fin.last n) = cast_pred := rfl
lemma pred_above_last_apply (i : fin n) : pred_above (fin.last n) i = i.cast_pred :=
by rw pred_above_last
lemma pred_above_above (p : fin n) (i : fin (n + 1)) (h : p.cast_succ < i) :
p.pred_above i = i.pred (p.cast_succ.zero_le.trans_lt h).ne.symm :=
by simp [pred_above, h]
lemma cast_pred_monotone : monotone (@cast_pred n) :=
pred_above_right_monotone (last _)
/-- Sending `fin (n+1)` to `fin n` by subtracting one from anything above `p`
then back to `fin (n+1)` with a gap around `p` is the identity away from `p`. -/
@[simp] lemma succ_above_pred_above {p : fin n} {i : fin (n + 1)} (h : i ≠ p.cast_succ) :
p.cast_succ.succ_above (p.pred_above i) = i :=
begin
dsimp [pred_above, succ_above],
rcases p with ⟨p, _⟩,
rcases i with ⟨i, _⟩,
cases lt_or_le i p with H H,
{ rw dif_neg, rw if_pos, refl, exact H, simp, apply le_of_lt H, },
{ rw dif_pos, rw if_neg,
swap 3, -- For some reason `simp` doesn't fire fully unless we discharge the third goal.
{ exact lt_of_le_of_ne H (ne.symm h), },
{ simp, },
{ simp only [fin.mk_eq_mk, ne.def, fin.cast_succ_mk] at h,
simp only [pred, fin.mk_lt_mk, not_lt],
exact nat.le_pred_of_lt (nat.lt_of_le_and_ne H (ne.symm h)), }, },
end
/-- Sending `fin n` into `fin (n + 1)` with a gap at `p`
then back to `fin n` by subtracting one from anything above `p` is the identity. -/
@[simp] lemma pred_above_succ_above (p : fin n) (i : fin n) :
p.pred_above (p.cast_succ.succ_above i) = i :=
begin
dsimp [pred_above, succ_above],
rcases p with ⟨p, _⟩,
rcases i with ⟨i, _⟩,
split_ifs,
{ rw dif_neg,
{ refl },
{ simp_rw [if_pos h],
simp only [subtype.mk_lt_mk, not_lt],
exact le_of_lt h, }, },
{ rw dif_pos,
{ refl, },
{ simp_rw [if_neg h],
exact lt_succ_iff.mpr (not_lt.mp h), }, },
end
lemma cast_succ_pred_eq_pred_cast_succ {a : fin (n + 1)} (ha : a ≠ 0)
(ha' := a.cast_succ_ne_zero_iff.mpr ha) : (a.pred ha).cast_succ = a.cast_succ.pred ha' :=
by { cases a, refl }
/-- `pred` commutes with `succ_above`. -/
lemma pred_succ_above_pred {a : fin (n + 2)} {b : fin (n + 1)} (ha : a ≠ 0) (hb : b ≠ 0)
(hk := succ_above_ne_zero ha hb) :
(a.pred ha).succ_above (b.pred hb) = (a.succ_above b).pred hk :=
begin
obtain hbelow | habove := lt_or_le b.cast_succ a, -- `rwa` uses them
{ rw fin.succ_above_below,
{ rwa [cast_succ_pred_eq_pred_cast_succ , fin.pred_inj, fin.succ_above_below] },
{ rwa [cast_succ_pred_eq_pred_cast_succ , pred_lt_pred_iff] } },
{ rw fin.succ_above_above,
have : (b.pred hb).succ = b.succ.pred (fin.succ_ne_zero _), by rw [succ_pred, pred_succ],
{ rwa [this, fin.pred_inj, fin.succ_above_above] },
{ rwa [cast_succ_pred_eq_pred_cast_succ , fin.pred_le_pred_iff] } }
end
/-- `succ` commutes with `pred_above`. -/
@[simp]
lemma succ_pred_above_succ (a : fin n) (b : fin (n+1)) :
a.succ.pred_above b.succ = (a.pred_above b).succ :=
begin
obtain h₁ | h₂ := lt_or_le a.cast_succ b,
{ rw [fin.pred_above_above _ _ h₁, fin.succ_pred,
fin.pred_above_above, fin.pred_succ],
simpa only [fin.lt_iff_coe_lt_coe, fin.coe_cast_succ,
fin.coe_succ, add_lt_add_iff_right] using h₁, },
{ cases n,
{ exfalso,
exact not_lt_zero' a.is_lt, },
{ rw [fin.pred_above_below a b h₂, fin.pred_above_below a.succ b.succ
(by simpa only [le_iff_coe_le_coe, coe_succ, coe_cast_succ,
add_le_add_iff_right] using h₂)],
ext,
have h₀ : (b : ℕ) < n+1,
{ simp only [le_iff_coe_le_coe, coe_cast_succ] at h₂,
simpa only [lt_succ_iff] using h₂.trans a.is_le, },
have h₁ : (b.succ : ℕ) < n+2,
{ rw ← nat.succ_lt_succ_iff at h₀,
simpa only [coe_succ] using h₀, },
simp only [coe_cast_pred b h₀, coe_cast_pred b.succ h₁, coe_succ], }, },
end
@[simp] theorem cast_pred_cast_succ (i : fin (n + 1)) :
cast_pred i.cast_succ = i :=
by simp [cast_pred, pred_above, le_last]
lemma cast_succ_cast_pred {i : fin (n + 2)} (h : i < last _) : cast_succ i.cast_pred = i :=
begin
rw [cast_pred, pred_above, dif_neg],
{ simp [fin.eq_iff_veq] },
{ exact h.not_le }
end
lemma coe_cast_pred_le_self (i : fin (n + 2)) : (i.cast_pred : ℕ) ≤ i :=
begin
rcases i.le_last.eq_or_lt with rfl|h,
{ simp },
{ rw [cast_pred, pred_above, dif_neg],
{ simp },
{ simpa [lt_iff_coe_lt_coe, le_iff_coe_le_coe, lt_succ_iff] using h } }
end
lemma coe_cast_pred_lt_iff {i : fin (n + 2)} : (i.cast_pred : ℕ) < i ↔ i = fin.last _ :=
begin
rcases i.le_last.eq_or_lt with rfl|H,
{ simp },
{ simp only [ne_of_lt H],
rw ←cast_succ_cast_pred H,
simp }
end
lemma lt_last_iff_coe_cast_pred {i : fin (n + 2)} : i < fin.last _ ↔ (i.cast_pred : ℕ) = i :=
begin
rcases i.le_last.eq_or_lt with rfl|H,
{ simp },
{ simp only [H],
rw ←cast_succ_cast_pred H,
simp }
end
end pred_above
/-- `min n m` as an element of `fin (m + 1)` -/
def clamp (n m : ℕ) : fin (m + 1) := of_nat $ min n m
@[simp] lemma coe_clamp (n m : ℕ) : (clamp n m : ℕ) = min n m :=
nat.mod_eq_of_lt $ nat.lt_succ_iff.mpr $ min_le_right _ _
@[simp] lemma coe_of_nat_eq_mod (m n : ℕ) [ne_zero m] :
((n : fin m) : ℕ) = n % m :=
rfl
section mul
/-!
### mul
-/
lemma val_mul {n : ℕ} : ∀ a b : fin n, (a * b).val = (a.val * b.val) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_mul {n : ℕ} : ∀ a b : fin n, ((a * b : fin n) : ℕ) = (a * b) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
@[simp] protected lemma mul_one [ne_zero n] (k : fin n) : k * 1 = k :=
begin
unfreezingI { cases n },
{ simp },
unfreezingI { cases n },
{ simp },
simp [eq_iff_veq, mul_def, mod_eq_of_lt (is_lt k)]
end
protected lemma mul_comm (a b : fin n) : a * b = b * a :=
fin.eq_of_veq $ by rw [mul_def, mul_def, mul_comm]
@[simp] protected lemma one_mul [ne_zero n] (k : fin n) : (1 : fin n) * k = k :=
by rw [fin.mul_comm, fin.mul_one]
@[simp] protected lemma mul_zero [ne_zero n] (k : fin n) : k * 0 = 0 :=
by simp [eq_iff_veq, mul_def]
@[simp] protected lemma zero_mul [ne_zero n] (k : fin n) : (0 : fin n) * k = 0 :=
by simp [eq_iff_veq, mul_def]
end mul
section
-- Note that here we are disabling the "safety" of reflected, to allow us to reuse `nat.mk_numeral`.
-- The usual way to provide the required `reflected` instance would be via rewriting to prove that
-- the expression we use here is equivalent.
local attribute [semireducible] reflected
meta instance reflect : Π n, has_reflect (fin n)
| 0 := fin_zero_elim
| (n + 1) := nat.mk_numeral `(fin n.succ)
`(by apply_instance : has_zero (fin n.succ))
`(by apply_instance : has_one (fin n.succ))
`(by apply_instance : has_add (fin n.succ)) ∘ fin.val
end
end fin
|
a6600f04f356fc3f27ba9fda3fdf9a9d78389658 | 6dc0c8ce7a76229dd81e73ed4474f15f88a9e294 | /stage0/src/Lean/Elab/DeclUtil.lean | ce0c7ad3ec5e36dda58f6d771ee4dbcb87a8aa9a | [
"Apache-2.0"
] | permissive | williamdemeo/lean4 | 72161c58fe65c3ad955d6a3050bb7d37c04c0d54 | 6d00fcf1d6d873e195f9220c668ef9c58e9c4a35 | refs/heads/master | 1,678,305,356,877 | 1,614,708,995,000 | 1,614,708,995,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,822 | 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, Sebastian Ullrich
-/
import Lean.Meta.ExprDefEq
namespace Lean.Meta
def forallTelescopeCompatibleAux {α} (k : Array Expr → Expr → Expr → MetaM α) : Nat → Expr → Expr → Array Expr → MetaM α
| 0, type₁, type₂, xs => k xs type₁ type₂
| i+1, type₁, type₂, xs => do
let type₁ ← whnf type₁
let type₂ ← whnf type₂
match type₁, type₂ with
| Expr.forallE n₁ d₁ b₁ c₁, Expr.forallE n₂ d₂ b₂ c₂ =>
unless n₁ == n₂ do
throwError! "parameter name mismatch '{n₁}', expected '{n₂}'"
unless (← isDefEq d₁ d₂) do
throwError! "parameter '{n₁}' {← mkHasTypeButIsExpectedMsg d₁ d₂}"
unless c₁.binderInfo == c₂.binderInfo do
throwError! "binder annotation mismatch at parameter '{n₁}'"
withLocalDecl n₁ c₁.binderInfo d₁ fun x =>
let type₁ := b₁.instantiate1 x
let type₂ := b₂.instantiate1 x
forallTelescopeCompatibleAux k i type₁ type₂ (xs.push x)
| _, _ => throwError "unexpected number of parameters"
/-- Given two forall-expressions `type₁` and `type₂`, ensure the first `numParams` parameters are compatible, and
then execute `k` with the parameters and remaining types. -/
@[inline] def forallTelescopeCompatible {α m} [Monad m] [MonadControlT MetaM m] (type₁ type₂ : Expr) (numParams : Nat) (k : Array Expr → Expr → Expr → m α) : m α :=
controlAt MetaM fun runInBase =>
forallTelescopeCompatibleAux (fun xs type₁ type₂ => runInBase $ k xs type₁ type₂) numParams type₁ type₂ #[]
end Meta
namespace Elab
def expandOptDeclSig (stx : Syntax) : Syntax × Option Syntax :=
-- many Term.bracketedBinder >> Term.optType
let binders := stx[0]
let optType := stx[1] -- optional (parser! " : " >> termParser)
if optType.isNone then
(binders, none)
else
let typeSpec := optType[0]
(binders, some typeSpec[1])
def expandDeclSig (stx : Syntax) : Syntax × Syntax :=
-- many Term.bracketedBinder >> Term.typeSpec
let binders := stx[0]
let typeSpec := stx[1]
(binders, typeSpec[1])
def mkFreshInstanceName (env : Environment) (nextIdx : Nat) : Name :=
(env.mainModule ++ `_instance).appendIndexAfter nextIdx
def isFreshInstanceName (name : Name) : Bool :=
match name with
| Name.str _ s _ => "_instance".isPrefixOf s
| _ => false
/--
Sort the given list of `usedParams` using the following order:
- If it is an explicit level `allUserParams`, then use user given order.
- Otherwise, use lexicographical.
Remark: `scopeParams` are the universe params introduced using the `universe` command. `allUserParams` contains
the universe params introduced using the `universe` command *and* the `.{...}` notation.
Remark: this function return an exception if there is an `u` not in `usedParams`, that is in `allUserParams` but not in `scopeParams`.
Remark: `explicitParams` are in reverse declaration order. That is, the head is the last declared parameter. -/
def sortDeclLevelParams (scopeParams : List Name) (allUserParams : List Name) (usedParams : Array Name) : Except String (List Name) :=
match allUserParams.find? $ fun u => !usedParams.contains u && !scopeParams.elem u with
| some u => throw s!"unused universe parameter '{u}'"
| none =>
let result := allUserParams.foldl (fun result levelName => if usedParams.elem levelName then levelName :: result else result) []
let remaining := usedParams.filter (fun levelParam => !allUserParams.elem levelParam)
let remaining := remaining.qsort Name.lt
pure $ result ++ remaining.toList
end Lean.Elab
|
fb94dd561e1361b8b18cf1f9767980936cbb1ab1 | 6094e25ea0b7699e642463b48e51b2ead6ddc23f | /library/data/tuple.lean | 196ba8ac14f06e5d218ba861ecddaaf93d7d3802 | [
"Apache-2.0"
] | permissive | gbaz/lean | a7835c4e3006fbbb079e8f8ffe18aacc45adebfb | a501c308be3acaa50a2c0610ce2e0d71becf8032 | refs/heads/master | 1,611,198,791,433 | 1,451,339,111,000 | 1,451,339,111,000 | 48,713,797 | 0 | 0 | null | 1,451,338,939,000 | 1,451,338,939,000 | null | UTF-8 | Lean | false | false | 13,004 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
Tuples are lists of a fixed size.
It is implemented as a subtype.
-/
import logic data.list data.fin
open nat list subtype function
definition tuple [reducible] (A : Type) (n : nat) := {l : list A | length l = n}
namespace tuple
variables {A B C : Type}
theorem induction_on [recursor 4] {P : ∀ {n}, tuple A n → Prop}
: ∀ {n} (v : tuple A n), (∀ (l : list A) {n : nat} (h : length l = n), P (tag l h)) → P v
| n (tag l h) H := @H l n h
definition nil : tuple A 0 :=
tag [] rfl
lemma length_succ {n : nat} {l : list A} (a : A) : length l = n → length (a::l) = succ n :=
λ h, congr_arg succ h
definition cons {n : nat} : A → tuple A n → tuple A (succ n)
| a (tag v h) := tag (a::v) (length_succ a h)
notation a :: b := cons a b
protected definition is_inhabited [instance] [h : inhabited A] : ∀ (n : nat), inhabited (tuple A n)
| 0 := inhabited.mk nil
| (succ n) := inhabited.mk (inhabited.value h :: inhabited.value (is_inhabited n))
protected definition has_decidable_eq [instance] [h : decidable_eq A] : ∀ (n : nat), decidable_eq (tuple A n) :=
λ n, subtype.has_decidable_eq
definition head {n : nat} : tuple A (succ n) → A
| (tag [] h) := by contradiction
| (tag (a::v) h) := a
definition tail {n : nat} : tuple A (succ n) → tuple A n
| (tag [] h) := by contradiction
| (tag (a::v) h) := tag v (succ.inj h)
theorem head_cons {n : nat} (a : A) (v : tuple A n) : head (a :: v) = a :=
by induction v; reflexivity
theorem tail_cons {n : nat} (a : A) (v : tuple A n) : tail (a :: v) = v :=
by induction v; reflexivity
theorem head_lcons {n : nat} (a : A) (l : list A) (h : length (a::l) = succ n) : head (tag (a::l) h) = a :=
rfl
theorem tail_lcons {n : nat} (a : A) (l : list A) (h : length (a::l) = succ n) : tail (tag (a::l) h) = tag l (succ.inj h) :=
rfl
definition last {n : nat} : tuple A (succ n) → A
| (tag l h) := list.last l (ne_nil_of_length_eq_succ h)
theorem eta : ∀ {n : nat} (v : tuple A (succ n)), head v :: tail v = v
| 0 (tag [] h) := by contradiction
| 0 (tag (a::l) h) := rfl
| (n+1) (tag [] h) := by contradiction
| (n+1) (tag (a::l) h) := rfl
definition of_list (l : list A) : tuple A (list.length l) :=
tag l rfl
definition to_list {n : nat} : tuple A n → list A
| (tag l h) := l
theorem to_list_of_list (l : list A) : to_list (of_list l) = l :=
rfl
theorem to_list_nil : to_list nil = ([] : list A) :=
rfl
theorem length_to_list {n : nat} : ∀ (v : tuple A n), list.length (to_list v) = n
| (tag l h) := h
theorem heq_of_list_eq {n m} : ∀ {v₁ : tuple A n} {v₂ : tuple A m}, to_list v₁ = to_list v₂ → n = m → v₁ == v₂
| (tag l₁ h₁) (tag l₂ h₂) e₁ e₂ := begin
clear heq_of_list_eq,
subst e₂, subst h₁,
unfold to_list at e₁,
subst l₁
end
theorem list_eq_of_heq {n m} {v₁ : tuple A n} {v₂ : tuple A m} : v₁ == v₂ → n = m → to_list v₁ = to_list v₂ :=
begin
intro h₁ h₂, revert v₁ v₂ h₁,
subst n, intro v₁ v₂ h₁, rewrite [heq.to_eq h₁]
end
theorem of_list_to_list {n : nat} (v : tuple A n) : of_list (to_list v) == v :=
begin
apply heq_of_list_eq, rewrite to_list_of_list, rewrite length_to_list
end
/- append -/
definition append {n m : nat} : tuple A n → tuple A m → tuple A (n + m)
| (tag l₁ h₁) (tag l₂ h₂) := tag (list.append l₁ l₂) (by rewrite [length_append, h₁, h₂])
infix ++ := append
open eq.ops
lemma push_eq_rec : ∀ {n m : nat} {l : list A} (h₁ : n = m) (h₂ : length l = n), h₁ ▹ (tag l h₂) = tag l (h₁ ▹ h₂)
| n n l (eq.refl n) h₂ := rfl
theorem append_nil_right {n : nat} (v : tuple A n) : v ++ nil = v :=
induction_on v (λ l n h, by unfold [tuple.append, tuple.nil]; congruence; apply list.append_nil_right)
theorem append_nil_left {n : nat} (v : tuple A n) : !zero_add ▹ (nil ++ v) = v :=
induction_on v (λ l n h, begin unfold [tuple.append, tuple.nil], rewrite [push_eq_rec] end)
theorem append_nil_left_heq {n : nat} (v : tuple A n) : nil ++ v == v :=
heq_of_eq_rec_left !zero_add (append_nil_left v)
theorem append.assoc {n₁ n₂ n₃} : ∀ (v₁ : tuple A n₁) (v₂ : tuple A n₂) (v₃ : tuple A n₃), !add.assoc ▹ ((v₁ ++ v₂) ++ v₃) = v₁ ++ (v₂ ++ v₃)
| (tag l₁ h₁) (tag l₂ h₂) (tag l₃ h₃) := begin
unfold tuple.append, rewrite push_eq_rec,
congruence,
apply list.append.assoc
end
theorem append.assoc_heq {n₁ n₂ n₃} (v₁ : tuple A n₁) (v₂ : tuple A n₂) (v₃ : tuple A n₃) : (v₁ ++ v₂) ++ v₃ == v₁ ++ (v₂ ++ v₃) :=
heq_of_eq_rec_left !add.assoc (append.assoc v₁ v₂ v₃)
/- reverse -/
definition reverse {n : nat} : tuple A n → tuple A n
| (tag l h) := tag (list.reverse l) (by rewrite [length_reverse, h])
theorem reverse_reverse {n : nat} (v : tuple A n) : reverse (reverse v) = v :=
induction_on v (λ l n h, begin unfold reverse, congruence, apply list.reverse_reverse end)
theorem tuple0_eq_nil : ∀ (v : tuple A 0), v = nil
| (tag [] h) := rfl
| (tag (a::l) h) := by contradiction
/- mem -/
definition mem {n : nat} (a : A) (v : tuple A n) : Prop :=
a ∈ elt_of v
notation e ∈ s := mem e s
notation e ∉ s := ¬ e ∈ s
theorem not_mem_nil (a : A) : a ∉ nil :=
list.not_mem_nil a
theorem mem_cons [simp] {n : nat} (a : A) (v : tuple A n) : a ∈ a :: v :=
induction_on v (λ l n h, !list.mem_cons)
theorem mem_cons_of_mem {n : nat} (y : A) {x : A} {v : tuple A n} : x ∈ v → x ∈ y :: v :=
induction_on v (λ l n h₁ h₂, list.mem_cons_of_mem y h₂)
theorem eq_or_mem_of_mem_cons {n : nat} {x y : A} {v : tuple A n} : x ∈ y::v → x = y ∨ x ∈ v :=
induction_on v (λ l n h₁ h₂, eq_or_mem_of_mem_cons h₂)
theorem mem_singleton {n : nat} {x a : A} : x ∈ (a::nil : tuple A 1) → x = a :=
assume h, list.mem_singleton h
/- map -/
definition map {n : nat} (f : A → B) : tuple A n → tuple B n
| (tag l h) := tag (list.map f l) (by clear map; substvars; rewrite length_map)
theorem map_nil (f : A → B) : map f nil = nil :=
rfl
theorem map_cons {n : nat} (f : A → B) (a : A) (v : tuple A n) : map f (a::v) = f a :: map f v :=
by induction v; reflexivity
theorem map_tag {n : nat} (f : A → B) (l : list A) (h : length l = n)
: map f (tag l h) = tag (list.map f l) (by substvars; rewrite length_map) :=
by reflexivity
theorem map_map {n : nat} (g : B → C) (f : A → B) (v : tuple A n) : map g (map f v) = map (g ∘ f) v :=
begin cases v, rewrite *map_tag, apply subtype.eq, apply list.map_map end
theorem map_id {n : nat} (v : tuple A n) : map id v = v :=
begin induction v, unfold map, congruence, apply list.map_id end
theorem mem_map {n : nat} {a : A} {v : tuple A n} (f : A → B) : a ∈ v → f a ∈ map f v :=
begin induction v, unfold map, apply list.mem_map end
theorem exists_of_mem_map {n : nat} {f : A → B} {b : B} {v : tuple A n} : b ∈ map f v → ∃a, a ∈ v ∧ f a = b :=
begin induction v, unfold map, apply list.exists_of_mem_map end
theorem eq_of_map_const {n : nat} {b₁ b₂ : B} {v : tuple A n} : b₁ ∈ map (const A b₂) v → b₁ = b₂ :=
begin induction v, unfold map, apply list.eq_of_map_const end
/- product -/
definition product {n m : nat} : tuple A n → tuple B m → tuple (A × B) (n * m)
| (tag l₁ h₁) (tag l₂ h₂) := tag (list.product l₁ l₂) (by rewrite [length_product, h₁, h₂])
theorem nil_product {m : nat} (v : tuple B m) : !zero_mul ▹ product (@nil A) v = nil :=
begin induction v, unfold [nil, product], rewrite push_eq_rec end
theorem nil_product_heq {m : nat} (v : tuple B m) : product (@nil A) v == (@nil (A × B)) :=
heq_of_eq_rec_left _ (nil_product v)
theorem product_nil {n : nat} (v : tuple A n) : product v (@nil B) = nil :=
begin induction v, unfold [nil, product], congruence, apply list.product_nil end
theorem mem_product {n m : nat} {a : A} {b : B} {v₁ : tuple A n} {v₂ : tuple B m} : a ∈ v₁ → b ∈ v₂ → (a, b) ∈ product v₁ v₂ :=
begin cases v₁, cases v₂, unfold product, apply list.mem_product end
theorem mem_of_mem_product_left {n m : nat} {a : A} {b : B} {v₁ : tuple A n} {v₂ : tuple B m} : (a, b) ∈ product v₁ v₂ → a ∈ v₁ :=
begin cases v₁, cases v₂, unfold product, apply list.mem_of_mem_product_left end
theorem mem_of_mem_product_right {n m : nat} {a : A} {b : B} {v₁ : tuple A n} {v₂ : tuple B m} : (a, b) ∈ product v₁ v₂ → b ∈ v₂ :=
begin cases v₁, cases v₂, unfold product, apply list.mem_of_mem_product_right end
/- ith -/
open fin
definition ith {n : nat} : tuple A n → fin n → A
| (tag l h₁) (mk i h₂) := list.ith l i (by rewrite h₁; exact h₂)
lemma ith_zero {n : nat} (a : A) (v : tuple A n) (h : 0 < succ n) : ith (a::v) (mk 0 h) = a :=
by induction v; reflexivity
lemma ith_fin_zero {n : nat} (a : A) (v : tuple A n) : ith (a::v) (fin.zero n) = a :=
by unfold fin.zero; apply ith_zero
lemma ith_succ {n : nat} (a : A) (v : tuple A n) (i : nat) (h : succ i < succ n)
: ith (a::v) (mk (succ i) h) = ith v (mk_pred i h) :=
by induction v; reflexivity
lemma ith_fin_succ {n : nat} (a : A) (v : tuple A n) (i : fin n)
: ith (a::v) (succ i) = ith v i :=
begin cases i, unfold fin.succ, rewrite ith_succ end
lemma ith_zero_eq_head {n : nat} (v : tuple A (nat.succ n)) : ith v (fin.zero n) = head v :=
by rewrite [-eta v, ith_fin_zero, head_cons]
lemma ith_succ_eq_ith_tail {n : nat} (v : tuple A (nat.succ n)) (i : fin n) : ith v (succ i) = ith (tail v) i :=
by rewrite [-eta v, ith_fin_succ, tail_cons]
protected lemma ext {n : nat} (v₁ v₂ : tuple A n) (h : ∀ i : fin n, ith v₁ i = ith v₂ i) : v₁ = v₂ :=
begin
induction n with n ih,
rewrite [tuple0_eq_nil v₁, tuple0_eq_nil v₂],
rewrite [-eta v₁, -eta v₂], congruence,
show head v₁ = head v₂, by rewrite [-ith_zero_eq_head, -ith_zero_eq_head]; apply h,
have ∀ i : fin n, ith (tail v₁) i = ith (tail v₂) i, from
take i, by rewrite [-ith_succ_eq_ith_tail, -ith_succ_eq_ith_tail]; apply h,
show tail v₁ = tail v₂, from ih _ _ this
end
/- tabulate -/
definition tabulate : Π {n : nat}, (fin n → A) → tuple A n
| 0 f := nil
| (n+1) f := f (fin.zero n) :: tabulate (λ i : fin n, f (succ i))
theorem ith_tabulate {n : nat} (f : fin n → A) (i : fin n) : ith (tabulate f) i = f i :=
begin
induction n with n ih,
apply elim0 i,
cases i with v hlt, cases v,
{unfold tabulate, rewrite ith_zero},
{unfold tabulate, rewrite [ith_succ, ih]}
end
variable {n : ℕ}
definition replicate : A → tuple A n
| a := tag (list.replicate n a) (length_replicate n a)
definition dropn : Π (i:ℕ), tuple A n → tuple A (n - i)
| i (tag l p) := tag (list.dropn i l) (p ▸ list.length_dropn i l)
definition firstn : Π (i:ℕ) {p:i ≤ n}, tuple A n → tuple A i
| i isLe (tag l p) :=
let q := calc list.length (list.firstn i l)
= min i (list.length l) : list.length_firstn_eq
... = min i n : p
... = i : min_eq_left isLe in
tag (list.firstn i l) q
definition map₂ : (A → B → C) → tuple A n → tuple B n → tuple C n
| f (tag x px) (tag y py) :=
let z : list C := list.map₂ f x y in
let p : list.length z = n := calc
list.length z = min (list.length x) (list.length y) : list.length_map₂
... = min n n : by rewrite [px, py]
... = n : min_self in
tag z p
section accum
open prod
variable {S : Type}
definition mapAccumR
: (A → S → S × B) → tuple A n → S → S × tuple B n
| f (tag x px) c :=
let z := list.mapAccumR f x c in
let p := calc
list.length (pr₂ (list.mapAccumR f x c))
= length x : length_mapAccumR
... = n : px in
(pr₁ z, tag (pr₂ z) p)
definition mapAccumR₂
: (A → B → S → S × C) → tuple A n → tuple B n → S → S × tuple C n
| f (tag x px) (tag y py) c :=
let z := list.mapAccumR₂ f x y c in
let p := calc
list.length (pr₂ (list.mapAccumR₂ f x y c))
= min (length x) (length y) : length_mapAccumR₂
... = n : by rewrite [ px, py, min_self ] in
(pr₁ z, tag (pr₂ z) p)
end accum
end tuple
|
76bd43c00147b7407bbcb642189274b7cfbc1278 | 3c693e12637d1cf47effc09ab5e21700d1278e73 | /src/analysis/limits.lean | d6e105456420ac346beb6feae3501ab0067e5191 | [] | no_license | ImperialCollegeLondon/Example-Lean-Projects | e731664ae046980921a69ccfeb2286674080c5bb | 87b27ba616eaf03f3642000829a481a1932dd08e | refs/heads/master | 1,685,399,670,721 | 1,623,092,696,000 | 1,623,092,696,000 | 275,571,570 | 19 | 1 | null | 1,593,361,524,000 | 1,593,344,124,000 | Lean | UTF-8 | Lean | false | false | 1,316 | lean | import data.real.basic
import algebra.pi_instances -- to make "a + b" work
/-- is_limit a t means that t is the limit of the sequence a(0), a(1),...-/
def is_limit (a : ℕ → ℝ) (t : ℝ) : Prop :=
∀ ε, (0 : ℝ) < ε → ∃ N, ∀ n, N ≤ n → abs (a n - t) ≤ ε
/-
A note on lambda notation.
λ n, n^2 is the squaring function. A mathematician might write it
n ↦ n^2 . Note that the advantage of these notations is that you
can talk about the function without ever giving it a name; if you
say "f(x)=x^2" then you just used up f.
(λ n, c) is the function that takes n as input, and then always returns c.
So it's a constant function.
-/
-- abs_zero is the lemma that abs 0 = 0
theorem limit_const (c : ℝ) : is_limit (λ n, c) c :=
begin
sorry
end
-- For this one you might find `abs_le` useful.
-- sum of the limits is the limit of the sums
theorem limit_add (a b : ℕ → ℝ) (s t : ℝ) :
is_limit a s → is_limit b t → is_limit (a + b) (s + t) :=
begin
sorry,
end
-- A sequence has at most one limit
theorem limit_unique (a : ℕ → ℝ) (s t : ℝ) :
is_limit a s → is_limit a t → s = t :=
begin
sorry,
end
/-
Hints:
1) This might be trickier than you think.
2) Prove by contradiction. WLOG s < t
3) Use linarith liberally
4) `abs_le` is helpful
-/
|
1b2084e555f9673691835a1b949a4679abd3de14 | 618003631150032a5676f229d13a079ac875ff77 | /src/data/set/basic.lean | e9cab456bc0ed8a5c9e118ccbe0136a06e64835c | [
"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 | 66,555 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
import tactic.basic
import tactic.finish
import data.subtype
import logic.unique
import data.prod
/-!
# Basic properties of sets
Sets in Lean are homogeneous; all their elements have the same type. Sets whose elements
have type `X` are thus defined as `set X := X → Prop`. Note that this function need not
be decidable. The definition is in the core library.
This file provides some basic definitions related to sets and functions not present in the core
library, as well as extra lemmas for functions in the core library (empty set, univ, union,
intersection, insert, singleton, set-theoretic difference, complement, and powerset).
Note that a set is a term, not a type. There is a coersion from `set α` to `Type*` sending
`s` to the corresponding subtype `↑s`.
See also the file `set_theory/zfc.lean`, which contains an encoding of ZFC set theory in Lean.
## Main definitions
Notation used here:
- `f : α → β` is a function,
- `s : set α` and `s₁ s₂ : set α` are subsets of `α`
- `t : set β` is a subset of `β`.
Definitions in the file:
* `strict_subset s₁ s₂ : Prop` : the predicate `s₁ ⊆ s₂` but `s₁ ≠ s₂`.
* `nonempty s : Prop` : the predicate `s ≠ ∅`. Note that this is the preferred way to express the
fact that `s` has an element (see the Implementation Notes).
* `preimage f t : set α` : the preimage f⁻¹(t) (written `f ⁻¹' t` in Lean) of a subset of β.
* `subsingleton s : Prop` : the predicate saying that `s` has at most one element.
* `range f : set β` : the image of `univ` under `f`.
Also works for `{p : Prop} (f : p → α)` (unlike `image`)
* `prod s t : set (α × β)` : the subset `s × t`.
* `inclusion s₁ s₂ : ↑s₁ → ↑s₂` : the map `↑s₁ → ↑s₂` induced by an inclusion `s₁ ⊆ s₂`.
## Notation
* `f ⁻¹' t` for `preimage f t`
* `f '' s` for `image f s`
## Implementation notes
`s.nonempty` is to be preferred to `s ≠ ∅` or `∃ x, x ∈ s`. It has the advantage that
the `s.nonempty` dot notation can be used.
## Tags
set, sets, subset, subsets, image, preimage, pre-image, range, union, intersection, insert,
singleton, complement, powerset
-/
/-! ### Set coercion to a type -/
open function
namespace set
/-- Coercion from a set to the corresponding subtype. -/
instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩
end set
section set_coe
universe u
variables {α : Type u}
theorem set.set_coe_eq_subtype (s : set α) :
coe_sort.{(u+1) (u+2)} s = {x // x ∈ s} := rfl
@[simp] theorem set_coe.forall {s : set α} {p : s → Prop} :
(∀ x : s, p x) ↔ (∀ x (h : x ∈ s), p ⟨x, h⟩) :=
subtype.forall
@[simp] theorem set_coe.exists {s : set α} {p : s → Prop} :
(∃ x : s, p x) ↔ (∃ x (h : x ∈ s), p ⟨x, h⟩) :=
subtype.exists
theorem set_coe.exists' {s : set α} {p : Π x, x ∈ s → Prop} :
(∃ x (h : x ∈ s), p x h) ↔ (∃ x : s, p x.1 x.2) :=
(@set_coe.exists _ _ $ λ x, p x.1 x.2).symm
@[simp] theorem set_coe_cast : ∀ {s t : set α} (H' : s = t) (H : @eq (Type u) s t) (x : s),
cast H x = ⟨x.1, H' ▸ x.2⟩
| s _ rfl _ ⟨x, h⟩ := rfl
theorem set_coe.ext {s : set α} {a b : s} : (↑a : α) = ↑b → a = b :=
subtype.eq
theorem set_coe.ext_iff {s : set α} {a b : s} : (↑a : α) = ↑b ↔ a = b :=
iff.intro set_coe.ext (assume h, h ▸ rfl)
end set_coe
lemma subtype.mem {α : Type*} {s : set α} (p : s) : (p : α) ∈ s := p.property
namespace set
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a : α} {s t : set α}
instance : inhabited (set α) := ⟨∅⟩
@[ext]
theorem ext {a b : set α} (h : ∀ x, x ∈ a ↔ x ∈ b) : a = b :=
funext (assume x, propext (h x))
theorem ext_iff {s t : set α} : s = t ↔ ∀ x, x ∈ s ↔ x ∈ t :=
⟨λ h x, by rw h, ext⟩
@[trans] theorem mem_of_mem_of_subset {x : α} {s t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx
/-! ### Lemmas about `mem` and `set_of` -/
@[simp] theorem mem_set_of_eq {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl
theorem nmem_set_of_eq {a : α} {P : α → Prop} : a ∉ {a : α | P a} = ¬ P a := rfl
@[simp] theorem set_of_mem_eq {s : set α} : {x | x ∈ s} = s := rfl
lemma set_of_app_iff {p : α → Prop} {x : α} : { x | p x } x ↔ p x := iff.rfl
theorem mem_def {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl
instance decidable_mem (s : set α) [H : decidable_pred s] : ∀ a, decidable (a ∈ s) := H
instance decidable_set_of (p : α → Prop) [H : decidable_pred p] : decidable_pred {a | p a} := H
@[simp] theorem set_of_subset_set_of {p q : α → Prop} :
{a | p a} ⊆ {a | q a} ↔ (∀a, p a → q a) := iff.rfl
@[simp] lemma sep_set_of {α} {p q : α → Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl
/-! ### Lemmas about subsets -/
-- TODO(Jeremy): write a tactic to unfold specific instances of generic notation?
theorem subset_def {s t : set α} : (s ⊆ t) = ∀ x, x ∈ s → x ∈ t := rfl
@[refl] theorem subset.refl (a : set α) : a ⊆ a := assume x, id
@[trans] theorem subset.trans {a b c : set α} (ab : a ⊆ b) (bc : b ⊆ c) : a ⊆ c :=
assume x h, bc (ab h)
@[trans] theorem mem_of_eq_of_mem {x y : α} {s : set α} (hx : x = y) (h : y ∈ s) : x ∈ s :=
hx.symm ▸ h
theorem subset.antisymm {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b :=
ext (λ x, iff.intro (λ ina, h₁ ina) (λ inb, h₂ inb))
theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a :=
⟨λ e, e ▸ ⟨subset.refl _, subset.refl _⟩,
λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩
-- an alternative name
theorem eq_of_subset_of_subset {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b :=
subset.antisymm h₁ h₂
theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ :=
assume h₁ h₂, h₁ h₂
theorem not_subset : (¬ s ⊆ t) ↔ ∃a ∈ s, a ∉ t :=
by simp [subset_def, classical.not_forall]
/-! ### Definition of strict subsets `s ⊂ t` and basic properties. -/
/-- `s ⊂ t` means that `s` is a strict subset of `t`, that is, `s ⊆ t` but `s ≠ t`. -/
def strict_subset (s t : set α) := s ⊆ t ∧ ¬ (t ⊆ s)
instance : has_ssubset (set α) := ⟨strict_subset⟩
theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ ¬ (t ⊆ s)) := rfl
theorem eq_or_ssubset_of_subset (h : s ⊆ t) : s = t ∨ s ⊂ t :=
classical.by_cases
(λ H : t ⊆ s, or.inl $ subset.antisymm h H)
(λ H, or.inr ⟨h, H⟩)
lemma exists_of_ssubset {s t : set α} (h : s ⊂ t) : (∃x∈t, x ∉ s) :=
not_subset.1 h.2
lemma ssubset_iff_subset_ne {s t : set α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t :=
by split; simp [set.ssubset_def, ne.def, set.subset.antisymm_iff] {contextual := tt}
theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) :=
assume h : x ∈ ∅, h
@[simp] theorem not_not_mem : ¬ (a ∉ s) ↔ a ∈ s :=
by { classical, exact not_not }
/-! ### Non-empty sets -/
/-- The property `s.nonempty` expresses the fact that the set `s` is not empty. It should be used
in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks
to the dot notation. -/
protected def nonempty (s : set α) : Prop := ∃ x, x ∈ s
lemma nonempty_of_mem {x} (h : x ∈ s) : s.nonempty := ⟨x, h⟩
theorem nonempty.not_subset_empty : s.nonempty → ¬(s ⊆ ∅)
| ⟨x, hx⟩ hs := hs hx
theorem nonempty.ne_empty : s.nonempty → s ≠ ∅
| ⟨x, hx⟩ hs := by { rw hs at hx, exact hx }
/-- Extract a witness from `s.nonempty`. This function might be used instead of case analysis
on the argument. Note that it makes a proof depend on the `classical.choice` axiom. -/
protected noncomputable def nonempty.some (h : s.nonempty) : α := classical.some h
protected lemma nonempty.some_mem (h : s.nonempty) : h.some ∈ s := classical.some_spec h
lemma nonempty.mono (ht : s ⊆ t) (hs : s.nonempty) : t.nonempty := hs.imp ht
lemma nonempty_of_ssubset (ht : s ⊂ t) : (t \ s).nonempty :=
let ⟨x, xt, xs⟩ := exists_of_ssubset ht in ⟨x, xt, xs⟩
lemma nonempty.of_diff (h : (s \ t).nonempty) : s.nonempty := h.imp $ λ _, and.left
lemma nonempty_of_ssubset' (ht : s ⊂ t) : t.nonempty := (nonempty_of_ssubset ht).of_diff
lemma nonempty.inl (hs : s.nonempty) : (s ∪ t).nonempty := hs.imp $ λ _, or.inl
lemma nonempty.inr (ht : t.nonempty) : (s ∪ t).nonempty := ht.imp $ λ _, or.inr
@[simp] lemma union_nonempty : (s ∪ t).nonempty ↔ s.nonempty ∨ t.nonempty := exists_or_distrib
lemma nonempty.left (h : (s ∩ t).nonempty) : s.nonempty := h.imp $ λ _, and.left
lemma nonempty.right (h : (s ∩ t).nonempty) : t.nonempty := h.imp $ λ _, and.right
lemma nonempty_iff_univ_nonempty : nonempty α ↔ (univ : set α).nonempty :=
⟨λ ⟨x⟩, ⟨x, trivial⟩, λ ⟨x, _⟩, ⟨x⟩⟩
@[simp] lemma univ_nonempty : ∀ [h : nonempty α], (univ : set α).nonempty
| ⟨x⟩ := ⟨x, trivial⟩
lemma nonempty.to_subtype (h : s.nonempty) : nonempty s :=
nonempty_subtype.2 h
/-! ### Lemmas about the empty set -/
theorem empty_def : (∅ : set α) = {x | false} := rfl
@[simp] theorem mem_empty_eq (x : α) : x ∈ (∅ : set α) = false := rfl
@[simp] theorem set_of_false : {a : α | false} = ∅ := rfl
theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s :=
by simp [ext_iff]
@[simp] theorem empty_subset (s : set α) : ∅ ⊆ s :=
assume x, assume h, false.elim h
theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ :=
by simp [subset.antisymm_iff]
theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ :=
subset_empty_iff.1
lemma not_nonempty_iff_eq_empty {s : set α} : ¬s.nonempty ↔ s = ∅ :=
by simp only [set.nonempty, eq_empty_iff_forall_not_mem, not_exists]
lemma empty_not_nonempty : ¬(∅ : set α).nonempty :=
not_nonempty_iff_eq_empty.2 rfl
lemma eq_empty_or_nonempty (s : set α) : s = ∅ ∨ s.nonempty :=
classical.by_cases or.inr (λ h, or.inl $ not_nonempty_iff_eq_empty.1 h)
theorem ne_empty_iff_nonempty : s ≠ ∅ ↔ s.nonempty :=
(not_congr not_nonempty_iff_eq_empty.symm).trans classical.not_not
theorem subset_eq_empty {s t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ :=
subset_empty_iff.1 $ e ▸ h
theorem ball_empty_iff {p : α → Prop} :
(∀ x ∈ (∅ : set α), p x) ↔ true :=
by simp [iff_def]
/-!
### Universal set.
In Lean `@univ α` (or `univ : set α`) is the set that contains all elements of type `α`.
Mathematically it is the same as `α` but it has a different type.
-/
theorem univ_def : @univ α = {x | true} := rfl
@[simp] theorem mem_univ (x : α) : x ∈ @univ α := trivial
theorem empty_ne_univ [h : nonempty α] : (∅ : set α) ≠ univ :=
by simp [ext_iff]
@[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial
theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ :=
by simp [subset.antisymm_iff]
theorem eq_univ_of_univ_subset {s : set α} : univ ⊆ s → s = univ :=
univ_subset_iff.1
theorem eq_univ_iff_forall {s : set α} : s = univ ↔ ∀ x, x ∈ s :=
by simp [ext_iff]
theorem eq_univ_of_forall {s : set α} : (∀ x, x ∈ s) → s = univ := eq_univ_iff_forall.2
@[simp] lemma univ_eq_empty_iff : (univ : set α) = ∅ ↔ ¬ nonempty α :=
eq_empty_iff_forall_not_mem.trans ⟨λ H ⟨x⟩, H x trivial, λ H x _, H ⟨x⟩⟩
lemma exists_mem_of_nonempty (α) : ∀ [nonempty α], ∃x:α, x ∈ (univ : set α)
| ⟨x⟩ := ⟨x, trivial⟩
instance univ_decidable : decidable_pred (@set.univ α) :=
λ x, is_true trivial
/-! ### Lemmas about union -/
theorem union_def {s₁ s₂ : set α} : s₁ ∪ s₂ = {a | a ∈ s₁ ∨ a ∈ s₂} := rfl
theorem mem_union_left {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := or.inl
theorem mem_union_right {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := or.inr
theorem mem_or_mem_of_mem_union {x : α} {a b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H
theorem mem_union.elim {x : α} {a b : set α} {P : Prop}
(H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P :=
or.elim H₁ H₂ H₃
theorem mem_union (x : α) (a b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl
@[simp] theorem mem_union_eq (x : α) (a b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl
@[simp] theorem union_self (a : set α) : a ∪ a = a :=
ext (assume x, or_self _)
@[simp] theorem union_empty (a : set α) : a ∪ ∅ = a :=
ext (assume x, or_false _)
@[simp] theorem empty_union (a : set α) : ∅ ∪ a = a :=
ext (assume x, false_or _)
theorem union_comm (a b : set α) : a ∪ b = b ∪ a :=
ext (assume x, or.comm)
theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) :=
ext (assume x, or.assoc)
instance union_is_assoc : is_associative (set α) (∪) :=
⟨union_assoc⟩
instance union_is_comm : is_commutative (set α) (∪) :=
⟨union_comm⟩
theorem union_left_comm (s₁ s₂ s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) :=
by finish
theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ :=
by finish
theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t :=
by finish [subset_def, ext_iff, iff_def]
theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s :=
by finish [subset_def, ext_iff, iff_def]
@[simp] theorem subset_union_left (s t : set α) : s ⊆ s ∪ t := λ x, or.inl
@[simp] theorem subset_union_right (s t : set α) : t ⊆ s ∪ t := λ x, or.inr
theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r :=
by finish [subset_def, union_def]
@[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u :=
by finish [iff_def, subset_def]
theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ :=
by finish [subset_def]
theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t :=
union_subset_union h (by refl)
theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ :=
union_subset_union (by refl) h
lemma subset_union_of_subset_left {s t : set α} (h : s ⊆ t) (u : set α) : s ⊆ t ∪ u :=
subset.trans h (subset_union_left t u)
lemma subset_union_of_subset_right {s u : set α} (h : s ⊆ u) (t : set α) : s ⊆ t ∪ u :=
subset.trans h (subset_union_right t u)
@[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ :=
⟨by finish [ext_iff], by finish [ext_iff]⟩
/-! ### Lemmas about intersection -/
theorem inter_def {s₁ s₂ : set α} : s₁ ∩ s₂ = {a | a ∈ s₁ ∧ a ∈ s₂} := rfl
theorem mem_inter_iff (x : α) (a b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl
@[simp] theorem mem_inter_eq (x : α) (a b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl
theorem mem_inter {x : α} {a b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b :=
⟨ha, hb⟩
theorem mem_of_mem_inter_left {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ a :=
h.left
theorem mem_of_mem_inter_right {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ b :=
h.right
@[simp] theorem inter_self (a : set α) : a ∩ a = a :=
ext (assume x, and_self _)
@[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ :=
ext (assume x, and_false _)
@[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ :=
ext (assume x, false_and _)
theorem inter_comm (a b : set α) : a ∩ b = b ∩ a :=
ext (assume x, and.comm)
theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) :=
ext (assume x, and.assoc)
instance inter_is_assoc : is_associative (set α) (∩) :=
⟨inter_assoc⟩
instance inter_is_comm : is_commutative (set α) (∩) :=
⟨inter_comm⟩
theorem inter_left_comm (s₁ s₂ s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) :=
by finish
theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ :=
by finish
@[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H
@[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H
theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t :=
by finish [subset_def, inter_def]
@[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t :=
⟨λ h, ⟨subset.trans h (inter_subset_left _ _), subset.trans h (inter_subset_right _ _)⟩,
λ ⟨h₁, h₂⟩, subset_inter h₁ h₂⟩
@[simp] theorem inter_univ (a : set α) : a ∩ univ = a :=
ext (assume x, and_true _)
@[simp] theorem univ_inter (a : set α) : univ ∩ a = a :=
ext (assume x, true_and _)
theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u :=
by finish [subset_def]
theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t :=
by finish [subset_def]
theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ :=
by finish [subset_def]
theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s :=
by finish [subset_def, ext_iff, iff_def]
theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t :=
by finish [subset_def, ext_iff, iff_def]
theorem union_inter_cancel_left {s t : set α} : (s ∪ t) ∩ s = s :=
by finish [ext_iff, iff_def]
theorem union_inter_cancel_right {s t : set α} : (s ∪ t) ∩ t = t :=
by finish [ext_iff, iff_def]
/-! ### Distributivity laws -/
theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) :=
ext (assume x, and_or_distrib_left)
theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) :=
ext (assume x, or_and_distrib_right)
theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) :=
ext (assume x, or_and_distrib_left)
theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) :=
ext (assume x, and_or_distrib_right)
/-!
### Lemmas about `insert`
`insert α s` is the set `{α} ∪ s`.
-/
theorem insert_def (x : α) (s : set α) : insert x s = { y | y = x ∨ y ∈ s } := rfl
@[simp] theorem subset_insert (x : α) (s : set α) : s ⊆ insert x s :=
assume y ys, or.inr ys
theorem mem_insert (x : α) (s : set α) : x ∈ insert x s :=
or.inl rfl
theorem mem_insert_of_mem {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := or.inr
theorem eq_or_mem_of_mem_insert {x a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := id
theorem mem_of_mem_insert_of_ne {x a : α} {s : set α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s :=
by finish [insert_def]
@[simp] theorem mem_insert_iff {x a : α} {s : set α} : x ∈ insert a s ↔ (x = a ∨ x ∈ s) := iff.rfl
@[simp] theorem insert_eq_of_mem {a : α} {s : set α} (h : a ∈ s) : insert a s = s :=
by finish [ext_iff, iff_def]
lemma ne_insert_of_not_mem {s : set α} (t : set α) {a : α} (h : a ∉ s) :
s ≠ insert a t :=
by { contrapose! h, simp [h] }
theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) :=
by simp [subset_def, or_imp_distrib, forall_and_distrib]
theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t :=
assume a', or.imp_right (@h a')
theorem ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s :=
by finish [ssubset_iff_subset_ne, ext_iff]
theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) :=
ext $ by simp [or.left_comm]
theorem insert_union : insert a s ∪ t = insert a (s ∪ t) :=
ext $ assume a, by simp [or.comm, or.left_comm]
@[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) :=
ext $ assume a, by simp [or.comm, or.left_comm]
theorem insert_nonempty (a : α) (s : set α) : (insert a s).nonempty :=
⟨a, mem_insert a s⟩
-- useful in proofs by induction
theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ insert a s → P x) :
∀ x, x ∈ s → P x :=
by finish
theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) :
∀ x, x ∈ insert a s → P x :=
by finish
theorem bex_insert_iff {P : α → Prop} {a : α} {s : set α} :
(∃ x ∈ insert a s, P x) ↔ (∃ x ∈ s, P x) ∨ P a :=
by finish [iff_def]
theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} :
(∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) :=
by finish [iff_def]
/-! ### Lemmas about singletons -/
theorem singleton_def (a : α) : ({a} : set α) = insert a ∅ :=
(insert_emptyc_eq _).symm
@[simp] theorem mem_singleton_iff {a b : α} : a ∈ ({b} : set α) ↔ a = b :=
iff.rfl
@[simp]
lemma set_of_eq_eq_singleton {a : α} : {n | n = a} = {a} := set.ext $ λ n, (set.mem_singleton_iff).symm
-- TODO: again, annotation needed
@[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := by finish
theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y :=
by finish
@[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y :=
by finish [ext_iff, iff_def]
theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) :=
by finish
theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s :=
by finish [ext_iff, or_comm]
@[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} :=
by finish
@[simp] theorem singleton_nonempty (a : α) : ({a} : set α).nonempty :=
⟨a, rfl⟩
@[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s :=
⟨λh, h (by simp), λh b e, by simp at e; simp [*]⟩
theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} :=
ext $ by simp
@[simp] theorem singleton_union : {a} ∪ s = insert a s :=
rfl
@[simp] theorem union_singleton : s ∪ {a} = insert a s :=
by rw [union_comm, singleton_union]
theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s :=
by simp [eq_empty_iff_forall_not_mem]
theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s :=
by rw [inter_comm, singleton_inter_eq_empty]
lemma nmem_singleton_empty {s : set α} : s ∉ ({∅} : set (set α)) ↔ s.nonempty :=
by rw [mem_singleton_iff, ← ne.def, ne_empty_iff_nonempty]
instance unique_singleton (a : α) : unique ↥({a} : set α) :=
{ default := ⟨a, mem_singleton a⟩,
uniq :=
begin
intros x,
apply subtype.coe_ext.2,
apply eq_of_mem_singleton (subtype.mem x),
end}
/-! ### Lemmas about sets defined as `{x ∈ s | p x}`. -/
theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} :=
⟨xs, px⟩
@[simp] theorem mem_sep_eq {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl
theorem mem_sep_iff {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x :=
iff.rfl
theorem eq_sep_of_subset {s t : set α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} :=
by finish [ext_iff, iff_def, subset_def]
theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s :=
assume x, and.left
theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) :
∀ x ∈ s, ¬ p x :=
by finish [ext_iff]
@[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} :=
set.ext $ by simp
/-! ### Lemmas about complement -/
theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ -s := h
lemma compl_set_of {α} (p : α → Prop) : - {a | p a} = { a | ¬ p a } := rfl
theorem not_mem_of_mem_compl {s : set α} {x : α} (h : x ∈ -s) : x ∉ s := h
@[simp] theorem mem_compl_eq (s : set α) (x : α) : x ∈ -s = (x ∉ s) := rfl
theorem mem_compl_iff (s : set α) (x : α) : x ∈ -s ↔ x ∉ s := iff.rfl
@[simp] theorem inter_compl_self (s : set α) : s ∩ -s = ∅ :=
by finish [ext_iff]
@[simp] theorem compl_inter_self (s : set α) : -s ∩ s = ∅ :=
by finish [ext_iff]
@[simp] theorem compl_empty : -(∅ : set α) = univ :=
by finish [ext_iff]
@[simp] theorem compl_union (s t : set α) : -(s ∪ t) = -s ∩ -t :=
by finish [ext_iff]
local attribute [simp] -- Will be generalized to lattices in `compl_compl'`
theorem compl_compl (s : set α) : -(-s) = s :=
by finish [ext_iff]
-- ditto
theorem compl_inter (s t : set α) : -(s ∩ t) = -s ∪ -t :=
by finish [ext_iff]
@[simp] theorem compl_univ : -(univ : set α) = ∅ :=
by finish [ext_iff]
lemma compl_empty_iff {s : set α} : -s = ∅ ↔ s = univ :=
by { split, intro h, rw [←compl_compl s, h, compl_empty], intro h, rw [h, compl_univ] }
lemma compl_univ_iff {s : set α} : -s = univ ↔ s = ∅ :=
by rw [←compl_empty_iff, compl_compl]
lemma nonempty_compl {s : set α} : (-s : set α).nonempty ↔ s ≠ univ :=
ne_empty_iff_nonempty.symm.trans $ not_congr $ compl_empty_iff
lemma mem_compl_singleton_iff {a x : α} : x ∈ -({a} : set α) ↔ x ≠ a :=
not_iff_not_of_iff mem_singleton_iff
lemma compl_singleton_eq (a : α) : -({a} : set α) = {x | x ≠ a} :=
ext $ λ x, mem_compl_singleton_iff
theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = -(-s ∩ -t) :=
by simp [compl_inter, compl_compl]
theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = -(-s ∪ -t) :=
by simp [compl_compl]
@[simp] theorem union_compl_self (s : set α) : s ∪ -s = univ :=
by finish [ext_iff]
@[simp] theorem compl_union_self (s : set α) : -s ∪ s = univ :=
by finish [ext_iff]
theorem compl_comp_compl : compl ∘ compl = @id (set α) :=
funext compl_compl
theorem compl_subset_comm {s t : set α} : -s ⊆ t ↔ -t ⊆ s :=
by haveI := classical.prop_decidable; exact
forall_congr (λ a, not_imp_comm)
lemma compl_subset_compl {s t : set α} : -s ⊆ -t ↔ t ⊆ s :=
by rw [compl_subset_comm, compl_compl]
theorem compl_subset_iff_union {s t : set α} : -s ⊆ t ↔ s ∪ t = univ :=
iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a,
by haveI := classical.prop_decidable; exact or_iff_not_imp_left
theorem subset_compl_comm {s t : set α} : s ⊆ -t ↔ t ⊆ -s :=
forall_congr $ λ a, imp_not_comm
theorem subset_compl_iff_disjoint {s t : set α} : s ⊆ -t ↔ s ∩ t = ∅ :=
iff.trans (forall_congr $ λ a, and_imp.symm) subset_empty_iff
lemma subset_compl_singleton_iff {a : α} {s : set α} : s ⊆ -({a} : set α) ↔ a ∉ s :=
by { rw subset_compl_comm, simp }
theorem inter_subset (a b c : set α) : a ∩ b ⊆ c ↔ a ⊆ -b ∪ c :=
begin
classical,
split,
{ intros h x xa, by_cases h' : x ∈ b, simp [h ⟨xa, h'⟩], simp [h'] },
intros h x, rintro ⟨xa, xb⟩, cases h xa, contradiction, assumption
end
/-! ### Lemmas about set difference -/
theorem diff_eq (s t : set α) : s \ t = s ∩ -t := rfl
@[simp] theorem mem_diff {s t : set α} (x : α) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.rfl
theorem mem_diff_of_mem {s t : set α} {x : α} (h1 : x ∈ s) (h2 : x ∉ t) : x ∈ s \ t :=
⟨h1, h2⟩
theorem mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s :=
h.left
theorem not_mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∉ t :=
h.right
theorem nonempty_diff {s t : set α} : (s \ t).nonempty ↔ ¬ (s ⊆ t) :=
⟨λ ⟨x, xs, xt⟩, not_subset.2 ⟨x, xs, xt⟩,
λ h, let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩⟩
theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t :=
by finish [ext_iff, iff_def, subset_def]
theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t :=
by finish [ext_iff, iff_def, subset_def]
theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s :=
by finish [ext_iff, iff_def, subset_def]
theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s :=
by finish [ext_iff, iff_def]
theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t :=
by finish [ext_iff, iff_def]
theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u :=
inter_distrib_right _ _ _
theorem inter_union_distrib_left {s t u : set α} : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) :=
set.ext $ λ _, and_or_distrib_left
theorem inter_union_distrib_right {s t u : set α} : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) :=
set.ext $ λ _, and_or_distrib_right
theorem union_inter_distrib_left {s t u : set α} : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) :=
set.ext $ λ _, or_and_distrib_left
theorem union_inter_distrib_right {s t u : set α} : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) :=
set.ext $ λ _, or_and_distrib_right
theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) :=
inter_assoc _ _ _
theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ :=
by finish [ext_iff]
theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s :=
by finish [ext_iff, iff_def]
theorem diff_subset (s t : set α) : s \ t ⊆ s :=
by finish [subset_def]
theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ :=
by finish [subset_def]
theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t :=
diff_subset_diff h (by refl)
theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t :=
diff_subset_diff (subset.refl s) h
theorem compl_eq_univ_diff (s : set α) : -s = univ \ s :=
by finish [ext_iff]
@[simp] lemma empty_diff (s : set α) : (∅ \ s : set α) = ∅ :=
eq_empty_of_subset_empty $ assume x ⟨hx, _⟩, hx
theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t :=
⟨assume h x hx, classical.by_contradiction $ assume : x ∉ t, show x ∈ (∅ : set α), from h ▸ ⟨hx, this⟩,
assume h, eq_empty_of_subset_empty $ assume x ⟨hx, hnx⟩, hnx $ h hx⟩
@[simp] theorem diff_empty {s : set α} : s \ ∅ = s :=
ext $ assume x, ⟨assume ⟨hx, _⟩, hx, assume h, ⟨h, not_false⟩⟩
theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) :=
ext $ by simp [not_or_distrib, and.comm, and.left_comm]
lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u :=
⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)),
assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩
lemma subset_diff_union (s t : set α) : s ⊆ (s \ t) ∪ t :=
by rw [union_comm, ←diff_subset_iff]
@[simp] lemma diff_singleton_subset_iff {x : α} {s t : set α} : s \ {x} ⊆ t ↔ s ⊆ insert x t :=
by { rw [←union_singleton, union_comm], apply diff_subset_iff }
lemma subset_insert_diff_singleton (x : α) (s : set α) : s ⊆ insert x (s \ {x}) :=
by rw [←diff_singleton_subset_iff]
lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t :=
by rw [diff_subset_iff, diff_subset_iff, union_comm]
@[simp] theorem insert_diff_of_mem (s) (h : a ∈ t) : insert a s \ t = s \ t :=
ext $ by intro; constructor; simp [or_imp_distrib, h] {contextual := tt}
theorem insert_diff_of_not_mem (s) (h : a ∉ t) : insert a s \ t = insert a (s \ t) :=
begin
classical,
ext x,
by_cases h' : x ∈ t,
{ have : x ≠ a,
{ assume H,
rw H at h',
exact h h' },
simp [h, h', this] },
{ simp [h, h'] }
end
theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t :=
by finish [ext_iff, iff_def]
theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t :=
by rw [union_comm, union_diff_self, union_comm]
theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ :=
ext $ by simp [iff_def] {contextual:=tt}
theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ :=
by finish [ext_iff, iff_def, subset_def]
@[simp] theorem diff_singleton_eq_self {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s :=
diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h]
@[simp] theorem insert_diff_singleton {a : α} {s : set α} :
insert a (s \ {a}) = insert a s :=
by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union]
@[simp] lemma diff_self {s : set α} : s \ s = ∅ := ext $ by simp
lemma mem_diff_singleton {s s' : set α} {t : set (set α)} : s ∈ t \ {s'} ↔ (s ∈ t ∧ s ≠ s') :=
by simp
lemma mem_diff_singleton_empty {s : set α} {t : set (set α)} :
s ∈ t \ {∅} ↔ (s ∈ t ∧ s.nonempty) :=
mem_diff_singleton.trans $ and_congr iff.rfl ne_empty_iff_nonempty
/-! ### Powerset -/
theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h
theorem subset_of_mem_powerset {x s : set α} (h : x ∈ powerset s) : x ⊆ s := h
theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl
/-! ### Inverse image -/
/-- The preimage of `s : set β` by `f : α → β`, written `f ⁻¹' s`,
is the set of `x : α` such that `f x ∈ s`. -/
def preimage {α : Type u} {β : Type v} (f : α → β) (s : set β) : set α := {x | f x ∈ s}
infix ` ⁻¹' `:80 := preimage
section preimage
variables {f : α → β} {g : β → γ}
@[simp] theorem preimage_empty : f ⁻¹' ∅ = ∅ := rfl
@[simp] theorem mem_preimage {s : set β} {a : α} : (a ∈ f ⁻¹' s) ↔ (f a ∈ s) := iff.rfl
theorem preimage_mono {s t : set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t :=
assume x hx, h hx
@[simp] theorem preimage_univ : f ⁻¹' univ = univ := rfl
theorem subset_preimage_univ {s : set α} : s ⊆ f ⁻¹' univ := subset_univ _
@[simp] theorem preimage_inter {s t : set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl
@[simp] theorem preimage_union {s t : set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl
@[simp] theorem preimage_compl {s : set β} : f ⁻¹' (- s) = - (f ⁻¹' s) := rfl
@[simp] theorem preimage_diff (f : α → β) (s t : set β) :
f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl
@[simp] theorem preimage_set_of_eq {p : α → Prop} {f : β → α} : f ⁻¹' {a | p a} = {a | p (f a)} :=
rfl
@[simp] theorem preimage_id {s : set α} : id ⁻¹' s = s := rfl
theorem preimage_comp {s : set γ} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl
theorem eq_preimage_subtype_val_iff {p : α → Prop} {s : set (subtype p)} {t : set α} :
s = subtype.val ⁻¹' t ↔ (∀x (h : p x), (⟨x, h⟩ : subtype p) ∈ s ↔ x ∈ t) :=
⟨assume s_eq x h, by rw [s_eq]; simp,
assume h, ext $ assume ⟨x, hx⟩, by simp [h]⟩
lemma if_preimage (s : set α) [decidable_pred s] (f g : α → β) (t : set β) :
(λa, if a ∈ s then f a else g a)⁻¹' t = (s ∩ f ⁻¹' t) ∪ (-s ∩ g ⁻¹' t) :=
begin
ext,
simp only [mem_inter_eq, mem_union_eq, mem_preimage],
split_ifs;
simp [mem_def, h]
end
end preimage
/-! ### Image of a set under a function -/
section image
infix ` '' `:80 := image
-- TODO(Jeremy): use bounded exists in image
theorem mem_image_iff_bex {f : α → β} {s : set α} {y : β} :
y ∈ f '' s ↔ ∃ x (_ : x ∈ s), f x = y := bex_def.symm
theorem mem_image_eq (f : α → β) (s : set α) (y: β) : y ∈ f '' s = ∃ x, x ∈ s ∧ f x = y := rfl
@[simp] theorem mem_image (f : α → β) (s : set α) (y : β) :
y ∈ f '' s ↔ ∃ x, x ∈ s ∧ f x = y := iff.rfl
theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a :=
⟨_, h, rfl⟩
theorem mem_image_of_injective {f : α → β} {a : α} {s : set α} (hf : injective f) :
f a ∈ f '' s ↔ a ∈ s :=
iff.intro
(assume ⟨b, hb, eq⟩, (hf eq) ▸ hb)
(assume h, mem_image_of_mem _ h)
theorem ball_image_of_ball {f : α → β} {s : set α} {p : β → Prop}
(h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f '' s, p y :=
by finish [mem_image_eq]
theorem ball_image_iff {f : α → β} {s : set α} {p : β → Prop} :
(∀ y ∈ f '' s, p y) ↔ (∀ x ∈ s, p (f x)) :=
iff.intro
(assume h a ha, h _ $ mem_image_of_mem _ ha)
(assume h b ⟨a, ha, eq⟩, eq ▸ h a ha)
theorem bex_image_iff {f : α → β} {s : set α} {p : β → Prop} :
(∃ y ∈ f '' s, p y) ↔ (∃ x ∈ s, p (f x)) :=
by simp
theorem mem_image_elim {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) :
∀{y : β}, y ∈ f '' s → C y
| ._ ⟨a, a_in, rfl⟩ := h a a_in
theorem mem_image_elim_on {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f '' s)
(h : ∀ (x : α), x ∈ s → C (f x)) : C y :=
mem_image_elim h h_y
@[congr] lemma image_congr {f g : α → β} {s : set α}
(h : ∀a∈s, f a = g a) : f '' s = g '' s :=
by safe [ext_iff, iff_def]
/-- A common special case of `image_congr` -/
lemma image_congr' {f g : α → β} {s : set α} (h : ∀ (x : α), f x = g x) : f '' s = g '' s :=
image_congr (λx _, h x)
theorem image_comp (f : β → γ) (g : α → β) (a : set α) : (f ∘ g) '' a = f '' (g '' a) :=
subset.antisymm
(ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha)
(ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha)
/- Proof is removed as it uses generated names
TODO(Jeremy): make automatic,
begin
safe [ext_iff, iff_def, mem_image, (∘)],
have h' := h_2 (g a_2),
finish
end -/
/-- A variant of `image_comp`, useful for rewriting -/
lemma image_image (g : β → γ) (f : α → β) (s : set α) : g '' (f '' s) = (λ x, g (f x)) '' s :=
(image_comp g f s).symm
/-- Image is monotone with respect to `⊆`. See `set.monotone_image` for the statement in
terms of `≤`. -/
theorem image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b :=
by finish [subset_def, mem_image_eq]
theorem image_union (f : α → β) (s t : set α) :
f '' (s ∪ t) = f '' s ∪ f '' t :=
by finish [ext_iff, iff_def, mem_image_eq]
@[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := ext $ by simp
lemma image_inter_subset (f : α → β) (s t : set α) :
f '' (s ∩ t) ⊆ f '' s ∩ f '' t :=
subset_inter (image_subset _ $ inter_subset_left _ _) (image_subset _ $ inter_subset_right _ _)
theorem image_inter_on {f : α → β} {s t : set α} (h : ∀x∈t, ∀y∈s, f x = f y → x = y) :
f '' s ∩ f '' t = f '' (s ∩ t) :=
subset.antisymm
(assume b ⟨⟨a₁, ha₁, h₁⟩, ⟨a₂, ha₂, h₂⟩⟩,
have a₂ = a₁, from h _ ha₂ _ ha₁ (by simp *),
⟨a₁, ⟨ha₁, this ▸ ha₂⟩, h₁⟩)
(image_inter_subset _ _ _)
theorem image_inter {f : α → β} {s t : set α} (H : injective f) :
f '' s ∩ f '' t = f '' (s ∩ t) :=
image_inter_on (assume x _ y _ h, H h)
theorem image_univ_of_surjective {ι : Type*} {f : ι → β} (H : surjective f) : f '' univ = univ :=
eq_univ_of_forall $ by simp [image]; exact H
@[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} :=
ext $ λ x, by simp [image]; rw eq_comm
theorem nonempty.image_const {s : set α} (hs : s.nonempty) (a : β) : (λ _, a) '' s = {a} :=
ext $ λ x, ⟨λ ⟨y, _, h⟩, h ▸ mem_singleton _,
λ h, (eq_of_mem_singleton h).symm ▸ hs.imp (λ y hy, ⟨hy, rfl⟩)⟩
@[simp] lemma image_eq_empty {α β} {f : α → β} {s : set α} : f '' s = ∅ ↔ s = ∅ :=
by simp only [eq_empty_iff_forall_not_mem]; exact
⟨λ H a ha, H _ ⟨_, ha, rfl⟩, λ H b ⟨_, ha, _⟩, H _ ha⟩
lemma inter_singleton_nonempty {s : set α} {a : α} : (s ∩ {a}).nonempty ↔ a ∈ s :=
by finish [set.nonempty]
theorem fix_set_compl (t : set α) : compl t = - t := rfl
-- TODO(Jeremy): there is an issue with - t unfolding to compl t
theorem mem_compl_image (t : set α) (S : set (set α)) :
t ∈ compl '' S ↔ -t ∈ S :=
begin
suffices : ∀ x, -x = t ↔ -t = x, {simp [fix_set_compl, this]},
intro x, split; { intro e, subst e, simp }
end
/-- A variant of `image_id` -/
@[simp] lemma image_id' (s : set α) : (λx, x) '' s = s := ext $ by simp
theorem image_id (s : set α) : id '' s = s := by simp
theorem compl_compl_image (S : set (set α)) :
compl '' (compl '' S) = S :=
by rw [← image_comp, compl_comp_compl, image_id]
theorem image_insert_eq {f : α → β} {a : α} {s : set α} :
f '' (insert a s) = insert (f a) (f '' s) :=
ext $ by simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm]
theorem image_pair (f : α → β) (a b : α) : f '' {a, b} = {f a, f b} :=
by simp only [image_insert_eq, image_singleton]
theorem image_subset_preimage_of_inverse {f : α → β} {g : β → α}
(I : left_inverse g f) (s : set α) : f '' s ⊆ g ⁻¹' s :=
λ b ⟨a, h, e⟩, e ▸ ((I a).symm ▸ h : g (f a) ∈ s)
theorem preimage_subset_image_of_inverse {f : α → β} {g : β → α}
(I : left_inverse g f) (s : set β) : f ⁻¹' s ⊆ g '' s :=
λ b h, ⟨f b, h, I b⟩
theorem image_eq_preimage_of_inverse {f : α → β} {g : β → α}
(h₁ : left_inverse g f) (h₂ : right_inverse g f) :
image f = preimage g :=
funext $ λ s, subset.antisymm
(image_subset_preimage_of_inverse h₁ s)
(preimage_subset_image_of_inverse h₂ s)
theorem mem_image_iff_of_inverse {f : α → β} {g : β → α} {b : β} {s : set α}
(h₁ : left_inverse g f) (h₂ : right_inverse g f) :
b ∈ f '' s ↔ g b ∈ s :=
by rw image_eq_preimage_of_inverse h₁ h₂; refl
theorem image_compl_subset {f : α → β} {s : set α} (H : injective f) : f '' -s ⊆ -(f '' s) :=
subset_compl_iff_disjoint.2 $ by simp [image_inter H]
theorem subset_image_compl {f : α → β} {s : set α} (H : surjective f) : -(f '' s) ⊆ f '' -s :=
compl_subset_iff_union.2 $
by rw ← image_union; simp [image_univ_of_surjective H]
theorem image_compl_eq {f : α → β} {s : set α} (H : bijective f) : f '' -s = -(f '' s) :=
subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2)
lemma nonempty.image (f : α → β) {s : set α} : s.nonempty → (f '' s).nonempty
| ⟨x, hx⟩ := ⟨f x, mem_image_of_mem f hx⟩
lemma nonempty.of_image {f : α → β} {s : set α} : (f '' s).nonempty → s.nonempty
| ⟨y, x, hx, _⟩ := ⟨x, hx⟩
@[simp] lemma nonempty_image_iff {f : α → β} {s : set α} :
(f '' s).nonempty ↔ s.nonempty :=
⟨nonempty.of_image, λ h, h.image f⟩
/-- image and preimage are a Galois connection -/
theorem image_subset_iff {s : set α} {t : set β} {f : α → β} :
f '' s ⊆ t ↔ s ⊆ f ⁻¹' t :=
ball_image_iff
theorem image_preimage_subset (f : α → β) (s : set β) :
f '' (f ⁻¹' s) ⊆ s :=
image_subset_iff.2 (subset.refl _)
theorem subset_preimage_image (f : α → β) (s : set α) :
s ⊆ f ⁻¹' (f '' s) :=
λ x, mem_image_of_mem f
theorem preimage_image_eq {f : α → β} (s : set α) (h : injective f) : f ⁻¹' (f '' s) = s :=
subset.antisymm
(λ x ⟨y, hy, e⟩, h e ▸ hy)
(subset_preimage_image f s)
theorem image_preimage_eq {f : α → β} {s : set β} (h : surjective f) : f '' (f ⁻¹' s) = s :=
subset.antisymm
(image_preimage_subset f s)
(λ x hx, let ⟨y, e⟩ := h x in ⟨y, (e.symm ▸ hx : f y ∈ s), e⟩)
lemma preimage_eq_preimage {f : β → α} (hf : surjective f) : f ⁻¹' s = preimage f t ↔ s = t :=
iff.intro
(assume eq, by rw [← @image_preimage_eq β α f s hf, ← @image_preimage_eq β α f t hf, eq])
(assume eq, eq ▸ rfl)
protected lemma push_pull (f : α → β) (s : set α) (t : set β) :
f '' (s ∩ f ⁻¹' t) = f '' s ∩ t :=
begin
apply subset.antisymm,
{ calc f '' (s ∩ f ⁻¹' t) ⊆ f '' s ∩ (f '' (f⁻¹' t)) : image_inter_subset _ _ _
... ⊆ f '' s ∩ t : inter_subset_inter_right _ (image_preimage_subset f t) },
{ rintros _ ⟨⟨x, h', rfl⟩, h⟩,
exact ⟨x, ⟨h', h⟩, rfl⟩ }
end
protected lemma push_pull' (f : α → β) (s : set α) (t : set β) :
f '' (f ⁻¹' t ∩ s) = t ∩ f '' s :=
by simp only [inter_comm, set.push_pull]
theorem compl_image : image (@compl α) = preimage compl :=
image_eq_preimage_of_inverse compl_compl compl_compl
theorem compl_image_set_of {p : set α → Prop} :
compl '' {s | p s} = {s | p (- s)} :=
congr_fun compl_image p
theorem inter_preimage_subset (s : set α) (t : set β) (f : α → β) :
s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) :=
λ x h, ⟨mem_image_of_mem _ h.left, h.right⟩
theorem union_preimage_subset (s : set α) (t : set β) (f : α → β) :
s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) :=
λ x h, or.elim h (λ l, or.inl $ mem_image_of_mem _ l) (λ r, or.inr r)
theorem subset_image_union (f : α → β) (s : set α) (t : set β) :
f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t :=
image_subset_iff.2 (union_preimage_subset _ _ _)
lemma preimage_subset_iff {A : set α} {B : set β} {f : α → β} :
f⁻¹' B ⊆ A ↔ (∀ a : α, f a ∈ B → a ∈ A) := iff.rfl
lemma image_eq_image {f : α → β} (hf : injective f) : f '' s = f '' t ↔ s = t :=
iff.symm $ iff.intro (assume eq, eq ▸ rfl) $ assume eq,
by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq]
lemma image_subset_image_iff {f : α → β} (hf : injective f) : f '' s ⊆ f '' t ↔ s ⊆ t :=
begin
refine (iff.symm $ iff.intro (image_subset f) $ assume h, _),
rw [← preimage_image_eq s hf, ← preimage_image_eq t hf],
exact preimage_mono h
end
lemma injective_image {f : α → β} (hf : injective f) : injective (('') f) :=
assume s t, (image_eq_image hf).1
lemma prod_quotient_preimage_eq_image [s : setoid α] (g : quotient s → β) {h : α → β}
(Hh : h = g ∘ quotient.mk) (r : set (β × β)) :
{x : quotient s × quotient s | (g x.1, g x.2) ∈ r} =
(λ a : α × α, (⟦a.1⟧, ⟦a.2⟧)) '' ((λ a : α × α, (h a.1, h a.2)) ⁻¹' r) :=
Hh.symm ▸ set.ext (λ ⟨a₁, a₂⟩, ⟨quotient.induction_on₂ a₁ a₂
(λ a₁ a₂ h, ⟨(a₁, a₂), h, rfl⟩),
λ ⟨⟨b₁, b₂⟩, h₁, h₂⟩, show (g a₁, g a₂) ∈ r, from
have h₃ : ⟦b₁⟧ = a₁ ∧ ⟦b₂⟧ = a₂ := prod.ext_iff.1 h₂,
h₃.1 ▸ h₃.2 ▸ h₁⟩)
/-- Restriction of `f` to `s` factors through `s.image_factorization f : s → f '' s`. -/
def image_factorization (f : α → β) (s : set α) : s → f '' s :=
λ p, ⟨f p.1, mem_image_of_mem f p.2⟩
lemma image_factorization_eq {f : α → β} {s : set α} :
subtype.val ∘ image_factorization f s = f ∘ subtype.val :=
funext $ λ p, rfl
lemma surjective_onto_image {f : α → β} {s : set α} :
surjective (image_factorization f s) :=
λ ⟨_, ⟨a, ha, rfl⟩⟩, ⟨⟨a, ha⟩, rfl⟩
end image
/-! ### Subsingleton -/
/-- A set `s` is a `subsingleton`, if it has at most one element. -/
protected def subsingleton (s : set α) : Prop :=
∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), x = y
lemma subsingleton.mono (ht : t.subsingleton) (hst : s ⊆ t) : s.subsingleton :=
λ x hx y hy, ht (hst hx) (hst hy)
lemma subsingleton.image (hs : s.subsingleton) (f : α → β) : (f '' s).subsingleton :=
λ _ ⟨x, hx, Hx⟩ _ ⟨y, hy, Hy⟩, Hx ▸ Hy ▸ congr_arg f (hs hx hy)
lemma subsingleton.eq_singleton_of_mem (hs : s.subsingleton) {x:α} (hx : x ∈ s) :
s = {x} :=
ext $ λ y, ⟨λ hy, (hs hx hy) ▸ mem_singleton _, λ hy, (eq_of_mem_singleton hy).symm ▸ hx⟩
lemma subsingleton_empty : (∅ : set α).subsingleton := λ x, false.elim
lemma subsingleton_singleton {a} : ({a} : set α).subsingleton :=
λ x hx y hy, (eq_of_mem_singleton hx).symm ▸ (eq_of_mem_singleton hy).symm ▸ rfl
lemma subsingleton.eq_empty_or_singleton (hs : s.subsingleton) :
s = ∅ ∨ ∃ x, s = {x} :=
s.eq_empty_or_nonempty.elim or.inl (λ ⟨x, hx⟩, or.inr ⟨x, hs.eq_singleton_of_mem hx⟩)
lemma subsingleton_univ [subsingleton α] : (univ : set α).subsingleton :=
λ x hx y hy, subsingleton.elim x y
theorem univ_eq_true_false : univ = ({true, false} : set Prop) :=
eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp)
/-! ### Lemmas about range of a function. -/
section range
variables {f : ι → α}
open function
/-- Range of a function.
This function is more flexible than `f '' univ`, as the image requires that the domain is in Type
and not an arbitrary Sort. -/
def range (f : ι → α) : set α := {x | ∃y, f y = x}
@[simp] theorem mem_range {x : α} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl
theorem mem_range_self (i : ι) : f i ∈ range f := ⟨i, rfl⟩
theorem forall_range_iff {p : α → Prop} : (∀ a ∈ range f, p a) ↔ (∀ i, p (f i)) :=
⟨assume h i, h (f i) (mem_range_self _), assume h a ⟨i, (hi : f i = a)⟩, hi ▸ h i⟩
theorem exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ (∃ i, p (f i)) :=
by simp
lemma exists_range_iff' {p : α → Prop} :
(∃ a, a ∈ range f ∧ p a) ↔ ∃ i, p (f i) :=
by simpa only [exists_prop] using exists_range_iff
theorem range_iff_surjective : range f = univ ↔ surjective f :=
eq_univ_iff_forall
@[simp] theorem range_id : range (@id α) = univ := range_iff_surjective.2 surjective_id
theorem range_inl_union_range_inr : range (@sum.inl α β) ∪ range sum.inr = univ :=
ext $ λ x, by cases x; simp
@[simp] theorem range_quot_mk (r : α → α → Prop) : range (quot.mk r) = univ :=
range_iff_surjective.2 quot.exists_rep
@[simp] theorem image_univ {ι : Type*} {f : ι → β} : f '' univ = range f :=
ext $ by simp [image, range]
theorem image_subset_range {ι : Type*} (f : ι → β) (s : set ι) : f '' s ⊆ range f :=
by rw ← image_univ; exact image_subset _ (subset_univ _)
theorem range_comp {g : α → β} : range (g ∘ f) = g '' range f :=
subset.antisymm
(forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _))
(ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self)
theorem range_subset_iff {s : set α} : range f ⊆ s ↔ ∀ y, f y ∈ s :=
forall_range_iff
lemma range_comp_subset_range (f : α → β) (g : β → γ) : range (g ∘ f) ⊆ range g :=
by rw range_comp; apply image_subset_range
lemma range_nonempty_iff_nonempty : (range f).nonempty ↔ nonempty ι :=
⟨λ ⟨y, x, hxy⟩, ⟨x⟩, λ ⟨x⟩, ⟨f x, mem_range_self x⟩⟩
lemma range_nonempty [h : nonempty ι] (f : ι → α) : (range f).nonempty :=
range_nonempty_iff_nonempty.2 h
@[simp] lemma range_eq_empty {f : ι → α} : range f = ∅ ↔ ¬ nonempty ι :=
not_nonempty_iff_eq_empty.symm.trans $ not_congr range_nonempty_iff_nonempty
theorem image_preimage_eq_inter_range {f : α → β} {t : set β} :
f '' (f ⁻¹' t) = t ∩ range f :=
ext $ assume x, ⟨assume ⟨x, hx, heq⟩, heq ▸ ⟨hx, mem_range_self _⟩,
assume ⟨hx, ⟨y, h_eq⟩⟩, h_eq ▸ mem_image_of_mem f $
show y ∈ f ⁻¹' t, by simp [preimage, h_eq, hx]⟩
lemma image_preimage_eq_of_subset {f : α → β} {s : set β} (hs : s ⊆ range f) :
f '' (f ⁻¹' s) = s :=
by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs]
lemma preimage_subset_preimage_iff {s t : set α} {f : β → α} (hs : s ⊆ range f) :
f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t :=
begin
split,
{ intros h x hx, rcases hs hx with ⟨y, rfl⟩, exact h hx },
intros h x, apply h
end
lemma preimage_eq_preimage' {s t : set α} {f : β → α} (hs : s ⊆ range f) (ht : t ⊆ range f) :
f ⁻¹' s = f ⁻¹' t ↔ s = t :=
begin
split,
{ intro h, apply subset.antisymm, rw [←preimage_subset_preimage_iff hs, h],
rw [←preimage_subset_preimage_iff ht, h] },
rintro rfl, refl
end
theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s :=
set.ext $ λ x, and_iff_left ⟨x, rfl⟩
theorem preimage_image_preimage {f : α → β} {s : set β} :
f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s :=
by rw [image_preimage_eq_inter_range, preimage_inter_range]
@[simp] theorem quot_mk_range_eq [setoid α] : range (λx : α, ⟦x⟧) = univ :=
range_iff_surjective.2 quot.exists_rep
lemma range_const_subset {c : α} : range (λx:ι, c) ⊆ {c} :=
range_subset_iff.2 $ λ x, rfl
@[simp] lemma range_const : ∀ [nonempty ι] {c : α}, range (λx:ι, c) = {c}
| ⟨x⟩ c := subset.antisymm range_const_subset $
assume y hy, (mem_singleton_iff.1 hy).symm ▸ mem_range_self x
/-- Any map `f : ι → β` factors through a map `range_factorization f : ι → range f`. -/
def range_factorization (f : ι → β) : ι → range f :=
λ i, ⟨f i, mem_range_self i⟩
lemma range_factorization_eq {f : ι → β} :
subtype.val ∘ range_factorization f = f :=
funext $ λ i, rfl
lemma surjective_onto_range : surjective (range_factorization f) :=
λ ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩
lemma image_eq_range (f : α → β) (s : set α) : f '' s = range (λ(x : s), f x.1) :=
by { ext, split, rintro ⟨x, h1, h2⟩, exact ⟨⟨x, h1⟩, h2⟩, rintro ⟨⟨x, h1⟩, h2⟩, exact ⟨x, h1, h2⟩ }
@[simp] lemma sum.elim_range {α β γ : Type*} (f : α → γ) (g : β → γ) :
range (sum.elim f g) = range f ∪ range g :=
by simp [set.ext_iff, mem_range]
lemma range_ite_subset' {p : Prop} [decidable p] {f g : α → β} :
range (if p then f else g) ⊆ range f ∪ range g :=
begin
by_cases h : p, {rw if_pos h, exact subset_union_left _ _},
{rw if_neg h, exact subset_union_right _ _}
end
lemma range_ite_subset {p : α → Prop} [decidable_pred p] {f g : α → β} :
range (λ x, if p x then f x else g x) ⊆ range f ∪ range g :=
begin
rw range_subset_iff, intro x, by_cases h : p x,
simp [if_pos h, mem_union, mem_range_self],
simp [if_neg h, mem_union, mem_range_self]
end
end range
/-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/
def pairwise_on (s : set α) (r : α → α → Prop) := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → r x y
theorem pairwise_on.mono {s t : set α} {r}
(h : t ⊆ s) (hp : pairwise_on s r) : pairwise_on t r :=
λ x xt y yt, hp x (h xt) y (h yt)
theorem pairwise_on.mono' {s : set α} {r r' : α → α → Prop}
(H : ∀ a b, r a b → r' a b) (hp : pairwise_on s r) : pairwise_on s r' :=
λ x xs y ys h, H _ _ (hp x xs y ys h)
end set
open set
/-! ### Image and preimage on subtypes -/
namespace subtype
variable {α : Type*}
lemma val_image {p : α → Prop} {s : set (subtype p)} :
subtype.val '' s = {x | ∃h : p x, (⟨x, h⟩ : subtype p) ∈ s} :=
set.ext $ assume a,
⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq ▸ ⟨ha', in_s⟩,
assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩
@[simp] lemma val_range {p : α → Prop} :
set.range (@subtype.val _ p) = {x | p x} :=
by rw ← set.image_univ; simp [-set.image_univ, val_image]
lemma range_val (s : set α) : range (subtype.val : s → α) = s :=
val_range
theorem val_image_subset (s : set α) (t : set (subtype s)) : t.image val ⊆ s :=
λ x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property
theorem val_image_univ (s : set α) : @val _ s '' set.univ = s :=
set.eq_of_subset_of_subset (val_image_subset _ _) (λ x xs, ⟨⟨x, xs⟩, ⟨set.mem_univ _, rfl⟩⟩)
theorem image_preimage_val (s t : set α) :
(@subtype.val _ s) '' ((@subtype.val _ s) ⁻¹' t) = t ∩ s :=
begin
ext x, simp, split,
{ rintros ⟨y, ys, yt, yx⟩, rw ←yx, exact ⟨yt, ys⟩ },
rintros ⟨xt, xs⟩, exact ⟨x, xs, xt, rfl⟩
end
theorem preimage_val_eq_preimage_val_iff (s t u : set α) :
((@subtype.val _ s) ⁻¹' t = (@subtype.val _ s) ⁻¹' u) ↔ (t ∩ s = u ∩ s) :=
begin
rw [←image_preimage_val, ←image_preimage_val],
split, { intro h, rw h },
intro h, exact set.injective_image (val_injective) h
end
lemma exists_set_subtype {t : set α} (p : set α → Prop) :
(∃(s : set t), p (subtype.val '' s)) ↔ ∃(s : set α), s ⊆ t ∧ p s :=
begin
split,
{ rintro ⟨s, hs⟩, refine ⟨subtype.val '' s, _, hs⟩,
convert image_subset_range _ _, rw [range_val] },
rintro ⟨s, hs₁, hs₂⟩, refine ⟨subtype.val ⁻¹' s, _⟩,
rw [image_preimage_eq_of_subset], exact hs₂, rw [range_val], exact hs₁
end
end subtype
namespace set
section range
variable {α : Type*}
@[simp] lemma range_coe_subtype (s : set α) : range (coe : s → α) = s :=
subtype.val_range
theorem preimage_coe_eq_preimage_coe_iff {s t u : set α} :
((coe : s → α) ⁻¹' t = coe ⁻¹' u) ↔ t ∩ s = u ∩ s :=
subtype.preimage_val_eq_preimage_val_iff _ _ _
end range
/-! ### Lemmas about cartesian product of sets -/
section prod
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
variables {s s₁ s₂ : set α} {t t₁ t₂ : set β}
/-- The cartesian product `prod s t` is the set of `(a, b)`
such that `a ∈ s` and `b ∈ t`. -/
protected def prod (s : set α) (t : set β) : set (α × β) :=
{p | p.1 ∈ s ∧ p.2 ∈ t}
lemma prod_eq (s : set α) (t : set β) : set.prod s t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl
theorem mem_prod_eq {p : α × β} : p ∈ set.prod s t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl
@[simp] theorem mem_prod {p : α × β} : p ∈ set.prod s t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl
lemma mk_mem_prod {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ set.prod s t := ⟨a_in, b_in⟩
lemma prod_subset_iff {P : set (α × β)} :
(set.prod s t ⊆ P) ↔ ∀ (x ∈ s) (y ∈ t), (x, y) ∈ P :=
⟨λ h _ xin _ yin, h (mk_mem_prod xin yin),
λ h _ pin, by { cases mem_prod.1 pin with hs ht, simpa using h _ hs _ ht }⟩
@[simp] theorem prod_empty : set.prod s ∅ = (∅ : set (α × β)) :=
ext $ by simp [set.prod]
@[simp] theorem empty_prod : set.prod ∅ t = (∅ : set (α × β)) :=
ext $ by simp [set.prod]
theorem insert_prod {a : α} {s : set α} {t : set β} :
set.prod (insert a s) t = (prod.mk a '' t) ∪ set.prod s t :=
ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end
theorem prod_insert {b : β} {s : set α} {t : set β} :
set.prod s (insert b t) = ((λa, (a, b)) '' s) ∪ set.prod s t :=
ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end
theorem prod_preimage_eq {f : γ → α} {g : δ → β} :
set.prod (preimage f s) (preimage g t) = preimage (λp, (f p.1, g p.2)) (set.prod s t) := rfl
theorem prod_mono {s₁ s₂ : set α} {t₁ t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) :
set.prod s₁ t₁ ⊆ set.prod s₂ t₂ :=
assume x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩
theorem prod_inter_prod : set.prod s₁ t₁ ∩ set.prod s₂ t₂ = set.prod (s₁ ∩ s₂) (t₁ ∩ t₂) :=
subset.antisymm
(assume ⟨a, b⟩ ⟨⟨ha₁, hb₁⟩, ⟨ha₂, hb₂⟩⟩, ⟨⟨ha₁, ha₂⟩, ⟨hb₁, hb₂⟩⟩)
(subset_inter
(prod_mono (inter_subset_left _ _) (inter_subset_left _ _))
(prod_mono (inter_subset_right _ _) (inter_subset_right _ _)))
theorem image_swap_prod : (λp:β×α, (p.2, p.1)) '' set.prod t s = set.prod s t :=
ext $ assume ⟨a, b⟩, by simp [mem_image_eq, set.prod, and_comm]; exact
⟨ assume ⟨b', a', ⟨h_a, h_b⟩, h⟩, by subst a'; subst b'; assumption,
assume h, ⟨b, a, ⟨rfl, rfl⟩, h⟩⟩
theorem image_swap_eq_preimage_swap : image (@prod.swap α β) = preimage prod.swap :=
image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse
theorem prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} :
set.prod (image m₁ s) (image m₂ t) = image (λp:α×β, (m₁ p.1, m₂ p.2)) (set.prod s t) :=
ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm]
theorem prod_range_range_eq {α β γ δ} {m₁ : α → γ} {m₂ : β → δ} :
set.prod (range m₁) (range m₂) = range (λp:α×β, (m₁ p.1, m₂ p.2)) :=
ext $ by simp [range]
theorem prod_range_univ_eq {α β γ} {m₁ : α → γ} :
set.prod (range m₁) (univ : set β) = range (λp:α×β, (m₁ p.1, p.2)) :=
ext $ by simp [range]
theorem prod_univ_range_eq {α β δ} {m₂ : β → δ} :
set.prod (univ : set α) (range m₂) = range (λp:α×β, (p.1, m₂ p.2)) :=
ext $ by simp [range]
@[simp] theorem prod_singleton_singleton {a : α} {b : β} :
set.prod {a} {b} = ({(a, b)} : set (α×β)) :=
ext $ by simp [set.prod]
theorem nonempty.prod : s.nonempty → t.nonempty → (s.prod t).nonempty
| ⟨x, hx⟩ ⟨y, hy⟩ := ⟨(x, y), ⟨hx, hy⟩⟩
theorem nonempty.fst : (s.prod t).nonempty → s.nonempty
| ⟨p, hp⟩ := ⟨p.1, hp.1⟩
theorem nonempty.snd : (s.prod t).nonempty → t.nonempty
| ⟨p, hp⟩ := ⟨p.2, hp.2⟩
theorem prod_nonempty_iff : (s.prod t).nonempty ↔ s.nonempty ∧ t.nonempty :=
⟨λ h, ⟨h.fst, h.snd⟩, λ h, nonempty.prod h.1 h.2⟩
theorem prod_eq_empty_iff {s : set α} {t : set β} :
set.prod s t = ∅ ↔ (s = ∅ ∨ t = ∅) :=
by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, classical.not_and_distrib]
@[simp] theorem prod_mk_mem_set_prod_eq {a : α} {b : β} {s : set α} {t : set β} :
(a, b) ∈ set.prod s t = (a ∈ s ∧ b ∈ t) := rfl
@[simp] theorem univ_prod_univ : set.prod (@univ α) (@univ β) = univ :=
ext $ assume ⟨a, b⟩, by simp
lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} :
set.prod s t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W :=
by simp [subset_def]
lemma fst_image_prod_subset (s : set α) (t : set β) :
prod.fst '' (set.prod s t) ⊆ s :=
λ _ h, let ⟨_, ⟨h₂, _⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂
lemma prod_subset_preimage_fst (s : set α) (t : set β) :
set.prod s t ⊆ prod.fst ⁻¹' s :=
image_subset_iff.1 (fst_image_prod_subset s t)
lemma fst_image_prod (s : set β) {t : set α} (ht : t.nonempty) :
prod.fst '' (set.prod s t) = s :=
set.subset.antisymm (fst_image_prod_subset _ _)
$ λ y y_in, let ⟨x, x_in⟩ := ht in
⟨(y, x), ⟨y_in, x_in⟩, rfl⟩
lemma snd_image_prod_subset (s : set α) (t : set β) :
prod.snd '' (set.prod s t) ⊆ t :=
λ _ h, let ⟨_, ⟨_, h₂⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂
lemma prod_subset_preimage_snd (s : set α) (t : set β) :
set.prod s t ⊆ prod.snd ⁻¹' t :=
image_subset_iff.1 (snd_image_prod_subset s t)
lemma snd_image_prod {s : set α} (hs : s.nonempty) (t : set β) :
prod.snd '' (set.prod s t) = t :=
set.subset.antisymm (snd_image_prod_subset _ _)
$ λ y y_in, let ⟨x, x_in⟩ := hs in
⟨(x, y), ⟨x_in, y_in⟩, rfl⟩
/-- A product set is included in a product set if and only factors are included, or a factor of the
first set is empty. -/
lemma prod_subset_prod_iff :
(set.prod s t ⊆ set.prod s₁ t₁) ↔ (s ⊆ s₁ ∧ t ⊆ t₁) ∨ (s = ∅) ∨ (t = ∅) :=
begin
classical,
cases (set.prod s t).eq_empty_or_nonempty with h h,
{ simp [h, prod_eq_empty_iff.1 h] },
{ have st : s.nonempty ∧ t.nonempty, by rwa [prod_nonempty_iff] at h,
split,
{ assume H : set.prod s t ⊆ set.prod s₁ t₁,
have h' : s₁.nonempty ∧ t₁.nonempty := prod_nonempty_iff.1 (h.mono H),
refine or.inl ⟨_, _⟩,
show s ⊆ s₁,
{ have := image_subset (prod.fst : α × β → α) H,
rwa [fst_image_prod _ st.2, fst_image_prod _ h'.2] at this },
show t ⊆ t₁,
{ have := image_subset (prod.snd : α × β → β) H,
rwa [snd_image_prod st.1, snd_image_prod h'.1] at this } },
{ assume H,
simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H,
exact prod_mono H.1 H.2 } }
end
end prod
/-! ### Lemmas about set-indexed products of sets -/
section pi
variables {α : Type*} {π : α → Type*}
/-- Given an index set `i` and a family of sets `s : Πa, set (π a)`, `pi i s`
is the set of dependent functions `f : Πa, π a` such that `f a` belongs to `π a`
whenever `a ∈ i`. -/
def pi (i : set α) (s : Πa, set (π a)) : set (Πa, π a) := { f | ∀a∈i, f a ∈ s a }
@[simp] lemma pi_empty_index (s : Πa, set (π a)) : pi ∅ s = univ := by ext; simp [pi]
@[simp] lemma pi_insert_index (a : α) (i : set α) (s : Πa, set (π a)) :
pi (insert a i) s = ((λf, f a) ⁻¹' s a) ∩ pi i s :=
by ext; simp [pi, or_imp_distrib, forall_and_distrib]
@[simp] lemma pi_singleton_index (a : α) (s : Πa, set (π a)) :
pi {a} s = ((λf:(Πa, π a), f a) ⁻¹' s a) :=
by ext; simp [pi]
lemma pi_if {p : α → Prop} [h : decidable_pred p] (i : set α) (s t : Πa, set (π a)) :
pi i (λa, if p a then s a else t a) = pi {a ∈ i | p a} s ∩ pi {a ∈ i | ¬ p a} t :=
begin
ext f,
split,
{ assume h, split; { rintros a ⟨hai, hpa⟩, simpa [*] using h a } },
{ rintros ⟨hs, ht⟩ a hai,
by_cases p a; simp [*, pi] at * }
end
end pi
/-! ### Lemmas about `inclusion`, the injection of subtypes induced by `⊆` -/
section inclusion
variable {α : Type*}
/-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s ⊆ t` -/
def inclusion {s t : set α} (h : s ⊆ t) : s → t :=
λ x : s, (⟨x, h x.2⟩ : t)
@[simp] lemma inclusion_self {s : set α} (x : s) :
inclusion (set.subset.refl _) x = x := by cases x; refl
@[simp] lemma inclusion_inclusion {s t u : set α} (hst : s ⊆ t) (htu : t ⊆ u)
(x : s) : inclusion htu (inclusion hst x) = inclusion (set.subset.trans hst htu) x :=
by cases x; refl
lemma inclusion_injective {s t : set α} (h : s ⊆ t) :
function.injective (inclusion h)
| ⟨_, _⟩ ⟨_, _⟩ := subtype.ext.2 ∘ subtype.ext.1
lemma range_inclusion {s t : set α} (h : s ⊆ t) : range (inclusion h) = {x : t | (x:α) ∈ s} :=
ext $ λ ⟨x, hx⟩ , by simp [inclusion]
end inclusion
end set
namespace subsingleton
variables {α : Type*} [subsingleton α]
lemma eq_univ_of_nonempty {s : set α} : s.nonempty → s = univ :=
λ ⟨x, hx⟩, eq_univ_of_forall $ λ y, subsingleton.elim x y ▸ hx
@[elab_as_eliminator]
lemma set_cases {p : set α → Prop} (h0 : p ∅) (h1 : p univ) (s) : p s :=
s.eq_empty_or_nonempty.elim (λ h, h.symm ▸ h0) $ λ h, (eq_univ_of_nonempty h).symm ▸ h1
end subsingleton
namespace function
variables {ι : Sort*} {α : Type*} {β : Type*}
lemma surjective.injective_preimage {f : β → α} (hf : surjective f) : injective (preimage f) :=
assume s t, (preimage_eq_preimage hf).1
lemma surjective.range_eq {f : ι → α} (hf : surjective f) : range f = univ :=
range_iff_surjective.2 hf
lemma surjective.range_comp (g : α → β) {f : ι → α} (hf : surjective f) :
range (g ∘ f) = range g :=
by rw [range_comp, hf.range_eq, image_univ]
end function
|
b9793808866ebdf4645fa99db245dfbaa1824219 | b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77 | /src/number_theory/divisors.lean | 921875eccda429d456c40cf5a2e8d3867b27bfea | [
"Apache-2.0"
] | permissive | molodiuc/mathlib | cae2ba3ef1601c1f42ca0b625c79b061b63fef5b | 98ebe5a6739fbe254f9ee9d401882d4388f91035 | refs/heads/master | 1,674,237,127,059 | 1,606,353,533,000 | 1,606,353,533,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,243 | lean | /-
Copyright (c) 2020 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import algebra.big_operators.order
import tactic
import data.nat.prime
/-!
# Divisor finsets
This file defines sets of divisors of a natural number. This is particularly useful as background
for defining Dirichlet convolution.
## Main Definitions
Let `n : ℕ`. All of the following definitions are in the `nat` namespace:
* `divisors n` is the `finset` of natural numbers that divide `n`.
* `proper_divisors n` is the `finset` of natural numbers that divide `n`, other than `n`.
* `divisors_antidiagonal n` is the `finset` of pairs `(x,y)` such that `x * y = n`.
* `perfect n` is true when `n` is positive and the sum of `proper_divisors n` is `n`.
## Implementation details
* `divisors 0`, `proper_divisors 0`, and `divisors_antidiagonal 0` are defined to be `∅`.
## Tags
divisors, perfect numbers
-/
open_locale classical
open_locale big_operators
open finset
namespace nat
variable (n : ℕ)
/-- `divisors n` is the `finset` of divisors of `n`. As a special case, `divisors 0 = ∅`. -/
def divisors : finset ℕ := finset.filter (λ x : ℕ, x ∣ n) (finset.Ico 1 (n + 1))
/-- `proper_divisors n` is the `finset` of divisors of `n`, other than `n`.
As a special case, `proper_divisors 0 = ∅`. -/
def proper_divisors : finset ℕ := finset.filter (λ x : ℕ, x ∣ n) (finset.Ico 1 n)
/-- `divisors_antidiagonal n` is the `finset` of pairs `(x,y)` such that `x * y = n`.
As a special case, `divisors_antidiagonal 0 = ∅`. -/
def divisors_antidiagonal : finset (ℕ × ℕ) :=
((finset.Ico 1 (n + 1)).product (finset.Ico 1 (n + 1))).filter (λ x, x.fst * x.snd = n)
variable {n}
lemma proper_divisors.not_self_mem : ¬ n ∈ proper_divisors n :=
begin
rw proper_divisors,
simp,
end
@[simp]
lemma mem_proper_divisors {m : ℕ} : n ∈ proper_divisors m ↔ n ∣ m ∧ n < m :=
begin
rw [proper_divisors, finset.mem_filter, finset.Ico.mem, and_comm],
apply and_congr_right,
rw and_iff_right_iff_imp,
intros hdvd hlt,
apply nat.pos_of_ne_zero _,
rintro rfl,
rw zero_dvd_iff.1 hdvd at hlt,
apply lt_irrefl 0 hlt,
end
lemma divisors_eq_proper_divisors_insert_self_of_pos (h : 0 < n):
divisors n = has_insert.insert n (proper_divisors n) :=
by rw [divisors, proper_divisors, finset.Ico.succ_top h, finset.filter_insert, if_pos (dvd_refl n)]
@[simp]
lemma mem_divisors {m : ℕ} :
n ∈ divisors m ↔ (n ∣ m ∧ m ≠ 0) :=
begin
cases m,
{ simp [divisors] },
simp only [divisors, finset.Ico.mem, ne.def, finset.mem_filter, succ_ne_zero, and_true,
and_iff_right_iff_imp, not_false_iff],
intro hdvd,
split,
{ apply nat.pos_of_ne_zero,
rintro rfl,
apply nat.succ_ne_zero,
rwa zero_dvd_iff at hdvd },
{ rw nat.lt_succ_iff,
apply nat.le_of_dvd (nat.succ_pos m) hdvd }
end
lemma dvd_of_mem_divisors {m : ℕ} (h : n ∈ divisors m) : n ∣ m :=
begin
cases m,
{ apply dvd_zero },
{ simp [mem_divisors.1 h], }
end
@[simp]
lemma mem_divisors_antidiagonal {x : ℕ × ℕ} :
x ∈ divisors_antidiagonal n ↔ x.fst * x.snd = n ∧ n ≠ 0 :=
begin
simp only [divisors_antidiagonal, finset.Ico.mem, ne.def, finset.mem_filter, finset.mem_product],
rw and_comm,
apply and_congr_right,
rintro rfl,
split; intro h,
{ contrapose! h, simp [h], },
{ rw [nat.lt_add_one_iff, nat.lt_add_one_iff],
rw [mul_eq_zero, decidable.not_or_iff_and_not] at h,
simp only [succ_le_of_lt (nat.pos_of_ne_zero h.1), succ_le_of_lt (nat.pos_of_ne_zero h.2),
true_and],
exact ⟨le_mul_of_pos_right (nat.pos_of_ne_zero h.2),
le_mul_of_pos_left (nat.pos_of_ne_zero h.1)⟩ }
end
variable {n}
lemma divisor_le {m : ℕ}:
n ∈ divisors m → n ≤ m :=
begin
cases m,
{ simp },
simp only [mem_divisors, m.succ_ne_zero, and_true, ne.def, not_false_iff],
exact nat.le_of_dvd (nat.succ_pos m),
end
@[simp]
lemma divisors_zero : divisors 0 = ∅ := by { ext, simp }
@[simp]
lemma proper_divisors_zero : proper_divisors 0 = ∅ := by { ext, simp }
lemma proper_divisors_subset_divisors : proper_divisors n ⊆ divisors n :=
begin
cases n,
{ simp },
rw [divisors_eq_proper_divisors_insert_self_of_pos (nat.succ_pos _)],
apply subset_insert,
end
@[simp]
lemma divisors_one : divisors 1 = {1} := by { ext, simp }
@[simp]
lemma proper_divisors_one : proper_divisors 1 = ∅ :=
begin
ext,
simp only [finset.not_mem_empty, nat.dvd_one, not_and, not_lt, mem_proper_divisors, iff_false],
apply ge_of_eq,
end
lemma pos_of_mem_divisors {m : ℕ} (h : m ∈ n.divisors) : 0 < m :=
begin
cases m,
{ rw [mem_divisors, zero_dvd_iff] at h,
rcases h with ⟨rfl, h⟩,
exfalso,
apply h rfl },
apply nat.succ_pos,
end
lemma pos_of_mem_proper_divisors {m : ℕ} (h : m ∈ n.proper_divisors) : 0 < m :=
pos_of_mem_divisors (proper_divisors_subset_divisors h)
lemma one_mem_proper_divisors_iff_one_lt :
1 ∈ n.proper_divisors ↔ 1 < n :=
by rw [mem_proper_divisors, and_iff_right (one_dvd _)]
@[simp]
lemma divisors_antidiagonal_zero : divisors_antidiagonal 0 = ∅ := by { ext, simp }
@[simp]
lemma divisors_antidiagonal_one : divisors_antidiagonal 1 = {(1,1)} :=
by { ext, simp [nat.mul_eq_one_iff, prod.ext_iff], }
lemma swap_mem_divisors_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) :
x.swap ∈ divisors_antidiagonal n :=
begin
rw [mem_divisors_antidiagonal, mul_comm] at h,
simp [h.1, h.2],
end
lemma fst_mem_divisors_of_mem_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) :
x.fst ∈ divisors n :=
begin
rw mem_divisors_antidiagonal at h,
simp [dvd.intro _ h.1, h.2],
end
lemma snd_mem_divisors_of_mem_antidiagonal {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) :
x.snd ∈ divisors n :=
begin
rw mem_divisors_antidiagonal at h,
simp [dvd.intro_left _ h.1, h.2],
end
@[simp]
lemma map_swap_divisors_antidiagonal :
(divisors_antidiagonal n).map ⟨prod.swap, prod.swap_right_inverse.injective⟩
= divisors_antidiagonal n :=
begin
ext,
simp only [exists_prop, mem_divisors_antidiagonal, finset.mem_map, function.embedding.coe_fn_mk,
ne.def, prod.swap_prod_mk, prod.exists],
split,
{ rintros ⟨x, y, ⟨⟨rfl, h⟩, rfl⟩⟩,
simp [mul_comm, h], },
{ rintros ⟨rfl, h⟩,
use [a.snd, a.fst],
rw mul_comm,
simp [h] }
end
lemma sum_divisors_eq_sum_proper_divisors_add_self :
∑ i in divisors n, i = ∑ i in proper_divisors n, i + n :=
begin
cases n,
{ simp },
{ rw [divisors_eq_proper_divisors_insert_self_of_pos (nat.succ_pos _),
finset.sum_insert (proper_divisors.not_self_mem), add_comm] }
end
/-- `n : ℕ` is perfect if and only the sum of the proper divisors of `n` is `n` and `n`
is positive. -/
def perfect (n : ℕ) : Prop := (∑ i in proper_divisors n, i = n) ∧ 0 < n
theorem perfect_iff_sum_proper_divisors (h : 0 < n) :
perfect n ↔ ∑ i in proper_divisors n, i = n := and_iff_left h
theorem perfect_iff_sum_divisors_eq_two_mul (h : 0 < n) :
perfect n ↔ ∑ i in divisors n, i = 2 * n :=
begin
rw [perfect_iff_sum_proper_divisors h, sum_divisors_eq_sum_proper_divisors_add_self, two_mul],
split; intro h,
{ rw h },
{ apply add_right_cancel h }
end
lemma mem_divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) {x : ℕ} :
x ∈ divisors (p ^ k) ↔ ∃ (j : ℕ) (H : j ≤ k), x = p ^ j :=
by rw [mem_divisors, nat.dvd_prime_pow pp, and_iff_left (ne_of_gt (pow_pos pp.pos k))]
lemma prime.divisors {p : ℕ} (pp : p.prime) :
divisors p = {1, p} :=
begin
ext,
simp only [pp.ne_zero, and_true, ne.def, not_false_iff, finset.mem_insert,
finset.mem_singleton, mem_divisors],
refine ⟨pp.2 a, λ h, _⟩,
rcases h; subst h,
apply one_dvd,
end
lemma prime.proper_divisors {p : ℕ} (pp : p.prime) :
proper_divisors p = {1} :=
by rw [← erase_insert (proper_divisors.not_self_mem),
← divisors_eq_proper_divisors_insert_self_of_pos pp.pos,
pp.divisors, insert_singleton_comm, erase_insert (λ con, pp.ne_one (mem_singleton.1 con))]
lemma divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) :
divisors (p ^ k) = (finset.range (k + 1)).map ⟨pow p, pow_right_injective pp.two_le⟩ :=
by { ext, simp [mem_divisors_prime_pow, pp, nat.lt_succ_iff, @eq_comm _ a] }
lemma eq_proper_divisors_of_subset_of_sum_eq_sum {s : finset ℕ} (hsub : s ⊆ n.proper_divisors) :
∑ x in s, x = ∑ x in n.proper_divisors, x → s = n.proper_divisors :=
begin
cases n,
{ rw [proper_divisors_zero, subset_empty] at hsub,
simp [hsub] },
classical,
rw [← sum_sdiff hsub],
intros h,
apply subset.antisymm hsub,
rw [← sdiff_eq_empty_iff_subset],
contrapose h,
rw [← ne.def, ← nonempty_iff_ne_empty] at h,
apply ne_of_lt,
rw [← zero_add (∑ x in s, x), ← add_assoc, add_zero],
apply add_lt_add_right,
have hlt := sum_lt_sum_of_nonempty h (λ x hx, pos_of_mem_proper_divisors (sdiff_subset _ _ hx)),
simp only [sum_const_zero] at hlt,
apply hlt
end
lemma sum_proper_divisors_dvd (h : ∑ x in n.proper_divisors, x ∣ n) :
(∑ x in n.proper_divisors, x = 1) ∨ (∑ x in n.proper_divisors, x = n) :=
begin
cases n,
{ simp },
cases n,
{ contrapose! h,
simp, },
rw or_iff_not_imp_right,
intro ne_n,
have hlt : ∑ x in n.succ.succ.proper_divisors, x < n.succ.succ :=
lt_of_le_of_ne (nat.le_of_dvd (nat.succ_pos _) h) ne_n,
symmetry,
rw [← mem_singleton, eq_proper_divisors_of_subset_of_sum_eq_sum (singleton_subset_iff.2
(mem_proper_divisors.2 ⟨h, hlt⟩)) sum_singleton, mem_proper_divisors],
refine ⟨one_dvd _, nat.succ_lt_succ (nat.succ_pos _)⟩,
end
@[simp]
lemma prime.sum_proper_divisors {α : Type*} [add_comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) :
∑ x in p.proper_divisors, f x = f 1 :=
by simp [h.proper_divisors]
@[simp]
lemma prime.sum_divisors {α : Type*} [add_comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) :
∑ x in p.divisors, f x = f p + f 1 :=
by rw [divisors_eq_proper_divisors_insert_self_of_pos h.pos,
sum_insert proper_divisors.not_self_mem, h.sum_proper_divisors]
lemma proper_divisors_eq_singleton_one_iff_prime :
n.proper_divisors = {1} ↔ n.prime :=
⟨λ h, begin
have h1 := mem_singleton.2 rfl,
rw [← h, mem_proper_divisors] at h1,
refine ⟨h1.2, _⟩,
intros m hdvd,
rw [← mem_singleton, ← h, mem_proper_divisors],
cases lt_or_eq_of_le (nat.le_of_dvd (lt_trans (nat.succ_pos _) h1.2) hdvd),
{ left,
exact ⟨hdvd, h_1⟩ },
{ right,
exact h_1 }
end, prime.proper_divisors⟩
lemma sum_proper_divisors_eq_one_iff_prime :
∑ x in n.proper_divisors, x = 1 ↔ n.prime :=
begin
cases n,
{ simp [nat.not_prime_zero] },
cases n,
{ simp [nat.not_prime_one] },
rw [← proper_divisors_eq_singleton_one_iff_prime],
refine ⟨λ h, _, λ h, h.symm ▸ sum_singleton⟩,
rw [@eq_comm (finset ℕ) _ _],
apply eq_proper_divisors_of_subset_of_sum_eq_sum
(singleton_subset_iff.2 (one_mem_proper_divisors_iff_one_lt.2 (succ_lt_succ (nat.succ_pos _))))
(eq.trans sum_singleton h.symm)
end
@[simp]
lemma prod_divisors_prime {α : Type*} [comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) :
∏ x in p.divisors, f x = f p * f 1 :=
@prime.sum_divisors (additive α) _ _ _ h
@[simp]
lemma sum_divisors_prime_pow {α : Type*} [add_comm_monoid α] {k p : ℕ} {f : ℕ → α} (h : p.prime) :
∑ x in (p ^ k).divisors, f x = ∑ x in range (k + 1), f (p ^ x) :=
by simp [h, divisors_prime_pow]
@[simp]
lemma prod_divisors_prime_pow {α : Type*} [comm_monoid α] {k p : ℕ} {f : ℕ → α} (h : p.prime) :
∏ x in (p ^ k).divisors, f x = ∏ x in range (k + 1), f (p ^ x) :=
@sum_divisors_prime_pow (additive α) _ _ _ _ h
@[simp]
lemma filter_dvd_eq_divisors {n : ℕ} (h : n ≠ 0) :
finset.filter (λ (x : ℕ), x ∣ n) (finset.range (n : ℕ).succ) = (n : ℕ).divisors :=
begin
apply finset.ext,
simp only [h, mem_filter, and_true, and_iff_right_iff_imp, cast_id, mem_range, ne.def,
not_false_iff, mem_divisors],
intros a ha,
exact nat.lt_succ_of_le (nat.divisor_le (nat.mem_divisors.2 ⟨ha, h⟩))
end
end nat
|
eb1d98f280cc56ac00b39b37e6c7861550b5001e | abd85493667895c57a7507870867b28124b3998f | /src/data/int/basic.lean | 3a4e4e632d4250d613c53cd701d69d384a2a2b2c | [
"Apache-2.0"
] | permissive | pechersky/mathlib | d56eef16bddb0bfc8bc552b05b7270aff5944393 | f1df14c2214ee114c9738e733efd5de174deb95d | refs/heads/master | 1,666,714,392,571 | 1,591,747,567,000 | 1,591,747,567,000 | 270,557,274 | 0 | 0 | Apache-2.0 | 1,591,597,975,000 | 1,591,597,974,000 | null | UTF-8 | Lean | false | false | 54,748 | 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 algebra.char_zero
import init_.data.int.order
import algebra.ring
import data.list.range
open nat
namespace int
instance : inhabited ℤ := ⟨int.zero⟩
@[simp] lemma default_eq_zero : default ℤ = 0 := rfl
meta instance : has_to_format ℤ := ⟨λ z, to_string z⟩
meta instance : has_reflect ℤ := by tactic.mk_has_reflect_instance
attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ
attribute [simp] int.of_nat_eq_coe int.bodd
@[simp] theorem add_def {a b : ℤ} : int.add a b = a + b := rfl
@[simp] theorem mul_def {a b : ℤ} : int.mul a b = a * b := rfl
@[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
@[simp, norm_cast]
theorem coe_nat_le {m n : ℕ} : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := coe_nat_le_coe_nat_iff m n
@[simp, norm_cast]
theorem coe_nat_lt {m n : ℕ} : (↑m : ℤ) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n
@[simp, norm_cast]
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']
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 (nat.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)
@[simp, norm_cast] theorem coe_nat_abs (n : ℕ) : abs (n : ℤ) = n :=
abs_of_nonneg (coe_nat_nonneg 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_lt_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] },
{ convert hn _ n_ih using 1, simp [sub_eq_neg_add] } },
exact this (i + 1) }
end
protected def induction_on' {C : ℤ → Sort*} (z : ℤ) (b : ℤ) :
C b → (∀ k, b ≤ k → C k → C (k + 1)) → (∀ k ≤ b, C k → C (k - 1)) → C z :=
λ H0 Hs Hp,
begin
rw ←sub_add_cancel z b,
induction (z - b),
{ induction a with n ih, { rwa [of_nat_zero, zero_add] },
rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc],
exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih },
{ induction a with n ih,
{ rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub],
exact Hp _ (le_refl _) H0 },
{ rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub],
exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } }
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 : ∀ (a b : ℕ), nat_abs (sub_nat_nat a (nat.succ b)) ≤ nat.succ (a + b),
{ 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 only [(*), int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs]
@[simp] lemma nat_abs_mul_self' (a : ℤ) : (nat_abs a * nat_abs a : ℤ) = a * a :=
by rw [← int.coe_nat_mul, nat_abs_mul_self]
theorem neg_succ_of_nat_eq' (m : ℕ) : -[1+ m] = -m - 1 :=
by simp [neg_succ_of_nat_eq, sub_eq_neg_add]
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] lemma nat_abs_eq_zero {a : ℤ} : a.nat_abs = 0 ↔ a = 0 :=
⟨int.eq_zero_of_nat_abs_eq_zero, λ h, h.symm ▸ rfl⟩
lemma nat_abs_lt_nat_abs_of_nonneg_of_lt {a b : ℤ} (w₁ : 0 ≤ a) (w₂ : a < b) :
a.nat_abs < b.nat_abs :=
begin
lift b to ℕ using le_trans w₁ (le_of_lt w₂),
lift a to ℕ using w₁,
simpa using w₂,
end
/- / -/
@[simp] theorem of_nat_div (m n : ℕ) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl
@[simp, norm_cast] theorem coe_nat_div (m n : ℕ) : ((m / n : ℕ) : ℤ) = m / n := rfl
theorem neg_succ_of_nat_div (m : ℕ) {b : ℤ} (H : 0 < b) :
-[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 : 0 < b) : 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 : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a / b :=
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 : 0 ≤ a) (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 : 0 < b) : 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
-- Will be generalized to Euclidean domains.
protected theorem zero_div : ∀ (b : ℤ), 0 / b = 0
| 0 := rfl
| (n+1:ℕ) := rfl
| -[1+ n] := rfl
local attribute [simp] -- Will be generalized to Euclidean domains.
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 : ℤ}, 0 < c → (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 : 0 < b) :
-[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 _ _)
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem zero_mod (b : ℤ) : 0 % b = 0 :=
congr_arg of_nat $ nat.zero_mod _
local attribute [simp] -- Will be generalized to Euclidean domains.
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 _
local attribute [simp] -- Will be generalized to Euclidean domains.
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 → 0 ≤ a % b
| (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 : 0 < b) : 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]
lemma add_mod (a b n : ℤ) : (a + b) % n = ((a % n) + (b % n)) % n :=
by rw [add_mod_mod, mod_add_mod]
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]
lemma mul_mod (a b n : ℤ) : (a * b) % n = ((a % n) * (b % n)) % n :=
begin
conv_lhs {
rw [←mod_add_div a n, ←mod_add_div b n, right_distrib, left_distrib, left_distrib,
mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left,
mul_comm _ (n * (b / n)), mul_assoc, add_mul_mod_self_left] }
end
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem mod_self {a : ℤ} : a % a = 0 :=
by have := mul_mod_left 1 a; rwa one_mul at this
@[simp] theorem mod_mod_of_dvd (n : int) {m k : int} (h : m ∣ k) : n % k % m = n % m :=
begin
conv { to_rhs, rw ←mod_add_div n k },
rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left]
end
@[simp] theorem 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 : 0 < a) : 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 : 0 < b) :
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 : 0 < a) : 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 : 0 < b) : 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 : 0 ≤ a) : 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]
lemma mod_two_eq_zero_or_one (n : ℤ) : n % 2 = 0 ∨ n % 2 = 1 :=
have h : n % 2 < 2 := abs_of_nonneg (show 0 ≤ (2 : ℤ), from dec_trivial) ▸ int.mod_lt _ dec_trivial,
have h₁ : 0 ≤ n % 2 := int.mod_nonneg _ dec_trivial,
match (n % 2), h, h₁ with
| (0 : ℕ) := λ _ _, or.inl rfl
| (1 : ℕ) := λ _ _, or.inr rfl
| (k + 2 : ℕ) := λ h _, absurd h dec_trivial
| -[1+ a] := λ _ h₁, absurd h₁ dec_trivial
end
/- dvd -/
@[norm_cast] 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 : 0 ≤ a) (H2 : 0 ≤ b) : 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⟩
/-- If `a % b = c` then `b` divides `a - c`. -/
lemma dvd_sub_of_mod_eq {a b c : ℤ} (h : a % b = c) : b ∣ a - c :=
begin
have hx : a % b % b = c % b, { rw h },
rw [mod_mod, ←mod_sub_cancel_right c, sub_self, zero_mod] at hx,
exact dvd_of_mod_eq_zero hx
end
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]
lemma add_div_of_dvd {a b c : ℤ} :
c ∣ a → c ∣ b → (a + b) / c = a / c + b / c :=
begin
intros h1 h2,
by_cases h3 : c = 0,
{ rw [h3, zero_dvd_iff] at *,
rw [h1, h2, h3], refl },
{ apply eq_of_mul_eq_mul_right h3,
rw add_mul, repeat {rw [int.div_mul_cancel]};
try {apply dvd_add}; assumption }
end
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 : 0 < b) (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 : 0 ≤ a) (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 : 0 ≤ a) (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 : 0 ≤ b) (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_dvd_of_le_of_pow_dvd {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_dvd_of_le_of_pow_dvd 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_dvd_of_le_of_pow_dvd hmn,
apply int.coe_nat_dvd.1,
apply dvd_of_dvd_neg,
exact hdiv }
end
lemma dvd_of_pow_dvd {p k : ℕ} {m : ℤ} (hk : 1 ≤ k) (hpk : ↑(p^k) ∣ m) : ↑p ∣ m :=
by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk
/- / 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 : 0 < c) (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 : 0 < c) (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 : 0 < c) (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 : 0 < c) (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 : 0 < c) : 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 : 0 < c) (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 : 0 < c) (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 : 0 < c) (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 : 0 < c) : 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 : 0 ≤ b) (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 : 0 ≤ b) (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 : 0 < c) (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 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b :=
int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul)
theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (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) (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
/-- If an integer with larger absolute value divides an integer, it is
zero. -/
lemma eq_zero_of_dvd_of_nat_abs_lt_nat_abs {a b : ℤ} (w : a ∣ b) (h : nat_abs b < nat_abs a) :
b = 0 :=
begin
rw [←nat_abs_dvd, ←dvd_nat_abs, coe_nat_dvd] at w,
rw ←nat_abs_eq_zero,
exact eq_zero_of_dvd_of_lt w h
end
lemma eq_zero_of_dvd_of_nonneg_of_lt {a b : ℤ} (w₁ : 0 ≤ a) (w₂ : a < b) (h : b ∣ a) : a = 0 :=
eq_zero_of_dvd_of_nat_abs_lt_nat_abs h (nat_abs_lt_nat_abs_of_nonneg_of_lt w₁ w₂)
/-- If two integers are congruent to a sufficiently large modulus,
they are equal. -/
lemma eq_of_mod_eq_of_nat_abs_sub_lt_nat_abs {a b c : ℤ} (h1 : a % b = c)
(h2 : nat_abs (a - c) < nat_abs b) :
a = c :=
eq_of_sub_eq_zero (eq_zero_of_dvd_of_nat_abs_lt_nat_abs (dvd_sub_of_mod_eq h1) h2)
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 : n.succ ≤ m) : 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] lemma to_nat_sub_of_le (a b : ℤ) (h : b ≤ a) : (to_nat (a + -b) : ℤ) = a + - b :=
int.to_nat_of_nonneg (sub_nonneg_of_le 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 _)
@[simp] theorem lt_to_nat {n : ℕ} {a : ℤ} : n < to_nat a ↔ (n : ℤ) < a :=
le_iff_le_iff_lt_iff_lt.1 to_nat_le
theorem to_nat_le_to_nat {a b : ℤ} (h : a ≤ b) : to_nat a ≤ to_nat b :=
by rw to_nat_le; exact le_trans h (le_to_nat b)
theorem to_nat_lt_to_nat {a b : ℤ} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b :=
⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end,
λ h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩
theorem lt_of_to_nat_lt {a b : ℤ} (h : to_nat a < to_nat b) : a < b :=
(to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h
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, norm_cast] lemma bodd_coe (n : ℕ) : int.bodd n = nat.bodd n := 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;
simp; cases i.bodd; simp
@[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; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe]
@[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, -of_nat_eq_coe, bool.bxor_comm]
@[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, -of_nat_eq_coe, bool.bxor_comm]
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 -/
section classical
open_locale classical
theorem exists_least_of_bdd {P : ℤ → Prop}
(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}
(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⟩
end classical
/- cast (injection into groups with one) -/
@[simp, push_cast] theorem nat_cast_eq_coe_nat : ∀ n,
@coe ℕ ℤ (@coe_to_lift _ _ 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)
/-- Coercion `ℕ → ℤ` as a `ring_hom`. -/
def of_nat_hom : ℕ →+* ℤ := ⟨coe, rfl, int.of_nat_mul, rfl, int.of_nat_add⟩
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)
-- see Note [coercion into rings]
@[priority 900] instance cast_coe : has_coe_t ℤ α := ⟨int.cast⟩
@[simp, norm_cast] theorem cast_zero : ((0 : ℤ) : α) = 0 := rfl
theorem cast_of_nat (n : ℕ) : (of_nat n : α) = n := rfl
@[simp, norm_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n := rfl
theorem cast_coe_nat' (n : ℕ) :
(@coe ℕ ℤ (@coe_to_lift _ _ nat.cast_coe) n : α) = n :=
by simp
@[simp, norm_cast] theorem cast_neg_succ_of_nat (n : ℕ) : (-[1+ n] : α) = -(n + 1) := rfl
end
@[simp, norm_cast] theorem cast_one [add_monoid α] [has_one α] [has_neg α] :
((1 : ℤ) : α) = 1 := nat.cast_one
@[simp, norm_cast] 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, norm_cast] theorem cast_neg_of_nat [add_group α] [has_one α] :
∀ n, ((neg_of_nat n : ℤ) : α) = -n
| 0 := neg_zero.symm
| (n+1) := rfl
@[simp, norm_cast] 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),
begin
rw [← neg_add_rev, ← nat.cast_add_one, ← nat.cast_add_one, ← nat.cast_add],
apply congr_arg (λ x:ℕ, -(x:α)),
ac_refl
end
@[simp, norm_cast] theorem cast_neg [add_group α] [has_one α] : ∀ n, ((-n : ℤ) : α) = -n
| (n : ℕ) := cast_neg_of_nat _
| -[1+ n] := (neg_neg _).symm
@[simp, norm_cast] theorem cast_sub [add_group α] [has_one α] (m n) : ((m - n : ℤ) : α) = m - n :=
by simp [sub_eq_add_neg]
@[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, norm_cast] 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
theorem cast_ne_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) ≠ 0 ↔ n ≠ 0 :=
not_congr cast_eq_zero
@[simp, norm_cast] 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]
/-- `coe : ℤ → α` as an `add_monoid_hom`. -/
def cast_add_hom (α : Type*) [add_group α] [has_one α] : ℤ →+ α := ⟨coe, cast_zero, cast_add⟩
@[simp] lemma coe_cast_add_hom [add_group α] [has_one α] : ⇑(cast_add_hom α) = coe := rfl
/-- `coe : ℤ → α` as a `ring_hom`. -/
def cast_ring_hom (α : Type*) [ring α] : ℤ →+* α := ⟨coe, cast_one, cast_mul, cast_zero, cast_add⟩
@[simp] lemma coe_cast_ring_hom [ring α] : ⇑(cast_ring_hom α) = coe := rfl
lemma cast_commute [ring α] (m : ℤ) (x : α) : commute ↑m x :=
int.cases_on m (λ n, n.cast_commute x) (λ n, ((n+1).cast_commute x).neg_left)
lemma commute_cast [ring α] (x : α) (m : ℤ) : commute x m :=
(m.cast_commute x).symm
@[simp, norm_cast]
theorem coe_nat_bit0 (n : ℕ) : (↑(bit0 n) : ℤ) = bit0 ↑n := by {unfold bit0, simp}
@[simp, norm_cast]
theorem coe_nat_bit1 (n : ℕ) : (↑(bit1 n) : ℤ) = bit1 ↑n := by {unfold bit1, unfold bit0, simp}
@[simp, norm_cast] theorem cast_bit0 [ring α] (n : ℤ) : ((bit0 n : ℤ) : α) = bit0 n := cast_add _ _
@[simp, norm_cast] theorem cast_bit1 [ring α] (n : ℤ) : ((bit1 n : ℤ) : α) = bit1 n :=
by rw [bit1, cast_add, cast_one, cast_bit0]; refl
lemma cast_two [ring α] : ((2 : ℤ) : α) = 2 := by simp
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, norm_cast] theorem cast_le [linear_ordered_ring α] {m n : ℤ} : (m : α) ≤ n ↔ m ≤ n :=
by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg]
@[simp, norm_cast] 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]
@[simp, norm_cast] 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, norm_cast] 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, norm_cast] theorem cast_abs [decidable_linear_ordered_comm_ring α] {q : ℤ} :
((abs q : ℤ) : α) = abs q :=
by simp [abs]
end cast
section decidable
/-- List enumerating `[m, n)`. -/
def range (m n : ℤ) : list ℤ :=
(list.range (to_nat (n-m))).map $ λ r, m+r
theorem mem_range_iff {m n r : ℤ} : r ∈ range m n ↔ m ≤ r ∧ r < n :=
⟨λ H, let ⟨s, h1, h2⟩ := list.mem_map.1 H in h2 ▸
⟨le_add_of_nonneg_right trivial,
add_lt_of_lt_sub_left $ match n-m, h1 with
| (k:ℕ), h1 := by rwa [list.mem_range, to_nat_coe_nat, ← coe_nat_lt] at h1
end⟩,
λ ⟨h1, h2⟩, list.mem_map.2 ⟨to_nat (r-m),
list.mem_range.2 $ by rw [← coe_nat_lt, to_nat_of_nonneg (sub_nonneg_of_le h1),
to_nat_of_nonneg (sub_nonneg_of_le (le_of_lt (lt_of_le_of_lt h1 h2)))];
exact sub_lt_sub_right h2 _,
show m + _ = _, by rw [to_nat_of_nonneg (sub_nonneg_of_le h1), add_sub_cancel'_right]⟩⟩
instance decidable_le_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) :
decidable (∀ r, m ≤ r → r < n → P r) :=
decidable_of_iff (∀ r ∈ range m n, P r) $ by simp only [mem_range_iff, and_imp]
instance decidable_le_le (P : int → Prop) [decidable_pred P] (m n : ℤ) :
decidable (∀ r, m ≤ r → r ≤ n → P r) :=
decidable_of_iff (∀ r ∈ range m (n+1), P r) $ by simp only [mem_range_iff, and_imp, lt_add_one_iff]
instance decidable_lt_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) :
decidable (∀ r, m < r → r < n → P r) :=
int.decidable_le_lt P _ _
instance decidable_lt_le (P : int → Prop) [decidable_pred P] (m n : ℤ) :
decidable (∀ r, m < r → r ≤ n → P r) :=
int.decidable_le_le P _ _
end decidable
end int
open int
namespace add_monoid_hom
variables {A : Type*}
/-- Two additive monoid homomorphisms `f`, `g` from `ℤ` to an additive monoid are equal
if `f 1 = g 1`. -/
@[ext] theorem ext_int [add_monoid A] {f g : ℤ →+ A} (h1 : f 1 = g 1) : f = g :=
have f.comp (int.of_nat_hom : ℕ →+ ℤ) = g.comp (int.of_nat_hom : ℕ →+ ℤ) := ext_nat h1,
have ∀ n : ℕ, f n = g n := ext_iff.1 this,
ext $ λ n, int.cases_on n this $ λ n, eq_on_neg (this $ n + 1)
variables [add_group A] [has_one A]
theorem eq_int_cast_hom (f : ℤ →+ A) (h1 : f 1 = 1) : f = int.cast_add_hom A :=
ext_int $ by simp [h1]
theorem eq_int_cast (f : ℤ →+ A) (h1 : f 1 = 1) : ∀ n : ℤ, f n = n :=
ext_iff.1 (f.eq_int_cast_hom h1)
end add_monoid_hom
namespace ring_hom
variables {α : Type*} {β : Type*} [ring α] [ring β]
@[simp] lemma eq_int_cast (f : ℤ →+* α) (n : ℤ) : f n = n :=
f.to_add_monoid_hom.eq_int_cast f.map_one n
lemma eq_int_cast' (f : ℤ →+* α) : f = int.cast_ring_hom α :=
ring_hom.ext f.eq_int_cast
@[simp] lemma map_int_cast (f : α →+* β) (n : ℤ) : f n = n :=
(f.comp (int.cast_ring_hom α)).eq_int_cast n
lemma ext_int {R : Type*} [semiring R] (f g : ℤ →+* R) : f = g :=
coe_add_monoid_hom_inj $ add_monoid_hom.ext_int $ f.map_one.trans g.map_one.symm
instance int.subsingleton_ring_hom {R : Type*} [semiring R] : subsingleton (ℤ →+* R) :=
⟨ring_hom.ext_int⟩
end ring_hom
@[simp, norm_cast] theorem int.cast_id (n : ℤ) : ↑n = n :=
((ring_hom.id ℤ).eq_int_cast n).symm
|
0a2d1c50037ea4646c2509fc6aada49bb266bb5c | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/products/default.lean | 816bf1996a34813a6ab43ca4a760f6c1dd364cf6 | [
"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 | 85 | lean | import category_theory.products.bifunctor
import category_theory.products.associator
|
ab807a9996e3f4205be3e8c37a8613d5a922f27b | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/tactic/tfae.lean | 0fac3413de11846635e834d6c1592a479d7a51fd | [
"Apache-2.0"
] | permissive | ChrisHughes24/mathlib | 98322577c460bc6b1fe5c21f42ce33ad1c3e5558 | a2a867e827c2a6702beb9efc2b9282bd801d5f9a | refs/heads/master | 1,583,848,251,477 | 1,565,164,247,000 | 1,565,164,247,000 | 129,409,993 | 0 | 1 | Apache-2.0 | 1,565,164,817,000 | 1,523,628,059,000 | Lean | UTF-8 | Lean | false | false | 3,058 | lean | /-
Copyright (c) 2018 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Reid Barton, Simon Hudon
"The Following Are Equivalent" (tfae) :
Tactic for proving the equivalence of a set of proposition
using various implications between them.
-/
import tactic.interactive data.list.basic
import tactic.scc
open expr tactic lean lean.parser
namespace tactic
open interactive interactive.types expr
export list (tfae)
namespace tfae
@[derive has_reflect] inductive arrow : Type
| right : arrow
| left_right : arrow
| left : arrow
meta def mk_implication : Π (re : arrow) (e₁ e₂ : expr), pexpr
| arrow.right e₁ e₂ := ``(%%e₁ → %%e₂)
| arrow.left_right e₁ e₂ := ``(%%e₁ ↔ %%e₂)
| arrow.left e₁ e₂ := ``(%%e₂ → %%e₁)
meta def mk_name : Π (re : arrow) (i₁ i₂ : nat), name
| arrow.right i₁ i₂ := ("tfae_" ++ to_string i₁ ++ "_to_" ++ to_string i₂ : string)
| arrow.left_right i₁ i₂ := ("tfae_" ++ to_string i₁ ++ "_iff_" ++ to_string i₂ : string)
| arrow.left i₁ i₂ := ("tfae_" ++ to_string i₂ ++ "_to_" ++ to_string i₁ : string)
end tfae
namespace interactive
open tactic.tfae list
meta def parse_list : expr → option (list expr)
| `([]) := pure []
| `(%%e :: %%es) := (::) e <$> parse_list es
| _ := none
/-- In a goal of the form `tfae [a₀, a₁, a₂]`,
`tfae_have : i → j` creates the assertion `aᵢ → aⱼ`. The other possible
notations are `tfae_have : i ← j` and `tfae_have : i ↔ j`. The user can
also provide a label for the assertion, as with `have`: `tfae_have h : i ↔ j`.
-/
meta def tfae_have
(h : parse $ optional ident <* tk ":")
(i₁ : parse (with_desc "i" small_nat))
(re : parse (((tk "→" <|> tk "->") *> return arrow.right) <|>
((tk "↔" <|> tk "<->") *> return arrow.left_right) <|>
((tk "←" <|> tk "<-") *> return arrow.left)))
(i₂ : parse (with_desc "j" small_nat))
(discharger : tactic unit := tactic.solve_by_elim) :
tactic unit := do
`(tfae %%l) <- target,
l ← parse_list l,
e₁ ← list.nth l (i₁ - 1) <|> fail format!"index {i₁} is not between 1 and {l.length}",
e₂ ← list.nth l (i₂ - 1) <|> fail format!"index {i₂} is not between 1 and {l.length}",
type ← to_expr (tfae.mk_implication re e₁ e₂),
let h := h.get_or_else (mk_name re i₁ i₂),
tactic.assert h type,
return ()
/-- Finds all implications and equivalences in the context
to prove a goal of the form `tfae [...]`. -/
meta def tfae_finish : tactic unit :=
applyc ``tfae_nil <|>
closure.mk_closure (λ cl,
do impl_graph.mk_scc cl,
`(tfae %%l) ← target,
l ← parse_list l,
(r,_) ← cl.root l.head,
refine ``(tfae_of_forall %%r _ _),
thm ← mk_const ``forall_mem_cons,
l.mmap' (λ e,
do rewrite_target thm, split,
(r',p) ← cl.root e,
tactic.exact p ),
applyc ``forall_mem_nil,
pure ())
end interactive
end tactic
|
3c8ccc31ba1c1b7cddebcb9072eb94ce4e52f0ed | 5df84495ec6c281df6d26411cc20aac5c941e745 | /src/formal_ml/exp_bound.lean | d33d7f3180a15bdcfc7a9dc60ef9ad4dafa51e61 | [
"Apache-2.0"
] | permissive | eric-wieser/formal-ml | e278df5a8df78aa3947bc8376650419e1b2b0a14 | 630011d19fdd9539c8d6493a69fe70af5d193590 | refs/heads/master | 1,681,491,589,256 | 1,612,642,743,000 | 1,612,642,743,000 | 360,114,136 | 0 | 0 | Apache-2.0 | 1,618,998,189,000 | 1,618,998,188,000 | null | UTF-8 | Lean | false | false | 3,529 | lean | /-
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
import analysis.calculus.mean_value
import data.complex.exponential
import formal_ml.convex_optimization
--import formal_ml.analytic_function
import analysis.special_functions.exp_log
import formal_ml.nnreal
/-
Note: after I proved this, I found that it already was in mathlib.
-/
--TODO: lift this until it doesn't depend upon has_derivative
lemma has_derivative_exp:has_derivative real.exp real.exp :=
begin
unfold has_derivative,
intro x,
apply real.has_deriv_at_exp,
end
lemma exp_bound3 (x:real):0 ≤ real.exp x - (x + 1) :=
begin
let f:=λ x,real.exp x - (x + 1),
let f':=λ x,real.exp x - 1,
let f'':=λ x,real.exp x,
begin
have f_def:f=λ x,real.exp x - (x + 1),
{
refl,
},
have f_def':f'=λ x,real.exp x - 1,
{
refl,
},
have f_def'':f''=λ x,real.exp x,
{
refl,
},
have A1:has_derivative f f',
{
rw f_def,
rw f_def',
apply has_derivative_sub real.exp real.exp (λ x, (x + 1)) (1),
{
apply has_derivative_exp,
},
{
apply has_derivative_add_const,
apply has_derivative_id,
},
},
have A2:has_derivative f' f'',
{
apply has_derivative_add_const,
apply has_derivative_exp,
},
have A3:∀ y, f 0 ≤ f y,
{
intro y,
apply is_minimum,
{
apply A1,
},
{
apply A2,
},
{
intro z,
rw f_def'',
apply le_of_lt,
apply real.exp_pos,
},
have A3D:f' 0 = real.exp 0 - 1,
{
refl,
},
rw A3D,
simp,
},
have A4:f 0 = 0,
{
rw f_def,
simp,
},
rw A4 at A3,
have A5:f x = real.exp x - (x + 1),
{
refl,
},
rw ← A5,
apply A3,
end
end
lemma exp_bound2 (x:real):1 - x ≤ real.exp(-x) :=
begin
have A1:0 ≤ real.exp(-x) - ((-x) + 1),
{
apply exp_bound3,
},
have A2:((-x) + 1) ≤ real.exp(-x) := le_of_sub_nonneg A1,
rw add_comm at A2,
rw sub_eq_add_neg,
exact A2,
end
--TODO: verify this is exactly what we need first.
lemma nnreal_exp_bound (x:nnreal):(1-x) ≤ nnreal.exp(-x) :=
begin
apply nnreal.coe_le_coe.mp,
rw nnreal_exp_eq,
have A1:((x:real) ≤ 1) ∨ (1 ≤ (x:real)),
{
apply linear_order.le_total,
},
cases A1,
{
rw nnreal.coe_sub,
apply exp_bound2 (↑x),
apply A1,
},
{
rw nnreal.sub_eq_zero,
{
apply le_of_lt,
apply real.exp_pos,
},
{
apply A1,
}
}
end
--This exact theorem is used for PAC bounds.
lemma nnreal_exp_bound2 (x:nnreal) (k:ℕ):(1 - x)^k ≤ nnreal.exp(-x * k) :=
begin
have A1:(1-x) ≤ nnreal.exp(-x),
{
apply nnreal_exp_bound,
},
have A2:(1 - x)^k ≤ (nnreal.exp(-x))^k,
{
apply nnreal_pow_mono,
exact A1,
},
have A3:nnreal.exp(-x)^k=nnreal.exp(-x * k),
{
symmetry,
rw mul_comm,
apply nnreal_exp_pow,
},
rw ← A3,
apply A2,
end
|
77e801423981dcef60c082f22caf4f66f1eaa751 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Lean/Elab/AutoBound.lean | 7331ded00a4dbf6af6e38308d95e95ae0f6f5baa | [
"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 | 1,891 | 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.Data.Options
/- Basic support for auto bound implicit local names -/
namespace Lean.Elab
register_builtin_option autoBoundImplicitLocal : Bool := {
defValue := true
descr := "Unbound local variables in declaration headers become implicit arguments if they are a lower case or greek letter followed by numeric digits. For example, `def f (x : Vector α n) : Vector α n :=` automatically introduces the implicit variables {α n}."
}
private def isValidAutoBoundSuffix (s : String) : Bool :=
s.toSubstring.drop 1 |>.all fun c => c.isDigit || isSubScriptAlnum c || c == '_' || c == '\''
/-
Remark: Issue #255 exposed a nasty interaction between macro scopes and auto-bound-implicit names.
```
local notation "A" => id x
theorem test : A = A := sorry
```
We used to use `n.eraseMacroScopes` at `isValidAutoBoundImplicitName` and `isValidAutoBoundLevelName`.
Thus, in the example above, when `A` is expanded, a `x` with a fresh macro scope is created.
`x`+macros-scope is not in scope and is a valid auto-bound implicit name after macro scopes are erased.
So, an auto-bound exception would be thrown, and `x`+macro-scope would be added as a new implicit.
When, we try again, a `x` with a new macro scope is created and this process keeps repeating.
Therefore, we do consider identifier with macro scopes anymore.
-/
def isValidAutoBoundImplicitName (n : Name) : Bool :=
match n with
| Name.str Name.anonymous s _ => s.length > 0 && (isGreek s[0] || s[0].isLower) && isValidAutoBoundSuffix s
| _ => false
def isValidAutoBoundLevelName (n : Name) : Bool :=
match n with
| Name.str Name.anonymous s _ => s.length > 0 && s[0].isLower && isValidAutoBoundSuffix s
| _ => false
end Lean.Elab
|
4df86ff01b258815a170f8248cfdf421929be23f | 4b846d8dabdc64e7ea03552bad8f7fa74763fc67 | /tests/lean/run/1331.lean | 217dbc81dba119f0bcfda78ebdfd421ff7de021c | [
"Apache-2.0"
] | permissive | pacchiano/lean | 9324b33f3ac3b5c5647285160f9f6ea8d0d767dc | fdadada3a970377a6df8afcd629a6f2eab6e84e8 | refs/heads/master | 1,611,357,380,399 | 1,489,870,101,000 | 1,489,870,101,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 485 | lean | def is_space : char → Prop
| #" " := true
| #"\x09" := true -- \t
| #"\n" := true
| #"\x0d" := true -- \r
| _ := false
instance is_space.decidable_pred : decidable_pred is_space :=
begin delta is_space, apply_instance end
def f (a : nat) : nat :=
a + 2
open tactic
meta def check_target (p : pexpr) : tactic unit :=
do t ← target, e ← to_expr p, guard (expr.alpha_eqv t e)
lemma flemma : f 0 = 2 :=
begin
delta f,
check_target `(0 + 2 = 2),
reflexivity
end
|
5b0e4c459c6ac9ce4ad1e0322d579c22a80dbc28 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/big_operators/norm_num.lean | a4067b58b34251ffc263e4e3a9423d6042f085b2 | [
"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 | 16,278 | lean | /-
Copyright (c) 2022 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import algebra.big_operators.basic
import data.int.interval
import tactic.norm_num
/-! ### `norm_num` plugin for big operators
This `norm_num` plugin provides support for computing sums and products of
lists, multisets and finsets.
Example goals this plugin can solve:
* `∑ i in finset.range 10, (i^2 : ℕ) = 285`
* `Π i in {1, 4, 9, 16}, nat.sqrt i = 24`
* `([1, 2, 1, 3]).sum = 7`
## Implementation notes
The tactic works by first converting the expression denoting the collection
(list, multiset, finset) to a list of expressions denoting each element. For
finsets, this involves erasing duplicate elements (the tactic fails if equality
or disequality cannot be determined).
After rewriting the big operator to a product/sum of lists, we evaluate this
using `norm_num` itself to handle multiplication/addition.
Finally, we package up the result using some congruence lemmas.
-/
open tactic
namespace tactic.norm_num
/-- Use `norm_num` to decide equality between two expressions.
If the decision procedure succeeds, the `bool` value indicates whether the expressions are equal,
and the `expr` is a proof of (dis)equality.
This procedure is partial: it will fail in cases where `norm_num` can't reduce either side
to a rational numeral.
-/
meta def decide_eq (l r : expr) : tactic (bool × expr) := do
(l', l'_pf) ← or_refl_conv norm_num.derive l,
(r', r'_pf) ← or_refl_conv norm_num.derive r,
n₁ ← l'.to_rat, n₂ ← r'.to_rat,
c ← infer_type l' >>= mk_instance_cache,
if n₁ = n₂ then do
pf ← i_to_expr ``(eq.trans %%l'_pf $ eq.symm %%r'_pf),
pure (tt, pf)
else do
(_, p) ← norm_num.prove_ne c l' r' n₁ n₂,
pure (ff, p)
lemma list.not_mem_cons {α : Type*} {x y : α} {ys : list α} (h₁ : x ≠ y) (h₂ : x ∉ ys) :
x ∉ y :: ys :=
λ h, ((list.mem_cons_iff _ _ _).mp h).elim h₁ h₂
/-- Use a decision procedure for the equality of list elements to decide list membership.
If the decision procedure succeeds, the `bool` value indicates whether the expressions are equal,
and the `expr` is a proof of (dis)equality.
This procedure is partial iff its parameter `decide_eq` is partial.
-/
meta def list.decide_mem (decide_eq : expr → expr → tactic (bool × expr)) :
expr → list expr → tactic (bool × expr)
| x [] := do
pf ← i_to_expr ``(list.not_mem_nil %%x),
pure (ff, pf)
| x (y :: ys) := do
(is_head, head_pf) ← decide_eq x y,
if is_head then do
pf ← i_to_expr ``((list.mem_cons_iff %%x %%y _).mpr (or.inl %%head_pf)),
pure (tt, pf)
else do
(mem_tail, tail_pf) ← list.decide_mem x ys,
if mem_tail then do
pf ← i_to_expr ``((list.mem_cons_iff %%x %%y _).mpr (or.inr %%tail_pf)),
pure (tt, pf)
else do
pf ← i_to_expr ``(list.not_mem_cons %%head_pf %%tail_pf),
pure (ff, pf)
lemma list.map_cons_congr {α β : Type*} (f : α → β) {x : α} {xs : list α} {fx : β} {fxs : list β}
(h₁ : f x = fx) (h₂ : xs.map f = fxs) : (x :: xs).map f = fx :: fxs :=
by rw [list.map_cons, h₁, h₂]
/-- Apply `ef : α → β` to all elements of the list, constructing an equality proof. -/
meta def eval_list_map (ef : expr) : list expr → tactic (list expr × expr)
| [] := do
eq ← i_to_expr ``(list.map_nil %%ef),
pure ([], eq)
| (x :: xs) := do
(fx, fx_eq) ← or_refl_conv norm_num.derive (expr.app ef x),
(fxs, fxs_eq) ← eval_list_map xs,
eq ← i_to_expr ``(list.map_cons_congr %%ef %%fx_eq %%fxs_eq),
pure (fx :: fxs, eq)
lemma list.cons_congr {α : Type*} (x : α) {xs : list α} {xs' : list α} (xs_eq : xs' = xs) :
x :: xs' = x :: xs :=
by rw xs_eq
lemma list.map_congr {α β : Type*} (f : α → β) {xs xs' : list α}
{ys : list β} (xs_eq : xs = xs') (ys_eq : xs'.map f = ys) :
xs.map f = ys :=
by rw [← ys_eq, xs_eq]
/-- Convert an expression denoting a list to a list of elements. -/
meta def eval_list : expr → tactic (list expr × expr)
| e@`(list.nil) := do
eq ← mk_eq_refl e,
pure ([], eq)
| e@`(list.cons %%x %%xs) := do
(xs, xs_eq) ← eval_list xs,
eq ← i_to_expr ``(list.cons_congr %%x %%xs_eq),
pure (x :: xs, eq)
| e@`(list.range %%en) := do
n ← expr.to_nat en,
eis ← (list.range n).mmap (λ i, expr.of_nat `(ℕ) i),
eq ← mk_eq_refl e,
pure (eis, eq)
| `(@list.map %%α %%β %%ef %%exs) := do
(xs, xs_eq) ← eval_list exs,
(ys, ys_eq) ← eval_list_map ef xs,
eq ← i_to_expr ``(list.map_congr %%ef %%xs_eq %%ys_eq),
pure (ys, eq)
| e@`(@list.fin_range %%en) := do
n ← expr.to_nat en,
eis ← (list.fin_range n).mmap (λ i, expr.of_nat `(fin %%en) i),
eq ← mk_eq_refl e,
pure (eis, eq)
| e := fail (to_fmt "Unknown list expression" ++ format.line ++ to_fmt e)
lemma multiset.cons_congr {α : Type*} (x : α) {xs : multiset α} {xs' : list α}
(xs_eq : (xs' : multiset α) = xs) : (list.cons x xs' : multiset α) = x ::ₘ xs :=
by rw [← xs_eq]; refl
lemma multiset.map_congr {α β : Type*} (f : α → β) {xs : multiset α}
{xs' : list α} {ys : list β} (xs_eq : xs = (xs' : multiset α)) (ys_eq : xs'.map f = ys) :
xs.map f = (ys : multiset β) :=
by rw [← ys_eq, ← multiset.coe_map, xs_eq]
/-- Convert an expression denoting a multiset to a list of elements.
We return a list rather than a finset, so we can more easily iterate over it
(without having to prove that our tactics are independent of the order of iteration,
which is in general not true).
-/
meta def eval_multiset : expr → tactic (list expr × expr)
| e@`(@has_zero.zero (multiset _) _) := do
eq ← mk_eq_refl e,
pure ([], eq)
| e@`(has_emptyc.emptyc) := do
eq ← mk_eq_refl e,
pure ([], eq)
| e@`(has_singleton.singleton %%x) := do
eq ← mk_eq_refl e,
pure ([x], eq)
| e@`(multiset.cons %%x %%xs) := do
(xs, xs_eq) ← eval_multiset xs,
eq ← i_to_expr ``(multiset.cons_congr %%x %%xs_eq),
pure (x :: xs, eq)
| e@`(@@has_insert.insert multiset.has_insert %%x %%xs) := do
(xs, xs_eq) ← eval_multiset xs,
eq ← i_to_expr ``(multiset.cons_congr %%x %%xs_eq),
pure (x :: xs, eq)
| e@`(multiset.range %%en) := do
n ← expr.to_nat en,
eis ← (list.range n).mmap (λ i, expr.of_nat `(ℕ) i),
eq ← mk_eq_refl e,
pure (eis, eq)
| `(@@coe (@@coe_to_lift (@@coe_base (multiset.has_coe))) %%exs) := do
(xs, xs_eq) ← eval_list exs,
eq ← i_to_expr ``(congr_arg coe %%xs_eq),
pure (xs, eq)
| `(@multiset.map %%α %%β %%ef %%exs) := do
(xs, xs_eq) ← eval_multiset exs,
(ys, ys_eq) ← eval_list_map ef xs,
eq ← i_to_expr ``(multiset.map_congr %%ef %%xs_eq %%ys_eq),
pure (ys, eq)
| e := fail (to_fmt "Unknown multiset expression" ++ format.line ++ to_fmt e)
lemma finset.mk_congr {α : Type*} {xs xs' : multiset α} (h : xs = xs') (nd nd') :
finset.mk xs nd = finset.mk xs' nd' :=
by congr; assumption
lemma finset.insert_eq_coe_list_of_mem {α : Type*} [decidable_eq α] (x : α) (xs : finset α)
{xs' : list α} (h : x ∈ xs') (nd_xs : xs'.nodup)
(hxs' : xs = finset.mk ↑xs' (multiset.coe_nodup.mpr nd_xs)) :
insert x xs = finset.mk ↑xs' (multiset.coe_nodup.mpr nd_xs) :=
have h : x ∈ xs, by simpa [hxs'] using h,
by rw [finset.insert_eq_of_mem h, hxs']
lemma finset.insert_eq_coe_list_cons {α : Type*} [decidable_eq α] (x : α) (xs : finset α)
{xs' : list α} (h : x ∉ xs') (nd_xs : xs'.nodup) (nd_xxs : (x :: xs').nodup)
(hxs' : xs = finset.mk ↑xs' (multiset.coe_nodup.mpr nd_xs)) :
insert x xs = finset.mk ↑(x :: xs') (multiset.coe_nodup.mpr nd_xxs) :=
have h : x ∉ xs, by simpa [hxs'] using h,
by { rw [← finset.val_inj, finset.insert_val_of_not_mem h, hxs'], simp only [multiset.cons_coe] }
/-- For now this only works on types that are contiguous subsets of the integers -/
meta def eval_finset_interval :
expr → tactic (option (list expr × expr × expr))
| e@`(@finset.Icc %%α %%inst_1 %%inst_2 %%ea %%eb) := do
a ← expr.to_int ea,
b ← expr.to_int eb,
eis ← (finset.Icc a b).val.unquot.mmap (λ i, expr.of_int α i),
eq ← mk_eq_refl e,
nd ← i_to_expr ``(finset.nodup %%e),
pure (eis, eq, nd)
| e@`(@finset.Ico %%α %%inst_1 %%inst_2 %%ea %%eb) := do
a ← expr.to_int ea,
b ← expr.to_int eb,
eis ← (finset.Ico a b).val.unquot.mmap (λ i, expr.of_int α i),
eq ← mk_eq_refl e,
nd ← i_to_expr ``(finset.nodup %%e),
pure (eis, eq, nd)
| e@`(@finset.Ioc %%α %%inst_1 %%inst_2 %%ea %%eb) := do
a ← expr.to_int ea,
b ← expr.to_int eb,
eis ← (finset.Ioc a b).val.unquot.mmap (λ i, expr.of_int α i),
eq ← mk_eq_refl e,
nd ← i_to_expr ``(finset.nodup %%e),
pure (eis, eq, nd)
| e@`(@finset.Ioo %%α %%inst_1 %%inst_2 %%ea %%eb) := do
a ← expr.to_int ea,
b ← expr.to_int eb,
eis ← (finset.Ioo a b).val.unquot.mmap (λ i, expr.of_int α i),
eq ← mk_eq_refl e,
nd ← i_to_expr ``(finset.nodup %%e),
pure (eis, eq, nd)
| _ := pure none
/-- Convert an expression denoting a finset to a list of elements,
a proof that this list is equal to the original finset,
and a proof that the list contains no duplicates.
We return a list rather than a finset, so we can more easily iterate over it
(without having to prove that our tactics are independent of the order of iteration,
which is in general not true).
`decide_eq` is a (partial) decision procedure for determining whether two
elements of the finset are equal, for example to parse `{2, 1, 2}` into `[2, 1]`.
-/
meta def eval_finset (decide_eq : expr → expr → tactic (bool × expr)) :
expr → tactic (list expr × expr × expr)
| e@`(finset.mk %%val %%nd) := do
(val', eq) ← eval_multiset val,
eq' ← i_to_expr ``(finset.mk_congr %%eq _ _),
pure (val', eq', nd)
| e@`(has_emptyc.emptyc) := do
eq ← mk_eq_refl e,
nd ← i_to_expr ``(list.nodup_nil),
pure ([], eq, nd)
| e@`(has_singleton.singleton %%x) := do
eq ← mk_eq_refl e,
nd ← i_to_expr ``(list.nodup_singleton %%x),
pure ([x], eq, nd)
| `(@@has_insert.insert (@@finset.has_insert %%dec) %%x %%xs) := do
(exs, xs_eq, xs_nd) ← eval_finset xs,
(is_mem, mem_pf) ← list.decide_mem decide_eq x exs,
if is_mem then do
pf ← i_to_expr ``(finset.insert_eq_coe_list_of_mem %%x %%xs %%mem_pf %%xs_nd %%xs_eq),
pure (exs, pf, xs_nd)
else do
nd ← i_to_expr ``(list.nodup_cons.mpr ⟨%%mem_pf, %%xs_nd⟩),
pf ← i_to_expr ``(finset.insert_eq_coe_list_cons %%x %%xs %%mem_pf %%xs_nd %%nd %%xs_eq),
pure (x :: exs, pf, nd)
| `(@@finset.univ %%ft) := do
-- Convert the fintype instance expression `ft` to a list of its elements.
-- Unfold it to the `fintype.mk` constructor and a list of arguments.
`fintype.mk ← get_app_fn_const_whnf ft
| fail (to_fmt "Unknown fintype expression" ++ format.line ++ to_fmt ft),
[_, args, _] ← get_app_args_whnf ft | fail (to_fmt "Expected 3 arguments to `fintype.mk`"),
eval_finset args
| e@`(finset.range %%en) := do
n ← expr.to_nat en,
eis ← (list.range n).mmap (λ i, expr.of_nat `(ℕ) i),
eq ← mk_eq_refl e,
nd ← i_to_expr ``(list.nodup_range %%en),
pure (eis, eq, nd)
| e := do
-- try some other parsers
some v ← eval_finset_interval e |
fail (to_fmt "Unknown finset expression" ++ format.line ++ to_fmt e),
pure v
@[to_additive]
lemma list.prod_cons_congr {α : Type*} [monoid α] (xs : list α) (x y z : α)
(his : xs.prod = y) (hi : x * y = z) : (x :: xs).prod = z :=
by rw [list.prod_cons, his, hi]
/-- Evaluate `list.prod %%xs`,
producing the evaluated expression and an equality proof. -/
meta def list.prove_prod (α : expr) : list expr → tactic (expr × expr)
| [] := do
result ← expr.of_nat α 1,
proof ← i_to_expr ``(@list.prod_nil %%α _),
pure (result, proof)
| (x :: xs) := do
eval_xs ← list.prove_prod xs,
xxs ← i_to_expr ``(%%x * %%eval_xs.1),
eval_xxs ← or_refl_conv norm_num.derive xxs,
exs ← expr.of_list α xs,
proof ← i_to_expr
``(list.prod_cons_congr %%exs%%x %%eval_xs.1 %%eval_xxs.1 %%eval_xs.2 %%eval_xxs.2),
pure (eval_xxs.1, proof)
/-- Evaluate `list.sum %%xs`,
sumucing the evaluated expression and an equality proof. -/
meta def list.prove_sum (α : expr) : list expr → tactic (expr × expr)
| [] := do
result ← expr.of_nat α 0,
proof ← i_to_expr ``(@list.sum_nil %%α _),
pure (result, proof)
| (x :: xs) := do
eval_xs ← list.prove_sum xs,
xxs ← i_to_expr ``(%%x + %%eval_xs.1),
eval_xxs ← or_refl_conv norm_num.derive xxs,
exs ← expr.of_list α xs,
proof ← i_to_expr
``(list.sum_cons_congr %%exs%%x %%eval_xs.1 %%eval_xxs.1 %%eval_xs.2 %%eval_xxs.2),
pure (eval_xxs.1, proof)
@[to_additive] lemma list.prod_congr {α : Type*} [monoid α] {xs xs' : list α} {z : α}
(h₁ : xs = xs') (h₂ : xs'.prod = z) : xs.prod = z := by cc
@[to_additive] lemma multiset.prod_congr {α : Type*} [comm_monoid α]
{xs : multiset α} {xs' : list α} {z : α}
(h₁ : xs = (xs' : multiset α)) (h₂ : xs'.prod = z) : xs.prod = z :=
by rw [← h₂, ← multiset.coe_prod, h₁]
/-- Evaluate `(%%xs.map (%%ef : %%α → %%β)).prod`,
producing the evaluated expression and an equality proof. -/
meta def list.prove_prod_map (β ef : expr) (xs : list expr) : tactic (expr × expr) := do
(fxs, fxs_eq) ← eval_list_map ef xs,
(prod, prod_eq) ← list.prove_prod β fxs,
eq ← i_to_expr ``(list.prod_congr %%fxs_eq %%prod_eq),
pure (prod, eq)
/-- Evaluate `(%%xs.map (%%ef : %%α → %%β)).sum`,
producing the evaluated expression and an equality proof. -/
meta def list.prove_sum_map (β ef : expr) (xs : list expr) : tactic (expr × expr) := do
(fxs, fxs_eq) ← eval_list_map ef xs,
(sum, sum_eq) ← list.prove_sum β fxs,
eq ← i_to_expr ``(list.sum_congr %%fxs_eq %%sum_eq),
pure (sum, eq)
@[to_additive]
lemma finset.eval_prod_of_list {β α : Type*} [comm_monoid β]
(s : finset α) (f : α → β) {is : list α} (his : is.nodup)
(hs : finset.mk ↑is (multiset.coe_nodup.mpr his) = s)
{x : β} (hx : (is.map f).prod = x) :
s.prod f = x :=
by rw [← hs, finset.prod_mk, multiset.coe_map, multiset.coe_prod, hx]
/-- `norm_num` plugin for evaluating big operators:
* `list.prod`
* `list.sum`
* `multiset.prod`
* `multiset.sum`
* `finset.prod`
* `finset.sum`
-/
@[norm_num] meta def eval_big_operators : expr → tactic (expr × expr)
| `(@list.prod %%α %%inst1 %%inst2 %%exs) :=
tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do
(xs, list_eq) ← eval_list exs,
(result, sum_eq) ← list.prove_prod α xs,
pf ← i_to_expr ``(list.prod_congr %%list_eq %%sum_eq),
pure (result, pf)
| `(@list.sum %%α %%inst1 %%inst2 %%exs) :=
tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do
(xs, list_eq) ← eval_list exs,
(result, sum_eq) ← list.prove_sum α xs,
pf ← i_to_expr ``(list.sum_congr %%list_eq %%sum_eq),
pure (result, pf)
| `(@multiset.prod %%α %%inst %%exs) :=
tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do
(xs, list_eq) ← eval_multiset exs,
(result, sum_eq) ← list.prove_prod α xs,
pf ← i_to_expr ``(multiset.prod_congr %%list_eq %%sum_eq),
pure (result, pf)
| `(@multiset.sum %%α %%inst %%exs) :=
tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do
(xs, list_eq) ← eval_multiset exs,
(result, sum_eq) ← list.prove_sum α xs,
pf ← i_to_expr ``(multiset.sum_congr %%list_eq %%sum_eq),
pure (result, pf)
| `(@finset.prod %%β %%α %%inst %%es %%ef) :=
tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do
(xs, list_eq, nodup) ← eval_finset decide_eq es,
(result, sum_eq) ← list.prove_prod_map β ef xs,
pf ← i_to_expr ``(finset.eval_prod_of_list %%es %%ef %%nodup %%list_eq %%sum_eq),
pure (result, pf)
| `(@finset.sum %%β %%α %%inst %%es %%ef) :=
tactic.trace_error "Internal error in `tactic.norm_num.eval_big_operators`:" $ do
(xs, list_eq, nodup) ← eval_finset decide_eq es,
(result, sum_eq) ← list.prove_sum_map β ef xs,
pf ← i_to_expr ``(finset.eval_sum_of_list %%es %%ef %%nodup %%list_eq %%sum_eq),
pure (result, pf)
| _ := failed
end tactic.norm_num
|
e817bc3e141e0a42a10d6ce521646e282e781c4d | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/syntaxAbbrevQuot.lean | 3e0e415c8df7967511aca13c1b904f3cdee59685 | [
"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 | 149 | lean | syntax foo := "a" <|> "b"
syntax "bla" foo : term -- TODO: necessary to declare a and b as keywords
#check `(foo| a)
#check (· matches `(foo| a))
|
84fd4938b44091556ff05e74f1ca471b81aa0b80 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/list/nodup.lean | 14985ba19792b3c3e49c8e813d9ff5d0fc296f02 | [
"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 | 12,089 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Kenny Lau
-/
import data.list.pairwise
import data.list.forall2
/-!
# Lists with no duplicates
`list.nodup` is defined in `data/list/defs`. In this file we prove various properties of this predicate.
-/
universes u v
open nat function
variables {α : Type u} {β : Type v}
namespace list
@[simp] theorem forall_mem_ne {a : α} {l : list α} : (∀ (a' : α), a' ∈ l → ¬a = a') ↔ a ∉ l :=
⟨λ h m, h _ m rfl, λ h a' m e, h (e.symm ▸ m)⟩
@[simp] theorem nodup_nil : @nodup α [] := pairwise.nil
@[simp] theorem nodup_cons {a : α} {l : list α} : nodup (a::l) ↔ a ∉ l ∧ nodup l :=
by simp only [nodup, pairwise_cons, forall_mem_ne]
lemma rel_nodup {r : α → β → Prop} (hr : relator.bi_unique r) : (forall₂ r ⇒ (↔)) nodup nodup
| _ _ forall₂.nil := by simp only [nodup_nil]
| _ _ (forall₂.cons hab h) :=
by simpa only [nodup_cons] using relator.rel_and (relator.rel_not (rel_mem hr hab h))
(rel_nodup h)
theorem nodup_cons_of_nodup {a : α} {l : list α} (m : a ∉ l) (n : nodup l) : nodup (a::l) :=
nodup_cons.2 ⟨m, n⟩
theorem nodup_singleton (a : α) : nodup [a] :=
nodup_cons_of_nodup (not_mem_nil a) nodup_nil
theorem nodup_of_nodup_cons {a : α} {l : list α} (h : nodup (a::l)) : nodup l :=
(nodup_cons.1 h).2
theorem not_mem_of_nodup_cons {a : α} {l : list α} (h : nodup (a::l)) : a ∉ l :=
(nodup_cons.1 h).1
theorem not_nodup_cons_of_mem {a : α} {l : list α} : a ∈ l → ¬ nodup (a :: l) :=
imp_not_comm.1 not_mem_of_nodup_cons
theorem nodup_of_sublist {l₁ l₂ : list α} : l₁ <+ l₂ → nodup l₂ → nodup l₁ :=
pairwise_of_sublist
theorem not_nodup_pair (a : α) : ¬ nodup [a, a] :=
not_nodup_cons_of_mem $ mem_singleton_self _
theorem nodup_iff_sublist {l : list α} : nodup l ↔ ∀ a, ¬ [a, a] <+ l :=
⟨λ d a h, not_nodup_pair a (nodup_of_sublist h d), begin
induction l with a l IH; intro h, {exact nodup_nil},
exact nodup_cons_of_nodup
(λ al, h a $ cons_sublist_cons _ $ singleton_sublist.2 al)
(IH $ λ a s, h a $ sublist_cons_of_sublist _ s)
end⟩
theorem nodup_iff_nth_le_inj {l : list α} :
nodup l ↔ ∀ i j h₁ h₂, nth_le l i h₁ = nth_le l j h₂ → i = j :=
pairwise_iff_nth_le.trans
⟨λ H i j h₁ h₂ h, ((lt_trichotomy _ _)
.resolve_left (λ h', H _ _ h₂ h' h))
.resolve_right (λ h', H _ _ h₁ h' h.symm),
λ H i j h₁ h₂ h, ne_of_lt h₂ (H _ _ _ _ h)⟩
@[simp] theorem nth_le_index_of [decidable_eq α] {l : list α} (H : nodup l) (n h) :
index_of (nth_le l n h) l = n :=
nodup_iff_nth_le_inj.1 H _ _ _ h $
index_of_nth_le $ index_of_lt_length.2 $ nth_le_mem _ _ _
theorem nodup_iff_count_le_one [decidable_eq α] {l : list α} : nodup l ↔ ∀ a, count a l ≤ 1 :=
nodup_iff_sublist.trans $ forall_congr $ λ a,
have [a, a] <+ l ↔ 1 < count a l, from (@le_count_iff_repeat_sublist _ _ a l 2).symm,
(not_congr this).trans not_lt
theorem nodup_repeat (a : α) : ∀ {n : ℕ}, nodup (repeat a n) ↔ n ≤ 1
| 0 := by simp [nat.zero_le]
| 1 := by simp
| (n+2) := iff_of_false
(λ H, nodup_iff_sublist.1 H a ((repeat_sublist_repeat _).2 (le_add_left 2 n)))
(not_le_of_lt $ le_add_left 2 n)
@[simp] theorem count_eq_one_of_mem [decidable_eq α] {a : α} {l : list α}
(d : nodup l) (h : a ∈ l) : count a l = 1 :=
le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h)
theorem nodup_of_nodup_append_left {l₁ l₂ : list α} : nodup (l₁++l₂) → nodup l₁ :=
nodup_of_sublist (sublist_append_left l₁ l₂)
theorem nodup_of_nodup_append_right {l₁ l₂ : list α} : nodup (l₁++l₂) → nodup l₂ :=
nodup_of_sublist (sublist_append_right l₁ l₂)
theorem nodup_append {l₁ l₂ : list α} : nodup (l₁++l₂) ↔ nodup l₁ ∧ nodup l₂ ∧ disjoint l₁ l₂ :=
by simp only [nodup, pairwise_append, disjoint_iff_ne]
theorem disjoint_of_nodup_append {l₁ l₂ : list α} (d : nodup (l₁++l₂)) : disjoint l₁ l₂ :=
(nodup_append.1 d).2.2
theorem nodup_append_of_nodup {l₁ l₂ : list α} (d₁ : nodup l₁) (d₂ : nodup l₂)
(dj : disjoint l₁ l₂) : nodup (l₁++l₂) :=
nodup_append.2 ⟨d₁, d₂, dj⟩
theorem nodup_append_comm {l₁ l₂ : list α} : nodup (l₁++l₂) ↔ nodup (l₂++l₁) :=
by simp only [nodup_append, and.left_comm, disjoint_comm]
theorem nodup_middle {a : α} {l₁ l₂ : list α} : nodup (l₁ ++ a::l₂) ↔ nodup (a::(l₁++l₂)) :=
by simp only [nodup_append, not_or_distrib, and.left_comm, and_assoc, nodup_cons, mem_append,
disjoint_cons_right]
theorem nodup_of_nodup_map (f : α → β) {l : list α} : nodup (map f l) → nodup l :=
pairwise_of_pairwise_map f $ λ a b, mt $ congr_arg f
theorem nodup_map_on {f : α → β} {l : list α} (H : ∀x∈l, ∀y∈l, f x = f y → x = y)
(d : nodup l) : nodup (map f l) :=
pairwise_map_of_pairwise _ (by exact λ a b ⟨ma, mb, n⟩ e, n (H a ma b mb e)) (pairwise.and_mem.1 d)
theorem nodup_map {f : α → β} {l : list α} (hf : injective f) : nodup l → nodup (map f l) :=
nodup_map_on (assume x _ y _ h, hf h)
theorem nodup_map_iff {f : α → β} {l : list α} (hf : injective f) : nodup (map f l) ↔ nodup l :=
⟨nodup_of_nodup_map _, nodup_map hf⟩
@[simp] theorem nodup_attach {l : list α} : nodup (attach l) ↔ nodup l :=
⟨λ h, attach_map_val l ▸ nodup_map (λ a b, subtype.eq) h,
λ h, nodup_of_nodup_map subtype.val ((attach_map_val l).symm ▸ h)⟩
theorem nodup_pmap {p : α → Prop} {f : Π a, p a → β} {l : list α} {H}
(hf : ∀ a ha b hb, f a ha = f b hb → a = b) (h : nodup l) : nodup (pmap f l H) :=
by rw [pmap_eq_map_attach]; exact nodup_map
(λ ⟨a, ha⟩ ⟨b, hb⟩ h, by congr; exact hf a (H _ ha) b (H _ hb) h)
(nodup_attach.2 h)
theorem nodup_filter (p : α → Prop) [decidable_pred p] {l} : nodup l → nodup (filter p l) :=
pairwise_filter_of_pairwise p
@[simp] theorem nodup_reverse {l : list α} : nodup (reverse l) ↔ nodup l :=
pairwise_reverse.trans $ by simp only [nodup, ne.def, eq_comm]
theorem nodup_erase_eq_filter [decidable_eq α] (a : α) {l} (d : nodup l) :
l.erase a = filter (≠ a) l :=
begin
induction d with b l m d IH, {refl},
by_cases b = a,
{ subst h, rw [erase_cons_head, filter_cons_of_neg],
symmetry, rw filter_eq_self, simpa only [ne.def, eq_comm] using m, exact not_not_intro rfl },
{ rw [erase_cons_tail _ h, filter_cons_of_pos, IH], exact h }
end
theorem nodup_erase_of_nodup [decidable_eq α] (a : α) {l} : nodup l → nodup (l.erase a) :=
nodup_of_sublist (erase_sublist _ _)
theorem nodup_diff [decidable_eq α] : ∀ {l₁ l₂ : list α} (h : l₁.nodup), (l₁.diff l₂).nodup
| l₁ [] h := h
| l₁ (a::l₂) h := by rw diff_cons; exact nodup_diff (nodup_erase_of_nodup _ h)
theorem mem_erase_iff_of_nodup [decidable_eq α] {a b : α} {l} (d : nodup l) :
a ∈ l.erase b ↔ a ≠ b ∧ a ∈ l :=
by rw nodup_erase_eq_filter b d; simp only [mem_filter, and_comm]
theorem mem_erase_of_nodup [decidable_eq α] {a : α} {l} (h : nodup l) : a ∉ l.erase a :=
λ H, ((mem_erase_iff_of_nodup h).1 H).1 rfl
theorem nodup_join {L : list (list α)} :
nodup (join L) ↔ (∀ l ∈ L, nodup l) ∧ pairwise disjoint L :=
by simp only [nodup, pairwise_join, disjoint_left.symm, forall_mem_ne]
theorem nodup_bind {l₁ : list α} {f : α → list β} : nodup (l₁.bind f) ↔
(∀ x ∈ l₁, nodup (f x)) ∧ pairwise (λ (a b : α), disjoint (f a) (f b)) l₁ :=
by simp only [list.bind, nodup_join, pairwise_map, and_comm, and.left_comm, mem_map,
exists_imp_distrib, and_imp];
rw [show (∀ (l : list β) (x : α), f x = l → x ∈ l₁ → nodup l) ↔
(∀ (x : α), x ∈ l₁ → nodup (f x)),
from forall_swap.trans $ forall_congr $ λ_, forall_eq']
theorem nodup_product {l₁ : list α} {l₂ : list β} (d₁ : nodup l₁) (d₂ : nodup l₂) :
nodup (product l₁ l₂) :=
nodup_bind.2
⟨λ a ma, nodup_map (left_inverse.injective (λ b, (rfl : (a,b).2 = b))) d₂,
d₁.imp $ λ a₁ a₂ n x h₁ h₂, begin
rcases mem_map.1 h₁ with ⟨b₁, mb₁, rfl⟩,
rcases mem_map.1 h₂ with ⟨b₂, mb₂, ⟨⟩⟩,
exact n rfl
end⟩
theorem nodup_sigma {σ : α → Type*} {l₁ : list α} {l₂ : Π a, list (σ a)}
(d₁ : nodup l₁) (d₂ : ∀ a, nodup (l₂ a)) : nodup (l₁.sigma l₂) :=
nodup_bind.2
⟨λ a ma, nodup_map (λ b b' h, by injection h with _ h; exact eq_of_heq h) (d₂ a),
d₁.imp $ λ a₁ a₂ n x h₁ h₂, begin
rcases mem_map.1 h₁ with ⟨b₁, mb₁, rfl⟩,
rcases mem_map.1 h₂ with ⟨b₂, mb₂, ⟨⟩⟩,
exact n rfl
end⟩
theorem nodup_filter_map {f : α → option β} {l : list α}
(H : ∀ (a a' : α) (b : β), b ∈ f a → b ∈ f a' → a = a') :
nodup l → nodup (filter_map f l) :=
pairwise_filter_map_of_pairwise f $ λ a a' n b bm b' bm' e, n $ H a a' b' (e ▸ bm) bm'
theorem nodup_concat {a : α} {l : list α} (h : a ∉ l) (h' : nodup l) : nodup (concat l a) :=
by rw concat_eq_append; exact nodup_append_of_nodup h' (nodup_singleton _) (disjoint_singleton.2 h)
theorem nodup_insert [decidable_eq α] {a : α} {l : list α} (h : nodup l) : nodup (insert a l) :=
if h' : a ∈ l then by rw [insert_of_mem h']; exact h
else by rw [insert_of_not_mem h', nodup_cons]; split; assumption
theorem nodup_union [decidable_eq α] (l₁ : list α) {l₂ : list α} (h : nodup l₂) :
nodup (l₁ ∪ l₂) :=
begin
induction l₁ with a l₁ ih generalizing l₂,
{ exact h },
apply nodup_insert,
exact ih h
end
theorem nodup_inter_of_nodup [decidable_eq α] {l₁ : list α} (l₂) : nodup l₁ → nodup (l₁ ∩ l₂) :=
nodup_filter _
@[simp] theorem nodup_sublists {l : list α} : nodup (sublists l) ↔ nodup l :=
⟨λ h, nodup_of_nodup_map _ (nodup_of_sublist (map_ret_sublist_sublists _) h),
λ h, (pairwise_sublists h).imp (λ _ _ h, mt reverse_inj.2 h.to_ne)⟩
@[simp] theorem nodup_sublists' {l : list α} : nodup (sublists' l) ↔ nodup l :=
by rw [sublists'_eq_sublists, nodup_map_iff reverse_injective,
nodup_sublists, nodup_reverse]
lemma nodup_sublists_len {α : Type*} (n) {l : list α}
(nd : nodup l) : (sublists_len n l).nodup :=
nodup_of_sublist (sublists_len_sublist_sublists' _ _) (nodup_sublists'.2 nd)
lemma diff_eq_filter_of_nodup [decidable_eq α] :
∀ {l₁ l₂ : list α} (hl₁ : l₁.nodup), l₁.diff l₂ = l₁.filter (∉ l₂)
| l₁ [] hl₁ := by simp
| l₁ (a::l₂) hl₁ :=
begin
rw [diff_cons, diff_eq_filter_of_nodup (nodup_erase_of_nodup _ hl₁),
nodup_erase_eq_filter _ hl₁, filter_filter],
simp only [mem_cons_iff, not_or_distrib, and.comm],
congr
end
lemma mem_diff_iff_of_nodup [decidable_eq α] {l₁ l₂ : list α} (hl₁ : l₁.nodup) {a : α} :
a ∈ l₁.diff l₂ ↔ a ∈ l₁ ∧ a ∉ l₂ :=
by rw [diff_eq_filter_of_nodup hl₁, mem_filter]
lemma nodup_update_nth : ∀ {l : list α} {n : ℕ} {a : α} (hl : l.nodup) (ha : a ∉ l),
(l.update_nth n a).nodup
| [] n a hl ha := nodup_nil
| (b::l) 0 a hl ha := nodup_cons.2 ⟨mt (mem_cons_of_mem _) ha, (nodup_cons.1 hl).2⟩
| (b::l) (n+1) a hl ha := nodup_cons.2
⟨λ h, (mem_or_eq_of_mem_update_nth h).elim
(nodup_cons.1 hl).1
(λ hba, ha (hba ▸ mem_cons_self _ _)),
nodup_update_nth (nodup_cons.1 hl).2 (mt (mem_cons_of_mem _) ha)⟩
lemma nodup.map_update [decidable_eq α] {l : list α} (hl : l.nodup) (f : α → β) (x : α) (y : β) :
l.map (function.update f x y) =
if x ∈ l then (l.map f).update_nth (l.index_of x) y else l.map f :=
begin
induction l with hd tl ihl, { simp },
rw [nodup_cons] at hl,
simp only [mem_cons_iff, map, ihl hl.2],
by_cases H : hd = x,
{ subst hd,
simp [update_nth, hl.1] },
{ simp [ne.symm H, H, update_nth, ← apply_ite (cons (f hd))] }
end
end list
theorem option.to_list_nodup {α} : ∀ o : option α, o.to_list.nodup
| none := list.nodup_nil
| (some x) := list.nodup_singleton x
|
1fa5ee01a8f9956ac7d7db9c29da291500b9c5cb | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Meta/Tactic/LinearArith/Nat/Solver.lean | 59fc01fdaf719a166e306502f6e0038b144d5120 | [
"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 | 521 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.LinearArith.Solver
import Lean.Meta.Tactic.LinearArith.Nat.Basic
namespace Lean.Meta.Linear.Nat
namespace Collect
inductive LinearArith
structure Cnstr where
cnstr : LinearArith
proof : Expr
structure State where
cnstrs : Array Cnstr
abbrev M := StateRefT State ToLinear.M
-- TODO
end Collect
end Lean.Meta.Linear.Nat
|
187e7d7cd353bc8a45bfe13ade2e3094390134f2 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Elab/StructInst.lean | 51cc3c1cc9eeb94dc2dc305935c00dc55659eb02 | [
"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 | 38,702 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Util.FindExpr
import Lean.Parser.Term
import Lean.Meta.Structure
import Lean.Elab.App
import Lean.Elab.Binders
namespace Lean.Elab.Term.StructInst
open Meta
open TSyntax.Compat
/-
Structure instances are of the form:
"{" >> optional (atomic (sepBy1 termParser ", " >> " with "))
>> manyIndent (group ((structInstFieldAbbrev <|> structInstField) >> optional ", "))
>> optEllipsis
>> optional (" : " >> termParser)
>> " }"
-/
@[builtin_macro Lean.Parser.Term.structInst] def expandStructInstExpectedType : Macro := fun stx =>
let expectedArg := stx[4]
if expectedArg.isNone then
Macro.throwUnsupported
else
let expected := expectedArg[1]
let stxNew := stx.setArg 4 mkNullNode
`(($stxNew : $expected))
/-- Expand field abbreviations. Example: `{ x, y := 0 }` expands to `{ x := x, y := 0 }` -/
@[builtin_macro Lean.Parser.Term.structInst] def expandStructInstFieldAbbrev : Macro
| `({ $[$srcs,* with]? $fields,* $[..%$ell]? $[: $ty]? }) =>
if fields.getElems.raw.any (·.getKind == ``Lean.Parser.Term.structInstFieldAbbrev) then do
let fieldsNew ← fields.getElems.mapM fun
| `(Parser.Term.structInstFieldAbbrev| $id:ident) =>
`(Parser.Term.structInstField| $id:ident := $id:ident)
| field => return field
`({ $[$srcs,* with]? $fieldsNew,* $[..%$ell]? $[: $ty]? })
else
Macro.throwUnsupported
| _ => Macro.throwUnsupported
/--
If `stx` is of the form `{ s₁, ..., sₙ with ... }` and `sᵢ` is not a local variable, expand into `let src := sᵢ; { ..., src, ... with ... }`.
Note that this one is not a `Macro` because we need to access the local context.
-/
private def expandNonAtomicExplicitSources (stx : Syntax) : TermElabM (Option Syntax) := do
let sourcesOpt := stx[1]
if sourcesOpt.isNone then
return none
else
let sources := sourcesOpt[0]
if sources.isMissing then
throwAbortTerm
let sources := sources.getSepArgs
if (← sources.allM fun source => return (← isLocalIdent? source).isSome) then
return none
if sources.any (·.isMissing) then
throwAbortTerm
return some (← go sources.toList #[])
where
go (sources : List Syntax) (sourcesNew : Array Syntax) : TermElabM Syntax := do
match sources with
| [] =>
let sources := Syntax.mkSep sourcesNew (mkAtomFrom stx ", ")
return stx.setArg 1 (stx[1].setArg 0 sources)
| source :: sources =>
if (← isLocalIdent? source).isSome then
go sources (sourcesNew.push source)
else
withFreshMacroScope do
let sourceNew ← `(src)
let r ← go sources (sourcesNew.push sourceNew)
`(let src := $source; $r)
structure ExplicitSourceInfo where
stx : Syntax
structName : Name
deriving Inhabited
structure Source where
explicit : Array ExplicitSourceInfo -- `s₁ ... sₙ with`
implicit : Option Syntax -- `..`
deriving Inhabited
def Source.isNone : Source → Bool
| { explicit := #[], implicit := none } => true
| _ => false
/-- `optional (atomic (sepBy1 termParser ", " >> " with ")` -/
private def mkSourcesWithSyntax (sources : Array Syntax) : Syntax :=
let ref := sources[0]!
let stx := Syntax.mkSep sources (mkAtomFrom ref ", ")
mkNullNode #[stx, mkAtomFrom ref "with "]
private def getStructSource (structStx : Syntax) : TermElabM Source :=
withRef structStx do
let explicitSource := structStx[1]
let implicitSource := structStx[3]
let explicit ← if explicitSource.isNone then
pure #[]
else
explicitSource[0].getSepArgs.mapM fun stx => do
let some src ← isLocalIdent? stx | unreachable!
addTermInfo' stx src
let srcType ← whnf (← inferType src)
tryPostponeIfMVar srcType
let structName ← getStructureName srcType
return { stx, structName }
let implicit := if implicitSource[0].isNone then none else implicitSource
return { explicit, implicit }
/--
We say a `{ ... }` notation is a `modifyOp` if it contains only one
```
def structInstArrayRef := leading_parser "[" >> termParser >>"]"
```
-/
private def isModifyOp? (stx : Syntax) : TermElabM (Option Syntax) := do
let s? ← stx[2].getSepArgs.foldlM (init := none) fun s? arg => do
/- arg is of the form `structInstFieldAbbrev <|> structInstField` -/
if arg.getKind == ``Lean.Parser.Term.structInstField then
/- Remark: the syntax for `structInstField` is
```
def structInstLVal := leading_parser (ident <|> numLit <|> structInstArrayRef) >> many (group ("." >> (ident <|> numLit)) <|> structInstArrayRef)
def structInstField := leading_parser structInstLVal >> " := " >> termParser
```
-/
let lval := arg[0]
let k := lval[0].getKind
if k == ``Lean.Parser.Term.structInstArrayRef then
match s? with
| none => return some arg
| some s =>
if s.getKind == ``Lean.Parser.Term.structInstArrayRef then
throwErrorAt arg "invalid \{...} notation, at most one `[..]` at a given level"
else
throwErrorAt arg "invalid \{...} notation, can't mix field and `[..]` at a given level"
else
match s? with
| none => return some arg
| some s =>
if s.getKind == ``Lean.Parser.Term.structInstArrayRef then
throwErrorAt arg "invalid \{...} notation, can't mix field and `[..]` at a given level"
else
return s?
else
return s?
match s? with
| none => return none
| some s => if s[0][0].getKind == ``Lean.Parser.Term.structInstArrayRef then return s? else return none
private def elabModifyOp (stx modifyOp : Syntax) (sources : Array ExplicitSourceInfo) (expectedType? : Option Expr) : TermElabM Expr := do
if sources.size > 1 then
throwError "invalid \{...} notation, multiple sources and array update is not supported."
let cont (val : Syntax) : TermElabM Expr := do
let lval := modifyOp[0][0]
let idx := lval[1]
let self := sources[0]!.stx
let stxNew ← `($(self).modifyOp (idx := $idx) (fun s => $val))
trace[Elab.struct.modifyOp] "{stx}\n===>\n{stxNew}"
withMacroExpansion stx stxNew <| elabTerm stxNew expectedType?
let rest := modifyOp[0][1]
if rest.isNone then
cont modifyOp[2]
else
let s ← `(s)
let valFirst := rest[0]
let valFirst := if valFirst.getKind == ``Lean.Parser.Term.structInstArrayRef then valFirst else valFirst[1]
let restArgs := rest.getArgs
let valRest := mkNullNode restArgs[1:restArgs.size]
let valField := modifyOp.setArg 0 <| mkNode ``Parser.Term.structInstLVal #[valFirst, valRest]
let valSource := mkSourcesWithSyntax #[s]
let val := stx.setArg 1 valSource
let val := val.setArg 2 <| mkNullNode #[valField]
trace[Elab.struct.modifyOp] "{stx}\nval: {val}"
cont val
/--
Get structure name.
This method triest to postpone execution if the expected type is not available.
If the expected type is available and it is a structure, then we use it.
Otherwise, we use the type of the first source. -/
private def getStructName (expectedType? : Option Expr) (sourceView : Source) : TermElabM Name := do
tryPostponeIfNoneOrMVar expectedType?
let useSource : Unit → TermElabM Name := fun _ => do
unless sourceView.explicit.isEmpty do
return sourceView.explicit[0]!.structName
match expectedType? with
| some expectedType => throwUnexpectedExpectedType expectedType
| none => throwUnknownExpectedType
match expectedType? with
| none => useSource ()
| some expectedType =>
let expectedType ← whnf expectedType
match expectedType.getAppFn with
| Expr.const constName _ =>
unless isStructure (← getEnv) constName do
throwError "invalid \{...} notation, structure type expected{indentExpr expectedType}"
return constName
| _ => useSource ()
where
throwUnknownExpectedType :=
throwError "invalid \{...} notation, expected type is not known"
throwUnexpectedExpectedType type (kind := "expected") := do
let type ← instantiateMVars type
if type.getAppFn.isMVar then
throwUnknownExpectedType
else
throwError "invalid \{...} notation, {kind} type is not of the form (C ...){indentExpr type}"
inductive FieldLHS where
| fieldName (ref : Syntax) (name : Name)
| fieldIndex (ref : Syntax) (idx : Nat)
| modifyOp (ref : Syntax) (index : Syntax)
deriving Inhabited
instance : ToFormat FieldLHS := ⟨fun lhs =>
match lhs with
| .fieldName _ n => format n
| .fieldIndex _ i => format i
| .modifyOp _ i => "[" ++ i.prettyPrint ++ "]"⟩
inductive FieldVal (σ : Type) where
| term (stx : Syntax) : FieldVal σ
| nested (s : σ) : FieldVal σ
| default : FieldVal σ -- mark that field must be synthesized using default value
deriving Inhabited
structure Field (σ : Type) where
ref : Syntax
lhs : List FieldLHS
val : FieldVal σ
expr? : Option Expr := none
deriving Inhabited
def Field.isSimple {σ} : Field σ → Bool
| { lhs := [_], .. } => true
| _ => false
inductive Struct where
/-- Remark: the field `params` is use for default value propagation. It is initially empty, and then set at `elabStruct`. -/
| mk (ref : Syntax) (structName : Name) (params : Array (Name × Expr)) (fields : List (Field Struct)) (source : Source)
deriving Inhabited
abbrev Fields := List (Field Struct)
def Struct.ref : Struct → Syntax
| ⟨ref, _, _, _, _⟩ => ref
def Struct.structName : Struct → Name
| ⟨_, structName, _, _, _⟩ => structName
def Struct.params : Struct → Array (Name × Expr)
| ⟨_, _, params, _, _⟩ => params
def Struct.fields : Struct → Fields
| ⟨_, _, _, fields, _⟩ => fields
def Struct.source : Struct → Source
| ⟨_, _, _, _, s⟩ => s
/-- `true` iff all fields of the given structure are marked as `default` -/
partial def Struct.allDefault (s : Struct) : Bool :=
s.fields.all fun { val := val, .. } => match val with
| .term _ => false
| .default => true
| .nested s => allDefault s
def formatField (formatStruct : Struct → Format) (field : Field Struct) : Format :=
Format.joinSep field.lhs " . " ++ " := " ++
match field.val with
| .term v => v.prettyPrint
| .nested s => formatStruct s
| .default => "<default>"
partial def formatStruct : Struct → Format
| ⟨_, _, _, fields, source⟩ =>
let fieldsFmt := Format.joinSep (fields.map (formatField formatStruct)) ", "
let implicitFmt := if source.implicit.isSome then " .. " else ""
if source.explicit.isEmpty then
"{" ++ fieldsFmt ++ implicitFmt ++ "}"
else
"{" ++ format (source.explicit.map (·.stx)) ++ " with " ++ fieldsFmt ++ implicitFmt ++ "}"
instance : ToFormat Struct := ⟨formatStruct⟩
instance : ToString Struct := ⟨toString ∘ format⟩
instance : ToFormat (Field Struct) := ⟨formatField formatStruct⟩
instance : ToString (Field Struct) := ⟨toString ∘ format⟩
/-
Recall that `structInstField` elements have the form
```
def structInstField := leading_parser structInstLVal >> " := " >> termParser
def structInstLVal := leading_parser (ident <|> numLit <|> structInstArrayRef) >> many (("." >> (ident <|> numLit)) <|> structInstArrayRef)
def structInstArrayRef := leading_parser "[" >> termParser >>"]"
```
-/
-- Remark: this code relies on the fact that `expandStruct` only transforms `fieldLHS.fieldName`
def FieldLHS.toSyntax (first : Bool) : FieldLHS → Syntax
| .modifyOp stx _ => stx
| .fieldName stx name => if first then mkIdentFrom stx name else mkGroupNode #[mkAtomFrom stx ".", mkIdentFrom stx name]
| .fieldIndex stx _ => if first then stx else mkGroupNode #[mkAtomFrom stx ".", stx]
def FieldVal.toSyntax : FieldVal Struct → Syntax
| .term stx => stx
| _ => unreachable!
def Field.toSyntax : Field Struct → Syntax
| field =>
let stx := field.ref
let stx := stx.setArg 2 field.val.toSyntax
match field.lhs with
| first::rest => stx.setArg 0 <| mkNullNode #[first.toSyntax true, mkNullNode <| rest.toArray.map (FieldLHS.toSyntax false) ]
| _ => unreachable!
private def toFieldLHS (stx : Syntax) : MacroM FieldLHS :=
if stx.getKind == ``Lean.Parser.Term.structInstArrayRef then
return FieldLHS.modifyOp stx stx[1]
else
-- Note that the representation of the first field is different.
let stx := if stx.getKind == groupKind then stx[1] else stx
if stx.isIdent then
return FieldLHS.fieldName stx stx.getId.eraseMacroScopes
else match stx.isFieldIdx? with
| some idx => return FieldLHS.fieldIndex stx idx
| none => Macro.throwError "unexpected structure syntax"
private def mkStructView (stx : Syntax) (structName : Name) (source : Source) : MacroM Struct := do
/- Recall that `stx` is of the form
```
leading_parser "{" >> optional (atomic (sepBy1 termParser ", " >> " with "))
>> sepByIndent (structInstFieldAbbrev <|> structInstField) ...
>> optional ".."
>> optional (" : " >> termParser)
>> " }"
```
This method assumes that `structInstFieldAbbrev` had already been expanded.
-/
let fields ← stx[2].getSepArgs.toList.mapM fun fieldStx => do
let val := fieldStx[2]
let first ← toFieldLHS fieldStx[0][0]
let rest ← fieldStx[0][1].getArgs.toList.mapM toFieldLHS
return { ref := fieldStx, lhs := first :: rest, val := FieldVal.term val : Field Struct }
return ⟨stx, structName, #[], fields, source⟩
def Struct.modifyFieldsM {m : Type → Type} [Monad m] (s : Struct) (f : Fields → m Fields) : m Struct :=
match s with
| ⟨ref, structName, params, fields, source⟩ => return ⟨ref, structName, params, (← f fields), source⟩
def Struct.modifyFields (s : Struct) (f : Fields → Fields) : Struct :=
Id.run <| s.modifyFieldsM f
def Struct.setFields (s : Struct) (fields : Fields) : Struct :=
s.modifyFields fun _ => fields
def Struct.setParams (s : Struct) (ps : Array (Name × Expr)) : Struct :=
match s with
| ⟨ref, structName, _, fields, source⟩ => ⟨ref, structName, ps, fields, source⟩
private def expandCompositeFields (s : Struct) : Struct :=
s.modifyFields fun fields => fields.map fun field => match field with
| { lhs := .fieldName _ (.str Name.anonymous ..) :: _, .. } => field
| { lhs := .fieldName ref n@(.str ..) :: rest, .. } =>
let newEntries := n.components.map <| FieldLHS.fieldName ref
{ field with lhs := newEntries ++ rest }
| _ => field
private def expandNumLitFields (s : Struct) : TermElabM Struct :=
s.modifyFieldsM fun fields => do
let env ← getEnv
let fieldNames := getStructureFields env s.structName
fields.mapM fun field => match field with
| { lhs := .fieldIndex ref idx :: rest, .. } =>
if idx == 0 then throwErrorAt ref "invalid field index, index must be greater than 0"
else if idx > fieldNames.size then throwErrorAt ref "invalid field index, structure has only #{fieldNames.size} fields"
else return { field with lhs := .fieldName ref fieldNames[idx - 1]! :: rest }
| _ => return field
/-- For example, consider the following structures:
```
structure A where
x : Nat
structure B extends A where
y : Nat
structure C extends B where
z : Bool
```
This method expands parent structure fields using the path to the parent structure.
For example,
```
{ x := 0, y := 0, z := true : C }
```
is expanded into
```
{ toB.toA.x := 0, toB.y := 0, z := true : C }
```
-/
private def expandParentFields (s : Struct) : TermElabM Struct := do
let env ← getEnv
s.modifyFieldsM fun fields => fields.mapM fun field => do match field with
| { lhs := .fieldName ref fieldName :: _, .. } =>
addCompletionInfo <| CompletionInfo.fieldId ref fieldName (← getLCtx) s.structName
match findField? env s.structName fieldName with
| none => throwErrorAt ref "'{fieldName}' is not a field of structure '{s.structName}'"
| some baseStructName =>
if baseStructName == s.structName then pure field
else match getPathToBaseStructure? env baseStructName s.structName with
| some path =>
let path := path.map fun funName => match funName with
| .str _ s => .fieldName ref (Name.mkSimple s)
| _ => unreachable!
return { field with lhs := path ++ field.lhs }
| _ => throwErrorAt ref "failed to access field '{fieldName}' in parent structure"
| _ => return field
private abbrev FieldMap := HashMap Name Fields
private def mkFieldMap (fields : Fields) : TermElabM FieldMap :=
fields.foldlM (init := {}) fun fieldMap field =>
match field.lhs with
| .fieldName _ fieldName :: _ =>
match fieldMap.find? fieldName with
| some (prevField::restFields) =>
if field.isSimple || prevField.isSimple then
throwErrorAt field.ref "field '{fieldName}' has already been specified"
else
return fieldMap.insert fieldName (field::prevField::restFields)
| _ => return fieldMap.insert fieldName [field]
| _ => unreachable!
private def isSimpleField? : Fields → Option (Field Struct)
| [field] => if field.isSimple then some field else none
| _ => none
private def getFieldIdx (structName : Name) (fieldNames : Array Name) (fieldName : Name) : TermElabM Nat := do
match fieldNames.findIdx? fun n => n == fieldName with
| some idx => return idx
| none => throwError "field '{fieldName}' is not a valid field of '{structName}'"
def mkProjStx? (s : Syntax) (structName : Name) (fieldName : Name) : TermElabM (Option Syntax) := do
if (findField? (← getEnv) structName fieldName).isNone then
return none
return some <| mkNode ``Parser.Term.proj #[s, mkAtomFrom s ".", mkIdentFrom s fieldName]
def findField? (fields : Fields) (fieldName : Name) : Option (Field Struct) :=
fields.find? fun field =>
match field.lhs with
| [.fieldName _ n] => n == fieldName
| _ => false
mutual
private partial def groupFields (s : Struct) : TermElabM Struct := do
let env ← getEnv
withRef s.ref do
s.modifyFieldsM fun fields => do
let fieldMap ← mkFieldMap fields
fieldMap.toList.mapM fun ⟨fieldName, fields⟩ => do
match isSimpleField? fields with
| some field => pure field
| none =>
let substructFields := fields.map fun field => { field with lhs := field.lhs.tail! }
let field := fields.head!
match Lean.isSubobjectField? env s.structName fieldName with
| some substructName =>
let substruct := Struct.mk s.ref substructName #[] substructFields s.source
let substruct ← expandStruct substruct
pure { field with lhs := [field.lhs.head!], val := FieldVal.nested substruct }
| none =>
let updateSource (structStx : Syntax) : TermElabM Syntax := do
let sourcesNew ← s.source.explicit.filterMapM fun source => mkProjStx? source.stx source.structName fieldName
let explicitSourceStx := if sourcesNew.isEmpty then mkNullNode else mkSourcesWithSyntax sourcesNew
let implicitSourceStx := s.source.implicit.getD mkNullNode
return (structStx.setArg 1 explicitSourceStx).setArg 3 implicitSourceStx
let valStx := s.ref -- construct substructure syntax using s.ref as template
let valStx := valStx.setArg 4 mkNullNode -- erase optional expected type
let args := substructFields.toArray.map (·.toSyntax)
let valStx := valStx.setArg 2 (mkNullNode <| mkSepArray args (mkAtom ","))
let valStx ← updateSource valStx
return { field with lhs := [field.lhs.head!], val := FieldVal.term valStx }
private partial def addMissingFields (s : Struct) : TermElabM Struct := do
let env ← getEnv
let fieldNames := getStructureFields env s.structName
let ref := s.ref.mkSynthetic
withRef ref do
let fields ← fieldNames.foldlM (init := []) fun fields fieldName => do
match findField? s.fields fieldName with
| some field => return field::fields
| none =>
let addField (val : FieldVal Struct) : TermElabM Fields := do
return { ref, lhs := [FieldLHS.fieldName ref fieldName], val := val } :: fields
match Lean.isSubobjectField? env s.structName fieldName with
| some substructName =>
-- If one of the sources has the subobject field, use it
if let some val ← s.source.explicit.findSomeM? fun source => mkProjStx? source.stx source.structName fieldName then
addField (FieldVal.term val)
else
let substruct := Struct.mk ref substructName #[] [] s.source
let substruct ← expandStruct substruct
addField (FieldVal.nested substruct)
| none =>
if let some val ← s.source.explicit.findSomeM? fun source => mkProjStx? source.stx source.structName fieldName then
addField (FieldVal.term val)
else if s.source.implicit.isSome then
addField (FieldVal.term (mkHole ref))
else
addField FieldVal.default
return s.setFields fields.reverse
private partial def expandStruct (s : Struct) : TermElabM Struct := do
let s := expandCompositeFields s
let s ← expandNumLitFields s
let s ← expandParentFields s
let s ← groupFields s
addMissingFields s
end
structure CtorHeaderResult where
ctorFn : Expr
ctorFnType : Expr
instMVars : Array MVarId
params : Array (Name × Expr)
private def mkCtorHeaderAux : Nat → Expr → Expr → Array MVarId → Array (Name × Expr) → TermElabM CtorHeaderResult
| 0, type, ctorFn, instMVars, params => return { ctorFn , ctorFnType := type, instMVars, params }
| n+1, type, ctorFn, instMVars, params => do
match (← whnfForall type) with
| .forallE paramName d b c =>
match c with
| .instImplicit =>
let a ← mkFreshExprMVar d .synthetic
mkCtorHeaderAux n (b.instantiate1 a) (mkApp ctorFn a) (instMVars.push a.mvarId!) (params.push (paramName, a))
| _ =>
let a ← mkFreshExprMVar d
mkCtorHeaderAux n (b.instantiate1 a) (mkApp ctorFn a) instMVars (params.push (paramName, a))
| _ => throwError "unexpected constructor type"
private partial def getForallBody : Nat → Expr → Option Expr
| i+1, .forallE _ _ b _ => getForallBody i b
| _+1, _ => none
| 0, type => type
private def propagateExpectedType (type : Expr) (numFields : Nat) (expectedType? : Option Expr) : TermElabM Unit := do
match expectedType? with
| none => return ()
| some expectedType =>
match getForallBody numFields type with
| none => pure ()
| some typeBody =>
unless typeBody.hasLooseBVars do
discard <| isDefEq expectedType typeBody
private def mkCtorHeader (ctorVal : ConstructorVal) (expectedType? : Option Expr) : TermElabM CtorHeaderResult := do
let us ← mkFreshLevelMVars ctorVal.levelParams.length
let val := Lean.mkConst ctorVal.name us
let type ← instantiateTypeLevelParams (ConstantInfo.ctorInfo ctorVal) us
let r ← mkCtorHeaderAux ctorVal.numParams type val #[] #[]
propagateExpectedType r.ctorFnType ctorVal.numFields expectedType?
synthesizeAppInstMVars r.instMVars r.ctorFn
return r
def markDefaultMissing (e : Expr) : Expr :=
mkAnnotation `structInstDefault e
def defaultMissing? (e : Expr) : Option Expr :=
annotation? `structInstDefault e
def throwFailedToElabField {α} (fieldName : Name) (structName : Name) (msgData : MessageData) : TermElabM α :=
throwError "failed to elaborate field '{fieldName}' of '{structName}, {msgData}"
def trySynthStructInstance? (s : Struct) (expectedType : Expr) : TermElabM (Option Expr) := do
if !s.allDefault then
return none
else
try synthInstance? expectedType catch _ => return none
structure ElabStructResult where
val : Expr
struct : Struct
instMVars : Array MVarId
private partial def elabStruct (s : Struct) (expectedType? : Option Expr) : TermElabM ElabStructResult := withRef s.ref do
let env ← getEnv
let ctorVal := getStructureCtor env s.structName
if isPrivateNameFromImportedModule env ctorVal.name then
throwError "invalid \{...} notation, constructor for `{s.structName}` is marked as private"
-- We store the parameters at the resulting `Struct`. We use this information during default value propagation.
let { ctorFn, ctorFnType, params, .. } ← mkCtorHeader ctorVal expectedType?
let (e, _, fields, instMVars) ← s.fields.foldlM (init := (ctorFn, ctorFnType, [], #[])) fun (e, type, fields, instMVars) field => do
match field.lhs with
| [.fieldName ref fieldName] =>
let type ← whnfForall type
trace[Elab.struct] "elabStruct {field}, {type}"
match type with
| .forallE _ d b bi =>
let cont (val : Expr) (field : Field Struct) (instMVars := instMVars) : TermElabM (Expr × Expr × Fields × Array MVarId) := do
pushInfoTree <| InfoTree.node (children := {}) <| Info.ofFieldInfo {
projName := s.structName.append fieldName, fieldName, lctx := (← getLCtx), val, stx := ref }
let e := mkApp e val
let type := b.instantiate1 val
let field := { field with expr? := some val }
return (e, type, field::fields, instMVars)
match field.val with
| .term stx => cont (← elabTermEnsuringType stx d.consumeTypeAnnotations) field
| .nested s =>
-- if all fields of `s` are marked as `default`, then try to synthesize instance
match (← trySynthStructInstance? s d) with
| some val => cont val { field with val := FieldVal.term (mkHole field.ref) }
| none =>
let { val, struct := sNew, instMVars := instMVarsNew } ← elabStruct s (some d)
let val ← ensureHasType d val
cont val { field with val := FieldVal.nested sNew } (instMVars ++ instMVarsNew)
| .default =>
match d.getAutoParamTactic? with
| some (.const tacticDecl ..) =>
match evalSyntaxConstant env (← getOptions) tacticDecl with
| .error err => throwError err
| .ok tacticSyntax =>
let stx ← `(by $tacticSyntax)
cont (← elabTermEnsuringType stx (d.getArg! 0).consumeTypeAnnotations) field
| _ =>
if bi == .instImplicit then
let val ← withRef field.ref <| mkFreshExprMVar d .synthetic
cont val field (instMVars.push val.mvarId!)
else
let val ← withRef field.ref <| mkFreshExprMVar (some d)
cont (markDefaultMissing val) field
| _ => withRef field.ref <| throwFailedToElabField fieldName s.structName m!"unexpected constructor type{indentExpr type}"
| _ => throwErrorAt field.ref "unexpected unexpanded structure field"
return { val := e, struct := s.setFields fields.reverse |>.setParams params, instMVars }
namespace DefaultFields
structure Context where
-- We must search for default values overriden in derived structures
structs : Array Struct := #[]
allStructNames : Array Name := #[]
/--
Consider the following example:
```
structure A where
x : Nat := 1
structure B extends A where
y : Nat := x + 1
x := y + 1
structure C extends B where
z : Nat := 2*y
x := z + 3
```
And we are trying to elaborate a structure instance for `C`. There are default values for `x` at `A`, `B`, and `C`.
We say the default value at `C` has distance 0, the one at `B` distance 1, and the one at `A` distance 2.
The field `maxDistance` specifies the maximum distance considered in a round of Default field computation.
Remark: since `C` does not set a default value of `y`, the default value at `B` is at distance 0.
The fixpoint for setting default values works in the following way.
- Keep computing default values using `maxDistance == 0`.
- We increase `maxDistance` whenever we failed to compute a new default value in a round.
- If `maxDistance > 0`, then we interrupt a round as soon as we compute some default value.
We use depth-first search.
- We sign an error if no progress is made when `maxDistance` == structure hierarchy depth (2 in the example above).
-/
maxDistance : Nat := 0
structure State where
progress : Bool := false
partial def collectStructNames (struct : Struct) (names : Array Name) : Array Name :=
let names := names.push struct.structName
struct.fields.foldl (init := names) fun names field =>
match field.val with
| .nested struct => collectStructNames struct names
| _ => names
partial def getHierarchyDepth (struct : Struct) : Nat :=
struct.fields.foldl (init := 0) fun max field =>
match field.val with
| .nested struct => Nat.max max (getHierarchyDepth struct + 1)
| _ => max
def isDefaultMissing? [Monad m] [MonadMCtx m] (field : Field Struct) : m Bool := do
if let some expr := field.expr? then
if let some (.mvar mvarId) := defaultMissing? expr then
unless (← mvarId.isAssigned) do
return true
return false
partial def findDefaultMissing? [Monad m] [MonadMCtx m] (struct : Struct) : m (Option (Field Struct)) :=
struct.fields.findSomeM? fun field => do
match field.val with
| .nested struct => findDefaultMissing? struct
| _ => return if (← isDefaultMissing? field) then field else none
partial def allDefaultMissing [Monad m] [MonadMCtx m] (struct : Struct) : m (Array (Field Struct)) :=
go struct *> get |>.run' #[]
where
go (struct : Struct) : StateT (Array (Field Struct)) m Unit :=
for field in struct.fields do
if let .nested struct := field.val then
go struct
else if (← isDefaultMissing? field) then
modify (·.push field)
def getFieldName (field : Field Struct) : Name :=
match field.lhs with
| [.fieldName _ fieldName] => fieldName
| _ => unreachable!
abbrev M := ReaderT Context (StateRefT State TermElabM)
def isRoundDone : M Bool := do
return (← get).progress && (← read).maxDistance > 0
def getFieldValue? (struct : Struct) (fieldName : Name) : Option Expr :=
struct.fields.findSome? fun field =>
if getFieldName field == fieldName then
field.expr?
else
none
partial def mkDefaultValueAux? (struct : Struct) : Expr → TermElabM (Option Expr)
| .lam n d b c => withRef struct.ref do
if c.isExplicit then
let fieldName := n
match getFieldValue? struct fieldName with
| none => return none
| some val =>
let valType ← inferType val
if (← isDefEq valType d) then
mkDefaultValueAux? struct (b.instantiate1 val)
else
return none
else
if let some (_, param) := struct.params.find? fun (paramName, _) => paramName == n then
-- Recall that we did not use to have support for parameter propagation here.
if (← isDefEq (← inferType param) d) then
mkDefaultValueAux? struct (b.instantiate1 param)
else
return none
else
let arg ← mkFreshExprMVar d
mkDefaultValueAux? struct (b.instantiate1 arg)
| e =>
if e.isAppOfArity ``id 2 then
return some e.appArg!
else
return some e
def mkDefaultValue? (struct : Struct) (cinfo : ConstantInfo) : TermElabM (Option Expr) :=
withRef struct.ref do
let us ← mkFreshLevelMVarsFor cinfo
mkDefaultValueAux? struct (← instantiateValueLevelParams cinfo us)
/-- Reduce default value. It performs beta reduction and projections of the given structures. -/
partial def reduce (structNames : Array Name) (e : Expr) : MetaM Expr := do
match e with
| .lam .. => lambdaLetTelescope e fun xs b => do mkLambdaFVars xs (← reduce structNames b)
| .forallE .. => forallTelescope e fun xs b => do mkForallFVars xs (← reduce structNames b)
| .letE .. => lambdaLetTelescope e fun xs b => do mkLetFVars xs (← reduce structNames b)
| .proj _ i b =>
match (← Meta.project? b i) with
| some r => reduce structNames r
| none => return e.updateProj! (← reduce structNames b)
| .app f .. =>
match (← reduceProjOf? e structNames.contains) with
| some r => reduce structNames r
| none =>
let f := f.getAppFn
let f' ← reduce structNames f
if f'.isLambda then
let revArgs := e.getAppRevArgs
reduce structNames (f'.betaRev revArgs)
else
let args ← e.getAppArgs.mapM (reduce structNames)
return mkAppN f' args
| .mdata _ b =>
let b ← reduce structNames b
if (defaultMissing? e).isSome && !b.isMVar then
return b
else
return e.updateMData! b
| .mvar mvarId =>
match (← getExprMVarAssignment? mvarId) with
| some val => if val.isMVar then pure val else reduce structNames val
| none => return e
| e => return e
partial def tryToSynthesizeDefault (structs : Array Struct) (allStructNames : Array Name) (maxDistance : Nat) (fieldName : Name) (mvarId : MVarId) : TermElabM Bool :=
let rec loop (i : Nat) (dist : Nat) := do
if dist > maxDistance then
return false
else if h : i < structs.size then
let struct := structs.get ⟨i, h⟩
match getDefaultFnForField? (← getEnv) struct.structName fieldName with
| some defFn =>
let cinfo ← getConstInfo defFn
let mctx ← getMCtx
match (← mkDefaultValue? struct cinfo) with
| none => setMCtx mctx; loop (i+1) (dist+1)
| some val =>
let val ← reduce allStructNames val
match val.find? fun e => (defaultMissing? e).isSome with
| some _ => setMCtx mctx; loop (i+1) (dist+1)
| none =>
let mvarDecl ← getMVarDecl mvarId
let val ← ensureHasType mvarDecl.type val
mvarId.assign val
return true
| _ => loop (i+1) dist
else
return false
loop 0 0
partial def step (struct : Struct) : M Unit :=
unless (← isRoundDone) do
withReader (fun ctx => { ctx with structs := ctx.structs.push struct }) do
for field in struct.fields do
match field.val with
| .nested struct => step struct
| _ => match field.expr? with
| none => unreachable!
| some expr =>
match defaultMissing? expr with
| some (.mvar mvarId) =>
unless (← mvarId.isAssigned) do
let ctx ← read
if (← withRef field.ref <| tryToSynthesizeDefault ctx.structs ctx.allStructNames ctx.maxDistance (getFieldName field) mvarId) then
modify fun _ => { progress := true }
| _ => pure ()
partial def propagateLoop (hierarchyDepth : Nat) (d : Nat) (struct : Struct) : M Unit := do
match (← findDefaultMissing? struct) with
| none => return () -- Done
| some field =>
trace[Elab.struct] "propagate [{d}] [field := {field}]: {struct}"
if d > hierarchyDepth then
let missingFields := (← allDefaultMissing struct).map getFieldName
let missingFieldsWithoutDefault :=
let env := (← getEnv)
let structs := (← read).allStructNames
missingFields.filter fun fieldName => structs.all fun struct =>
(getDefaultFnForField? env struct fieldName).isNone
let fieldsToReport :=
if missingFieldsWithoutDefault.isEmpty then missingFields else missingFieldsWithoutDefault
throwErrorAt field.ref "fields missing: {fieldsToReport.toList.map (s!"'{·}'") |> ", ".intercalate}"
else withReader (fun ctx => { ctx with maxDistance := d }) do
modify fun _ => { progress := false }
step struct
if (← get).progress then
propagateLoop hierarchyDepth 0 struct
else
propagateLoop hierarchyDepth (d+1) struct
def propagate (struct : Struct) : TermElabM Unit :=
let hierarchyDepth := getHierarchyDepth struct
let structNames := collectStructNames struct #[]
propagateLoop hierarchyDepth 0 struct { allStructNames := structNames } |>.run' {}
end DefaultFields
private def elabStructInstAux (stx : Syntax) (expectedType? : Option Expr) (source : Source) : TermElabM Expr := do
let structName ← getStructName expectedType? source
let struct ← liftMacroM <| mkStructView stx structName source
let struct ← expandStruct struct
trace[Elab.struct] "{struct}"
/- We try to synthesize pending problems with `withSynthesize` combinator before trying to use default values.
This is important in examples such as
```
structure MyStruct where
{α : Type u}
{β : Type v}
a : α
b : β
#check { a := 10, b := true : MyStruct }
```
were the `α` will remain "unknown" until the default instance for `OfNat` is used to ensure that `10` is a `Nat`.
TODO: investigate whether this design decision may have unintended side effects or produce confusing behavior.
-/
let { val := r, struct, instMVars } ← withSynthesize (mayPostpone := true) <| elabStruct struct expectedType?
trace[Elab.struct] "before propagate {r}"
DefaultFields.propagate struct
synthesizeAppInstMVars instMVars r
return r
@[builtin_term_elab structInst] def elabStructInst : TermElab := fun stx expectedType? => do
match (← expandNonAtomicExplicitSources stx) with
| some stxNew => withMacroExpansion stx stxNew <| elabTerm stxNew expectedType?
| none =>
let sourceView ← getStructSource stx
if let some modifyOp ← isModifyOp? stx then
if sourceView.explicit.isEmpty then
throwError "invalid \{...} notation, explicit source is required when using '[<index>] := <value>'"
elabModifyOp stx modifyOp sourceView.explicit expectedType?
else
elabStructInstAux stx expectedType? sourceView
builtin_initialize registerTraceClass `Elab.struct
end Lean.Elab.Term.StructInst
|
13f3f1807fe95d32ed43013090360c6ae676bb80 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /tests/lean/run/mutwf4.lean | bcc0f654b86c8e65ce0e431f96edc85e4d36d01b | [
"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 | 667 | lean | set_option trace.Elab.definition.wf true in
mutual
def f : Nat → Bool → Nat
| n, true => 2 * f n false
| 0, false => 1
| n, false => n + g n
def g (n : Nat) : Nat :=
if h : n ≠ 0 then
f (n-1) true
else
n
end
termination_by
invImage
(fun
| Sum.inl ⟨n, true⟩ => (n, 2)
| Sum.inl ⟨n, false⟩ => (n, 1)
| Sum.inr n => (n, 0))
$ Prod.lex sizeOfWFRel sizeOfWFRel
decreasing_by
simp [invImage, InvImage, Prod.lex, sizeOfWFRel, measure, Nat.lt_wfRel]
first
| apply Prod.Lex.left
apply Nat.pred_lt
assumption
| apply Prod.Lex.right
decide
#print f
#print g
#print f._unary._mutual
|
a7be2ecf64b051000cb753377926114b01a67831 | ec5a7ae10c533e1b1f4b0bc7713e91ecf829a3eb | /ijcar16/examples/cc21.lean | a509f3f47206574e020952d3eaac75d77cfd7e63 | [
"MIT"
] | permissive | leanprover/leanprover.github.io | cf248934af7c7e9aeff17cf8df3c12c5e7e73f1a | 071a20d2e059a2c3733e004c681d3949cac3c07a | refs/heads/master | 1,692,621,047,417 | 1,691,396,994,000 | 1,691,396,994,000 | 19,366,263 | 18 | 27 | MIT | 1,693,989,071,000 | 1,399,006,345,000 | Lean | UTF-8 | Lean | false | false | 815 | lean | /-
Example/test file for the congruence closure procedure described in the paper:
"Congruence Closure for Intensional Type Theory"
Daniel Selsam and Leonardo de Moura
The tactic `by blast` has been configured in this file to use just
the congruence closure procedure using the command
set_option blast.strategy "cc"
-/
open subtype
set_option blast.strategy "cc"
example (f g : Π {A : Type₁}, A → A → A) (h : nat → nat) (a b : nat) :
h = f a → h b = f a b :=
by blast
example (f g : Π {A : Type₁}, A → A → A) (h : nat → nat) (a b : nat) :
h = f a → h b = f a b :=
by blast
example (f g : Π {A : Type₁} (a b : A), {x : A | x ≠ b}) (h : Π (b : nat), {x : nat | x ≠ b}) (a b₁ b₂ : nat) :
h = f a → b₁ = b₂ → h b₁ == f a b₂ :=
by blast
|
0bf1f710fb1b35e98d84c2fbf6ef26d3ce87e004 | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/support.lean | 011ffba0d4488727ce9efd2eb75b98ffffa4651c | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 7,202 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import order.conditionally_complete_lattice
import algebra.big_operators.basic
import algebra.group.prod
import algebra.group.pi
import algebra.module.pi
/-!
# Support of a function
In this file we define `function.support f = {x | f x ≠ 0}` and prove its basic properties.
-/
universes u v w x y
open set
open_locale big_operators
namespace function
variables {α : Type u} {β : Type v} {ι : Sort w} {A : Type x} {B : Type y}
/-- `support` of a function is the set of points `x` such that `f x ≠ 0`. -/
def support [has_zero A] (f : α → A) : set α := {x | f x ≠ 0}
lemma nmem_support [has_zero A] {f : α → A} {x : α} :
x ∉ support f ↔ f x = 0 :=
not_not
@[simp]
lemma mem_support [has_zero A] {f : α → A} {x : α} :
x ∈ support f ↔ f x ≠ 0 :=
iff.rfl
lemma support_subset_iff [has_zero A] {f : α → A} {s : set α} :
support f ⊆ s ↔ ∀ x, f x ≠ 0 → x ∈ s :=
iff.rfl
lemma support_subset_iff' [has_zero A] {f : α → A} {s : set α} :
support f ⊆ s ↔ ∀ x ∉ s, f x = 0 :=
forall_congr $ λ x, by classical; exact not_imp_comm
@[simp] lemma support_eq_empty_iff [has_zero A] {f : α → A} :
support f = ∅ ↔ f = 0 :=
by { simp_rw [← subset_empty_iff, support_subset_iff', funext_iff], simp }
@[simp] lemma support_zero' [has_zero A] : support (0 : α → A) = ∅ :=
support_eq_empty_iff.2 rfl
@[simp] lemma support_zero [has_zero A] : support (λ x : α, (0 : A)) = ∅ :=
support_zero'
lemma support_binop_subset [has_zero A] (op : A → A → A) (op0 : op 0 0 = 0) (f g : α → A) :
support (λ x, op (f x) (g x)) ⊆ support f ∪ support g :=
λ x hx, classical.by_cases
(λ hf : f x = 0, or.inr $ λ hg, hx $ by simp only [hf, hg, op0])
or.inl
lemma support_add [add_monoid A] (f g : α → A) :
support (λ x, f x + g x) ⊆ support f ∪ support g :=
support_binop_subset (+) (zero_add _) f g
@[simp] lemma support_neg [add_group A] (f : α → A) :
support (λ x, -f x) = support f :=
set.ext $ λ x, not_congr neg_eq_zero
lemma support_sub [add_group A] (f g : α → A) :
support (λ x, f x - g x) ⊆ support f ∪ support g :=
support_binop_subset (has_sub.sub) (sub_self _) f g
@[simp] lemma support_mul [mul_zero_class A] [no_zero_divisors A] (f g : α → A) :
support (λ x, f x * g x) = support f ∩ support g :=
set.ext $ λ x, by simp only [support, ne.def, mul_eq_zero, mem_set_of_eq,
mem_inter_iff, not_or_distrib]
lemma support_smul_subset_right [add_monoid A] [monoid B] [distrib_mul_action B A]
(b : B) (f : α → A) :
support (b • f) ⊆ support f :=
λ x hbf hf, hbf $ by rw [pi.smul_apply, hf, smul_zero]
lemma support_smul_subset_left {R M} [semiring R] [add_comm_monoid M] [semimodule R M]
(f : α → R) (g : α → M) :
support (f • g) ⊆ support f :=
λ x hfg hf, hfg $ by rw [pi.smul_apply', hf, zero_smul]
lemma support_smul {R M} [semiring R] [add_comm_monoid M] [semimodule R M]
[no_zero_smul_divisors R M] (f : α → R) (g : α → M) :
support (f • g) = support f ∩ support g :=
ext $ λ x, smul_ne_zero
@[simp] lemma support_inv [division_ring A] (f : α → A) :
support (λ x, (f x)⁻¹) = support f :=
set.ext $ λ x, not_congr inv_eq_zero
@[simp] lemma support_div [division_ring A] (f g : α → A) :
support (λ x, f x / g x) = support f ∩ support g :=
by simp [div_eq_mul_inv]
lemma support_sup [has_zero A] [semilattice_sup A] (f g : α → A) :
support (λ x, (f x) ⊔ (g x)) ⊆ support f ∪ support g :=
support_binop_subset (⊔) sup_idem f g
lemma support_inf [has_zero A] [semilattice_inf A] (f g : α → A) :
support (λ x, (f x) ⊓ (g x)) ⊆ support f ∪ support g :=
support_binop_subset (⊓) inf_idem f g
lemma support_max [has_zero A] [linear_order A] (f g : α → A) :
support (λ x, max (f x) (g x)) ⊆ support f ∪ support g :=
support_sup f g
lemma support_min [has_zero A] [linear_order A] (f g : α → A) :
support (λ x, min (f x) (g x)) ⊆ support f ∪ support g :=
support_inf f g
lemma support_supr [has_zero A] [conditionally_complete_lattice A] [nonempty ι] (f : ι → α → A) :
support (λ x, ⨆ i, f i x) ⊆ ⋃ i, support (f i) :=
begin
intros x hx,
classical,
contrapose hx,
simp only [mem_Union, not_exists, nmem_support] at hx ⊢,
simp only [hx, csupr_const]
end
lemma support_infi [has_zero A] [conditionally_complete_lattice A] [nonempty ι] (f : ι → α → A) :
support (λ x, ⨅ i, f i x) ⊆ ⋃ i, support (f i) :=
@support_supr _ _ (order_dual A) ⟨(0:A)⟩ _ _ f
lemma support_sum [add_comm_monoid A] (s : finset α) (f : α → β → A) :
support (λ x, ∑ i in s, f i x) ⊆ ⋃ i ∈ s, support (f i) :=
begin
intros x hx,
classical,
contrapose hx,
simp only [mem_Union, not_exists, nmem_support] at hx ⊢,
exact finset.sum_eq_zero hx
end
lemma support_prod_subset [comm_monoid_with_zero A] (s : finset α) (f : α → β → A) :
support (λ x, ∏ i in s, f i x) ⊆ ⋂ i ∈ s, support (f i) :=
λ x hx, mem_bInter_iff.2 $ λ i hi H, hx $ finset.prod_eq_zero hi H
lemma support_prod [comm_monoid_with_zero A] [no_zero_divisors A] [nontrivial A]
(s : finset α) (f : α → β → A) :
support (λ x, ∏ i in s, f i x) = ⋂ i ∈ s, support (f i) :=
set.ext $ λ x, by
simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists]
lemma support_comp_subset [has_zero A] [has_zero B] {g : A → B} (hg : g 0 = 0) (f : α → A) :
support (g ∘ f) ⊆ support f :=
λ x, mt $ λ h, by simp [(∘), *]
lemma support_subset_comp [has_zero A] [has_zero B] {g : A → B} (hg : ∀ {x}, g x = 0 → x = 0)
(f : α → A) :
support f ⊆ support (g ∘ f) :=
λ x, mt hg
lemma support_comp_eq [has_zero A] [has_zero B] (g : A → B) (hg : ∀ {x}, g x = 0 ↔ x = 0)
(f : α → A) :
support (g ∘ f) = support f :=
set.ext $ λ x, not_congr hg
lemma support_comp_eq_preimage [has_zero B] (g : A → B) (f : α → A) :
support (g ∘ f) = f ⁻¹' support g :=
rfl
lemma support_prod_mk [has_zero A] [has_zero B] (f : α → A) (g : α → B) :
support (λ x, (f x, g x)) = support f ∪ support g :=
set.ext $ λ x, by simp only [support, not_and_distrib, mem_union_eq, mem_set_of_eq,
prod.mk_eq_zero, ne.def]
end function
namespace pi
variables {A : Type*} {B : Type*} [decidable_eq A] [has_zero B] {a : A} {b : B}
lemma support_single_zero : function.support (pi.single a (0 : B)) = ∅ := by simp
@[simp]
lemma support_single_of_ne (h : b ≠ 0) :
function.support (pi.single a b) = {a} :=
begin
ext,
simp only [mem_singleton_iff, ne.def, function.mem_support],
split,
{ contrapose!,
exact λ h', single_eq_of_ne h' b },
{ rintro rfl,
rw single_eq_same,
exact h }
end
lemma support_single [decidable_eq B] :
function.support (pi.single a b) = if b = 0 then ∅ else {a} := by { split_ifs with h; simp [h] }
lemma support_single_subset : function.support (pi.single a b) ⊆ {a} :=
begin
classical,
rw support_single,
split_ifs;
simp
end
end pi
|
2b83066a7b5293e7cd9a83d7599430cb9d2b68c8 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/ring_theory/localization/at_prime.lean | 1b3a5bc1a141860df5bcb4dcbe668debee9e8a05 | [
"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 | 9,807 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston, Anne Baanen
-/
import ring_theory.ideal.local_ring
import ring_theory.localization.ideal
/-!
# Localizations of commutative rings at the complement of a prime ideal
## Main definitions
* `is_localization.at_prime (I : ideal R) [is_prime I] (S : Type*)` expresses that `S` is a
localization at (the complement of) a prime ideal `I`, as an abbreviation of
`is_localization I.prime_compl S`
## Main results
* `is_localization.at_prime.local_ring`: a theorem (not an instance) stating a localization at the
complement of a prime ideal is a local ring
## Implementation notes
See `src/ring_theory/localization/basic.lean` for a design overview.
## Tags
localization, ring localization, commutative ring localization, characteristic predicate,
commutative ring, field of fractions
-/
variables {R : Type*} [comm_semiring R] (M : submonoid R) (S : Type*) [comm_semiring S]
variables [algebra R S] {P : Type*} [comm_semiring P]
section at_prime
variables (I : ideal R) [hp : I.is_prime]
include hp
namespace ideal
/-- The complement of a prime ideal `I ⊆ R` is a submonoid of `R`. -/
def prime_compl :
submonoid R :=
{ carrier := (Iᶜ : set R),
one_mem' := by convert I.ne_top_iff_one.1 hp.1; refl,
mul_mem' := λ x y hnx hny hxy, or.cases_on (hp.mem_or_mem hxy) hnx hny }
lemma prime_compl_le_non_zero_divisors [no_zero_divisors R] : I.prime_compl ≤ non_zero_divisors R :=
le_non_zero_divisors_of_no_zero_divisors $ not_not_intro I.zero_mem
end ideal
variables (S)
/-- Given a prime ideal `P`, the typeclass `is_localization.at_prime S P` states that `S` is
isomorphic to the localization of `R` at the complement of `P`. -/
protected abbreviation is_localization.at_prime := is_localization I.prime_compl S
/-- Given a prime ideal `P`, `localization.at_prime S P` is a localization of
`R` at the complement of `P`, as a quotient type. -/
protected abbreviation localization.at_prime := localization I.prime_compl
namespace is_localization
lemma at_prime.nontrivial [is_localization.at_prime S I] : nontrivial S :=
nontrivial_of_ne (0 : S) 1 $ λ hze,
begin
rw [←(algebra_map R S).map_one, ←(algebra_map R S).map_zero] at hze,
obtain ⟨t, ht⟩ := (eq_iff_exists I.prime_compl S).1 hze,
have htz : (t : R) = 0, by simpa using ht.symm,
exact t.2 (htz.symm ▸ I.zero_mem : ↑t ∈ I)
end
local attribute [instance] at_prime.nontrivial
theorem at_prime.local_ring [is_localization.at_prime S I] : local_ring S :=
local_ring.of_nonunits_add
begin
intros x y hx hy hu,
cases is_unit_iff_exists_inv.1 hu with z hxyz,
have : ∀ {r : R} {s : I.prime_compl}, mk' S r s ∈ nonunits S → r ∈ I, from
λ (r : R) (s : I.prime_compl), not_imp_comm.1
(λ nr, is_unit_iff_exists_inv.2 ⟨mk' S ↑s (⟨r, nr⟩ : I.prime_compl),
mk'_mul_mk'_eq_one' _ _ nr⟩),
rcases mk'_surjective I.prime_compl x with ⟨rx, sx, hrx⟩,
rcases mk'_surjective I.prime_compl y with ⟨ry, sy, hry⟩,
rcases mk'_surjective I.prime_compl z with ⟨rz, sz, hrz⟩,
rw [←hrx, ←hry, ←hrz, ←mk'_add, ←mk'_mul,
←mk'_self S I.prime_compl.one_mem] at hxyz,
rw ←hrx at hx, rw ←hry at hy,
obtain ⟨t, ht⟩ := is_localization.eq.1 hxyz,
simp only [mul_one, one_mul, submonoid.coe_mul, subtype.coe_mk] at ht,
suffices : ↑t * (↑sx * ↑sy * ↑sz) ∈ I, from
not_or (mt hp.mem_or_mem $ not_or sx.2 sy.2) sz.2
(hp.mem_or_mem $ (hp.mem_or_mem this).resolve_left t.2),
rw [←ht],
exact I.mul_mem_left _ (I.mul_mem_right _ (I.add_mem (I.mul_mem_right _ $ this hx)
(I.mul_mem_right _ $ this hy))),
end
end is_localization
namespace localization
/-- The localization of `R` at the complement of a prime ideal is a local ring. -/
instance at_prime.local_ring : local_ring (localization I.prime_compl) :=
is_localization.at_prime.local_ring (localization I.prime_compl) I
end localization
end at_prime
namespace is_localization
variables {A : Type*} [comm_ring A] [is_domain A]
/--
The localization of an integral domain at the complement of a prime ideal is an integral domain.
-/
instance is_domain_of_local_at_prime {P : ideal A} (hp : P.is_prime) :
is_domain (localization.at_prime P) :=
is_domain_localization P.prime_compl_le_non_zero_divisors
namespace at_prime
variables (I : ideal R) [hI : I.is_prime] [is_localization.at_prime S I]
include hI
lemma is_unit_to_map_iff (x : R) :
is_unit ((algebra_map R S) x) ↔ x ∈ I.prime_compl :=
⟨λ h hx, (is_prime_of_is_prime_disjoint I.prime_compl S I hI disjoint_compl_left).ne_top $
(ideal.map (algebra_map R S) I).eq_top_of_is_unit_mem (ideal.mem_map_of_mem _ hx) h,
λ h, map_units S ⟨x, h⟩⟩
-- Can't use typeclasses to infer the `local_ring` instance, so use an `opt_param` instead
-- (since `local_ring` is a `Prop`, there should be no unification issues.)
lemma to_map_mem_maximal_iff (x : R) (h : _root_.local_ring S := local_ring S I) :
algebra_map R S x ∈ local_ring.maximal_ideal S ↔ x ∈ I :=
not_iff_not.mp $ by
simpa only [local_ring.mem_maximal_ideal, mem_nonunits_iff, not_not]
using is_unit_to_map_iff S I x
lemma comap_maximal_ideal (h : _root_.local_ring S := local_ring S I) :
(local_ring.maximal_ideal S).comap (algebra_map R S) = I :=
ideal.ext $ λ x, by simpa only [ideal.mem_comap] using to_map_mem_maximal_iff _ I x
lemma is_unit_mk'_iff (x : R) (y : I.prime_compl) :
is_unit (mk' S x y) ↔ x ∈ I.prime_compl :=
⟨λ h hx, mk'_mem_iff.mpr ((to_map_mem_maximal_iff S I x).mpr hx) h,
λ h, is_unit_iff_exists_inv.mpr ⟨mk' S ↑y ⟨x, h⟩, mk'_mul_mk'_eq_one ⟨x, h⟩ y⟩⟩
lemma mk'_mem_maximal_iff (x : R) (y : I.prime_compl) (h : _root_.local_ring S := local_ring S I) :
mk' S x y ∈ local_ring.maximal_ideal S ↔ x ∈ I :=
not_iff_not.mp $ by
simpa only [local_ring.mem_maximal_ideal, mem_nonunits_iff, not_not]
using is_unit_mk'_iff S I x y
end at_prime
end is_localization
namespace localization
open is_localization
local attribute [instance] classical.prop_decidable
variables (I : ideal R) [hI : I.is_prime]
include hI
variables {I}
/-- The unique maximal ideal of the localization at `I.prime_compl` lies over the ideal `I`. -/
lemma at_prime.comap_maximal_ideal :
ideal.comap (algebra_map R (localization.at_prime I))
(local_ring.maximal_ideal (localization I.prime_compl)) = I :=
at_prime.comap_maximal_ideal _ _
/-- The image of `I` in the localization at `I.prime_compl` is a maximal ideal, and in particular
it is the unique maximal ideal given by the local ring structure `at_prime.local_ring` -/
lemma at_prime.map_eq_maximal_ideal :
ideal.map (algebra_map R (localization.at_prime I)) I =
(local_ring.maximal_ideal (localization I.prime_compl)) :=
begin
convert congr_arg (ideal.map _) at_prime.comap_maximal_ideal.symm,
rw map_comap I.prime_compl
end
lemma le_comap_prime_compl_iff {J : ideal P} [hJ : J.is_prime] {f : R →+* P} :
I.prime_compl ≤ J.prime_compl.comap f ↔ J.comap f ≤ I :=
⟨λ h x hx, by { contrapose! hx, exact h hx },
λ h x hx hfxJ, hx (h hfxJ)⟩
variables (I)
/--
For a ring hom `f : R →+* S` and a prime ideal `J` in `S`, the induced ring hom from the
localization of `R` at `J.comap f` to the localization of `S` at `J`.
To make this definition more flexible, we allow any ideal `I` of `R` as input, together with a proof
that `I = J.comap f`. This can be useful when `I` is not definitionally equal to `J.comap f`.
-/
noncomputable def local_ring_hom (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) :
localization.at_prime I →+* localization.at_prime J :=
is_localization.map (localization.at_prime J) f (le_comap_prime_compl_iff.mpr (ge_of_eq hIJ))
lemma local_ring_hom_to_map (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) (x : R) :
local_ring_hom I J f hIJ (algebra_map _ _ x) = algebra_map _ _ (f x) :=
map_eq _ _
lemma local_ring_hom_mk' (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) (x : R) (y : I.prime_compl) :
local_ring_hom I J f hIJ (is_localization.mk' _ x y) =
is_localization.mk' (localization.at_prime J) (f x)
(⟨f y, le_comap_prime_compl_iff.mpr (ge_of_eq hIJ) y.2⟩ : J.prime_compl) :=
map_mk' _ _ _
instance is_local_ring_hom_local_ring_hom (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) :
is_local_ring_hom (local_ring_hom I J f hIJ) :=
is_local_ring_hom.mk $ λ x hx,
begin
rcases is_localization.mk'_surjective I.prime_compl x with ⟨r, s, rfl⟩,
rw local_ring_hom_mk' at hx,
rw at_prime.is_unit_mk'_iff at hx ⊢,
exact λ hr, hx ((set_like.ext_iff.mp hIJ r).mp hr),
end
lemma local_ring_hom_unique (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) {j : localization.at_prime I →+* localization.at_prime J}
(hj : ∀ x : R, j (algebra_map _ _ x) = algebra_map _ _ (f x)) :
local_ring_hom I J f hIJ = j :=
map_unique _ _ hj
@[simp] lemma local_ring_hom_id :
local_ring_hom I I (ring_hom.id R) (ideal.comap_id I).symm = ring_hom.id _ :=
local_ring_hom_unique _ _ _ _ (λ x, rfl)
@[simp] lemma local_ring_hom_comp {S : Type*} [comm_semiring S]
(J : ideal S) [hJ : J.is_prime] (K : ideal P) [hK : K.is_prime]
(f : R →+* S) (hIJ : I = J.comap f) (g : S →+* P) (hJK : J = K.comap g) :
local_ring_hom I K (g.comp f) (by rw [hIJ, hJK, ideal.comap_comap f g]) =
(local_ring_hom J K g hJK).comp (local_ring_hom I J f hIJ) :=
local_ring_hom_unique _ _ _ _
(λ r, by simp only [function.comp_app, ring_hom.coe_comp, local_ring_hom_to_map])
end localization
|
9752f416eeda0f2e1e55ec941a708a6cda8f03ee | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebra/module/opposites_auto.lean | 8d48dccb4ed6209f00e521dbe262f934b7c2f2be | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,263 | lean | /-
Copyright (c) 2020 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.module.linear_map
import Mathlib.algebra.opposites
import Mathlib.PostPort
universes u v
namespace Mathlib
/-!
# Module operations on `Mᵒᵖ`
This file contains definitions that could not be placed into `algebra.opposites` due to import
cycles.
-/
namespace opposite
/-- `opposite.distrib_mul_action` extends to a `semimodule` -/
protected instance semimodule (R : Type u) {M : Type v} [semiring R] [add_comm_monoid M]
[semimodule R M] : semimodule R (Mᵒᵖ) :=
semimodule.mk sorry sorry
/-- The function `op` is a linear equivalence. -/
def op_linear_equiv (R : Type u) {M : Type v} [semiring R] [add_comm_monoid M] [semimodule R M] :
linear_equiv R M (Mᵒᵖ) :=
linear_equiv.mk (add_equiv.to_fun op_add_equiv) sorry sorry (add_equiv.inv_fun op_add_equiv) sorry
sorry
@[simp] theorem coe_op_linear_equiv (R : Type u) {M : Type v} [semiring R] [add_comm_monoid M]
[semimodule R M] : ⇑(op_linear_equiv R) = op :=
rfl
@[simp] theorem coe_op_linear_equiv_symm (R : Type u) {M : Type v} [semiring R] [add_comm_monoid M]
[semimodule R M] : ⇑(linear_equiv.symm (op_linear_equiv R)) = unop :=
rfl
@[simp] theorem coe_op_linear_equiv_to_linear_map (R : Type u) {M : Type v} [semiring R]
[add_comm_monoid M] [semimodule R M] : ⇑(linear_equiv.to_linear_map (op_linear_equiv R)) = op :=
rfl
@[simp] theorem coe_op_linear_equiv_symm_to_linear_map (R : Type u) {M : Type v} [semiring R]
[add_comm_monoid M] [semimodule R M] :
⇑(linear_equiv.to_linear_map (linear_equiv.symm (op_linear_equiv R))) = unop :=
rfl
@[simp] theorem op_linear_equiv_to_add_equiv (R : Type u) {M : Type v} [semiring R]
[add_comm_monoid M] [semimodule R M] :
linear_equiv.to_add_equiv (op_linear_equiv R) = op_add_equiv :=
rfl
@[simp] theorem op_linear_equiv_symm_to_add_equiv (R : Type u) {M : Type v} [semiring R]
[add_comm_monoid M] [semimodule R M] :
linear_equiv.to_add_equiv (linear_equiv.symm (op_linear_equiv R)) =
add_equiv.symm op_add_equiv :=
rfl
end Mathlib |
ba0d642153819c2b3abfe4529a51eadb3911698f | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /tests/bench/unionfind.lean | a83f11e043c3c31b01bcd5cd4ca7236d19b7bf20 | [
"Apache-2.0"
] | permissive | dupuisf/lean4 | d082d13b01243e1de29ae680eefb476961221eef | 6a39c65bd28eb0e28c3870188f348c8914502718 | refs/heads/master | 1,676,948,755,391 | 1,610,665,114,000 | 1,610,665,114,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,458 | lean | #lang lean4
def StateT' (m : Type → Type) (σ : Type) (α : Type) := σ → m (α × σ)
namespace StateT'
variables {m : Type → Type} [Monad m] {σ : Type} {α β : Type}
@[inline] protected def pure (a : α) : StateT' m σ α := fun s => pure (a, s)
@[inline] protected def bind (x : StateT' m σ α) (f : α → StateT' m σ β) : StateT' m σ β := fun s => do let (a, s') ← x s; f a s'
@[inline] def read : StateT' m σ σ := fun s => pure (s, s)
@[inline] def write (s' : σ) : StateT' m σ Unit := fun s => pure ((), s')
@[inline] def updt (f : σ → σ) : StateT' m σ Unit := fun s => pure ((), f s)
instance : Monad (StateT' m σ) :=
{pure := @StateT'.pure _ _ _, bind := @StateT'.bind _ _ _}
end StateT'
def ExceptT' (m : Type → Type) (ε : Type) (α : Type) := m (Except ε α)
namespace ExceptT'
variables {m : Type → Type} [Monad m] {ε : Type} {α β : Type}
@[inline] protected def pure (a : α) : ExceptT' m ε α := (pure (Except.ok a) : m (Except ε α))
@[inline] protected def bind (x : ExceptT' m ε α) (f : α → ExceptT' m ε β) : ExceptT' m ε β :=
(do { let v ← x; match v with
| Except.error e => pure (Except.error e)
| Except.ok a => f a } : m (Except ε β))
@[inline] def error (e : ε) : ExceptT' m ε α := (pure (Except.error e) : m (Except ε α))
@[inline] def lift (x : m α) : ExceptT' m ε α := (do {let a ← x; pure (Except.ok a) } : m (Except ε α))
instance : Monad (ExceptT' m ε) :=
{pure := @ExceptT'.pure _ _ _, bind := @ExceptT'.bind _ _ _}
end ExceptT'
abbrev Node := Nat
structure nodeData :=
(find : Node) (rank : Nat := 0)
abbrev ufData := Array nodeData
abbrev M (α : Type) := ExceptT' (StateT' Id ufData) String α
@[inline] def read : M ufData := ExceptT'.lift StateT'.read
@[inline] def write (s : ufData) : M Unit := ExceptT'.lift (StateT'.write s)
@[inline] def updt (f : ufData → ufData) : M Unit := ExceptT'.lift (StateT'.updt f)
@[inline] def error {α : Type} (e : String) : M α := ExceptT'.error e
def run {α : Type} (x : M α) (s : ufData := ∅) : Except String α × ufData :=
x s
def capacity : M Nat :=
do let d ← read; pure d.size
def findEntryAux : Nat → Node → M nodeData
| 0, n => error "out of fuel"
| i+1, n =>
do let s ← read;
if h : n < s.size then
do { let e := s.get ⟨n, h⟩;
if e.find = n then pure e
else do let e₁ ← findEntryAux i e.find;
updt (fun s => s.set! n e₁);
pure e₁ }
else error "invalid Node"
def findEntry (n : Node) : M nodeData :=
do let c ← capacity;
findEntryAux c n
def find (n : Node) : M Node :=
do let e ← findEntry n; pure e.find
def mk : M Node :=
do let n ← capacity;
updt $ fun s => s.push {find := n, rank := 1};
pure n
def union (n₁ n₂ : Node) : M Unit :=
do let r₁ ← findEntry n₁;
let r₂ ← findEntry n₂;
if r₁.find = r₂.find then pure ()
else updt $ fun s =>
if r₁.rank < r₂.rank then s.set! r₁.find { find := r₂.find }
else if r₁.rank = r₂.rank then
let s₁ := s.set! r₁.find { find := r₂.find };
s₁.set! r₂.find { r₂ with rank := r₂.rank + 1 }
else s.set! r₂.find { find := r₁.find }
def mkNodes : Nat → M Unit
| 0 => pure ()
| n+1 => do _ ← mk; mkNodes n
def checkEq (n₁ n₂ : Node) : M Unit :=
do let r₁ ← find n₁; let r₂ ← find n₂;
unless (r₁ = r₂) do error "nodes are not equal"
def mergePackAux : Nat → Nat → Nat → M Unit
| 0, _, _ => pure ()
| i+1, n, d => do
let c ← capacity;
if (n+d) < c
then union n (n+d) *> mergePackAux i (n+1) d
else pure ()
def mergePack (d : Nat) : M Unit :=
do let c ← capacity; mergePackAux c 0 d
def numEqsAux : Nat → Node → Nat → M Nat
| 0, _, r => pure r
| i+1, n, r =>
do let c ← capacity;
if n < c
then do { let n₁ ← find n; numEqsAux i (n+1) (if n = n₁ then r else r+1) }
else pure r
def numEqs : M Nat :=
do let c ← capacity;
numEqsAux c 0 0
def test (n : Nat) : M Nat :=
if n < 2 then error "input must be greater than 1"
else do
mkNodes n;
mergePack 50000;
mergePack 10000;
mergePack 5000;
mergePack 1000;
numEqs
def main (xs : List String) : IO UInt32 :=
let n := xs.head!.toNat!;
match run (test n) with
| (Except.ok v, s) => IO.println ("ok " ++ toString v) *> pure 0
| (Except.error e, s) => IO.println ("Error : " ++ e) *> pure 1
|
42519a50fd8d693f9a6186aaea44aa0521fae253 | 957a80ea22c5abb4f4670b250d55534d9db99108 | /tests/lean/run/IO3.lean | e03e3b2ba62bde6cb8dd16a400773601425ba4d9 | [
"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 | 241 | lean | import system.io
open io
variable [io.interface]
def main : io unit :=
do { l ← get_line,
if l = "hello" then
put_str "you have typed hello\n"
else
do {put_str "you did not type hello\n", put_str "-----------\n"} }
|
b87f751a0aa894e731148f264cf6915772f814e8 | 92b50235facfbc08dfe7f334827d47281471333b | /library/data/set/function.lean | 2edb267b2386acb427c200de895500fa0cca078b | [
"Apache-2.0"
] | permissive | htzh/lean | 24f6ed7510ab637379ec31af406d12584d31792c | d70c79f4e30aafecdfc4a60b5d3512199200ab6e | refs/heads/master | 1,607,677,731,270 | 1,437,089,952,000 | 1,437,089,952,000 | 37,078,816 | 0 | 0 | null | 1,433,780,956,000 | 1,433,780,955,000 | null | UTF-8 | Lean | false | false | 10,916 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jeremy Avigad, Andrew Zipperer, Haitao Zhang
Functions between subsets of finite types.
-/
import .basic
open function eq.ops
namespace set
variables {X Y Z : Type}
abbreviation eq_on (f1 f2 : X → Y) (a : set X) : Prop :=
∀₀ x ∈ a, f1 x = f2 x
/- image -/
definition image (f : X → Y) (a : set X) : set Y := {y : Y | ∃x, x ∈ a ∧ f x = y}
notation f `'[`:max a `]` := image f a
theorem image_eq_image_of_eq_on {f1 f2 : X → Y} {a : set X} (H1 : eq_on f1 f2 a) :
f1 '[a] = f2 '[a] :=
setext (take y, iff.intro
(assume H2,
obtain x (H3 : x ∈ a ∧ f1 x = y), from H2,
have H4 : x ∈ a, from and.left H3,
have H5 : f2 x = y, from (H1 H4)⁻¹ ⬝ and.right H3,
exists.intro x (and.intro H4 H5))
(assume H2,
obtain x (H3 : x ∈ a ∧ f2 x = y), from H2,
have H4 : x ∈ a, from and.left H3,
have H5 : f1 x = y, from (H1 H4) ⬝ and.right H3,
exists.intro x (and.intro H4 H5)))
theorem mem_image {f : X → Y} {a : set X} {x : X} {y : Y}
(H1 : x ∈ a) (H2 : f x = y) : y ∈ f '[a] :=
exists.intro x (and.intro H1 H2)
theorem mem_image_of_mem (f : X → Y) {x : X} {a : set X} (H : x ∈ a) : f x ∈ image f a :=
mem_image H rfl
lemma image_compose (f : Y → Z) (g : X → Y) (a : set X) : (f ∘ g) '[a] = f '[g '[a]] :=
setext (take z,
iff.intro
(assume Hz : z ∈ (f ∘ g) '[a],
obtain x (Hx₁ : x ∈ a) (Hx₂ : f (g x) = z), from Hz,
have Hgx : g x ∈ g '[a], from mem_image Hx₁ rfl,
show z ∈ f '[g '[a]], from mem_image Hgx Hx₂)
(assume Hz : z ∈ f '[g '[a]],
obtain y (Hy₁ : y ∈ g '[a]) (Hy₂ : f y = z), from Hz,
obtain x (Hz₁ : x ∈ a) (Hz₂ : g x = y), from Hy₁,
show z ∈ (f ∘ g) '[a], from mem_image Hz₁ (Hz₂⁻¹ ▸ Hy₂)))
lemma image_subset {a b : set X} (f : X → Y) (H : a ⊆ b) : f '[a] ⊆ f '[b] :=
take y, assume Hy : y ∈ f '[a],
obtain x (Hx₁ : x ∈ a) (Hx₂ : f x = y), from Hy,
mem_image (H Hx₁) Hx₂
/- maps to -/
definition maps_to [reducible] (f : X → Y) (a : set X) (b : set Y) : Prop := ∀⦃x⦄, x ∈ a → f x ∈ b
theorem maps_to_of_eq_on {f1 f2 : X → Y} {a : set X} {b : set Y} (eq_on_a : eq_on f1 f2 a)
(maps_to_f1 : maps_to f1 a b) :
maps_to f2 a b :=
take x,
assume xa : x ∈ a,
have H : f1 x ∈ b, from maps_to_f1 xa,
show f2 x ∈ b, from eq_on_a xa ▸ H
theorem maps_to_compose {g : Y → Z} {f : X → Y} {a : set X} {b : set Y} {c : set Z}
(H1 : maps_to g b c) (H2 : maps_to f a b) : maps_to (g ∘ f) a c :=
take x, assume H : x ∈ a, H1 (H2 H)
theorem maps_to_univ_univ (f : X → Y) : maps_to f univ univ :=
take x, assume H, trivial
/- injectivity -/
definition inj_on [reducible] (f : X → Y) (a : set X) : Prop :=
∀⦃x1 x2 : X⦄, x1 ∈ a → x2 ∈ a → f x1 = f x2 → x1 = x2
theorem inj_on_empty (f : X → Y) : inj_on f ∅ :=
take x₁ x₂, assume H₁ H₂ H₃, false.elim H₁
theorem inj_on_of_eq_on {f1 f2 : X → Y} {a : set X} (eq_f1_f2 : eq_on f1 f2 a)
(inj_f1 : inj_on f1 a) :
inj_on f2 a :=
take x1 x2 : X,
assume ax1 : x1 ∈ a,
assume ax2 : x2 ∈ a,
assume H : f2 x1 = f2 x2,
have H' : f1 x1 = f1 x2, from eq_f1_f2 ax1 ⬝ H ⬝ (eq_f1_f2 ax2)⁻¹,
show x1 = x2, from inj_f1 ax1 ax2 H'
theorem inj_on_compose {g : Y → Z} {f : X → Y} {a : set X} {b : set Y}
(fab : maps_to f a b) (Hg : inj_on g b) (Hf: inj_on f a) :
inj_on (g ∘ f) a :=
take x1 x2 : X,
assume x1a : x1 ∈ a,
assume x2a : x2 ∈ a,
have fx1b : f x1 ∈ b, from fab x1a,
have fx2b : f x2 ∈ b, from fab x2a,
assume H1 : g (f x1) = g (f x2),
have H2 : f x1 = f x2, from Hg fx1b fx2b H1,
show x1 = x2, from Hf x1a x2a H2
theorem inj_on_of_inj_on_of_subset {f : X → Y} {a b : set X} (H1 : inj_on f b) (H2 : a ⊆ b) :
inj_on f a :=
take x1 x2 : X, assume (x1a : x1 ∈ a) (x2a : x2 ∈ a),
assume H : f x1 = f x2,
show x1 = x2, from H1 (H2 x1a) (H2 x2a) H
lemma injective_iff_inj_on_univ {f : X → Y} : injective f ↔ inj_on f univ :=
iff.intro
(assume H, take x₁ x₂, assume ax₁ ax₂, H x₁ x₂)
(assume H : inj_on f univ,
take x₁ x₂ Heq,
show x₁ = x₂, from H trivial trivial Heq)
/- surjectivity -/
definition surj_on [reducible] (f : X → Y) (a : set X) (b : set Y) : Prop := b ⊆ f '[a]
theorem surj_on_of_eq_on {f1 f2 : X → Y} {a : set X} {b : set Y} (eq_f1_f2 : eq_on f1 f2 a)
(surj_f1 : surj_on f1 a b) :
surj_on f2 a b :=
take y, assume H : y ∈ b,
obtain x (H1 : x ∈ a ∧ f1 x = y), from surj_f1 H,
have H2 : x ∈ a, from and.left H1,
have H3 : f2 x = y, from (eq_f1_f2 H2)⁻¹ ⬝ and.right H1,
exists.intro x (and.intro H2 H3)
theorem surj_on_compose {g : Y → Z} {f : X → Y} {a : set X} {b : set Y} {c : set Z}
(Hg : surj_on g b c) (Hf: surj_on f a b) :
surj_on (g ∘ f) a c :=
take z,
assume zc : z ∈ c,
obtain y (H1 : y ∈ b ∧ g y = z), from Hg zc,
obtain x (H2 : x ∈ a ∧ f x = y), from Hf (and.left H1),
show ∃x, x ∈ a ∧ g (f x) = z, from
exists.intro x
(and.intro
(and.left H2)
(calc
g (f x) = g y : {and.right H2}
... = z : and.right H1))
lemma surjective_iff_surj_on_univ {f : X → Y} : surjective f ↔ surj_on f univ univ :=
iff.intro
(assume H, take y, assume Hy,
obtain x Hx, from H y,
mem_image trivial Hx)
(assume H, take y,
obtain x H1x H2x, from H y trivial,
exists.intro x H2x)
/- bijectivity -/
definition bij_on [reducible] (f : X → Y) (a : set X) (b : set Y) : Prop :=
maps_to f a b ∧ inj_on f a ∧ surj_on f a b
theorem bij_on_of_eq_on {f1 f2 : X → Y} {a : set X} {b : set Y} (eqf : eq_on f1 f2 a)
(H : bij_on f1 a b) : bij_on f2 a b :=
match H with and.intro Hmap (and.intro Hinj Hsurj) :=
and.intro
(maps_to_of_eq_on eqf Hmap)
(and.intro
(inj_on_of_eq_on eqf Hinj)
(surj_on_of_eq_on eqf Hsurj))
end
theorem bij_on_compose {g : Y → Z} {f : X → Y} {a : set X} {b : set Y} {c : set Z}
(Hg : bij_on g b c) (Hf: bij_on f a b) :
bij_on (g ∘ f) a c :=
match Hg with and.intro Hgmap (and.intro Hginj Hgsurj) :=
match Hf with and.intro Hfmap (and.intro Hfinj Hfsurj) :=
and.intro
(maps_to_compose Hgmap Hfmap)
(and.intro
(inj_on_compose Hfmap Hginj Hfinj)
(surj_on_compose Hgsurj Hfsurj))
end
end
lemma bijective_iff_bij_on_univ {f : X → Y} : bijective f ↔ bij_on f univ univ :=
iff.intro
(assume H,
obtain Hinj Hsurj, from H,
and.intro (maps_to_univ_univ f)
(and.intro
(iff.mp !injective_iff_inj_on_univ Hinj)
(iff.mp !surjective_iff_surj_on_univ Hsurj)))
(assume H,
obtain Hmaps Hinj Hsurj, from H,
(and.intro
(iff.mp' !injective_iff_inj_on_univ Hinj)
(iff.mp' !surjective_iff_surj_on_univ Hsurj)))
/- left inverse -/
-- g is a left inverse to f on a
definition left_inv_on [reducible] (g : Y → X) (f : X → Y) (a : set X) : Prop :=
∀₀ x ∈ a, g (f x) = x
theorem left_inv_on_of_eq_on_left {g1 g2 : Y → X} {f : X → Y} {a : set X} {b : set Y}
(fab : maps_to f a b) (eqg : eq_on g1 g2 b) (H : left_inv_on g1 f a) : left_inv_on g2 f a :=
take x,
assume xa : x ∈ a,
calc
g2 (f x) = g1 (f x) : (eqg (fab xa))⁻¹
... = x : H xa
theorem left_inv_on_of_eq_on_right {g : Y → X} {f1 f2 : X → Y} {a : set X}
(eqf : eq_on f1 f2 a) (H : left_inv_on g f1 a) : left_inv_on g f2 a :=
take x,
assume xa : x ∈ a,
calc
g (f2 x) = g (f1 x) : {(eqf xa)⁻¹}
... = x : H xa
theorem inj_on_of_left_inv_on {g : Y → X} {f : X → Y} {a : set X} (H : left_inv_on g f a) :
inj_on f a :=
take x1 x2,
assume x1a : x1 ∈ a,
assume x2a : x2 ∈ a,
assume H1 : f x1 = f x2,
calc
x1 = g (f x1) : H x1a
... = g (f x2) : H1
... = x2 : H x2a
theorem left_inv_on_compose {f' : Y → X} {g' : Z → Y} {g : Y → Z} {f : X → Y}
{a : set X} {b : set Y} (fab : maps_to f a b)
(Hf : left_inv_on f' f a) (Hg : left_inv_on g' g b) : left_inv_on (f' ∘ g') (g ∘ f) a :=
take x : X,
assume xa : x ∈ a,
have fxb : f x ∈ b, from fab xa,
calc
f' (g' (g (f x))) = f' (f x) : Hg fxb
... = x : Hf xa
/- right inverse -/
-- g is a right inverse to f on a
definition right_inv_on [reducible] (g : Y → X) (f : X → Y) (b : set Y) : Prop :=
left_inv_on f g b
theorem right_inv_on_of_eq_on_left {g1 g2 : Y → X} {f : X → Y} {a : set X} {b : set Y}
(eqg : eq_on g1 g2 b) (H : right_inv_on g1 f b) : right_inv_on g2 f b :=
left_inv_on_of_eq_on_right eqg H
theorem right_inv_on_of_eq_on_right {g : Y → X} {f1 f2 : X → Y} {a : set X} {b : set Y}
(gba : maps_to g b a) (eqf : eq_on f1 f2 a) (H : right_inv_on g f1 b) : right_inv_on g f2 b :=
left_inv_on_of_eq_on_left gba eqf H
theorem surj_on_of_right_inv_on {g : Y → X} {f : X → Y} {a : set X} {b : set Y}
(gba : maps_to g b a) (H : right_inv_on g f b) :
surj_on f a b :=
take y,
assume yb : y ∈ b,
have gya : g y ∈ a, from gba yb,
have H1 : f (g y) = y, from H yb,
exists.intro (g y) (and.intro gya H1)
theorem right_inv_on_compose {f' : Y → X} {g' : Z → Y} {g : Y → Z} {f : X → Y}
{c : set Z} {b : set Y} (g'cb : maps_to g' c b)
(Hf : right_inv_on f' f b) (Hg : right_inv_on g' g c) : right_inv_on (f' ∘ g') (g ∘ f) c :=
left_inv_on_compose g'cb Hg Hf
theorem right_inv_on_of_inj_on_of_left_inv_on {f : X → Y} {g : Y → X} {a : set X} {b : set Y}
(fab : maps_to f a b) (gba : maps_to g b a) (injf : inj_on f a) (lfg : left_inv_on f g b) :
right_inv_on f g a :=
take x, assume xa : x ∈ a,
have H : f (g (f x)) = f x, from lfg (fab xa),
injf (gba (fab xa)) xa H
theorem eq_on_of_left_inv_of_right_inv {g1 g2 : Y → X} {f : X → Y} {a : set X} {b : set Y}
(g2ba : maps_to g2 b a) (Hg1 : left_inv_on g1 f a) (Hg2 : right_inv_on g2 f b) : eq_on g1 g2 b :=
take y,
assume yb : y ∈ b,
calc
g1 y = g1 (f (g2 y)) : {(Hg2 yb)⁻¹}
... = g2 y : Hg1 (g2ba yb)
theorem left_inv_on_of_surj_on_right_inv_on {f : X → Y} {g : Y → X} {a : set X} {b : set Y}
(surjf : surj_on f a b) (rfg : right_inv_on f g a) :
left_inv_on f g b :=
take y, assume yb : y ∈ b,
obtain x (xa : x ∈ a) (Hx : f x = y), from surjf yb,
calc
f (g y) = f (g (f x)) : Hx
... = f x : rfg xa
... = y : Hx
/- inverses -/
-- g is an inverse to f viewed as a map from a to b
definition inv_on [reducible] (g : Y → X) (f : X → Y) (a : set X) (b : set Y) : Prop :=
left_inv_on g f a ∧ right_inv_on g f b
theorem bij_on_of_inv_on {g : Y → X} {f : X → Y} {a : set X} {b : set Y} (fab : maps_to f a b)
(gba : maps_to g b a) (H : inv_on g f a b) : bij_on f a b :=
and.intro fab
(and.intro
(inj_on_of_left_inv_on (and.left H))
(surj_on_of_right_inv_on gba (and.right H)))
end set
|
1ba74d116943f40584083605cddc9883d34c3332 | ac89c256db07448984849346288e0eeffe8b20d0 | /stage0/src/Leanpkg.lean | c43ba4731322607586f0fe7f81e33d9cc65dafd9 | [
"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 | 8,887 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner, Sebastian Ullrich
-/
import Leanpkg.Resolve
import Leanpkg.Git
import Leanpkg.Build
open System
namespace Leanpkg
def readManifest : IO Manifest := do
let m ← Manifest.fromFile leanpkgTomlFn
if m.leanVersion ≠ leanVersionString then
IO.eprintln $ "\nWARNING: Lean version mismatch: installed version is " ++ leanVersionString
++ ", but package requires " ++ m.leanVersion ++ "\n"
return m
def writeManifest (manifest : Lean.Syntax) (fn : FilePath) : IO Unit := do
IO.FS.writeFile fn manifest.reprint.get!
def lockFileName : System.FilePath := ⟨".leanpkg-lock"⟩
partial def withLockFile (x : IO α) : IO α := do
acquire
try
x
finally
IO.FS.removeFile lockFileName
where
acquire (firstTime := true) :=
try
-- TODO: lock file should ideally contain PID
if !System.Platform.isWindows then
discard <| IO.FS.Handle.mkPrim lockFileName "wx"
else
-- `x` mode doesn't seem to work on Windows even though it's listed at
-- https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-160
-- ...? Let's use the slightly racy approach then.
if ← lockFileName.pathExists then
throw <| IO.Error.alreadyExists none 0 ""
discard <| IO.FS.Handle.mk lockFileName IO.FS.Mode.write
catch
| IO.Error.alreadyExists .. => do
if firstTime then
IO.eprintln s!"Waiting for prior leanpkg invocation to finish... (remove '{lockFileName}' if stuck)"
IO.sleep (ms := 300)
acquire (firstTime := false)
| e => throw e
def getRootPart (pkg : FilePath := ".") : IO Lean.Name := do
let entries ← pkg.readDir
match entries.filter (FilePath.extension ·.fileName == "lean") with
| #[rootFile] => FilePath.withExtension rootFile.fileName "" |>.toString
| #[] => throw <| IO.userError s!"no '.lean' file found in {← IO.FS.realPath "."}"
| _ => throw <| IO.userError s!"{← IO.FS.realPath "."} must contain a unique '.lean' file as the package root"
structure Configuration :=
leanPath : String
leanSrcPath : String
moreDeps : List FilePath
def configure : IO Configuration := do
let d ← readManifest
IO.eprintln $ "configuring " ++ d.name ++ " " ++ d.version
let assg ← solveDeps d
let paths ← constructPath assg
let mut moreDeps := [leanpkgTomlFn]
for path in paths do
unless path == FilePath.mk "." / "." do
-- build recursively
-- TODO: share build of common dependencies
execCmd {
cmd := (← IO.appPath).toString
cwd := path
args := #["build"]
}
moreDeps := (path / Build.buildPath / (← getRootPart path).toString |>.withExtension "olean") :: moreDeps
return {
leanPath := SearchPath.toString <| paths.map (· / Build.buildPath)
leanSrcPath := SearchPath.toString paths
moreDeps
}
def execMake (makeArgs : List String) (cfg : Build.Config) : IO Unit := withLockFile do
let manifest ← readManifest
let leanArgs := (match manifest.timeout with | some t => ["-T", toString t] | none => []) ++ cfg.leanArgs
let mut spawnArgs := {
cmd := "sh"
cwd := manifest.effectivePath
args := #["-c", s!"\"{← IO.appDir}/leanmake\" PKG={cfg.pkg} LEAN_OPTS=\"{" ".intercalate leanArgs}\" LEAN_PATH=\"{cfg.leanPath}\" {" ".intercalate makeArgs} MORE_DEPS+=\"{" ".intercalate (cfg.moreDeps.map toString)}\" >&2"]
}
execCmd spawnArgs
def buildImports (imports : List String) (leanArgs : List String) : IO Unit := do
unless ← leanpkgTomlFn.pathExists do
return
let manifest ← readManifest
let cfg ← configure
let imports := imports.map (·.toName)
let root ← getRootPart
let localImports := imports.filter (·.getRoot == root)
if localImports != [] then
let buildCfg : Build.Config := { pkg := root, leanArgs, leanPath := cfg.leanPath, moreDeps := cfg.moreDeps }
if ← FilePath.pathExists "Makefile" then
let oleans := localImports.map fun i => Lean.modToFilePath "build" i "olean" |>.toString
execMake oleans buildCfg
else
Build.buildModules buildCfg localImports
IO.println cfg.leanPath
IO.println cfg.leanSrcPath
def build (makeArgs leanArgs : List String) : IO Unit := do
let cfg ← configure
let root ← getRootPart
let buildCfg : Build.Config := { pkg := root, leanArgs, leanPath := cfg.leanPath, moreDeps := cfg.moreDeps }
if makeArgs != [] || (← FilePath.pathExists "Makefile") then
execMake makeArgs buildCfg
else
Build.buildModules buildCfg [root]
def initGitignoreContents :=
"/build
"
def initPkg (n : String) (fromNew : Bool) : IO Unit := do
IO.FS.writeFile leanpkgTomlFn s!"[package]
name = \"{n}\"
version = \"0.1\"
lean_version = \"{leanVersionString}\"
"
IO.FS.writeFile ⟨s!"{n.capitalize}.lean"⟩ "def main : IO Unit :=
IO.println \"Hello, world!\"
"
let h ← IO.FS.Handle.mk ⟨".gitignore"⟩ IO.FS.Mode.append (bin := false)
h.putStr initGitignoreContents
unless ← System.FilePath.isDir ⟨".git"⟩ do
(do
execCmd {cmd := "git", args := #["init", "-q"]}
unless upstreamGitBranch = "master" do
execCmd {cmd := "git", args := #["checkout", "-B", upstreamGitBranch]}
) <|> IO.eprintln "WARNING: failed to initialize git repository"
def init (n : String) := initPkg n false
def usage :=
"Lean package manager, version " ++ uiLeanVersionString ++ "
Usage: leanpkg <command>
init <name> create a Lean package in the current directory
configure download and build dependencies
build [<args>] configure and build *.olean files
See `leanpkg help <command>` for more information on a specific command."
def main : (cmd : String) → (leanpkgArgs leanArgs : List String) → IO Unit
| "init", [Name], [] => init Name
| "configure", [], [] => discard <| configure
| "print-paths", leanpkgArgs, leanArgs => buildImports leanpkgArgs leanArgs
| "build", makeArgs, leanArgs => build makeArgs leanArgs
| "help", ["configure"], [] => IO.println "Download dependencies
Usage:
leanpkg configure
This command sets up the `build/deps` directory.
For each (transitive) git dependency, the specified commit is checked out
into a sub-directory of `build/deps`. If there are dependencies on multiple
versions of the same package, the version materialized is undefined. No copy
is made of local dependencies."
| "help", ["build"], [] => IO.println "download dependencies and build *.olean files
Usage:
leanpkg build [<leanmake-args>] [-- <lean-args>]
This command invokes `leanpkg configure` followed by `leanmake <leanmake-args> LEAN_OPTS=<lean-args>`.
If defined, the `package.timeout` configuration value is passed to Lean via its `-T` parameter.
If no <lean-args> are given, only .olean files will be produced in `build/`. If `lib` or `bin`
is passed instead, the extracted C code is compiled with `c++` and a static library in `build/lib`
or an executable in `build/bin`, respectively, is created. `leanpkg build bin` requires a declaration
of name `main` in the root namespace, which must return `IO Unit` or `IO UInt32` (the exit code) and
may accept the program's command line arguments as a `List String` parameter.
NOTE: building and linking dependent libraries currently has to be done manually, e.g.
```
$ (cd a; leanpkg build lib)
$ (cd b; leanpkg build bin LINK_OPTS=../a/build/lib/libA.a)
```"
| "help", ["init"], [] => IO.println "Create a new Lean package in the current directory
Usage:
leanpkg init <name>
This command creates a new Lean package with the given name in the current
directory."
| "help", _, [] => IO.println usage
| _, _, _ => throw <| IO.userError usage
private def splitCmdlineArgsCore : List String → List String × List String
| [] => ([], [])
| (arg::args) => if arg == "--"
then ([], args)
else
let (outerArgs, innerArgs) := splitCmdlineArgsCore args
(arg::outerArgs, innerArgs)
def splitCmdlineArgs : List String → IO (String × List String × List String)
| [] => throw <| IO.userError usage
| [cmd] => return (cmd, [], [])
| (cmd::rest) =>
let (outerArgs, innerArgs) := splitCmdlineArgsCore rest
return (cmd, outerArgs, innerArgs)
end Leanpkg
def main (args : List String) : IO UInt32 := do
try
Lean.initSearchPath none -- HACK
let (cmd, outerArgs, innerArgs) ← Leanpkg.splitCmdlineArgs args
Leanpkg.main cmd outerArgs innerArgs
pure 0
catch e =>
IO.eprintln e -- avoid "uncaught exception: ..."
pure 1
|
df51091840c9aead04ac96a7e82847d663de43ac | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/linear_algebra/matrix/circulant.lean | 9a0702d86b4b43cef454cfa589b900e58d43d599 | [
"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,834 | lean | /-
Copyright (c) 2021 Lu-Ming Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Lu-Ming Zhang
-/
import linear_algebra.matrix.symmetric
/-!
# Circulant matrices
This file contains the definition and basic results about circulant matrices.
Given a vector `v : n → α` indexed by a type that is endowed with subtraction,
`matrix.circulant v` is the matrix whose `(i, j)`th entry is `v (i - j)`.
## Main results
- `matrix.circulant`: the circulant matrix generated by a given vector `v : n → α`.
- `matrix.circulant_mul`: the product of two circulant matrices `circulant v` and `circulant w` is
the circulant matrix generated by `mul_vec (circulant v) w`.
- `matrix.circulant_mul_comm`: multiplication of circulant matrices commutes when the elements do.
## Implementation notes
`matrix.fin.foo` is the `fin n` version of `matrix.foo`.
Namely, the index type of the circulant matrices in discussion is `fin n`.
## Tags
circulant, matrix
-/
variables {α β m n R : Type*}
namespace matrix
open function
open_locale matrix big_operators
/-- Given the condition `[has_sub n]` and a vector `v : n → α`,
we define `circulant v` to be the circulant matrix generated by `v` of type `matrix n n α`.
The `(i,j)`th entry is defined to be `v (i - j)`. -/
@[simp]
def circulant [has_sub n] (v : n → α) : matrix n n α
| i j := v (i - j)
lemma circulant_col_zero_eq [add_group n] (v : n → α) (i : n) : circulant v i 0 = v i :=
congr_arg v (sub_zero _)
lemma circulant_injective [add_group n] : injective (circulant : (n → α) → matrix n n α) :=
begin
intros v w h,
ext k,
rw [← circulant_col_zero_eq v, ← circulant_col_zero_eq w, h]
end
lemma fin.circulant_injective : ∀ n, injective (λ v : fin n → α, circulant v)
| 0 := dec_trivial
| (n+1) := circulant_injective
@[simp] lemma circulant_inj [add_group n] {v w : n → α} :
circulant v = circulant w ↔ v = w :=
circulant_injective.eq_iff
@[simp] lemma fin.circulant_inj {n} {v w : fin n → α} :
circulant v = circulant w ↔ v = w :=
(fin.circulant_injective n).eq_iff
lemma transpose_circulant [add_group n] (v : n → α) :
(circulant v)ᵀ = circulant (λ i, v (-i)) :=
by ext; simp
lemma conj_transpose_circulant [has_star α] [add_group n] (v : n → α) :
(circulant v)ᴴ = circulant (star (λ i, v (-i))) :=
by ext; simp
lemma fin.transpose_circulant : ∀ {n} (v : fin n → α), (circulant v)ᵀ = circulant (λ i, v (-i))
| 0 := dec_trivial
| (n+1) := transpose_circulant
lemma fin.conj_transpose_circulant [has_star α] :
∀ {n} (v : fin n → α), (circulant v)ᴴ = circulant (star (λ i, v (-i)))
| 0 := dec_trivial
| (n+1) := conj_transpose_circulant
lemma map_circulant [has_sub n] (v : n → α) (f : α → β) :
(circulant v).map f = circulant (λ i, f (v i)) :=
ext $ λ _ _, rfl
lemma circulant_neg [has_neg α] [has_sub n] (v : n → α) :
circulant (- v) = - circulant v :=
ext $ λ _ _, rfl
@[simp] lemma circulant_zero (α n) [has_zero α] [has_sub n] :
circulant 0 = (0 : matrix n n α) :=
ext $ λ _ _, rfl
lemma circulant_add [has_add α] [has_sub n] (v w : n → α) :
circulant (v + w) = circulant v + circulant w :=
ext $ λ _ _, rfl
lemma circulant_sub [has_sub α] [has_sub n] (v w : n → α) :
circulant (v - w) = circulant v - circulant w :=
ext $ λ _ _, rfl
/-- The product of two circulant matrices `circulant v` and `circulant w` is
the circulant matrix generated by `mul_vec (circulant v) w`. -/
lemma circulant_mul [semiring α] [fintype n] [add_group n] (v w : n → α) :
circulant v ⬝ circulant w = circulant (mul_vec (circulant v) w) :=
begin
ext i j,
simp only [mul_apply, mul_vec, circulant, dot_product],
refine fintype.sum_equiv (equiv.sub_right j) _ _ _,
intro x,
simp only [equiv.sub_right_apply, sub_sub_sub_cancel_right],
end
lemma fin.circulant_mul [semiring α] :
∀ {n} (v w : fin n → α), circulant v ⬝ circulant w = circulant (mul_vec (circulant v) w)
| 0 := dec_trivial
| (n+1) := circulant_mul
/-- Multiplication of circulant matrices commutes when the elements do. -/
lemma circulant_mul_comm
[comm_semigroup α] [add_comm_monoid α] [fintype n] [add_comm_group n] (v w : n → α) :
circulant v ⬝ circulant w = circulant w ⬝ circulant v :=
begin
ext i j,
simp only [mul_apply, circulant, mul_comm],
refine fintype.sum_equiv ((equiv.sub_left i).trans (equiv.add_right j)) _ _ _,
intro x,
congr' 2,
{ simp },
{ simp only [equiv.coe_add_right, function.comp_app,
equiv.coe_trans, equiv.sub_left_apply],
abel }
end
lemma fin.circulant_mul_comm [comm_semigroup α] [add_comm_monoid α] :
∀ {n} (v w : fin n → α), circulant v ⬝ circulant w = circulant w ⬝ circulant v
| 0 := dec_trivial
| (n+1) := circulant_mul_comm
/-- `k • circulant v` is another circulant matrix `circulant (k • v)`. -/
lemma circulant_smul [has_sub n] [has_smul R α] (k : R) (v : n → α) :
circulant (k • v) = k • circulant v :=
by ext; simp
@[simp] lemma circulant_single_one
(α n) [has_zero α] [has_one α] [decidable_eq n] [add_group n] :
circulant (pi.single 0 1 : n → α) = (1 : matrix n n α) :=
by { ext i j, simp [one_apply, pi.single_apply, sub_eq_zero] }
@[simp] lemma circulant_single
(n) [semiring α] [decidable_eq n] [add_group n] [fintype n] (a : α) :
circulant (pi.single 0 a : n → α) = scalar n a :=
begin
ext i j,
simp [pi.single_apply, one_apply, sub_eq_zero],
end
/-- Note we use `↑i = 0` instead of `i = 0` as `fin 0` has no `0`.
This means that we cannot state this with `pi.single` as we did with `matrix.circulant_single`. -/
lemma fin.circulant_ite (α) [has_zero α] [has_one α] :
∀ n, circulant (λ i, ite (↑i = 0) 1 0 : fin n → α) = 1
| 0 := dec_trivial
| (n+1) :=
begin
rw [←circulant_single_one],
congr' with j,
simp only [pi.single_apply, fin.ext_iff],
congr
end
/-- A circulant of `v` is symmetric iff `v` equals its reverse. -/
lemma circulant_is_symm_iff [add_group n] {v : n → α} :
(circulant v).is_symm ↔ ∀ i, v (- i) = v i :=
by rw [is_symm, transpose_circulant, circulant_inj, funext_iff]
lemma fin.circulant_is_symm_iff :
∀ {n} {v : fin n → α}, (circulant v).is_symm ↔ ∀ i, v (- i) = v i
| 0 := λ v, by simp [is_symm.ext_iff, is_empty.forall_iff]
| (n+1) := λ v, circulant_is_symm_iff
/-- If `circulant v` is symmetric, `∀ i j : I, v (- i) = v i`. -/
lemma circulant_is_symm_apply [add_group n] {v : n → α} (h : (circulant v).is_symm) (i : n) :
v (-i) = v i :=
circulant_is_symm_iff.1 h i
lemma fin.circulant_is_symm_apply {n} {v : fin n → α} (h : (circulant v).is_symm) (i : fin n) :
v (-i) = v i :=
fin.circulant_is_symm_iff.1 h i
end matrix
|
ed16a67d00e9425f28c1c3b9bd97b3c1962bea73 | 9a0192b31a07f48502dacee8122e0b8beda7b110 | /Lista2Corrigido.lean | 40ce04d6e4069ee4bb0cb613b86203e749e4648e | [] | no_license | ana-/Lean | 8a5b9f2e13685bd43efd72c6665401c0851157cb | 4527da83de947fa1df368b2f9bcf322a4d14e121 | refs/heads/master | 1,593,588,619,552 | 1,565,196,720,000 | 1,565,196,720,000 | 201,090,102 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,115 | lean | /- Exercício 5
Formalize os argumentos abaixo e determine quais são válidos. Justifique via tabela verdade.
-/
-- (a) Se Jones é um comunista, então Jones é ateu. Jones é ateu. Logo, Jones é comunista.
-- resposta
/- NÃO ESTÁ BOM
PRIMEIRO, IDENTIFIQUE AS PROPOSIÇÕES:
p : jONES É COMUNISTA
q : jONES É ATEU
SEGUNDO, UMA FÓRMULA PARA CADA AFIRMAÇÃO.
O ARGUMENTO FICA
p → q, q ⊧ p (esse símbolo é feito escrevendo \Vdash)
POR FIM, PODE MONTAR A FÓRMULA COMPLETA
#CHECK ((p → q) ∧ q) → p
ACHO QUE AQUI SERIA MAIS INTERESSANTE USAR O MODELO EXAMPLE,
PARA IDENTIFICAR PREMISSAS E CONCLUSÃO (veja abaixo).
-/
/-
Example x Theorem :
The example command states a theorem without
naming it or storing it in the permanent context.
Essentially, it just checks that the given term has the indicated type.
The Theorem: it is never necessary to unfold the “definition” of a theorem;
by proof irrelevance, any two proofs of that theorem are definitionally equal.
Once the proof of a theorem is complete, typically we only need to know that the proof exists;
it doesn’t matter what the proof is.
In light of that fact, Lean tags proofs as irreducible, which serves as a hint to the parser (more precisely, the elaborator)
that there is generally no need to unfold it when processing a file.
In fact, Lean is generally able to process and check proofs in parallel, since assessing the correctness of one proof does not require knowing the details of another.
-/
/- variables p q : Prop
example (h₁ : p → q) (h₂ : q) : p := sorry
--ou
example : ((p → q) ∧ q) → p := sorry
-- NOTE QUE NÃO É ESSA FÓRMULA QUE QUEREMOS VERIFICAR SE É TAUTOLOGIA.
#check ( p → q ) ∧ (q → p)
-/
/- b) Se a temperatura e a pressão do ar estavam constantes, então não houve chuva. A temperatura permaneceu
-- constante. Portanto, se choveu, então a pressão do ar não se manteve constante.
--resposta
t : Temperatura constante
p: Pressao constante
c: houve chuva
-/
variables t p c : Prop
example (h1 : t ∧ p) (h2 : ¬ c) : c → ¬ p := sorry
#check (((t ∧ p ) → ¬ c ) ∧ t) → (c → ¬ p)
/- (c) Se Gorton ganhar a eleição, os impostos subirão se o déficit permanecer alto. Se Gorton ganhar a eleição,
o déficit permanecerá alto. Ou seja, se Gorton ganhar, haverá aumento de impostos.
resposta
g: Gorton ganhará a eleição
i: impostos subirão
d: déficit alto
-/
variables g i d : Prop
example (h1: g → d → i) (h2: g → d) : g → i := sorry
#check (g → d → i) ∧ (g → d ) → (g → i )
/- d) Se o número x termina em 0, então é divisível por 5. O número x não termina em 0. Logo, x não é
divisível por 5.
n : x termina em zero
s : x divisivel por 5
--/
variables n s : Prop
example (h1: n → s) (h2: ¬ n) : ¬ s := sorry
#check ((n → s) ∧ ¬ n) → ¬s
/-
(e) Se a = 0 e b = 0, então ab = 0. Mas ab , 0. Portanto, a , 0 e b , 0.
Professora não entendi essa questão
-/
/- (f) Uma condição suficiente para f ser integrável é que g seja limitada. Uma condição necessária para que h
seja contínua é que f seja integrável. Logo, se g é limitada ou h é contínua, então f é integrável.
f : f é integravel
l : g é limitada
h : h é continua
--/
variables f l h : Prop
example (h1: l → f) (h2: h → f) : (l ∨ h) → f := sorry
#check (l → f) ∧ ( h → f) → (l ∨ h ) → f
/-
g) Smith não pode, ao mesmo tempo, ser maratonista e fumante. Smith não é maratonista. Logo, Smith é
fumante.
m : smith é maratonista
a: smith é fumante
--/
variables m a: Prop
example (h1: ¬ (m ∧ a ) ) (h2: ¬ m) : a := sorry
#check (¬ (m ∧ a) ∧ ¬ m) → a
/-
(h) Se Jones dirigiu o carro, Smith é inocente. Se Brown disparou a arma, Smith não é inocente. Portanto,
se Brown disparou a arma, então Jones não dirigiu o carro.
j: jones dirigiu o carro
o: smith é inocente
b: brown disparou a arma
-/
variables j o b : Prop
example(h1: j → o ) (h2: b → ¬ o) : b → ¬ j := sorry
#check (j → o ) ∧ (b → ¬ o) → (b → ¬ j ) |
0907ecad621a30c1d4970360544f024168933212 | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /src/Lean/Elab/Import.lean | dca30ec44b9d28af5e95b18dabae7fbd115700fc | [
"Apache-2.0"
] | permissive | dupuisf/lean4 | d082d13b01243e1de29ae680eefb476961221eef | 6a39c65bd28eb0e28c3870188f348c8914502718 | refs/heads/master | 1,676,948,755,391 | 1,610,665,114,000 | 1,610,665,114,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,743 | 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, Sebastian Ullrich
-/
import Lean.Parser.Module
namespace Lean.Elab
def headerToImports (header : Syntax) : List Import :=
let imports := if header[0].isNone then [{ module := `Init : Import }] else []
imports ++ header[1].getArgs.toList.map fun stx =>
-- `stx` is of the form `(Module.import "import" "runtime"? id)
let runtime := !stx[1].isNone
let id := stx[2].getId
{ module := id, runtimeOnly := runtime }
def processHeader (header : Syntax) (opts : Options) (messages : MessageLog) (inputCtx : Parser.InputContext) (trustLevel : UInt32 := 0)
: IO (Environment × MessageLog) := do
try
let env ← importModules (headerToImports header) opts trustLevel
pure (env, messages)
catch e =>
let env ← mkEmptyEnvironment
let spos := header.getPos.getD 0
let pos := inputCtx.fileMap.toPosition spos
pure (env, messages.add { fileName := inputCtx.fileName, data := toString e, pos := pos })
def parseImports (input : String) (fileName : Option String := none) : IO (List Import × Position × MessageLog) := do
let fileName := fileName.getD "<input>"
let inputCtx := Parser.mkInputContext input fileName
let (header, parserState, messages) ← Parser.parseHeader inputCtx
pure (headerToImports header, inputCtx.fileMap.toPosition parserState.pos, messages)
@[export lean_print_imports]
def printImports (input : String) (fileName : Option String) : IO Unit := do
let (deps, pos, log) ← parseImports input fileName
for dep in deps do
let fname ← findOLean dep.module
IO.println fname
end Lean.Elab
|
1af8796fa1c4b8182bd5e3d1bdeb803390cb1b26 | 5ee26964f602030578ef0159d46145dd2e357ba5 | /src/for_mathlib/algebra.lean | 053b26421772b2f02695307be2b1b783136ad635 | [
"Apache-2.0"
] | permissive | fpvandoorn/lean-perfectoid-spaces | 569b4006fdfe491ca8b58dd817bb56138ada761f | 06cec51438b168837fc6e9268945735037fd1db6 | refs/heads/master | 1,590,154,571,918 | 1,557,685,392,000 | 1,557,685,392,000 | 186,363,547 | 0 | 0 | Apache-2.0 | 1,557,730,933,000 | 1,557,730,933,000 | null | UTF-8 | Lean | false | false | 4,579 | lean | import ring_theory.algebra_operations
import ring_theory.localization
import tactic.tidy
import for_mathlib.rings
local attribute [instance] set.pointwise_mul_semiring
namespace localization
variables {R : Type*} [comm_ring R] (s : set R) [is_submonoid s]
instance : algebra R (localization R s) :=
algebra.of_ring_hom of (by apply_instance)
end localization
namespace algebra
section span_mul
open submodule
variables {R : Type*} {A : Type*} [comm_ring R] [ring A] [algebra R A]
variables (M : submodule R A)
lemma mul_left_span_singleton_eq_image (a : A) :
span R {a} * M = map (lmul_left R A a) M :=
begin
apply le_antisymm,
{ rw mul_le,
intros a' ha' m hm,
rw mem_span_singleton at ha',
rcases ha' with ⟨r, rfl⟩,
use [r • m, M.smul_mem r hm],
simp },
{ rintros _ ⟨m, hm, rfl⟩,
apply mul_mem_mul _ hm,
apply subset_span,
simp }
end
lemma mul_left_span_singleton_eq_image' (a : A) : ↑(span _ {a} * M) = (*) a '' M :=
congr_arg submodule.carrier (mul_left_span_singleton_eq_image M a)
end span_mul
section to_linear_map
variables {R : Type*} {A : Type*} {B : Type*} {C : Type*}
variables [comm_ring R] [ring A] [ring B] [ring C]
variables [algebra R A] [algebra R B] [algebra R C]
@[simp] lemma to_linear_map_comp (f : A →ₐ[R] B) (g : B →ₐ[R] C) :
(g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
end to_linear_map
section
variables {R : Type*} [comm_ring R]
variables {A : Type*} [ring A] [algebra R A]
variables {B : Type*} [ring B] [algebra R B]
variables (f : A →ₐ[R] B)
lemma map_lmul_left (a : A) :
f ∘ (lmul_left R A a) = (lmul_left R B (f a)) ∘ f :=
funext $ λ b, f.map_mul a b
lemma lmul_left_mul (a b : A) :
lmul_left R A (a * b) = (lmul_left R A a).comp (lmul_left R A b) :=
linear_map.ext $ λ _, mul_assoc _ _ _
instance : is_monoid_hom (lmul_left R A) :=
{ map_one := linear_map.ext $ λ _, one_mul _,
map_mul := lmul_left_mul }
end
end algebra
namespace submodule
open algebra
variables {R : Type*} [comm_ring R]
section
variables {A : Type*} [ring A] [algebra R A]
variables {B : Type*} [ring B] [algebra R B]
variables (f : A →ₐ[R] B)
variables (M N : submodule R A)
attribute [simp] comap_id
lemma map_mul : (M * N).map f.to_linear_map = M.map f.to_linear_map * N.map f.to_linear_map :=
begin
apply le_antisymm,
{ rw [map_le_iff_le_comap, mul_le],
intros,
erw [mem_comap, f.map_mul],
apply mul_mem_mul; refine ⟨_, ‹_›, rfl⟩ },
{ rw mul_le,
rintros _ ⟨m, hm, rfl⟩ _ ⟨n, hn, rfl⟩,
use [m * n, mul_mem_mul hm hn],
apply f.map_mul }
end
@[simp] lemma lmul_left_one : lmul_left R A 1 = linear_map.id :=
linear_map.ext $ λ _, one_mul _
lemma lmul_left_comp (a : A) :
f.to_linear_map.comp (lmul_left R A a) = (lmul_left R B (f a)).comp f.to_linear_map :=
linear_map.ext $ λ b, f.map_mul a b
lemma map_eq_comap_symm {M : Type*} {N : Type*}
[add_comm_group M] [add_comm_group N] [module R M] [module R N]
(f : M ≃ₗ[R] N) :
map (↑f : M →ₗ[R] N) = comap ↑f.symm :=
funext $ λ P, ext $ λ x,
begin
rw [mem_map, mem_comap],
split; intro h,
{ rcases h with ⟨p, h₁, h₂⟩,
erw ← f.to_equiv.eq_symm_apply at h₂,
rwa h₂ at h₁ },
{ exact ⟨_, h, f.apply_symm_apply x⟩ }
end
def linear_equiv_of_units {M : Type*} [add_comm_group M] [module R M]
(f : units (M →ₗ[R] M)) : (M ≃ₗ[R] M) :=
{ inv_fun := f.inv.to_fun,
left_inv := λ m, show (f.inv * f.val) m = m,
by erw f.inv_val; simp,
right_inv := λ m, show (f.val * f.inv) m = m,
by erw f.val_inv; simp,
..f.val }
def units_of_linear_equiv {M : Type*} [add_comm_group M] [module R M]
(f : (M ≃ₗ[R] M)) : units (M →ₗ[R] M) :=
{ val := f,
inv := f.symm,
val_inv := linear_map.ext $ λ _, f.apply_symm_apply _,
inv_val := linear_map.ext $ λ _, f.symm_apply_apply _ }
def units_equiv {M : Type*} [add_comm_group M] [module R M] :
units (M →ₗ[R] M) ≃ (M ≃ₗ[R] M) :=
{ to_fun := linear_equiv_of_units,
inv_fun := units_of_linear_equiv,
left_inv := by tidy,
right_inv := λ f,
begin
delta linear_equiv_of_units units_of_linear_equiv,
cases f,
tidy
end }
lemma map_lmul_left_inv (a : units A) :
map (lmul_left R A ↑a⁻¹) = comap (lmul_left R A a) :=
map_eq_comap_symm $ linear_equiv_of_units $
units.map (lmul_left R A) a⁻¹
lemma lmul_left_units_le_iff (a : units A) :
M.map (lmul_left _ _ a) ≤ N ↔ M ≤ N.map (lmul_left _ _ ↑a⁻¹) :=
by rw [map_le_iff_le_comap, map_lmul_left_inv]
end
end submodule
|
0b462ddbdd150f4f11ae5cc1920c608e0dd9732b | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/tactic/explode.lean | bea9b1abe9e9d7d1a788381c2af950a03eeb549c | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 8,795 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro, Minchao Wu
-/
import tactic.core
/-!
# `#explode` command
Displays a proof term in a line by line format somewhat akin to a Fitch style
proof or the Metamath proof style.
-/
open expr tactic
namespace tactic
namespace explode
@[derive inhabited]
inductive status : Type | reg | intro | lam | sintro
meta structure entry : Type :=
(expr : expr)
(line : nat)
(depth : nat)
(status : status)
(thm : string)
(deps : list nat)
meta def pad_right (l : list string) : list string :=
let n := l.foldl (λ r (s:string), max r s.length) 0 in
l.map $ λ s, nat.iterate (λ s, s.push ' ') (n - s.length) s
@[derive inhabited]
meta structure entries : Type := mk' ::
(s : expr_map entry)
(l : list entry)
meta def entries.find (es : entries) (e : expr) : option entry := es.s.find e
meta def entries.size (es : entries) : ℕ := es.s.size
meta def entries.add : entries → entry → entries
| es@⟨s, l⟩ e := if s.contains e.expr then es else ⟨s.insert e.expr e, e :: l⟩
meta def entries.head (es : entries) : option entry := es.l.head'
meta def format_aux : list string → list string → list string → list entry → tactic format
| (line :: lines) (dep :: deps) (thm :: thms) (en :: es) := do
fmt ← do {
let margin := string.join (list.repeat " │" en.depth),
let margin := match en.status with
| status.sintro := " ├" ++ margin
| status.intro := " │" ++ margin ++ " ┌"
| status.reg := " │" ++ margin ++ ""
| status.lam := " │" ++ margin ++ ""
end,
p ← infer_type en.expr >>= pp,
let lhs := line ++ "│" ++ dep ++ "│ " ++ thm ++ margin ++ " ",
return $ format.of_string lhs ++ to_string p ++ format.line },
(++ fmt) <$> format_aux lines deps thms es
| _ _ _ _ := return format.nil
meta instance : has_to_tactic_format entries :=
⟨λ es : entries,
let lines := pad_right $ es.l.map (λ en, to_string en.line),
deps := pad_right $ es.l.map (λ en, string.intercalate "," (en.deps.map to_string)),
thms := pad_right $ es.l.map entry.thm in
format_aux lines deps thms es.l⟩
meta def append_dep (filter : expr → tactic unit)
(es : entries) (e : expr) (deps : list nat) : tactic (list nat) :=
do { ei ← es.find e,
filter ei.expr,
return (ei.line :: deps) }
<|> return deps
meta def may_be_proof (e : expr) : tactic bool :=
do expr.sort u ← infer_type e >>= infer_type,
return $ bnot u.nonzero
end explode
open explode
meta mutual def explode.core, explode.args (filter : expr → tactic unit)
with explode.core : expr → bool → nat → entries → tactic entries
| e@(lam n bi d b) si depth es := do
m ← mk_fresh_name,
let l := local_const m n bi d,
let b' := instantiate_var b l,
if si then
let en : entry := ⟨l, es.size, depth, status.sintro, to_string n, []⟩ in do
es' ← explode.core b' si depth (es.add en),
return $ es'.add ⟨e, es'.size, depth, status.lam, "∀I", [es.size, es'.size - 1]⟩
else do
let en : entry := ⟨l, es.size, depth, status.intro, to_string n, []⟩,
es' ← explode.core b' si (depth + 1) (es.add en),
-- in case of a "have" clause, the b' here has an annotation
deps' ← explode.append_dep filter es' b'.erase_annotations [],
deps' ← explode.append_dep filter es' l deps',
return $ es'.add ⟨e, es'.size, depth, status.lam, "∀I", deps'⟩
| e@(elet n t a b) si depth es := explode.core (reduce_lets e) si depth es
| e@(macro n l) si depth es := explode.core l.head si depth es
| e si depth es := filter e >>
match get_app_fn_args e with
| (const n _, args) :=
explode.args e args depth es (to_string n) []
| (fn, []) := do
p ← pp fn,
let en : entry := ⟨fn, es.size, depth, status.reg, to_string p, []⟩,
return (es.add en)
| (fn, args) := do
es' ← explode.core fn ff depth es,
-- in case of a "have" clause, the fn here has an annotation
deps ← explode.append_dep filter es' fn.erase_annotations [],
explode.args e args depth es' "∀E" deps
end
with explode.args : expr → list expr → nat → entries → string → list nat → tactic entries
| e (arg :: args) depth es thm deps := do
es' ← explode.core arg ff depth es <|> return es,
deps' ← explode.append_dep filter es' arg deps,
explode.args e args depth es' thm deps'
| e [] depth es thm deps :=
return (es.add ⟨e, es.size, depth, status.reg, thm, deps.reverse⟩)
meta def explode_expr (e : expr) (hide_non_prop := tt) : tactic entries :=
let filter := if hide_non_prop then λ e, may_be_proof e >>= guardb else λ _, skip in
tactic.explode.core filter e tt 0 (default _)
meta def explode (n : name) : tactic unit :=
do const n _ ← resolve_name n | fail "cannot resolve name",
d ← get_decl n,
v ← match d with
| (declaration.defn _ _ _ v _ _) := return v
| (declaration.thm _ _ _ v) := return v.get
| _ := fail "not a definition"
end,
t ← pp d.type,
explode_expr v <* trace (to_fmt n ++ " : " ++ t) >>= trace
open interactive lean lean.parser interaction_monad.result
/--
`#explode decl_name` displays a proof term in a line-by-line format somewhat akin to a Fitch-style
proof or the Metamath proof style.
`#explode iff_true_intro` produces
```lean
iff_true_intro : ∀ {a : Prop}, a → (a ↔ true)
0│ │ a ├ Prop
1│ │ h ├ a
2│ │ hl │ ┌ a
3│ │ trivial │ │ true
4│2,3│ ∀I │ a → true
5│ │ hr │ ┌ true
6│5,1│ ∀I │ true → a
7│4,6│ iff.intro │ a ↔ true
8│1,7│ ∀I │ a → (a ↔ true)
9│0,8│ ∀I │ ∀ {a : Prop}, a → (a ↔ true)
```
In more detail:
The output of `#explode` is a Fitch-style proof in a four-column diagram modeled after Metamath
proof displays like [this](http://us.metamath.org/mpeuni/ru.html). The headers of the columns are
"Step", "Hyp", "Ref", "Type" (or "Expression" in the case of Metamath):
* Step: An increasing sequence of numbers to number each step in the proof, used in the Hyp field.
* Hyp: The direct children of the current step. Most theorems are implications like `A -> B -> C`,
and so on the step proving `C` the Hyp field will refer to the steps that prove `A` and `B`.
* Ref: The name of the theorem being applied. This is well-defined in Metamath, but in Lean there
are some special steps that may have long names because the structure of proof terms doesn't
exactly match this mold.
* If the theorem is `foo (x y : Z) : A x -> B y -> C x y`:
* the Ref field will contain `foo`,
* `x` and `y` will be suppressed, because term construction is not interesting, and
* the Hyp field will reference steps proving `A x` and `B y`. This corresponds to a proof term
like `@foo x y pA pB` where `pA` and `pB` are subproofs.
* If the head of the proof term is a local constant or lambda, then in this case the Ref will
say `∀E` for forall-elimination. This happens when you have for example `h : A -> B` and
`ha : A` and prove `b` by `h ha`; we reinterpret this as if it said `∀E h ha` where `∀E` is
(n-ary) modus ponens.
* If the proof term is a lambda, we will also use `∀I` for forall-introduction, referencing the
body of the lambda. The indentation level will increase, and a bracket will surround the proof
of the body of the lambda, starting at a proof step labeled with the name of the lambda variable
and its type, and ending with the `∀I` step. Metamath doesn't have steps like this, but the
style is based on Fitch proofs in first-order logic.
* Type: This contains the type of the proof term, the theorem being proven at the current step.
This proof layout differs from `#print` in using lots of intermediate step displays so that you
can follow along and don't have to see term construction steps because they are implicitly in the
intermediate step displays.
Also, it is common for a Lean theorem to begin with a sequence of lambdas introducing local
constants of the theorem. In order to minimize the indentation level, the `∀I` steps at the end of
the proof will be introduced in a group and the indentation will stay fixed. (The indentation
brackets are only needed in order to delimit the scope of assumptions, and these assumptions
have global scope anyway so detailed tracking is not necessary.)
-/
@[user_command]
meta def explode_cmd (_ : parse $ tk "#explode") : parser unit :=
do n ← ident,
explode n
.
add_tactic_doc
{ name := "#explode",
category := doc_category.cmd,
decl_names := [`tactic.explode_cmd],
tags := ["proof display"] }
end tactic
|
de0e3f3a0f10c1f928908aa56f8c04ab977c7cee | c9e78e68dc955b2325401aec3a6d3240cd8b83f4 | /src/property_catalogue/LTL/sat/absent.lean | 4bef50f239ed73d96a8588b8ba042cb7f668a0fe | [] | 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 | 6,863 | lean | import LTS property_catalogue.LTL.patterns tactic proof_state
open tactic
variable {M : LTS}
variable {α : Type}
namespace absent
namespace globally
lemma by_partition_before_aft {π : path M} (P S : formula M) :
(sat (exist.globally S) π ) → (sat (absent.before P S) π) → (sat (absent.after P S) π) → (sat (absent.globally P) π) :=
begin
intros H1 H2 H3,
rw absent.globally, rw sat,
rw exist.globally at H1, rw sat at H1,
rw absent.before at H2, iterate 3 {rw sat at H2},
rw absent.after at H3, iterate 3 {rw sat at H3},
simp at *,
cases H1 with k H1,
intro i,
replace H2 := H2 k,
replace H2 := H2 H1,
cases H2 with w H2,
have EM : (i < w) ∨ ¬ (i < w), from em (i<w),
cases EM,
apply H2.2,
assumption,
simp at EM,
replace H3 := H3 w,
cases H2 with L R,
replace H3 := H3 L,
have : ∃ j, i = w + j, from le_iff_exists_add.mp EM,
cases this with j H4, rw H4,
replace H3 := H3 j,
rw path.drop_drop at H3,
assumption,
end
meta def solve_by_partition (tok1 tok2 : expr) (ps : PROOF_STATE α ): tactic (PROOF_STATE α) :=
do
tactic.interactive.apply ``(by_partition_before_aft %%tok1 %%tok2),
return ps
-- t1 ← tok1.log_format, t2 ← tok2.log_format,
-- s.log $ "apply by_partition_before_aft" ++ t1 ++ t2 ++ "\n"
meta def solve (tok : expr) (ps : PROOF_STATE α) : list expr → tactic (PROOF_STATE α)
| [] := return ps
| (h::t) :=
do typ ← infer_type h,
match typ with
| `(sat (absent.before %%tok %%new) %%path):=
do {ps ← solve_by_partition tok new ps, return ps }<|> solve t
| `(sat (absent.after %%tok %%new) %%path) :=
do {ps ← solve_by_partition tok new ps, return ps }<|> solve t
| _ := do solve t
end
end globally
namespace between
theorem absent_between_response {M : LTS} {p : path M} { B I C : formula M} ( A : formula M) :
(sat (responds.globally (C) (A) ) p) ∧
(sat (absent.between (B) (C) (A)) p) ∧
(sat (absent.between (B) (A) (I)) p)→ (sat (absent.between (B) (C) (I)) p) :=
begin rintros ⟨ H1, H2, H3⟩,
intro i,
replace H1 := H1 i,
intro Hcond, cases Hcond with L R,
replace H1 := H1 L,
rw absent.between at H2,
have : ((p.drop i) ⊨ (C & ◆A)), by {rw sat, split,assumption,assumption},
replace H2 := H2 (i) this,
cases H1 with w Hw,
cases R with k Hk,
clear this,
cases H2 with z Hz,
cases Hz with z1 z2,
have : k < z ∨ ¬ (k < z), from or_not,
cases this,
use k,
split, assumption,
intros j Hj,
have fact : j < z, by omega,
replace z2 := z2 j fact, assumption,
simp at this,
have EM : z = k ∨ z < k, by omega,
clear this,
cases EM, use k,
split, assumption, rw ← EM, assumption,
replace H3 := H3 (i+z),
rw ← path.drop_drop at H3,
have help : (((p.drop i).drop z) ⊨ ◆(I)), by {use (k-z),
rw path.drop_drop, rw path.drop_drop,have : i + (z + (k - z)) = i+k, by omega, rw this, rw ← path.drop_drop, assumption,},
have : ( ((p.drop i).drop z) ⊨ (A & ◆I)), by {rw sat, split, assumption, assumption,},
clear help, replace H3 := H3 this,
cases H3 with t Ht,
clear this,
cases Ht with Ht Ht',
rw path.drop_drop at *,
use (z+t),split,
assumption,
intros j Hj,
have : j < z ∨ ¬ (j < z), from or_not,
cases this, replace z2 := z2 j this,
assumption,
simp at this,
have EM' : z = j ∨ z < j, by omega,
cases EM', rw EM' at Hj,
replace Ht' := Ht' 0 _,
rw path.drop_drop at Ht',
rw← EM',
simp at Ht', rw path.drop_drop,assumption,
omega,
clear this,
replace Ht' := Ht' (j-z),
rw path.drop_drop at Ht',
have : (i + z + (j - z)) = (i + j), by omega,
rw this at Ht',
rw path.drop_drop,
apply Ht', omega,
end
theorem foo {M : LTS} {P Q R : formula M} {x : path M} : (x ⊨ R ⇒ (P W Q)) ↔ (x ⊨ R ⇒ (P U Q)) ∨ (x ⊨ R ⇒ ◾ P) :=
begin
split,
intro H,
rw sat at H,
rw sat.weak_until at H,
rw imp_or_distrib at H,
cases H,
right, assumption,
left, assumption,
intro H,
rw sat,
rw sat.weak_until,
cases H,
intro Hr, replace H := H Hr,
right, assumption,
intro Hr, replace H := H Hr,left, assumption,
end
theorem absent_after_between_response {M : LTS} {p : path M} { B I C : formula M} ( A : formula M) :
(sat (responds.globally (C) (A) ) p) ∧
(sat (absent.between (B) (C) (A)) p) ∧
(sat (absent.after_until (B) (A) (I)) p)→ (sat (absent.after_until (B) (C) (I)) p) :=
begin
rintros ⟨H1, H2, H3⟩,
rw after_until,
intro i,
rw foo,
left,
apply absent_between_response A,split,assumption,
split,assumption,
clear H2, clear H1,clear i,
rw after_until at H3,
rw between,
intros i H,
replace H3 := H3 i H,
cases H with L R,
cases H3,
cases R with w Hw, use w, split, assumption,
intros i _,
replace H3 := H3 i, assumption, assumption,
end
meta def solve_by_absent_between_response (A : expr) (ps : PROOF_STATE α): tactic (PROOF_STATE α) :=
do
tactic.interactive.apply ``(absent_between_response %%A),
repeat1 (applyc `and.intro), `[repeat {assumption}],
return ps
meta def solve (ps : PROOF_STATE α) : list expr → tactic (PROOF_STATE α)
| [] := return ps
| (h::t) :=
do typ ← infer_type h,
match typ with
| `(sat (responds.globally %%C %%A) _):=
do {ps ← solve_by_absent_between_response A ps, return ps} <|> solve t
| _ := do solve t
end
end between
namespace after_until
theorem from_absent_between_response {M : LTS} {p : path M} { B I C : formula M} ( A : formula M) :
(sat (responds.globally (C) (A) ) p) ∧
(sat (absent.between (B) (C) (A)) p) ∧
(sat (absent.after_until (B) (A) (I)) p)→ (sat (absent.after_until (B) (C) (I)) p) :=
begin
rintros ⟨H1, H2, H3⟩,
rw after_until,
intro i,
rw between.foo,
left,
apply between.absent_between_response A,split,assumption,
split,assumption,
clear H2, clear H1,clear i,
rw after_until at H3,
rw between,
intros i H,
replace H3 := H3 i H,
cases H with L R,
cases H3,
cases R with w Hw, use w, split, assumption,
intros i _,
replace H3 := H3 i, assumption, assumption,
end
meta def solve_by_absent_between_response (A : expr) (ps : PROOF_STATE α): tactic (PROOF_STATE α) :=
do
tactic.interactive.apply ``(from_absent_between_response %%A),
ps ← ps.log "apply absent.after_until.from_absent_between_response",
let ps := {used := ps.used ++ ["apply absent.after_until.from_absent_between_response"], ..ps},
repeat1 (applyc `and.intro), `[repeat {assumption}],
ps ← ps.log "match_premises",
return {used := ps.used ++ ["match_premises"], ..ps}
meta def solve (ps : PROOF_STATE α) : list expr → tactic (PROOF_STATE α)
| [] := return ps
| (h::t) :=
do typ ← infer_type h,
match typ with
| `(sat (responds.globally %%C %%A) _):=
do {ps ← solve_by_absent_between_response A ps, return ps} <|> solve t
| _ := do solve t
end
end after_until
end absent
|
5fb6e6f1b0c0b374f27a0e0e740f17fee2ad4b02 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/blast5.lean | eda312965ffb1daedb32d4b51cc8b607b0ef8fef | [
"Apache-2.0"
] | permissive | soonhokong/lean | cb8aa01055ffe2af0fb99a16b4cda8463b882cd1 | 38607e3eb57f57f77c0ac114ad169e9e4262e24f | refs/heads/master | 1,611,187,284,081 | 1,450,766,737,000 | 1,476,122,547,000 | 11,513,992 | 2 | 0 | null | 1,401,763,102,000 | 1,374,182,235,000 | C++ | UTF-8 | Lean | false | false | 241 | lean | set_option blast.strategy "preprocess"
example (a b : nat) : a = b → b = a :=
by blast
example (a b c : nat) : a = b → a = c → b = c :=
by blast
example (p : nat → Prop) (a b c : nat) : a = b → a = c → p b → p c :=
by blast
|
38fb8d1eed81aceab18b5b0aed67cff0d270e32a | bb31430994044506fa42fd667e2d556327e18dfe | /src/algebraic_geometry/structure_sheaf.lean | 236af107c5bda2fa14782f6f73fe1e89dbc65c96 | [
"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 | 49,001 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Scott Morrison
-/
import algebraic_geometry.prime_spectrum.basic
import algebra.category.Ring.colimits
import algebra.category.Ring.limits
import topology.sheaves.local_predicate
import ring_theory.localization.at_prime
import ring_theory.subring.basic
/-!
# The structure sheaf on `prime_spectrum R`.
We define the structure sheaf on `Top.of (prime_spectrum R)`, for a commutative ring `R` and prove
basic properties about it. We define this as a subsheaf of the sheaf of dependent functions into the
localizations, cut out by the condition that the function must be locally equal to a ratio of
elements of `R`.
Because the condition "is equal to a fraction" passes to smaller open subsets,
the subset of functions satisfying this condition is automatically a subpresheaf.
Because the condition "is locally equal to a fraction" is local,
it is also a subsheaf.
(It may be helpful to refer back to `topology.sheaves.sheaf_of_functions`,
where we show that dependent functions into any type family form a sheaf,
and also `topology.sheaves.local_predicate`, where we characterise the predicates
which pick out sub-presheaves and sub-sheaves of these sheaves.)
We also set up the ring structure, obtaining
`structure_sheaf R : sheaf CommRing (Top.of (prime_spectrum R))`.
We then construct two basic isomorphisms, relating the structure sheaf to the underlying ring `R`.
First, `structure_sheaf.stalk_iso` gives an isomorphism between the stalk of the structure sheaf
at a point `p` and the localization of `R` at the prime ideal `p`. Second,
`structure_sheaf.basic_open_iso` gives an isomorphism between the structure sheaf on `basic_open f`
and the localization of `R` at the submonoid of powers of `f`.
## References
* [Robin Hartshorne, *Algebraic Geometry*][Har77]
-/
universe u
noncomputable theory
variables (R : Type u) [comm_ring R]
open Top
open topological_space
open category_theory
open opposite
namespace algebraic_geometry
/--
The prime spectrum, just as a topological space.
-/
def prime_spectrum.Top : Top := Top.of (prime_spectrum R)
namespace structure_sheaf
/--
The type family over `prime_spectrum R` consisting of the localization over each point.
-/
@[derive [comm_ring, local_ring]]
def localizations (P : prime_spectrum.Top R) : Type u := localization.at_prime P.as_ideal
instance (P : prime_spectrum.Top R) : inhabited (localizations R P) :=
⟨1⟩
instance (U : opens (prime_spectrum.Top R)) (x : U) :
algebra R (localizations R x) :=
localization.algebra
instance (U : opens (prime_spectrum.Top R)) (x : U) :
is_localization.at_prime (localizations R x) (x : prime_spectrum.Top R).as_ideal :=
localization.is_localization
variables {R}
/--
The predicate saying that a dependent function on an open `U` is realised as a fixed fraction
`r / s` in each of the stalks (which are localizations at various prime ideals).
-/
def is_fraction {U : opens (prime_spectrum.Top R)} (f : Π x : U, localizations R x) : Prop :=
∃ (r s : R), ∀ x : U,
¬ (s ∈ x.1.as_ideal) ∧ f x * algebra_map _ _ s = algebra_map _ _ r
lemma is_fraction.eq_mk' {U : opens (prime_spectrum.Top R)} {f : Π x : U, localizations R x}
(hf : is_fraction f) :
∃ (r s : R) , ∀ x : U, ∃ (hs : s ∉ x.1.as_ideal), f x =
is_localization.mk' (localization.at_prime _) r
(⟨s, hs⟩ : (x : prime_spectrum.Top R).as_ideal.prime_compl) :=
begin
rcases hf with ⟨r, s, h⟩,
refine ⟨r, s, λ x, ⟨(h x).1, (is_localization.mk'_eq_iff_eq_mul.mpr _).symm⟩⟩,
exact (h x).2.symm,
end
variables (R)
/--
The predicate `is_fraction` is "prelocal",
in the sense that if it holds on `U` it holds on any open subset `V` of `U`.
-/
def is_fraction_prelocal : prelocal_predicate (localizations R) :=
{ pred := λ U f, is_fraction f,
res := by { rintro V U i f ⟨r, s, w⟩, exact ⟨r, s, λ x, w (i x)⟩ } }
/--
We will define the structure sheaf as
the subsheaf of all dependent functions in `Π x : U, localizations R x`
consisting of those functions which can locally be expressed as a ratio of
(the images in the localization of) elements of `R`.
Quoting Hartshorne:
For an open set $U ⊆ Spec A$, we define $𝒪(U)$ to be the set of functions
$s : U → ⨆_{𝔭 ∈ U} A_𝔭$, such that $s(𝔭) ∈ A_𝔭$ for each $𝔭$,
and such that $s$ is locally a quotient of elements of $A$:
to be precise, we require that for each $𝔭 ∈ U$, there is a neighborhood $V$ of $𝔭$,
contained in $U$, and elements $a, f ∈ A$, such that for each $𝔮 ∈ V, f ∉ 𝔮$,
and $s(𝔮) = a/f$ in $A_𝔮$.
Now Hartshorne had the disadvantage of not knowing about dependent functions,
so we replace his circumlocution about functions into a disjoint union with
`Π x : U, localizations x`.
-/
def is_locally_fraction : local_predicate (localizations R) :=
(is_fraction_prelocal R).sheafify
@[simp]
lemma is_locally_fraction_pred
{U : opens (prime_spectrum.Top R)} (f : Π x : U, localizations R x) :
(is_locally_fraction R).pred f =
∀ x : U, ∃ (V) (m : x.1 ∈ V) (i : V ⟶ U),
∃ (r s : R), ∀ y : V,
¬ (s ∈ y.1.as_ideal) ∧
f (i y : U) * algebra_map _ _ s = algebra_map _ _ r :=
rfl
/--
The functions satisfying `is_locally_fraction` form a subring.
-/
def sections_subring (U : (opens (prime_spectrum.Top R))ᵒᵖ) :
subring (Π x : unop U, localizations R x) :=
{ carrier := { f | (is_locally_fraction R).pred f },
zero_mem' :=
begin
refine λ x, ⟨unop U, x.2, 𝟙 _, 0, 1, λ y, ⟨_, _⟩⟩,
{ rw ←ideal.ne_top_iff_one, exact y.1.is_prime.1, },
{ simp, },
end,
one_mem' :=
begin
refine λ x, ⟨unop U, x.2, 𝟙 _, 1, 1, λ y, ⟨_, _⟩⟩,
{ rw ←ideal.ne_top_iff_one, exact y.1.is_prime.1, },
{ simp, },
end,
add_mem' :=
begin
intros a b ha hb x,
rcases ha x with ⟨Va, ma, ia, ra, sa, wa⟩,
rcases hb x with ⟨Vb, mb, ib, rb, sb, wb⟩,
refine ⟨Va ⊓ Vb, ⟨ma, mb⟩, opens.inf_le_left _ _ ≫ ia, ra * sb + rb * sa, sa * sb, _⟩,
intro y,
rcases wa (opens.inf_le_left _ _ y) with ⟨nma, wa⟩,
rcases wb (opens.inf_le_right _ _ y) with ⟨nmb, wb⟩,
fsplit,
{ intro H, cases y.1.is_prime.mem_or_mem H; contradiction, },
{ simp only [add_mul, ring_hom.map_add, pi.add_apply, ring_hom.map_mul],
erw [←wa, ←wb],
simp only [mul_assoc],
congr' 2,
rw [mul_comm], refl, }
end,
neg_mem' :=
begin
intros a ha x,
rcases ha x with ⟨V, m, i, r, s, w⟩,
refine ⟨V, m, i, -r, s, _⟩,
intro y,
rcases w y with ⟨nm, w⟩,
fsplit,
{ exact nm, },
{ simp only [ring_hom.map_neg, pi.neg_apply],
erw [←w],
simp only [neg_mul], }
end,
mul_mem' :=
begin
intros a b ha hb x,
rcases ha x with ⟨Va, ma, ia, ra, sa, wa⟩,
rcases hb x with ⟨Vb, mb, ib, rb, sb, wb⟩,
refine ⟨Va ⊓ Vb, ⟨ma, mb⟩, opens.inf_le_left _ _ ≫ ia, ra * rb, sa * sb, _⟩,
intro y,
rcases wa (opens.inf_le_left _ _ y) with ⟨nma, wa⟩,
rcases wb (opens.inf_le_right _ _ y) with ⟨nmb, wb⟩,
fsplit,
{ intro H, cases y.1.is_prime.mem_or_mem H; contradiction, },
{ simp only [pi.mul_apply, ring_hom.map_mul],
erw [←wa, ←wb],
simp only [mul_left_comm, mul_assoc, mul_comm],
refl, }
end, }
end structure_sheaf
open structure_sheaf
/--
The structure sheaf (valued in `Type`, not yet `CommRing`) is the subsheaf consisting of
functions satisfying `is_locally_fraction`.
-/
def structure_sheaf_in_Type : sheaf (Type u) (prime_spectrum.Top R):=
subsheaf_to_Types (is_locally_fraction R)
instance comm_ring_structure_sheaf_in_Type_obj (U : (opens (prime_spectrum.Top R))ᵒᵖ) :
comm_ring ((structure_sheaf_in_Type R).1.obj U) :=
(sections_subring R U).to_comm_ring
open _root_.prime_spectrum
/--
The structure presheaf, valued in `CommRing`, constructed by dressing up the `Type` valued
structure presheaf.
-/
@[simps]
def structure_presheaf_in_CommRing : presheaf CommRing (prime_spectrum.Top R) :=
{ obj := λ U, CommRing.of ((structure_sheaf_in_Type R).1.obj U),
map := λ U V i,
{ to_fun := ((structure_sheaf_in_Type R).1.map i),
map_zero' := rfl,
map_add' := λ x y, rfl,
map_one' := rfl,
map_mul' := λ x y, rfl, }, }
/--
Some glue, verifying that that structure presheaf valued in `CommRing` agrees
with the `Type` valued structure presheaf.
-/
def structure_presheaf_comp_forget :
structure_presheaf_in_CommRing R ⋙ (forget CommRing) ≅ (structure_sheaf_in_Type R).1 :=
nat_iso.of_components
(λ U, iso.refl _)
(by tidy)
open Top.presheaf
/--
The structure sheaf on $Spec R$, valued in `CommRing`.
This is provided as a bundled `SheafedSpace` as `Spec.SheafedSpace R` later.
-/
def Spec.structure_sheaf : sheaf CommRing (prime_spectrum.Top R) :=
⟨structure_presheaf_in_CommRing R,
-- We check the sheaf condition under `forget CommRing`.
(is_sheaf_iff_is_sheaf_comp _ _).mpr
(is_sheaf_of_iso (structure_presheaf_comp_forget R).symm
(structure_sheaf_in_Type R).cond)⟩
open Spec (structure_sheaf)
namespace structure_sheaf
@[simp] lemma res_apply (U V : opens (prime_spectrum.Top R)) (i : V ⟶ U)
(s : (structure_sheaf R).1.obj (op U)) (x : V) :
((structure_sheaf R).1.map i.op s).1 x = (s.1 (i x) : _) :=
rfl
/-
Notation in this comment
X = Spec R
OX = structure sheaf
In the following we construct an isomorphism between OX_p and R_p given any point p corresponding
to a prime ideal in R.
We do this via 8 steps:
1. def const (f g : R) (V) (hv : V ≤ D_g) : OX(V) [for api]
2. def to_open (U) : R ⟶ OX(U)
3. [2] def to_stalk (p : Spec R) : R ⟶ OX_p
4. [2] def to_basic_open (f : R) : R_f ⟶ OX(D_f)
5. [3] def localization_to_stalk (p : Spec R) : R_p ⟶ OX_p
6. def open_to_localization (U) (p) (hp : p ∈ U) : OX(U) ⟶ R_p
7. [6] def stalk_to_fiber_ring_hom (p : Spec R) : OX_p ⟶ R_p
8. [5,7] def stalk_iso (p : Spec R) : OX_p ≅ R_p
In the square brackets we list the dependencies of a construction on the previous steps.
-/
/-- The section of `structure_sheaf R` on an open `U` sending each `x ∈ U` to the element
`f/g` in the localization of `R` at `x`. -/
def const (f g : R) (U : opens (prime_spectrum.Top R))
(hu : ∀ x ∈ U, g ∈ (x : prime_spectrum.Top R).as_ideal.prime_compl) :
(structure_sheaf R).1.obj (op U) :=
⟨λ x, is_localization.mk' _ f ⟨g, hu x x.2⟩,
λ x, ⟨U, x.2, 𝟙 _, f, g, λ y, ⟨hu y y.2, is_localization.mk'_spec _ _ _⟩⟩⟩
@[simp] lemma const_apply (f g : R) (U : opens (prime_spectrum.Top R))
(hu : ∀ x ∈ U, g ∈ (x : prime_spectrum.Top R).as_ideal.prime_compl) (x : U) :
(const R f g U hu).1 x = is_localization.mk' _ f ⟨g, hu x x.2⟩ :=
rfl
lemma const_apply' (f g : R) (U : opens (prime_spectrum.Top R))
(hu : ∀ x ∈ U, g ∈ (x : prime_spectrum.Top R).as_ideal.prime_compl) (x : U)
(hx : g ∈ (as_ideal (x : prime_spectrum.Top R)).prime_compl) :
(const R f g U hu).1 x = is_localization.mk' _ f ⟨g, hx⟩ :=
rfl
lemma exists_const (U) (s : (structure_sheaf R).1.obj (op U)) (x : prime_spectrum.Top R)
(hx : x ∈ U) :
∃ (V : opens (prime_spectrum.Top R)) (hxV : x ∈ V) (i : V ⟶ U) (f g : R) hg,
const R f g V hg = (structure_sheaf R).1.map i.op s :=
let ⟨V, hxV, iVU, f, g, hfg⟩ := s.2 ⟨x, hx⟩ in
⟨V, hxV, iVU, f, g, λ y hyV, (hfg ⟨y, hyV⟩).1, subtype.eq $ funext $ λ y,
is_localization.mk'_eq_iff_eq_mul.2 $ eq.symm $ (hfg y).2⟩
@[simp] lemma res_const (f g : R) (U hu V hv i) :
(structure_sheaf R).1.map i (const R f g U hu) = const R f g V hv :=
rfl
lemma res_const' (f g : R) (V hv) :
(structure_sheaf R).1.map (hom_of_le hv).op (const R f g (basic_open g) (λ _, id)) =
const R f g V hv :=
rfl
lemma const_zero (f : R) (U hu) : const R 0 f U hu = 0 :=
subtype.eq $ funext $ λ x, is_localization.mk'_eq_iff_eq_mul.2 $
by erw [ring_hom.map_zero, subtype.val_eq_coe, subring.coe_zero, pi.zero_apply, zero_mul]
lemma const_self (f : R) (U hu) : const R f f U hu = 1 :=
subtype.eq $ funext $ λ x, is_localization.mk'_self _ _
lemma const_one (U) : const R 1 1 U (λ p _, submonoid.one_mem _) = 1 :=
const_self R 1 U _
lemma const_add (f₁ f₂ g₁ g₂ : R) (U hu₁ hu₂) :
const R f₁ g₁ U hu₁ + const R f₂ g₂ U hu₂ =
const R (f₁ * g₂ + f₂ * g₁) (g₁ * g₂) U (λ x hx, submonoid.mul_mem _ (hu₁ x hx) (hu₂ x hx)) :=
subtype.eq $ funext $ λ x, eq.symm $
by convert is_localization.mk'_add f₁ f₂ ⟨g₁, hu₁ x x.2⟩ ⟨g₂, hu₂ x x.2⟩
lemma const_mul (f₁ f₂ g₁ g₂ : R) (U hu₁ hu₂) :
const R f₁ g₁ U hu₁ * const R f₂ g₂ U hu₂ =
const R (f₁ * f₂) (g₁ * g₂) U (λ x hx, submonoid.mul_mem _ (hu₁ x hx) (hu₂ x hx)) :=
subtype.eq $ funext $ λ x, eq.symm $
by convert is_localization.mk'_mul _ f₁ f₂ ⟨g₁, hu₁ x x.2⟩ ⟨g₂, hu₂ x x.2⟩
lemma const_ext {f₁ f₂ g₁ g₂ : R} {U hu₁ hu₂} (h : f₁ * g₂ = f₂ * g₁) :
const R f₁ g₁ U hu₁ = const R f₂ g₂ U hu₂ :=
subtype.eq $ funext $ λ x, is_localization.mk'_eq_of_eq h.symm
lemma const_congr {f₁ f₂ g₁ g₂ : R} {U hu} (hf : f₁ = f₂) (hg : g₁ = g₂) :
const R f₁ g₁ U hu = const R f₂ g₂ U (hg ▸ hu) :=
by substs hf hg
lemma const_mul_rev (f g : R) (U hu₁ hu₂) :
const R f g U hu₁ * const R g f U hu₂ = 1 :=
by rw [const_mul, const_congr R rfl (mul_comm g f), const_self]
lemma const_mul_cancel (f g₁ g₂ : R) (U hu₁ hu₂) :
const R f g₁ U hu₁ * const R g₁ g₂ U hu₂ = const R f g₂ U hu₂ :=
by { rw [const_mul, const_ext], rw mul_assoc }
lemma const_mul_cancel' (f g₁ g₂ : R) (U hu₁ hu₂) :
const R g₁ g₂ U hu₂ * const R f g₁ U hu₁ = const R f g₂ U hu₂ :=
by rw [mul_comm, const_mul_cancel]
/-- The canonical ring homomorphism interpreting an element of `R` as
a section of the structure sheaf. -/
def to_open (U : opens (prime_spectrum.Top R)) :
CommRing.of R ⟶ (structure_sheaf R).1.obj (op U) :=
{ to_fun := λ f, ⟨λ x, algebra_map R _ f,
λ x, ⟨U, x.2, 𝟙 _, f, 1, λ y, ⟨(ideal.ne_top_iff_one _).1 y.1.2.1,
by { rw [ring_hom.map_one, mul_one], refl } ⟩⟩⟩,
map_one' := subtype.eq $ funext $ λ x, ring_hom.map_one _,
map_mul' := λ f g, subtype.eq $ funext $ λ x, ring_hom.map_mul _ _ _,
map_zero' := subtype.eq $ funext $ λ x, ring_hom.map_zero _,
map_add' := λ f g, subtype.eq $ funext $ λ x, ring_hom.map_add _ _ _ }
@[simp] lemma to_open_res (U V : opens (prime_spectrum.Top R)) (i : V ⟶ U) :
to_open R U ≫ (structure_sheaf R).1.map i.op = to_open R V :=
rfl
@[simp] lemma to_open_apply (U : opens (prime_spectrum.Top R)) (f : R) (x : U) :
(to_open R U f).1 x = algebra_map _ _ f :=
rfl
lemma to_open_eq_const (U : opens (prime_spectrum.Top R)) (f : R) : to_open R U f =
const R f 1 U (λ x _, (ideal.ne_top_iff_one _).1 x.2.1) :=
subtype.eq $ funext $ λ x, eq.symm $ is_localization.mk'_one _ f
/-- The canonical ring homomorphism interpreting an element of `R` as an element of
the stalk of `structure_sheaf R` at `x`. -/
def to_stalk (x : prime_spectrum.Top R) : CommRing.of R ⟶ (structure_sheaf R).presheaf.stalk x :=
(to_open R ⊤ ≫ (structure_sheaf R).presheaf.germ ⟨x, ⟨⟩⟩ : _)
@[simp] lemma to_open_germ (U : opens (prime_spectrum.Top R)) (x : U) :
to_open R U ≫ (structure_sheaf R).presheaf.germ x =
to_stalk R x :=
by { rw [← to_open_res R ⊤ U (hom_of_le le_top : U ⟶ ⊤), category.assoc, presheaf.germ_res], refl }
@[simp] lemma germ_to_open (U : opens (prime_spectrum.Top R)) (x : U) (f : R) :
(structure_sheaf R).presheaf.germ x (to_open R U f) = to_stalk R x f :=
by { rw ← to_open_germ, refl }
lemma germ_to_top (x : prime_spectrum.Top R) (f : R) :
(structure_sheaf R).presheaf.germ (⟨x, trivial⟩ : (⊤ : opens (prime_spectrum.Top R)))
(to_open R ⊤ f) =
to_stalk R x f :=
rfl
lemma is_unit_to_basic_open_self (f : R) : is_unit (to_open R (basic_open f) f) :=
is_unit_of_mul_eq_one _ (const R 1 f (basic_open f) (λ _, id)) $
by rw [to_open_eq_const, const_mul_rev]
lemma is_unit_to_stalk (x : prime_spectrum.Top R) (f : x.as_ideal.prime_compl) :
is_unit (to_stalk R x (f : R)) :=
by { erw ← germ_to_open R (basic_open (f : R)) ⟨x, f.2⟩ (f : R),
exact ring_hom.is_unit_map _ (is_unit_to_basic_open_self R f) }
/-- The canonical ring homomorphism from the localization of `R` at `p` to the stalk
of the structure sheaf at the point `p`. -/
def localization_to_stalk (x : prime_spectrum.Top R) :
CommRing.of (localization.at_prime x.as_ideal) ⟶ (structure_sheaf R).presheaf.stalk x :=
show localization.at_prime x.as_ideal →+* _, from
is_localization.lift (is_unit_to_stalk R x)
@[simp] lemma localization_to_stalk_of (x : prime_spectrum.Top R) (f : R) :
localization_to_stalk R x (algebra_map _ (localization _) f) = to_stalk R x f :=
is_localization.lift_eq _ f
@[simp] lemma localization_to_stalk_mk' (x : prime_spectrum.Top R) (f : R)
(s : (as_ideal x).prime_compl) :
localization_to_stalk R x (is_localization.mk' _ f s : localization _) =
(structure_sheaf R).presheaf.germ (⟨x, s.2⟩ : basic_open (s : R))
(const R f s (basic_open s) (λ _, id)) :=
(is_localization.lift_mk'_spec _ _ _ _).2 $
by erw [← germ_to_open R (basic_open s) ⟨x, s.2⟩, ← germ_to_open R (basic_open s) ⟨x, s.2⟩,
← ring_hom.map_mul, to_open_eq_const, to_open_eq_const, const_mul_cancel']
/-- The ring homomorphism that takes a section of the structure sheaf of `R` on the open set `U`,
implemented as a subtype of dependent functions to localizations at prime ideals, and evaluates
the section on the point corresponding to a given prime ideal. -/
def open_to_localization (U : opens (prime_spectrum.Top R)) (x : prime_spectrum.Top R)
(hx : x ∈ U) :
(structure_sheaf R).1.obj (op U) ⟶ CommRing.of (localization.at_prime x.as_ideal) :=
{ to_fun := λ s, (s.1 ⟨x, hx⟩ : _),
map_one' := rfl,
map_mul' := λ _ _, rfl,
map_zero' := rfl,
map_add' := λ _ _, rfl }
@[simp] lemma coe_open_to_localization (U : opens (prime_spectrum.Top R)) (x : prime_spectrum.Top R)
(hx : x ∈ U) :
(open_to_localization R U x hx :
(structure_sheaf R).1.obj (op U) → localization.at_prime x.as_ideal) =
(λ s, (s.1 ⟨x, hx⟩ : _)) :=
rfl
lemma open_to_localization_apply (U : opens (prime_spectrum.Top R)) (x : prime_spectrum.Top R)
(hx : x ∈ U)
(s : (structure_sheaf R).1.obj (op U)) :
open_to_localization R U x hx s = (s.1 ⟨x, hx⟩ : _) :=
rfl
/-- The ring homomorphism from the stalk of the structure sheaf of `R` at a point corresponding to
a prime ideal `p` to the localization of `R` at `p`,
formed by gluing the `open_to_localization` maps. -/
def stalk_to_fiber_ring_hom (x : prime_spectrum.Top R) :
(structure_sheaf R).presheaf.stalk x ⟶ CommRing.of (localization.at_prime x.as_ideal) :=
limits.colimit.desc (((open_nhds.inclusion x).op) ⋙ (structure_sheaf R).1)
{ X := _,
ι :=
{ app := λ U, open_to_localization R ((open_nhds.inclusion _).obj (unop U)) x (unop U).2, } }
@[simp] lemma germ_comp_stalk_to_fiber_ring_hom (U : opens (prime_spectrum.Top R)) (x : U) :
(structure_sheaf R).presheaf.germ x ≫ stalk_to_fiber_ring_hom R x =
open_to_localization R U x x.2 :=
limits.colimit.ι_desc _ _
@[simp] lemma stalk_to_fiber_ring_hom_germ' (U : opens (prime_spectrum.Top R))
(x : prime_spectrum.Top R) (hx : x ∈ U) (s : (structure_sheaf R).1.obj (op U)) :
stalk_to_fiber_ring_hom R x ((structure_sheaf R).presheaf.germ ⟨x, hx⟩ s) = (s.1 ⟨x, hx⟩ : _) :=
ring_hom.ext_iff.1 (germ_comp_stalk_to_fiber_ring_hom R U ⟨x, hx⟩ : _) s
@[simp] lemma stalk_to_fiber_ring_hom_germ (U : opens (prime_spectrum.Top R)) (x : U)
(s : (structure_sheaf R).1.obj (op U)) :
stalk_to_fiber_ring_hom R x ((structure_sheaf R).presheaf.germ x s) = s.1 x :=
by { cases x, exact stalk_to_fiber_ring_hom_germ' R U _ _ _ }
@[simp] lemma to_stalk_comp_stalk_to_fiber_ring_hom (x : prime_spectrum.Top R) :
to_stalk R x ≫ stalk_to_fiber_ring_hom R x = (algebra_map _ _ : R →+* localization _) :=
by { erw [to_stalk, category.assoc, germ_comp_stalk_to_fiber_ring_hom], refl }
@[simp] lemma stalk_to_fiber_ring_hom_to_stalk (x : prime_spectrum.Top R) (f : R) :
stalk_to_fiber_ring_hom R x (to_stalk R x f) = algebra_map _ (localization _) f :=
ring_hom.ext_iff.1 (to_stalk_comp_stalk_to_fiber_ring_hom R x) _
/-- The ring isomorphism between the stalk of the structure sheaf of `R` at a point `p`
corresponding to a prime ideal in `R` and the localization of `R` at `p`. -/
@[simps] def stalk_iso (x : prime_spectrum.Top R) :
(structure_sheaf R).presheaf.stalk x ≅ CommRing.of (localization.at_prime x.as_ideal) :=
{ hom := stalk_to_fiber_ring_hom R x,
inv := localization_to_stalk R x,
hom_inv_id' := (structure_sheaf R).presheaf.stalk_hom_ext $ λ U hxU,
begin
ext s, simp only [comp_apply], rw [id_apply, stalk_to_fiber_ring_hom_germ'],
obtain ⟨V, hxV, iVU, f, g, hg, hs⟩ := exists_const _ _ s x hxU,
erw [← res_apply R U V iVU s ⟨x, hxV⟩, ← hs, const_apply, localization_to_stalk_mk'],
refine (structure_sheaf R).presheaf.germ_ext V hxV (hom_of_le hg) iVU _,
erw [← hs, res_const']
end,
inv_hom_id' := @is_localization.ring_hom_ext R _ x.as_ideal.prime_compl
(localization.at_prime x.as_ideal) _ _ (localization.at_prime x.as_ideal) _ _
(ring_hom.comp (stalk_to_fiber_ring_hom R x) (localization_to_stalk R x))
(ring_hom.id (localization.at_prime _)) $
by { ext f, simp only [ring_hom.comp_apply, ring_hom.id_apply, localization_to_stalk_of,
stalk_to_fiber_ring_hom_to_stalk] } }
instance (x : prime_spectrum R) : is_iso (stalk_to_fiber_ring_hom R x) :=
is_iso.of_iso (stalk_iso R x)
instance (x : prime_spectrum R) : is_iso (localization_to_stalk R x) :=
is_iso.of_iso (stalk_iso R x).symm
@[simp, reassoc] lemma stalk_to_fiber_ring_hom_localization_to_stalk (x : prime_spectrum.Top R) :
stalk_to_fiber_ring_hom R x ≫ localization_to_stalk R x = 𝟙 _ :=
(stalk_iso R x).hom_inv_id
@[simp, reassoc] lemma localization_to_stalk_stalk_to_fiber_ring_hom (x : prime_spectrum.Top R) :
localization_to_stalk R x ≫ stalk_to_fiber_ring_hom R x = 𝟙 _ :=
(stalk_iso R x).inv_hom_id
/-- The canonical ring homomorphism interpreting `s ∈ R_f` as a section of the structure sheaf
on the basic open defined by `f ∈ R`. -/
def to_basic_open (f : R) : localization.away f →+*
(structure_sheaf R).1.obj (op $ basic_open f) :=
is_localization.away.lift f (is_unit_to_basic_open_self R f)
@[simp] lemma to_basic_open_mk' (s f : R) (g : submonoid.powers s) :
to_basic_open R s (is_localization.mk' (localization.away s) f g) =
const R f g (basic_open s) (λ x hx, submonoid.powers_subset hx g.2) :=
(is_localization.lift_mk'_spec _ _ _ _).2 $
by rw [to_open_eq_const, to_open_eq_const, const_mul_cancel']
@[simp] lemma localization_to_basic_open (f : R) :
ring_hom.comp (to_basic_open R f) (algebra_map R (localization.away f)) =
to_open R (basic_open f) :=
ring_hom.ext $ λ g,
by rw [to_basic_open, is_localization.away.lift, ring_hom.comp_apply, is_localization.lift_eq]
@[simp] lemma to_basic_open_to_map (s f : R) :
to_basic_open R s (algebra_map R (localization.away s) f) =
const R f 1 (basic_open s) (λ _ _, submonoid.one_mem _) :=
(is_localization.lift_eq _ _).trans $ to_open_eq_const _ _ _
-- The proof here follows the argument in Hartshorne's Algebraic Geometry, Proposition II.2.2.
lemma to_basic_open_injective (f : R) : function.injective (to_basic_open R f) :=
begin
intros s t h_eq,
obtain ⟨a, ⟨b, hb⟩, rfl⟩ := is_localization.mk'_surjective (submonoid.powers f) s,
obtain ⟨c, ⟨d, hd⟩, rfl⟩ := is_localization.mk'_surjective (submonoid.powers f) t,
simp only [to_basic_open_mk'] at h_eq,
rw is_localization.eq,
-- We know that the fractions `a/b` and `c/d` are equal as sections of the structure sheaf on
-- `basic_open f`. We need to show that they agree as elements in the localization of `R` at `f`.
-- This amounts showing that `a * d * r = c * b * r`, for some power `r = f ^ n` of `f`.
-- We define `I` as the ideal of *all* elements `r` satisfying the above equation.
let I : ideal R :=
{ carrier := {r : R | a * d * r = c * b * r},
zero_mem' := by simp only [set.mem_set_of_eq, mul_zero],
add_mem' := λ r₁ r₂ hr₁ hr₂, by { dsimp at hr₁ hr₂ ⊢, simp only [mul_add, hr₁, hr₂] },
smul_mem' := λ r₁ r₂ hr₂, by { dsimp at hr₂ ⊢, simp only [mul_comm r₁ r₂, ← mul_assoc, hr₂] }},
-- Our claim now reduces to showing that `f` is contained in the radical of `I`
suffices : f ∈ I.radical,
{ cases this with n hn,
exact ⟨⟨f ^ n, n, rfl⟩, hn⟩ },
rw [← vanishing_ideal_zero_locus_eq_radical, mem_vanishing_ideal],
intros p hfp,
contrapose hfp,
rw [mem_zero_locus, set.not_subset],
have := congr_fun (congr_arg subtype.val h_eq) ⟨p,hfp⟩,
rw [const_apply, const_apply, is_localization.eq] at this,
cases this with r hr,
exact ⟨r.1, hr, r.2⟩
end
/-
Auxiliary lemma for surjectivity of `to_basic_open`.
Every section can locally be represented on basic opens `basic_opens g` as a fraction `f/g`
-/
lemma locally_const_basic_open (U : opens (prime_spectrum.Top R))
(s : (structure_sheaf R).1.obj (op U)) (x : U) :
∃ (f g : R) (i : basic_open g ⟶ U), x.1 ∈ basic_open g ∧
const R f g (basic_open g) (λ y hy, hy) = (structure_sheaf R).1.map i.op s :=
begin
-- First, any section `s` can be represented as a fraction `f/g` on some open neighborhood of `x`
-- and we may pass to a `basic_open h`, since these form a basis
obtain ⟨V, (hxV : x.1 ∈ V.1), iVU, f, g, (hVDg : V ⊆ basic_open g), s_eq⟩ :=
exists_const R U s x.1 x.2,
obtain ⟨_, ⟨h, rfl⟩, hxDh, (hDhV : basic_open h ⊆ V)⟩ :=
is_topological_basis_basic_opens.exists_subset_of_mem_open hxV V.2,
-- The problem is of course, that `g` and `h` don't need to coincide.
-- But, since `basic_open h ≤ basic_open g`, some power of `h` must be a multiple of `g`
cases (basic_open_le_basic_open_iff h g).mp (set.subset.trans hDhV hVDg) with n hn,
-- Actually, we will need a *nonzero* power of `h`.
-- This is because we will need the equality `basic_open (h ^ n) = basic_open h`, which only
-- holds for a nonzero power `n`. We therefore artificially increase `n` by one.
replace hn := ideal.mul_mem_left (ideal.span {g}) h hn,
rw [← pow_succ, ideal.mem_span_singleton'] at hn,
cases hn with c hc,
have basic_opens_eq := basic_open_pow h (n+1) (by linarith),
have i_basic_open := eq_to_hom basic_opens_eq ≫ hom_of_le hDhV,
-- We claim that `(f * c) / h ^ (n+1)` is our desired representation
use [f * c, h ^ (n+1), i_basic_open ≫ iVU, (basic_opens_eq.symm.le : _) hxDh],
rw [op_comp, functor.map_comp, comp_apply, ← s_eq, res_const],
-- Note that the last rewrite here generated an additional goal, which was a parameter
-- of `res_const`. We prove this goal first
swap,
{ intros y hy,
rw basic_opens_eq at hy,
exact (set.subset.trans hDhV hVDg : _) hy },
-- All that is left is a simple calculation
apply const_ext,
rw [mul_assoc f c g, hc],
end
/-
Auxiliary lemma for surjectivity of `to_basic_open`.
A local representation of a section `s` as fractions `a i / h i` on finitely many basic opens
`basic_open (h i)` can be "normalized" in such a way that `a i * h j = h i * a j` for all `i, j`
-/
lemma normalize_finite_fraction_representation (U : opens (prime_spectrum.Top R))
(s : (structure_sheaf R).1.obj (op U)) {ι : Type*} (t : finset ι) (a h : ι → R)
(iDh : Π i : ι, basic_open (h i) ⟶ U) (h_cover : U.1 ⊆ ⋃ i ∈ t, (basic_open (h i)).1)
(hs : ∀ i : ι, const R (a i) (h i) (basic_open (h i)) (λ y hy, hy) =
(structure_sheaf R).1.map (iDh i).op s) :
∃ (a' h' : ι → R) (iDh' : Π i : ι, (basic_open (h' i)) ⟶ U),
(U.1 ⊆ ⋃ i ∈ t, (basic_open (h' i)).1) ∧
(∀ i j ∈ t, a' i * h' j = h' i * a' j) ∧
(∀ i ∈ t, (structure_sheaf R).1.map (iDh' i).op s =
const R (a' i) (h' i) (basic_open (h' i)) (λ y hy, hy)) :=
begin
-- First we show that the fractions `(a i * h j) / (h i * h j)` and `(h i * a j) / (h i * h j)`
-- coincide in the localization of `R` at `h i * h j`
have fractions_eq : ∀ (i j : ι),
is_localization.mk' (localization.away _) (a i * h j) ⟨h i * h j, submonoid.mem_powers _⟩ =
is_localization.mk' _ (h i * a j) ⟨h i * h j, submonoid.mem_powers _⟩,
{ intros i j,
let D := basic_open (h i * h j),
let iDi : D ⟶ basic_open (h i) := hom_of_le (basic_open_mul_le_left _ _),
let iDj : D ⟶ basic_open (h j) := hom_of_le (basic_open_mul_le_right _ _),
-- Crucially, we need injectivity of `to_basic_open`
apply to_basic_open_injective R (h i * h j),
rw [to_basic_open_mk', to_basic_open_mk'],
simp only [set_like.coe_mk],
-- Here, both sides of the equation are equal to a restriction of `s`
transitivity,
convert congr_arg ((structure_sheaf R).1.map iDj.op) (hs j).symm using 1,
convert congr_arg ((structure_sheaf R).1.map iDi.op) (hs i) using 1, swap,
all_goals { rw res_const, apply const_ext, ring },
-- The remaining two goals were generated during the rewrite of `res_const`
-- These can be solved immediately
exacts [basic_open_mul_le_right _ _, basic_open_mul_le_left _ _] },
-- From the equality in the localization, we obtain for each `(i,j)` some power `(h i * h j) ^ n`
-- which equalizes `a i * h j` and `h i * a j`
have exists_power : ∀ (i j : ι), ∃ n : ℕ,
a i * h j * (h i * h j) ^ n = h i * a j * (h i * h j) ^ n,
{ intros i j,
obtain ⟨⟨c, n, rfl⟩, hc⟩ := is_localization.eq.mp (fractions_eq i j),
use (n+1),
rw pow_succ,
dsimp at hc,
convert hc using 1 ; ring },
let n := λ (p : ι × ι), (exists_power p.1 p.2).some,
have n_spec := λ (p : ι × ι), (exists_power p.fst p.snd).some_spec,
-- We need one power `(h i * h j) ^ N` that works for *all* pairs `(i,j)`
-- Since there are only finitely many indices involved, we can pick the supremum.
let N := (t ×ˢ t).sup n,
have basic_opens_eq : ∀ i : ι, basic_open ((h i) ^ (N+1)) = basic_open (h i) :=
λ i, basic_open_pow _ _ (by linarith),
-- Expanding the fraction `a i / h i` by the power `(h i) ^ N` gives the desired normalization
refine ⟨(λ i, a i * (h i) ^ N), (λ i, (h i) ^ (N + 1)),
(λ i, eq_to_hom (basic_opens_eq i) ≫ iDh i), _, _, _⟩,
{ simpa only [basic_opens_eq] using h_cover },
{ intros i hi j hj,
-- Here we need to show that our new fractions `a i / h i` satisfy the normalization condition
-- Of course, the power `N` we used to expand the fractions might be bigger than the power
-- `n (i, j)` which was originally chosen. We denote their difference by `k`
have n_le_N : n (i, j) ≤ N := finset.le_sup (finset.mem_product.mpr ⟨hi, hj⟩),
cases nat.le.dest n_le_N with k hk,
simp only [← hk, pow_add, pow_one],
-- To accommodate for the difference `k`, we multiply both sides of the equation `n_spec (i, j)`
-- by `(h i * h j) ^ k`
convert congr_arg (λ z, z * (h i * h j) ^ k) (n_spec (i, j)) using 1 ;
{ simp only [n, mul_pow], ring } },
-- Lastly, we need to show that the new fractions still represent our original `s`
intros i hi,
rw [op_comp, functor.map_comp, comp_apply, ← hs, res_const],
-- additional goal spit out by `res_const`
swap, exact (basic_opens_eq i).le,
apply const_ext,
rw pow_succ,
ring
end
open_locale classical
open_locale big_operators
-- The proof here follows the argument in Hartshorne's Algebraic Geometry, Proposition II.2.2.
lemma to_basic_open_surjective (f : R) : function.surjective (to_basic_open R f) :=
begin
intro s,
-- In this proof, `basic_open f` will play two distinct roles: Firstly, it is an open set in the
-- prime spectrum. Secondly, it is used as an indexing type for various families of objects
-- (open sets, ring elements, ...). In order to make the distinction clear, we introduce a type
-- alias `ι` that is used whenever we want think of it as an indexing type.
let ι : Type u := basic_open f,
-- First, we pick some cover of basic opens, on which we can represent `s` as a fraction
choose a' h' iDh' hxDh' s_eq' using locally_const_basic_open R (basic_open f) s,
-- Since basic opens are compact, we can pass to a finite subcover
obtain ⟨t, ht_cover'⟩ := (is_compact_basic_open f).elim_finite_subcover
(λ (i : ι), (basic_open (h' i)).1) (λ i, is_open_basic_open) (λ x hx, _),
swap,
{ -- Here, we need to show that our basic opens actually form a cover of `basic_open f`
rw set.mem_Union,
exact ⟨⟨x,hx⟩, hxDh' ⟨x, hx⟩⟩ },
-- We use the normalization lemma from above to obtain the relation `a i * h j = h i * a j`
obtain ⟨a, h, iDh, ht_cover, ah_ha, s_eq⟩ := normalize_finite_fraction_representation R
(basic_open f) s t a' h' iDh' ht_cover' s_eq',
clear s_eq' iDh' hxDh' ht_cover' a' h',
-- Next we show that some power of `f` is a linear combination of the `h i`
obtain ⟨n, hn⟩ : f ∈ (ideal.span (h '' ↑t)).radical,
{ rw [← vanishing_ideal_zero_locus_eq_radical, zero_locus_span],
simp_rw [subtype.val_eq_coe, basic_open_eq_zero_locus_compl] at ht_cover,
rw set.compl_subset_comm at ht_cover, -- Why doesn't `simp_rw` do this?
simp_rw [set.compl_Union, compl_compl, ← zero_locus_Union, ← finset.set_bUnion_coe,
← set.image_eq_Union ] at ht_cover,
apply vanishing_ideal_anti_mono ht_cover,
exact subset_vanishing_ideal_zero_locus {f} (set.mem_singleton f) },
replace hn := ideal.mul_mem_left _ f hn,
erw [←pow_succ, finsupp.mem_span_image_iff_total] at hn,
rcases hn with ⟨b, b_supp, hb⟩,
rw finsupp.total_apply_of_mem_supported R b_supp at hb,
dsimp at hb,
-- Finally, we have all the ingredients.
-- We claim that our preimage is given by `(∑ (i : ι) in t, b i * a i) / f ^ (n+1)`
use is_localization.mk' (localization.away f) (∑ (i : ι) in t, b i * a i)
(⟨f ^ (n+1), n+1, rfl⟩ : submonoid.powers _),
rw to_basic_open_mk',
-- Since the structure sheaf is a sheaf, we can show the desired equality locally.
-- Annoyingly, `sheaf.eq_of_locally_eq` requires an open cover indexed by a *type*, so we need to
-- coerce our finset `t` to a type first.
let tt := ((t : set (basic_open f)) : Type u),
apply (structure_sheaf R).eq_of_locally_eq'
(λ i : tt, basic_open (h i)) (basic_open f) (λ i : tt, iDh i),
{ -- This feels a little redundant, since already have `ht_cover` as a hypothesis
-- Unfortunately, `ht_cover` uses a bounded union over the set `t`, while here we have the
-- Union indexed by the type `tt`, so we need some boilerplate to translate one to the other
intros x hx,
erw topological_space.opens.mem_supr,
have := ht_cover hx,
rw [← finset.set_bUnion_coe, set.mem_Union₂] at this,
rcases this with ⟨i, i_mem, x_mem⟩,
use [i, i_mem] },
rintro ⟨i, hi⟩,
dsimp,
change (structure_sheaf R).1.map _ _ = (structure_sheaf R).1.map _ _,
rw [s_eq i hi, res_const],
-- Again, `res_const` spits out an additional goal
swap,
{ intros y hy,
change y ∈ basic_open (f ^ (n+1)),
rw basic_open_pow f (n+1) (by linarith),
exact (le_of_hom (iDh i) : _) hy },
-- The rest of the proof is just computation
apply const_ext,
rw [← hb, finset.sum_mul, finset.mul_sum],
apply finset.sum_congr rfl,
intros j hj,
rw [mul_assoc, ah_ha j hj i hi],
ring
end
instance is_iso_to_basic_open (f : R) : is_iso (show CommRing.of _ ⟶ _, from to_basic_open R f) :=
begin
haveI : is_iso ((forget CommRing).map (show CommRing.of _ ⟶ _, from to_basic_open R f)) :=
(is_iso_iff_bijective _).mpr ⟨to_basic_open_injective R f, to_basic_open_surjective R f⟩,
exact is_iso_of_reflects_iso _ (forget CommRing),
end
/-- The ring isomorphism between the structure sheaf on `basic_open f` and the localization of `R`
at the submonoid of powers of `f`. -/
def basic_open_iso (f : R) : (structure_sheaf R).1.obj (op (basic_open f)) ≅
CommRing.of (localization.away f) :=
(as_iso (show CommRing.of _ ⟶ _, from to_basic_open R f)).symm
instance stalk_algebra (p : prime_spectrum R) : algebra R ((structure_sheaf R).presheaf.stalk p) :=
(to_stalk R p).to_algebra
@[simp] lemma stalk_algebra_map (p : prime_spectrum R) (r : R) :
algebra_map R ((structure_sheaf R).presheaf.stalk p) r = to_stalk R p r := rfl
/-- Stalk of the structure sheaf at a prime p as localization of R -/
instance is_localization.to_stalk (p : prime_spectrum R) :
is_localization.at_prime ((structure_sheaf R).presheaf.stalk p) p.as_ideal :=
begin
convert (is_localization.is_localization_iff_of_ring_equiv _ (stalk_iso R p).symm
.CommRing_iso_to_ring_equiv).mp localization.is_localization,
apply algebra.algebra_ext,
intro _,
rw stalk_algebra_map,
congr' 1,
erw iso.eq_comp_inv,
exact to_stalk_comp_stalk_to_fiber_ring_hom R p,
end
instance open_algebra (U : (opens (prime_spectrum R))ᵒᵖ) :
algebra R ((structure_sheaf R).val.obj U) :=
(to_open R (unop U)).to_algebra
@[simp] lemma open_algebra_map (U : (opens (prime_spectrum R))ᵒᵖ) (r : R) :
algebra_map R ((structure_sheaf R).val.obj U) r = to_open R (unop U) r := rfl
/-- Sections of the structure sheaf of Spec R on a basic open as localization of R -/
instance is_localization.to_basic_open (r : R) :
is_localization.away r ((structure_sheaf R).val.obj (op $ basic_open r)) :=
begin
convert (is_localization.is_localization_iff_of_ring_equiv _ (basic_open_iso R r).symm
.CommRing_iso_to_ring_equiv).mp localization.is_localization,
apply algebra.algebra_ext,
intro x,
congr' 1,
exact (localization_to_basic_open R r).symm
end
instance to_basic_open_epi (r : R) : epi (to_open R (basic_open r)) :=
⟨λ S f g h, by { refine is_localization.ring_hom_ext _ _,
swap 5, exact is_localization.to_basic_open R r, exact h }⟩
@[elementwise] lemma to_global_factors : to_open R ⊤ =
CommRing.of_hom (algebra_map R (localization.away (1 : R))) ≫ to_basic_open R (1 : R) ≫
(structure_sheaf R).1.map (eq_to_hom (basic_open_one.symm)).op :=
begin
rw ← category.assoc,
change to_open R ⊤ = (to_basic_open R 1).comp _ ≫ _,
unfold CommRing.of_hom,
rw [localization_to_basic_open R, to_open_res],
end
instance is_iso_to_global : is_iso (to_open R ⊤) :=
begin
let hom := CommRing.of_hom (algebra_map R (localization.away (1 : R))),
haveI : is_iso hom := is_iso.of_iso
((is_localization.at_one R (localization.away (1 : R))).to_ring_equiv.to_CommRing_iso),
rw to_global_factors R,
apply_instance
end
/-- The ring isomorphism between the ring `R` and the global sections `Γ(X, 𝒪ₓ)`. -/
@[simps {rhs_md := tactic.transparency.semireducible}]
def global_sections_iso : CommRing.of R ≅ (structure_sheaf R).1.obj (op ⊤) :=
as_iso (to_open R ⊤)
@[simp] lemma global_sections_iso_hom (R : CommRing) :
(global_sections_iso R).hom = to_open R ⊤ := rfl
@[simp, reassoc, elementwise]
lemma to_stalk_stalk_specializes {R : Type*} [comm_ring R]
{x y : prime_spectrum R} (h : x ⤳ y) :
to_stalk R y ≫ (structure_sheaf R).presheaf.stalk_specializes h = to_stalk R x :=
by { dsimp[to_stalk], simpa [-to_open_germ], }
@[simp, reassoc, elementwise]
lemma localization_to_stalk_stalk_specializes {R : Type*} [comm_ring R]
{x y : prime_spectrum R} (h : x ⤳ y) :
structure_sheaf.localization_to_stalk R y ≫ (structure_sheaf R).presheaf.stalk_specializes h =
CommRing.of_hom (prime_spectrum.localization_map_of_specializes h) ≫
structure_sheaf.localization_to_stalk R x :=
begin
apply is_localization.ring_hom_ext y.as_ideal.prime_compl,
any_goals { dsimp, apply_instance },
erw ring_hom.comp_assoc,
conv_rhs { erw ring_hom.comp_assoc },
dsimp [CommRing.of_hom, localization_to_stalk, prime_spectrum.localization_map_of_specializes],
rw [is_localization.lift_comp, is_localization.lift_comp, is_localization.lift_comp],
exact to_stalk_stalk_specializes h
end
@[simp, reassoc, elementwise]
lemma stalk_specializes_stalk_to_fiber {R : Type*} [comm_ring R]
{x y : prime_spectrum R} (h : x ⤳ y) :
(structure_sheaf R).presheaf.stalk_specializes h ≫ structure_sheaf.stalk_to_fiber_ring_hom R x =
structure_sheaf.stalk_to_fiber_ring_hom R y ≫
prime_spectrum.localization_map_of_specializes h :=
begin
change _ ≫ (structure_sheaf.stalk_iso R x).hom = (structure_sheaf.stalk_iso R y).hom ≫ _,
rw [← iso.eq_comp_inv, category.assoc, ← iso.inv_comp_eq],
exact localization_to_stalk_stalk_specializes h,
end
section comap
variables {R} {S : Type u} [comm_ring S] {P : Type u} [comm_ring P]
/--
Given a ring homomorphism `f : R →+* S`, an open set `U` of the prime spectrum of `R` and an open
set `V` of the prime spectrum of `S`, such that `V ⊆ (comap f) ⁻¹' U`, we can push a section `s`
on `U` to a section on `V`, by composing with `localization.local_ring_hom _ _ f` from the left and
`comap f` from the right. Explicitly, if `s` evaluates on `comap f p` to `a / b`, its image on `V`
evaluates on `p` to `f(a) / f(b)`.
At the moment, we work with arbitrary dependent functions `s : Π x : U, localizations R x`. Below,
we prove the predicate `is_locally_fraction` is preserved by this map, hence it can be extended to
a morphism between the structure sheaves of `R` and `S`.
-/
def comap_fun (f : R →+* S) (U : opens (prime_spectrum.Top R))
(V : opens (prime_spectrum.Top S)) (hUV : V.1 ⊆ (prime_spectrum.comap f) ⁻¹' U.1)
(s : Π x : U, localizations R x) (y : V) : localizations S y :=
localization.local_ring_hom (prime_spectrum.comap f y.1).as_ideal _ f rfl
(s ⟨(prime_spectrum.comap f y.1), hUV y.2⟩ : _)
lemma comap_fun_is_locally_fraction (f : R →+* S)
(U : opens (prime_spectrum.Top R)) (V : opens (prime_spectrum.Top S))
(hUV : V.1 ⊆ (prime_spectrum.comap f) ⁻¹' U.1) (s : Π x : U, localizations R x)
(hs : (is_locally_fraction R).to_prelocal_predicate.pred s) :
(is_locally_fraction S).to_prelocal_predicate.pred (comap_fun f U V hUV s) :=
begin
rintro ⟨p, hpV⟩,
-- Since `s` is locally fraction, we can find a neighborhood `W` of `prime_spectrum.comap f p`
-- in `U`, such that `s = a / b` on `W`, for some ring elements `a, b : R`.
rcases hs ⟨prime_spectrum.comap f p, hUV hpV⟩ with ⟨W, m, iWU, a, b, h_frac⟩,
-- We claim that we can write our new section as the fraction `f a / f b` on the neighborhood
-- `(comap f) ⁻¹ W ⊓ V` of `p`.
refine ⟨opens.comap (comap f) W ⊓ V, ⟨m, hpV⟩, opens.inf_le_right _ _, f a, f b, _⟩,
rintro ⟨q, ⟨hqW, hqV⟩⟩,
specialize h_frac ⟨prime_spectrum.comap f q, hqW⟩,
refine ⟨h_frac.1, _⟩,
dsimp only [comap_fun],
erw [← localization.local_ring_hom_to_map ((prime_spectrum.comap f q).as_ideal),
← ring_hom.map_mul, h_frac.2, localization.local_ring_hom_to_map],
refl,
end
/--
For a ring homomorphism `f : R →+* S` and open sets `U` and `V` of the prime spectra of `R` and
`S` such that `V ⊆ (comap f) ⁻¹ U`, the induced ring homomorphism from the structure sheaf of `R`
at `U` to the structure sheaf of `S` at `V`.
Explicitly, this map is given as follows: For a point `p : V`, if the section `s` evaluates on `p`
to the fraction `a / b`, its image on `V` evaluates on `p` to the fraction `f(a) / f(b)`.
-/
def comap (f : R →+* S) (U : opens (prime_spectrum.Top R))
(V : opens (prime_spectrum.Top S)) (hUV : V.1 ⊆ (prime_spectrum.comap f) ⁻¹' U.1) :
(structure_sheaf R).1.obj (op U) →+* (structure_sheaf S).1.obj (op V) :=
{ to_fun := λ s, ⟨comap_fun f U V hUV s.1, comap_fun_is_locally_fraction f U V hUV s.1 s.2⟩,
map_one' := subtype.ext $ funext $ λ p, by
{ rw [subtype.coe_mk, subtype.val_eq_coe, comap_fun, (sections_subring R (op U)).coe_one,
pi.one_apply, ring_hom.map_one], refl },
map_zero' := subtype.ext $ funext $ λ p, by
{ rw [subtype.coe_mk, subtype.val_eq_coe, comap_fun, (sections_subring R (op U)).coe_zero,
pi.zero_apply, ring_hom.map_zero], refl },
map_add' := λ s t, subtype.ext $ funext $ λ p, by
{ rw [subtype.coe_mk, subtype.val_eq_coe, comap_fun, (sections_subring R (op U)).coe_add,
pi.add_apply, ring_hom.map_add], refl },
map_mul' := λ s t, subtype.ext $ funext $ λ p, by
{ rw [subtype.coe_mk, subtype.val_eq_coe, comap_fun, (sections_subring R (op U)).coe_mul,
pi.mul_apply, ring_hom.map_mul], refl } }
@[simp]
lemma comap_apply (f : R →+* S) (U : opens (prime_spectrum.Top R))
(V : opens (prime_spectrum.Top S)) (hUV : V.1 ⊆ (prime_spectrum.comap f) ⁻¹' U.1)
(s : (structure_sheaf R).1.obj (op U)) (p : V) :
(comap f U V hUV s).1 p =
localization.local_ring_hom (prime_spectrum.comap f p.1).as_ideal _ f rfl
(s.1 ⟨(prime_spectrum.comap f p.1), hUV p.2⟩ : _) :=
rfl
lemma comap_const (f : R →+* S) (U : opens (prime_spectrum.Top R))
(V : opens (prime_spectrum.Top S)) (hUV : V.1 ⊆ (prime_spectrum.comap f) ⁻¹' U.1)
(a b : R) (hb : ∀ x : prime_spectrum R, x ∈ U → b ∈ x.as_ideal.prime_compl) :
comap f U V hUV (const R a b U hb) =
const S (f a) (f b) V (λ p hpV, hb (prime_spectrum.comap f p) (hUV hpV)) :=
subtype.eq $ funext $ λ p,
begin
rw [comap_apply, const_apply, const_apply],
erw localization.local_ring_hom_mk',
refl,
end
/--
For an inclusion `i : V ⟶ U` between open sets of the prime spectrum of `R`, the comap of the
identity from OO_X(U) to OO_X(V) equals as the restriction map of the structure sheaf.
This is a generalization of the fact that, for fixed `U`, the comap of the identity from OO_X(U)
to OO_X(U) is the identity.
-/
lemma comap_id_eq_map (U V : opens (prime_spectrum.Top R)) (iVU : V ⟶ U) :
comap (ring_hom.id R) U V
(λ p hpV, le_of_hom iVU $ by rwa prime_spectrum.comap_id) =
(structure_sheaf R).1.map iVU.op :=
ring_hom.ext $ λ s, subtype.eq $ funext $ λ p,
begin
rw comap_apply,
-- Unfortunately, we cannot use `localization.local_ring_hom_id` here, because
-- `prime_spectrum.comap (ring_hom.id R) p` is not *definitionally* equal to `p`. Instead, we use
-- that we can write `s` as a fraction `a/b` in a small neighborhood around `p`. Since
-- `prime_spectrum.comap (ring_hom.id R) p` equals `p`, it is also contained in the same
-- neighborhood, hence `s` equals `a/b` there too.
obtain ⟨W, hpW, iWU, h⟩ := s.2 (iVU p),
obtain ⟨a, b, h'⟩ := h.eq_mk',
obtain ⟨hb₁, s_eq₁⟩ := h' ⟨p, hpW⟩,
obtain ⟨hb₂, s_eq₂⟩ := h' ⟨prime_spectrum.comap (ring_hom.id _) p.1,
by rwa prime_spectrum.comap_id⟩,
dsimp only at s_eq₁ s_eq₂,
erw [s_eq₂, localization.local_ring_hom_mk', ← s_eq₁, ← res_apply],
end
/--
The comap of the identity is the identity. In this variant of the lemma, two open subsets `U` and
`V` are given as arguments, together with a proof that `U = V`. This is be useful when `U` and `V`
are not definitionally equal.
-/
lemma comap_id (U V : opens (prime_spectrum.Top R)) (hUV : U = V) :
comap (ring_hom.id R) U V (λ p hpV, by rwa [hUV, prime_spectrum.comap_id]) =
eq_to_hom (show (structure_sheaf R).1.obj (op U) = _, by rw hUV) :=
by erw [comap_id_eq_map U V (eq_to_hom hUV.symm), eq_to_hom_op, eq_to_hom_map]
@[simp] lemma comap_id' (U : opens (prime_spectrum.Top R)) :
comap (ring_hom.id R) U U (λ p hpU, by rwa prime_spectrum.comap_id) =
ring_hom.id _ :=
by { rw comap_id U U rfl, refl }
lemma comap_comp (f : R →+* S) (g : S →+* P) (U : opens (prime_spectrum.Top R))
(V : opens (prime_spectrum.Top S)) (W : opens (prime_spectrum.Top P))
(hUV : ∀ p ∈ V, prime_spectrum.comap f p ∈ U) (hVW : ∀ p ∈ W, prime_spectrum.comap g p ∈ V) :
comap (g.comp f) U W (λ p hpW, hUV (prime_spectrum.comap g p) (hVW p hpW)) =
(comap g V W hVW).comp (comap f U V hUV) :=
ring_hom.ext $ λ s, subtype.eq $ funext $ λ p,
begin
rw comap_apply,
erw localization.local_ring_hom_comp _ (prime_spectrum.comap g p.1).as_ideal,
-- refl works here, because `prime_spectrum.comap (g.comp f) p` is defeq to
-- `prime_spectrum.comap f (prime_spectrum.comap g p)`
refl,
end
@[elementwise, reassoc] lemma to_open_comp_comap (f : R →+* S)
(U : opens (prime_spectrum.Top R)) :
to_open R U ≫ comap f U (opens.comap (prime_spectrum.comap f) U) (λ _, id) =
CommRing.of_hom f ≫ to_open S _ :=
ring_hom.ext $ λ s, subtype.eq $ funext $ λ p,
begin
simp_rw [comp_apply, comap_apply, subtype.val_eq_coe],
erw localization.local_ring_hom_to_map,
refl,
end
end comap
end structure_sheaf
end algebraic_geometry
|
9b09e5c8186af56459907e63693f5d2df726750a | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/data/unit/default.lean | 7b2dbca50f227e27ea356b31816877caf6da0fc8 | [
"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 | 219 | lean | -- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Leonardo de Moura
import data.unit.decl data.unit.thms data.unit.insts
|
8b9d5eff7899e1d8dbd4aa36f9e5542c3b8ac021 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/limits/concrete_category.lean | c0d1768e3668c8dae686899f20ff594d8c583afc | [
"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 | 12,236 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Adam Topaz
-/
import category_theory.limits.preserves.basic
import category_theory.limits.types
import category_theory.limits.shapes.wide_pullbacks
import category_theory.limits.shapes.multiequalizer
import category_theory.concrete_category.elementwise
/-!
# Facts about (co)limits of functors into concrete categories
-/
universes w v u
open category_theory
namespace category_theory.limits
local attribute [instance] concrete_category.has_coe_to_fun concrete_category.has_coe_to_sort
section limits
variables {C : Type u} [category.{v} C] [concrete_category.{v} C]
{J : Type v} [small_category J] (F : J ⥤ C) [preserves_limit F (forget C)]
lemma concrete.to_product_injective_of_is_limit {D : cone F} (hD : is_limit D) :
function.injective (λ (x : D.X) (j : J), D.π.app j x) :=
begin
let E := (forget C).map_cone D,
let hE : is_limit E := is_limit_of_preserves _ hD,
let G := types.limit_cone.{v v} (F ⋙ forget C),
let hG := types.limit_cone_is_limit.{v v} (F ⋙ forget C),
let T : E.X ≅ G.X := hE.cone_point_unique_up_to_iso hG,
change function.injective (T.hom ≫ (λ x j, G.π.app j x)),
have h : function.injective T.hom,
{ intros a b h,
suffices : T.inv (T.hom a) = T.inv (T.hom b), by simpa,
rw h },
suffices : function.injective (λ (x : G.X) j, G.π.app j x),
by exact this.comp h,
apply subtype.ext,
end
lemma concrete.is_limit_ext {D : cone F} (hD : is_limit D) (x y : D.X) :
(∀ j, D.π.app j x = D.π.app j y) → x = y :=
λ h, concrete.to_product_injective_of_is_limit _ hD (funext h)
lemma concrete.limit_ext [has_limit F] (x y : limit F) :
(∀ j, limit.π F j x = limit.π F j y) → x = y :=
concrete.is_limit_ext F (limit.is_limit _) _ _
section wide_pullback
open wide_pullback
open wide_pullback_shape
lemma concrete.wide_pullback_ext {B : C} {ι : Type*} {X : ι → C} (f : Π j : ι, X j ⟶ B)
[has_wide_pullback B X f] [preserves_limit (wide_cospan B X f) (forget C)]
(x y : wide_pullback B X f) (h₀ : base f x = base f y)
(h : ∀ j, π f j x = π f j y) : x = y :=
begin
apply concrete.limit_ext,
rintro (_|j),
{ exact h₀ },
{ apply h }
end
lemma concrete.wide_pullback_ext' {B : C} {ι : Type*} [nonempty ι]
{X : ι → C} (f : Π j : ι, X j ⟶ B) [has_wide_pullback B X f]
[preserves_limit (wide_cospan B X f) (forget C)]
(x y : wide_pullback B X f) (h : ∀ j, π f j x = π f j y) : x = y :=
begin
apply concrete.wide_pullback_ext _ _ _ _ h,
inhabit ι,
simp only [← π_arrow f (arbitrary _), comp_apply, h],
end
end wide_pullback
section multiequalizer
lemma concrete.multiequalizer_ext {I : multicospan_index C} [has_multiequalizer I]
[preserves_limit I.multicospan (forget C)] (x y : multiequalizer I)
(h : ∀ (t : I.L), multiequalizer.ι I t x = multiequalizer.ι I t y) : x = y :=
begin
apply concrete.limit_ext,
rintros (a|b),
{ apply h },
{ rw [← limit.w I.multicospan (walking_multicospan.hom.fst b),
comp_apply, comp_apply, h] }
end
/-- An auxiliary equivalence to be used in `multiequalizer_equiv` below.-/
def concrete.multiequalizer_equiv_aux (I : multicospan_index C) :
(I.multicospan ⋙ (forget C)).sections ≃
{ x : Π (i : I.L), I.left i // ∀ (i : I.R), I.fst i (x _) = I.snd i (x _) } :=
{ to_fun := λ x, ⟨λ i, x.1 (walking_multicospan.left _), λ i, begin
have a := x.2 (walking_multicospan.hom.fst i),
have b := x.2 (walking_multicospan.hom.snd i),
rw ← b at a,
exact a,
end⟩,
inv_fun := λ x,
{ val := λ j,
match j with
| walking_multicospan.left a := x.1 _
| walking_multicospan.right b := I.fst b (x.1 _)
end,
property := begin
rintros (a|b) (a'|b') (f|f|f),
{ change (I.multicospan.map (𝟙 _)) _ = _, simp },
{ refl },
{ dsimp, erw ← x.2 b', refl },
{ change (I.multicospan.map (𝟙 _)) _ = _, simp },
end },
left_inv := begin
intros x, ext (a|b),
{ refl },
{ change _ = x.val _,
rw ← x.2 (walking_multicospan.hom.fst b),
refl }
end,
right_inv := by { intros x, ext i, refl } }
/-- The equivalence between the noncomputable multiequalizer and
and the concrete multiequalizer. -/
noncomputable
def concrete.multiequalizer_equiv (I : multicospan_index C) [has_multiequalizer I]
[preserves_limit I.multicospan (forget C)] : (multiequalizer I : C) ≃
{ x : Π (i : I.L), I.left i // ∀ (i : I.R), I.fst i (x _) = I.snd i (x _) } :=
let h1 := (limit.is_limit I.multicospan),
h2 := (is_limit_of_preserves (forget C) h1),
E := h2.cone_point_unique_up_to_iso (types.limit_cone_is_limit.{v v} _) in
equiv.trans E.to_equiv (concrete.multiequalizer_equiv_aux I)
@[simp]
lemma concrete.multiequalizer_equiv_apply (I : multicospan_index C) [has_multiequalizer I]
[preserves_limit I.multicospan (forget C)] (x : multiequalizer I) (i : I.L) :
((concrete.multiequalizer_equiv I) x : Π (i : I.L), I.left i) i = multiequalizer.ι I i x := rfl
end multiequalizer
-- TODO: Add analogous lemmas about products and equalizers.
end limits
section colimits
-- We don't mark this as an `@[ext]` lemma as we don't always want to work elementwise.
lemma cokernel_funext {C : Type*} [category C] [has_zero_morphisms C] [concrete_category C]
{M N K : C} {f : M ⟶ N} [has_cokernel f] {g h : cokernel f ⟶ K}
(w : ∀ (n : N), g (cokernel.π f n) = h (cokernel.π f n)) : g = h :=
begin
apply coequalizer.hom_ext,
apply concrete_category.hom_ext _ _,
simpa using w,
end
variables {C : Type u} [category.{v} C] [concrete_category.{v} C]
{J : Type v} [small_category J] (F : J ⥤ C) [preserves_colimit F (forget C)]
lemma concrete.from_union_surjective_of_is_colimit {D : cocone F} (hD : is_colimit D) :
let ff : (Σ (j : J), F.obj j) → D.X := λ a, D.ι.app a.1 a.2 in function.surjective ff :=
begin
intro ff,
let E := (forget C).map_cocone D,
let hE : is_colimit E := is_colimit_of_preserves _ hD,
let G := types.colimit_cocone.{v v} (F ⋙ forget C),
let hG := types.colimit_cocone_is_colimit.{v v} (F ⋙ forget C),
let T : E ≅ G := hE.unique_up_to_iso hG,
let TX : E.X ≅ G.X := (cocones.forget _).map_iso T,
suffices : function.surjective (TX.hom ∘ ff),
{ intro a,
obtain ⟨b, hb⟩ := this (TX.hom a),
refine ⟨b, _⟩,
apply_fun TX.inv at hb,
change (TX.hom ≫ TX.inv) (ff b) = (TX.hom ≫ TX.inv) _ at hb,
simpa only [TX.hom_inv_id] using hb },
have : TX.hom ∘ ff = λ a, G.ι.app a.1 a.2,
{ ext a,
change (E.ι.app a.1 ≫ hE.desc G) a.2 = _,
rw hE.fac },
rw this,
rintro ⟨⟨j,a⟩⟩,
exact ⟨⟨j,a⟩,rfl⟩,
end
lemma concrete.is_colimit_exists_rep {D : cocone F} (hD : is_colimit D) (x : D.X) :
∃ (j : J) (y : F.obj j), D.ι.app j y = x :=
begin
obtain ⟨a, rfl⟩ := concrete.from_union_surjective_of_is_colimit F hD x,
exact ⟨a.1, a.2, rfl⟩,
end
lemma concrete.colimit_exists_rep [has_colimit F] (x : colimit F) :
∃ (j : J) (y : F.obj j), colimit.ι F j y = x :=
concrete.is_colimit_exists_rep F (colimit.is_colimit _) x
lemma concrete.is_colimit_rep_eq_of_exists {D : cocone F} {i j : J} (hD : is_colimit D)
(x : F.obj i) (y : F.obj j) (h : ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y) :
D.ι.app i x = D.ι.app j y :=
begin
let E := (forget C).map_cocone D,
let hE : is_colimit E := is_colimit_of_preserves _ hD,
let G := types.colimit_cocone.{v v} (F ⋙ forget C),
let hG := types.colimit_cocone_is_colimit.{v v} (F ⋙ forget C),
let T : E ≅ G := hE.unique_up_to_iso hG,
let TX : E.X ≅ G.X := (cocones.forget _).map_iso T,
apply_fun TX.hom,
swap, { suffices : function.bijective TX.hom, by exact this.1,
rw ← is_iso_iff_bijective, apply is_iso.of_iso },
change (E.ι.app i ≫ TX.hom) x = (E.ι.app j ≫ TX.hom) y,
erw [T.hom.w, T.hom.w],
obtain ⟨k, f, g, h⟩ := h,
have : G.ι.app i x = (G.ι.app k (F.map f x) : G.X) := quot.sound ⟨f,rfl⟩,
rw [this, h],
symmetry,
exact quot.sound ⟨g,rfl⟩,
end
lemma concrete.colimit_rep_eq_of_exists [has_colimit F] {i j : J}
(x : F.obj i) (y : F.obj j) (h : ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y) :
colimit.ι F i x = colimit.ι F j y :=
concrete.is_colimit_rep_eq_of_exists F (colimit.is_colimit _) x y h
section filtered_colimits
variable [is_filtered J]
lemma concrete.is_colimit_exists_of_rep_eq {D : cocone F} {i j : J} (hD : is_colimit D)
(x : F.obj i) (y : F.obj j) (h : D.ι.app _ x = D.ι.app _ y) :
∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y :=
begin
let E := (forget C).map_cocone D,
let hE : is_colimit E := is_colimit_of_preserves _ hD,
let G := types.colimit_cocone.{v v} (F ⋙ forget C),
let hG := types.colimit_cocone_is_colimit.{v v} (F ⋙ forget C),
let T : E ≅ G := hE.unique_up_to_iso hG,
let TX : E.X ≅ G.X := (cocones.forget _).map_iso T,
apply_fun TX.hom at h,
change (E.ι.app i ≫ TX.hom) x = (E.ι.app j ≫ TX.hom) y at h,
erw [T.hom.w, T.hom.w] at h,
replace h := quot.exact _ h,
suffices : ∀ (a b : Σ j, F.obj j)
(h : eqv_gen (limits.types.quot.rel.{v v} (F ⋙ forget C)) a b),
∃ k (f : a.1 ⟶ k) (g : b.1 ⟶ k), F.map f a.2 = F.map g b.2,
{ exact this ⟨i,x⟩ ⟨j,y⟩ h },
intros a b h,
induction h,
case eqv_gen.rel : x y hh
{ obtain ⟨e,he⟩ := hh,
use [y.1, e, 𝟙 _],
simpa using he.symm },
case eqv_gen.refl : x { use [x.1, 𝟙 _, 𝟙 _, rfl] },
case eqv_gen.symm : x y _ hh
{ obtain ⟨k, f, g, hh⟩ := hh,
use [k, g, f, hh.symm] },
case eqv_gen.trans : x y z _ _ hh1 hh2
{ obtain ⟨k1, f1, g1, h1⟩ := hh1,
obtain ⟨k2, f2, g2, h2⟩ := hh2,
let k0 : J := is_filtered.max k1 k2,
let e1 : k1 ⟶ k0 := is_filtered.left_to_max _ _,
let e2 : k2 ⟶ k0 := is_filtered.right_to_max _ _,
let k : J := is_filtered.coeq (g1 ≫ e1) (f2 ≫ e2),
let e : k0 ⟶ k := is_filtered.coeq_hom _ _,
use [k, f1 ≫ e1 ≫ e, g2 ≫ e2 ≫ e],
simp only [F.map_comp, comp_apply, h1, ← h2],
simp only [← comp_apply, ← F.map_comp],
rw is_filtered.coeq_condition },
end
theorem concrete.is_colimit_rep_eq_iff_exists {D : cocone F} {i j : J}
(hD : is_colimit D) (x : F.obj i) (y : F.obj j) :
D.ι.app i x = D.ι.app j y ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y :=
⟨concrete.is_colimit_exists_of_rep_eq _ hD _ _, concrete.is_colimit_rep_eq_of_exists _ hD _ _⟩
lemma concrete.colimit_exists_of_rep_eq [has_colimit F] {i j : J}
(x : F.obj i) (y : F.obj j) (h : colimit.ι F _ x = colimit.ι F _ y) :
∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y :=
concrete.is_colimit_exists_of_rep_eq F (colimit.is_colimit _) x y h
theorem concrete.colimit_rep_eq_iff_exists [has_colimit F] {i j : J}
(x : F.obj i) (y : F.obj j) :
colimit.ι F i x = colimit.ι F j y ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y :=
⟨concrete.colimit_exists_of_rep_eq _ _ _, concrete.colimit_rep_eq_of_exists _ _ _⟩
end filtered_colimits
section wide_pushout
open wide_pushout
open wide_pushout_shape
lemma concrete.wide_pushout_exists_rep {B : C} {α : Type*} {X : α → C} (f : Π j : α, B ⟶ X j)
[has_wide_pushout B X f] [preserves_colimit (wide_span B X f) (forget C)]
(x : wide_pushout B X f) : (∃ y : B, head f y = x) ∨ (∃ (i : α) (y : X i), ι f i y = x) :=
begin
obtain ⟨_ | j, y, rfl⟩ := concrete.colimit_exists_rep _ x,
{ use y },
{ right,
use [j,y] }
end
lemma concrete.wide_pushout_exists_rep' {B : C} {α : Type*} [nonempty α] {X : α → C}
(f : Π j : α, B ⟶ X j) [has_wide_pushout B X f]
[preserves_colimit (wide_span B X f) (forget C)] (x : wide_pushout B X f) :
∃ (i : α) (y : X i), ι f i y = x :=
begin
rcases concrete.wide_pushout_exists_rep f x with ⟨y, rfl⟩ | ⟨i, y, rfl⟩,
{ inhabit α,
use [arbitrary _, f _ y],
simp only [← arrow_ι _ (arbitrary α), comp_apply] },
{ use [i,y] }
end
end wide_pushout
-- TODO: Add analogous lemmas about coproducts and coequalizers.
end colimits
end category_theory.limits
|
70bf9f310ccc88eb18919fc37f67b8ebc0a0fe42 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Init/Data/Array/Subarray.lean | 76ca13608c9f5b71649b0447a3a9dd4168f94f53 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 4,687 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Data.Array.Basic
universe u v w
structure Subarray (α : Type u) where
as : Array α
start : Nat
stop : Nat
h₁ : start ≤ stop
h₂ : stop ≤ as.size
namespace Subarray
def popFront (s : Subarray α) : Subarray α :=
if h : s.start < s.stop then
{ s with start := s.start + 1, h₁ := Nat.le_of_lt_succ (Nat.add_lt_add_right h 1) }
else
s
@[inline] unsafe def forInUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (s : Subarray α) (b : β) (f : α → β → m (ForInStep β)) : m β :=
let sz := USize.ofNat s.stop
let rec @[specialize] loop (i : USize) (b : β) : m β := do
if i < sz then
let a := s.as.uget i lcProof
match (← f a b) with
| ForInStep.done b => pure b
| ForInStep.yield b => loop (i+1) b
else
pure b
loop (USize.ofNat s.start) b
-- TODO: provide reference implementation
@[implementedBy Subarray.forInUnsafe]
protected constant forIn {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (s : Subarray α) (b : β) (f : α → β → m (ForInStep β)) : m β :=
pure b
instance : ForIn m (Subarray α) α where
forIn := Subarray.forIn
@[inline]
def foldlM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Subarray α) : m β :=
as.as.foldlM f (init := init) (start := as.start) (stop := as.stop)
@[inline]
def foldrM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Subarray α) : m β :=
as.as.foldrM f (init := init) (start := as.stop) (stop := as.start)
@[inline]
def anyM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool :=
as.as.anyM p (start := as.start) (stop := as.stop)
@[inline]
def allM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool :=
as.as.allM p (start := as.start) (stop := as.stop)
@[inline]
def forM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit :=
as.as.forM f (start := as.start) (stop := as.stop)
@[inline]
def forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit :=
as.as.forRevM f (start := as.stop) (stop := as.start)
@[inline]
def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Subarray α) : β :=
Id.run <| as.foldlM f (init := init)
@[inline]
def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Subarray α) : β :=
Id.run <| as.foldrM f (init := init)
@[inline]
def any {α : Type u} (p : α → Bool) (as : Subarray α) : Bool :=
Id.run <| as.anyM p
@[inline]
def all {α : Type u} (p : α → Bool) (as : Subarray α) : Bool :=
Id.run <| as.allM p
end Subarray
namespace Array
variable {α : Type u}
def toSubarray (as : Array α) (start : Nat := 0) (stop : Nat := as.size) : Subarray α :=
if h₂ : stop ≤ as.size then
if h₁ : start ≤ stop then
{ as := as, start := start, stop := stop, h₁ := h₁, h₂ := h₂ }
else
{ as := as, start := stop, stop := stop, h₁ := Nat.le_refl _, h₂ := h₂ }
else
if h₁ : start ≤ as.size then
{ as := as, start := start, stop := as.size, h₁ := h₁, h₂ := Nat.le_refl _ }
else
{ as := as, start := as.size, stop := as.size, h₁ := Nat.le_refl _, h₂ := Nat.le_refl _ }
def ofSubarray (s : Subarray α) : Array α := Id.run <| do
let mut as := mkEmpty (s.stop - s.start)
for a in s do
as := as.push a
return as
def extract (as : Array α) (start stop : Nat) : Array α :=
ofSubarray (as.toSubarray start stop)
instance : Coe (Subarray α) (Array α) := ⟨ofSubarray⟩
syntax:max term noWs "[" term ":" term "]" : term
syntax:max term noWs "[" term ":" "]" : term
syntax:max term noWs "[" ":" term "]" : term
macro_rules
| `($a[$start : $stop]) => `(Array.toSubarray $a $start $stop)
| `($a[ : $stop]) => `(Array.toSubarray $a 0 $stop)
| `($a[$start : ]) => `(let a := $a; Array.toSubarray a $start a.size)
end Array
def Subarray.toArray (s : Subarray α) : Array α :=
Array.ofSubarray s
instance : Append (Subarray α) where
append x y :=
let a := x.toArray ++ y.toArray
a.toSubarray 0 a.size
instance [Repr α] : Repr (Subarray α) where
reprPrec s _ := repr s.toArray ++ ".toSubarray"
instance [ToString α] : ToString (Subarray α) where
toString s := toString s.toArray
|
05db899d1c4fbcad254477a74abbc2f3a4a7f517 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/string/defs_auto.lean | d18a608c2c3a4f0e7d1c6e83b710810d969254e1 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,245 | lean | /-
Copyright (c) 2019 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Simon Hudon, Keeley Hoek, Floris van Doorn
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.list.defs
import Mathlib.PostPort
namespace Mathlib
namespace string
/-- `s.split_on c` tokenizes `s : string` on `c : char`. -/
def split_on (s : string) (c : char) : List string := split (fun (_x : char) => to_bool (_x = c)) s
/-- `string.map_tokens c f s` tokenizes `s : string` on `c : char`, maps `f` over each token, and
then reassembles the string by intercalating the separator token `c` over the mapped tokens. -/
def map_tokens (c : char) (f : string → string) : string → string :=
intercalate (singleton c) ∘ list.map f ∘ split fun (_x : char) => to_bool (_x = c)
/-- Tests whether the first string is a prefix of the second string. -/
def is_prefix_of (x : string) (y : string) : Bool := list.is_prefix_of (to_list x) (to_list y)
/-- Tests whether the first string is a suffix of the second string. -/
def is_suffix_of (x : string) (y : string) : Bool := list.is_suffix_of (to_list x) (to_list y)
/-- `x.starts_with y` is true if `y` is a prefix of `x`, and is false otherwise. -/
def starts_with (x : string) (y : string) : Bool := is_prefix_of y x
/-- `x.ends_with y` is true if `y` is a suffix of `x`, and is false otherwise. -/
def ends_with (x : string) (y : string) : Bool := is_suffix_of y x
/-- `get_rest s t` returns `some r` if `s = t ++ r`.
If `t` is not a prefix of `s`, returns `none` -/
def get_rest (s : string) (t : string) : Option string :=
list.as_string <$> list.get_rest (to_list s) (to_list t)
/-- Removes the first `n` elements from the string `s` -/
def popn (s : string) (n : ℕ) : string := iterator.next_to_string (iterator.nextn (mk_iterator s) n)
/-- `is_nat s` is true iff `s` is a nonempty sequence of digits. -/
def is_nat (s : string) : Bool :=
to_bool (¬↥(is_empty s) ∧ ↥(list.all (to_list s) fun (c : char) => to_bool (char.is_digit c)))
/-- Produce the head character from the string `s`, if `s` is not empty, otherwise 'A'. -/
def head (s : string) : char := iterator.curr (mk_iterator s)
end Mathlib |
18e7d9f5180bbfbc11ca8b01502718b894984ab4 | d31b9f832ff922a603f76cf32e0f3aa822640508 | /src/hott/init/trunc.lean | b640ce1fe071e3dcbc5f2903cee52cbf94dbc33d | [
"Apache-2.0"
] | permissive | javra/hott3 | 6e7a9e72a991a2fae32e5764982e521dca617b16 | cd51f2ab2aa48c1246a188f9b525b30f76c3d651 | refs/heads/master | 1,585,819,679,148 | 1,531,232,382,000 | 1,536,682,965,000 | 154,294,022 | 0 | 0 | Apache-2.0 | 1,540,284,376,000 | 1,540,284,375,000 | null | UTF-8 | Lean | false | false | 13,562 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Floris van Doorn
Definition of is_trunc (n-truncatedness)
Ported from Coq HoTT.
-/
import .equiv .pathover .logic
universes u v l
hott_theory
namespace hott
open hott.eq nat sigma unit
/- Truncation levels -/
inductive trunc_index : Type
| minus_two : trunc_index
| succ : trunc_index → trunc_index
open trunc_index
/-
notation for trunc_index is -2, -1, 0, 1, ...
from 0 and up this comes from the way numerals are parsed in Lean.
Any structure with a 0, a 1, and a + have numerals defined in them.
-/
notation `ℕ₋₂` := trunc_index -- input using \N-2
instance has_zero_trunc_index : has_zero ℕ₋₂ :=
has_zero.mk (succ (succ minus_two))
instance has_one_trunc_index : has_one ℕ₋₂ :=
has_one.mk (succ (succ (succ minus_two)))
namespace trunc_index
notation `-1` := trunc_index.succ trunc_index.minus_two -- ISSUE: -1 gets printed as -2.+1?
notation `-2` := trunc_index.minus_two
postfix `.+1`:(max+1) := trunc_index.succ
postfix `.+2`:(max+1) := λn, (n .+1 .+1)
--addition, where we add two to the result
@[hott] def add_plus_two (n m : ℕ₋₂) : ℕ₋₂ :=
trunc_index.rec_on m n (λ k l, l .+1)
infix ` +2+ `:65 := trunc_index.add_plus_two
-- addition of trunc_indices, where results smaller than -2 are changed to -2
@[hott] protected def add (n m : ℕ₋₂) : ℕ₋₂ :=
trunc_index.cases_on m
(trunc_index.cases_on n -2 (λn', (trunc_index.cases_on n' -2 id)))
(λm', trunc_index.cases_on m'
(trunc_index.cases_on n -2 id)
(add_plus_two n))
/- we give a weird name to the reflexivity step to avoid overloading le.refl
(which can be used if types.trunc is imported) -/
inductive le (a : ℕ₋₂) : ℕ₋₂ → Type
| tr_refl : le a
| step : Π {b}, le b → le (b.+1)
attribute [refl] le.tr_refl
end trunc_index
local infix ` ≤ ` := trunc_index.le
instance has_add_trunc_index : has_add ℕ₋₂ :=
has_add.mk trunc_index.add
namespace trunc_index
@[hott] def sub_two (n : ℕ) : ℕ₋₂ :=
nat.rec_on n -2 (λ n k, k.+1)
@[hott] def add_two (n : ℕ₋₂) : ℕ :=
trunc_index.rec_on n nat.zero (λ n k, nat.succ k)
postfix `.-2`:(max+1) := sub_two
postfix `.-1`:(max+1) := λn, (n .-2 .+1)
@[hott] def of_nat (n : ℕ) : ℕ₋₂ :=
n.-2.+2
instance: has_coe ℕ ℕ₋₂ := ⟨of_nat⟩
@[hott] def succ_le_succ {n m : ℕ₋₂} (H : n ≤ m) : n.+1 ≤ m.+1 :=
begin induction H, refl, apply le.step; assumption end
@[hott] def minus_two_le (n : ℕ₋₂) : -2 ≤ n :=
begin induction n, refl, apply le.step; assumption end
end trunc_index open trunc_index
namespace is_trunc
-- export [notation] [coercion] trunc_index
/- truncated types -/
/-
Just as in Coq HoTT we define an internal version of contractibility and is_trunc, but we only
use `is_trunc` and `is_contr`
-/
structure contr_internal (A : Type u) :=
(center : A)
(center_eq : Π(a : A), center = a)
@[hott] def is_trunc_internal (n : ℕ₋₂) : Type u → Type u :=
trunc_index.rec_on n
(λA, contr_internal A)
(λn trunc_n A, (Π(x y : A), trunc_n (x = y)))
end is_trunc open is_trunc
class is_trunc (n : ℕ₋₂) (A : Type u) :=
(to_internal : is_trunc_internal n A)
open nat trunc_index
namespace is_trunc
notation `is_contr` := is_trunc -2
notation `is_prop` := is_trunc -1
notation `is_set` := is_trunc 0
variables {A : Type u} {B : Type v}
@[hott] def is_trunc_succ_intro {A : Type _} {n : ℕ₋₂} (H : ∀x y : A, is_trunc n (x = y))
: is_trunc n.+1 A :=
is_trunc.mk (λ x y, is_trunc.to_internal _ _)
@[hott, instance] def is_trunc_eq
(n : ℕ₋₂) [H : is_trunc (n.+1) A] (x y : A) : is_trunc n (x = y) :=
is_trunc.mk (is_trunc.to_internal (n.+1) A x y)
/- contractibility -/
@[hott] def is_contr.mk (center : A) (center_eq : Π(a : A), center = a) : is_contr A :=
is_trunc.mk (contr_internal.mk center center_eq)
@[hott] def center (A : Type _) [H : is_contr A] : A :=
contr_internal.center (is_trunc.to_internal -2 A)
@[hott] def center' {A : Type _} (H : is_contr A) : A := center A
@[hott] def center_eq [H : is_contr A] (a : A) : center _ = a :=
contr_internal.center_eq (is_trunc.to_internal -2 A) a
@[hott] def eq_of_is_contr [H : is_contr A] (x y : A) : x = y :=
(center_eq x)⁻¹ ⬝ (center_eq y)
@[hott] def prop_eq_of_is_contr {A : Type _} [H : is_contr A] {x y : A} (p q : x = y) : p = q :=
have K : ∀ (r : x = y), eq_of_is_contr x y = r, from (λ r, by induction r; apply con.left_inv),
(K p)⁻¹ ⬝ K q
@[hott] def is_contr_eq {A : Type _} [H : is_contr A] (x y : A) : is_contr (x = y) :=
is_contr.mk (eq_of_is_contr _ _) (λ p, prop_eq_of_is_contr _ _)
/- truncation is upward close -/
-- n-types are also (n+1)-types
@[hott, instance] def is_trunc_succ (A : Type _) (n : ℕ₋₂)
[H : is_trunc n A] : is_trunc (n.+1) A :=
begin
unfreezeI,
induction n with n IH generalizing A; apply is_trunc_succ_intro; intros x y,
{ exact @is_contr_eq A H x y },
{ refine @IH _ (@is_trunc_eq _ _ H _ _) , }
end
--in the proof the type of H is given explicitly to make it available for class inference
@[hott] def is_trunc_of_le (A : Type.{l}) {n m : ℕ₋₂} (Hnm : n ≤ m)
[Hn : is_trunc n A] : is_trunc m A :=
begin
induction Hnm with m Hnm IH,
{ exact Hn },
{ exact @is_trunc_succ _ _ IH }
end
@[hott] def is_trunc_of_imp_is_trunc {n : ℕ₋₂} (H : A → is_trunc (n.+1) A)
: is_trunc (n.+1) A :=
@is_trunc_succ_intro _ _ (λx y, @is_trunc_eq _ _ (H x) x y)
@[hott] def is_trunc_of_imp_is_trunc_of_le {n : ℕ₋₂} (Hn : -1 ≤ n) (H : A → is_trunc n A)
: is_trunc n A :=
begin
unfreezeI, induction Hn with n' Hn'; apply is_trunc_of_imp_is_trunc H
end
-- these must be definitions, because we need them to compute sometimes
@[hott] def is_trunc_of_is_contr (A : Type _) (n : ℕ₋₂) [H : is_contr A] : is_trunc n A :=
trunc_index.rec_on n H (λn H, by unfreezeI; apply_instance)
@[hott] def is_trunc_succ_of_is_prop (A : Type _) (n : ℕ₋₂) [H : is_prop A]
: is_trunc (n.+1) A :=
is_trunc_of_le A (show -1 ≤ n.+1, from succ_le_succ (minus_two_le n))
@[hott] def is_trunc_succ_succ_of_is_set (A : Type _) (n : ℕ₋₂) [H : is_set A]
: is_trunc (n.+2) A :=
is_trunc_of_le A (show 0 ≤ n.+2, from succ_le_succ (succ_le_succ (minus_two_le n)))
/- props -/
@[hott] def is_prop.elim [H : is_prop A] (x y : A) : x = y :=
by apply center
@[hott] def is_prop.elim' (x y : A) (H : is_prop A) : x = y :=
is_prop.elim x y
@[hott] def is_contr_of_inhabited_prop {A : Type _} [H : is_prop A] (x : A) : is_contr A :=
is_contr.mk x (λy, by apply is_prop.elim)
@[hott] def is_prop_of_imp_is_contr {A : Type _} (H : A → is_contr A) : is_prop A :=
@is_trunc_succ_intro A -2
(λx y,
have H2 : is_contr A, from H x,
by unfreezeI; apply is_contr_eq)
@[hott] def is_prop.mk {A : Type _} (H : ∀x y : A, x = y) : is_prop A :=
is_prop_of_imp_is_contr (λ x, is_contr.mk x (H x))
@[hott] def is_prop_elim_self {A : Type _} {H : is_prop A} (x : A) : is_prop.elim x x = idp :=
by apply is_prop.elim
/- sets -/
@[hott] def is_set.mk (A : Type _) (H : ∀(x y : A) (p q : x = y), p = q) : is_set A :=
@is_trunc_succ_intro _ _ (λ x y, is_prop.mk (H x y))
@[hott] def is_set.elim [H : is_set A] ⦃x y : A⦄ (p q : x = y) : p = q :=
by apply is_prop.elim
@[hott] def is_set.elim' ⦃x y : A⦄ (p q : x = y) (H : is_set A) : p = q :=
is_set.elim p q
/- instances -/
@[instance, hott] def is_contr_sigma_eq {A : Type u} (a : A)
: is_contr (Σ(x : A), a = x) :=
is_contr.mk (sigma.mk a idp) (λ ⟨b,q⟩, by induction q; refl)
@[instance, hott] def is_contr_sigma_eq' {A : Type u} (a : A)
: is_contr (Σ(x : A), x = a) :=
is_contr.mk (sigma.mk a idp) (λ ⟨b,q⟩, by clear _fun_match; induction q; refl)
@[hott] def ap_fst_center_eq_sigma_eq {A : Type _} {a x : A} (p : a = x)
: ap sigma.fst (center_eq ⟨x, p⟩) = p :=
by induction p; reflexivity
@[hott] def ap_fst_center_eq_sigma_eq' {A : Type _} {a x : A} (p : x = a)
: ap sigma.fst (center_eq ⟨x, p⟩) = p⁻¹ :=
by induction p; reflexivity
@[hott] def is_contr_unit : is_contr unit :=
is_contr.mk star (λp, punit.rec_on p idp)
@[hott] def is_prop_empty : is_prop empty :=
is_prop.mk (λx, by induction x)
local attribute [instance] is_contr_unit is_prop_empty
@[hott,instance] def is_trunc_unit (n : ℕ₋₂) : is_trunc n unit :=
by apply is_trunc_of_is_contr
@[hott,instance] def is_trunc_empty (n : ℕ₋₂) : is_trunc (n.+1) empty :=
by apply is_trunc_succ_of_is_prop
/- interaction with equivalences -/
section
open hott.is_equiv hott.equiv
@[hott] def is_contr_is_equiv_closed (f : A → B) [Hf : is_equiv f] [HA: is_contr A]
: (is_contr B) :=
is_contr.mk (f (center A)) (λp, eq_of_eq_inv (center_eq _))
@[hott] def is_contr_equiv_closed (H : A ≃ B) [HA: is_contr A] : is_contr B :=
is_contr_is_equiv_closed H
@[hott] def equiv_of_is_contr_of_is_contr [HA : is_contr A] [HB : is_contr B] : A ≃ B :=
equiv.mk
(λa, center B)
(is_equiv.adjointify (λa, center B) (λb, center A) center_eq center_eq)
@[hott] def is_trunc_is_equiv_closed (n : ℕ₋₂) (f : A → B) [H : is_equiv f]
(HA : is_trunc n A) : is_trunc n B :=
begin
unfreezeI, revert A B f H HA, induction n with n IH; intros,
{ exactI is_contr_is_equiv_closed f },
{ apply is_trunc_succ_intro, intros, apply IH (ap f⁻¹ᶠ)⁻¹ᶠ, all_goals {apply_instance} }
end
@[hott] def is_trunc_is_equiv_closed_rev (n : ℕ₋₂) (f : A → B) [H : is_equiv f]
(HA : is_trunc n B) : is_trunc n A :=
is_trunc_is_equiv_closed n f⁻¹ᶠ HA
@[hott] def is_trunc_equiv_closed (n : ℕ₋₂) (f : A ≃ B) (HA : is_trunc n A)
: is_trunc n B :=
is_trunc_is_equiv_closed n f HA
@[hott] def is_trunc_equiv_closed_rev (n : ℕ₋₂) (f : A ≃ B) (HA : is_trunc n B)
: is_trunc n A :=
is_trunc_is_equiv_closed n f⁻¹ᶠ HA
@[hott] def is_equiv_of_is_prop [HA : is_prop A] [HB : is_prop B]
(f : A → B) (g : B → A) : is_equiv f :=
by fapply is_equiv.mk f g; intros; {apply is_prop.elim <|> apply is_set.elim}
@[hott] def is_equiv_of_is_contr [HA : is_contr A] [HB : is_contr B]
(f : A → B) : is_equiv f :=
by fapply is_equiv.mk f; intros; {apply center <|> apply is_prop.elim <|> apply is_set.elim}
@[hott] def equiv_of_is_prop [HA : is_prop A] [HB : is_prop B]
(f : A → B) (g : B → A) : A ≃ B :=
equiv.mk f (is_equiv_of_is_prop f g)
@[hott] def equiv_of_iff_of_is_prop [HA : is_prop A] [HB : is_prop B] (H : A ↔ B) : A ≃ B :=
equiv_of_is_prop (iff.elim_left H) (iff.elim_right H)
/- truncatedness of lift -/
@[hott,instance] def is_trunc_lift (A : Type _) (n : ℕ₋₂)
[H : is_trunc n A] : is_trunc n (ulift A) :=
is_trunc_equiv_closed _ (equiv_lift _) H
end
/- interaction with the Unit type -/
open equiv
/- A contractible type is equivalent to unit. -/
variable (A)
@[hott] def equiv_unit_of_is_contr [H : is_contr A] : A ≃ unit :=
equiv.MK (λ (x : A), ())
(λ (u : unit), center A)
(λ (u : unit), punit.rec_on u idp)
(λ (x : A), center_eq x)
/- interaction with pathovers -/
variable {A}
variables {C : A → Type _}
{a a₂ : A} (p : a = a₂)
(c : C a) (c₂ : C a₂)
instance is_trunc_pathover
(n : ℕ₋₂) [H : is_trunc (n.+1) (C a)] : is_trunc n (c =[p] c₂) :=
begin
refine @is_trunc_equiv_closed_rev _ _ n _ _, tactic.swap,
apply pathover_equiv_eq_tr,
apply is_trunc_eq,
end
@[hott] def is_prop.elimo [H : is_prop (C a)] : c =[p] c₂ :=
pathover_of_eq_tr (by apply is_prop.elim)
@[hott] def is_prop.elimo' (H : is_prop (C a)) : c =[p] c₂ :=
is_prop.elimo p c c₂
@[hott] def is_prop_elimo_self {A : Type _} (B : A → Type _) {a : A} (b : B a) {H : is_prop (B a)} :
@is_prop.elimo A B a a idp b b H = idpo :=
by apply is_prop.elim
variables {p c c₂}
@[hott] def is_set.elimo (q q' : c =[p] c₂) [H : is_set (C a)] : q = q' :=
by apply is_prop.elim
-- TODO: port "Truncated morphisms"
/- truncated universe -/
end is_trunc
structure trunctype (n : ℕ₋₂) :=
(carrier : Type u)
(struct : is_trunc n carrier)
@[hott] instance (n): has_coe_to_sort (trunctype n) := {
S := Type u,
coe := trunctype.carrier
}
notation n `-Type` := trunctype n
hott_theory_cmd "local notation Prop := -1-Type"
notation `Set` := 0-Type
-- attribute trunctype.carrier [coercion]
attribute [instance] trunctype.struct
notation `Prop.mk` := @trunctype.mk -1
notation `Set.mk` := @trunctype.mk (-1.+1)
@[hott] protected def trunctype.mk' (n : ℕ₋₂) (A : Type _) [H : is_trunc n A]
: n-Type :=
trunctype.mk A H
@[hott, hsimp] def coe_trunctype_mk {n : ℕ₋₂} (A : Type _) (H : is_trunc n A) :
↥(trunctype.mk A H) = A :=
by refl
@[hott, hsimp] def coe_trunctype_mk' {n : ℕ₋₂} (A : Type _) (H : is_trunc n A) :
↥(trunctype.mk' n A) = A :=
by refl
namespace is_trunc
@[hott] def tlift {n : ℕ₋₂} (A : trunctype.{u} n)
: trunctype.{max u v} n :=
trunctype.mk (ulift A) (is_trunc_lift _ _)
end is_trunc
end hott
|
7485c5e49315de78dad0046aa3b78bc49740946f | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/order/pfilter.lean | faf1be073cbc3fe383619208af35be3d12cc13de | [
"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 | 3,304 | lean | /-
Copyright (c) 2020 Mathieu Guay-Paquet. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mathieu Guay-Paquet
-/
import order.ideal
/-!
# Order filters
## Main definitions
Throughout this file, `P` is at least a preorder, but some sections
require more structure, such as a bottom element, a top element, or
a join-semilattice structure.
- `pfilter P`: The type of nonempty, downward directed, upward closed
subsets of `P`. This is dual to `order.ideal`, so it
simply wraps `order.ideal (order_dual P)`.
Note the relation between `order/filter` and `order/pfilter`: for any
type `α`, `filter α` represents the same mathematical object as
`pfilter (set α)`.
## References
- https://en.wikipedia.org/wiki/Filter_(mathematics)
## Tags
pfilter, filter, ideal, dual
-/
variables {P : Type*}
/-- A filter on a preorder `P` is a subset of `P` that is
- nonempty
- downward directed
- upward closed. -/
structure pfilter (P) [preorder P] :=
(dual : order.ideal (order_dual P))
namespace pfilter
section preorder
variables [preorder P] {x y : P} {F G : pfilter P}
/-- A filter on `P` is a subset of `P`. -/
instance : has_coe (pfilter P) (set P) := ⟨λ F, F.dual.carrier⟩
/-- For the notation `x ∈ F`. -/
instance : has_mem P (pfilter P) := ⟨λ x F, x ∈ (F : set P)⟩
lemma nonempty : (F : set P).nonempty := F.dual.nonempty
lemma directed : directed_on (≥) (F : set P) := F.dual.directed
lemma mem_of_le : x ≤ y → x ∈ F → y ∈ F := λ h, F.dual.mem_of_le h
/-- The smallest filter containing a given element. -/
def principal (p : P) : pfilter P := ⟨order.ideal.principal p⟩
instance [inhabited P] : inhabited (pfilter P) := ⟨⟨default _⟩⟩
/-- Two filters are equal when their underlying sets are equal. -/
@[ext] lemma ext : ∀ (F G : pfilter P), (F : set P) = G → F = G
| ⟨⟨_, _, _, _⟩⟩ ⟨⟨_, _, _, _⟩⟩ rfl := rfl
/-- The partial ordering by subset inclusion, inherited from `set P`. -/
instance : partial_order (pfilter P) := partial_order.lift coe ext
@[trans] lemma mem_of_mem_of_le : x ∈ F → F ≤ G → x ∈ G :=
order.ideal.mem_of_mem_of_le
@[simp] lemma principal_le_iff : principal x ≤ F ↔ x ∈ F :=
order.ideal.principal_le_iff
end preorder
section order_top
variables [order_top P] {F : pfilter P}
/-- A specific witness of `pfilter.nonempty` when `P` has a top element. -/
@[simp] lemma top_mem : ⊤ ∈ F :=
order.ideal.bot_mem
/-- There is a bottom filter when `P` has a top element. -/
instance : order_bot (pfilter P) :=
{ bot := ⟨⊥⟩,
bot_le := λ F, (bot_le : ⊥ ≤ F.dual),
.. pfilter.partial_order }
end order_top
/-- There is a top filter when `P` has a bottom element. -/
instance {P} [order_bot P] : order_top (pfilter P) :=
{ top := ⟨⊤⟩,
le_top := λ F, (le_top : F.dual ≤ ⊤),
.. pfilter.partial_order }
section semilattice_inf
variables [semilattice_inf P] {x y : P} {F : pfilter P}
/-- A specific witness of `pfilter.directed` when `P` has meets. -/
lemma inf_mem (x y ∈ F) : x ⊓ y ∈ F :=
order.ideal.sup_mem x y ‹x ∈ F› ‹y ∈ F›
@[simp] lemma inf_mem_iff : x ⊓ y ∈ F ↔ x ∈ F ∧ y ∈ F :=
order.ideal.sup_mem_iff
end semilattice_inf
end pfilter
|
9a9a20d8c303368139a953256a40d3f0d1d5bc43 | 41ebf3cb010344adfa84907b3304db00e02db0a6 | /uexp/src/uexp/rules/ex2sigmod92.lean | 7e2cd5b78e339ed51e8f29a1b681da4d56f94977 | [
"BSD-2-Clause"
] | permissive | ReinierKoops/Cosette | e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb | eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29 | refs/heads/master | 1,686,483,953,198 | 1,624,293,498,000 | 1,624,293,498,000 | 378,997,885 | 0 | 0 | BSD-2-Clause | 1,624,293,485,000 | 1,624,293,484,000 | null | UTF-8 | Lean | false | false | 2,147 | lean | import ..sql
import ..tactics
import ..u_semiring
import ..extra_constants
import ..meta.UDP ..meta.canonize
import ..meta.cosette_tactics
set_option profiler true
open Expr
open Proj
open Pred
open SQL
variable i1000 : const datatypes.int
theorem rule :
forall (Γ scm_itm scm_itp : Schema)
(rel_itm : relation scm_itm)
(rel_itp : relation scm_itp)
(itm_itemno : Column datatypes.int scm_itm)
(itm_type : Column datatypes.int scm_itm)
(itp_itemno : Column datatypes.int scm_itp)
(itp_np : Column datatypes.int scm_itp)
(ik : isKey itm_itemno rel_itm),
denoteSQL
(SELECT1 (combine (right⋅left⋅right)
(combine (right⋅right⋅itm_type)
(right⋅right⋅itm_itemno)))
FROM1 (product (DISTINCT (SELECT1 (combine (right⋅itp_itemno)
(right⋅itp_np))
FROM1 (table rel_itp)
WHERE (castPred (combine (right⋅itp_np)
(e2p (constantExpr i1000)))
predicates.gt)))
(table rel_itm))
WHERE (equal (uvariable (right⋅left⋅left))
(uvariable (right⋅right⋅itm_itemno))) : SQL Γ _) =
denoteSQL
(DISTINCT (SELECT1 (combine (right⋅left⋅itp_np)
(combine (right⋅right⋅itm_type)
(right⋅right⋅itm_itemno)))
FROM1 (product (table rel_itp)
(table rel_itm))
WHERE (and (castPred (combine (right⋅left⋅itp_np)
(e2p (constantExpr i1000)))
predicates.gt)
(equal (uvariable (right⋅left⋅itp_itemno))
(uvariable (right⋅right⋅itm_itemno))))) : SQL Γ _) :=
begin
intros,
unfold_all_denotations,
funext,
simp,
canonize,
sorry
end |
e28c7336e08c73f7ffe406720f30bc41bd822935 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Init/Control/Except.lean | 3e1efa5ed77b88af14a0f6c03d5befe5b0a456e2 | [
"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 | 7,230 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jared Roesch, Sebastian Ullrich
The Except monad transformer.
-/
prelude
import Init.Control.Basic
import Init.Control.Id
import Init.Coe
namespace Except
variable {ε : Type u}
@[always_inline, inline]
protected def pure (a : α) : Except ε α :=
Except.ok a
@[always_inline, inline]
protected def map (f : α → β) : Except ε α → Except ε β
| Except.error err => Except.error err
| Except.ok v => Except.ok <| f v
@[simp] theorem map_id : Except.map (ε := ε) (α := α) (β := α) id = id := by
apply funext
intro e
simp [Except.map]; cases e <;> rfl
@[always_inline, inline]
protected def mapError (f : ε → ε') : Except ε α → Except ε' α
| Except.error err => Except.error <| f err
| Except.ok v => Except.ok v
@[always_inline, inline]
protected def bind (ma : Except ε α) (f : α → Except ε β) : Except ε β :=
match ma with
| Except.error err => Except.error err
| Except.ok v => f v
/-- Returns true if the value is `Except.ok`, false otherwise. -/
@[always_inline, inline]
protected def toBool : Except ε α → Bool
| Except.ok _ => true
| Except.error _ => false
abbrev isOk : Except ε α → Bool := Except.toBool
@[always_inline, inline]
protected def toOption : Except ε α → Option α
| Except.ok a => some a
| Except.error _ => none
@[always_inline, inline]
protected def tryCatch (ma : Except ε α) (handle : ε → Except ε α) : Except ε α :=
match ma with
| Except.ok a => Except.ok a
| Except.error e => handle e
def orElseLazy (x : Except ε α) (y : Unit → Except ε α) : Except ε α :=
match x with
| Except.ok a => Except.ok a
| Except.error _ => y ()
@[always_inline]
instance : Monad (Except ε) where
pure := Except.pure
bind := Except.bind
map := Except.map
end Except
def ExceptT (ε : Type u) (m : Type u → Type v) (α : Type u) : Type v :=
m (Except ε α)
@[always_inline, inline]
def ExceptT.mk {ε : Type u} {m : Type u → Type v} {α : Type u} (x : m (Except ε α)) : ExceptT ε m α := x
@[always_inline, inline]
def ExceptT.run {ε : Type u} {m : Type u → Type v} {α : Type u} (x : ExceptT ε m α) : m (Except ε α) := x
namespace ExceptT
variable {ε : Type u} {m : Type u → Type v} [Monad m]
@[always_inline, inline]
protected def pure {α : Type u} (a : α) : ExceptT ε m α :=
ExceptT.mk <| pure (Except.ok a)
@[always_inline, inline]
protected def bindCont {α β : Type u} (f : α → ExceptT ε m β) : Except ε α → m (Except ε β)
| Except.ok a => f a
| Except.error e => pure (Except.error e)
@[always_inline, inline]
protected def bind {α β : Type u} (ma : ExceptT ε m α) (f : α → ExceptT ε m β) : ExceptT ε m β :=
ExceptT.mk <| ma >>= ExceptT.bindCont f
@[always_inline, inline]
protected def map {α β : Type u} (f : α → β) (x : ExceptT ε m α) : ExceptT ε m β :=
ExceptT.mk <| x >>= fun a => match a with
| (Except.ok a) => pure <| Except.ok (f a)
| (Except.error e) => pure <| Except.error e
@[always_inline, inline]
protected def lift {α : Type u} (t : m α) : ExceptT ε m α :=
ExceptT.mk <| Except.ok <$> t
@[always_inline]
instance : MonadLift (Except ε) (ExceptT ε m) := ⟨fun e => ExceptT.mk <| pure e⟩
instance : MonadLift m (ExceptT ε m) := ⟨ExceptT.lift⟩
@[always_inline, inline]
protected def tryCatch {α : Type u} (ma : ExceptT ε m α) (handle : ε → ExceptT ε m α) : ExceptT ε m α :=
ExceptT.mk <| ma >>= fun res => match res with
| Except.ok a => pure (Except.ok a)
| Except.error e => (handle e)
instance : MonadFunctor m (ExceptT ε m) := ⟨fun f x => f x⟩
@[always_inline]
instance : Monad (ExceptT ε m) where
pure := ExceptT.pure
bind := ExceptT.bind
map := ExceptT.map
@[always_inline, inline]
protected def adapt {ε' α : Type u} (f : ε → ε') : ExceptT ε m α → ExceptT ε' m α := fun x =>
ExceptT.mk <| Except.mapError f <$> x
end ExceptT
@[always_inline]
instance (m : Type u → Type v) (ε₁ : Type u) (ε₂ : Type u) [Monad m] [MonadExceptOf ε₁ m] : MonadExceptOf ε₁ (ExceptT ε₂ m) where
throw e := ExceptT.mk <| throwThe ε₁ e
tryCatch x handle := ExceptT.mk <| tryCatchThe ε₁ x handle
@[always_inline]
instance (m : Type u → Type v) (ε : Type u) [Monad m] : MonadExceptOf ε (ExceptT ε m) where
throw e := ExceptT.mk <| pure (Except.error e)
tryCatch := ExceptT.tryCatch
instance [Monad m] [Inhabited ε] : Inhabited (ExceptT ε m α) where
default := throw default
instance (ε) : MonadExceptOf ε (Except ε) where
throw := Except.error
tryCatch := Except.tryCatch
namespace MonadExcept
variable {ε : Type u} {m : Type v → Type w}
/-- Alternative orelse operator that allows to select which exception should be used.
The default is to use the first exception since the standard `orelse` uses the second. -/
@[always_inline, inline]
def orelse' [MonadExcept ε m] {α : Type v} (t₁ t₂ : m α) (useFirstEx := true) : m α :=
tryCatch t₁ fun e₁ => tryCatch t₂ fun e₂ => throw (if useFirstEx then e₁ else e₂)
end MonadExcept
@[always_inline, inline]
def observing {ε α : Type u} {m : Type u → Type v} [Monad m] [MonadExcept ε m] (x : m α) : m (Except ε α) :=
tryCatch (do let a ← x; pure (Except.ok a)) (fun ex => pure (Except.error ex))
def liftExcept [MonadExceptOf ε m] [Pure m] : Except ε α → m α
| Except.ok a => pure a
| Except.error e => throw e
instance (ε : Type u) (m : Type u → Type v) [Monad m] : MonadControl m (ExceptT ε m) where
stM := Except ε
liftWith f := liftM <| f fun x => x.run
restoreM x := x
class MonadFinally (m : Type u → Type v) where
/-- `tryFinally' x f` runs `x` and then the "finally" computation `f`.
When `x` succeeds with `a : α`, `f (some a)` is returned. If `x` fails
for `m`'s definition of failure, `f none` is returned. Hence `tryFinally'`
can be thought of as performing the same role as a `finally` block in
an imperative programming language. -/
tryFinally' {α β} : m α → (Option α → m β) → m (α × β)
export MonadFinally (tryFinally')
/-- Execute `x` and then execute `finalizer` even if `x` threw an exception -/
@[always_inline, inline]
def tryFinally {m : Type u → Type v} {α β : Type u} [MonadFinally m] [Functor m] (x : m α) (finalizer : m β) : m α :=
let y := tryFinally' x (fun _ => finalizer)
(·.1) <$> y
@[always_inline]
instance Id.finally : MonadFinally Id where
tryFinally' := fun x h =>
let a := x
let b := h (some x)
pure (a, b)
@[always_inline]
instance ExceptT.finally {m : Type u → Type v} {ε : Type u} [MonadFinally m] [Monad m] : MonadFinally (ExceptT ε m) where
tryFinally' := fun x h => ExceptT.mk do
let r ← tryFinally' x fun e? => match e? with
| some (.ok a) => h (some a)
| _ => h none
match r with
| (.ok a, .ok b) => pure (.ok (a, b))
| (_, .error e) => pure (.error e) -- second error has precedence
| (.error e, _) => pure (.error e)
|
9cadb65bf1caabdc360c35b0ff3c581a2f555d46 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/idempotents/biproducts.lean | c91026bedf70b830e78abc6123f53fa5d11d35fd | [
"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,033 | lean | /-
Copyright (c) 2022 Joël Riou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joël Riou
-/
import category_theory.idempotents.karoubi
import category_theory.additive.basic
/-!
# Biproducts in the idempotent completion of a preadditive category
In this file, we define an instance expressing that if `C` is an additive category,
then `karoubi C` is also an additive category.
We also obtain that for all `P : karoubi C` where `C` is a preadditive category `C`, there
is a canonical isomorphism `P ⊞ P.complement ≅ (to_karoubi C).obj P.X` in the category
`karoubi C` where `P.complement` is the formal direct factor of `P.X` corresponding to
the idempotent endomorphism `𝟙 P.X - P.p`.
-/
noncomputable theory
open category_theory.category
open category_theory.limits
open category_theory.preadditive
universes v
namespace category_theory
namespace idempotents
namespace karoubi
variables {C : Type*} [category.{v} C] [preadditive C]
namespace biproducts
/-- The `bicone` used in order to obtain the existence of
the biproduct of a functor `J ⥤ karoubi C` when the category `C` is additive. -/
@[simps]
def bicone [has_finite_biproducts C] {J : Type} [fintype J]
(F : J → karoubi C) : bicone F :=
{ X :=
{ X := biproduct (λ j, (F j).X),
p := biproduct.map (λ j, (F j).p),
idem := begin
ext j,
simp only [biproduct.ι_map_assoc, biproduct.ι_map],
slice_lhs 1 2 { rw (F j).idem, },
end, },
π := λ j,
{ f := biproduct.map (λ j, (F j).p) ≫ bicone.π _ j,
comm := by simp only [assoc, biproduct.bicone_π, biproduct.map_π,
biproduct.map_π_assoc, (F j).idem], },
ι := λ j,
{ f := (by exact bicone.ι _ j) ≫ biproduct.map (λ j, (F j).p),
comm := by rw [biproduct.ι_map, ← assoc, ← assoc, (F j).idem,
assoc, biproduct.ι_map, ← assoc, (F j).idem], },
ι_π := λ j j', begin
split_ifs,
{ subst h,
simp only [assoc, idem, biproduct.map_π, biproduct.map_π_assoc, eq_to_hom_refl,
id_eq, hom_ext, comp_f, biproduct.ι_π_self_assoc], },
{ simp only [biproduct.ι_π_ne_assoc _ h, assoc, biproduct.map_π,
biproduct.map_π_assoc, hom_ext, comp_f, zero_comp, quiver.hom.add_comm_group_zero_f], },
end, }
end biproducts
lemma karoubi_has_finite_biproducts [has_finite_biproducts C] :
has_finite_biproducts (karoubi C) :=
{ out := λ n,
{ has_biproduct := λ F, begin
classical,
apply has_biproduct_of_total (biproducts.bicone F),
ext1, ext1,
simp only [id_eq, comp_id, biproducts.bicone_X_p, biproduct.ι_map],
rw [sum_hom, comp_sum, finset.sum_eq_single j], rotate,
{ intros j' h1 h2,
simp only [biproduct.ι_map, biproducts.bicone_ι_f, biproducts.bicone_π_f,
assoc, comp_f, biproduct.map_π],
slice_lhs 1 2 { rw biproduct.ι_π, },
split_ifs,
{ exfalso, exact h2 h.symm, },
{ simp only [zero_comp], } },
{ intro h,
exfalso,
simpa only [finset.mem_univ, not_true] using h, },
{ simp only [biproducts.bicone_π_f, comp_f,
biproduct.ι_map, assoc, biproducts.bicone_ι_f, biproduct.map_π],
slice_lhs 1 2 { rw biproduct.ι_π, },
split_ifs, swap, { exfalso, exact h rfl, },
simp only [eq_to_hom_refl, id_comp, (F j).idem], },
end, } }
instance {D : Type*} [category D] [additive_category D] : additive_category (karoubi D) :=
{ to_preadditive := infer_instance,
to_has_finite_biproducts := karoubi_has_finite_biproducts }
/-- `P.complement` is the formal direct factor of `P.X` given by the idempotent
endomorphism `𝟙 P.X - P.p` -/
@[simps]
def complement (P : karoubi C) : karoubi C :=
{ X := P.X,
p := 𝟙 _ - P.p,
idem := idem_of_id_sub_idem P.p P.idem, }
instance (P : karoubi C) : has_binary_biproduct P P.complement :=
has_binary_biproduct_of_total
{ X := P.X,
fst := P.decomp_id_p,
snd := P.complement.decomp_id_p,
inl := P.decomp_id_i,
inr := P.complement.decomp_id_i,
inl_fst' := P.decomp_id.symm,
inl_snd' := begin
simp only [decomp_id_i_f, decomp_id_p_f, complement_p, comp_sub, comp_f,
hom_ext, quiver.hom.add_comm_group_zero_f, P.idem],
erw [comp_id, sub_self],
end,
inr_fst' := begin
simp only [decomp_id_i_f, complement_p, decomp_id_p_f, sub_comp, comp_f,
hom_ext, quiver.hom.add_comm_group_zero_f, P.idem],
erw [id_comp, sub_self],
end,
inr_snd' := P.complement.decomp_id.symm, }
(by simp only [hom_ext, ← decomp_p, quiver.hom.add_comm_group_add_f,
to_karoubi_map_f, id_eq, coe_p, complement_p, add_sub_cancel'_right])
/-- A formal direct factor `P : karoubi C` of an object `P.X : C` in a
preadditive category is actually a direct factor of the image `(to_karoubi C).obj P.X`
of `P.X` in the category `karoubi C` -/
def decomposition (P : karoubi C) : P ⊞ P.complement ≅ (to_karoubi _).obj P.X :=
{ hom := biprod.desc P.decomp_id_i P.complement.decomp_id_i,
inv := biprod.lift P.decomp_id_p P.complement.decomp_id_p,
hom_inv_id' := begin
ext1,
{ simp only [← assoc, biprod.inl_desc, comp_id, biprod.lift_eq, comp_add,
← decomp_id, id_comp, add_right_eq_self],
convert zero_comp,
ext,
simp only [decomp_id_i_f, decomp_id_p_f, complement_p, comp_sub, comp_f,
quiver.hom.add_comm_group_zero_f, P.idem],
erw [comp_id, sub_self], },
{ simp only [← assoc, biprod.inr_desc, biprod.lift_eq, comp_add,
← decomp_id, comp_id, id_comp, add_left_eq_self],
convert zero_comp,
ext,
simp only [decomp_id_i_f, decomp_id_p_f, complement_p, sub_comp, comp_f,
quiver.hom.add_comm_group_zero_f, P.idem],
erw [id_comp, sub_self], }
end,
inv_hom_id' := begin
rw biprod.lift_desc,
simp only [← decomp_p],
ext,
dsimp only [complement, to_karoubi],
simp only [quiver.hom.add_comm_group_add_f, add_sub_cancel'_right, id_eq],
end, }
end karoubi
end idempotents
end category_theory
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.